dtls.c 37 KB

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