handshake_helper.c 60 KB

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