quicapitest.c 50 KB

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