handshake.c 58 KB

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