2
0

handshake.c 59 KB

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