quicapitest.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281
  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. #include "internal/quic_error.h"
  20. static OSSL_LIB_CTX *libctx = NULL;
  21. static OSSL_PROVIDER *defctxnull = NULL;
  22. static char *certsdir = NULL;
  23. static char *cert = NULL;
  24. static char *ccert = NULL;
  25. static char *cauthca = NULL;
  26. static char *privkey = NULL;
  27. static char *cprivkey = NULL;
  28. static char *datadir = NULL;
  29. static int is_fips = 0;
  30. /* The ssltrace test assumes some options are switched on/off */
  31. #if !defined(OPENSSL_NO_SSL_TRACE) \
  32. && defined(OPENSSL_NO_BROTLI) && defined(OPENSSL_NO_ZSTD) \
  33. && !defined(OPENSSL_NO_ECX) && !defined(OPENSSL_NO_DH)
  34. # define DO_SSL_TRACE_TEST
  35. #endif
  36. /*
  37. * Test that we read what we've written.
  38. * Test 0: Non-blocking
  39. * Test 1: Blocking
  40. * Test 2: Blocking, introduce socket error, test error handling.
  41. */
  42. static int test_quic_write_read(int idx)
  43. {
  44. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  45. SSL_CTX *sctx = NULL;
  46. SSL *clientquic = NULL;
  47. QUIC_TSERVER *qtserv = NULL;
  48. int j, k, ret = 0;
  49. unsigned char buf[20], scratch[64];
  50. static char *msg = "A test message";
  51. size_t msglen = strlen(msg);
  52. size_t numbytes = 0;
  53. int ssock = 0, csock = 0;
  54. uint64_t sid = UINT64_MAX;
  55. SSL_SESSION *sess = NULL;
  56. if (idx >= 1 && !qtest_supports_blocking())
  57. return TEST_skip("Blocking tests not supported in this build");
  58. for (k = 0; k < 2; k++) {
  59. if (!TEST_ptr(cctx)
  60. || !TEST_true(qtest_create_quic_objects(libctx, cctx, sctx,
  61. cert, privkey,
  62. idx >= 1
  63. ? QTEST_FLAG_BLOCK
  64. : 0,
  65. &qtserv, &clientquic,
  66. NULL, NULL))
  67. || !TEST_true(SSL_set_tlsext_host_name(clientquic, "localhost")))
  68. goto end;
  69. if (sess != NULL && !TEST_true(SSL_set_session(clientquic, sess)))
  70. goto end;
  71. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  72. goto end;
  73. if (idx >= 1) {
  74. if (!TEST_true(BIO_get_fd(ossl_quic_tserver_get0_rbio(qtserv),
  75. &ssock)))
  76. goto end;
  77. if (!TEST_int_gt(csock = SSL_get_rfd(clientquic), 0))
  78. goto end;
  79. }
  80. sid = 0; /* client-initiated bidirectional stream */
  81. for (j = 0; j < 2; j++) {
  82. /* Check that sending and receiving app data is ok */
  83. if (!TEST_true(SSL_write_ex(clientquic, msg, msglen, &numbytes))
  84. || !TEST_size_t_eq(numbytes, msglen))
  85. goto end;
  86. if (idx >= 1) {
  87. do {
  88. if (!TEST_true(wait_until_sock_readable(ssock)))
  89. goto end;
  90. ossl_quic_tserver_tick(qtserv);
  91. if (!TEST_true(ossl_quic_tserver_read(qtserv, sid, buf,
  92. sizeof(buf),
  93. &numbytes)))
  94. goto end;
  95. } while (numbytes == 0);
  96. if (!TEST_mem_eq(buf, numbytes, msg, msglen))
  97. goto end;
  98. }
  99. if (idx >= 2 && j > 0)
  100. /* Introduce permanent socket error */
  101. BIO_closesocket(csock);
  102. ossl_quic_tserver_tick(qtserv);
  103. if (!TEST_true(ossl_quic_tserver_write(qtserv, sid,
  104. (unsigned char *)msg,
  105. msglen, &numbytes)))
  106. goto end;
  107. ossl_quic_tserver_tick(qtserv);
  108. SSL_handle_events(clientquic);
  109. if (idx >= 2 && j > 0) {
  110. if (!TEST_false(SSL_read_ex(clientquic, buf, 1, &numbytes))
  111. || !TEST_int_eq(SSL_get_error(clientquic, 0),
  112. SSL_ERROR_SYSCALL)
  113. || !TEST_false(SSL_write_ex(clientquic, msg, msglen,
  114. &numbytes))
  115. || !TEST_int_eq(SSL_get_error(clientquic, 0),
  116. SSL_ERROR_SYSCALL))
  117. goto end;
  118. break;
  119. }
  120. /*
  121. * In blocking mode the SSL_read_ex call will block until the socket
  122. * is readable and has our data. In non-blocking mode we're doing
  123. * everything in memory, so it should be immediately available
  124. */
  125. if (!TEST_true(SSL_read_ex(clientquic, buf, 1, &numbytes))
  126. || !TEST_size_t_eq(numbytes, 1)
  127. || !TEST_true(SSL_has_pending(clientquic))
  128. || !TEST_int_eq(SSL_pending(clientquic), msglen - 1)
  129. || !TEST_true(SSL_read_ex(clientquic, buf + 1,
  130. sizeof(buf) - 1, &numbytes))
  131. || !TEST_mem_eq(buf, numbytes + 1, msg, msglen))
  132. goto end;
  133. }
  134. /* Test that exporters work. */
  135. if (!TEST_true(SSL_export_keying_material(clientquic, scratch,
  136. sizeof(scratch), "test", 4, (unsigned char *)"ctx", 3,
  137. 1)))
  138. goto end;
  139. if (sess == NULL) {
  140. /* We didn't supply a session so we're not expecting resumption */
  141. if (!TEST_false(SSL_session_reused(clientquic)))
  142. goto end;
  143. /* We should have a session ticket by now */
  144. sess = SSL_get1_session(clientquic);
  145. if (!TEST_ptr(sess))
  146. goto end;
  147. } else {
  148. /* We supplied a session so we should have resumed */
  149. if (!TEST_true(SSL_session_reused(clientquic)))
  150. goto end;
  151. }
  152. if (!TEST_true(qtest_shutdown(qtserv, clientquic)))
  153. goto end;
  154. if (sctx == NULL) {
  155. sctx = ossl_quic_tserver_get0_ssl_ctx(qtserv);
  156. if (!TEST_true(SSL_CTX_up_ref(sctx))) {
  157. sctx = NULL;
  158. goto end;
  159. }
  160. }
  161. ossl_quic_tserver_free(qtserv);
  162. qtserv = NULL;
  163. SSL_free(clientquic);
  164. clientquic = NULL;
  165. if (idx >= 2)
  166. break;
  167. }
  168. ret = 1;
  169. end:
  170. SSL_SESSION_free(sess);
  171. ossl_quic_tserver_free(qtserv);
  172. SSL_free(clientquic);
  173. SSL_CTX_free(cctx);
  174. SSL_CTX_free(sctx);
  175. return ret;
  176. }
  177. /*
  178. * Test that sending FIN with no data to a client blocking in SSL_read_ex() will
  179. * wake up the client.
  180. */
  181. static int test_fin_only_blocking(void)
  182. {
  183. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  184. SSL_CTX *sctx = NULL;
  185. SSL *clientquic = NULL;
  186. QUIC_TSERVER *qtserv = NULL;
  187. const char *msg = "Hello World";
  188. uint64_t sid;
  189. size_t numbytes;
  190. unsigned char buf[32];
  191. int ret = 0;
  192. OSSL_TIME timer, timediff;
  193. if (!qtest_supports_blocking())
  194. return TEST_skip("Blocking tests not supported in this build");
  195. if (!TEST_ptr(cctx)
  196. || !TEST_true(qtest_create_quic_objects(libctx, cctx, sctx,
  197. cert, privkey,
  198. QTEST_FLAG_BLOCK,
  199. &qtserv, &clientquic,
  200. NULL, NULL))
  201. || !TEST_true(SSL_set_tlsext_host_name(clientquic, "localhost")))
  202. goto end;
  203. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  204. goto end;
  205. if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, 0, &sid))
  206. || !TEST_true(ossl_quic_tserver_write(qtserv, sid,
  207. (unsigned char *)msg,
  208. strlen(msg), &numbytes))
  209. || !TEST_size_t_eq(strlen(msg), numbytes))
  210. goto end;
  211. ossl_quic_tserver_tick(qtserv);
  212. if (!TEST_true(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes))
  213. || !TEST_mem_eq(msg, strlen(msg), buf, numbytes))
  214. goto end;
  215. if (!TEST_true(ossl_quic_tserver_conclude(qtserv, sid)))
  216. goto end;
  217. timer = ossl_time_now();
  218. if (!TEST_false(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes)))
  219. goto end;
  220. timediff = ossl_time_subtract(ossl_time_now(), timer);
  221. if (!TEST_int_eq(SSL_get_error(clientquic, 0), SSL_ERROR_ZERO_RETURN)
  222. /*
  223. * We expect the SSL_read_ex to not have blocked so this should
  224. * be very fast. 20ms should be plenty.
  225. */
  226. || !TEST_uint64_t_le(ossl_time2ms(timediff), 20))
  227. goto end;
  228. if (!TEST_true(qtest_shutdown(qtserv, clientquic)))
  229. goto end;
  230. ret = 1;
  231. end:
  232. ossl_quic_tserver_free(qtserv);
  233. SSL_free(clientquic);
  234. SSL_CTX_free(cctx);
  235. SSL_CTX_free(sctx);
  236. return ret;
  237. }
  238. /* Test that a vanilla QUIC SSL object has the expected ciphersuites available */
  239. static int test_ciphersuites(void)
  240. {
  241. SSL_CTX *ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  242. SSL *ssl;
  243. int testresult = 0;
  244. const STACK_OF(SSL_CIPHER) *ciphers = NULL;
  245. const SSL_CIPHER *cipher;
  246. /* We expect this exact list of ciphersuites by default */
  247. int cipherids[] = {
  248. TLS1_3_CK_AES_256_GCM_SHA384,
  249. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  250. TLS1_3_CK_CHACHA20_POLY1305_SHA256,
  251. #endif
  252. TLS1_3_CK_AES_128_GCM_SHA256
  253. };
  254. size_t i, j;
  255. if (!TEST_ptr(ctx))
  256. return 0;
  257. ssl = SSL_new(ctx);
  258. if (!TEST_ptr(ssl))
  259. goto err;
  260. ciphers = SSL_get_ciphers(ssl);
  261. for (i = 0, j = 0; i < OSSL_NELEM(cipherids); i++) {
  262. if (cipherids[i] == TLS1_3_CK_CHACHA20_POLY1305_SHA256 && is_fips)
  263. continue;
  264. cipher = sk_SSL_CIPHER_value(ciphers, j++);
  265. if (!TEST_ptr(cipher))
  266. goto err;
  267. if (!TEST_uint_eq(SSL_CIPHER_get_id(cipher), cipherids[i]))
  268. goto err;
  269. }
  270. /* We should have checked all the ciphers in the stack */
  271. if (!TEST_int_eq(sk_SSL_CIPHER_num(ciphers), j))
  272. goto err;
  273. testresult = 1;
  274. err:
  275. SSL_free(ssl);
  276. SSL_CTX_free(ctx);
  277. return testresult;
  278. }
  279. static int test_cipher_find(void)
  280. {
  281. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  282. SSL *clientquic = NULL;
  283. struct {
  284. const unsigned char *cipherbytes;
  285. int ok;
  286. } testciphers[] = {
  287. { TLS13_AES_128_GCM_SHA256_BYTES, 1 },
  288. { TLS13_AES_256_GCM_SHA384_BYTES, 1 },
  289. { TLS13_CHACHA20_POLY1305_SHA256_BYTES, 1 },
  290. { TLS13_AES_128_CCM_SHA256_BYTES, 0 },
  291. { TLS13_AES_128_CCM_8_SHA256_BYTES, 0 }
  292. };
  293. size_t i;
  294. int testresult = 0;
  295. if (!TEST_ptr(cctx))
  296. goto err;
  297. clientquic = SSL_new(cctx);
  298. if (!TEST_ptr(clientquic))
  299. goto err;
  300. for (i = 0; i < OSSL_NELEM(testciphers); i++)
  301. if (testciphers[i].ok) {
  302. if (!TEST_ptr(SSL_CIPHER_find(clientquic,
  303. testciphers[i].cipherbytes)))
  304. goto err;
  305. } else {
  306. if (!TEST_ptr_null(SSL_CIPHER_find(clientquic,
  307. testciphers[i].cipherbytes)))
  308. goto err;
  309. }
  310. testresult = 1;
  311. err:
  312. SSL_free(clientquic);
  313. SSL_CTX_free(cctx);
  314. return testresult;
  315. }
  316. /*
  317. * Test that SSL_version, SSL_get_version, SSL_is_quic, SSL_is_tls and
  318. * SSL_is_dtls return the expected results for a QUIC connection. Compare with
  319. * test_version() in sslapitest.c which does the same thing for TLS/DTLS
  320. * connections.
  321. */
  322. static int test_version(void)
  323. {
  324. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  325. SSL *clientquic = NULL;
  326. QUIC_TSERVER *qtserv = NULL;
  327. int testresult = 0;
  328. if (!TEST_ptr(cctx)
  329. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  330. privkey, 0, &qtserv,
  331. &clientquic, NULL, NULL))
  332. || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  333. goto err;
  334. if (!TEST_int_eq(SSL_version(clientquic), OSSL_QUIC1_VERSION)
  335. || !TEST_str_eq(SSL_get_version(clientquic), "QUICv1"))
  336. goto err;
  337. if (!TEST_true(SSL_is_quic(clientquic))
  338. || !TEST_false(SSL_is_tls(clientquic))
  339. || !TEST_false(SSL_is_dtls(clientquic)))
  340. goto err;
  341. testresult = 1;
  342. err:
  343. ossl_quic_tserver_free(qtserv);
  344. SSL_free(clientquic);
  345. SSL_CTX_free(cctx);
  346. return testresult;
  347. }
  348. #if defined(DO_SSL_TRACE_TEST)
  349. static void strip_line_ends(char *str)
  350. {
  351. size_t i;
  352. for (i = strlen(str);
  353. i > 0 && (str[i - 1] == '\n' || str[i - 1] == '\r');
  354. i--);
  355. str[i] = '\0';
  356. }
  357. static int compare_with_file(BIO *membio)
  358. {
  359. BIO *file = NULL, *newfile = NULL;
  360. char buf1[512], buf2[512];
  361. char *reffile;
  362. int ret = 0;
  363. size_t i;
  364. #ifdef OPENSSL_NO_ZLIB
  365. reffile = test_mk_file_path(datadir, "ssltraceref.txt");
  366. #else
  367. reffile = test_mk_file_path(datadir, "ssltraceref-zlib.txt");
  368. #endif
  369. if (!TEST_ptr(reffile))
  370. goto err;
  371. file = BIO_new_file(reffile, "rb");
  372. if (!TEST_ptr(file))
  373. goto err;
  374. newfile = BIO_new_file("ssltraceref-new.txt", "wb");
  375. if (!TEST_ptr(newfile))
  376. goto err;
  377. while (BIO_gets(membio, buf2, sizeof(buf2)) > 0)
  378. if (BIO_puts(newfile, buf2) <= 0) {
  379. TEST_error("Failed writing new file data");
  380. goto err;
  381. }
  382. if (!TEST_int_ge(BIO_seek(membio, 0), 0))
  383. goto err;
  384. while (BIO_gets(file, buf1, sizeof(buf1)) > 0) {
  385. if (BIO_gets(membio, buf2, sizeof(buf2)) <= 0) {
  386. TEST_error("Failed reading mem data");
  387. goto err;
  388. }
  389. strip_line_ends(buf1);
  390. strip_line_ends(buf2);
  391. if (strlen(buf1) != strlen(buf2)) {
  392. TEST_error("Actual and ref line data length mismatch");
  393. TEST_info("%s", buf1);
  394. TEST_info("%s", buf2);
  395. goto err;
  396. }
  397. for (i = 0; i < strlen(buf1); i++) {
  398. /* '?' is a wild card character in the reference text */
  399. if (buf1[i] == '?')
  400. buf2[i] = '?';
  401. }
  402. if (!TEST_str_eq(buf1, buf2))
  403. goto err;
  404. }
  405. if (!TEST_true(BIO_eof(file))
  406. || !TEST_true(BIO_eof(membio)))
  407. goto err;
  408. ret = 1;
  409. err:
  410. OPENSSL_free(reffile);
  411. BIO_free(file);
  412. BIO_free(newfile);
  413. return ret;
  414. }
  415. /*
  416. * Tests that the SSL_trace() msg_callback works as expected with a QUIC
  417. * connection. This also provides testing of the msg_callback at the same time.
  418. */
  419. static int test_ssl_trace(void)
  420. {
  421. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  422. SSL *clientquic = NULL;
  423. QUIC_TSERVER *qtserv = NULL;
  424. int testresult = 0;
  425. BIO *bio = BIO_new(BIO_s_mem());
  426. /*
  427. * Ensure we only configure ciphersuites that are available with both the
  428. * default and fips providers to get the same output in both cases
  429. */
  430. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256")))
  431. goto err;
  432. if (!TEST_ptr(cctx)
  433. || !TEST_ptr(bio)
  434. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  435. privkey,
  436. QTEST_FLAG_FAKE_TIME,
  437. &qtserv,
  438. &clientquic, NULL, NULL)))
  439. goto err;
  440. SSL_set_msg_callback(clientquic, SSL_trace);
  441. SSL_set_msg_callback_arg(clientquic, bio);
  442. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  443. goto err;
  444. if (!TEST_true(compare_with_file(bio)))
  445. goto err;
  446. testresult = 1;
  447. err:
  448. ossl_quic_tserver_free(qtserv);
  449. SSL_free(clientquic);
  450. SSL_CTX_free(cctx);
  451. BIO_free(bio);
  452. return testresult;
  453. }
  454. #endif
  455. static int ensure_valid_ciphers(const STACK_OF(SSL_CIPHER) *ciphers)
  456. {
  457. size_t i;
  458. /* Ensure ciphersuite list is suitably subsetted. */
  459. for (i = 0; i < (size_t)sk_SSL_CIPHER_num(ciphers); ++i) {
  460. const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
  461. switch (SSL_CIPHER_get_id(cipher)) {
  462. case TLS1_3_CK_AES_128_GCM_SHA256:
  463. case TLS1_3_CK_AES_256_GCM_SHA384:
  464. case TLS1_3_CK_CHACHA20_POLY1305_SHA256:
  465. break;
  466. default:
  467. TEST_error("forbidden cipher: %s", SSL_CIPHER_get_name(cipher));
  468. return 0;
  469. }
  470. }
  471. return 1;
  472. }
  473. /*
  474. * Test that handshake-layer APIs which shouldn't work don't work with QUIC.
  475. */
  476. static int test_quic_forbidden_apis_ctx(void)
  477. {
  478. int testresult = 0;
  479. SSL_CTX *ctx = NULL;
  480. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
  481. goto err;
  482. #ifndef OPENSSL_NO_SRTP
  483. /* This function returns 0 on success and 1 on error, and should fail. */
  484. if (!TEST_true(SSL_CTX_set_tlsext_use_srtp(ctx, "SRTP_AEAD_AES_128_GCM")))
  485. goto err;
  486. #endif
  487. /*
  488. * List of ciphersuites we do and don't allow in QUIC.
  489. */
  490. #define QUIC_CIPHERSUITES \
  491. "TLS_AES_128_GCM_SHA256:" \
  492. "TLS_AES_256_GCM_SHA384:" \
  493. "TLS_CHACHA20_POLY1305_SHA256"
  494. #define NON_QUIC_CIPHERSUITES \
  495. "TLS_AES_128_CCM_SHA256:" \
  496. "TLS_AES_256_CCM_SHA384:" \
  497. "TLS_AES_128_CCM_8_SHA256"
  498. /* Set TLSv1.3 ciphersuite list for the SSL_CTX. */
  499. if (!TEST_true(SSL_CTX_set_ciphersuites(ctx,
  500. QUIC_CIPHERSUITES ":"
  501. NON_QUIC_CIPHERSUITES)))
  502. goto err;
  503. /*
  504. * Forbidden ciphersuites should show up in SSL_CTX accessors, they are only
  505. * filtered in SSL_get1_supported_ciphers, so we don't check for
  506. * non-inclusion here.
  507. */
  508. testresult = 1;
  509. err:
  510. SSL_CTX_free(ctx);
  511. return testresult;
  512. }
  513. static int test_quic_forbidden_apis(void)
  514. {
  515. int testresult = 0;
  516. SSL_CTX *ctx = NULL;
  517. SSL *ssl = NULL;
  518. STACK_OF(SSL_CIPHER) *ciphers = NULL;
  519. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
  520. goto err;
  521. if (!TEST_ptr(ssl = SSL_new(ctx)))
  522. goto err;
  523. #ifndef OPENSSL_NO_SRTP
  524. /* This function returns 0 on success and 1 on error, and should fail. */
  525. if (!TEST_true(SSL_set_tlsext_use_srtp(ssl, "SRTP_AEAD_AES_128_GCM")))
  526. goto err;
  527. #endif
  528. /* Set TLSv1.3 ciphersuite list for the SSL_CTX. */
  529. if (!TEST_true(SSL_set_ciphersuites(ssl,
  530. QUIC_CIPHERSUITES ":"
  531. NON_QUIC_CIPHERSUITES)))
  532. goto err;
  533. /* Non-QUIC ciphersuites must not appear in supported ciphers list. */
  534. if (!TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl))
  535. || !TEST_true(ensure_valid_ciphers(ciphers)))
  536. goto err;
  537. testresult = 1;
  538. err:
  539. sk_SSL_CIPHER_free(ciphers);
  540. SSL_free(ssl);
  541. SSL_CTX_free(ctx);
  542. return testresult;
  543. }
  544. static int test_quic_forbidden_options(void)
  545. {
  546. int testresult = 0;
  547. SSL_CTX *ctx = NULL;
  548. SSL *ssl = NULL;
  549. char buf[16];
  550. size_t len;
  551. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
  552. goto err;
  553. /* QUIC options restrictions do not affect SSL_CTX */
  554. SSL_CTX_set_options(ctx, UINT64_MAX);
  555. if (!TEST_uint64_t_eq(SSL_CTX_get_options(ctx), UINT64_MAX))
  556. goto err;
  557. /* Set options on CTX which should not be inherited (tested below). */
  558. SSL_CTX_set_read_ahead(ctx, 1);
  559. SSL_CTX_set_max_early_data(ctx, 1);
  560. SSL_CTX_set_recv_max_early_data(ctx, 1);
  561. SSL_CTX_set_quiet_shutdown(ctx, 1);
  562. if (!TEST_ptr(ssl = SSL_new(ctx)))
  563. goto err;
  564. /* Only permitted options get transferred to SSL object */
  565. if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS))
  566. goto err;
  567. /* Try again using SSL_set_options */
  568. SSL_set_options(ssl, UINT64_MAX);
  569. if (!TEST_uint64_t_eq(SSL_get_options(ssl), OSSL_QUIC_PERMITTED_OPTIONS))
  570. goto err;
  571. /* Clear everything */
  572. SSL_clear_options(ssl, UINT64_MAX);
  573. if (!TEST_uint64_t_eq(SSL_get_options(ssl), 0))
  574. goto err;
  575. /* Readahead */
  576. if (!TEST_false(SSL_get_read_ahead(ssl)))
  577. goto err;
  578. SSL_set_read_ahead(ssl, 1);
  579. if (!TEST_false(SSL_get_read_ahead(ssl)))
  580. goto err;
  581. /* Block padding */
  582. if (!TEST_true(SSL_set_block_padding(ssl, 0))
  583. || !TEST_true(SSL_set_block_padding(ssl, 1))
  584. || !TEST_false(SSL_set_block_padding(ssl, 2)))
  585. goto err;
  586. /* Max fragment length */
  587. if (!TEST_true(SSL_set_tlsext_max_fragment_length(ssl, TLSEXT_max_fragment_length_DISABLED))
  588. || !TEST_false(SSL_set_tlsext_max_fragment_length(ssl, TLSEXT_max_fragment_length_512)))
  589. goto err;
  590. /* Max early data */
  591. if (!TEST_false(SSL_set_recv_max_early_data(ssl, 1))
  592. || !TEST_false(SSL_set_max_early_data(ssl, 1)))
  593. goto err;
  594. /* Read/Write */
  595. if (!TEST_false(SSL_read_early_data(ssl, buf, sizeof(buf), &len))
  596. || !TEST_false(SSL_write_early_data(ssl, buf, sizeof(buf), &len)))
  597. goto err;
  598. /* Buffer Management */
  599. if (!TEST_true(SSL_alloc_buffers(ssl))
  600. || !TEST_false(SSL_free_buffers(ssl)))
  601. goto err;
  602. /* Pipelining */
  603. if (!TEST_false(SSL_set_max_send_fragment(ssl, 2))
  604. || !TEST_false(SSL_set_split_send_fragment(ssl, 2))
  605. || !TEST_false(SSL_set_max_pipelines(ssl, 2)))
  606. goto err;
  607. /* HRR */
  608. if (!TEST_false(SSL_stateless(ssl)))
  609. goto err;
  610. /* Quiet Shutdown */
  611. if (!TEST_false(SSL_get_quiet_shutdown(ssl)))
  612. goto err;
  613. /* No duplication */
  614. if (!TEST_ptr_null(SSL_dup(ssl)))
  615. goto err;
  616. /* No clear */
  617. if (!TEST_false(SSL_clear(ssl)))
  618. goto err;
  619. testresult = 1;
  620. err:
  621. SSL_free(ssl);
  622. SSL_CTX_free(ctx);
  623. return testresult;
  624. }
  625. static int test_quic_set_fd(int idx)
  626. {
  627. int testresult = 0;
  628. SSL_CTX *ctx = NULL;
  629. SSL *ssl = NULL;
  630. int fd = -1, resfd = -1;
  631. BIO *bio = NULL;
  632. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
  633. goto err;
  634. if (!TEST_ptr(ssl = SSL_new(ctx)))
  635. goto err;
  636. if (!TEST_int_ge(fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0), 0))
  637. goto err;
  638. if (idx == 0) {
  639. if (!TEST_true(SSL_set_fd(ssl, fd)))
  640. goto err;
  641. if (!TEST_ptr(bio = SSL_get_rbio(ssl)))
  642. goto err;
  643. if (!TEST_ptr_eq(bio, SSL_get_wbio(ssl)))
  644. goto err;
  645. } else if (idx == 1) {
  646. if (!TEST_true(SSL_set_rfd(ssl, fd)))
  647. goto err;
  648. if (!TEST_ptr(bio = SSL_get_rbio(ssl)))
  649. goto err;
  650. if (!TEST_ptr_null(SSL_get_wbio(ssl)))
  651. goto err;
  652. } else {
  653. if (!TEST_true(SSL_set_wfd(ssl, fd)))
  654. goto err;
  655. if (!TEST_ptr(bio = SSL_get_wbio(ssl)))
  656. goto err;
  657. if (!TEST_ptr_null(SSL_get_rbio(ssl)))
  658. goto err;
  659. }
  660. if (!TEST_int_eq(BIO_method_type(bio), BIO_TYPE_DGRAM))
  661. goto err;
  662. if (!TEST_true(BIO_get_fd(bio, &resfd))
  663. || !TEST_int_eq(resfd, fd))
  664. goto err;
  665. testresult = 1;
  666. err:
  667. SSL_free(ssl);
  668. SSL_CTX_free(ctx);
  669. if (fd >= 0)
  670. BIO_closesocket(fd);
  671. return testresult;
  672. }
  673. #define MAXLOOPS 1000
  674. static int test_bio_ssl(void)
  675. {
  676. /*
  677. * We just use OSSL_QUIC_client_method() rather than
  678. * OSSL_QUIC_client_thread_method(). We will never leave the connection idle
  679. * so we will always be implicitly handling time events anyway via other
  680. * IO calls.
  681. */
  682. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  683. SSL *clientquic = NULL, *stream = NULL;
  684. QUIC_TSERVER *qtserv = NULL;
  685. int testresult = 0;
  686. BIO *cbio = NULL, *strbio = NULL, *thisbio;
  687. const char *msg = "Hello world";
  688. int abortctr = 0, err, clienterr = 0, servererr = 0, retc = 0, rets = 0;
  689. size_t written, readbytes, msglen;
  690. int sid = 0, i;
  691. unsigned char buf[80];
  692. if (!TEST_ptr(cctx))
  693. goto err;
  694. cbio = BIO_new_ssl(cctx, 1);
  695. if (!TEST_ptr(cbio))
  696. goto err;
  697. /*
  698. * We must configure the ALPN/peer address etc so we get the SSL object in
  699. * order to pass it to qtest_create_quic_objects for configuration.
  700. */
  701. if (!TEST_int_eq(BIO_get_ssl(cbio, &clientquic), 1))
  702. goto err;
  703. if (!TEST_true(qtest_create_quic_objects(libctx, NULL, NULL, cert, privkey,
  704. 0, &qtserv, &clientquic, NULL,
  705. NULL)))
  706. goto err;
  707. msglen = strlen(msg);
  708. do {
  709. err = BIO_FLAGS_WRITE;
  710. while (!clienterr && !retc && err == BIO_FLAGS_WRITE) {
  711. retc = BIO_write_ex(cbio, msg, msglen, &written);
  712. if (!retc) {
  713. if (BIO_should_retry(cbio))
  714. err = BIO_retry_type(cbio);
  715. else
  716. err = 0;
  717. }
  718. }
  719. if (!clienterr && retc <= 0 && err != BIO_FLAGS_READ) {
  720. TEST_info("BIO_write_ex() failed %d, %d", retc, err);
  721. TEST_openssl_errors();
  722. clienterr = 1;
  723. }
  724. if (!servererr && rets <= 0) {
  725. ossl_quic_tserver_tick(qtserv);
  726. servererr = ossl_quic_tserver_is_term_any(qtserv);
  727. if (!servererr)
  728. rets = ossl_quic_tserver_is_handshake_confirmed(qtserv);
  729. }
  730. if (clienterr && servererr)
  731. goto err;
  732. if (++abortctr == MAXLOOPS) {
  733. TEST_info("No progress made");
  734. goto err;
  735. }
  736. } while ((!retc && !clienterr) || (rets <= 0 && !servererr));
  737. /*
  738. * 2 loops: The first using the default stream, and the second using a new
  739. * client initiated bidi stream.
  740. */
  741. for (i = 0, thisbio = cbio; i < 2; i++) {
  742. if (!TEST_true(ossl_quic_tserver_read(qtserv, sid, buf, sizeof(buf),
  743. &readbytes))
  744. || !TEST_mem_eq(msg, msglen, buf, readbytes))
  745. goto err;
  746. if (!TEST_true(ossl_quic_tserver_write(qtserv, sid, (unsigned char *)msg,
  747. msglen, &written)))
  748. goto err;
  749. ossl_quic_tserver_tick(qtserv);
  750. if (!TEST_true(BIO_read_ex(thisbio, buf, sizeof(buf), &readbytes))
  751. || !TEST_mem_eq(msg, msglen, buf, readbytes))
  752. goto err;
  753. if (i == 1)
  754. break;
  755. if (!TEST_true(SSL_set_mode(clientquic, 0)))
  756. goto err;
  757. /*
  758. * Now create a new stream and repeat. The bottom two bits of the stream
  759. * id represents whether the stream is bidi and whether it is client
  760. * initiated or not. For client initiated bidi they are both 0. So the
  761. * first client initiated bidi stream is 0 and the next one is 4.
  762. */
  763. sid = 4;
  764. stream = SSL_new_stream(clientquic, 0);
  765. if (!TEST_ptr(stream))
  766. goto err;
  767. if (!TEST_true(SSL_set_mode(stream, 0)))
  768. goto err;
  769. thisbio = strbio = BIO_new(BIO_f_ssl());
  770. if (!TEST_ptr(strbio))
  771. goto err;
  772. if (!TEST_int_eq(BIO_set_ssl(thisbio, stream, BIO_CLOSE), 1))
  773. goto err;
  774. stream = NULL;
  775. if (!TEST_true(BIO_write_ex(thisbio, msg, msglen, &written)))
  776. goto err;
  777. ossl_quic_tserver_tick(qtserv);
  778. }
  779. testresult = 1;
  780. err:
  781. BIO_free_all(cbio);
  782. BIO_free_all(strbio);
  783. SSL_free(stream);
  784. ossl_quic_tserver_free(qtserv);
  785. SSL_CTX_free(cctx);
  786. return testresult;
  787. }
  788. #define BACK_PRESSURE_NUM_LOOPS 10000
  789. /*
  790. * Test that sending data from the client to the server faster than the server
  791. * can process it eventually results in back pressure on the client.
  792. */
  793. static int test_back_pressure(void)
  794. {
  795. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  796. SSL *clientquic = NULL;
  797. QUIC_TSERVER *qtserv = NULL;
  798. int testresult = 0;
  799. unsigned char *msg = NULL;
  800. const size_t msglen = 1024;
  801. unsigned char buf[64];
  802. size_t readbytes, written;
  803. int i;
  804. if (!TEST_ptr(cctx)
  805. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  806. privkey, 0, &qtserv,
  807. &clientquic, NULL, NULL))
  808. || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  809. goto err;
  810. msg = OPENSSL_malloc(msglen);
  811. if (!TEST_ptr(msg))
  812. goto err;
  813. if (!TEST_int_eq(RAND_bytes_ex(libctx, msg, msglen, 0), 1))
  814. goto err;
  815. /*
  816. * Limit to 10000 loops. If we've not seen any back pressure after that
  817. * we're going to run out of memory, so abort.
  818. */
  819. for (i = 0; i < BACK_PRESSURE_NUM_LOOPS; i++) {
  820. /* Send data from the client */
  821. if (!SSL_write_ex(clientquic, msg, msglen, &written)) {
  822. /* Check if we are seeing back pressure */
  823. if (SSL_get_error(clientquic, 0) == SSL_ERROR_WANT_WRITE)
  824. break;
  825. TEST_error("Unexpected client failure");
  826. goto err;
  827. }
  828. /* Receive data at the server */
  829. ossl_quic_tserver_tick(qtserv);
  830. if (!TEST_true(ossl_quic_tserver_read(qtserv, 0, buf, sizeof(buf),
  831. &readbytes)))
  832. goto err;
  833. }
  834. if (i == BACK_PRESSURE_NUM_LOOPS) {
  835. TEST_error("No back pressure seen");
  836. goto err;
  837. }
  838. testresult = 1;
  839. err:
  840. SSL_free(clientquic);
  841. ossl_quic_tserver_free(qtserv);
  842. SSL_CTX_free(cctx);
  843. OPENSSL_free(msg);
  844. return testresult;
  845. }
  846. static int dgram_ctr = 0;
  847. static void dgram_cb(int write_p, int version, int content_type,
  848. const void *buf, size_t msglen, SSL *ssl, void *arg)
  849. {
  850. if (!write_p)
  851. return;
  852. if (content_type != SSL3_RT_QUIC_DATAGRAM)
  853. return;
  854. dgram_ctr++;
  855. }
  856. /* Test that we send multiple datagrams in one go when appropriate */
  857. static int test_multiple_dgrams(void)
  858. {
  859. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  860. SSL *clientquic = NULL;
  861. QUIC_TSERVER *qtserv = NULL;
  862. int testresult = 0;
  863. unsigned char *buf;
  864. const size_t buflen = 1400;
  865. size_t written;
  866. buf = OPENSSL_zalloc(buflen);
  867. if (!TEST_ptr(cctx)
  868. || !TEST_ptr(buf)
  869. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  870. privkey, 0, &qtserv,
  871. &clientquic, NULL, NULL))
  872. || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  873. goto err;
  874. dgram_ctr = 0;
  875. SSL_set_msg_callback(clientquic, dgram_cb);
  876. if (!TEST_true(SSL_write_ex(clientquic, buf, buflen, &written))
  877. || !TEST_size_t_eq(written, buflen)
  878. /* We wrote enough data for 2 datagrams */
  879. || !TEST_int_eq(dgram_ctr, 2))
  880. goto err;
  881. testresult = 1;
  882. err:
  883. OPENSSL_free(buf);
  884. SSL_free(clientquic);
  885. ossl_quic_tserver_free(qtserv);
  886. SSL_CTX_free(cctx);
  887. return testresult;
  888. }
  889. static int non_io_retry_cert_verify_cb(X509_STORE_CTX *ctx, void *arg)
  890. {
  891. int idx = SSL_get_ex_data_X509_STORE_CTX_idx();
  892. SSL *ssl;
  893. const int *allow = (int *)arg;
  894. /* this should not happen but check anyway */
  895. if (idx < 0
  896. || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL)
  897. return 0;
  898. /* If this is our first attempt then retry */
  899. if (*allow == 0)
  900. return SSL_set_retry_verify(ssl);
  901. /* Otherwise do nothing - verification succeeds. Continue as normal */
  902. return 1;
  903. }
  904. /* Test that we can handle a non-io related retry error
  905. * Test 0: Non-blocking
  906. * Test 1: Blocking
  907. */
  908. static int test_non_io_retry(int idx)
  909. {
  910. SSL_CTX *cctx;
  911. SSL *clientquic = NULL;
  912. QUIC_TSERVER *qtserv = NULL;
  913. int testresult = 0;
  914. int flags = 0, allow = 0;
  915. if (idx >= 1 && !qtest_supports_blocking())
  916. return TEST_skip("Blocking tests not supported in this build");
  917. cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  918. if (!TEST_ptr(cctx))
  919. goto err;
  920. SSL_CTX_set_cert_verify_callback(cctx, non_io_retry_cert_verify_cb, &allow);
  921. flags = (idx >= 1) ? QTEST_FLAG_BLOCK : 0;
  922. if (!TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert, privkey,
  923. flags, &qtserv, &clientquic, NULL,
  924. NULL))
  925. || !TEST_true(qtest_create_quic_connection_ex(qtserv, clientquic,
  926. SSL_ERROR_WANT_RETRY_VERIFY))
  927. || !TEST_int_eq(SSL_want(clientquic), SSL_RETRY_VERIFY))
  928. goto err;
  929. allow = 1;
  930. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  931. goto err;
  932. testresult = 1;
  933. err:
  934. SSL_free(clientquic);
  935. ossl_quic_tserver_free(qtserv);
  936. SSL_CTX_free(cctx);
  937. return testresult;
  938. }
  939. static int use_session_cb_cnt = 0;
  940. static int find_session_cb_cnt = 0;
  941. static const char *pskid = "Identity";
  942. static SSL_SESSION *serverpsk = NULL, *clientpsk = NULL;
  943. static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
  944. size_t *idlen, SSL_SESSION **sess)
  945. {
  946. use_session_cb_cnt++;
  947. if (clientpsk == NULL)
  948. return 0;
  949. SSL_SESSION_up_ref(clientpsk);
  950. *sess = clientpsk;
  951. *id = (const unsigned char *)pskid;
  952. *idlen = strlen(pskid);
  953. return 1;
  954. }
  955. static int find_session_cb(SSL *ssl, const unsigned char *identity,
  956. size_t identity_len, SSL_SESSION **sess)
  957. {
  958. find_session_cb_cnt++;
  959. if (serverpsk == NULL)
  960. return 0;
  961. /* Identity should match that set by the client */
  962. if (strlen(pskid) != identity_len
  963. || strncmp(pskid, (const char *)identity, identity_len) != 0)
  964. return 0;
  965. SSL_SESSION_up_ref(serverpsk);
  966. *sess = serverpsk;
  967. return 1;
  968. }
  969. static int test_quic_psk(void)
  970. {
  971. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  972. SSL *clientquic = NULL;
  973. QUIC_TSERVER *qtserv = NULL;
  974. int testresult = 0;
  975. if (!TEST_ptr(cctx)
  976. /* No cert or private key for the server, i.e. PSK only */
  977. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, NULL,
  978. NULL, 0, &qtserv,
  979. &clientquic, NULL, NULL)))
  980. goto end;
  981. SSL_set_psk_use_session_callback(clientquic, use_session_cb);
  982. ossl_quic_tserver_set_psk_find_session_cb(qtserv, find_session_cb);
  983. use_session_cb_cnt = 0;
  984. find_session_cb_cnt = 0;
  985. clientpsk = serverpsk = create_a_psk(clientquic, SHA384_DIGEST_LENGTH);
  986. if (!TEST_ptr(clientpsk))
  987. goto end;
  988. /* We already had one ref. Add another one */
  989. SSL_SESSION_up_ref(clientpsk);
  990. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic))
  991. || !TEST_int_eq(1, find_session_cb_cnt)
  992. || !TEST_int_eq(1, use_session_cb_cnt)
  993. /* Check that we actually used the PSK */
  994. || !TEST_true(SSL_session_reused(clientquic)))
  995. goto end;
  996. testresult = 1;
  997. end:
  998. SSL_free(clientquic);
  999. ossl_quic_tserver_free(qtserv);
  1000. SSL_CTX_free(cctx);
  1001. SSL_SESSION_free(clientpsk);
  1002. SSL_SESSION_free(serverpsk);
  1003. clientpsk = serverpsk = NULL;
  1004. return testresult;
  1005. }
  1006. static int test_client_auth(int idx)
  1007. {
  1008. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  1009. SSL_CTX *sctx = SSL_CTX_new_ex(libctx, NULL, TLS_method());
  1010. SSL *clientquic = NULL;
  1011. QUIC_TSERVER *qtserv = NULL;
  1012. int testresult = 0;
  1013. unsigned char buf[20];
  1014. static char *msg = "A test message";
  1015. size_t msglen = strlen(msg);
  1016. size_t numbytes = 0;
  1017. if (!TEST_ptr(cctx) || !TEST_ptr(sctx))
  1018. goto err;
  1019. SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT
  1020. | SSL_VERIFY_CLIENT_ONCE, NULL);
  1021. if (!TEST_true(SSL_CTX_load_verify_file(sctx, cauthca)))
  1022. goto err;
  1023. if (idx > 0
  1024. && (!TEST_true(SSL_CTX_use_certificate_chain_file(cctx, ccert))
  1025. || !TEST_true(SSL_CTX_use_PrivateKey_file(cctx, cprivkey,
  1026. SSL_FILETYPE_PEM))))
  1027. goto err;
  1028. if (!TEST_true(qtest_create_quic_objects(libctx, cctx, sctx, cert,
  1029. privkey, 0, &qtserv,
  1030. &clientquic, NULL, NULL)))
  1031. goto err;
  1032. if (idx > 1) {
  1033. if (!TEST_true(ssl_ctx_add_large_cert_chain(libctx, cctx, ccert))
  1034. || !TEST_true(ssl_ctx_add_large_cert_chain(libctx, sctx, cert)))
  1035. goto err;
  1036. }
  1037. if (idx == 0) {
  1038. if (!TEST_false(qtest_create_quic_connection(qtserv, clientquic)))
  1039. goto err;
  1040. /* negative test passed */
  1041. testresult = 1;
  1042. goto err;
  1043. }
  1044. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  1045. goto err;
  1046. /* Check that sending and receiving app data is ok */
  1047. if (!TEST_true(SSL_write_ex(clientquic, msg, msglen, &numbytes))
  1048. || !TEST_size_t_eq(numbytes, msglen))
  1049. goto err;
  1050. ossl_quic_tserver_tick(qtserv);
  1051. if (!TEST_true(ossl_quic_tserver_write(qtserv, 0,
  1052. (unsigned char *)msg,
  1053. msglen, &numbytes)))
  1054. goto err;
  1055. ossl_quic_tserver_tick(qtserv);
  1056. SSL_handle_events(clientquic);
  1057. if (!TEST_true(SSL_read_ex(clientquic, buf, sizeof(buf), &numbytes))
  1058. || !TEST_size_t_eq(numbytes, msglen)
  1059. || !TEST_mem_eq(buf, numbytes, msg, msglen))
  1060. goto err;
  1061. if (!TEST_true(qtest_shutdown(qtserv, clientquic)))
  1062. goto err;
  1063. testresult = 1;
  1064. err:
  1065. SSL_free(clientquic);
  1066. ossl_quic_tserver_free(qtserv);
  1067. SSL_CTX_free(sctx);
  1068. SSL_CTX_free(cctx);
  1069. return testresult;
  1070. }
  1071. /*
  1072. * Test that we correctly handle ALPN supplied by the application
  1073. * Test 0: ALPN is provided
  1074. * Test 1: No ALPN is provided
  1075. */
  1076. static int test_alpn(int idx)
  1077. {
  1078. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  1079. SSL *clientquic = NULL;
  1080. QUIC_TSERVER *qtserv = NULL;
  1081. int testresult = 0;
  1082. int ret;
  1083. /*
  1084. * Ensure we only configure ciphersuites that are available with both the
  1085. * default and fips providers to get the same output in both cases
  1086. */
  1087. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_128_GCM_SHA256")))
  1088. goto err;
  1089. if (!TEST_ptr(cctx)
  1090. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  1091. privkey,
  1092. QTEST_FLAG_FAKE_TIME,
  1093. &qtserv,
  1094. &clientquic, NULL, NULL)))
  1095. goto err;
  1096. if (idx == 0) {
  1097. /*
  1098. * Clear the ALPN we set in qtest_create_quic_objects. We use TEST_false
  1099. * because SSL_set_alpn_protos returns 0 for success.
  1100. */
  1101. if (!TEST_false(SSL_set_alpn_protos(clientquic, NULL, 0)))
  1102. goto err;
  1103. }
  1104. ret = SSL_connect(clientquic);
  1105. if (!TEST_int_le(ret, 0))
  1106. goto err;
  1107. if (idx == 0) {
  1108. /* We expect an immediate error due to lack of ALPN */
  1109. if (!TEST_int_eq(SSL_get_error(clientquic, ret), SSL_ERROR_SSL))
  1110. goto err;
  1111. } else {
  1112. /* ALPN was provided so we expect the connection to succeed */
  1113. if (!TEST_int_eq(SSL_get_error(clientquic, ret), SSL_ERROR_WANT_READ)
  1114. || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  1115. goto err;
  1116. }
  1117. testresult = 1;
  1118. err:
  1119. ossl_quic_tserver_free(qtserv);
  1120. SSL_free(clientquic);
  1121. SSL_CTX_free(cctx);
  1122. return testresult;
  1123. }
  1124. /*
  1125. * Test SSL_get_shutdown() behavior.
  1126. */
  1127. static int test_get_shutdown(void)
  1128. {
  1129. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  1130. SSL *clientquic = NULL;
  1131. QUIC_TSERVER *qtserv = NULL;
  1132. int testresult = 0;
  1133. if (!TEST_ptr(cctx)
  1134. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  1135. privkey,
  1136. QTEST_FLAG_FAKE_TIME,
  1137. &qtserv, &clientquic,
  1138. NULL, NULL))
  1139. || !TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  1140. goto err;
  1141. if (!TEST_int_eq(SSL_get_shutdown(clientquic), 0))
  1142. goto err;
  1143. if (!TEST_int_eq(SSL_shutdown(clientquic), 0))
  1144. goto err;
  1145. if (!TEST_int_eq(SSL_get_shutdown(clientquic), SSL_SENT_SHUTDOWN))
  1146. goto err;
  1147. do {
  1148. ossl_quic_tserver_tick(qtserv);
  1149. qtest_add_time(100);
  1150. } while (SSL_shutdown(clientquic) == 0);
  1151. if (!TEST_int_eq(SSL_get_shutdown(clientquic),
  1152. SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN))
  1153. goto err;
  1154. testresult = 1;
  1155. err:
  1156. ossl_quic_tserver_free(qtserv);
  1157. SSL_free(clientquic);
  1158. SSL_CTX_free(cctx);
  1159. return testresult;
  1160. }
  1161. #define MAX_LOOPS 2000
  1162. /*
  1163. * Keep retrying SSL_read_ex until it succeeds or we give up. Accept a stream
  1164. * if we don't already have one
  1165. */
  1166. static int unreliable_client_read(SSL *clientquic, SSL **stream, void *buf,
  1167. size_t buflen, size_t *readbytes,
  1168. QUIC_TSERVER *qtserv)
  1169. {
  1170. int abortctr;
  1171. /* We just do this in a loop with a sleep for simplicity */
  1172. for (abortctr = 0; abortctr < MAX_LOOPS; abortctr++) {
  1173. if (*stream == NULL) {
  1174. SSL_handle_events(clientquic);
  1175. *stream = SSL_accept_stream(clientquic, 0);
  1176. }
  1177. if (*stream != NULL) {
  1178. if (SSL_read_ex(*stream, buf, buflen, readbytes))
  1179. return 1;
  1180. if (!TEST_int_eq(SSL_get_error(*stream, 0), SSL_ERROR_WANT_READ))
  1181. return 0;
  1182. }
  1183. ossl_quic_tserver_tick(qtserv);
  1184. qtest_add_time(1);
  1185. qtest_wait_for_timeout(clientquic, qtserv);
  1186. }
  1187. TEST_error("No progress made");
  1188. return 0;
  1189. }
  1190. /* Keep retrying ossl_quic_tserver_read until it succeeds or we give up */
  1191. static int unreliable_server_read(QUIC_TSERVER *qtserv, uint64_t sid,
  1192. void *buf, size_t buflen, size_t *readbytes,
  1193. SSL *clientquic)
  1194. {
  1195. int abortctr;
  1196. /* We just do this in a loop with a sleep for simplicity */
  1197. for (abortctr = 0; abortctr < MAX_LOOPS; abortctr++) {
  1198. if (ossl_quic_tserver_read(qtserv, sid, buf, buflen, readbytes)
  1199. && *readbytes > 1)
  1200. return 1;
  1201. ossl_quic_tserver_tick(qtserv);
  1202. SSL_handle_events(clientquic);
  1203. qtest_add_time(1);
  1204. qtest_wait_for_timeout(clientquic, qtserv);
  1205. }
  1206. TEST_error("No progress made");
  1207. return 0;
  1208. }
  1209. /*
  1210. * Create a connection and send data using an unreliable transport. We introduce
  1211. * random noise to drop, delay and duplicate datagrams.
  1212. * Test 0: Introduce random noise to datagrams
  1213. * Test 1: As with test 0 but also split datagrams containing multiple packets
  1214. * into individual datagrams so that individual packets can be affected
  1215. * by noise - not just a whole datagram.
  1216. */
  1217. static int test_noisy_dgram(int idx)
  1218. {
  1219. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  1220. SSL *clientquic = NULL, *stream[2] = { NULL, NULL };
  1221. QUIC_TSERVER *qtserv = NULL;
  1222. int testresult = 0;
  1223. uint64_t sid = 0;
  1224. char *msg = "Hello world!";
  1225. size_t msglen = strlen(msg), written, readbytes, i, j;
  1226. unsigned char buf[80];
  1227. int flags = QTEST_FLAG_NOISE | QTEST_FLAG_FAKE_TIME;
  1228. QTEST_FAULT *fault = NULL;
  1229. if (idx == 1)
  1230. flags |= QTEST_FLAG_PACKET_SPLIT;
  1231. if (!TEST_ptr(cctx)
  1232. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  1233. privkey, flags,
  1234. &qtserv,
  1235. &clientquic, &fault, NULL)))
  1236. goto err;
  1237. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  1238. goto err;
  1239. if (!TEST_true(SSL_set_incoming_stream_policy(clientquic,
  1240. SSL_INCOMING_STREAM_POLICY_ACCEPT,
  1241. 0))
  1242. || !TEST_true(SSL_set_default_stream_mode(clientquic,
  1243. SSL_DEFAULT_STREAM_MODE_NONE)))
  1244. goto err;
  1245. for (j = 0; j < 2; j++) {
  1246. if (!TEST_true(ossl_quic_tserver_stream_new(qtserv, 0, &sid)))
  1247. goto err;
  1248. ossl_quic_tserver_tick(qtserv);
  1249. qtest_add_time(1);
  1250. /*
  1251. * Send data from the server to the client. Some datagrams may get
  1252. * lost, modified, dropped or re-ordered. We repeat 20 times to ensure
  1253. * we are sending enough datagrams for problems to be noticed.
  1254. */
  1255. for (i = 0; i < 20; i++) {
  1256. if (!TEST_true(ossl_quic_tserver_write(qtserv, sid,
  1257. (unsigned char *)msg, msglen,
  1258. &written))
  1259. || !TEST_size_t_eq(msglen, written))
  1260. goto err;
  1261. ossl_quic_tserver_tick(qtserv);
  1262. qtest_add_time(1);
  1263. /*
  1264. * Since the underlying BIO is now noisy we may get failures that
  1265. * need to be retried - so we use unreliable_client_read() to
  1266. * handle that
  1267. */
  1268. if (!TEST_true(unreliable_client_read(clientquic, &stream[j], buf,
  1269. sizeof(buf), &readbytes,
  1270. qtserv))
  1271. || !TEST_mem_eq(msg, msglen, buf, readbytes))
  1272. goto err;
  1273. }
  1274. /* Send data from the client to the server */
  1275. for (i = 0; i < 20; i++) {
  1276. if (!TEST_true(SSL_write_ex(stream[j], (unsigned char *)msg,
  1277. msglen, &written))
  1278. || !TEST_size_t_eq(msglen, written))
  1279. goto err;
  1280. ossl_quic_tserver_tick(qtserv);
  1281. qtest_add_time(1);
  1282. /*
  1283. * Since the underlying BIO is now noisy we may get failures that
  1284. * need to be retried - so we use unreliable_server_read() to
  1285. * handle that
  1286. */
  1287. if (!TEST_true(unreliable_server_read(qtserv, sid, buf, sizeof(buf),
  1288. &readbytes, clientquic))
  1289. || !TEST_mem_eq(msg, msglen, buf, readbytes))
  1290. goto err;
  1291. }
  1292. }
  1293. testresult = 1;
  1294. err:
  1295. ossl_quic_tserver_free(qtserv);
  1296. SSL_free(stream[0]);
  1297. SSL_free(stream[1]);
  1298. SSL_free(clientquic);
  1299. SSL_CTX_free(cctx);
  1300. qtest_fault_free(fault);
  1301. return testresult;
  1302. }
  1303. /*
  1304. * Create a connection and send some big data using a transport with limited bandwidth.
  1305. */
  1306. #define TEST_TRANSFER_DATA_SIZE (2*1024*1024) /* 2 MBytes */
  1307. #define TEST_SINGLE_WRITE_SIZE (16*1024) /* 16 kBytes */
  1308. #define TEST_BW_LIMIT 1000 /* 1000 Bytes/ms */
  1309. static int test_bw_limit(void)
  1310. {
  1311. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method());
  1312. SSL *clientquic = NULL;
  1313. QUIC_TSERVER *qtserv = NULL;
  1314. int testresult = 0;
  1315. unsigned char *msg = NULL, *recvbuf = NULL;
  1316. size_t sendlen = TEST_TRANSFER_DATA_SIZE;
  1317. size_t recvlen = TEST_TRANSFER_DATA_SIZE;
  1318. size_t written, readbytes;
  1319. int flags = QTEST_FLAG_NOISE | QTEST_FLAG_FAKE_TIME;
  1320. QTEST_FAULT *fault = NULL;
  1321. uint64_t real_bw;
  1322. if (!TEST_ptr(cctx)
  1323. || !TEST_true(qtest_create_quic_objects(libctx, cctx, NULL, cert,
  1324. privkey, flags,
  1325. &qtserv,
  1326. &clientquic, &fault, NULL)))
  1327. goto err;
  1328. if (!TEST_ptr(msg = OPENSSL_zalloc(TEST_SINGLE_WRITE_SIZE))
  1329. || !TEST_ptr(recvbuf = OPENSSL_zalloc(TEST_SINGLE_WRITE_SIZE)))
  1330. goto err;
  1331. /* Set BW to 1000 Bytes/ms -> 1MByte/s both ways */
  1332. if (!TEST_true(qtest_fault_set_bw_limit(fault, 1000, 1000, 0)))
  1333. goto err;
  1334. if (!TEST_true(qtest_create_quic_connection(qtserv, clientquic)))
  1335. goto err;
  1336. qtest_start_stopwatch();
  1337. while (recvlen > 0) {
  1338. qtest_add_time(1);
  1339. if (sendlen > 0) {
  1340. if (!SSL_write_ex(clientquic, msg,
  1341. sendlen > TEST_SINGLE_WRITE_SIZE ? TEST_SINGLE_WRITE_SIZE
  1342. : sendlen,
  1343. &written)) {
  1344. TEST_info("Retrying to send: %llu", (unsigned long long) sendlen);
  1345. if (!TEST_int_eq(SSL_get_error(clientquic, 0), SSL_ERROR_WANT_WRITE))
  1346. goto err;
  1347. } else {
  1348. sendlen -= written;
  1349. TEST_info("Remaining to send: %llu", (unsigned long long) sendlen);
  1350. }
  1351. } else {
  1352. SSL_handle_events(clientquic);
  1353. }
  1354. if (ossl_quic_tserver_read(qtserv, 0, recvbuf,
  1355. recvlen > TEST_SINGLE_WRITE_SIZE ? TEST_SINGLE_WRITE_SIZE
  1356. : recvlen,
  1357. &readbytes)
  1358. && readbytes > 1) {
  1359. recvlen -= readbytes;
  1360. TEST_info("Remaining to recv: %llu", (unsigned long long) recvlen);
  1361. } else {
  1362. TEST_info("No progress on recv: %llu", (unsigned long long) recvlen);
  1363. }
  1364. ossl_quic_tserver_tick(qtserv);
  1365. }
  1366. real_bw = TEST_TRANSFER_DATA_SIZE / qtest_get_stopwatch_time();
  1367. TEST_info("BW limit: %d Bytes/ms Real bandwidth reached: %llu Bytes/ms",
  1368. TEST_BW_LIMIT, (unsigned long long)real_bw);
  1369. if (!TEST_uint64_t_lt(real_bw, TEST_BW_LIMIT))
  1370. goto err;
  1371. testresult = 1;
  1372. err:
  1373. OPENSSL_free(msg);
  1374. OPENSSL_free(recvbuf);
  1375. ossl_quic_tserver_free(qtserv);
  1376. SSL_free(clientquic);
  1377. SSL_CTX_free(cctx);
  1378. qtest_fault_free(fault);
  1379. return testresult;
  1380. }
  1381. enum {
  1382. TPARAM_OP_DUP,
  1383. TPARAM_OP_DROP,
  1384. TPARAM_OP_INJECT,
  1385. TPARAM_OP_INJECT_TWICE,
  1386. TPARAM_OP_INJECT_RAW,
  1387. TPARAM_OP_DROP_INJECT,
  1388. TPARAM_OP_MUTATE
  1389. };
  1390. #define TPARAM_CHECK_DUP(name, reason) \
  1391. { QUIC_TPARAM_##name, TPARAM_OP_DUP, (reason) },
  1392. #define TPARAM_CHECK_DROP(name, reason) \
  1393. { QUIC_TPARAM_##name, TPARAM_OP_DROP, (reason) },
  1394. #define TPARAM_CHECK_INJECT(name, buf, buf_len, reason) \
  1395. { QUIC_TPARAM_##name, TPARAM_OP_INJECT, (reason), \
  1396. (buf), (buf_len) },
  1397. #define TPARAM_CHECK_INJECT_A(name, buf, reason) \
  1398. TPARAM_CHECK_INJECT(name, buf, sizeof(buf), reason)
  1399. #define TPARAM_CHECK_DROP_INJECT(name, buf, buf_len, reason) \
  1400. { QUIC_TPARAM_##name, TPARAM_OP_DROP_INJECT, (reason), \
  1401. (buf), (buf_len) },
  1402. #define TPARAM_CHECK_DROP_INJECT_A(name, buf, reason) \
  1403. TPARAM_CHECK_DROP_INJECT(name, buf, sizeof(buf), reason)
  1404. #define TPARAM_CHECK_INJECT_TWICE(name, buf, buf_len, reason) \
  1405. { QUIC_TPARAM_##name, TPARAM_OP_INJECT_TWICE, (reason), \
  1406. (buf), (buf_len) },
  1407. #define TPARAM_CHECK_INJECT_TWICE_A(name, buf, reason) \
  1408. TPARAM_CHECK_INJECT_TWICE(name, buf, sizeof(buf), reason)
  1409. #define TPARAM_CHECK_INJECT_RAW(buf, buf_len, reason) \
  1410. { 0, TPARAM_OP_INJECT_RAW, (reason), \
  1411. (buf), (buf_len) },
  1412. #define TPARAM_CHECK_INJECT_RAW_A(buf, reason) \
  1413. TPARAM_CHECK_INJECT_RAW(buf, sizeof(buf), reason)
  1414. #define TPARAM_CHECK_MUTATE(name, reason) \
  1415. { QUIC_TPARAM_##name, TPARAM_OP_MUTATE, (reason) },
  1416. #define TPARAM_CHECK_INT(name, reason) \
  1417. TPARAM_CHECK_DROP_INJECT(name, NULL, 0, reason) \
  1418. TPARAM_CHECK_DROP_INJECT_A(name, bogus_int, reason) \
  1419. TPARAM_CHECK_DROP_INJECT_A(name, int_with_trailer, reason)
  1420. struct tparam_test {
  1421. uint64_t id;
  1422. int op;
  1423. const char *expect_fail; /* substring to expect in reason */
  1424. const void *buf;
  1425. size_t buf_len;
  1426. };
  1427. static const unsigned char retry_scid_1[8] = { 0 };
  1428. static const unsigned char disable_active_migration_1[] = {
  1429. 0x00
  1430. };
  1431. static const unsigned char malformed_stateless_reset_token_1[] = {
  1432. 0x02, 0xff
  1433. };
  1434. static const unsigned char malformed_stateless_reset_token_2[] = {
  1435. 0x01
  1436. };
  1437. static const unsigned char malformed_stateless_reset_token_3[15] = { 0 };
  1438. static const unsigned char malformed_stateless_reset_token_4[17] = { 0 };
  1439. static const unsigned char malformed_preferred_addr_1[] = {
  1440. 0x0d, 0xff
  1441. };
  1442. static const unsigned char malformed_preferred_addr_2[42] = {
  1443. 0x0d, 0x28, /* too short */
  1444. };
  1445. static const unsigned char malformed_preferred_addr_3[64] = {
  1446. 0x0d, 0x3e, /* too long */
  1447. };
  1448. static const unsigned char malformed_preferred_addr_4[] = {
  1449. /* TPARAM too short for CID length indicated */
  1450. 0x0d, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1451. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1452. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1453. 0x00, 0x00, 0x01, 0x55,
  1454. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1455. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1456. };
  1457. static const unsigned char malformed_unknown_1[] = {
  1458. 0xff
  1459. };
  1460. static const unsigned char malformed_unknown_2[] = {
  1461. 0x55, 0x55,
  1462. };
  1463. static const unsigned char malformed_unknown_3[] = {
  1464. 0x55, 0x55, 0x01,
  1465. };
  1466. static const unsigned char ack_delay_exp[] = {
  1467. 0x03
  1468. };
  1469. static const unsigned char stateless_reset_token[16] = { 0x42 };
  1470. static const unsigned char preferred_addr[] = {
  1471. 0x44, 0x44, 0x44, 0x44,
  1472. 0x55, 0x55,
  1473. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  1474. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  1475. 0x77, 0x77,
  1476. 0x02, 0xAA, 0xBB,
  1477. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  1478. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  1479. };
  1480. static const unsigned char long_cid[21] = { 0x42 };
  1481. static const unsigned char excess_ack_delay_exp[] = {
  1482. 0x15,
  1483. };
  1484. static const unsigned char excess_max_ack_delay[] = {
  1485. 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
  1486. };
  1487. static const unsigned char excess_initial_max_streams[] = {
  1488. 0xD0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  1489. };
  1490. static const unsigned char undersize_udp_payload_size[] = {
  1491. 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xaf,
  1492. };
  1493. static const unsigned char undersize_active_conn_id_limit[] = {
  1494. 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  1495. };
  1496. static const unsigned char bogus_int[9] = { 0 };
  1497. static const unsigned char int_with_trailer[2] = { 0x01 };
  1498. #define QUIC_TPARAM_UNKNOWN_1 0xf1f1
  1499. static const struct tparam_test tparam_tests[] = {
  1500. TPARAM_CHECK_DUP(ORIG_DCID,
  1501. "ORIG_DCID appears multiple times")
  1502. TPARAM_CHECK_DUP(INITIAL_SCID,
  1503. "INITIAL_SCID appears multiple times")
  1504. TPARAM_CHECK_DUP(INITIAL_MAX_DATA,
  1505. "INITIAL_MAX_DATA appears multiple times")
  1506. TPARAM_CHECK_DUP(INITIAL_MAX_STREAM_DATA_BIDI_LOCAL,
  1507. "INITIAL_MAX_STREAM_DATA_BIDI_LOCAL appears multiple times")
  1508. TPARAM_CHECK_DUP(INITIAL_MAX_STREAM_DATA_BIDI_REMOTE,
  1509. "INITIAL_MAX_STREAM_DATA_BIDI_REMOTE appears multiple times")
  1510. TPARAM_CHECK_DUP(INITIAL_MAX_STREAM_DATA_UNI,
  1511. "INITIAL_MAX_STREAM_DATA_UNI appears multiple times")
  1512. TPARAM_CHECK_DUP(INITIAL_MAX_STREAMS_BIDI,
  1513. "INITIAL_MAX_STREAMS_BIDI appears multiple times")
  1514. TPARAM_CHECK_DUP(INITIAL_MAX_STREAMS_UNI,
  1515. "INITIAL_MAX_STREAMS_UNI appears multiple times")
  1516. TPARAM_CHECK_DUP(MAX_IDLE_TIMEOUT,
  1517. "MAX_IDLE_TIMEOUT appears multiple times")
  1518. TPARAM_CHECK_DUP(MAX_UDP_PAYLOAD_SIZE,
  1519. "MAX_UDP_PAYLOAD_SIZE appears multiple times")
  1520. TPARAM_CHECK_DUP(ACTIVE_CONN_ID_LIMIT,
  1521. "ACTIVE_CONN_ID_LIMIT appears multiple times")
  1522. TPARAM_CHECK_DUP(DISABLE_ACTIVE_MIGRATION,
  1523. "DISABLE_ACTIVE_MIGRATION appears multiple times")
  1524. TPARAM_CHECK_DROP(INITIAL_SCID,
  1525. "INITIAL_SCID was not sent but is required")
  1526. TPARAM_CHECK_DROP(ORIG_DCID,
  1527. "ORIG_DCID was not sent but is required")
  1528. TPARAM_CHECK_INJECT_A(RETRY_SCID, retry_scid_1,
  1529. "RETRY_SCID sent when not performing a retry")
  1530. TPARAM_CHECK_DROP_INJECT_A(DISABLE_ACTIVE_MIGRATION, disable_active_migration_1,
  1531. "DISABLE_ACTIVE_MIGRATION is malformed")
  1532. TPARAM_CHECK_INJECT(UNKNOWN_1, NULL, 0,
  1533. NULL)
  1534. TPARAM_CHECK_INJECT_RAW_A(malformed_stateless_reset_token_1,
  1535. "STATELESS_RESET_TOKEN is malformed")
  1536. TPARAM_CHECK_INJECT_A(STATELESS_RESET_TOKEN,
  1537. malformed_stateless_reset_token_2,
  1538. "STATELESS_RESET_TOKEN is malformed")
  1539. TPARAM_CHECK_INJECT_A(STATELESS_RESET_TOKEN,
  1540. malformed_stateless_reset_token_3,
  1541. "STATELESS_RESET_TOKEN is malformed")
  1542. TPARAM_CHECK_INJECT_A(STATELESS_RESET_TOKEN,
  1543. malformed_stateless_reset_token_4,
  1544. "STATELESS_RESET_TOKEN is malformed")
  1545. TPARAM_CHECK_INJECT(STATELESS_RESET_TOKEN,
  1546. NULL, 0,
  1547. "STATELESS_RESET_TOKEN is malformed")
  1548. TPARAM_CHECK_INJECT_RAW_A(malformed_preferred_addr_1,
  1549. "PREFERRED_ADDR is malformed")
  1550. TPARAM_CHECK_INJECT_RAW_A(malformed_preferred_addr_2,
  1551. "PREFERRED_ADDR is malformed")
  1552. TPARAM_CHECK_INJECT_RAW_A(malformed_preferred_addr_3,
  1553. "PREFERRED_ADDR is malformed")
  1554. TPARAM_CHECK_INJECT_RAW_A(malformed_preferred_addr_4,
  1555. "PREFERRED_ADDR is malformed")
  1556. TPARAM_CHECK_INJECT_RAW_A(malformed_unknown_1,
  1557. "bad transport parameter")
  1558. TPARAM_CHECK_INJECT_RAW_A(malformed_unknown_2,
  1559. "bad transport parameter")
  1560. TPARAM_CHECK_INJECT_RAW_A(malformed_unknown_3,
  1561. "bad transport parameter")
  1562. TPARAM_CHECK_INJECT_A(ACK_DELAY_EXP, excess_ack_delay_exp,
  1563. "ACK_DELAY_EXP is malformed")
  1564. TPARAM_CHECK_INJECT_A(MAX_ACK_DELAY, excess_max_ack_delay,
  1565. "MAX_ACK_DELAY is malformed")
  1566. TPARAM_CHECK_DROP_INJECT_A(INITIAL_MAX_STREAMS_BIDI, excess_initial_max_streams,
  1567. "INITIAL_MAX_STREAMS_BIDI is malformed")
  1568. TPARAM_CHECK_DROP_INJECT_A(INITIAL_MAX_STREAMS_UNI, excess_initial_max_streams,
  1569. "INITIAL_MAX_STREAMS_UNI is malformed")
  1570. TPARAM_CHECK_DROP_INJECT_A(MAX_UDP_PAYLOAD_SIZE, undersize_udp_payload_size,
  1571. "MAX_UDP_PAYLOAD_SIZE is malformed")
  1572. TPARAM_CHECK_DROP_INJECT_A(ACTIVE_CONN_ID_LIMIT, undersize_active_conn_id_limit,
  1573. "ACTIVE_CONN_ID_LIMIT is malformed")
  1574. TPARAM_CHECK_INJECT_TWICE_A(ACK_DELAY_EXP, ack_delay_exp,
  1575. "ACK_DELAY_EXP appears multiple times")
  1576. TPARAM_CHECK_INJECT_TWICE_A(MAX_ACK_DELAY, ack_delay_exp,
  1577. "MAX_ACK_DELAY appears multiple times")
  1578. TPARAM_CHECK_INJECT_TWICE_A(STATELESS_RESET_TOKEN, stateless_reset_token,
  1579. "STATELESS_RESET_TOKEN appears multiple times")
  1580. TPARAM_CHECK_INJECT_TWICE_A(PREFERRED_ADDR, preferred_addr,
  1581. "PREFERRED_ADDR appears multiple times")
  1582. TPARAM_CHECK_MUTATE(ORIG_DCID,
  1583. "ORIG_DCID does not match expected value")
  1584. TPARAM_CHECK_MUTATE(INITIAL_SCID,
  1585. "INITIAL_SCID does not match expected value")
  1586. TPARAM_CHECK_DROP_INJECT_A(ORIG_DCID, long_cid,
  1587. "ORIG_DCID is malformed")
  1588. TPARAM_CHECK_DROP_INJECT_A(INITIAL_SCID, long_cid,
  1589. "INITIAL_SCID is malformed")
  1590. TPARAM_CHECK_INT(INITIAL_MAX_DATA,
  1591. "INITIAL_MAX_DATA is malformed")
  1592. TPARAM_CHECK_INT(INITIAL_MAX_STREAM_DATA_BIDI_LOCAL,
  1593. "INITIAL_MAX_STREAM_DATA_BIDI_LOCAL is malformed")
  1594. TPARAM_CHECK_INT(INITIAL_MAX_STREAM_DATA_BIDI_REMOTE,
  1595. "INITIAL_MAX_STREAM_DATA_BIDI_REMOTE is malformed")
  1596. TPARAM_CHECK_INT(INITIAL_MAX_STREAM_DATA_UNI,
  1597. "INITIAL_MAX_STREAM_DATA_UNI is malformed")
  1598. TPARAM_CHECK_INT(ACK_DELAY_EXP,
  1599. "ACK_DELAY_EXP is malformed")
  1600. TPARAM_CHECK_INT(MAX_ACK_DELAY,
  1601. "MAX_ACK_DELAY is malformed")
  1602. TPARAM_CHECK_INT(INITIAL_MAX_STREAMS_BIDI,
  1603. "INITIAL_MAX_STREAMS_BIDI is malformed")
  1604. TPARAM_CHECK_INT(INITIAL_MAX_STREAMS_UNI,
  1605. "INITIAL_MAX_STREAMS_UNI is malformed")
  1606. TPARAM_CHECK_INT(MAX_IDLE_TIMEOUT,
  1607. "MAX_IDLE_TIMEOUT is malformed")
  1608. TPARAM_CHECK_INT(MAX_UDP_PAYLOAD_SIZE,
  1609. "MAX_UDP_PAYLOAD_SIZE is malformed")
  1610. TPARAM_CHECK_INT(ACTIVE_CONN_ID_LIMIT,
  1611. "ACTIVE_CONN_ID_LIMIT is malformed")
  1612. };
  1613. struct tparam_ctx {
  1614. const struct tparam_test *t;
  1615. };
  1616. static int tparam_handle(struct tparam_ctx *ctx,
  1617. uint64_t id, unsigned char *data,
  1618. size_t data_len,
  1619. WPACKET *wpkt)
  1620. {
  1621. const struct tparam_test *t = ctx->t;
  1622. switch (t->op) {
  1623. case TPARAM_OP_DUP:
  1624. if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id,
  1625. data, data_len)))
  1626. return 0;
  1627. /*
  1628. * If this is the matching ID, write it again, duplicating the TPARAM.
  1629. */
  1630. if (id == t->id
  1631. && !TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id,
  1632. data, data_len)))
  1633. return 0;
  1634. return 1;
  1635. case TPARAM_OP_DROP:
  1636. case TPARAM_OP_DROP_INJECT:
  1637. /* Pass through unless ID matches. */
  1638. if (id != t->id
  1639. && !TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id,
  1640. data, data_len)))
  1641. return 0;
  1642. return 1;
  1643. case TPARAM_OP_INJECT:
  1644. case TPARAM_OP_INJECT_TWICE:
  1645. case TPARAM_OP_INJECT_RAW:
  1646. /* Always pass through. */
  1647. if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id,
  1648. data, data_len)))
  1649. return 0;
  1650. return 1;
  1651. case TPARAM_OP_MUTATE:
  1652. if (id == t->id) {
  1653. if (!TEST_size_t_gt(data_len, 0))
  1654. return 0;
  1655. data[0] ^= 1;
  1656. }
  1657. if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(wpkt, id,
  1658. data, data_len)))
  1659. return 0;
  1660. if (id == t->id)
  1661. data[0] ^= 1;
  1662. return 1;
  1663. default:
  1664. return 0;
  1665. }
  1666. }
  1667. static int tparam_on_enc_ext(QTEST_FAULT *qtf, QTEST_ENCRYPTED_EXTENSIONS *ee,
  1668. size_t ee_len, void *arg)
  1669. {
  1670. int rc = 0;
  1671. struct tparam_ctx *ctx = arg;
  1672. PACKET pkt = {0};
  1673. WPACKET wpkt;
  1674. int have_wpkt = 0;
  1675. BUF_MEM *old_bufm = NULL, *new_bufm = NULL;
  1676. unsigned char *tp_p;
  1677. size_t tp_len, written, old_len, eb_len;
  1678. uint64_t id;
  1679. if (!TEST_ptr(old_bufm = BUF_MEM_new()))
  1680. goto err;
  1681. /*
  1682. * Delete transport parameters TLS extension and capture the contents of the
  1683. * extension which was removed.
  1684. */
  1685. if (!TEST_true(qtest_fault_delete_extension(qtf, TLSEXT_TYPE_quic_transport_parameters,
  1686. ee->extensions, &ee->extensionslen,
  1687. old_bufm)))
  1688. goto err;
  1689. if (!TEST_true(PACKET_buf_init(&pkt, (unsigned char *)old_bufm->data, old_bufm->length))
  1690. || !TEST_ptr(new_bufm = BUF_MEM_new())
  1691. || !TEST_true(WPACKET_init(&wpkt, new_bufm)))
  1692. goto err;
  1693. have_wpkt = 1;
  1694. /*
  1695. * Open transport parameters TLS extension:
  1696. *
  1697. * u16 Extension ID (quic_transport_parameters)
  1698. * u16 Extension Data Length
  1699. * ... Extension Data
  1700. *
  1701. */
  1702. if (!TEST_true(WPACKET_put_bytes_u16(&wpkt,
  1703. TLSEXT_TYPE_quic_transport_parameters))
  1704. || !TEST_true(WPACKET_start_sub_packet_u16(&wpkt)))
  1705. goto err;
  1706. for (; PACKET_remaining(&pkt) > 0; ) {
  1707. tp_p = (unsigned char *)ossl_quic_wire_decode_transport_param_bytes(&pkt,
  1708. &id,
  1709. &tp_len);
  1710. if (!TEST_ptr(tp_p)) {
  1711. TEST_mem_eq(PACKET_data(&pkt), PACKET_remaining(&pkt), NULL, 0);
  1712. goto err;
  1713. }
  1714. if (!TEST_true(tparam_handle(ctx, id, tp_p, tp_len, &wpkt)))
  1715. goto err;
  1716. }
  1717. if (ctx->t->op == TPARAM_OP_INJECT || ctx->t->op == TPARAM_OP_DROP_INJECT
  1718. || ctx->t->op == TPARAM_OP_INJECT_TWICE) {
  1719. if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(&wpkt, ctx->t->id,
  1720. ctx->t->buf,
  1721. ctx->t->buf_len)))
  1722. goto err;
  1723. if (ctx->t->op == TPARAM_OP_INJECT_TWICE
  1724. && !TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(&wpkt, ctx->t->id,
  1725. ctx->t->buf,
  1726. ctx->t->buf_len)))
  1727. goto err;
  1728. } else if (ctx->t->op == TPARAM_OP_INJECT_RAW) {
  1729. if (!TEST_true(WPACKET_memcpy(&wpkt, ctx->t->buf, ctx->t->buf_len)))
  1730. goto err;
  1731. }
  1732. if (!TEST_true(WPACKET_close(&wpkt))) /* end extension data, set length */
  1733. goto err;
  1734. if (!TEST_true(WPACKET_get_total_written(&wpkt, &written)))
  1735. goto err;
  1736. WPACKET_finish(&wpkt);
  1737. have_wpkt = 0;
  1738. /*
  1739. * Append the constructed extension blob to the extension block.
  1740. */
  1741. old_len = ee->extensionslen;
  1742. if (!qtest_fault_resize_message(qtf, ee->extensionslen + written))
  1743. goto err;
  1744. memcpy(ee->extensions + old_len, new_bufm->data, written);
  1745. /* Fixup the extension block header (u16 length of entire block). */
  1746. eb_len = (((uint16_t)ee->extensions[0]) << 8) + (uint16_t)ee->extensions[1];
  1747. eb_len += written;
  1748. ee->extensions[0] = (unsigned char)((eb_len >> 8) & 0xFF);
  1749. ee->extensions[1] = (unsigned char)( eb_len & 0xFF);
  1750. rc = 1;
  1751. err:
  1752. if (have_wpkt)
  1753. WPACKET_cleanup(&wpkt);
  1754. BUF_MEM_free(old_bufm);
  1755. BUF_MEM_free(new_bufm);
  1756. return rc;
  1757. }
  1758. static int test_tparam(int idx)
  1759. {
  1760. int testresult = 0;
  1761. SSL_CTX *c_ctx = NULL;
  1762. SSL *c_ssl = NULL;
  1763. QUIC_TSERVER *s = NULL;
  1764. QTEST_FAULT *qtf = NULL;
  1765. struct tparam_ctx ctx = {0};
  1766. ctx.t = &tparam_tests[idx];
  1767. if (!TEST_ptr(c_ctx = SSL_CTX_new_ex(libctx, NULL, OSSL_QUIC_client_method())))
  1768. goto err;
  1769. if (!TEST_true(qtest_create_quic_objects(libctx, c_ctx, NULL, cert,
  1770. privkey, 0, &s,
  1771. &c_ssl, &qtf, NULL)))
  1772. goto err;
  1773. if (!TEST_true(qtest_fault_set_hand_enc_ext_listener(qtf, tparam_on_enc_ext,
  1774. &ctx)))
  1775. goto err;
  1776. if (!TEST_true(qtest_create_quic_connection_ex(s, c_ssl,
  1777. ctx.t->expect_fail != NULL)))
  1778. goto err;
  1779. if (ctx.t->expect_fail != NULL) {
  1780. SSL_CONN_CLOSE_INFO info = {0};
  1781. if (!TEST_true(SSL_get_conn_close_info(c_ssl, &info, sizeof(info))))
  1782. goto err;
  1783. if (!TEST_true((info.flags & SSL_CONN_CLOSE_FLAG_TRANSPORT) != 0)
  1784. || !TEST_uint64_t_eq(info.error_code, OSSL_QUIC_ERR_TRANSPORT_PARAMETER_ERROR)
  1785. || !TEST_ptr(strstr(info.reason, ctx.t->expect_fail))) {
  1786. TEST_error("expected connection closure information mismatch"
  1787. " during TPARAM test: flags=%llu ec=%llu reason='%s'",
  1788. (unsigned long long)info.flags,
  1789. (unsigned long long)info.error_code,
  1790. info.reason);
  1791. goto err;
  1792. }
  1793. }
  1794. testresult = 1;
  1795. err:
  1796. if (!testresult) {
  1797. if (ctx.t->expect_fail != NULL)
  1798. TEST_info("failed during test for id=%llu, op=%d, bl=%zu, "
  1799. "expected failure='%s'", (unsigned long long)ctx.t->id,
  1800. ctx.t->op, ctx.t->buf_len, ctx.t->expect_fail);
  1801. else
  1802. TEST_info("failed during test for id=%llu, op=%d, bl=%zu",
  1803. (unsigned long long)ctx.t->id, ctx.t->op, ctx.t->buf_len);
  1804. }
  1805. ossl_quic_tserver_free(s);
  1806. SSL_free(c_ssl);
  1807. SSL_CTX_free(c_ctx);
  1808. qtest_fault_free(qtf);
  1809. return testresult;
  1810. }
  1811. /***********************************************************************************/
  1812. OPT_TEST_DECLARE_USAGE("provider config certsdir datadir\n")
  1813. int setup_tests(void)
  1814. {
  1815. char *modulename;
  1816. char *configfile;
  1817. libctx = OSSL_LIB_CTX_new();
  1818. if (!TEST_ptr(libctx))
  1819. return 0;
  1820. defctxnull = OSSL_PROVIDER_load(NULL, "null");
  1821. /*
  1822. * Verify that the default and fips providers in the default libctx are not
  1823. * available
  1824. */
  1825. if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
  1826. || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
  1827. goto err;
  1828. if (!test_skip_common_options()) {
  1829. TEST_error("Error parsing test options\n");
  1830. goto err;
  1831. }
  1832. if (!TEST_ptr(modulename = test_get_argument(0))
  1833. || !TEST_ptr(configfile = test_get_argument(1))
  1834. || !TEST_ptr(certsdir = test_get_argument(2))
  1835. || !TEST_ptr(datadir = test_get_argument(3)))
  1836. goto err;
  1837. if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
  1838. goto err;
  1839. /* Check we have the expected provider available */
  1840. if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
  1841. goto err;
  1842. /* Check the default provider is not available */
  1843. if (strcmp(modulename, "default") != 0
  1844. && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
  1845. goto err;
  1846. if (strcmp(modulename, "fips") == 0)
  1847. is_fips = 1;
  1848. cert = test_mk_file_path(certsdir, "servercert.pem");
  1849. if (cert == NULL)
  1850. goto err;
  1851. ccert = test_mk_file_path(certsdir, "ee-client-chain.pem");
  1852. if (ccert == NULL)
  1853. goto err;
  1854. cauthca = test_mk_file_path(certsdir, "root-cert.pem");
  1855. if (cauthca == NULL)
  1856. goto err;
  1857. privkey = test_mk_file_path(certsdir, "serverkey.pem");
  1858. if (privkey == NULL)
  1859. goto err;
  1860. cprivkey = test_mk_file_path(certsdir, "ee-key.pem");
  1861. if (privkey == NULL)
  1862. goto err;
  1863. ADD_ALL_TESTS(test_quic_write_read, 3);
  1864. ADD_TEST(test_fin_only_blocking);
  1865. ADD_TEST(test_ciphersuites);
  1866. ADD_TEST(test_cipher_find);
  1867. ADD_TEST(test_version);
  1868. #if defined(DO_SSL_TRACE_TEST)
  1869. ADD_TEST(test_ssl_trace);
  1870. #endif
  1871. ADD_TEST(test_quic_forbidden_apis_ctx);
  1872. ADD_TEST(test_quic_forbidden_apis);
  1873. ADD_TEST(test_quic_forbidden_options);
  1874. ADD_ALL_TESTS(test_quic_set_fd, 3);
  1875. ADD_TEST(test_bio_ssl);
  1876. ADD_TEST(test_back_pressure);
  1877. ADD_TEST(test_multiple_dgrams);
  1878. ADD_ALL_TESTS(test_non_io_retry, 2);
  1879. ADD_TEST(test_quic_psk);
  1880. ADD_ALL_TESTS(test_client_auth, 3);
  1881. ADD_ALL_TESTS(test_alpn, 2);
  1882. ADD_ALL_TESTS(test_noisy_dgram, 2);
  1883. ADD_TEST(test_bw_limit);
  1884. ADD_TEST(test_get_shutdown);
  1885. ADD_ALL_TESTS(test_tparam, OSSL_NELEM(tparam_tests));
  1886. return 1;
  1887. err:
  1888. cleanup_tests();
  1889. return 0;
  1890. }
  1891. void cleanup_tests(void)
  1892. {
  1893. bio_f_noisy_dgram_filter_free();
  1894. bio_f_pkt_split_dgram_filter_free();
  1895. OPENSSL_free(cert);
  1896. OPENSSL_free(privkey);
  1897. OPENSSL_free(ccert);
  1898. OPENSSL_free(cauthca);
  1899. OPENSSL_free(cprivkey);
  1900. OSSL_PROVIDER_unload(defctxnull);
  1901. OSSL_LIB_CTX_free(libctx);
  1902. }