handshake_helper.c 58 KB

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