ssl_tls13_server.c 105 KB

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