ssl_tls12_client.c 127 KB

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