handshake_helper.c 57 KB

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