dtls.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /* dtls.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. /*
  22. * WOLFSSL_DTLS_NO_HVR_ON_RESUME
  23. * WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  24. * If defined, a DTLS server will not do a cookie exchange on successful
  25. * client resumption: the resumption will be faster (one RTT less) and
  26. * will consume less bandwidth (one ClientHello and one
  27. * HelloVerifyRequest/HelloRetryRequest less). On the other hand, if a valid
  28. * SessionID/ticket/psk is collected, forged clientHello messages will
  29. * consume resources on the server. For DTLS 1.3, using this option also
  30. * allows for the server to process Early Data/0-RTT Data. Without this, the
  31. * Early Data would be dropped since the server doesn't enter stateful
  32. * processing until receiving a verified ClientHello with the cookie.
  33. *
  34. * To allow DTLS 1.3 resumption without the cookie exchange:
  35. * - Compile wolfSSL with WOLFSSL_DTLS13_NO_HRR_ON_RESUME defined
  36. * - Call wolfSSL_dtls13_no_hrr_on_resume(ssl, 1) on the WOLFSSL object to
  37. * disable the cookie exchange on resumption
  38. * - Continue like with a normal connection
  39. * WOLFSSL_DTLS_CH_FRAG
  40. * Allow a server to process a fragmented second/verified (one containing a
  41. * valid cookie response) ClientHello message. The first/unverified (one
  42. * without a cookie extension) ClientHello MUST be unfragmented so that the
  43. * DTLS server can process it statelessly. This is only implemented for
  44. * DTLS 1.3. The user MUST call wolfSSL_dtls13_allow_ch_frag() on the server
  45. * to explicitly enable this during runtime.
  46. */
  47. #ifdef HAVE_CONFIG_H
  48. #include <config.h>
  49. #endif
  50. #include <wolfssl/wolfcrypt/settings.h>
  51. #ifndef WOLFCRYPT_ONLY
  52. #include <wolfssl/error-ssl.h>
  53. #include <wolfssl/internal.h>
  54. #include <wolfssl/ssl.h>
  55. #ifdef NO_INLINE
  56. #include <wolfssl/wolfcrypt/misc.h>
  57. #else
  58. #define WOLFSSL_MISC_INCLUDED
  59. #include <wolfcrypt/src/misc.c>
  60. #endif
  61. #define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; }
  62. #ifdef WOLFSSL_DTLS
  63. void DtlsResetState(WOLFSSL* ssl)
  64. {
  65. /* Reset the state so that we can statelessly await the
  66. * ClientHello that contains the cookie. Don't gate on IsAtLeastTLSv1_3
  67. * to handle the edge case when the peer wants a lower version. */
  68. /* Reset DTLS window */
  69. #ifdef WOLFSSL_DTLS13
  70. w64Zero(&ssl->dtls13Epochs[0].nextSeqNumber);
  71. w64Zero(&ssl->dtls13Epochs[0].nextPeerSeqNumber);
  72. XMEMSET(ssl->dtls13Epochs[0].window, 0,
  73. sizeof(ssl->dtls13Epochs[0].window));
  74. Dtls13FreeFsmResources(ssl);
  75. #endif
  76. ssl->keys.dtls_expected_peer_handshake_number = 0;
  77. ssl->keys.dtls_handshake_number = 0;
  78. ssl->keys.dtls_sequence_number_hi = 0;
  79. ssl->keys.dtls_sequence_number_lo = 0;
  80. /* Reset states */
  81. ssl->options.serverState = NULL_STATE;
  82. ssl->options.clientState = NULL_STATE;
  83. ssl->options.connectState = CONNECT_BEGIN;
  84. ssl->options.acceptState = ACCEPT_BEGIN;
  85. ssl->options.handShakeState = NULL_STATE;
  86. ssl->options.seenUnifiedHdr = 0;
  87. ssl->msgsReceived.got_client_hello = 0;
  88. ssl->keys.dtls_handshake_number = 0;
  89. ssl->keys.dtls_expected_peer_handshake_number = 0;
  90. XMEMSET(ssl->keys.peerSeq, 0, sizeof(ssl->keys.peerSeq));
  91. ssl->options.tls = 0;
  92. ssl->options.tls1_1 = 0;
  93. ssl->options.tls1_3 = 0;
  94. }
  95. int DtlsIgnoreError(int err)
  96. {
  97. /* Whitelist of errors not to ignore */
  98. switch (err) {
  99. case MEMORY_E:
  100. case MEMORY_ERROR:
  101. case ASYNC_INIT_E:
  102. case ASYNC_OP_E:
  103. case SOCKET_ERROR_E:
  104. case WANT_READ:
  105. case WANT_WRITE:
  106. case COOKIE_ERROR:
  107. return 0;
  108. default:
  109. return 1;
  110. }
  111. }
  112. void DtlsSetSeqNumForReply(WOLFSSL* ssl)
  113. {
  114. /* We cover both DTLS 1.2 and 1.3 cases because we may be negotiating
  115. * protocols. */
  116. /* We should continue with the same sequence number as the
  117. * Client Hello. */
  118. ssl->keys.dtls_sequence_number_hi = ssl->keys.curSeq_hi;
  119. ssl->keys.dtls_sequence_number_lo = ssl->keys.curSeq_lo;
  120. #ifdef WOLFSSL_DTLS13
  121. if (ssl->dtls13EncryptEpoch != NULL) {
  122. ssl->dtls13EncryptEpoch->nextSeqNumber =
  123. w64From32(ssl->keys.curSeq_hi, ssl->keys.curSeq_lo);
  124. }
  125. #endif
  126. /* We should continue with the same handshake number as the
  127. * Client Hello. */
  128. ssl->keys.dtls_handshake_number =
  129. ssl->keys.dtls_peer_handshake_number;
  130. }
  131. #if !defined(NO_WOLFSSL_SERVER)
  132. #if defined(NO_SHA) && defined(NO_SHA256)
  133. #error "DTLS needs either SHA or SHA-256"
  134. #endif /* NO_SHA && NO_SHA256 */
  135. #if !defined(NO_SHA) && defined(NO_SHA256)
  136. #define DTLS_COOKIE_TYPE WC_SHA
  137. #define DTLS_COOKIE_SZ WC_SHA_DIGEST_SIZE
  138. #endif /* !NO_SHA && NO_SHA256 */
  139. #ifndef NO_SHA256
  140. #define DTLS_COOKIE_TYPE WC_SHA256
  141. #define DTLS_COOKIE_SZ WC_SHA256_DIGEST_SIZE
  142. #endif /* !NO_SHA256 */
  143. #if defined(WOLFSSL_DTLS13) && (defined(HAVE_SESSION_TICKET) || \
  144. !defined(NO_PSK))
  145. typedef struct PskInfo {
  146. byte cipherSuite0;
  147. byte cipherSuite;
  148. byte isValid:1;
  149. } PskInfo;
  150. #endif
  151. typedef struct WolfSSL_ConstVector {
  152. word32 size;
  153. const byte* elements;
  154. } WolfSSL_ConstVector;
  155. typedef struct WolfSSL_CH {
  156. ProtocolVersion* pv;
  157. const byte* random;
  158. WolfSSL_ConstVector sessionId;
  159. WolfSSL_ConstVector cookie;
  160. WolfSSL_ConstVector cipherSuite;
  161. WolfSSL_ConstVector compression;
  162. WolfSSL_ConstVector extension;
  163. WolfSSL_ConstVector cookieExt;
  164. const byte* raw;
  165. word32 length;
  166. /* Store the DTLS 1.2 cookie since we can just compute it once in dtls.c */
  167. byte dtls12cookie[DTLS_COOKIE_SZ];
  168. byte dtls12cookieSet:1;
  169. } WolfSSL_CH;
  170. static int ReadVector8(const byte* input, WolfSSL_ConstVector* v)
  171. {
  172. v->size = *input;
  173. v->elements = input + OPAQUE8_LEN;
  174. return v->size + OPAQUE8_LEN;
  175. }
  176. static int ReadVector16(const byte* input, WolfSSL_ConstVector* v)
  177. {
  178. word16 size16;
  179. ato16(input, &size16);
  180. v->size = (word32)size16;
  181. v->elements = input + OPAQUE16_LEN;
  182. return v->size + OPAQUE16_LEN;
  183. }
  184. static int CreateDtls12Cookie(const WOLFSSL* ssl, const WolfSSL_CH* ch,
  185. byte* cookie)
  186. {
  187. int ret;
  188. Hmac cookieHmac;
  189. if (ssl->buffers.dtlsCookieSecret.buffer == NULL ||
  190. ssl->buffers.dtlsCookieSecret.length == 0) {
  191. WOLFSSL_MSG("Missing DTLS 1.2 cookie secret");
  192. return COOKIE_ERROR;
  193. }
  194. ret = wc_HmacInit(&cookieHmac, ssl->heap, ssl->devId);
  195. if (ret == 0) {
  196. ret = wc_HmacSetKey(&cookieHmac, DTLS_COOKIE_TYPE,
  197. ssl->buffers.dtlsCookieSecret.buffer,
  198. ssl->buffers.dtlsCookieSecret.length);
  199. if (ret == 0) {
  200. ret = wc_HmacUpdate(&cookieHmac,
  201. (const byte*)ssl->buffers.dtlsCtx.peer.sa,
  202. ssl->buffers.dtlsCtx.peer.sz);
  203. }
  204. if (ret == 0)
  205. ret = wc_HmacUpdate(&cookieHmac, (byte*)ch->pv, OPAQUE16_LEN);
  206. if (ret == 0)
  207. ret = wc_HmacUpdate(&cookieHmac, (byte*)ch->random, RAN_LEN);
  208. if (ret == 0) {
  209. ret = wc_HmacUpdate(&cookieHmac, (byte*)ch->sessionId.elements,
  210. ch->sessionId.size);
  211. }
  212. if (ret == 0) {
  213. ret = wc_HmacUpdate(&cookieHmac, (byte*)ch->cipherSuite.elements,
  214. ch->cipherSuite.size);
  215. }
  216. if (ret == 0) {
  217. ret = wc_HmacUpdate(&cookieHmac, (byte*)ch->compression.elements,
  218. ch->compression.size);
  219. }
  220. if (ret == 0)
  221. ret = wc_HmacFinal(&cookieHmac, cookie);
  222. wc_HmacFree(&cookieHmac);
  223. }
  224. return ret;
  225. }
  226. static int CheckDtlsCookie(const WOLFSSL* ssl, WolfSSL_CH* ch,
  227. byte isTls13, byte* cookieGood)
  228. {
  229. int ret = 0;
  230. (void)isTls13;
  231. *cookieGood = 0;
  232. #ifdef WOLFSSL_DTLS13
  233. if (isTls13) {
  234. word16 len;
  235. if (ch->cookieExt.size < OPAQUE16_LEN + 1)
  236. return BUFFER_E;
  237. ato16(ch->cookieExt.elements, &len);
  238. if (ch->cookieExt.size - OPAQUE16_LEN != len)
  239. return BUFFER_E;
  240. ret = TlsCheckCookie(ssl, ch->cookieExt.elements + OPAQUE16_LEN,
  241. (word16)(ch->cookieExt.size - OPAQUE16_LEN));
  242. if (ret < 0 && ret != HRR_COOKIE_ERROR)
  243. return ret;
  244. *cookieGood = ret > 0;
  245. ret = 0;
  246. }
  247. else
  248. #endif
  249. {
  250. if (ch->cookie.size != DTLS_COOKIE_SZ)
  251. return 0;
  252. if (!ch->dtls12cookieSet) {
  253. ret = CreateDtls12Cookie(ssl, ch, ch->dtls12cookie);
  254. if (ret != 0)
  255. return ret;
  256. ch->dtls12cookieSet = 1;
  257. }
  258. *cookieGood = ConstantCompare(ch->cookie.elements, ch->dtls12cookie,
  259. DTLS_COOKIE_SZ) == 0;
  260. }
  261. return ret;
  262. }
  263. static int ParseClientHello(const byte* input, word32 helloSz, WolfSSL_CH* ch,
  264. byte isFirstCHFrag)
  265. {
  266. word32 idx = 0;
  267. (void)isFirstCHFrag;
  268. /* protocol version, random and session id length check */
  269. if (OPAQUE16_LEN + RAN_LEN + OPAQUE8_LEN > helloSz)
  270. return BUFFER_ERROR;
  271. ch->raw = input;
  272. ch->pv = (ProtocolVersion*)(input + idx);
  273. idx += OPAQUE16_LEN;
  274. ch->random = (byte*)(input + idx);
  275. idx += RAN_LEN;
  276. idx += ReadVector8(input + idx, &ch->sessionId);
  277. if (idx > helloSz - OPAQUE8_LEN)
  278. return BUFFER_ERROR;
  279. idx += ReadVector8(input + idx, &ch->cookie);
  280. if (idx > helloSz - OPAQUE16_LEN)
  281. return BUFFER_ERROR;
  282. idx += ReadVector16(input + idx, &ch->cipherSuite);
  283. if (idx > helloSz - OPAQUE8_LEN)
  284. return BUFFER_ERROR;
  285. idx += ReadVector8(input + idx, &ch->compression);
  286. if (idx < helloSz - OPAQUE16_LEN) {
  287. /* Extensions are optional */
  288. #ifdef WOLFSSL_DTLS_CH_FRAG
  289. word32 extStart = idx + OPAQUE16_LEN;
  290. #endif
  291. idx += ReadVector16(input + idx, &ch->extension);
  292. if (idx > helloSz) {
  293. #ifdef WOLFSSL_DTLS_CH_FRAG
  294. idx = helloSz;
  295. /* Allow incomplete extensions if we are parsing a fragment */
  296. if (isFirstCHFrag && extStart < helloSz)
  297. ch->extension.size = helloSz - extStart;
  298. else
  299. #endif
  300. return BUFFER_ERROR;
  301. }
  302. }
  303. if (idx != helloSz)
  304. return BUFFER_ERROR;
  305. ch->length = idx;
  306. return 0;
  307. }
  308. #if (defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME) && defined(HAVE_SESSION_TICKET)) \
  309. || defined(WOLFSSL_DTLS13)
  310. static int FindExtByType(WolfSSL_ConstVector* ret, word16 extType,
  311. WolfSSL_ConstVector exts, int* tlsxFound)
  312. {
  313. word32 len, idx = 0;
  314. word16 type;
  315. WolfSSL_ConstVector ext;
  316. XMEMSET(ret, 0, sizeof(*ret));
  317. len = exts.size;
  318. *tlsxFound = FALSE;
  319. /* type + len */
  320. while (len >= OPAQUE16_LEN + OPAQUE16_LEN) {
  321. ato16(exts.elements + idx, &type);
  322. idx += OPAQUE16_LEN;
  323. idx += ReadVector16(exts.elements + idx, &ext);
  324. if (idx > exts.size)
  325. return BUFFER_ERROR;
  326. if (type == extType) {
  327. XMEMCPY(ret, &ext, sizeof(ext));
  328. *tlsxFound = TRUE;
  329. return 0;
  330. }
  331. len = exts.size - idx;
  332. }
  333. return 0;
  334. }
  335. #endif
  336. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  337. #ifdef HAVE_SESSION_TICKET
  338. static int TlsTicketIsValid(const WOLFSSL* ssl, WolfSSL_ConstVector exts,
  339. int* resume)
  340. {
  341. WolfSSL_ConstVector tlsxSessionTicket;
  342. byte tempTicket[SESSION_TICKET_LEN];
  343. InternalTicket* it = NULL;
  344. int ret = 0;
  345. int tlsxFound;
  346. *resume = FALSE;
  347. ret = FindExtByType(&tlsxSessionTicket, TLSX_SESSION_TICKET, exts,
  348. &tlsxFound);
  349. if (ret != 0)
  350. return ret;
  351. if (tlsxSessionTicket.size == 0)
  352. return 0;
  353. if (tlsxSessionTicket.size > SESSION_TICKET_LEN)
  354. return 0;
  355. XMEMCPY(tempTicket, tlsxSessionTicket.elements, tlsxSessionTicket.size);
  356. ret = DoDecryptTicket(ssl, tempTicket, (word32)tlsxSessionTicket.size, &it);
  357. if (ret == WOLFSSL_TICKET_RET_OK || ret == WOLFSSL_TICKET_RET_CREATE) {
  358. /* This logic is only for TLS <= 1.2 tickets. Don't accept TLS 1.3. */
  359. if (!IsAtLeastTLSv1_3(it->pv))
  360. *resume = TRUE;
  361. }
  362. if (it != NULL)
  363. ForceZero(it, sizeof(InternalTicket));
  364. return 0;
  365. }
  366. #endif /* HAVE_SESSION_TICKET */
  367. static int TlsSessionIdIsValid(const WOLFSSL* ssl, WolfSSL_ConstVector sessionID,
  368. int* resume)
  369. {
  370. const WOLFSSL_SESSION* sess;
  371. word32 sessRow;
  372. int ret;
  373. #ifdef HAVE_EXT_CACHE
  374. int copy;
  375. #endif
  376. *resume = FALSE;
  377. if (ssl->options.sessionCacheOff)
  378. return 0;
  379. if (sessionID.size != ID_LEN)
  380. return 0;
  381. #ifdef HAVE_EXT_CACHE
  382. if (ssl->ctx->get_sess_cb != NULL) {
  383. WOLFSSL_SESSION* extSess =
  384. ssl->ctx->get_sess_cb((WOLFSSL*)ssl, sessionID.elements, ID_LEN,
  385. &copy);
  386. if (extSess != NULL) {
  387. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  388. defined(HAVE_SESSION_TICKET))
  389. /* This logic is only for TLS <= 1.2 tickets. Don't accept
  390. * TLS 1.3. */
  391. if (!IsAtLeastTLSv1_3(extSess->version))
  392. #endif
  393. *resume = TRUE;
  394. if (!copy)
  395. wolfSSL_FreeSession(ssl->ctx, extSess);
  396. if (*resume)
  397. return 0;
  398. }
  399. }
  400. if (ssl->ctx->internalCacheLookupOff)
  401. return 0;
  402. #endif
  403. ret = TlsSessionCacheGetAndRdLock(sessionID.elements, &sess, &sessRow,
  404. ssl->options.side);
  405. if (ret == 0 && sess != NULL) {
  406. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  407. defined(HAVE_SESSION_TICKET))
  408. /* This logic is only for TLS <= 1.2 tickets. Don't accept
  409. * TLS 1.3. */
  410. if (!IsAtLeastTLSv1_3(sess->version))
  411. #endif
  412. *resume = TRUE;
  413. TlsSessionCacheUnlockRow(sessRow);
  414. }
  415. return 0;
  416. }
  417. static int TlsResumptionIsValid(const WOLFSSL* ssl, WolfSSL_CH* ch,
  418. int* resume)
  419. {
  420. int ret;
  421. #ifdef HAVE_SESSION_TICKET
  422. ret = TlsTicketIsValid(ssl, ch->extension, resume);
  423. if (ret != 0)
  424. return ret;
  425. if (*resume)
  426. return 0;
  427. #endif /* HAVE_SESSION_TICKET */
  428. ret = TlsSessionIdIsValid(ssl, ch->sessionId, resume);
  429. return ret;
  430. }
  431. #endif /* WOLFSSL_DTLS13 || WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  432. #ifdef WOLFSSL_DTLS13
  433. static int TlsCheckSupportedVersion(const WOLFSSL* ssl,
  434. WolfSSL_CH* ch, byte *isTls13)
  435. {
  436. WolfSSL_ConstVector tlsxSupportedVersions;
  437. int ret;
  438. ProtocolVersion pv = ssl->version;
  439. int tlsxFound;
  440. ret = FindExtByType(&tlsxSupportedVersions, TLSX_SUPPORTED_VERSIONS,
  441. ch->extension, &tlsxFound);
  442. if (ret != 0)
  443. return ret;
  444. if (!tlsxFound) {
  445. *isTls13 = 0;
  446. return 0;
  447. }
  448. ret = TLSX_SupportedVersions_Parse(ssl, tlsxSupportedVersions.elements,
  449. (word16)tlsxSupportedVersions.size, client_hello, &pv, NULL, NULL);
  450. if (ret != 0)
  451. return ret;
  452. if (IsAtLeastTLSv1_3(pv))
  453. *isTls13 = 1;
  454. else
  455. *isTls13 = 0;
  456. return 0;
  457. }
  458. #endif
  459. #if defined(WOLFSSL_DTLS13) && \
  460. (!defined(NO_PSK) || defined(HAVE_SESSION_TICKET))
  461. /* Very simplified version of CheckPreSharedKeys to find the current suite */
  462. static void FindPskSuiteFromExt(const WOLFSSL* ssl, TLSX* extensions,
  463. PskInfo* pskInfo, Suites* suites)
  464. {
  465. TLSX* pskExt = TLSX_Find(extensions, TLSX_PRE_SHARED_KEY);
  466. PreSharedKey* current;
  467. int i;
  468. int ret;
  469. if (pskExt == NULL)
  470. return;
  471. for (i = 0; i < suites->suiteSz; i += 2) {
  472. for (current = (PreSharedKey*)pskExt->data; current != NULL;
  473. current = current->next) {
  474. #ifdef HAVE_SESSION_TICKET
  475. {
  476. /* Decode the identity. */
  477. switch (current->decryptRet) {
  478. case PSK_DECRYPT_NONE:
  479. ret = DoClientTicket_ex(ssl, current, 0);
  480. break;
  481. case PSK_DECRYPT_OK:
  482. ret = WOLFSSL_TICKET_RET_OK;
  483. break;
  484. case PSK_DECRYPT_CREATE:
  485. ret = WOLFSSL_TICKET_RET_CREATE;
  486. break;
  487. case PSK_DECRYPT_FAIL:
  488. default:
  489. ret = WOLFSSL_TICKET_RET_REJECT;
  490. break;
  491. }
  492. if (ret == WOLFSSL_TICKET_RET_OK) {
  493. if (DoClientTicketCheck(ssl, current, ssl->timeout,
  494. suites->suites + i) != 0) {
  495. continue;
  496. }
  497. pskInfo->cipherSuite0 = current->it->suite[0];
  498. pskInfo->cipherSuite = current->it->suite[1];
  499. pskInfo->isValid = 1;
  500. goto cleanup;
  501. }
  502. }
  503. #endif
  504. #ifndef NO_PSK
  505. {
  506. int found = 0;
  507. byte psk_key[MAX_PSK_KEY_LEN];
  508. word32 psk_keySz;
  509. byte foundSuite[SUITE_LEN];
  510. ret = FindPskSuite(ssl, current, psk_key, &psk_keySz,
  511. suites->suites + i, &found, foundSuite);
  512. /* Clear the key just in case */
  513. ForceZero(psk_key, sizeof(psk_key));
  514. if (ret == 0 && found) {
  515. pskInfo->cipherSuite0 = foundSuite[0];
  516. pskInfo->cipherSuite = foundSuite[1];
  517. pskInfo->isValid = 1;
  518. goto cleanup;
  519. }
  520. }
  521. #endif
  522. }
  523. }
  524. /* Empty return necessary so we can have both the label and macro guard */
  525. cleanup:
  526. #ifdef HAVE_SESSION_TICKET
  527. CleanupClientTickets((PreSharedKey*)pskExt->data);
  528. #endif
  529. return;
  530. }
  531. #endif
  532. #ifdef WOLFSSL_DTLS13
  533. #ifndef WOLFSSL_SEND_HRR_COOKIE
  534. #error "WOLFSSL_SEND_HRR_COOKIE has to be defined to use DTLS 1.3 server"
  535. #endif
  536. #ifdef WOLFSSL_PSK_ONE_ID
  537. #error WOLFSSL_PSK_ONE_ID is not compatible with stateless DTLS 1.3 server. \
  538. wolfSSL needs to be able to make multiple calls for the same PSK.
  539. #endif
  540. static int SendStatelessReplyDtls13(const WOLFSSL* ssl, WolfSSL_CH* ch)
  541. {
  542. int ret = -1;
  543. TLSX* parsedExts = NULL;
  544. WolfSSL_ConstVector tlsx;
  545. int tlsxFound;
  546. Suites suites;
  547. byte haveSA = 0;
  548. byte haveKS = 0;
  549. byte haveSG = 0;
  550. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  551. byte usePSK = 0;
  552. byte doKE = 0;
  553. #endif
  554. CipherSuite cs;
  555. CipherSpecs specs;
  556. byte cookieHash[WC_MAX_DIGEST_SIZE];
  557. int cookieHashSz;
  558. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  559. PskInfo pskInfo;
  560. XMEMSET(&pskInfo, 0, sizeof(pskInfo));
  561. #endif
  562. #ifndef HAVE_SUPPORTED_CURVES
  563. (void)doKE;
  564. #endif /* !HAVE_SUPPORTED_CURVES */
  565. XMEMSET(&cs, 0, sizeof(cs));
  566. /* We need to echo the session ID sent by the client */
  567. if (ch->sessionId.size > ID_LEN) {
  568. /* Too large. We can't echo this. */
  569. ERROR_OUT(INVALID_PARAMETER, dtls13_cleanup);
  570. }
  571. /* Populate the suites struct to find a common ciphersuite */
  572. XMEMSET(&suites, 0, sizeof(suites));
  573. suites.suiteSz = (word16)ch->cipherSuite.size;
  574. if ((suites.suiteSz % 2) != 0)
  575. ERROR_OUT(INVALID_PARAMETER, dtls13_cleanup);
  576. if (suites.suiteSz > WOLFSSL_MAX_SUITE_SZ)
  577. ERROR_OUT(BUFFER_ERROR, dtls13_cleanup);
  578. XMEMCPY(suites.suites, ch->cipherSuite.elements, suites.suiteSz);
  579. /* Populate extensions */
  580. /* Supported versions always need to be present. Has to appear after
  581. * key share as that is the order we reconstruct it in
  582. * RestartHandshakeHashWithCookie. */
  583. ret = TLSX_Push(&parsedExts,
  584. TLSX_SUPPORTED_VERSIONS, ssl, ssl->heap);
  585. if (ret != 0)
  586. goto dtls13_cleanup;
  587. /* Set that this is a response extension */
  588. parsedExts->resp = 1;
  589. #if defined(HAVE_SUPPORTED_CURVES)
  590. ret = TLSX_SupportedCurve_Copy(ssl->extensions, &parsedExts, ssl->heap);
  591. if (ret != 0)
  592. goto dtls13_cleanup;
  593. #endif
  594. #if !defined(NO_CERTS)
  595. /* Signature algs */
  596. ret = FindExtByType(&tlsx, TLSX_SIGNATURE_ALGORITHMS,
  597. ch->extension, &tlsxFound);
  598. if (ret != 0)
  599. goto dtls13_cleanup;
  600. if (tlsxFound) {
  601. WolfSSL_ConstVector sigAlgs;
  602. if (tlsx.size < OPAQUE16_LEN)
  603. ERROR_OUT(BUFFER_ERROR, dtls13_cleanup);
  604. ReadVector16(tlsx.elements, &sigAlgs);
  605. if (sigAlgs.size != tlsx.size - OPAQUE16_LEN)
  606. ERROR_OUT(BUFFER_ERROR, dtls13_cleanup);
  607. if ((sigAlgs.size % 2) != 0)
  608. ERROR_OUT(BUFFER_ERROR, dtls13_cleanup);
  609. suites.hashSigAlgoSz = (word16)sigAlgs.size;
  610. XMEMCPY(suites.hashSigAlgo, sigAlgs.elements, sigAlgs.size);
  611. haveSA = 1;
  612. }
  613. #endif /* !defined(NO_CERTS) */
  614. #ifdef HAVE_SUPPORTED_CURVES
  615. /* Supported groups */
  616. ret = FindExtByType(&tlsx, TLSX_SUPPORTED_GROUPS,
  617. ch->extension, &tlsxFound);
  618. if (ret != 0)
  619. goto dtls13_cleanup;
  620. if (tlsxFound) {
  621. ret = TLSX_SupportedCurve_Parse(ssl, tlsx.elements,
  622. (word16)tlsx.size, 1, &parsedExts);
  623. if (ret != 0)
  624. goto dtls13_cleanup;
  625. haveSG = 1;
  626. }
  627. /* Key share */
  628. ret = FindExtByType(&tlsx, TLSX_KEY_SHARE,
  629. ch->extension, &tlsxFound);
  630. if (ret != 0)
  631. goto dtls13_cleanup;
  632. if (tlsxFound) {
  633. ret = TLSX_KeyShare_Parse_ClientHello(ssl, tlsx.elements,
  634. (word16)tlsx.size, &parsedExts);
  635. if (ret != 0)
  636. goto dtls13_cleanup;
  637. haveKS = 1;
  638. }
  639. #endif /* HAVE_SUPPORTED_CURVES */
  640. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  641. /* Pre-shared key */
  642. ret = FindExtByType(&tlsx, TLSX_PRE_SHARED_KEY, ch->extension, &tlsxFound);
  643. if (ret != 0)
  644. goto dtls13_cleanup;
  645. if (tlsxFound) {
  646. /* Let's just assume that the binders are correct here. We will
  647. * actually verify this in the stateful part of the processing
  648. * and if they don't match we will error out there anyway. */
  649. byte modes;
  650. /* Ask the user for the ciphersuite matching this identity */
  651. if (TLSX_PreSharedKey_Parse_ClientHello(&parsedExts,
  652. tlsx.elements, tlsx.size, ssl->heap) == 0)
  653. FindPskSuiteFromExt(ssl, parsedExts, &pskInfo, &suites);
  654. /* Revert to full handshake if PSK parsing failed */
  655. if (pskInfo.isValid) {
  656. ret = FindExtByType(&tlsx, TLSX_PSK_KEY_EXCHANGE_MODES,
  657. ch->extension, &tlsxFound);
  658. if (ret != 0)
  659. goto dtls13_cleanup;
  660. if (!tlsxFound)
  661. ERROR_OUT(PSK_KEY_ERROR, dtls13_cleanup);
  662. ret = TLSX_PskKeyModes_Parse_Modes(tlsx.elements, tlsx.size,
  663. client_hello, &modes);
  664. if (ret != 0)
  665. goto dtls13_cleanup;
  666. if ((modes & (1 << PSK_DHE_KE)) &&
  667. !ssl->options.noPskDheKe) {
  668. if (!haveKS)
  669. ERROR_OUT(PSK_KEY_ERROR, dtls13_cleanup);
  670. doKE = 1;
  671. }
  672. else if ((modes & (1 << PSK_KE)) == 0) {
  673. ERROR_OUT(PSK_KEY_ERROR, dtls13_cleanup);
  674. }
  675. usePSK = 1;
  676. }
  677. }
  678. #endif
  679. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  680. if (usePSK && pskInfo.isValid) {
  681. cs.cipherSuite0 = pskInfo.cipherSuite0;
  682. cs.cipherSuite = pskInfo.cipherSuite;
  683. /* https://datatracker.ietf.org/doc/html/rfc8446#section-9.2 */
  684. if (haveSG ^ haveKS) {
  685. WOLFSSL_MSG("Client needs to send both or none of KeyShare and "
  686. "SupportedGroups");
  687. ERROR_OUT(INCOMPLETE_DATA, dtls13_cleanup);
  688. }
  689. #ifdef HAVE_SUPPORTED_CURVES
  690. if (doKE) {
  691. byte searched = 0;
  692. ret = TLSX_KeyShare_Choose(ssl, parsedExts, cs.cipherSuite0,
  693. cs.cipherSuite, &cs.clientKSE, &searched);
  694. if (ret != 0)
  695. goto dtls13_cleanup;
  696. if (cs.clientKSE == NULL && searched)
  697. cs.doHelloRetry = 1;
  698. }
  699. #endif /* HAVE_SUPPORTED_CURVES */
  700. }
  701. else
  702. #endif /* defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) */
  703. {
  704. /* https://datatracker.ietf.org/doc/html/rfc8446#section-9.2 */
  705. if (!haveKS || !haveSA || !haveSG) {
  706. WOLFSSL_MSG("Client didn't send KeyShare or SigAlgs or "
  707. "SupportedGroups.");
  708. ERROR_OUT(INCOMPLETE_DATA, dtls13_cleanup);
  709. }
  710. /* TLSX_KeyShare_Choose is done deep inside MatchSuite_ex */
  711. ret = MatchSuite_ex(ssl, &suites, &cs, parsedExts);
  712. if (ret < 0) {
  713. WOLFSSL_MSG("Unsupported cipher suite, ClientHello DTLS 1.3");
  714. ERROR_OUT(INCOMPLETE_DATA, dtls13_cleanup);
  715. }
  716. }
  717. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  718. if (ssl->options.dtls13NoHrrOnResume && usePSK && pskInfo.isValid &&
  719. !cs.doHelloRetry) {
  720. /* Skip HRR on resumption */
  721. ((WOLFSSL*)ssl)->options.dtlsStateful = 1;
  722. goto dtls13_cleanup;
  723. }
  724. #endif
  725. #ifdef HAVE_SUPPORTED_CURVES
  726. if (cs.doHelloRetry) {
  727. ret = TLSX_KeyShare_SetSupported(ssl, &parsedExts);
  728. if (ret != 0)
  729. goto dtls13_cleanup;
  730. }
  731. else {
  732. /* Need to remove the keyshare ext if we found a common group
  733. * and are not doing curve negotiation. */
  734. TLSX_Remove(&parsedExts, TLSX_KEY_SHARE, ssl->heap);
  735. }
  736. #endif /* HAVE_SUPPORTED_CURVES */
  737. /* This is required to correctly generate the hash */
  738. ret = GetCipherSpec(WOLFSSL_SERVER_END, cs.cipherSuite0,
  739. cs.cipherSuite, &specs, NULL);
  740. if (ret != 0)
  741. goto dtls13_cleanup;
  742. /* Calculate the cookie hash */
  743. ret = Dtls13HashClientHello(ssl, cookieHash, &cookieHashSz, ch->raw,
  744. ch->length, &specs);
  745. if (ret != 0)
  746. goto dtls13_cleanup;
  747. /* Push the cookie to extensions */
  748. ret = CreateCookieExt(ssl, cookieHash, (word16)cookieHashSz,
  749. &parsedExts, cs.cipherSuite0, cs.cipherSuite);
  750. if (ret != 0)
  751. goto dtls13_cleanup;
  752. {
  753. WOLFSSL* nonConstSSL = (WOLFSSL*)ssl;
  754. TLSX* sslExts = nonConstSSL->extensions;
  755. if (ret != 0)
  756. goto dtls13_cleanup;
  757. nonConstSSL->options.tls = 1;
  758. nonConstSSL->options.tls1_1 = 1;
  759. nonConstSSL->options.tls1_3 = 1;
  760. XMEMCPY(nonConstSSL->session->sessionID, ch->sessionId.elements,
  761. ch->sessionId.size);
  762. nonConstSSL->session->sessionIDSz = (byte)ch->sessionId.size;
  763. nonConstSSL->options.cipherSuite0 = cs.cipherSuite0;
  764. nonConstSSL->options.cipherSuite = cs.cipherSuite;
  765. nonConstSSL->extensions = parsedExts;
  766. ret = SendTls13ServerHello(nonConstSSL, hello_retry_request);
  767. /* Can be modified inside SendTls13ServerHello */
  768. parsedExts = nonConstSSL->extensions;
  769. nonConstSSL->session->sessionIDSz = 0;
  770. nonConstSSL->options.cipherSuite0 = 0;
  771. nonConstSSL->options.cipherSuite = 0;
  772. nonConstSSL->extensions = sslExts;
  773. nonConstSSL->options.tls = 0;
  774. nonConstSSL->options.tls1_1 = 0;
  775. nonConstSSL->options.tls1_3 = 0;
  776. }
  777. dtls13_cleanup:
  778. TLSX_FreeAll(parsedExts, ssl->heap);
  779. return ret;
  780. }
  781. #endif
  782. static int SendStatelessReply(const WOLFSSL* ssl, WolfSSL_CH* ch, byte isTls13)
  783. {
  784. int ret;
  785. (void)isTls13;
  786. #ifdef WOLFSSL_DTLS13
  787. if (isTls13) {
  788. ret = SendStatelessReplyDtls13(ssl, ch);
  789. }
  790. else
  791. #endif
  792. {
  793. #if !defined(WOLFSSL_NO_TLS12)
  794. if (!ch->dtls12cookieSet) {
  795. ret = CreateDtls12Cookie(ssl, ch, ch->dtls12cookie);
  796. if (ret != 0)
  797. return ret;
  798. ch->dtls12cookieSet = 1;
  799. }
  800. ret = SendHelloVerifyRequest((WOLFSSL*)ssl, ch->dtls12cookie,
  801. DTLS_COOKIE_SZ);
  802. #else
  803. WOLFSSL_MSG("DTLS1.2 disabled with WOLFSSL_NO_TLS12");
  804. WOLFSSL_ERROR_VERBOSE(NOT_COMPILED_IN);
  805. ret = NOT_COMPILED_IN;
  806. #endif
  807. }
  808. return ret;
  809. }
  810. static int ClientHelloSanityCheck(WolfSSL_CH* ch, byte isTls13)
  811. {
  812. /* Do basic checks on the basic fields */
  813. /* Check the protocol version */
  814. if (ch->pv->major != DTLS_MAJOR)
  815. return VERSION_ERROR;
  816. if (ch->pv->minor != DTLSv1_2_MINOR && ch->pv->minor != DTLS_MINOR)
  817. return VERSION_ERROR;
  818. if (isTls13) {
  819. if (ch->cookie.size != 0)
  820. return INVALID_PARAMETER;
  821. if (ch->compression.size != COMP_LEN)
  822. return INVALID_PARAMETER;
  823. if (ch->compression.elements[0] != NO_COMPRESSION)
  824. return INVALID_PARAMETER;
  825. }
  826. return 0;
  827. }
  828. int DoClientHelloStateless(WOLFSSL* ssl, const byte* input, word32 helloSz,
  829. byte isFirstCHFrag, byte* tls13)
  830. {
  831. int ret;
  832. WolfSSL_CH ch;
  833. byte isTls13 = 0;
  834. WOLFSSL_ENTER("DoClientHelloStateless");
  835. if (isFirstCHFrag) {
  836. #ifdef WOLFSSL_DTLS_CH_FRAG
  837. WOLFSSL_MSG("\tProcessing fragmented ClientHello");
  838. #else
  839. WOLFSSL_MSG("\tProcessing fragmented ClientHello but "
  840. "WOLFSSL_DTLS_CH_FRAG is not defined. This should not happen.");
  841. return BAD_STATE_E;
  842. #endif
  843. }
  844. if (tls13 != NULL)
  845. *tls13 = 0;
  846. XMEMSET(&ch, 0, sizeof(ch));
  847. ssl->options.dtlsStateful = 0;
  848. ret = ParseClientHello(input, helloSz, &ch, isFirstCHFrag);
  849. if (ret != 0)
  850. return ret;
  851. #ifdef WOLFSSL_DTLS13
  852. if (IsAtLeastTLSv1_3(ssl->version)) {
  853. ret = TlsCheckSupportedVersion(ssl, &ch, &isTls13);
  854. if (ret != 0)
  855. return ret;
  856. if (tls13 != NULL)
  857. *tls13 = isTls13;
  858. if (isTls13) {
  859. int tlsxFound;
  860. ret = FindExtByType(&ch.cookieExt, TLSX_COOKIE, ch.extension,
  861. &tlsxFound);
  862. if (ret != 0)
  863. return ret;
  864. }
  865. }
  866. #endif
  867. ret = ClientHelloSanityCheck(&ch, isTls13);
  868. if (ret != 0)
  869. return ret;
  870. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  871. if (!isTls13 && !isFirstCHFrag) {
  872. int resume = FALSE;
  873. ret = TlsResumptionIsValid(ssl, &ch, &resume);
  874. if (ret != 0)
  875. return ret;
  876. if (resume) {
  877. ssl->options.dtlsStateful = 1;
  878. return 0;
  879. }
  880. }
  881. #endif
  882. if (ch.cookie.size == 0 && ch.cookieExt.size == 0) {
  883. #ifdef WOLFSSL_DTLS_CH_FRAG
  884. /* Don't send anything here when processing fragment */
  885. if (isFirstCHFrag)
  886. ret = COOKIE_ERROR;
  887. else
  888. #endif
  889. ret = SendStatelessReply(ssl, &ch, isTls13);
  890. }
  891. else {
  892. byte cookieGood;
  893. ret = CheckDtlsCookie(ssl, &ch, isTls13, &cookieGood);
  894. if (ret != 0)
  895. return ret;
  896. if (!cookieGood) {
  897. #ifdef WOLFSSL_DTLS13
  898. /* Invalid cookie for DTLS 1.3 results in an alert. Alert to be sent
  899. * in DoTls13ClientHello. */
  900. if (isTls13)
  901. ret = INVALID_PARAMETER;
  902. else
  903. #endif
  904. #ifdef WOLFSSL_DTLS_CH_FRAG
  905. /* Don't send anything here when processing fragment */
  906. if (isFirstCHFrag)
  907. ret = COOKIE_ERROR;
  908. else
  909. #endif
  910. ret = SendStatelessReply(ssl, &ch, isTls13);
  911. }
  912. else {
  913. ssl->options.dtlsStateful = 1;
  914. /* Update the window now that we enter the stateful parsing */
  915. #ifdef WOLFSSL_DTLS13
  916. if (isTls13)
  917. ret = Dtls13UpdateWindowRecordRecvd(ssl);
  918. else
  919. #endif
  920. DtlsUpdateWindow(ssl);
  921. }
  922. }
  923. return ret;
  924. }
  925. #endif /* !defined(NO_WOLFSSL_SERVER) */
  926. #if defined(WOLFSSL_DTLS_CID)
  927. typedef struct ConnectionID {
  928. byte length;
  929. /* Ignore "nonstandard extension used : zero-sized array in struct/union"
  930. * MSVC warning */
  931. #ifdef _MSC_VER
  932. #pragma warning(disable: 4200)
  933. #endif
  934. byte id[];
  935. } ConnectionID;
  936. typedef struct CIDInfo {
  937. ConnectionID* tx;
  938. ConnectionID* rx;
  939. byte negotiated : 1;
  940. } CIDInfo;
  941. static ConnectionID* DtlsCidNew(const byte* cid, byte size, void* heap)
  942. {
  943. ConnectionID* ret;
  944. ret = (ConnectionID*)XMALLOC(sizeof(ConnectionID) + size, heap,
  945. DYNAMIC_TYPE_TLSX);
  946. if (ret == NULL)
  947. return NULL;
  948. ret->length = size;
  949. XMEMCPY(ret->id, cid, size);
  950. return ret;
  951. }
  952. static WC_INLINE CIDInfo* DtlsCidGetInfo(WOLFSSL* ssl)
  953. {
  954. return ssl->dtlsCidInfo;
  955. }
  956. static int DtlsCidGetSize(WOLFSSL* ssl, unsigned int* size, int rx)
  957. {
  958. ConnectionID* id;
  959. CIDInfo* info;
  960. if (ssl == NULL || size == NULL)
  961. return BAD_FUNC_ARG;
  962. info = DtlsCidGetInfo(ssl);
  963. if (info == NULL)
  964. return WOLFSSL_FAILURE;
  965. id = rx ? info->rx : info->tx;
  966. if (id == NULL) {
  967. *size = 0;
  968. return WOLFSSL_SUCCESS;
  969. }
  970. *size = id->length;
  971. return WOLFSSL_SUCCESS;
  972. }
  973. static int DtlsCidGet(WOLFSSL* ssl, unsigned char* buf, int bufferSz, int rx)
  974. {
  975. ConnectionID* id;
  976. CIDInfo* info;
  977. if (ssl == NULL || buf == NULL)
  978. return BAD_FUNC_ARG;
  979. info = DtlsCidGetInfo(ssl);
  980. if (info == NULL)
  981. return WOLFSSL_FAILURE;
  982. id = rx ? info->rx : info->tx;
  983. if (id == NULL || id->length == 0)
  984. return WOLFSSL_SUCCESS;
  985. if (id->length > bufferSz)
  986. return LENGTH_ERROR;
  987. XMEMCPY(buf, id->id, id->length);
  988. return WOLFSSL_SUCCESS;
  989. }
  990. static CIDInfo* DtlsCidGetInfoFromExt(byte* ext)
  991. {
  992. WOLFSSL** sslPtr;
  993. WOLFSSL* ssl;
  994. if (ext == NULL)
  995. return NULL;
  996. sslPtr = (WOLFSSL**)ext;
  997. ssl = *sslPtr;
  998. if (ssl == NULL)
  999. return NULL;
  1000. return ssl->dtlsCidInfo;
  1001. }
  1002. static void DtlsCidUnsetInfoFromExt(byte* ext)
  1003. {
  1004. WOLFSSL** sslPtr;
  1005. WOLFSSL* ssl;
  1006. if (ext == NULL)
  1007. return;
  1008. sslPtr = (WOLFSSL**)ext;
  1009. ssl = *sslPtr;
  1010. if (ssl == NULL)
  1011. return;
  1012. ssl->dtlsCidInfo = NULL;
  1013. }
  1014. void TLSX_ConnectionID_Free(byte* ext, void* heap)
  1015. {
  1016. CIDInfo* info;
  1017. (void)heap;
  1018. info = DtlsCidGetInfoFromExt(ext);
  1019. if (info == NULL)
  1020. return;
  1021. if (info->rx != NULL)
  1022. XFREE(info->rx, heap, DYNAMIC_TYPE_TLSX);
  1023. if (info->tx != NULL)
  1024. XFREE(info->tx, heap, DYNAMIC_TYPE_TLSX);
  1025. XFREE(info, heap, DYNAMIC_TYPE_TLSX);
  1026. DtlsCidUnsetInfoFromExt(ext);
  1027. XFREE(ext, heap, DYNAMIC_TYPE_TLSX);
  1028. }
  1029. word16 TLSX_ConnectionID_Write(byte* ext, byte* output)
  1030. {
  1031. CIDInfo* info;
  1032. info = DtlsCidGetInfoFromExt(ext);
  1033. if (info == NULL)
  1034. return 0;
  1035. /* empty CID */
  1036. if (info->rx == NULL) {
  1037. *output = 0;
  1038. return OPAQUE8_LEN;
  1039. }
  1040. *output = info->rx->length;
  1041. XMEMCPY(output + OPAQUE8_LEN, info->rx->id, info->rx->length);
  1042. return OPAQUE8_LEN + info->rx->length;
  1043. }
  1044. word16 TLSX_ConnectionID_GetSize(byte* ext)
  1045. {
  1046. CIDInfo* info = DtlsCidGetInfoFromExt(ext);
  1047. if (info == NULL)
  1048. return 0;
  1049. return info->rx == NULL ? OPAQUE8_LEN : OPAQUE8_LEN + info->rx->length;
  1050. }
  1051. int TLSX_ConnectionID_Use(WOLFSSL* ssl)
  1052. {
  1053. CIDInfo* info;
  1054. WOLFSSL** ext;
  1055. int ret;
  1056. ext = (WOLFSSL**)TLSX_Find(ssl->extensions, TLSX_CONNECTION_ID);
  1057. if (ext != NULL)
  1058. return 0;
  1059. info = (CIDInfo*)XMALLOC(sizeof(CIDInfo), ssl->heap, DYNAMIC_TYPE_TLSX);
  1060. if (info == NULL)
  1061. return MEMORY_ERROR;
  1062. ext = (WOLFSSL**)XMALLOC(sizeof(WOLFSSL**), ssl->heap, DYNAMIC_TYPE_TLSX);
  1063. if (ext == NULL) {
  1064. XFREE(info, ssl->heap, DYNAMIC_TYPE_TLSX);
  1065. return MEMORY_ERROR;
  1066. }
  1067. XMEMSET(info, 0, sizeof(CIDInfo));
  1068. /* CIDInfo needs to be accessed every time we send or receive a record. To
  1069. * avoid the cost of the extension lookup save a pointer to the structure
  1070. * inside the SSL object itself, and save a pointer to the SSL object in the
  1071. * extension. The extension freeing routine uses the pointer to the SSL
  1072. * object to find the structure and to set ssl->dtlsCidInfo pointer to NULL
  1073. * after freeing the structure. */
  1074. ssl->dtlsCidInfo = info;
  1075. *ext = ssl;
  1076. ret =
  1077. TLSX_Push(&ssl->extensions, TLSX_CONNECTION_ID, (void*)ext, ssl->heap);
  1078. if (ret != 0) {
  1079. XFREE(info, ssl->heap, DYNAMIC_TYPE_TLSX);
  1080. XFREE(ext, ssl->heap, DYNAMIC_TYPE_TLSX);
  1081. ssl->dtlsCidInfo = NULL;
  1082. return ret;
  1083. }
  1084. return 0;
  1085. }
  1086. int TLSX_ConnectionID_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  1087. byte isRequest)
  1088. {
  1089. ConnectionID* id;
  1090. CIDInfo* info;
  1091. byte cidSize;
  1092. TLSX* ext;
  1093. ext = TLSX_Find(ssl->extensions, TLSX_CONNECTION_ID);
  1094. if (ext == NULL) {
  1095. /* CID not enabled */
  1096. if (isRequest) {
  1097. WOLFSSL_MSG("Received CID ext but it's not enabled, ignoring");
  1098. return 0;
  1099. }
  1100. else {
  1101. WOLFSSL_MSG("CID ext not requested by the Client, aborting");
  1102. return UNSUPPORTED_EXTENSION;
  1103. }
  1104. }
  1105. info = DtlsCidGetInfo(ssl);
  1106. if (info == NULL)
  1107. return BAD_STATE_E;
  1108. /* it may happen if we process two ClientHello because the server sent an
  1109. * HRR request */
  1110. if (info->tx != NULL) {
  1111. if (ssl->options.side != WOLFSSL_SERVER_END &&
  1112. ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE)
  1113. return BAD_STATE_E;
  1114. XFREE(info->tx, ssl->heap, DYNAMIC_TYPE_TLSX);
  1115. info->tx = NULL;
  1116. }
  1117. if (length < OPAQUE8_LEN)
  1118. return BUFFER_ERROR;
  1119. cidSize = *input;
  1120. if (cidSize + OPAQUE8_LEN > length)
  1121. return BUFFER_ERROR;
  1122. if (cidSize > 0) {
  1123. id = (ConnectionID*)XMALLOC(sizeof(*id) + cidSize, ssl->heap,
  1124. DYNAMIC_TYPE_TLSX);
  1125. if (id == NULL)
  1126. return MEMORY_ERROR;
  1127. XMEMCPY(id->id, input + OPAQUE8_LEN, cidSize);
  1128. id->length = cidSize;
  1129. info->tx = id;
  1130. }
  1131. info->negotiated = 1;
  1132. if (isRequest)
  1133. ext->resp = 1;
  1134. return 0;
  1135. }
  1136. void DtlsCIDOnExtensionsParsed(WOLFSSL* ssl)
  1137. {
  1138. CIDInfo* info;
  1139. info = DtlsCidGetInfo(ssl);
  1140. if (info == NULL)
  1141. return;
  1142. if (!info->negotiated) {
  1143. TLSX_Remove(&ssl->extensions, TLSX_CONNECTION_ID, ssl->heap);
  1144. return;
  1145. }
  1146. }
  1147. byte DtlsCIDCheck(WOLFSSL* ssl, const byte* input, word16 inputSize)
  1148. {
  1149. CIDInfo* info;
  1150. info = DtlsCidGetInfo(ssl);
  1151. if (info == NULL || info->rx == NULL || info->rx->length == 0)
  1152. return 0;
  1153. if (inputSize < info->rx->length)
  1154. return 0;
  1155. return XMEMCMP(input, info->rx->id, info->rx->length) == 0;
  1156. }
  1157. int wolfSSL_dtls_cid_use(WOLFSSL* ssl)
  1158. {
  1159. int ret;
  1160. /* CID is supported on DTLSv1.3 only */
  1161. if (!IsAtLeastTLSv1_3(ssl->version))
  1162. return WOLFSSL_FAILURE;
  1163. ssl->options.useDtlsCID = 1;
  1164. ret = TLSX_ConnectionID_Use(ssl);
  1165. if (ret != 0)
  1166. return ret;
  1167. return WOLFSSL_SUCCESS;
  1168. }
  1169. int wolfSSL_dtls_cid_is_enabled(WOLFSSL* ssl)
  1170. {
  1171. return DtlsCidGetInfo(ssl) != NULL;
  1172. }
  1173. int wolfSSL_dtls_cid_set(WOLFSSL* ssl, unsigned char* cid, unsigned int size)
  1174. {
  1175. ConnectionID* newCid;
  1176. CIDInfo* cidInfo;
  1177. if (!ssl->options.useDtlsCID)
  1178. return WOLFSSL_FAILURE;
  1179. cidInfo = DtlsCidGetInfo(ssl);
  1180. if (cidInfo == NULL)
  1181. return WOLFSSL_FAILURE;
  1182. if (cidInfo->rx != NULL) {
  1183. XFREE(cidInfo->rx, ssl->heap, DYNAMIC_TYPE_TLSX);
  1184. cidInfo->rx = NULL;
  1185. }
  1186. /* empty CID */
  1187. if (size == 0)
  1188. return WOLFSSL_SUCCESS;
  1189. if (size > DTLS_CID_MAX_SIZE)
  1190. return LENGTH_ERROR;
  1191. newCid = DtlsCidNew(cid, (byte)size, ssl->heap);
  1192. if (newCid == NULL)
  1193. return MEMORY_ERROR;
  1194. cidInfo->rx = newCid;
  1195. return WOLFSSL_SUCCESS;
  1196. }
  1197. int wolfSSL_dtls_cid_get_rx_size(WOLFSSL* ssl, unsigned int* size)
  1198. {
  1199. return DtlsCidGetSize(ssl, size, 1);
  1200. }
  1201. int wolfSSL_dtls_cid_get_rx(WOLFSSL* ssl, unsigned char* buf,
  1202. unsigned int bufferSz)
  1203. {
  1204. return DtlsCidGet(ssl, buf, bufferSz, 1);
  1205. }
  1206. int wolfSSL_dtls_cid_get_tx_size(WOLFSSL* ssl, unsigned int* size)
  1207. {
  1208. return DtlsCidGetSize(ssl, size, 0);
  1209. }
  1210. int wolfSSL_dtls_cid_get_tx(WOLFSSL* ssl, unsigned char* buf,
  1211. unsigned int bufferSz)
  1212. {
  1213. return DtlsCidGet(ssl, buf, bufferSz, 0);
  1214. }
  1215. #endif /* WOLFSSL_DTLS_CID */
  1216. #endif /* WOLFSSL_DTLS */
  1217. #endif /* WOLFCRYPT_ONLY */