handshake_helper.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  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.force_pha)
  664. SSL_force_post_handshake_auth(client);
  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 **session_out)
  1271. {
  1272. PEER server, client;
  1273. BIO *client_to_server = NULL, *server_to_client = NULL;
  1274. HANDSHAKE_EX_DATA server_ex_data, client_ex_data;
  1275. CTX_DATA client_ctx_data, server_ctx_data, server2_ctx_data;
  1276. HANDSHAKE_RESULT *ret = HANDSHAKE_RESULT_new();
  1277. int client_turn = 1, client_turn_count = 0, client_wait_count = 0;
  1278. connect_phase_t phase = HANDSHAKE;
  1279. handshake_status_t status = HANDSHAKE_RETRY;
  1280. const unsigned char* tick = NULL;
  1281. size_t tick_len = 0;
  1282. const unsigned char* sess_id = NULL;
  1283. unsigned int sess_id_len = 0;
  1284. SSL_SESSION* sess = NULL;
  1285. const unsigned char *proto = NULL;
  1286. /* API dictates unsigned int rather than size_t. */
  1287. unsigned int proto_len = 0;
  1288. EVP_PKEY *tmp_key;
  1289. const STACK_OF(X509_NAME) *names;
  1290. time_t start;
  1291. const char* cipher;
  1292. if (ret == NULL)
  1293. return NULL;
  1294. memset(&server_ctx_data, 0, sizeof(server_ctx_data));
  1295. memset(&server2_ctx_data, 0, sizeof(server2_ctx_data));
  1296. memset(&client_ctx_data, 0, sizeof(client_ctx_data));
  1297. memset(&server, 0, sizeof(server));
  1298. memset(&client, 0, sizeof(client));
  1299. memset(&server_ex_data, 0, sizeof(server_ex_data));
  1300. memset(&client_ex_data, 0, sizeof(client_ex_data));
  1301. if (!configure_handshake_ctx(server_ctx, server2_ctx, client_ctx,
  1302. test_ctx, extra, &server_ctx_data,
  1303. &server2_ctx_data, &client_ctx_data)) {
  1304. TEST_note("configure_handshake_ctx");
  1305. return NULL;
  1306. }
  1307. /* Setup SSL and buffers; additional configuration happens below. */
  1308. if (!create_peer(&server, server_ctx)) {
  1309. TEST_note("creating server context");
  1310. goto err;
  1311. }
  1312. if (!create_peer(&client, client_ctx)) {
  1313. TEST_note("creating client context");
  1314. goto err;
  1315. }
  1316. server.bytes_to_write = client.bytes_to_read = test_ctx->app_data_size;
  1317. client.bytes_to_write = server.bytes_to_read = test_ctx->app_data_size;
  1318. configure_handshake_ssl(server.ssl, client.ssl, extra);
  1319. if (session_in != NULL) {
  1320. /* In case we're testing resumption without tickets. */
  1321. if (!TEST_true(SSL_CTX_add_session(server_ctx, session_in))
  1322. || !TEST_true(SSL_set_session(client.ssl, session_in)))
  1323. goto err;
  1324. }
  1325. ret->result = SSL_TEST_INTERNAL_ERROR;
  1326. if (test_ctx->use_sctp) {
  1327. #if !defined(OPENSSL_NO_SCTP) && !defined(OPENSSL_NO_SOCK)
  1328. int csock, ssock;
  1329. if (create_sctp_socks(&ssock, &csock)) {
  1330. client_to_server = BIO_new_dgram_sctp(csock, BIO_CLOSE);
  1331. server_to_client = BIO_new_dgram_sctp(ssock, BIO_CLOSE);
  1332. }
  1333. #endif
  1334. } else {
  1335. client_to_server = BIO_new(BIO_s_mem());
  1336. server_to_client = BIO_new(BIO_s_mem());
  1337. }
  1338. if (!TEST_ptr(client_to_server)
  1339. || !TEST_ptr(server_to_client))
  1340. goto err;
  1341. /* Non-blocking bio. */
  1342. BIO_set_nbio(client_to_server, 1);
  1343. BIO_set_nbio(server_to_client, 1);
  1344. SSL_set_connect_state(client.ssl);
  1345. SSL_set_accept_state(server.ssl);
  1346. /* The bios are now owned by the SSL object. */
  1347. if (test_ctx->use_sctp) {
  1348. SSL_set_bio(client.ssl, client_to_server, client_to_server);
  1349. SSL_set_bio(server.ssl, server_to_client, server_to_client);
  1350. } else {
  1351. SSL_set_bio(client.ssl, server_to_client, client_to_server);
  1352. if (!TEST_int_gt(BIO_up_ref(server_to_client), 0)
  1353. || !TEST_int_gt(BIO_up_ref(client_to_server), 0))
  1354. goto err;
  1355. SSL_set_bio(server.ssl, client_to_server, server_to_client);
  1356. }
  1357. ex_data_idx = SSL_get_ex_new_index(0, "ex data", NULL, NULL, NULL);
  1358. if (!TEST_int_ge(ex_data_idx, 0)
  1359. || !TEST_int_eq(SSL_set_ex_data(server.ssl, ex_data_idx, &server_ex_data), 1)
  1360. || !TEST_int_eq(SSL_set_ex_data(client.ssl, ex_data_idx, &client_ex_data), 1))
  1361. goto err;
  1362. SSL_set_info_callback(server.ssl, &info_cb);
  1363. SSL_set_info_callback(client.ssl, &info_cb);
  1364. client.status = PEER_RETRY;
  1365. server.status = PEER_WAITING;
  1366. start = time(NULL);
  1367. /*
  1368. * Half-duplex handshake loop.
  1369. * Client and server speak to each other synchronously in the same process.
  1370. * We use non-blocking BIOs, so whenever one peer blocks for read, it
  1371. * returns PEER_RETRY to indicate that it's the other peer's turn to write.
  1372. * The handshake succeeds once both peers have succeeded. If one peer
  1373. * errors out, we also let the other peer retry (and presumably fail).
  1374. */
  1375. for(;;) {
  1376. if (client_turn) {
  1377. do_connect_step(test_ctx, &client, phase);
  1378. status = handshake_status(client.status, server.status,
  1379. 1 /* client went last */);
  1380. if (server.status == PEER_WAITING)
  1381. server.status = PEER_RETRY;
  1382. } else {
  1383. do_connect_step(test_ctx, &server, phase);
  1384. status = handshake_status(server.status, client.status,
  1385. 0 /* server went last */);
  1386. }
  1387. switch (status) {
  1388. case HANDSHAKE_SUCCESS:
  1389. client_turn_count = 0;
  1390. phase = next_phase(test_ctx, phase);
  1391. if (phase == CONNECTION_DONE) {
  1392. ret->result = SSL_TEST_SUCCESS;
  1393. goto err;
  1394. } else {
  1395. client.status = server.status = PEER_RETRY;
  1396. /*
  1397. * For now, client starts each phase. Since each phase is
  1398. * started separately, we can later control this more
  1399. * precisely, for example, to test client-initiated and
  1400. * server-initiated shutdown.
  1401. */
  1402. client_turn = 1;
  1403. break;
  1404. }
  1405. case CLIENT_ERROR:
  1406. ret->result = SSL_TEST_CLIENT_FAIL;
  1407. goto err;
  1408. case SERVER_ERROR:
  1409. ret->result = SSL_TEST_SERVER_FAIL;
  1410. goto err;
  1411. case INTERNAL_ERROR:
  1412. ret->result = SSL_TEST_INTERNAL_ERROR;
  1413. goto err;
  1414. case HANDSHAKE_RETRY:
  1415. if (test_ctx->use_sctp) {
  1416. if (time(NULL) - start > 3) {
  1417. /*
  1418. * We've waited for too long. Give up.
  1419. */
  1420. ret->result = SSL_TEST_INTERNAL_ERROR;
  1421. goto err;
  1422. }
  1423. /*
  1424. * With "real" sockets we only swap to processing the peer
  1425. * if they are expecting to retry. Otherwise we just retry the
  1426. * same endpoint again.
  1427. */
  1428. if ((client_turn && server.status == PEER_RETRY)
  1429. || (!client_turn && client.status == PEER_RETRY))
  1430. client_turn ^= 1;
  1431. } else {
  1432. if (client_turn_count++ >= 2000) {
  1433. /*
  1434. * At this point, there's been so many PEER_RETRY in a row
  1435. * that it's likely both sides are stuck waiting for a read.
  1436. * It's time to give up.
  1437. */
  1438. ret->result = SSL_TEST_INTERNAL_ERROR;
  1439. goto err;
  1440. }
  1441. if (client_turn && server.status == PEER_SUCCESS) {
  1442. /*
  1443. * The server may finish before the client because the
  1444. * client spends some turns processing NewSessionTickets.
  1445. */
  1446. if (client_wait_count++ >= 2) {
  1447. ret->result = SSL_TEST_INTERNAL_ERROR;
  1448. goto err;
  1449. }
  1450. } else {
  1451. /* Continue. */
  1452. client_turn ^= 1;
  1453. }
  1454. }
  1455. break;
  1456. }
  1457. }
  1458. err:
  1459. ret->server_alert_sent = server_ex_data.alert_sent;
  1460. ret->server_num_fatal_alerts_sent = server_ex_data.num_fatal_alerts_sent;
  1461. ret->server_alert_received = client_ex_data.alert_received;
  1462. ret->client_alert_sent = client_ex_data.alert_sent;
  1463. ret->client_num_fatal_alerts_sent = client_ex_data.num_fatal_alerts_sent;
  1464. ret->client_alert_received = server_ex_data.alert_received;
  1465. ret->server_protocol = SSL_version(server.ssl);
  1466. ret->client_protocol = SSL_version(client.ssl);
  1467. ret->servername = server_ex_data.servername;
  1468. if ((sess = SSL_get0_session(client.ssl)) != NULL) {
  1469. SSL_SESSION_get0_ticket(sess, &tick, &tick_len);
  1470. sess_id = SSL_SESSION_get_id(sess, &sess_id_len);
  1471. }
  1472. if (tick == NULL || tick_len == 0)
  1473. ret->session_ticket = SSL_TEST_SESSION_TICKET_NO;
  1474. else
  1475. ret->session_ticket = SSL_TEST_SESSION_TICKET_YES;
  1476. ret->compression = (SSL_get_current_compression(client.ssl) == NULL)
  1477. ? SSL_TEST_COMPRESSION_NO
  1478. : SSL_TEST_COMPRESSION_YES;
  1479. if (sess_id == NULL || sess_id_len == 0)
  1480. ret->session_id = SSL_TEST_SESSION_ID_NO;
  1481. else
  1482. ret->session_id = SSL_TEST_SESSION_ID_YES;
  1483. ret->session_ticket_do_not_call = server_ex_data.session_ticket_do_not_call;
  1484. #ifndef OPENSSL_NO_NEXTPROTONEG
  1485. SSL_get0_next_proto_negotiated(client.ssl, &proto, &proto_len);
  1486. ret->client_npn_negotiated = dup_str(proto, proto_len);
  1487. SSL_get0_next_proto_negotiated(server.ssl, &proto, &proto_len);
  1488. ret->server_npn_negotiated = dup_str(proto, proto_len);
  1489. #endif
  1490. SSL_get0_alpn_selected(client.ssl, &proto, &proto_len);
  1491. ret->client_alpn_negotiated = dup_str(proto, proto_len);
  1492. SSL_get0_alpn_selected(server.ssl, &proto, &proto_len);
  1493. ret->server_alpn_negotiated = dup_str(proto, proto_len);
  1494. if ((sess = SSL_get0_session(server.ssl)) != NULL) {
  1495. SSL_SESSION_get0_ticket_appdata(sess, (void**)&tick, &tick_len);
  1496. ret->result_session_ticket_app_data = OPENSSL_strndup((const char*)tick, tick_len);
  1497. }
  1498. ret->client_resumed = SSL_session_reused(client.ssl);
  1499. ret->server_resumed = SSL_session_reused(server.ssl);
  1500. cipher = SSL_CIPHER_get_name(SSL_get_current_cipher(client.ssl));
  1501. ret->cipher = dup_str((const unsigned char*)cipher, strlen(cipher));
  1502. if (session_out != NULL)
  1503. *session_out = SSL_get1_session(client.ssl);
  1504. if (SSL_get_server_tmp_key(client.ssl, &tmp_key)) {
  1505. ret->tmp_key_type = pkey_type(tmp_key);
  1506. EVP_PKEY_free(tmp_key);
  1507. }
  1508. SSL_get_peer_signature_nid(client.ssl, &ret->server_sign_hash);
  1509. SSL_get_peer_signature_nid(server.ssl, &ret->client_sign_hash);
  1510. SSL_get_peer_signature_type_nid(client.ssl, &ret->server_sign_type);
  1511. SSL_get_peer_signature_type_nid(server.ssl, &ret->client_sign_type);
  1512. names = SSL_get0_peer_CA_list(client.ssl);
  1513. if (names == NULL)
  1514. ret->client_ca_names = NULL;
  1515. else
  1516. ret->client_ca_names = SSL_dup_CA_list(names);
  1517. names = SSL_get0_peer_CA_list(server.ssl);
  1518. if (names == NULL)
  1519. ret->server_ca_names = NULL;
  1520. else
  1521. ret->server_ca_names = SSL_dup_CA_list(names);
  1522. ret->server_cert_type = peer_pkey_type(client.ssl);
  1523. ret->client_cert_type = peer_pkey_type(server.ssl);
  1524. ctx_data_free_data(&server_ctx_data);
  1525. ctx_data_free_data(&server2_ctx_data);
  1526. ctx_data_free_data(&client_ctx_data);
  1527. peer_free_data(&server);
  1528. peer_free_data(&client);
  1529. return ret;
  1530. }
  1531. HANDSHAKE_RESULT *do_handshake(SSL_CTX *server_ctx, SSL_CTX *server2_ctx,
  1532. SSL_CTX *client_ctx, SSL_CTX *resume_server_ctx,
  1533. SSL_CTX *resume_client_ctx,
  1534. const SSL_TEST_CTX *test_ctx)
  1535. {
  1536. HANDSHAKE_RESULT *result;
  1537. SSL_SESSION *session = NULL;
  1538. result = do_handshake_internal(server_ctx, server2_ctx, client_ctx,
  1539. test_ctx, &test_ctx->extra,
  1540. NULL, &session);
  1541. if (result == NULL
  1542. || test_ctx->handshake_mode != SSL_TEST_HANDSHAKE_RESUME
  1543. || result->result == SSL_TEST_INTERNAL_ERROR)
  1544. goto end;
  1545. if (result->result != SSL_TEST_SUCCESS) {
  1546. result->result = SSL_TEST_FIRST_HANDSHAKE_FAILED;
  1547. goto end;
  1548. }
  1549. HANDSHAKE_RESULT_free(result);
  1550. /* We don't support SNI on second handshake yet, so server2_ctx is NULL. */
  1551. result = do_handshake_internal(resume_server_ctx, NULL, resume_client_ctx,
  1552. test_ctx, &test_ctx->resume_extra,
  1553. session, NULL);
  1554. end:
  1555. SSL_SESSION_free(session);
  1556. return result;
  1557. }