handshake.c 59 KB

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