dtls.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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. /* Extensions are optional */
  261. idx += ReadVector16(input + idx, &ch->extension);
  262. if (idx > helloSz)
  263. return BUFFER_ERROR;
  264. }
  265. if (idx != helloSz)
  266. return BUFFER_ERROR;
  267. ch->length = idx;
  268. return 0;
  269. }
  270. #if (defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME) && defined(HAVE_SESSION_TICKET)) \
  271. || defined(WOLFSSL_DTLS13)
  272. static int FindExtByType(WolfSSL_ConstVector* ret, word16 extType,
  273. WolfSSL_ConstVector exts, int* tlsxFound)
  274. {
  275. word32 len, idx = 0;
  276. word16 type;
  277. WolfSSL_ConstVector ext;
  278. XMEMSET(ret, 0, sizeof(*ret));
  279. len = exts.size;
  280. *tlsxFound = FALSE;
  281. /* type + len */
  282. while (len >= OPAQUE16_LEN + OPAQUE16_LEN) {
  283. ato16(exts.elements + idx, &type);
  284. idx += OPAQUE16_LEN;
  285. idx += ReadVector16(exts.elements + idx, &ext);
  286. if (idx > exts.size)
  287. return BUFFER_ERROR;
  288. if (type == extType) {
  289. XMEMCPY(ret, &ext, sizeof(ext));
  290. *tlsxFound = TRUE;
  291. return 0;
  292. }
  293. len = exts.size - idx;
  294. }
  295. return 0;
  296. }
  297. #endif
  298. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  299. #ifdef HAVE_SESSION_TICKET
  300. static int TlsTicketIsValid(const WOLFSSL* ssl, WolfSSL_ConstVector exts,
  301. int* resume)
  302. {
  303. WolfSSL_ConstVector tlsxSessionTicket;
  304. byte tempTicket[SESSION_TICKET_LEN];
  305. InternalTicket* it = NULL;
  306. int ret = 0;
  307. int tlsxFound;
  308. *resume = FALSE;
  309. ret = FindExtByType(&tlsxSessionTicket, TLSX_SESSION_TICKET, exts,
  310. &tlsxFound);
  311. if (ret != 0)
  312. return ret;
  313. if (tlsxSessionTicket.size == 0)
  314. return 0;
  315. if (tlsxSessionTicket.size > SESSION_TICKET_LEN)
  316. return 0;
  317. XMEMCPY(tempTicket, tlsxSessionTicket.elements, tlsxSessionTicket.size);
  318. ret = DoDecryptTicket(ssl, tempTicket, (word32)tlsxSessionTicket.size, &it);
  319. if (ret == WOLFSSL_TICKET_RET_OK || ret == WOLFSSL_TICKET_RET_CREATE) {
  320. /* This logic is only for TLS <= 1.2 tickets. Don't accept TLS 1.3. */
  321. if (!IsAtLeastTLSv1_3(it->pv))
  322. *resume = TRUE;
  323. }
  324. if (it != NULL)
  325. ForceZero(it, sizeof(InternalTicket));
  326. return 0;
  327. }
  328. #endif /* HAVE_SESSION_TICKET */
  329. static int TlsSessionIdIsValid(const WOLFSSL* ssl, WolfSSL_ConstVector sessionID,
  330. int* resume)
  331. {
  332. const WOLFSSL_SESSION* sess;
  333. word32 sessRow;
  334. int ret;
  335. #ifdef HAVE_EXT_CACHE
  336. int copy;
  337. #endif
  338. *resume = FALSE;
  339. if (ssl->options.sessionCacheOff)
  340. return 0;
  341. if (sessionID.size != ID_LEN)
  342. return 0;
  343. #ifdef HAVE_EXT_CACHE
  344. if (ssl->ctx->get_sess_cb != NULL) {
  345. WOLFSSL_SESSION* extSess =
  346. ssl->ctx->get_sess_cb((WOLFSSL*)ssl, sessionID.elements, ID_LEN,
  347. &copy);
  348. if (extSess != NULL) {
  349. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  350. defined(HAVE_SESSION_TICKET))
  351. /* This logic is only for TLS <= 1.2 tickets. Don't accept
  352. * TLS 1.3. */
  353. if (!IsAtLeastTLSv1_3(extSess->version))
  354. #endif
  355. *resume = TRUE;
  356. if (!copy)
  357. wolfSSL_FreeSession(ssl->ctx, extSess);
  358. if (*resume)
  359. return 0;
  360. }
  361. }
  362. if (ssl->ctx->internalCacheLookupOff)
  363. return 0;
  364. #endif
  365. ret = TlsSessionCacheGetAndRdLock(sessionID.elements, &sess, &sessRow,
  366. ssl->options.side);
  367. if (ret == 0 && sess != NULL) {
  368. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  369. defined(HAVE_SESSION_TICKET))
  370. /* This logic is only for TLS <= 1.2 tickets. Don't accept
  371. * TLS 1.3. */
  372. if (!IsAtLeastTLSv1_3(sess->version))
  373. #endif
  374. *resume = TRUE;
  375. TlsSessionCacheUnlockRow(sessRow);
  376. }
  377. return 0;
  378. }
  379. static int TlsResumptionIsValid(const WOLFSSL* ssl, WolfSSL_CH* ch,
  380. int* resume)
  381. {
  382. int ret;
  383. #ifdef HAVE_SESSION_TICKET
  384. ret = TlsTicketIsValid(ssl, ch->extension, resume);
  385. if (ret != 0)
  386. return ret;
  387. if (*resume)
  388. return 0;
  389. #endif /* HAVE_SESSION_TICKET */
  390. ret = TlsSessionIdIsValid(ssl, ch->sessionId, resume);
  391. return ret;
  392. }
  393. #endif /* WOLFSSL_DTLS13 || WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  394. #ifdef WOLFSSL_DTLS13
  395. static int TlsCheckSupportedVersion(const WOLFSSL* ssl,
  396. WolfSSL_CH* ch, byte *isTls13)
  397. {
  398. WolfSSL_ConstVector tlsxSupportedVersions;
  399. int ret;
  400. ProtocolVersion pv = ssl->version;
  401. int tlsxFound;
  402. ret = FindExtByType(&tlsxSupportedVersions, TLSX_SUPPORTED_VERSIONS,
  403. ch->extension, &tlsxFound);
  404. if (ret != 0)
  405. return ret;
  406. if (!tlsxFound) {
  407. *isTls13 = 0;
  408. return 0;
  409. }
  410. ret = TLSX_SupportedVersions_Parse(ssl, tlsxSupportedVersions.elements,
  411. (word16)tlsxSupportedVersions.size, client_hello, &pv, NULL, NULL);
  412. if (ret != 0)
  413. return ret;
  414. if (IsAtLeastTLSv1_3(pv))
  415. *isTls13 = 1;
  416. else
  417. *isTls13 = 0;
  418. return 0;
  419. }
  420. #endif
  421. #if defined(WOLFSSL_DTLS13) && \
  422. (!defined(NO_PSK) || defined(HAVE_SESSION_TICKET))
  423. /* Very simplified version of CheckPreSharedKeys to find the current suite */
  424. static void FindPskSuiteFromExt(const WOLFSSL* ssl, TLSX* extensions,
  425. PskInfo* pskInfo, Suites* suites)
  426. {
  427. TLSX* pskExt = TLSX_Find(extensions, TLSX_PRE_SHARED_KEY);
  428. PreSharedKey* current;
  429. int i;
  430. int ret;
  431. if (pskExt == NULL)
  432. return;
  433. for (i = 0; i < suites->suiteSz; i += 2) {
  434. for (current = (PreSharedKey*)pskExt->data; current != NULL;
  435. current = current->next) {
  436. #ifdef HAVE_SESSION_TICKET
  437. {
  438. /* Decode the identity. */
  439. switch (current->decryptRet) {
  440. case PSK_DECRYPT_NONE:
  441. ret = DoClientTicket_ex(ssl, current, 0);
  442. break;
  443. case PSK_DECRYPT_OK:
  444. ret = WOLFSSL_TICKET_RET_OK;
  445. break;
  446. case PSK_DECRYPT_CREATE:
  447. ret = WOLFSSL_TICKET_RET_CREATE;
  448. break;
  449. case PSK_DECRYPT_FAIL:
  450. default:
  451. ret = WOLFSSL_TICKET_RET_REJECT;
  452. break;
  453. }
  454. if (ret == WOLFSSL_TICKET_RET_OK) {
  455. if (DoClientTicketCheck(ssl, current, ssl->timeout,
  456. suites->suites + i) != 0) {
  457. continue;
  458. }
  459. pskInfo->cipherSuite0 = current->it->suite[0];
  460. pskInfo->cipherSuite = current->it->suite[1];
  461. pskInfo->isValid = 1;
  462. goto cleanup;
  463. }
  464. }
  465. #endif
  466. #ifndef NO_PSK
  467. {
  468. int found = 0;
  469. byte psk_key[MAX_PSK_KEY_LEN];
  470. word32 psk_keySz;
  471. byte foundSuite[SUITE_LEN];
  472. ret = FindPskSuite(ssl, current, psk_key, &psk_keySz,
  473. suites->suites + i, &found, foundSuite);
  474. /* Clear the key just in case */
  475. ForceZero(psk_key, sizeof(psk_key));
  476. if (ret == 0 && found) {
  477. pskInfo->cipherSuite0 = foundSuite[0];
  478. pskInfo->cipherSuite = foundSuite[1];
  479. pskInfo->isValid = 1;
  480. goto cleanup;
  481. }
  482. }
  483. #endif
  484. }
  485. }
  486. /* Empty return necessary so we can have both the label and macro guard */
  487. cleanup:
  488. #ifdef HAVE_SESSION_TICKET
  489. CleanupClientTickets((PreSharedKey*)pskExt->data);
  490. #endif
  491. return;
  492. }
  493. #endif
  494. #ifdef WOLFSSL_DTLS13
  495. #ifndef WOLFSSL_SEND_HRR_COOKIE
  496. #error "WOLFSSL_SEND_HRR_COOKIE has to be defined to use DTLS 1.3 server"
  497. #endif
  498. #ifdef WOLFSSL_PSK_ONE_ID
  499. #error WOLFSSL_PSK_ONE_ID is not compatible with stateless DTLS 1.3 server. \
  500. wolfSSL needs to be able to make multiple calls for the same PSK.
  501. #endif
  502. static int SendStatelessReplyDtls13(const WOLFSSL* ssl, WolfSSL_CH* ch)
  503. {
  504. int ret = -1;
  505. TLSX* parsedExts = NULL;
  506. WolfSSL_ConstVector tlsx;
  507. int tlsxFound;
  508. Suites suites;
  509. byte haveSA = 0;
  510. byte haveKS = 0;
  511. byte haveSG = 0;
  512. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  513. byte usePSK = 0;
  514. byte doKE = 0;
  515. #endif
  516. CipherSuite cs;
  517. CipherSpecs specs;
  518. byte cookieHash[WC_MAX_DIGEST_SIZE];
  519. int cookieHashSz;
  520. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  521. PskInfo pskInfo;
  522. XMEMSET(&pskInfo, 0, sizeof(pskInfo));
  523. #endif
  524. #ifndef HAVE_SUPPORTED_CURVES
  525. (void)doKE;
  526. #endif /* !HAVE_SUPPORTED_CURVES */
  527. XMEMSET(&cs, 0, sizeof(cs));
  528. /* We need to echo the session ID sent by the client */
  529. if (ch->sessionId.size > ID_LEN) {
  530. /* Too large. We can't echo this. */
  531. ERROR_OUT(INVALID_PARAMETER, dtls13_cleanup);
  532. }
  533. /* Populate the suites struct to find a common ciphersuite */
  534. XMEMSET(&suites, 0, sizeof(suites));
  535. suites.suiteSz = (word16)ch->cipherSuite.size;
  536. if ((suites.suiteSz % 2) != 0)
  537. ERROR_OUT(INVALID_PARAMETER, dtls13_cleanup);
  538. if (suites.suiteSz > WOLFSSL_MAX_SUITE_SZ)
  539. ERROR_OUT(BUFFER_ERROR, dtls13_cleanup);
  540. XMEMCPY(suites.suites, ch->cipherSuite.elements, suites.suiteSz);
  541. /* Populate extensions */
  542. /* Supported versions always need to be present. Has to appear after
  543. * key share as that is the order we reconstruct it in
  544. * RestartHandshakeHashWithCookie. */
  545. ret = TLSX_Push(&parsedExts,
  546. TLSX_SUPPORTED_VERSIONS, ssl, ssl->heap);
  547. if (ret != 0)
  548. goto dtls13_cleanup;
  549. /* Set that this is a response extension */
  550. parsedExts->resp = 1;
  551. #if defined(HAVE_SUPPORTED_CURVES)
  552. ret = TLSX_SupportedCurve_Copy(ssl->extensions, &parsedExts, ssl->heap);
  553. if (ret != 0)
  554. goto dtls13_cleanup;
  555. #endif
  556. #if !defined(NO_CERTS)
  557. /* Signature algs */
  558. ret = FindExtByType(&tlsx, TLSX_SIGNATURE_ALGORITHMS,
  559. ch->extension, &tlsxFound);
  560. if (ret != 0)
  561. goto dtls13_cleanup;
  562. if (tlsxFound) {
  563. WolfSSL_ConstVector sigAlgs;
  564. if (tlsx.size < OPAQUE16_LEN)
  565. ERROR_OUT(BUFFER_ERROR, dtls13_cleanup);
  566. ReadVector16(tlsx.elements, &sigAlgs);
  567. if (sigAlgs.size != tlsx.size - OPAQUE16_LEN)
  568. ERROR_OUT(BUFFER_ERROR, dtls13_cleanup);
  569. if ((sigAlgs.size % 2) != 0)
  570. ERROR_OUT(BUFFER_ERROR, dtls13_cleanup);
  571. suites.hashSigAlgoSz = (word16)sigAlgs.size;
  572. XMEMCPY(suites.hashSigAlgo, sigAlgs.elements, sigAlgs.size);
  573. haveSA = 1;
  574. }
  575. #endif /* !defined(NO_CERTS) */
  576. #ifdef HAVE_SUPPORTED_CURVES
  577. /* Supported groups */
  578. ret = FindExtByType(&tlsx, TLSX_SUPPORTED_GROUPS,
  579. ch->extension, &tlsxFound);
  580. if (ret != 0)
  581. goto dtls13_cleanup;
  582. if (tlsxFound) {
  583. ret = TLSX_SupportedCurve_Parse(ssl, tlsx.elements,
  584. (word16)tlsx.size, 1, &parsedExts);
  585. if (ret != 0)
  586. goto dtls13_cleanup;
  587. haveSG = 1;
  588. }
  589. /* Key share */
  590. ret = FindExtByType(&tlsx, TLSX_KEY_SHARE,
  591. ch->extension, &tlsxFound);
  592. if (ret != 0)
  593. goto dtls13_cleanup;
  594. if (tlsxFound) {
  595. ret = TLSX_KeyShare_Parse_ClientHello(ssl, tlsx.elements,
  596. (word16)tlsx.size, &parsedExts);
  597. if (ret != 0)
  598. goto dtls13_cleanup;
  599. haveKS = 1;
  600. }
  601. #endif /* HAVE_SUPPORTED_CURVES */
  602. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  603. /* Pre-shared key */
  604. ret = FindExtByType(&tlsx, TLSX_PRE_SHARED_KEY, ch->extension, &tlsxFound);
  605. if (ret != 0)
  606. goto dtls13_cleanup;
  607. if (tlsxFound) {
  608. /* Let's just assume that the binders are correct here. We will
  609. * actually verify this in the stateful part of the processing
  610. * and if they don't match we will error out there anyway. */
  611. byte modes;
  612. /* Ask the user for the ciphersuite matching this identity */
  613. if (TLSX_PreSharedKey_Parse_ClientHello(&parsedExts,
  614. tlsx.elements, tlsx.size, ssl->heap) == 0)
  615. FindPskSuiteFromExt(ssl, parsedExts, &pskInfo, &suites);
  616. /* Revert to full handshake if PSK parsing failed */
  617. if (pskInfo.isValid) {
  618. ret = FindExtByType(&tlsx, TLSX_PSK_KEY_EXCHANGE_MODES,
  619. ch->extension, &tlsxFound);
  620. if (ret != 0)
  621. goto dtls13_cleanup;
  622. if (!tlsxFound)
  623. ERROR_OUT(PSK_KEY_ERROR, dtls13_cleanup);
  624. ret = TLSX_PskKeyModes_Parse_Modes(tlsx.elements, tlsx.size,
  625. client_hello, &modes);
  626. if (ret != 0)
  627. goto dtls13_cleanup;
  628. if ((modes & (1 << PSK_DHE_KE)) &&
  629. !ssl->options.noPskDheKe) {
  630. if (!haveKS)
  631. ERROR_OUT(PSK_KEY_ERROR, dtls13_cleanup);
  632. doKE = 1;
  633. }
  634. else if ((modes & (1 << PSK_KE)) == 0) {
  635. ERROR_OUT(PSK_KEY_ERROR, dtls13_cleanup);
  636. }
  637. usePSK = 1;
  638. }
  639. }
  640. #endif
  641. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  642. if (usePSK && pskInfo.isValid) {
  643. cs.cipherSuite0 = pskInfo.cipherSuite0;
  644. cs.cipherSuite = pskInfo.cipherSuite;
  645. /* https://datatracker.ietf.org/doc/html/rfc8446#section-9.2 */
  646. if (haveSG ^ haveKS) {
  647. WOLFSSL_MSG("Client needs to send both or none of KeyShare and "
  648. "SupportedGroups");
  649. ERROR_OUT(INCOMPLETE_DATA, dtls13_cleanup);
  650. }
  651. #ifdef HAVE_SUPPORTED_CURVES
  652. if (doKE) {
  653. byte searched = 0;
  654. ret = TLSX_KeyShare_Choose(ssl, parsedExts, cs.cipherSuite0,
  655. cs.cipherSuite, &cs.clientKSE, &searched);
  656. if (ret != 0)
  657. goto dtls13_cleanup;
  658. if (cs.clientKSE == NULL && searched)
  659. cs.doHelloRetry = 1;
  660. }
  661. #endif /* HAVE_SUPPORTED_CURVES */
  662. }
  663. else
  664. #endif /* defined(HAVE_SESSION_TICKET) || !defined(NO_PSK) */
  665. {
  666. /* https://datatracker.ietf.org/doc/html/rfc8446#section-9.2 */
  667. if (!haveKS || !haveSA || !haveSG) {
  668. WOLFSSL_MSG("Client didn't send KeyShare or SigAlgs or "
  669. "SupportedGroups.");
  670. ERROR_OUT(INCOMPLETE_DATA, dtls13_cleanup);
  671. }
  672. /* TLSX_KeyShare_Choose is done deep inside MatchSuite_ex */
  673. ret = MatchSuite_ex(ssl, &suites, &cs, parsedExts);
  674. if (ret < 0) {
  675. WOLFSSL_MSG("Unsupported cipher suite, ClientHello DTLS 1.3");
  676. ERROR_OUT(INCOMPLETE_DATA, dtls13_cleanup);
  677. }
  678. }
  679. #ifdef HAVE_SUPPORTED_CURVES
  680. if (cs.doHelloRetry) {
  681. ret = TLSX_KeyShare_SetSupported(ssl, &parsedExts);
  682. if (ret != 0)
  683. goto dtls13_cleanup;
  684. }
  685. else {
  686. /* Need to remove the keyshare ext if we found a common group
  687. * and are not doing curve negotiation. */
  688. TLSX_Remove(&parsedExts, TLSX_KEY_SHARE, ssl->heap);
  689. }
  690. #endif /* HAVE_SUPPORTED_CURVES */
  691. /* This is required to correctly generate the hash */
  692. ret = GetCipherSpec(WOLFSSL_SERVER_END, cs.cipherSuite0,
  693. cs.cipherSuite, &specs, NULL);
  694. if (ret != 0)
  695. goto dtls13_cleanup;
  696. /* Calculate the cookie hash */
  697. ret = Dtls13HashClientHello(ssl, cookieHash, &cookieHashSz, ch->raw,
  698. ch->length, &specs);
  699. if (ret != 0)
  700. goto dtls13_cleanup;
  701. /* Push the cookie to extensions */
  702. ret = CreateCookieExt(ssl, cookieHash, (word16)cookieHashSz,
  703. &parsedExts, cs.cipherSuite0, cs.cipherSuite);
  704. if (ret != 0)
  705. goto dtls13_cleanup;
  706. {
  707. WOLFSSL* nonConstSSL = (WOLFSSL*)ssl;
  708. TLSX* sslExts = nonConstSSL->extensions;
  709. if (ret != 0)
  710. goto dtls13_cleanup;
  711. nonConstSSL->options.tls = 1;
  712. nonConstSSL->options.tls1_1 = 1;
  713. nonConstSSL->options.tls1_3 = 1;
  714. XMEMCPY(nonConstSSL->session->sessionID, ch->sessionId.elements,
  715. ch->sessionId.size);
  716. nonConstSSL->session->sessionIDSz = (byte)ch->sessionId.size;
  717. nonConstSSL->options.cipherSuite0 = cs.cipherSuite0;
  718. nonConstSSL->options.cipherSuite = cs.cipherSuite;
  719. nonConstSSL->extensions = parsedExts;
  720. ret = SendTls13ServerHello(nonConstSSL, hello_retry_request);
  721. /* Can be modified inside SendTls13ServerHello */
  722. parsedExts = nonConstSSL->extensions;
  723. nonConstSSL->session->sessionIDSz = 0;
  724. nonConstSSL->options.cipherSuite0 = 0;
  725. nonConstSSL->options.cipherSuite = 0;
  726. nonConstSSL->extensions = sslExts;
  727. nonConstSSL->options.tls = 0;
  728. nonConstSSL->options.tls1_1 = 0;
  729. nonConstSSL->options.tls1_3 = 0;
  730. }
  731. dtls13_cleanup:
  732. TLSX_FreeAll(parsedExts, ssl->heap);
  733. return ret;
  734. }
  735. #endif
  736. static int SendStatelessReply(const WOLFSSL* ssl, WolfSSL_CH* ch, byte isTls13)
  737. {
  738. int ret;
  739. (void)isTls13;
  740. #ifdef WOLFSSL_DTLS13
  741. if (isTls13) {
  742. ret = SendStatelessReplyDtls13(ssl, ch);
  743. }
  744. else
  745. #endif
  746. {
  747. if (!ch->dtls12cookieSet) {
  748. ret = CreateDtls12Cookie(ssl, ch, ch->dtls12cookie);
  749. if (ret != 0)
  750. return ret;
  751. ch->dtls12cookieSet = 1;
  752. }
  753. ret = SendHelloVerifyRequest((WOLFSSL*)ssl, ch->dtls12cookie,
  754. DTLS_COOKIE_SZ);
  755. }
  756. return ret;
  757. }
  758. static int ClientHelloSanityCheck(WolfSSL_CH* ch, byte isTls13)
  759. {
  760. /* Do basic checks on the basic fields */
  761. /* Check the protocol version */
  762. if (ch->pv->major != DTLS_MAJOR)
  763. return VERSION_ERROR;
  764. if (ch->pv->minor != DTLSv1_2_MINOR && ch->pv->minor != DTLS_MINOR)
  765. return VERSION_ERROR;
  766. if (isTls13) {
  767. if (ch->cookie.size != 0)
  768. return INVALID_PARAMETER;
  769. if (ch->compression.size != COMP_LEN)
  770. return INVALID_PARAMETER;
  771. if (ch->compression.elements[0] != NO_COMPRESSION)
  772. return INVALID_PARAMETER;
  773. }
  774. return 0;
  775. }
  776. int DoClientHelloStateless(WOLFSSL* ssl, const byte* input,
  777. word32* inOutIdx, word32 helloSz)
  778. {
  779. int ret;
  780. WolfSSL_CH ch;
  781. byte isTls13 = 0;
  782. XMEMSET(&ch, 0, sizeof(ch));
  783. ssl->options.dtlsStateful = 0;
  784. ret = ParseClientHello(input + *inOutIdx, helloSz, &ch);
  785. if (ret != 0)
  786. return ret;
  787. #ifdef WOLFSSL_DTLS13
  788. if (IsAtLeastTLSv1_3(ssl->version)) {
  789. ret = TlsCheckSupportedVersion(ssl, &ch, &isTls13);
  790. if (ret != 0)
  791. return ret;
  792. if (isTls13) {
  793. int tlsxFound;
  794. ret = FindExtByType(&ch.cookieExt, TLSX_COOKIE, ch.extension,
  795. &tlsxFound);
  796. if (ret != 0)
  797. return ret;
  798. }
  799. }
  800. #endif
  801. ret = ClientHelloSanityCheck(&ch, isTls13);
  802. if (ret != 0)
  803. return ret;
  804. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  805. if (!isTls13) {
  806. int resume = FALSE;
  807. ret = TlsResumptionIsValid(ssl, &ch, &resume);
  808. if (ret != 0)
  809. return ret;
  810. if (resume) {
  811. ssl->options.dtlsStateful = 1;
  812. return 0;
  813. }
  814. }
  815. #endif
  816. if (ch.cookie.size == 0 && ch.cookieExt.size == 0) {
  817. ret = SendStatelessReply((WOLFSSL*)ssl, &ch, isTls13);
  818. }
  819. else {
  820. byte cookieGood;
  821. ret = CheckDtlsCookie(ssl, &ch, isTls13, &cookieGood);
  822. if (ret != 0)
  823. return ret;
  824. if (!cookieGood) {
  825. #ifdef WOLFSSL_DTLS13
  826. /* Invalid cookie for DTLS 1.3 results in an alert. Alert to be sent
  827. * in DoTls13ClientHello. */
  828. if (isTls13)
  829. ret = INVALID_PARAMETER;
  830. else
  831. #endif
  832. ret = SendStatelessReply((WOLFSSL*)ssl, &ch, isTls13);
  833. }
  834. else
  835. ssl->options.dtlsStateful = 1;
  836. }
  837. return ret;
  838. }
  839. #endif /* !defined(NO_WOLFSSL_SERVER) */
  840. #if defined(WOLFSSL_DTLS_CID)
  841. typedef struct ConnectionID {
  842. byte length;
  843. /* Ignore "nonstandard extension used : zero-sized array in struct/union"
  844. * MSVC warning */
  845. #ifdef _MSC_VER
  846. #pragma warning(disable: 4200)
  847. #endif
  848. byte id[];
  849. } ConnectionID;
  850. typedef struct CIDInfo {
  851. ConnectionID* tx;
  852. ConnectionID* rx;
  853. byte negotiated : 1;
  854. } CIDInfo;
  855. static ConnectionID* DtlsCidNew(const byte* cid, byte size, void* heap)
  856. {
  857. ConnectionID* ret;
  858. ret = (ConnectionID*)XMALLOC(sizeof(ConnectionID) + size, heap,
  859. DYNAMIC_TYPE_TLSX);
  860. if (ret == NULL)
  861. return NULL;
  862. ret->length = size;
  863. XMEMCPY(ret->id, cid, size);
  864. return ret;
  865. }
  866. static WC_INLINE CIDInfo* DtlsCidGetInfo(WOLFSSL* ssl)
  867. {
  868. return ssl->dtlsCidInfo;
  869. }
  870. static int DtlsCidGetSize(WOLFSSL* ssl, unsigned int* size, int rx)
  871. {
  872. ConnectionID* id;
  873. CIDInfo* info;
  874. if (ssl == NULL || size == NULL)
  875. return BAD_FUNC_ARG;
  876. info = DtlsCidGetInfo(ssl);
  877. if (info == NULL)
  878. return WOLFSSL_FAILURE;
  879. id = rx ? info->rx : info->tx;
  880. if (id == NULL) {
  881. *size = 0;
  882. return WOLFSSL_SUCCESS;
  883. }
  884. *size = id->length;
  885. return WOLFSSL_SUCCESS;
  886. }
  887. static int DtlsCidGet(WOLFSSL* ssl, unsigned char* buf, int bufferSz, int rx)
  888. {
  889. ConnectionID* id;
  890. CIDInfo* info;
  891. if (ssl == NULL || buf == NULL)
  892. return BAD_FUNC_ARG;
  893. info = DtlsCidGetInfo(ssl);
  894. if (info == NULL)
  895. return WOLFSSL_FAILURE;
  896. id = rx ? info->rx : info->tx;
  897. if (id == NULL || id->length == 0)
  898. return WOLFSSL_SUCCESS;
  899. if (id->length > bufferSz)
  900. return LENGTH_ERROR;
  901. XMEMCPY(buf, id->id, id->length);
  902. return WOLFSSL_SUCCESS;
  903. }
  904. static CIDInfo* DtlsCidGetInfoFromExt(byte* ext)
  905. {
  906. WOLFSSL** sslPtr;
  907. WOLFSSL* ssl;
  908. if (ext == NULL)
  909. return NULL;
  910. sslPtr = (WOLFSSL**)ext;
  911. ssl = *sslPtr;
  912. if (ssl == NULL)
  913. return NULL;
  914. return ssl->dtlsCidInfo;
  915. }
  916. static void DtlsCidUnsetInfoFromExt(byte* ext)
  917. {
  918. WOLFSSL** sslPtr;
  919. WOLFSSL* ssl;
  920. if (ext == NULL)
  921. return;
  922. sslPtr = (WOLFSSL**)ext;
  923. ssl = *sslPtr;
  924. if (ssl == NULL)
  925. return;
  926. ssl->dtlsCidInfo = NULL;
  927. }
  928. void TLSX_ConnectionID_Free(byte* ext, void* heap)
  929. {
  930. CIDInfo* info;
  931. (void)heap;
  932. info = DtlsCidGetInfoFromExt(ext);
  933. if (info == NULL)
  934. return;
  935. if (info->rx != NULL)
  936. XFREE(info->rx, heap, DYNAMIC_TYPE_TLSX);
  937. if (info->tx != NULL)
  938. XFREE(info->tx, heap, DYNAMIC_TYPE_TLSX);
  939. XFREE(info, heap, DYNAMIC_TYPE_TLSX);
  940. DtlsCidUnsetInfoFromExt(ext);
  941. XFREE(ext, heap, DYNAMIC_TYPE_TLSX);
  942. }
  943. word16 TLSX_ConnectionID_Write(byte* ext, byte* output)
  944. {
  945. CIDInfo* info;
  946. info = DtlsCidGetInfoFromExt(ext);
  947. if (info == NULL)
  948. return 0;
  949. /* empty CID */
  950. if (info->rx == NULL) {
  951. *output = 0;
  952. return OPAQUE8_LEN;
  953. }
  954. *output = info->rx->length;
  955. XMEMCPY(output + OPAQUE8_LEN, info->rx->id, info->rx->length);
  956. return OPAQUE8_LEN + info->rx->length;
  957. }
  958. word16 TLSX_ConnectionID_GetSize(byte* ext)
  959. {
  960. CIDInfo* info = DtlsCidGetInfoFromExt(ext);
  961. if (info == NULL)
  962. return 0;
  963. return info->rx == NULL ? OPAQUE8_LEN : OPAQUE8_LEN + info->rx->length;
  964. }
  965. int TLSX_ConnectionID_Use(WOLFSSL* ssl)
  966. {
  967. CIDInfo* info;
  968. WOLFSSL** ext;
  969. int ret;
  970. ext = (WOLFSSL**)TLSX_Find(ssl->extensions, TLSX_CONNECTION_ID);
  971. if (ext != NULL)
  972. return 0;
  973. info = (CIDInfo*)XMALLOC(sizeof(CIDInfo), ssl->heap, DYNAMIC_TYPE_TLSX);
  974. if (info == NULL)
  975. return MEMORY_ERROR;
  976. ext = (WOLFSSL**)XMALLOC(sizeof(WOLFSSL**), ssl->heap, DYNAMIC_TYPE_TLSX);
  977. if (ext == NULL) {
  978. XFREE(info, ssl->heap, DYNAMIC_TYPE_TLSX);
  979. return MEMORY_ERROR;
  980. }
  981. XMEMSET(info, 0, sizeof(CIDInfo));
  982. /* CIDInfo needs to be accessed every time we send or receive a record. To
  983. * avoid the cost of the extension lookup save a pointer to the structure
  984. * inside the SSL object itself, and save a pointer to the SSL object in the
  985. * extension. The extension freeing routine uses the pointer to the SSL
  986. * object to find the structure and to set ssl->dtlsCidInfo pointer to NULL
  987. * after freeing the structure. */
  988. ssl->dtlsCidInfo = info;
  989. *ext = ssl;
  990. ret =
  991. TLSX_Push(&ssl->extensions, TLSX_CONNECTION_ID, (void*)ext, ssl->heap);
  992. if (ret != 0) {
  993. XFREE(info, ssl->heap, DYNAMIC_TYPE_TLSX);
  994. XFREE(ext, ssl->heap, DYNAMIC_TYPE_TLSX);
  995. ssl->dtlsCidInfo = NULL;
  996. return ret;
  997. }
  998. return 0;
  999. }
  1000. int TLSX_ConnectionID_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  1001. byte isRequest)
  1002. {
  1003. ConnectionID* id;
  1004. CIDInfo* info;
  1005. byte cidSize;
  1006. TLSX* ext;
  1007. ext = TLSX_Find(ssl->extensions, TLSX_CONNECTION_ID);
  1008. if (ext == NULL) {
  1009. /* CID not enabled */
  1010. if (isRequest) {
  1011. WOLFSSL_MSG("Received CID ext but it's not enabled, ignoring");
  1012. return 0;
  1013. }
  1014. else {
  1015. WOLFSSL_MSG("CID ext not requested by the Client, aborting");
  1016. return UNSUPPORTED_EXTENSION;
  1017. }
  1018. }
  1019. info = DtlsCidGetInfo(ssl);
  1020. if (info == NULL)
  1021. return BAD_STATE_E;
  1022. /* it may happen if we process two ClientHello because the server sent an
  1023. * HRR request */
  1024. if (info->tx != NULL) {
  1025. if (ssl->options.side != WOLFSSL_SERVER_END &&
  1026. ssl->options.serverState != SERVER_HELLO_RETRY_REQUEST_COMPLETE)
  1027. return BAD_STATE_E;
  1028. XFREE(info->tx, ssl->heap, DYNAMIC_TYPE_TLSX);
  1029. info->tx = NULL;
  1030. }
  1031. if (length < OPAQUE8_LEN)
  1032. return BUFFER_ERROR;
  1033. cidSize = *input;
  1034. if (cidSize + OPAQUE8_LEN > length)
  1035. return BUFFER_ERROR;
  1036. if (cidSize > 0) {
  1037. id = (ConnectionID*)XMALLOC(sizeof(*id) + cidSize, ssl->heap,
  1038. DYNAMIC_TYPE_TLSX);
  1039. if (id == NULL)
  1040. return MEMORY_ERROR;
  1041. XMEMCPY(id->id, input + OPAQUE8_LEN, cidSize);
  1042. id->length = cidSize;
  1043. info->tx = id;
  1044. }
  1045. info->negotiated = 1;
  1046. if (isRequest)
  1047. ext->resp = 1;
  1048. return 0;
  1049. }
  1050. void DtlsCIDOnExtensionsParsed(WOLFSSL* ssl)
  1051. {
  1052. CIDInfo* info;
  1053. info = DtlsCidGetInfo(ssl);
  1054. if (info == NULL)
  1055. return;
  1056. if (!info->negotiated) {
  1057. TLSX_Remove(&ssl->extensions, TLSX_CONNECTION_ID, ssl->heap);
  1058. return;
  1059. }
  1060. }
  1061. byte DtlsCIDCheck(WOLFSSL* ssl, const byte* input, word16 inputSize)
  1062. {
  1063. CIDInfo* info;
  1064. info = DtlsCidGetInfo(ssl);
  1065. if (info == NULL || info->rx == NULL || info->rx->length == 0)
  1066. return 0;
  1067. if (inputSize < info->rx->length)
  1068. return 0;
  1069. return XMEMCMP(input, info->rx->id, info->rx->length) == 0;
  1070. }
  1071. int wolfSSL_dtls_cid_use(WOLFSSL* ssl)
  1072. {
  1073. int ret;
  1074. /* CID is supported on DTLSv1.3 only */
  1075. if (!IsAtLeastTLSv1_3(ssl->version))
  1076. return WOLFSSL_FAILURE;
  1077. ssl->options.useDtlsCID = 1;
  1078. ret = TLSX_ConnectionID_Use(ssl);
  1079. if (ret != 0)
  1080. return ret;
  1081. return WOLFSSL_SUCCESS;
  1082. }
  1083. int wolfSSL_dtls_cid_is_enabled(WOLFSSL* ssl)
  1084. {
  1085. return DtlsCidGetInfo(ssl) != NULL;
  1086. }
  1087. int wolfSSL_dtls_cid_set(WOLFSSL* ssl, unsigned char* cid, unsigned int size)
  1088. {
  1089. ConnectionID* newCid;
  1090. CIDInfo* cidInfo;
  1091. if (!ssl->options.useDtlsCID)
  1092. return WOLFSSL_FAILURE;
  1093. cidInfo = DtlsCidGetInfo(ssl);
  1094. if (cidInfo == NULL)
  1095. return WOLFSSL_FAILURE;
  1096. if (cidInfo->rx != NULL) {
  1097. XFREE(cidInfo->rx, ssl->heap, DYNAMIC_TYPE_TLSX);
  1098. cidInfo->rx = NULL;
  1099. }
  1100. /* empty CID */
  1101. if (size == 0)
  1102. return WOLFSSL_SUCCESS;
  1103. if (size > DTLS_CID_MAX_SIZE)
  1104. return LENGTH_ERROR;
  1105. newCid = DtlsCidNew(cid, (byte)size, ssl->heap);
  1106. if (newCid == NULL)
  1107. return MEMORY_ERROR;
  1108. cidInfo->rx = newCid;
  1109. return WOLFSSL_SUCCESS;
  1110. }
  1111. int wolfSSL_dtls_cid_get_rx_size(WOLFSSL* ssl, unsigned int* size)
  1112. {
  1113. return DtlsCidGetSize(ssl, size, 1);
  1114. }
  1115. int wolfSSL_dtls_cid_get_rx(WOLFSSL* ssl, unsigned char* buf,
  1116. unsigned int bufferSz)
  1117. {
  1118. return DtlsCidGet(ssl, buf, bufferSz, 1);
  1119. }
  1120. int wolfSSL_dtls_cid_get_tx_size(WOLFSSL* ssl, unsigned int* size)
  1121. {
  1122. return DtlsCidGetSize(ssl, size, 0);
  1123. }
  1124. int wolfSSL_dtls_cid_get_tx(WOLFSSL* ssl, unsigned char* buf,
  1125. unsigned int bufferSz)
  1126. {
  1127. return DtlsCidGet(ssl, buf, bufferSz, 0);
  1128. }
  1129. #endif /* WOLFSSL_DTLS_CID */
  1130. #endif /* WOLFSSL_DTLS */
  1131. #endif /* WOLFCRYPT_ONLY */