quic.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /* quic.c
  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. /* Name change compatibility layer no longer needs to be included here */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #ifdef NO_INLINE
  27. #include <wolfssl/wolfcrypt/misc.h>
  28. #else
  29. #define WOLFSSL_MISC_INCLUDED
  30. #include <wolfcrypt/src/misc.c>
  31. #endif
  32. #ifndef WOLFCRYPT_ONLY
  33. #ifdef WOLFSSL_QUIC
  34. #include <wolfssl/error-ssl.h>
  35. #include <wolfssl/ssl.h>
  36. #include <wolfssl/internal.h>
  37. #include <wolfssl/openssl/buffer.h>
  38. #include <wolfssl/openssl/ecdsa.h>
  39. #include <wolfssl/openssl/evp.h>
  40. #include <wolfssl/openssl/kdf.h>
  41. static int qr_length(const uint8_t *data, size_t len)
  42. {
  43. word32 rlen;
  44. if (len < 4) {
  45. return 0;
  46. }
  47. c24to32(&data[1], &rlen);
  48. return (int)rlen + 4;
  49. }
  50. static void quic_record_free(WOLFSSL *ssl, QuicRecord *r)
  51. {
  52. (void)ssl;
  53. if (r->data) {
  54. ForceZero(r->data, r->capacity);
  55. XFREE(r->data, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  56. }
  57. XFREE(r, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  58. }
  59. static QuicRecord *quic_record_make(WOLFSSL *ssl,
  60. WOLFSSL_ENCRYPTION_LEVEL level,
  61. const uint8_t *data, size_t len)
  62. {
  63. QuicRecord *qr;
  64. qr = (QuicRecord*)XMALLOC(sizeof(*qr), ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
  65. if (qr) {
  66. memset(qr, 0, sizeof(*qr));
  67. qr->level = level;
  68. if (level == wolfssl_encryption_early_data) {
  69. qr->capacity = qr->len = (word32)len;
  70. }
  71. else {
  72. qr->capacity = qr->len = qr_length(data, len);
  73. }
  74. if (qr->capacity == 0) {
  75. qr->capacity = 2*1024;
  76. }
  77. qr->data = (uint8_t*)XMALLOC(qr->capacity, ssl->heap,
  78. DYNAMIC_TYPE_TMP_BUFFER);
  79. if (!qr->data) {
  80. quic_record_free(ssl, qr);
  81. return NULL;
  82. }
  83. }
  84. return qr;
  85. }
  86. static int quic_record_complete(QuicRecord *r)
  87. {
  88. return r->len && r->end >= r->len;
  89. }
  90. static int quic_record_done(QuicRecord *r)
  91. {
  92. return r->len && r->end >= r->len && r->start >= r->end;
  93. }
  94. static int quic_record_append(WOLFSSL *ssl, QuicRecord *qr, const uint8_t *data,
  95. size_t len, size_t *pconsumed)
  96. {
  97. size_t missing, consumed = 0;
  98. int ret = WOLFSSL_SUCCESS;
  99. (void)ssl;
  100. if (!qr->len && len) {
  101. missing = 4 - qr->end;
  102. if (len < missing) {
  103. XMEMCPY(qr->data + qr->end, data, len);
  104. qr->end += len;
  105. consumed = len;
  106. goto cleanup; /* len consumed, but qr->len still unkown */
  107. }
  108. XMEMCPY(qr->data + qr->end, data, missing);
  109. qr->end += missing;
  110. len -= missing;
  111. data += missing;
  112. consumed = missing;
  113. qr->len = qr_length(qr->data, qr->end);
  114. if (qr->len > qr->capacity) {
  115. uint8_t *ndata = (uint8_t*)XREALLOC(qr->data, qr->len, ssl->head,
  116. DYNAMIC_TYPE_TMP_BUFFER);
  117. if (!ndata) {
  118. ret = WOLFSSL_FAILURE;
  119. goto cleanup;
  120. }
  121. qr->data = ndata;
  122. qr->capacity = qr->len;
  123. }
  124. }
  125. if (quic_record_complete(qr) || len == 0) {
  126. return 0;
  127. }
  128. missing = qr->len - qr->end;
  129. if (len > missing) {
  130. len = missing;
  131. }
  132. XMEMCPY(qr->data + qr->end, data, len);
  133. qr->end += len;
  134. consumed += len;
  135. cleanup:
  136. *pconsumed = (ret == WOLFSSL_SUCCESS) ? consumed : 0;
  137. return ret;
  138. }
  139. static word32 add_rec_header(byte* output, word32 length, int type)
  140. {
  141. RecordLayerHeader* rl;
  142. /* record layer header */
  143. rl = (RecordLayerHeader*)output;
  144. if (rl == NULL) {
  145. return 0;
  146. }
  147. rl->type = type;
  148. rl->pvMajor = SSLv3_MAJOR;
  149. rl->pvMinor = TLSv1_2_MINOR;
  150. c16toa((word16)length, rl->length);
  151. return RECORD_HEADER_SZ;
  152. }
  153. static word32 quic_record_transfer(QuicRecord* qr, byte* buf, word32 sz)
  154. {
  155. word32 len = qr->end - qr->start;
  156. word32 offset = 0;
  157. word16 rlen;
  158. if (len <= 0) {
  159. return 0;
  160. }
  161. if (qr->rec_hdr_remain == 0) {
  162. /* start a new TLS record */
  163. rlen = (qr->len <= (word32)MAX_RECORD_SIZE) ?
  164. qr->len : (word32)MAX_RECORD_SIZE;
  165. offset += add_rec_header(buf, rlen,
  166. (qr->level == wolfssl_encryption_early_data) ?
  167. application_data : handshake);
  168. qr->rec_hdr_remain = rlen;
  169. sz -= offset;
  170. }
  171. if (len > qr->rec_hdr_remain) {
  172. len = qr->rec_hdr_remain;
  173. }
  174. if (len > sz) {
  175. len = sz;
  176. }
  177. if (len > 0) {
  178. XMEMCPY(buf + offset, qr->data + qr->start, len);
  179. qr->start += len;
  180. qr->rec_hdr_remain -= len;
  181. }
  182. return len + offset;
  183. }
  184. const QuicTransportParam* QuicTransportParam_new(const uint8_t* data,
  185. size_t len, void* heap)
  186. {
  187. QuicTransportParam* tp;
  188. if (len > 65353) return NULL;
  189. tp = (QuicTransportParam*)XMALLOC(sizeof(*tp), heap, DYNAMIC_TYPE_TLSX);
  190. if (!tp) return NULL;
  191. tp->data = (uint8_t*)XMALLOC(len, heap, DYNAMIC_TYPE_TLSX);
  192. if (!tp->data) {
  193. XFREE(tp, heap, DYNAMIC_TYPE_TLSX);
  194. return NULL;
  195. }
  196. XMEMCPY((uint8_t*)tp->data, data, len);
  197. tp->len = len;
  198. return tp;
  199. }
  200. const QuicTransportParam* QuicTransportParam_dup(const QuicTransportParam* tp,
  201. void* heap)
  202. {
  203. QuicTransportParam* tp2;
  204. tp2 = (QuicTransportParam*)XMALLOC(sizeof(*tp2), heap, DYNAMIC_TYPE_TLSX);
  205. if (!tp2) return NULL;
  206. tp2->data = (uint8_t*)XMALLOC(tp->len, heap, DYNAMIC_TYPE_TLSX);
  207. if (!tp2->data) {
  208. XFREE(tp2, heap, DYNAMIC_TYPE_TLSX);
  209. return NULL;
  210. }
  211. XMEMCPY((uint8_t*)tp2->data, tp->data, tp->len);
  212. tp2->len = tp->len;
  213. return tp2;
  214. }
  215. void QuicTransportParam_free(const QuicTransportParam* tp, void* heap)
  216. {
  217. (void)heap;
  218. if (tp) {
  219. if (tp->data) XFREE((uint8_t*)tp->data, heap, DYNAMIC_TYPE_TLSX);
  220. XFREE((void*)tp, heap, DYNAMIC_TYPE_TLSX);
  221. }
  222. }
  223. void wolfSSL_quic_clear(WOLFSSL* ssl)
  224. {
  225. QuicEncData* qd;
  226. /* keep
  227. * - ssl->quic.transport_local
  228. * - ssl->quic.method
  229. * - ssl->quic.transport_version
  230. * reset/free everything else
  231. */
  232. if (ssl->quic.transport_peer) {
  233. QTP_FREE(ssl->quic.transport_peer, ssl->heap);
  234. ssl->quic.transport_peer = NULL;
  235. }
  236. if (ssl->quic.transport_peer_draft) {
  237. QTP_FREE(ssl->quic.transport_peer_draft, ssl->heap);
  238. ssl->quic.transport_peer_draft = NULL;
  239. }
  240. ssl->quic.enc_level_write = wolfssl_encryption_initial;
  241. ssl->quic.enc_level_latest_recvd = wolfssl_encryption_initial;
  242. while ((qd = ssl->quic.input_head)) {
  243. ssl->quic.input_head = qd->next;
  244. quic_record_free(ssl, qd);
  245. }
  246. ssl->quic.input_tail = NULL;
  247. ssl->quic.output_rec_remain = 0;
  248. if (ssl->quic.scratch) {
  249. quic_record_free(ssl, ssl->quic.scratch);
  250. ssl->quic.scratch = NULL;
  251. }
  252. }
  253. void wolfSSL_quic_free(WOLFSSL* ssl)
  254. {
  255. wolfSSL_quic_clear(ssl);
  256. if (ssl->quic.transport_local) {
  257. QTP_FREE(ssl->quic.transport_local, ssl->heap);
  258. ssl->quic.transport_local = NULL;
  259. }
  260. ssl->quic.method = NULL;
  261. }
  262. static int ctx_check_quic_compat(const WOLFSSL_CTX* ctx)
  263. {
  264. WOLFSSL_ENTER("ctx_check_quic_compat");
  265. if (ctx->method->version.major != SSLv3_MAJOR
  266. || ctx->method->version.minor != TLSv1_3_MINOR
  267. || (ctx->method->downgrade && ctx->minDowngrade < TLSv1_3_MINOR)) {
  268. WOLFSSL_MSG_EX("ctx not quic compatible: vmajor=%d, vminor=%d, downgrade=%d",
  269. ctx->method->version.major,
  270. ctx->method->version.minor,
  271. ctx->method->downgrade
  272. );
  273. return WOLFSSL_FAILURE;
  274. }
  275. return WOLFSSL_SUCCESS;
  276. }
  277. static int check_method_sanity(const WOLFSSL_QUIC_METHOD* m)
  278. {
  279. WOLFSSL_ENTER("check_method_sanity");
  280. if (m && m->set_encryption_secrets
  281. && m->add_handshake_data
  282. && m->flush_flight
  283. && m->send_alert) {
  284. return WOLFSSL_SUCCESS;
  285. }
  286. return WOLFSSL_FAILURE;
  287. }
  288. int wolfSSL_CTX_set_quic_method(WOLFSSL_CTX* ctx,
  289. const WOLFSSL_QUIC_METHOD* quic_method)
  290. {
  291. WOLFSSL_ENTER("wolfSSL_CTX_set_quic_method");
  292. if (ctx_check_quic_compat(ctx) != WOLFSSL_SUCCESS
  293. || check_method_sanity(quic_method) != WOLFSSL_SUCCESS) {
  294. return WOLFSSL_FAILURE;
  295. }
  296. ctx->quic.method = quic_method;
  297. return WOLFSSL_SUCCESS;
  298. }
  299. int wolfSSL_set_quic_method(WOLFSSL* ssl,
  300. const WOLFSSL_QUIC_METHOD* quic_method)
  301. {
  302. WOLFSSL_ENTER("wolfSSL_set_quic_method");
  303. if (ctx_check_quic_compat(ssl->ctx) != WOLFSSL_SUCCESS
  304. || check_method_sanity(quic_method) != WOLFSSL_SUCCESS) {
  305. return WOLFSSL_FAILURE;
  306. }
  307. ssl->quic.method = quic_method;
  308. return WOLFSSL_SUCCESS;
  309. }
  310. int wolfSSL_is_quic(WOLFSSL* ssl)
  311. {
  312. return WOLFSSL_IS_QUIC(ssl);
  313. }
  314. WOLFSSL_ENCRYPTION_LEVEL wolfSSL_quic_read_level(const WOLFSSL* ssl)
  315. {
  316. return ssl->quic.enc_level_read;
  317. }
  318. WOLFSSL_ENCRYPTION_LEVEL wolfSSL_quic_write_level(const WOLFSSL* ssl)
  319. {
  320. return ssl->quic.enc_level_write;
  321. }
  322. int wolfSSL_set_quic_transport_params(WOLFSSL* ssl,
  323. const uint8_t* params,
  324. size_t params_len)
  325. {
  326. const QuicTransportParam* tp;
  327. int ret = WOLFSSL_SUCCESS;
  328. WOLFSSL_ENTER("SSL_set_quic_transport_params");
  329. if (!params || params_len == 0) {
  330. tp = NULL;
  331. }
  332. else {
  333. tp = QuicTransportParam_new(params, params_len, ssl->heap);
  334. if (!tp) {
  335. ret = WOLFSSL_FAILURE;
  336. goto cleanup;
  337. }
  338. }
  339. if (ssl->quic.transport_local)
  340. QTP_FREE(ssl->quic.transport_local, ssl->heap);
  341. ssl->quic.transport_local = tp;
  342. cleanup:
  343. WOLFSSL_LEAVE("SSL_set_quic_transport_params", ret);
  344. return ret;
  345. }
  346. void wolfSSL_get_peer_quic_transport_params(const WOLFSSL* ssl,
  347. const uint8_t** out_params,
  348. size_t* out_params_len)
  349. {
  350. const QuicTransportParam* tp = ssl->quic.transport_peer ?
  351. ssl->quic.transport_peer : ssl->quic.transport_peer_draft;
  352. *out_params = tp ? tp->data : NULL;
  353. *out_params_len = tp ? tp->len : 0;
  354. }
  355. int wolfSSL_get_peer_quic_transport_version(const WOLFSSL* ssl)
  356. {
  357. return ssl->quic.transport_peer ?
  358. TLSX_KEY_QUIC_TP_PARAMS : (ssl->quic.transport_peer_draft ?
  359. TLSX_KEY_QUIC_TP_PARAMS : -1);
  360. }
  361. void wolfSSL_set_quic_use_legacy_codepoint(WOLFSSL* ssl, int use_legacy)
  362. {
  363. ssl->quic.transport_version = use_legacy ? TLSX_KEY_QUIC_TP_PARAMS_DRAFT
  364. : TLSX_KEY_QUIC_TP_PARAMS;
  365. }
  366. void wolfSSL_set_quic_transport_version(WOLFSSL* ssl, int version)
  367. {
  368. if (version == TLSX_KEY_QUIC_TP_PARAMS
  369. || version == TLSX_KEY_QUIC_TP_PARAMS_DRAFT
  370. || !version) {
  371. ssl->quic.transport_version = version;
  372. }
  373. else {
  374. WOLFSSL_MSG("wolfSSL_set_quic_transport_version: invalid version");
  375. }
  376. }
  377. int wolfSSL_get_quic_transport_version(const WOLFSSL* ssl)
  378. {
  379. return ssl->quic.transport_version;
  380. }
  381. int wolfSSL_quic_add_transport_extensions(WOLFSSL* ssl, int msg_type)
  382. {
  383. /* RFC 9001, ch. 8.2: "The quic_transport_parameters extension is carried
  384. * in the ClientHello and the EncryptedExtensions messages during the
  385. * handshake. Endpoints MUST send the quic_transport_parameters extension;"
  386. * Which means, at least one. There can be more to signal compatibility to
  387. * older/newer versions.
  388. */
  389. int ret = 0, is_resp = (msg_type == encrypted_extensions);
  390. if (ssl->quic.transport_local == NULL) {
  391. return QUIC_TP_MISSING_E;
  392. }
  393. if (is_resp) {
  394. /* server response: time to decide which version to use */
  395. if (ssl->quic.transport_peer && ssl->quic.transport_peer_draft) {
  396. if (ssl->quic.transport_version == TLSX_KEY_QUIC_TP_PARAMS_DRAFT) {
  397. ret = TLSX_QuicTP_Use(ssl,
  398. TLSX_KEY_QUIC_TP_PARAMS_DRAFT, is_resp);
  399. QTP_FREE(ssl->quic.transport_peer, ssl->heap);
  400. ssl->quic.transport_peer = NULL;
  401. }
  402. else {
  403. ret = TLSX_QuicTP_Use(ssl, TLSX_KEY_QUIC_TP_PARAMS, is_resp);
  404. QTP_FREE(ssl->quic.transport_peer_draft,
  405. ssl->heap);
  406. ssl->quic.transport_peer_draft = NULL;
  407. }
  408. }
  409. else {
  410. if (ssl->quic.transport_version == TLSX_KEY_QUIC_TP_PARAMS_DRAFT
  411. && ssl->quic.transport_peer_draft) {
  412. ret = TLSX_QuicTP_Use(ssl, TLSX_KEY_QUIC_TP_PARAMS_DRAFT,
  413. is_resp);
  414. }
  415. else if (ssl->quic.transport_peer) {
  416. ret = TLSX_QuicTP_Use(ssl, TLSX_KEY_QUIC_TP_PARAMS, is_resp);
  417. }
  418. else {
  419. /* no match, send none, will let the client fail */
  420. }
  421. }
  422. }
  423. else {
  424. /* client hello */
  425. if (ssl->quic.transport_version == 0) {
  426. /* not being set to a particular id, we send both draft+v1 */
  427. ret = TLSX_QuicTP_Use(ssl, TLSX_KEY_QUIC_TP_PARAMS, is_resp)
  428. || TLSX_QuicTP_Use(ssl, TLSX_KEY_QUIC_TP_PARAMS_DRAFT, is_resp);
  429. }
  430. else {
  431. /* otherwise, send the version configured */
  432. ret = TLSX_QuicTP_Use(ssl, (TLSX_Type)ssl->quic.transport_version,
  433. is_resp);
  434. }
  435. }
  436. return ret;
  437. }
  438. #define QUIC_HS_FLIGHT_LIMIT_DEFAULT (16* 1024)
  439. size_t wolfSSL_quic_max_handshake_flight_len(const WOLFSSL* ssl,
  440. WOLFSSL_ENCRYPTION_LEVEL level)
  441. {
  442. switch (level) {
  443. case wolfssl_encryption_initial:
  444. case wolfssl_encryption_application:
  445. return QUIC_HS_FLIGHT_LIMIT_DEFAULT;
  446. case wolfssl_encryption_early_data:
  447. return 0; /* QUIC does not send at this level */
  448. case wolfssl_encryption_handshake:
  449. /* during handshake itself, certificates may be exchanged which
  450. * exceed our default limit, advise a higher limit one.
  451. */
  452. if (ssl->options.side == WOLFSSL_SERVER_END) {
  453. if (ssl->options.verifyPeer
  454. && MAX_CERTIFICATE_SZ > QUIC_HS_FLIGHT_LIMIT_DEFAULT)
  455. return MAX_CERTIFICATE_SZ;
  456. }
  457. else {
  458. /* clients may receive the server cert chain
  459. */
  460. if (2*MAX_CERTIFICATE_SZ > QUIC_HS_FLIGHT_LIMIT_DEFAULT)
  461. return 2*MAX_CERTIFICATE_SZ;
  462. }
  463. return QUIC_HS_FLIGHT_LIMIT_DEFAULT;
  464. }
  465. return 0;
  466. }
  467. #ifdef WOLFSSL_EARLY_DATA
  468. void wolfSSL_set_quic_early_data_enabled(WOLFSSL* ssl, int enabled)
  469. {
  470. /* This only has effect on server and when the handshake has
  471. * not started yet.
  472. * This function is part of the quictls/openssl API and does
  473. * not return any error, sadly. So we just ignore any
  474. * unsuccessful use. But we can produce some warnings.
  475. */
  476. if (!WOLFSSL_IS_QUIC(ssl)) {
  477. WOLFSSL_MSG("wolfSSL_set_quic_early_data_enabled: not a QUIC SSL");
  478. }
  479. else if (ssl->options.handShakeState != NULL_STATE) {
  480. WOLFSSL_MSG("wolfSSL_set_quic_early_data_enabled: handshake started");
  481. }
  482. else {
  483. wolfSSL_set_max_early_data(ssl, enabled ? UINT32_MAX : 0);
  484. }
  485. }
  486. #endif /* WOLFSSL_EARLY_DATA */
  487. int wolfSSL_quic_do_handshake(WOLFSSL* ssl)
  488. {
  489. int ret = WOLFSSL_SUCCESS;
  490. WOLFSSL_ENTER("wolfSSL_quic_do_handshake");
  491. if (!wolfSSL_is_quic(ssl)) {
  492. WOLFSSL_MSG("WOLFSSL_QUIC_DO_HANDSHAKE not a QUIC SSL");
  493. ret = WOLFSSL_FAILURE;
  494. goto cleanup;
  495. }
  496. while (ssl->options.handShakeState != HANDSHAKE_DONE) {
  497. /* Peculiar: do_handshake() is successful, but the state
  498. * indicates that we are not DONE. This seems to happen
  499. * when resuming sessions and an EARLY_DATA indicator
  500. * is presented by the client.
  501. * Theory: wolfSSL expects the APP to read the early data
  502. * and silently continues the handshake when the EndOfEarlyData
  503. * and the client Finished arrives.
  504. * This confuses the QUIC state handling.
  505. */
  506. #ifdef WOLFSSL_EARLY_DATA
  507. if (ssl->options.maxEarlyDataSz) {
  508. byte tmpbuffer[256];
  509. int len;
  510. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  511. if (ssl->options.resuming) {
  512. ret = wolfSSL_write_early_data(ssl, tmpbuffer, 0, &len);
  513. }
  514. }
  515. else {
  516. ret = wolfSSL_read_early_data(ssl, tmpbuffer,
  517. sizeof(tmpbuffer), &len);
  518. if (ret < 0 && ssl->error == ZERO_RETURN) {
  519. /* this is expected, since QUIC handles the actual early
  520. * data separately. */
  521. ret = WOLFSSL_SUCCESS;
  522. }
  523. }
  524. if (ret < 0) {
  525. goto cleanup;
  526. }
  527. }
  528. #endif /* WOLFSSL_EARLY_DATA */
  529. ret = wolfSSL_SSL_do_handshake_internal(ssl);
  530. if (ret <= 0)
  531. goto cleanup;
  532. }
  533. cleanup:
  534. if (ret <= 0
  535. && ssl->options.handShakeState == HANDSHAKE_DONE
  536. && (ssl->error == ZERO_RETURN || ssl->error == WANT_READ)) {
  537. ret = WOLFSSL_SUCCESS;
  538. }
  539. if (ret == WOLFSSL_SUCCESS) {
  540. ssl->error = WOLFSSL_ERROR_NONE;
  541. }
  542. WOLFSSL_LEAVE("wolfSSL_quic_do_handshake", ret);
  543. return ret;
  544. }
  545. int wolfSSL_quic_read_write(WOLFSSL* ssl)
  546. {
  547. int ret = WOLFSSL_SUCCESS;
  548. WOLFSSL_ENTER("wolfSSL_quic_read_write");
  549. if (!wolfSSL_is_quic(ssl)) {
  550. WOLFSSL_MSG("WOLFSSL_QUIC_READ_WRITE not a QUIC SSL");
  551. ret = WOLFSSL_FAILURE;
  552. goto cleanup;
  553. }
  554. if (ssl->options.handShakeState != HANDSHAKE_DONE) {
  555. ret = wolfSSL_quic_do_handshake(ssl);
  556. if (ret != WOLFSSL_SUCCESS)
  557. goto cleanup;
  558. }
  559. ret = wolfSSL_process_quic_post_handshake(ssl);
  560. cleanup:
  561. WOLFSSL_LEAVE("wolfSSL_quic_read_write", ret);
  562. return ret;
  563. }
  564. int wolfSSL_process_quic_post_handshake(WOLFSSL* ssl)
  565. {
  566. int ret = WOLFSSL_SUCCESS, nret;
  567. WOLFSSL_ENTER("wolfSSL_process_quic_post_handshake");
  568. if (!wolfSSL_is_quic(ssl)) {
  569. WOLFSSL_MSG("WOLFSSL_QUIC_POST_HS not a QUIC SSL");
  570. ret = WOLFSSL_FAILURE;
  571. goto cleanup;
  572. }
  573. if (ssl->options.handShakeState != HANDSHAKE_DONE) {
  574. WOLFSSL_MSG("WOLFSSL_QUIC_POST_HS handshake is not done yet");
  575. ret = WOLFSSL_FAILURE;
  576. goto cleanup;
  577. }
  578. while (ssl->quic.input_head != NULL
  579. || ssl->buffers.inputBuffer.length > 0) {
  580. if ((nret = ProcessReply(ssl)) < 0) {
  581. ret = nret;
  582. break;
  583. }
  584. }
  585. while (ssl->buffers.outputBuffer.length > 0) {
  586. SendBuffered(ssl);
  587. }
  588. cleanup:
  589. WOLFSSL_LEAVE("wolfSSL_process_quic_post_handshake", ret);
  590. return ret;
  591. }
  592. int wolfSSL_provide_quic_data(WOLFSSL* ssl, WOLFSSL_ENCRYPTION_LEVEL level,
  593. const uint8_t* data, size_t len)
  594. {
  595. int ret = WOLFSSL_SUCCESS;
  596. size_t l;
  597. WOLFSSL_ENTER("wolfSSL_provide_quic_data");
  598. if (!wolfSSL_is_quic(ssl)) {
  599. WOLFSSL_MSG("WOLFSSL_QUIC_PROVIDE_DATA not a QUIC SSL");
  600. ret = WOLFSSL_FAILURE;
  601. goto cleanup;
  602. }
  603. if (level < wolfSSL_quic_read_level(ssl)
  604. || (ssl->quic.input_tail && level < ssl->quic.input_tail->level)
  605. || level < ssl->quic.enc_level_latest_recvd) {
  606. WOLFSSL_MSG("WOLFSSL_QUIC_PROVIDE_DATA wrong encryption level");
  607. ret = WOLFSSL_FAILURE;
  608. goto cleanup;
  609. }
  610. while (len > 0) {
  611. if (ssl->quic.scratch) {
  612. if (ssl->quic.scratch->level != level) {
  613. WOLFSSL_MSG("WOLFSSL_QUIC_PROVIDE_DATA wrong encryption level");
  614. ret = WOLFSSL_FAILURE;
  615. goto cleanup;
  616. }
  617. ret = quic_record_append(ssl, ssl->quic.scratch, data, len, &l);
  618. if (ret != WOLFSSL_SUCCESS) {
  619. goto cleanup;
  620. }
  621. data += l;
  622. len -= l;
  623. if (quic_record_complete(ssl->quic.scratch)) {
  624. if (ssl->quic.input_tail) {
  625. ssl->quic.input_tail->next = ssl->quic.scratch;
  626. ssl->quic.input_tail = ssl->quic.scratch;
  627. }
  628. else {
  629. ssl->quic.input_head = ssl->quic.input_tail =
  630. ssl->quic.scratch;
  631. }
  632. ssl->quic.scratch = NULL;
  633. }
  634. }
  635. else {
  636. /* start of next record with all bytes for the header */
  637. ssl->quic.scratch = quic_record_make(ssl, level, data, len);
  638. if (!ssl->quic.scratch) {
  639. ret = WOLFSSL_FAILURE;
  640. goto cleanup;
  641. }
  642. }
  643. }
  644. ssl->quic.enc_level_latest_recvd = level;
  645. cleanup:
  646. WOLFSSL_LEAVE("wolfSSL_provide_quic_data", ret);
  647. return ret;
  648. }
  649. /* Called internally when SSL wants a certain amount of input. */
  650. int wolfSSL_quic_receive(WOLFSSL* ssl, byte* buf, word32 sz)
  651. {
  652. word32 n = 0;
  653. int transferred = 0;
  654. WOLFSSL_ENTER("wolfSSL_quic_receive");
  655. while (sz > 0) {
  656. n = 0;
  657. if (ssl->quic.input_head) {
  658. n = quic_record_transfer(ssl->quic.input_head, buf, sz);
  659. if (quic_record_done(ssl->quic.input_head)) {
  660. QuicRecord* qr = ssl->quic.input_head;
  661. ssl->quic.input_head = qr->next;
  662. if (!qr->next) {
  663. ssl->quic.input_tail = NULL;
  664. }
  665. quic_record_free(ssl, qr);
  666. }
  667. }
  668. if (n == 0) {
  669. if (transferred > 0) {
  670. goto cleanup;
  671. }
  672. ssl->error = transferred = WANT_READ;
  673. goto cleanup;
  674. }
  675. sz -= n;
  676. buf += n;
  677. transferred += n;
  678. }
  679. cleanup:
  680. WOLFSSL_LEAVE("wolfSSL_quic_receive", transferred);
  681. return transferred;
  682. }
  683. /**
  684. * We need to forward the HANDSHAKE messages to the QUIC protocol stack
  685. * via ssl->quic.method->add_handshake_data().
  686. * The messages in the output buffer are unencrypted TLS records. We need
  687. * to forward the content of those records.
  688. */
  689. static int wolfSSL_quic_send_internal(WOLFSSL* ssl)
  690. {
  691. int ret = 0, aret;
  692. size_t len;
  693. RecordLayerHeader* rl;
  694. word16 rlen;
  695. word32 idx, length;
  696. byte* output;
  697. WOLFSSL_ENTER("wolfSSL_quic_send");
  698. idx = ssl->buffers.outputBuffer.idx;
  699. length = ssl->buffers.outputBuffer.length;
  700. output = ssl->buffers.outputBuffer.buffer + idx;
  701. while (length > 0) {
  702. if (ssl->quic.output_rec_remain > 0) {
  703. len = ssl->quic.output_rec_remain;
  704. if (len > length) {
  705. len = length;
  706. }
  707. aret = ssl->quic.method->add_handshake_data(ssl,
  708. ssl->quic.output_rec_level, (const uint8_t*)output, len);
  709. if (aret != 1) {
  710. /* The application has an error. General disaster. */
  711. WOLFSSL_MSG("WOLFSSL_QUIC_SEND application failed");
  712. ret = FWRITE_ERROR;
  713. goto cleanup;
  714. }
  715. output += len;
  716. length -= len;
  717. ssl->quic.output_rec_remain -= len;
  718. }
  719. else {
  720. /* at start of a TLS Record */
  721. rl = (RecordLayerHeader*)output;
  722. ato16(rl->length, &rlen);
  723. output += RECORD_HEADER_SZ;
  724. length -= RECORD_HEADER_SZ;
  725. ssl->quic.output_rec_remain = rlen;
  726. ssl->quic.output_rec_level = ssl->quic.enc_level_write;
  727. if (rl->type == application_data) {
  728. if (ssl->options.handShakeState != HANDSHAKE_DONE) {
  729. ssl->quic.output_rec_level = wolfssl_encryption_early_data;
  730. }
  731. else {
  732. WOLFSSL_MSG("WOLFSSL_QUIC_SEND app data after handshake");
  733. ret = FWRITE_ERROR;
  734. goto cleanup;
  735. }
  736. }
  737. }
  738. }
  739. ssl->buffers.outputBuffer.idx = 0;
  740. ssl->buffers.outputBuffer.length = 0;
  741. cleanup:
  742. WOLFSSL_LEAVE("wolfSSL_quic_send", ret);
  743. return ret;
  744. }
  745. int wolfSSL_quic_send(WOLFSSL* ssl)
  746. {
  747. return wolfSSL_quic_send_internal(ssl);
  748. }
  749. int wolfSSL_quic_forward_secrets(WOLFSSL* ssl, int ktype, int side)
  750. {
  751. const uint8_t* rx_secret = NULL, *tx_secret = NULL;
  752. WOLFSSL_ENCRYPTION_LEVEL level;
  753. int ret = 0;
  754. WOLFSSL_ENTER("wolfSSL_quic_forward_secrets");
  755. switch (ktype) {
  756. case early_data_key:
  757. level = wolfssl_encryption_early_data;
  758. break;
  759. case handshake_key:
  760. level = wolfssl_encryption_handshake;
  761. break;
  762. case traffic_key:
  763. FALL_THROUGH;
  764. case update_traffic_key:
  765. level = wolfssl_encryption_application;
  766. break;
  767. case no_key:
  768. FALL_THROUGH;
  769. default:
  770. /* ignore */
  771. goto cleanup;
  772. }
  773. if (side == ENCRYPT_AND_DECRYPT_SIDE || side == ENCRYPT_SIDE_ONLY) {
  774. tx_secret = (ssl->options.side == WOLFSSL_CLIENT_END) ?
  775. ssl->clientSecret : ssl->serverSecret;
  776. }
  777. if (side == ENCRYPT_AND_DECRYPT_SIDE || side == DECRYPT_SIDE_ONLY) {
  778. rx_secret = (ssl->options.side == WOLFSSL_CLIENT_END) ?
  779. ssl->serverSecret : ssl->clientSecret;
  780. }
  781. if (!tx_secret && !rx_secret) {
  782. WOLFSSL_MSG("WOLFSSL_QUIC_FORWARD_SECRETS neither "
  783. "enc- nor decrypt specified");
  784. goto cleanup;
  785. }
  786. ret = !ssl->quic.method->set_encryption_secrets(
  787. ssl, level, rx_secret, tx_secret, ssl->specs.hash_size);
  788. /* Having installed the secrets, any future read/write will happen
  789. * at the level. Except early data, which is detected on the record
  790. * type and the handshake state. */
  791. if (ktype == early_data_key) {
  792. goto cleanup;
  793. }
  794. if (tx_secret && ssl->quic.enc_level_write != level) {
  795. ssl->quic.enc_level_write_next = level;
  796. }
  797. if (rx_secret && ssl->quic.enc_level_read != level) {
  798. ssl->quic.enc_level_read_next = level;
  799. }
  800. cleanup:
  801. WOLFSSL_LEAVE("wolfSSL_quic_forward_secrets", ret);
  802. return ret;
  803. }
  804. int wolfSSL_quic_keys_active(WOLFSSL* ssl, enum encrypt_side side)
  805. {
  806. int ret = 0;
  807. WOLFSSL_ENTER("wolfSSL_quic_keys_active");
  808. /* Keys derived from recent secrets have been activated */
  809. if (side == ENCRYPT_AND_DECRYPT_SIDE || side == ENCRYPT_SIDE_ONLY) {
  810. /* If there is data in the output buffers, it was supposed to be
  811. * encrypted at the previous level. We need to remember that when
  812. * forwarding this data to the QUIC protocol application. */
  813. if (ssl->buffers.outputBuffer.length > 0) {
  814. ret = wolfSSL_quic_send_internal(ssl);
  815. if (ret)
  816. goto cleanup;
  817. }
  818. ssl->quic.enc_level_write = ssl->quic.enc_level_write_next;
  819. }
  820. if (side == ENCRYPT_AND_DECRYPT_SIDE || side == DECRYPT_SIDE_ONLY) {
  821. ssl->quic.enc_level_read = ssl->quic.enc_level_read_next;
  822. }
  823. cleanup:
  824. WOLFSSL_LEAVE("wolfSSL_quic_keys_active", ret);
  825. return ret;
  826. }
  827. const WOLFSSL_EVP_CIPHER* wolfSSL_quic_get_aead(WOLFSSL* ssl)
  828. {
  829. WOLFSSL_CIPHER* cipher = wolfSSL_get_current_cipher(ssl);
  830. const WOLFSSL_EVP_CIPHER* evp_cipher;
  831. switch (cipher->cipherSuite) {
  832. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  833. case TLS_AES_128_GCM_SHA256:
  834. evp_cipher = wolfSSL_EVP_aes_128_gcm();
  835. break;
  836. case TLS_AES_256_GCM_SHA384:
  837. evp_cipher = wolfSSL_EVP_aes_256_gcm();
  838. break;
  839. #endif
  840. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  841. case TLS_CHACHA20_POLY1305_SHA256:
  842. evp_cipher = wolfSSL_EVP_chacha20_poly1305();
  843. break;
  844. #endif
  845. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  846. case TLS_AES_128_CCM_SHA256:
  847. FALL_THROUGH;
  848. case TLS_AES_128_CCM_8_SHA256:
  849. evp_cipher = wolfSSL_EVP_aes_128_ctr();
  850. break;
  851. #endif
  852. default:
  853. evp_cipher = NULL;
  854. break;
  855. }
  856. if (!evp_cipher) {
  857. /* should not happen, as SSL* should not have negotiated it? */
  858. WOLFSSL_MSG("wolfSSL_quic_get_aead: current cipher not supported");
  859. return NULL;
  860. }
  861. return evp_cipher;
  862. }
  863. static int evp_cipher_eq(const WOLFSSL_EVP_CIPHER* c1,
  864. const WOLFSSL_EVP_CIPHER* c2)
  865. {
  866. /* We could check on nid equality, but we seem to have singulars */
  867. return c1 == c2;
  868. }
  869. const WOLFSSL_EVP_CIPHER* wolfSSL_quic_get_hp(WOLFSSL* ssl)
  870. {
  871. WOLFSSL_CIPHER* cipher = wolfSSL_get_current_cipher(ssl);
  872. const WOLFSSL_EVP_CIPHER* evp_cipher;
  873. switch (cipher->cipherSuite) {
  874. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  875. case TLS_AES_128_GCM_SHA256:
  876. evp_cipher = wolfSSL_EVP_aes_128_ctr();
  877. break;
  878. case TLS_AES_256_GCM_SHA384:
  879. evp_cipher = wolfSSL_EVP_aes_256_ctr();
  880. break;
  881. #endif
  882. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  883. case TLS_CHACHA20_POLY1305_SHA256:
  884. evp_cipher = wolfSSL_EVP_chacha20();
  885. break;
  886. #endif
  887. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  888. case TLS_AES_128_CCM_SHA256:
  889. FALL_THROUGH;
  890. case TLS_AES_128_CCM_8_SHA256:
  891. evp_cipher = wolfSSL_EVP_aes_128_ctr();
  892. break;
  893. #endif
  894. default:
  895. evp_cipher = NULL;
  896. break;
  897. }
  898. if (!evp_cipher) {
  899. /* should not happen, as SSL* should not have negotiated it? */
  900. WOLFSSL_MSG("wolfSSL_quic_get_hp: current cipher not supported");
  901. return NULL;
  902. }
  903. return evp_cipher;
  904. }
  905. size_t wolfSSL_quic_get_aead_tag_len(const WOLFSSL_EVP_CIPHER* aead_cipher)
  906. {
  907. size_t ret;
  908. #ifdef WOLFSSL_SMALL_STACK
  909. WOLFSSL_EVP_CIPHER_CTX *ctx = (WOLFSSL_EVP_CIPHER_CTX *)XMALLOC(
  910. sizeof(*ctx), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  911. if (ctx == NULL)
  912. return 0;
  913. #else
  914. WOLFSSL_EVP_CIPHER_CTX ctx[1];
  915. #endif
  916. XMEMSET(ctx, 0, sizeof(*ctx));
  917. if (wolfSSL_EVP_CipherInit(ctx, aead_cipher, NULL, NULL, 0)
  918. == WOLFSSL_SUCCESS) {
  919. ret = ctx->authTagSz;
  920. } else {
  921. ret = 0;
  922. }
  923. #ifdef WOLFSSL_SMALL_STACK
  924. XFREE(ctx, NULL, DYNAMIC_TYPE_TMP_BUF);
  925. #endif
  926. return ret;
  927. }
  928. int wolfSSL_quic_aead_is_gcm(const WOLFSSL_EVP_CIPHER* aead_cipher)
  929. {
  930. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  931. if (evp_cipher_eq(aead_cipher, wolfSSL_EVP_aes_128_gcm())
  932. #ifdef WOLFSSL_AES_256
  933. || evp_cipher_eq(aead_cipher, wolfSSL_EVP_aes_256_gcm())
  934. #endif
  935. ) {
  936. return 1;
  937. }
  938. #else
  939. (void)aead_cipher;
  940. #endif
  941. return 0;
  942. }
  943. int wolfSSL_quic_aead_is_ccm(const WOLFSSL_EVP_CIPHER* aead_cipher)
  944. {
  945. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  946. if (evp_cipher_eq(aead_cipher, wolfSSL_EVP_aes_128_ctr())) {
  947. return 1;
  948. }
  949. #else
  950. (void)aead_cipher;
  951. #endif
  952. return 0;
  953. }
  954. int wolfSSL_quic_aead_is_chacha20(const WOLFSSL_EVP_CIPHER* aead_cipher)
  955. {
  956. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  957. return evp_cipher_eq(aead_cipher, wolfSSL_EVP_chacha20_poly1305());
  958. #else
  959. (void)aead_cipher;
  960. return 0;
  961. #endif
  962. }
  963. const WOLFSSL_EVP_MD* wolfSSL_quic_get_md(WOLFSSL* ssl)
  964. {
  965. /* a copy from the handshake md setup */
  966. switch(ssl->specs.mac_algorithm) {
  967. case no_mac:
  968. #ifndef NO_MD5
  969. case md5_mac:
  970. return wolfSSL_EVP_md5();
  971. #endif
  972. #ifndef NO_SHA
  973. case sha_mac:
  974. return wolfSSL_EVP_sha1();
  975. #endif
  976. #ifdef WOLFSSL_SHA224
  977. case sha224_mac:
  978. return wolfSSL_EVP_sha224();
  979. #endif
  980. case sha256_mac:
  981. return wolfSSL_EVP_sha256();
  982. #ifdef WOLFSSL_SHA384
  983. case sha384_mac:
  984. return wolfSSL_EVP_sha384();
  985. #endif
  986. #ifdef WOLFSSL_SHA512
  987. case sha512_mac:
  988. return wolfSSL_EVP_sha512();
  989. #endif
  990. case rmd_mac:
  991. case blake2b_mac:
  992. WOLFSSL_MSG("no suitable EVP_MD");
  993. return NULL;
  994. default:
  995. WOLFSSL_MSG("Unknown mac algorithm");
  996. return NULL;
  997. }
  998. }
  999. #ifdef OPENSSL_EXTRA
  1000. int wolfSSL_quic_hkdf_extract(uint8_t* dest, const WOLFSSL_EVP_MD* md,
  1001. const uint8_t* secret, size_t secretlen,
  1002. const uint8_t* salt, size_t saltlen)
  1003. {
  1004. WOLFSSL_EVP_PKEY_CTX* pctx = NULL;
  1005. size_t destlen = (size_t)wolfSSL_EVP_MD_size(md);
  1006. int ret = WOLFSSL_SUCCESS;
  1007. WOLFSSL_ENTER("wolfSSL_quic_hkdf_extract");
  1008. pctx = wolfSSL_EVP_PKEY_CTX_new_id(NID_hkdf, NULL);
  1009. if (pctx == NULL) {
  1010. ret = WOLFSSL_FAILURE;
  1011. goto cleanup;
  1012. }
  1013. if (wolfSSL_EVP_PKEY_derive_init(pctx) != WOLFSSL_SUCCESS
  1014. || wolfSSL_EVP_PKEY_CTX_hkdf_mode(
  1015. pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) != WOLFSSL_SUCCESS
  1016. || wolfSSL_EVP_PKEY_CTX_set_hkdf_md(pctx, md) != WOLFSSL_SUCCESS
  1017. || wolfSSL_EVP_PKEY_CTX_set1_hkdf_salt(
  1018. pctx, (byte*)salt, (int)saltlen) != WOLFSSL_SUCCESS
  1019. || wolfSSL_EVP_PKEY_CTX_set1_hkdf_key(
  1020. pctx, (byte*)secret, (int)secretlen) != WOLFSSL_SUCCESS
  1021. || wolfSSL_EVP_PKEY_derive(pctx, dest, &destlen) != WOLFSSL_SUCCESS) {
  1022. ret = WOLFSSL_FAILURE;
  1023. goto cleanup;
  1024. }
  1025. cleanup:
  1026. if (pctx)
  1027. wolfSSL_EVP_PKEY_CTX_free(pctx);
  1028. WOLFSSL_LEAVE("wolfSSL_quic_hkdf_extract", ret);
  1029. return ret;
  1030. }
  1031. int wolfSSL_quic_hkdf_expand(uint8_t* dest, size_t destlen,
  1032. const WOLFSSL_EVP_MD* md,
  1033. const uint8_t* secret, size_t secretlen,
  1034. const uint8_t* info, size_t infolen)
  1035. {
  1036. WOLFSSL_EVP_PKEY_CTX* pctx = NULL;
  1037. int ret = WOLFSSL_SUCCESS;
  1038. WOLFSSL_ENTER("wolfSSL_quic_hkdf_expand");
  1039. pctx = wolfSSL_EVP_PKEY_CTX_new_id(NID_hkdf, NULL);
  1040. if (pctx == NULL) {
  1041. ret = WOLFSSL_FAILURE;
  1042. goto cleanup;
  1043. }
  1044. if (wolfSSL_EVP_PKEY_derive_init(pctx) != WOLFSSL_SUCCESS
  1045. || wolfSSL_EVP_PKEY_CTX_hkdf_mode(
  1046. pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) != WOLFSSL_SUCCESS
  1047. || wolfSSL_EVP_PKEY_CTX_set_hkdf_md(pctx, md) != WOLFSSL_SUCCESS
  1048. || wolfSSL_EVP_PKEY_CTX_set1_hkdf_salt(
  1049. pctx, (byte*)"", 0) != WOLFSSL_SUCCESS
  1050. || wolfSSL_EVP_PKEY_CTX_set1_hkdf_key(
  1051. pctx, (byte*)secret, (int)secretlen) != WOLFSSL_SUCCESS
  1052. || wolfSSL_EVP_PKEY_CTX_add1_hkdf_info(
  1053. pctx, (byte*)info, (int)infolen) != WOLFSSL_SUCCESS
  1054. || wolfSSL_EVP_PKEY_derive(pctx, dest, &destlen) != WOLFSSL_SUCCESS) {
  1055. ret = WOLFSSL_FAILURE;
  1056. goto cleanup;
  1057. }
  1058. cleanup:
  1059. if (pctx)
  1060. EVP_PKEY_CTX_free(pctx);
  1061. WOLFSSL_LEAVE("wolfSSL_quic_hkdf_expand", ret);
  1062. return ret;
  1063. }
  1064. int wolfSSL_quic_hkdf(uint8_t* dest, size_t destlen,
  1065. const WOLFSSL_EVP_MD* md,
  1066. const uint8_t* secret, size_t secretlen,
  1067. const uint8_t* salt, size_t saltlen,
  1068. const uint8_t* info, size_t infolen)
  1069. {
  1070. WOLFSSL_EVP_PKEY_CTX* pctx = NULL;
  1071. int ret = WOLFSSL_SUCCESS;
  1072. WOLFSSL_ENTER("wolfSSL_quic_hkdf");
  1073. pctx = wolfSSL_EVP_PKEY_CTX_new_id(NID_hkdf, NULL);
  1074. if (pctx == NULL) {
  1075. ret = WOLFSSL_FAILURE;
  1076. goto cleanup;
  1077. }
  1078. if (wolfSSL_EVP_PKEY_derive_init(pctx) != WOLFSSL_SUCCESS
  1079. || wolfSSL_EVP_PKEY_CTX_hkdf_mode(
  1080. pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND) != WOLFSSL_SUCCESS
  1081. || wolfSSL_EVP_PKEY_CTX_set_hkdf_md(pctx, md) != WOLFSSL_SUCCESS
  1082. || wolfSSL_EVP_PKEY_CTX_set1_hkdf_salt(
  1083. pctx, (byte*)salt, (int)saltlen) != WOLFSSL_SUCCESS
  1084. || wolfSSL_EVP_PKEY_CTX_set1_hkdf_key(
  1085. pctx, (byte*)secret, (int)secretlen) != WOLFSSL_SUCCESS
  1086. || wolfSSL_EVP_PKEY_CTX_add1_hkdf_info(
  1087. pctx, (byte*)info, (int)infolen) != WOLFSSL_SUCCESS
  1088. || wolfSSL_EVP_PKEY_derive(pctx, dest, &destlen) != WOLFSSL_SUCCESS) {
  1089. ret = WOLFSSL_FAILURE;
  1090. goto cleanup;
  1091. }
  1092. cleanup:
  1093. if (pctx)
  1094. EVP_PKEY_CTX_free(pctx);
  1095. WOLFSSL_LEAVE("wolfSSL_quic_hkdf", ret);
  1096. return ret;
  1097. }
  1098. #endif /* OPENSSL_EXTRA */
  1099. WOLFSSL_EVP_CIPHER_CTX* wolfSSL_quic_crypt_new(const WOLFSSL_EVP_CIPHER* cipher,
  1100. const uint8_t* key,
  1101. const uint8_t* iv,
  1102. int encrypt)
  1103. {
  1104. WOLFSSL_EVP_CIPHER_CTX* ctx;
  1105. ctx = wolfSSL_EVP_CIPHER_CTX_new();
  1106. if (ctx == NULL) {
  1107. return NULL;
  1108. }
  1109. if (wolfSSL_EVP_CipherInit(ctx, cipher, key, iv, encrypt)
  1110. != WOLFSSL_SUCCESS) {
  1111. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  1112. return NULL;
  1113. }
  1114. return ctx;
  1115. }
  1116. int wolfSSL_quic_aead_encrypt(uint8_t* dest, WOLFSSL_EVP_CIPHER_CTX* ctx,
  1117. const uint8_t* plain, size_t plainlen,
  1118. const uint8_t* iv, const uint8_t* aad,
  1119. size_t aadlen)
  1120. {
  1121. int len;
  1122. /* A case can be made if this really should be a function in wolfSSL, since
  1123. * the same should be doable from the API by a QUIC protocol stack.
  1124. * What speaks for this:
  1125. * - it gives us a decent testing point
  1126. * - API users do not have to re-invent (it fits into ngtcp2 use).
  1127. * picotls offers a similar abstraction level for AEAD.
  1128. * TODO: there is some fiddling in OpenSSL+quic in regard to CCM ciphers
  1129. * which we need to check.
  1130. */
  1131. if (wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1) != WOLFSSL_SUCCESS
  1132. || wolfSSL_EVP_CipherUpdate(
  1133. ctx, NULL, &len, aad, (int)aadlen) != WOLFSSL_SUCCESS
  1134. || wolfSSL_EVP_CipherUpdate(
  1135. ctx, dest, &len, plain, (int)plainlen) != WOLFSSL_SUCCESS
  1136. || wolfSSL_EVP_CipherFinal(ctx, dest + len, &len) != WOLFSSL_SUCCESS
  1137. || wolfSSL_EVP_CIPHER_CTX_ctrl(
  1138. ctx, EVP_CTRL_AEAD_GET_TAG, ctx->authTagSz, dest + plainlen)
  1139. != WOLFSSL_SUCCESS) {
  1140. return WOLFSSL_FAILURE;
  1141. }
  1142. return WOLFSSL_SUCCESS;
  1143. }
  1144. int wolfSSL_quic_aead_decrypt(uint8_t* dest, WOLFSSL_EVP_CIPHER_CTX* ctx,
  1145. const uint8_t* enc, size_t enclen,
  1146. const uint8_t* iv, const uint8_t* aad,
  1147. size_t aadlen)
  1148. {
  1149. int len;
  1150. const uint8_t* tag;
  1151. /* See rationale for wolfSSL_quic_aead_encrypt() on why this is here */
  1152. if (enclen > INT_MAX || ctx->authTagSz > (int)enclen) {
  1153. return WOLFSSL_FAILURE;
  1154. }
  1155. enclen -= ctx->authTagSz;
  1156. tag = enc + enclen;
  1157. if (wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0) != WOLFSSL_SUCCESS
  1158. || wolfSSL_EVP_CIPHER_CTX_ctrl(
  1159. ctx, EVP_CTRL_AEAD_SET_TAG, ctx->authTagSz, (uint8_t*)tag)
  1160. != WOLFSSL_SUCCESS
  1161. || wolfSSL_EVP_CipherUpdate(ctx, NULL, &len, aad, (int)aadlen)
  1162. != WOLFSSL_SUCCESS
  1163. || wolfSSL_EVP_CipherUpdate(ctx, dest, &len, enc, (int)enclen)
  1164. != WOLFSSL_SUCCESS
  1165. || wolfSSL_EVP_CipherFinal(ctx, dest, &len) != WOLFSSL_SUCCESS) {
  1166. return WOLFSSL_FAILURE;
  1167. }
  1168. return WOLFSSL_SUCCESS;
  1169. }
  1170. #endif /* WOLFSSL_QUIC */
  1171. #endif /* WOLFCRYPT_ONLY */