ssl_tls13_server.c 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608
  1. /*
  2. * TLS 1.3 server-side functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
  9. #include "debug_internal.h"
  10. #include "mbedtls/error.h"
  11. #include "mbedtls/platform.h"
  12. #include "mbedtls/constant_time.h"
  13. #include "mbedtls/oid.h"
  14. #include "mbedtls/psa_util.h"
  15. #include "ssl_misc.h"
  16. #include "ssl_tls13_keys.h"
  17. #include "ssl_debug_helpers.h"
  18. static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
  19. mbedtls_ssl_context *ssl,
  20. unsigned int cipher_suite)
  21. {
  22. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  23. if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
  24. return NULL;
  25. }
  26. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
  27. if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
  28. ssl->tls_version,
  29. ssl->tls_version) != 0)) {
  30. return NULL;
  31. }
  32. return ciphersuite_info;
  33. }
  34. static void ssl_tls13_select_ciphersuite(
  35. mbedtls_ssl_context *ssl,
  36. const unsigned char *cipher_suites,
  37. const unsigned char *cipher_suites_end,
  38. int psk_ciphersuite_id,
  39. psa_algorithm_t psk_hash_alg,
  40. const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
  41. {
  42. *selected_ciphersuite_info = NULL;
  43. /*
  44. * In a compliant ClientHello the byte-length of the list of ciphersuites
  45. * is even and this function relies on this fact. This should have been
  46. * checked in the main ClientHello parsing function. Double check here.
  47. */
  48. if ((cipher_suites_end - cipher_suites) & 1) {
  49. return;
  50. }
  51. for (const unsigned char *p = cipher_suites;
  52. p < cipher_suites_end; p += 2) {
  53. /*
  54. * "cipher_suites_end - p is even" is an invariant of the loop. As
  55. * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and it
  56. * is thus safe to read two bytes.
  57. */
  58. uint16_t id = MBEDTLS_GET_UINT16_BE(p, 0);
  59. const mbedtls_ssl_ciphersuite_t *info =
  60. ssl_tls13_validate_peer_ciphersuite(ssl, id);
  61. if (info == NULL) {
  62. continue;
  63. }
  64. /*
  65. * If a valid PSK ciphersuite identifier has been passed in, we want
  66. * an exact match.
  67. */
  68. if (psk_ciphersuite_id != 0) {
  69. if (id != psk_ciphersuite_id) {
  70. continue;
  71. }
  72. } else if (psk_hash_alg != PSA_ALG_NONE) {
  73. if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) info->mac) !=
  74. psk_hash_alg) {
  75. continue;
  76. }
  77. }
  78. *selected_ciphersuite_info = info;
  79. return;
  80. }
  81. MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite, psk_ciphersuite_id=%x, psk_hash_alg=%lx",
  82. (unsigned) psk_ciphersuite_id,
  83. (unsigned long) psk_hash_alg));
  84. }
  85. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  86. /* From RFC 8446:
  87. *
  88. * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
  89. * struct {
  90. * PskKeyExchangeMode ke_modes<1..255>;
  91. * } PskKeyExchangeModes;
  92. */
  93. MBEDTLS_CHECK_RETURN_CRITICAL
  94. static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
  95. const unsigned char *buf,
  96. const unsigned char *end)
  97. {
  98. const unsigned char *p = buf;
  99. size_t ke_modes_len;
  100. int ke_modes = 0;
  101. /* Read ke_modes length (1 Byte) */
  102. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
  103. ke_modes_len = *p++;
  104. /* Currently, there are only two PSK modes, so even without looking
  105. * at the content, something's wrong if the list has more than 2 items. */
  106. if (ke_modes_len > 2) {
  107. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  108. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  109. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  110. }
  111. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
  112. while (ke_modes_len-- != 0) {
  113. switch (*p++) {
  114. case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
  115. ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
  116. MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
  117. break;
  118. case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
  119. ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
  120. MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
  121. break;
  122. default:
  123. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  124. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  125. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  126. }
  127. }
  128. ssl->handshake->tls13_kex_modes = ke_modes;
  129. return 0;
  130. }
  131. /*
  132. * Non-error return values of
  133. * ssl_tls13_offered_psks_check_identity_match_ticket() and
  134. * ssl_tls13_offered_psks_check_identity_match(). They are positive to
  135. * not collide with error codes that are negative. Zero
  136. * (SSL_TLS1_3_PSK_IDENTITY_MATCH) in case of success as it may be propagated
  137. * up by the callers of this function as a generic success condition.
  138. *
  139. * The return value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE means
  140. * that the pre-shared-key identity matches that of a ticket or an externally-
  141. * provisioned pre-shared-key. We have thus been able to retrieve the
  142. * attributes of the pre-shared-key but at least one of them does not meet
  143. * some criteria and the pre-shared-key cannot be used. For example, a ticket
  144. * is expired or its version is not TLS 1.3. Note eventually that the return
  145. * value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE does not have
  146. * anything to do with binder check. A binder check is done only when a
  147. * suitable pre-shared-key has been selected and only for that selected
  148. * pre-shared-key: if the binder check fails, we fail the handshake and we do
  149. * not try to find another pre-shared-key for which the binder check would
  150. * succeed as recommended by the specification.
  151. */
  152. #define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
  153. #define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
  154. #define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
  155. MBEDTLS_CHECK_RETURN_CRITICAL
  156. static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
  157. MBEDTLS_CHECK_RETURN_CRITICAL
  158. static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
  159. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  160. MBEDTLS_CHECK_RETURN_CRITICAL
  161. static int ssl_tls13_offered_psks_check_identity_match_ticket(
  162. mbedtls_ssl_context *ssl,
  163. const unsigned char *identity,
  164. size_t identity_len,
  165. uint32_t obfuscated_ticket_age,
  166. mbedtls_ssl_session *session)
  167. {
  168. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  169. unsigned char *ticket_buffer;
  170. #if defined(MBEDTLS_HAVE_TIME)
  171. mbedtls_ms_time_t now;
  172. mbedtls_ms_time_t server_age;
  173. uint32_t client_age;
  174. mbedtls_ms_time_t age_diff;
  175. #endif
  176. ((void) obfuscated_ticket_age);
  177. MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
  178. /* Ticket parser is not configured, Skip */
  179. if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
  180. return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
  181. }
  182. /* We create a copy of the encrypted ticket since the ticket parsing
  183. * function is allowed to use its input buffer as an output buffer
  184. * (in-place decryption). We do, however, need the original buffer for
  185. * computing the PSK binder value.
  186. */
  187. ticket_buffer = mbedtls_calloc(1, identity_len);
  188. if (ticket_buffer == NULL) {
  189. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  190. }
  191. memcpy(ticket_buffer, identity, identity_len);
  192. ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
  193. session,
  194. ticket_buffer, identity_len);
  195. switch (ret) {
  196. case 0:
  197. ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
  198. break;
  199. case MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED:
  200. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
  201. ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
  202. break;
  203. case MBEDTLS_ERR_SSL_INVALID_MAC:
  204. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
  205. ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
  206. break;
  207. default:
  208. MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
  209. ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
  210. }
  211. /* We delete the temporary buffer */
  212. mbedtls_free(ticket_buffer);
  213. if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
  214. goto exit;
  215. }
  216. /*
  217. * The identity matches that of a ticket. Now check that it has suitable
  218. * attributes and bet it will not be the case.
  219. */
  220. ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
  221. if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
  222. MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
  223. goto exit;
  224. }
  225. #if defined(MBEDTLS_HAVE_TIME)
  226. now = mbedtls_ms_time();
  227. if (now < session->ticket_creation_time) {
  228. MBEDTLS_SSL_DEBUG_MSG(
  229. 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
  230. ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
  231. now, session->ticket_creation_time));
  232. goto exit;
  233. }
  234. server_age = now - session->ticket_creation_time;
  235. /* RFC 8446 section 4.6.1
  236. *
  237. * Servers MUST NOT use any value greater than 604800 seconds (7 days).
  238. *
  239. * RFC 8446 section 4.2.11.1
  240. *
  241. * Clients MUST NOT attempt to use tickets which have ages greater than
  242. * the "ticket_lifetime" value which was provided with the ticket.
  243. *
  244. */
  245. if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
  246. MBEDTLS_SSL_DEBUG_MSG(
  247. 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
  248. server_age));
  249. goto exit;
  250. }
  251. /* RFC 8446 section 4.2.10
  252. *
  253. * For PSKs provisioned via NewSessionTicket, a server MUST validate that
  254. * the ticket age for the selected PSK identity (computed by subtracting
  255. * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
  256. * within a small tolerance of the time since the ticket was issued.
  257. *
  258. * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
  259. * million (360 to 72 milliseconds per hour). Default tolerance
  260. * window is 6s, thus in the worst case clients and servers must
  261. * sync up their system time every 6000/360/2~=8 hours.
  262. */
  263. client_age = obfuscated_ticket_age - session->ticket_age_add;
  264. age_diff = server_age - (mbedtls_ms_time_t) client_age;
  265. if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
  266. age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
  267. MBEDTLS_SSL_DEBUG_MSG(
  268. 3, ("Ticket age outside tolerance window ( diff = %"
  269. MBEDTLS_PRINTF_MS_TIME ")",
  270. age_diff));
  271. goto exit;
  272. }
  273. #endif /* MBEDTLS_HAVE_TIME */
  274. /*
  275. * All good, we have found a suitable ticket.
  276. */
  277. ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
  278. exit:
  279. if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
  280. mbedtls_ssl_session_free(session);
  281. }
  282. MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
  283. return ret;
  284. }
  285. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  286. MBEDTLS_CHECK_RETURN_CRITICAL
  287. static int ssl_tls13_offered_psks_check_identity_match(
  288. mbedtls_ssl_context *ssl,
  289. const unsigned char *identity,
  290. size_t identity_len,
  291. uint32_t obfuscated_ticket_age,
  292. int *psk_type,
  293. mbedtls_ssl_session *session)
  294. {
  295. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  296. ((void) session);
  297. ((void) obfuscated_ticket_age);
  298. *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
  299. MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
  300. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  301. ret = ssl_tls13_offered_psks_check_identity_match_ticket(
  302. ssl, identity, identity_len, obfuscated_ticket_age, session);
  303. if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
  304. *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
  305. ret = mbedtls_ssl_set_hs_psk(ssl,
  306. session->resumption_key,
  307. session->resumption_key_len);
  308. if (ret != 0) {
  309. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
  310. return ret;
  311. }
  312. MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
  313. session->resumption_key,
  314. session->resumption_key_len);
  315. MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
  316. (unsigned) obfuscated_ticket_age));
  317. return SSL_TLS1_3_PSK_IDENTITY_MATCH;
  318. } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
  319. return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
  320. }
  321. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  322. /* Check identity with external configured function */
  323. if (ssl->conf->f_psk != NULL) {
  324. if (ssl->conf->f_psk(
  325. ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
  326. return SSL_TLS1_3_PSK_IDENTITY_MATCH;
  327. }
  328. return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
  329. }
  330. MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
  331. /* Check identity with pre-configured psk */
  332. if (ssl->conf->psk_identity != NULL &&
  333. identity_len == ssl->conf->psk_identity_len &&
  334. mbedtls_ct_memcmp(ssl->conf->psk_identity,
  335. identity, identity_len) == 0) {
  336. ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
  337. if (ret != 0) {
  338. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
  339. return ret;
  340. }
  341. return SSL_TLS1_3_PSK_IDENTITY_MATCH;
  342. }
  343. return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
  344. }
  345. /*
  346. * Non-error return values of ssl_tls13_offered_psks_check_binder_match().
  347. * They are positive to not collide with error codes that are negative. Zero
  348. * (SSL_TLS1_3_BINDER_MATCH) in case of success as it may be propagated up
  349. * by the callers of this function as a generic success condition.
  350. */
  351. #define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
  352. #define SSL_TLS1_3_BINDER_MATCH 0
  353. MBEDTLS_CHECK_RETURN_CRITICAL
  354. static int ssl_tls13_offered_psks_check_binder_match(
  355. mbedtls_ssl_context *ssl,
  356. const unsigned char *binder, size_t binder_len,
  357. int psk_type, psa_algorithm_t psk_hash_alg)
  358. {
  359. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  360. unsigned char transcript[PSA_HASH_MAX_SIZE];
  361. size_t transcript_len;
  362. unsigned char *psk;
  363. size_t psk_len;
  364. unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
  365. if (binder_len != PSA_HASH_LENGTH(psk_hash_alg)) {
  366. return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
  367. }
  368. /* Get current state of handshake transcript. */
  369. ret = mbedtls_ssl_get_handshake_transcript(
  370. ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
  371. transcript, sizeof(transcript), &transcript_len);
  372. if (ret != 0) {
  373. return ret;
  374. }
  375. ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
  376. if (ret != 0) {
  377. return ret;
  378. }
  379. ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
  380. psk, psk_len, psk_type,
  381. transcript,
  382. server_computed_binder);
  383. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  384. mbedtls_free((void *) psk);
  385. #endif
  386. if (ret != 0) {
  387. MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
  388. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  389. }
  390. MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
  391. server_computed_binder, transcript_len);
  392. MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
  393. if (mbedtls_ct_memcmp(server_computed_binder,
  394. binder,
  395. PSA_HASH_LENGTH(psk_hash_alg)) == 0) {
  396. return SSL_TLS1_3_BINDER_MATCH;
  397. }
  398. mbedtls_platform_zeroize(server_computed_binder,
  399. sizeof(server_computed_binder));
  400. return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
  401. }
  402. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  403. MBEDTLS_CHECK_RETURN_CRITICAL
  404. static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
  405. const mbedtls_ssl_session *src)
  406. {
  407. dst->ticket_age_add = src->ticket_age_add;
  408. dst->ticket_flags = src->ticket_flags;
  409. dst->resumption_key_len = src->resumption_key_len;
  410. if (src->resumption_key_len == 0) {
  411. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  412. }
  413. memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
  414. #if defined(MBEDTLS_SSL_EARLY_DATA)
  415. dst->max_early_data_size = src->max_early_data_size;
  416. #if defined(MBEDTLS_SSL_ALPN)
  417. int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
  418. if (ret != 0) {
  419. return ret;
  420. }
  421. #endif /* MBEDTLS_SSL_ALPN */
  422. #endif /* MBEDTLS_SSL_EARLY_DATA*/
  423. return 0;
  424. }
  425. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  426. struct psk_attributes {
  427. int type;
  428. int key_exchange_mode;
  429. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  430. };
  431. #define PSK_ATTRIBUTES_INIT { 0, 0, NULL }
  432. /* Parser for pre_shared_key extension in client hello
  433. * struct {
  434. * opaque identity<1..2^16-1>;
  435. * uint32 obfuscated_ticket_age;
  436. * } PskIdentity;
  437. *
  438. * opaque PskBinderEntry<32..255>;
  439. *
  440. * struct {
  441. * PskIdentity identities<7..2^16-1>;
  442. * PskBinderEntry binders<33..2^16-1>;
  443. * } OfferedPsks;
  444. *
  445. * struct {
  446. * select (Handshake.msg_type) {
  447. * case client_hello: OfferedPsks;
  448. * ....
  449. * };
  450. * } PreSharedKeyExtension;
  451. */
  452. MBEDTLS_CHECK_RETURN_CRITICAL
  453. static int ssl_tls13_parse_pre_shared_key_ext(
  454. mbedtls_ssl_context *ssl,
  455. const unsigned char *pre_shared_key_ext,
  456. const unsigned char *pre_shared_key_ext_end,
  457. const unsigned char *ciphersuites,
  458. const unsigned char *ciphersuites_end,
  459. struct psk_attributes *psk)
  460. {
  461. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  462. const unsigned char *identities = pre_shared_key_ext;
  463. const unsigned char *p_identity_len;
  464. size_t identities_len;
  465. const unsigned char *identities_end;
  466. const unsigned char *binders;
  467. const unsigned char *p_binder_len;
  468. size_t binders_len;
  469. const unsigned char *binders_end;
  470. int matched_identity = -1;
  471. int identity_id = -1;
  472. MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
  473. pre_shared_key_ext,
  474. pre_shared_key_ext_end - pre_shared_key_ext);
  475. /* identities_len 2 bytes
  476. * identities_data >= 7 bytes
  477. */
  478. MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
  479. identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
  480. p_identity_len = identities + 2;
  481. MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
  482. identities_len);
  483. identities_end = p_identity_len + identities_len;
  484. /* binders_len 2 bytes
  485. * binders >= 33 bytes
  486. */
  487. binders = identities_end;
  488. MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
  489. binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
  490. p_binder_len = binders + 2;
  491. MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
  492. binders_end = p_binder_len + binders_len;
  493. ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
  494. identities_end - pre_shared_key_ext);
  495. if (0 != ret) {
  496. MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
  497. return ret;
  498. }
  499. while (p_identity_len < identities_end && p_binder_len < binders_end) {
  500. const unsigned char *identity;
  501. size_t identity_len;
  502. uint32_t obfuscated_ticket_age;
  503. const unsigned char *binder;
  504. size_t binder_len;
  505. int psk_ciphersuite_id;
  506. psa_algorithm_t psk_hash_alg;
  507. int allowed_key_exchange_modes;
  508. mbedtls_ssl_session session;
  509. mbedtls_ssl_session_init(&session);
  510. MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
  511. identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
  512. identity = p_identity_len + 2;
  513. MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
  514. obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
  515. p_identity_len += identity_len + 6;
  516. MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
  517. binder_len = *p_binder_len;
  518. binder = p_binder_len + 1;
  519. MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
  520. p_binder_len += binder_len + 1;
  521. identity_id++;
  522. if (matched_identity != -1) {
  523. continue;
  524. }
  525. ret = ssl_tls13_offered_psks_check_identity_match(
  526. ssl, identity, identity_len, obfuscated_ticket_age,
  527. &psk->type, &session);
  528. if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
  529. continue;
  530. }
  531. MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
  532. switch (psk->type) {
  533. case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
  534. psk_ciphersuite_id = 0;
  535. psk_hash_alg = PSA_ALG_SHA_256;
  536. allowed_key_exchange_modes =
  537. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
  538. break;
  539. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  540. case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
  541. psk_ciphersuite_id = session.ciphersuite;
  542. psk_hash_alg = PSA_ALG_NONE;
  543. ssl->session_negotiate->ticket_flags = session.ticket_flags;
  544. allowed_key_exchange_modes =
  545. session.ticket_flags &
  546. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
  547. break;
  548. #endif
  549. default:
  550. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  551. }
  552. psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
  553. if ((allowed_key_exchange_modes &
  554. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
  555. ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
  556. psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
  557. } else if ((allowed_key_exchange_modes &
  558. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
  559. ssl_tls13_key_exchange_is_psk_available(ssl)) {
  560. psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
  561. }
  562. if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
  563. MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
  564. continue;
  565. }
  566. ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
  567. psk_ciphersuite_id, psk_hash_alg,
  568. &psk->ciphersuite_info);
  569. if (psk->ciphersuite_info == NULL) {
  570. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  571. mbedtls_ssl_session_free(&session);
  572. #endif
  573. /*
  574. * We consider finding a ciphersuite suitable for the PSK as part
  575. * of the validation of its binder. Thus if we do not find one, we
  576. * abort the handshake with a decrypt_error alert.
  577. */
  578. MBEDTLS_SSL_PEND_FATAL_ALERT(
  579. MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
  580. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  581. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  582. }
  583. ret = ssl_tls13_offered_psks_check_binder_match(
  584. ssl, binder, binder_len, psk->type,
  585. mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
  586. if (ret != SSL_TLS1_3_BINDER_MATCH) {
  587. /* For security reasons, the handshake should be aborted when we
  588. * fail to validate a binder value. See RFC 8446 section 4.2.11.2
  589. * and appendix E.6. */
  590. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  591. mbedtls_ssl_session_free(&session);
  592. #endif
  593. MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
  594. MBEDTLS_SSL_DEBUG_RET(
  595. 1, "ssl_tls13_offered_psks_check_binder_match", ret);
  596. MBEDTLS_SSL_PEND_FATAL_ALERT(
  597. MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
  598. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  599. return ret;
  600. }
  601. matched_identity = identity_id;
  602. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  603. if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
  604. ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
  605. &session);
  606. mbedtls_ssl_session_free(&session);
  607. if (ret != 0) {
  608. return ret;
  609. }
  610. }
  611. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  612. }
  613. if (p_identity_len != identities_end || p_binder_len != binders_end) {
  614. MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
  615. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  616. MBEDTLS_ERR_SSL_DECODE_ERROR);
  617. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  618. }
  619. /* Update the handshake transcript with the binder list. */
  620. ret = ssl->handshake->update_checksum(
  621. ssl, identities_end, (size_t) (binders_end - identities_end));
  622. if (0 != ret) {
  623. MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
  624. return ret;
  625. }
  626. if (matched_identity == -1) {
  627. MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
  628. return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
  629. }
  630. ssl->handshake->selected_identity = (uint16_t) matched_identity;
  631. MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
  632. return 0;
  633. }
  634. /*
  635. * struct {
  636. * select ( Handshake.msg_type ) {
  637. * ....
  638. * case server_hello:
  639. * uint16 selected_identity;
  640. * }
  641. * } PreSharedKeyExtension;
  642. */
  643. static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
  644. unsigned char *buf,
  645. unsigned char *end,
  646. size_t *olen)
  647. {
  648. unsigned char *p = (unsigned char *) buf;
  649. *olen = 0;
  650. int not_using_psk = 0;
  651. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  652. not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
  653. #else
  654. not_using_psk = (ssl->handshake->psk == NULL);
  655. #endif
  656. if (not_using_psk) {
  657. /* We shouldn't have called this extension writer unless we've
  658. * chosen to use a PSK. */
  659. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  660. }
  661. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
  662. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
  663. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
  664. MBEDTLS_PUT_UINT16_BE(2, p, 2);
  665. MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
  666. *olen = 6;
  667. MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
  668. ssl->handshake->selected_identity));
  669. mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
  670. return 0;
  671. }
  672. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
  673. /* From RFC 8446:
  674. * struct {
  675. * ProtocolVersion versions<2..254>;
  676. * } SupportedVersions;
  677. */
  678. MBEDTLS_CHECK_RETURN_CRITICAL
  679. static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
  680. const unsigned char *buf,
  681. const unsigned char *end)
  682. {
  683. const unsigned char *p = buf;
  684. size_t versions_len;
  685. const unsigned char *versions_end;
  686. uint16_t tls_version;
  687. int found_supported_version = 0;
  688. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
  689. versions_len = p[0];
  690. p += 1;
  691. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
  692. versions_end = p + versions_len;
  693. while (p < versions_end) {
  694. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
  695. tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
  696. p += 2;
  697. if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
  698. found_supported_version = 1;
  699. break;
  700. }
  701. if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
  702. mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
  703. found_supported_version = 1;
  704. break;
  705. }
  706. }
  707. if (!found_supported_version) {
  708. MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
  709. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
  710. MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
  711. return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
  712. }
  713. MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
  714. (unsigned int) tls_version));
  715. return (int) tls_version;
  716. }
  717. #if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
  718. /*
  719. *
  720. * From RFC 8446:
  721. * enum {
  722. * ... (0xFFFF)
  723. * } NamedGroup;
  724. * struct {
  725. * NamedGroup named_group_list<2..2^16-1>;
  726. * } NamedGroupList;
  727. */
  728. MBEDTLS_CHECK_RETURN_CRITICAL
  729. static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
  730. const unsigned char *buf,
  731. const unsigned char *end)
  732. {
  733. const unsigned char *p = buf;
  734. size_t named_group_list_len;
  735. const unsigned char *named_group_list_end;
  736. MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
  737. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  738. named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
  739. p += 2;
  740. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
  741. named_group_list_end = p + named_group_list_len;
  742. ssl->handshake->hrr_selected_group = 0;
  743. while (p < named_group_list_end) {
  744. uint16_t named_group;
  745. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
  746. named_group = MBEDTLS_GET_UINT16_BE(p, 0);
  747. p += 2;
  748. MBEDTLS_SSL_DEBUG_MSG(2,
  749. ("got named group: %s(%04x)",
  750. mbedtls_ssl_named_group_to_str(named_group),
  751. named_group));
  752. if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
  753. !mbedtls_ssl_named_group_is_supported(named_group) ||
  754. ssl->handshake->hrr_selected_group != 0) {
  755. continue;
  756. }
  757. MBEDTLS_SSL_DEBUG_MSG(2,
  758. ("add named group %s(%04x) into received list.",
  759. mbedtls_ssl_named_group_to_str(named_group),
  760. named_group));
  761. ssl->handshake->hrr_selected_group = named_group;
  762. }
  763. return 0;
  764. }
  765. #endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
  766. #define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
  767. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
  768. /*
  769. * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
  770. * extension is correct and stores the first acceptable key share and its
  771. * associated group.
  772. *
  773. * Possible return values are:
  774. * - 0: Successful processing of the client provided key share extension.
  775. * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
  776. * the client does not match a group supported by the server. A
  777. * HelloRetryRequest will be needed.
  778. * - A negative value for fatal errors.
  779. */
  780. MBEDTLS_CHECK_RETURN_CRITICAL
  781. static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
  782. const unsigned char *buf,
  783. const unsigned char *end)
  784. {
  785. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  786. unsigned char const *p = buf;
  787. unsigned char const *client_shares_end;
  788. size_t client_shares_len;
  789. /* From RFC 8446:
  790. *
  791. * struct {
  792. * KeyShareEntry client_shares<0..2^16-1>;
  793. * } KeyShareClientHello;
  794. *
  795. */
  796. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  797. client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
  798. p += 2;
  799. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
  800. ssl->handshake->offered_group_id = 0;
  801. client_shares_end = p + client_shares_len;
  802. /* We try to find a suitable key share entry and copy it to the
  803. * handshake context. Later, we have to find out whether we can do
  804. * something with the provided key share or whether we have to
  805. * dismiss it and send a HelloRetryRequest message.
  806. */
  807. while (p < client_shares_end) {
  808. uint16_t group;
  809. size_t key_exchange_len;
  810. const unsigned char *key_exchange;
  811. /*
  812. * struct {
  813. * NamedGroup group;
  814. * opaque key_exchange<1..2^16-1>;
  815. * } KeyShareEntry;
  816. */
  817. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
  818. group = MBEDTLS_GET_UINT16_BE(p, 0);
  819. key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
  820. p += 4;
  821. key_exchange = p;
  822. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
  823. p += key_exchange_len;
  824. /* Continue parsing even if we have already found a match,
  825. * for input validation purposes.
  826. */
  827. if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
  828. !mbedtls_ssl_named_group_is_supported(group) ||
  829. ssl->handshake->offered_group_id != 0) {
  830. continue;
  831. }
  832. /*
  833. * ECDHE and FFDHE groups are supported
  834. */
  835. if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
  836. mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
  837. MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
  838. mbedtls_ssl_named_group_to_str(group),
  839. group));
  840. ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
  841. ssl, key_exchange - 2, key_exchange_len + 2);
  842. if (ret != 0) {
  843. return ret;
  844. }
  845. } else {
  846. MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
  847. (unsigned) group));
  848. continue;
  849. }
  850. ssl->handshake->offered_group_id = group;
  851. }
  852. if (ssl->handshake->offered_group_id == 0) {
  853. MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
  854. return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
  855. }
  856. return 0;
  857. }
  858. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
  859. MBEDTLS_CHECK_RETURN_CRITICAL
  860. static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
  861. int exts_mask)
  862. {
  863. int masked = ssl->handshake->received_extensions & exts_mask;
  864. return masked == exts_mask;
  865. }
  866. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  867. MBEDTLS_CHECK_RETURN_CRITICAL
  868. static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
  869. mbedtls_ssl_context *ssl)
  870. {
  871. return ssl_tls13_client_hello_has_exts(
  872. ssl,
  873. MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
  874. MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
  875. MBEDTLS_SSL_EXT_MASK(SIG_ALG));
  876. }
  877. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  878. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
  879. MBEDTLS_CHECK_RETURN_CRITICAL
  880. static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
  881. mbedtls_ssl_context *ssl)
  882. {
  883. return ssl_tls13_client_hello_has_exts(
  884. ssl,
  885. MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
  886. MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
  887. }
  888. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
  889. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
  890. MBEDTLS_CHECK_RETURN_CRITICAL
  891. static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
  892. mbedtls_ssl_context *ssl)
  893. {
  894. return ssl_tls13_client_hello_has_exts(
  895. ssl,
  896. MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
  897. MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
  898. MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
  899. MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
  900. }
  901. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
  902. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  903. MBEDTLS_CHECK_RETURN_CRITICAL
  904. static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
  905. {
  906. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
  907. return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
  908. mbedtls_ssl_tls13_is_psk_supported(ssl) &&
  909. ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
  910. #else
  911. ((void) ssl);
  912. return 0;
  913. #endif
  914. }
  915. MBEDTLS_CHECK_RETURN_CRITICAL
  916. static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
  917. {
  918. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
  919. return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
  920. mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
  921. ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
  922. #else
  923. ((void) ssl);
  924. return 0;
  925. #endif
  926. }
  927. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
  928. MBEDTLS_CHECK_RETURN_CRITICAL
  929. static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
  930. {
  931. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  932. return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
  933. ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
  934. #else
  935. ((void) ssl);
  936. return 0;
  937. #endif
  938. }
  939. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  940. defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  941. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  942. static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
  943. {
  944. switch (sig_alg) {
  945. case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
  946. return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
  947. case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
  948. return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
  949. case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
  950. return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
  951. case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
  952. return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
  953. case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
  954. return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
  955. case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
  956. return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
  957. case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
  958. return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
  959. case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
  960. return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
  961. case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
  962. return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
  963. default:
  964. return PSA_ALG_NONE;
  965. }
  966. }
  967. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  968. /*
  969. * Pick best ( private key, certificate chain ) pair based on the signature
  970. * algorithms supported by the client.
  971. */
  972. MBEDTLS_CHECK_RETURN_CRITICAL
  973. static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
  974. {
  975. mbedtls_ssl_key_cert *key_cert, *key_cert_list;
  976. const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
  977. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  978. if (ssl->handshake->sni_key_cert != NULL) {
  979. key_cert_list = ssl->handshake->sni_key_cert;
  980. } else
  981. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  982. key_cert_list = ssl->conf->key_cert;
  983. if (key_cert_list == NULL) {
  984. MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
  985. return -1;
  986. }
  987. for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
  988. if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
  989. continue;
  990. }
  991. if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
  992. continue;
  993. }
  994. for (key_cert = key_cert_list; key_cert != NULL;
  995. key_cert = key_cert->next) {
  996. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  997. psa_algorithm_t psa_alg = PSA_ALG_NONE;
  998. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  999. MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
  1000. key_cert->cert);
  1001. /*
  1002. * This avoids sending the client a cert it'll reject based on
  1003. * keyUsage or other extensions.
  1004. */
  1005. if (mbedtls_x509_crt_check_key_usage(
  1006. key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
  1007. mbedtls_x509_crt_check_extended_key_usage(
  1008. key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
  1009. MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
  1010. MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
  1011. "(extended) key usage extension"));
  1012. continue;
  1013. }
  1014. MBEDTLS_SSL_DEBUG_MSG(3,
  1015. ("ssl_tls13_pick_key_cert:"
  1016. "check signature algorithm %s [%04x]",
  1017. mbedtls_ssl_sig_alg_to_str(*sig_alg),
  1018. *sig_alg));
  1019. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1020. psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
  1021. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1022. if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
  1023. *sig_alg, &key_cert->cert->pk)
  1024. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1025. && psa_alg != PSA_ALG_NONE &&
  1026. mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
  1027. PSA_KEY_USAGE_SIGN_HASH) == 1
  1028. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1029. ) {
  1030. ssl->handshake->key_cert = key_cert;
  1031. MBEDTLS_SSL_DEBUG_MSG(3,
  1032. ("ssl_tls13_pick_key_cert:"
  1033. "selected signature algorithm"
  1034. " %s [%04x]",
  1035. mbedtls_ssl_sig_alg_to_str(*sig_alg),
  1036. *sig_alg));
  1037. MBEDTLS_SSL_DEBUG_CRT(
  1038. 3, "selected certificate (chain)",
  1039. ssl->handshake->key_cert->cert);
  1040. return 0;
  1041. }
  1042. }
  1043. }
  1044. MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
  1045. "no suitable certificate found"));
  1046. return -1;
  1047. }
  1048. #endif /* MBEDTLS_X509_CRT_PARSE_C &&
  1049. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  1050. /*
  1051. *
  1052. * STATE HANDLING: ClientHello
  1053. *
  1054. * There are three possible classes of outcomes when parsing the ClientHello:
  1055. *
  1056. * 1) The ClientHello was well-formed and matched the server's configuration.
  1057. *
  1058. * In this case, the server progresses to sending its ServerHello.
  1059. *
  1060. * 2) The ClientHello was well-formed but didn't match the server's
  1061. * configuration.
  1062. *
  1063. * For example, the client might not have offered a key share which
  1064. * the server supports, or the server might require a cookie.
  1065. *
  1066. * In this case, the server sends a HelloRetryRequest.
  1067. *
  1068. * 3) The ClientHello was ill-formed
  1069. *
  1070. * In this case, we abort the handshake.
  1071. *
  1072. */
  1073. /*
  1074. * Structure of this message:
  1075. *
  1076. * uint16 ProtocolVersion;
  1077. * opaque Random[32];
  1078. * uint8 CipherSuite[2]; // Cryptographic suite selector
  1079. *
  1080. * struct {
  1081. * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
  1082. * Random random;
  1083. * opaque legacy_session_id<0..32>;
  1084. * CipherSuite cipher_suites<2..2^16-2>;
  1085. * opaque legacy_compression_methods<1..2^8-1>;
  1086. * Extension extensions<8..2^16-1>;
  1087. * } ClientHello;
  1088. */
  1089. #define SSL_CLIENT_HELLO_OK 0
  1090. #define SSL_CLIENT_HELLO_HRR_REQUIRED 1
  1091. #define SSL_CLIENT_HELLO_TLS1_2 2
  1092. MBEDTLS_CHECK_RETURN_CRITICAL
  1093. static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
  1094. const unsigned char *buf,
  1095. const unsigned char *end)
  1096. {
  1097. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1098. const unsigned char *p = buf;
  1099. const unsigned char *random;
  1100. size_t legacy_session_id_len;
  1101. const unsigned char *legacy_session_id;
  1102. size_t cipher_suites_len;
  1103. const unsigned char *cipher_suites;
  1104. const unsigned char *cipher_suites_end;
  1105. size_t extensions_len;
  1106. const unsigned char *extensions_end;
  1107. const unsigned char *supported_versions_data;
  1108. const unsigned char *supported_versions_data_end;
  1109. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  1110. int hrr_required = 0;
  1111. int no_usable_share_for_key_agreement = 0;
  1112. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1113. int got_psk = 0;
  1114. struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
  1115. const unsigned char *pre_shared_key_ext = NULL;
  1116. const unsigned char *pre_shared_key_ext_end = NULL;
  1117. #endif
  1118. /*
  1119. * ClientHello layout:
  1120. * 0 . 1 protocol version
  1121. * 2 . 33 random bytes
  1122. * 34 . 34 session id length ( 1 byte )
  1123. * 35 . 34+x session id
  1124. * .. . .. ciphersuite list length ( 2 bytes )
  1125. * .. . .. ciphersuite list
  1126. * .. . .. compression alg. list length ( 1 byte )
  1127. * .. . .. compression alg. list
  1128. * .. . .. extensions length ( 2 bytes, optional )
  1129. * .. . .. extensions ( optional )
  1130. */
  1131. /*
  1132. * Minimal length ( with everything empty and extensions omitted ) is
  1133. * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
  1134. * read at least up to session id length without worrying.
  1135. */
  1136. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
  1137. /* ...
  1138. * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
  1139. * ...
  1140. * with ProtocolVersion defined as:
  1141. * uint16 ProtocolVersion;
  1142. */
  1143. if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
  1144. MBEDTLS_SSL_VERSION_TLS1_2) {
  1145. MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
  1146. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
  1147. MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
  1148. return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
  1149. }
  1150. p += 2;
  1151. /* ...
  1152. * Random random;
  1153. * ...
  1154. * with Random defined as:
  1155. * opaque Random[32];
  1156. */
  1157. random = p;
  1158. p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
  1159. /* ...
  1160. * opaque legacy_session_id<0..32>;
  1161. * ...
  1162. */
  1163. legacy_session_id_len = *(p++);
  1164. legacy_session_id = p;
  1165. /*
  1166. * Check we have enough data for the legacy session identifier
  1167. * and the ciphersuite list length.
  1168. */
  1169. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
  1170. p += legacy_session_id_len;
  1171. /* ...
  1172. * CipherSuite cipher_suites<2..2^16-2>;
  1173. * ...
  1174. * with CipherSuite defined as:
  1175. * uint8 CipherSuite[2];
  1176. */
  1177. cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
  1178. p += 2;
  1179. cipher_suites = p;
  1180. /*
  1181. * The length of the ciphersuite list has to be even.
  1182. */
  1183. if (cipher_suites_len & 1) {
  1184. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  1185. MBEDTLS_ERR_SSL_DECODE_ERROR);
  1186. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1187. }
  1188. /* Check we have enough data for the ciphersuite list, the legacy
  1189. * compression methods and the length of the extensions.
  1190. *
  1191. * cipher_suites cipher_suites_len bytes
  1192. * legacy_compression_methods length 1 byte
  1193. */
  1194. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 1);
  1195. p += cipher_suites_len;
  1196. cipher_suites_end = p;
  1197. /* Check if we have enough data for legacy_compression_methods
  1198. * and the length of the extensions (2 bytes).
  1199. */
  1200. MBEDTLS_SSL_CHK_BUF_READ_PTR(p + 1, end, p[0] + 2);
  1201. /*
  1202. * Search for the supported versions extension and parse it to determine
  1203. * if the client supports TLS 1.3.
  1204. */
  1205. ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
  1206. ssl, p + 1 + p[0], end,
  1207. &supported_versions_data, &supported_versions_data_end);
  1208. if (ret < 0) {
  1209. MBEDTLS_SSL_DEBUG_RET(1,
  1210. ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
  1211. return ret;
  1212. }
  1213. if (ret == 0) {
  1214. return SSL_CLIENT_HELLO_TLS1_2;
  1215. }
  1216. if (ret == 1) {
  1217. ret = ssl_tls13_parse_supported_versions_ext(ssl,
  1218. supported_versions_data,
  1219. supported_versions_data_end);
  1220. if (ret < 0) {
  1221. MBEDTLS_SSL_DEBUG_RET(1,
  1222. ("ssl_tls13_parse_supported_versions_ext"), ret);
  1223. return ret;
  1224. }
  1225. /*
  1226. * The supported versions extension was parsed successfully as the
  1227. * value returned by ssl_tls13_parse_supported_versions_ext() is
  1228. * positive. The return value is then equal to
  1229. * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
  1230. * the TLS version to negotiate.
  1231. */
  1232. if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
  1233. return SSL_CLIENT_HELLO_TLS1_2;
  1234. }
  1235. }
  1236. /*
  1237. * We negotiate TLS 1.3.
  1238. */
  1239. ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
  1240. ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
  1241. ssl->session_negotiate->endpoint = ssl->conf->endpoint;
  1242. /* Before doing any crypto, make sure we can. */
  1243. ret = mbedtls_ssl_tls13_crypto_init(ssl);
  1244. if (ret != 0) {
  1245. return ret;
  1246. }
  1247. /*
  1248. * We are negotiating the version 1.3 of the protocol. Do what we have
  1249. * postponed: copy of the client random bytes, copy of the legacy session
  1250. * identifier and selection of the TLS 1.3 cipher suite.
  1251. */
  1252. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
  1253. random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
  1254. memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
  1255. if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
  1256. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1257. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1258. }
  1259. ssl->session_negotiate->id_len = legacy_session_id_len;
  1260. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
  1261. legacy_session_id, legacy_session_id_len);
  1262. memcpy(&ssl->session_negotiate->id[0],
  1263. legacy_session_id, legacy_session_id_len);
  1264. /*
  1265. * Search for a matching ciphersuite
  1266. */
  1267. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
  1268. cipher_suites, cipher_suites_len);
  1269. ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
  1270. 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
  1271. if (handshake->ciphersuite_info == NULL) {
  1272. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  1273. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  1274. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1275. }
  1276. ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
  1277. MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
  1278. ((unsigned) handshake->ciphersuite_info->id),
  1279. handshake->ciphersuite_info->name));
  1280. /* ...
  1281. * opaque legacy_compression_methods<1..2^8-1>;
  1282. * ...
  1283. */
  1284. if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
  1285. MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
  1286. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  1287. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1288. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1289. }
  1290. p += 2;
  1291. /* ...
  1292. * Extension extensions<8..2^16-1>;
  1293. * ...
  1294. * with Extension defined as:
  1295. * struct {
  1296. * ExtensionType extension_type;
  1297. * opaque extension_data<0..2^16-1>;
  1298. * } Extension;
  1299. */
  1300. extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
  1301. p += 2;
  1302. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
  1303. extensions_end = p + extensions_len;
  1304. MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
  1305. handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
  1306. while (p < extensions_end) {
  1307. unsigned int extension_type;
  1308. size_t extension_data_len;
  1309. const unsigned char *extension_data_end;
  1310. uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
  1311. if (ssl->handshake->hello_retry_request_flag) {
  1312. /* Do not accept early data extension in 2nd ClientHello */
  1313. allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
  1314. }
  1315. /* RFC 8446, section 4.2.11
  1316. *
  1317. * The "pre_shared_key" extension MUST be the last extension in the
  1318. * ClientHello (this facilitates implementation as described below).
  1319. * Servers MUST check that it is the last extension and otherwise fail
  1320. * the handshake with an "illegal_parameter" alert.
  1321. */
  1322. if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
  1323. MBEDTLS_SSL_DEBUG_MSG(
  1324. 3, ("pre_shared_key is not last extension."));
  1325. MBEDTLS_SSL_PEND_FATAL_ALERT(
  1326. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  1327. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1328. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1329. }
  1330. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
  1331. extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
  1332. extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
  1333. p += 4;
  1334. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
  1335. extension_data_end = p + extension_data_len;
  1336. ret = mbedtls_ssl_tls13_check_received_extension(
  1337. ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
  1338. allowed_exts);
  1339. if (ret != 0) {
  1340. return ret;
  1341. }
  1342. switch (extension_type) {
  1343. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  1344. case MBEDTLS_TLS_EXT_SERVERNAME:
  1345. MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
  1346. ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
  1347. extension_data_end);
  1348. if (ret != 0) {
  1349. MBEDTLS_SSL_DEBUG_RET(
  1350. 1, "mbedtls_ssl_parse_servername_ext", ret);
  1351. return ret;
  1352. }
  1353. break;
  1354. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  1355. #if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
  1356. case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
  1357. MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
  1358. /* Supported Groups Extension
  1359. *
  1360. * When sent by the client, the "supported_groups" extension
  1361. * indicates the named groups which the client supports,
  1362. * ordered from most preferred to least preferred.
  1363. */
  1364. ret = ssl_tls13_parse_supported_groups_ext(
  1365. ssl, p, extension_data_end);
  1366. if (ret != 0) {
  1367. MBEDTLS_SSL_DEBUG_RET(
  1368. 1, "ssl_tls13_parse_supported_groups_ext", ret);
  1369. return ret;
  1370. }
  1371. break;
  1372. #endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
  1373. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
  1374. case MBEDTLS_TLS_EXT_KEY_SHARE:
  1375. MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
  1376. /*
  1377. * Key Share Extension
  1378. *
  1379. * When sent by the client, the "key_share" extension
  1380. * contains the endpoint's cryptographic parameters for
  1381. * ECDHE/DHE key establishment methods.
  1382. */
  1383. ret = ssl_tls13_parse_key_shares_ext(
  1384. ssl, p, extension_data_end);
  1385. if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
  1386. MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
  1387. no_usable_share_for_key_agreement = 1;
  1388. }
  1389. if (ret < 0) {
  1390. MBEDTLS_SSL_DEBUG_RET(
  1391. 1, "ssl_tls13_parse_key_shares_ext", ret);
  1392. return ret;
  1393. }
  1394. break;
  1395. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
  1396. case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
  1397. /* Already parsed */
  1398. break;
  1399. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1400. case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
  1401. MBEDTLS_SSL_DEBUG_MSG(
  1402. 3, ("found psk key exchange modes extension"));
  1403. ret = ssl_tls13_parse_key_exchange_modes_ext(
  1404. ssl, p, extension_data_end);
  1405. if (ret != 0) {
  1406. MBEDTLS_SSL_DEBUG_RET(
  1407. 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
  1408. return ret;
  1409. }
  1410. break;
  1411. #endif
  1412. case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
  1413. MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
  1414. if ((handshake->received_extensions &
  1415. MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
  1416. MBEDTLS_SSL_PEND_FATAL_ALERT(
  1417. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  1418. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1419. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1420. }
  1421. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1422. /* Delay processing of the PSK identity once we have
  1423. * found out which algorithms to use. We keep a pointer
  1424. * to the buffer and the size for later processing.
  1425. */
  1426. pre_shared_key_ext = p;
  1427. pre_shared_key_ext_end = extension_data_end;
  1428. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
  1429. break;
  1430. #if defined(MBEDTLS_SSL_ALPN)
  1431. case MBEDTLS_TLS_EXT_ALPN:
  1432. MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
  1433. ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
  1434. if (ret != 0) {
  1435. MBEDTLS_SSL_DEBUG_RET(
  1436. 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
  1437. return ret;
  1438. }
  1439. break;
  1440. #endif /* MBEDTLS_SSL_ALPN */
  1441. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  1442. case MBEDTLS_TLS_EXT_SIG_ALG:
  1443. MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
  1444. ret = mbedtls_ssl_parse_sig_alg_ext(
  1445. ssl, p, extension_data_end);
  1446. if (ret != 0) {
  1447. MBEDTLS_SSL_DEBUG_RET(
  1448. 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
  1449. return ret;
  1450. }
  1451. break;
  1452. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  1453. #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
  1454. case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
  1455. MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
  1456. ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
  1457. ssl, p, extension_data_end);
  1458. if (ret != 0) {
  1459. MBEDTLS_SSL_DEBUG_RET(
  1460. 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
  1461. return ret;
  1462. }
  1463. break;
  1464. #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
  1465. default:
  1466. MBEDTLS_SSL_PRINT_EXT(
  1467. 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
  1468. extension_type, "( ignored )");
  1469. break;
  1470. }
  1471. p += extension_data_len;
  1472. }
  1473. MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
  1474. handshake->received_extensions);
  1475. ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
  1476. MBEDTLS_SSL_HS_CLIENT_HELLO,
  1477. p - buf);
  1478. if (0 != ret) {
  1479. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
  1480. return ret;
  1481. }
  1482. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1483. /* Update checksum with either
  1484. * - The entire content of the CH message, if no PSK extension is present
  1485. * - The content up to but excluding the PSK extension, if present.
  1486. * Always parse the pre-shared-key extension when present in the
  1487. * ClientHello even if some pre-requisites for PSK key exchange modes are
  1488. * not met. That way we always validate the syntax of the extension.
  1489. */
  1490. if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
  1491. ret = handshake->update_checksum(ssl, buf,
  1492. pre_shared_key_ext - buf);
  1493. if (0 != ret) {
  1494. MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
  1495. return ret;
  1496. }
  1497. ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
  1498. pre_shared_key_ext,
  1499. pre_shared_key_ext_end,
  1500. cipher_suites,
  1501. cipher_suites_end,
  1502. &psk);
  1503. if (ret == 0) {
  1504. got_psk = 1;
  1505. } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
  1506. MBEDTLS_SSL_DEBUG_RET(
  1507. 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
  1508. return ret;
  1509. }
  1510. } else
  1511. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
  1512. {
  1513. ret = handshake->update_checksum(ssl, buf, p - buf);
  1514. if (0 != ret) {
  1515. MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
  1516. return ret;
  1517. }
  1518. }
  1519. /*
  1520. * Determine the key exchange algorithm to use.
  1521. * There are three types of key exchanges supported in TLS 1.3:
  1522. * - (EC)DH with ECDSA,
  1523. * - (EC)DH with PSK,
  1524. * - plain PSK.
  1525. *
  1526. * The PSK-based key exchanges may additionally be used with 0-RTT.
  1527. *
  1528. * Our built-in order of preference is
  1529. * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
  1530. * 2 ) Certificate Mode ( ephemeral )
  1531. * 3 ) Plain PSK Mode ( psk )
  1532. */
  1533. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1534. if (got_psk && (psk.key_exchange_mode ==
  1535. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
  1536. handshake->key_exchange_mode =
  1537. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
  1538. MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
  1539. } else
  1540. #endif
  1541. if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
  1542. handshake->key_exchange_mode =
  1543. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
  1544. MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
  1545. }
  1546. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1547. else if (got_psk && (psk.key_exchange_mode ==
  1548. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
  1549. handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
  1550. MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
  1551. }
  1552. #endif
  1553. else {
  1554. MBEDTLS_SSL_DEBUG_MSG(
  1555. 1,
  1556. ("ClientHello message misses mandatory extensions."));
  1557. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
  1558. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1559. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1560. }
  1561. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1562. if (handshake->key_exchange_mode &
  1563. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
  1564. handshake->ciphersuite_info = psk.ciphersuite_info;
  1565. ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
  1566. MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
  1567. ((unsigned) psk.ciphersuite_info->id),
  1568. psk.ciphersuite_info->name));
  1569. if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
  1570. handshake->resume = 1;
  1571. }
  1572. }
  1573. #endif
  1574. if (handshake->key_exchange_mode !=
  1575. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
  1576. hrr_required = (no_usable_share_for_key_agreement != 0);
  1577. }
  1578. mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
  1579. return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
  1580. }
  1581. #if defined(MBEDTLS_SSL_EARLY_DATA)
  1582. static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
  1583. {
  1584. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  1585. if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
  1586. MBEDTLS_SSL_DEBUG_MSG(
  1587. 1,
  1588. ("EarlyData: rejected, feature disabled in server configuration."));
  1589. return -1;
  1590. }
  1591. if (!handshake->resume) {
  1592. /* We currently support early data only in the case of PSKs established
  1593. via a NewSessionTicket message thus in the case of a session
  1594. resumption. */
  1595. MBEDTLS_SSL_DEBUG_MSG(
  1596. 1, ("EarlyData: rejected, not a session resumption."));
  1597. return -1;
  1598. }
  1599. /* RFC 8446 4.2.10
  1600. *
  1601. * In order to accept early data, the server MUST have accepted a PSK cipher
  1602. * suite and selected the first key offered in the client's "pre_shared_key"
  1603. * extension. In addition, it MUST verify that the following values are the
  1604. * same as those associated with the selected PSK:
  1605. * - The TLS version number
  1606. * - The selected cipher suite
  1607. * - The selected ALPN [RFC7301] protocol, if any
  1608. *
  1609. * NOTE:
  1610. * - The TLS version number is checked in
  1611. * ssl_tls13_offered_psks_check_identity_match_ticket().
  1612. */
  1613. if (handshake->selected_identity != 0) {
  1614. MBEDTLS_SSL_DEBUG_MSG(
  1615. 1, ("EarlyData: rejected, the selected key in "
  1616. "`pre_shared_key` is not the first one."));
  1617. return -1;
  1618. }
  1619. if (handshake->ciphersuite_info->id !=
  1620. ssl->session_negotiate->ciphersuite) {
  1621. MBEDTLS_SSL_DEBUG_MSG(
  1622. 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
  1623. "of the selected pre-shared key."));
  1624. return -1;
  1625. }
  1626. if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
  1627. MBEDTLS_SSL_DEBUG_MSG(
  1628. 1,
  1629. ("EarlyData: rejected, early_data not allowed in ticket "
  1630. "permission bits."));
  1631. return -1;
  1632. }
  1633. #if defined(MBEDTLS_SSL_ALPN)
  1634. const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
  1635. size_t alpn_len;
  1636. if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
  1637. return 0;
  1638. }
  1639. if (alpn != NULL) {
  1640. alpn_len = strlen(alpn);
  1641. }
  1642. if (alpn == NULL ||
  1643. ssl->session_negotiate->ticket_alpn == NULL ||
  1644. alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
  1645. (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
  1646. MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
  1647. "from the one associated with the pre-shared key."));
  1648. return -1;
  1649. }
  1650. #endif
  1651. return 0;
  1652. }
  1653. #endif /* MBEDTLS_SSL_EARLY_DATA */
  1654. /* Update the handshake state machine */
  1655. MBEDTLS_CHECK_RETURN_CRITICAL
  1656. static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
  1657. int hrr_required)
  1658. {
  1659. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1660. /*
  1661. * Server certificate selection
  1662. */
  1663. if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
  1664. MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
  1665. return ret;
  1666. }
  1667. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  1668. ssl->handshake->sni_name = NULL;
  1669. ssl->handshake->sni_name_len = 0;
  1670. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  1671. ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
  1672. if (ret != 0) {
  1673. MBEDTLS_SSL_DEBUG_RET(1,
  1674. "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
  1675. return ret;
  1676. }
  1677. #if defined(MBEDTLS_SSL_EARLY_DATA)
  1678. if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
  1679. ssl->handshake->early_data_accepted =
  1680. (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
  1681. if (ssl->handshake->early_data_accepted) {
  1682. ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
  1683. if (ret != 0) {
  1684. MBEDTLS_SSL_DEBUG_RET(
  1685. 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
  1686. return ret;
  1687. }
  1688. } else {
  1689. ssl->discard_early_data_record =
  1690. hrr_required ?
  1691. MBEDTLS_SSL_EARLY_DATA_DISCARD :
  1692. MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
  1693. }
  1694. }
  1695. #else
  1696. ((void) hrr_required);
  1697. #endif /* MBEDTLS_SSL_EARLY_DATA */
  1698. return 0;
  1699. }
  1700. /*
  1701. * Main entry point from the state machine; orchestrates the otherfunctions.
  1702. */
  1703. MBEDTLS_CHECK_RETURN_CRITICAL
  1704. static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
  1705. {
  1706. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1707. unsigned char *buf = NULL;
  1708. size_t buflen = 0;
  1709. int parse_client_hello_ret;
  1710. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
  1711. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
  1712. ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
  1713. &buf, &buflen));
  1714. MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
  1715. buf + buflen));
  1716. parse_client_hello_ret = ret; /* Store positive return value of
  1717. * parse_client_hello,
  1718. * as negative error codes are handled
  1719. * by MBEDTLS_SSL_PROC_CHK_NEG. */
  1720. /*
  1721. * Version 1.2 of the protocol has to be used for the handshake.
  1722. * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
  1723. * ssl->keep_current_message flag for the ClientHello to be kept and parsed
  1724. * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
  1725. * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
  1726. * will dispatch to the TLS 1.2 state machine.
  1727. */
  1728. if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
  1729. /* Check if server supports TLS 1.2 */
  1730. if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
  1731. MBEDTLS_SSL_DEBUG_MSG(
  1732. 1, ("TLS 1.2 not supported."));
  1733. MBEDTLS_SSL_PEND_FATAL_ALERT(
  1734. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
  1735. MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
  1736. return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
  1737. }
  1738. ssl->keep_current_message = 1;
  1739. ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
  1740. return 0;
  1741. }
  1742. MBEDTLS_SSL_PROC_CHK(
  1743. ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
  1744. SSL_CLIENT_HELLO_HRR_REQUIRED));
  1745. if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
  1746. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
  1747. } else {
  1748. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
  1749. }
  1750. cleanup:
  1751. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
  1752. return ret;
  1753. }
  1754. /*
  1755. * Handler for MBEDTLS_SSL_SERVER_HELLO
  1756. */
  1757. MBEDTLS_CHECK_RETURN_CRITICAL
  1758. static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
  1759. {
  1760. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1761. unsigned char *server_randbytes =
  1762. ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
  1763. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
  1764. MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
  1765. MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
  1766. return ret;
  1767. }
  1768. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
  1769. MBEDTLS_SERVER_HELLO_RANDOM_LEN);
  1770. #if defined(MBEDTLS_HAVE_TIME)
  1771. ssl->session_negotiate->start = mbedtls_time(NULL);
  1772. #endif /* MBEDTLS_HAVE_TIME */
  1773. return ret;
  1774. }
  1775. /*
  1776. * ssl_tls13_write_server_hello_supported_versions_ext ():
  1777. *
  1778. * struct {
  1779. * ProtocolVersion selected_version;
  1780. * } SupportedVersions;
  1781. */
  1782. MBEDTLS_CHECK_RETURN_CRITICAL
  1783. static int ssl_tls13_write_server_hello_supported_versions_ext(
  1784. mbedtls_ssl_context *ssl,
  1785. unsigned char *buf,
  1786. unsigned char *end,
  1787. size_t *out_len)
  1788. {
  1789. *out_len = 0;
  1790. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
  1791. /* Check if we have space to write the extension:
  1792. * - extension_type (2 bytes)
  1793. * - extension_data_length (2 bytes)
  1794. * - selected_version (2 bytes)
  1795. */
  1796. MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
  1797. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
  1798. MBEDTLS_PUT_UINT16_BE(2, buf, 2);
  1799. mbedtls_ssl_write_version(buf + 4,
  1800. ssl->conf->transport,
  1801. ssl->tls_version);
  1802. MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
  1803. ssl->tls_version));
  1804. *out_len = 6;
  1805. mbedtls_ssl_tls13_set_hs_sent_ext_mask(
  1806. ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
  1807. return 0;
  1808. }
  1809. /* Generate and export a single key share. For hybrid KEMs, this can
  1810. * be called multiple times with the different components of the hybrid. */
  1811. MBEDTLS_CHECK_RETURN_CRITICAL
  1812. static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
  1813. uint16_t named_group,
  1814. unsigned char *buf,
  1815. unsigned char *end,
  1816. size_t *out_len)
  1817. {
  1818. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1819. *out_len = 0;
  1820. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
  1821. if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
  1822. mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
  1823. ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
  1824. ssl, named_group, buf, end, out_len);
  1825. if (ret != 0) {
  1826. MBEDTLS_SSL_DEBUG_RET(
  1827. 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
  1828. ret);
  1829. return ret;
  1830. }
  1831. } else
  1832. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
  1833. if (0 /* Other kinds of KEMs */) {
  1834. } else {
  1835. ((void) ssl);
  1836. ((void) named_group);
  1837. ((void) buf);
  1838. ((void) end);
  1839. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1840. }
  1841. return ret;
  1842. }
  1843. /*
  1844. * ssl_tls13_write_key_share_ext
  1845. *
  1846. * Structure of key_share extension in ServerHello:
  1847. *
  1848. * struct {
  1849. * NamedGroup group;
  1850. * opaque key_exchange<1..2^16-1>;
  1851. * } KeyShareEntry;
  1852. * struct {
  1853. * KeyShareEntry server_share;
  1854. * } KeyShareServerHello;
  1855. */
  1856. MBEDTLS_CHECK_RETURN_CRITICAL
  1857. static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
  1858. unsigned char *buf,
  1859. unsigned char *end,
  1860. size_t *out_len)
  1861. {
  1862. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1863. unsigned char *p = buf;
  1864. uint16_t group = ssl->handshake->offered_group_id;
  1865. unsigned char *server_share = buf + 4;
  1866. size_t key_exchange_length;
  1867. *out_len = 0;
  1868. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
  1869. MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
  1870. mbedtls_ssl_named_group_to_str(group),
  1871. group));
  1872. /* Check if we have space for header and length fields:
  1873. * - extension_type (2 bytes)
  1874. * - extension_data_length (2 bytes)
  1875. * - group (2 bytes)
  1876. * - key_exchange_length (2 bytes)
  1877. */
  1878. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
  1879. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
  1880. MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
  1881. p += 8;
  1882. /* When we introduce PQC-ECDHE hybrids, we'll want to call this
  1883. * function multiple times. */
  1884. ret = ssl_tls13_generate_and_write_key_share(
  1885. ssl, group, server_share + 4, end, &key_exchange_length);
  1886. if (ret != 0) {
  1887. return ret;
  1888. }
  1889. p += key_exchange_length;
  1890. MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
  1891. MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
  1892. *out_len = p - buf;
  1893. mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
  1894. return 0;
  1895. }
  1896. MBEDTLS_CHECK_RETURN_CRITICAL
  1897. static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
  1898. unsigned char *buf,
  1899. unsigned char *end,
  1900. size_t *out_len)
  1901. {
  1902. uint16_t selected_group = ssl->handshake->hrr_selected_group;
  1903. /* key_share Extension
  1904. *
  1905. * struct {
  1906. * select (Handshake.msg_type) {
  1907. * ...
  1908. * case hello_retry_request:
  1909. * NamedGroup selected_group;
  1910. * ...
  1911. * };
  1912. * } KeyShare;
  1913. */
  1914. *out_len = 0;
  1915. /*
  1916. * For a pure PSK key exchange, there is no group to agree upon. The purpose
  1917. * of the HRR is then to transmit a cookie to force the client to demonstrate
  1918. * reachability at their apparent network address (primarily useful for DTLS).
  1919. */
  1920. if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
  1921. return 0;
  1922. }
  1923. /* We should only send the key_share extension if the client's initial
  1924. * key share was not acceptable. */
  1925. if (ssl->handshake->offered_group_id != 0) {
  1926. MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
  1927. return 0;
  1928. }
  1929. if (selected_group == 0) {
  1930. MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
  1931. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1932. }
  1933. /* Check if we have enough space:
  1934. * - extension_type (2 bytes)
  1935. * - extension_data_length (2 bytes)
  1936. * - selected_group (2 bytes)
  1937. */
  1938. MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
  1939. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
  1940. MBEDTLS_PUT_UINT16_BE(2, buf, 2);
  1941. MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
  1942. MBEDTLS_SSL_DEBUG_MSG(3,
  1943. ("HRR selected_group: %s (%x)",
  1944. mbedtls_ssl_named_group_to_str(selected_group),
  1945. selected_group));
  1946. *out_len = 6;
  1947. mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
  1948. return 0;
  1949. }
  1950. /*
  1951. * Structure of ServerHello message:
  1952. *
  1953. * struct {
  1954. * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
  1955. * Random random;
  1956. * opaque legacy_session_id_echo<0..32>;
  1957. * CipherSuite cipher_suite;
  1958. * uint8 legacy_compression_method = 0;
  1959. * Extension extensions<6..2^16-1>;
  1960. * } ServerHello;
  1961. */
  1962. MBEDTLS_CHECK_RETURN_CRITICAL
  1963. static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
  1964. unsigned char *buf,
  1965. unsigned char *end,
  1966. size_t *out_len,
  1967. int is_hrr)
  1968. {
  1969. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1970. unsigned char *p = buf;
  1971. unsigned char *p_extensions_len;
  1972. size_t output_len;
  1973. *out_len = 0;
  1974. ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
  1975. /* ...
  1976. * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
  1977. * ...
  1978. * with ProtocolVersion defined as:
  1979. * uint16 ProtocolVersion;
  1980. */
  1981. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  1982. MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
  1983. p += 2;
  1984. /* ...
  1985. * Random random;
  1986. * ...
  1987. * with Random defined as:
  1988. * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
  1989. */
  1990. MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
  1991. if (is_hrr) {
  1992. memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
  1993. MBEDTLS_SERVER_HELLO_RANDOM_LEN);
  1994. } else {
  1995. memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
  1996. MBEDTLS_SERVER_HELLO_RANDOM_LEN);
  1997. }
  1998. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
  1999. p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
  2000. p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
  2001. /* ...
  2002. * opaque legacy_session_id_echo<0..32>;
  2003. * ...
  2004. */
  2005. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
  2006. *p++ = (unsigned char) ssl->session_negotiate->id_len;
  2007. if (ssl->session_negotiate->id_len > 0) {
  2008. memcpy(p, &ssl->session_negotiate->id[0],
  2009. ssl->session_negotiate->id_len);
  2010. p += ssl->session_negotiate->id_len;
  2011. MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
  2012. ssl->session_negotiate->id_len);
  2013. }
  2014. /* ...
  2015. * CipherSuite cipher_suite;
  2016. * ...
  2017. * with CipherSuite defined as:
  2018. * uint8 CipherSuite[2];
  2019. */
  2020. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  2021. MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
  2022. p += 2;
  2023. MBEDTLS_SSL_DEBUG_MSG(3,
  2024. ("server hello, chosen ciphersuite: %s ( id=%d )",
  2025. mbedtls_ssl_get_ciphersuite_name(
  2026. ssl->session_negotiate->ciphersuite),
  2027. ssl->session_negotiate->ciphersuite));
  2028. /* ...
  2029. * uint8 legacy_compression_method = 0;
  2030. * ...
  2031. */
  2032. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
  2033. *p++ = MBEDTLS_SSL_COMPRESS_NULL;
  2034. /* ...
  2035. * Extension extensions<6..2^16-1>;
  2036. * ...
  2037. * struct {
  2038. * ExtensionType extension_type; (2 bytes)
  2039. * opaque extension_data<0..2^16-1>;
  2040. * } Extension;
  2041. */
  2042. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  2043. p_extensions_len = p;
  2044. p += 2;
  2045. if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
  2046. ssl, p, end, &output_len)) != 0) {
  2047. MBEDTLS_SSL_DEBUG_RET(
  2048. 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
  2049. return ret;
  2050. }
  2051. p += output_len;
  2052. if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
  2053. if (is_hrr) {
  2054. ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
  2055. } else {
  2056. ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
  2057. }
  2058. if (ret != 0) {
  2059. return ret;
  2060. }
  2061. p += output_len;
  2062. }
  2063. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  2064. if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
  2065. ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
  2066. if (ret != 0) {
  2067. MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
  2068. ret);
  2069. return ret;
  2070. }
  2071. p += output_len;
  2072. }
  2073. #endif
  2074. MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
  2075. MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
  2076. p_extensions_len, p - p_extensions_len);
  2077. *out_len = p - buf;
  2078. MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
  2079. MBEDTLS_SSL_PRINT_EXTS(
  2080. 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
  2081. MBEDTLS_SSL_HS_SERVER_HELLO,
  2082. ssl->handshake->sent_extensions);
  2083. return ret;
  2084. }
  2085. MBEDTLS_CHECK_RETURN_CRITICAL
  2086. static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
  2087. {
  2088. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2089. ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
  2090. if (ret != 0) {
  2091. MBEDTLS_SSL_DEBUG_RET(1,
  2092. "mbedtls_ssl_tls13_compute_handshake_transform",
  2093. ret);
  2094. return ret;
  2095. }
  2096. return ret;
  2097. }
  2098. MBEDTLS_CHECK_RETURN_CRITICAL
  2099. static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
  2100. {
  2101. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2102. unsigned char *buf;
  2103. size_t buf_len, msg_len;
  2104. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
  2105. MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
  2106. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
  2107. ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
  2108. MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
  2109. buf + buf_len,
  2110. &msg_len,
  2111. 0));
  2112. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
  2113. ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
  2114. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
  2115. ssl, buf_len, msg_len));
  2116. MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
  2117. #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
  2118. /* The server sends a dummy change_cipher_spec record immediately
  2119. * after its first handshake message. This may either be after
  2120. * a ServerHello or a HelloRetryRequest.
  2121. */
  2122. mbedtls_ssl_handshake_set_state(
  2123. ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
  2124. #else
  2125. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
  2126. #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
  2127. cleanup:
  2128. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
  2129. return ret;
  2130. }
  2131. /*
  2132. * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
  2133. */
  2134. MBEDTLS_CHECK_RETURN_CRITICAL
  2135. static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
  2136. {
  2137. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2138. if (ssl->handshake->hello_retry_request_flag) {
  2139. MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
  2140. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  2141. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  2142. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  2143. }
  2144. /*
  2145. * Create stateless transcript hash for HRR
  2146. */
  2147. MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
  2148. ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
  2149. if (ret != 0) {
  2150. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
  2151. return ret;
  2152. }
  2153. mbedtls_ssl_session_reset_msg_layer(ssl, 0);
  2154. return 0;
  2155. }
  2156. MBEDTLS_CHECK_RETURN_CRITICAL
  2157. static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
  2158. {
  2159. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2160. unsigned char *buf;
  2161. size_t buf_len, msg_len;
  2162. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
  2163. MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
  2164. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
  2165. ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
  2166. &buf, &buf_len));
  2167. MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
  2168. buf + buf_len,
  2169. &msg_len,
  2170. 1));
  2171. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
  2172. ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
  2173. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
  2174. msg_len));
  2175. ssl->handshake->hello_retry_request_flag = 1;
  2176. #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
  2177. /* The server sends a dummy change_cipher_spec record immediately
  2178. * after its first handshake message. This may either be after
  2179. * a ServerHello or a HelloRetryRequest.
  2180. */
  2181. mbedtls_ssl_handshake_set_state(
  2182. ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
  2183. #else
  2184. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
  2185. #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
  2186. cleanup:
  2187. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
  2188. return ret;
  2189. }
  2190. /*
  2191. * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
  2192. */
  2193. /*
  2194. * struct {
  2195. * Extension extensions<0..2 ^ 16 - 1>;
  2196. * } EncryptedExtensions;
  2197. *
  2198. */
  2199. MBEDTLS_CHECK_RETURN_CRITICAL
  2200. static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
  2201. unsigned char *buf,
  2202. unsigned char *end,
  2203. size_t *out_len)
  2204. {
  2205. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2206. unsigned char *p = buf;
  2207. size_t extensions_len = 0;
  2208. unsigned char *p_extensions_len;
  2209. size_t output_len;
  2210. *out_len = 0;
  2211. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  2212. p_extensions_len = p;
  2213. p += 2;
  2214. ((void) ssl);
  2215. ((void) ret);
  2216. ((void) output_len);
  2217. #if defined(MBEDTLS_SSL_ALPN)
  2218. ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
  2219. if (ret != 0) {
  2220. return ret;
  2221. }
  2222. p += output_len;
  2223. #endif /* MBEDTLS_SSL_ALPN */
  2224. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2225. if (ssl->handshake->early_data_accepted) {
  2226. ret = mbedtls_ssl_tls13_write_early_data_ext(
  2227. ssl, 0, p, end, &output_len);
  2228. if (ret != 0) {
  2229. return ret;
  2230. }
  2231. p += output_len;
  2232. }
  2233. #endif /* MBEDTLS_SSL_EARLY_DATA */
  2234. #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
  2235. if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
  2236. ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
  2237. ssl, p, end, &output_len);
  2238. if (ret != 0) {
  2239. return ret;
  2240. }
  2241. p += output_len;
  2242. }
  2243. #endif
  2244. extensions_len = (p - p_extensions_len) - 2;
  2245. MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
  2246. *out_len = p - buf;
  2247. MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
  2248. MBEDTLS_SSL_PRINT_EXTS(
  2249. 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
  2250. return 0;
  2251. }
  2252. MBEDTLS_CHECK_RETURN_CRITICAL
  2253. static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
  2254. {
  2255. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2256. unsigned char *buf;
  2257. size_t buf_len, msg_len;
  2258. mbedtls_ssl_set_outbound_transform(ssl,
  2259. ssl->handshake->transform_handshake);
  2260. MBEDTLS_SSL_DEBUG_MSG(
  2261. 3, ("switching to handshake transform for outbound data"));
  2262. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
  2263. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
  2264. ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
  2265. &buf, &buf_len));
  2266. MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
  2267. ssl, buf, buf + buf_len, &msg_len));
  2268. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
  2269. ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
  2270. buf, msg_len));
  2271. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
  2272. ssl, buf_len, msg_len));
  2273. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  2274. if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
  2275. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
  2276. } else {
  2277. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
  2278. }
  2279. #else
  2280. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
  2281. #endif
  2282. cleanup:
  2283. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
  2284. return ret;
  2285. }
  2286. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  2287. #define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
  2288. #define SSL_CERTIFICATE_REQUEST_SKIP 1
  2289. /* Coordination:
  2290. * Check whether a CertificateRequest message should be written.
  2291. * Returns a negative code on failure, or
  2292. * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
  2293. * - SSL_CERTIFICATE_REQUEST_SKIP
  2294. * indicating if the writing of the CertificateRequest
  2295. * should be skipped or not.
  2296. */
  2297. MBEDTLS_CHECK_RETURN_CRITICAL
  2298. static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
  2299. {
  2300. int authmode;
  2301. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  2302. if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
  2303. authmode = ssl->handshake->sni_authmode;
  2304. } else
  2305. #endif
  2306. authmode = ssl->conf->authmode;
  2307. if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
  2308. ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
  2309. return SSL_CERTIFICATE_REQUEST_SKIP;
  2310. }
  2311. ssl->handshake->certificate_request_sent = 1;
  2312. return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
  2313. }
  2314. /*
  2315. * struct {
  2316. * opaque certificate_request_context<0..2^8-1>;
  2317. * Extension extensions<2..2^16-1>;
  2318. * } CertificateRequest;
  2319. *
  2320. */
  2321. MBEDTLS_CHECK_RETURN_CRITICAL
  2322. static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
  2323. unsigned char *buf,
  2324. const unsigned char *end,
  2325. size_t *out_len)
  2326. {
  2327. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2328. unsigned char *p = buf;
  2329. size_t output_len = 0;
  2330. unsigned char *p_extensions_len;
  2331. *out_len = 0;
  2332. /* Check if we have enough space:
  2333. * - certificate_request_context (1 byte)
  2334. * - extensions length (2 bytes)
  2335. */
  2336. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
  2337. /*
  2338. * Write certificate_request_context
  2339. */
  2340. /*
  2341. * We use a zero length context for the normal handshake
  2342. * messages. For post-authentication handshake messages
  2343. * this request context would be set to a non-zero value.
  2344. */
  2345. *p++ = 0x0;
  2346. /*
  2347. * Write extensions
  2348. */
  2349. /* The extensions must contain the signature_algorithms. */
  2350. p_extensions_len = p;
  2351. p += 2;
  2352. ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
  2353. if (ret != 0) {
  2354. return ret;
  2355. }
  2356. p += output_len;
  2357. MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
  2358. *out_len = p - buf;
  2359. MBEDTLS_SSL_PRINT_EXTS(
  2360. 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
  2361. return 0;
  2362. }
  2363. MBEDTLS_CHECK_RETURN_CRITICAL
  2364. static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
  2365. {
  2366. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2367. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
  2368. MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
  2369. if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
  2370. unsigned char *buf;
  2371. size_t buf_len, msg_len;
  2372. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
  2373. ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
  2374. &buf, &buf_len));
  2375. MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
  2376. ssl, buf, buf + buf_len, &msg_len));
  2377. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
  2378. ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
  2379. buf, msg_len));
  2380. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
  2381. ssl, buf_len, msg_len));
  2382. } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
  2383. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
  2384. ret = 0;
  2385. } else {
  2386. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2387. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2388. goto cleanup;
  2389. }
  2390. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
  2391. cleanup:
  2392. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
  2393. return ret;
  2394. }
  2395. /*
  2396. * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
  2397. */
  2398. MBEDTLS_CHECK_RETURN_CRITICAL
  2399. static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
  2400. {
  2401. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2402. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2403. if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
  2404. mbedtls_ssl_own_cert(ssl) == NULL) {
  2405. MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
  2406. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  2407. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  2408. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  2409. }
  2410. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2411. ret = mbedtls_ssl_tls13_write_certificate(ssl);
  2412. if (ret != 0) {
  2413. return ret;
  2414. }
  2415. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
  2416. return 0;
  2417. }
  2418. /*
  2419. * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
  2420. */
  2421. MBEDTLS_CHECK_RETURN_CRITICAL
  2422. static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
  2423. {
  2424. int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
  2425. if (ret != 0) {
  2426. return ret;
  2427. }
  2428. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
  2429. return 0;
  2430. }
  2431. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  2432. /*
  2433. * RFC 8446 section A.2
  2434. *
  2435. * | Send ServerHello
  2436. * | K_send = handshake
  2437. * | Send EncryptedExtensions
  2438. * | [Send CertificateRequest]
  2439. * Can send | [Send Certificate + CertificateVerify]
  2440. * app data | Send Finished
  2441. * after --> | K_send = application
  2442. * here +--------+--------+
  2443. * No 0-RTT | | 0-RTT
  2444. * | |
  2445. * K_recv = handshake | | K_recv = early data
  2446. * [Skip decrypt errors] | +------> WAIT_EOED -+
  2447. * | | Recv | | Recv EndOfEarlyData
  2448. * | | early data | | K_recv = handshake
  2449. * | +------------+ |
  2450. * | |
  2451. * +> WAIT_FLIGHT2 <--------+
  2452. * |
  2453. * +--------+--------+
  2454. * No auth | | Client auth
  2455. * | |
  2456. * | v
  2457. * | WAIT_CERT
  2458. * | Recv | | Recv Certificate
  2459. * | empty | v
  2460. * | Certificate | WAIT_CV
  2461. * | | | Recv
  2462. * | v | CertificateVerify
  2463. * +-> WAIT_FINISHED <---+
  2464. * | Recv Finished
  2465. *
  2466. *
  2467. * The following function handles the state changes after WAIT_FLIGHT2 in the
  2468. * above diagram. We are not going to receive early data related messages
  2469. * anymore, prepare to receive the first handshake message of the client
  2470. * second flight.
  2471. */
  2472. static void ssl_tls13_prepare_for_handshake_second_flight(
  2473. mbedtls_ssl_context *ssl)
  2474. {
  2475. if (ssl->handshake->certificate_request_sent) {
  2476. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
  2477. } else {
  2478. MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
  2479. MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
  2480. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
  2481. }
  2482. }
  2483. /*
  2484. * Handler for MBEDTLS_SSL_SERVER_FINISHED
  2485. */
  2486. MBEDTLS_CHECK_RETURN_CRITICAL
  2487. static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
  2488. {
  2489. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2490. ret = mbedtls_ssl_tls13_write_finished_message(ssl);
  2491. if (ret != 0) {
  2492. return ret;
  2493. }
  2494. ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
  2495. if (ret != 0) {
  2496. MBEDTLS_SSL_PEND_FATAL_ALERT(
  2497. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  2498. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  2499. return ret;
  2500. }
  2501. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2502. if (ssl->handshake->early_data_accepted) {
  2503. /* See RFC 8446 section A.2 for more information */
  2504. MBEDTLS_SSL_DEBUG_MSG(
  2505. 1, ("Switch to early keys for inbound traffic. "
  2506. "( K_recv = early data )"));
  2507. mbedtls_ssl_set_inbound_transform(
  2508. ssl, ssl->handshake->transform_earlydata);
  2509. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
  2510. return 0;
  2511. }
  2512. #endif /* MBEDTLS_SSL_EARLY_DATA */
  2513. MBEDTLS_SSL_DEBUG_MSG(
  2514. 1, ("Switch to handshake keys for inbound traffic "
  2515. "( K_recv = handshake )"));
  2516. mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
  2517. ssl_tls13_prepare_for_handshake_second_flight(ssl);
  2518. return 0;
  2519. }
  2520. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2521. /*
  2522. * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
  2523. */
  2524. #define SSL_GOT_END_OF_EARLY_DATA 0
  2525. #define SSL_GOT_EARLY_DATA 1
  2526. /* Coordination:
  2527. * Deals with the ambiguity of not knowing if the next message is an
  2528. * EndOfEarlyData message or an application message containing early data.
  2529. * Returns a negative code on failure, or
  2530. * - SSL_GOT_END_OF_EARLY_DATA
  2531. * - SSL_GOT_EARLY_DATA
  2532. * indicating which message is received.
  2533. */
  2534. MBEDTLS_CHECK_RETURN_CRITICAL
  2535. static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
  2536. {
  2537. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2538. if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
  2539. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  2540. return ret;
  2541. }
  2542. ssl->keep_current_message = 1;
  2543. if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
  2544. ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
  2545. MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
  2546. return SSL_GOT_END_OF_EARLY_DATA;
  2547. }
  2548. if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
  2549. if (ssl->in_offt == NULL) {
  2550. MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
  2551. /* Set the reading pointer */
  2552. ssl->in_offt = ssl->in_msg;
  2553. ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
  2554. if (ret != 0) {
  2555. return ret;
  2556. }
  2557. }
  2558. return SSL_GOT_EARLY_DATA;
  2559. }
  2560. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
  2561. MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
  2562. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  2563. }
  2564. MBEDTLS_CHECK_RETURN_CRITICAL
  2565. static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
  2566. const unsigned char *buf,
  2567. const unsigned char *end)
  2568. {
  2569. /* RFC 8446 section 4.5
  2570. *
  2571. * struct {} EndOfEarlyData;
  2572. */
  2573. if (buf != end) {
  2574. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  2575. MBEDTLS_ERR_SSL_DECODE_ERROR);
  2576. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2577. }
  2578. return 0;
  2579. }
  2580. /*
  2581. * RFC 8446 section A.2
  2582. *
  2583. * | Send ServerHello
  2584. * | K_send = handshake
  2585. * | Send EncryptedExtensions
  2586. * | [Send CertificateRequest]
  2587. * Can send | [Send Certificate + CertificateVerify]
  2588. * app data | Send Finished
  2589. * after --> | K_send = application
  2590. * here +--------+--------+
  2591. * No 0-RTT | | 0-RTT
  2592. * | |
  2593. * K_recv = handshake | | K_recv = early data
  2594. * [Skip decrypt errors] | +------> WAIT_EOED -+
  2595. * | | Recv | | Recv EndOfEarlyData
  2596. * | | early data | | K_recv = handshake
  2597. * | +------------+ |
  2598. * | |
  2599. * +> WAIT_FLIGHT2 <--------+
  2600. * |
  2601. * +--------+--------+
  2602. * No auth | | Client auth
  2603. * | |
  2604. * | v
  2605. * | WAIT_CERT
  2606. * | Recv | | Recv Certificate
  2607. * | empty | v
  2608. * | Certificate | WAIT_CV
  2609. * | | | Recv
  2610. * | v | CertificateVerify
  2611. * +-> WAIT_FINISHED <---+
  2612. * | Recv Finished
  2613. *
  2614. * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
  2615. * the above diagram.
  2616. */
  2617. MBEDTLS_CHECK_RETURN_CRITICAL
  2618. static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
  2619. {
  2620. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2621. MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
  2622. MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
  2623. if (ret == SSL_GOT_END_OF_EARLY_DATA) {
  2624. unsigned char *buf;
  2625. size_t buf_len;
  2626. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
  2627. ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
  2628. &buf, &buf_len));
  2629. MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
  2630. ssl, buf, buf + buf_len));
  2631. MBEDTLS_SSL_DEBUG_MSG(
  2632. 1, ("Switch to handshake keys for inbound traffic"
  2633. "( K_recv = handshake )"));
  2634. mbedtls_ssl_set_inbound_transform(
  2635. ssl, ssl->handshake->transform_handshake);
  2636. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
  2637. ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
  2638. buf, buf_len));
  2639. ssl_tls13_prepare_for_handshake_second_flight(ssl);
  2640. } else if (ret == SSL_GOT_EARLY_DATA) {
  2641. ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
  2642. goto cleanup;
  2643. } else {
  2644. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2645. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2646. goto cleanup;
  2647. }
  2648. cleanup:
  2649. MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
  2650. return ret;
  2651. }
  2652. #endif /* MBEDTLS_SSL_EARLY_DATA */
  2653. /*
  2654. * Handler for MBEDTLS_SSL_CLIENT_FINISHED
  2655. */
  2656. MBEDTLS_CHECK_RETURN_CRITICAL
  2657. static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
  2658. {
  2659. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2660. ret = mbedtls_ssl_tls13_process_finished_message(ssl);
  2661. if (ret != 0) {
  2662. return ret;
  2663. }
  2664. ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
  2665. if (ret != 0) {
  2666. MBEDTLS_SSL_DEBUG_RET(
  2667. 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
  2668. }
  2669. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
  2670. return 0;
  2671. }
  2672. /*
  2673. * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
  2674. */
  2675. MBEDTLS_CHECK_RETURN_CRITICAL
  2676. static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
  2677. {
  2678. MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
  2679. mbedtls_ssl_tls13_handshake_wrapup(ssl);
  2680. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
  2681. defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  2682. /* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
  2683. * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
  2684. * expected to be resolved with issue#6395.
  2685. */
  2686. /* Sent NewSessionTicket message only when client supports PSK */
  2687. if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
  2688. mbedtls_ssl_handshake_set_state(
  2689. ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
  2690. } else
  2691. #endif
  2692. {
  2693. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
  2694. }
  2695. return 0;
  2696. }
  2697. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2698. /*
  2699. * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
  2700. */
  2701. #define SSL_NEW_SESSION_TICKET_SKIP 0
  2702. #define SSL_NEW_SESSION_TICKET_WRITE 1
  2703. MBEDTLS_CHECK_RETURN_CRITICAL
  2704. static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
  2705. {
  2706. /* Check whether the use of session tickets is enabled */
  2707. if (ssl->conf->f_ticket_write == NULL) {
  2708. MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
  2709. " callback is not set"));
  2710. return SSL_NEW_SESSION_TICKET_SKIP;
  2711. }
  2712. if (ssl->conf->new_session_tickets_count == 0) {
  2713. MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
  2714. " configured count is zero"));
  2715. return SSL_NEW_SESSION_TICKET_SKIP;
  2716. }
  2717. if (ssl->handshake->new_session_tickets_count == 0) {
  2718. MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
  2719. "been sent."));
  2720. return SSL_NEW_SESSION_TICKET_SKIP;
  2721. }
  2722. return SSL_NEW_SESSION_TICKET_WRITE;
  2723. }
  2724. MBEDTLS_CHECK_RETURN_CRITICAL
  2725. static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
  2726. unsigned char *ticket_nonce,
  2727. size_t ticket_nonce_size)
  2728. {
  2729. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2730. mbedtls_ssl_session *session = ssl->session;
  2731. mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  2732. psa_algorithm_t psa_hash_alg;
  2733. int hash_length;
  2734. MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
  2735. /* Set ticket_flags depends on the advertised psk key exchange mode */
  2736. mbedtls_ssl_tls13_session_clear_ticket_flags(
  2737. session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
  2738. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  2739. mbedtls_ssl_tls13_session_set_ticket_flags(
  2740. session, ssl->handshake->tls13_kex_modes);
  2741. #endif
  2742. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2743. if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
  2744. ssl->conf->max_early_data_size > 0) {
  2745. mbedtls_ssl_tls13_session_set_ticket_flags(
  2746. session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
  2747. session->max_early_data_size = ssl->conf->max_early_data_size;
  2748. }
  2749. #endif /* MBEDTLS_SSL_EARLY_DATA */
  2750. MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
  2751. #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
  2752. if (session->ticket_alpn == NULL) {
  2753. ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
  2754. if (ret != 0) {
  2755. return ret;
  2756. }
  2757. }
  2758. #endif
  2759. /* Generate ticket_age_add */
  2760. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
  2761. (unsigned char *) &session->ticket_age_add,
  2762. sizeof(session->ticket_age_add)) != 0)) {
  2763. MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
  2764. return ret;
  2765. }
  2766. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
  2767. (unsigned int) session->ticket_age_add));
  2768. /* Generate ticket_nonce */
  2769. ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
  2770. if (ret != 0) {
  2771. MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
  2772. return ret;
  2773. }
  2774. MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
  2775. ticket_nonce, ticket_nonce_size);
  2776. ciphersuite_info =
  2777. (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
  2778. psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
  2779. hash_length = PSA_HASH_LENGTH(psa_hash_alg);
  2780. if (hash_length == -1 ||
  2781. (size_t) hash_length > sizeof(session->resumption_key)) {
  2782. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2783. }
  2784. /* In this code the psk key length equals the length of the hash */
  2785. session->resumption_key_len = hash_length;
  2786. session->ciphersuite = ciphersuite_info->id;
  2787. /* Compute resumption key
  2788. *
  2789. * HKDF-Expand-Label( resumption_master_secret,
  2790. * "resumption", ticket_nonce, Hash.length )
  2791. */
  2792. ret = mbedtls_ssl_tls13_hkdf_expand_label(
  2793. psa_hash_alg,
  2794. session->app_secrets.resumption_master_secret,
  2795. hash_length,
  2796. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
  2797. ticket_nonce,
  2798. ticket_nonce_size,
  2799. session->resumption_key,
  2800. hash_length);
  2801. if (ret != 0) {
  2802. MBEDTLS_SSL_DEBUG_RET(2,
  2803. "Creating the ticket-resumed PSK failed",
  2804. ret);
  2805. return ret;
  2806. }
  2807. MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
  2808. session->resumption_key,
  2809. session->resumption_key_len);
  2810. MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
  2811. session->app_secrets.resumption_master_secret,
  2812. hash_length);
  2813. return 0;
  2814. }
  2815. /* This function creates a NewSessionTicket message in the following format:
  2816. *
  2817. * struct {
  2818. * uint32 ticket_lifetime;
  2819. * uint32 ticket_age_add;
  2820. * opaque ticket_nonce<0..255>;
  2821. * opaque ticket<1..2^16-1>;
  2822. * Extension extensions<0..2^16-2>;
  2823. * } NewSessionTicket;
  2824. *
  2825. * The ticket inside the NewSessionTicket message is an encrypted container
  2826. * carrying the necessary information so that the server is later able to
  2827. * re-start the communication.
  2828. *
  2829. * The following fields are placed inside the ticket by the
  2830. * f_ticket_write() function:
  2831. *
  2832. * - creation time (ticket_creation_time)
  2833. * - flags (ticket_flags)
  2834. * - age add (ticket_age_add)
  2835. * - key (resumption_key)
  2836. * - key length (resumption_key_len)
  2837. * - ciphersuite (ciphersuite)
  2838. * - max_early_data_size (max_early_data_size)
  2839. */
  2840. MBEDTLS_CHECK_RETURN_CRITICAL
  2841. static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
  2842. unsigned char *buf,
  2843. unsigned char *end,
  2844. size_t *out_len,
  2845. unsigned char *ticket_nonce,
  2846. size_t ticket_nonce_size)
  2847. {
  2848. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2849. unsigned char *p = buf;
  2850. mbedtls_ssl_session *session = ssl->session;
  2851. size_t ticket_len;
  2852. uint32_t ticket_lifetime;
  2853. unsigned char *p_extensions_len;
  2854. *out_len = 0;
  2855. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
  2856. /*
  2857. * ticket_lifetime 4 bytes
  2858. * ticket_age_add 4 bytes
  2859. * ticket_nonce 1 + ticket_nonce_size bytes
  2860. * ticket >=2 bytes
  2861. */
  2862. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
  2863. /* Generate ticket and ticket_lifetime */
  2864. #if defined(MBEDTLS_HAVE_TIME)
  2865. session->ticket_creation_time = mbedtls_ms_time();
  2866. #endif
  2867. ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
  2868. session,
  2869. p + 9 + ticket_nonce_size + 2,
  2870. end,
  2871. &ticket_len,
  2872. &ticket_lifetime);
  2873. if (ret != 0) {
  2874. MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
  2875. return ret;
  2876. }
  2877. /* RFC 8446 section 4.6.1
  2878. *
  2879. * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
  2880. * unsigned integer in network byte order from the time of ticket
  2881. * issuance. Servers MUST NOT use any value greater than
  2882. * 604800 seconds (7 days) ...
  2883. */
  2884. if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
  2885. MBEDTLS_SSL_DEBUG_MSG(
  2886. 1, ("Ticket lifetime (%u) is greater than 7 days.",
  2887. (unsigned int) ticket_lifetime));
  2888. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  2889. }
  2890. MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
  2891. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
  2892. (unsigned int) ticket_lifetime));
  2893. /* Write ticket_age_add */
  2894. MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
  2895. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
  2896. (unsigned int) session->ticket_age_add));
  2897. /* Write ticket_nonce */
  2898. p[8] = (unsigned char) ticket_nonce_size;
  2899. if (ticket_nonce_size > 0) {
  2900. memcpy(p + 9, ticket_nonce, ticket_nonce_size);
  2901. }
  2902. p += 9 + ticket_nonce_size;
  2903. /* Write ticket */
  2904. MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
  2905. p += 2;
  2906. MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
  2907. p += ticket_len;
  2908. /* Ticket Extensions
  2909. *
  2910. * Extension extensions<0..2^16-2>;
  2911. */
  2912. ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
  2913. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  2914. p_extensions_len = p;
  2915. p += 2;
  2916. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2917. if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
  2918. size_t output_len;
  2919. if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
  2920. ssl, 1, p, end, &output_len)) != 0) {
  2921. MBEDTLS_SSL_DEBUG_RET(
  2922. 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
  2923. return ret;
  2924. }
  2925. p += output_len;
  2926. } else {
  2927. MBEDTLS_SSL_DEBUG_MSG(
  2928. 4, ("early_data not allowed, "
  2929. "skip early_data extension in NewSessionTicket"));
  2930. }
  2931. #endif /* MBEDTLS_SSL_EARLY_DATA */
  2932. MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
  2933. *out_len = p - buf;
  2934. MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
  2935. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
  2936. MBEDTLS_SSL_PRINT_EXTS(
  2937. 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
  2938. return 0;
  2939. }
  2940. /*
  2941. * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
  2942. */
  2943. static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
  2944. {
  2945. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2946. MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
  2947. if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
  2948. unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
  2949. unsigned char *buf;
  2950. size_t buf_len, msg_len;
  2951. MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
  2952. ssl, ticket_nonce, sizeof(ticket_nonce)));
  2953. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
  2954. ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
  2955. &buf, &buf_len));
  2956. MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
  2957. ssl, buf, buf + buf_len, &msg_len,
  2958. ticket_nonce, sizeof(ticket_nonce)));
  2959. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
  2960. ssl, buf_len, msg_len));
  2961. /* Limit session tickets count to one when resumption connection.
  2962. *
  2963. * See document of mbedtls_ssl_conf_new_session_tickets.
  2964. */
  2965. if (ssl->handshake->resume == 1) {
  2966. ssl->handshake->new_session_tickets_count = 0;
  2967. } else {
  2968. ssl->handshake->new_session_tickets_count--;
  2969. }
  2970. mbedtls_ssl_handshake_set_state(
  2971. ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
  2972. } else {
  2973. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
  2974. }
  2975. cleanup:
  2976. return ret;
  2977. }
  2978. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  2979. /*
  2980. * TLS 1.3 State Machine -- server side
  2981. */
  2982. int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
  2983. {
  2984. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2985. if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
  2986. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  2987. }
  2988. MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
  2989. mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
  2990. ssl->state));
  2991. switch (ssl->state) {
  2992. /* start state */
  2993. case MBEDTLS_SSL_HELLO_REQUEST:
  2994. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
  2995. ret = 0;
  2996. break;
  2997. case MBEDTLS_SSL_CLIENT_HELLO:
  2998. ret = ssl_tls13_process_client_hello(ssl);
  2999. if (ret != 0) {
  3000. MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
  3001. }
  3002. break;
  3003. case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
  3004. ret = ssl_tls13_write_hello_retry_request(ssl);
  3005. if (ret != 0) {
  3006. MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
  3007. return ret;
  3008. }
  3009. break;
  3010. case MBEDTLS_SSL_SERVER_HELLO:
  3011. ret = ssl_tls13_write_server_hello(ssl);
  3012. break;
  3013. case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
  3014. ret = ssl_tls13_write_encrypted_extensions(ssl);
  3015. if (ret != 0) {
  3016. MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
  3017. return ret;
  3018. }
  3019. break;
  3020. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  3021. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  3022. ret = ssl_tls13_write_certificate_request(ssl);
  3023. break;
  3024. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  3025. ret = ssl_tls13_write_server_certificate(ssl);
  3026. break;
  3027. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  3028. ret = ssl_tls13_write_certificate_verify(ssl);
  3029. break;
  3030. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  3031. /*
  3032. * Injection of dummy-CCS's for middlebox compatibility
  3033. */
  3034. #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
  3035. case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
  3036. ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
  3037. if (ret == 0) {
  3038. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
  3039. }
  3040. break;
  3041. case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
  3042. ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
  3043. if (ret != 0) {
  3044. break;
  3045. }
  3046. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
  3047. break;
  3048. #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
  3049. case MBEDTLS_SSL_SERVER_FINISHED:
  3050. ret = ssl_tls13_write_server_finished(ssl);
  3051. break;
  3052. #if defined(MBEDTLS_SSL_EARLY_DATA)
  3053. case MBEDTLS_SSL_END_OF_EARLY_DATA:
  3054. ret = ssl_tls13_process_end_of_early_data(ssl);
  3055. break;
  3056. #endif /* MBEDTLS_SSL_EARLY_DATA */
  3057. case MBEDTLS_SSL_CLIENT_FINISHED:
  3058. ret = ssl_tls13_process_client_finished(ssl);
  3059. break;
  3060. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  3061. ret = ssl_tls13_handshake_wrapup(ssl);
  3062. break;
  3063. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  3064. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  3065. ret = mbedtls_ssl_tls13_process_certificate(ssl);
  3066. if (ret == 0) {
  3067. if (ssl->session_negotiate->peer_cert != NULL) {
  3068. mbedtls_ssl_handshake_set_state(
  3069. ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
  3070. } else {
  3071. MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
  3072. mbedtls_ssl_handshake_set_state(
  3073. ssl, MBEDTLS_SSL_CLIENT_FINISHED);
  3074. }
  3075. }
  3076. break;
  3077. case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
  3078. ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
  3079. if (ret == 0) {
  3080. mbedtls_ssl_handshake_set_state(
  3081. ssl, MBEDTLS_SSL_CLIENT_FINISHED);
  3082. }
  3083. break;
  3084. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  3085. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3086. case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
  3087. ret = ssl_tls13_write_new_session_ticket(ssl);
  3088. if (ret != 0) {
  3089. MBEDTLS_SSL_DEBUG_RET(1,
  3090. "ssl_tls13_write_new_session_ticket ",
  3091. ret);
  3092. }
  3093. break;
  3094. case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
  3095. /* This state is necessary to do the flush of the New Session
  3096. * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
  3097. * as part of ssl_prepare_handshake_step.
  3098. */
  3099. ret = 0;
  3100. if (ssl->handshake->new_session_tickets_count == 0) {
  3101. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
  3102. } else {
  3103. mbedtls_ssl_handshake_set_state(
  3104. ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
  3105. }
  3106. break;
  3107. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  3108. default:
  3109. MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
  3110. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3111. }
  3112. return ret;
  3113. }
  3114. #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */