ssl_tls12_client.c 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603
  1. /*
  2. * TLS client-side functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
  9. #include "mbedtls/platform.h"
  10. #include "mbedtls/ssl.h"
  11. #include "ssl_client.h"
  12. #include "ssl_misc.h"
  13. #include "debug_internal.h"
  14. #include "mbedtls/error.h"
  15. #include "mbedtls/constant_time.h"
  16. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  17. #include "psa_util_internal.h"
  18. #include "psa/crypto.h"
  19. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  20. /* Define a local translating function to save code size by not using too many
  21. * arguments in each translating place. */
  22. static int local_err_translation(psa_status_t status)
  23. {
  24. return psa_status_to_mbedtls(status, psa_to_ssl_errors,
  25. ARRAY_LENGTH(psa_to_ssl_errors),
  26. psa_generic_status_to_mbedtls);
  27. }
  28. #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
  29. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  30. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  31. #include <string.h>
  32. #include <stdint.h>
  33. #if defined(MBEDTLS_HAVE_TIME)
  34. #include "mbedtls/platform_time.h"
  35. #endif
  36. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  37. #include "mbedtls/platform_util.h"
  38. #endif
  39. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  40. MBEDTLS_CHECK_RETURN_CRITICAL
  41. static int ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
  42. unsigned char *buf,
  43. const unsigned char *end,
  44. size_t *olen)
  45. {
  46. unsigned char *p = buf;
  47. *olen = 0;
  48. /* We're always including a TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
  49. * initial ClientHello, in which case also adding the renegotiation
  50. * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
  51. if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
  52. return 0;
  53. }
  54. MBEDTLS_SSL_DEBUG_MSG(3,
  55. ("client hello, adding renegotiation extension"));
  56. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + ssl->verify_data_len);
  57. /*
  58. * Secure renegotiation
  59. */
  60. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
  61. p += 2;
  62. *p++ = 0x00;
  63. *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len + 1);
  64. *p++ = MBEDTLS_BYTE_0(ssl->verify_data_len);
  65. memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
  66. *olen = 5 + ssl->verify_data_len;
  67. return 0;
  68. }
  69. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  70. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
  71. defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
  72. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  73. MBEDTLS_CHECK_RETURN_CRITICAL
  74. static int ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
  75. unsigned char *buf,
  76. const unsigned char *end,
  77. size_t *olen)
  78. {
  79. unsigned char *p = buf;
  80. (void) ssl; /* ssl used for debugging only */
  81. *olen = 0;
  82. MBEDTLS_SSL_DEBUG_MSG(3,
  83. ("client hello, adding supported_point_formats extension"));
  84. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
  85. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
  86. p += 2;
  87. *p++ = 0x00;
  88. *p++ = 2;
  89. *p++ = 1;
  90. *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
  91. *olen = 6;
  92. return 0;
  93. }
  94. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
  95. MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
  96. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  97. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  98. MBEDTLS_CHECK_RETURN_CRITICAL
  99. static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
  100. unsigned char *buf,
  101. const unsigned char *end,
  102. size_t *olen)
  103. {
  104. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  105. unsigned char *p = buf;
  106. size_t kkpp_len = 0;
  107. *olen = 0;
  108. /* Skip costly extension if we can't use EC J-PAKE anyway */
  109. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  110. if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
  111. return 0;
  112. }
  113. #else
  114. if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
  115. return 0;
  116. }
  117. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  118. MBEDTLS_SSL_DEBUG_MSG(3,
  119. ("client hello, adding ecjpake_kkpp extension"));
  120. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
  121. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
  122. p += 2;
  123. /*
  124. * We may need to send ClientHello multiple times for Hello verification.
  125. * We don't want to compute fresh values every time (both for performance
  126. * and consistency reasons), so cache the extension content.
  127. */
  128. if (ssl->handshake->ecjpake_cache == NULL ||
  129. ssl->handshake->ecjpake_cache_len == 0) {
  130. MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
  131. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  132. ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
  133. p + 2, end - p - 2, &kkpp_len,
  134. MBEDTLS_ECJPAKE_ROUND_ONE);
  135. if (ret != 0) {
  136. psa_destroy_key(ssl->handshake->psa_pake_password);
  137. psa_pake_abort(&ssl->handshake->psa_pake_ctx);
  138. MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
  139. return ret;
  140. }
  141. #else
  142. ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
  143. p + 2, end - p - 2, &kkpp_len,
  144. ssl->conf->f_rng, ssl->conf->p_rng);
  145. if (ret != 0) {
  146. MBEDTLS_SSL_DEBUG_RET(1,
  147. "mbedtls_ecjpake_write_round_one", ret);
  148. return ret;
  149. }
  150. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  151. ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
  152. if (ssl->handshake->ecjpake_cache == NULL) {
  153. MBEDTLS_SSL_DEBUG_MSG(1, ("allocation failed"));
  154. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  155. }
  156. memcpy(ssl->handshake->ecjpake_cache, p + 2, kkpp_len);
  157. ssl->handshake->ecjpake_cache_len = kkpp_len;
  158. } else {
  159. MBEDTLS_SSL_DEBUG_MSG(3, ("re-using cached ecjpake parameters"));
  160. kkpp_len = ssl->handshake->ecjpake_cache_len;
  161. MBEDTLS_SSL_CHK_BUF_PTR(p + 2, end, kkpp_len);
  162. memcpy(p + 2, ssl->handshake->ecjpake_cache, kkpp_len);
  163. }
  164. MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
  165. p += 2;
  166. *olen = kkpp_len + 4;
  167. return 0;
  168. }
  169. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  170. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  171. MBEDTLS_CHECK_RETURN_CRITICAL
  172. static int ssl_write_cid_ext(mbedtls_ssl_context *ssl,
  173. unsigned char *buf,
  174. const unsigned char *end,
  175. size_t *olen)
  176. {
  177. unsigned char *p = buf;
  178. size_t ext_len;
  179. /*
  180. * struct {
  181. * opaque cid<0..2^8-1>;
  182. * } ConnectionId;
  183. */
  184. *olen = 0;
  185. if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
  186. ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
  187. return 0;
  188. }
  189. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding CID extension"));
  190. /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
  191. * which is at most 255, so the increment cannot overflow. */
  192. MBEDTLS_SSL_CHK_BUF_PTR(p, end, (unsigned) (ssl->own_cid_len + 5));
  193. /* Add extension ID + size */
  194. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
  195. p += 2;
  196. ext_len = (size_t) ssl->own_cid_len + 1;
  197. MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
  198. p += 2;
  199. *p++ = (uint8_t) ssl->own_cid_len;
  200. memcpy(p, ssl->own_cid, ssl->own_cid_len);
  201. *olen = ssl->own_cid_len + 5;
  202. return 0;
  203. }
  204. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  205. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  206. MBEDTLS_CHECK_RETURN_CRITICAL
  207. static int ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
  208. unsigned char *buf,
  209. const unsigned char *end,
  210. size_t *olen)
  211. {
  212. unsigned char *p = buf;
  213. *olen = 0;
  214. if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
  215. return 0;
  216. }
  217. MBEDTLS_SSL_DEBUG_MSG(3,
  218. ("client hello, adding max_fragment_length extension"));
  219. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5);
  220. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
  221. p += 2;
  222. *p++ = 0x00;
  223. *p++ = 1;
  224. *p++ = ssl->conf->mfl_code;
  225. *olen = 5;
  226. return 0;
  227. }
  228. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  229. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  230. MBEDTLS_CHECK_RETURN_CRITICAL
  231. static int ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
  232. unsigned char *buf,
  233. const unsigned char *end,
  234. size_t *olen)
  235. {
  236. unsigned char *p = buf;
  237. *olen = 0;
  238. if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
  239. return 0;
  240. }
  241. MBEDTLS_SSL_DEBUG_MSG(3,
  242. ("client hello, adding encrypt_then_mac extension"));
  243. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
  244. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
  245. p += 2;
  246. *p++ = 0x00;
  247. *p++ = 0x00;
  248. *olen = 4;
  249. return 0;
  250. }
  251. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  252. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  253. MBEDTLS_CHECK_RETURN_CRITICAL
  254. static int ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
  255. unsigned char *buf,
  256. const unsigned char *end,
  257. size_t *olen)
  258. {
  259. unsigned char *p = buf;
  260. *olen = 0;
  261. if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
  262. return 0;
  263. }
  264. MBEDTLS_SSL_DEBUG_MSG(3,
  265. ("client hello, adding extended_master_secret extension"));
  266. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
  267. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
  268. p += 2;
  269. *p++ = 0x00;
  270. *p++ = 0x00;
  271. *olen = 4;
  272. return 0;
  273. }
  274. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  275. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  276. MBEDTLS_CHECK_RETURN_CRITICAL
  277. static int ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
  278. unsigned char *buf,
  279. const unsigned char *end,
  280. size_t *olen)
  281. {
  282. unsigned char *p = buf;
  283. size_t tlen = ssl->session_negotiate->ticket_len;
  284. *olen = 0;
  285. if (mbedtls_ssl_conf_get_session_tickets(ssl->conf) ==
  286. MBEDTLS_SSL_SESSION_TICKETS_DISABLED) {
  287. return 0;
  288. }
  289. MBEDTLS_SSL_DEBUG_MSG(3,
  290. ("client hello, adding session ticket extension"));
  291. /* The addition is safe here since the ticket length is 16 bit. */
  292. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + tlen);
  293. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
  294. p += 2;
  295. MBEDTLS_PUT_UINT16_BE(tlen, p, 0);
  296. p += 2;
  297. *olen = 4;
  298. if (ssl->session_negotiate->ticket == NULL || tlen == 0) {
  299. return 0;
  300. }
  301. MBEDTLS_SSL_DEBUG_MSG(3,
  302. ("sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen));
  303. memcpy(p, ssl->session_negotiate->ticket, tlen);
  304. *olen += tlen;
  305. return 0;
  306. }
  307. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  308. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  309. MBEDTLS_CHECK_RETURN_CRITICAL
  310. static int ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
  311. unsigned char *buf,
  312. const unsigned char *end,
  313. size_t *olen)
  314. {
  315. unsigned char *p = buf;
  316. size_t protection_profiles_index = 0, ext_len = 0;
  317. uint16_t mki_len = 0, profile_value = 0;
  318. *olen = 0;
  319. if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
  320. (ssl->conf->dtls_srtp_profile_list == NULL) ||
  321. (ssl->conf->dtls_srtp_profile_list_len == 0)) {
  322. return 0;
  323. }
  324. /* RFC 5764 section 4.1.1
  325. * uint8 SRTPProtectionProfile[2];
  326. *
  327. * struct {
  328. * SRTPProtectionProfiles SRTPProtectionProfiles;
  329. * opaque srtp_mki<0..255>;
  330. * } UseSRTPData;
  331. * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
  332. */
  333. if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
  334. mki_len = ssl->dtls_srtp_info.mki_len;
  335. }
  336. /* Extension length = 2 bytes for profiles length,
  337. * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
  338. * 1 byte for srtp_mki vector length and the mki_len value
  339. */
  340. ext_len = 2 + 2 * (ssl->conf->dtls_srtp_profile_list_len) + 1 + mki_len;
  341. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding use_srtp extension"));
  342. /* Check there is room in the buffer for the extension + 4 bytes
  343. * - the extension tag (2 bytes)
  344. * - the extension length (2 bytes)
  345. */
  346. MBEDTLS_SSL_CHK_BUF_PTR(p, end, ext_len + 4);
  347. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, p, 0);
  348. p += 2;
  349. MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
  350. p += 2;
  351. /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
  352. /* micro-optimization:
  353. * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
  354. * which is lower than 127, so the upper byte of the length is always 0
  355. * For the documentation, the more generic code is left in comments
  356. * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
  357. * >> 8 ) & 0xFF );
  358. */
  359. *p++ = 0;
  360. *p++ = MBEDTLS_BYTE_0(2 * ssl->conf->dtls_srtp_profile_list_len);
  361. for (protection_profiles_index = 0;
  362. protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
  363. protection_profiles_index++) {
  364. profile_value = mbedtls_ssl_check_srtp_profile_value
  365. (ssl->conf->dtls_srtp_profile_list[protection_profiles_index]);
  366. if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
  367. MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_write_use_srtp_ext, add profile: %04x",
  368. profile_value));
  369. MBEDTLS_PUT_UINT16_BE(profile_value, p, 0);
  370. p += 2;
  371. } else {
  372. /*
  373. * Note: we shall never arrive here as protection profiles
  374. * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
  375. */
  376. MBEDTLS_SSL_DEBUG_MSG(3,
  377. ("client hello, "
  378. "illegal DTLS-SRTP protection profile %d",
  379. ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
  380. ));
  381. return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  382. }
  383. }
  384. *p++ = mki_len & 0xFF;
  385. if (mki_len != 0) {
  386. memcpy(p, ssl->dtls_srtp_info.mki_value, mki_len);
  387. /*
  388. * Increment p to point to the current position.
  389. */
  390. p += mki_len;
  391. MBEDTLS_SSL_DEBUG_BUF(3, "sending mki", ssl->dtls_srtp_info.mki_value,
  392. ssl->dtls_srtp_info.mki_len);
  393. }
  394. /*
  395. * total extension length: extension type (2 bytes)
  396. * + extension length (2 bytes)
  397. * + protection profile length (2 bytes)
  398. * + 2 * number of protection profiles
  399. * + srtp_mki vector length(1 byte)
  400. * + mki value
  401. */
  402. *olen = p - buf;
  403. return 0;
  404. }
  405. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  406. int mbedtls_ssl_tls12_write_client_hello_exts(mbedtls_ssl_context *ssl,
  407. unsigned char *buf,
  408. const unsigned char *end,
  409. int uses_ec,
  410. size_t *out_len)
  411. {
  412. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  413. unsigned char *p = buf;
  414. size_t ext_len = 0;
  415. (void) ssl;
  416. (void) end;
  417. (void) uses_ec;
  418. (void) ret;
  419. (void) ext_len;
  420. *out_len = 0;
  421. /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
  422. * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
  423. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  424. if ((ret = ssl_write_renegotiation_ext(ssl, p, end, &ext_len)) != 0) {
  425. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_renegotiation_ext", ret);
  426. return ret;
  427. }
  428. p += ext_len;
  429. #endif
  430. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
  431. defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
  432. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  433. if (uses_ec) {
  434. if ((ret = ssl_write_supported_point_formats_ext(ssl, p, end,
  435. &ext_len)) != 0) {
  436. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_supported_point_formats_ext", ret);
  437. return ret;
  438. }
  439. p += ext_len;
  440. }
  441. #endif
  442. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  443. if ((ret = ssl_write_ecjpake_kkpp_ext(ssl, p, end, &ext_len)) != 0) {
  444. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_ecjpake_kkpp_ext", ret);
  445. return ret;
  446. }
  447. p += ext_len;
  448. #endif
  449. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  450. if ((ret = ssl_write_cid_ext(ssl, p, end, &ext_len)) != 0) {
  451. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_cid_ext", ret);
  452. return ret;
  453. }
  454. p += ext_len;
  455. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  456. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  457. if ((ret = ssl_write_max_fragment_length_ext(ssl, p, end,
  458. &ext_len)) != 0) {
  459. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_max_fragment_length_ext", ret);
  460. return ret;
  461. }
  462. p += ext_len;
  463. #endif
  464. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  465. if ((ret = ssl_write_encrypt_then_mac_ext(ssl, p, end, &ext_len)) != 0) {
  466. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_encrypt_then_mac_ext", ret);
  467. return ret;
  468. }
  469. p += ext_len;
  470. #endif
  471. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  472. if ((ret = ssl_write_extended_ms_ext(ssl, p, end, &ext_len)) != 0) {
  473. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_extended_ms_ext", ret);
  474. return ret;
  475. }
  476. p += ext_len;
  477. #endif
  478. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  479. if ((ret = ssl_write_use_srtp_ext(ssl, p, end, &ext_len)) != 0) {
  480. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_use_srtp_ext", ret);
  481. return ret;
  482. }
  483. p += ext_len;
  484. #endif
  485. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  486. if ((ret = ssl_write_session_ticket_ext(ssl, p, end, &ext_len)) != 0) {
  487. MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_session_ticket_ext", ret);
  488. return ret;
  489. }
  490. p += ext_len;
  491. #endif
  492. *out_len = (size_t) (p - buf);
  493. return 0;
  494. }
  495. MBEDTLS_CHECK_RETURN_CRITICAL
  496. static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
  497. const unsigned char *buf,
  498. size_t len)
  499. {
  500. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  501. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  502. /* Check verify-data in constant-time. The length OTOH is no secret */
  503. if (len != 1 + ssl->verify_data_len * 2 ||
  504. buf[0] != ssl->verify_data_len * 2 ||
  505. mbedtls_ct_memcmp(buf + 1,
  506. ssl->own_verify_data, ssl->verify_data_len) != 0 ||
  507. mbedtls_ct_memcmp(buf + 1 + ssl->verify_data_len,
  508. ssl->peer_verify_data, ssl->verify_data_len) != 0) {
  509. MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
  510. mbedtls_ssl_send_alert_message(
  511. ssl,
  512. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  513. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  514. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  515. }
  516. } else
  517. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  518. {
  519. if (len != 1 || buf[0] != 0x00) {
  520. MBEDTLS_SSL_DEBUG_MSG(1,
  521. ("non-zero length renegotiation info"));
  522. mbedtls_ssl_send_alert_message(
  523. ssl,
  524. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  525. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  526. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  527. }
  528. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  529. }
  530. return 0;
  531. }
  532. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  533. MBEDTLS_CHECK_RETURN_CRITICAL
  534. static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
  535. const unsigned char *buf,
  536. size_t len)
  537. {
  538. /*
  539. * server should use the extension only if we did,
  540. * and if so the server's value should match ours (and len is always 1)
  541. */
  542. if (ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
  543. len != 1 ||
  544. buf[0] != ssl->conf->mfl_code) {
  545. MBEDTLS_SSL_DEBUG_MSG(1,
  546. ("non-matching max fragment length extension"));
  547. mbedtls_ssl_send_alert_message(
  548. ssl,
  549. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  550. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  551. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  552. }
  553. return 0;
  554. }
  555. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  556. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  557. MBEDTLS_CHECK_RETURN_CRITICAL
  558. static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
  559. const unsigned char *buf,
  560. size_t len)
  561. {
  562. size_t peer_cid_len;
  563. if ( /* CID extension only makes sense in DTLS */
  564. ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
  565. /* The server must only send the CID extension if we have offered it. */
  566. ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
  567. MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension unexpected"));
  568. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  569. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
  570. return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
  571. }
  572. if (len == 0) {
  573. MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
  574. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  575. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  576. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  577. }
  578. peer_cid_len = *buf++;
  579. len--;
  580. if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
  581. MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
  582. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  583. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  584. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  585. }
  586. if (len != peer_cid_len) {
  587. MBEDTLS_SSL_DEBUG_MSG(1, ("CID extension invalid"));
  588. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  589. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  590. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  591. }
  592. ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
  593. ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
  594. memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
  595. MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
  596. MBEDTLS_SSL_DEBUG_BUF(3, "Server CID", buf, peer_cid_len);
  597. return 0;
  598. }
  599. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  600. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  601. MBEDTLS_CHECK_RETURN_CRITICAL
  602. static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
  603. const unsigned char *buf,
  604. size_t len)
  605. {
  606. if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
  607. len != 0) {
  608. MBEDTLS_SSL_DEBUG_MSG(1,
  609. ("non-matching encrypt-then-MAC extension"));
  610. mbedtls_ssl_send_alert_message(
  611. ssl,
  612. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  613. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
  614. return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
  615. }
  616. ((void) buf);
  617. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
  618. return 0;
  619. }
  620. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  621. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  622. MBEDTLS_CHECK_RETURN_CRITICAL
  623. static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
  624. const unsigned char *buf,
  625. size_t len)
  626. {
  627. if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
  628. len != 0) {
  629. MBEDTLS_SSL_DEBUG_MSG(1,
  630. ("non-matching extended master secret extension"));
  631. mbedtls_ssl_send_alert_message(
  632. ssl,
  633. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  634. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
  635. return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
  636. }
  637. ((void) buf);
  638. ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  639. return 0;
  640. }
  641. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  642. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  643. MBEDTLS_CHECK_RETURN_CRITICAL
  644. static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
  645. const unsigned char *buf,
  646. size_t len)
  647. {
  648. if ((mbedtls_ssl_conf_get_session_tickets(ssl->conf) ==
  649. MBEDTLS_SSL_SESSION_TICKETS_DISABLED) ||
  650. len != 0) {
  651. MBEDTLS_SSL_DEBUG_MSG(1,
  652. ("non-matching session ticket extension"));
  653. mbedtls_ssl_send_alert_message(
  654. ssl,
  655. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  656. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
  657. return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
  658. }
  659. ((void) buf);
  660. ssl->handshake->new_session_ticket = 1;
  661. return 0;
  662. }
  663. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  664. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
  665. defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
  666. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  667. MBEDTLS_CHECK_RETURN_CRITICAL
  668. static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
  669. const unsigned char *buf,
  670. size_t len)
  671. {
  672. size_t list_size;
  673. const unsigned char *p;
  674. if (len == 0 || (size_t) (buf[0] + 1) != len) {
  675. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  676. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  677. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  678. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  679. }
  680. list_size = buf[0];
  681. p = buf + 1;
  682. while (list_size > 0) {
  683. if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
  684. p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
  685. #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
  686. defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
  687. ssl->handshake->ecdh_ctx.point_format = p[0];
  688. #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */
  689. #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
  690. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  691. mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
  692. p[0]);
  693. #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  694. MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
  695. return 0;
  696. }
  697. list_size--;
  698. p++;
  699. }
  700. MBEDTLS_SSL_DEBUG_MSG(1, ("no point format in common"));
  701. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  702. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  703. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  704. }
  705. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
  706. MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
  707. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  708. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  709. MBEDTLS_CHECK_RETURN_CRITICAL
  710. static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
  711. const unsigned char *buf,
  712. size_t len)
  713. {
  714. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  715. if (ssl->handshake->ciphersuite_info->key_exchange !=
  716. MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
  717. MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
  718. return 0;
  719. }
  720. /* If we got here, we no longer need our cached extension */
  721. mbedtls_free(ssl->handshake->ecjpake_cache);
  722. ssl->handshake->ecjpake_cache = NULL;
  723. ssl->handshake->ecjpake_cache_len = 0;
  724. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  725. if ((ret = mbedtls_psa_ecjpake_read_round(
  726. &ssl->handshake->psa_pake_ctx, buf, len,
  727. MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
  728. psa_destroy_key(ssl->handshake->psa_pake_password);
  729. psa_pake_abort(&ssl->handshake->psa_pake_ctx);
  730. MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
  731. mbedtls_ssl_send_alert_message(
  732. ssl,
  733. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  734. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  735. return ret;
  736. }
  737. return 0;
  738. #else
  739. if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
  740. buf, len)) != 0) {
  741. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
  742. mbedtls_ssl_send_alert_message(
  743. ssl,
  744. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  745. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  746. return ret;
  747. }
  748. return 0;
  749. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  750. }
  751. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  752. #if defined(MBEDTLS_SSL_ALPN)
  753. MBEDTLS_CHECK_RETURN_CRITICAL
  754. static int ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
  755. const unsigned char *buf, size_t len)
  756. {
  757. size_t list_len, name_len;
  758. const char **p;
  759. /* If we didn't send it, the server shouldn't send it */
  760. if (ssl->conf->alpn_list == NULL) {
  761. MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching ALPN extension"));
  762. mbedtls_ssl_send_alert_message(
  763. ssl,
  764. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  765. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT);
  766. return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
  767. }
  768. /*
  769. * opaque ProtocolName<1..2^8-1>;
  770. *
  771. * struct {
  772. * ProtocolName protocol_name_list<2..2^16-1>
  773. * } ProtocolNameList;
  774. *
  775. * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
  776. */
  777. /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
  778. if (len < 4) {
  779. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  780. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  781. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  782. }
  783. list_len = MBEDTLS_GET_UINT16_BE(buf, 0);
  784. if (list_len != len - 2) {
  785. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  786. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  787. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  788. }
  789. name_len = buf[2];
  790. if (name_len != list_len - 1) {
  791. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  792. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  793. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  794. }
  795. /* Check that the server chosen protocol was in our list and save it */
  796. for (p = ssl->conf->alpn_list; *p != NULL; p++) {
  797. if (name_len == strlen(*p) &&
  798. memcmp(buf + 3, *p, name_len) == 0) {
  799. ssl->alpn_chosen = *p;
  800. return 0;
  801. }
  802. }
  803. MBEDTLS_SSL_DEBUG_MSG(1, ("ALPN extension: no matching protocol"));
  804. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  805. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  806. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  807. }
  808. #endif /* MBEDTLS_SSL_ALPN */
  809. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  810. MBEDTLS_CHECK_RETURN_CRITICAL
  811. static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
  812. const unsigned char *buf,
  813. size_t len)
  814. {
  815. mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
  816. size_t i, mki_len = 0;
  817. uint16_t server_protection_profile_value = 0;
  818. /* If use_srtp is not configured, just ignore the extension */
  819. if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
  820. (ssl->conf->dtls_srtp_profile_list == NULL) ||
  821. (ssl->conf->dtls_srtp_profile_list_len == 0)) {
  822. return 0;
  823. }
  824. /* RFC 5764 section 4.1.1
  825. * uint8 SRTPProtectionProfile[2];
  826. *
  827. * struct {
  828. * SRTPProtectionProfiles SRTPProtectionProfiles;
  829. * opaque srtp_mki<0..255>;
  830. * } UseSRTPData;
  831. * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
  832. *
  833. */
  834. if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
  835. mki_len = ssl->dtls_srtp_info.mki_len;
  836. }
  837. /*
  838. * Length is 5 + optional mki_value : one protection profile length (2 bytes)
  839. * + protection profile (2 bytes)
  840. * + mki_len(1 byte)
  841. * and optional srtp_mki
  842. */
  843. if ((len < 5) || (len != (buf[4] + 5u))) {
  844. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  845. }
  846. /*
  847. * get the server protection profile
  848. */
  849. /*
  850. * protection profile length must be 0x0002 as we must have only
  851. * one protection profile in server Hello
  852. */
  853. if ((buf[0] != 0) || (buf[1] != 2)) {
  854. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  855. }
  856. server_protection_profile_value = (buf[2] << 8) | buf[3];
  857. server_protection = mbedtls_ssl_check_srtp_profile_value(
  858. server_protection_profile_value);
  859. if (server_protection != MBEDTLS_TLS_SRTP_UNSET) {
  860. MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
  861. mbedtls_ssl_get_srtp_profile_as_string(
  862. server_protection)));
  863. }
  864. ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
  865. /*
  866. * Check we have the server profile in our list
  867. */
  868. for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
  869. if (server_protection == ssl->conf->dtls_srtp_profile_list[i]) {
  870. ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
  871. MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
  872. mbedtls_ssl_get_srtp_profile_as_string(
  873. server_protection)));
  874. break;
  875. }
  876. }
  877. /* If no match was found : server problem, it shall never answer with incompatible profile */
  878. if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
  879. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  880. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  881. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  882. }
  883. /* If server does not use mki in its reply, make sure the client won't keep
  884. * one as negotiated */
  885. if (len == 5) {
  886. ssl->dtls_srtp_info.mki_len = 0;
  887. }
  888. /*
  889. * RFC5764:
  890. * If the client detects a nonzero-length MKI in the server's response
  891. * that is different than the one the client offered, then the client
  892. * MUST abort the handshake and SHOULD send an invalid_parameter alert.
  893. */
  894. if (len > 5 && (buf[4] != mki_len ||
  895. (memcmp(ssl->dtls_srtp_info.mki_value, &buf[5], mki_len)))) {
  896. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  897. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  898. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  899. }
  900. #if defined(MBEDTLS_DEBUG_C)
  901. if (len > 5) {
  902. MBEDTLS_SSL_DEBUG_BUF(3, "received mki", ssl->dtls_srtp_info.mki_value,
  903. ssl->dtls_srtp_info.mki_len);
  904. }
  905. #endif
  906. return 0;
  907. }
  908. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  909. /*
  910. * Parse HelloVerifyRequest. Only called after verifying the HS type.
  911. */
  912. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  913. MBEDTLS_CHECK_RETURN_CRITICAL
  914. static int ssl_parse_hello_verify_request(mbedtls_ssl_context *ssl)
  915. {
  916. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  917. const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
  918. uint16_t dtls_legacy_version;
  919. #if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
  920. uint8_t cookie_len;
  921. #else
  922. uint16_t cookie_len;
  923. #endif
  924. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse hello verify request"));
  925. /* Check that there is enough room for:
  926. * - 2 bytes of version
  927. * - 1 byte of cookie_len
  928. */
  929. if (mbedtls_ssl_hs_hdr_len(ssl) + 3 > ssl->in_msglen) {
  930. MBEDTLS_SSL_DEBUG_MSG(1,
  931. ("incoming HelloVerifyRequest message is too short"));
  932. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  933. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  934. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  935. }
  936. /*
  937. * struct {
  938. * ProtocolVersion server_version;
  939. * opaque cookie<0..2^8-1>;
  940. * } HelloVerifyRequest;
  941. */
  942. MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
  943. dtls_legacy_version = MBEDTLS_GET_UINT16_BE(p, 0);
  944. p += 2;
  945. /*
  946. * Since the RFC is not clear on this point, accept DTLS 1.0 (0xfeff)
  947. * The DTLS 1.3 (current draft) renames ProtocolVersion server_version to
  948. * legacy_version and locks the value of legacy_version to 0xfefd (DTLS 1.2)
  949. */
  950. if (dtls_legacy_version != 0xfefd && dtls_legacy_version != 0xfeff) {
  951. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server version"));
  952. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  953. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
  954. return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
  955. }
  956. cookie_len = *p++;
  957. if ((ssl->in_msg + ssl->in_msglen) - p < cookie_len) {
  958. MBEDTLS_SSL_DEBUG_MSG(1,
  959. ("cookie length does not match incoming message size"));
  960. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  961. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  962. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  963. }
  964. MBEDTLS_SSL_DEBUG_BUF(3, "cookie", p, cookie_len);
  965. mbedtls_free(ssl->handshake->cookie);
  966. ssl->handshake->cookie = mbedtls_calloc(1, cookie_len);
  967. if (ssl->handshake->cookie == NULL) {
  968. MBEDTLS_SSL_DEBUG_MSG(1, ("alloc failed (%d bytes)", cookie_len));
  969. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  970. }
  971. memcpy(ssl->handshake->cookie, p, cookie_len);
  972. ssl->handshake->cookie_len = cookie_len;
  973. /* Start over at ClientHello */
  974. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  975. ret = mbedtls_ssl_reset_checksum(ssl);
  976. if (0 != ret) {
  977. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_reset_checksum"), ret);
  978. return ret;
  979. }
  980. mbedtls_ssl_recv_flight_completed(ssl);
  981. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse hello verify request"));
  982. return 0;
  983. }
  984. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  985. MBEDTLS_CHECK_RETURN_CRITICAL
  986. static int ssl_parse_server_hello(mbedtls_ssl_context *ssl)
  987. {
  988. int ret, i;
  989. size_t n;
  990. size_t ext_len;
  991. unsigned char *buf, *ext;
  992. unsigned char comp;
  993. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  994. int renegotiation_info_seen = 0;
  995. #endif
  996. int handshake_failure = 0;
  997. const mbedtls_ssl_ciphersuite_t *suite_info;
  998. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello"));
  999. if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
  1000. /* No alert on a read error. */
  1001. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  1002. return ret;
  1003. }
  1004. buf = ssl->in_msg;
  1005. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
  1006. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1007. if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
  1008. ssl->renego_records_seen++;
  1009. if (ssl->conf->renego_max_records >= 0 &&
  1010. ssl->renego_records_seen > ssl->conf->renego_max_records) {
  1011. MBEDTLS_SSL_DEBUG_MSG(1,
  1012. ("renegotiation requested, but not honored by server"));
  1013. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  1014. }
  1015. MBEDTLS_SSL_DEBUG_MSG(1,
  1016. ("non-handshake message during renegotiation"));
  1017. ssl->keep_current_message = 1;
  1018. return MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO;
  1019. }
  1020. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1021. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1022. mbedtls_ssl_send_alert_message(
  1023. ssl,
  1024. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1025. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
  1026. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  1027. }
  1028. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1029. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1030. if (buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
  1031. MBEDTLS_SSL_DEBUG_MSG(2, ("received hello verify request"));
  1032. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
  1033. return ssl_parse_hello_verify_request(ssl);
  1034. } else {
  1035. /* We made it through the verification process */
  1036. mbedtls_free(ssl->handshake->cookie);
  1037. ssl->handshake->cookie = NULL;
  1038. ssl->handshake->cookie_len = 0;
  1039. }
  1040. }
  1041. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1042. if (ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len(ssl) ||
  1043. buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO) {
  1044. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1045. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1046. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1047. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1048. }
  1049. /*
  1050. * 0 . 1 server_version
  1051. * 2 . 33 random (maybe including 4 bytes of Unix time)
  1052. * 34 . 34 session_id length = n
  1053. * 35 . 34+n session_id
  1054. * 35+n . 36+n cipher_suite
  1055. * 37+n . 37+n compression_method
  1056. *
  1057. * 38+n . 39+n extensions length (optional)
  1058. * 40+n . .. extensions
  1059. */
  1060. buf += mbedtls_ssl_hs_hdr_len(ssl);
  1061. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", buf, 2);
  1062. ssl->tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(buf,
  1063. ssl->conf->transport);
  1064. ssl->session_negotiate->tls_version = ssl->tls_version;
  1065. ssl->session_negotiate->endpoint = ssl->conf->endpoint;
  1066. if (ssl->tls_version < ssl->conf->min_tls_version ||
  1067. ssl->tls_version > ssl->conf->max_tls_version) {
  1068. MBEDTLS_SSL_DEBUG_MSG(1,
  1069. (
  1070. "server version out of bounds - min: [0x%x], server: [0x%x], max: [0x%x]",
  1071. (unsigned) ssl->conf->min_tls_version,
  1072. (unsigned) ssl->tls_version,
  1073. (unsigned) ssl->conf->max_tls_version));
  1074. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1075. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
  1076. return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
  1077. }
  1078. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %lu",
  1079. ((unsigned long) buf[2] << 24) |
  1080. ((unsigned long) buf[3] << 16) |
  1081. ((unsigned long) buf[4] << 8) |
  1082. ((unsigned long) buf[5])));
  1083. memcpy(ssl->handshake->randbytes + 32, buf + 2, 32);
  1084. n = buf[34];
  1085. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 2, 32);
  1086. if (n > 32) {
  1087. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1088. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1089. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1090. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1091. }
  1092. if (ssl->in_hslen > mbedtls_ssl_hs_hdr_len(ssl) + 39 + n) {
  1093. ext_len = MBEDTLS_GET_UINT16_BE(buf, 38 + n);
  1094. if ((ext_len > 0 && ext_len < 4) ||
  1095. ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 40 + n + ext_len) {
  1096. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1097. mbedtls_ssl_send_alert_message(
  1098. ssl,
  1099. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1100. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1101. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1102. }
  1103. } else if (ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl) + 38 + n) {
  1104. ext_len = 0;
  1105. } else {
  1106. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1107. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1108. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1109. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1110. }
  1111. /* ciphersuite (used later) */
  1112. i = (int) MBEDTLS_GET_UINT16_BE(buf, n + 35);
  1113. /*
  1114. * Read and check compression
  1115. */
  1116. comp = buf[37 + n];
  1117. if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
  1118. MBEDTLS_SSL_DEBUG_MSG(1,
  1119. ("server hello, bad compression: %d", comp));
  1120. mbedtls_ssl_send_alert_message(
  1121. ssl,
  1122. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1123. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1124. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1125. }
  1126. /*
  1127. * Initialize update checksum functions
  1128. */
  1129. ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(i);
  1130. if (ssl->handshake->ciphersuite_info == NULL) {
  1131. MBEDTLS_SSL_DEBUG_MSG(1,
  1132. ("ciphersuite info for %04x not found", (unsigned int) i));
  1133. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1134. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
  1135. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  1136. }
  1137. mbedtls_ssl_optimize_checksum(ssl, ssl->handshake->ciphersuite_info);
  1138. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
  1139. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 35, n);
  1140. /*
  1141. * Check if the session can be resumed
  1142. */
  1143. if (ssl->handshake->resume == 0 || n == 0 ||
  1144. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1145. ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
  1146. #endif
  1147. ssl->session_negotiate->ciphersuite != i ||
  1148. ssl->session_negotiate->id_len != n ||
  1149. memcmp(ssl->session_negotiate->id, buf + 35, n) != 0) {
  1150. ssl->state++;
  1151. ssl->handshake->resume = 0;
  1152. #if defined(MBEDTLS_HAVE_TIME)
  1153. ssl->session_negotiate->start = mbedtls_time(NULL);
  1154. #endif
  1155. ssl->session_negotiate->ciphersuite = i;
  1156. ssl->session_negotiate->id_len = n;
  1157. memcpy(ssl->session_negotiate->id, buf + 35, n);
  1158. } else {
  1159. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  1160. }
  1161. MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
  1162. ssl->handshake->resume ? "a" : "no"));
  1163. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %04x", (unsigned) i));
  1164. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: %d",
  1165. buf[37 + n]));
  1166. /*
  1167. * Perform cipher suite validation in same way as in ssl_write_client_hello.
  1168. */
  1169. i = 0;
  1170. while (1) {
  1171. if (ssl->conf->ciphersuite_list[i] == 0) {
  1172. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1173. mbedtls_ssl_send_alert_message(
  1174. ssl,
  1175. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1176. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1177. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1178. }
  1179. if (ssl->conf->ciphersuite_list[i++] ==
  1180. ssl->session_negotiate->ciphersuite) {
  1181. break;
  1182. }
  1183. }
  1184. suite_info = mbedtls_ssl_ciphersuite_from_id(
  1185. ssl->session_negotiate->ciphersuite);
  1186. if (mbedtls_ssl_validate_ciphersuite(ssl, suite_info, ssl->tls_version,
  1187. ssl->tls_version) != 0) {
  1188. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1189. mbedtls_ssl_send_alert_message(
  1190. ssl,
  1191. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1192. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1193. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1194. }
  1195. MBEDTLS_SSL_DEBUG_MSG(3,
  1196. ("server hello, chosen ciphersuite: %s", suite_info->name));
  1197. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  1198. if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
  1199. ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
  1200. ssl->handshake->ecrs_enabled = 1;
  1201. }
  1202. #endif
  1203. if (comp != MBEDTLS_SSL_COMPRESS_NULL) {
  1204. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1205. mbedtls_ssl_send_alert_message(
  1206. ssl,
  1207. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1208. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1209. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1210. }
  1211. ext = buf + 40 + n;
  1212. MBEDTLS_SSL_DEBUG_MSG(2,
  1213. ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
  1214. ext_len));
  1215. while (ext_len) {
  1216. unsigned int ext_id = MBEDTLS_GET_UINT16_BE(ext, 0);
  1217. unsigned int ext_size = MBEDTLS_GET_UINT16_BE(ext, 2);
  1218. if (ext_size + 4 > ext_len) {
  1219. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1220. mbedtls_ssl_send_alert_message(
  1221. ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1222. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1223. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1224. }
  1225. switch (ext_id) {
  1226. case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
  1227. MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
  1228. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1229. renegotiation_info_seen = 1;
  1230. #endif
  1231. if ((ret = ssl_parse_renegotiation_info(ssl, ext + 4,
  1232. ext_size)) != 0) {
  1233. return ret;
  1234. }
  1235. break;
  1236. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1237. case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
  1238. MBEDTLS_SSL_DEBUG_MSG(3,
  1239. ("found max_fragment_length extension"));
  1240. if ((ret = ssl_parse_max_fragment_length_ext(ssl,
  1241. ext + 4, ext_size)) != 0) {
  1242. return ret;
  1243. }
  1244. break;
  1245. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  1246. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1247. case MBEDTLS_TLS_EXT_CID:
  1248. MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
  1249. if ((ret = ssl_parse_cid_ext(ssl,
  1250. ext + 4,
  1251. ext_size)) != 0) {
  1252. return ret;
  1253. }
  1254. break;
  1255. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1256. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1257. case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
  1258. MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt_then_mac extension"));
  1259. if ((ret = ssl_parse_encrypt_then_mac_ext(ssl,
  1260. ext + 4, ext_size)) != 0) {
  1261. return ret;
  1262. }
  1263. break;
  1264. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  1265. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1266. case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
  1267. MBEDTLS_SSL_DEBUG_MSG(3,
  1268. ("found extended_master_secret extension"));
  1269. if ((ret = ssl_parse_extended_ms_ext(ssl,
  1270. ext + 4, ext_size)) != 0) {
  1271. return ret;
  1272. }
  1273. break;
  1274. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  1275. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1276. case MBEDTLS_TLS_EXT_SESSION_TICKET:
  1277. MBEDTLS_SSL_DEBUG_MSG(3, ("found session_ticket extension"));
  1278. if ((ret = ssl_parse_session_ticket_ext(ssl,
  1279. ext + 4, ext_size)) != 0) {
  1280. return ret;
  1281. }
  1282. break;
  1283. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1284. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
  1285. defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
  1286. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1287. case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
  1288. MBEDTLS_SSL_DEBUG_MSG(3,
  1289. ("found supported_point_formats extension"));
  1290. if ((ret = ssl_parse_supported_point_formats_ext(ssl,
  1291. ext + 4, ext_size)) != 0) {
  1292. return ret;
  1293. }
  1294. break;
  1295. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED ||
  1296. MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED ||
  1297. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1298. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1299. case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
  1300. MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake_kkpp extension"));
  1301. if ((ret = ssl_parse_ecjpake_kkpp(ssl,
  1302. ext + 4, ext_size)) != 0) {
  1303. return ret;
  1304. }
  1305. break;
  1306. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1307. #if defined(MBEDTLS_SSL_ALPN)
  1308. case MBEDTLS_TLS_EXT_ALPN:
  1309. MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
  1310. if ((ret = ssl_parse_alpn_ext(ssl, ext + 4, ext_size)) != 0) {
  1311. return ret;
  1312. }
  1313. break;
  1314. #endif /* MBEDTLS_SSL_ALPN */
  1315. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  1316. case MBEDTLS_TLS_EXT_USE_SRTP:
  1317. MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
  1318. if ((ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size)) != 0) {
  1319. return ret;
  1320. }
  1321. break;
  1322. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  1323. default:
  1324. MBEDTLS_SSL_DEBUG_MSG(3,
  1325. ("unknown extension found: %u (ignoring)", ext_id));
  1326. }
  1327. ext_len -= 4 + ext_size;
  1328. ext += 4 + ext_size;
  1329. if (ext_len > 0 && ext_len < 4) {
  1330. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello message"));
  1331. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1332. }
  1333. }
  1334. /*
  1335. * mbedtls_ssl_derive_keys() has to be called after the parsing of the
  1336. * extensions. It sets the transform data for the resumed session which in
  1337. * case of DTLS includes the server CID extracted from the CID extension.
  1338. */
  1339. if (ssl->handshake->resume) {
  1340. if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
  1341. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
  1342. mbedtls_ssl_send_alert_message(
  1343. ssl,
  1344. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1345. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
  1346. return ret;
  1347. }
  1348. }
  1349. /*
  1350. * Renegotiation security checks
  1351. */
  1352. if (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1353. ssl->conf->allow_legacy_renegotiation ==
  1354. MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
  1355. MBEDTLS_SSL_DEBUG_MSG(1,
  1356. ("legacy renegotiation, breaking off handshake"));
  1357. handshake_failure = 1;
  1358. }
  1359. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1360. else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1361. ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  1362. renegotiation_info_seen == 0) {
  1363. MBEDTLS_SSL_DEBUG_MSG(1,
  1364. ("renegotiation_info extension missing (secure)"));
  1365. handshake_failure = 1;
  1366. } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1367. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1368. ssl->conf->allow_legacy_renegotiation ==
  1369. MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
  1370. MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
  1371. handshake_failure = 1;
  1372. } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1373. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1374. renegotiation_info_seen == 1) {
  1375. MBEDTLS_SSL_DEBUG_MSG(1,
  1376. ("renegotiation_info extension present (legacy)"));
  1377. handshake_failure = 1;
  1378. }
  1379. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1380. if (handshake_failure == 1) {
  1381. mbedtls_ssl_send_alert_message(
  1382. ssl,
  1383. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1384. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1385. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1386. }
  1387. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello"));
  1388. return 0;
  1389. }
  1390. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1391. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  1392. MBEDTLS_CHECK_RETURN_CRITICAL
  1393. static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
  1394. unsigned char **p,
  1395. unsigned char *end)
  1396. {
  1397. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1398. size_t dhm_actual_bitlen;
  1399. /*
  1400. * Ephemeral DH parameters:
  1401. *
  1402. * struct {
  1403. * opaque dh_p<1..2^16-1>;
  1404. * opaque dh_g<1..2^16-1>;
  1405. * opaque dh_Ys<1..2^16-1>;
  1406. * } ServerDHParams;
  1407. */
  1408. if ((ret = mbedtls_dhm_read_params(&ssl->handshake->dhm_ctx,
  1409. p, end)) != 0) {
  1410. MBEDTLS_SSL_DEBUG_RET(2, ("mbedtls_dhm_read_params"), ret);
  1411. return ret;
  1412. }
  1413. dhm_actual_bitlen = mbedtls_dhm_get_bitlen(&ssl->handshake->dhm_ctx);
  1414. if (dhm_actual_bitlen < ssl->conf->dhm_min_bitlen) {
  1415. MBEDTLS_SSL_DEBUG_MSG(1, ("DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
  1416. dhm_actual_bitlen,
  1417. ssl->conf->dhm_min_bitlen));
  1418. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1419. }
  1420. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
  1421. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
  1422. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
  1423. return ret;
  1424. }
  1425. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1426. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  1427. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1428. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1429. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  1430. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1431. MBEDTLS_CHECK_RETURN_CRITICAL
  1432. static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
  1433. unsigned char **p,
  1434. unsigned char *end)
  1435. {
  1436. uint16_t tls_id;
  1437. size_t ecpoint_len;
  1438. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  1439. psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
  1440. size_t ec_bits = 0;
  1441. /*
  1442. * struct {
  1443. * ECParameters curve_params;
  1444. * ECPoint public;
  1445. * } ServerECDHParams;
  1446. *
  1447. * 1 curve_type (must be "named_curve")
  1448. * 2..3 NamedCurve
  1449. * 4 ECPoint.len
  1450. * 5+ ECPoint contents
  1451. */
  1452. if (end - *p < 4) {
  1453. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1454. }
  1455. /* First byte is curve_type; only named_curve is handled */
  1456. if (*(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
  1457. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1458. }
  1459. /* Next two bytes are the namedcurve value */
  1460. tls_id = MBEDTLS_GET_UINT16_BE(*p, 0);
  1461. *p += 2;
  1462. /* Check it's a curve we offered */
  1463. if (mbedtls_ssl_check_curve_tls_id(ssl, tls_id) != 0) {
  1464. MBEDTLS_SSL_DEBUG_MSG(2,
  1465. ("bad server key exchange message (ECDHE curve): %u",
  1466. (unsigned) tls_id));
  1467. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1468. }
  1469. /* Convert EC's TLS ID to PSA key type. */
  1470. if (mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
  1471. &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
  1472. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1473. }
  1474. handshake->xxdh_psa_type = key_type;
  1475. handshake->xxdh_psa_bits = ec_bits;
  1476. /* Keep a copy of the peer's public key */
  1477. ecpoint_len = *(*p)++;
  1478. if ((size_t) (end - *p) < ecpoint_len) {
  1479. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1480. }
  1481. if (ecpoint_len > sizeof(handshake->xxdh_psa_peerkey)) {
  1482. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1483. }
  1484. memcpy(handshake->xxdh_psa_peerkey, *p, ecpoint_len);
  1485. handshake->xxdh_psa_peerkey_len = ecpoint_len;
  1486. *p += ecpoint_len;
  1487. return 0;
  1488. }
  1489. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1490. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  1491. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  1492. #else
  1493. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1494. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1495. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  1496. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  1497. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1498. MBEDTLS_CHECK_RETURN_CRITICAL
  1499. static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
  1500. {
  1501. uint16_t tls_id;
  1502. mbedtls_ecp_group_id grp_id;
  1503. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  1504. grp_id = ssl->handshake->ecdh_ctx.grp.id;
  1505. #else
  1506. grp_id = ssl->handshake->ecdh_ctx.grp_id;
  1507. #endif
  1508. tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
  1509. if (tls_id == 0) {
  1510. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  1511. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1512. }
  1513. MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
  1514. mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
  1515. if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
  1516. return -1;
  1517. }
  1518. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  1519. MBEDTLS_DEBUG_ECDH_QP);
  1520. return 0;
  1521. }
  1522. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1523. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  1524. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  1525. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  1526. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1527. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1528. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  1529. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1530. MBEDTLS_CHECK_RETURN_CRITICAL
  1531. static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
  1532. unsigned char **p,
  1533. unsigned char *end)
  1534. {
  1535. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1536. /*
  1537. * Ephemeral ECDH parameters:
  1538. *
  1539. * struct {
  1540. * ECParameters curve_params;
  1541. * ECPoint public;
  1542. * } ServerECDHParams;
  1543. */
  1544. if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
  1545. (const unsigned char **) p, end)) != 0) {
  1546. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
  1547. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  1548. if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
  1549. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  1550. }
  1551. #endif
  1552. return ret;
  1553. }
  1554. if (ssl_check_server_ecdh_params(ssl) != 0) {
  1555. MBEDTLS_SSL_DEBUG_MSG(1,
  1556. ("bad server key exchange message (ECDHE curve)"));
  1557. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1558. }
  1559. return ret;
  1560. }
  1561. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
  1562. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
  1563. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  1564. #endif /* !MBEDTLS_USE_PSA_CRYPTO */
  1565. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  1566. MBEDTLS_CHECK_RETURN_CRITICAL
  1567. static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
  1568. unsigned char **p,
  1569. unsigned char *end)
  1570. {
  1571. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1572. uint16_t len;
  1573. ((void) ssl);
  1574. /*
  1575. * PSK parameters:
  1576. *
  1577. * opaque psk_identity_hint<0..2^16-1>;
  1578. */
  1579. if (end - (*p) < 2) {
  1580. MBEDTLS_SSL_DEBUG_MSG(1,
  1581. ("bad server key exchange message (psk_identity_hint length)"));
  1582. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1583. }
  1584. len = MBEDTLS_GET_UINT16_BE(*p, 0);
  1585. *p += 2;
  1586. if (end - (*p) < len) {
  1587. MBEDTLS_SSL_DEBUG_MSG(1,
  1588. ("bad server key exchange message (psk_identity_hint length)"));
  1589. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1590. }
  1591. /*
  1592. * Note: we currently ignore the PSK identity hint, as we only allow one
  1593. * PSK to be provisioned on the client. This could be changed later if
  1594. * someone needs that feature.
  1595. */
  1596. *p += len;
  1597. ret = 0;
  1598. return ret;
  1599. }
  1600. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  1601. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  1602. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  1603. /*
  1604. * Generate a pre-master secret and encrypt it with the server's RSA key
  1605. */
  1606. MBEDTLS_CHECK_RETURN_CRITICAL
  1607. static int ssl_write_encrypted_pms(mbedtls_ssl_context *ssl,
  1608. size_t offset, size_t *olen,
  1609. size_t pms_offset)
  1610. {
  1611. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1612. size_t len_bytes = 2;
  1613. unsigned char *p = ssl->handshake->premaster + pms_offset;
  1614. mbedtls_pk_context *peer_pk;
  1615. if (offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN) {
  1616. MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small for encrypted pms"));
  1617. return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
  1618. }
  1619. /*
  1620. * Generate (part of) the pre-master as
  1621. * struct {
  1622. * ProtocolVersion client_version;
  1623. * opaque random[46];
  1624. * } PreMasterSecret;
  1625. */
  1626. mbedtls_ssl_write_version(p, ssl->conf->transport,
  1627. MBEDTLS_SSL_VERSION_TLS1_2);
  1628. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p + 2, 46)) != 0) {
  1629. MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
  1630. return ret;
  1631. }
  1632. ssl->handshake->pmslen = 48;
  1633. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  1634. peer_pk = &ssl->handshake->peer_pubkey;
  1635. #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  1636. if (ssl->session_negotiate->peer_cert == NULL) {
  1637. /* Should never happen */
  1638. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  1639. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1640. }
  1641. peer_pk = &ssl->session_negotiate->peer_cert->pk;
  1642. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  1643. /*
  1644. * Now write it out, encrypted
  1645. */
  1646. if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_RSA)) {
  1647. MBEDTLS_SSL_DEBUG_MSG(1, ("certificate key type mismatch"));
  1648. return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
  1649. }
  1650. if ((ret = mbedtls_pk_encrypt(peer_pk,
  1651. p, ssl->handshake->pmslen,
  1652. ssl->out_msg + offset + len_bytes, olen,
  1653. MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
  1654. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  1655. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_rsa_pkcs1_encrypt", ret);
  1656. return ret;
  1657. }
  1658. if (len_bytes == 2) {
  1659. MBEDTLS_PUT_UINT16_BE(*olen, ssl->out_msg, offset);
  1660. *olen += 2;
  1661. }
  1662. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  1663. /* We don't need the peer's public key anymore. Free it. */
  1664. mbedtls_pk_free(peer_pk);
  1665. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  1666. return 0;
  1667. }
  1668. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
  1669. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  1670. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1671. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1672. MBEDTLS_CHECK_RETURN_CRITICAL
  1673. static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
  1674. {
  1675. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1676. mbedtls_pk_context *peer_pk;
  1677. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  1678. peer_pk = &ssl->handshake->peer_pubkey;
  1679. #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  1680. if (ssl->session_negotiate->peer_cert == NULL) {
  1681. /* Should never happen */
  1682. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  1683. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1684. }
  1685. peer_pk = &ssl->session_negotiate->peer_cert->pk;
  1686. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  1687. /* This is a public key, so it can't be opaque, so can_do() is a good
  1688. * enough check to ensure pk_ec() is safe to use below. */
  1689. if (!mbedtls_pk_can_do(peer_pk, MBEDTLS_PK_ECKEY)) {
  1690. MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
  1691. return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
  1692. }
  1693. #if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  1694. const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
  1695. #endif /* !defined(MBEDTLS_PK_USE_PSA_EC_DATA) */
  1696. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1697. uint16_t tls_id = 0;
  1698. psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
  1699. mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(peer_pk);
  1700. if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
  1701. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
  1702. return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
  1703. }
  1704. tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
  1705. if (tls_id == 0) {
  1706. MBEDTLS_SSL_DEBUG_MSG(1, ("ECC group %u not suported",
  1707. grp_id));
  1708. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1709. }
  1710. /* If the above conversion to TLS ID was fine, then also this one will be,
  1711. so there is no need to check the return value here */
  1712. mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &key_type,
  1713. &ssl->handshake->xxdh_psa_bits);
  1714. ssl->handshake->xxdh_psa_type = key_type;
  1715. /* Store peer's public key in psa format. */
  1716. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  1717. memcpy(ssl->handshake->xxdh_psa_peerkey, peer_pk->pub_raw, peer_pk->pub_raw_len);
  1718. ssl->handshake->xxdh_psa_peerkey_len = peer_pk->pub_raw_len;
  1719. ret = 0;
  1720. #else /* MBEDTLS_PK_USE_PSA_EC_DATA */
  1721. size_t olen = 0;
  1722. ret = mbedtls_ecp_point_write_binary(&peer_key->grp, &peer_key->Q,
  1723. MBEDTLS_ECP_PF_UNCOMPRESSED, &olen,
  1724. ssl->handshake->xxdh_psa_peerkey,
  1725. sizeof(ssl->handshake->xxdh_psa_peerkey));
  1726. if (ret != 0) {
  1727. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecp_point_write_binary"), ret);
  1728. return ret;
  1729. }
  1730. ssl->handshake->xxdh_psa_peerkey_len = olen;
  1731. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  1732. #else /* MBEDTLS_USE_PSA_CRYPTO */
  1733. if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
  1734. MBEDTLS_ECDH_THEIRS)) != 0) {
  1735. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
  1736. return ret;
  1737. }
  1738. if (ssl_check_server_ecdh_params(ssl) != 0) {
  1739. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
  1740. return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
  1741. }
  1742. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1743. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  1744. /* We don't need the peer's public key anymore. Free it,
  1745. * so that more RAM is available for upcoming expensive
  1746. * operations like ECDHE. */
  1747. mbedtls_pk_free(peer_pk);
  1748. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  1749. return ret;
  1750. }
  1751. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
  1752. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1753. MBEDTLS_CHECK_RETURN_CRITICAL
  1754. static int ssl_parse_server_key_exchange(mbedtls_ssl_context *ssl)
  1755. {
  1756. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1757. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  1758. ssl->handshake->ciphersuite_info;
  1759. unsigned char *p = NULL, *end = NULL;
  1760. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server key exchange"));
  1761. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  1762. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
  1763. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
  1764. ssl->state++;
  1765. return 0;
  1766. }
  1767. ((void) p);
  1768. ((void) end);
  1769. #endif
  1770. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1771. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1772. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  1773. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
  1774. if ((ret = ssl_get_ecdh_params_from_cert(ssl)) != 0) {
  1775. MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
  1776. mbedtls_ssl_send_alert_message(
  1777. ssl,
  1778. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1779. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1780. return ret;
  1781. }
  1782. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse server key exchange"));
  1783. ssl->state++;
  1784. return 0;
  1785. }
  1786. ((void) p);
  1787. ((void) end);
  1788. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  1789. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1790. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  1791. if (ssl->handshake->ecrs_enabled &&
  1792. ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing) {
  1793. goto start_processing;
  1794. }
  1795. #endif
  1796. if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
  1797. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  1798. return ret;
  1799. }
  1800. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
  1801. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  1802. mbedtls_ssl_send_alert_message(
  1803. ssl,
  1804. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1805. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
  1806. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  1807. }
  1808. /*
  1809. * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
  1810. * doesn't use a psk_identity_hint
  1811. */
  1812. if (ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE) {
  1813. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  1814. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
  1815. /* Current message is probably either
  1816. * CertificateRequest or ServerHelloDone */
  1817. ssl->keep_current_message = 1;
  1818. goto exit;
  1819. }
  1820. MBEDTLS_SSL_DEBUG_MSG(1,
  1821. ("server key exchange message must not be skipped"));
  1822. mbedtls_ssl_send_alert_message(
  1823. ssl,
  1824. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1825. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
  1826. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  1827. }
  1828. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  1829. if (ssl->handshake->ecrs_enabled) {
  1830. ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
  1831. }
  1832. start_processing:
  1833. #endif
  1834. p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
  1835. end = ssl->in_msg + ssl->in_hslen;
  1836. MBEDTLS_SSL_DEBUG_BUF(3, "server key exchange", p, (size_t) (end - p));
  1837. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  1838. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  1839. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  1840. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  1841. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
  1842. if (ssl_parse_server_psk_hint(ssl, &p, end) != 0) {
  1843. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  1844. mbedtls_ssl_send_alert_message(
  1845. ssl,
  1846. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1847. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1848. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1849. }
  1850. } /* FALLTHROUGH */
  1851. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  1852. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
  1853. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  1854. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  1855. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
  1856. ; /* nothing more to do */
  1857. } else
  1858. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
  1859. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  1860. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1861. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  1862. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
  1863. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
  1864. if (ssl_parse_server_dh_params(ssl, &p, end) != 0) {
  1865. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  1866. mbedtls_ssl_send_alert_message(
  1867. ssl,
  1868. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1869. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1870. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1871. }
  1872. } else
  1873. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1874. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  1875. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1876. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  1877. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1878. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  1879. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  1880. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA) {
  1881. if (ssl_parse_server_ecdh_params(ssl, &p, end) != 0) {
  1882. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  1883. mbedtls_ssl_send_alert_message(
  1884. ssl,
  1885. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1886. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1887. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1888. }
  1889. } else
  1890. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1891. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  1892. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  1893. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1894. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
  1895. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1896. /*
  1897. * The first 3 bytes are:
  1898. * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
  1899. * [1, 2] elliptic curve's TLS ID
  1900. *
  1901. * However since we only support secp256r1 for now, we check only
  1902. * that TLS ID here
  1903. */
  1904. uint16_t read_tls_id = MBEDTLS_GET_UINT16_BE(p, 1);
  1905. uint16_t exp_tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
  1906. MBEDTLS_ECP_DP_SECP256R1);
  1907. if (exp_tls_id == 0) {
  1908. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1909. }
  1910. if ((*p != MBEDTLS_ECP_TLS_NAMED_CURVE) ||
  1911. (read_tls_id != exp_tls_id)) {
  1912. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1913. }
  1914. p += 3;
  1915. if ((ret = mbedtls_psa_ecjpake_read_round(
  1916. &ssl->handshake->psa_pake_ctx, p, end - p,
  1917. MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
  1918. psa_destroy_key(ssl->handshake->psa_pake_password);
  1919. psa_pake_abort(&ssl->handshake->psa_pake_ctx);
  1920. MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
  1921. mbedtls_ssl_send_alert_message(
  1922. ssl,
  1923. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1924. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1925. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1926. }
  1927. #else
  1928. ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
  1929. p, end - p);
  1930. if (ret != 0) {
  1931. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
  1932. mbedtls_ssl_send_alert_message(
  1933. ssl,
  1934. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1935. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1936. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1937. }
  1938. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1939. } else
  1940. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1941. {
  1942. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  1943. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1944. }
  1945. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  1946. if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
  1947. size_t sig_len, hashlen;
  1948. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  1949. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  1950. mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
  1951. unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
  1952. size_t params_len = (size_t) (p - params);
  1953. void *rs_ctx = NULL;
  1954. uint16_t sig_alg;
  1955. mbedtls_pk_context *peer_pk;
  1956. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  1957. peer_pk = &ssl->handshake->peer_pubkey;
  1958. #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  1959. if (ssl->session_negotiate->peer_cert == NULL) {
  1960. /* Should never happen */
  1961. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  1962. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1963. }
  1964. peer_pk = &ssl->session_negotiate->peer_cert->pk;
  1965. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  1966. /*
  1967. * Handle the digitally-signed structure
  1968. */
  1969. MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
  1970. sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
  1971. if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
  1972. sig_alg, &pk_alg, &md_alg) != 0 &&
  1973. !mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg) &&
  1974. !mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
  1975. MBEDTLS_SSL_DEBUG_MSG(1,
  1976. ("bad server key exchange message"));
  1977. mbedtls_ssl_send_alert_message(
  1978. ssl,
  1979. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1980. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1981. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1982. }
  1983. p += 2;
  1984. if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
  1985. MBEDTLS_SSL_DEBUG_MSG(1,
  1986. ("bad server key exchange message"));
  1987. mbedtls_ssl_send_alert_message(
  1988. ssl,
  1989. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1990. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  1991. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  1992. }
  1993. /*
  1994. * Read signature
  1995. */
  1996. if (p > end - 2) {
  1997. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  1998. mbedtls_ssl_send_alert_message(
  1999. ssl,
  2000. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2001. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2002. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2003. }
  2004. sig_len = MBEDTLS_GET_UINT16_BE(p, 0);
  2005. p += 2;
  2006. if (p != end - sig_len) {
  2007. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  2008. mbedtls_ssl_send_alert_message(
  2009. ssl,
  2010. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2011. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2012. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2013. }
  2014. MBEDTLS_SSL_DEBUG_BUF(3, "signature", p, sig_len);
  2015. /*
  2016. * Compute the hash that has been signed
  2017. */
  2018. if (md_alg != MBEDTLS_MD_NONE) {
  2019. ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
  2020. params, params_len,
  2021. md_alg);
  2022. if (ret != 0) {
  2023. return ret;
  2024. }
  2025. } else {
  2026. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2027. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2028. }
  2029. MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
  2030. /*
  2031. * Verify signature
  2032. */
  2033. if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
  2034. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server key exchange message"));
  2035. mbedtls_ssl_send_alert_message(
  2036. ssl,
  2037. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2038. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  2039. return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
  2040. }
  2041. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2042. if (ssl->handshake->ecrs_enabled) {
  2043. rs_ctx = &ssl->handshake->ecrs_ctx.pk;
  2044. }
  2045. #endif
  2046. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  2047. if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
  2048. mbedtls_pk_rsassa_pss_options rsassa_pss_options;
  2049. rsassa_pss_options.mgf1_hash_id = md_alg;
  2050. rsassa_pss_options.expected_salt_len =
  2051. mbedtls_md_get_size_from_type(md_alg);
  2052. if (rsassa_pss_options.expected_salt_len == 0) {
  2053. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2054. }
  2055. ret = mbedtls_pk_verify_ext(pk_alg, &rsassa_pss_options,
  2056. peer_pk,
  2057. md_alg, hash, hashlen,
  2058. p, sig_len);
  2059. } else
  2060. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  2061. ret = mbedtls_pk_verify_restartable(peer_pk,
  2062. md_alg, hash, hashlen, p, sig_len, rs_ctx);
  2063. if (ret != 0) {
  2064. int send_alert_msg = 1;
  2065. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2066. send_alert_msg = (ret != MBEDTLS_ERR_ECP_IN_PROGRESS);
  2067. #endif
  2068. if (send_alert_msg) {
  2069. mbedtls_ssl_send_alert_message(
  2070. ssl,
  2071. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2072. MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
  2073. }
  2074. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
  2075. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2076. if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
  2077. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  2078. }
  2079. #endif
  2080. return ret;
  2081. }
  2082. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2083. /* We don't need the peer's public key anymore. Free it,
  2084. * so that more RAM is available for upcoming expensive
  2085. * operations like ECDHE. */
  2086. mbedtls_pk_free(peer_pk);
  2087. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2088. }
  2089. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
  2090. exit:
  2091. ssl->state++;
  2092. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server key exchange"));
  2093. return 0;
  2094. }
  2095. #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
  2096. MBEDTLS_CHECK_RETURN_CRITICAL
  2097. static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
  2098. {
  2099. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2100. ssl->handshake->ciphersuite_info;
  2101. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
  2102. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  2103. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
  2104. ssl->state++;
  2105. return 0;
  2106. }
  2107. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2108. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2109. }
  2110. #else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  2111. MBEDTLS_CHECK_RETURN_CRITICAL
  2112. static int ssl_parse_certificate_request(mbedtls_ssl_context *ssl)
  2113. {
  2114. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2115. unsigned char *buf;
  2116. size_t n = 0;
  2117. size_t cert_type_len = 0, dn_len = 0;
  2118. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2119. ssl->handshake->ciphersuite_info;
  2120. size_t sig_alg_len;
  2121. #if defined(MBEDTLS_DEBUG_C)
  2122. unsigned char *sig_alg;
  2123. unsigned char *dn;
  2124. #endif
  2125. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
  2126. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  2127. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate request"));
  2128. ssl->state++;
  2129. return 0;
  2130. }
  2131. if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
  2132. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  2133. return ret;
  2134. }
  2135. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
  2136. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
  2137. mbedtls_ssl_send_alert_message(
  2138. ssl,
  2139. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2140. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
  2141. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  2142. }
  2143. ssl->state++;
  2144. ssl->handshake->client_auth =
  2145. (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST);
  2146. MBEDTLS_SSL_DEBUG_MSG(3, ("got %s certificate request",
  2147. ssl->handshake->client_auth ? "a" : "no"));
  2148. if (ssl->handshake->client_auth == 0) {
  2149. /* Current message is probably the ServerHelloDone */
  2150. ssl->keep_current_message = 1;
  2151. goto exit;
  2152. }
  2153. /*
  2154. * struct {
  2155. * ClientCertificateType certificate_types<1..2^8-1>;
  2156. * SignatureAndHashAlgorithm
  2157. * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
  2158. * DistinguishedName certificate_authorities<0..2^16-1>;
  2159. * } CertificateRequest;
  2160. *
  2161. * Since we only support a single certificate on clients, let's just
  2162. * ignore all the information that's supposed to help us pick a
  2163. * certificate.
  2164. *
  2165. * We could check that our certificate matches the request, and bail out
  2166. * if it doesn't, but it's simpler to just send the certificate anyway,
  2167. * and give the server the opportunity to decide if it should terminate
  2168. * the connection when it doesn't like our certificate.
  2169. *
  2170. * Same goes for the hash in TLS 1.2's signature_algorithms: at this
  2171. * point we only have one hash available (see comments in
  2172. * write_certificate_verify), so let's just use what we have.
  2173. *
  2174. * However, we still minimally parse the message to check it is at least
  2175. * superficially sane.
  2176. */
  2177. buf = ssl->in_msg;
  2178. /* certificate_types */
  2179. if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl)) {
  2180. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
  2181. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2182. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2183. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2184. }
  2185. cert_type_len = buf[mbedtls_ssl_hs_hdr_len(ssl)];
  2186. n = cert_type_len;
  2187. /*
  2188. * In the subsequent code there are two paths that read from buf:
  2189. * * the length of the signature algorithms field (if minor version of
  2190. * SSL is 3),
  2191. * * distinguished name length otherwise.
  2192. * Both reach at most the index:
  2193. * ...hdr_len + 2 + n,
  2194. * therefore the buffer length at this point must be greater than that
  2195. * regardless of the actual code path.
  2196. */
  2197. if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 2 + n) {
  2198. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
  2199. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2200. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2201. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2202. }
  2203. /* supported_signature_algorithms */
  2204. sig_alg_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
  2205. /*
  2206. * The furthest access in buf is in the loop few lines below:
  2207. * sig_alg[i + 1],
  2208. * where:
  2209. * sig_alg = buf + ...hdr_len + 3 + n,
  2210. * max(i) = sig_alg_len - 1.
  2211. * Therefore the furthest access is:
  2212. * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
  2213. * which reduces to:
  2214. * buf[...hdr_len + 3 + n + sig_alg_len],
  2215. * which is one less than we need the buf to be.
  2216. */
  2217. if (ssl->in_hslen <= mbedtls_ssl_hs_hdr_len(ssl) + 3 + n + sig_alg_len) {
  2218. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
  2219. mbedtls_ssl_send_alert_message(
  2220. ssl,
  2221. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2222. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2223. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2224. }
  2225. #if defined(MBEDTLS_DEBUG_C)
  2226. sig_alg = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n;
  2227. for (size_t i = 0; i < sig_alg_len; i += 2) {
  2228. MBEDTLS_SSL_DEBUG_MSG(3,
  2229. ("Supported Signature Algorithm found: %02x %02x",
  2230. sig_alg[i], sig_alg[i + 1]));
  2231. }
  2232. #endif
  2233. n += 2 + sig_alg_len;
  2234. /* certificate_authorities */
  2235. dn_len = MBEDTLS_GET_UINT16_BE(buf, mbedtls_ssl_hs_hdr_len(ssl) + 1 + n);
  2236. n += dn_len;
  2237. if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + 3 + n) {
  2238. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
  2239. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2240. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2241. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2242. }
  2243. #if defined(MBEDTLS_DEBUG_C)
  2244. dn = buf + mbedtls_ssl_hs_hdr_len(ssl) + 3 + n - dn_len;
  2245. for (size_t i = 0, dni_len = 0; i < dn_len; i += 2 + dni_len) {
  2246. unsigned char *p = dn + i + 2;
  2247. mbedtls_x509_name name;
  2248. size_t asn1_len;
  2249. char s[MBEDTLS_X509_MAX_DN_NAME_SIZE];
  2250. memset(&name, 0, sizeof(name));
  2251. dni_len = MBEDTLS_GET_UINT16_BE(dn + i, 0);
  2252. if (dni_len > dn_len - i - 2 ||
  2253. mbedtls_asn1_get_tag(&p, p + dni_len, &asn1_len,
  2254. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) != 0 ||
  2255. mbedtls_x509_get_name(&p, p + asn1_len, &name) != 0) {
  2256. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate request message"));
  2257. mbedtls_ssl_send_alert_message(
  2258. ssl,
  2259. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2260. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2261. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2262. }
  2263. MBEDTLS_SSL_DEBUG_MSG(3,
  2264. ("DN hint: %.*s",
  2265. mbedtls_x509_dn_gets(s, sizeof(s), &name), s));
  2266. mbedtls_asn1_free_named_data_list_shallow(name.next);
  2267. }
  2268. #endif
  2269. exit:
  2270. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
  2271. return 0;
  2272. }
  2273. #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  2274. MBEDTLS_CHECK_RETURN_CRITICAL
  2275. static int ssl_parse_server_hello_done(mbedtls_ssl_context *ssl)
  2276. {
  2277. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2278. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse server hello done"));
  2279. if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
  2280. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  2281. return ret;
  2282. }
  2283. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
  2284. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
  2285. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  2286. }
  2287. if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) ||
  2288. ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE) {
  2289. MBEDTLS_SSL_DEBUG_MSG(1, ("bad server hello done message"));
  2290. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2291. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2292. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2293. }
  2294. ssl->state++;
  2295. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2296. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  2297. mbedtls_ssl_recv_flight_completed(ssl);
  2298. }
  2299. #endif
  2300. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse server hello done"));
  2301. return 0;
  2302. }
  2303. MBEDTLS_CHECK_RETURN_CRITICAL
  2304. static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
  2305. {
  2306. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2307. size_t header_len;
  2308. size_t content_len;
  2309. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2310. ssl->handshake->ciphersuite_info;
  2311. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client key exchange"));
  2312. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  2313. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
  2314. /*
  2315. * DHM key exchange -- send G^X mod P
  2316. */
  2317. content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
  2318. MBEDTLS_PUT_UINT16_BE(content_len, ssl->out_msg, 4);
  2319. header_len = 6;
  2320. ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
  2321. (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
  2322. &ssl->out_msg[header_len], content_len,
  2323. ssl->conf->f_rng, ssl->conf->p_rng);
  2324. if (ret != 0) {
  2325. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
  2326. return ret;
  2327. }
  2328. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
  2329. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
  2330. if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
  2331. ssl->handshake->premaster,
  2332. MBEDTLS_PREMASTER_SIZE,
  2333. &ssl->handshake->pmslen,
  2334. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  2335. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
  2336. return ret;
  2337. }
  2338. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
  2339. } else
  2340. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  2341. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2342. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  2343. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2344. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  2345. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  2346. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
  2347. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  2348. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
  2349. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2350. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2351. psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
  2352. psa_key_attributes_t key_attributes;
  2353. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  2354. header_len = 4;
  2355. MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
  2356. /*
  2357. * Generate EC private key for ECDHE exchange.
  2358. */
  2359. /* The master secret is obtained from the shared ECDH secret by
  2360. * applying the TLS 1.2 PRF with a specific salt and label. While
  2361. * the PSA Crypto API encourages combining key agreement schemes
  2362. * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
  2363. * yet support the provisioning of salt + label to the KDF.
  2364. * For the time being, we therefore need to split the computation
  2365. * of the ECDH secret and the application of the TLS 1.2 PRF. */
  2366. key_attributes = psa_key_attributes_init();
  2367. psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
  2368. psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
  2369. psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
  2370. psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
  2371. /* Generate ECDH private key. */
  2372. status = psa_generate_key(&key_attributes,
  2373. &handshake->xxdh_psa_privkey);
  2374. if (status != PSA_SUCCESS) {
  2375. return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  2376. }
  2377. /* Export the public part of the ECDH private key from PSA.
  2378. * The export format is an ECPoint structure as expected by TLS,
  2379. * but we just need to add a length byte before that. */
  2380. unsigned char *own_pubkey = ssl->out_msg + header_len + 1;
  2381. unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
  2382. size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
  2383. size_t own_pubkey_len;
  2384. status = psa_export_public_key(handshake->xxdh_psa_privkey,
  2385. own_pubkey, own_pubkey_max_len,
  2386. &own_pubkey_len);
  2387. if (status != PSA_SUCCESS) {
  2388. psa_destroy_key(handshake->xxdh_psa_privkey);
  2389. handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  2390. return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  2391. }
  2392. ssl->out_msg[header_len] = (unsigned char) own_pubkey_len;
  2393. content_len = own_pubkey_len + 1;
  2394. /* The ECDH secret is the premaster secret used for key derivation. */
  2395. /* Compute ECDH shared secret. */
  2396. status = psa_raw_key_agreement(PSA_ALG_ECDH,
  2397. handshake->xxdh_psa_privkey,
  2398. handshake->xxdh_psa_peerkey,
  2399. handshake->xxdh_psa_peerkey_len,
  2400. ssl->handshake->premaster,
  2401. sizeof(ssl->handshake->premaster),
  2402. &ssl->handshake->pmslen);
  2403. destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
  2404. handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  2405. if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
  2406. return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  2407. }
  2408. #else
  2409. /*
  2410. * ECDH key exchange -- send client public value
  2411. */
  2412. header_len = 4;
  2413. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2414. if (ssl->handshake->ecrs_enabled) {
  2415. if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
  2416. goto ecdh_calc_secret;
  2417. }
  2418. mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
  2419. }
  2420. #endif
  2421. ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
  2422. &content_len,
  2423. &ssl->out_msg[header_len], 1000,
  2424. ssl->conf->f_rng, ssl->conf->p_rng);
  2425. if (ret != 0) {
  2426. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
  2427. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2428. if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
  2429. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  2430. }
  2431. #endif
  2432. return ret;
  2433. }
  2434. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  2435. MBEDTLS_DEBUG_ECDH_Q);
  2436. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2437. if (ssl->handshake->ecrs_enabled) {
  2438. ssl->handshake->ecrs_n = content_len;
  2439. ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
  2440. }
  2441. ecdh_calc_secret:
  2442. if (ssl->handshake->ecrs_enabled) {
  2443. content_len = ssl->handshake->ecrs_n;
  2444. }
  2445. #endif
  2446. if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
  2447. &ssl->handshake->pmslen,
  2448. ssl->handshake->premaster,
  2449. MBEDTLS_MPI_MAX_SIZE,
  2450. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  2451. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
  2452. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2453. if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
  2454. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  2455. }
  2456. #endif
  2457. return ret;
  2458. }
  2459. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  2460. MBEDTLS_DEBUG_ECDH_Z);
  2461. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  2462. } else
  2463. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2464. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  2465. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  2466. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  2467. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  2468. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  2469. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
  2470. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2471. psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
  2472. psa_key_attributes_t key_attributes;
  2473. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  2474. /*
  2475. * opaque psk_identity<0..2^16-1>;
  2476. */
  2477. if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
  2478. /* We don't offer PSK suites if we don't have a PSK,
  2479. * and we check that the server's choice is among the
  2480. * ciphersuites we offered, so this should never happen. */
  2481. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2482. }
  2483. /* uint16 to store content length */
  2484. const size_t content_len_size = 2;
  2485. header_len = 4;
  2486. if (header_len + content_len_size + ssl->conf->psk_identity_len
  2487. > MBEDTLS_SSL_OUT_CONTENT_LEN) {
  2488. MBEDTLS_SSL_DEBUG_MSG(1,
  2489. ("psk identity too long or SSL buffer too short"));
  2490. return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
  2491. }
  2492. unsigned char *p = ssl->out_msg + header_len;
  2493. *p++ = MBEDTLS_BYTE_1(ssl->conf->psk_identity_len);
  2494. *p++ = MBEDTLS_BYTE_0(ssl->conf->psk_identity_len);
  2495. header_len += content_len_size;
  2496. memcpy(p, ssl->conf->psk_identity,
  2497. ssl->conf->psk_identity_len);
  2498. p += ssl->conf->psk_identity_len;
  2499. header_len += ssl->conf->psk_identity_len;
  2500. MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
  2501. /*
  2502. * Generate EC private key for ECDHE exchange.
  2503. */
  2504. /* The master secret is obtained from the shared ECDH secret by
  2505. * applying the TLS 1.2 PRF with a specific salt and label. While
  2506. * the PSA Crypto API encourages combining key agreement schemes
  2507. * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
  2508. * yet support the provisioning of salt + label to the KDF.
  2509. * For the time being, we therefore need to split the computation
  2510. * of the ECDH secret and the application of the TLS 1.2 PRF. */
  2511. key_attributes = psa_key_attributes_init();
  2512. psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
  2513. psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
  2514. psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
  2515. psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
  2516. /* Generate ECDH private key. */
  2517. status = psa_generate_key(&key_attributes,
  2518. &handshake->xxdh_psa_privkey);
  2519. if (status != PSA_SUCCESS) {
  2520. return PSA_TO_MBEDTLS_ERR(status);
  2521. }
  2522. /* Export the public part of the ECDH private key from PSA.
  2523. * The export format is an ECPoint structure as expected by TLS,
  2524. * but we just need to add a length byte before that. */
  2525. unsigned char *own_pubkey = p + 1;
  2526. unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
  2527. size_t own_pubkey_max_len = (size_t) (end - own_pubkey);
  2528. size_t own_pubkey_len = 0;
  2529. status = psa_export_public_key(handshake->xxdh_psa_privkey,
  2530. own_pubkey, own_pubkey_max_len,
  2531. &own_pubkey_len);
  2532. if (status != PSA_SUCCESS) {
  2533. psa_destroy_key(handshake->xxdh_psa_privkey);
  2534. handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  2535. return PSA_TO_MBEDTLS_ERR(status);
  2536. }
  2537. *p = (unsigned char) own_pubkey_len;
  2538. content_len = own_pubkey_len + 1;
  2539. /* As RFC 5489 section 2, the premaster secret is formed as follows:
  2540. * - a uint16 containing the length (in octets) of the ECDH computation
  2541. * - the octet string produced by the ECDH computation
  2542. * - a uint16 containing the length (in octets) of the PSK
  2543. * - the PSK itself
  2544. */
  2545. unsigned char *pms = ssl->handshake->premaster;
  2546. const unsigned char * const pms_end = pms +
  2547. sizeof(ssl->handshake->premaster);
  2548. /* uint16 to store length (in octets) of the ECDH computation */
  2549. const size_t zlen_size = 2;
  2550. size_t zlen = 0;
  2551. /* Perform ECDH computation after the uint16 reserved for the length */
  2552. status = psa_raw_key_agreement(PSA_ALG_ECDH,
  2553. handshake->xxdh_psa_privkey,
  2554. handshake->xxdh_psa_peerkey,
  2555. handshake->xxdh_psa_peerkey_len,
  2556. pms + zlen_size,
  2557. pms_end - (pms + zlen_size),
  2558. &zlen);
  2559. destruction_status = psa_destroy_key(handshake->xxdh_psa_privkey);
  2560. handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  2561. if (status != PSA_SUCCESS) {
  2562. return PSA_TO_MBEDTLS_ERR(status);
  2563. } else if (destruction_status != PSA_SUCCESS) {
  2564. return PSA_TO_MBEDTLS_ERR(destruction_status);
  2565. }
  2566. /* Write the ECDH computation length before the ECDH computation */
  2567. MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
  2568. pms += zlen_size + zlen;
  2569. } else
  2570. #endif /* MBEDTLS_USE_PSA_CRYPTO &&
  2571. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  2572. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  2573. if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
  2574. /*
  2575. * opaque psk_identity<0..2^16-1>;
  2576. */
  2577. if (mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
  2578. /* We don't offer PSK suites if we don't have a PSK,
  2579. * and we check that the server's choice is among the
  2580. * ciphersuites we offered, so this should never happen. */
  2581. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2582. }
  2583. header_len = 4;
  2584. content_len = ssl->conf->psk_identity_len;
  2585. if (header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
  2586. MBEDTLS_SSL_DEBUG_MSG(1,
  2587. ("psk identity too long or SSL buffer too short"));
  2588. return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
  2589. }
  2590. ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
  2591. ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
  2592. memcpy(ssl->out_msg + header_len,
  2593. ssl->conf->psk_identity,
  2594. ssl->conf->psk_identity_len);
  2595. header_len += ssl->conf->psk_identity_len;
  2596. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  2597. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
  2598. content_len = 0;
  2599. } else
  2600. #endif
  2601. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  2602. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
  2603. if ((ret = ssl_write_encrypted_pms(ssl, header_len,
  2604. &content_len, 2)) != 0) {
  2605. return ret;
  2606. }
  2607. } else
  2608. #endif
  2609. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  2610. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
  2611. /*
  2612. * ClientDiffieHellmanPublic public (DHM send G^X mod P)
  2613. */
  2614. content_len = mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx);
  2615. if (header_len + 2 + content_len >
  2616. MBEDTLS_SSL_OUT_CONTENT_LEN) {
  2617. MBEDTLS_SSL_DEBUG_MSG(1,
  2618. ("psk identity or DHM size too long or SSL buffer too short"));
  2619. return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
  2620. }
  2621. ssl->out_msg[header_len++] = MBEDTLS_BYTE_1(content_len);
  2622. ssl->out_msg[header_len++] = MBEDTLS_BYTE_0(content_len);
  2623. ret = mbedtls_dhm_make_public(&ssl->handshake->dhm_ctx,
  2624. (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
  2625. &ssl->out_msg[header_len], content_len,
  2626. ssl->conf->f_rng, ssl->conf->p_rng);
  2627. if (ret != 0) {
  2628. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_public", ret);
  2629. return ret;
  2630. }
  2631. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2632. unsigned char *pms = ssl->handshake->premaster;
  2633. unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
  2634. size_t pms_len;
  2635. /* Write length only when we know the actual value */
  2636. if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
  2637. pms + 2, pms_end - (pms + 2), &pms_len,
  2638. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  2639. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
  2640. return ret;
  2641. }
  2642. MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
  2643. pms += 2 + pms_len;
  2644. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
  2645. #endif
  2646. } else
  2647. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  2648. #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
  2649. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  2650. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
  2651. /*
  2652. * ClientECDiffieHellmanPublic public;
  2653. */
  2654. ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
  2655. &content_len,
  2656. &ssl->out_msg[header_len],
  2657. MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
  2658. ssl->conf->f_rng, ssl->conf->p_rng);
  2659. if (ret != 0) {
  2660. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
  2661. return ret;
  2662. }
  2663. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  2664. MBEDTLS_DEBUG_ECDH_Q);
  2665. } else
  2666. #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  2667. {
  2668. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2669. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2670. }
  2671. #if !defined(MBEDTLS_USE_PSA_CRYPTO)
  2672. if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
  2673. (mbedtls_key_exchange_type_t) ciphersuite_info->
  2674. key_exchange)) != 0) {
  2675. MBEDTLS_SSL_DEBUG_RET(1,
  2676. "mbedtls_ssl_psk_derive_premaster", ret);
  2677. return ret;
  2678. }
  2679. #endif /* !MBEDTLS_USE_PSA_CRYPTO */
  2680. } else
  2681. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  2682. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  2683. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
  2684. header_len = 4;
  2685. if ((ret = ssl_write_encrypted_pms(ssl, header_len,
  2686. &content_len, 0)) != 0) {
  2687. return ret;
  2688. }
  2689. } else
  2690. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  2691. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2692. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
  2693. header_len = 4;
  2694. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2695. unsigned char *out_p = ssl->out_msg + header_len;
  2696. unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
  2697. header_len;
  2698. ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
  2699. out_p, end_p - out_p, &content_len,
  2700. MBEDTLS_ECJPAKE_ROUND_TWO);
  2701. if (ret != 0) {
  2702. psa_destroy_key(ssl->handshake->psa_pake_password);
  2703. psa_pake_abort(&ssl->handshake->psa_pake_ctx);
  2704. MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
  2705. return ret;
  2706. }
  2707. #else
  2708. ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
  2709. ssl->out_msg + header_len,
  2710. MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
  2711. &content_len,
  2712. ssl->conf->f_rng, ssl->conf->p_rng);
  2713. if (ret != 0) {
  2714. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
  2715. return ret;
  2716. }
  2717. ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
  2718. ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
  2719. ssl->conf->f_rng, ssl->conf->p_rng);
  2720. if (ret != 0) {
  2721. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
  2722. return ret;
  2723. }
  2724. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  2725. } else
  2726. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  2727. {
  2728. ((void) ciphersuite_info);
  2729. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2730. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2731. }
  2732. ssl->out_msglen = header_len + content_len;
  2733. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2734. ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
  2735. ssl->state++;
  2736. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  2737. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  2738. return ret;
  2739. }
  2740. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client key exchange"));
  2741. return 0;
  2742. }
  2743. #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
  2744. MBEDTLS_CHECK_RETURN_CRITICAL
  2745. static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
  2746. {
  2747. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2748. ssl->handshake->ciphersuite_info;
  2749. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2750. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
  2751. if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
  2752. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
  2753. return ret;
  2754. }
  2755. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  2756. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
  2757. ssl->state++;
  2758. return 0;
  2759. }
  2760. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2761. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2762. }
  2763. #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  2764. MBEDTLS_CHECK_RETURN_CRITICAL
  2765. static int ssl_write_certificate_verify(mbedtls_ssl_context *ssl)
  2766. {
  2767. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2768. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2769. ssl->handshake->ciphersuite_info;
  2770. size_t n = 0, offset = 0;
  2771. unsigned char hash[48];
  2772. unsigned char *hash_start = hash;
  2773. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  2774. size_t hashlen;
  2775. void *rs_ctx = NULL;
  2776. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  2777. size_t out_buf_len = ssl->out_buf_len - (size_t) (ssl->out_msg - ssl->out_buf);
  2778. #else
  2779. size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (size_t) (ssl->out_msg - ssl->out_buf);
  2780. #endif
  2781. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
  2782. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2783. if (ssl->handshake->ecrs_enabled &&
  2784. ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign) {
  2785. goto sign;
  2786. }
  2787. #endif
  2788. if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
  2789. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
  2790. return ret;
  2791. }
  2792. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  2793. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
  2794. ssl->state++;
  2795. return 0;
  2796. }
  2797. if (ssl->handshake->client_auth == 0 ||
  2798. mbedtls_ssl_own_cert(ssl) == NULL) {
  2799. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate verify"));
  2800. ssl->state++;
  2801. return 0;
  2802. }
  2803. if (mbedtls_ssl_own_key(ssl) == NULL) {
  2804. MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key for certificate"));
  2805. return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
  2806. }
  2807. /*
  2808. * Make a signature of the handshake digests
  2809. */
  2810. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2811. if (ssl->handshake->ecrs_enabled) {
  2812. ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
  2813. }
  2814. sign:
  2815. #endif
  2816. ret = ssl->handshake->calc_verify(ssl, hash, &hashlen);
  2817. if (0 != ret) {
  2818. MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
  2819. return ret;
  2820. }
  2821. /*
  2822. * digitally-signed struct {
  2823. * opaque handshake_messages[handshake_messages_length];
  2824. * };
  2825. *
  2826. * Taking shortcut here. We assume that the server always allows the
  2827. * PRF Hash function and has sent it in the allowed signature
  2828. * algorithms list received in the Certificate Request message.
  2829. *
  2830. * Until we encounter a server that does not, we will take this
  2831. * shortcut.
  2832. *
  2833. * Reason: Otherwise we should have running hashes for SHA512 and
  2834. * SHA224 in order to satisfy 'weird' needs from the server
  2835. * side.
  2836. */
  2837. if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
  2838. md_alg = MBEDTLS_MD_SHA384;
  2839. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
  2840. } else {
  2841. md_alg = MBEDTLS_MD_SHA256;
  2842. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
  2843. }
  2844. ssl->out_msg[5] = mbedtls_ssl_sig_from_pk(mbedtls_ssl_own_key(ssl));
  2845. /* Info from md_alg will be used instead */
  2846. hashlen = 0;
  2847. offset = 2;
  2848. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2849. if (ssl->handshake->ecrs_enabled) {
  2850. rs_ctx = &ssl->handshake->ecrs_ctx.pk;
  2851. }
  2852. #endif
  2853. if ((ret = mbedtls_pk_sign_restartable(mbedtls_ssl_own_key(ssl),
  2854. md_alg, hash_start, hashlen,
  2855. ssl->out_msg + 6 + offset,
  2856. out_buf_len - 6 - offset,
  2857. &n,
  2858. ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx)) != 0) {
  2859. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
  2860. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2861. if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
  2862. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  2863. }
  2864. #endif
  2865. return ret;
  2866. }
  2867. MBEDTLS_PUT_UINT16_BE(n, ssl->out_msg, offset + 4);
  2868. ssl->out_msglen = 6 + n + offset;
  2869. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2870. ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
  2871. ssl->state++;
  2872. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  2873. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  2874. return ret;
  2875. }
  2876. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
  2877. return ret;
  2878. }
  2879. #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  2880. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2881. MBEDTLS_CHECK_RETURN_CRITICAL
  2882. static int ssl_parse_new_session_ticket(mbedtls_ssl_context *ssl)
  2883. {
  2884. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2885. uint32_t lifetime;
  2886. size_t ticket_len;
  2887. unsigned char *ticket;
  2888. const unsigned char *msg;
  2889. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
  2890. if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
  2891. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  2892. return ret;
  2893. }
  2894. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
  2895. MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
  2896. mbedtls_ssl_send_alert_message(
  2897. ssl,
  2898. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2899. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
  2900. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  2901. }
  2902. /*
  2903. * struct {
  2904. * uint32 ticket_lifetime_hint;
  2905. * opaque ticket<0..2^16-1>;
  2906. * } NewSessionTicket;
  2907. *
  2908. * 0 . 3 ticket_lifetime_hint
  2909. * 4 . 5 ticket_len (n)
  2910. * 6 . 5+n ticket content
  2911. */
  2912. if (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
  2913. ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len(ssl)) {
  2914. MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
  2915. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2916. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2917. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2918. }
  2919. msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
  2920. lifetime = MBEDTLS_GET_UINT32_BE(msg, 0);
  2921. ticket_len = MBEDTLS_GET_UINT16_BE(msg, 4);
  2922. if (ticket_len + 6 + mbedtls_ssl_hs_hdr_len(ssl) != ssl->in_hslen) {
  2923. MBEDTLS_SSL_DEBUG_MSG(1, ("bad new session ticket message"));
  2924. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2925. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  2926. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2927. }
  2928. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len));
  2929. /* We're not waiting for a NewSessionTicket message any more */
  2930. ssl->handshake->new_session_ticket = 0;
  2931. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  2932. /*
  2933. * Zero-length ticket means the server changed his mind and doesn't want
  2934. * to send a ticket after all, so just forget it
  2935. */
  2936. if (ticket_len == 0) {
  2937. return 0;
  2938. }
  2939. if (ssl->session != NULL && ssl->session->ticket != NULL) {
  2940. mbedtls_zeroize_and_free(ssl->session->ticket,
  2941. ssl->session->ticket_len);
  2942. ssl->session->ticket = NULL;
  2943. ssl->session->ticket_len = 0;
  2944. }
  2945. mbedtls_zeroize_and_free(ssl->session_negotiate->ticket,
  2946. ssl->session_negotiate->ticket_len);
  2947. ssl->session_negotiate->ticket = NULL;
  2948. ssl->session_negotiate->ticket_len = 0;
  2949. if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
  2950. MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
  2951. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2952. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
  2953. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  2954. }
  2955. memcpy(ticket, msg + 6, ticket_len);
  2956. ssl->session_negotiate->ticket = ticket;
  2957. ssl->session_negotiate->ticket_len = ticket_len;
  2958. ssl->session_negotiate->ticket_lifetime = lifetime;
  2959. /*
  2960. * RFC 5077 section 3.4:
  2961. * "If the client receives a session ticket from the server, then it
  2962. * discards any Session ID that was sent in the ServerHello."
  2963. */
  2964. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket in use, discarding session id"));
  2965. ssl->session_negotiate->id_len = 0;
  2966. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
  2967. return 0;
  2968. }
  2969. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  2970. /*
  2971. * SSL handshake -- client side -- single step
  2972. */
  2973. int mbedtls_ssl_handshake_client_step(mbedtls_ssl_context *ssl)
  2974. {
  2975. int ret = 0;
  2976. /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
  2977. * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
  2978. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2979. if (ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
  2980. ssl->handshake->new_session_ticket != 0) {
  2981. ssl->state = MBEDTLS_SSL_NEW_SESSION_TICKET;
  2982. }
  2983. #endif
  2984. switch (ssl->state) {
  2985. case MBEDTLS_SSL_HELLO_REQUEST:
  2986. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  2987. break;
  2988. /*
  2989. * ==> ClientHello
  2990. */
  2991. case MBEDTLS_SSL_CLIENT_HELLO:
  2992. ret = mbedtls_ssl_write_client_hello(ssl);
  2993. break;
  2994. /*
  2995. * <== ServerHello
  2996. * Certificate
  2997. * ( ServerKeyExchange )
  2998. * ( CertificateRequest )
  2999. * ServerHelloDone
  3000. */
  3001. case MBEDTLS_SSL_SERVER_HELLO:
  3002. ret = ssl_parse_server_hello(ssl);
  3003. break;
  3004. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  3005. ret = mbedtls_ssl_parse_certificate(ssl);
  3006. break;
  3007. case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
  3008. ret = ssl_parse_server_key_exchange(ssl);
  3009. break;
  3010. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  3011. ret = ssl_parse_certificate_request(ssl);
  3012. break;
  3013. case MBEDTLS_SSL_SERVER_HELLO_DONE:
  3014. ret = ssl_parse_server_hello_done(ssl);
  3015. break;
  3016. /*
  3017. * ==> ( Certificate/Alert )
  3018. * ClientKeyExchange
  3019. * ( CertificateVerify )
  3020. * ChangeCipherSpec
  3021. * Finished
  3022. */
  3023. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  3024. ret = mbedtls_ssl_write_certificate(ssl);
  3025. break;
  3026. case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
  3027. ret = ssl_write_client_key_exchange(ssl);
  3028. break;
  3029. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  3030. ret = ssl_write_certificate_verify(ssl);
  3031. break;
  3032. case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
  3033. ret = mbedtls_ssl_write_change_cipher_spec(ssl);
  3034. break;
  3035. case MBEDTLS_SSL_CLIENT_FINISHED:
  3036. ret = mbedtls_ssl_write_finished(ssl);
  3037. break;
  3038. /*
  3039. * <== ( NewSessionTicket )
  3040. * ChangeCipherSpec
  3041. * Finished
  3042. */
  3043. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3044. case MBEDTLS_SSL_NEW_SESSION_TICKET:
  3045. ret = ssl_parse_new_session_ticket(ssl);
  3046. break;
  3047. #endif
  3048. case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
  3049. ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
  3050. break;
  3051. case MBEDTLS_SSL_SERVER_FINISHED:
  3052. ret = mbedtls_ssl_parse_finished(ssl);
  3053. break;
  3054. case MBEDTLS_SSL_FLUSH_BUFFERS:
  3055. MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
  3056. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  3057. break;
  3058. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  3059. mbedtls_ssl_handshake_wrapup(ssl);
  3060. break;
  3061. default:
  3062. MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
  3063. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  3064. }
  3065. return ret;
  3066. }
  3067. #endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_2 */