quicapitest.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /*
  2. * Copyright 2022-2023 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 <stdio.h>
  10. #include <string.h>
  11. #include <openssl/opensslconf.h>
  12. #include <openssl/quic.h>
  13. #include <openssl/rand.h>
  14. #include "helpers/ssltestlib.h"
  15. #include "helpers/quictestlib.h"
  16. #include "testutil.h"
  17. #include "testutil/output.h"
  18. #include "../ssl/ssl_local.h"
  19. static OSSL_LIB_CTX *libctx = NULL;
  20. static OSSL_PROVIDER *defctxnull = NULL;
  21. static char *certsdir = NULL;
  22. static char *cert = NULL;
  23. static char *ccert = NULL;
  24. static char *cauthca = NULL;
  25. static char *privkey = NULL;
  26. static char *cprivkey = NULL;
  27. static char *datadir = NULL;
  28. static int is_fips = 0;
  29. /* The ssltrace test assumes some options are switched on/off */
  30. #if !defined(OPENSSL_NO_SSL_TRACE) \
  31. && defined(OPENSSL_NO_BROTLI) && defined(OPENSSL_NO_ZSTD) \
  32. && !defined(OPENSSL_NO_ECX) && !defined(OPENSSL_NO_DH)
  33. # define DO_SSL_TRACE_TEST
  34. #endif
  35. /*
  36. * Test that we read what we've written.
  37. * Test 0: Non-blocking
  38. * Test 1: Blocking
  39. * Test 2: Blocking, introduce socket error, test error handling.
  40. */
  41. static int test_quic_write_read(int idx)
  42. {
  43. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  44. SSL_CTX *sctx = NULL;
  45. SSL *clientquic = NULL;
  46. QUIC_TSERVER *qtserv = NULL;
  47. int j, k, ret = 0;
  48. unsigned char buf[20];
  49. static char *msg = "A test message";
  50. size_t msglen = strlen(msg);
  51. size_t numbytes = 0;
  52. int ssock = 0, csock = 0;
  53. uint64_t sid = UINT64_MAX;
  54. SSL_SESSION *sess = NULL;
  55. if (idx >= 1 && !qtest_supports_blocking())
  56. return TEST_skip("Blocking tests not supported in this build");
  57. for (k = 0; k < 2; k++) {
  58. if (!TEST_ptr(cctx)
  59. || !TEST_true(qtest_create_quic_objects(libctx, cctx, sctx,
  60. cert, privkey,
  61. idx >= 1
  62. ? QTEST_FLAG_BLOCK
  63. : 0,
  64. &qtserv, &clientquic,
  65. NULL, NULL))
  66. || !TEST_true(SSL_set_tlsext_host_name(clientquic, "localhost")))
  67. goto end;
  68. if (sess != NULL && !TEST_true(SSL_set_session(clientquic, sess)))
  69. goto end;
  70. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  71. goto end;
  72. if (idx >= 1) {
  73. if (!TEST_true(BIO_get_fd(ossl_quic_tserver_get0_rbio(qtserv),
  74. &ssock)))
  75. goto end;
  76. if (!TEST_int_gt(csock = SSL_get_rfd(clientquic), 0))
  77. goto end;
  78. }
  79. sid = 0; /* client-initiated bidirectional stream */
  80. for (j = 0; j < 2; j++) {
  81. /* Check that sending and receiving app data is ok */
  82. if (!TEST_true(SSL_write_ex(clientquic, msg, msglen, &numbytes))
  83. || !TEST_size_t_eq(numbytes, msglen))
  84. goto end;
  85. if (idx >= 1) {
  86. do {
  87. if (!TEST_true(wait_until_sock_readable(ssock)))
  88. goto end;
  89. ossl_quic_tserver_tick(qtserv);
  90. if (!TEST_true(ossl_quic_tserver_read(qtserv, sid, buf,
  91. sizeof(buf),
  92. &numbytes)))
  93. goto end;
  94. } while (numbytes == 0);
  95. if (!TEST_mem_eq(buf, numbytes, msg, msglen))
  96. goto end;
  97. }
  98. if (idx >= 2 && j > 0)
  99. /* Introduce permanent socket error */
  100. BIO_closesocket(csock);
  101. ossl_quic_tserver_tick(qtserv);
  102. if (!TEST_true(ossl_quic_tserver_write(qtserv, sid,
  103. (unsigned char *)msg,
  104. msglen, &numbytes)))
  105. goto end;
  106. ossl_quic_tserver_tick(qtserv);
  107. SSL_handle_events(clientquic);
  108. if (idx >= 2 && j > 0) {
  109. if (!TEST_false(SSL_read_ex(clientquic, buf, 1, &numbytes))
  110. || !TEST_int_eq(SSL_get_error(clientquic, 0),
  111. SSL_ERROR_SYSCALL)
  112. || !TEST_false(SSL_write_ex(clientquic, msg, msglen,
  113. &numbytes))
  114. || !TEST_int_eq(SSL_get_error(clientquic, 0),
  115. SSL_ERROR_SYSCALL))
  116. goto end;
  117. break;
  118. }
  119. /*
  120. * In blocking mode the SSL_read_ex call will block until the socket
  121. * is readable and has our data. In non-blocking mode we're doing
  122. * everything in memory, so it should be immediately available
  123. */
  124. if (!TEST_true(SSL_read_ex(clientquic, buf, 1, &numbytes))
  125. || !TEST_size_t_eq(numbytes, 1)
  126. || !TEST_true(SSL_has_pending(clientquic))
  127. || !TEST_int_eq(SSL_pending(clientquic), msglen - 1)
  128. || !TEST_true(SSL_read_ex(clientquic, buf + 1,
  129. sizeof(buf) - 1, &numbytes))
  130. || !TEST_mem_eq(buf, numbytes + 1, msg, msglen))
  131. goto end;
  132. }
  133. if (sess == NULL) {
  134. /* We didn't supply a session so we're not expecting resumption */
  135. if (!TEST_false(SSL_session_reused(clientquic)))
  136. goto end;
  137. /* We should have a session ticket by now */
  138. sess = SSL_get1_session(clientquic);
  139. if (!TEST_ptr(sess))
  140. goto end;
  141. } else {
  142. /* We supplied a session so we should have resumed */
  143. if (!TEST_true(SSL_session_reused(clientquic)))
  144. goto end;
  145. }
  146. if (!TEST_true(qtest_shutdown(qtserv, clientquic)))
  147. goto end;
  148. if (sctx == NULL) {
  149. sctx = ossl_quic_tserver_get0_ssl_ctx(qtserv);
  150. if (!TEST_true(SSL_CTX_up_ref(sctx))) {
  151. sctx = NULL;
  152. goto end;
  153. }
  154. }
  155. ossl_quic_tserver_free(qtserv);
  156. qtserv = NULL;
  157. SSL_free(clientquic);
  158. clientquic = NULL;
  159. if (idx >= 2)
  160. break;
  161. }
  162. ret = 1;
  163. end:
  164. SSL_SESSION_free(sess);
  165. ossl_quic_tserver_free(qtserv);
  166. SSL_free(clientquic);
  167. SSL_CTX_free(cctx);
  168. SSL_CTX_free(sctx);
  169. return ret;
  170. }
  171. /*
  172. * Test that sending FIN with no data to a client blocking in SSL_read_ex() will
  173. * wake up the client.
  174. */
  175. static int test_fin_only_blocking(void)
  176. {
  177. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  178. SSL_CTX *sctx = NULL;
  179. SSL *clientquic = NULL;
  180. QUIC_TSERVER *qtserv = NULL;
  181. const char *msg = "Hello World";
  182. uint64_t sid;
  183. size_t numbytes;
  184. unsigned char buf[32];
  185. int ret = 0;
  186. OSSL_TIME timer, timediff;
  187. if (!qtest_supports_blocking())
  188. return TEST_skip("Blocking tests not supported in this build");
  189. if (!TEST_ptr(cctx)
  190. || !TEST_true(qtest_create_quic_objects(libctx, cctx, sctx,
  191. cert, privkey,
  192. QTEST_FLAG_BLOCK,
  193. &qtserv, &clientquic,
  194. NULL, NULL))
  195. || !TEST_true(SSL_set_tlsext_host_name(clientquic, "localhost")))
  196. goto end;
  197. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  198. goto end;
  199. if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, 0, &sid))
  200. || !TEST_true(ossl_quic_tserver_write(qtserv, sid,
  201. (unsigned char *)msg,
  202. strlen(msg), &numbytes))
  203. || !TEST_size_t_eq(strlen(msg), numbytes))
  204. goto end;
  205. ossl_quic_tserver_tick(qtserv);
  206. if (!TEST_true(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes))
  207. || !TEST_mem_eq(msg, strlen(msg), buf, numbytes))
  208. goto end;
  209. if (!TEST_true(ossl_quic_tserver_conclude(qtserv, sid)))
  210. goto end;
  211. timer = ossl_time_now();
  212. if (!TEST_false(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes)))
  213. goto end;
  214. timediff = ossl_time_subtract(ossl_time_now(), timer);
  215. if (!TEST_int_eq(SSL_get_error(clientquic, 0), SSL_ERROR_ZERO_RETURN)
  216. /*
  217. * We expect the SSL_read_ex to not have blocked so this should
  218. * be very fast. 20ms should be plenty.
  219. */
  220. || !TEST_uint64_t_le(ossl_time2ms(timediff), 20))
  221. goto end;
  222. if (!TEST_true(qtest_shutdown(qtserv, clientquic)))
  223. goto end;
  224. ret = 1;
  225. end:
  226. ossl_quic_tserver_free(qtserv);
  227. SSL_free(clientquic);
  228. SSL_CTX_free(cctx);
  229. SSL_CTX_free(sctx);
  230. return ret;
  231. }
  232. /* Test that a vanilla QUIC SSL object has the expected ciphersuites available */
  233. static int test_ciphersuites(void)
  234. {
  235. SSL_CTX *ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  236. SSL *ssl;
  237. int testresult = 0;
  238. const STACK_OF(SSL_CIPHER) *ciphers = NULL;
  239. const SSL_CIPHER *cipher;
  240. /* We expect this exact list of ciphersuites by default */
  241. int cipherids[] = {
  242. TLS1_3_CK_AES_256_GCM_SHA384,
  243. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  244. TLS1_3_CK_CHACHA20_POLY1305_SHA256,
  245. #endif
  246. TLS1_3_CK_AES_128_GCM_SHA256
  247. };
  248. size_t i, j;
  249. if (!TEST_ptr(ctx))
  250. return 0;
  251. ssl = SSL_new(ctx);
  252. if (!TEST_ptr(ssl))
  253. goto err;
  254. ciphers = SSL_get_ciphers(ssl);
  255. for (i = 0, j = 0; i < OSSL_NELEM(cipherids); i++) {
  256. if (cipherids[i] == TLS1_3_CK_CHACHA20_POLY1305_SHA256 && is_fips)
  257. continue;
  258. cipher = sk_SSL_CIPHER_value(ciphers, j++);
  259. if (!TEST_ptr(cipher))
  260. goto err;
  261. if (!TEST_uint_eq(SSL_CIPHER_get_id(cipher), cipherids[i]))
  262. goto err;
  263. }
  264. /* We should have checked all the ciphers in the stack */
  265. if (!TEST_int_eq(sk_SSL_CIPHER_num(ciphers), j))
  266. goto err;
  267. testresult = 1;
  268. err:
  269. SSL_free(ssl);
  270. SSL_CTX_free(ctx);
  271. return testresult;
  272. }
  273. static int test_cipher_find(void)
  274. {
  275. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  276. SSL *clientquic = NULL;
  277. struct {
  278. const unsigned char *cipherbytes;
  279. int ok;
  280. } testciphers[] = {
  281. { TLS13_AES_128_GCM_SHA256_BYTES, 1 },
  282. { TLS13_AES_256_GCM_SHA384_BYTES, 1 },
  283. { TLS13_CHACHA20_POLY1305_SHA256_BYTES, 1 },
  284. { TLS13_AES_128_CCM_SHA256_BYTES, 0 },
  285. { TLS13_AES_128_CCM_8_SHA256_BYTES, 0 }
  286. };
  287. size_t i;
  288. int testresult = 0;
  289. if (!TEST_ptr(cctx))
  290. goto err;
  291. clientquic = SSL_new(cctx);
  292. if (!TEST_ptr(clientquic))
  293. goto err;
  294. for (i = 0; i < OSSL_NELEM(testciphers); i++)
  295. if (testciphers[i].ok) {
  296. if (!TEST_ptr(SSL_CIPHER_find(clientquic,
  297. testciphers[i].cipherbytes)))
  298. goto err;
  299. } else {
  300. if (!TEST_ptr_null(SSL_CIPHER_find(clientquic,
  301. testciphers[i].cipherbytes)))
  302. goto err;
  303. }
  304. testresult = 1;
  305. err:
  306. SSL_free(clientquic);
  307. SSL_CTX_free(cctx);
  308. return testresult;
  309. }
  310. /*
  311. * Test that SSL_version, SSL_get_version, SSL_is_quic, SSL_is_tls and
  312. * SSL_is_dtls return the expected results for a QUIC connection. Compare with
  313. * test_version() in sslapitest.c which does the same thing for TLS/DTLS
  314. * connections.
  315. */
  316. static int test_version(void)
  317. {
  318. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  319. SSL *clientquic = NULL;
  320. QUIC_TSERVER *qtserv = NULL;
  321. int testresult = 0;
  322. if (!TEST_ptr(cctx)
  323. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  324. privkey, 0, &qtserv,
  325. &clientquic, NULL, NULL))
  326. || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  327. goto err;
  328. if (!TEST_int_eq(SSL_version(clientquic), OSSL_QUIC1_VERSION)
  329. || !TEST_str_eq(SSL_get_version(clientquic), "QUICv1"))
  330. goto err;
  331. if (!TEST_true(SSL_is_quic(clientquic))
  332. || !TEST_false(SSL_is_tls(clientquic))
  333. || !TEST_false(SSL_is_dtls(clientquic)))
  334. goto err;
  335. testresult = 1;
  336. err:
  337. ossl_quic_tserver_free(qtserv);
  338. SSL_free(clientquic);
  339. SSL_CTX_free(cctx);
  340. return testresult;
  341. }
  342. #if defined(DO_SSL_TRACE_TEST)
  343. static void strip_line_ends(char *str)
  344. {
  345. size_t i;
  346. for (i = strlen(str);
  347. i > 0 && (str[i - 1] == '\n' || str[i - 1] == '\r');
  348. i--);
  349. str[i] = '\0';
  350. }
  351. static int compare_with_file(BIO *membio)
  352. {
  353. BIO *file = NULL, *newfile = NULL;
  354. char buf1[512], buf2[512];
  355. char *reffile;
  356. int ret = 0;
  357. size_t i;
  358. #ifdef OPENSSL_NO_ZLIB
  359. reffile = test_mk_file_path(datadir, "ssltraceref.txt");
  360. #else
  361. reffile = test_mk_file_path(datadir, "ssltraceref-zlib.txt");
  362. #endif
  363. if (!TEST_ptr(reffile))
  364. goto err;
  365. file = BIO_new_file(reffile, "rb");
  366. if (!TEST_ptr(file))
  367. goto err;
  368. newfile = BIO_new_file("ssltraceref-new.txt", "wb");
  369. if (!TEST_ptr(newfile))
  370. goto err;
  371. while (BIO_gets(membio, buf2, sizeof(buf2)) > 0)
  372. if (BIO_puts(newfile, buf2) <= 0) {
  373. TEST_error("Failed writing new file data");
  374. goto err;
  375. }
  376. if (!TEST_int_ge(BIO_seek(membio, 0), 0))
  377. goto err;
  378. while (BIO_gets(file, buf1, sizeof(buf1)) > 0) {
  379. if (BIO_gets(membio, buf2, sizeof(buf2)) <= 0) {
  380. TEST_error("Failed reading mem data");
  381. goto err;
  382. }
  383. strip_line_ends(buf1);
  384. strip_line_ends(buf2);
  385. if (strlen(buf1) != strlen(buf2)) {
  386. TEST_error("Actual and ref line data length mismatch");
  387. TEST_info("%s", buf1);
  388. TEST_info("%s", buf2);
  389. goto err;
  390. }
  391. for (i = 0; i < strlen(buf1); i++) {
  392. /* '?' is a wild card character in the reference text */
  393. if (buf1[i] == '?')
  394. buf2[i] = '?';
  395. }
  396. if (!TEST_str_eq(buf1, buf2))
  397. goto err;
  398. }
  399. if (!TEST_true(BIO_eof(file))
  400. || !TEST_true(BIO_eof(membio)))
  401. goto err;
  402. ret = 1;
  403. err:
  404. OPENSSL_free(reffile);
  405. BIO_free(file);
  406. BIO_free(newfile);
  407. return ret;
  408. }
  409. /*
  410. * Tests that the SSL_trace() msg_callback works as expected with a QUIC
  411. * connection. This also provides testing of the msg_callback at the same time.
  412. */
  413. static int test_ssl_trace(void)
  414. {
  415. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  416. SSL *clientquic = NULL;
  417. QUIC_TSERVER *qtserv = NULL;
  418. int testresult = 0;
  419. BIO *bio = BIO_new(BIO_s_mem());
  420. /*
  421. * Ensure we only configure ciphersuites that are available with both the
  422. * default and fips providers to get the same output in both cases
  423. */
  424. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256")))
  425. goto err;
  426. if (!TEST_ptr(cctx)
  427. || !TEST_ptr(bio)
  428. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  429. privkey,
  430. QTEST_FLAG_FAKE_TIME,
  431. &qtserv,
  432. &clientquic, NULL, NULL)))
  433. goto err;
  434. SSL_set_msg_callback(clientquic, SSL_trace);
  435. SSL_set_msg_callback_arg(clientquic, bio);
  436. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  437. goto err;
  438. if (!TEST_true(compare_with_file(bio)))
  439. goto err;
  440. testresult = 1;
  441. err:
  442. ossl_quic_tserver_free(qtserv);
  443. SSL_free(clientquic);
  444. SSL_CTX_free(cctx);
  445. BIO_free(bio);
  446. return testresult;
  447. }
  448. #endif
  449. static int ensure_valid_ciphers(const STACK_OF(SSL_CIPHER) *ciphers)
  450. {
  451. size_t i;
  452. /* Ensure ciphersuite list is suitably subsetted. */
  453. for (i = 0; i < (size_t)sk_SSL_CIPHER_num(ciphers); ++i) {
  454. const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
  455. switch (SSL_CIPHER_get_id(cipher)) {
  456. case TLS1_3_CK_AES_128_GCM_SHA256:
  457. case TLS1_3_CK_AES_256_GCM_SHA384:
  458. case TLS1_3_CK_CHACHA20_POLY1305_SHA256:
  459. break;
  460. default:
  461. TEST_error("forbidden cipher: %s", SSL_CIPHER_get_name(cipher));
  462. return 0;
  463. }
  464. }
  465. return 1;
  466. }
  467. /*
  468. * Test that handshake-layer APIs which shouldn't work don't work with QUIC.
  469. */
  470. static int test_quic_forbidden_apis_ctx(void)
  471. {
  472. int testresult = 0;
  473. SSL_CTX *ctx = NULL;
  474. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
  475. goto err;
  476. #ifndef OPENSSL_NO_SRTP
  477. /* This function returns 0 on success and 1 on error, and should fail. */
  478. if (!TEST_true(SSL_CTX_set_tlsext_use_srtp(ctx, "SRTP_AEAD_AES_128_GCM")))
  479. goto err;
  480. #endif
  481. /*
  482. * List of ciphersuites we do and don't allow in QUIC.
  483. */
  484. #define QUIC_CIPHERSUITES \
  485. "TLS_AES_128_GCM_SHA256:" \
  486. "TLS_AES_256_GCM_SHA384:" \
  487. "TLS_CHACHA20_POLY1305_SHA256"
  488. #define NON_QUIC_CIPHERSUITES \
  489. "TLS_AES_128_CCM_SHA256:" \
  490. "TLS_AES_256_CCM_SHA384:" \
  491. "TLS_AES_128_CCM_8_SHA256"
  492. /* Set TLSv1.3 ciphersuite list for the SSL_CTX. */
  493. if (!TEST_true(SSL_CTX_set_ciphersuites(ctx,
  494. QUIC_CIPHERSUITES ":"
  495. NON_QUIC_CIPHERSUITES)))
  496. goto err;
  497. /*
  498. * Forbidden ciphersuites should show up in SSL_CTX accessors, they are only
  499. * filtered in SSL_get1_supported_ciphers, so we don't check for
  500. * non-inclusion here.
  501. */
  502. testresult = 1;
  503. err:
  504. SSL_CTX_free(ctx);
  505. return testresult;
  506. }
  507. static int test_quic_forbidden_apis(void)
  508. {
  509. int testresult = 0;
  510. SSL_CTX *ctx = NULL;
  511. SSL *ssl = NULL;
  512. STACK_OF(SSL_CIPHER) *ciphers = NULL;
  513. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
  514. goto err;
  515. if (!TEST_ptr(ssl = SSL_new(ctx)))
  516. goto err;
  517. #ifndef OPENSSL_NO_SRTP
  518. /* This function returns 0 on success and 1 on error, and should fail. */
  519. if (!TEST_true(SSL_set_tlsext_use_srtp(ssl, "SRTP_AEAD_AES_128_GCM")))
  520. goto err;
  521. #endif
  522. /* Set TLSv1.3 ciphersuite list for the SSL_CTX. */
  523. if (!TEST_true(SSL_set_ciphersuites(ssl,
  524. QUIC_CIPHERSUITES ":"
  525. NON_QUIC_CIPHERSUITES)))
  526. goto err;
  527. /* Non-QUIC ciphersuites must not appear in supported ciphers list. */
  528. if (!TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl))
  529. || !TEST_true(ensure_valid_ciphers(ciphers)))
  530. goto err;
  531. testresult = 1;
  532. err:
  533. sk_SSL_CIPHER_free(ciphers);
  534. SSL_free(ssl);
  535. SSL_CTX_free(ctx);
  536. return testresult;
  537. }
  538. static int test_quic_forbidden_options(void)
  539. {
  540. int testresult = 0;
  541. SSL_CTX *ctx = NULL;
  542. SSL *ssl = NULL;
  543. char buf[16];
  544. size_t len;
  545. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
  546. goto err;
  547. /* QUIC options restrictions do not affect SSL_CTX */
  548. SSL_CTX_set_options(ctx, UINT64_MAX);
  549. if (!TEST_uint64_t_eq(SSL_CTX_get_options(ctx), UINT64_MAX))
  550. goto err;
  551. /* Set options on CTX which should not be inherited (tested below). */
  552. SSL_CTX_set_read_ahead(ctx, 1);
  553. SSL_CTX_set_max_early_data(ctx, 1);
  554. SSL_CTX_set_recv_max_early_data(ctx, 1);
  555. SSL_CTX_set_quiet_shutdown(ctx, 1);
  556. if (!TEST_ptr(ssl = SSL_new(ctx)))
  557. goto err;
  558. /* Only permitted options get transferred to SSL object */
  559. if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS))
  560. goto err;
  561. /* Try again using SSL_set_options */
  562. SSL_set_options(ssl, UINT64_MAX);
  563. if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS))
  564. goto err;
  565. /* Clear everything */
  566. SSL_clear_options(ssl, UINT64_MAX);
  567. if (!TEST_uint64_t_eq(SSL_get_options(ssl), 0))
  568. goto err;
  569. /* Readahead */
  570. if (!TEST_false(SSL_get_read_ahead(ssl)))
  571. goto err;
  572. SSL_set_read_ahead(ssl, 1);
  573. if (!TEST_false(SSL_get_read_ahead(ssl)))
  574. goto err;
  575. /* Block padding */
  576. if (!TEST_true(SSL_set_block_padding(ssl, 0))
  577. || !TEST_true(SSL_set_block_padding(ssl, 1))
  578. || !TEST_false(SSL_set_block_padding(ssl, 2)))
  579. goto err;
  580. /* Max fragment length */
  581. if (!TEST_true(SSL_set_tlsext_max_fragment_length(ssl, TLSEXT_max_fragment_length_DISABLED))
  582. || !TEST_false(SSL_set_tlsext_max_fragment_length(ssl, TLSEXT_max_fragment_length_512)))
  583. goto err;
  584. /* Max early data */
  585. if (!TEST_false(SSL_set_recv_max_early_data(ssl, 1))
  586. || !TEST_false(SSL_set_max_early_data(ssl, 1)))
  587. goto err;
  588. /* Read/Write */
  589. if (!TEST_false(SSL_read_early_data(ssl, buf, sizeof(buf), &len))
  590. || !TEST_false(SSL_write_early_data(ssl, buf, sizeof(buf), &len)))
  591. goto err;
  592. /* Buffer Management */
  593. if (!TEST_true(SSL_alloc_buffers(ssl))
  594. || !TEST_false(SSL_free_buffers(ssl)))
  595. goto err;
  596. /* Pipelining */
  597. if (!TEST_false(SSL_set_max_send_fragment(ssl, 2))
  598. || !TEST_false(SSL_set_split_send_fragment(ssl, 2))
  599. || !TEST_false(SSL_set_max_pipelines(ssl, 2)))
  600. goto err;
  601. /* HRR */
  602. if (!TEST_false(SSL_stateless(ssl)))
  603. goto err;
  604. /* Quiet Shutdown */
  605. if (!TEST_false(SSL_get_quiet_shutdown(ssl)))
  606. goto err;
  607. /* No duplication */
  608. if (!TEST_ptr_null(SSL_dup(ssl)))
  609. goto err;
  610. /* No clear */
  611. if (!TEST_false(SSL_clear(ssl)))
  612. goto err;
  613. testresult = 1;
  614. err:
  615. SSL_free(ssl);
  616. SSL_CTX_free(ctx);
  617. return testresult;
  618. }
  619. static int test_quic_set_fd(int idx)
  620. {
  621. int testresult = 0;
  622. SSL_CTX *ctx = NULL;
  623. SSL *ssl = NULL;
  624. int fd = -1, resfd = -1;
  625. BIO *bio = NULL;
  626. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
  627. goto err;
  628. if (!TEST_ptr(ssl = SSL_new(ctx)))
  629. goto err;
  630. if (!TEST_int_ge(fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0), 0))
  631. goto err;
  632. if (idx == 0) {
  633. if (!TEST_true(SSL_set_fd(ssl, fd)))
  634. goto err;
  635. if (!TEST_ptr(bio = SSL_get_rbio(ssl)))
  636. goto err;
  637. if (!TEST_ptr_eq(bio, SSL_get_wbio(ssl)))
  638. goto err;
  639. } else if (idx == 1) {
  640. if (!TEST_true(SSL_set_rfd(ssl, fd)))
  641. goto err;
  642. if (!TEST_ptr(bio = SSL_get_rbio(ssl)))
  643. goto err;
  644. if (!TEST_ptr_null(SSL_get_wbio(ssl)))
  645. goto err;
  646. } else {
  647. if (!TEST_true(SSL_set_wfd(ssl, fd)))
  648. goto err;
  649. if (!TEST_ptr(bio = SSL_get_wbio(ssl)))
  650. goto err;
  651. if (!TEST_ptr_null(SSL_get_rbio(ssl)))
  652. goto err;
  653. }
  654. if (!TEST_int_eq(BIO_method_type(bio), BIO_TYPE_DGRAM))
  655. goto err;
  656. if (!TEST_true(BIO_get_fd(bio, &resfd))
  657. || !TEST_int_eq(resfd, fd))
  658. goto err;
  659. testresult = 1;
  660. err:
  661. SSL_free(ssl);
  662. SSL_CTX_free(ctx);
  663. if (fd >= 0)
  664. BIO_closesocket(fd);
  665. return testresult;
  666. }
  667. #define MAXLOOPS 1000
  668. static int test_bio_ssl(void)
  669. {
  670. /*
  671. * We just use OSSL_QUIC_client_method() rather than
  672. * OSSL_QUIC_client_thread_method(). We will never leave the connection idle
  673. * so we will always be implicitly handling time events anyway via other
  674. * IO calls.
  675. */
  676. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  677. SSL *clientquic = NULL, *stream = NULL;
  678. QUIC_TSERVER *qtserv = NULL;
  679. int testresult = 0;
  680. BIO *cbio = NULL, *strbio = NULL, *thisbio;
  681. const char *msg = "Hello world";
  682. int abortctr = 0, err, clienterr = 0, servererr = 0, retc = 0, rets = 0;
  683. size_t written, readbytes, msglen;
  684. int sid = 0, i;
  685. unsigned char buf[80];
  686. if (!TEST_ptr(cctx))
  687. goto err;
  688. cbio = BIO_new_ssl(cctx, 1);
  689. if (!TEST_ptr(cbio))
  690. goto err;
  691. /*
  692. * We must configure the ALPN/peer address etc so we get the SSL object in
  693. * order to pass it to qtest_create_quic_objects for configuration.
  694. */
  695. if (!TEST_int_eq(BIO_get_ssl(cbio, &clientquic), 1))
  696. goto err;
  697. if (!TEST_true(qtest_create_quic_objects(libctx, NULL, NULL, cert, privkey,
  698. 0, &qtserv, &clientquic, NULL,
  699. NULL)))
  700. goto err;
  701. msglen = strlen(msg);
  702. do {
  703. err = BIO_FLAGS_WRITE;
  704. while (!clienterr && !retc && err == BIO_FLAGS_WRITE) {
  705. retc = BIO_write_ex(cbio, msg, msglen, &written);
  706. if (!retc) {
  707. if (BIO_should_retry(cbio))
  708. err = BIO_retry_type(cbio);
  709. else
  710. err = 0;
  711. }
  712. }
  713. if (!clienterr && retc <= 0 && err != BIO_FLAGS_READ) {
  714. TEST_info("BIO_write_ex() failed %d, %d", retc, err);
  715. TEST_openssl_errors();
  716. clienterr = 1;
  717. }
  718. if (!servererr && rets <= 0) {
  719. ossl_quic_tserver_tick(qtserv);
  720. servererr = ossl_quic_tserver_is_term_any(qtserv);
  721. if (!servererr)
  722. rets = ossl_quic_tserver_is_handshake_confirmed(qtserv);
  723. }
  724. if (clienterr && servererr)
  725. goto err;
  726. if (++abortctr == MAXLOOPS) {
  727. TEST_info("No progress made");
  728. goto err;
  729. }
  730. } while ((!retc && !clienterr) || (rets <= 0 && !servererr));
  731. /*
  732. * 2 loops: The first using the default stream, and the second using a new
  733. * client initiated bidi stream.
  734. */
  735. for (i = 0, thisbio = cbio; i < 2; i++) {
  736. if (!TEST_true(ossl_quic_tserver_read(qtserv, sid, buf, sizeof(buf),
  737. &readbytes))
  738. || !TEST_mem_eq(msg, msglen, buf, readbytes))
  739. goto err;
  740. if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg,
  741. msglen, &written)))
  742. goto err;
  743. ossl_quic_tserver_tick(qtserv);
  744. if (!TEST_true(BIO_read_ex(thisbio, buf, sizeof(buf), &readbytes))
  745. || !TEST_mem_eq(msg, msglen, buf, readbytes))
  746. goto err;
  747. if (i == 1)
  748. break;
  749. /*
  750. * Now create a new stream and repeat. The bottom two bits of the stream
  751. * id represents whether the stream is bidi and whether it is client
  752. * initiated or not. For client initiated bidi they are both 0. So the
  753. * first client initiated bidi stream is 0 and the next one is 4.
  754. */
  755. sid = 4;
  756. stream = SSL_new_stream(clientquic, 0);
  757. if (!TEST_ptr(stream))
  758. goto err;
  759. thisbio = strbio = BIO_new(BIO_f_ssl());
  760. if (!TEST_ptr(strbio))
  761. goto err;
  762. if (!TEST_int_eq(BIO_set_ssl(thisbio, stream, BIO_CLOSE), 1))
  763. goto err;
  764. stream = NULL;
  765. if (!TEST_true(BIO_write_ex(thisbio, msg, msglen, &written)))
  766. goto err;
  767. ossl_quic_tserver_tick(qtserv);
  768. }
  769. testresult = 1;
  770. err:
  771. BIO_free_all(cbio);
  772. BIO_free_all(strbio);
  773. SSL_free(stream);
  774. ossl_quic_tserver_free(qtserv);
  775. SSL_CTX_free(cctx);
  776. return testresult;
  777. }
  778. #define BACK_PRESSURE_NUM_LOOPS 10000
  779. /*
  780. * Test that sending data from the client to the server faster than the server
  781. * can process it eventually results in back pressure on the client.
  782. */
  783. static int test_back_pressure(void)
  784. {
  785. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  786. SSL *clientquic = NULL;
  787. QUIC_TSERVER *qtserv = NULL;
  788. int testresult = 0;
  789. unsigned char *msg = NULL;
  790. const size_t msglen = 1024;
  791. unsigned char buf[64];
  792. size_t readbytes, written;
  793. int i;
  794. if (!TEST_ptr(cctx)
  795. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  796. privkey, 0, &qtserv,
  797. &clientquic, NULL, NULL))
  798. || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  799. goto err;
  800. msg = OPENSSL_malloc(msglen);
  801. if (!TEST_ptr(msg))
  802. goto err;
  803. if (!TEST_int_eq(RAND_bytes_ex(libctx, msg, msglen, 0), 1))
  804. goto err;
  805. /*
  806. * Limit to 10000 loops. If we've not seen any back pressure after that
  807. * we're going to run out of memory, so abort.
  808. */
  809. for (i = 0; i < BACK_PRESSURE_NUM_LOOPS; i++) {
  810. /* Send data from the client */
  811. if (!SSL_write_ex(clientquic, msg, msglen, &written)) {
  812. /* Check if we are seeing back pressure */
  813. if (SSL_get_error(clientquic, 0) == SSL_ERROR_WANT_WRITE)
  814. break;
  815. TEST_error("Unexpected client failure");
  816. goto err;
  817. }
  818. /* Receive data at the server */
  819. ossl_quic_tserver_tick(qtserv);
  820. if (!TEST_true(ossl_quic_tserver_read(qtserv, 0, buf, sizeof(buf),
  821. &readbytes)))
  822. goto err;
  823. }
  824. if (i == BACK_PRESSURE_NUM_LOOPS) {
  825. TEST_error("No back pressure seen");
  826. goto err;
  827. }
  828. testresult = 1;
  829. err:
  830. SSL_free(clientquic);
  831. ossl_quic_tserver_free(qtserv);
  832. SSL_CTX_free(cctx);
  833. OPENSSL_free(msg);
  834. return testresult;
  835. }
  836. static int dgram_ctr = 0;
  837. static void dgram_cb(int write_p, int version, int content_type,
  838. const void *buf, size_t msglen, SSL *ssl, void *arg)
  839. {
  840. if (!write_p)
  841. return;
  842. if (content_type != SSL3_RT_QUIC_DATAGRAM)
  843. return;
  844. dgram_ctr++;
  845. }
  846. /* Test that we send multiple datagrams in one go when appropriate */
  847. static int test_multiple_dgrams(void)
  848. {
  849. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  850. SSL *clientquic = NULL;
  851. QUIC_TSERVER *qtserv = NULL;
  852. int testresult = 0;
  853. unsigned char *buf;
  854. const size_t buflen = 1400;
  855. size_t written;
  856. buf = OPENSSL_zalloc(buflen);
  857. if (!TEST_ptr(cctx)
  858. || !TEST_ptr(buf)
  859. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  860. privkey, 0, &qtserv,
  861. &clientquic, NULL, NULL))
  862. || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  863. goto err;
  864. dgram_ctr = 0;
  865. SSL_set_msg_callback(clientquic, dgram_cb);
  866. if (!TEST_true(SSL_write_ex(clientquic, buf, buflen, &written))
  867. || !TEST_size_t_eq(written, buflen)
  868. /* We wrote enough data for 2 datagrams */
  869. || !TEST_int_eq(dgram_ctr, 2))
  870. goto err;
  871. testresult = 1;
  872. err:
  873. OPENSSL_free(buf);
  874. SSL_free(clientquic);
  875. ossl_quic_tserver_free(qtserv);
  876. SSL_CTX_free(cctx);
  877. return testresult;
  878. }
  879. static int non_io_retry_cert_verify_cb(X509_STORE_CTX *ctx, void *arg)
  880. {
  881. int idx = SSL_get_ex_data_X509_STORE_CTX_idx();
  882. SSL *ssl;
  883. int *ctr = (int *)arg;
  884. /* this should not happen but check anyway */
  885. if (idx < 0
  886. || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL)
  887. return 0;
  888. /* If this is the first time we've been called then retry */
  889. if (((*ctr)++) == 0)
  890. return SSL_set_retry_verify(ssl);
  891. /* Otherwise do nothing - verification succeeds. Continue as normal */
  892. return 1;
  893. }
  894. /* Test that we can handle a non-io related retry error
  895. * Test 0: Non-blocking
  896. * Test 1: Blocking
  897. */
  898. static int test_non_io_retry(int idx)
  899. {
  900. SSL_CTX *cctx;
  901. SSL *clientquic = NULL;
  902. QUIC_TSERVER *qtserv = NULL;
  903. int testresult = 0;
  904. int flags = 0, ctr = 0;
  905. if (idx >= 1 && !qtest_supports_blocking())
  906. return TEST_skip("Blocking tests not supported in this build");
  907. cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  908. if (!TEST_ptr(cctx))
  909. goto err;
  910. SSL_CTX_set_cert_verify_callback(cctx, non_io_retry_cert_verify_cb, &ctr);
  911. flags = (idx >= 1) ? QTEST_FLAG_BLOCK : 0;
  912. if (!TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey,
  913. flags, &qtserv, &clientquic, NULL,
  914. NULL))
  915. || !TEST_true(qtest_create_quic_connection_ex(qtserv, clientquic,
  916. SSL_ERROR_WANT_RETRY_VERIFY))
  917. || !TEST_int_eq(SSL_want(clientquic), SSL_RETRY_VERIFY)
  918. || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  919. goto err;
  920. testresult = 1;
  921. err:
  922. SSL_free(clientquic);
  923. ossl_quic_tserver_free(qtserv);
  924. SSL_CTX_free(cctx);
  925. return testresult;
  926. }
  927. static int use_session_cb_cnt = 0;
  928. static int find_session_cb_cnt = 0;
  929. static const char *pskid = "Identity";
  930. static SSL_SESSION *serverpsk = NULL, *clientpsk = NULL;
  931. static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
  932. size_t *idlen, SSL_SESSION **sess)
  933. {
  934. use_session_cb_cnt++;
  935. if (clientpsk == NULL)
  936. return 0;
  937. SSL_SESSION_up_ref(clientpsk);
  938. *sess = clientpsk;
  939. *id = (const unsigned char *)pskid;
  940. *idlen = strlen(pskid);
  941. return 1;
  942. }
  943. static int find_session_cb(SSL *ssl, const unsigned char *identity,
  944. size_t identity_len, SSL_SESSION **sess)
  945. {
  946. find_session_cb_cnt++;
  947. if (serverpsk == NULL)
  948. return 0;
  949. /* Identity should match that set by the client */
  950. if (strlen(pskid) != identity_len
  951. || strncmp(pskid, (const char *)identity, identity_len) != 0)
  952. return 0;
  953. SSL_SESSION_up_ref(serverpsk);
  954. *sess = serverpsk;
  955. return 1;
  956. }
  957. static int test_quic_psk(void)
  958. {
  959. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  960. SSL *clientquic = NULL;
  961. QUIC_TSERVER *qtserv = NULL;
  962. int testresult = 0;
  963. if (!TEST_ptr(cctx)
  964. /* No cert or private key for the server, i.e. PSK only */
  965. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, NULL,
  966. NULL, 0, &qtserv,
  967. &clientquic, NULL, NULL)))
  968. goto end;
  969. SSL_set_psk_use_session_callback(clientquic, use_session_cb);
  970. ossl_quic_tserver_set_psk_find_session_cb(qtserv, find_session_cb);
  971. use_session_cb_cnt = 0;
  972. find_session_cb_cnt = 0;
  973. clientpsk = serverpsk = create_a_psk(clientquic, SHA384_DIGEST_LENGTH);
  974. if (!TEST_ptr(clientpsk))
  975. goto end;
  976. /* We already had one ref. Add another one */
  977. SSL_SESSION_up_ref(clientpsk);
  978. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))
  979. || !TEST_int_eq(1, find_session_cb_cnt)
  980. || !TEST_int_eq(1, use_session_cb_cnt)
  981. /* Check that we actually used the PSK */
  982. || !TEST_true(SSL_session_reused(clientquic)))
  983. goto end;
  984. testresult = 1;
  985. end:
  986. SSL_free(clientquic);
  987. ossl_quic_tserver_free(qtserv);
  988. SSL_CTX_free(cctx);
  989. SSL_SESSION_free(clientpsk);
  990. SSL_SESSION_free(serverpsk);
  991. clientpsk = serverpsk = NULL;
  992. return testresult;
  993. }
  994. static int test_client_auth(int idx)
  995. {
  996. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  997. SSL_CTX *sctx = SSL_CTX_new_ex(libctx, NULL, TLS_method());
  998. SSL *clientquic = NULL;
  999. QUIC_TSERVER *qtserv = NULL;
  1000. int testresult = 0;
  1001. unsigned char buf[20];
  1002. static char *msg = "A test message";
  1003. size_t msglen = strlen(msg);
  1004. size_t numbytes = 0;
  1005. if (!TEST_ptr(cctx) || !TEST_ptr(sctx))
  1006. goto err;
  1007. SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT
  1008. | SSL_VERIFY_CLIENT_ONCE, NULL);
  1009. if (!TEST_true(SSL_CTX_load_verify_file(sctx, cauthca)))
  1010. goto err;
  1011. if (idx > 0
  1012. && (!TEST_true(SSL_CTX_use_certificate_chain_file(cctx, ccert))
  1013. || !TEST_true(SSL_CTX_use_PrivateKey_file(cctx, cprivkey,
  1014. SSL_FILETYPE_PEM))))
  1015. goto err;
  1016. if (!TEST_true(qtest_create_quic_objects(libctx, cctx, sctx, cert,
  1017. privkey, 0, &qtserv,
  1018. &clientquic, NULL, NULL)))
  1019. goto err;
  1020. if (idx == 0) {
  1021. if (!TEST_false(qtest_create_quic_connection(qtserv, clientquic)))
  1022. goto err;
  1023. /* negative test passed */
  1024. testresult = 1;
  1025. goto err;
  1026. }
  1027. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  1028. goto err;
  1029. /* Check that sending and receiving app data is ok */
  1030. if (!TEST_true(SSL_write_ex(clientquic, msg, msglen, &numbytes))
  1031. || !TEST_size_t_eq(numbytes, msglen))
  1032. goto err;
  1033. ossl_quic_tserver_tick(qtserv);
  1034. if (!TEST_true(ossl_quic_tserver_write(qtserv, 0,
  1035. (unsigned char *)msg,
  1036. msglen, &numbytes)))
  1037. goto err;
  1038. ossl_quic_tserver_tick(qtserv);
  1039. SSL_handle_events(clientquic);
  1040. if (!TEST_true(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes))
  1041. || !TEST_size_t_eq(numbytes, msglen)
  1042. || !TEST_mem_eq(buf, numbytes, msg, msglen))
  1043. goto err;
  1044. if (!TEST_true(qtest_shutdown(qtserv, clientquic)))
  1045. goto err;
  1046. testresult = 1;
  1047. err:
  1048. SSL_free(clientquic);
  1049. ossl_quic_tserver_free(qtserv);
  1050. SSL_CTX_free(sctx);
  1051. SSL_CTX_free(cctx);
  1052. return testresult;
  1053. }
  1054. /*
  1055. * Test that we correctly handle ALPN supplied by the application
  1056. * Test 0: ALPN is provided
  1057. * Test 1: No ALPN is provided
  1058. */
  1059. static int test_alpn(int idx)
  1060. {
  1061. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  1062. SSL *clientquic = NULL;
  1063. QUIC_TSERVER *qtserv = NULL;
  1064. int testresult = 0;
  1065. int ret;
  1066. /*
  1067. * Ensure we only configure ciphersuites that are available with both the
  1068. * default and fips providers to get the same output in both cases
  1069. */
  1070. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256")))
  1071. goto err;
  1072. if (!TEST_ptr(cctx)
  1073. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  1074. privkey,
  1075. QTEST_FLAG_FAKE_TIME,
  1076. &qtserv,
  1077. &clientquic, NULL, NULL)))
  1078. goto err;
  1079. if (idx == 0) {
  1080. /*
  1081. * Clear the ALPN we set in qtest_create_quic_objects. We use TEST_false
  1082. * because SSL_set_alpn_protos returns 0 for success.
  1083. */
  1084. if (!TEST_false(SSL_set_alpn_protos(clientquic, NULL, 0)))
  1085. goto err;
  1086. }
  1087. ret = SSL_connect(clientquic);
  1088. if (!TEST_int_le(ret, 0))
  1089. goto err;
  1090. if (idx == 0) {
  1091. /* We expect an immediate error due to lack of ALPN */
  1092. if (!TEST_int_eq(SSL_get_error(clientquic, ret), SSL_ERROR_SSL))
  1093. goto err;
  1094. } else {
  1095. /* ALPN was provided so we expect the connection to succeed */
  1096. if (!TEST_int_eq(SSL_get_error(clientquic, ret), SSL_ERROR_WANT_READ)
  1097. || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  1098. goto err;
  1099. }
  1100. testresult = 1;
  1101. err:
  1102. ossl_quic_tserver_free(qtserv);
  1103. SSL_free(clientquic);
  1104. SSL_CTX_free(cctx);
  1105. return testresult;
  1106. }
  1107. #define MAX_LOOPS 2000
  1108. /*
  1109. * Keep retrying SSL_read_ex until it succeeds or we give up. Accept a stream
  1110. * if we don't already have one
  1111. */
  1112. static int unreliable_client_read(SSL *clientquic, SSL **stream, void *buf,
  1113. size_t buflen, size_t *readbytes,
  1114. QUIC_TSERVER *qtserv)
  1115. {
  1116. int abortctr;
  1117. /* We just do this in a loop with a sleep for simplicity */
  1118. for (abortctr = 0; abortctr < MAX_LOOPS; abortctr++) {
  1119. if (*stream == NULL) {
  1120. SSL_handle_events(clientquic);
  1121. *stream = SSL_accept_stream(clientquic, 0);
  1122. }
  1123. if (*stream != NULL) {
  1124. if (SSL_read_ex(*stream, buf, buflen, readbytes))
  1125. return 1;
  1126. if (!TEST_int_eq(SSL_get_error(*stream, 0), SSL_ERROR_WANT_READ))
  1127. return 0;
  1128. }
  1129. ossl_quic_tserver_tick(qtserv);
  1130. qtest_add_time(1);
  1131. qtest_wait_for_timeout(clientquic, qtserv);
  1132. }
  1133. TEST_error("No progress made");
  1134. return 0;
  1135. }
  1136. /* Keep retrying ossl_quic_tserver_read until it succeeds or we give up */
  1137. static int unreliable_server_read(QUIC_TSERVER *qtserv, uint64_t sid,
  1138. void *buf, size_t buflen, size_t *readbytes,
  1139. SSL *clientquic)
  1140. {
  1141. int abortctr;
  1142. /* We just do this in a loop with a sleep for simplicity */
  1143. for (abortctr = 0; abortctr < MAX_LOOPS; abortctr++) {
  1144. if (ossl_quic_tserver_read(qtserv, sid, buf, buflen, readbytes)
  1145. && *readbytes > 1)
  1146. return 1;
  1147. ossl_quic_tserver_tick(qtserv);
  1148. SSL_handle_events(clientquic);
  1149. qtest_add_time(1);
  1150. qtest_wait_for_timeout(clientquic, qtserv);
  1151. }
  1152. TEST_error("No progress made");
  1153. return 0;
  1154. }
  1155. /*
  1156. * Create a connection and send data using an unreliable transport. We introduce
  1157. * random noise to drop, delay and duplicate datagrams.
  1158. * Test 0: Introduce random noise to datagrams
  1159. * Test 1: As with test 0 but also split datagrams containing multiple packets
  1160. * into individual datagrams so that individual packets can be affected
  1161. * by noise - not just a whole datagram.
  1162. */
  1163. static int test_noisy_dgram(int idx)
  1164. {
  1165. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  1166. SSL *clientquic = NULL, *stream[2] = { NULL, NULL };
  1167. QUIC_TSERVER *qtserv = NULL;
  1168. int testresult = 0;
  1169. uint64_t sid = 0;
  1170. char *msg = "Hello world!";
  1171. size_t msglen = strlen(msg), written, readbytes, i, j;
  1172. unsigned char buf[80];
  1173. int flags = QTEST_FLAG_NOISE | QTEST_FLAG_FAKE_TIME;
  1174. QTEST_FAULT *fault = NULL;
  1175. if (idx == 1)
  1176. flags |= QTEST_FLAG_PACKET_SPLIT;
  1177. if (!TEST_ptr(cctx)
  1178. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  1179. privkey, flags,
  1180. &qtserv,
  1181. &clientquic, &fault, NULL)))
  1182. goto err;
  1183. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  1184. goto err;
  1185. if (!TEST_true(SSL_set_incoming_stream_policy(clientquic,
  1186. SSL_INCOMING_STREAM_POLICY_ACCEPT,
  1187. 0))
  1188. || !TEST_true(SSL_set_default_stream_mode(clientquic,
  1189. SSL_DEFAULT_STREAM_MODE_NONE)))
  1190. goto err;
  1191. for (j = 0; j < 2; j++) {
  1192. if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, 0, &sid)))
  1193. goto err;
  1194. ossl_quic_tserver_tick(qtserv);
  1195. qtest_add_time(1);
  1196. /*
  1197. * Send data from the server to the client. Some datagrams may get
  1198. * lost, modified, dropped or re-ordered. We repeat 20 times to ensure
  1199. * we are sending enough datagrams for problems to be noticed.
  1200. */
  1201. for (i = 0; i < 20; i++) {
  1202. if (!TEST_true(ossl_quic_tserver_write(qtserv, sid,
  1203. (unsigned char *)msg, msglen,
  1204. &written))
  1205. || !TEST_size_t_eq(msglen, written))
  1206. goto err;
  1207. ossl_quic_tserver_tick(qtserv);
  1208. qtest_add_time(1);
  1209. /*
  1210. * Since the underlying BIO is now noisy we may get failures that
  1211. * need to be retried - so we use unreliable_client_read() to
  1212. * handle that
  1213. */
  1214. if (!TEST_true(unreliable_client_read(clientquic, &stream[j], buf,
  1215. sizeof(buf), &readbytes,
  1216. qtserv))
  1217. || !TEST_mem_eq(msg, msglen, buf, readbytes))
  1218. goto err;
  1219. }
  1220. /* Send data from the client to the server */
  1221. for (i = 0; i < 20; i++) {
  1222. if (!TEST_true(SSL_write_ex(stream[j], (unsigned char *)msg,
  1223. msglen, &written))
  1224. || !TEST_size_t_eq(msglen, written))
  1225. goto err;
  1226. ossl_quic_tserver_tick(qtserv);
  1227. qtest_add_time(1);
  1228. /*
  1229. * Since the underlying BIO is now noisy we may get failures that
  1230. * need to be retried - so we use unreliable_server_read() to
  1231. * handle that
  1232. */
  1233. if (!TEST_true(unreliable_server_read(qtserv, sid, buf, sizeof(buf),
  1234. &readbytes, clientquic))
  1235. || !TEST_mem_eq(msg, msglen, buf, readbytes))
  1236. goto err;
  1237. }
  1238. }
  1239. testresult = 1;
  1240. err:
  1241. ossl_quic_tserver_free(qtserv);
  1242. SSL_free(stream[0]);
  1243. SSL_free(stream[1]);
  1244. SSL_free(clientquic);
  1245. SSL_CTX_free(cctx);
  1246. qtest_fault_free(fault);
  1247. return testresult;
  1248. }
  1249. OPT_TEST_DECLARE_USAGE("provider config certsdir datadir\n")
  1250. int setup_tests(void)
  1251. {
  1252. char *modulename;
  1253. char *configfile;
  1254. libctx = OSSL_LIB_CTX_new();
  1255. if (!TEST_ptr(libctx))
  1256. return 0;
  1257. defctxnull = OSSL_PROVIDER_load(NULL, "null");
  1258. /*
  1259. * Verify that the default and fips providers in the default libctx are not
  1260. * available
  1261. */
  1262. if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
  1263. || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
  1264. goto err;
  1265. if (!test_skip_common_options()) {
  1266. TEST_error("Error parsing test options\n");
  1267. goto err;
  1268. }
  1269. if (!TEST_ptr(modulename = test_get_argument(0))
  1270. || !TEST_ptr(configfile = test_get_argument(1))
  1271. || !TEST_ptr(certsdir = test_get_argument(2))
  1272. || !TEST_ptr(datadir = test_get_argument(3)))
  1273. goto err;
  1274. if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
  1275. goto err;
  1276. /* Check we have the expected provider available */
  1277. if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
  1278. goto err;
  1279. /* Check the default provider is not available */
  1280. if (strcmp(modulename, "default") != 0
  1281. && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
  1282. goto err;
  1283. if (strcmp(modulename, "fips") == 0)
  1284. is_fips = 1;
  1285. cert = test_mk_file_path(certsdir, "servercert.pem");
  1286. if (cert == NULL)
  1287. goto err;
  1288. ccert = test_mk_file_path(certsdir, "ee-client-chain.pem");
  1289. if (ccert == NULL)
  1290. goto err;
  1291. cauthca = test_mk_file_path(certsdir, "root-cert.pem");
  1292. if (cauthca == NULL)
  1293. goto err;
  1294. privkey = test_mk_file_path(certsdir, "serverkey.pem");
  1295. if (privkey == NULL)
  1296. goto err;
  1297. cprivkey = test_mk_file_path(certsdir, "ee-key.pem");
  1298. if (privkey == NULL)
  1299. goto err;
  1300. ADD_ALL_TESTS(test_quic_write_read, 3);
  1301. ADD_TEST(test_fin_only_blocking);
  1302. ADD_TEST(test_ciphersuites);
  1303. ADD_TEST(test_cipher_find);
  1304. ADD_TEST(test_version);
  1305. #if defined(DO_SSL_TRACE_TEST)
  1306. ADD_TEST(test_ssl_trace);
  1307. #endif
  1308. ADD_TEST(test_quic_forbidden_apis_ctx);
  1309. ADD_TEST(test_quic_forbidden_apis);
  1310. ADD_TEST(test_quic_forbidden_options);
  1311. ADD_ALL_TESTS(test_quic_set_fd, 3);
  1312. ADD_TEST(test_bio_ssl);
  1313. ADD_TEST(test_back_pressure);
  1314. ADD_TEST(test_multiple_dgrams);
  1315. ADD_ALL_TESTS(test_non_io_retry, 2);
  1316. ADD_TEST(test_quic_psk);
  1317. ADD_ALL_TESTS(test_client_auth, 2);
  1318. ADD_ALL_TESTS(test_alpn, 2);
  1319. ADD_ALL_TESTS(test_noisy_dgram, 2);
  1320. return 1;
  1321. err:
  1322. cleanup_tests();
  1323. return 0;
  1324. }
  1325. void cleanup_tests(void)
  1326. {
  1327. bio_f_noisy_dgram_filter_free();
  1328. bio_f_pkt_split_dgram_filter_free();
  1329. OPENSSL_free(cert);
  1330. OPENSSL_free(privkey);
  1331. OPENSSL_free(ccert);
  1332. OPENSSL_free(cauthca);
  1333. OPENSSL_free(cprivkey);
  1334. OSSL_PROVIDER_unload(defctxnull);
  1335. OSSL_LIB_CTX_free(libctx);
  1336. }