sslapitest.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. /*
  2. * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/opensslconf.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/crypto.h>
  13. #include <openssl/ssl.h>
  14. #include <openssl/ocsp.h>
  15. #include "ssltestlib.h"
  16. #include "testutil.h"
  17. #include "e_os.h"
  18. static char *cert = NULL;
  19. static char *privkey = NULL;
  20. #define LOG_BUFFER_SIZE 1024
  21. static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
  22. static size_t server_log_buffer_index = 0;
  23. static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
  24. static size_t client_log_buffer_index = 0;
  25. static int error_writing_log = 0;
  26. #ifndef OPENSSL_NO_OCSP
  27. static const unsigned char orespder[] = "Dummy OCSP Response";
  28. static int ocsp_server_called = 0;
  29. static int ocsp_client_called = 0;
  30. static int cdummyarg = 1;
  31. static X509 *ocspcert = NULL;
  32. #endif
  33. #define NUM_EXTRA_CERTS 40
  34. /*
  35. * This structure is used to validate that the correct number of log messages
  36. * of various types are emitted when emitting secret logs.
  37. */
  38. struct sslapitest_log_counts {
  39. unsigned int rsa_key_exchange_count;
  40. unsigned int master_secret_count;
  41. unsigned int client_handshake_secret_count;
  42. unsigned int server_handshake_secret_count;
  43. unsigned int client_application_secret_count;
  44. unsigned int server_application_secret_count;
  45. };
  46. static void client_keylog_callback(const SSL *ssl, const char *line)
  47. {
  48. int line_length = strlen(line);
  49. /* If the log doesn't fit, error out. */
  50. if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
  51. TEST_info("Client log too full");
  52. error_writing_log = 1;
  53. return;
  54. }
  55. strcat(client_log_buffer, line);
  56. client_log_buffer_index += line_length;
  57. client_log_buffer[client_log_buffer_index++] = '\n';
  58. }
  59. static void server_keylog_callback(const SSL *ssl, const char *line)
  60. {
  61. int line_length = strlen(line);
  62. /* If the log doesn't fit, error out. */
  63. if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
  64. TEST_info("Server og too full");
  65. error_writing_log = 1;
  66. return;
  67. }
  68. strcat(server_log_buffer, line);
  69. server_log_buffer_index += line_length;
  70. server_log_buffer[server_log_buffer_index++] = '\n';
  71. }
  72. static int compare_hex_encoded_buffer(const char *hex_encoded,
  73. size_t hex_length,
  74. const uint8_t *raw,
  75. size_t raw_length)
  76. {
  77. size_t i, j;
  78. char hexed[3];
  79. if (!TEST_size_t_eq(raw_length * 2, hex_length))
  80. return 1;
  81. for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
  82. sprintf(hexed, "%02x", raw[i]);
  83. if (!TEST_int_eq(hexed[0], hex_encoded[j])
  84. || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. static int test_keylog_output(char *buffer, const SSL *ssl,
  90. const SSL_SESSION *session,
  91. struct sslapitest_log_counts *expected)
  92. {
  93. char *token = NULL;
  94. unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
  95. size_t client_random_size = SSL3_RANDOM_SIZE;
  96. unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
  97. size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
  98. unsigned int rsa_key_exchange_count = 0;
  99. unsigned int master_secret_count = 0;
  100. unsigned int client_handshake_secret_count = 0;
  101. unsigned int server_handshake_secret_count = 0;
  102. unsigned int client_application_secret_count = 0;
  103. unsigned int server_application_secret_count = 0;
  104. for (token = strtok(buffer, " \n"); token != NULL;
  105. token = strtok(NULL, " \n")) {
  106. if (strcmp(token, "RSA") == 0) {
  107. /*
  108. * Premaster secret. Tokens should be: 16 ASCII bytes of
  109. * hex-encoded encrypted secret, then the hex-encoded pre-master
  110. * secret.
  111. */
  112. if (!TEST_ptr(token = strtok(NULL, " \n")))
  113. return 0;
  114. if (!TEST_size_t_eq(strlen(token), 16))
  115. return 0;
  116. if (!TEST_ptr(token = strtok(NULL, " \n")))
  117. return 0;
  118. /*
  119. * We can't sensibly check the log because the premaster secret is
  120. * transient, and OpenSSL doesn't keep hold of it once the master
  121. * secret is generated.
  122. */
  123. rsa_key_exchange_count++;
  124. } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
  125. /*
  126. * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
  127. * client random, then the hex-encoded master secret.
  128. */
  129. client_random_size = SSL_get_client_random(ssl,
  130. actual_client_random,
  131. SSL3_RANDOM_SIZE);
  132. if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
  133. return 0;
  134. if (!TEST_ptr(token = strtok(NULL, " \n")))
  135. return 0;
  136. if (!TEST_size_t_eq(strlen(token), 64))
  137. return 0;
  138. if (!TEST_false(compare_hex_encoded_buffer(token, 64,
  139. actual_client_random,
  140. client_random_size)))
  141. return 0;
  142. if (!TEST_ptr(token = strtok(NULL, " \n")))
  143. return 0;
  144. master_key_size = SSL_SESSION_get_master_key(session,
  145. actual_master_key,
  146. master_key_size);
  147. if (!TEST_size_t_ne(master_key_size, 0))
  148. return 0;
  149. if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
  150. actual_master_key,
  151. master_key_size)))
  152. return 0;
  153. master_secret_count++;
  154. } else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
  155. || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
  156. || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
  157. || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0) {
  158. /*
  159. * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
  160. * client random, and then the hex-encoded secret. In this case,
  161. * we treat all of these secrets identically and then just
  162. * distinguish between them when counting what we saw.
  163. */
  164. if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
  165. client_handshake_secret_count++;
  166. else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
  167. server_handshake_secret_count++;
  168. else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
  169. client_application_secret_count++;
  170. else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
  171. server_application_secret_count++;
  172. client_random_size = SSL_get_client_random(ssl,
  173. actual_client_random,
  174. SSL3_RANDOM_SIZE);
  175. if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
  176. return 0;
  177. if (!TEST_ptr(token = strtok(NULL, " \n")))
  178. return 0;
  179. if (!TEST_size_t_eq(strlen(token), 64))
  180. return 0;
  181. if (!TEST_false(compare_hex_encoded_buffer(token, 64,
  182. actual_client_random,
  183. client_random_size)))
  184. return 0;
  185. if (!TEST_ptr(token = strtok(NULL, " \n")))
  186. return 0;
  187. /*
  188. * TODO(TLS1.3): test that application traffic secrets are what
  189. * we expect */
  190. } else {
  191. TEST_info("Unexpected token %s\n", token);
  192. return 0;
  193. }
  194. }
  195. /* Got what we expected? */
  196. if (!TEST_size_t_eq(rsa_key_exchange_count,
  197. expected->rsa_key_exchange_count)
  198. || !TEST_size_t_eq(master_secret_count,
  199. expected->master_secret_count)
  200. || !TEST_size_t_eq(client_handshake_secret_count,
  201. expected->client_handshake_secret_count)
  202. || !TEST_size_t_eq(server_handshake_secret_count,
  203. expected->server_handshake_secret_count)
  204. || !TEST_size_t_eq(client_application_secret_count,
  205. expected->client_application_secret_count)
  206. || !TEST_size_t_eq(server_application_secret_count,
  207. expected->server_application_secret_count))
  208. return 0;
  209. return 1;
  210. }
  211. static int test_keylog(void)
  212. {
  213. SSL_CTX *cctx = NULL, *sctx = NULL;
  214. SSL *clientssl = NULL, *serverssl = NULL;
  215. int testresult = 0;
  216. struct sslapitest_log_counts expected = {0};
  217. /* Clean up logging space */
  218. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  219. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  220. client_log_buffer_index = 0;
  221. server_log_buffer_index = 0;
  222. error_writing_log = 0;
  223. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  224. TLS_client_method(),
  225. &sctx, &cctx, cert, privkey)))
  226. return 0;
  227. /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
  228. SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
  229. SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
  230. /* We also want to ensure that we use RSA-based key exchange. */
  231. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
  232. goto end;
  233. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
  234. || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
  235. goto end;
  236. SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
  237. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
  238. == client_keylog_callback))
  239. goto end;
  240. SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
  241. if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
  242. == server_keylog_callback))
  243. goto end;
  244. /* Now do a handshake and check that the logs have been written to. */
  245. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  246. &clientssl, NULL, NULL))
  247. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  248. SSL_ERROR_NONE))
  249. || !TEST_false(error_writing_log)
  250. || !TEST_int_gt(client_log_buffer_index, 0)
  251. || !TEST_int_gt(server_log_buffer_index, 0))
  252. goto end;
  253. /*
  254. * Now we want to test that our output data was vaguely sensible. We
  255. * do that by using strtok and confirming that we have more or less the
  256. * data we expect. For both client and server, we expect to see one master
  257. * secret. The client should also see a RSA key exchange.
  258. */
  259. expected.rsa_key_exchange_count = 1;
  260. expected.master_secret_count = 1;
  261. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  262. SSL_get_session(clientssl), &expected)))
  263. goto end;
  264. expected.rsa_key_exchange_count = 0;
  265. if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
  266. SSL_get_session(serverssl), &expected)))
  267. goto end;
  268. testresult = 1;
  269. end:
  270. SSL_free(serverssl);
  271. SSL_free(clientssl);
  272. SSL_CTX_free(sctx);
  273. SSL_CTX_free(cctx);
  274. return testresult;
  275. }
  276. #ifndef OPENSSL_NO_TLS1_3
  277. static int test_keylog_no_master_key(void)
  278. {
  279. SSL_CTX *cctx = NULL, *sctx = NULL;
  280. SSL *clientssl = NULL, *serverssl = NULL;
  281. int testresult = 0;
  282. struct sslapitest_log_counts expected = {0};
  283. /* Clean up logging space */
  284. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  285. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  286. client_log_buffer_index = 0;
  287. server_log_buffer_index = 0;
  288. error_writing_log = 0;
  289. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  290. TLS_client_method(), &sctx,
  291. &cctx, cert, privkey)))
  292. return 0;
  293. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
  294. || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
  295. goto end;
  296. SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
  297. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
  298. == client_keylog_callback))
  299. goto end;
  300. SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
  301. if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
  302. == server_keylog_callback))
  303. goto end;
  304. /* Now do a handshake and check that the logs have been written to. */
  305. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  306. &clientssl, NULL, NULL))
  307. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  308. SSL_ERROR_NONE))
  309. || !TEST_false(error_writing_log))
  310. goto end;
  311. /*
  312. * Now we want to test that our output data was vaguely sensible. For this
  313. * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
  314. * TLSv1.3, but we do expect both client and server to emit keys.
  315. */
  316. expected.client_handshake_secret_count = 1;
  317. expected.server_handshake_secret_count = 1;
  318. expected.client_application_secret_count = 1;
  319. expected.server_application_secret_count = 1;
  320. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  321. SSL_get_session(clientssl), &expected))
  322. || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
  323. SSL_get_session(serverssl),
  324. &expected)))
  325. goto end;
  326. testresult = 1;
  327. end:
  328. SSL_free(serverssl);
  329. SSL_free(clientssl);
  330. SSL_CTX_free(sctx);
  331. SSL_CTX_free(cctx);
  332. return testresult;
  333. }
  334. #endif
  335. #ifndef OPENSSL_NO_TLS1_2
  336. static int full_early_callback(SSL *s, int *al, void *arg)
  337. {
  338. int *ctr = arg;
  339. const unsigned char *p;
  340. /* We only configure two ciphers, but the SCSV is added automatically. */
  341. #ifdef OPENSSL_NO_EC
  342. const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
  343. #else
  344. const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
  345. 0x2c, 0x00, 0xff};
  346. #endif
  347. size_t len;
  348. /* Make sure we can defer processing and get called back. */
  349. if ((*ctr)++ == 0)
  350. return -1;
  351. len = SSL_early_get0_ciphers(s, &p);
  352. if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
  353. || !TEST_size_t_eq(SSL_early_get0_compression_methods(s, &p), 1)
  354. || !TEST_int_eq(*p, 0))
  355. return 0;
  356. return 1;
  357. }
  358. static int test_early_cb(void)
  359. {
  360. SSL_CTX *cctx = NULL, *sctx = NULL;
  361. SSL *clientssl = NULL, *serverssl = NULL;
  362. int testctr = 0, testresult = 0;
  363. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  364. TLS_client_method(), &sctx,
  365. &cctx, cert, privkey)))
  366. goto end;
  367. SSL_CTX_set_early_cb(sctx, full_early_callback, &testctr);
  368. /* The gimpy cipher list we configure can't do TLS 1.3. */
  369. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  370. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  371. "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
  372. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  373. &clientssl, NULL, NULL))
  374. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  375. SSL_ERROR_WANT_EARLY))
  376. /*
  377. * Passing a -1 literal is a hack since
  378. * the real value was lost.
  379. * */
  380. || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_EARLY)
  381. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  382. SSL_ERROR_NONE)))
  383. goto end;
  384. testresult = 1;
  385. end:
  386. SSL_free(serverssl);
  387. SSL_free(clientssl);
  388. SSL_CTX_free(sctx);
  389. SSL_CTX_free(cctx);
  390. return testresult;
  391. }
  392. #endif
  393. static int execute_test_large_message(const SSL_METHOD *smeth,
  394. const SSL_METHOD *cmeth, int read_ahead)
  395. {
  396. SSL_CTX *cctx = NULL, *sctx = NULL;
  397. SSL *clientssl = NULL, *serverssl = NULL;
  398. int testresult = 0;
  399. int i;
  400. BIO *certbio = NULL;
  401. X509 *chaincert = NULL;
  402. int certlen;
  403. if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))
  404. goto end;
  405. chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
  406. BIO_free(certbio);
  407. certbio = NULL;
  408. if (!TEST_ptr(chaincert))
  409. goto end;
  410. if (!TEST_true(create_ssl_ctx_pair(smeth, cmeth, &sctx,
  411. &cctx, cert, privkey)))
  412. goto end;
  413. if (read_ahead) {
  414. /*
  415. * Test that read_ahead works correctly when dealing with large
  416. * records
  417. */
  418. SSL_CTX_set_read_ahead(cctx, 1);
  419. }
  420. /*
  421. * We assume the supplied certificate is big enough so that if we add
  422. * NUM_EXTRA_CERTS it will make the overall message large enough. The
  423. * default buffer size is requested to be 16k, but due to the way BUF_MEM
  424. * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
  425. * test we need to have a message larger than that.
  426. */
  427. certlen = i2d_X509(chaincert, NULL);
  428. OPENSSL_assert(certlen * NUM_EXTRA_CERTS >
  429. (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
  430. for (i = 0; i < NUM_EXTRA_CERTS; i++) {
  431. if (!X509_up_ref(chaincert))
  432. goto end;
  433. if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
  434. X509_free(chaincert);
  435. goto end;
  436. }
  437. }
  438. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  439. NULL, NULL))
  440. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  441. SSL_ERROR_NONE)))
  442. goto end;
  443. /*
  444. * Calling SSL_clear() first is not required but this tests that SSL_clear()
  445. * doesn't leak (when using enable-crypto-mdebug).
  446. */
  447. if (!TEST_true(SSL_clear(serverssl)))
  448. goto end;
  449. testresult = 1;
  450. end:
  451. X509_free(chaincert);
  452. SSL_free(serverssl);
  453. SSL_free(clientssl);
  454. SSL_CTX_free(sctx);
  455. SSL_CTX_free(cctx);
  456. return testresult;
  457. }
  458. static int test_large_message_tls(void)
  459. {
  460. return execute_test_large_message(TLS_server_method(), TLS_client_method(),
  461. 0);
  462. }
  463. static int test_large_message_tls_read_ahead(void)
  464. {
  465. return execute_test_large_message(TLS_server_method(), TLS_client_method(),
  466. 1);
  467. }
  468. #ifndef OPENSSL_NO_DTLS
  469. static int test_large_message_dtls(void)
  470. {
  471. /*
  472. * read_ahead is not relevant to DTLS because DTLS always acts as if
  473. * read_ahead is set.
  474. */
  475. return execute_test_large_message(DTLS_server_method(),
  476. DTLS_client_method(), 0);
  477. }
  478. #endif
  479. #ifndef OPENSSL_NO_OCSP
  480. static int ocsp_server_cb(SSL *s, void *arg)
  481. {
  482. int *argi = (int *)arg;
  483. unsigned char *copy = NULL;
  484. STACK_OF(OCSP_RESPID) *ids = NULL;
  485. OCSP_RESPID *id = NULL;
  486. if (*argi == 2) {
  487. /* In this test we are expecting exactly 1 OCSP_RESPID */
  488. SSL_get_tlsext_status_ids(s, &ids);
  489. if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
  490. return SSL_TLSEXT_ERR_ALERT_FATAL;
  491. id = sk_OCSP_RESPID_value(ids, 0);
  492. if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
  493. return SSL_TLSEXT_ERR_ALERT_FATAL;
  494. } else if (*argi != 1) {
  495. return SSL_TLSEXT_ERR_ALERT_FATAL;
  496. }
  497. if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
  498. return SSL_TLSEXT_ERR_ALERT_FATAL;
  499. SSL_set_tlsext_status_ocsp_resp(s, copy, sizeof(orespder));
  500. ocsp_server_called = 1;
  501. return SSL_TLSEXT_ERR_OK;
  502. }
  503. static int ocsp_client_cb(SSL *s, void *arg)
  504. {
  505. int *argi = (int *)arg;
  506. const unsigned char *respderin;
  507. size_t len;
  508. if (*argi != 1 && *argi != 2)
  509. return 0;
  510. len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
  511. if (!TEST_mem_eq(orespder, len, respderin, len))
  512. return 0;
  513. ocsp_client_called = 1;
  514. return 1;
  515. }
  516. static int test_tlsext_status_type(void)
  517. {
  518. SSL_CTX *cctx = NULL, *sctx = NULL;
  519. SSL *clientssl = NULL, *serverssl = NULL;
  520. int testresult = 0;
  521. STACK_OF(OCSP_RESPID) *ids = NULL;
  522. OCSP_RESPID *id = NULL;
  523. BIO *certbio = NULL;
  524. if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,
  525. &cctx, cert, privkey))
  526. return 0;
  527. if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
  528. goto end;
  529. /* First just do various checks getting and setting tlsext_status_type */
  530. clientssl = SSL_new(cctx);
  531. if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
  532. || !TEST_true(SSL_set_tlsext_status_type(clientssl,
  533. TLSEXT_STATUSTYPE_ocsp))
  534. || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
  535. TLSEXT_STATUSTYPE_ocsp))
  536. goto end;
  537. SSL_free(clientssl);
  538. clientssl = NULL;
  539. if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
  540. || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
  541. goto end;
  542. clientssl = SSL_new(cctx);
  543. if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
  544. goto end;
  545. SSL_free(clientssl);
  546. clientssl = NULL;
  547. /*
  548. * Now actually do a handshake and check OCSP information is exchanged and
  549. * the callbacks get called
  550. */
  551. SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
  552. SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
  553. SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
  554. SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
  555. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  556. &clientssl, NULL, NULL))
  557. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  558. SSL_ERROR_NONE))
  559. || !TEST_true(ocsp_client_called)
  560. || !TEST_true(ocsp_server_called))
  561. goto end;
  562. SSL_free(serverssl);
  563. SSL_free(clientssl);
  564. serverssl = NULL;
  565. clientssl = NULL;
  566. /* Try again but this time force the server side callback to fail */
  567. ocsp_client_called = 0;
  568. ocsp_server_called = 0;
  569. cdummyarg = 0;
  570. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  571. &clientssl, NULL, NULL))
  572. /* This should fail because the callback will fail */
  573. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  574. SSL_ERROR_NONE))
  575. || !TEST_false(ocsp_client_called)
  576. || !TEST_false(ocsp_server_called))
  577. goto end;
  578. SSL_free(serverssl);
  579. SSL_free(clientssl);
  580. serverssl = NULL;
  581. clientssl = NULL;
  582. /*
  583. * This time we'll get the client to send an OCSP_RESPID that it will
  584. * accept.
  585. */
  586. ocsp_client_called = 0;
  587. ocsp_server_called = 0;
  588. cdummyarg = 2;
  589. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  590. &clientssl, NULL, NULL)))
  591. goto end;
  592. /*
  593. * We'll just use any old cert for this test - it doesn't have to be an OCSP
  594. * specific one. We'll use the server cert.
  595. */
  596. if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
  597. || !TEST_ptr(id = OCSP_RESPID_new())
  598. || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
  599. || !TEST_ptr(ocspcert = PEM_read_bio_X509(certbio,
  600. NULL, NULL, NULL))
  601. || !TEST_true(OCSP_RESPID_set_by_key(id, ocspcert))
  602. || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
  603. goto end;
  604. id = NULL;
  605. SSL_set_tlsext_status_ids(clientssl, ids);
  606. /* Control has been transferred */
  607. ids = NULL;
  608. BIO_free(certbio);
  609. certbio = NULL;
  610. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  611. SSL_ERROR_NONE))
  612. || !TEST_true(ocsp_client_called)
  613. || !TEST_true(ocsp_server_called))
  614. goto end;
  615. testresult = 1;
  616. end:
  617. SSL_free(serverssl);
  618. SSL_free(clientssl);
  619. SSL_CTX_free(sctx);
  620. SSL_CTX_free(cctx);
  621. sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
  622. OCSP_RESPID_free(id);
  623. BIO_free(certbio);
  624. X509_free(ocspcert);
  625. ocspcert = NULL;
  626. return testresult;
  627. }
  628. #endif
  629. typedef struct ssl_session_test_fixture {
  630. const char *test_case_name;
  631. int use_ext_cache;
  632. int use_int_cache;
  633. } SSL_SESSION_TEST_FIXTURE;
  634. static int new_called = 0, remove_called = 0;
  635. static SSL_SESSION_TEST_FIXTURE
  636. ssl_session_set_up(const char *const test_case_name)
  637. {
  638. SSL_SESSION_TEST_FIXTURE fixture;
  639. fixture.test_case_name = test_case_name;
  640. fixture.use_ext_cache = 1;
  641. fixture.use_int_cache = 1;
  642. new_called = remove_called = 0;
  643. return fixture;
  644. }
  645. static void ssl_session_tear_down(SSL_SESSION_TEST_FIXTURE fixture)
  646. {
  647. }
  648. static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
  649. {
  650. new_called++;
  651. return 1;
  652. }
  653. static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
  654. {
  655. remove_called++;
  656. }
  657. static int execute_test_session(SSL_SESSION_TEST_FIXTURE fix)
  658. {
  659. SSL_CTX *sctx = NULL, *cctx = NULL;
  660. SSL *serverssl1 = NULL, *clientssl1 = NULL;
  661. SSL *serverssl2 = NULL, *clientssl2 = NULL;
  662. #ifndef OPENSSL_NO_TLS1_1
  663. SSL *serverssl3 = NULL, *clientssl3 = NULL;
  664. #endif
  665. SSL_SESSION *sess1 = NULL, *sess2 = NULL;
  666. int testresult = 0;
  667. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  668. TLS_client_method(), &sctx,
  669. &cctx, cert, privkey)))
  670. return 0;
  671. #ifndef OPENSSL_NO_TLS1_2
  672. /* Only allow TLS1.2 so we can force a connection failure later */
  673. SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);
  674. #endif
  675. /* Set up session cache */
  676. if (fix.use_ext_cache) {
  677. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  678. SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
  679. }
  680. if (fix.use_int_cache) {
  681. /* Also covers instance where both are set */
  682. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
  683. } else {
  684. SSL_CTX_set_session_cache_mode(cctx,
  685. SSL_SESS_CACHE_CLIENT
  686. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  687. }
  688. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  689. NULL, NULL))
  690. || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
  691. SSL_ERROR_NONE))
  692. || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
  693. goto end;
  694. /* Should fail because it should already be in the cache */
  695. if (fix.use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
  696. goto end;
  697. if (fix.use_ext_cache && (new_called != 1 || remove_called != 0))
  698. goto end;
  699. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  700. &clientssl2, NULL, NULL))
  701. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  702. SSL_ERROR_NONE)))
  703. goto end;
  704. if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
  705. goto end;
  706. if (fix.use_ext_cache && (new_called != 2 || remove_called != 0))
  707. goto end;
  708. /*
  709. * This should clear sess2 from the cache because it is a "bad" session.
  710. * See SSL_set_session() documentation.
  711. */
  712. if (!TEST_true(SSL_set_session(clientssl2, sess1)))
  713. goto end;
  714. if (fix.use_ext_cache && (new_called != 2 || remove_called != 1))
  715. goto end;
  716. if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
  717. goto end;
  718. if (fix.use_int_cache) {
  719. /* Should succeeded because it should not already be in the cache */
  720. if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
  721. || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
  722. goto end;
  723. /*
  724. * This is for the purposes of internal cache testing...ignore the
  725. * counter for external cache
  726. */
  727. if (fix.use_ext_cache)
  728. remove_called--;
  729. }
  730. /* This shouldn't be in the cache so should fail */
  731. if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
  732. goto end;
  733. if (fix.use_ext_cache && (new_called != 2 || remove_called != 2))
  734. goto end;
  735. #if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)
  736. /* Force a connection failure */
  737. SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
  738. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
  739. &clientssl3, NULL, NULL))
  740. || !TEST_true(SSL_set_session(clientssl3, sess1))
  741. /* This should fail because of the mismatched protocol versions */
  742. || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
  743. SSL_ERROR_NONE)))
  744. goto end;
  745. /* We should have automatically removed the session from the cache */
  746. if (fix.use_ext_cache && (new_called != 2 || remove_called != 3))
  747. goto end;
  748. /* Should succeed because it should not already be in the cache */
  749. if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2))
  750. goto end;
  751. #endif
  752. testresult = 1;
  753. end:
  754. SSL_free(serverssl1);
  755. SSL_free(clientssl1);
  756. SSL_free(serverssl2);
  757. SSL_free(clientssl2);
  758. #ifndef OPENSSL_NO_TLS1_1
  759. SSL_free(serverssl3);
  760. SSL_free(clientssl3);
  761. #endif
  762. SSL_SESSION_free(sess1);
  763. SSL_SESSION_free(sess2);
  764. /*
  765. * Check if we need to remove any sessions up-refed for the external cache
  766. */
  767. if (new_called >= 1)
  768. SSL_SESSION_free(sess1);
  769. if (new_called >= 2)
  770. SSL_SESSION_free(sess2);
  771. SSL_CTX_free(sctx);
  772. SSL_CTX_free(cctx);
  773. return testresult;
  774. }
  775. static int test_session_with_only_int_cache(void)
  776. {
  777. SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
  778. fixture.use_ext_cache = 0;
  779. EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
  780. }
  781. static int test_session_with_only_ext_cache(void)
  782. {
  783. SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
  784. fixture.use_int_cache = 0;
  785. EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
  786. }
  787. static int test_session_with_both_cache(void)
  788. {
  789. SETUP_TEST_FIXTURE(SSL_SESSION_TEST_FIXTURE, ssl_session_set_up);
  790. EXECUTE_TEST(execute_test_session, ssl_session_tear_down);
  791. }
  792. #define USE_NULL 0
  793. #define USE_BIO_1 1
  794. #define USE_BIO_2 2
  795. #define TOTAL_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
  796. static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
  797. {
  798. switch (type) {
  799. case USE_NULL:
  800. *res = NULL;
  801. break;
  802. case USE_BIO_1:
  803. *res = bio1;
  804. break;
  805. case USE_BIO_2:
  806. *res = bio2;
  807. break;
  808. }
  809. }
  810. static int test_ssl_set_bio(int idx)
  811. {
  812. SSL_CTX *ctx;
  813. BIO *bio1 = NULL;
  814. BIO *bio2 = NULL;
  815. BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
  816. SSL *ssl = NULL;
  817. int initrbio, initwbio, newrbio, newwbio;
  818. int testresult = 0;
  819. initrbio = idx % 3;
  820. idx /= 3;
  821. initwbio = idx % 3;
  822. idx /= 3;
  823. newrbio = idx % 3;
  824. idx /= 3;
  825. newwbio = idx;
  826. if (!TEST_int_le(newwbio, 2))
  827. return 0;
  828. if (!TEST_ptr(ctx = SSL_CTX_new(TLS_method()))
  829. || !TEST_ptr(ssl = SSL_new(ctx)))
  830. goto end;
  831. if (initrbio == USE_BIO_1
  832. || initwbio == USE_BIO_1
  833. || newrbio == USE_BIO_1
  834. || newwbio == USE_BIO_1) {
  835. if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
  836. goto end;
  837. }
  838. if (initrbio == USE_BIO_2
  839. || initwbio == USE_BIO_2
  840. || newrbio == USE_BIO_2
  841. || newwbio == USE_BIO_2) {
  842. if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
  843. goto end;
  844. }
  845. setupbio(&irbio, bio1, bio2, initrbio);
  846. setupbio(&iwbio, bio1, bio2, initwbio);
  847. /*
  848. * We want to maintain our own refs to these BIO, so do an up ref for each
  849. * BIO that will have ownership transferred in the SSL_set_bio() call
  850. */
  851. if (irbio != NULL)
  852. BIO_up_ref(irbio);
  853. if (iwbio != NULL && iwbio != irbio)
  854. BIO_up_ref(iwbio);
  855. SSL_set_bio(ssl, irbio, iwbio);
  856. setupbio(&nrbio, bio1, bio2, newrbio);
  857. setupbio(&nwbio, bio1, bio2, newwbio);
  858. /*
  859. * We will (maybe) transfer ownership again so do more up refs.
  860. * SSL_set_bio() has some really complicated ownership rules where BIOs have
  861. * already been set!
  862. */
  863. if (nrbio != NULL
  864. && nrbio != irbio
  865. && (nwbio != iwbio || nrbio != nwbio))
  866. BIO_up_ref(nrbio);
  867. if (nwbio != NULL
  868. && nwbio != nrbio
  869. && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
  870. BIO_up_ref(nwbio);
  871. SSL_set_bio(ssl, nrbio, nwbio);
  872. testresult = 1;
  873. end:
  874. SSL_free(ssl);
  875. BIO_free(bio1);
  876. BIO_free(bio2);
  877. /*
  878. * This test is checking that the ref counting for SSL_set_bio is correct.
  879. * If we get here and we did too many frees then we will fail in the above
  880. * functions. If we haven't done enough then this will only be detected in
  881. * a crypto-mdebug build
  882. */
  883. SSL_CTX_free(ctx);
  884. return testresult;
  885. }
  886. typedef struct ssl_bio_test_fixture {
  887. const char *test_case_name;
  888. int pop_ssl;
  889. enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } change_bio;
  890. } SSL_BIO_TEST_FIXTURE;
  891. static SSL_BIO_TEST_FIXTURE ssl_bio_set_up(const char *const test_case_name)
  892. {
  893. SSL_BIO_TEST_FIXTURE fixture;
  894. fixture.test_case_name = test_case_name;
  895. fixture.pop_ssl = 0;
  896. fixture.change_bio = NO_BIO_CHANGE;
  897. return fixture;
  898. }
  899. static void ssl_bio_tear_down(SSL_BIO_TEST_FIXTURE fixture)
  900. {
  901. }
  902. static int execute_test_ssl_bio(SSL_BIO_TEST_FIXTURE fix)
  903. {
  904. BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
  905. SSL_CTX *ctx;
  906. SSL *ssl = NULL;
  907. int testresult = 0;
  908. if (!TEST_ptr(ctx = SSL_CTX_new(TLS_method()))
  909. || !TEST_ptr(ssl = SSL_new(ctx))
  910. || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
  911. || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
  912. goto end;
  913. BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
  914. /*
  915. * If anything goes wrong here then we could leak memory, so this will
  916. * be caught in a crypto-mdebug build
  917. */
  918. BIO_push(sslbio, membio1);
  919. /* Verify changing the rbio/wbio directly does not cause leaks */
  920. if (fix.change_bio != NO_BIO_CHANGE) {
  921. if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem())))
  922. goto end;
  923. if (fix.change_bio == CHANGE_RBIO)
  924. SSL_set0_rbio(ssl, membio2);
  925. else
  926. SSL_set0_wbio(ssl, membio2);
  927. }
  928. ssl = NULL;
  929. if (fix.pop_ssl)
  930. BIO_pop(sslbio);
  931. else
  932. BIO_pop(membio1);
  933. testresult = 1;
  934. end:
  935. BIO_free(membio1);
  936. BIO_free(sslbio);
  937. SSL_free(ssl);
  938. SSL_CTX_free(ctx);
  939. return testresult;
  940. }
  941. static int test_ssl_bio_pop_next_bio(void)
  942. {
  943. SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
  944. EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
  945. }
  946. static int test_ssl_bio_pop_ssl_bio(void)
  947. {
  948. SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
  949. fixture.pop_ssl = 1;
  950. EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
  951. }
  952. static int test_ssl_bio_change_rbio(void)
  953. {
  954. SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
  955. fixture.change_bio = CHANGE_RBIO;
  956. EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
  957. }
  958. static int test_ssl_bio_change_wbio(void)
  959. {
  960. SETUP_TEST_FIXTURE(SSL_BIO_TEST_FIXTURE, ssl_bio_set_up);
  961. fixture.change_bio = CHANGE_WBIO;
  962. EXECUTE_TEST(execute_test_ssl_bio, ssl_bio_tear_down);
  963. }
  964. typedef struct {
  965. /* The list of sig algs */
  966. const int *list;
  967. /* The length of the list */
  968. size_t listlen;
  969. /* A sigalgs list in string format */
  970. const char *liststr;
  971. /* Whether setting the list should succeed */
  972. int valid;
  973. /* Whether creating a connection with the list should succeed */
  974. int connsuccess;
  975. } sigalgs_list;
  976. static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
  977. #ifndef OPENSSL_NO_EC
  978. static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
  979. static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
  980. #endif
  981. static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
  982. static const int invalidlist2[] = {NID_sha256, NID_undef};
  983. static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
  984. static const int invalidlist4[] = {NID_sha256};
  985. static const sigalgs_list testsigalgs[] = {
  986. {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
  987. #ifndef OPENSSL_NO_EC
  988. {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
  989. {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
  990. #endif
  991. {NULL, 0, "RSA+SHA256", 1, 1},
  992. #ifndef OPENSSL_NO_EC
  993. {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
  994. {NULL, 0, "ECDSA+SHA512", 1, 0},
  995. #endif
  996. {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
  997. {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
  998. {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
  999. {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
  1000. {NULL, 0, "RSA", 0, 0},
  1001. {NULL, 0, "SHA256", 0, 0},
  1002. {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
  1003. {NULL, 0, "Invalid", 0, 0}
  1004. };
  1005. static int test_set_sigalgs(int idx)
  1006. {
  1007. SSL_CTX *cctx = NULL, *sctx = NULL;
  1008. SSL *clientssl = NULL, *serverssl = NULL;
  1009. int testresult = 0;
  1010. const sigalgs_list *curr;
  1011. int testctx;
  1012. /* Should never happen */
  1013. if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
  1014. return 0;
  1015. testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
  1016. curr = testctx ? &testsigalgs[idx]
  1017. : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
  1018. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  1019. TLS_client_method(), &sctx,
  1020. &cctx, cert, privkey)))
  1021. return 0;
  1022. /*
  1023. * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
  1024. * for TLSv1.2 for now until we add a new API.
  1025. */
  1026. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  1027. if (testctx) {
  1028. int ret;
  1029. if (curr->list != NULL)
  1030. ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
  1031. else
  1032. ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
  1033. if (!ret) {
  1034. if (curr->valid)
  1035. TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
  1036. else
  1037. testresult = 1;
  1038. goto end;
  1039. }
  1040. if (!curr->valid) {
  1041. TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
  1042. goto end;
  1043. }
  1044. }
  1045. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1046. &clientssl, NULL, NULL)))
  1047. goto end;
  1048. if (!testctx) {
  1049. int ret;
  1050. if (curr->list != NULL)
  1051. ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
  1052. else
  1053. ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
  1054. if (!ret) {
  1055. if (curr->valid)
  1056. TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
  1057. else
  1058. testresult = 1;
  1059. goto end;
  1060. }
  1061. if (!curr->valid)
  1062. goto end;
  1063. }
  1064. if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
  1065. SSL_ERROR_NONE),
  1066. curr->connsuccess))
  1067. goto end;
  1068. testresult = 1;
  1069. end:
  1070. SSL_free(serverssl);
  1071. SSL_free(clientssl);
  1072. SSL_CTX_free(sctx);
  1073. SSL_CTX_free(cctx);
  1074. return testresult;
  1075. }
  1076. #ifndef OPENSSL_NO_TLS1_3
  1077. #define MSG1 "Hello"
  1078. #define MSG2 "World."
  1079. #define MSG3 "This"
  1080. #define MSG4 "is"
  1081. #define MSG5 "a"
  1082. #define MSG6 "test"
  1083. #define MSG7 "message."
  1084. /*
  1085. * Helper method to setup objects for early data test. Caller frees objects on
  1086. * error.
  1087. */
  1088. static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
  1089. SSL **serverssl, SSL_SESSION **sess, int idx)
  1090. {
  1091. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  1092. TLS_client_method(), sctx,
  1093. cctx, cert, privkey)))
  1094. return 0;
  1095. /* When idx == 1 we repeat the tests with read_ahead set */
  1096. if (idx > 0) {
  1097. SSL_CTX_set_read_ahead(*cctx, 1);
  1098. SSL_CTX_set_read_ahead(*sctx, 1);
  1099. }
  1100. if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
  1101. NULL, NULL))
  1102. || !TEST_true(create_ssl_connection(*serverssl, *clientssl,
  1103. SSL_ERROR_NONE)))
  1104. return 0;
  1105. *sess = SSL_get1_session(*clientssl);
  1106. SSL_shutdown(*clientssl);
  1107. SSL_shutdown(*serverssl);
  1108. SSL_free(*serverssl);
  1109. SSL_free(*clientssl);
  1110. *serverssl = *clientssl = NULL;
  1111. if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
  1112. clientssl, NULL, NULL))
  1113. || !TEST_true(SSL_set_session(*clientssl, *sess)))
  1114. return 0;
  1115. return 1;
  1116. }
  1117. static int test_early_data_read_write(int idx)
  1118. {
  1119. SSL_CTX *cctx = NULL, *sctx = NULL;
  1120. SSL *clientssl = NULL, *serverssl = NULL;
  1121. int testresult = 0;
  1122. SSL_SESSION *sess = NULL;
  1123. unsigned char buf[20], data[1024];
  1124. size_t readbytes, written, eoedlen, rawread, rawwritten;
  1125. BIO *rbio;
  1126. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  1127. &serverssl, &sess, idx)))
  1128. goto end;
  1129. /* Write and read some early data */
  1130. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  1131. &written))
  1132. || !TEST_size_t_eq(written, strlen(MSG1))
  1133. || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
  1134. sizeof(buf), &readbytes),
  1135. SSL_READ_EARLY_DATA_SUCCESS)
  1136. || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
  1137. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  1138. SSL_EARLY_DATA_ACCEPTED))
  1139. goto end;
  1140. /*
  1141. * Server should be able to write data, and client should be able to
  1142. * read it.
  1143. */
  1144. if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
  1145. &written))
  1146. || !TEST_size_t_eq(written, strlen(MSG2))
  1147. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  1148. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  1149. goto end;
  1150. /* Even after reading normal data, client should be able write early data */
  1151. if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
  1152. &written))
  1153. || !TEST_size_t_eq(written, strlen(MSG3)))
  1154. goto end;
  1155. /* Server should still be able read early data after writing data */
  1156. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  1157. &readbytes),
  1158. SSL_READ_EARLY_DATA_SUCCESS)
  1159. || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
  1160. goto end;
  1161. /* Write more data from server and read it from client */
  1162. if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
  1163. &written))
  1164. || !TEST_size_t_eq(written, strlen(MSG4))
  1165. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  1166. || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
  1167. goto end;
  1168. /*
  1169. * If client writes normal data it should mean writing early data is no
  1170. * longer possible.
  1171. */
  1172. if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
  1173. || !TEST_size_t_eq(written, strlen(MSG5))
  1174. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  1175. SSL_EARLY_DATA_ACCEPTED))
  1176. goto end;
  1177. /*
  1178. * At this point the client has written EndOfEarlyData, ClientFinished and
  1179. * normal (fully protected) data. We are going to cause a delay between the
  1180. * arrival of EndOfEarlyData and ClientFinished. We read out all the data
  1181. * in the read BIO, and then just put back the EndOfEarlyData message.
  1182. */
  1183. rbio = SSL_get_rbio(serverssl);
  1184. if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
  1185. || !TEST_size_t_lt(rawread, sizeof(data))
  1186. || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
  1187. goto end;
  1188. /* Record length is in the 4th and 5th bytes of the record header */
  1189. eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
  1190. if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
  1191. || !TEST_size_t_eq(rawwritten, eoedlen))
  1192. goto end;
  1193. /* Server should be told that there is no more early data */
  1194. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  1195. &readbytes),
  1196. SSL_READ_EARLY_DATA_FINISH)
  1197. || !TEST_size_t_eq(readbytes, 0))
  1198. goto end;
  1199. /*
  1200. * Server has not finished init yet, so should still be able to write early
  1201. * data.
  1202. */
  1203. if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
  1204. &written))
  1205. || !TEST_size_t_eq(written, strlen(MSG6)))
  1206. goto end;
  1207. /* Push the ClientFinished and the normal data back into the server rbio */
  1208. if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
  1209. &rawwritten))
  1210. || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
  1211. goto end;
  1212. /* Server should be able to read normal data */
  1213. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  1214. || !TEST_size_t_eq(readbytes, strlen(MSG5)))
  1215. goto end;
  1216. /* Client and server should not be able to write/read early data now */
  1217. if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
  1218. &written)))
  1219. goto end;
  1220. ERR_clear_error();
  1221. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  1222. &readbytes),
  1223. SSL_READ_EARLY_DATA_ERROR))
  1224. goto end;
  1225. ERR_clear_error();
  1226. /* Client should be able to read the data sent by the server */
  1227. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  1228. || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
  1229. goto end;
  1230. /*
  1231. * Make sure we process the NewSessionTicket. This arrives post-handshake.
  1232. * We attempt a read which we do not expect to return any data.
  1233. */
  1234. if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)))
  1235. goto end;
  1236. /* Server should be able to write normal data */
  1237. if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
  1238. || !TEST_size_t_eq(written, strlen(MSG7))
  1239. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  1240. || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
  1241. goto end;
  1242. SSL_SESSION_free(sess);
  1243. sess = SSL_get1_session(clientssl);
  1244. SSL_shutdown(clientssl);
  1245. SSL_shutdown(serverssl);
  1246. SSL_free(serverssl);
  1247. SSL_free(clientssl);
  1248. serverssl = clientssl = NULL;
  1249. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1250. &clientssl, NULL, NULL))
  1251. || !TEST_true(SSL_set_session(clientssl, sess)))
  1252. goto end;
  1253. /* Write and read some early data */
  1254. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  1255. &written))
  1256. || !TEST_size_t_eq(written, strlen(MSG1))
  1257. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  1258. &readbytes),
  1259. SSL_READ_EARLY_DATA_SUCCESS)
  1260. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
  1261. goto end;
  1262. if (!TEST_int_gt(SSL_connect(clientssl), 0)
  1263. || !TEST_int_gt(SSL_accept(serverssl), 0))
  1264. goto end;
  1265. /* Client and server should not be able to write/read early data now */
  1266. if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
  1267. &written)))
  1268. goto end;
  1269. ERR_clear_error();
  1270. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  1271. &readbytes),
  1272. SSL_READ_EARLY_DATA_ERROR))
  1273. goto end;
  1274. ERR_clear_error();
  1275. /* Client and server should be able to write/read normal data */
  1276. if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
  1277. || !TEST_size_t_eq(written, strlen(MSG5))
  1278. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  1279. || !TEST_size_t_eq(readbytes, strlen(MSG5)))
  1280. goto end;
  1281. testresult = 1;
  1282. end:
  1283. SSL_SESSION_free(sess);
  1284. SSL_free(serverssl);
  1285. SSL_free(clientssl);
  1286. SSL_CTX_free(sctx);
  1287. SSL_CTX_free(cctx);
  1288. return testresult;
  1289. }
  1290. /*
  1291. * Test that a server attempting to read early data can handle a connection
  1292. * from a client where the early data is not acceptable.
  1293. */
  1294. static int test_early_data_skip(int idx)
  1295. {
  1296. SSL_CTX *cctx = NULL, *sctx = NULL;
  1297. SSL *clientssl = NULL, *serverssl = NULL;
  1298. int testresult = 0;
  1299. SSL_SESSION *sess;
  1300. unsigned char buf[20];
  1301. size_t readbytes, written;
  1302. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  1303. &serverssl, &sess, idx)))
  1304. goto end;
  1305. /*
  1306. * Deliberately corrupt the creation time. We take 20 seconds off the time.
  1307. * It could be any value as long as it is not within tolerance. This should
  1308. * mean the ticket is rejected.
  1309. */
  1310. if (!TEST_true(SSL_SESSION_set_time(sess, time(NULL) - 20)))
  1311. goto end;
  1312. /* Write some early data */
  1313. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  1314. &written))
  1315. || !TEST_size_t_eq(written, strlen(MSG1)))
  1316. goto end;
  1317. /* Server should reject the early data and skip over it */
  1318. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  1319. &readbytes),
  1320. SSL_READ_EARLY_DATA_FINISH)
  1321. || !TEST_size_t_eq(readbytes, 0)
  1322. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  1323. SSL_EARLY_DATA_REJECTED))
  1324. goto end;
  1325. /* Should be able to send normal data despite rejection of early data */
  1326. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  1327. || !TEST_size_t_eq(written, strlen(MSG2))
  1328. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  1329. SSL_EARLY_DATA_REJECTED)
  1330. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  1331. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  1332. goto end;
  1333. testresult = 1;
  1334. end:
  1335. SSL_SESSION_free(sess);
  1336. SSL_free(serverssl);
  1337. SSL_free(clientssl);
  1338. SSL_CTX_free(sctx);
  1339. SSL_CTX_free(cctx);
  1340. return testresult;
  1341. }
  1342. /*
  1343. * Test that a server attempting to read early data can handle a connection
  1344. * from a client that doesn't send any.
  1345. */
  1346. static int test_early_data_not_sent(int idx)
  1347. {
  1348. SSL_CTX *cctx = NULL, *sctx = NULL;
  1349. SSL *clientssl = NULL, *serverssl = NULL;
  1350. int testresult = 0;
  1351. SSL_SESSION *sess;
  1352. unsigned char buf[20];
  1353. size_t readbytes, written;
  1354. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  1355. &serverssl, &sess, idx)))
  1356. goto end;
  1357. /* Write some data - should block due to handshake with server */
  1358. SSL_set_connect_state(clientssl);
  1359. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
  1360. goto end;
  1361. /* Server should detect that early data has not been sent */
  1362. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  1363. &readbytes),
  1364. SSL_READ_EARLY_DATA_FINISH)
  1365. || !TEST_size_t_eq(readbytes, 0)
  1366. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  1367. SSL_EARLY_DATA_NOT_SENT)
  1368. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  1369. SSL_EARLY_DATA_NOT_SENT))
  1370. goto end;
  1371. /* Continue writing the message we started earlier */
  1372. if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  1373. || !TEST_size_t_eq(written, strlen(MSG1))
  1374. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  1375. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  1376. || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
  1377. || !TEST_size_t_eq(written, strlen(MSG2)))
  1378. goto end;
  1379. /*
  1380. * Should block due to the NewSessionTicket arrival unless we're using
  1381. * read_ahead
  1382. */
  1383. if (idx == 0) {
  1384. if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)))
  1385. goto end;
  1386. }
  1387. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  1388. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  1389. goto end;
  1390. testresult = 1;
  1391. end:
  1392. SSL_SESSION_free(sess);
  1393. SSL_free(serverssl);
  1394. SSL_free(clientssl);
  1395. SSL_CTX_free(sctx);
  1396. SSL_CTX_free(cctx);
  1397. return testresult;
  1398. }
  1399. /*
  1400. * Test that a server that doesn't try to read early data can handle a
  1401. * client sending some.
  1402. */
  1403. static int test_early_data_not_expected(int idx)
  1404. {
  1405. SSL_CTX *cctx = NULL, *sctx = NULL;
  1406. SSL *clientssl = NULL, *serverssl = NULL;
  1407. int testresult = 0;
  1408. SSL_SESSION *sess;
  1409. unsigned char buf[20];
  1410. size_t readbytes, written;
  1411. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  1412. &serverssl, &sess, idx)))
  1413. goto end;
  1414. /* Write some early data */
  1415. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  1416. &written)))
  1417. goto end;
  1418. /*
  1419. * Server should skip over early data and then block waiting for client to
  1420. * continue handshake
  1421. */
  1422. if (!TEST_int_le(SSL_accept(serverssl), 0)
  1423. || !TEST_int_gt(SSL_connect(clientssl), 0)
  1424. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  1425. SSL_EARLY_DATA_REJECTED)
  1426. || !TEST_int_gt(SSL_accept(serverssl), 0)
  1427. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  1428. SSL_EARLY_DATA_REJECTED))
  1429. goto end;
  1430. /* Send some normal data from client to server */
  1431. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  1432. || !TEST_size_t_eq(written, strlen(MSG2)))
  1433. goto end;
  1434. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  1435. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  1436. goto end;
  1437. testresult = 1;
  1438. end:
  1439. SSL_SESSION_free(sess);
  1440. SSL_free(serverssl);
  1441. SSL_free(clientssl);
  1442. SSL_CTX_free(sctx);
  1443. SSL_CTX_free(cctx);
  1444. return testresult;
  1445. }
  1446. # ifndef OPENSSL_NO_TLS1_2
  1447. /*
  1448. * Test that a server attempting to read early data can handle a connection
  1449. * from a TLSv1.2 client.
  1450. */
  1451. static int test_early_data_tls1_2(int idx)
  1452. {
  1453. SSL_CTX *cctx = NULL, *sctx = NULL;
  1454. SSL *clientssl = NULL, *serverssl = NULL;
  1455. int testresult = 0;
  1456. unsigned char buf[20];
  1457. size_t readbytes, written;
  1458. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  1459. TLS_client_method(), &sctx,
  1460. &cctx, cert, privkey)))
  1461. goto end;
  1462. /* When idx == 1 we repeat the tests with read_ahead set */
  1463. if (idx > 0) {
  1464. SSL_CTX_set_read_ahead(cctx, 1);
  1465. SSL_CTX_set_read_ahead(sctx, 1);
  1466. }
  1467. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1468. &clientssl, NULL, NULL)))
  1469. goto end;
  1470. /* Write some data - should block due to handshake with server */
  1471. SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
  1472. SSL_set_connect_state(clientssl);
  1473. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
  1474. goto end;
  1475. /*
  1476. * Server should do TLSv1.2 handshake. First it will block waiting for more
  1477. * messages from client after ServerDone. Then SSL_read_early_data should
  1478. * finish and detect that early data has not been sent
  1479. */
  1480. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  1481. &readbytes),
  1482. SSL_READ_EARLY_DATA_ERROR))
  1483. goto end;
  1484. /*
  1485. * Continue writing the message we started earlier. Will still block waiting
  1486. * for the CCS/Finished from server
  1487. */
  1488. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  1489. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  1490. &readbytes),
  1491. SSL_READ_EARLY_DATA_FINISH)
  1492. || !TEST_size_t_eq(readbytes, 0)
  1493. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  1494. SSL_EARLY_DATA_NOT_SENT))
  1495. goto end;
  1496. /* Continue writing the message we started earlier */
  1497. if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  1498. || !TEST_size_t_eq(written, strlen(MSG1))
  1499. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  1500. SSL_EARLY_DATA_NOT_SENT)
  1501. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  1502. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  1503. || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
  1504. || !TEST_size_t_eq(written, strlen(MSG2))
  1505. || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
  1506. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  1507. goto end;
  1508. testresult = 1;
  1509. end:
  1510. SSL_free(serverssl);
  1511. SSL_free(clientssl);
  1512. SSL_CTX_free(sctx);
  1513. SSL_CTX_free(cctx);
  1514. return testresult;
  1515. }
  1516. # endif
  1517. #endif
  1518. static int clntaddoldcb = 0;
  1519. static int clntparseoldcb = 0;
  1520. static int srvaddoldcb = 0;
  1521. static int srvparseoldcb = 0;
  1522. static int clntaddnewcb = 0;
  1523. static int clntparsenewcb = 0;
  1524. static int srvaddnewcb = 0;
  1525. static int srvparsenewcb = 0;
  1526. #define TEST_EXT_TYPE1 0xff00
  1527. static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
  1528. size_t *outlen, int *al, void *add_arg)
  1529. {
  1530. int *server = (int *)add_arg;
  1531. unsigned char *data;
  1532. if (SSL_is_server(s))
  1533. srvaddoldcb++;
  1534. else
  1535. clntaddoldcb++;
  1536. if (*server != SSL_is_server(s)
  1537. || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
  1538. return -1;
  1539. *data = 1;
  1540. *out = data;
  1541. *outlen = sizeof(char);
  1542. return 1;
  1543. }
  1544. static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
  1545. void *add_arg)
  1546. {
  1547. OPENSSL_free((unsigned char *)out);
  1548. }
  1549. static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
  1550. size_t inlen, int *al, void *parse_arg)
  1551. {
  1552. int *server = (int *)parse_arg;
  1553. if (SSL_is_server(s))
  1554. srvparseoldcb++;
  1555. else
  1556. clntparseoldcb++;
  1557. if (*server != SSL_is_server(s)
  1558. || inlen != sizeof(char)
  1559. || *in != 1)
  1560. return -1;
  1561. return 1;
  1562. }
  1563. static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
  1564. const unsigned char **out, size_t *outlen, X509 *x,
  1565. size_t chainidx, int *al, void *add_arg)
  1566. {
  1567. int *server = (int *)add_arg;
  1568. unsigned char *data;
  1569. if (SSL_is_server(s))
  1570. srvaddnewcb++;
  1571. else
  1572. clntaddnewcb++;
  1573. if (*server != SSL_is_server(s)
  1574. || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
  1575. return -1;
  1576. *data = 1;
  1577. *out = data;
  1578. *outlen = sizeof(*data);
  1579. return 1;
  1580. }
  1581. static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
  1582. const unsigned char *out, void *add_arg)
  1583. {
  1584. OPENSSL_free((unsigned char *)out);
  1585. }
  1586. static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
  1587. const unsigned char *in, size_t inlen, X509 *x,
  1588. size_t chainidx, int *al, void *parse_arg)
  1589. {
  1590. int *server = (int *)parse_arg;
  1591. if (SSL_is_server(s))
  1592. srvparsenewcb++;
  1593. else
  1594. clntparsenewcb++;
  1595. if (*server != SSL_is_server(s)
  1596. || inlen != sizeof(char) || *in != 1)
  1597. return -1;
  1598. return 1;
  1599. }
  1600. /*
  1601. * Custom call back tests.
  1602. * Test 0: Old style callbacks in TLSv1.2
  1603. * Test 1: New style callbacks in TLSv1.2
  1604. * Test 2: New style callbacks in TLSv1.3. Extensions in CH and EE
  1605. * Test 3: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
  1606. */
  1607. static int test_custom_exts(int tst)
  1608. {
  1609. SSL_CTX *cctx = NULL, *sctx = NULL;
  1610. SSL *clientssl = NULL, *serverssl = NULL;
  1611. int testresult = 0;
  1612. static int server = 1;
  1613. static int client = 0;
  1614. SSL_SESSION *sess = NULL;
  1615. unsigned int context;
  1616. /* Reset callback counters */
  1617. clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
  1618. clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
  1619. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  1620. TLS_client_method(), &sctx,
  1621. &cctx, cert, privkey)))
  1622. goto end;
  1623. if (tst < 2) {
  1624. SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
  1625. SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
  1626. }
  1627. if (tst == 3) {
  1628. context = SSL_EXT_CLIENT_HELLO
  1629. | SSL_EXT_TLS1_2_SERVER_HELLO
  1630. | SSL_EXT_TLS1_3_SERVER_HELLO
  1631. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  1632. | SSL_EXT_TLS1_3_CERTIFICATE
  1633. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
  1634. } else {
  1635. context = SSL_EXT_CLIENT_HELLO
  1636. | SSL_EXT_TLS1_2_SERVER_HELLO
  1637. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
  1638. }
  1639. /* Create a client side custom extension */
  1640. if (tst == 0) {
  1641. if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
  1642. old_add_cb, old_free_cb,
  1643. &client, old_parse_cb,
  1644. &client)))
  1645. goto end;
  1646. } else {
  1647. if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
  1648. new_add_cb, new_free_cb,
  1649. &client, new_parse_cb, &client)))
  1650. goto end;
  1651. }
  1652. /* Should not be able to add duplicates */
  1653. if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
  1654. old_add_cb, old_free_cb,
  1655. &client, old_parse_cb,
  1656. &client))
  1657. || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
  1658. context, new_add_cb,
  1659. new_free_cb, &client,
  1660. new_parse_cb, &client)))
  1661. goto end;
  1662. /* Create a server side custom extension */
  1663. if (tst == 0) {
  1664. if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
  1665. old_add_cb, old_free_cb,
  1666. &server, old_parse_cb,
  1667. &server)))
  1668. goto end;
  1669. } else {
  1670. if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
  1671. new_add_cb, new_free_cb,
  1672. &server, new_parse_cb, &server)))
  1673. goto end;
  1674. }
  1675. /* Should not be able to add duplicates */
  1676. if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
  1677. old_add_cb, old_free_cb,
  1678. &server, old_parse_cb,
  1679. &server))
  1680. || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
  1681. context, new_add_cb,
  1682. new_free_cb, &server,
  1683. new_parse_cb, &server)))
  1684. goto end;
  1685. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1686. &clientssl, NULL, NULL))
  1687. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  1688. SSL_ERROR_NONE)))
  1689. goto end;
  1690. if (tst == 0) {
  1691. if (clntaddoldcb != 1
  1692. || clntparseoldcb != 1
  1693. || srvaddoldcb != 1
  1694. || srvparseoldcb != 1)
  1695. goto end;
  1696. } else if (tst == 1 || tst == 2) {
  1697. if (clntaddnewcb != 1
  1698. || clntparsenewcb != 1
  1699. || srvaddnewcb != 1
  1700. || srvparsenewcb != 1)
  1701. goto end;
  1702. } else {
  1703. if (clntaddnewcb != 1
  1704. || clntparsenewcb != 4
  1705. || srvaddnewcb != 4
  1706. || srvparsenewcb != 1)
  1707. goto end;
  1708. }
  1709. sess = SSL_get1_session(clientssl);
  1710. SSL_shutdown(clientssl);
  1711. SSL_shutdown(serverssl);
  1712. SSL_free(serverssl);
  1713. SSL_free(clientssl);
  1714. serverssl = clientssl = NULL;
  1715. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  1716. NULL, NULL))
  1717. || !TEST_true(SSL_set_session(clientssl, sess))
  1718. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  1719. SSL_ERROR_NONE)))
  1720. goto end;
  1721. /*
  1722. * For a resumed session we expect to add the ClientHello extension. For the
  1723. * old style callbacks we ignore it on the server side because they set
  1724. * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
  1725. * them.
  1726. */
  1727. if (tst == 0) {
  1728. if (clntaddoldcb != 2
  1729. || clntparseoldcb != 1
  1730. || srvaddoldcb != 1
  1731. || srvparseoldcb != 1)
  1732. goto end;
  1733. } else if (tst == 1 || tst == 2) {
  1734. if (clntaddnewcb != 2
  1735. || clntparsenewcb != 2
  1736. || srvaddnewcb != 2
  1737. || srvparsenewcb != 2)
  1738. goto end;
  1739. } else {
  1740. /* No Certificate message extensions in the resumption handshake */
  1741. if (clntaddnewcb != 2
  1742. || clntparsenewcb != 7
  1743. || srvaddnewcb != 7
  1744. || srvparsenewcb != 2)
  1745. goto end;
  1746. }
  1747. testresult = 1;
  1748. end:
  1749. SSL_SESSION_free(sess);
  1750. SSL_free(serverssl);
  1751. SSL_free(clientssl);
  1752. SSL_CTX_free(sctx);
  1753. SSL_CTX_free(cctx);
  1754. return testresult;
  1755. }
  1756. int test_main(int argc, char *argv[])
  1757. {
  1758. int testresult = 1;
  1759. if (argc != 3) {
  1760. TEST_error("Wrong argument count");
  1761. return 0;
  1762. }
  1763. cert = argv[1];
  1764. privkey = argv[2];
  1765. ADD_TEST(test_large_message_tls);
  1766. ADD_TEST(test_large_message_tls_read_ahead);
  1767. #ifndef OPENSSL_NO_DTLS
  1768. ADD_TEST(test_large_message_dtls);
  1769. #endif
  1770. #ifndef OPENSSL_NO_OCSP
  1771. ADD_TEST(test_tlsext_status_type);
  1772. #endif
  1773. ADD_TEST(test_session_with_only_int_cache);
  1774. ADD_TEST(test_session_with_only_ext_cache);
  1775. ADD_TEST(test_session_with_both_cache);
  1776. ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
  1777. ADD_TEST(test_ssl_bio_pop_next_bio);
  1778. ADD_TEST(test_ssl_bio_pop_ssl_bio);
  1779. ADD_TEST(test_ssl_bio_change_rbio);
  1780. ADD_TEST(test_ssl_bio_change_wbio);
  1781. ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
  1782. ADD_TEST(test_keylog);
  1783. #ifndef OPENSSL_NO_TLS1_3
  1784. ADD_TEST(test_keylog_no_master_key);
  1785. #endif
  1786. #ifndef OPENSSL_NO_TLS1_2
  1787. ADD_TEST(test_early_cb);
  1788. #endif
  1789. #ifndef OPENSSL_NO_TLS1_3
  1790. ADD_ALL_TESTS(test_early_data_read_write, 2);
  1791. ADD_ALL_TESTS(test_early_data_skip, 2);
  1792. ADD_ALL_TESTS(test_early_data_not_sent, 2);
  1793. ADD_ALL_TESTS(test_early_data_not_expected, 2);
  1794. # ifndef OPENSSL_NO_TLS1_2
  1795. ADD_ALL_TESTS(test_early_data_tls1_2, 2);
  1796. # endif
  1797. #endif
  1798. #ifndef OPENSSL_NO_TLS1_3
  1799. ADD_ALL_TESTS(test_custom_exts, 4);
  1800. #else
  1801. ADD_ALL_TESTS(test_custom_exts, 2);
  1802. #endif
  1803. testresult = run_tests(argv[0]);
  1804. bio_s_mempacket_test_free();
  1805. return testresult;
  1806. }