quicapitest.c 69 KB

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