ssl_tls13_client.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064
  1. /*
  2. * TLS 1.3 client-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. * This file is part of mbed TLS ( https://tls.mbed.org )
  20. */
  21. #include "common.h"
  22. #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
  23. #include <string.h>
  24. #include "mbedtls/debug.h"
  25. #include "mbedtls/error.h"
  26. #include "mbedtls/platform.h"
  27. #include "ssl_misc.h"
  28. #include "ssl_client.h"
  29. #include "ssl_tls13_keys.h"
  30. #include "ssl_debug_helpers.h"
  31. #define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
  32. psa_to_ssl_errors, \
  33. psa_generic_status_to_mbedtls)
  34. /* Write extensions */
  35. /*
  36. * ssl_tls13_write_supported_versions_ext():
  37. *
  38. * struct {
  39. * ProtocolVersion versions<2..254>;
  40. * } SupportedVersions;
  41. */
  42. MBEDTLS_CHECK_RETURN_CRITICAL
  43. static int ssl_tls13_write_supported_versions_ext(mbedtls_ssl_context *ssl,
  44. unsigned char *buf,
  45. unsigned char *end,
  46. size_t *out_len)
  47. {
  48. unsigned char *p = buf;
  49. unsigned char versions_len = (ssl->handshake->min_tls_version <=
  50. MBEDTLS_SSL_VERSION_TLS1_2) ? 4 : 2;
  51. *out_len = 0;
  52. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported versions extension"));
  53. /* Check if we have space to write the extension:
  54. * - extension_type (2 bytes)
  55. * - extension_data_length (2 bytes)
  56. * - versions_length (1 byte )
  57. * - versions (2 or 4 bytes)
  58. */
  59. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + versions_len);
  60. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0);
  61. MBEDTLS_PUT_UINT16_BE(versions_len + 1, p, 2);
  62. p += 4;
  63. /* Length of versions */
  64. *p++ = versions_len;
  65. /* Write values of supported versions.
  66. * They are defined by the configuration.
  67. * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
  68. */
  69. mbedtls_ssl_write_version(p, MBEDTLS_SSL_TRANSPORT_STREAM,
  70. MBEDTLS_SSL_VERSION_TLS1_3);
  71. MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [3:4]"));
  72. if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2) {
  73. mbedtls_ssl_write_version(p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
  74. MBEDTLS_SSL_VERSION_TLS1_2);
  75. MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [3:3]"));
  76. }
  77. *out_len = 5 + versions_len;
  78. mbedtls_ssl_tls13_set_hs_sent_ext_mask(
  79. ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
  80. return 0;
  81. }
  82. MBEDTLS_CHECK_RETURN_CRITICAL
  83. static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
  84. const unsigned char *buf,
  85. const unsigned char *end)
  86. {
  87. ((void) ssl);
  88. MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 2);
  89. if (mbedtls_ssl_read_version(buf, ssl->conf->transport) !=
  90. MBEDTLS_SSL_VERSION_TLS1_3) {
  91. MBEDTLS_SSL_DEBUG_MSG(1, ("unexpected version"));
  92. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  93. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  94. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  95. }
  96. if (&buf[2] != end) {
  97. MBEDTLS_SSL_DEBUG_MSG(1, ("supported_versions ext data length incorrect"));
  98. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  99. MBEDTLS_ERR_SSL_DECODE_ERROR);
  100. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  101. }
  102. return 0;
  103. }
  104. #if defined(MBEDTLS_SSL_ALPN)
  105. MBEDTLS_CHECK_RETURN_CRITICAL
  106. static int ssl_tls13_parse_alpn_ext(mbedtls_ssl_context *ssl,
  107. const unsigned char *buf, size_t len)
  108. {
  109. const unsigned char *p = buf;
  110. const unsigned char *end = buf + len;
  111. size_t protocol_name_list_len, protocol_name_len;
  112. const unsigned char *protocol_name_list_end;
  113. /* If we didn't send it, the server shouldn't send it */
  114. if (ssl->conf->alpn_list == NULL) {
  115. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  116. }
  117. /*
  118. * opaque ProtocolName<1..2^8-1>;
  119. *
  120. * struct {
  121. * ProtocolName protocol_name_list<2..2^16-1>
  122. * } ProtocolNameList;
  123. *
  124. * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
  125. */
  126. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  127. protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
  128. p += 2;
  129. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
  130. protocol_name_list_end = p + protocol_name_list_len;
  131. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, 1);
  132. protocol_name_len = *p++;
  133. /* Check that the server chosen protocol was in our list and save it */
  134. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, protocol_name_len);
  135. for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
  136. if (protocol_name_len == strlen(*alpn) &&
  137. memcmp(p, *alpn, protocol_name_len) == 0) {
  138. ssl->alpn_chosen = *alpn;
  139. return 0;
  140. }
  141. }
  142. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  143. }
  144. #endif /* MBEDTLS_SSL_ALPN */
  145. MBEDTLS_CHECK_RETURN_CRITICAL
  146. static int ssl_tls13_reset_key_share(mbedtls_ssl_context *ssl)
  147. {
  148. uint16_t group_id = ssl->handshake->offered_group_id;
  149. if (group_id == 0) {
  150. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  151. }
  152. #if defined(MBEDTLS_ECDH_C)
  153. if (mbedtls_ssl_tls13_named_group_is_ecdhe(group_id)) {
  154. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  155. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  156. /* Destroy generated private key. */
  157. status = psa_destroy_key(ssl->handshake->ecdh_psa_privkey);
  158. if (status != PSA_SUCCESS) {
  159. ret = PSA_TO_MBEDTLS_ERR(status);
  160. MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
  161. return ret;
  162. }
  163. ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  164. return 0;
  165. } else
  166. #endif /* MBEDTLS_ECDH_C */
  167. if (0 /* other KEMs? */) {
  168. /* Do something */
  169. }
  170. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  171. }
  172. /*
  173. * Functions for writing key_share extension.
  174. */
  175. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
  176. MBEDTLS_CHECK_RETURN_CRITICAL
  177. static int ssl_tls13_get_default_group_id(mbedtls_ssl_context *ssl,
  178. uint16_t *group_id)
  179. {
  180. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  181. #if defined(MBEDTLS_ECDH_C)
  182. const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
  183. /* Pick first available ECDHE group compatible with TLS 1.3 */
  184. if (group_list == NULL) {
  185. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  186. }
  187. for (; *group_list != 0; group_list++) {
  188. if ((mbedtls_ssl_get_psa_curve_info_from_tls_id(*group_list,
  189. NULL, NULL) == PSA_SUCCESS) &&
  190. mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list)) {
  191. *group_id = *group_list;
  192. return 0;
  193. }
  194. }
  195. #else
  196. ((void) ssl);
  197. ((void) group_id);
  198. #endif /* MBEDTLS_ECDH_C */
  199. /*
  200. * Add DHE named groups here.
  201. * Pick first available DHE group compatible with TLS 1.3
  202. */
  203. return ret;
  204. }
  205. /*
  206. * ssl_tls13_write_key_share_ext
  207. *
  208. * Structure of key_share extension in ClientHello:
  209. *
  210. * struct {
  211. * NamedGroup group;
  212. * opaque key_exchange<1..2^16-1>;
  213. * } KeyShareEntry;
  214. * struct {
  215. * KeyShareEntry client_shares<0..2^16-1>;
  216. * } KeyShareClientHello;
  217. */
  218. MBEDTLS_CHECK_RETURN_CRITICAL
  219. static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
  220. unsigned char *buf,
  221. unsigned char *end,
  222. size_t *out_len)
  223. {
  224. unsigned char *p = buf;
  225. unsigned char *client_shares; /* Start of client_shares */
  226. size_t client_shares_len; /* Length of client_shares */
  227. uint16_t group_id;
  228. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  229. *out_len = 0;
  230. /* Check if we have space for header and length fields:
  231. * - extension_type (2 bytes)
  232. * - extension_data_length (2 bytes)
  233. * - client_shares_length (2 bytes)
  234. */
  235. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
  236. p += 6;
  237. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello: adding key share extension"));
  238. /* HRR could already have requested something else. */
  239. group_id = ssl->handshake->offered_group_id;
  240. if (!mbedtls_ssl_tls13_named_group_is_ecdhe(group_id) &&
  241. !mbedtls_ssl_tls13_named_group_is_dhe(group_id)) {
  242. MBEDTLS_SSL_PROC_CHK(ssl_tls13_get_default_group_id(ssl,
  243. &group_id));
  244. }
  245. /*
  246. * Dispatch to type-specific key generation function.
  247. *
  248. * So far, we're only supporting ECDHE. With the introduction
  249. * of PQC KEMs, we'll want to have multiple branches, one per
  250. * type of KEM, and dispatch to the corresponding crypto. And
  251. * only one key share entry is allowed.
  252. */
  253. client_shares = p;
  254. #if defined(MBEDTLS_ECDH_C)
  255. if (mbedtls_ssl_tls13_named_group_is_ecdhe(group_id)) {
  256. /* Pointer to group */
  257. unsigned char *group = p;
  258. /* Length of key_exchange */
  259. size_t key_exchange_len = 0;
  260. /* Check there is space for header of KeyShareEntry
  261. * - group (2 bytes)
  262. * - key_exchange_length (2 bytes)
  263. */
  264. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
  265. p += 4;
  266. ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
  267. ssl, group_id, p, end, &key_exchange_len);
  268. p += key_exchange_len;
  269. if (ret != 0) {
  270. return ret;
  271. }
  272. /* Write group */
  273. MBEDTLS_PUT_UINT16_BE(group_id, group, 0);
  274. /* Write key_exchange_length */
  275. MBEDTLS_PUT_UINT16_BE(key_exchange_len, group, 2);
  276. } else
  277. #endif /* MBEDTLS_ECDH_C */
  278. if (0 /* other KEMs? */) {
  279. /* Do something */
  280. } else {
  281. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  282. }
  283. /* Length of client_shares */
  284. client_shares_len = p - client_shares;
  285. if (client_shares_len == 0) {
  286. MBEDTLS_SSL_DEBUG_MSG(1, ("No key share defined."));
  287. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  288. }
  289. /* Write extension_type */
  290. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
  291. /* Write extension_data_length */
  292. MBEDTLS_PUT_UINT16_BE(client_shares_len + 2, buf, 2);
  293. /* Write client_shares_length */
  294. MBEDTLS_PUT_UINT16_BE(client_shares_len, buf, 4);
  295. /* Update offered_group_id field */
  296. ssl->handshake->offered_group_id = group_id;
  297. /* Output the total length of key_share extension. */
  298. *out_len = p - buf;
  299. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, key_share extension", buf, *out_len);
  300. mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
  301. cleanup:
  302. return ret;
  303. }
  304. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
  305. /*
  306. * ssl_tls13_parse_hrr_key_share_ext()
  307. * Parse key_share extension in Hello Retry Request
  308. *
  309. * struct {
  310. * NamedGroup selected_group;
  311. * } KeyShareHelloRetryRequest;
  312. */
  313. MBEDTLS_CHECK_RETURN_CRITICAL
  314. static int ssl_tls13_parse_hrr_key_share_ext(mbedtls_ssl_context *ssl,
  315. const unsigned char *buf,
  316. const unsigned char *end)
  317. {
  318. #if defined(MBEDTLS_ECDH_C)
  319. const unsigned char *p = buf;
  320. int selected_group;
  321. int found = 0;
  322. const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
  323. if (group_list == NULL) {
  324. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  325. }
  326. MBEDTLS_SSL_DEBUG_BUF(3, "key_share extension", p, end - buf);
  327. /* Read selected_group */
  328. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  329. selected_group = MBEDTLS_GET_UINT16_BE(p, 0);
  330. MBEDTLS_SSL_DEBUG_MSG(3, ("selected_group ( %d )", selected_group));
  331. /* Upon receipt of this extension in a HelloRetryRequest, the client
  332. * MUST first verify that the selected_group field corresponds to a
  333. * group which was provided in the "supported_groups" extension in the
  334. * original ClientHello.
  335. * The supported_group was based on the info in ssl->conf->group_list.
  336. *
  337. * If the server provided a key share that was not sent in the ClientHello
  338. * then the client MUST abort the handshake with an "illegal_parameter" alert.
  339. */
  340. for (; *group_list != 0; group_list++) {
  341. if ((mbedtls_ssl_get_psa_curve_info_from_tls_id(*group_list,
  342. NULL, NULL) == PSA_ERROR_NOT_SUPPORTED) ||
  343. *group_list != selected_group) {
  344. continue;
  345. }
  346. /* We found a match */
  347. found = 1;
  348. break;
  349. }
  350. /* Client MUST verify that the selected_group field does not
  351. * correspond to a group which was provided in the "key_share"
  352. * extension in the original ClientHello. If the server sent an
  353. * HRR message with a key share already provided in the
  354. * ClientHello then the client MUST abort the handshake with
  355. * an "illegal_parameter" alert.
  356. */
  357. if (found == 0 || selected_group == ssl->handshake->offered_group_id) {
  358. MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid key share in HRR"));
  359. MBEDTLS_SSL_PEND_FATAL_ALERT(
  360. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  361. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  362. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  363. }
  364. /* Remember server's preference for next ClientHello */
  365. ssl->handshake->offered_group_id = selected_group;
  366. return 0;
  367. #else
  368. (void) ssl;
  369. (void) buf;
  370. (void) end;
  371. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  372. #endif
  373. }
  374. /*
  375. * ssl_tls13_parse_key_share_ext()
  376. * Parse key_share extension in Server Hello
  377. *
  378. * struct {
  379. * KeyShareEntry server_share;
  380. * } KeyShareServerHello;
  381. * struct {
  382. * NamedGroup group;
  383. * opaque key_exchange<1..2^16-1>;
  384. * } KeyShareEntry;
  385. */
  386. MBEDTLS_CHECK_RETURN_CRITICAL
  387. static int ssl_tls13_parse_key_share_ext(mbedtls_ssl_context *ssl,
  388. const unsigned char *buf,
  389. const unsigned char *end)
  390. {
  391. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  392. const unsigned char *p = buf;
  393. uint16_t group, offered_group;
  394. /* ...
  395. * NamedGroup group; (2 bytes)
  396. * ...
  397. */
  398. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  399. group = MBEDTLS_GET_UINT16_BE(p, 0);
  400. p += 2;
  401. /* Check that the chosen group matches the one we offered. */
  402. offered_group = ssl->handshake->offered_group_id;
  403. if (offered_group != group) {
  404. MBEDTLS_SSL_DEBUG_MSG(1,
  405. ("Invalid server key share, our group %u, their group %u",
  406. (unsigned) offered_group, (unsigned) group));
  407. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  408. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  409. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  410. }
  411. #if defined(MBEDTLS_ECDH_C)
  412. if (mbedtls_ssl_tls13_named_group_is_ecdhe(group)) {
  413. if (mbedtls_ssl_get_psa_curve_info_from_tls_id(group, NULL, NULL)
  414. == PSA_ERROR_NOT_SUPPORTED) {
  415. MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid TLS curve group id"));
  416. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  417. }
  418. MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
  419. mbedtls_ssl_get_curve_name_from_tls_id(group)));
  420. ret = mbedtls_ssl_tls13_read_public_ecdhe_share(ssl, p, end - p);
  421. if (ret != 0) {
  422. return ret;
  423. }
  424. } else
  425. #endif /* MBEDTLS_ECDH_C */
  426. if (0 /* other KEMs? */) {
  427. /* Do something */
  428. } else {
  429. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  430. }
  431. return ret;
  432. }
  433. /*
  434. * ssl_tls13_parse_cookie_ext()
  435. * Parse cookie extension in Hello Retry Request
  436. *
  437. * struct {
  438. * opaque cookie<1..2^16-1>;
  439. * } Cookie;
  440. *
  441. * When sending a HelloRetryRequest, the server MAY provide a "cookie"
  442. * extension to the client (this is an exception to the usual rule that
  443. * the only extensions that may be sent are those that appear in the
  444. * ClientHello). When sending the new ClientHello, the client MUST copy
  445. * the contents of the extension received in the HelloRetryRequest into
  446. * a "cookie" extension in the new ClientHello. Clients MUST NOT use
  447. * cookies in their initial ClientHello in subsequent connections.
  448. */
  449. MBEDTLS_CHECK_RETURN_CRITICAL
  450. static int ssl_tls13_parse_cookie_ext(mbedtls_ssl_context *ssl,
  451. const unsigned char *buf,
  452. const unsigned char *end)
  453. {
  454. uint16_t cookie_len;
  455. const unsigned char *p = buf;
  456. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  457. /* Retrieve length field of cookie */
  458. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  459. cookie_len = MBEDTLS_GET_UINT16_BE(p, 0);
  460. p += 2;
  461. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cookie_len);
  462. MBEDTLS_SSL_DEBUG_BUF(3, "cookie extension", p, cookie_len);
  463. mbedtls_free(handshake->cookie);
  464. handshake->cookie_len = 0;
  465. handshake->cookie = mbedtls_calloc(1, cookie_len);
  466. if (handshake->cookie == NULL) {
  467. MBEDTLS_SSL_DEBUG_MSG(1,
  468. ("alloc failed ( %ud bytes )",
  469. cookie_len));
  470. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  471. }
  472. memcpy(handshake->cookie, p, cookie_len);
  473. handshake->cookie_len = cookie_len;
  474. return 0;
  475. }
  476. MBEDTLS_CHECK_RETURN_CRITICAL
  477. static int ssl_tls13_write_cookie_ext(mbedtls_ssl_context *ssl,
  478. unsigned char *buf,
  479. unsigned char *end,
  480. size_t *out_len)
  481. {
  482. unsigned char *p = buf;
  483. *out_len = 0;
  484. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  485. if (handshake->cookie == NULL) {
  486. MBEDTLS_SSL_DEBUG_MSG(3, ("no cookie to send; skip extension"));
  487. return 0;
  488. }
  489. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
  490. handshake->cookie,
  491. handshake->cookie_len);
  492. MBEDTLS_SSL_CHK_BUF_PTR(p, end, handshake->cookie_len + 6);
  493. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding cookie extension"));
  494. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_COOKIE, p, 0);
  495. MBEDTLS_PUT_UINT16_BE(handshake->cookie_len + 2, p, 2);
  496. MBEDTLS_PUT_UINT16_BE(handshake->cookie_len, p, 4);
  497. p += 6;
  498. /* Cookie */
  499. memcpy(p, handshake->cookie, handshake->cookie_len);
  500. *out_len = handshake->cookie_len + 6;
  501. mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_COOKIE);
  502. return 0;
  503. }
  504. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  505. /*
  506. * ssl_tls13_write_psk_key_exchange_modes_ext() structure:
  507. *
  508. * enum { psk_ke( 0 ), psk_dhe_ke( 1 ), ( 255 ) } PskKeyExchangeMode;
  509. *
  510. * struct {
  511. * PskKeyExchangeMode ke_modes<1..255>;
  512. * } PskKeyExchangeModes;
  513. */
  514. MBEDTLS_CHECK_RETURN_CRITICAL
  515. static int ssl_tls13_write_psk_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
  516. unsigned char *buf,
  517. unsigned char *end,
  518. size_t *out_len)
  519. {
  520. unsigned char *p = buf;
  521. int ke_modes_len = 0;
  522. ((void) ke_modes_len);
  523. *out_len = 0;
  524. /* Skip writing extension if no PSK key exchange mode
  525. * is enabled in the config.
  526. */
  527. if (!mbedtls_ssl_conf_tls13_some_psk_enabled(ssl)) {
  528. MBEDTLS_SSL_DEBUG_MSG(3, ("skip psk_key_exchange_modes extension"));
  529. return 0;
  530. }
  531. /* Require 7 bytes of data, otherwise fail,
  532. * even if extension might be shorter.
  533. */
  534. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7);
  535. MBEDTLS_SSL_DEBUG_MSG(
  536. 3, ("client hello, adding psk_key_exchange_modes extension"));
  537. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, p, 0);
  538. /* Skip extension length (2 bytes) and
  539. * ke_modes length (1 byte) for now.
  540. */
  541. p += 5;
  542. if (mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl)) {
  543. *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE;
  544. ke_modes_len++;
  545. MBEDTLS_SSL_DEBUG_MSG(4, ("Adding PSK-ECDHE key exchange mode"));
  546. }
  547. if (mbedtls_ssl_conf_tls13_psk_enabled(ssl)) {
  548. *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE;
  549. ke_modes_len++;
  550. MBEDTLS_SSL_DEBUG_MSG(4, ("Adding pure PSK key exchange mode"));
  551. }
  552. /* Now write the extension and ke_modes length */
  553. MBEDTLS_PUT_UINT16_BE(ke_modes_len + 1, buf, 2);
  554. buf[4] = ke_modes_len;
  555. *out_len = p - buf;
  556. mbedtls_ssl_tls13_set_hs_sent_ext_mask(
  557. ssl, MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES);
  558. return 0;
  559. }
  560. static psa_algorithm_t ssl_tls13_get_ciphersuite_hash_alg(int ciphersuite)
  561. {
  562. const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
  563. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
  564. if (ciphersuite_info != NULL) {
  565. return mbedtls_psa_translate_md(ciphersuite_info->mac);
  566. }
  567. return PSA_ALG_NONE;
  568. }
  569. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  570. static int ssl_tls13_has_configured_ticket(mbedtls_ssl_context *ssl)
  571. {
  572. mbedtls_ssl_session *session = ssl->session_negotiate;
  573. return ssl->handshake->resume &&
  574. session != NULL && session->ticket != NULL &&
  575. mbedtls_ssl_conf_tls13_check_kex_modes(
  576. ssl, mbedtls_ssl_session_get_ticket_flags(
  577. session, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL));
  578. }
  579. #if defined(MBEDTLS_SSL_EARLY_DATA)
  580. static int ssl_tls13_early_data_has_valid_ticket(mbedtls_ssl_context *ssl)
  581. {
  582. mbedtls_ssl_session *session = ssl->session_negotiate;
  583. return ssl->handshake->resume &&
  584. session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
  585. (session->ticket_flags &
  586. MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA) &&
  587. mbedtls_ssl_tls13_cipher_suite_is_offered(
  588. ssl, session->ciphersuite);
  589. }
  590. #endif
  591. MBEDTLS_CHECK_RETURN_CRITICAL
  592. static int ssl_tls13_ticket_get_identity(mbedtls_ssl_context *ssl,
  593. psa_algorithm_t *hash_alg,
  594. const unsigned char **identity,
  595. size_t *identity_len)
  596. {
  597. mbedtls_ssl_session *session = ssl->session_negotiate;
  598. if (!ssl_tls13_has_configured_ticket(ssl)) {
  599. return -1;
  600. }
  601. *hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
  602. *identity = session->ticket;
  603. *identity_len = session->ticket_len;
  604. return 0;
  605. }
  606. MBEDTLS_CHECK_RETURN_CRITICAL
  607. static int ssl_tls13_ticket_get_psk(mbedtls_ssl_context *ssl,
  608. psa_algorithm_t *hash_alg,
  609. const unsigned char **psk,
  610. size_t *psk_len)
  611. {
  612. mbedtls_ssl_session *session = ssl->session_negotiate;
  613. if (!ssl_tls13_has_configured_ticket(ssl)) {
  614. return -1;
  615. }
  616. *hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
  617. *psk = session->resumption_key;
  618. *psk_len = session->resumption_key_len;
  619. return 0;
  620. }
  621. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  622. MBEDTLS_CHECK_RETURN_CRITICAL
  623. static int ssl_tls13_psk_get_identity(mbedtls_ssl_context *ssl,
  624. psa_algorithm_t *hash_alg,
  625. const unsigned char **identity,
  626. size_t *identity_len)
  627. {
  628. if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
  629. return -1;
  630. }
  631. *hash_alg = PSA_ALG_SHA_256;
  632. *identity = ssl->conf->psk_identity;
  633. *identity_len = ssl->conf->psk_identity_len;
  634. return 0;
  635. }
  636. MBEDTLS_CHECK_RETURN_CRITICAL
  637. static int ssl_tls13_psk_get_psk(mbedtls_ssl_context *ssl,
  638. psa_algorithm_t *hash_alg,
  639. const unsigned char **psk,
  640. size_t *psk_len)
  641. {
  642. if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
  643. return -1;
  644. }
  645. *hash_alg = PSA_ALG_SHA_256;
  646. *psk = ssl->conf->psk;
  647. *psk_len = ssl->conf->psk_len;
  648. return 0;
  649. }
  650. static int ssl_tls13_get_configured_psk_count(mbedtls_ssl_context *ssl)
  651. {
  652. int configured_psk_count = 0;
  653. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  654. if (ssl_tls13_has_configured_ticket(ssl)) {
  655. MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket is configured"));
  656. configured_psk_count++;
  657. }
  658. #endif
  659. if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
  660. MBEDTLS_SSL_DEBUG_MSG(3, ("PSK is configured"));
  661. configured_psk_count++;
  662. }
  663. return configured_psk_count;
  664. }
  665. MBEDTLS_CHECK_RETURN_CRITICAL
  666. static int ssl_tls13_write_identity(mbedtls_ssl_context *ssl,
  667. unsigned char *buf,
  668. unsigned char *end,
  669. const unsigned char *identity,
  670. size_t identity_len,
  671. uint32_t obfuscated_ticket_age,
  672. size_t *out_len)
  673. {
  674. ((void) ssl);
  675. *out_len = 0;
  676. /*
  677. * - identity_len (2 bytes)
  678. * - identity (psk_identity_len bytes)
  679. * - obfuscated_ticket_age (4 bytes)
  680. */
  681. MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6 + identity_len);
  682. MBEDTLS_PUT_UINT16_BE(identity_len, buf, 0);
  683. memcpy(buf + 2, identity, identity_len);
  684. MBEDTLS_PUT_UINT32_BE(obfuscated_ticket_age, buf, 2 + identity_len);
  685. MBEDTLS_SSL_DEBUG_BUF(4, "write identity", buf, 6 + identity_len);
  686. *out_len = 6 + identity_len;
  687. return 0;
  688. }
  689. MBEDTLS_CHECK_RETURN_CRITICAL
  690. static int ssl_tls13_write_binder(mbedtls_ssl_context *ssl,
  691. unsigned char *buf,
  692. unsigned char *end,
  693. int psk_type,
  694. psa_algorithm_t hash_alg,
  695. const unsigned char *psk,
  696. size_t psk_len,
  697. size_t *out_len)
  698. {
  699. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  700. unsigned char binder_len;
  701. unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  702. size_t transcript_len = 0;
  703. *out_len = 0;
  704. binder_len = PSA_HASH_LENGTH(hash_alg);
  705. /*
  706. * - binder_len (1 bytes)
  707. * - binder (binder_len bytes)
  708. */
  709. MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1 + binder_len);
  710. buf[0] = binder_len;
  711. /* Get current state of handshake transcript. */
  712. ret = mbedtls_ssl_get_handshake_transcript(
  713. ssl, mbedtls_hash_info_md_from_psa(hash_alg),
  714. transcript, sizeof(transcript), &transcript_len);
  715. if (ret != 0) {
  716. return ret;
  717. }
  718. ret = mbedtls_ssl_tls13_create_psk_binder(ssl, hash_alg,
  719. psk, psk_len, psk_type,
  720. transcript, buf + 1);
  721. if (ret != 0) {
  722. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_create_psk_binder", ret);
  723. return ret;
  724. }
  725. MBEDTLS_SSL_DEBUG_BUF(4, "write binder", buf, 1 + binder_len);
  726. *out_len = 1 + binder_len;
  727. return 0;
  728. }
  729. /*
  730. * mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext() structure:
  731. *
  732. * struct {
  733. * opaque identity<1..2^16-1>;
  734. * uint32 obfuscated_ticket_age;
  735. * } PskIdentity;
  736. *
  737. * opaque PskBinderEntry<32..255>;
  738. *
  739. * struct {
  740. * PskIdentity identities<7..2^16-1>;
  741. * PskBinderEntry binders<33..2^16-1>;
  742. * } OfferedPsks;
  743. *
  744. * struct {
  745. * select (Handshake.msg_type) {
  746. * case client_hello: OfferedPsks;
  747. * ...
  748. * };
  749. * } PreSharedKeyExtension;
  750. *
  751. */
  752. int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
  753. mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end,
  754. size_t *out_len, size_t *binders_len)
  755. {
  756. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  757. int configured_psk_count = 0;
  758. unsigned char *p = buf;
  759. psa_algorithm_t hash_alg = PSA_ALG_NONE;
  760. const unsigned char *identity;
  761. size_t identity_len;
  762. size_t l_binders_len = 0;
  763. size_t output_len;
  764. *out_len = 0;
  765. *binders_len = 0;
  766. /* Check if we have any PSKs to offer. If no, skip pre_shared_key */
  767. configured_psk_count = ssl_tls13_get_configured_psk_count(ssl);
  768. if (configured_psk_count == 0) {
  769. MBEDTLS_SSL_DEBUG_MSG(3, ("skip pre_shared_key extensions"));
  770. return 0;
  771. }
  772. MBEDTLS_SSL_DEBUG_MSG(4, ("Pre-configured PSK number = %d",
  773. configured_psk_count));
  774. /* Check if we have space to write the extension, binders included.
  775. * - extension_type (2 bytes)
  776. * - extension_data_len (2 bytes)
  777. * - identities_len (2 bytes)
  778. */
  779. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
  780. p += 6;
  781. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  782. if (ssl_tls13_ticket_get_identity(
  783. ssl, &hash_alg, &identity, &identity_len) == 0) {
  784. #if defined(MBEDTLS_HAVE_TIME)
  785. mbedtls_time_t now = mbedtls_time(NULL);
  786. mbedtls_ssl_session *session = ssl->session_negotiate;
  787. uint32_t obfuscated_ticket_age =
  788. (uint32_t) (now - session->ticket_received);
  789. /*
  790. * The ticket timestamp is in seconds but the ticket age is in
  791. * milliseconds. If the ticket was received at the end of a second and
  792. * re-used here just at the beginning of the next second, the computed
  793. * age `now - session->ticket_received` is equal to 1s thus 1000 ms
  794. * while the actual age could be just a few milliseconds or tens of
  795. * milliseconds. If the server has more accurate ticket timestamps
  796. * (typically timestamps in milliseconds), as part of the processing of
  797. * the ClientHello, it may compute a ticket lifetime smaller than the
  798. * one computed here and potentially reject the ticket. To avoid that,
  799. * remove one second to the ticket age if possible.
  800. */
  801. if (obfuscated_ticket_age > 0) {
  802. obfuscated_ticket_age -= 1;
  803. }
  804. obfuscated_ticket_age *= 1000;
  805. obfuscated_ticket_age += session->ticket_age_add;
  806. ret = ssl_tls13_write_identity(ssl, p, end,
  807. identity, identity_len,
  808. obfuscated_ticket_age,
  809. &output_len);
  810. #else
  811. ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len,
  812. 0, &output_len);
  813. #endif /* MBEDTLS_HAVE_TIME */
  814. if (ret != 0) {
  815. return ret;
  816. }
  817. p += output_len;
  818. l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
  819. }
  820. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  821. if (ssl_tls13_psk_get_identity(
  822. ssl, &hash_alg, &identity, &identity_len) == 0) {
  823. ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len, 0,
  824. &output_len);
  825. if (ret != 0) {
  826. return ret;
  827. }
  828. p += output_len;
  829. l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
  830. }
  831. MBEDTLS_SSL_DEBUG_MSG(3,
  832. ("client hello, adding pre_shared_key extension, "
  833. "omitting PSK binder list"));
  834. /* Take into account the two bytes for the length of the binders. */
  835. l_binders_len += 2;
  836. /* Check if there is enough space for binders */
  837. MBEDTLS_SSL_CHK_BUF_PTR(p, end, l_binders_len);
  838. /*
  839. * - extension_type (2 bytes)
  840. * - extension_data_len (2 bytes)
  841. * - identities_len (2 bytes)
  842. */
  843. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, buf, 0);
  844. MBEDTLS_PUT_UINT16_BE(p - buf - 4 + l_binders_len, buf, 2);
  845. MBEDTLS_PUT_UINT16_BE(p - buf - 6, buf, 4);
  846. *out_len = (p - buf) + l_binders_len;
  847. *binders_len = l_binders_len;
  848. MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key identities", buf, p - buf);
  849. return 0;
  850. }
  851. int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
  852. mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end)
  853. {
  854. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  855. unsigned char *p = buf;
  856. psa_algorithm_t hash_alg = PSA_ALG_NONE;
  857. const unsigned char *psk;
  858. size_t psk_len;
  859. size_t output_len;
  860. /* Check if we have space to write binders_len.
  861. * - binders_len (2 bytes)
  862. */
  863. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  864. p += 2;
  865. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  866. if (ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
  867. ret = ssl_tls13_write_binder(ssl, p, end,
  868. MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION,
  869. hash_alg, psk, psk_len,
  870. &output_len);
  871. if (ret != 0) {
  872. return ret;
  873. }
  874. p += output_len;
  875. }
  876. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  877. if (ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
  878. ret = ssl_tls13_write_binder(ssl, p, end,
  879. MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL,
  880. hash_alg, psk, psk_len,
  881. &output_len);
  882. if (ret != 0) {
  883. return ret;
  884. }
  885. p += output_len;
  886. }
  887. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding PSK binder list."));
  888. /*
  889. * - binders_len (2 bytes)
  890. */
  891. MBEDTLS_PUT_UINT16_BE(p - buf - 2, buf, 0);
  892. MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key binders", buf, p - buf);
  893. mbedtls_ssl_tls13_set_hs_sent_ext_mask(
  894. ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
  895. return 0;
  896. }
  897. /*
  898. * struct {
  899. * opaque identity<1..2^16-1>;
  900. * uint32 obfuscated_ticket_age;
  901. * } PskIdentity;
  902. *
  903. * opaque PskBinderEntry<32..255>;
  904. *
  905. * struct {
  906. *
  907. * select (Handshake.msg_type) {
  908. * ...
  909. * case server_hello: uint16 selected_identity;
  910. * };
  911. *
  912. * } PreSharedKeyExtension;
  913. *
  914. */
  915. MBEDTLS_CHECK_RETURN_CRITICAL
  916. static int ssl_tls13_parse_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
  917. const unsigned char *buf,
  918. const unsigned char *end)
  919. {
  920. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  921. int selected_identity;
  922. const unsigned char *psk;
  923. size_t psk_len;
  924. psa_algorithm_t hash_alg;
  925. MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 2);
  926. selected_identity = MBEDTLS_GET_UINT16_BE(buf, 0);
  927. ssl->handshake->selected_identity = (uint16_t) selected_identity;
  928. MBEDTLS_SSL_DEBUG_MSG(3, ("selected_identity = %d", selected_identity));
  929. if (selected_identity >= ssl_tls13_get_configured_psk_count(ssl)) {
  930. MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid PSK identity."));
  931. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  932. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  933. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  934. }
  935. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  936. if (selected_identity == 0 && ssl_tls13_has_configured_ticket(ssl)) {
  937. ret = ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len);
  938. } else
  939. #endif
  940. if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
  941. ret = ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len);
  942. } else {
  943. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  944. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  945. }
  946. if (ret != 0) {
  947. return ret;
  948. }
  949. if (mbedtls_psa_translate_md(ssl->handshake->ciphersuite_info->mac)
  950. != hash_alg) {
  951. MBEDTLS_SSL_DEBUG_MSG(
  952. 1, ("Invalid ciphersuite for external psk."));
  953. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  954. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  955. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  956. }
  957. ret = mbedtls_ssl_set_hs_psk(ssl, psk, psk_len);
  958. if (ret != 0) {
  959. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
  960. return ret;
  961. }
  962. return 0;
  963. }
  964. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
  965. int mbedtls_ssl_tls13_write_client_hello_exts(mbedtls_ssl_context *ssl,
  966. unsigned char *buf,
  967. unsigned char *end,
  968. size_t *out_len)
  969. {
  970. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  971. unsigned char *p = buf;
  972. size_t ext_len;
  973. *out_len = 0;
  974. /* Write supported_versions extension
  975. *
  976. * Supported Versions Extension is mandatory with TLS 1.3.
  977. */
  978. ret = ssl_tls13_write_supported_versions_ext(ssl, p, end, &ext_len);
  979. if (ret != 0) {
  980. return ret;
  981. }
  982. p += ext_len;
  983. /* Echo the cookie if the server provided one in its preceding
  984. * HelloRetryRequest message.
  985. */
  986. ret = ssl_tls13_write_cookie_ext(ssl, p, end, &ext_len);
  987. if (ret != 0) {
  988. return ret;
  989. }
  990. p += ext_len;
  991. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
  992. if (mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
  993. ret = ssl_tls13_write_key_share_ext(ssl, p, end, &ext_len);
  994. if (ret != 0) {
  995. return ret;
  996. }
  997. p += ext_len;
  998. }
  999. #endif
  1000. #if defined(MBEDTLS_SSL_EARLY_DATA)
  1001. if (mbedtls_ssl_conf_tls13_some_psk_enabled(ssl) &&
  1002. ssl_tls13_early_data_has_valid_ticket(ssl) &&
  1003. ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED) {
  1004. ret = mbedtls_ssl_tls13_write_early_data_ext(ssl, p, end, &ext_len);
  1005. if (ret != 0) {
  1006. return ret;
  1007. }
  1008. p += ext_len;
  1009. /* Initializes the status to `rejected`. It will be updated to
  1010. * `accepted` if the EncryptedExtension message contain an early data
  1011. * indication extension.
  1012. */
  1013. ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
  1014. } else {
  1015. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write early_data extension"));
  1016. ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT;
  1017. }
  1018. #endif /* MBEDTLS_SSL_EARLY_DATA */
  1019. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1020. /* For PSK-based key exchange we need the pre_shared_key extension
  1021. * and the psk_key_exchange_modes extension.
  1022. *
  1023. * The pre_shared_key extension MUST be the last extension in the
  1024. * ClientHello. Servers MUST check that it is the last extension and
  1025. * otherwise fail the handshake with an "illegal_parameter" alert.
  1026. *
  1027. * Add the psk_key_exchange_modes extension.
  1028. */
  1029. ret = ssl_tls13_write_psk_key_exchange_modes_ext(ssl, p, end, &ext_len);
  1030. if (ret != 0) {
  1031. return ret;
  1032. }
  1033. p += ext_len;
  1034. #endif
  1035. *out_len = p - buf;
  1036. return 0;
  1037. }
  1038. int mbedtls_ssl_tls13_finalize_client_hello(mbedtls_ssl_context *ssl)
  1039. {
  1040. ((void) ssl);
  1041. #if defined(MBEDTLS_SSL_EARLY_DATA)
  1042. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1043. psa_algorithm_t hash_alg = PSA_ALG_NONE;
  1044. const unsigned char *psk;
  1045. size_t psk_len;
  1046. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  1047. if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED) {
  1048. #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
  1049. mbedtls_ssl_handshake_set_state(
  1050. ssl, MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO);
  1051. #endif
  1052. MBEDTLS_SSL_DEBUG_MSG(
  1053. 1, ("Set hs psk for early data when writing the first psk"));
  1054. ret = ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len);
  1055. if (ret != 0) {
  1056. MBEDTLS_SSL_DEBUG_RET(
  1057. 1, "ssl_tls13_ticket_get_psk", ret);
  1058. return ret;
  1059. }
  1060. ret = mbedtls_ssl_set_hs_psk(ssl, psk, psk_len);
  1061. if (ret != 0) {
  1062. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
  1063. return ret;
  1064. }
  1065. /*
  1066. * Early data are going to be encrypted using the ciphersuite
  1067. * associated with the pre-shared key used for the handshake.
  1068. * Note that if the server rejects early data, the handshake
  1069. * based on the pre-shared key may complete successfully
  1070. * with a selected ciphersuite different from the ciphersuite
  1071. * associated with the pre-shared key. Only the hashes of the
  1072. * two ciphersuites have to be the same. In that case, the
  1073. * encrypted handshake data and application data are
  1074. * encrypted using a different ciphersuite than the one used for
  1075. * the rejected early data.
  1076. */
  1077. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
  1078. ssl->session_negotiate->ciphersuite);
  1079. ssl->handshake->ciphersuite_info = ciphersuite_info;
  1080. /* Enable psk and psk_ephemeral to make stage early happy */
  1081. ssl->handshake->key_exchange_mode =
  1082. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
  1083. /* Start the TLS 1.3 key schedule:
  1084. * Set the PSK and derive early secret.
  1085. */
  1086. ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
  1087. if (ret != 0) {
  1088. MBEDTLS_SSL_DEBUG_RET(
  1089. 1, "mbedtls_ssl_tls13_key_schedule_stage_early", ret);
  1090. return ret;
  1091. }
  1092. /* Derive early data key material */
  1093. ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
  1094. if (ret != 0) {
  1095. MBEDTLS_SSL_DEBUG_RET(
  1096. 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
  1097. return ret;
  1098. }
  1099. }
  1100. #endif /* MBEDTLS_SSL_EARLY_DATA */
  1101. return 0;
  1102. }
  1103. /*
  1104. * Functions for parsing and processing Server Hello
  1105. */
  1106. /**
  1107. * \brief Detect if the ServerHello contains a supported_versions extension
  1108. * or not.
  1109. *
  1110. * \param[in] ssl SSL context
  1111. * \param[in] buf Buffer containing the ServerHello message
  1112. * \param[in] end End of the buffer containing the ServerHello message
  1113. *
  1114. * \return 0 if the ServerHello does not contain a supported_versions extension
  1115. * \return 1 if the ServerHello contains a supported_versions extension
  1116. * \return A negative value if an error occurred while parsing the ServerHello.
  1117. */
  1118. MBEDTLS_CHECK_RETURN_CRITICAL
  1119. static int ssl_tls13_is_supported_versions_ext_present(
  1120. mbedtls_ssl_context *ssl,
  1121. const unsigned char *buf,
  1122. const unsigned char *end)
  1123. {
  1124. const unsigned char *p = buf;
  1125. size_t legacy_session_id_echo_len;
  1126. size_t extensions_len;
  1127. const unsigned char *extensions_end;
  1128. /*
  1129. * Check there is enough data to access the legacy_session_id_echo vector
  1130. * length:
  1131. * - legacy_version 2 bytes
  1132. * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
  1133. * - legacy_session_id_echo length 1 byte
  1134. */
  1135. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3);
  1136. p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
  1137. legacy_session_id_echo_len = *p;
  1138. /*
  1139. * Jump to the extensions, jumping over:
  1140. * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
  1141. * - cipher_suite 2 bytes
  1142. * - legacy_compression_method 1 byte
  1143. */
  1144. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len + 4);
  1145. p += legacy_session_id_echo_len + 4;
  1146. /* Case of no extension */
  1147. if (p == end) {
  1148. return 0;
  1149. }
  1150. /* ...
  1151. * Extension extensions<6..2^16-1>;
  1152. * ...
  1153. * struct {
  1154. * ExtensionType extension_type; (2 bytes)
  1155. * opaque extension_data<0..2^16-1>;
  1156. * } Extension;
  1157. */
  1158. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  1159. extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
  1160. p += 2;
  1161. /* Check extensions do not go beyond the buffer of data. */
  1162. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
  1163. extensions_end = p + extensions_len;
  1164. while (p < extensions_end) {
  1165. unsigned int extension_type;
  1166. size_t extension_data_len;
  1167. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
  1168. extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
  1169. extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
  1170. p += 4;
  1171. if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
  1172. return 1;
  1173. }
  1174. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
  1175. p += extension_data_len;
  1176. }
  1177. return 0;
  1178. }
  1179. /* Returns a negative value on failure, and otherwise
  1180. * - 1 if the last eight bytes of the ServerHello random bytes indicate that
  1181. * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
  1182. * - 0 otherwise
  1183. */
  1184. MBEDTLS_CHECK_RETURN_CRITICAL
  1185. static int ssl_tls13_is_downgrade_negotiation(mbedtls_ssl_context *ssl,
  1186. const unsigned char *buf,
  1187. const unsigned char *end)
  1188. {
  1189. /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
  1190. static const unsigned char magic_downgrade_string[] =
  1191. { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
  1192. const unsigned char *last_eight_bytes_of_random;
  1193. unsigned char last_byte_of_random;
  1194. MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2);
  1195. last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
  1196. if (memcmp(last_eight_bytes_of_random,
  1197. magic_downgrade_string,
  1198. sizeof(magic_downgrade_string)) == 0) {
  1199. last_byte_of_random = last_eight_bytes_of_random[7];
  1200. return last_byte_of_random == 0 ||
  1201. last_byte_of_random == 1;
  1202. }
  1203. return 0;
  1204. }
  1205. /* Returns a negative value on failure, and otherwise
  1206. * - SSL_SERVER_HELLO or
  1207. * - SSL_SERVER_HELLO_HRR
  1208. * to indicate which message is expected and to be parsed next.
  1209. */
  1210. #define SSL_SERVER_HELLO 0
  1211. #define SSL_SERVER_HELLO_HRR 1
  1212. MBEDTLS_CHECK_RETURN_CRITICAL
  1213. static int ssl_server_hello_is_hrr(mbedtls_ssl_context *ssl,
  1214. const unsigned char *buf,
  1215. const unsigned char *end)
  1216. {
  1217. /* Check whether this message is a HelloRetryRequest ( HRR ) message.
  1218. *
  1219. * Server Hello and HRR are only distinguished by Random set to the
  1220. * special value of the SHA-256 of "HelloRetryRequest".
  1221. *
  1222. * struct {
  1223. * ProtocolVersion legacy_version = 0x0303;
  1224. * Random random;
  1225. * opaque legacy_session_id_echo<0..32>;
  1226. * CipherSuite cipher_suite;
  1227. * uint8 legacy_compression_method = 0;
  1228. * Extension extensions<6..2^16-1>;
  1229. * } ServerHello;
  1230. *
  1231. */
  1232. MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end,
  1233. 2 + sizeof(mbedtls_ssl_tls13_hello_retry_request_magic));
  1234. if (memcmp(buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
  1235. sizeof(mbedtls_ssl_tls13_hello_retry_request_magic)) == 0) {
  1236. return SSL_SERVER_HELLO_HRR;
  1237. }
  1238. return SSL_SERVER_HELLO;
  1239. }
  1240. /*
  1241. * Returns a negative value on failure, and otherwise
  1242. * - SSL_SERVER_HELLO or
  1243. * - SSL_SERVER_HELLO_HRR or
  1244. * - SSL_SERVER_HELLO_TLS1_2
  1245. */
  1246. #define SSL_SERVER_HELLO_TLS1_2 2
  1247. MBEDTLS_CHECK_RETURN_CRITICAL
  1248. static int ssl_tls13_preprocess_server_hello(mbedtls_ssl_context *ssl,
  1249. const unsigned char *buf,
  1250. const unsigned char *end)
  1251. {
  1252. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1253. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  1254. MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_is_supported_versions_ext_present(
  1255. ssl, buf, end));
  1256. if (ret == 0) {
  1257. MBEDTLS_SSL_PROC_CHK_NEG(
  1258. ssl_tls13_is_downgrade_negotiation(ssl, buf, end));
  1259. /* If the server is negotiating TLS 1.2 or below and:
  1260. * . we did not propose TLS 1.2 or
  1261. * . the server responded it is TLS 1.3 capable but negotiating a lower
  1262. * version of the protocol and thus we are under downgrade attack
  1263. * abort the handshake with an "illegal parameter" alert.
  1264. */
  1265. if (handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret) {
  1266. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  1267. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1268. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1269. }
  1270. ssl->keep_current_message = 1;
  1271. ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
  1272. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
  1273. MBEDTLS_SSL_HS_SERVER_HELLO,
  1274. buf, (size_t) (end - buf)));
  1275. if (mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
  1276. ret = ssl_tls13_reset_key_share(ssl);
  1277. if (ret != 0) {
  1278. return ret;
  1279. }
  1280. }
  1281. return SSL_SERVER_HELLO_TLS1_2;
  1282. }
  1283. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1284. ssl->session_negotiate->endpoint = ssl->conf->endpoint;
  1285. ssl->session_negotiate->tls_version = ssl->tls_version;
  1286. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1287. handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
  1288. ret = ssl_server_hello_is_hrr(ssl, buf, end);
  1289. switch (ret) {
  1290. case SSL_SERVER_HELLO:
  1291. MBEDTLS_SSL_DEBUG_MSG(2, ("received ServerHello message"));
  1292. break;
  1293. case SSL_SERVER_HELLO_HRR:
  1294. MBEDTLS_SSL_DEBUG_MSG(2, ("received HelloRetryRequest message"));
  1295. /* If a client receives a second
  1296. * HelloRetryRequest in the same connection (i.e., where the ClientHello
  1297. * was itself in response to a HelloRetryRequest), it MUST abort the
  1298. * handshake with an "unexpected_message" alert.
  1299. */
  1300. if (handshake->hello_retry_request_count > 0) {
  1301. MBEDTLS_SSL_DEBUG_MSG(1, ("Multiple HRRs received"));
  1302. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
  1303. MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
  1304. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  1305. }
  1306. /*
  1307. * Clients must abort the handshake with an "illegal_parameter"
  1308. * alert if the HelloRetryRequest would not result in any change
  1309. * in the ClientHello.
  1310. * In a PSK only key exchange that what we expect.
  1311. */
  1312. if (!mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
  1313. MBEDTLS_SSL_DEBUG_MSG(1,
  1314. ("Unexpected HRR in pure PSK key exchange."));
  1315. MBEDTLS_SSL_PEND_FATAL_ALERT(
  1316. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  1317. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1318. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1319. }
  1320. handshake->hello_retry_request_count++;
  1321. break;
  1322. }
  1323. cleanup:
  1324. return ret;
  1325. }
  1326. MBEDTLS_CHECK_RETURN_CRITICAL
  1327. static int ssl_tls13_check_server_hello_session_id_echo(mbedtls_ssl_context *ssl,
  1328. const unsigned char **buf,
  1329. const unsigned char *end)
  1330. {
  1331. const unsigned char *p = *buf;
  1332. size_t legacy_session_id_echo_len;
  1333. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
  1334. legacy_session_id_echo_len = *p++;
  1335. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len);
  1336. /* legacy_session_id_echo */
  1337. if (ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
  1338. memcmp(ssl->session_negotiate->id, p, legacy_session_id_echo_len) != 0) {
  1339. MBEDTLS_SSL_DEBUG_BUF(3, "Expected Session ID",
  1340. ssl->session_negotiate->id,
  1341. ssl->session_negotiate->id_len);
  1342. MBEDTLS_SSL_DEBUG_BUF(3, "Received Session ID", p,
  1343. legacy_session_id_echo_len);
  1344. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  1345. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1346. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1347. }
  1348. p += legacy_session_id_echo_len;
  1349. *buf = p;
  1350. MBEDTLS_SSL_DEBUG_BUF(3, "Session ID", ssl->session_negotiate->id,
  1351. ssl->session_negotiate->id_len);
  1352. return 0;
  1353. }
  1354. /* Parse ServerHello message and configure context
  1355. *
  1356. * struct {
  1357. * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
  1358. * Random random;
  1359. * opaque legacy_session_id_echo<0..32>;
  1360. * CipherSuite cipher_suite;
  1361. * uint8 legacy_compression_method = 0;
  1362. * Extension extensions<6..2^16-1>;
  1363. * } ServerHello;
  1364. */
  1365. MBEDTLS_CHECK_RETURN_CRITICAL
  1366. static int ssl_tls13_parse_server_hello(mbedtls_ssl_context *ssl,
  1367. const unsigned char *buf,
  1368. const unsigned char *end,
  1369. int is_hrr)
  1370. {
  1371. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1372. const unsigned char *p = buf;
  1373. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  1374. size_t extensions_len;
  1375. const unsigned char *extensions_end;
  1376. uint16_t cipher_suite;
  1377. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  1378. int fatal_alert = 0;
  1379. uint32_t allowed_extensions_mask;
  1380. int hs_msg_type = is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
  1381. MBEDTLS_SSL_HS_SERVER_HELLO;
  1382. /*
  1383. * Check there is space for minimal fields
  1384. *
  1385. * - legacy_version ( 2 bytes)
  1386. * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
  1387. * - legacy_session_id_echo ( 1 byte ), minimum size
  1388. * - cipher_suite ( 2 bytes)
  1389. * - legacy_compression_method ( 1 byte )
  1390. */
  1391. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6);
  1392. MBEDTLS_SSL_DEBUG_BUF(4, "server hello", p, end - p);
  1393. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", p, 2);
  1394. /* ...
  1395. * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
  1396. * ...
  1397. * with ProtocolVersion defined as:
  1398. * uint16 ProtocolVersion;
  1399. */
  1400. if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
  1401. MBEDTLS_SSL_VERSION_TLS1_2) {
  1402. MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
  1403. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
  1404. MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
  1405. ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
  1406. goto cleanup;
  1407. }
  1408. p += 2;
  1409. /* ...
  1410. * Random random;
  1411. * ...
  1412. * with Random defined as:
  1413. * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
  1414. */
  1415. if (!is_hrr) {
  1416. memcpy(&handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
  1417. MBEDTLS_SERVER_HELLO_RANDOM_LEN);
  1418. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
  1419. p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
  1420. }
  1421. p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
  1422. /* ...
  1423. * opaque legacy_session_id_echo<0..32>;
  1424. * ...
  1425. */
  1426. if (ssl_tls13_check_server_hello_session_id_echo(ssl, &p, end) != 0) {
  1427. fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
  1428. goto cleanup;
  1429. }
  1430. /* ...
  1431. * CipherSuite cipher_suite;
  1432. * ...
  1433. * with CipherSuite defined as:
  1434. * uint8 CipherSuite[2];
  1435. */
  1436. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  1437. cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
  1438. p += 2;
  1439. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
  1440. /*
  1441. * Check whether this ciphersuite is valid and offered.
  1442. */
  1443. if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
  1444. ssl->tls_version,
  1445. ssl->tls_version) != 0) ||
  1446. !mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
  1447. fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
  1448. }
  1449. /*
  1450. * If we received an HRR before and that the proposed selected
  1451. * ciphersuite in this server hello is not the same as the one
  1452. * proposed in the HRR, we abort the handshake and send an
  1453. * "illegal_parameter" alert.
  1454. */
  1455. else if ((!is_hrr) && (handshake->hello_retry_request_count > 0) &&
  1456. (cipher_suite != ssl->session_negotiate->ciphersuite)) {
  1457. fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
  1458. }
  1459. if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
  1460. MBEDTLS_SSL_DEBUG_MSG(1, ("invalid ciphersuite(%04x) parameter",
  1461. cipher_suite));
  1462. goto cleanup;
  1463. }
  1464. /* Configure ciphersuites */
  1465. mbedtls_ssl_optimize_checksum(ssl, ciphersuite_info);
  1466. handshake->ciphersuite_info = ciphersuite_info;
  1467. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: ( %04x ) - %s",
  1468. cipher_suite, ciphersuite_info->name));
  1469. #if defined(MBEDTLS_HAVE_TIME)
  1470. ssl->session_negotiate->start = time(NULL);
  1471. #endif /* MBEDTLS_HAVE_TIME */
  1472. /* ...
  1473. * uint8 legacy_compression_method = 0;
  1474. * ...
  1475. */
  1476. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
  1477. if (p[0] != MBEDTLS_SSL_COMPRESS_NULL) {
  1478. MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
  1479. fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
  1480. goto cleanup;
  1481. }
  1482. p++;
  1483. /* ...
  1484. * Extension extensions<6..2^16-1>;
  1485. * ...
  1486. * struct {
  1487. * ExtensionType extension_type; (2 bytes)
  1488. * opaque extension_data<0..2^16-1>;
  1489. * } Extension;
  1490. */
  1491. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  1492. extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
  1493. p += 2;
  1494. /* Check extensions do not go beyond the buffer of data. */
  1495. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
  1496. extensions_end = p + extensions_len;
  1497. MBEDTLS_SSL_DEBUG_BUF(3, "server hello extensions", p, extensions_len);
  1498. handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
  1499. allowed_extensions_mask = is_hrr ?
  1500. MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_HRR :
  1501. MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_SH;
  1502. while (p < extensions_end) {
  1503. unsigned int extension_type;
  1504. size_t extension_data_len;
  1505. const unsigned char *extension_data_end;
  1506. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
  1507. extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
  1508. extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
  1509. p += 4;
  1510. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
  1511. extension_data_end = p + extension_data_len;
  1512. ret = mbedtls_ssl_tls13_check_received_extension(
  1513. ssl, hs_msg_type, extension_type, allowed_extensions_mask);
  1514. if (ret != 0) {
  1515. return ret;
  1516. }
  1517. switch (extension_type) {
  1518. case MBEDTLS_TLS_EXT_COOKIE:
  1519. ret = ssl_tls13_parse_cookie_ext(ssl,
  1520. p, extension_data_end);
  1521. if (ret != 0) {
  1522. MBEDTLS_SSL_DEBUG_RET(1,
  1523. "ssl_tls13_parse_cookie_ext",
  1524. ret);
  1525. goto cleanup;
  1526. }
  1527. break;
  1528. case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
  1529. ret = ssl_tls13_parse_supported_versions_ext(ssl,
  1530. p,
  1531. extension_data_end);
  1532. if (ret != 0) {
  1533. goto cleanup;
  1534. }
  1535. break;
  1536. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  1537. case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
  1538. MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
  1539. if ((ret = ssl_tls13_parse_server_pre_shared_key_ext(
  1540. ssl, p, extension_data_end)) != 0) {
  1541. MBEDTLS_SSL_DEBUG_RET(
  1542. 1, ("ssl_tls13_parse_server_pre_shared_key_ext"), ret);
  1543. return ret;
  1544. }
  1545. break;
  1546. #endif
  1547. case MBEDTLS_TLS_EXT_KEY_SHARE:
  1548. MBEDTLS_SSL_DEBUG_MSG(3, ("found key_shares extension"));
  1549. if (!mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
  1550. fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
  1551. goto cleanup;
  1552. }
  1553. if (is_hrr) {
  1554. ret = ssl_tls13_parse_hrr_key_share_ext(ssl,
  1555. p, extension_data_end);
  1556. } else {
  1557. ret = ssl_tls13_parse_key_share_ext(ssl,
  1558. p, extension_data_end);
  1559. }
  1560. if (ret != 0) {
  1561. MBEDTLS_SSL_DEBUG_RET(1,
  1562. "ssl_tls13_parse_key_share_ext",
  1563. ret);
  1564. goto cleanup;
  1565. }
  1566. break;
  1567. default:
  1568. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1569. goto cleanup;
  1570. }
  1571. p += extension_data_len;
  1572. }
  1573. MBEDTLS_SSL_PRINT_EXTS(3, hs_msg_type, handshake->received_extensions);
  1574. cleanup:
  1575. if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT) {
  1576. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
  1577. MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
  1578. ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
  1579. } else if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
  1580. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  1581. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1582. ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1583. }
  1584. return ret;
  1585. }
  1586. #if defined(MBEDTLS_DEBUG_C)
  1587. static const char *ssl_tls13_get_kex_mode_str(int mode)
  1588. {
  1589. switch (mode) {
  1590. case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK:
  1591. return "psk";
  1592. case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL:
  1593. return "ephemeral";
  1594. case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL:
  1595. return "psk_ephemeral";
  1596. default:
  1597. return "unknown mode";
  1598. }
  1599. }
  1600. #endif /* MBEDTLS_DEBUG_C */
  1601. MBEDTLS_CHECK_RETURN_CRITICAL
  1602. static int ssl_tls13_postprocess_server_hello(mbedtls_ssl_context *ssl)
  1603. {
  1604. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1605. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  1606. /* Determine the key exchange mode:
  1607. * 1) If both the pre_shared_key and key_share extensions were received
  1608. * then the key exchange mode is PSK with EPHEMERAL.
  1609. * 2) If only the pre_shared_key extension was received then the key
  1610. * exchange mode is PSK-only.
  1611. * 3) If only the key_share extension was received then the key
  1612. * exchange mode is EPHEMERAL-only.
  1613. */
  1614. switch (handshake->received_extensions &
  1615. (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) | MBEDTLS_SSL_EXT_MASK(KEY_SHARE))) {
  1616. /* Only the pre_shared_key extension was received */
  1617. case MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY):
  1618. handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
  1619. break;
  1620. /* Only the key_share extension was received */
  1621. case MBEDTLS_SSL_EXT_MASK(KEY_SHARE):
  1622. handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
  1623. break;
  1624. /* Both the pre_shared_key and key_share extensions were received */
  1625. case (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) | MBEDTLS_SSL_EXT_MASK(KEY_SHARE)):
  1626. handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
  1627. break;
  1628. /* Neither pre_shared_key nor key_share extension was received */
  1629. default:
  1630. MBEDTLS_SSL_DEBUG_MSG(1, ("Unknown key exchange."));
  1631. ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1632. goto cleanup;
  1633. }
  1634. #if defined(MBEDTLS_SSL_EARLY_DATA)
  1635. if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA) &&
  1636. (handshake->selected_identity != 0 ||
  1637. handshake->ciphersuite_info->id !=
  1638. ssl->session_negotiate->ciphersuite)) {
  1639. /* RFC8446 4.2.11
  1640. * If the server supplies an "early_data" extension, the
  1641. * client MUST verify that the server's selected_identity
  1642. * is 0. If any other value is returned, the client MUST
  1643. * abort the handshake with an "illegal_parameter" alert.
  1644. *
  1645. * RFC 8446 4.2.10
  1646. * In order to accept early data, the server MUST have accepted a PSK
  1647. * cipher suite and selected the first key offered in the client's
  1648. * "pre_shared_key" extension. In addition, it MUST verify that the
  1649. * following values are the same as those associated with the
  1650. * selected PSK:
  1651. * - The TLS version number
  1652. * - The selected cipher suite
  1653. * - The selected ALPN [RFC7301] protocol, if any
  1654. *
  1655. * We check here that when early data is involved the server
  1656. * selected the cipher suite associated to the pre-shared key
  1657. * as it must have.
  1658. */
  1659. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  1660. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  1661. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1662. }
  1663. #endif
  1664. if (!mbedtls_ssl_conf_tls13_check_kex_modes(
  1665. ssl, handshake->key_exchange_mode)) {
  1666. ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1667. MBEDTLS_SSL_DEBUG_MSG(2,
  1668. ("Key exchange mode(%s) is not supported.",
  1669. ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
  1670. goto cleanup;
  1671. }
  1672. MBEDTLS_SSL_DEBUG_MSG(3,
  1673. ("Selected key exchange mode: %s",
  1674. ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
  1675. /* Start the TLS 1.3 key scheduling if not already done.
  1676. *
  1677. * If we proposed early data then we have already derived an
  1678. * early secret using the selected PSK and its associated hash.
  1679. * It means that if the negotiated key exchange mode is psk or
  1680. * psk_ephemeral, we have already correctly computed the
  1681. * early secret and thus we do not do it again. In all other
  1682. * cases we compute it here.
  1683. */
  1684. #if defined(MBEDTLS_SSL_EARLY_DATA)
  1685. if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT ||
  1686. handshake->key_exchange_mode ==
  1687. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL)
  1688. #endif
  1689. {
  1690. ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
  1691. if (ret != 0) {
  1692. MBEDTLS_SSL_DEBUG_RET(
  1693. 1, "mbedtls_ssl_tls13_key_schedule_stage_early", ret);
  1694. goto cleanup;
  1695. }
  1696. }
  1697. ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
  1698. if (ret != 0) {
  1699. MBEDTLS_SSL_DEBUG_RET(1,
  1700. "mbedtls_ssl_tls13_compute_handshake_transform",
  1701. ret);
  1702. goto cleanup;
  1703. }
  1704. mbedtls_ssl_set_inbound_transform(ssl, handshake->transform_handshake);
  1705. MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
  1706. ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
  1707. ssl->session_in = ssl->session_negotiate;
  1708. cleanup:
  1709. if (ret != 0) {
  1710. MBEDTLS_SSL_PEND_FATAL_ALERT(
  1711. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  1712. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  1713. }
  1714. return ret;
  1715. }
  1716. MBEDTLS_CHECK_RETURN_CRITICAL
  1717. static int ssl_tls13_postprocess_hrr(mbedtls_ssl_context *ssl)
  1718. {
  1719. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1720. mbedtls_ssl_session_reset_msg_layer(ssl, 0);
  1721. /*
  1722. * We are going to re-generate a shared secret corresponding to the group
  1723. * selected by the server, which is different from the group for which we
  1724. * generated a shared secret in the first client hello.
  1725. * Thus, reset the shared secret.
  1726. */
  1727. ret = ssl_tls13_reset_key_share(ssl);
  1728. if (ret != 0) {
  1729. return ret;
  1730. }
  1731. ssl->session_negotiate->ciphersuite = ssl->handshake->ciphersuite_info->id;
  1732. return 0;
  1733. }
  1734. /*
  1735. * Wait and parse ServerHello handshake message.
  1736. * Handler for MBEDTLS_SSL_SERVER_HELLO
  1737. */
  1738. MBEDTLS_CHECK_RETURN_CRITICAL
  1739. static int ssl_tls13_process_server_hello(mbedtls_ssl_context *ssl)
  1740. {
  1741. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1742. unsigned char *buf = NULL;
  1743. size_t buf_len = 0;
  1744. int is_hrr = 0;
  1745. MBEDTLS_SSL_DEBUG_MSG(2, ("=> %s", __func__));
  1746. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
  1747. MBEDTLS_SSL_HS_SERVER_HELLO,
  1748. &buf, &buf_len));
  1749. ret = ssl_tls13_preprocess_server_hello(ssl, buf, buf + buf_len);
  1750. if (ret < 0) {
  1751. goto cleanup;
  1752. } else {
  1753. is_hrr = (ret == SSL_SERVER_HELLO_HRR);
  1754. }
  1755. if (ret == SSL_SERVER_HELLO_TLS1_2) {
  1756. ret = 0;
  1757. goto cleanup;
  1758. }
  1759. MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_server_hello(ssl, buf,
  1760. buf + buf_len,
  1761. is_hrr));
  1762. if (is_hrr) {
  1763. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_reset_transcript_for_hrr(ssl));
  1764. }
  1765. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
  1766. MBEDTLS_SSL_HS_SERVER_HELLO, buf,
  1767. buf_len));
  1768. if (is_hrr) {
  1769. MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_hrr(ssl));
  1770. #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
  1771. /* If not offering early data, the client sends a dummy CCS record
  1772. * immediately before its second flight. This may either be before
  1773. * its second ClientHello or before its encrypted handshake flight.
  1774. */
  1775. mbedtls_ssl_handshake_set_state(ssl,
  1776. MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO);
  1777. #else
  1778. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
  1779. #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
  1780. } else {
  1781. MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_server_hello(ssl));
  1782. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
  1783. }
  1784. cleanup:
  1785. MBEDTLS_SSL_DEBUG_MSG(2, ("<= %s ( %s )", __func__,
  1786. is_hrr ? "HelloRetryRequest" : "ServerHello"));
  1787. return ret;
  1788. }
  1789. /*
  1790. *
  1791. * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
  1792. *
  1793. * The EncryptedExtensions message contains any extensions which
  1794. * should be protected, i.e., any which are not needed to establish
  1795. * the cryptographic context.
  1796. */
  1797. /* Parse EncryptedExtensions message
  1798. * struct {
  1799. * Extension extensions<0..2^16-1>;
  1800. * } EncryptedExtensions;
  1801. */
  1802. MBEDTLS_CHECK_RETURN_CRITICAL
  1803. static int ssl_tls13_parse_encrypted_extensions(mbedtls_ssl_context *ssl,
  1804. const unsigned char *buf,
  1805. const unsigned char *end)
  1806. {
  1807. int ret = 0;
  1808. size_t extensions_len;
  1809. const unsigned char *p = buf;
  1810. const unsigned char *extensions_end;
  1811. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  1812. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  1813. extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
  1814. p += 2;
  1815. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
  1816. extensions_end = p + extensions_len;
  1817. MBEDTLS_SSL_DEBUG_BUF(3, "encrypted extensions", p, extensions_len);
  1818. handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
  1819. while (p < extensions_end) {
  1820. unsigned int extension_type;
  1821. size_t extension_data_len;
  1822. /*
  1823. * struct {
  1824. * ExtensionType extension_type; (2 bytes)
  1825. * opaque extension_data<0..2^16-1>;
  1826. * } Extension;
  1827. */
  1828. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
  1829. extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
  1830. extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
  1831. p += 4;
  1832. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
  1833. ret = mbedtls_ssl_tls13_check_received_extension(
  1834. ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, extension_type,
  1835. MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_EE);
  1836. if (ret != 0) {
  1837. return ret;
  1838. }
  1839. switch (extension_type) {
  1840. #if defined(MBEDTLS_SSL_ALPN)
  1841. case MBEDTLS_TLS_EXT_ALPN:
  1842. MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
  1843. if ((ret = ssl_tls13_parse_alpn_ext(ssl, p, (size_t) extension_data_len)) != 0) {
  1844. return ret;
  1845. }
  1846. break;
  1847. #endif /* MBEDTLS_SSL_ALPN */
  1848. #if defined(MBEDTLS_SSL_EARLY_DATA)
  1849. case MBEDTLS_TLS_EXT_EARLY_DATA:
  1850. if (extension_data_len != 0) {
  1851. /* The message must be empty. */
  1852. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  1853. MBEDTLS_ERR_SSL_DECODE_ERROR);
  1854. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1855. }
  1856. break;
  1857. #endif /* MBEDTLS_SSL_EARLY_DATA */
  1858. #if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
  1859. case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
  1860. MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
  1861. ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(ssl, p, p + extension_data_len);
  1862. /* TODO: Return unconditionally here until we handle the record size limit correctly.
  1863. * Once handled correctly, only return in case of errors. */
  1864. return ret;
  1865. break;
  1866. #endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
  1867. default:
  1868. MBEDTLS_SSL_PRINT_EXT(
  1869. 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
  1870. extension_type, "( ignored )");
  1871. break;
  1872. }
  1873. p += extension_data_len;
  1874. }
  1875. MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
  1876. handshake->received_extensions);
  1877. /* Check that we consumed all the message. */
  1878. if (p != end) {
  1879. MBEDTLS_SSL_DEBUG_MSG(1, ("EncryptedExtension lengths misaligned"));
  1880. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  1881. MBEDTLS_ERR_SSL_DECODE_ERROR);
  1882. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1883. }
  1884. return ret;
  1885. }
  1886. MBEDTLS_CHECK_RETURN_CRITICAL
  1887. static int ssl_tls13_process_encrypted_extensions(mbedtls_ssl_context *ssl)
  1888. {
  1889. int ret;
  1890. unsigned char *buf;
  1891. size_t buf_len;
  1892. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse encrypted extensions"));
  1893. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
  1894. MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
  1895. &buf, &buf_len));
  1896. /* Process the message contents */
  1897. MBEDTLS_SSL_PROC_CHK(
  1898. ssl_tls13_parse_encrypted_extensions(ssl, buf, buf + buf_len));
  1899. #if defined(MBEDTLS_SSL_EARLY_DATA)
  1900. if (ssl->handshake->received_extensions &
  1901. MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
  1902. ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED;
  1903. }
  1904. #endif
  1905. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
  1906. MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
  1907. buf, buf_len));
  1908. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  1909. if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
  1910. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
  1911. } else {
  1912. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
  1913. }
  1914. #else
  1915. ((void) ssl);
  1916. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
  1917. #endif
  1918. cleanup:
  1919. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse encrypted extensions"));
  1920. return ret;
  1921. }
  1922. /*
  1923. * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
  1924. *
  1925. * RFC 8446 section 4.5
  1926. *
  1927. * struct {} EndOfEarlyData;
  1928. *
  1929. * If the server sent an "early_data" extension in EncryptedExtensions, the
  1930. * client MUST send an EndOfEarlyData message after receiving the server
  1931. * Finished. Otherwise, the client MUST NOT send an EndOfEarlyData message.
  1932. */
  1933. MBEDTLS_CHECK_RETURN_CRITICAL
  1934. static int ssl_tls13_write_end_of_early_data(mbedtls_ssl_context *ssl)
  1935. {
  1936. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1937. unsigned char *buf = NULL;
  1938. size_t buf_len;
  1939. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write EndOfEarlyData"));
  1940. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
  1941. ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
  1942. &buf, &buf_len));
  1943. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_hdr_to_checksum(
  1944. ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA, 0));
  1945. MBEDTLS_SSL_PROC_CHK(
  1946. mbedtls_ssl_finish_handshake_msg(ssl, buf_len, 0));
  1947. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
  1948. cleanup:
  1949. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write EndOfEarlyData"));
  1950. return ret;
  1951. }
  1952. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  1953. /*
  1954. * STATE HANDLING: CertificateRequest
  1955. *
  1956. */
  1957. #define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
  1958. #define SSL_CERTIFICATE_REQUEST_SKIP 1
  1959. /* Coordination:
  1960. * Deals with the ambiguity of not knowing if a CertificateRequest
  1961. * will be sent. Returns a negative code on failure, or
  1962. * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
  1963. * - SSL_CERTIFICATE_REQUEST_SKIP
  1964. * indicating if a Certificate Request is expected or not.
  1965. */
  1966. MBEDTLS_CHECK_RETURN_CRITICAL
  1967. static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
  1968. {
  1969. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1970. if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
  1971. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  1972. return ret;
  1973. }
  1974. ssl->keep_current_message = 1;
  1975. if ((ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) &&
  1976. (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST)) {
  1977. MBEDTLS_SSL_DEBUG_MSG(3, ("got a certificate request"));
  1978. return SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST;
  1979. }
  1980. MBEDTLS_SSL_DEBUG_MSG(3, ("got no certificate request"));
  1981. return SSL_CERTIFICATE_REQUEST_SKIP;
  1982. }
  1983. /*
  1984. * ssl_tls13_parse_certificate_request()
  1985. * Parse certificate request
  1986. * struct {
  1987. * opaque certificate_request_context<0..2^8-1>;
  1988. * Extension extensions<2..2^16-1>;
  1989. * } CertificateRequest;
  1990. */
  1991. MBEDTLS_CHECK_RETURN_CRITICAL
  1992. static int ssl_tls13_parse_certificate_request(mbedtls_ssl_context *ssl,
  1993. const unsigned char *buf,
  1994. const unsigned char *end)
  1995. {
  1996. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1997. const unsigned char *p = buf;
  1998. size_t certificate_request_context_len = 0;
  1999. size_t extensions_len = 0;
  2000. const unsigned char *extensions_end;
  2001. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  2002. /* ...
  2003. * opaque certificate_request_context<0..2^8-1>
  2004. * ...
  2005. */
  2006. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
  2007. certificate_request_context_len = (size_t) p[0];
  2008. p += 1;
  2009. if (certificate_request_context_len > 0) {
  2010. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_request_context_len);
  2011. MBEDTLS_SSL_DEBUG_BUF(3, "Certificate Request Context",
  2012. p, certificate_request_context_len);
  2013. handshake->certificate_request_context =
  2014. mbedtls_calloc(1, certificate_request_context_len);
  2015. if (handshake->certificate_request_context == NULL) {
  2016. MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
  2017. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  2018. }
  2019. memcpy(handshake->certificate_request_context, p,
  2020. certificate_request_context_len);
  2021. p += certificate_request_context_len;
  2022. }
  2023. /* ...
  2024. * Extension extensions<2..2^16-1>;
  2025. * ...
  2026. */
  2027. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  2028. extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
  2029. p += 2;
  2030. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
  2031. extensions_end = p + extensions_len;
  2032. handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
  2033. while (p < extensions_end) {
  2034. unsigned int extension_type;
  2035. size_t extension_data_len;
  2036. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
  2037. extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
  2038. extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
  2039. p += 4;
  2040. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
  2041. ret = mbedtls_ssl_tls13_check_received_extension(
  2042. ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, extension_type,
  2043. MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CR);
  2044. if (ret != 0) {
  2045. return ret;
  2046. }
  2047. switch (extension_type) {
  2048. case MBEDTLS_TLS_EXT_SIG_ALG:
  2049. MBEDTLS_SSL_DEBUG_MSG(3,
  2050. ("found signature algorithms extension"));
  2051. ret = mbedtls_ssl_parse_sig_alg_ext(ssl, p,
  2052. p + extension_data_len);
  2053. if (ret != 0) {
  2054. return ret;
  2055. }
  2056. break;
  2057. default:
  2058. MBEDTLS_SSL_PRINT_EXT(
  2059. 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
  2060. extension_type, "( ignored )");
  2061. break;
  2062. }
  2063. p += extension_data_len;
  2064. }
  2065. MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
  2066. handshake->received_extensions);
  2067. /* Check that we consumed all the message. */
  2068. if (p != end) {
  2069. MBEDTLS_SSL_DEBUG_MSG(1,
  2070. ("CertificateRequest misaligned"));
  2071. goto decode_error;
  2072. }
  2073. /* RFC 8446 section 4.3.2
  2074. *
  2075. * The "signature_algorithms" extension MUST be specified
  2076. */
  2077. if ((handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(SIG_ALG)) == 0) {
  2078. MBEDTLS_SSL_DEBUG_MSG(3,
  2079. ("no signature algorithms extension found"));
  2080. goto decode_error;
  2081. }
  2082. ssl->handshake->client_auth = 1;
  2083. return 0;
  2084. decode_error:
  2085. MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  2086. MBEDTLS_ERR_SSL_DECODE_ERROR);
  2087. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2088. }
  2089. /*
  2090. * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
  2091. */
  2092. MBEDTLS_CHECK_RETURN_CRITICAL
  2093. static int ssl_tls13_process_certificate_request(mbedtls_ssl_context *ssl)
  2094. {
  2095. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2096. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
  2097. MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
  2098. if (ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST) {
  2099. unsigned char *buf;
  2100. size_t buf_len;
  2101. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
  2102. MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
  2103. &buf, &buf_len));
  2104. MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_request(ssl,
  2105. buf, buf + buf_len));
  2106. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
  2107. MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
  2108. buf, buf_len));
  2109. } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
  2110. ret = 0;
  2111. } else {
  2112. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2113. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2114. goto cleanup;
  2115. }
  2116. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
  2117. cleanup:
  2118. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
  2119. return ret;
  2120. }
  2121. /*
  2122. * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
  2123. */
  2124. MBEDTLS_CHECK_RETURN_CRITICAL
  2125. static int ssl_tls13_process_server_certificate(mbedtls_ssl_context *ssl)
  2126. {
  2127. int ret;
  2128. ret = mbedtls_ssl_tls13_process_certificate(ssl);
  2129. if (ret != 0) {
  2130. return ret;
  2131. }
  2132. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
  2133. return 0;
  2134. }
  2135. /*
  2136. * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
  2137. */
  2138. MBEDTLS_CHECK_RETURN_CRITICAL
  2139. static int ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
  2140. {
  2141. int ret;
  2142. ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
  2143. if (ret != 0) {
  2144. return ret;
  2145. }
  2146. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
  2147. return 0;
  2148. }
  2149. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  2150. /*
  2151. * Handler for MBEDTLS_SSL_SERVER_FINISHED
  2152. */
  2153. MBEDTLS_CHECK_RETURN_CRITICAL
  2154. static int ssl_tls13_process_server_finished(mbedtls_ssl_context *ssl)
  2155. {
  2156. int ret;
  2157. ret = mbedtls_ssl_tls13_process_finished_message(ssl);
  2158. if (ret != 0) {
  2159. return ret;
  2160. }
  2161. ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
  2162. if (ret != 0) {
  2163. MBEDTLS_SSL_PEND_FATAL_ALERT(
  2164. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  2165. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
  2166. return ret;
  2167. }
  2168. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2169. if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED) {
  2170. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
  2171. } else if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED) {
  2172. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
  2173. } else
  2174. #endif /* MBEDTLS_SSL_EARLY_DATA */
  2175. {
  2176. #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
  2177. mbedtls_ssl_handshake_set_state(
  2178. ssl, MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED);
  2179. #else
  2180. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
  2181. #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
  2182. }
  2183. return 0;
  2184. }
  2185. /*
  2186. * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
  2187. */
  2188. MBEDTLS_CHECK_RETURN_CRITICAL
  2189. static int ssl_tls13_write_client_certificate(mbedtls_ssl_context *ssl)
  2190. {
  2191. int non_empty_certificate_msg = 0;
  2192. MBEDTLS_SSL_DEBUG_MSG(1,
  2193. ("Switch to handshake traffic keys for outbound traffic"));
  2194. mbedtls_ssl_set_outbound_transform(ssl, ssl->handshake->transform_handshake);
  2195. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  2196. if (ssl->handshake->client_auth) {
  2197. int ret = mbedtls_ssl_tls13_write_certificate(ssl);
  2198. if (ret != 0) {
  2199. return ret;
  2200. }
  2201. if (mbedtls_ssl_own_cert(ssl) != NULL) {
  2202. non_empty_certificate_msg = 1;
  2203. }
  2204. } else {
  2205. MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate"));
  2206. }
  2207. #endif
  2208. if (non_empty_certificate_msg) {
  2209. mbedtls_ssl_handshake_set_state(ssl,
  2210. MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
  2211. } else {
  2212. MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate verify"));
  2213. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
  2214. }
  2215. return 0;
  2216. }
  2217. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  2218. /*
  2219. * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
  2220. */
  2221. MBEDTLS_CHECK_RETURN_CRITICAL
  2222. static int ssl_tls13_write_client_certificate_verify(mbedtls_ssl_context *ssl)
  2223. {
  2224. int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
  2225. if (ret == 0) {
  2226. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
  2227. }
  2228. return ret;
  2229. }
  2230. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  2231. /*
  2232. * Handler for MBEDTLS_SSL_CLIENT_FINISHED
  2233. */
  2234. MBEDTLS_CHECK_RETURN_CRITICAL
  2235. static int ssl_tls13_write_client_finished(mbedtls_ssl_context *ssl)
  2236. {
  2237. int ret;
  2238. ret = mbedtls_ssl_tls13_write_finished_message(ssl);
  2239. if (ret != 0) {
  2240. return ret;
  2241. }
  2242. ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
  2243. if (ret != 0) {
  2244. MBEDTLS_SSL_DEBUG_RET(1,
  2245. "mbedtls_ssl_tls13_compute_resumption_master_secret ", ret);
  2246. return ret;
  2247. }
  2248. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_FLUSH_BUFFERS);
  2249. return 0;
  2250. }
  2251. /*
  2252. * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
  2253. */
  2254. MBEDTLS_CHECK_RETURN_CRITICAL
  2255. static int ssl_tls13_flush_buffers(mbedtls_ssl_context *ssl)
  2256. {
  2257. MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
  2258. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
  2259. return 0;
  2260. }
  2261. /*
  2262. * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
  2263. */
  2264. MBEDTLS_CHECK_RETURN_CRITICAL
  2265. static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
  2266. {
  2267. mbedtls_ssl_tls13_handshake_wrapup(ssl);
  2268. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
  2269. return 0;
  2270. }
  2271. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2272. MBEDTLS_CHECK_RETURN_CRITICAL
  2273. static int ssl_tls13_parse_new_session_ticket_exts(mbedtls_ssl_context *ssl,
  2274. const unsigned char *buf,
  2275. const unsigned char *end)
  2276. {
  2277. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  2278. const unsigned char *p = buf;
  2279. handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
  2280. while (p < end) {
  2281. unsigned int extension_type;
  2282. size_t extension_data_len;
  2283. int ret;
  2284. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
  2285. extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
  2286. extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
  2287. p += 4;
  2288. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extension_data_len);
  2289. ret = mbedtls_ssl_tls13_check_received_extension(
  2290. ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, extension_type,
  2291. MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_NST);
  2292. if (ret != 0) {
  2293. return ret;
  2294. }
  2295. switch (extension_type) {
  2296. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2297. case MBEDTLS_TLS_EXT_EARLY_DATA:
  2298. if (extension_data_len != 4) {
  2299. MBEDTLS_SSL_PEND_FATAL_ALERT(
  2300. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  2301. MBEDTLS_ERR_SSL_DECODE_ERROR);
  2302. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2303. }
  2304. if (ssl->session != NULL) {
  2305. ssl->session->ticket_flags |=
  2306. MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA;
  2307. }
  2308. break;
  2309. #endif /* MBEDTLS_SSL_EARLY_DATA */
  2310. default:
  2311. MBEDTLS_SSL_PRINT_EXT(
  2312. 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
  2313. extension_type, "( ignored )");
  2314. break;
  2315. }
  2316. p += extension_data_len;
  2317. }
  2318. MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
  2319. handshake->received_extensions);
  2320. return 0;
  2321. }
  2322. /*
  2323. * From RFC8446, page 74
  2324. *
  2325. * struct {
  2326. * uint32 ticket_lifetime;
  2327. * uint32 ticket_age_add;
  2328. * opaque ticket_nonce<0..255>;
  2329. * opaque ticket<1..2^16-1>;
  2330. * Extension extensions<0..2^16-2>;
  2331. * } NewSessionTicket;
  2332. *
  2333. */
  2334. MBEDTLS_CHECK_RETURN_CRITICAL
  2335. static int ssl_tls13_parse_new_session_ticket(mbedtls_ssl_context *ssl,
  2336. unsigned char *buf,
  2337. unsigned char *end,
  2338. unsigned char **ticket_nonce,
  2339. size_t *ticket_nonce_len)
  2340. {
  2341. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2342. unsigned char *p = buf;
  2343. mbedtls_ssl_session *session = ssl->session;
  2344. size_t ticket_len;
  2345. unsigned char *ticket;
  2346. size_t extensions_len;
  2347. *ticket_nonce = NULL;
  2348. *ticket_nonce_len = 0;
  2349. /*
  2350. * ticket_lifetime 4 bytes
  2351. * ticket_age_add 4 bytes
  2352. * ticket_nonce_len 1 byte
  2353. */
  2354. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 9);
  2355. session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
  2356. MBEDTLS_SSL_DEBUG_MSG(3,
  2357. ("ticket_lifetime: %u",
  2358. (unsigned int) session->ticket_lifetime));
  2359. session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 4);
  2360. MBEDTLS_SSL_DEBUG_MSG(3,
  2361. ("ticket_age_add: %u",
  2362. (unsigned int) session->ticket_age_add));
  2363. *ticket_nonce_len = p[8];
  2364. p += 9;
  2365. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, *ticket_nonce_len);
  2366. *ticket_nonce = p;
  2367. MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len);
  2368. p += *ticket_nonce_len;
  2369. /* Ticket */
  2370. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  2371. ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
  2372. p += 2;
  2373. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ticket_len);
  2374. MBEDTLS_SSL_DEBUG_BUF(3, "received ticket", p, ticket_len);
  2375. /* Check if we previously received a ticket already. */
  2376. if (session->ticket != NULL || session->ticket_len > 0) {
  2377. mbedtls_free(session->ticket);
  2378. session->ticket = NULL;
  2379. session->ticket_len = 0;
  2380. }
  2381. if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
  2382. MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
  2383. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  2384. }
  2385. memcpy(ticket, p, ticket_len);
  2386. p += ticket_len;
  2387. session->ticket = ticket;
  2388. session->ticket_len = ticket_len;
  2389. /* Clear all flags in ticket_flags */
  2390. mbedtls_ssl_session_clear_ticket_flags(
  2391. session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
  2392. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  2393. extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
  2394. p += 2;
  2395. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
  2396. MBEDTLS_SSL_DEBUG_BUF(3, "ticket extension", p, extensions_len);
  2397. ret = ssl_tls13_parse_new_session_ticket_exts(ssl, p, p + extensions_len);
  2398. if (ret != 0) {
  2399. MBEDTLS_SSL_DEBUG_RET(1,
  2400. "ssl_tls13_parse_new_session_ticket_exts",
  2401. ret);
  2402. return ret;
  2403. }
  2404. /* session has been updated, allow export */
  2405. session->exported = 0;
  2406. return 0;
  2407. }
  2408. MBEDTLS_CHECK_RETURN_CRITICAL
  2409. static int ssl_tls13_postprocess_new_session_ticket(mbedtls_ssl_context *ssl,
  2410. unsigned char *ticket_nonce,
  2411. size_t ticket_nonce_len)
  2412. {
  2413. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2414. mbedtls_ssl_session *session = ssl->session;
  2415. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  2416. psa_algorithm_t psa_hash_alg;
  2417. int hash_length;
  2418. #if defined(MBEDTLS_HAVE_TIME)
  2419. /* Store ticket creation time */
  2420. session->ticket_received = mbedtls_time(NULL);
  2421. #endif
  2422. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
  2423. if (ciphersuite_info == NULL) {
  2424. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2425. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2426. }
  2427. psa_hash_alg = mbedtls_psa_translate_md(ciphersuite_info->mac);
  2428. hash_length = PSA_HASH_LENGTH(psa_hash_alg);
  2429. if (hash_length == -1 ||
  2430. (size_t) hash_length > sizeof(session->resumption_key)) {
  2431. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2432. }
  2433. MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
  2434. session->app_secrets.resumption_master_secret,
  2435. hash_length);
  2436. /* Compute resumption key
  2437. *
  2438. * HKDF-Expand-Label( resumption_master_secret,
  2439. * "resumption", ticket_nonce, Hash.length )
  2440. */
  2441. ret = mbedtls_ssl_tls13_hkdf_expand_label(
  2442. psa_hash_alg,
  2443. session->app_secrets.resumption_master_secret,
  2444. hash_length,
  2445. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
  2446. ticket_nonce,
  2447. ticket_nonce_len,
  2448. session->resumption_key,
  2449. hash_length);
  2450. if (ret != 0) {
  2451. MBEDTLS_SSL_DEBUG_RET(2,
  2452. "Creating the ticket-resumed PSK failed",
  2453. ret);
  2454. return ret;
  2455. }
  2456. session->resumption_key_len = hash_length;
  2457. MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
  2458. session->resumption_key,
  2459. session->resumption_key_len);
  2460. /* Set ticket_flags depends on the selected key exchange modes */
  2461. mbedtls_ssl_session_set_ticket_flags(
  2462. session, ssl->conf->tls13_kex_modes);
  2463. MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
  2464. return 0;
  2465. }
  2466. /*
  2467. * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
  2468. */
  2469. MBEDTLS_CHECK_RETURN_CRITICAL
  2470. static int ssl_tls13_process_new_session_ticket(mbedtls_ssl_context *ssl)
  2471. {
  2472. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2473. unsigned char *buf;
  2474. size_t buf_len;
  2475. unsigned char *ticket_nonce;
  2476. size_t ticket_nonce_len;
  2477. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
  2478. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
  2479. ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
  2480. &buf, &buf_len));
  2481. MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_new_session_ticket(
  2482. ssl, buf, buf + buf_len,
  2483. &ticket_nonce, &ticket_nonce_len));
  2484. MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_new_session_ticket(
  2485. ssl, ticket_nonce, ticket_nonce_len));
  2486. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
  2487. cleanup:
  2488. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
  2489. return ret;
  2490. }
  2491. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  2492. int mbedtls_ssl_tls13_handshake_client_step(mbedtls_ssl_context *ssl)
  2493. {
  2494. int ret = 0;
  2495. switch (ssl->state) {
  2496. case MBEDTLS_SSL_HELLO_REQUEST:
  2497. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
  2498. break;
  2499. case MBEDTLS_SSL_CLIENT_HELLO:
  2500. ret = mbedtls_ssl_write_client_hello(ssl);
  2501. break;
  2502. case MBEDTLS_SSL_SERVER_HELLO:
  2503. ret = ssl_tls13_process_server_hello(ssl);
  2504. break;
  2505. case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
  2506. ret = ssl_tls13_process_encrypted_extensions(ssl);
  2507. break;
  2508. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  2509. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  2510. ret = ssl_tls13_process_certificate_request(ssl);
  2511. break;
  2512. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  2513. ret = ssl_tls13_process_server_certificate(ssl);
  2514. break;
  2515. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  2516. ret = ssl_tls13_process_certificate_verify(ssl);
  2517. break;
  2518. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  2519. case MBEDTLS_SSL_SERVER_FINISHED:
  2520. ret = ssl_tls13_process_server_finished(ssl);
  2521. break;
  2522. case MBEDTLS_SSL_END_OF_EARLY_DATA:
  2523. ret = ssl_tls13_write_end_of_early_data(ssl);
  2524. break;
  2525. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  2526. ret = ssl_tls13_write_client_certificate(ssl);
  2527. break;
  2528. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
  2529. case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
  2530. ret = ssl_tls13_write_client_certificate_verify(ssl);
  2531. break;
  2532. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
  2533. case MBEDTLS_SSL_CLIENT_FINISHED:
  2534. ret = ssl_tls13_write_client_finished(ssl);
  2535. break;
  2536. case MBEDTLS_SSL_FLUSH_BUFFERS:
  2537. ret = ssl_tls13_flush_buffers(ssl);
  2538. break;
  2539. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  2540. ret = ssl_tls13_handshake_wrapup(ssl);
  2541. break;
  2542. /*
  2543. * Injection of dummy-CCS's for middlebox compatibility
  2544. */
  2545. #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
  2546. case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
  2547. ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
  2548. if (ret == 0) {
  2549. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
  2550. }
  2551. break;
  2552. case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
  2553. ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
  2554. if (ret == 0) {
  2555. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
  2556. }
  2557. break;
  2558. case MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO:
  2559. ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
  2560. if (ret == 0) {
  2561. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
  2562. #if defined(MBEDTLS_SSL_EARLY_DATA)
  2563. MBEDTLS_SSL_DEBUG_MSG(
  2564. 1, ("Switch to early data keys for outbound traffic"));
  2565. mbedtls_ssl_set_outbound_transform(
  2566. ssl, ssl->handshake->transform_earlydata);
  2567. #endif
  2568. }
  2569. break;
  2570. #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
  2571. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2572. case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
  2573. ret = ssl_tls13_process_new_session_ticket(ssl);
  2574. if (ret != 0) {
  2575. break;
  2576. }
  2577. ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
  2578. break;
  2579. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  2580. default:
  2581. MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
  2582. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  2583. }
  2584. return ret;
  2585. }
  2586. #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */