quic.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. /* quic.c QUIC unit tests
  2. *
  3. * Copyright (C) 2006-2022 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 / 0x10));
  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)
  857. {
  858. int n;
  859. if (!conv->started) {
  860. AssertTrue(wolfSSL_connect(conv->client->ssl) != WOLFSSL_SUCCESS);
  861. AssertIntEQ(SSL_ERROR_WANT_READ, wolfSSL_get_error(conv->client->ssl, 0));
  862. conv->started = 1;
  863. }
  864. if (conv->server->output.len > 0) {
  865. QuicTestContext_forward(conv->server, conv->client, conv->rec_log, sizeof(conv->rec_log));
  866. n = wolfSSL_quic_read_write(conv->client->ssl);
  867. if (n != WOLFSSL_SUCCESS) {
  868. AssertIntEQ(wolfSSL_get_error(conv->client->ssl, 0), SSL_ERROR_WANT_READ);
  869. }
  870. return 1;
  871. }
  872. else if (conv->client->output.len > 0) {
  873. QuicTestContext_forward(conv->client, conv->server, conv->rec_log, sizeof(conv->rec_log));
  874. #ifdef WOLFSSL_EARLY_DATA
  875. if (conv->accept_early_data) {
  876. int written;
  877. n = wolfSSL_read_early_data(conv->server->ssl,
  878. conv->early_data + conv->early_data_len,
  879. (int)(sizeof(conv->early_data) - conv->early_data_len),
  880. &written);
  881. if (n < 0) {
  882. AssertIntEQ(wolfSSL_get_error(conv->server->ssl, 0), SSL_ERROR_WANT_READ);
  883. }
  884. else if (n > 0) {
  885. conv->early_data_len += n;
  886. if (conv->verbose)
  887. printf("RECVed early data, len now=%d\n", (int)conv->early_data_len);
  888. }
  889. }
  890. else
  891. #endif /* WOLFSSL_EARLY_DATA */
  892. {
  893. n = wolfSSL_quic_read_write(conv->server->ssl);
  894. if (n != WOLFSSL_SUCCESS) {
  895. AssertIntEQ(wolfSSL_get_error(conv->server->ssl, 0), SSL_ERROR_WANT_READ);
  896. }
  897. }
  898. return 1;
  899. }
  900. return 0;
  901. }
  902. static void QuicConversation_do(QuicConversation *conv)
  903. {
  904. if (!conv->started) {
  905. QuicConversation_start(conv, NULL, 0, NULL);
  906. }
  907. while (1) {
  908. if (!QuicConversation_step(conv)) {
  909. int c_err = wolfSSL_get_error(conv->client->ssl, 0);
  910. int s_err = wolfSSL_get_error(conv->server->ssl, 0);
  911. if (c_err == 0 && s_err == 0) {
  912. break; /* handshake done */
  913. }
  914. printf("Neither tclient nor server have anything to send, "
  915. "but client_error=%d, server_error=%d\n",
  916. c_err, s_err);
  917. AssertFalse(1);
  918. }
  919. }
  920. }
  921. static int test_quic_client_hello(int verbose) {
  922. WOLFSSL_CTX *ctx;
  923. int ret = 0;
  924. QuicTestContext tctx;
  925. (void)ctx_dump_output;
  926. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  927. QuicTestContext_init(&tctx, ctx, "client", verbose);
  928. /* Without any QUIC transport params, this needs to fail */
  929. AssertTrue(wolfSSL_set_quic_transport_params(tctx.ssl, NULL, 0) == WOLFSSL_SUCCESS);
  930. AssertTrue(wolfSSL_quic_read_write(tctx.ssl) != 0);
  931. AssertIntEQ(wolfSSL_get_error(tctx.ssl, 0), QUIC_TP_MISSING_E);
  932. QuicTestContext_free(&tctx);
  933. /* Set transport params, expect both extensions */
  934. QuicTestContext_init(&tctx, ctx, "client", verbose);
  935. #ifdef HAVE_SNI
  936. wolfSSL_UseSNI(tctx.ssl, WOLFSSL_SNI_HOST_NAME, "wolfssl.com", sizeof("wolfssl.com")-1);
  937. #endif
  938. AssertTrue(wolfSSL_connect(tctx.ssl) != 0);
  939. AssertIntEQ(wolfSSL_get_error(tctx.ssl, 0), SSL_ERROR_WANT_READ);
  940. check_quic_client_hello_tp(&tctx.output, 1, 1);
  941. QuicTestContext_free(&tctx);
  942. /* Set transport params v1, expect v1 extension */
  943. QuicTestContext_init(&tctx, ctx, "client", verbose);
  944. wolfSSL_set_quic_transport_version(tctx.ssl, TLSX_KEY_QUIC_TP_PARAMS);
  945. AssertTrue(wolfSSL_connect(tctx.ssl) != 0);
  946. check_quic_client_hello_tp(&tctx.output, 1, 0);
  947. QuicTestContext_free(&tctx);
  948. /* Set transport params draft, expect draft extension */
  949. QuicTestContext_init(&tctx, ctx, "client", verbose);
  950. wolfSSL_set_quic_transport_version(tctx.ssl, TLSX_KEY_QUIC_TP_PARAMS_DRAFT);
  951. AssertTrue(wolfSSL_connect(tctx.ssl) != 0);
  952. check_quic_client_hello_tp(&tctx.output, 0, 1);
  953. QuicTestContext_free(&tctx);
  954. /* Set transport params 0, expect both extension */
  955. QuicTestContext_init(&tctx, ctx, "client", verbose);
  956. wolfSSL_set_quic_transport_version(tctx.ssl, 0);
  957. AssertTrue(wolfSSL_connect(tctx.ssl) != 0);
  958. check_quic_client_hello_tp(&tctx.output, 1, 1);
  959. QuicTestContext_free(&tctx);
  960. wolfSSL_CTX_free(ctx);
  961. printf(" test_quic_client_hello: %s\n", (ret == 0)? passed : failed);
  962. return ret;
  963. }
  964. static int test_quic_server_hello(int verbose) {
  965. WOLFSSL_CTX *ctx_c, *ctx_s;
  966. int ret = 0;
  967. QuicTestContext tclient, tserver;
  968. QuicConversation conv;
  969. AssertNotNull(ctx_c = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  970. AssertNotNull(ctx_s = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  971. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile, WOLFSSL_FILETYPE_PEM));
  972. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  973. /* setup ssls */
  974. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  975. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  976. /* connect */
  977. QuicConversation_init(&conv, &tclient, &tserver);
  978. QuicConversation_step(&conv);
  979. /* check established/missing secrets */
  980. check_secrets(&tserver, wolfssl_encryption_initial, 0, 0);
  981. check_secrets(&tserver, wolfssl_encryption_handshake, 32, 32);
  982. check_secrets(&tserver, wolfssl_encryption_application, 32, 32);
  983. check_secrets(&tclient, wolfssl_encryption_handshake, 0, 0);
  984. /* feed the server data to the client */
  985. QuicConversation_step(&conv);
  986. /* client has generated handshake secret */
  987. check_secrets(&tclient, wolfssl_encryption_handshake, 32, 32);
  988. /* continue the handshake till done */
  989. conv.started = 1;
  990. /* run till end */
  991. QuicConversation_do(&conv);
  992. AssertIntEQ(tclient.output.len, 0);
  993. AssertIntEQ(tserver.output.len, 0);
  994. /* what have we seen? */
  995. #ifdef HAVE_SESSION_TICKET
  996. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:Certificate:CertificateVerify:Finished:Finished:SessionTicket");
  997. #else
  998. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:Certificate:CertificateVerify:Finished:Finished");
  999. #endif
  1000. /* we are at application encryption level */
  1001. AssertTrue(wolfSSL_quic_read_level(tclient.ssl) == wolfssl_encryption_application);
  1002. AssertTrue(wolfSSL_quic_write_level(tclient.ssl) == wolfssl_encryption_application);
  1003. AssertTrue(wolfSSL_quic_read_level(tserver.ssl) == wolfssl_encryption_application);
  1004. AssertTrue(wolfSSL_quic_write_level(tserver.ssl) == wolfssl_encryption_application);
  1005. /* the last client write (FINISHED) was at handshake level */
  1006. AssertTrue(tclient.output.level == wolfssl_encryption_handshake);
  1007. /* we have the app secrets */
  1008. check_secrets(&tclient, wolfssl_encryption_application, 32, 32);
  1009. check_secrets(&tserver, wolfssl_encryption_application, 32, 32);
  1010. /* verify client and server have the same secrets establishd */
  1011. assert_secrets_EQ(&tclient, &tserver, wolfssl_encryption_handshake);
  1012. assert_secrets_EQ(&tclient, &tserver, wolfssl_encryption_application);
  1013. /* AEAD cipher should be known */
  1014. AssertNotNull(wolfSSL_quic_get_aead(tclient.ssl));
  1015. AssertNotNull(wolfSSL_quic_get_aead(tserver.ssl));
  1016. /* What was negiotiated and is it the same? */
  1017. AssertIntEQ(wolfSSL_get_peer_quic_transport_version(tclient.ssl),
  1018. wolfSSL_get_peer_quic_transport_version(tserver.ssl));
  1019. QuicTestContext_free(&tclient);
  1020. QuicTestContext_free(&tserver);
  1021. wolfSSL_CTX_free(ctx_c);
  1022. wolfSSL_CTX_free(ctx_s);
  1023. printf(" test_quic_server_hello: %s\n", (ret == 0)? passed : failed);
  1024. return ret;
  1025. }
  1026. #ifdef HAVE_SESSION_TICKET
  1027. static int test_quic_resumption(int verbose) {
  1028. WOLFSSL_CTX *ctx_c, *ctx_s;
  1029. WOLFSSL_SESSION *session;
  1030. int ret = 0;
  1031. QuicTestContext tclient, tserver;
  1032. QuicConversation conv;
  1033. AssertNotNull(ctx_c = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  1034. AssertNotNull(ctx_s = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  1035. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1036. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1037. /* setup ssls */
  1038. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1039. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1040. QuicConversation_init(&conv, &tclient, &tserver);
  1041. /* run till end */
  1042. QuicConversation_do(&conv);
  1043. /* what have we seen? */
  1044. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:Certificate:CertificateVerify:Finished:Finished:SessionTicket");
  1045. /* Should have received a session ticket, save the session */
  1046. AssertTrue(tclient.ticket_len > 0);
  1047. AssertNotNull(session = wolfSSL_get1_session(tclient.ssl));
  1048. QuicTestContext_free(&tserver);
  1049. QuicTestContext_free(&tclient);
  1050. /* Do a Session resumption with the ticket */
  1051. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1052. QuicTestContext_init(&tclient, ctx_c, "client_resume", verbose);
  1053. AssertIntEQ(wolfSSL_set_session(tclient.ssl, session), WOLFSSL_SUCCESS);
  1054. /* let them talk */
  1055. QuicConversation_init(&conv, &tclient, &tserver);
  1056. QuicConversation_do(&conv);
  1057. /* this is what should happen. Look Ma, no certificate! */
  1058. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:Finished:Finished:SessionTicket");
  1059. QuicTestContext_free(&tclient);
  1060. QuicTestContext_free(&tserver);
  1061. wolfSSL_SESSION_free(session);
  1062. wolfSSL_CTX_free(ctx_c);
  1063. wolfSSL_CTX_free(ctx_s);
  1064. printf(" test_quic_resumption: %s\n", (ret == 0)? passed : failed);
  1065. return ret;
  1066. }
  1067. #ifdef WOLFSSL_EARLY_DATA
  1068. static int test_quic_early_data(int verbose) {
  1069. WOLFSSL_CTX *ctx_c, *ctx_s;
  1070. int ret = 0;
  1071. QuicTestContext tclient, tserver;
  1072. QuicConversation conv;
  1073. const byte early_data[] = "Nulla dies sine linea!";
  1074. size_t ed_written;
  1075. WOLFSSL_SESSION *session;
  1076. unsigned int max_early_sz;
  1077. AssertNotNull(ctx_c = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  1078. wolfSSL_CTX_UseSessionTicket(ctx_c);
  1079. AssertNotNull(ctx_s = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  1080. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1081. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1082. /* setup ssls */
  1083. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1084. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1085. wolfSSL_set_quic_early_data_enabled(tserver.ssl, 1);
  1086. /* QUIC only allows 0xffffffff or 0x0 as values */
  1087. AssertIntEQ(wolfSSL_get_max_early_data(tserver.ssl), UINT32_MAX);
  1088. QuicConversation_init(&conv, &tclient, &tserver);
  1089. /* run till end */
  1090. QuicConversation_do(&conv);
  1091. /* what have we seen? */
  1092. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:Certificate:CertificateVerify:Finished:Finished:SessionTicket");
  1093. /* Should have received a session ticket, save the session */
  1094. AssertTrue(tclient.ticket_len > 0);
  1095. AssertNotNull(session = wolfSSL_get1_session(tclient.ssl));
  1096. QuicTestContext_free(&tclient);
  1097. QuicTestContext_free(&tserver);
  1098. /* QUIC requires 0 or 0xffffffff as only allowed values.
  1099. * Since we enabled early data in the server that created the session,
  1100. * we need to see it here. */
  1101. max_early_sz = wolfSSL_SESSION_get_max_early_data(session);
  1102. AssertIntEQ(max_early_sz, UINT32_MAX);
  1103. /* Do a Session resumption with the ticket */
  1104. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1105. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1106. AssertIntEQ(wolfSSL_set_session(tclient.ssl, session), WOLFSSL_SUCCESS);
  1107. /* enable early data -*/
  1108. wolfSSL_set_quic_early_data_enabled(tserver.ssl, 1);
  1109. /* client will send, and server will receive implicitly */
  1110. QuicConversation_init(&conv, &tclient, &tserver);
  1111. QuicConversation_start(&conv, early_data, sizeof(early_data), &ed_written);
  1112. QuicConversation_do(&conv);
  1113. AssertIntEQ(wolfSSL_get_early_data_status(tclient.ssl), WOLFSSL_EARLY_DATA_ACCEPTED);
  1114. QuicTestContext_free(&tclient);
  1115. QuicTestContext_free(&tserver);
  1116. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1117. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1118. AssertIntEQ(wolfSSL_set_session(tclient.ssl, session), WOLFSSL_SUCCESS);
  1119. /* client will send, and server will receive */
  1120. QuicConversation_init(&conv, &tclient, &tserver);
  1121. /* make QuicConversation_do() use wolfSSL_read_early_data() */
  1122. conv.accept_early_data = 1;
  1123. QuicConversation_start(&conv, early_data, sizeof(early_data), &ed_written);
  1124. QuicConversation_do(&conv);
  1125. AssertIntEQ(wolfSSL_get_early_data_status(tclient.ssl), WOLFSSL_EARLY_DATA_ACCEPTED);
  1126. AssertIntEQ(conv.early_data_len, sizeof(early_data));
  1127. AssertStrEQ(conv.early_data, (const char*)early_data);
  1128. QuicTestContext_free(&tclient);
  1129. QuicTestContext_free(&tserver);
  1130. wolfSSL_SESSION_free(session);
  1131. wolfSSL_CTX_free(ctx_c);
  1132. wolfSSL_CTX_free(ctx_s);
  1133. printf(" test_quic_early_data: %s\n", (ret == 0)? passed : failed);
  1134. return ret;
  1135. }
  1136. #endif /* WOLFSSL_EARLY_DATA */
  1137. static int new_session_cb(WOLFSSL *ssl, WOLFSSL_SESSION *session)
  1138. {
  1139. QuicTestContext *ctx = (QuicTestContext*)wolfSSL_get_app_data(ssl);
  1140. byte *data;
  1141. int ret = 0;
  1142. int sz;
  1143. sz = wolfSSL_i2d_SSL_SESSION(session, NULL);
  1144. if (sz <= 0) {
  1145. printf("[%s] session serialization error: %d <- ", ctx->name, sz);
  1146. return sz;
  1147. }
  1148. if ((size_t)sz > sizeof(ctx->session)) {
  1149. printf("[%s] session serialization too large: %d <- ", ctx->name, sz);
  1150. return -1;
  1151. }
  1152. data = ctx->session;
  1153. ctx->session_len = wolfSSL_i2d_SSL_SESSION(session, &data);
  1154. if (ctx->verbose) {
  1155. printf("[%s]", ctx->name);
  1156. dump_buffer(" new SESSION", ctx->session, ctx->session_len, 4);
  1157. }
  1158. return ret;
  1159. }
  1160. static int test_quic_session_export(int verbose)
  1161. {
  1162. WOLFSSL_CTX *ctx_c, *ctx_s;
  1163. WOLFSSL_SESSION *session = NULL;
  1164. int ret = 0;
  1165. QuicTestContext tclient, tserver;
  1166. QuicConversation conv;
  1167. byte session_data[16*1024];
  1168. const byte *bp;
  1169. word32 session_len;
  1170. AssertNotNull(ctx_c = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  1171. AssertNotNull(ctx_s = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  1172. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile, WOLFSSL_FILETYPE_PEM));
  1173. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1174. /* Uses CTX session callback for new sessions */
  1175. wolfSSL_CTX_sess_set_new_cb(ctx_c, new_session_cb);
  1176. /* setup ssls */
  1177. QuicTestContext_init(&tclient, ctx_c, "client", verbose);
  1178. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1179. QuicConversation_init(&conv, &tclient, &tserver);
  1180. /* run till end */
  1181. QuicConversation_do(&conv);
  1182. /* what have we seen? */
  1183. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:Certificate:CertificateVerify:Finished:Finished:SessionTicket");
  1184. /* Should have received a session, save it */
  1185. AssertTrue(tclient.session_len > 0);
  1186. memcpy(session_data, tclient.session, tclient.session_len);
  1187. session_len = tclient.session_len;
  1188. if (verbose)
  1189. dump_buffer("copied SESSION", session_data, session_len, 0);
  1190. QuicTestContext_free(&tserver);
  1191. QuicTestContext_free(&tclient);
  1192. /* Do a Session resumption with the ticket */
  1193. QuicTestContext_init(&tserver, ctx_s, "server", verbose);
  1194. QuicTestContext_init(&tclient, ctx_c, "client_resume", verbose);
  1195. bp = session_data;
  1196. AssertNotNull(session = wolfSSL_d2i_SSL_SESSION(NULL, &bp, session_len));
  1197. AssertIntEQ(wolfSSL_set_session(tclient.ssl, session), WOLFSSL_SUCCESS);
  1198. wolfSSL_SESSION_free(session);
  1199. /* let them talk */
  1200. QuicConversation_init(&conv, &tclient, &tserver);
  1201. QuicConversation_do(&conv);
  1202. /* this is what should happen. Look Ma, no certificate! */
  1203. AssertStrEQ(conv.rec_log, "ClientHello:ServerHello:EncryptedExtension:Finished:Finished:SessionTicket");
  1204. QuicTestContext_free(&tclient);
  1205. QuicTestContext_free(&tserver);
  1206. wolfSSL_CTX_free(ctx_c);
  1207. wolfSSL_CTX_free(ctx_s);
  1208. printf(" test_quic_session_export: %s\n", (ret == 0)? passed : failed);
  1209. return ret;
  1210. }
  1211. #endif /* WOLFSSL_SESSION_EXPORT */
  1212. #endif /* WOLFSSL_QUIC */
  1213. int QuicTest(void)
  1214. {
  1215. int ret = 0;
  1216. #ifdef WOLFSSL_QUIC
  1217. int verbose = 0;
  1218. printf(" Begin QUIC Tests\n");
  1219. if ((ret = test_set_quic_method()) != 0) goto leave;
  1220. if ((ret = test_provide_quic_data()) != 0) goto leave;
  1221. if ((ret = test_quic_crypt()) != 0) goto leave;
  1222. if ((ret = test_quic_client_hello(verbose)) != 0) goto leave;
  1223. if ((ret = test_quic_server_hello(verbose)) != 0) goto leave;
  1224. #ifdef HAVE_SESSION_TICKET
  1225. if ((ret = test_quic_resumption(verbose)) != 0) goto leave;
  1226. #ifdef WOLFSSL_EARLY_DATA
  1227. if ((ret = test_quic_early_data(verbose)) != 0) goto leave;
  1228. #endif /* WOLFSSL_EARLY_DATA */
  1229. if ((ret = test_quic_session_export(verbose)) != 0) goto leave;
  1230. #endif /* HAVE_SESSION_TICKET */
  1231. leave:
  1232. if (ret != 0)
  1233. printf(" FAILED: some tests did not pass.\n");
  1234. printf(" End QUIC Tests\n");
  1235. #endif
  1236. return ret;
  1237. }