quic.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. /* quic.c QUIC unit tests
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <tests/unit.h>
  26. #ifdef WOLFSSL_QUIC
  27. #include <wolfssl/ssl.h>
  28. #include <wolfssl/quic.h>
  29. #ifdef NO_INLINE
  30. #include <wolfssl/wolfcrypt/misc.h>
  31. #else
  32. #define WOLFSSL_MISC_INCLUDED
  33. #include <wolfcrypt/src/misc.c>
  34. #endif
  35. #include <wolfssl/error-ssl.h>
  36. #include <wolfssl/internal.h>
  37. #define testingFmt " %s:"
  38. #define resultFmt " %s\n"
  39. static const char* passed = "passed";
  40. static const char* failed = "failed";
  41. typedef struct {
  42. const char *name;
  43. WOLFSSL_METHOD *method;
  44. int is_server;
  45. } ctx_setups;
  46. static int dummy_set_encryption_secrets(WOLFSSL *ssl, WOLFSSL_ENCRYPTION_LEVEL level,
  47. const uint8_t *read_secret,
  48. const uint8_t *write_secret, size_t secret_len)
  49. {
  50. (void)ssl;
  51. printf("QUIC_set_encryption_secrets(level=%d, length=%d, rx=%s, tx=%s)\n",
  52. level, (int)secret_len, read_secret? "yes" : "no", write_secret? "yes" : "no");
  53. return 1;
  54. }
  55. static int dummy_add_handshake_data(WOLFSSL *ssl, WOLFSSL_ENCRYPTION_LEVEL level,
  56. const uint8_t *data, size_t len)
  57. {
  58. (void)ssl;
  59. (void)data;
  60. printf("QUIC_add_handshake_data(level=%d, length=%d)\n", level, (int)len);
  61. return 1;
  62. }
  63. static int dummy_flush_flight(WOLFSSL *ssl)
  64. {
  65. (void)ssl;
  66. printf("QUIC_flush_flight()\n");
  67. return 1;
  68. }
  69. static int dummy_send_alert(WOLFSSL *ssl, WOLFSSL_ENCRYPTION_LEVEL level, uint8_t err)
  70. {
  71. (void)ssl;
  72. printf("QUIC_send_alert(level=%d, err=%d)\n", level, err);
  73. return 1;
  74. }
  75. static WOLFSSL_QUIC_METHOD dummy_method = {
  76. dummy_set_encryption_secrets,
  77. dummy_add_handshake_data,
  78. dummy_flush_flight,
  79. dummy_send_alert,
  80. };
  81. static WOLFSSL_QUIC_METHOD null_method = {
  82. NULL, NULL, NULL, NULL
  83. };
  84. static int test_set_quic_method(void) {
  85. WOLFSSL_CTX *ctx;
  86. WOLFSSL *ssl;
  87. int ret = 0, i;
  88. const uint8_t *data;
  89. size_t data_len;
  90. ctx_setups valids[] = {
  91. #ifdef WOLFSSL_TLS13
  92. { "TLSv1.3 server", wolfTLSv1_3_server_method(), 1},
  93. { "TLSv1.3 client", wolfTLSv1_3_client_method(), 0},
  94. #endif
  95. { NULL, NULL, 0}
  96. };
  97. ctx_setups invalids[] = {
  98. #ifndef WOLFSSL_NO_TLS12
  99. { "TLSv1.2 server", wolfTLSv1_2_server_method(), 1},
  100. { "TLSv1.2 client", wolfTLSv1_2_client_method(), 0},
  101. #endif
  102. #ifndef NO_OLD_TLS
  103. { "TLSv1.1 server", wolfTLSv1_1_server_method(), 1},
  104. { "TLSv1.1 client", wolfTLSv1_1_client_method(), 0},
  105. #endif
  106. { NULL, NULL, 0}
  107. };
  108. for (i = 0; valids[i].name != NULL; ++i) {
  109. AssertNotNull(ctx = wolfSSL_CTX_new(valids[i].method));
  110. if (valids[i].is_server) {
  111. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  112. WOLFSSL_FILETYPE_PEM));
  113. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  114. WOLFSSL_FILETYPE_PEM));
  115. }
  116. /* ctx does not have quic enabled, so will SSL* derived from it */
  117. AssertNotNull(ssl = wolfSSL_new(ctx));
  118. AssertFalse(wolfSSL_is_quic(ssl));
  119. /* Enable quic on the SSL* */
  120. AssertFalse(wolfSSL_set_quic_method(ssl, &null_method) == WOLFSSL_SUCCESS);
  121. AssertTrue(wolfSSL_set_quic_method(ssl, &dummy_method) == WOLFSSL_SUCCESS);
  122. AssertTrue(wolfSSL_is_quic(ssl));
  123. /* Check some default, initial behaviour */
  124. AssertTrue(wolfSSL_set_quic_transport_params(ssl, NULL, 0) == WOLFSSL_SUCCESS);
  125. wolfSSL_get_peer_quic_transport_params(ssl, &data, &data_len);
  126. AssertNull(data);
  127. AssertTrue(data_len == 0);
  128. AssertTrue(wolfSSL_quic_read_level(ssl) == wolfssl_encryption_initial);
  129. AssertTrue(wolfSSL_quic_write_level(ssl) == wolfssl_encryption_initial);
  130. AssertTrue(wolfSSL_get_quic_transport_version(ssl) == 0);
  131. wolfSSL_set_quic_transport_version(ssl, TLSX_KEY_QUIC_TP_PARAMS);
  132. AssertTrue(wolfSSL_get_quic_transport_version(ssl) == TLSX_KEY_QUIC_TP_PARAMS);
  133. wolfSSL_set_quic_use_legacy_codepoint(ssl, 1);
  134. AssertTrue(wolfSSL_get_quic_transport_version(ssl) == TLSX_KEY_QUIC_TP_PARAMS_DRAFT);
  135. wolfSSL_set_quic_use_legacy_codepoint(ssl, 0);
  136. AssertTrue(wolfSSL_get_quic_transport_version(ssl) == TLSX_KEY_QUIC_TP_PARAMS);
  137. /* max flight len during stages of handhshake, we us 16k initial and on
  138. * app data, and during handshake allow larger for cert exchange. This is
  139. * more advisory for the network code. ngtcp2 has its own ideas, for example.
  140. */
  141. data_len = wolfSSL_quic_max_handshake_flight_len(ssl, wolfssl_encryption_initial);
  142. AssertTrue(data_len == 16*1024);
  143. data_len = wolfSSL_quic_max_handshake_flight_len(ssl, wolfssl_encryption_early_data);
  144. AssertTrue(data_len == 0);
  145. data_len = wolfSSL_quic_max_handshake_flight_len(ssl, wolfssl_encryption_handshake);
  146. AssertTrue(data_len >= 16*1024);
  147. data_len = wolfSSL_quic_max_handshake_flight_len(ssl, wolfssl_encryption_application);
  148. AssertTrue(data_len == 16*1024);
  149. wolfSSL_free(ssl);
  150. /* Enabled quic on the ctx */
  151. AssertTrue(wolfSSL_CTX_set_quic_method(ctx, &dummy_method) == WOLFSSL_SUCCESS);
  152. /* It will be enabled on the SSL* */
  153. AssertNotNull(ssl = wolfSSL_new(ctx));
  154. AssertTrue(wolfSSL_is_quic(ssl));
  155. wolfSSL_free(ssl);
  156. wolfSSL_CTX_free(ctx);
  157. }
  158. for (i = 0; invalids[i].name != NULL; ++i) {
  159. AssertNotNull(ctx = wolfSSL_CTX_new(invalids[i].method));
  160. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  161. WOLFSSL_FILETYPE_PEM));
  162. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  163. WOLFSSL_FILETYPE_PEM));
  164. AssertFalse(wolfSSL_CTX_set_quic_method(ctx, &dummy_method) == WOLFSSL_SUCCESS);
  165. AssertNotNull(ssl = wolfSSL_new(ctx));
  166. AssertFalse(wolfSSL_set_quic_method(ssl, &dummy_method) == WOLFSSL_SUCCESS);
  167. AssertFalse(wolfSSL_is_quic(ssl));
  168. /* even though not quic, this is the only level we can return */
  169. AssertTrue(wolfSSL_quic_read_level(ssl) == wolfssl_encryption_initial);
  170. AssertTrue(wolfSSL_quic_write_level(ssl) == wolfssl_encryption_initial);
  171. wolfSSL_free(ssl);
  172. wolfSSL_CTX_free(ctx);
  173. }
  174. printf(" test_set_quic_method: %s\n", (ret == 0)? passed : failed);
  175. return ret;
  176. }
  177. static size_t fake_record(byte rtype, word32 rlen, uint8_t *rec)
  178. {
  179. rec[0] = (uint8_t)rtype;
  180. c32to24(rlen, rec+1);
  181. return rlen + 4;
  182. }
  183. static size_t shift_record(uint8_t *rec, size_t len, size_t written)
  184. {
  185. len -= written;
  186. XMEMMOVE(rec, rec+written, len);
  187. return len;
  188. }
  189. static void dump_buffer(const char *name, const byte *p, size_t len, int indent)
  190. {
  191. size_t i = 0;
  192. printf("%s[%d] = {", name, (int)len);
  193. while((p != NULL) && (i < len)) {
  194. if((i % 0x10) == 0) {
  195. printf("\n");
  196. printf("%*s %04X - ", indent, " ", (int)i);
  197. }
  198. else if((i % 0x08) == 0) {
  199. printf(" ");
  200. }
  201. printf("%02X ", p[i]);
  202. i++;
  203. }
  204. printf("\n%*s};\n", indent, " ");
  205. }
  206. static void dump_ssl_buffers(WOLFSSL *ssl, FILE *fp)
  207. {
  208. QuicRecord *qr = ssl->quic.input_head;
  209. fprintf(fp, "SSL quic data buffered: \n");
  210. while (qr) {
  211. fprintf(fp, " - %d-%d/%d (cap %d, level=%d)\n",
  212. qr->start, qr->end, qr->len, qr->capacity, qr->level);
  213. qr = qr->next;
  214. }
  215. if ((qr = ssl->quic.scratch)) {
  216. fprintf(fp, " scratch: %d-%d/%d (cap %d, level=%d)\n",
  217. qr->start, qr->end, qr->len, qr->capacity, qr->level);
  218. }
  219. else {
  220. fprintf(fp, " scratch: -\n");
  221. }
  222. }
  223. static int provide_data(WOLFSSL *ssl, WOLFSSL_ENCRYPTION_LEVEL level,
  224. const uint8_t *data, size_t len, int excpect_fail)
  225. {
  226. int ret;
  227. ret = (wolfSSL_provide_quic_data(ssl, level, data, len) == WOLFSSL_SUCCESS);
  228. if (!!ret != !excpect_fail) {
  229. dump_ssl_buffers(ssl, stdout);
  230. return 0;
  231. }
  232. return 1;
  233. }
  234. static int test_provide_quic_data(void) {
  235. WOLFSSL_CTX *ctx;
  236. WOLFSSL *ssl;
  237. uint8_t lbuffer[16*1024];
  238. size_t len;
  239. int ret = 0;
  240. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  241. AssertTrue(wolfSSL_CTX_set_quic_method(ctx, &dummy_method) == WOLFSSL_SUCCESS);
  242. /* provide_quic_data() feeds CRYPTO packets inside a QUIC Frame into
  243. * the TLSv1.3 state machine.
  244. * The data fed is not the QUIC frame, but the TLS record inside it.
  245. * This may be called several times before SSL_do_handshake() is invoked
  246. * to process them.
  247. * During buffering this data, the code checks that:
  248. * - encryption level only ever increases for subsequent TLS records
  249. * - a TLS record is received complete before the encryption level increases
  250. */
  251. AssertNotNull(ssl = wolfSSL_new(ctx));
  252. len = fake_record(1, 100, lbuffer);
  253. AssertTrue(provide_data(ssl, wolfssl_encryption_initial, lbuffer, len, 0));
  254. len = fake_record(2, 1523, lbuffer);
  255. AssertTrue(provide_data(ssl, wolfssl_encryption_handshake, lbuffer, len, 0));
  256. len = fake_record(2, 1, lbuffer);
  257. len += fake_record(3, 190, lbuffer+len);
  258. AssertTrue(provide_data(ssl, wolfssl_encryption_handshake, lbuffer, len, 0));
  259. len = fake_record(5, 2049, lbuffer);
  260. AssertTrue(provide_data(ssl, wolfssl_encryption_application, lbuffer, len, 0));
  261. /* adding another record with decreased level must fail */
  262. len = fake_record(1, 100, lbuffer);
  263. AssertTrue(provide_data(ssl, wolfssl_encryption_initial, lbuffer, len, 1));
  264. wolfSSL_free(ssl);
  265. AssertNotNull(ssl = wolfSSL_new(ctx));
  266. len = fake_record(1, 100, lbuffer);
  267. AssertTrue(provide_data(ssl, wolfssl_encryption_initial, lbuffer, 24, 0));
  268. len = shift_record(lbuffer, len, 24);
  269. len += fake_record(2, 4000, lbuffer+len);
  270. AssertTrue(provide_data(ssl, wolfssl_encryption_initial, lbuffer, len - 99, 0));
  271. len = shift_record(lbuffer, len, len - 99);
  272. len += fake_record(5, 2049, lbuffer+len);
  273. AssertTrue(provide_data(ssl, wolfssl_encryption_initial, lbuffer, len, 0));
  274. /* should be recognized as complete and level increase needs to be accepted */
  275. len = fake_record(2, 1, lbuffer);
  276. len += fake_record(3, 190, lbuffer+len);
  277. AssertTrue(provide_data(ssl, wolfssl_encryption_handshake, lbuffer, len - 10, 0));
  278. len = shift_record(lbuffer, len, len - 10);
  279. /* Change level with incomplete record in lbuffer, needs to fail */
  280. len += fake_record(5, 8102, lbuffer+len);
  281. AssertTrue(provide_data(ssl, wolfssl_encryption_application, lbuffer, len - 10, 1));
  282. wolfSSL_free(ssl);
  283. wolfSSL_CTX_free(ctx);
  284. printf(" test_provide_quic_data: %s\n", (ret == 0)? passed : failed);
  285. return 0;
  286. }
  287. static int test_quic_crypt(void) {
  288. WOLFSSL_CTX *ctx;
  289. WOLFSSL *ssl;
  290. const WOLFSSL_EVP_CIPHER *aead_cipher;
  291. int ret = 0;
  292. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  293. AssertTrue(wolfSSL_CTX_set_quic_method(ctx, &dummy_method) == WOLFSSL_SUCCESS);
  294. AssertNotNull(ssl = wolfSSL_new(ctx));
  295. /* don't have an AEAD cipher selected before start */
  296. AssertTrue(wolfSSL_CIPHER_get_id(wolfSSL_get_current_cipher(ssl)) == 0);
  297. AssertNotNull(aead_cipher = wolfSSL_EVP_aes_128_gcm());
  298. AssertTrue(wolfSSL_quic_aead_is_gcm(aead_cipher) != 0);
  299. AssertTrue(wolfSSL_quic_aead_is_ccm(aead_cipher) == 0);
  300. AssertTrue(wolfSSL_quic_aead_is_chacha20(aead_cipher) == 0);
  301. if (1) {
  302. /* check that our enc-/decrypt support in quic rount-trips */
  303. static const uint8_t key[16] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  304. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
  305. static const uint8_t aad[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
  306. static const uint8_t iv[] = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
  307. static const uint8_t plaintext[] = "hello world\nhello world\nhello world\nhello world\nhello world\nhello world\nhello world\n";
  308. static const uint8_t expected[] = {0xd3, 0xa8, 0x1d, 0x96, 0x4c, 0x9b, 0x02, 0xd7, 0x9a, 0xb0, 0x41, 0x07, 0x4c, 0x8c, 0xe2,
  309. 0xe0, 0x2e, 0x83, 0x54, 0x52, 0x45, 0xcb, 0xd4, 0x68, 0xc8, 0x43, 0x45, 0xca, 0x91, 0xfb,
  310. 0xa3, 0x7a, 0x67, 0xed, 0xe8, 0xd7, 0x5e, 0xe2, 0x33, 0xd1, 0x3e, 0xbf, 0x50, 0xc2, 0x4b,
  311. 0x86, 0x83, 0x55, 0x11, 0xbb, 0x17, 0x4f, 0xf5, 0x78, 0xb8, 0x65, 0xeb, 0x9a, 0x2b, 0x8f,
  312. 0x77, 0x08, 0xa9, 0x60, 0x17, 0x73, 0xc5, 0x07, 0xf3, 0x04, 0xc9, 0x3f, 0x67, 0x4d, 0x12,
  313. 0xa1, 0x02, 0x93, 0xc2, 0x3c, 0xd3, 0xf8, 0x59, 0x33, 0xd5, 0x01, 0xc3, 0xbb, 0xaa, 0xe6,
  314. 0x3f, 0xbb, 0x23, 0x66, 0x94, 0x26, 0x28, 0x43, 0xa5, 0xfd, 0x2f};
  315. WOLFSSL_EVP_CIPHER_CTX *enc_ctx, *dec_ctx;
  316. uint8_t *encrypted, *decrypted;
  317. size_t tag_len, enc_len, dec_len;
  318. AssertTrue((tag_len = wolfSSL_quic_get_aead_tag_len(aead_cipher)) == 16);
  319. dec_len = sizeof(plaintext);
  320. enc_len = dec_len + tag_len;
  321. encrypted = (uint8_t*)XMALLOC(enc_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  322. AssertNotNull(encrypted);
  323. decrypted = (uint8_t*)XMALLOC(dec_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  324. AssertNotNull(decrypted);
  325. AssertNotNull(enc_ctx = wolfSSL_quic_crypt_new(aead_cipher, key, iv, 1));
  326. AssertTrue(wolfSSL_quic_aead_encrypt(encrypted, enc_ctx,
  327. plaintext, sizeof(plaintext),
  328. NULL, aad, sizeof(aad)) == WOLFSSL_SUCCESS);
  329. AssertTrue(memcmp(expected, encrypted, dec_len) == 0);
  330. AssertTrue(memcmp(expected+dec_len, encrypted+dec_len, tag_len) == 0);
  331. AssertNotNull(dec_ctx = wolfSSL_quic_crypt_new(aead_cipher, key, iv, 0));
  332. AssertTrue(wolfSSL_quic_aead_decrypt(decrypted, dec_ctx,
  333. encrypted, enc_len,
  334. NULL, aad, sizeof(aad)) == WOLFSSL_SUCCESS);
  335. AssertTrue(memcmp(plaintext, decrypted, dec_len) == 0);
  336. XFREE(encrypted, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  337. XFREE(decrypted, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  338. wolfSSL_EVP_CIPHER_CTX_free(enc_ctx);
  339. wolfSSL_EVP_CIPHER_CTX_free(dec_ctx);
  340. }
  341. wolfSSL_free(ssl);
  342. wolfSSL_CTX_free(ctx);
  343. printf(" test_quic_crypt: %s\n", (ret == 0)? passed : failed);
  344. return ret;
  345. }
  346. typedef struct OutputBuffer {
  347. byte data[64*1024];
  348. size_t len;
  349. WOLFSSL_ENCRYPTION_LEVEL level;
  350. struct OutputBuffer *next;
  351. } OutputBuffer;
  352. typedef struct {
  353. const char *name;
  354. WOLFSSL *ssl;
  355. OutputBuffer output;
  356. byte rx_secret[4][1024];
  357. size_t rx_secret_len[4];
  358. byte tx_secret[4][1024];
  359. size_t tx_secret_len[4];
  360. int handshake_done;
  361. int alert_level;
  362. int alert;
  363. int flushed;
  364. int verbose;
  365. byte ticket[16*1024];
  366. word32 ticket_len;
  367. byte session[16*1024];
  368. word32 session_len;
  369. } QuicTestContext;
  370. static int ctx_set_encryption_secrets(WOLFSSL *ssl, WOLFSSL_ENCRYPTION_LEVEL level,
  371. const uint8_t *read_secret,
  372. const uint8_t *write_secret, size_t secret_len);
  373. static int ctx_add_handshake_data(WOLFSSL *ssl, WOLFSSL_ENCRYPTION_LEVEL level,
  374. const uint8_t *data, size_t len);
  375. static int ctx_flush_flight(WOLFSSL *ssl);
  376. static int ctx_send_alert(WOLFSSL *ssl, WOLFSSL_ENCRYPTION_LEVEL level, uint8_t err);
  377. #ifdef HAVE_SESSION_TICKET
  378. static int ctx_session_ticket_cb(WOLFSSL* ssl,
  379. const unsigned char* ticket, int ticketSz,
  380. void* cb_ctx);
  381. #endif
  382. static WOLFSSL_QUIC_METHOD ctx_method = {
  383. ctx_set_encryption_secrets,
  384. ctx_add_handshake_data,
  385. ctx_flush_flight,
  386. ctx_send_alert,
  387. };
  388. static void QuicTestContext_init(QuicTestContext *tctx, WOLFSSL_CTX *ctx,
  389. const char *name, int verbose)
  390. {
  391. static const byte tp_params_c[] = {0, 1, 2, 3, 4, 5, 6, 7};
  392. static const byte tp_params_s[] = {7, 6, 5, 4, 3, 2, 1, 0, 1};
  393. AssertNotNull(tctx);
  394. memset(tctx, 0, sizeof(*tctx));
  395. tctx->name = name;
  396. AssertNotNull((tctx->ssl = wolfSSL_new(ctx)));
  397. tctx->verbose = verbose;
  398. wolfSSL_set_app_data(tctx->ssl, tctx);
  399. AssertTrue(wolfSSL_set_quic_method(tctx->ssl, &ctx_method) == WOLFSSL_SUCCESS);
  400. wolfSSL_set_verify(tctx->ssl, SSL_VERIFY_NONE, 0);
  401. #ifdef HAVE_SESSION_TICKET
  402. wolfSSL_UseSessionTicket(tctx->ssl);
  403. wolfSSL_set_SessionTicket_cb(tctx->ssl, ctx_session_ticket_cb, NULL);
  404. #endif
  405. if (wolfSSL_is_server(tctx->ssl)) {
  406. wolfSSL_set_quic_transport_version(tctx->ssl, 0);
  407. wolfSSL_set_quic_transport_params(tctx->ssl, tp_params_s, sizeof(tp_params_s));
  408. }
  409. else {
  410. wolfSSL_set_quic_transport_version(tctx->ssl, 0);
  411. wolfSSL_set_quic_transport_params(tctx->ssl, tp_params_c, sizeof(tp_params_c));
  412. }
  413. }
  414. static void QuicTestContext_free(QuicTestContext *tctx)
  415. {
  416. OutputBuffer *out, *n;
  417. if (tctx->ssl) {
  418. wolfSSL_free(tctx->ssl);
  419. tctx->ssl = NULL;
  420. }
  421. out = tctx->output.next;
  422. while (out) {
  423. n = out->next;
  424. free(out);
  425. out = n;
  426. }
  427. }
  428. static int ctx_set_encryption_secrets(WOLFSSL *ssl, WOLFSSL_ENCRYPTION_LEVEL level,
  429. const uint8_t *read_secret,
  430. const uint8_t *write_secret, size_t secret_len)
  431. {
  432. QuicTestContext *ctx = (QuicTestContext*)wolfSSL_get_app_data(ssl);
  433. AssertNotNull(ctx);
  434. AssertTrue(secret_len <= sizeof(ctx->rx_secret[0]));
  435. if (read_secret) {
  436. memcpy(ctx->rx_secret[level], read_secret, secret_len);
  437. ctx->rx_secret_len[level] = secret_len;
  438. }
  439. if (write_secret) {
  440. memcpy(ctx->tx_secret[level], write_secret, secret_len);
  441. ctx->tx_secret_len[level] = secret_len;
  442. }
  443. AssertNotNull(ctx);
  444. return 1;
  445. }
  446. static int ctx_add_handshake_data(WOLFSSL *ssl, WOLFSSL_ENCRYPTION_LEVEL level,
  447. const uint8_t *data, size_t len)
  448. {
  449. QuicTestContext *ctx = (QuicTestContext*)wolfSSL_get_app_data(ssl);
  450. OutputBuffer *out;
  451. AssertNotNull(ctx);
  452. out = &ctx->output;
  453. while (out->next) {
  454. out = out->next;
  455. }
  456. if (out->level != level) {
  457. if (out->len > 0) {
  458. out->next = (OutputBuffer*)calloc(1, sizeof(OutputBuffer));
  459. out = out->next;
  460. AssertNotNull(out);
  461. }
  462. out->level = level;
  463. }
  464. if (ctx->verbose) {
  465. printf("[%s] add_handshake[enc_level=%d]: %d bytes\n", ctx->name, level, (int)len);
  466. /* dump_buffer("add", data, len, 0); */
  467. }
  468. if (len > 0) {
  469. AssertTrue(out->len + len < sizeof(out->data));
  470. memcpy(out->data + out->len, data, len);
  471. out->len += len;
  472. }
  473. return 1;
  474. }
  475. static int ctx_flush_flight(WOLFSSL *ssl)
  476. {
  477. QuicTestContext *ctx = (QuicTestContext*)wolfSSL_get_app_data(ssl);
  478. AssertNotNull(ctx);
  479. ctx->flushed = 1;
  480. return 1;
  481. }
  482. static int ctx_send_alert(WOLFSSL *ssl, WOLFSSL_ENCRYPTION_LEVEL level, uint8_t err)
  483. {
  484. QuicTestContext *ctx = (QuicTestContext*)wolfSSL_get_app_data(ssl);
  485. if (ctx->verbose) {
  486. printf("[%s] send_alert: level=%d, err=%d\n", ctx->name, level, err);
  487. }
  488. AssertNotNull(ctx);
  489. ctx->alert_level = level;
  490. ctx->alert = alert;
  491. return 1;
  492. }
  493. #ifdef HAVE_SESSION_TICKET
  494. static int ctx_session_ticket_cb(WOLFSSL* ssl,
  495. const unsigned char* ticket, int ticketSz,
  496. void* cb_ctx)
  497. {
  498. QuicTestContext *ctx = (QuicTestContext*)wolfSSL_get_app_data(ssl);
  499. (void)cb_ctx;
  500. if (ticketSz < 0 || (size_t)ticketSz > sizeof(ctx->ticket)) {
  501. printf("SESSION TICKET callback: ticket given is too large: %d bytes\n", ticketSz);
  502. return 1;
  503. }
  504. memset(ctx->ticket, 0, sizeof(ctx->ticket));
  505. ctx->ticket_len = (word32)ticketSz;
  506. memcpy(ctx->ticket, ticket, ticketSz);
  507. if (ctx->verbose) {
  508. printf("Session Ticket[%s]: ", ctx->name);
  509. dump_buffer("", ticket, ticketSz, 4);
  510. }
  511. return 0;
  512. }
  513. #endif
  514. static void ctx_dump_output(QuicTestContext *ctx)
  515. {
  516. dump_buffer("Output", ctx->output.data, ctx->output.len, 0);
  517. }
  518. static void check_handshake_record(const byte *data, size_t data_len, int *ptype, size_t *prlen)
  519. {
  520. word32 rlen;
  521. AssertTrue(data_len >= HANDSHAKE_HEADER_SZ);
  522. *ptype = data[0];
  523. c24to32(&data[1], &rlen);
  524. *prlen = rlen + HANDSHAKE_HEADER_SZ;
  525. }
  526. static void ext_dump(const byte *data, size_t data_len, int indent)
  527. {
  528. size_t idx = 0;
  529. word16 len16, etype, i;
  530. printf("%*sextensions:\n", indent, " ");
  531. while (idx < data_len) {
  532. ato16(&data[idx], &etype); /* extension type */
  533. ato16(&data[idx+2], &len16); /* extension length */
  534. printf(" extension: %04x [", etype);
  535. for (i = 0; i < len16; ++i) {
  536. printf("%s0x%02x", (i? ", ": ""), data[idx+4+i]);
  537. }
  538. printf("]\n");
  539. idx += 2 + 2 + len16;
  540. }
  541. }
  542. static const byte *ext_find(const byte *data, size_t data_len, int ext_type)
  543. {
  544. size_t idx = 0;
  545. word16 len16, etype;
  546. while (idx < data_len) {
  547. ato16(&data[idx], &etype); /* extension type */
  548. if (etype == ext_type) {
  549. return data + idx;
  550. }
  551. ato16(&data[idx+2], &len16); /* extension length */
  552. idx += 2 + 2 + len16;
  553. }
  554. return NULL;
  555. }
  556. static int ext_has(const byte *data, size_t data_len, int ext_type)
  557. {
  558. return ext_find(data, data_len,ext_type) != NULL;
  559. }
  560. static void ext_equals(const byte *data, size_t data_len, int ext_type,
  561. const byte *exp_data, size_t exp_len)
  562. {
  563. const byte *ext;
  564. word16 len16;
  565. AssertNotNull(ext = ext_find(data, data_len, ext_type));
  566. ato16(&ext[2], &len16);
  567. AssertTrue(len16 == exp_len);
  568. AssertTrue(memcmp(ext + 4, exp_data, exp_len) == 0);
  569. }
  570. static void check_quic_client_hello(const byte *data, size_t data_len, int verbose, int indent)
  571. {
  572. size_t idx;
  573. word16 len16;
  574. const byte *exts;
  575. size_t exts_len, rec_len;
  576. int rec_type;
  577. static byte ext_sup_version[3] = {0x02, 0x03, 0x04};
  578. check_handshake_record(data, data_len, &rec_type, &rec_len);
  579. AssertIntEQ(rec_type, client_hello);
  580. idx = HANDSHAKE_HEADER_SZ;
  581. /* the client hello arrives alone */
  582. AssertIntEQ(rec_len, data_len);
  583. AssertTrue(data[idx++] == SSLv3_MAJOR);
  584. AssertTrue(data[idx++] == TLSv1_2_MINOR);
  585. idx += 32; /* 32 bytes RANDOM */
  586. AssertIntEQ(data[idx], 0); /* session id length MUST be 0, RFC9001 ch. 8.4 */
  587. idx += 1 + data[idx];
  588. ato16(&data[idx], &len16); /* ciphers length */
  589. AssertTrue(len16 > 0);
  590. idx += 2 + len16;
  591. AssertTrue(data[idx] == 1); /* compressions */
  592. AssertTrue(data[idx+1] == 0); /* no compression */
  593. idx += 2;
  594. ato16(&data[idx], &len16); /* extensions length */
  595. AssertTrue(len16 > 0);
  596. exts_len = len16;
  597. idx += 2;
  598. exts = &data[idx];
  599. idx += exts_len;
  600. AssertTrue(idx <= rec_len); /* should fit */
  601. for (; idx < rec_len; ++idx) {
  602. AssertTrue(data[idx] == 0); /* padding */
  603. }
  604. ext_equals(exts, exts_len, TLSX_SUPPORTED_VERSIONS,
  605. ext_sup_version, sizeof(ext_sup_version));
  606. if (verbose) {
  607. ext_dump(exts, exts_len, indent);
  608. dump_buffer("", data, data_len, indent);
  609. }
  610. }
  611. static void check_quic_client_hello_tp(OutputBuffer *out, int tp_v1, int tp_draft)
  612. {
  613. size_t idx;
  614. word16 len16;
  615. const byte *exts;
  616. size_t exts_len, rec_len;
  617. int rec_type;
  618. check_handshake_record(out->data, out->len, &rec_type, &rec_len);
  619. AssertIntEQ(rec_type, client_hello);
  620. idx = HANDSHAKE_HEADER_SZ;
  621. idx += 2; /* old version */
  622. idx += 32; /* 32 bytes RANDOM */
  623. idx += 1 + out->data[idx]; /* session id */
  624. ato16(&out->data[idx], &len16); /* ciphers length */
  625. idx += 2 + len16;
  626. idx += 2; /* compression */
  627. ato16(&out->data[idx], &len16); /* extensions length */
  628. AssertTrue(len16 > 0);
  629. exts_len = len16;
  630. idx += 2;
  631. exts = &out->data[idx];
  632. AssertTrue(!ext_has(exts, exts_len, TLSX_KEY_QUIC_TP_PARAMS) == !tp_v1);
  633. AssertTrue(!ext_has(exts, exts_len, TLSX_KEY_QUIC_TP_PARAMS_DRAFT) == !tp_draft);
  634. }
  635. static void check_secrets(QuicTestContext *ctx, WOLFSSL_ENCRYPTION_LEVEL level, size_t rx_len, size_t tx_len)
  636. {
  637. int idx = (int)level;
  638. AssertTrue(idx < 4);
  639. AssertIntEQ(ctx->rx_secret_len[idx], rx_len);
  640. AssertIntEQ(ctx->tx_secret_len[idx], tx_len);
  641. }
  642. static void assert_secrets_EQ(QuicTestContext *ctx1, QuicTestContext *ctx2,
  643. WOLFSSL_ENCRYPTION_LEVEL level)
  644. {
  645. int idx = (int)level;
  646. /* rx secrets are the other ones tx secrets */
  647. AssertIntEQ(ctx1->rx_secret_len[idx], ctx2->tx_secret_len[idx]);
  648. AssertIntEQ(ctx1->tx_secret_len[idx], ctx2->rx_secret_len[idx]);
  649. AssertIntEQ(memcmp(ctx1->rx_secret[idx], ctx2->tx_secret[idx], ctx1->rx_secret_len[idx]), 0);
  650. AssertIntEQ(memcmp(ctx1->tx_secret[idx], ctx2->rx_secret[idx], ctx1->tx_secret_len[idx]), 0);
  651. }
  652. static void check_ee(const byte *data, size_t data_len, int verbose, int indent)
  653. {
  654. size_t rec_len, exts_len, idx;
  655. word16 len16;
  656. const byte *exts;
  657. int rec_type;
  658. check_handshake_record(data, data_len, &rec_type, &rec_len);
  659. AssertIntEQ(rec_type, encrypted_extensions);
  660. idx = HANDSHAKE_HEADER_SZ;
  661. ato16(&data[idx], &len16); /* extensions length */
  662. AssertTrue(len16 > 0);
  663. exts_len = len16;
  664. idx += 2;
  665. exts = &data[idx];
  666. if (verbose) {
  667. ext_dump(exts, exts_len, indent);
  668. dump_buffer("", data, data_len, indent);
  669. }
  670. }
  671. static void check_quic_server_hello(const byte *data, size_t data_len, int verbose, int indent)
  672. {
  673. size_t idx;
  674. word16 len16, cipher;
  675. const byte *exts;
  676. size_t exts_len, rec_len;
  677. static byte ext_sup_version[2] = {0x03, 0x04};
  678. int rec_type;
  679. check_handshake_record(data, data_len, &rec_type, &rec_len);
  680. AssertIntEQ(rec_type, server_hello);
  681. idx = HANDSHAKE_HEADER_SZ;
  682. AssertTrue(data[idx++] == SSLv3_MAJOR);
  683. AssertTrue(data[idx++] == TLSv1_2_MINOR);
  684. idx += 32; /* 32 bytes RANDOM */
  685. /* AssertIntEQ(data[idx], 0); session id of len 0 */
  686. idx += 1 + data[idx];
  687. ato16(&data[idx], &cipher); /* cipher selected */
  688. AssertTrue(cipher != 0);
  689. idx += 2;
  690. AssertTrue(data[idx] == 0); /* null compression */
  691. idx += 1;
  692. ato16(&data[idx], &len16); /* extensions length */
  693. AssertTrue(len16 > 0);
  694. exts_len = len16;
  695. idx += 2;
  696. exts = &data[idx];
  697. idx += exts_len;
  698. AssertTrue(idx <= rec_len); /* should fit */
  699. for (; idx < rec_len; ++idx) {
  700. AssertTrue(data[idx] == 0); /* padding */
  701. }
  702. if (verbose) {
  703. ext_dump(exts, exts_len, indent);
  704. dump_buffer("", data, rec_len, indent);
  705. }
  706. ext_equals(exts, exts_len, TLSX_SUPPORTED_VERSIONS,
  707. ext_sup_version, sizeof(ext_sup_version));
  708. }
  709. static void check_crypto_rec(const byte *data, size_t data_len, int verbose, int indent)
  710. {
  711. size_t rec_len;
  712. int rec_type;
  713. check_handshake_record(data, data_len, &rec_type, &rec_len);
  714. if (verbose) {
  715. dump_buffer("", data, rec_len, indent);
  716. }
  717. }
  718. static void check_crypto_records(QuicTestContext *from, OutputBuffer *out, int indent, char *rec_log, size_t rec_log_size)
  719. {
  720. const byte *data = out->data;
  721. size_t data_len = out->len;
  722. size_t rec_len;
  723. int rec_type;
  724. const char *rec_name;
  725. char lbuffer[128];
  726. void (*check_rec) (const byte *d, size_t l, int v, int indent);
  727. while (data_len > 0) {
  728. check_handshake_record(data, data_len, &rec_type, &rec_len);
  729. if (rec_len > data_len) {
  730. printf("%*sINCOMPLETE CRYPTO?: ", indent, " ");
  731. dump_buffer("", data, data_len, indent);
  732. }
  733. AssertTrue(rec_len <= data_len);
  734. check_rec = check_crypto_rec;
  735. switch (rec_type) {
  736. case client_hello:
  737. rec_name = "ClientHello";
  738. check_rec = check_quic_client_hello;
  739. break;
  740. case server_hello:
  741. rec_name = "ServerHello";
  742. check_rec = check_quic_server_hello;
  743. break;
  744. case session_ticket:
  745. rec_name = "SessionTicket";
  746. break;
  747. case encrypted_extensions:
  748. rec_name = "EncryptedExtension";
  749. check_rec = check_ee;
  750. break;
  751. case certificate:
  752. rec_name = "Certificate";
  753. break;
  754. case certificate_verify:
  755. rec_name = "CertificateVerify";
  756. break;
  757. case finished:
  758. rec_name = "Finished";
  759. break;
  760. default:
  761. sprintf(lbuffer, "%d", rec_type);
  762. rec_name = lbuffer;
  763. break;
  764. }
  765. if (rec_log) {
  766. if (*rec_log) XSTRLCAT(rec_log, ":", rec_log_size);
  767. XSTRLCAT(rec_log, rec_name, rec_log_size);
  768. }
  769. if (from->verbose) printf("%*sCRYPTO[%s]: ", indent, " ", rec_name);
  770. check_rec(data, rec_len, from->verbose, indent);
  771. if (from->verbose) printf("\n");
  772. data += rec_len;
  773. data_len -= rec_len;
  774. }
  775. }
  776. static void QuicTestContext_forward(QuicTestContext *from, QuicTestContext *to, char *rec_log, size_t rec_log_size)
  777. {
  778. int ret;
  779. OutputBuffer *out, *old;
  780. out = &from->output;
  781. while (out->len > 0) {
  782. if (from->verbose) {
  783. printf("[%s -> %s] forward %d bytes at level %d\n",
  784. from->name, to->name, (int)out->len, out->level);
  785. }
  786. if (out->level == wolfssl_encryption_early_data) {
  787. if (from->verbose) dump_buffer("EarlyData", out->data, out->len, 4);
  788. }
  789. else {
  790. check_crypto_records(from, out, 4, rec_log, rec_log_size);
  791. }
  792. ret = wolfSSL_provide_quic_data(to->ssl, out->level, out->data, out->len);
  793. out->len = 0;
  794. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  795. if (out->next) {
  796. old = out->next;
  797. memcpy(out, out->next, sizeof(*out));
  798. free(old);
  799. }
  800. }
  801. }
  802. typedef struct {
  803. QuicTestContext *client;
  804. QuicTestContext *server;
  805. int started;
  806. int verbose;
  807. char rec_log[16*1024];
  808. int sent_early_data;
  809. int accept_early_data;
  810. char early_data[16*1024];
  811. size_t early_data_len;
  812. } QuicConversation;
  813. static void QuicConversation_init(QuicConversation *conv,
  814. QuicTestContext *tclient, QuicTestContext *tserver)
  815. {
  816. memset(conv, 0, sizeof(*conv));
  817. conv->client = tclient;
  818. conv->server = tserver;
  819. conv->verbose = tclient->verbose && tserver->verbose;
  820. }
  821. static int QuicConversation_start(QuicConversation *conv, const byte *data,
  822. size_t data_len, size_t *pwritten)
  823. {
  824. int ret;
  825. AssertFalse(conv->started);
  826. if (conv->verbose) {
  827. printf("[%s <-> %s] starting\n", conv->client->name, conv->server->name);
  828. }
  829. if (data && data_len > 0) {
  830. #ifdef WOLFSSL_EARLY_DATA
  831. int written;
  832. ret = wolfSSL_write_early_data(conv->client->ssl, data, (int)data_len, &written);
  833. if (ret < 0) {
  834. int err = wolfSSL_get_error(conv->client->ssl, ret);
  835. char lbuffer[1024];
  836. printf("EARLY DATA ret = %d, error = %d, %s\n", ret, err, wolfSSL_ERR_error_string(err, lbuffer));
  837. AssertTrue(0);
  838. }
  839. *pwritten = (size_t)written;
  840. conv->sent_early_data = 1;
  841. #else
  842. fprintf(stderr, "Cannot send EARLY DATA without feature enabled!\n");
  843. AssertTrue(0);
  844. #endif
  845. }
  846. else {
  847. ret = wolfSSL_connect(conv->client->ssl);
  848. if (ret != WOLFSSL_SUCCESS) {
  849. AssertIntEQ(wolfSSL_get_error(conv->client->ssl, 0), SSL_ERROR_WANT_READ);
  850. }
  851. if (pwritten) *pwritten = 0;
  852. }
  853. conv->started = 1;
  854. return ret;
  855. }
  856. static int QuicConversation_step(QuicConversation *conv, int may_fail)
  857. {
  858. int n;
  859. if (!conv->started) {
  860. n = wolfSSL_connect(conv->client->ssl);
  861. if (n != WOLFSSL_SUCCESS
  862. && wolfSSL_get_error(conv->client->ssl, 0) != SSL_ERROR_WANT_READ) {
  863. if (may_fail) return 0;
  864. AssertIntEQ(SSL_ERROR_WANT_READ, wolfSSL_get_error(conv->client->ssl, 0));
  865. }
  866. conv->started = 1;
  867. }
  868. if (conv->server->output.len > 0) {
  869. QuicTestContext_forward(conv->server, conv->client, conv->rec_log, sizeof(conv->rec_log));
  870. n = wolfSSL_quic_read_write(conv->client->ssl);
  871. if (n != WOLFSSL_SUCCESS
  872. && wolfSSL_get_error(conv->client->ssl, 0) != SSL_ERROR_WANT_READ) {
  873. if (may_fail) return 0;
  874. AssertIntEQ(SSL_ERROR_WANT_READ, wolfSSL_get_error(conv->client->ssl, 0));
  875. }
  876. return 1;
  877. }
  878. else if (conv->client->output.len > 0) {
  879. QuicTestContext_forward(conv->client, conv->server, conv->rec_log, sizeof(conv->rec_log));
  880. #ifdef WOLFSSL_EARLY_DATA
  881. if (conv->accept_early_data) {
  882. int written;
  883. n = wolfSSL_read_early_data(conv->server->ssl,
  884. conv->early_data + conv->early_data_len,
  885. (int)(sizeof(conv->early_data) - conv->early_data_len),
  886. &written);
  887. if (n < 0) {
  888. if (wolfSSL_get_error(conv->server->ssl, 0) != SSL_ERROR_WANT_READ) {
  889. if (may_fail) return 0;
  890. AssertIntEQ(wolfSSL_get_error(conv->server->ssl, 0), SSL_ERROR_WANT_READ);
  891. }
  892. }
  893. else if (n > 0) {
  894. conv->early_data_len += n;
  895. if (conv->verbose)
  896. printf("RECVed early data, len now=%d\n", (int)conv->early_data_len);
  897. }
  898. }
  899. else
  900. #endif /* WOLFSSL_EARLY_DATA */
  901. {
  902. n = wolfSSL_quic_read_write(conv->server->ssl);
  903. if (n != WOLFSSL_SUCCESS
  904. && wolfSSL_get_error(conv->server->ssl, 0) != SSL_ERROR_WANT_READ) {
  905. if (may_fail) return 0;
  906. AssertIntEQ(wolfSSL_get_error(conv->server->ssl, 0), SSL_ERROR_WANT_READ);
  907. }
  908. }
  909. return 1;
  910. }
  911. return 0;
  912. }
  913. static void QuicConversation_do(QuicConversation *conv)
  914. {
  915. if (!conv->started) {
  916. QuicConversation_start(conv, NULL, 0, NULL);
  917. }
  918. while (1) {
  919. if (!QuicConversation_step(conv, 0)) {
  920. int c_err = wolfSSL_get_error(conv->client->ssl, 0);
  921. int s_err = wolfSSL_get_error(conv->server->ssl, 0);
  922. if (c_err == 0 && s_err == 0) {
  923. break; /* handshake done */
  924. }
  925. printf("Neither tclient nor server have anything to send, "
  926. "but client_error=%d, server_error=%d\n",
  927. c_err, s_err);
  928. AssertFalse(1);
  929. }
  930. }
  931. }
  932. #ifdef HAVE_SESSION_TICKET
  933. static void QuicConversation_fail(QuicConversation *conv)
  934. {
  935. if (!conv->started) {
  936. QuicConversation_start(conv, NULL, 0, NULL);
  937. }
  938. while (1) {
  939. if (!QuicConversation_step(conv, 1)) {
  940. int c_err = wolfSSL_get_error(conv->client->ssl, 0);
  941. int s_err = wolfSSL_get_error(conv->server->ssl, 0);
  942. AssertTrue(c_err != 0 || s_err != 0);
  943. break;
  944. }
  945. }
  946. }
  947. #endif /* HAVE_SESSION_TICKET */
  948. static int test_quic_client_hello(int verbose) {
  949. WOLFSSL_CTX *ctx;
  950. int ret = 0;
  951. QuicTestContext tctx;
  952. (void)ctx_dump_output;
  953. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  954. QuicTestContext_init(&tctx, ctx, "client", verbose);
  955. /* Without any QUIC transport params, this needs to fail */
  956. AssertTrue(wolfSSL_set_quic_transport_params(tctx.ssl, NULL, 0) == WOLFSSL_SUCCESS);
  957. AssertTrue(wolfSSL_quic_read_write(tctx.ssl) != 0);
  958. AssertIntEQ(wolfSSL_get_error(tctx.ssl, 0), QUIC_TP_MISSING_E);
  959. QuicTestContext_free(&tctx);
  960. /* Set transport params, expect both extensions */
  961. QuicTestContext_init(&tctx, ctx, "client", verbose);
  962. #ifdef HAVE_SNI
  963. wolfSSL_UseSNI(tctx.ssl, WOLFSSL_SNI_HOST_NAME,
  964. "wolfssl.com", sizeof("wolfssl.com")-1);
  965. #endif
  966. AssertTrue(wolfSSL_connect(tctx.ssl) != 0);
  967. AssertIntEQ(wolfSSL_get_error(tctx.ssl, 0), SSL_ERROR_WANT_READ);
  968. check_quic_client_hello_tp(&tctx.output, 1, 1);
  969. QuicTestContext_free(&tctx);
  970. /* Set transport params v1, expect v1 extension */
  971. QuicTestContext_init(&tctx, ctx, "client", verbose);
  972. wolfSSL_set_quic_transport_version(tctx.ssl, TLSX_KEY_QUIC_TP_PARAMS);
  973. AssertTrue(wolfSSL_connect(tctx.ssl) != 0);
  974. check_quic_client_hello_tp(&tctx.output, 1, 0);
  975. QuicTestContext_free(&tctx);
  976. /* Set transport params draft, expect draft extension */
  977. QuicTestContext_init(&tctx, ctx, "client", verbose);
  978. wolfSSL_set_quic_transport_version(tctx.ssl, TLSX_KEY_QUIC_TP_PARAMS_DRAFT);
  979. AssertTrue(wolfSSL_connect(tctx.ssl) != 0);
  980. check_quic_client_hello_tp(&tctx.output, 0, 1);
  981. QuicTestContext_free(&tctx);
  982. /* Set transport params 0, expect both extension */
  983. QuicTestContext_init(&tctx, ctx, "client", verbose);
  984. wolfSSL_set_quic_transport_version(tctx.ssl, 0);
  985. AssertTrue(wolfSSL_connect(tctx.ssl) != 0);
  986. check_quic_client_hello_tp(&tctx.output, 1, 1);
  987. QuicTestContext_free(&tctx);
  988. wolfSSL_CTX_free(ctx);
  989. printf(" test_quic_client_hello: %s\n", (ret == 0)? passed : failed);
  990. return ret;
  991. }
  992. static int test_quic_server_hello(int verbose) {
  993. WOLFSSL_CTX *ctx_c, *ctx_s;
  994. int ret = 0;
  995. QuicTestContext tclient, tserver;
  996. QuicConversation conv;
  997. AssertNotNull(ctx_c = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  998. AssertNotNull(ctx_s = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  999. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1000. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1001. /* setup ssls */
  1002. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1003. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1004. /* connect */
  1005. QuicConversation_init(&conv, &tclient, &tserver);
  1006. QuicConversation_step(&conv, 0);
  1007. /* check established/missing secrets */
  1008. check_secrets(&tserver, wolfssl_encryption_initial, 0, 0);
  1009. check_secrets(&tserver, wolfssl_encryption_handshake, 32, 32);
  1010. check_secrets(&tserver, wolfssl_encryption_application, 32, 32);
  1011. check_secrets(&tclient, wolfssl_encryption_handshake, 0, 0);
  1012. /* feed the server data to the client */
  1013. QuicConversation_step(&conv, 0);
  1014. /* client has generated handshake secret */
  1015. check_secrets(&tclient, wolfssl_encryption_handshake, 32, 32);
  1016. /* continue the handshake till done */
  1017. conv.started = 1;
  1018. /* run till end */
  1019. QuicConversation_do(&conv);
  1020. AssertIntEQ(tclient.output.len, 0);
  1021. AssertIntEQ(tserver.output.len, 0);
  1022. /* what have we seen? */
  1023. #ifdef HAVE_SESSION_TICKET
  1024. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:"
  1025. "Certificate:CertificateVerify:Finished:Finished:SessionTicket");
  1026. #else
  1027. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:"
  1028. "Certificate:CertificateVerify:Finished:Finished");
  1029. #endif
  1030. /* we are at application encryption level */
  1031. AssertTrue(wolfSSL_quic_read_level(tclient.ssl) == wolfssl_encryption_application);
  1032. AssertTrue(wolfSSL_quic_write_level(tclient.ssl) == wolfssl_encryption_application);
  1033. AssertTrue(wolfSSL_quic_read_level(tserver.ssl) == wolfssl_encryption_application);
  1034. AssertTrue(wolfSSL_quic_write_level(tserver.ssl) == wolfssl_encryption_application);
  1035. /* the last client write (FINISHED) was at handshake level */
  1036. AssertTrue(tclient.output.level == wolfssl_encryption_handshake);
  1037. /* we have the app secrets */
  1038. check_secrets(&tclient, wolfssl_encryption_application, 32, 32);
  1039. check_secrets(&tserver, wolfssl_encryption_application, 32, 32);
  1040. /* verify client and server have the same secrets establishd */
  1041. assert_secrets_EQ(&tclient, &tserver, wolfssl_encryption_handshake);
  1042. assert_secrets_EQ(&tclient, &tserver, wolfssl_encryption_application);
  1043. /* AEAD cipher should be known */
  1044. AssertNotNull(wolfSSL_quic_get_aead(tclient.ssl));
  1045. AssertNotNull(wolfSSL_quic_get_aead(tserver.ssl));
  1046. /* What was negiotiated and is it the same? */
  1047. AssertIntEQ(wolfSSL_get_peer_quic_transport_version(tclient.ssl),
  1048. wolfSSL_get_peer_quic_transport_version(tserver.ssl));
  1049. QuicTestContext_free(&tclient);
  1050. QuicTestContext_free(&tserver);
  1051. wolfSSL_CTX_free(ctx_c);
  1052. wolfSSL_CTX_free(ctx_s);
  1053. printf(" test_quic_server_hello: %s\n", (ret == 0)? passed : failed);
  1054. return ret;
  1055. }
  1056. /* This has gotten a bit out of hand. */
  1057. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  1058. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  1059. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  1060. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) \
  1061. && defined(HAVE_ALPN) && defined(HAVE_SNI)
  1062. #define REALLY_HAVE_ALPN_AND_SNI
  1063. #else
  1064. #undef REALLY_HAVE_ALPN_AND_SNI
  1065. #endif
  1066. #ifdef REALLY_HAVE_ALPN_AND_SNI
  1067. struct stripe_buffer {
  1068. char stripe[256];
  1069. };
  1070. static int inspect_SNI(WOLFSSL *ssl, int *ad, void *baton)
  1071. {
  1072. struct stripe_buffer *stripe = (struct stripe_buffer *)baton;
  1073. (void)ssl;
  1074. *ad = 0;
  1075. XSTRLCAT(stripe->stripe, "S", sizeof(stripe->stripe));
  1076. return 0;
  1077. }
  1078. static int select_ALPN(WOLFSSL *ssl,
  1079. const unsigned char **out,
  1080. unsigned char *outlen,
  1081. const unsigned char *in,
  1082. unsigned int inlen,
  1083. void *baton)
  1084. {
  1085. struct stripe_buffer *stripe = (struct stripe_buffer *)baton;
  1086. (void)ssl;
  1087. (void)inlen;
  1088. /* just select the first */
  1089. *out = in + 1;
  1090. *outlen = in[0];
  1091. XSTRLCAT(stripe->stripe, "A", sizeof(stripe->stripe));
  1092. return 0;
  1093. }
  1094. static int test_quic_alpn(int verbose) {
  1095. WOLFSSL_CTX *ctx_c, *ctx_s;
  1096. int ret = 0;
  1097. QuicTestContext tclient, tserver;
  1098. QuicConversation conv;
  1099. struct stripe_buffer stripe;
  1100. unsigned char alpn_protos[256];
  1101. AssertNotNull(ctx_c = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  1102. AssertNotNull(ctx_s = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  1103. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1104. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1105. stripe.stripe[0] = '\0';
  1106. wolfSSL_CTX_set_servername_callback(ctx_s, inspect_SNI);
  1107. wolfSSL_CTX_set_servername_arg(ctx_s, &stripe);
  1108. wolfSSL_CTX_set_alpn_select_cb(ctx_s, select_ALPN, &stripe);
  1109. /* setup ssls */
  1110. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1111. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1112. /* set SNI and ALPN callbacks on server side,
  1113. * provide values on client side */
  1114. wolfSSL_UseSNI(tclient.ssl, WOLFSSL_SNI_HOST_NAME,
  1115. "wolfssl.com", sizeof("wolfssl.com")-1);
  1116. /* connect */
  1117. QuicConversation_init(&conv, &tclient, &tserver);
  1118. XSTRLCPY((char*)(alpn_protos + 1), "test", sizeof(alpn_protos));
  1119. alpn_protos[0] = strlen("test");
  1120. wolfSSL_set_alpn_protos(tclient.ssl, alpn_protos, 1 + strlen("test"));
  1121. QuicConversation_do(&conv);
  1122. AssertIntEQ(tclient.output.len, 0);
  1123. AssertIntEQ(tserver.output.len, 0);
  1124. /* SNI callback needs to be called before ALPN callback */
  1125. AssertStrEQ(stripe.stripe, "SA");
  1126. QuicTestContext_free(&tclient);
  1127. QuicTestContext_free(&tserver);
  1128. wolfSSL_CTX_free(ctx_c);
  1129. wolfSSL_CTX_free(ctx_s);
  1130. printf(" test_quic_alpn: %s\n", (ret == 0)? passed : failed);
  1131. return ret;
  1132. }
  1133. #endif /* REALLY_HAVE_ALPN_AND_SNI */
  1134. #ifdef HAVE_SESSION_TICKET
  1135. static int test_quic_key_share(int verbose) {
  1136. WOLFSSL_CTX *ctx_c, *ctx_s;
  1137. int ret = 0;
  1138. QuicTestContext tclient, tserver;
  1139. QuicConversation conv;
  1140. AssertNotNull(ctx_c = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  1141. AssertNotNull(ctx_s = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  1142. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1143. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1144. /* setup & handshake defaults */
  1145. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1146. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1147. QuicConversation_init(&conv, &tclient, &tserver);
  1148. QuicConversation_do(&conv);
  1149. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:"
  1150. "Certificate:CertificateVerify:Finished:Finished:SessionTicket");
  1151. QuicTestContext_free(&tclient);
  1152. QuicTestContext_free(&tserver);
  1153. /* setup & handshake, restricted groups. KEY_SHARE should use
  1154. * the first configured group. */
  1155. /*If that is supported by the server, expect a smooth handshake.*/
  1156. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1157. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1158. AssertTrue(wolfSSL_set1_curves_list(tclient.ssl, "X25519:P-256")
  1159. == WOLFSSL_SUCCESS);
  1160. AssertTrue(wolfSSL_set1_curves_list(tserver.ssl, "X25519")
  1161. == WOLFSSL_SUCCESS);
  1162. QuicConversation_init(&conv, &tclient, &tserver);
  1163. QuicConversation_do(&conv);
  1164. AssertStrEQ(conv.rec_log,
  1165. "ClientHello:ServerHello:EncryptedExtension:"
  1166. "Certificate:CertificateVerify:Finished:Finished:SessionTicket");
  1167. QuicTestContext_free(&tclient);
  1168. QuicTestContext_free(&tserver);
  1169. printf(" test_quic_key_share: priority ok\n");
  1170. /* If group is not supported by server, expect HelloRetry */
  1171. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1172. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1173. AssertTrue(wolfSSL_set1_curves_list(tclient.ssl, "X25519:P-256")
  1174. == WOLFSSL_SUCCESS);
  1175. AssertTrue(wolfSSL_set1_curves_list(tserver.ssl, "P-256")
  1176. == WOLFSSL_SUCCESS);
  1177. QuicConversation_init(&conv, &tclient, &tserver);
  1178. QuicConversation_do(&conv);
  1179. AssertStrEQ(conv.rec_log,
  1180. "ClientHello:ServerHello:ClientHello:ServerHello:EncryptedExtension:"
  1181. "Certificate:CertificateVerify:Finished:Finished:SessionTicket");
  1182. QuicTestContext_free(&tclient);
  1183. QuicTestContext_free(&tserver);
  1184. printf(" test_quic_key_share: retry ok\n");
  1185. /* If no group overlap, expect failure */
  1186. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1187. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1188. AssertTrue(wolfSSL_set1_curves_list(tclient.ssl, "P-256")
  1189. == WOLFSSL_SUCCESS);
  1190. AssertTrue(wolfSSL_set1_curves_list(tserver.ssl, "X25519")
  1191. == WOLFSSL_SUCCESS);
  1192. QuicConversation_init(&conv, &tclient, &tserver);
  1193. QuicConversation_fail(&conv);
  1194. AssertIntEQ(wolfSSL_get_error(tserver.ssl, 0), SSL_ERROR_WANT_READ);
  1195. AssertIntEQ(wolfSSL_get_error(tclient.ssl, 0), BAD_KEY_SHARE_DATA);
  1196. QuicTestContext_free(&tclient);
  1197. QuicTestContext_free(&tserver);
  1198. printf(" test_quic_key_share: no match ok\n");
  1199. wolfSSL_CTX_free(ctx_c);
  1200. wolfSSL_CTX_free(ctx_s);
  1201. printf(" test_quic_key_share: %s\n", (ret == 0)? passed : failed);
  1202. return ret;
  1203. }
  1204. static int test_quic_resumption(int verbose) {
  1205. WOLFSSL_CTX *ctx_c, *ctx_s;
  1206. WOLFSSL_SESSION *session, *session_restored;
  1207. int ret = 0;
  1208. QuicTestContext tclient, tserver;
  1209. QuicConversation conv;
  1210. unsigned char session_buffer[16 * 1024], *session_data;
  1211. const unsigned char *session_data2;
  1212. unsigned int session_size;
  1213. AssertNotNull(ctx_c = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  1214. AssertNotNull(ctx_s = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  1215. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1216. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1217. /* setup ssls */
  1218. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1219. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1220. QuicConversation_init(&conv, &tclient, &tserver);
  1221. /* run till end */
  1222. QuicConversation_do(&conv);
  1223. /* what have we seen? */
  1224. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:"
  1225. "Certificate:CertificateVerify:Finished:Finished:SessionTicket");
  1226. /* Should have received a session ticket, save the session
  1227. * and also make a serialized/deserialized copy to check that persisting
  1228. * a session works. */
  1229. AssertTrue(tclient.ticket_len > 0);
  1230. AssertNotNull(session = wolfSSL_get1_session(tclient.ssl));
  1231. AssertTrue((session_size = wolfSSL_i2d_SSL_SESSION(session, NULL)) > 0);
  1232. AssertTrue((size_t)session_size < sizeof(session_buffer));
  1233. session_data2 = session_data = session_buffer;
  1234. session_size = wolfSSL_i2d_SSL_SESSION(session, &session_data);
  1235. session_restored = wolfSSL_d2i_SSL_SESSION(NULL, &session_data2, session_size);
  1236. AssertNotNull(session_restored);
  1237. QuicTestContext_free(&tserver);
  1238. QuicTestContext_free(&tclient);
  1239. /* Do a Session resumption with the session object */
  1240. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1241. QuicTestContext_init(&tclient, ctx_c, "client_resume", verbose);
  1242. AssertIntEQ(wolfSSL_set_session(tclient.ssl, session), WOLFSSL_SUCCESS);
  1243. /* let them talk */
  1244. QuicConversation_init(&conv, &tclient, &tserver);
  1245. QuicConversation_do(&conv);
  1246. /* this is what should happen. Look Ma, no certificate! */
  1247. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:"
  1248. "Finished:Finished:SessionTicket");
  1249. QuicTestContext_free(&tclient);
  1250. QuicTestContext_free(&tserver);
  1251. /* Do a Session resumption with the restored session object */
  1252. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1253. QuicTestContext_init(&tclient, ctx_c, "client_resume_restored", verbose);
  1254. AssertIntEQ(wolfSSL_set_session(tclient.ssl, session_restored), WOLFSSL_SUCCESS);
  1255. /* let them talk */
  1256. QuicConversation_init(&conv, &tclient, &tserver);
  1257. QuicConversation_do(&conv);
  1258. /* this is what should happen. Look Ma, no certificate! */
  1259. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:"
  1260. "Finished:Finished:SessionTicket");
  1261. QuicTestContext_free(&tclient);
  1262. QuicTestContext_free(&tserver);
  1263. {
  1264. /* Do a Session resumption with a new server ctx */
  1265. WOLFSSL_CTX *ctx_s2;
  1266. AssertNotNull(ctx_s2 = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  1267. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx_s2, eccCertFile, WOLFSSL_FILETYPE_PEM));
  1268. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx_s2, eccKeyFile, WOLFSSL_FILETYPE_PEM));
  1269. QuicTestContext_init(&tserver, ctx_s2, "server2", verbose);
  1270. QuicTestContext_init(&tclient, ctx_c, "client_resume2", verbose);
  1271. AssertIntEQ(wolfSSL_set_session(tclient.ssl, session_restored), WOLFSSL_SUCCESS);
  1272. /* let them talk */
  1273. QuicConversation_init(&conv, &tclient, &tserver);
  1274. QuicConversation_do(&conv);
  1275. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:"
  1276. "Certificate:CertificateVerify:Finished:Finished:SessionTicket");
  1277. QuicTestContext_free(&tclient);
  1278. QuicTestContext_free(&tserver);
  1279. wolfSSL_CTX_free(ctx_s2);
  1280. }
  1281. wolfSSL_SESSION_free(session);
  1282. wolfSSL_SESSION_free(session_restored);
  1283. wolfSSL_CTX_free(ctx_c);
  1284. wolfSSL_CTX_free(ctx_s);
  1285. printf(" test_quic_resumption: %s\n", (ret == 0)? passed : failed);
  1286. return ret;
  1287. }
  1288. #ifdef WOLFSSL_EARLY_DATA
  1289. static int test_quic_early_data(int verbose) {
  1290. WOLFSSL_CTX *ctx_c, *ctx_s;
  1291. int ret = 0;
  1292. QuicTestContext tclient, tserver;
  1293. QuicConversation conv;
  1294. const byte early_data[] = "Nulla dies sine linea!";
  1295. size_t ed_written;
  1296. WOLFSSL_SESSION *session;
  1297. unsigned int max_early_sz;
  1298. AssertNotNull(ctx_c = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  1299. wolfSSL_CTX_UseSessionTicket(ctx_c);
  1300. AssertNotNull(ctx_s = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  1301. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1302. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1303. /* setup ssls */
  1304. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1305. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1306. wolfSSL_set_quic_early_data_enabled(tserver.ssl, 1);
  1307. /* QUIC only allows 0xffffffff or 0x0 as values */
  1308. AssertIntEQ(wolfSSL_get_max_early_data(tserver.ssl), UINT32_MAX);
  1309. QuicConversation_init(&conv, &tclient, &tserver);
  1310. /* run till end */
  1311. QuicConversation_do(&conv);
  1312. /* what have we seen? */
  1313. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:"
  1314. "Certificate:CertificateVerify:Finished:Finished:SessionTicket");
  1315. /* Should have received a session ticket, save the session */
  1316. AssertTrue(tclient.ticket_len > 0);
  1317. AssertNotNull(session = wolfSSL_get1_session(tclient.ssl));
  1318. QuicTestContext_free(&tclient);
  1319. QuicTestContext_free(&tserver);
  1320. /* QUIC requires 0 or 0xffffffff as only allowed values.
  1321. * Since we enabled early data in the server that created the session,
  1322. * we need to see it here. */
  1323. max_early_sz = wolfSSL_SESSION_get_max_early_data(session);
  1324. AssertIntEQ(max_early_sz, UINT32_MAX);
  1325. /* Do a Session resumption with the ticket */
  1326. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1327. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1328. AssertIntEQ(wolfSSL_set_session(tclient.ssl, session), WOLFSSL_SUCCESS);
  1329. /* enable early data -*/
  1330. wolfSSL_set_quic_early_data_enabled(tserver.ssl, 1);
  1331. /* client will send, and server will receive implicitly */
  1332. QuicConversation_init(&conv, &tclient, &tserver);
  1333. QuicConversation_start(&conv, early_data, sizeof(early_data), &ed_written);
  1334. QuicConversation_do(&conv);
  1335. AssertIntEQ(wolfSSL_get_early_data_status(tclient.ssl), WOLFSSL_EARLY_DATA_ACCEPTED);
  1336. QuicTestContext_free(&tclient);
  1337. QuicTestContext_free(&tserver);
  1338. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1339. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1340. AssertIntEQ(wolfSSL_set_session(tclient.ssl, session), WOLFSSL_SUCCESS);
  1341. /* client will send, and server will receive */
  1342. QuicConversation_init(&conv, &tclient, &tserver);
  1343. /* make QuicConversation_do() use wolfSSL_read_early_data() */
  1344. conv.accept_early_data = 1;
  1345. QuicConversation_start(&conv, early_data, sizeof(early_data), &ed_written);
  1346. QuicConversation_do(&conv);
  1347. AssertIntEQ(wolfSSL_get_early_data_status(tclient.ssl), WOLFSSL_EARLY_DATA_ACCEPTED);
  1348. AssertIntEQ(conv.early_data_len, sizeof(early_data));
  1349. AssertStrEQ(conv.early_data, (const char*)early_data);
  1350. QuicTestContext_free(&tclient);
  1351. QuicTestContext_free(&tserver);
  1352. wolfSSL_SESSION_free(session);
  1353. wolfSSL_CTX_free(ctx_c);
  1354. wolfSSL_CTX_free(ctx_s);
  1355. printf(" test_quic_early_data: %s\n", (ret == 0)? passed : failed);
  1356. return ret;
  1357. }
  1358. #endif /* WOLFSSL_EARLY_DATA */
  1359. static int new_session_cb(WOLFSSL *ssl, WOLFSSL_SESSION *session)
  1360. {
  1361. QuicTestContext *ctx = (QuicTestContext*)wolfSSL_get_app_data(ssl);
  1362. byte *data;
  1363. int ret = 0;
  1364. int sz;
  1365. sz = wolfSSL_i2d_SSL_SESSION(session, NULL);
  1366. if (sz <= 0) {
  1367. printf("[%s] session serialization error: %d <- ", ctx->name, sz);
  1368. return sz;
  1369. }
  1370. if ((size_t)sz > sizeof(ctx->session)) {
  1371. printf("[%s] session serialization too large: %d <- ", ctx->name, sz);
  1372. return -1;
  1373. }
  1374. data = ctx->session;
  1375. ctx->session_len = wolfSSL_i2d_SSL_SESSION(session, &data);
  1376. if (ctx->verbose) {
  1377. printf("[%s]", ctx->name);
  1378. dump_buffer(" new SESSION", ctx->session, ctx->session_len, 4);
  1379. }
  1380. return ret;
  1381. }
  1382. static int test_quic_session_export(int verbose)
  1383. {
  1384. WOLFSSL_CTX *ctx_c, *ctx_s;
  1385. WOLFSSL_SESSION *session = NULL;
  1386. int ret = 0;
  1387. QuicTestContext tclient, tserver;
  1388. QuicConversation conv;
  1389. byte session_data[16*1024];
  1390. const byte *bp;
  1391. word32 session_len;
  1392. AssertNotNull(ctx_c = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  1393. AssertNotNull(ctx_s = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  1394. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1395. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1396. /* Uses CTX session callback for new sessions */
  1397. wolfSSL_CTX_sess_set_new_cb(ctx_c, new_session_cb);
  1398. /* setup ssls */
  1399. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1400. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1401. QuicConversation_init(&conv, &tclient, &tserver);
  1402. /* run till end */
  1403. QuicConversation_do(&conv);
  1404. /* what have we seen? */
  1405. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:Certificate:CertificateVerify:Finished:Finished:SessionTicket");
  1406. /* Should have received a session, save it */
  1407. AssertTrue(tclient.session_len > 0);
  1408. memcpy(session_data, tclient.session, tclient.session_len);
  1409. session_len = tclient.session_len;
  1410. if (verbose)
  1411. dump_buffer("copied SESSION", session_data, session_len, 0);
  1412. QuicTestContext_free(&tserver);
  1413. QuicTestContext_free(&tclient);
  1414. /* Do a Session resumption with the ticket */
  1415. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1416. QuicTestContext_init(&tclient, ctx_c, "client_resume", verbose);
  1417. bp = session_data;
  1418. AssertNotNull(session = wolfSSL_d2i_SSL_SESSION(NULL, &bp, session_len));
  1419. AssertIntEQ(wolfSSL_set_session(tclient.ssl, session), WOLFSSL_SUCCESS);
  1420. wolfSSL_SESSION_free(session);
  1421. /* let them talk */
  1422. QuicConversation_init(&conv, &tclient, &tserver);
  1423. QuicConversation_do(&conv);
  1424. /* this is what should happen. Look Ma, no certificate! */
  1425. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:Finished:Finished:SessionTicket");
  1426. QuicTestContext_free(&tclient);
  1427. QuicTestContext_free(&tserver);
  1428. wolfSSL_CTX_free(ctx_c);
  1429. wolfSSL_CTX_free(ctx_s);
  1430. printf(" test_quic_session_export: %s\n", (ret == 0)? passed : failed);
  1431. return ret;
  1432. }
  1433. #endif /* WOLFSSL_SESSION_EXPORT */
  1434. #endif /* WOLFSSL_QUIC */
  1435. int QuicTest(void)
  1436. {
  1437. int ret = 0;
  1438. #ifdef WOLFSSL_QUIC
  1439. int verbose = 0;
  1440. printf(" Begin QUIC Tests\n");
  1441. if ((ret = test_set_quic_method()) != 0) goto leave;
  1442. if ((ret = test_provide_quic_data()) != 0) goto leave;
  1443. if ((ret = test_quic_crypt()) != 0) goto leave;
  1444. if ((ret = test_quic_client_hello(verbose)) != 0) goto leave;
  1445. if ((ret = test_quic_server_hello(verbose)) != 0) goto leave;
  1446. #ifdef REALLY_HAVE_ALPN_AND_SNI
  1447. if ((ret = test_quic_alpn(verbose)) != 0) goto leave;
  1448. #endif /* REALLY_HAVE_ALPN_AND_SNI */
  1449. #ifdef HAVE_SESSION_TICKET
  1450. if ((ret = test_quic_key_share(verbose)) != 0) goto leave;
  1451. if ((ret = test_quic_resumption(verbose)) != 0) goto leave;
  1452. #ifdef WOLFSSL_EARLY_DATA
  1453. if ((ret = test_quic_early_data(verbose)) != 0) goto leave;
  1454. #endif /* WOLFSSL_EARLY_DATA */
  1455. if ((ret = test_quic_session_export(verbose)) != 0) goto leave;
  1456. #endif /* HAVE_SESSION_TICKET */
  1457. leave:
  1458. if (ret != 0)
  1459. printf(" FAILED: some tests did not pass.\n");
  1460. printf(" End QUIC Tests\n");
  1461. #endif
  1462. return ret;
  1463. }