handshake_helper.c 60 KB

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