quicapitest.c 75 KB

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