handshake_helper.c 60 KB

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