quictestlib.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. /*
  2. * Copyright 2022-2024 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 <assert.h>
  10. #include <openssl/configuration.h>
  11. #include <openssl/bio.h>
  12. #include "internal/e_os.h" /* For struct timeval */
  13. #include "quictestlib.h"
  14. #include "ssltestlib.h"
  15. #include "../testutil.h"
  16. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  17. # include "../threadstest.h"
  18. #endif
  19. #include "internal/quic_ssl.h"
  20. #include "internal/quic_wire_pkt.h"
  21. #include "internal/quic_record_tx.h"
  22. #include "internal/quic_error.h"
  23. #include "internal/packet.h"
  24. #include "internal/tsan_assist.h"
  25. #define GROWTH_ALLOWANCE 1024
  26. struct noise_args_data_st {
  27. BIO *cbio;
  28. BIO *sbio;
  29. BIO *tracebio;
  30. int flags;
  31. };
  32. struct qtest_fault {
  33. QUIC_TSERVER *qtserv;
  34. /* Plain packet mutations */
  35. /* Header for the plaintext packet */
  36. QUIC_PKT_HDR pplainhdr;
  37. /* iovec for the plaintext packet data buffer */
  38. OSSL_QTX_IOVEC pplainio;
  39. /* Allocated size of the plaintext packet data buffer */
  40. size_t pplainbuf_alloc;
  41. qtest_fault_on_packet_plain_cb pplaincb;
  42. void *pplaincbarg;
  43. /* Handshake message mutations */
  44. /* Handshake message buffer */
  45. unsigned char *handbuf;
  46. /* Allocated size of the handshake message buffer */
  47. size_t handbufalloc;
  48. /* Actual length of the handshake message */
  49. size_t handbuflen;
  50. qtest_fault_on_handshake_cb handshakecb;
  51. void *handshakecbarg;
  52. qtest_fault_on_enc_ext_cb encextcb;
  53. void *encextcbarg;
  54. /* Cipher packet mutations */
  55. qtest_fault_on_packet_cipher_cb pciphercb;
  56. void *pciphercbarg;
  57. /* Datagram mutations */
  58. qtest_fault_on_datagram_cb datagramcb;
  59. void *datagramcbarg;
  60. /* The currently processed message */
  61. BIO_MSG msg;
  62. /* Allocated size of msg data buffer */
  63. size_t msgalloc;
  64. struct noise_args_data_st noiseargs;
  65. };
  66. static void packet_plain_finish(void *arg);
  67. static void handshake_finish(void *arg);
  68. static OSSL_TIME qtest_get_time(void);
  69. static void qtest_reset_time(void);
  70. static int using_fake_time = 0;
  71. static OSSL_TIME fake_now;
  72. static CRYPTO_RWLOCK *fake_now_lock = NULL;
  73. static OSSL_TIME start_time;
  74. static OSSL_TIME fake_now_cb(void *arg)
  75. {
  76. return qtest_get_time();
  77. }
  78. static void noise_msg_callback(int write_p, int version, int content_type,
  79. const void *buf, size_t len, SSL *ssl,
  80. void *arg)
  81. {
  82. struct noise_args_data_st *noiseargs = (struct noise_args_data_st *)arg;
  83. if (content_type == SSL3_RT_QUIC_FRAME_FULL) {
  84. PACKET pkt;
  85. uint64_t frame_type;
  86. if (!PACKET_buf_init(&pkt, buf, len))
  87. return;
  88. if (!ossl_quic_wire_peek_frame_header(&pkt, &frame_type, NULL))
  89. return;
  90. if (frame_type == OSSL_QUIC_FRAME_TYPE_PING) {
  91. /*
  92. * If either endpoint issues a ping frame then we are in danger
  93. * of our noise being too much such that the connection itself
  94. * fails. We back off on the noise for a bit to avoid that.
  95. */
  96. (void)BIO_ctrl(noiseargs->cbio, BIO_CTRL_NOISE_BACK_OFF, 0, NULL);
  97. (void)BIO_ctrl(noiseargs->sbio, BIO_CTRL_NOISE_BACK_OFF, 0, NULL);
  98. }
  99. }
  100. #ifndef OPENSSL_NO_SSL_TRACE
  101. if ((noiseargs->flags & QTEST_FLAG_CLIENT_TRACE) != 0
  102. && !SSL_is_server(ssl))
  103. SSL_trace(write_p, version, content_type, buf, len, ssl,
  104. noiseargs->tracebio);
  105. #endif
  106. }
  107. int qtest_create_quic_objects(OSSL_LIB_CTX *libctx, SSL_CTX *clientctx,
  108. SSL_CTX *serverctx, char *certfile, char *keyfile,
  109. int flags, QUIC_TSERVER **qtserv, SSL **cssl,
  110. QTEST_FAULT **fault, BIO **tracebio)
  111. {
  112. /* ALPN value as recognised by QUIC_TSERVER */
  113. unsigned char alpn[] = { 8, 'o', 's', 's', 'l', 't', 'e', 's', 't' };
  114. QUIC_TSERVER_ARGS tserver_args = {0};
  115. BIO *cbio = NULL, *sbio = NULL, *fisbio = NULL;
  116. BIO_ADDR *peeraddr = NULL;
  117. struct in_addr ina = {0};
  118. BIO *tmpbio = NULL;
  119. *qtserv = NULL;
  120. if (*cssl == NULL) {
  121. *cssl = SSL_new(clientctx);
  122. if (!TEST_ptr(*cssl))
  123. return 0;
  124. }
  125. if (fault != NULL) {
  126. *fault = OPENSSL_zalloc(sizeof(**fault));
  127. if (*fault == NULL)
  128. goto err;
  129. }
  130. #ifndef OPENSSL_NO_SSL_TRACE
  131. if ((flags & QTEST_FLAG_CLIENT_TRACE) != 0) {
  132. tmpbio = BIO_new_fp(stdout, BIO_NOCLOSE);
  133. if (!TEST_ptr(tmpbio))
  134. goto err;
  135. SSL_set_msg_callback(*cssl, SSL_trace);
  136. SSL_set_msg_callback_arg(*cssl, tmpbio);
  137. }
  138. #endif
  139. if (tracebio != NULL)
  140. *tracebio = tmpbio;
  141. /* SSL_set_alpn_protos returns 0 for success! */
  142. if (!TEST_false(SSL_set_alpn_protos(*cssl, alpn, sizeof(alpn))))
  143. goto err;
  144. if (!TEST_ptr(peeraddr = BIO_ADDR_new()))
  145. goto err;
  146. if ((flags & QTEST_FLAG_BLOCK) != 0) {
  147. #if !defined(OPENSSL_NO_POSIX_IO)
  148. int cfd, sfd;
  149. /*
  150. * For blocking mode we need to create actual sockets rather than doing
  151. * everything in memory
  152. */
  153. if (!TEST_true(create_test_sockets(&cfd, &sfd, SOCK_DGRAM, peeraddr)))
  154. goto err;
  155. cbio = BIO_new_dgram(cfd, 1);
  156. if (!TEST_ptr(cbio)) {
  157. close(cfd);
  158. close(sfd);
  159. goto err;
  160. }
  161. sbio = BIO_new_dgram(sfd, 1);
  162. if (!TEST_ptr(sbio)) {
  163. close(sfd);
  164. goto err;
  165. }
  166. #else
  167. goto err;
  168. #endif
  169. } else {
  170. if (!TEST_true(BIO_new_bio_dgram_pair(&cbio, 0, &sbio, 0)))
  171. goto err;
  172. if (!TEST_true(BIO_dgram_set_caps(cbio, BIO_DGRAM_CAP_HANDLES_DST_ADDR))
  173. || !TEST_true(BIO_dgram_set_caps(sbio, BIO_DGRAM_CAP_HANDLES_DST_ADDR)))
  174. goto err;
  175. /* Dummy server address */
  176. if (!TEST_true(BIO_ADDR_rawmake(peeraddr, AF_INET, &ina, sizeof(ina),
  177. htons(0))))
  178. goto err;
  179. }
  180. if ((flags & QTEST_FLAG_PACKET_SPLIT) != 0) {
  181. BIO *pktsplitbio = BIO_new(bio_f_pkt_split_dgram_filter());
  182. if (!TEST_ptr(pktsplitbio))
  183. goto err;
  184. cbio = BIO_push(pktsplitbio, cbio);
  185. pktsplitbio = BIO_new(bio_f_pkt_split_dgram_filter());
  186. if (!TEST_ptr(pktsplitbio))
  187. goto err;
  188. sbio = BIO_push(pktsplitbio, sbio);
  189. }
  190. if ((flags & QTEST_FLAG_NOISE) != 0) {
  191. BIO *noisebio;
  192. struct bio_noise_now_cb_st now_cb = { fake_now_cb, NULL };
  193. /*
  194. * It is an error to not have a QTEST_FAULT object when introducing noise
  195. */
  196. if (!TEST_ptr(fault))
  197. goto err;
  198. noisebio = BIO_new(bio_f_noisy_dgram_filter());
  199. if (!TEST_ptr(noisebio))
  200. goto err;
  201. cbio = BIO_push(noisebio, cbio);
  202. if ((flags & QTEST_FLAG_FAKE_TIME) != 0) {
  203. if (!TEST_int_eq(BIO_ctrl(cbio, BIO_CTRL_NOISE_SET_NOW_CB,
  204. 0, &now_cb), 1))
  205. goto err;
  206. }
  207. noisebio = BIO_new(bio_f_noisy_dgram_filter());
  208. if (!TEST_ptr(noisebio))
  209. goto err;
  210. sbio = BIO_push(noisebio, sbio);
  211. if ((flags & QTEST_FLAG_FAKE_TIME) != 0) {
  212. if (!TEST_int_eq(BIO_ctrl(sbio, BIO_CTRL_NOISE_SET_NOW_CB,
  213. 0, &now_cb), 1))
  214. goto err;
  215. }
  216. /*
  217. * TODO(QUIC SERVER):
  218. * Currently the simplistic handler of the quic tserver cannot cope
  219. * with noise introduced in the first packet received from the
  220. * client. This needs to be removed once we have proper server side
  221. * handling.
  222. */
  223. (void)BIO_ctrl(sbio, BIO_CTRL_NOISE_BACK_OFF, 0, NULL);
  224. (*fault)->noiseargs.cbio = cbio;
  225. (*fault)->noiseargs.sbio = sbio;
  226. (*fault)->noiseargs.tracebio = tmpbio;
  227. (*fault)->noiseargs.flags = flags;
  228. SSL_set_msg_callback(*cssl, noise_msg_callback);
  229. SSL_set_msg_callback_arg(*cssl, &(*fault)->noiseargs);
  230. }
  231. SSL_set_bio(*cssl, cbio, cbio);
  232. if (!TEST_true(SSL_set_blocking_mode(*cssl,
  233. (flags & QTEST_FLAG_BLOCK) != 0 ? 1 : 0)))
  234. goto err;
  235. if (!TEST_true(SSL_set1_initial_peer_addr(*cssl, peeraddr)))
  236. goto err;
  237. fisbio = BIO_new(qtest_get_bio_method());
  238. if (!TEST_ptr(fisbio))
  239. goto err;
  240. BIO_set_data(fisbio, fault == NULL ? NULL : *fault);
  241. if (!BIO_up_ref(sbio))
  242. goto err;
  243. if (!TEST_ptr(BIO_push(fisbio, sbio))) {
  244. BIO_free(sbio);
  245. goto err;
  246. }
  247. tserver_args.libctx = libctx;
  248. tserver_args.net_rbio = sbio;
  249. tserver_args.net_wbio = fisbio;
  250. tserver_args.alpn = NULL;
  251. if (serverctx != NULL && !TEST_true(SSL_CTX_up_ref(serverctx)))
  252. goto err;
  253. tserver_args.ctx = serverctx;
  254. if (fake_now_lock == NULL) {
  255. fake_now_lock = CRYPTO_THREAD_lock_new();
  256. if (fake_now_lock == NULL)
  257. goto err;
  258. }
  259. if ((flags & QTEST_FLAG_FAKE_TIME) != 0) {
  260. using_fake_time = 1;
  261. qtest_reset_time();
  262. tserver_args.now_cb = fake_now_cb;
  263. (void)ossl_quic_conn_set_override_now_cb(*cssl, fake_now_cb, NULL);
  264. } else {
  265. using_fake_time = 0;
  266. }
  267. if (!TEST_ptr(*qtserv = ossl_quic_tserver_new(&tserver_args, certfile,
  268. keyfile)))
  269. goto err;
  270. /* Ownership of fisbio and sbio is now held by *qtserv */
  271. sbio = NULL;
  272. fisbio = NULL;
  273. if ((flags & QTEST_FLAG_NOISE) != 0)
  274. ossl_quic_tserver_set_msg_callback(*qtserv, noise_msg_callback,
  275. &(*fault)->noiseargs);
  276. if (fault != NULL)
  277. (*fault)->qtserv = *qtserv;
  278. BIO_ADDR_free(peeraddr);
  279. return 1;
  280. err:
  281. SSL_CTX_free(tserver_args.ctx);
  282. BIO_ADDR_free(peeraddr);
  283. BIO_free_all(cbio);
  284. BIO_free_all(fisbio);
  285. BIO_free_all(sbio);
  286. SSL_free(*cssl);
  287. *cssl = NULL;
  288. ossl_quic_tserver_free(*qtserv);
  289. if (fault != NULL)
  290. OPENSSL_free(*fault);
  291. BIO_free(tmpbio);
  292. if (tracebio != NULL)
  293. *tracebio = NULL;
  294. return 0;
  295. }
  296. void qtest_add_time(uint64_t millis)
  297. {
  298. if (!CRYPTO_THREAD_write_lock(fake_now_lock))
  299. return;
  300. fake_now = ossl_time_add(fake_now, ossl_ms2time(millis));
  301. CRYPTO_THREAD_unlock(fake_now_lock);
  302. }
  303. static OSSL_TIME qtest_get_time(void)
  304. {
  305. OSSL_TIME ret;
  306. if (!CRYPTO_THREAD_read_lock(fake_now_lock))
  307. return ossl_time_zero();
  308. ret = fake_now;
  309. CRYPTO_THREAD_unlock(fake_now_lock);
  310. return ret;
  311. }
  312. static void qtest_reset_time(void)
  313. {
  314. if (!CRYPTO_THREAD_write_lock(fake_now_lock))
  315. return;
  316. fake_now = ossl_time_zero();
  317. CRYPTO_THREAD_unlock(fake_now_lock);
  318. /* zero time can have a special meaning, bump it */
  319. qtest_add_time(1);
  320. }
  321. void qtest_start_stopwatch(void)
  322. {
  323. start_time = qtest_get_time();
  324. }
  325. uint64_t qtest_get_stopwatch_time(void)
  326. {
  327. return ossl_time2ms(ossl_time_subtract(qtest_get_time(), start_time));
  328. }
  329. QTEST_FAULT *qtest_create_injector(QUIC_TSERVER *ts)
  330. {
  331. QTEST_FAULT *f;
  332. f = OPENSSL_zalloc(sizeof(*f));
  333. if (f == NULL)
  334. return NULL;
  335. f->qtserv = ts;
  336. return f;
  337. }
  338. int qtest_supports_blocking(void)
  339. {
  340. #if !defined(OPENSSL_NO_POSIX_IO) && defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  341. return 1;
  342. #else
  343. return 0;
  344. #endif
  345. }
  346. #define MAXLOOPS 1000
  347. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  348. static int globserverret = 0;
  349. static TSAN_QUALIFIER int abortserverthread = 0;
  350. static QUIC_TSERVER *globtserv;
  351. static const thread_t thread_zero;
  352. static void run_server_thread(void)
  353. {
  354. /*
  355. * This will operate in a busy loop because the server does not block,
  356. * but should be acceptable because it is local and we expect this to be
  357. * fast
  358. */
  359. globserverret = qtest_create_quic_connection(globtserv, NULL);
  360. }
  361. #endif
  362. int qtest_wait_for_timeout(SSL *s, QUIC_TSERVER *qtserv)
  363. {
  364. struct timeval tv;
  365. OSSL_TIME ctimeout, stimeout, mintimeout, now;
  366. int cinf;
  367. /* We don't need to wait in blocking mode */
  368. if (s == NULL || SSL_get_blocking_mode(s))
  369. return 1;
  370. /* Don't wait if either BIO has data waiting */
  371. if (BIO_pending(SSL_get_rbio(s)) > 0
  372. || BIO_pending(ossl_quic_tserver_get0_rbio(qtserv)) > 0)
  373. return 1;
  374. /*
  375. * Neither endpoint has data waiting to be read. We assume data transmission
  376. * is instantaneous due to using mem based BIOs, so there is no data "in
  377. * flight" and no more data will be sent by either endpoint until some time
  378. * based event has occurred. Therefore, wait for a timeout to occur. This
  379. * might happen if we are using the noisy BIO and datagrams have been lost.
  380. */
  381. if (!SSL_get_event_timeout(s, &tv, &cinf))
  382. return 0;
  383. if (using_fake_time)
  384. now = qtest_get_time();
  385. else
  386. now = ossl_time_now();
  387. ctimeout = cinf ? ossl_time_infinite() : ossl_time_from_timeval(tv);
  388. stimeout = ossl_time_subtract(ossl_quic_tserver_get_deadline(qtserv), now);
  389. mintimeout = ossl_time_min(ctimeout, stimeout);
  390. if (ossl_time_is_infinite(mintimeout))
  391. return 0;
  392. if (using_fake_time)
  393. qtest_add_time(ossl_time2ms(mintimeout));
  394. else
  395. OSSL_sleep(ossl_time2ms(mintimeout));
  396. return 1;
  397. }
  398. int qtest_create_quic_connection_ex(QUIC_TSERVER *qtserv, SSL *clientssl,
  399. int wanterr)
  400. {
  401. int retc = -1, rets = 0, abortctr = 0, ret = 0;
  402. int clienterr = 0, servererr = 0;
  403. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  404. /*
  405. * Pointless initialisation to avoid bogus compiler warnings about using
  406. * t uninitialised
  407. */
  408. thread_t t = thread_zero;
  409. if (clientssl != NULL)
  410. abortserverthread = 0;
  411. #endif
  412. if (!TEST_ptr(qtserv)) {
  413. goto err;
  414. } else if (clientssl == NULL) {
  415. retc = 1;
  416. } else if (SSL_get_blocking_mode(clientssl) > 0) {
  417. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  418. /*
  419. * clientssl is blocking. We will need a thread to complete the
  420. * connection
  421. */
  422. globtserv = qtserv;
  423. if (!TEST_true(run_thread(&t, run_server_thread)))
  424. goto err;
  425. qtserv = NULL;
  426. rets = 1;
  427. #else
  428. TEST_error("No thread support in this build");
  429. goto err;
  430. #endif
  431. }
  432. do {
  433. if (!clienterr && retc <= 0) {
  434. int err;
  435. retc = SSL_connect(clientssl);
  436. if (retc <= 0) {
  437. err = SSL_get_error(clientssl, retc);
  438. if (err == wanterr) {
  439. retc = 1;
  440. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  441. if (qtserv == NULL && rets > 0)
  442. tsan_store(&abortserverthread, 1);
  443. else
  444. #endif
  445. rets = 1;
  446. } else {
  447. if (err != SSL_ERROR_WANT_READ
  448. && err != SSL_ERROR_WANT_WRITE) {
  449. TEST_info("SSL_connect() failed %d, %d", retc, err);
  450. TEST_openssl_errors();
  451. clienterr = 1;
  452. }
  453. }
  454. }
  455. }
  456. qtest_add_time(1);
  457. if (clientssl != NULL)
  458. SSL_handle_events(clientssl);
  459. if (qtserv != NULL)
  460. ossl_quic_tserver_tick(qtserv);
  461. if (!servererr && rets <= 0) {
  462. servererr = ossl_quic_tserver_is_term_any(qtserv);
  463. if (!servererr)
  464. rets = ossl_quic_tserver_is_handshake_confirmed(qtserv);
  465. }
  466. if (clienterr && servererr)
  467. goto err;
  468. if (clientssl != NULL && ++abortctr == MAXLOOPS) {
  469. TEST_info("No progress made");
  470. goto err;
  471. }
  472. if ((retc <= 0 && !clienterr) || (rets <= 0 && !servererr)) {
  473. if (!qtest_wait_for_timeout(clientssl, qtserv))
  474. goto err;
  475. }
  476. } while ((retc <= 0 && !clienterr)
  477. || (rets <= 0 && !servererr
  478. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  479. && !tsan_load(&abortserverthread)
  480. #endif
  481. ));
  482. if (qtserv == NULL && rets > 0) {
  483. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  484. if (!TEST_true(wait_for_thread(t)) || !TEST_true(globserverret))
  485. goto err;
  486. #else
  487. TEST_error("Should not happen");
  488. goto err;
  489. #endif
  490. }
  491. if (!clienterr && !servererr)
  492. ret = 1;
  493. err:
  494. return ret;
  495. }
  496. int qtest_create_quic_connection(QUIC_TSERVER *qtserv, SSL *clientssl)
  497. {
  498. return qtest_create_quic_connection_ex(qtserv, clientssl, SSL_ERROR_NONE);
  499. }
  500. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  501. static TSAN_QUALIFIER int shutdowndone;
  502. static void run_server_shutdown_thread(void)
  503. {
  504. /*
  505. * This will operate in a busy loop because the server does not block,
  506. * but should be acceptable because it is local and we expect this to be
  507. * fast
  508. */
  509. do {
  510. ossl_quic_tserver_tick(globtserv);
  511. } while(!tsan_load(&shutdowndone));
  512. }
  513. #endif
  514. int qtest_shutdown(QUIC_TSERVER *qtserv, SSL *clientssl)
  515. {
  516. int tickserver = 1;
  517. int ret = 0;
  518. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  519. /*
  520. * Pointless initialisation to avoid bogus compiler warnings about using
  521. * t uninitialised
  522. */
  523. thread_t t = thread_zero;
  524. #endif
  525. if (SSL_get_blocking_mode(clientssl) > 0) {
  526. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  527. /*
  528. * clientssl is blocking. We will need a thread to complete the
  529. * connection
  530. */
  531. globtserv = qtserv;
  532. shutdowndone = 0;
  533. if (!TEST_true(run_thread(&t, run_server_shutdown_thread)))
  534. return 0;
  535. tickserver = 0;
  536. #else
  537. TEST_error("No thread support in this build");
  538. return 0;
  539. #endif
  540. }
  541. /* Busy loop in non-blocking mode. It should be quick because its local */
  542. for (;;) {
  543. int rc = SSL_shutdown(clientssl);
  544. if (rc == 1) {
  545. ret = 1;
  546. break;
  547. }
  548. if (rc < 0)
  549. break;
  550. if (tickserver)
  551. ossl_quic_tserver_tick(qtserv);
  552. }
  553. #if defined(OPENSSL_THREADS) && !defined(CRYPTO_TDEBUG)
  554. tsan_store(&shutdowndone, 1);
  555. if (!tickserver) {
  556. if (!TEST_true(wait_for_thread(t)))
  557. ret = 0;
  558. }
  559. #endif
  560. return ret;
  561. }
  562. int qtest_check_server_transport_err(QUIC_TSERVER *qtserv, uint64_t code)
  563. {
  564. const QUIC_TERMINATE_CAUSE *cause;
  565. ossl_quic_tserver_tick(qtserv);
  566. /*
  567. * Check that the server has closed with the specified code from the client
  568. */
  569. if (!TEST_true(ossl_quic_tserver_is_term_any(qtserv)))
  570. return 0;
  571. cause = ossl_quic_tserver_get_terminate_cause(qtserv);
  572. if (!TEST_ptr(cause)
  573. || !TEST_true(cause->remote)
  574. || !TEST_false(cause->app)
  575. || !TEST_uint64_t_eq(cause->error_code, code))
  576. return 0;
  577. return 1;
  578. }
  579. int qtest_check_server_protocol_err(QUIC_TSERVER *qtserv)
  580. {
  581. return qtest_check_server_transport_err(qtserv, OSSL_QUIC_ERR_PROTOCOL_VIOLATION);
  582. }
  583. int qtest_check_server_frame_encoding_err(QUIC_TSERVER *qtserv)
  584. {
  585. return qtest_check_server_transport_err(qtserv, OSSL_QUIC_ERR_FRAME_ENCODING_ERROR);
  586. }
  587. void qtest_fault_free(QTEST_FAULT *fault)
  588. {
  589. if (fault == NULL)
  590. return;
  591. packet_plain_finish(fault);
  592. handshake_finish(fault);
  593. OPENSSL_free(fault);
  594. }
  595. static int packet_plain_mutate(const QUIC_PKT_HDR *hdrin,
  596. const OSSL_QTX_IOVEC *iovecin, size_t numin,
  597. QUIC_PKT_HDR **hdrout,
  598. const OSSL_QTX_IOVEC **iovecout,
  599. size_t *numout,
  600. void *arg)
  601. {
  602. QTEST_FAULT *fault = arg;
  603. size_t i, bufsz = 0;
  604. unsigned char *cur;
  605. /* Coalesce our data into a single buffer */
  606. /* First calculate required buffer size */
  607. for (i = 0; i < numin; i++)
  608. bufsz += iovecin[i].buf_len;
  609. fault->pplainio.buf_len = bufsz;
  610. /* Add an allowance for possible growth */
  611. bufsz += GROWTH_ALLOWANCE;
  612. fault->pplainio.buf = cur = OPENSSL_malloc(bufsz);
  613. if (cur == NULL) {
  614. fault->pplainio.buf_len = 0;
  615. return 0;
  616. }
  617. fault->pplainbuf_alloc = bufsz;
  618. /* Copy in the data from the input buffers */
  619. for (i = 0; i < numin; i++) {
  620. memcpy(cur, iovecin[i].buf, iovecin[i].buf_len);
  621. cur += iovecin[i].buf_len;
  622. }
  623. fault->pplainhdr = *hdrin;
  624. /* Cast below is safe because we allocated the buffer */
  625. if (fault->pplaincb != NULL
  626. && !fault->pplaincb(fault, &fault->pplainhdr,
  627. (unsigned char *)fault->pplainio.buf,
  628. fault->pplainio.buf_len, fault->pplaincbarg))
  629. return 0;
  630. *hdrout = &fault->pplainhdr;
  631. *iovecout = &fault->pplainio;
  632. *numout = 1;
  633. return 1;
  634. }
  635. static void packet_plain_finish(void *arg)
  636. {
  637. QTEST_FAULT *fault = arg;
  638. /* Cast below is safe because we allocated the buffer */
  639. OPENSSL_free((unsigned char *)fault->pplainio.buf);
  640. fault->pplainio.buf_len = 0;
  641. fault->pplainbuf_alloc = 0;
  642. fault->pplainio.buf = NULL;
  643. }
  644. int qtest_fault_set_packet_plain_listener(QTEST_FAULT *fault,
  645. qtest_fault_on_packet_plain_cb pplaincb,
  646. void *pplaincbarg)
  647. {
  648. fault->pplaincb = pplaincb;
  649. fault->pplaincbarg = pplaincbarg;
  650. return ossl_quic_tserver_set_plain_packet_mutator(fault->qtserv,
  651. packet_plain_mutate,
  652. packet_plain_finish,
  653. fault);
  654. }
  655. /* To be called from a packet_plain_listener callback */
  656. int qtest_fault_resize_plain_packet(QTEST_FAULT *fault, size_t newlen)
  657. {
  658. unsigned char *buf;
  659. size_t oldlen = fault->pplainio.buf_len;
  660. /*
  661. * Alloc'd size should always be non-zero, so if this fails we've been
  662. * incorrectly called
  663. */
  664. if (fault->pplainbuf_alloc == 0)
  665. return 0;
  666. if (newlen > fault->pplainbuf_alloc) {
  667. /* This exceeds our growth allowance. Fail */
  668. return 0;
  669. }
  670. /* Cast below is safe because we allocated the buffer */
  671. buf = (unsigned char *)fault->pplainio.buf;
  672. if (newlen > oldlen) {
  673. /* Extend packet with 0 bytes */
  674. memset(buf + oldlen, 0, newlen - oldlen);
  675. } /* else we're truncating or staying the same */
  676. fault->pplainio.buf_len = newlen;
  677. fault->pplainhdr.len = newlen;
  678. return 1;
  679. }
  680. /*
  681. * Prepend frame data into a packet. To be called from a packet_plain_listener
  682. * callback
  683. */
  684. int qtest_fault_prepend_frame(QTEST_FAULT *fault, const unsigned char *frame,
  685. size_t frame_len)
  686. {
  687. unsigned char *buf;
  688. size_t old_len;
  689. /*
  690. * Alloc'd size should always be non-zero, so if this fails we've been
  691. * incorrectly called
  692. */
  693. if (fault->pplainbuf_alloc == 0)
  694. return 0;
  695. /* Cast below is safe because we allocated the buffer */
  696. buf = (unsigned char *)fault->pplainio.buf;
  697. old_len = fault->pplainio.buf_len;
  698. /* Extend the size of the packet by the size of the new frame */
  699. if (!TEST_true(qtest_fault_resize_plain_packet(fault,
  700. old_len + frame_len)))
  701. return 0;
  702. memmove(buf + frame_len, buf, old_len);
  703. memcpy(buf, frame, frame_len);
  704. return 1;
  705. }
  706. static int handshake_mutate(const unsigned char *msgin, size_t msginlen,
  707. unsigned char **msgout, size_t *msgoutlen,
  708. void *arg)
  709. {
  710. QTEST_FAULT *fault = arg;
  711. unsigned char *buf;
  712. unsigned long payloadlen;
  713. unsigned int msgtype;
  714. PACKET pkt;
  715. buf = OPENSSL_malloc(msginlen + GROWTH_ALLOWANCE);
  716. if (buf == NULL)
  717. return 0;
  718. fault->handbuf = buf;
  719. fault->handbuflen = msginlen;
  720. fault->handbufalloc = msginlen + GROWTH_ALLOWANCE;
  721. memcpy(buf, msgin, msginlen);
  722. if (!PACKET_buf_init(&pkt, buf, msginlen)
  723. || !PACKET_get_1(&pkt, &msgtype)
  724. || !PACKET_get_net_3(&pkt, &payloadlen)
  725. || PACKET_remaining(&pkt) != payloadlen)
  726. return 0;
  727. /* Parse specific message types */
  728. switch (msgtype) {
  729. case SSL3_MT_ENCRYPTED_EXTENSIONS:
  730. {
  731. QTEST_ENCRYPTED_EXTENSIONS ee;
  732. if (fault->encextcb == NULL)
  733. break;
  734. /*
  735. * The EncryptedExtensions message is very simple. It just has an
  736. * extensions block in it and nothing else.
  737. */
  738. ee.extensions = (unsigned char *)PACKET_data(&pkt);
  739. ee.extensionslen = payloadlen;
  740. if (!fault->encextcb(fault, &ee, payloadlen, fault->encextcbarg))
  741. return 0;
  742. }
  743. default:
  744. /* No specific handlers for these message types yet */
  745. break;
  746. }
  747. if (fault->handshakecb != NULL
  748. && !fault->handshakecb(fault, buf, fault->handbuflen,
  749. fault->handshakecbarg))
  750. return 0;
  751. *msgout = buf;
  752. *msgoutlen = fault->handbuflen;
  753. return 1;
  754. }
  755. static void handshake_finish(void *arg)
  756. {
  757. QTEST_FAULT *fault = arg;
  758. OPENSSL_free(fault->handbuf);
  759. fault->handbuf = NULL;
  760. }
  761. int qtest_fault_set_handshake_listener(QTEST_FAULT *fault,
  762. qtest_fault_on_handshake_cb handshakecb,
  763. void *handshakecbarg)
  764. {
  765. fault->handshakecb = handshakecb;
  766. fault->handshakecbarg = handshakecbarg;
  767. return ossl_quic_tserver_set_handshake_mutator(fault->qtserv,
  768. handshake_mutate,
  769. handshake_finish,
  770. fault);
  771. }
  772. int qtest_fault_set_hand_enc_ext_listener(QTEST_FAULT *fault,
  773. qtest_fault_on_enc_ext_cb encextcb,
  774. void *encextcbarg)
  775. {
  776. fault->encextcb = encextcb;
  777. fault->encextcbarg = encextcbarg;
  778. return ossl_quic_tserver_set_handshake_mutator(fault->qtserv,
  779. handshake_mutate,
  780. handshake_finish,
  781. fault);
  782. }
  783. /* To be called from a handshake_listener callback */
  784. int qtest_fault_resize_handshake(QTEST_FAULT *fault, size_t newlen)
  785. {
  786. unsigned char *buf;
  787. size_t oldlen = fault->handbuflen;
  788. /*
  789. * Alloc'd size should always be non-zero, so if this fails we've been
  790. * incorrectly called
  791. */
  792. if (fault->handbufalloc == 0)
  793. return 0;
  794. if (newlen > fault->handbufalloc) {
  795. /* This exceeds our growth allowance. Fail */
  796. return 0;
  797. }
  798. buf = (unsigned char *)fault->handbuf;
  799. if (newlen > oldlen) {
  800. /* Extend packet with 0 bytes */
  801. memset(buf + oldlen, 0, newlen - oldlen);
  802. } /* else we're truncating or staying the same */
  803. fault->handbuflen = newlen;
  804. return 1;
  805. }
  806. /* To be called from message specific listener callbacks */
  807. int qtest_fault_resize_message(QTEST_FAULT *fault, size_t newlen)
  808. {
  809. /* First resize the underlying message */
  810. if (!qtest_fault_resize_handshake(fault, newlen + SSL3_HM_HEADER_LENGTH))
  811. return 0;
  812. /* Fixup the handshake message header */
  813. fault->handbuf[1] = (unsigned char)((newlen >> 16) & 0xff);
  814. fault->handbuf[2] = (unsigned char)((newlen >> 8) & 0xff);
  815. fault->handbuf[3] = (unsigned char)((newlen ) & 0xff);
  816. return 1;
  817. }
  818. int qtest_fault_delete_extension(QTEST_FAULT *fault,
  819. unsigned int exttype, unsigned char *ext,
  820. size_t *extlen,
  821. BUF_MEM *old_ext)
  822. {
  823. PACKET pkt, sub, subext;
  824. WPACKET old_ext_wpkt;
  825. unsigned int type;
  826. const unsigned char *start, *end;
  827. size_t newlen, w;
  828. size_t msglen = fault->handbuflen;
  829. if (!PACKET_buf_init(&pkt, ext, *extlen))
  830. return 0;
  831. /* Extension block starts with 2 bytes for extension block length */
  832. if (!PACKET_as_length_prefixed_2(&pkt, &sub))
  833. return 0;
  834. do {
  835. start = PACKET_data(&sub);
  836. if (!PACKET_get_net_2(&sub, &type)
  837. || !PACKET_get_length_prefixed_2(&sub, &subext))
  838. return 0;
  839. } while (type != exttype);
  840. /* Found it */
  841. end = PACKET_data(&sub);
  842. if (old_ext != NULL) {
  843. if (!WPACKET_init(&old_ext_wpkt, old_ext))
  844. return 0;
  845. if (!WPACKET_memcpy(&old_ext_wpkt, PACKET_data(&subext),
  846. PACKET_remaining(&subext))
  847. || !WPACKET_get_total_written(&old_ext_wpkt, &w)) {
  848. WPACKET_cleanup(&old_ext_wpkt);
  849. return 0;
  850. }
  851. WPACKET_finish(&old_ext_wpkt);
  852. old_ext->length = w;
  853. }
  854. /*
  855. * If we're not the last extension we need to move the rest earlier. The
  856. * cast below is safe because we own the underlying buffer and we're no
  857. * longer making PACKET calls.
  858. */
  859. if (end < ext + *extlen)
  860. memmove((unsigned char *)start, end, end - start);
  861. /*
  862. * Calculate new extensions payload length =
  863. * Original length
  864. * - 2 extension block length bytes
  865. * - length of removed extension
  866. */
  867. newlen = *extlen - 2 - (end - start);
  868. /* Fixup the length bytes for the extension block */
  869. ext[0] = (unsigned char)((newlen >> 8) & 0xff);
  870. ext[1] = (unsigned char)((newlen ) & 0xff);
  871. /*
  872. * Length of the whole extension block is the new payload length plus the
  873. * 2 bytes for the length
  874. */
  875. *extlen = newlen + 2;
  876. /* We can now resize the message */
  877. if ((size_t)(end - start) + SSL3_HM_HEADER_LENGTH > msglen)
  878. return 0; /* Should not happen */
  879. msglen -= (end - start) + SSL3_HM_HEADER_LENGTH;
  880. if (!qtest_fault_resize_message(fault, msglen))
  881. return 0;
  882. return 1;
  883. }
  884. #define BIO_TYPE_CIPHER_PACKET_FILTER (0x80 | BIO_TYPE_FILTER)
  885. static BIO_METHOD *pcipherbiometh = NULL;
  886. # define BIO_MSG_N(array, stride, n) (*(BIO_MSG *)((char *)(array) + (n)*(stride)))
  887. static int pcipher_sendmmsg(BIO *b, BIO_MSG *msg, size_t stride,
  888. size_t num_msg, uint64_t flags,
  889. size_t *num_processed)
  890. {
  891. QTEST_FAULT *fault;
  892. BIO *next = BIO_next(b);
  893. ossl_ssize_t ret = 0;
  894. size_t i = 0, tmpnump;
  895. QUIC_PKT_HDR hdr;
  896. PACKET pkt;
  897. unsigned char *tmpdata;
  898. if (next == NULL)
  899. return 0;
  900. fault = BIO_get_data(b);
  901. if (fault == NULL
  902. || (fault->pciphercb == NULL && fault->datagramcb == NULL))
  903. return BIO_sendmmsg(next, msg, stride, num_msg, flags, num_processed);
  904. if (num_msg == 0) {
  905. *num_processed = 0;
  906. return 1;
  907. }
  908. for (i = 0; i < num_msg; ++i) {
  909. fault->msg = BIO_MSG_N(msg, stride, i);
  910. /* Take a copy of the data so that callbacks can modify it */
  911. tmpdata = OPENSSL_malloc(fault->msg.data_len + GROWTH_ALLOWANCE);
  912. if (tmpdata == NULL)
  913. return 0;
  914. memcpy(tmpdata, fault->msg.data, fault->msg.data_len);
  915. fault->msg.data = tmpdata;
  916. fault->msgalloc = fault->msg.data_len + GROWTH_ALLOWANCE;
  917. if (fault->pciphercb != NULL) {
  918. if (!PACKET_buf_init(&pkt, fault->msg.data, fault->msg.data_len))
  919. return 0;
  920. do {
  921. if (!ossl_quic_wire_decode_pkt_hdr(&pkt,
  922. /*
  923. * TODO(QUIC SERVER):
  924. * Needs to be set to the actual short header CID length
  925. * when testing the server implementation.
  926. */
  927. 0,
  928. 1,
  929. 0, &hdr, NULL))
  930. goto out;
  931. /*
  932. * hdr.data is const - but its our buffer so casting away the
  933. * const is safe
  934. */
  935. if (!fault->pciphercb(fault, &hdr, (unsigned char *)hdr.data,
  936. hdr.len, fault->pciphercbarg))
  937. goto out;
  938. /*
  939. * At the moment modifications to hdr by the callback
  940. * are ignored. We might need to rewrite the QUIC header to
  941. * enable tests to change this. We also don't yet have a
  942. * mechanism for the callback to change the encrypted data
  943. * length. It's not clear if that's needed or not.
  944. */
  945. } while (PACKET_remaining(&pkt) > 0);
  946. }
  947. if (fault->datagramcb != NULL
  948. && !fault->datagramcb(fault, &fault->msg, stride,
  949. fault->datagramcbarg))
  950. goto out;
  951. if (!BIO_sendmmsg(next, &fault->msg, stride, 1, flags, &tmpnump)) {
  952. *num_processed = i;
  953. goto out;
  954. }
  955. OPENSSL_free(fault->msg.data);
  956. fault->msg.data = NULL;
  957. fault->msgalloc = 0;
  958. }
  959. *num_processed = i;
  960. out:
  961. ret = i > 0;
  962. OPENSSL_free(fault->msg.data);
  963. fault->msg.data = NULL;
  964. return ret;
  965. }
  966. static long pcipher_ctrl(BIO *b, int cmd, long larg, void *parg)
  967. {
  968. BIO *next = BIO_next(b);
  969. if (next == NULL)
  970. return -1;
  971. return BIO_ctrl(next, cmd, larg, parg);
  972. }
  973. BIO_METHOD *qtest_get_bio_method(void)
  974. {
  975. BIO_METHOD *tmp;
  976. if (pcipherbiometh != NULL)
  977. return pcipherbiometh;
  978. tmp = BIO_meth_new(BIO_TYPE_CIPHER_PACKET_FILTER, "Cipher Packet Filter");
  979. if (!TEST_ptr(tmp))
  980. return NULL;
  981. if (!TEST_true(BIO_meth_set_sendmmsg(tmp, pcipher_sendmmsg))
  982. || !TEST_true(BIO_meth_set_ctrl(tmp, pcipher_ctrl)))
  983. goto err;
  984. pcipherbiometh = tmp;
  985. tmp = NULL;
  986. err:
  987. BIO_meth_free(tmp);
  988. return pcipherbiometh;
  989. }
  990. int qtest_fault_set_packet_cipher_listener(QTEST_FAULT *fault,
  991. qtest_fault_on_packet_cipher_cb pciphercb,
  992. void *pciphercbarg)
  993. {
  994. fault->pciphercb = pciphercb;
  995. fault->pciphercbarg = pciphercbarg;
  996. return 1;
  997. }
  998. int qtest_fault_set_datagram_listener(QTEST_FAULT *fault,
  999. qtest_fault_on_datagram_cb datagramcb,
  1000. void *datagramcbarg)
  1001. {
  1002. fault->datagramcb = datagramcb;
  1003. fault->datagramcbarg = datagramcbarg;
  1004. return 1;
  1005. }
  1006. /* To be called from a datagram_listener callback */
  1007. int qtest_fault_resize_datagram(QTEST_FAULT *fault, size_t newlen)
  1008. {
  1009. if (newlen > fault->msgalloc)
  1010. return 0;
  1011. if (newlen > fault->msg.data_len)
  1012. memset((unsigned char *)fault->msg.data + fault->msg.data_len, 0,
  1013. newlen - fault->msg.data_len);
  1014. fault->msg.data_len = newlen;
  1015. return 1;
  1016. }
  1017. int qtest_fault_set_bw_limit(QTEST_FAULT *fault,
  1018. size_t ctos_bw, size_t stoc_bw,
  1019. int noise_rate)
  1020. {
  1021. BIO *sbio = fault->noiseargs.sbio;
  1022. BIO *cbio = fault->noiseargs.cbio;
  1023. if (!TEST_ptr(sbio) || !TEST_ptr(cbio))
  1024. return 0;
  1025. if (!TEST_int_eq(BIO_ctrl(sbio, BIO_CTRL_NOISE_RATE, noise_rate, NULL), 1))
  1026. return 0;
  1027. if (!TEST_int_eq(BIO_ctrl(cbio, BIO_CTRL_NOISE_RATE, noise_rate, NULL), 1))
  1028. return 0;
  1029. /* We set the bandwidth limit on the sending side */
  1030. if (!TEST_int_eq(BIO_ctrl(cbio, BIO_CTRL_NOISE_SEND_BANDWIDTH,
  1031. (long)ctos_bw, NULL), 1))
  1032. return 0;
  1033. if (!TEST_int_eq(BIO_ctrl(sbio, BIO_CTRL_NOISE_SEND_BANDWIDTH,
  1034. (long)stoc_bw, NULL), 1))
  1035. return 0;
  1036. return 1;
  1037. }
  1038. int bio_msg_copy(BIO_MSG *dst, BIO_MSG *src)
  1039. {
  1040. /*
  1041. * Note it is assumed that the originally allocated data sizes for dst and
  1042. * src are the same
  1043. */
  1044. memcpy(dst->data, src->data, src->data_len);
  1045. dst->data_len = src->data_len;
  1046. dst->flags = src->flags;
  1047. if (dst->local != NULL) {
  1048. if (src->local != NULL) {
  1049. if (!TEST_true(BIO_ADDR_copy(dst->local, src->local)))
  1050. return 0;
  1051. } else {
  1052. BIO_ADDR_clear(dst->local);
  1053. }
  1054. }
  1055. if (!TEST_true(BIO_ADDR_copy(dst->peer, src->peer)))
  1056. return 0;
  1057. return 1;
  1058. }