quic.c 41 KB

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