ssl_tls13_server.c 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611
  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(1, ("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. MBEDTLS_SSL_DEBUG_MSG(2, ("no supported_versions extension"));
  1215. return SSL_CLIENT_HELLO_TLS1_2;
  1216. }
  1217. if (ret == 1) {
  1218. ret = ssl_tls13_parse_supported_versions_ext(ssl,
  1219. supported_versions_data,
  1220. supported_versions_data_end);
  1221. if (ret < 0) {
  1222. MBEDTLS_SSL_DEBUG_RET(1,
  1223. ("ssl_tls13_parse_supported_versions_ext"), ret);
  1224. return ret;
  1225. }
  1226. /*
  1227. * The supported versions extension was parsed successfully as the
  1228. * value returned by ssl_tls13_parse_supported_versions_ext() is
  1229. * positive. The return value is then equal to
  1230. * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
  1231. * the TLS version to negotiate.
  1232. */
  1233. if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
  1234. MBEDTLS_SSL_DEBUG_MSG(2, ("supported_versions without 1.3"));
  1235. return SSL_CLIENT_HELLO_TLS1_2;
  1236. }
  1237. }
  1238. /*
  1239. * We negotiate TLS 1.3.
  1240. */
  1241. ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
  1242. ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
  1243. ssl->session_negotiate->endpoint = ssl->conf->endpoint;
  1244. /* Before doing any crypto, make sure we can. */
  1245. ret = mbedtls_ssl_tls13_crypto_init(ssl);
  1246. if (ret != 0) {
  1247. return ret;
  1248. }
  1249. /*
  1250. * We are negotiating the version 1.3 of the protocol. Do what we have
  1251. * postponed: copy of the client random bytes, copy of the legacy session
  1252. * identifier and selection of the TLS 1.3 cipher suite.
  1253. */
  1254. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
  1255. random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
  1256. memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
  1257. if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
  1258. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1259. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1260. }
  1261. ssl->session_negotiate->id_len = legacy_session_id_len;
  1262. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
  1263. legacy_session_id, legacy_session_id_len);
  1264. memcpy(&ssl->session_negotiate->id[0],
  1265. legacy_session_id, legacy_session_id_len);
  1266. /*
  1267. * Search for a matching ciphersuite
  1268. */
  1269. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
  1270. cipher_suites, cipher_suites_len);
  1271. ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
  1272. 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
  1273. if (handshake->ciphersuite_info == NULL) {
  1274. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  1275. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  1276. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1277. }
  1278. ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
  1279. MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
  1280. ((unsigned) handshake->ciphersuite_info->id),
  1281. handshake->ciphersuite_info->name));
  1282. /* ...
  1283. * opaque legacy_compression_methods<1..2^8-1>;
  1284. * ...
  1285. */
  1286. if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
  1287. MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
  1288. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  1289. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1290. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1291. }
  1292. p += 2;
  1293. /* ...
  1294. * Extension extensions<8..2^16-1>;
  1295. * ...
  1296. * with Extension defined as:
  1297. * struct {
  1298. * ExtensionType extension_type;
  1299. * opaque extension_data<0..2^16-1>;
  1300. * } Extension;
  1301. */
  1302. extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
  1303. p += 2;
  1304. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
  1305. extensions_end = p + extensions_len;
  1306. MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
  1307. handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
  1308. while (p < extensions_end) {
  1309. unsigned int extension_type;
  1310. size_t extension_data_len;
  1311. const unsigned char *extension_data_end;
  1312. uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
  1313. if (ssl->handshake->hello_retry_request_flag) {
  1314. /* Do not accept early data extension in 2nd ClientHello */
  1315. allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
  1316. }
  1317. /* RFC 8446, section 4.2.11
  1318. *
  1319. * The "pre_shared_key" extension MUST be the last extension in the
  1320. * ClientHello (this facilitates implementation as described below).
  1321. * Servers MUST check that it is the last extension and otherwise fail
  1322. * the handshake with an "illegal_parameter" alert.
  1323. */
  1324. if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
  1325. MBEDTLS_SSL_DEBUG_MSG(
  1326. 3, ("pre_shared_key is not last extension."));
  1327. MBEDTLS_SSL_PEND_FATAL_ALERT(
  1328. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  1329. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1330. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1331. }
  1332. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
  1333. extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
  1334. extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
  1335. p += 4;
  1336. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
  1337. extension_data_end = p + extension_data_len;
  1338. ret = mbedtls_ssl_tls13_check_received_extension(
  1339. ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
  1340. allowed_exts);
  1341. if (ret != 0) {
  1342. return ret;
  1343. }
  1344. switch (extension_type) {
  1345. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  1346. case MBEDTLS_TLS_EXT_SERVERNAME:
  1347. MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
  1348. ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
  1349. extension_data_end);
  1350. if (ret != 0) {
  1351. MBEDTLS_SSL_DEBUG_RET(
  1352. 1, "mbedtls_ssl_parse_servername_ext", ret);
  1353. return ret;
  1354. }
  1355. break;
  1356. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  1357. #if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
  1358. case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
  1359. MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
  1360. /* Supported Groups Extension
  1361. *
  1362. * When sent by the client, the "supported_groups" extension
  1363. * indicates the named groups which the client supports,
  1364. * ordered from most preferred to least preferred.
  1365. */
  1366. ret = ssl_tls13_parse_supported_groups_ext(
  1367. ssl, p, extension_data_end);
  1368. if (ret != 0) {
  1369. MBEDTLS_SSL_DEBUG_RET(
  1370. 1, "ssl_tls13_parse_supported_groups_ext", ret);
  1371. return ret;
  1372. }
  1373. break;
  1374. #endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
  1375. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
  1376. case MBEDTLS_TLS_EXT_KEY_SHARE:
  1377. MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
  1378. /*
  1379. * Key Share Extension
  1380. *
  1381. * When sent by the client, the "key_share" extension
  1382. * contains the endpoint's cryptographic parameters for
  1383. * ECDHE/DHE key establishment methods.
  1384. */
  1385. ret = ssl_tls13_parse_key_shares_ext(
  1386. ssl, p, extension_data_end);
  1387. if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
  1388. MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
  1389. no_usable_share_for_key_agreement = 1;
  1390. }
  1391. if (ret < 0) {
  1392. MBEDTLS_SSL_DEBUG_RET(
  1393. 1, "ssl_tls13_parse_key_shares_ext", ret);
  1394. return ret;
  1395. }
  1396. break;
  1397. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
  1398. case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
  1399. /* Already parsed */
  1400. break;
  1401. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1402. case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
  1403. MBEDTLS_SSL_DEBUG_MSG(
  1404. 3, ("found psk key exchange modes extension"));
  1405. ret = ssl_tls13_parse_key_exchange_modes_ext(
  1406. ssl, p, extension_data_end);
  1407. if (ret != 0) {
  1408. MBEDTLS_SSL_DEBUG_RET(
  1409. 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
  1410. return ret;
  1411. }
  1412. break;
  1413. #endif
  1414. case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
  1415. MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
  1416. if ((handshake->received_extensions &
  1417. MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
  1418. MBEDTLS_SSL_PEND_FATAL_ALERT(
  1419. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  1420. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1421. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1422. }
  1423. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1424. /* Delay processing of the PSK identity once we have
  1425. * found out which algorithms to use. We keep a pointer
  1426. * to the buffer and the size for later processing.
  1427. */
  1428. pre_shared_key_ext = p;
  1429. pre_shared_key_ext_end = extension_data_end;
  1430. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
  1431. break;
  1432. #if defined(MBEDTLS_SSL_ALPN)
  1433. case MBEDTLS_TLS_EXT_ALPN:
  1434. MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
  1435. ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
  1436. if (ret != 0) {
  1437. MBEDTLS_SSL_DEBUG_RET(
  1438. 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
  1439. return ret;
  1440. }
  1441. break;
  1442. #endif /* MBEDTLS_SSL_ALPN */
  1443. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  1444. case MBEDTLS_TLS_EXT_SIG_ALG:
  1445. MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
  1446. ret = mbedtls_ssl_parse_sig_alg_ext(
  1447. ssl, p, extension_data_end);
  1448. if (ret != 0) {
  1449. MBEDTLS_SSL_DEBUG_RET(
  1450. 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
  1451. return ret;
  1452. }
  1453. break;
  1454. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  1455. #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
  1456. case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
  1457. MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
  1458. ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
  1459. ssl, p, extension_data_end);
  1460. if (ret != 0) {
  1461. MBEDTLS_SSL_DEBUG_RET(
  1462. 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
  1463. return ret;
  1464. }
  1465. break;
  1466. #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
  1467. default:
  1468. MBEDTLS_SSL_PRINT_EXT(
  1469. 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
  1470. extension_type, "( ignored )");
  1471. break;
  1472. }
  1473. p += extension_data_len;
  1474. }
  1475. MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
  1476. handshake->received_extensions);
  1477. ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
  1478. MBEDTLS_SSL_HS_CLIENT_HELLO,
  1479. p - buf);
  1480. if (0 != ret) {
  1481. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
  1482. return ret;
  1483. }
  1484. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1485. /* Update checksum with either
  1486. * - The entire content of the CH message, if no PSK extension is present
  1487. * - The content up to but excluding the PSK extension, if present.
  1488. * Always parse the pre-shared-key extension when present in the
  1489. * ClientHello even if some pre-requisites for PSK key exchange modes are
  1490. * not met. That way we always validate the syntax of the extension.
  1491. */
  1492. if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
  1493. ret = handshake->update_checksum(ssl, buf,
  1494. pre_shared_key_ext - buf);
  1495. if (0 != ret) {
  1496. MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
  1497. return ret;
  1498. }
  1499. ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
  1500. pre_shared_key_ext,
  1501. pre_shared_key_ext_end,
  1502. cipher_suites,
  1503. cipher_suites_end,
  1504. &psk);
  1505. if (ret == 0) {
  1506. got_psk = 1;
  1507. } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
  1508. MBEDTLS_SSL_DEBUG_RET(
  1509. 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
  1510. return ret;
  1511. }
  1512. } else
  1513. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
  1514. {
  1515. ret = handshake->update_checksum(ssl, buf, p - buf);
  1516. if (0 != ret) {
  1517. MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
  1518. return ret;
  1519. }
  1520. }
  1521. /*
  1522. * Determine the key exchange algorithm to use.
  1523. * There are three types of key exchanges supported in TLS 1.3:
  1524. * - (EC)DH with ECDSA,
  1525. * - (EC)DH with PSK,
  1526. * - plain PSK.
  1527. *
  1528. * The PSK-based key exchanges may additionally be used with 0-RTT.
  1529. *
  1530. * Our built-in order of preference is
  1531. * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
  1532. * 2 ) Certificate Mode ( ephemeral )
  1533. * 3 ) Plain PSK Mode ( psk )
  1534. */
  1535. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1536. if (got_psk && (psk.key_exchange_mode ==
  1537. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
  1538. handshake->key_exchange_mode =
  1539. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
  1540. MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
  1541. } else
  1542. #endif
  1543. if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
  1544. handshake->key_exchange_mode =
  1545. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
  1546. MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
  1547. }
  1548. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1549. else if (got_psk && (psk.key_exchange_mode ==
  1550. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
  1551. handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
  1552. MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
  1553. }
  1554. #endif
  1555. else {
  1556. MBEDTLS_SSL_DEBUG_MSG(
  1557. 1,
  1558. ("ClientHello message misses mandatory extensions."));
  1559. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
  1560. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1561. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1562. }
  1563. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1564. if (handshake->key_exchange_mode &
  1565. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
  1566. handshake->ciphersuite_info = psk.ciphersuite_info;
  1567. ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
  1568. MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
  1569. ((unsigned) psk.ciphersuite_info->id),
  1570. psk.ciphersuite_info->name));
  1571. if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
  1572. handshake->resume = 1;
  1573. }
  1574. }
  1575. #endif
  1576. if (handshake->key_exchange_mode !=
  1577. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
  1578. hrr_required = (no_usable_share_for_key_agreement != 0);
  1579. }
  1580. mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
  1581. return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
  1582. }
  1583. #if defined(MBEDTLS_SSL_EARLY_DATA)
  1584. static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
  1585. {
  1586. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  1587. if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
  1588. MBEDTLS_SSL_DEBUG_MSG(
  1589. 1,
  1590. ("EarlyData: rejected, feature disabled in server configuration."));
  1591. return -1;
  1592. }
  1593. if (!handshake->resume) {
  1594. /* We currently support early data only in the case of PSKs established
  1595. via a NewSessionTicket message thus in the case of a session
  1596. resumption. */
  1597. MBEDTLS_SSL_DEBUG_MSG(
  1598. 1, ("EarlyData: rejected, not a session resumption."));
  1599. return -1;
  1600. }
  1601. /* RFC 8446 4.2.10
  1602. *
  1603. * In order to accept early data, the server MUST have accepted a PSK cipher
  1604. * suite and selected the first key offered in the client's "pre_shared_key"
  1605. * extension. In addition, it MUST verify that the following values are the
  1606. * same as those associated with the selected PSK:
  1607. * - The TLS version number
  1608. * - The selected cipher suite
  1609. * - The selected ALPN [RFC7301] protocol, if any
  1610. *
  1611. * NOTE:
  1612. * - The TLS version number is checked in
  1613. * ssl_tls13_offered_psks_check_identity_match_ticket().
  1614. */
  1615. if (handshake->selected_identity != 0) {
  1616. MBEDTLS_SSL_DEBUG_MSG(
  1617. 1, ("EarlyData: rejected, the selected key in "
  1618. "`pre_shared_key` is not the first one."));
  1619. return -1;
  1620. }
  1621. if (handshake->ciphersuite_info->id !=
  1622. ssl->session_negotiate->ciphersuite) {
  1623. MBEDTLS_SSL_DEBUG_MSG(
  1624. 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
  1625. "of the selected pre-shared key."));
  1626. return -1;
  1627. }
  1628. if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
  1629. MBEDTLS_SSL_DEBUG_MSG(
  1630. 1,
  1631. ("EarlyData: rejected, early_data not allowed in ticket "
  1632. "permission bits."));
  1633. return -1;
  1634. }
  1635. #if defined(MBEDTLS_SSL_ALPN)
  1636. const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
  1637. size_t alpn_len;
  1638. if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
  1639. return 0;
  1640. }
  1641. if (alpn != NULL) {
  1642. alpn_len = strlen(alpn);
  1643. }
  1644. if (alpn == NULL ||
  1645. ssl->session_negotiate->ticket_alpn == NULL ||
  1646. alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
  1647. (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
  1648. MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
  1649. "from the one associated with the pre-shared key."));
  1650. return -1;
  1651. }
  1652. #endif
  1653. return 0;
  1654. }
  1655. #endif /* MBEDTLS_SSL_EARLY_DATA */
  1656. /* Update the handshake state machine */
  1657. MBEDTLS_CHECK_RETURN_CRITICAL
  1658. static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
  1659. int hrr_required)
  1660. {
  1661. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1662. /*
  1663. * Server certificate selection
  1664. */
  1665. if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
  1666. MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
  1667. return ret;
  1668. }
  1669. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  1670. ssl->handshake->sni_name = NULL;
  1671. ssl->handshake->sni_name_len = 0;
  1672. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  1673. ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
  1674. if (ret != 0) {
  1675. MBEDTLS_SSL_DEBUG_RET(1,
  1676. "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
  1677. return ret;
  1678. }
  1679. #if defined(MBEDTLS_SSL_EARLY_DATA)
  1680. if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
  1681. ssl->handshake->early_data_accepted =
  1682. (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
  1683. if (ssl->handshake->early_data_accepted) {
  1684. ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
  1685. if (ret != 0) {
  1686. MBEDTLS_SSL_DEBUG_RET(
  1687. 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
  1688. return ret;
  1689. }
  1690. } else {
  1691. ssl->discard_early_data_record =
  1692. hrr_required ?
  1693. MBEDTLS_SSL_EARLY_DATA_DISCARD :
  1694. MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
  1695. }
  1696. }
  1697. #else
  1698. ((void) hrr_required);
  1699. #endif /* MBEDTLS_SSL_EARLY_DATA */
  1700. return 0;
  1701. }
  1702. /*
  1703. * Main entry point from the state machine; orchestrates the otherfunctions.
  1704. */
  1705. MBEDTLS_CHECK_RETURN_CRITICAL
  1706. static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
  1707. {
  1708. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1709. unsigned char *buf = NULL;
  1710. size_t buflen = 0;
  1711. int parse_client_hello_ret;
  1712. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
  1713. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
  1714. ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
  1715. &buf, &buflen));
  1716. MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
  1717. buf + buflen));
  1718. parse_client_hello_ret = ret; /* Store positive return value of
  1719. * parse_client_hello,
  1720. * as negative error codes are handled
  1721. * by MBEDTLS_SSL_PROC_CHK_NEG. */
  1722. /*
  1723. * Version 1.2 of the protocol has to be used for the handshake.
  1724. * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
  1725. * ssl->keep_current_message flag for the ClientHello to be kept and parsed
  1726. * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
  1727. * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
  1728. * will dispatch to the TLS 1.2 state machine.
  1729. */
  1730. if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
  1731. /* Check if server supports TLS 1.2 */
  1732. if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
  1733. MBEDTLS_SSL_DEBUG_MSG(
  1734. 1, ("TLS 1.2 not supported."));
  1735. MBEDTLS_SSL_PEND_FATAL_ALERT(
  1736. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
  1737. MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
  1738. return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
  1739. }
  1740. ssl->keep_current_message = 1;
  1741. ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
  1742. MBEDTLS_SSL_DEBUG_MSG(1, ("non-1.3 ClientHello left for later processing"));
  1743. return 0;
  1744. }
  1745. MBEDTLS_SSL_PROC_CHK(
  1746. ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
  1747. SSL_CLIENT_HELLO_HRR_REQUIRED));
  1748. if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
  1749. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
  1750. } else {
  1751. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
  1752. }
  1753. cleanup:
  1754. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
  1755. return ret;
  1756. }
  1757. /*
  1758. * Handler for MBEDTLS_SSL_SERVER_HELLO
  1759. */
  1760. MBEDTLS_CHECK_RETURN_CRITICAL
  1761. static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
  1762. {
  1763. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1764. unsigned char *server_randbytes =
  1765. ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
  1766. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
  1767. MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
  1768. MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
  1769. return ret;
  1770. }
  1771. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
  1772. MBEDTLS_SERVER_HELLO_RANDOM_LEN);
  1773. #if defined(MBEDTLS_HAVE_TIME)
  1774. ssl->session_negotiate->start = mbedtls_time(NULL);
  1775. #endif /* MBEDTLS_HAVE_TIME */
  1776. return ret;
  1777. }
  1778. /*
  1779. * ssl_tls13_write_server_hello_supported_versions_ext ():
  1780. *
  1781. * struct {
  1782. * ProtocolVersion selected_version;
  1783. * } SupportedVersions;
  1784. */
  1785. MBEDTLS_CHECK_RETURN_CRITICAL
  1786. static int ssl_tls13_write_server_hello_supported_versions_ext(
  1787. mbedtls_ssl_context *ssl,
  1788. unsigned char *buf,
  1789. unsigned char *end,
  1790. size_t *out_len)
  1791. {
  1792. *out_len = 0;
  1793. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
  1794. /* Check if we have space to write the extension:
  1795. * - extension_type (2 bytes)
  1796. * - extension_data_length (2 bytes)
  1797. * - selected_version (2 bytes)
  1798. */
  1799. MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
  1800. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
  1801. MBEDTLS_PUT_UINT16_BE(2, buf, 2);
  1802. mbedtls_ssl_write_version(buf + 4,
  1803. ssl->conf->transport,
  1804. ssl->tls_version);
  1805. MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
  1806. ssl->tls_version));
  1807. *out_len = 6;
  1808. mbedtls_ssl_tls13_set_hs_sent_ext_mask(
  1809. ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
  1810. return 0;
  1811. }
  1812. /* Generate and export a single key share. For hybrid KEMs, this can
  1813. * be called multiple times with the different components of the hybrid. */
  1814. MBEDTLS_CHECK_RETURN_CRITICAL
  1815. static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
  1816. uint16_t named_group,
  1817. unsigned char *buf,
  1818. unsigned char *end,
  1819. size_t *out_len)
  1820. {
  1821. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1822. *out_len = 0;
  1823. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
  1824. if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
  1825. mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
  1826. ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
  1827. ssl, named_group, buf, end, out_len);
  1828. if (ret != 0) {
  1829. MBEDTLS_SSL_DEBUG_RET(
  1830. 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
  1831. ret);
  1832. return ret;
  1833. }
  1834. } else
  1835. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
  1836. if (0 /* Other kinds of KEMs */) {
  1837. } else {
  1838. ((void) ssl);
  1839. ((void) named_group);
  1840. ((void) buf);
  1841. ((void) end);
  1842. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1843. }
  1844. return ret;
  1845. }
  1846. /*
  1847. * ssl_tls13_write_key_share_ext
  1848. *
  1849. * Structure of key_share extension in ServerHello:
  1850. *
  1851. * struct {
  1852. * NamedGroup group;
  1853. * opaque key_exchange<1..2^16-1>;
  1854. * } KeyShareEntry;
  1855. * struct {
  1856. * KeyShareEntry server_share;
  1857. * } KeyShareServerHello;
  1858. */
  1859. MBEDTLS_CHECK_RETURN_CRITICAL
  1860. static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
  1861. unsigned char *buf,
  1862. unsigned char *end,
  1863. size_t *out_len)
  1864. {
  1865. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1866. unsigned char *p = buf;
  1867. uint16_t group = ssl->handshake->offered_group_id;
  1868. unsigned char *server_share = buf + 4;
  1869. size_t key_exchange_length;
  1870. *out_len = 0;
  1871. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
  1872. MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
  1873. mbedtls_ssl_named_group_to_str(group),
  1874. group));
  1875. /* Check if we have space for header and length fields:
  1876. * - extension_type (2 bytes)
  1877. * - extension_data_length (2 bytes)
  1878. * - group (2 bytes)
  1879. * - key_exchange_length (2 bytes)
  1880. */
  1881. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
  1882. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
  1883. MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
  1884. p += 8;
  1885. /* When we introduce PQC-ECDHE hybrids, we'll want to call this
  1886. * function multiple times. */
  1887. ret = ssl_tls13_generate_and_write_key_share(
  1888. ssl, group, server_share + 4, end, &key_exchange_length);
  1889. if (ret != 0) {
  1890. return ret;
  1891. }
  1892. p += key_exchange_length;
  1893. MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
  1894. MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
  1895. *out_len = p - buf;
  1896. mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
  1897. return 0;
  1898. }
  1899. MBEDTLS_CHECK_RETURN_CRITICAL
  1900. static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
  1901. unsigned char *buf,
  1902. unsigned char *end,
  1903. size_t *out_len)
  1904. {
  1905. uint16_t selected_group = ssl->handshake->hrr_selected_group;
  1906. /* key_share Extension
  1907. *
  1908. * struct {
  1909. * select (Handshake.msg_type) {
  1910. * ...
  1911. * case hello_retry_request:
  1912. * NamedGroup selected_group;
  1913. * ...
  1914. * };
  1915. * } KeyShare;
  1916. */
  1917. *out_len = 0;
  1918. /*
  1919. * For a pure PSK key exchange, there is no group to agree upon. The purpose
  1920. * of the HRR is then to transmit a cookie to force the client to demonstrate
  1921. * reachability at their apparent network address (primarily useful for DTLS).
  1922. */
  1923. if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
  1924. return 0;
  1925. }
  1926. /* We should only send the key_share extension if the client's initial
  1927. * key share was not acceptable. */
  1928. if (ssl->handshake->offered_group_id != 0) {
  1929. MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
  1930. return 0;
  1931. }
  1932. if (selected_group == 0) {
  1933. MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
  1934. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1935. }
  1936. /* Check if we have enough space:
  1937. * - extension_type (2 bytes)
  1938. * - extension_data_length (2 bytes)
  1939. * - selected_group (2 bytes)
  1940. */
  1941. MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
  1942. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
  1943. MBEDTLS_PUT_UINT16_BE(2, buf, 2);
  1944. MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
  1945. MBEDTLS_SSL_DEBUG_MSG(3,
  1946. ("HRR selected_group: %s (%x)",
  1947. mbedtls_ssl_named_group_to_str(selected_group),
  1948. selected_group));
  1949. *out_len = 6;
  1950. mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
  1951. return 0;
  1952. }
  1953. /*
  1954. * Structure of ServerHello message:
  1955. *
  1956. * struct {
  1957. * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
  1958. * Random random;
  1959. * opaque legacy_session_id_echo<0..32>;
  1960. * CipherSuite cipher_suite;
  1961. * uint8 legacy_compression_method = 0;
  1962. * Extension extensions<6..2^16-1>;
  1963. * } ServerHello;
  1964. */
  1965. MBEDTLS_CHECK_RETURN_CRITICAL
  1966. static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
  1967. unsigned char *buf,
  1968. unsigned char *end,
  1969. size_t *out_len,
  1970. int is_hrr)
  1971. {
  1972. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1973. unsigned char *p = buf;
  1974. unsigned char *p_extensions_len;
  1975. size_t output_len;
  1976. *out_len = 0;
  1977. ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
  1978. /* ...
  1979. * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
  1980. * ...
  1981. * with ProtocolVersion defined as:
  1982. * uint16 ProtocolVersion;
  1983. */
  1984. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  1985. MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
  1986. p += 2;
  1987. /* ...
  1988. * Random random;
  1989. * ...
  1990. * with Random defined as:
  1991. * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
  1992. */
  1993. MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
  1994. if (is_hrr) {
  1995. memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
  1996. MBEDTLS_SERVER_HELLO_RANDOM_LEN);
  1997. } else {
  1998. memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
  1999. MBEDTLS_SERVER_HELLO_RANDOM_LEN);
  2000. }
  2001. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
  2002. p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
  2003. p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
  2004. /* ...
  2005. * opaque legacy_session_id_echo<0..32>;
  2006. * ...
  2007. */
  2008. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
  2009. *p++ = (unsigned char) ssl->session_negotiate->id_len;
  2010. if (ssl->session_negotiate->id_len > 0) {
  2011. memcpy(p, &ssl->session_negotiate->id[0],
  2012. ssl->session_negotiate->id_len);
  2013. p += ssl->session_negotiate->id_len;
  2014. MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
  2015. ssl->session_negotiate->id_len);
  2016. }
  2017. /* ...
  2018. * CipherSuite cipher_suite;
  2019. * ...
  2020. * with CipherSuite defined as:
  2021. * uint8 CipherSuite[2];
  2022. */
  2023. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  2024. MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
  2025. p += 2;
  2026. MBEDTLS_SSL_DEBUG_MSG(3,
  2027. ("server hello, chosen ciphersuite: %s ( id=%d )",
  2028. mbedtls_ssl_get_ciphersuite_name(
  2029. ssl->session_negotiate->ciphersuite),
  2030. ssl->session_negotiate->ciphersuite));
  2031. /* ...
  2032. * uint8 legacy_compression_method = 0;
  2033. * ...
  2034. */
  2035. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
  2036. *p++ = MBEDTLS_SSL_COMPRESS_NULL;
  2037. /* ...
  2038. * Extension extensions<6..2^16-1>;
  2039. * ...
  2040. * struct {
  2041. * ExtensionType extension_type; (2 bytes)
  2042. * opaque extension_data<0..2^16-1>;
  2043. * } Extension;
  2044. */
  2045. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  2046. p_extensions_len = p;
  2047. p += 2;
  2048. if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
  2049. ssl, p, end, &output_len)) != 0) {
  2050. MBEDTLS_SSL_DEBUG_RET(
  2051. 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
  2052. return ret;
  2053. }
  2054. p += output_len;
  2055. if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
  2056. if (is_hrr) {
  2057. ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
  2058. } else {
  2059. ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
  2060. }
  2061. if (ret != 0) {
  2062. return ret;
  2063. }
  2064. p += output_len;
  2065. }
  2066. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  2067. if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
  2068. ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
  2069. if (ret != 0) {
  2070. MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
  2071. ret);
  2072. return ret;
  2073. }
  2074. p += output_len;
  2075. }
  2076. #endif
  2077. MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
  2078. MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
  2079. p_extensions_len, p - p_extensions_len);
  2080. *out_len = p - buf;
  2081. MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
  2082. MBEDTLS_SSL_PRINT_EXTS(
  2083. 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
  2084. MBEDTLS_SSL_HS_SERVER_HELLO,
  2085. ssl->handshake->sent_extensions);
  2086. return ret;
  2087. }
  2088. MBEDTLS_CHECK_RETURN_CRITICAL
  2089. static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
  2090. {
  2091. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2092. ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
  2093. if (ret != 0) {
  2094. MBEDTLS_SSL_DEBUG_RET(1,
  2095. "mbedtls_ssl_tls13_compute_handshake_transform",
  2096. ret);
  2097. return ret;
  2098. }
  2099. return ret;
  2100. }
  2101. MBEDTLS_CHECK_RETURN_CRITICAL
  2102. static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
  2103. {
  2104. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2105. unsigned char *buf;
  2106. size_t buf_len, msg_len;
  2107. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
  2108. MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
  2109. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
  2110. ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
  2111. MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
  2112. buf + buf_len,
  2113. &msg_len,
  2114. 0));
  2115. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
  2116. ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
  2117. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
  2118. ssl, buf_len, msg_len));
  2119. MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
  2120. #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
  2121. /* The server sends a dummy change_cipher_spec record immediately
  2122. * after its first handshake message. This may either be after
  2123. * a ServerHello or a HelloRetryRequest.
  2124. */
  2125. mbedtls_ssl_handshake_set_state(
  2126. ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
  2127. #else
  2128. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
  2129. #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
  2130. cleanup:
  2131. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
  2132. return ret;
  2133. }
  2134. /*
  2135. * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
  2136. */
  2137. MBEDTLS_CHECK_RETURN_CRITICAL
  2138. static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
  2139. {
  2140. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2141. if (ssl->handshake->hello_retry_request_flag) {
  2142. MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
  2143. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  2144. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  2145. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  2146. }
  2147. /*
  2148. * Create stateless transcript hash for HRR
  2149. */
  2150. MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
  2151. ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
  2152. if (ret != 0) {
  2153. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
  2154. return ret;
  2155. }
  2156. mbedtls_ssl_session_reset_msg_layer(ssl, 0);
  2157. return 0;
  2158. }
  2159. MBEDTLS_CHECK_RETURN_CRITICAL
  2160. static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
  2161. {
  2162. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2163. unsigned char *buf;
  2164. size_t buf_len, msg_len;
  2165. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
  2166. MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
  2167. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
  2168. ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
  2169. &buf, &buf_len));
  2170. MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
  2171. buf + buf_len,
  2172. &msg_len,
  2173. 1));
  2174. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
  2175. ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
  2176. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
  2177. msg_len));
  2178. ssl->handshake->hello_retry_request_flag = 1;
  2179. #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
  2180. /* The server sends a dummy change_cipher_spec record immediately
  2181. * after its first handshake message. This may either be after
  2182. * a ServerHello or a HelloRetryRequest.
  2183. */
  2184. mbedtls_ssl_handshake_set_state(
  2185. ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
  2186. #else
  2187. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
  2188. #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
  2189. cleanup:
  2190. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
  2191. return ret;
  2192. }
  2193. /*
  2194. * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
  2195. */
  2196. /*
  2197. * struct {
  2198. * Extension extensions<0..2 ^ 16 - 1>;
  2199. * } EncryptedExtensions;
  2200. *
  2201. */
  2202. MBEDTLS_CHECK_RETURN_CRITICAL
  2203. static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
  2204. unsigned char *buf,
  2205. unsigned char *end,
  2206. size_t *out_len)
  2207. {
  2208. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2209. unsigned char *p = buf;
  2210. size_t extensions_len = 0;
  2211. unsigned char *p_extensions_len;
  2212. size_t output_len;
  2213. *out_len = 0;
  2214. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  2215. p_extensions_len = p;
  2216. p += 2;
  2217. ((void) ssl);
  2218. ((void) ret);
  2219. ((void) output_len);
  2220. #if defined(MBEDTLS_SSL_ALPN)
  2221. ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
  2222. if (ret != 0) {
  2223. return ret;
  2224. }
  2225. p += output_len;
  2226. #endif /* MBEDTLS_SSL_ALPN */
  2227. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2228. if (ssl->handshake->early_data_accepted) {
  2229. ret = mbedtls_ssl_tls13_write_early_data_ext(
  2230. ssl, 0, p, end, &output_len);
  2231. if (ret != 0) {
  2232. return ret;
  2233. }
  2234. p += output_len;
  2235. }
  2236. #endif /* MBEDTLS_SSL_EARLY_DATA */
  2237. #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
  2238. if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
  2239. ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
  2240. ssl, p, end, &output_len);
  2241. if (ret != 0) {
  2242. return ret;
  2243. }
  2244. p += output_len;
  2245. }
  2246. #endif
  2247. extensions_len = (p - p_extensions_len) - 2;
  2248. MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
  2249. *out_len = p - buf;
  2250. MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
  2251. MBEDTLS_SSL_PRINT_EXTS(
  2252. 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
  2253. return 0;
  2254. }
  2255. MBEDTLS_CHECK_RETURN_CRITICAL
  2256. static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
  2257. {
  2258. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2259. unsigned char *buf;
  2260. size_t buf_len, msg_len;
  2261. mbedtls_ssl_set_outbound_transform(ssl,
  2262. ssl->handshake->transform_handshake);
  2263. MBEDTLS_SSL_DEBUG_MSG(
  2264. 3, ("switching to handshake transform for outbound data"));
  2265. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
  2266. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
  2267. ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
  2268. &buf, &buf_len));
  2269. MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
  2270. ssl, buf, buf + buf_len, &msg_len));
  2271. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
  2272. ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
  2273. buf, msg_len));
  2274. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
  2275. ssl, buf_len, msg_len));
  2276. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  2277. if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
  2278. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
  2279. } else {
  2280. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
  2281. }
  2282. #else
  2283. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
  2284. #endif
  2285. cleanup:
  2286. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
  2287. return ret;
  2288. }
  2289. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  2290. #define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
  2291. #define SSL_CERTIFICATE_REQUEST_SKIP 1
  2292. /* Coordination:
  2293. * Check whether a CertificateRequest message should be written.
  2294. * Returns a negative code on failure, or
  2295. * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
  2296. * - SSL_CERTIFICATE_REQUEST_SKIP
  2297. * indicating if the writing of the CertificateRequest
  2298. * should be skipped or not.
  2299. */
  2300. MBEDTLS_CHECK_RETURN_CRITICAL
  2301. static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
  2302. {
  2303. int authmode;
  2304. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  2305. if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
  2306. authmode = ssl->handshake->sni_authmode;
  2307. } else
  2308. #endif
  2309. authmode = ssl->conf->authmode;
  2310. if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
  2311. ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
  2312. return SSL_CERTIFICATE_REQUEST_SKIP;
  2313. }
  2314. ssl->handshake->certificate_request_sent = 1;
  2315. return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
  2316. }
  2317. /*
  2318. * struct {
  2319. * opaque certificate_request_context<0..2^8-1>;
  2320. * Extension extensions<2..2^16-1>;
  2321. * } CertificateRequest;
  2322. *
  2323. */
  2324. MBEDTLS_CHECK_RETURN_CRITICAL
  2325. static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
  2326. unsigned char *buf,
  2327. const unsigned char *end,
  2328. size_t *out_len)
  2329. {
  2330. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2331. unsigned char *p = buf;
  2332. size_t output_len = 0;
  2333. unsigned char *p_extensions_len;
  2334. *out_len = 0;
  2335. /* Check if we have enough space:
  2336. * - certificate_request_context (1 byte)
  2337. * - extensions length (2 bytes)
  2338. */
  2339. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
  2340. /*
  2341. * Write certificate_request_context
  2342. */
  2343. /*
  2344. * We use a zero length context for the normal handshake
  2345. * messages. For post-authentication handshake messages
  2346. * this request context would be set to a non-zero value.
  2347. */
  2348. *p++ = 0x0;
  2349. /*
  2350. * Write extensions
  2351. */
  2352. /* The extensions must contain the signature_algorithms. */
  2353. p_extensions_len = p;
  2354. p += 2;
  2355. ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
  2356. if (ret != 0) {
  2357. return ret;
  2358. }
  2359. p += output_len;
  2360. MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
  2361. *out_len = p - buf;
  2362. MBEDTLS_SSL_PRINT_EXTS(
  2363. 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
  2364. return 0;
  2365. }
  2366. MBEDTLS_CHECK_RETURN_CRITICAL
  2367. static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
  2368. {
  2369. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2370. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
  2371. MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
  2372. if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
  2373. unsigned char *buf;
  2374. size_t buf_len, msg_len;
  2375. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
  2376. ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
  2377. &buf, &buf_len));
  2378. MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
  2379. ssl, buf, buf + buf_len, &msg_len));
  2380. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
  2381. ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
  2382. buf, msg_len));
  2383. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
  2384. ssl, buf_len, msg_len));
  2385. } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
  2386. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
  2387. ret = 0;
  2388. } else {
  2389. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2390. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2391. goto cleanup;
  2392. }
  2393. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
  2394. cleanup:
  2395. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
  2396. return ret;
  2397. }
  2398. /*
  2399. * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
  2400. */
  2401. MBEDTLS_CHECK_RETURN_CRITICAL
  2402. static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
  2403. {
  2404. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2405. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2406. if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
  2407. mbedtls_ssl_own_cert(ssl) == NULL) {
  2408. MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
  2409. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  2410. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  2411. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  2412. }
  2413. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2414. ret = mbedtls_ssl_tls13_write_certificate(ssl);
  2415. if (ret != 0) {
  2416. return ret;
  2417. }
  2418. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
  2419. return 0;
  2420. }
  2421. /*
  2422. * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
  2423. */
  2424. MBEDTLS_CHECK_RETURN_CRITICAL
  2425. static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
  2426. {
  2427. int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
  2428. if (ret != 0) {
  2429. return ret;
  2430. }
  2431. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
  2432. return 0;
  2433. }
  2434. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  2435. /*
  2436. * RFC 8446 section A.2
  2437. *
  2438. * | Send ServerHello
  2439. * | K_send = handshake
  2440. * | Send EncryptedExtensions
  2441. * | [Send CertificateRequest]
  2442. * Can send | [Send Certificate + CertificateVerify]
  2443. * app data | Send Finished
  2444. * after --> | K_send = application
  2445. * here +--------+--------+
  2446. * No 0-RTT | | 0-RTT
  2447. * | |
  2448. * K_recv = handshake | | K_recv = early data
  2449. * [Skip decrypt errors] | +------> WAIT_EOED -+
  2450. * | | Recv | | Recv EndOfEarlyData
  2451. * | | early data | | K_recv = handshake
  2452. * | +------------+ |
  2453. * | |
  2454. * +> WAIT_FLIGHT2 <--------+
  2455. * |
  2456. * +--------+--------+
  2457. * No auth | | Client auth
  2458. * | |
  2459. * | v
  2460. * | WAIT_CERT
  2461. * | Recv | | Recv Certificate
  2462. * | empty | v
  2463. * | Certificate | WAIT_CV
  2464. * | | | Recv
  2465. * | v | CertificateVerify
  2466. * +-> WAIT_FINISHED <---+
  2467. * | Recv Finished
  2468. *
  2469. *
  2470. * The following function handles the state changes after WAIT_FLIGHT2 in the
  2471. * above diagram. We are not going to receive early data related messages
  2472. * anymore, prepare to receive the first handshake message of the client
  2473. * second flight.
  2474. */
  2475. static void ssl_tls13_prepare_for_handshake_second_flight(
  2476. mbedtls_ssl_context *ssl)
  2477. {
  2478. if (ssl->handshake->certificate_request_sent) {
  2479. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
  2480. } else {
  2481. MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
  2482. MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
  2483. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
  2484. }
  2485. }
  2486. /*
  2487. * Handler for MBEDTLS_SSL_SERVER_FINISHED
  2488. */
  2489. MBEDTLS_CHECK_RETURN_CRITICAL
  2490. static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
  2491. {
  2492. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2493. ret = mbedtls_ssl_tls13_write_finished_message(ssl);
  2494. if (ret != 0) {
  2495. return ret;
  2496. }
  2497. ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
  2498. if (ret != 0) {
  2499. MBEDTLS_SSL_PEND_FATAL_ALERT(
  2500. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  2501. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  2502. return ret;
  2503. }
  2504. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2505. if (ssl->handshake->early_data_accepted) {
  2506. /* See RFC 8446 section A.2 for more information */
  2507. MBEDTLS_SSL_DEBUG_MSG(
  2508. 1, ("Switch to early keys for inbound traffic. "
  2509. "( K_recv = early data )"));
  2510. mbedtls_ssl_set_inbound_transform(
  2511. ssl, ssl->handshake->transform_earlydata);
  2512. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
  2513. return 0;
  2514. }
  2515. #endif /* MBEDTLS_SSL_EARLY_DATA */
  2516. MBEDTLS_SSL_DEBUG_MSG(
  2517. 1, ("Switch to handshake keys for inbound traffic "
  2518. "( K_recv = handshake )"));
  2519. mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
  2520. ssl_tls13_prepare_for_handshake_second_flight(ssl);
  2521. return 0;
  2522. }
  2523. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2524. /*
  2525. * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
  2526. */
  2527. #define SSL_GOT_END_OF_EARLY_DATA 0
  2528. #define SSL_GOT_EARLY_DATA 1
  2529. /* Coordination:
  2530. * Deals with the ambiguity of not knowing if the next message is an
  2531. * EndOfEarlyData message or an application message containing early data.
  2532. * Returns a negative code on failure, or
  2533. * - SSL_GOT_END_OF_EARLY_DATA
  2534. * - SSL_GOT_EARLY_DATA
  2535. * indicating which message is received.
  2536. */
  2537. MBEDTLS_CHECK_RETURN_CRITICAL
  2538. static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
  2539. {
  2540. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2541. if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
  2542. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  2543. return ret;
  2544. }
  2545. ssl->keep_current_message = 1;
  2546. if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
  2547. ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
  2548. MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
  2549. return SSL_GOT_END_OF_EARLY_DATA;
  2550. }
  2551. if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
  2552. if (ssl->in_offt == NULL) {
  2553. MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
  2554. /* Set the reading pointer */
  2555. ssl->in_offt = ssl->in_msg;
  2556. ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
  2557. if (ret != 0) {
  2558. return ret;
  2559. }
  2560. }
  2561. return SSL_GOT_EARLY_DATA;
  2562. }
  2563. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
  2564. MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
  2565. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  2566. }
  2567. MBEDTLS_CHECK_RETURN_CRITICAL
  2568. static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
  2569. const unsigned char *buf,
  2570. const unsigned char *end)
  2571. {
  2572. /* RFC 8446 section 4.5
  2573. *
  2574. * struct {} EndOfEarlyData;
  2575. */
  2576. if (buf != end) {
  2577. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  2578. MBEDTLS_ERR_SSL_DECODE_ERROR);
  2579. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2580. }
  2581. return 0;
  2582. }
  2583. /*
  2584. * RFC 8446 section A.2
  2585. *
  2586. * | Send ServerHello
  2587. * | K_send = handshake
  2588. * | Send EncryptedExtensions
  2589. * | [Send CertificateRequest]
  2590. * Can send | [Send Certificate + CertificateVerify]
  2591. * app data | Send Finished
  2592. * after --> | K_send = application
  2593. * here +--------+--------+
  2594. * No 0-RTT | | 0-RTT
  2595. * | |
  2596. * K_recv = handshake | | K_recv = early data
  2597. * [Skip decrypt errors] | +------> WAIT_EOED -+
  2598. * | | Recv | | Recv EndOfEarlyData
  2599. * | | early data | | K_recv = handshake
  2600. * | +------------+ |
  2601. * | |
  2602. * +> WAIT_FLIGHT2 <--------+
  2603. * |
  2604. * +--------+--------+
  2605. * No auth | | Client auth
  2606. * | |
  2607. * | v
  2608. * | WAIT_CERT
  2609. * | Recv | | Recv Certificate
  2610. * | empty | v
  2611. * | Certificate | WAIT_CV
  2612. * | | | Recv
  2613. * | v | CertificateVerify
  2614. * +-> WAIT_FINISHED <---+
  2615. * | Recv Finished
  2616. *
  2617. * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
  2618. * the above diagram.
  2619. */
  2620. MBEDTLS_CHECK_RETURN_CRITICAL
  2621. static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
  2622. {
  2623. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2624. MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
  2625. MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
  2626. if (ret == SSL_GOT_END_OF_EARLY_DATA) {
  2627. unsigned char *buf;
  2628. size_t buf_len;
  2629. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
  2630. ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
  2631. &buf, &buf_len));
  2632. MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
  2633. ssl, buf, buf + buf_len));
  2634. MBEDTLS_SSL_DEBUG_MSG(
  2635. 1, ("Switch to handshake keys for inbound traffic"
  2636. "( K_recv = handshake )"));
  2637. mbedtls_ssl_set_inbound_transform(
  2638. ssl, ssl->handshake->transform_handshake);
  2639. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
  2640. ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
  2641. buf, buf_len));
  2642. ssl_tls13_prepare_for_handshake_second_flight(ssl);
  2643. } else if (ret == SSL_GOT_EARLY_DATA) {
  2644. ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
  2645. goto cleanup;
  2646. } else {
  2647. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2648. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2649. goto cleanup;
  2650. }
  2651. cleanup:
  2652. MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
  2653. return ret;
  2654. }
  2655. #endif /* MBEDTLS_SSL_EARLY_DATA */
  2656. /*
  2657. * Handler for MBEDTLS_SSL_CLIENT_FINISHED
  2658. */
  2659. MBEDTLS_CHECK_RETURN_CRITICAL
  2660. static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
  2661. {
  2662. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2663. ret = mbedtls_ssl_tls13_process_finished_message(ssl);
  2664. if (ret != 0) {
  2665. return ret;
  2666. }
  2667. ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
  2668. if (ret != 0) {
  2669. MBEDTLS_SSL_DEBUG_RET(
  2670. 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
  2671. }
  2672. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
  2673. return 0;
  2674. }
  2675. /*
  2676. * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
  2677. */
  2678. MBEDTLS_CHECK_RETURN_CRITICAL
  2679. static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
  2680. {
  2681. MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
  2682. mbedtls_ssl_tls13_handshake_wrapup(ssl);
  2683. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
  2684. defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  2685. /* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
  2686. * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
  2687. * expected to be resolved with issue#6395.
  2688. */
  2689. /* Sent NewSessionTicket message only when client supports PSK */
  2690. if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
  2691. mbedtls_ssl_handshake_set_state(
  2692. ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
  2693. } else
  2694. #endif
  2695. {
  2696. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
  2697. }
  2698. return 0;
  2699. }
  2700. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2701. /*
  2702. * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
  2703. */
  2704. #define SSL_NEW_SESSION_TICKET_SKIP 0
  2705. #define SSL_NEW_SESSION_TICKET_WRITE 1
  2706. MBEDTLS_CHECK_RETURN_CRITICAL
  2707. static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
  2708. {
  2709. /* Check whether the use of session tickets is enabled */
  2710. if (ssl->conf->f_ticket_write == NULL) {
  2711. MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
  2712. " callback is not set"));
  2713. return SSL_NEW_SESSION_TICKET_SKIP;
  2714. }
  2715. if (ssl->conf->new_session_tickets_count == 0) {
  2716. MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
  2717. " configured count is zero"));
  2718. return SSL_NEW_SESSION_TICKET_SKIP;
  2719. }
  2720. if (ssl->handshake->new_session_tickets_count == 0) {
  2721. MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
  2722. "been sent."));
  2723. return SSL_NEW_SESSION_TICKET_SKIP;
  2724. }
  2725. return SSL_NEW_SESSION_TICKET_WRITE;
  2726. }
  2727. MBEDTLS_CHECK_RETURN_CRITICAL
  2728. static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
  2729. unsigned char *ticket_nonce,
  2730. size_t ticket_nonce_size)
  2731. {
  2732. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2733. mbedtls_ssl_session *session = ssl->session;
  2734. mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  2735. psa_algorithm_t psa_hash_alg;
  2736. int hash_length;
  2737. MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
  2738. /* Set ticket_flags depends on the advertised psk key exchange mode */
  2739. mbedtls_ssl_tls13_session_clear_ticket_flags(
  2740. session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
  2741. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  2742. mbedtls_ssl_tls13_session_set_ticket_flags(
  2743. session, ssl->handshake->tls13_kex_modes);
  2744. #endif
  2745. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2746. if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
  2747. ssl->conf->max_early_data_size > 0) {
  2748. mbedtls_ssl_tls13_session_set_ticket_flags(
  2749. session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
  2750. session->max_early_data_size = ssl->conf->max_early_data_size;
  2751. }
  2752. #endif /* MBEDTLS_SSL_EARLY_DATA */
  2753. MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
  2754. #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
  2755. if (session->ticket_alpn == NULL) {
  2756. ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
  2757. if (ret != 0) {
  2758. return ret;
  2759. }
  2760. }
  2761. #endif
  2762. /* Generate ticket_age_add */
  2763. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
  2764. (unsigned char *) &session->ticket_age_add,
  2765. sizeof(session->ticket_age_add)) != 0)) {
  2766. MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
  2767. return ret;
  2768. }
  2769. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
  2770. (unsigned int) session->ticket_age_add));
  2771. /* Generate ticket_nonce */
  2772. ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
  2773. if (ret != 0) {
  2774. MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
  2775. return ret;
  2776. }
  2777. MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
  2778. ticket_nonce, ticket_nonce_size);
  2779. ciphersuite_info =
  2780. (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
  2781. psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
  2782. hash_length = PSA_HASH_LENGTH(psa_hash_alg);
  2783. if (hash_length == -1 ||
  2784. (size_t) hash_length > sizeof(session->resumption_key)) {
  2785. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2786. }
  2787. /* In this code the psk key length equals the length of the hash */
  2788. session->resumption_key_len = hash_length;
  2789. session->ciphersuite = ciphersuite_info->id;
  2790. /* Compute resumption key
  2791. *
  2792. * HKDF-Expand-Label( resumption_master_secret,
  2793. * "resumption", ticket_nonce, Hash.length )
  2794. */
  2795. ret = mbedtls_ssl_tls13_hkdf_expand_label(
  2796. psa_hash_alg,
  2797. session->app_secrets.resumption_master_secret,
  2798. hash_length,
  2799. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
  2800. ticket_nonce,
  2801. ticket_nonce_size,
  2802. session->resumption_key,
  2803. hash_length);
  2804. if (ret != 0) {
  2805. MBEDTLS_SSL_DEBUG_RET(2,
  2806. "Creating the ticket-resumed PSK failed",
  2807. ret);
  2808. return ret;
  2809. }
  2810. MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
  2811. session->resumption_key,
  2812. session->resumption_key_len);
  2813. MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
  2814. session->app_secrets.resumption_master_secret,
  2815. hash_length);
  2816. return 0;
  2817. }
  2818. /* This function creates a NewSessionTicket message in the following format:
  2819. *
  2820. * struct {
  2821. * uint32 ticket_lifetime;
  2822. * uint32 ticket_age_add;
  2823. * opaque ticket_nonce<0..255>;
  2824. * opaque ticket<1..2^16-1>;
  2825. * Extension extensions<0..2^16-2>;
  2826. * } NewSessionTicket;
  2827. *
  2828. * The ticket inside the NewSessionTicket message is an encrypted container
  2829. * carrying the necessary information so that the server is later able to
  2830. * re-start the communication.
  2831. *
  2832. * The following fields are placed inside the ticket by the
  2833. * f_ticket_write() function:
  2834. *
  2835. * - creation time (ticket_creation_time)
  2836. * - flags (ticket_flags)
  2837. * - age add (ticket_age_add)
  2838. * - key (resumption_key)
  2839. * - key length (resumption_key_len)
  2840. * - ciphersuite (ciphersuite)
  2841. * - max_early_data_size (max_early_data_size)
  2842. */
  2843. MBEDTLS_CHECK_RETURN_CRITICAL
  2844. static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
  2845. unsigned char *buf,
  2846. unsigned char *end,
  2847. size_t *out_len,
  2848. unsigned char *ticket_nonce,
  2849. size_t ticket_nonce_size)
  2850. {
  2851. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2852. unsigned char *p = buf;
  2853. mbedtls_ssl_session *session = ssl->session;
  2854. size_t ticket_len;
  2855. uint32_t ticket_lifetime;
  2856. unsigned char *p_extensions_len;
  2857. *out_len = 0;
  2858. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
  2859. /*
  2860. * ticket_lifetime 4 bytes
  2861. * ticket_age_add 4 bytes
  2862. * ticket_nonce 1 + ticket_nonce_size bytes
  2863. * ticket >=2 bytes
  2864. */
  2865. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
  2866. /* Generate ticket and ticket_lifetime */
  2867. #if defined(MBEDTLS_HAVE_TIME)
  2868. session->ticket_creation_time = mbedtls_ms_time();
  2869. #endif
  2870. ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
  2871. session,
  2872. p + 9 + ticket_nonce_size + 2,
  2873. end,
  2874. &ticket_len,
  2875. &ticket_lifetime);
  2876. if (ret != 0) {
  2877. MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
  2878. return ret;
  2879. }
  2880. /* RFC 8446 section 4.6.1
  2881. *
  2882. * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
  2883. * unsigned integer in network byte order from the time of ticket
  2884. * issuance. Servers MUST NOT use any value greater than
  2885. * 604800 seconds (7 days) ...
  2886. */
  2887. if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
  2888. MBEDTLS_SSL_DEBUG_MSG(
  2889. 1, ("Ticket lifetime (%u) is greater than 7 days.",
  2890. (unsigned int) ticket_lifetime));
  2891. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  2892. }
  2893. MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
  2894. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
  2895. (unsigned int) ticket_lifetime));
  2896. /* Write ticket_age_add */
  2897. MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
  2898. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
  2899. (unsigned int) session->ticket_age_add));
  2900. /* Write ticket_nonce */
  2901. p[8] = (unsigned char) ticket_nonce_size;
  2902. if (ticket_nonce_size > 0) {
  2903. memcpy(p + 9, ticket_nonce, ticket_nonce_size);
  2904. }
  2905. p += 9 + ticket_nonce_size;
  2906. /* Write ticket */
  2907. MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
  2908. p += 2;
  2909. MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
  2910. p += ticket_len;
  2911. /* Ticket Extensions
  2912. *
  2913. * Extension extensions<0..2^16-2>;
  2914. */
  2915. ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
  2916. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  2917. p_extensions_len = p;
  2918. p += 2;
  2919. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2920. if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
  2921. size_t output_len;
  2922. if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
  2923. ssl, 1, p, end, &output_len)) != 0) {
  2924. MBEDTLS_SSL_DEBUG_RET(
  2925. 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
  2926. return ret;
  2927. }
  2928. p += output_len;
  2929. } else {
  2930. MBEDTLS_SSL_DEBUG_MSG(
  2931. 4, ("early_data not allowed, "
  2932. "skip early_data extension in NewSessionTicket"));
  2933. }
  2934. #endif /* MBEDTLS_SSL_EARLY_DATA */
  2935. MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
  2936. *out_len = p - buf;
  2937. MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
  2938. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
  2939. MBEDTLS_SSL_PRINT_EXTS(
  2940. 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
  2941. return 0;
  2942. }
  2943. /*
  2944. * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
  2945. */
  2946. static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
  2947. {
  2948. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2949. MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
  2950. if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
  2951. unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
  2952. unsigned char *buf;
  2953. size_t buf_len, msg_len;
  2954. MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
  2955. ssl, ticket_nonce, sizeof(ticket_nonce)));
  2956. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
  2957. ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
  2958. &buf, &buf_len));
  2959. MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
  2960. ssl, buf, buf + buf_len, &msg_len,
  2961. ticket_nonce, sizeof(ticket_nonce)));
  2962. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
  2963. ssl, buf_len, msg_len));
  2964. /* Limit session tickets count to one when resumption connection.
  2965. *
  2966. * See document of mbedtls_ssl_conf_new_session_tickets.
  2967. */
  2968. if (ssl->handshake->resume == 1) {
  2969. ssl->handshake->new_session_tickets_count = 0;
  2970. } else {
  2971. ssl->handshake->new_session_tickets_count--;
  2972. }
  2973. mbedtls_ssl_handshake_set_state(
  2974. ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
  2975. } else {
  2976. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
  2977. }
  2978. cleanup:
  2979. return ret;
  2980. }
  2981. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  2982. /*
  2983. * TLS 1.3 State Machine -- server side
  2984. */
  2985. int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
  2986. {
  2987. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2988. if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
  2989. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  2990. }
  2991. MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
  2992. mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
  2993. ssl->state));
  2994. switch (ssl->state) {
  2995. /* start state */
  2996. case MBEDTLS_SSL_HELLO_REQUEST:
  2997. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
  2998. ret = 0;
  2999. break;
  3000. case MBEDTLS_SSL_CLIENT_HELLO:
  3001. ret = ssl_tls13_process_client_hello(ssl);
  3002. if (ret != 0) {
  3003. MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
  3004. }
  3005. break;
  3006. case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
  3007. ret = ssl_tls13_write_hello_retry_request(ssl);
  3008. if (ret != 0) {
  3009. MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
  3010. return ret;
  3011. }
  3012. break;
  3013. case MBEDTLS_SSL_SERVER_HELLO:
  3014. ret = ssl_tls13_write_server_hello(ssl);
  3015. break;
  3016. case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
  3017. ret = ssl_tls13_write_encrypted_extensions(ssl);
  3018. if (ret != 0) {
  3019. MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
  3020. return ret;
  3021. }
  3022. break;
  3023. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  3024. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  3025. ret = ssl_tls13_write_certificate_request(ssl);
  3026. break;
  3027. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  3028. ret = ssl_tls13_write_server_certificate(ssl);
  3029. break;
  3030. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  3031. ret = ssl_tls13_write_certificate_verify(ssl);
  3032. break;
  3033. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  3034. /*
  3035. * Injection of dummy-CCS's for middlebox compatibility
  3036. */
  3037. #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
  3038. case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
  3039. ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
  3040. if (ret == 0) {
  3041. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
  3042. }
  3043. break;
  3044. case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
  3045. ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
  3046. if (ret != 0) {
  3047. break;
  3048. }
  3049. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
  3050. break;
  3051. #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
  3052. case MBEDTLS_SSL_SERVER_FINISHED:
  3053. ret = ssl_tls13_write_server_finished(ssl);
  3054. break;
  3055. #if defined(MBEDTLS_SSL_EARLY_DATA)
  3056. case MBEDTLS_SSL_END_OF_EARLY_DATA:
  3057. ret = ssl_tls13_process_end_of_early_data(ssl);
  3058. break;
  3059. #endif /* MBEDTLS_SSL_EARLY_DATA */
  3060. case MBEDTLS_SSL_CLIENT_FINISHED:
  3061. ret = ssl_tls13_process_client_finished(ssl);
  3062. break;
  3063. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  3064. ret = ssl_tls13_handshake_wrapup(ssl);
  3065. break;
  3066. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  3067. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  3068. ret = mbedtls_ssl_tls13_process_certificate(ssl);
  3069. if (ret == 0) {
  3070. if (ssl->session_negotiate->peer_cert != NULL) {
  3071. mbedtls_ssl_handshake_set_state(
  3072. ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
  3073. } else {
  3074. MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
  3075. mbedtls_ssl_handshake_set_state(
  3076. ssl, MBEDTLS_SSL_CLIENT_FINISHED);
  3077. }
  3078. }
  3079. break;
  3080. case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
  3081. ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
  3082. if (ret == 0) {
  3083. mbedtls_ssl_handshake_set_state(
  3084. ssl, MBEDTLS_SSL_CLIENT_FINISHED);
  3085. }
  3086. break;
  3087. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  3088. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3089. case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
  3090. ret = ssl_tls13_write_new_session_ticket(ssl);
  3091. if (ret != 0) {
  3092. MBEDTLS_SSL_DEBUG_RET(1,
  3093. "ssl_tls13_write_new_session_ticket ",
  3094. ret);
  3095. }
  3096. break;
  3097. case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
  3098. /* This state is necessary to do the flush of the New Session
  3099. * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
  3100. * as part of ssl_prepare_handshake_step.
  3101. */
  3102. ret = 0;
  3103. if (ssl->handshake->new_session_tickets_count == 0) {
  3104. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
  3105. } else {
  3106. mbedtls_ssl_handshake_set_state(
  3107. ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
  3108. }
  3109. break;
  3110. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  3111. default:
  3112. MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
  3113. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3114. }
  3115. return ret;
  3116. }
  3117. #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */