handshake.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  1. /*
  2. * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/bio.h>
  11. #include <openssl/x509_vfy.h>
  12. #include <openssl/ssl.h>
  13. #include <openssl/core_names.h>
  14. #include "../../ssl/ssl_local.h"
  15. #include "internal/sockets.h"
  16. #include "internal/nelem.h"
  17. #include "handshake.h"
  18. #include "../testutil.h"
  19. #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK)
  20. #include <netinet/sctp.h>
  21. #endif
  22. HANDSHAKE_RESULT *HANDSHAKE_RESULT_new(void)
  23. {
  24. HANDSHAKE_RESULT *ret;
  25. TEST_ptr(ret = OPENSSL_zalloc(sizeof(*ret)));
  26. return ret;
  27. }
  28. void HANDSHAKE_RESULT_free(HANDSHAKE_RESULT *result)
  29. {
  30. if (result == NULL)
  31. return;
  32. OPENSSL_free(result->client_npn_negotiated);
  33. OPENSSL_free(result->server_npn_negotiated);
  34. OPENSSL_free(result->client_alpn_negotiated);
  35. OPENSSL_free(result->server_alpn_negotiated);
  36. OPENSSL_free(result->result_session_ticket_app_data);
  37. sk_X509_NAME_pop_free(result->server_ca_names, X509_NAME_free);
  38. sk_X509_NAME_pop_free(result->client_ca_names, X509_NAME_free);
  39. OPENSSL_free(result->cipher);
  40. OPENSSL_free(result);
  41. }
  42. /*
  43. * Since there appears to be no way to extract the sent/received alert
  44. * from the SSL object directly, we use the info callback and stash
  45. * the result in ex_data.
  46. */
  47. typedef struct handshake_ex_data_st {
  48. int alert_sent;
  49. int num_fatal_alerts_sent;
  50. int alert_received;
  51. int session_ticket_do_not_call;
  52. ssl_servername_t servername;
  53. } HANDSHAKE_EX_DATA;
  54. /* |ctx_data| itself is stack-allocated. */
  55. static void ctx_data_free_data(CTX_DATA *ctx_data)
  56. {
  57. OPENSSL_free(ctx_data->npn_protocols);
  58. ctx_data->npn_protocols = NULL;
  59. OPENSSL_free(ctx_data->alpn_protocols);
  60. ctx_data->alpn_protocols = NULL;
  61. OPENSSL_free(ctx_data->srp_user);
  62. ctx_data->srp_user = NULL;
  63. OPENSSL_free(ctx_data->srp_password);
  64. ctx_data->srp_password = NULL;
  65. OPENSSL_free(ctx_data->session_ticket_app_data);
  66. ctx_data->session_ticket_app_data = NULL;
  67. }
  68. static int ex_data_idx;
  69. static void info_cb(const SSL *s, int where, int ret)
  70. {
  71. if (where & SSL_CB_ALERT) {
  72. HANDSHAKE_EX_DATA *ex_data =
  73. (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));
  74. if (where & SSL_CB_WRITE) {
  75. ex_data->alert_sent = ret;
  76. if (strcmp(SSL_alert_type_string(ret), "F") == 0
  77. || strcmp(SSL_alert_desc_string(ret), "CN") == 0)
  78. ex_data->num_fatal_alerts_sent++;
  79. } else {
  80. ex_data->alert_received = ret;
  81. }
  82. }
  83. }
  84. /* Select the appropriate server CTX.
  85. * Returns SSL_TLSEXT_ERR_OK if a match was found.
  86. * If |ignore| is 1, returns SSL_TLSEXT_ERR_NOACK on mismatch.
  87. * Otherwise, returns SSL_TLSEXT_ERR_ALERT_FATAL on mismatch.
  88. * An empty SNI extension also returns SSL_TSLEXT_ERR_NOACK.
  89. */
  90. static int select_server_ctx(SSL *s, void *arg, int ignore)
  91. {
  92. const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  93. HANDSHAKE_EX_DATA *ex_data =
  94. (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));
  95. if (servername == NULL) {
  96. ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;
  97. return SSL_TLSEXT_ERR_NOACK;
  98. }
  99. if (strcmp(servername, "server2") == 0) {
  100. SSL_CTX *new_ctx = (SSL_CTX*)arg;
  101. SSL_set_SSL_CTX(s, new_ctx);
  102. /*
  103. * Copy over all the SSL_CTX options - reasonable behavior
  104. * allows testing of cases where the options between two
  105. * contexts differ/conflict
  106. */
  107. SSL_clear_options(s, 0xFFFFFFFFL);
  108. SSL_set_options(s, SSL_CTX_get_options(new_ctx));
  109. ex_data->servername = SSL_TEST_SERVERNAME_SERVER2;
  110. return SSL_TLSEXT_ERR_OK;
  111. } else if (strcmp(servername, "server1") == 0) {
  112. ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;
  113. return SSL_TLSEXT_ERR_OK;
  114. } else if (ignore) {
  115. ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;
  116. return SSL_TLSEXT_ERR_NOACK;
  117. } else {
  118. /* Don't set an explicit alert, to test library defaults. */
  119. return SSL_TLSEXT_ERR_ALERT_FATAL;
  120. }
  121. }
  122. static int client_hello_select_server_ctx(SSL *s, void *arg, int ignore)
  123. {
  124. const char *servername;
  125. const unsigned char *p;
  126. size_t len, remaining;
  127. HANDSHAKE_EX_DATA *ex_data =
  128. (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));
  129. /*
  130. * The server_name extension was given too much extensibility when it
  131. * was written, so parsing the normal case is a bit complex.
  132. */
  133. if (!SSL_client_hello_get0_ext(s, TLSEXT_TYPE_server_name, &p,
  134. &remaining) ||
  135. remaining <= 2)
  136. return 0;
  137. /* Extract the length of the supplied list of names. */
  138. len = (*(p++) << 8);
  139. len += *(p++);
  140. if (len + 2 != remaining)
  141. return 0;
  142. remaining = len;
  143. /*
  144. * The list in practice only has a single element, so we only consider
  145. * the first one.
  146. */
  147. if (remaining == 0 || *p++ != TLSEXT_NAMETYPE_host_name)
  148. return 0;
  149. remaining--;
  150. /* Now we can finally pull out the byte array with the actual hostname. */
  151. if (remaining <= 2)
  152. return 0;
  153. len = (*(p++) << 8);
  154. len += *(p++);
  155. if (len + 2 > remaining)
  156. return 0;
  157. remaining = len;
  158. servername = (const char *)p;
  159. if (len == strlen("server2") && strncmp(servername, "server2", len) == 0) {
  160. SSL_CTX *new_ctx = arg;
  161. SSL_set_SSL_CTX(s, new_ctx);
  162. /*
  163. * Copy over all the SSL_CTX options - reasonable behavior
  164. * allows testing of cases where the options between two
  165. * contexts differ/conflict
  166. */
  167. SSL_clear_options(s, 0xFFFFFFFFL);
  168. SSL_set_options(s, SSL_CTX_get_options(new_ctx));
  169. ex_data->servername = SSL_TEST_SERVERNAME_SERVER2;
  170. return 1;
  171. } else if (len == strlen("server1") &&
  172. strncmp(servername, "server1", len) == 0) {
  173. ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;
  174. return 1;
  175. } else if (ignore) {
  176. ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;
  177. return 1;
  178. }
  179. return 0;
  180. }
  181. /*
  182. * (RFC 6066):
  183. * If the server understood the ClientHello extension but
  184. * does not recognize the server name, the server SHOULD take one of two
  185. * actions: either abort the handshake by sending a fatal-level
  186. * unrecognized_name(112) alert or continue the handshake.
  187. *
  188. * This behaviour is up to the application to configure; we test both
  189. * configurations to ensure the state machine propagates the result
  190. * correctly.
  191. */
  192. static int servername_ignore_cb(SSL *s, int *ad, void *arg)
  193. {
  194. return select_server_ctx(s, arg, 1);
  195. }
  196. static int servername_reject_cb(SSL *s, int *ad, void *arg)
  197. {
  198. return select_server_ctx(s, arg, 0);
  199. }
  200. static int client_hello_ignore_cb(SSL *s, int *al, void *arg)
  201. {
  202. if (!client_hello_select_server_ctx(s, arg, 1)) {
  203. *al = SSL_AD_UNRECOGNIZED_NAME;
  204. return SSL_CLIENT_HELLO_ERROR;
  205. }
  206. return SSL_CLIENT_HELLO_SUCCESS;
  207. }
  208. static int client_hello_reject_cb(SSL *s, int *al, void *arg)
  209. {
  210. if (!client_hello_select_server_ctx(s, arg, 0)) {
  211. *al = SSL_AD_UNRECOGNIZED_NAME;
  212. return SSL_CLIENT_HELLO_ERROR;
  213. }
  214. return SSL_CLIENT_HELLO_SUCCESS;
  215. }
  216. static int client_hello_nov12_cb(SSL *s, int *al, void *arg)
  217. {
  218. int ret;
  219. unsigned int v;
  220. const unsigned char *p;
  221. v = SSL_client_hello_get0_legacy_version(s);
  222. if (v > TLS1_2_VERSION || v < SSL3_VERSION) {
  223. *al = SSL_AD_PROTOCOL_VERSION;
  224. return SSL_CLIENT_HELLO_ERROR;
  225. }
  226. (void)SSL_client_hello_get0_session_id(s, &p);
  227. if (p == NULL ||
  228. SSL_client_hello_get0_random(s, &p) == 0 ||
  229. SSL_client_hello_get0_ciphers(s, &p) == 0 ||
  230. SSL_client_hello_get0_compression_methods(s, &p) == 0) {
  231. *al = SSL_AD_INTERNAL_ERROR;
  232. return SSL_CLIENT_HELLO_ERROR;
  233. }
  234. ret = client_hello_select_server_ctx(s, arg, 0);
  235. SSL_set_max_proto_version(s, TLS1_1_VERSION);
  236. if (!ret) {
  237. *al = SSL_AD_UNRECOGNIZED_NAME;
  238. return SSL_CLIENT_HELLO_ERROR;
  239. }
  240. return SSL_CLIENT_HELLO_SUCCESS;
  241. }
  242. static unsigned char dummy_ocsp_resp_good_val = 0xff;
  243. static unsigned char dummy_ocsp_resp_bad_val = 0xfe;
  244. static int server_ocsp_cb(SSL *s, void *arg)
  245. {
  246. unsigned char *resp;
  247. resp = OPENSSL_malloc(1);
  248. if (resp == NULL)
  249. return SSL_TLSEXT_ERR_ALERT_FATAL;
  250. /*
  251. * For the purposes of testing we just send back a dummy OCSP response
  252. */
  253. *resp = *(unsigned char *)arg;
  254. if (!SSL_set_tlsext_status_ocsp_resp(s, resp, 1))
  255. return SSL_TLSEXT_ERR_ALERT_FATAL;
  256. return SSL_TLSEXT_ERR_OK;
  257. }
  258. static int client_ocsp_cb(SSL *s, void *arg)
  259. {
  260. const unsigned char *resp;
  261. int len;
  262. len = SSL_get_tlsext_status_ocsp_resp(s, &resp);
  263. if (len != 1 || *resp != dummy_ocsp_resp_good_val)
  264. return 0;
  265. return 1;
  266. }
  267. static int verify_reject_cb(X509_STORE_CTX *ctx, void *arg) {
  268. X509_STORE_CTX_set_error(ctx, X509_V_ERR_APPLICATION_VERIFICATION);
  269. return 0;
  270. }
  271. static int n_retries = 0;
  272. static int verify_retry_cb(X509_STORE_CTX *ctx, void *arg) {
  273. if (--n_retries < 0)
  274. return 1;
  275. X509_STORE_CTX_set_error(ctx, X509_V_ERR_APPLICATION_VERIFICATION);
  276. return -1;
  277. }
  278. static int verify_accept_cb(X509_STORE_CTX *ctx, void *arg) {
  279. return 1;
  280. }
  281. static int broken_session_ticket_cb(SSL *s, unsigned char *key_name,
  282. unsigned char *iv, EVP_CIPHER_CTX *ctx,
  283. EVP_MAC_CTX *hctx, int enc)
  284. {
  285. return 0;
  286. }
  287. static int do_not_call_session_ticket_cb(SSL *s, unsigned char *key_name,
  288. unsigned char *iv,
  289. EVP_CIPHER_CTX *ctx,
  290. EVP_MAC_CTX *hctx, int enc)
  291. {
  292. HANDSHAKE_EX_DATA *ex_data =
  293. (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));
  294. ex_data->session_ticket_do_not_call = 1;
  295. return 0;
  296. }
  297. /* Parse the comma-separated list into TLS format. */
  298. static int parse_protos(const char *protos, unsigned char **out, size_t *outlen)
  299. {
  300. size_t len, i, prefix;
  301. len = strlen(protos);
  302. /* Should never have reuse. */
  303. if (!TEST_ptr_null(*out)
  304. /* Test values are small, so we omit length limit checks. */
  305. || !TEST_ptr(*out = OPENSSL_malloc(len + 1)))
  306. return 0;
  307. *outlen = len + 1;
  308. /*
  309. * foo => '3', 'f', 'o', 'o'
  310. * foo,bar => '3', 'f', 'o', 'o', '3', 'b', 'a', 'r'
  311. */
  312. memcpy(*out + 1, protos, len);
  313. prefix = 0;
  314. i = prefix + 1;
  315. while (i <= len) {
  316. if ((*out)[i] == ',') {
  317. if (!TEST_int_gt(i - 1, prefix))
  318. goto err;
  319. (*out)[prefix] = (unsigned char)(i - 1 - prefix);
  320. prefix = i;
  321. }
  322. i++;
  323. }
  324. if (!TEST_int_gt(len, prefix))
  325. goto err;
  326. (*out)[prefix] = (unsigned char)(len - prefix);
  327. return 1;
  328. err:
  329. OPENSSL_free(*out);
  330. *out = NULL;
  331. return 0;
  332. }
  333. #ifndef OPENSSL_NO_NEXTPROTONEG
  334. /*
  335. * The client SHOULD select the first protocol advertised by the server that it
  336. * also supports. In the event that the client doesn't support any of server's
  337. * protocols, or the server doesn't advertise any, it SHOULD select the first
  338. * protocol that it supports.
  339. */
  340. static int client_npn_cb(SSL *s, unsigned char **out, unsigned char *outlen,
  341. const unsigned char *in, unsigned int inlen,
  342. void *arg)
  343. {
  344. CTX_DATA *ctx_data = (CTX_DATA*)(arg);
  345. int ret;
  346. ret = SSL_select_next_proto(out, outlen, in, inlen,
  347. ctx_data->npn_protocols,
  348. ctx_data->npn_protocols_len);
  349. /* Accept both OPENSSL_NPN_NEGOTIATED and OPENSSL_NPN_NO_OVERLAP. */
  350. return TEST_true(ret == OPENSSL_NPN_NEGOTIATED || ret == OPENSSL_NPN_NO_OVERLAP)
  351. ? SSL_TLSEXT_ERR_OK : SSL_TLSEXT_ERR_ALERT_FATAL;
  352. }
  353. static int server_npn_cb(SSL *s, const unsigned char **data,
  354. unsigned int *len, void *arg)
  355. {
  356. CTX_DATA *ctx_data = (CTX_DATA*)(arg);
  357. *data = ctx_data->npn_protocols;
  358. *len = ctx_data->npn_protocols_len;
  359. return SSL_TLSEXT_ERR_OK;
  360. }
  361. #endif
  362. /*
  363. * The server SHOULD select the most highly preferred protocol that it supports
  364. * and that is also advertised by the client. In the event that the server
  365. * supports no protocols that the client advertises, then the server SHALL
  366. * respond with a fatal "no_application_protocol" alert.
  367. */
  368. static int server_alpn_cb(SSL *s, const unsigned char **out,
  369. unsigned char *outlen, const unsigned char *in,
  370. unsigned int inlen, void *arg)
  371. {
  372. CTX_DATA *ctx_data = (CTX_DATA*)(arg);
  373. int ret;
  374. /* SSL_select_next_proto isn't const-correct... */
  375. unsigned char *tmp_out;
  376. /*
  377. * The result points either to |in| or to |ctx_data->alpn_protocols|.
  378. * The callback is allowed to point to |in| or to a long-lived buffer,
  379. * so we can return directly without storing a copy.
  380. */
  381. ret = SSL_select_next_proto(&tmp_out, outlen,
  382. ctx_data->alpn_protocols,
  383. ctx_data->alpn_protocols_len, in, inlen);
  384. *out = tmp_out;
  385. /* Unlike NPN, we don't tolerate a mismatch. */
  386. return ret == OPENSSL_NPN_NEGOTIATED ? SSL_TLSEXT_ERR_OK
  387. : SSL_TLSEXT_ERR_ALERT_FATAL;
  388. }
  389. static int generate_session_ticket_cb(SSL *s, void *arg)
  390. {
  391. CTX_DATA *server_ctx_data = arg;
  392. SSL_SESSION *ss = SSL_get_session(s);
  393. char *app_data = server_ctx_data->session_ticket_app_data;
  394. if (ss == NULL || app_data == NULL)
  395. return 0;
  396. return SSL_SESSION_set1_ticket_appdata(ss, app_data, strlen(app_data));
  397. }
  398. static int decrypt_session_ticket_cb(SSL *s, SSL_SESSION *ss,
  399. const unsigned char *keyname,
  400. size_t keyname_len,
  401. SSL_TICKET_STATUS status,
  402. void *arg)
  403. {
  404. switch (status) {
  405. case SSL_TICKET_EMPTY:
  406. case SSL_TICKET_NO_DECRYPT:
  407. return SSL_TICKET_RETURN_IGNORE_RENEW;
  408. case SSL_TICKET_SUCCESS:
  409. return SSL_TICKET_RETURN_USE;
  410. case SSL_TICKET_SUCCESS_RENEW:
  411. return SSL_TICKET_RETURN_USE_RENEW;
  412. default:
  413. break;
  414. }
  415. return SSL_TICKET_RETURN_ABORT;
  416. }
  417. /*
  418. * Configure callbacks and other properties that can't be set directly
  419. * in the server/client CONF.
  420. */
  421. static int configure_handshake_ctx(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
  422. SSL_CTX *client_ctx,
  423. const SSL_TEST_CTX *test,
  424. const SSL_TEST_EXTRA_CONF *extra,
  425. CTX_DATA *server_ctx_data,
  426. CTX_DATA *server2_ctx_data,
  427. CTX_DATA *client_ctx_data)
  428. {
  429. unsigned char *ticket_keys;
  430. size_t ticket_key_len;
  431. if (!TEST_int_eq(SSL_CTX_set_max_send_fragment(server_ctx,
  432. test->max_fragment_size), 1))
  433. goto err;
  434. if (server2_ctx != NULL) {
  435. if (!TEST_int_eq(SSL_CTX_set_max_send_fragment(server2_ctx,
  436. test->max_fragment_size),
  437. 1))
  438. goto err;
  439. }
  440. if (!TEST_int_eq(SSL_CTX_set_max_send_fragment(client_ctx,
  441. test->max_fragment_size), 1))
  442. goto err;
  443. switch (extra->client.verify_callback) {
  444. case SSL_TEST_VERIFY_ACCEPT_ALL:
  445. SSL_CTX_set_cert_verify_callback(client_ctx, &verify_accept_cb, NULL);
  446. break;
  447. case SSL_TEST_VERIFY_RETRY_ONCE:
  448. n_retries = 1;
  449. SSL_CTX_set_cert_verify_callback(client_ctx, &verify_retry_cb, NULL);
  450. break;
  451. case SSL_TEST_VERIFY_REJECT_ALL:
  452. SSL_CTX_set_cert_verify_callback(client_ctx, &verify_reject_cb, NULL);
  453. break;
  454. case SSL_TEST_VERIFY_NONE:
  455. break;
  456. }
  457. switch (extra->client.max_fragment_len_mode) {
  458. case TLSEXT_max_fragment_length_512:
  459. case TLSEXT_max_fragment_length_1024:
  460. case TLSEXT_max_fragment_length_2048:
  461. case TLSEXT_max_fragment_length_4096:
  462. case TLSEXT_max_fragment_length_DISABLED:
  463. SSL_CTX_set_tlsext_max_fragment_length(
  464. client_ctx, extra->client.max_fragment_len_mode);
  465. break;
  466. }
  467. /*
  468. * Link the two contexts for SNI purposes.
  469. * Also do ClientHello callbacks here, as setting both ClientHello and SNI
  470. * is bad.
  471. */
  472. switch (extra->server.servername_callback) {
  473. case SSL_TEST_SERVERNAME_IGNORE_MISMATCH:
  474. SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_ignore_cb);
  475. SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx);
  476. break;
  477. case SSL_TEST_SERVERNAME_REJECT_MISMATCH:
  478. SSL_CTX_set_tlsext_servername_callback(server_ctx, servername_reject_cb);
  479. SSL_CTX_set_tlsext_servername_arg(server_ctx, server2_ctx);
  480. break;
  481. case SSL_TEST_SERVERNAME_CB_NONE:
  482. break;
  483. case SSL_TEST_SERVERNAME_CLIENT_HELLO_IGNORE_MISMATCH:
  484. SSL_CTX_set_client_hello_cb(server_ctx, client_hello_ignore_cb, server2_ctx);
  485. break;
  486. case SSL_TEST_SERVERNAME_CLIENT_HELLO_REJECT_MISMATCH:
  487. SSL_CTX_set_client_hello_cb(server_ctx, client_hello_reject_cb, server2_ctx);
  488. break;
  489. case SSL_TEST_SERVERNAME_CLIENT_HELLO_NO_V12:
  490. SSL_CTX_set_client_hello_cb(server_ctx, client_hello_nov12_cb, server2_ctx);
  491. }
  492. if (extra->server.cert_status != SSL_TEST_CERT_STATUS_NONE) {
  493. SSL_CTX_set_tlsext_status_type(client_ctx, TLSEXT_STATUSTYPE_ocsp);
  494. SSL_CTX_set_tlsext_status_cb(client_ctx, client_ocsp_cb);
  495. SSL_CTX_set_tlsext_status_arg(client_ctx, NULL);
  496. SSL_CTX_set_tlsext_status_cb(server_ctx, server_ocsp_cb);
  497. SSL_CTX_set_tlsext_status_arg(server_ctx,
  498. ((extra->server.cert_status == SSL_TEST_CERT_STATUS_GOOD_RESPONSE)
  499. ? &dummy_ocsp_resp_good_val : &dummy_ocsp_resp_bad_val));
  500. }
  501. /*
  502. * The initial_ctx/session_ctx always handles the encrypt/decrypt of the
  503. * session ticket. This ticket_key callback is assigned to the second
  504. * session (assigned via SNI), and should never be invoked
  505. */
  506. if (server2_ctx != NULL)
  507. SSL_CTX_set_tlsext_ticket_key_evp_cb(server2_ctx,
  508. do_not_call_session_ticket_cb);
  509. if (extra->server.broken_session_ticket) {
  510. SSL_CTX_set_tlsext_ticket_key_evp_cb(server_ctx,
  511. broken_session_ticket_cb);
  512. }
  513. #ifndef OPENSSL_NO_NEXTPROTONEG
  514. if (extra->server.npn_protocols != NULL) {
  515. if (!TEST_true(parse_protos(extra->server.npn_protocols,
  516. &server_ctx_data->npn_protocols,
  517. &server_ctx_data->npn_protocols_len)))
  518. goto err;
  519. SSL_CTX_set_npn_advertised_cb(server_ctx, server_npn_cb,
  520. server_ctx_data);
  521. }
  522. if (extra->server2.npn_protocols != NULL) {
  523. if (!TEST_true(parse_protos(extra->server2.npn_protocols,
  524. &server2_ctx_data->npn_protocols,
  525. &server2_ctx_data->npn_protocols_len))
  526. || !TEST_ptr(server2_ctx))
  527. goto err;
  528. SSL_CTX_set_npn_advertised_cb(server2_ctx, server_npn_cb,
  529. server2_ctx_data);
  530. }
  531. if (extra->client.npn_protocols != NULL) {
  532. if (!TEST_true(parse_protos(extra->client.npn_protocols,
  533. &client_ctx_data->npn_protocols,
  534. &client_ctx_data->npn_protocols_len)))
  535. goto err;
  536. SSL_CTX_set_next_proto_select_cb(client_ctx, client_npn_cb,
  537. client_ctx_data);
  538. }
  539. #endif
  540. if (extra->server.alpn_protocols != NULL) {
  541. if (!TEST_true(parse_protos(extra->server.alpn_protocols,
  542. &server_ctx_data->alpn_protocols,
  543. &server_ctx_data->alpn_protocols_len)))
  544. goto err;
  545. SSL_CTX_set_alpn_select_cb(server_ctx, server_alpn_cb, server_ctx_data);
  546. }
  547. if (extra->server2.alpn_protocols != NULL) {
  548. if (!TEST_ptr(server2_ctx)
  549. || !TEST_true(parse_protos(extra->server2.alpn_protocols,
  550. &server2_ctx_data->alpn_protocols,
  551. &server2_ctx_data->alpn_protocols_len
  552. )))
  553. goto err;
  554. SSL_CTX_set_alpn_select_cb(server2_ctx, server_alpn_cb,
  555. server2_ctx_data);
  556. }
  557. if (extra->client.alpn_protocols != NULL) {
  558. unsigned char *alpn_protos = NULL;
  559. size_t alpn_protos_len = 0;
  560. if (!TEST_true(parse_protos(extra->client.alpn_protocols,
  561. &alpn_protos, &alpn_protos_len))
  562. /* Reversed return value convention... */
  563. || !TEST_int_eq(SSL_CTX_set_alpn_protos(client_ctx, alpn_protos,
  564. alpn_protos_len), 0))
  565. goto err;
  566. OPENSSL_free(alpn_protos);
  567. }
  568. if (extra->server.session_ticket_app_data != NULL) {
  569. server_ctx_data->session_ticket_app_data =
  570. OPENSSL_strdup(extra->server.session_ticket_app_data);
  571. SSL_CTX_set_session_ticket_cb(server_ctx, generate_session_ticket_cb,
  572. decrypt_session_ticket_cb, server_ctx_data);
  573. }
  574. if (extra->server2.session_ticket_app_data != NULL) {
  575. if (!TEST_ptr(server2_ctx))
  576. goto err;
  577. server2_ctx_data->session_ticket_app_data =
  578. OPENSSL_strdup(extra->server2.session_ticket_app_data);
  579. SSL_CTX_set_session_ticket_cb(server2_ctx, NULL,
  580. decrypt_session_ticket_cb, server2_ctx_data);
  581. }
  582. /*
  583. * Use fixed session ticket keys so that we can decrypt a ticket created with
  584. * one CTX in another CTX. Don't address server2 for the moment.
  585. */
  586. ticket_key_len = SSL_CTX_set_tlsext_ticket_keys(server_ctx, NULL, 0);
  587. if (!TEST_ptr(ticket_keys = OPENSSL_zalloc(ticket_key_len))
  588. || !TEST_int_eq(SSL_CTX_set_tlsext_ticket_keys(server_ctx,
  589. ticket_keys,
  590. ticket_key_len), 1)) {
  591. OPENSSL_free(ticket_keys);
  592. goto err;
  593. }
  594. OPENSSL_free(ticket_keys);
  595. /* The default log list includes EC keys, so CT can't work without EC. */
  596. #if !defined(OPENSSL_NO_CT) && !defined(OPENSSL_NO_EC)
  597. if (!TEST_true(SSL_CTX_set_default_ctlog_list_file(client_ctx)))
  598. goto err;
  599. switch (extra->client.ct_validation) {
  600. case SSL_TEST_CT_VALIDATION_PERMISSIVE:
  601. if (!TEST_true(SSL_CTX_enable_ct(client_ctx,
  602. SSL_CT_VALIDATION_PERMISSIVE)))
  603. goto err;
  604. break;
  605. case SSL_TEST_CT_VALIDATION_STRICT:
  606. if (!TEST_true(SSL_CTX_enable_ct(client_ctx, SSL_CT_VALIDATION_STRICT)))
  607. goto err;
  608. break;
  609. case SSL_TEST_CT_VALIDATION_NONE:
  610. break;
  611. }
  612. #endif
  613. #ifndef OPENSSL_NO_SRP
  614. if (!configure_handshake_ctx_for_srp(server_ctx, server2_ctx, client_ctx,
  615. extra, server_ctx_data,
  616. server2_ctx_data, client_ctx_data))
  617. goto err;
  618. #endif /* !OPENSSL_NO_SRP */
  619. return 1;
  620. err:
  621. return 0;
  622. }
  623. /* Configure per-SSL callbacks and other properties. */
  624. static void configure_handshake_ssl(SSL *server, SSL *client,
  625. const SSL_TEST_EXTRA_CONF *extra)
  626. {
  627. if (extra->client.servername != SSL_TEST_SERVERNAME_NONE)
  628. SSL_set_tlsext_host_name(client,
  629. ssl_servername_name(extra->client.servername));
  630. if (extra->client.enable_pha)
  631. SSL_set_post_handshake_auth(client, 1);
  632. }
  633. /* The status for each connection phase. */
  634. typedef enum {
  635. PEER_SUCCESS,
  636. PEER_RETRY,
  637. PEER_ERROR,
  638. PEER_WAITING,
  639. PEER_TEST_FAILURE
  640. } peer_status_t;
  641. /* An SSL object and associated read-write buffers. */
  642. typedef struct peer_st {
  643. SSL *ssl;
  644. /* Buffer lengths are int to match the SSL read/write API. */
  645. unsigned char *write_buf;
  646. int write_buf_len;
  647. unsigned char *read_buf;
  648. int read_buf_len;
  649. int bytes_to_write;
  650. int bytes_to_read;
  651. peer_status_t status;
  652. } PEER;
  653. static int create_peer(PEER *peer, SSL_CTX *ctx)
  654. {
  655. static const int peer_buffer_size = 64 * 1024;
  656. SSL *ssl = NULL;
  657. unsigned char *read_buf = NULL, *write_buf = NULL;
  658. if (!TEST_ptr(ssl = SSL_new(ctx))
  659. || !TEST_ptr(write_buf = OPENSSL_zalloc(peer_buffer_size))
  660. || !TEST_ptr(read_buf = OPENSSL_zalloc(peer_buffer_size)))
  661. goto err;
  662. peer->ssl = ssl;
  663. peer->write_buf = write_buf;
  664. peer->read_buf = read_buf;
  665. peer->write_buf_len = peer->read_buf_len = peer_buffer_size;
  666. return 1;
  667. err:
  668. SSL_free(ssl);
  669. OPENSSL_free(write_buf);
  670. OPENSSL_free(read_buf);
  671. return 0;
  672. }
  673. static void peer_free_data(PEER *peer)
  674. {
  675. SSL_free(peer->ssl);
  676. OPENSSL_free(peer->write_buf);
  677. OPENSSL_free(peer->read_buf);
  678. }
  679. /*
  680. * Note that we could do the handshake transparently under an SSL_write,
  681. * but separating the steps is more helpful for debugging test failures.
  682. */
  683. static void do_handshake_step(PEER *peer)
  684. {
  685. if (!TEST_int_eq(peer->status, PEER_RETRY)) {
  686. peer->status = PEER_TEST_FAILURE;
  687. } else {
  688. int ret = SSL_do_handshake(peer->ssl);
  689. if (ret == 1) {
  690. peer->status = PEER_SUCCESS;
  691. } else if (ret == 0) {
  692. peer->status = PEER_ERROR;
  693. } else {
  694. int error = SSL_get_error(peer->ssl, ret);
  695. /* Memory bios should never block with SSL_ERROR_WANT_WRITE. */
  696. if (error != SSL_ERROR_WANT_READ
  697. && error != SSL_ERROR_WANT_RETRY_VERIFY)
  698. peer->status = PEER_ERROR;
  699. }
  700. }
  701. }
  702. /*-
  703. * Send/receive some application data. The read-write sequence is
  704. * Peer A: (R) W - first read will yield no data
  705. * Peer B: R W
  706. * ...
  707. * Peer A: R W
  708. * Peer B: R W
  709. * Peer A: R
  710. */
  711. static void do_app_data_step(PEER *peer)
  712. {
  713. int ret = 1, write_bytes;
  714. if (!TEST_int_eq(peer->status, PEER_RETRY)) {
  715. peer->status = PEER_TEST_FAILURE;
  716. return;
  717. }
  718. /* We read everything available... */
  719. while (ret > 0 && peer->bytes_to_read) {
  720. ret = SSL_read(peer->ssl, peer->read_buf, peer->read_buf_len);
  721. if (ret > 0) {
  722. if (!TEST_int_le(ret, peer->bytes_to_read)) {
  723. peer->status = PEER_TEST_FAILURE;
  724. return;
  725. }
  726. peer->bytes_to_read -= ret;
  727. } else if (ret == 0) {
  728. peer->status = PEER_ERROR;
  729. return;
  730. } else {
  731. int error = SSL_get_error(peer->ssl, ret);
  732. if (error != SSL_ERROR_WANT_READ) {
  733. peer->status = PEER_ERROR;
  734. return;
  735. } /* Else continue with write. */
  736. }
  737. }
  738. /* ... but we only write one write-buffer-full of data. */
  739. write_bytes = peer->bytes_to_write < peer->write_buf_len ? peer->bytes_to_write :
  740. peer->write_buf_len;
  741. if (write_bytes) {
  742. ret = SSL_write(peer->ssl, peer->write_buf, write_bytes);
  743. if (ret > 0) {
  744. /* SSL_write will only succeed with a complete write. */
  745. if (!TEST_int_eq(ret, write_bytes)) {
  746. peer->status = PEER_TEST_FAILURE;
  747. return;
  748. }
  749. peer->bytes_to_write -= ret;
  750. } else {
  751. /*
  752. * We should perhaps check for SSL_ERROR_WANT_READ/WRITE here
  753. * but this doesn't yet occur with current app data sizes.
  754. */
  755. peer->status = PEER_ERROR;
  756. return;
  757. }
  758. }
  759. /*
  760. * We could simply finish when there was nothing to read, and we have
  761. * nothing left to write. But keeping track of the expected number of bytes
  762. * to read gives us somewhat better guarantees that all data sent is in fact
  763. * received.
  764. */
  765. if (peer->bytes_to_write == 0 && peer->bytes_to_read == 0) {
  766. peer->status = PEER_SUCCESS;
  767. }
  768. }
  769. static void do_reneg_setup_step(const SSL_TEST_CTX *test_ctx, PEER *peer)
  770. {
  771. int ret;
  772. char buf;
  773. if (peer->status == PEER_SUCCESS) {
  774. /*
  775. * We are a client that succeeded this step previously, but the server
  776. * wanted to retry. Probably there is a no_renegotiation warning alert
  777. * waiting for us. Attempt to continue the handshake.
  778. */
  779. peer->status = PEER_RETRY;
  780. do_handshake_step(peer);
  781. return;
  782. }
  783. if (!TEST_int_eq(peer->status, PEER_RETRY)
  784. || !TEST_true(test_ctx->handshake_mode
  785. == SSL_TEST_HANDSHAKE_RENEG_SERVER
  786. || test_ctx->handshake_mode
  787. == SSL_TEST_HANDSHAKE_RENEG_CLIENT
  788. || test_ctx->handshake_mode
  789. == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER
  790. || test_ctx->handshake_mode
  791. == SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT
  792. || test_ctx->handshake_mode
  793. == SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH)) {
  794. peer->status = PEER_TEST_FAILURE;
  795. return;
  796. }
  797. /* Reset the count of the amount of app data we need to read/write */
  798. peer->bytes_to_write = peer->bytes_to_read = test_ctx->app_data_size;
  799. /* Check if we are the peer that is going to initiate */
  800. if ((test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_SERVER
  801. && SSL_is_server(peer->ssl))
  802. || (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_RENEG_CLIENT
  803. && !SSL_is_server(peer->ssl))) {
  804. /*
  805. * If we already asked for a renegotiation then fall through to the
  806. * SSL_read() below.
  807. */
  808. if (!SSL_renegotiate_pending(peer->ssl)) {
  809. /*
  810. * If we are the client we will always attempt to resume the
  811. * session. The server may or may not resume dependent on the
  812. * setting of SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  813. */
  814. if (SSL_is_server(peer->ssl)) {
  815. ret = SSL_renegotiate(peer->ssl);
  816. } else {
  817. int full_reneg = 0;
  818. if (test_ctx->extra.client.no_extms_on_reneg) {
  819. SSL_set_options(peer->ssl, SSL_OP_NO_EXTENDED_MASTER_SECRET);
  820. full_reneg = 1;
  821. }
  822. if (test_ctx->extra.client.reneg_ciphers != NULL) {
  823. if (!SSL_set_cipher_list(peer->ssl,
  824. test_ctx->extra.client.reneg_ciphers)) {
  825. peer->status = PEER_ERROR;
  826. return;
  827. }
  828. full_reneg = 1;
  829. }
  830. if (full_reneg)
  831. ret = SSL_renegotiate(peer->ssl);
  832. else
  833. ret = SSL_renegotiate_abbreviated(peer->ssl);
  834. }
  835. if (!ret) {
  836. peer->status = PEER_ERROR;
  837. return;
  838. }
  839. do_handshake_step(peer);
  840. /*
  841. * If status is PEER_RETRY it means we're waiting on the peer to
  842. * continue the handshake. As far as setting up the renegotiation is
  843. * concerned that is a success. The next step will continue the
  844. * handshake to its conclusion.
  845. *
  846. * If status is PEER_SUCCESS then we are the server and we have
  847. * successfully sent the HelloRequest. We need to continue to wait
  848. * until the handshake arrives from the client.
  849. */
  850. if (peer->status == PEER_RETRY)
  851. peer->status = PEER_SUCCESS;
  852. else if (peer->status == PEER_SUCCESS)
  853. peer->status = PEER_RETRY;
  854. return;
  855. }
  856. } else if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER
  857. || test_ctx->handshake_mode
  858. == SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT) {
  859. if (SSL_is_server(peer->ssl)
  860. != (test_ctx->handshake_mode
  861. == SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER)) {
  862. peer->status = PEER_SUCCESS;
  863. return;
  864. }
  865. ret = SSL_key_update(peer->ssl, test_ctx->key_update_type);
  866. if (!ret) {
  867. peer->status = PEER_ERROR;
  868. return;
  869. }
  870. do_handshake_step(peer);
  871. /*
  872. * This is a one step handshake. We shouldn't get anything other than
  873. * PEER_SUCCESS
  874. */
  875. if (peer->status != PEER_SUCCESS)
  876. peer->status = PEER_ERROR;
  877. return;
  878. } else if (test_ctx->handshake_mode == SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH) {
  879. if (SSL_is_server(peer->ssl)) {
  880. /* Make the server believe it's received the extension */
  881. if (test_ctx->extra.server.force_pha)
  882. peer->ssl->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
  883. ret = SSL_verify_client_post_handshake(peer->ssl);
  884. if (!ret) {
  885. peer->status = PEER_ERROR;
  886. return;
  887. }
  888. }
  889. do_handshake_step(peer);
  890. /*
  891. * This is a one step handshake. We shouldn't get anything other than
  892. * PEER_SUCCESS
  893. */
  894. if (peer->status != PEER_SUCCESS)
  895. peer->status = PEER_ERROR;
  896. return;
  897. }
  898. /*
  899. * The SSL object is still expecting app data, even though it's going to
  900. * get a handshake message. We try to read, and it should fail - after which
  901. * we should be in a handshake
  902. */
  903. ret = SSL_read(peer->ssl, &buf, sizeof(buf));
  904. if (ret >= 0) {
  905. /*
  906. * We're not actually expecting data - we're expecting a reneg to
  907. * start
  908. */
  909. peer->status = PEER_ERROR;
  910. return;
  911. } else {
  912. int error = SSL_get_error(peer->ssl, ret);
  913. if (error != SSL_ERROR_WANT_READ) {
  914. peer->status = PEER_ERROR;
  915. return;
  916. }
  917. /* If we're not in init yet then we're not done with setup yet */
  918. if (!SSL_in_init(peer->ssl))
  919. return;
  920. }
  921. peer->status = PEER_SUCCESS;
  922. }
  923. /*
  924. * RFC 5246 says:
  925. *
  926. * Note that as of TLS 1.1,
  927. * failure to properly close a connection no longer requires that a
  928. * session not be resumed. This is a change from TLS 1.0 to conform
  929. * with widespread implementation practice.
  930. *
  931. * However,
  932. * (a) OpenSSL requires that a connection be shutdown for all protocol versions.
  933. * (b) We test lower versions, too.
  934. * So we just implement shutdown. We do a full bidirectional shutdown so that we
  935. * can compare sent and received close_notify alerts and get some test coverage
  936. * for SSL_shutdown as a bonus.
  937. */
  938. static void do_shutdown_step(PEER *peer)
  939. {
  940. int ret;
  941. if (!TEST_int_eq(peer->status, PEER_RETRY)) {
  942. peer->status = PEER_TEST_FAILURE;
  943. return;
  944. }
  945. ret = SSL_shutdown(peer->ssl);
  946. if (ret == 1) {
  947. peer->status = PEER_SUCCESS;
  948. } else if (ret < 0) { /* On 0, we retry. */
  949. int error = SSL_get_error(peer->ssl, ret);
  950. if (error != SSL_ERROR_WANT_READ && error != SSL_ERROR_WANT_WRITE)
  951. peer->status = PEER_ERROR;
  952. }
  953. }
  954. typedef enum {
  955. HANDSHAKE,
  956. RENEG_APPLICATION_DATA,
  957. RENEG_SETUP,
  958. RENEG_HANDSHAKE,
  959. APPLICATION_DATA,
  960. SHUTDOWN,
  961. CONNECTION_DONE
  962. } connect_phase_t;
  963. static int renegotiate_op(const SSL_TEST_CTX *test_ctx)
  964. {
  965. switch (test_ctx->handshake_mode) {
  966. case SSL_TEST_HANDSHAKE_RENEG_SERVER:
  967. case SSL_TEST_HANDSHAKE_RENEG_CLIENT:
  968. return 1;
  969. default:
  970. return 0;
  971. }
  972. }
  973. static int post_handshake_op(const SSL_TEST_CTX *test_ctx)
  974. {
  975. switch (test_ctx->handshake_mode) {
  976. case SSL_TEST_HANDSHAKE_KEY_UPDATE_CLIENT:
  977. case SSL_TEST_HANDSHAKE_KEY_UPDATE_SERVER:
  978. case SSL_TEST_HANDSHAKE_POST_HANDSHAKE_AUTH:
  979. return 1;
  980. default:
  981. return 0;
  982. }
  983. }
  984. static connect_phase_t next_phase(const SSL_TEST_CTX *test_ctx,
  985. connect_phase_t phase)
  986. {
  987. switch (phase) {
  988. case HANDSHAKE:
  989. if (renegotiate_op(test_ctx) || post_handshake_op(test_ctx))
  990. return RENEG_APPLICATION_DATA;
  991. return APPLICATION_DATA;
  992. case RENEG_APPLICATION_DATA:
  993. return RENEG_SETUP;
  994. case RENEG_SETUP:
  995. if (post_handshake_op(test_ctx))
  996. return APPLICATION_DATA;
  997. return RENEG_HANDSHAKE;
  998. case RENEG_HANDSHAKE:
  999. return APPLICATION_DATA;
  1000. case APPLICATION_DATA:
  1001. return SHUTDOWN;
  1002. case SHUTDOWN:
  1003. return CONNECTION_DONE;
  1004. case CONNECTION_DONE:
  1005. TEST_error("Trying to progress after connection done");
  1006. break;
  1007. }
  1008. return -1;
  1009. }
  1010. static void do_connect_step(const SSL_TEST_CTX *test_ctx, PEER *peer,
  1011. connect_phase_t phase)
  1012. {
  1013. switch (phase) {
  1014. case HANDSHAKE:
  1015. do_handshake_step(peer);
  1016. break;
  1017. case RENEG_APPLICATION_DATA:
  1018. do_app_data_step(peer);
  1019. break;
  1020. case RENEG_SETUP:
  1021. do_reneg_setup_step(test_ctx, peer);
  1022. break;
  1023. case RENEG_HANDSHAKE:
  1024. do_handshake_step(peer);
  1025. break;
  1026. case APPLICATION_DATA:
  1027. do_app_data_step(peer);
  1028. break;
  1029. case SHUTDOWN:
  1030. do_shutdown_step(peer);
  1031. break;
  1032. case CONNECTION_DONE:
  1033. TEST_error("Action after connection done");
  1034. break;
  1035. }
  1036. }
  1037. typedef enum {
  1038. /* Both parties succeeded. */
  1039. HANDSHAKE_SUCCESS,
  1040. /* Client errored. */
  1041. CLIENT_ERROR,
  1042. /* Server errored. */
  1043. SERVER_ERROR,
  1044. /* Peers are in inconsistent state. */
  1045. INTERNAL_ERROR,
  1046. /* One or both peers not done. */
  1047. HANDSHAKE_RETRY
  1048. } handshake_status_t;
  1049. /*
  1050. * Determine the handshake outcome.
  1051. * last_status: the status of the peer to have acted last.
  1052. * previous_status: the status of the peer that didn't act last.
  1053. * client_spoke_last: 1 if the client went last.
  1054. */
  1055. static handshake_status_t handshake_status(peer_status_t last_status,
  1056. peer_status_t previous_status,
  1057. int client_spoke_last)
  1058. {
  1059. switch (last_status) {
  1060. case PEER_TEST_FAILURE:
  1061. return INTERNAL_ERROR;
  1062. case PEER_WAITING:
  1063. /* Shouldn't ever happen */
  1064. return INTERNAL_ERROR;
  1065. case PEER_SUCCESS:
  1066. switch (previous_status) {
  1067. case PEER_TEST_FAILURE:
  1068. return INTERNAL_ERROR;
  1069. case PEER_SUCCESS:
  1070. /* Both succeeded. */
  1071. return HANDSHAKE_SUCCESS;
  1072. case PEER_WAITING:
  1073. case PEER_RETRY:
  1074. /* Let the first peer finish. */
  1075. return HANDSHAKE_RETRY;
  1076. case PEER_ERROR:
  1077. /*
  1078. * Second peer succeeded despite the fact that the first peer
  1079. * already errored. This shouldn't happen.
  1080. */
  1081. return INTERNAL_ERROR;
  1082. }
  1083. break;
  1084. case PEER_RETRY:
  1085. return HANDSHAKE_RETRY;
  1086. case PEER_ERROR:
  1087. switch (previous_status) {
  1088. case PEER_TEST_FAILURE:
  1089. return INTERNAL_ERROR;
  1090. case PEER_WAITING:
  1091. /* The client failed immediately before sending the ClientHello */
  1092. return client_spoke_last ? CLIENT_ERROR : INTERNAL_ERROR;
  1093. case PEER_SUCCESS:
  1094. /*
  1095. * First peer succeeded but second peer errored.
  1096. * TODO(emilia): we should be able to continue here (with some
  1097. * application data?) to ensure the first peer receives the
  1098. * alert / close_notify.
  1099. * (No tests currently exercise this branch.)
  1100. */
  1101. return client_spoke_last ? CLIENT_ERROR : SERVER_ERROR;
  1102. case PEER_RETRY:
  1103. /* We errored; let the peer finish. */
  1104. return HANDSHAKE_RETRY;
  1105. case PEER_ERROR:
  1106. /* Both peers errored. Return the one that errored first. */
  1107. return client_spoke_last ? SERVER_ERROR : CLIENT_ERROR;
  1108. }
  1109. }
  1110. /* Control should never reach here. */
  1111. return INTERNAL_ERROR;
  1112. }
  1113. /* Convert unsigned char buf's that shouldn't contain any NUL-bytes to char. */
  1114. static char *dup_str(const unsigned char *in, size_t len)
  1115. {
  1116. char *ret = NULL;
  1117. if (len == 0)
  1118. return NULL;
  1119. /* Assert that the string does not contain NUL-bytes. */
  1120. if (TEST_size_t_eq(OPENSSL_strnlen((const char*)(in), len), len))
  1121. TEST_ptr(ret = OPENSSL_strndup((const char*)(in), len));
  1122. return ret;
  1123. }
  1124. static int pkey_type(EVP_PKEY *pkey)
  1125. {
  1126. if (EVP_PKEY_is_a(pkey, "EC")) {
  1127. char name[80];
  1128. size_t name_len;
  1129. if (!EVP_PKEY_get_group_name(pkey, name, sizeof(name), &name_len))
  1130. return NID_undef;
  1131. return OBJ_txt2nid(name);
  1132. }
  1133. return EVP_PKEY_id(pkey);
  1134. }
  1135. static int peer_pkey_type(SSL *s)
  1136. {
  1137. X509 *x = SSL_get0_peer_certificate(s);
  1138. if (x != NULL)
  1139. return pkey_type(X509_get0_pubkey(x));
  1140. return NID_undef;
  1141. }
  1142. #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK)
  1143. static int set_sock_as_sctp(int sock)
  1144. {
  1145. struct sctp_assocparams assocparams;
  1146. struct sctp_rtoinfo rto_info;
  1147. BIO *tmpbio;
  1148. /*
  1149. * To allow tests to fail fast (within a second or so), reduce the
  1150. * retransmission timeouts and the number of retransmissions.
  1151. */
  1152. memset(&rto_info, 0, sizeof(struct sctp_rtoinfo));
  1153. rto_info.srto_initial = 100;
  1154. rto_info.srto_max = 200;
  1155. rto_info.srto_min = 50;
  1156. (void)setsockopt(sock, IPPROTO_SCTP, SCTP_RTOINFO,
  1157. (const void *)&rto_info, sizeof(struct sctp_rtoinfo));
  1158. memset(&assocparams, 0, sizeof(struct sctp_assocparams));
  1159. assocparams.sasoc_asocmaxrxt = 2;
  1160. (void)setsockopt(sock, IPPROTO_SCTP, SCTP_ASSOCINFO,
  1161. (const void *)&assocparams,
  1162. sizeof(struct sctp_assocparams));
  1163. /*
  1164. * For SCTP we have to set various options on the socket prior to
  1165. * connecting. This is done automatically by BIO_new_dgram_sctp().
  1166. * We don't actually need the created BIO though so we free it again
  1167. * immediately.
  1168. */
  1169. tmpbio = BIO_new_dgram_sctp(sock, BIO_NOCLOSE);
  1170. if (tmpbio == NULL)
  1171. return 0;
  1172. BIO_free(tmpbio);
  1173. return 1;
  1174. }
  1175. static int create_sctp_socks(int *ssock, int *csock)
  1176. {
  1177. BIO_ADDRINFO *res = NULL;
  1178. const BIO_ADDRINFO *ai = NULL;
  1179. int lsock = INVALID_SOCKET, asock = INVALID_SOCKET;
  1180. int consock = INVALID_SOCKET;
  1181. int ret = 0;
  1182. int family = 0;
  1183. if (BIO_sock_init() != 1)
  1184. return 0;
  1185. /*
  1186. * Port is 4463. It could be anything. It will fail if it's already being
  1187. * used for some other SCTP service. It seems unlikely though so we don't
  1188. * worry about it here.
  1189. */
  1190. if (!BIO_lookup_ex(NULL, "4463", BIO_LOOKUP_SERVER, family, SOCK_STREAM,
  1191. IPPROTO_SCTP, &res))
  1192. return 0;
  1193. for (ai = res; ai != NULL; ai = BIO_ADDRINFO_next(ai)) {
  1194. family = BIO_ADDRINFO_family(ai);
  1195. lsock = BIO_socket(family, SOCK_STREAM, IPPROTO_SCTP, 0);
  1196. if (lsock == INVALID_SOCKET) {
  1197. /* Maybe the kernel doesn't support the socket family, even if
  1198. * BIO_lookup() added it in the returned result...
  1199. */
  1200. continue;
  1201. }
  1202. if (!set_sock_as_sctp(lsock)
  1203. || !BIO_listen(lsock, BIO_ADDRINFO_address(ai),
  1204. BIO_SOCK_REUSEADDR)) {
  1205. BIO_closesocket(lsock);
  1206. lsock = INVALID_SOCKET;
  1207. continue;
  1208. }
  1209. /* Success, don't try any more addresses */
  1210. break;
  1211. }
  1212. if (lsock == INVALID_SOCKET)
  1213. goto err;
  1214. BIO_ADDRINFO_free(res);
  1215. res = NULL;
  1216. if (!BIO_lookup_ex(NULL, "4463", BIO_LOOKUP_CLIENT, family, SOCK_STREAM,
  1217. IPPROTO_SCTP, &res))
  1218. goto err;
  1219. consock = BIO_socket(family, SOCK_STREAM, IPPROTO_SCTP, 0);
  1220. if (consock == INVALID_SOCKET)
  1221. goto err;
  1222. if (!set_sock_as_sctp(consock)
  1223. || !BIO_connect(consock, BIO_ADDRINFO_address(res), 0)
  1224. || !BIO_socket_nbio(consock, 1))
  1225. goto err;
  1226. asock = BIO_accept_ex(lsock, NULL, BIO_SOCK_NONBLOCK);
  1227. if (asock == INVALID_SOCKET)
  1228. goto err;
  1229. *csock = consock;
  1230. *ssock = asock;
  1231. consock = asock = INVALID_SOCKET;
  1232. ret = 1;
  1233. err:
  1234. BIO_ADDRINFO_free(res);
  1235. if (consock != INVALID_SOCKET)
  1236. BIO_closesocket(consock);
  1237. if (lsock != INVALID_SOCKET)
  1238. BIO_closesocket(lsock);
  1239. if (asock != INVALID_SOCKET)
  1240. BIO_closesocket(asock);
  1241. return ret;
  1242. }
  1243. #endif
  1244. /*
  1245. * Note that |extra| points to the correct client/server configuration
  1246. * within |test_ctx|. When configuring the handshake, general mode settings
  1247. * are taken from |test_ctx|, and client/server-specific settings should be
  1248. * taken from |extra|.
  1249. *
  1250. * The configuration code should never reach into |test_ctx->extra| or
  1251. * |test_ctx->resume_extra| directly.
  1252. *
  1253. * (We could refactor test mode settings into a substructure. This would result
  1254. * in cleaner argument passing but would complicate the test configuration
  1255. * parsing.)
  1256. */
  1257. static HANDSHAKE_RESULT *do_handshake_internal(
  1258. SSL_CTX *server_ctx, SSL_CTX *server2_ctx, SSL_CTX *client_ctx,
  1259. const SSL_TEST_CTX *test_ctx, const SSL_TEST_EXTRA_CONF *extra,
  1260. SSL_SESSION *session_in, SSL_SESSION *serv_sess_in,
  1261. SSL_SESSION **session_out, SSL_SESSION **serv_sess_out)
  1262. {
  1263. PEER server, client;
  1264. BIO *client_to_server = NULL, *server_to_client = NULL;
  1265. HANDSHAKE_EX_DATA server_ex_data, client_ex_data;
  1266. CTX_DATA client_ctx_data, server_ctx_data, server2_ctx_data;
  1267. HANDSHAKE_RESULT *ret = HANDSHAKE_RESULT_new();
  1268. int client_turn = 1, client_turn_count = 0, client_wait_count = 0;
  1269. connect_phase_t phase = HANDSHAKE;
  1270. handshake_status_t status = HANDSHAKE_RETRY;
  1271. const unsigned char* tick = NULL;
  1272. size_t tick_len = 0;
  1273. const unsigned char* sess_id = NULL;
  1274. unsigned int sess_id_len = 0;
  1275. SSL_SESSION* sess = NULL;
  1276. const unsigned char *proto = NULL;
  1277. /* API dictates unsigned int rather than size_t. */
  1278. unsigned int proto_len = 0;
  1279. EVP_PKEY *tmp_key;
  1280. const STACK_OF(X509_NAME) *names;
  1281. time_t start;
  1282. const char* cipher;
  1283. if (ret == NULL)
  1284. return NULL;
  1285. memset(&server_ctx_data, 0, sizeof(server_ctx_data));
  1286. memset(&server2_ctx_data, 0, sizeof(server2_ctx_data));
  1287. memset(&client_ctx_data, 0, sizeof(client_ctx_data));
  1288. memset(&server, 0, sizeof(server));
  1289. memset(&client, 0, sizeof(client));
  1290. memset(&server_ex_data, 0, sizeof(server_ex_data));
  1291. memset(&client_ex_data, 0, sizeof(client_ex_data));
  1292. if (!configure_handshake_ctx(server_ctx, server2_ctx, client_ctx,
  1293. test_ctx, extra, &server_ctx_data,
  1294. &server2_ctx_data, &client_ctx_data)) {
  1295. TEST_note("configure_handshake_ctx");
  1296. return NULL;
  1297. }
  1298. #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK)
  1299. if (test_ctx->enable_client_sctp_label_bug)
  1300. SSL_CTX_set_mode(client_ctx, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG);
  1301. if (test_ctx->enable_server_sctp_label_bug)
  1302. SSL_CTX_set_mode(server_ctx, SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG);
  1303. #endif
  1304. /* Setup SSL and buffers; additional configuration happens below. */
  1305. if (!create_peer(&server, server_ctx)) {
  1306. TEST_note("creating server context");
  1307. goto err;
  1308. }
  1309. if (!create_peer(&client, client_ctx)) {
  1310. TEST_note("creating client context");
  1311. goto err;
  1312. }
  1313. server.bytes_to_write = client.bytes_to_read = test_ctx->app_data_size;
  1314. client.bytes_to_write = server.bytes_to_read = test_ctx->app_data_size;
  1315. configure_handshake_ssl(server.ssl, client.ssl, extra);
  1316. if (session_in != NULL) {
  1317. SSL_SESSION_get_id(serv_sess_in, &sess_id_len);
  1318. /* In case we're testing resumption without tickets. */
  1319. if ((sess_id_len > 0
  1320. && !TEST_true(SSL_CTX_add_session(server_ctx,
  1321. serv_sess_in)))
  1322. || !TEST_true(SSL_set_session(client.ssl, session_in)))
  1323. goto err;
  1324. sess_id_len = 0;
  1325. }
  1326. ret->result = SSL_TEST_INTERNAL_ERROR;
  1327. if (test_ctx->use_sctp) {
  1328. #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK)
  1329. int csock, ssock;
  1330. if (create_sctp_socks(&ssock, &csock)) {
  1331. client_to_server = BIO_new_dgram_sctp(csock, BIO_CLOSE);
  1332. server_to_client = BIO_new_dgram_sctp(ssock, BIO_CLOSE);
  1333. }
  1334. #endif
  1335. } else {
  1336. client_to_server = BIO_new(BIO_s_mem());
  1337. server_to_client = BIO_new(BIO_s_mem());
  1338. }
  1339. if (!TEST_ptr(client_to_server)
  1340. || !TEST_ptr(server_to_client))
  1341. goto err;
  1342. /* Non-blocking bio. */
  1343. BIO_set_nbio(client_to_server, 1);
  1344. BIO_set_nbio(server_to_client, 1);
  1345. SSL_set_connect_state(client.ssl);
  1346. SSL_set_accept_state(server.ssl);
  1347. /* The bios are now owned by the SSL object. */
  1348. if (test_ctx->use_sctp) {
  1349. SSL_set_bio(client.ssl, client_to_server, client_to_server);
  1350. SSL_set_bio(server.ssl, server_to_client, server_to_client);
  1351. } else {
  1352. SSL_set_bio(client.ssl, server_to_client, client_to_server);
  1353. if (!TEST_int_gt(BIO_up_ref(server_to_client), 0)
  1354. || !TEST_int_gt(BIO_up_ref(client_to_server), 0))
  1355. goto err;
  1356. SSL_set_bio(server.ssl, client_to_server, server_to_client);
  1357. }
  1358. ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL);
  1359. if (!TEST_int_ge(ex_data_idx, 0)
  1360. || !TEST_int_eq(SSL_set_ex_data(server.ssl, ex_data_idx, &server_ex_data), 1)
  1361. || !TEST_int_eq(SSL_set_ex_data(client.ssl, ex_data_idx, &client_ex_data), 1))
  1362. goto err;
  1363. SSL_set_info_callback(server.ssl, &info_cb);
  1364. SSL_set_info_callback(client.ssl, &info_cb);
  1365. client.status = PEER_RETRY;
  1366. server.status = PEER_WAITING;
  1367. start = time(NULL);
  1368. /*
  1369. * Half-duplex handshake loop.
  1370. * Client and server speak to each other synchronously in the same process.
  1371. * We use non-blocking BIOs, so whenever one peer blocks for read, it
  1372. * returns PEER_RETRY to indicate that it's the other peer's turn to write.
  1373. * The handshake succeeds once both peers have succeeded. If one peer
  1374. * errors out, we also let the other peer retry (and presumably fail).
  1375. */
  1376. for(;;) {
  1377. if (client_turn) {
  1378. do_connect_step(test_ctx, &client, phase);
  1379. status = handshake_status(client.status, server.status,
  1380. 1 /* client went last */);
  1381. if (server.status == PEER_WAITING)
  1382. server.status = PEER_RETRY;
  1383. } else {
  1384. do_connect_step(test_ctx, &server, phase);
  1385. status = handshake_status(server.status, client.status,
  1386. 0 /* server went last */);
  1387. }
  1388. switch (status) {
  1389. case HANDSHAKE_SUCCESS:
  1390. client_turn_count = 0;
  1391. phase = next_phase(test_ctx, phase);
  1392. if (phase == CONNECTION_DONE) {
  1393. ret->result = SSL_TEST_SUCCESS;
  1394. goto err;
  1395. } else {
  1396. client.status = server.status = PEER_RETRY;
  1397. /*
  1398. * For now, client starts each phase. Since each phase is
  1399. * started separately, we can later control this more
  1400. * precisely, for example, to test client-initiated and
  1401. * server-initiated shutdown.
  1402. */
  1403. client_turn = 1;
  1404. break;
  1405. }
  1406. case CLIENT_ERROR:
  1407. ret->result = SSL_TEST_CLIENT_FAIL;
  1408. goto err;
  1409. case SERVER_ERROR:
  1410. ret->result = SSL_TEST_SERVER_FAIL;
  1411. goto err;
  1412. case INTERNAL_ERROR:
  1413. ret->result = SSL_TEST_INTERNAL_ERROR;
  1414. goto err;
  1415. case HANDSHAKE_RETRY:
  1416. if (test_ctx->use_sctp) {
  1417. if (time(NULL) - start > 3) {
  1418. /*
  1419. * We've waited for too long. Give up.
  1420. */
  1421. ret->result = SSL_TEST_INTERNAL_ERROR;
  1422. goto err;
  1423. }
  1424. /*
  1425. * With "real" sockets we only swap to processing the peer
  1426. * if they are expecting to retry. Otherwise we just retry the
  1427. * same endpoint again.
  1428. */
  1429. if ((client_turn && server.status == PEER_RETRY)
  1430. || (!client_turn && client.status == PEER_RETRY))
  1431. client_turn ^= 1;
  1432. } else {
  1433. if (client_turn_count++ >= 2000) {
  1434. /*
  1435. * At this point, there's been so many PEER_RETRY in a row
  1436. * that it's likely both sides are stuck waiting for a read.
  1437. * It's time to give up.
  1438. */
  1439. ret->result = SSL_TEST_INTERNAL_ERROR;
  1440. goto err;
  1441. }
  1442. if (client_turn && server.status == PEER_SUCCESS) {
  1443. /*
  1444. * The server may finish before the client because the
  1445. * client spends some turns processing NewSessionTickets.
  1446. */
  1447. if (client_wait_count++ >= 2) {
  1448. ret->result = SSL_TEST_INTERNAL_ERROR;
  1449. goto err;
  1450. }
  1451. } else {
  1452. /* Continue. */
  1453. client_turn ^= 1;
  1454. }
  1455. }
  1456. break;
  1457. }
  1458. }
  1459. err:
  1460. ret->server_alert_sent = server_ex_data.alert_sent;
  1461. ret->server_num_fatal_alerts_sent = server_ex_data.num_fatal_alerts_sent;
  1462. ret->server_alert_received = client_ex_data.alert_received;
  1463. ret->client_alert_sent = client_ex_data.alert_sent;
  1464. ret->client_num_fatal_alerts_sent = client_ex_data.num_fatal_alerts_sent;
  1465. ret->client_alert_received = server_ex_data.alert_received;
  1466. ret->server_protocol = SSL_version(server.ssl);
  1467. ret->client_protocol = SSL_version(client.ssl);
  1468. ret->servername = server_ex_data.servername;
  1469. if ((sess = SSL_get0_session(client.ssl)) != NULL) {
  1470. SSL_SESSION_get0_ticket(sess, &tick, &tick_len);
  1471. sess_id = SSL_SESSION_get_id(sess, &sess_id_len);
  1472. }
  1473. if (tick == NULL || tick_len == 0)
  1474. ret->session_ticket = SSL_TEST_SESSION_TICKET_NO;
  1475. else
  1476. ret->session_ticket = SSL_TEST_SESSION_TICKET_YES;
  1477. ret->compression = (SSL_get_current_compression(client.ssl) == NULL)
  1478. ? SSL_TEST_COMPRESSION_NO
  1479. : SSL_TEST_COMPRESSION_YES;
  1480. if (sess_id == NULL || sess_id_len == 0)
  1481. ret->session_id = SSL_TEST_SESSION_ID_NO;
  1482. else
  1483. ret->session_id = SSL_TEST_SESSION_ID_YES;
  1484. ret->session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call;
  1485. if (extra->client.verify_callback == SSL_TEST_VERIFY_RETRY_ONCE
  1486. && n_retries != -1)
  1487. ret->result = SSL_TEST_SERVER_FAIL;
  1488. #ifndef OPENSSL_NO_NEXTPROTONEG
  1489. SSL_get0_next_proto_negotiated(client.ssl, &proto, &proto_len);
  1490. ret->client_npn_negotiated = dup_str(proto, proto_len);
  1491. SSL_get0_next_proto_negotiated(server.ssl, &proto, &proto_len);
  1492. ret->server_npn_negotiated = dup_str(proto, proto_len);
  1493. #endif
  1494. SSL_get0_alpn_selected(client.ssl, &proto, &proto_len);
  1495. ret->client_alpn_negotiated = dup_str(proto, proto_len);
  1496. SSL_get0_alpn_selected(server.ssl, &proto, &proto_len);
  1497. ret->server_alpn_negotiated = dup_str(proto, proto_len);
  1498. if ((sess = SSL_get0_session(server.ssl)) != NULL) {
  1499. SSL_SESSION_get0_ticket_appdata(sess, (void**)&tick, &tick_len);
  1500. ret->result_session_ticket_app_data = OPENSSL_strndup((const char*)tick, tick_len);
  1501. }
  1502. ret->client_resumed = SSL_session_reused(client.ssl);
  1503. ret->server_resumed = SSL_session_reused(server.ssl);
  1504. cipher = SSL_CIPHER_get_name(SSL_get_current_cipher(client.ssl));
  1505. ret->cipher = dup_str((const unsigned char*)cipher, strlen(cipher));
  1506. if (session_out != NULL)
  1507. *session_out = SSL_get1_session(client.ssl);
  1508. if (serv_sess_out != NULL) {
  1509. SSL_SESSION *tmp = SSL_get_session(server.ssl);
  1510. /*
  1511. * We create a fresh copy that is not in the server session ctx linked
  1512. * list.
  1513. */
  1514. if (tmp != NULL)
  1515. *serv_sess_out = SSL_SESSION_dup(tmp);
  1516. }
  1517. if (SSL_get_peer_tmp_key(client.ssl, &tmp_key)) {
  1518. ret->tmp_key_type = pkey_type(tmp_key);
  1519. EVP_PKEY_free(tmp_key);
  1520. }
  1521. SSL_get_peer_signature_nid(client.ssl, &ret->server_sign_hash);
  1522. SSL_get_peer_signature_nid(server.ssl, &ret->client_sign_hash);
  1523. SSL_get_peer_signature_type_nid(client.ssl, &ret->server_sign_type);
  1524. SSL_get_peer_signature_type_nid(server.ssl, &ret->client_sign_type);
  1525. names = SSL_get0_peer_CA_list(client.ssl);
  1526. if (names == NULL)
  1527. ret->client_ca_names = NULL;
  1528. else
  1529. ret->client_ca_names = SSL_dup_CA_list(names);
  1530. names = SSL_get0_peer_CA_list(server.ssl);
  1531. if (names == NULL)
  1532. ret->server_ca_names = NULL;
  1533. else
  1534. ret->server_ca_names = SSL_dup_CA_list(names);
  1535. ret->server_cert_type = peer_pkey_type(client.ssl);
  1536. ret->client_cert_type = peer_pkey_type(server.ssl);
  1537. ctx_data_free_data(&server_ctx_data);
  1538. ctx_data_free_data(&server2_ctx_data);
  1539. ctx_data_free_data(&client_ctx_data);
  1540. peer_free_data(&server);
  1541. peer_free_data(&client);
  1542. return ret;
  1543. }
  1544. HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
  1545. SSL_CTX *client_ctx, SSL_CTX *resume_server_ctx,
  1546. SSL_CTX *resume_client_ctx,
  1547. const SSL_TEST_CTX *test_ctx)
  1548. {
  1549. HANDSHAKE_RESULT *result;
  1550. SSL_SESSION *session = NULL, *serv_sess = NULL;
  1551. result = do_handshake_internal(server_ctx, server2_ctx, client_ctx,
  1552. test_ctx, &test_ctx->extra,
  1553. NULL, NULL, &session, &serv_sess);
  1554. if (result == NULL
  1555. || test_ctx->handshake_mode != SSL_TEST_HANDSHAKE_RESUME
  1556. || result->result == SSL_TEST_INTERNAL_ERROR)
  1557. goto end;
  1558. if (result->result != SSL_TEST_SUCCESS) {
  1559. result->result = SSL_TEST_FIRST_HANDSHAKE_FAILED;
  1560. goto end;
  1561. }
  1562. HANDSHAKE_RESULT_free(result);
  1563. /* We don't support SNI on second handshake yet, so server2_ctx is NULL. */
  1564. result = do_handshake_internal(resume_server_ctx, NULL, resume_client_ctx,
  1565. test_ctx, &test_ctx->resume_extra,
  1566. session, serv_sess, NULL, NULL);
  1567. end:
  1568. SSL_SESSION_free(session);
  1569. SSL_SESSION_free(serv_sess);
  1570. return result;
  1571. }