ssl_tls12_server.c 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341
  1. /*
  2. * TLS server-side functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
  21. #include "mbedtls/platform.h"
  22. #include "mbedtls/ssl.h"
  23. #include "ssl_misc.h"
  24. #include "mbedtls/debug.h"
  25. #include "mbedtls/error.h"
  26. #include "mbedtls/platform_util.h"
  27. #include "constant_time_internal.h"
  28. #include "mbedtls/constant_time.h"
  29. #include "hash_info.h"
  30. #include <string.h>
  31. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  32. #define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
  33. psa_to_ssl_errors, \
  34. psa_generic_status_to_mbedtls)
  35. #endif
  36. #if defined(MBEDTLS_ECP_C)
  37. #include "mbedtls/ecp.h"
  38. #endif
  39. #if defined(MBEDTLS_HAVE_TIME)
  40. #include "mbedtls/platform_time.h"
  41. #endif
  42. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  43. int mbedtls_ssl_set_client_transport_id(mbedtls_ssl_context *ssl,
  44. const unsigned char *info,
  45. size_t ilen)
  46. {
  47. if (ssl->conf->endpoint != MBEDTLS_SSL_IS_SERVER) {
  48. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  49. }
  50. mbedtls_free(ssl->cli_id);
  51. if ((ssl->cli_id = mbedtls_calloc(1, ilen)) == NULL) {
  52. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  53. }
  54. memcpy(ssl->cli_id, info, ilen);
  55. ssl->cli_id_len = ilen;
  56. return 0;
  57. }
  58. void mbedtls_ssl_conf_dtls_cookies(mbedtls_ssl_config *conf,
  59. mbedtls_ssl_cookie_write_t *f_cookie_write,
  60. mbedtls_ssl_cookie_check_t *f_cookie_check,
  61. void *p_cookie)
  62. {
  63. conf->f_cookie_write = f_cookie_write;
  64. conf->f_cookie_check = f_cookie_check;
  65. conf->p_cookie = p_cookie;
  66. }
  67. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  68. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  69. MBEDTLS_CHECK_RETURN_CRITICAL
  70. static int ssl_conf_has_psk_or_cb(mbedtls_ssl_config const *conf)
  71. {
  72. if (conf->f_psk != NULL) {
  73. return 1;
  74. }
  75. if (conf->psk_identity_len == 0 || conf->psk_identity == NULL) {
  76. return 0;
  77. }
  78. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  79. if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
  80. return 1;
  81. }
  82. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  83. if (conf->psk != NULL && conf->psk_len != 0) {
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  89. MBEDTLS_CHECK_RETURN_CRITICAL
  90. static int ssl_parse_renegotiation_info(mbedtls_ssl_context *ssl,
  91. const unsigned char *buf,
  92. size_t len)
  93. {
  94. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  95. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  96. /* Check verify-data in constant-time. The length OTOH is no secret */
  97. if (len != 1 + ssl->verify_data_len ||
  98. buf[0] != ssl->verify_data_len ||
  99. mbedtls_ct_memcmp(buf + 1, ssl->peer_verify_data,
  100. ssl->verify_data_len) != 0) {
  101. MBEDTLS_SSL_DEBUG_MSG(1, ("non-matching renegotiation info"));
  102. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  103. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  104. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  105. }
  106. } else
  107. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  108. {
  109. if (len != 1 || buf[0] != 0x0) {
  110. MBEDTLS_SSL_DEBUG_MSG(1, ("non-zero length renegotiation info"));
  111. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  112. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  113. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  114. }
  115. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  116. }
  117. return 0;
  118. }
  119. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  120. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  121. /*
  122. * Function for parsing a supported groups (TLS 1.3) or supported elliptic
  123. * curves (TLS 1.2) extension.
  124. *
  125. * The "extension_data" field of a supported groups extension contains a
  126. * "NamedGroupList" value (TLS 1.3 RFC8446):
  127. * enum {
  128. * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
  129. * x25519(0x001D), x448(0x001E),
  130. * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
  131. * ffdhe6144(0x0103), ffdhe8192(0x0104),
  132. * ffdhe_private_use(0x01FC..0x01FF),
  133. * ecdhe_private_use(0xFE00..0xFEFF),
  134. * (0xFFFF)
  135. * } NamedGroup;
  136. * struct {
  137. * NamedGroup named_group_list<2..2^16-1>;
  138. * } NamedGroupList;
  139. *
  140. * The "extension_data" field of a supported elliptic curves extension contains
  141. * a "NamedCurveList" value (TLS 1.2 RFC 8422):
  142. * enum {
  143. * deprecated(1..22),
  144. * secp256r1 (23), secp384r1 (24), secp521r1 (25),
  145. * x25519(29), x448(30),
  146. * reserved (0xFE00..0xFEFF),
  147. * deprecated(0xFF01..0xFF02),
  148. * (0xFFFF)
  149. * } NamedCurve;
  150. * struct {
  151. * NamedCurve named_curve_list<2..2^16-1>
  152. * } NamedCurveList;
  153. *
  154. * The TLS 1.3 supported groups extension was defined to be a compatible
  155. * generalization of the TLS 1.2 supported elliptic curves extension. They both
  156. * share the same extension identifier.
  157. *
  158. * DHE groups are not supported yet.
  159. */
  160. MBEDTLS_CHECK_RETURN_CRITICAL
  161. static int ssl_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
  162. const unsigned char *buf,
  163. size_t len)
  164. {
  165. size_t list_size, our_size;
  166. const unsigned char *p;
  167. uint16_t *curves_tls_id;
  168. if (len < 2) {
  169. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  170. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  171. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  172. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  173. }
  174. list_size = ((buf[0] << 8) | (buf[1]));
  175. if (list_size + 2 != len ||
  176. list_size % 2 != 0) {
  177. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  178. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  179. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  180. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  181. }
  182. /* Should never happen unless client duplicates the extension */
  183. if (ssl->handshake->curves_tls_id != NULL) {
  184. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  185. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  186. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  187. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  188. }
  189. /* Don't allow our peer to make us allocate too much memory,
  190. * and leave room for a final 0 */
  191. our_size = list_size / 2 + 1;
  192. if (our_size > MBEDTLS_ECP_DP_MAX) {
  193. our_size = MBEDTLS_ECP_DP_MAX;
  194. }
  195. if ((curves_tls_id = mbedtls_calloc(our_size,
  196. sizeof(*curves_tls_id))) == NULL) {
  197. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  198. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
  199. return MBEDTLS_ERR_SSL_ALLOC_FAILED;
  200. }
  201. ssl->handshake->curves_tls_id = curves_tls_id;
  202. p = buf + 2;
  203. while (list_size > 0 && our_size > 1) {
  204. uint16_t curr_tls_id = MBEDTLS_GET_UINT16_BE(p, 0);
  205. if (mbedtls_ssl_get_ecp_group_id_from_tls_id(curr_tls_id) !=
  206. MBEDTLS_ECP_DP_NONE) {
  207. *curves_tls_id++ = curr_tls_id;
  208. our_size--;
  209. }
  210. list_size -= 2;
  211. p += 2;
  212. }
  213. return 0;
  214. }
  215. MBEDTLS_CHECK_RETURN_CRITICAL
  216. static int ssl_parse_supported_point_formats(mbedtls_ssl_context *ssl,
  217. const unsigned char *buf,
  218. size_t len)
  219. {
  220. size_t list_size;
  221. const unsigned char *p;
  222. if (len == 0 || (size_t) (buf[0] + 1) != len) {
  223. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  224. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  225. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  226. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  227. }
  228. list_size = buf[0];
  229. p = buf + 1;
  230. while (list_size > 0) {
  231. if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
  232. p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
  233. #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
  234. (defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C))
  235. ssl->handshake->ecdh_ctx.point_format = p[0];
  236. #endif /* !MBEDTLS_USE_PSA_CRYPTO &&
  237. ( MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ) */
  238. #if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
  239. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  240. mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
  241. p[0]);
  242. #endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  243. MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
  244. return 0;
  245. }
  246. list_size--;
  247. p++;
  248. }
  249. return 0;
  250. }
  251. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  252. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  253. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  254. MBEDTLS_CHECK_RETURN_CRITICAL
  255. static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
  256. const unsigned char *buf,
  257. size_t len)
  258. {
  259. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  260. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  261. if (ssl->handshake->psa_pake_ctx_is_ok != 1)
  262. #else
  263. if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
  264. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  265. {
  266. MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
  267. return 0;
  268. }
  269. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  270. if ((ret = mbedtls_psa_ecjpake_read_round(
  271. &ssl->handshake->psa_pake_ctx, buf, len,
  272. MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
  273. psa_destroy_key(ssl->handshake->psa_pake_password);
  274. psa_pake_abort(&ssl->handshake->psa_pake_ctx);
  275. MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round one", ret);
  276. mbedtls_ssl_send_alert_message(
  277. ssl,
  278. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  279. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  280. return ret;
  281. }
  282. #else
  283. if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
  284. buf, len)) != 0) {
  285. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
  286. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  287. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  288. return ret;
  289. }
  290. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  291. /* Only mark the extension as OK when we're sure it is */
  292. ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
  293. return 0;
  294. }
  295. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  296. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  297. MBEDTLS_CHECK_RETURN_CRITICAL
  298. static int ssl_parse_max_fragment_length_ext(mbedtls_ssl_context *ssl,
  299. const unsigned char *buf,
  300. size_t len)
  301. {
  302. if (len != 1 || buf[0] >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID) {
  303. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  304. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  305. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  306. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  307. }
  308. ssl->session_negotiate->mfl_code = buf[0];
  309. return 0;
  310. }
  311. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  312. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  313. MBEDTLS_CHECK_RETURN_CRITICAL
  314. static int ssl_parse_cid_ext(mbedtls_ssl_context *ssl,
  315. const unsigned char *buf,
  316. size_t len)
  317. {
  318. size_t peer_cid_len;
  319. /* CID extension only makes sense in DTLS */
  320. if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  321. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  322. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  323. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  324. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  325. }
  326. /*
  327. * struct {
  328. * opaque cid<0..2^8-1>;
  329. * } ConnectionId;
  330. */
  331. if (len < 1) {
  332. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  333. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  334. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  335. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  336. }
  337. peer_cid_len = *buf++;
  338. len--;
  339. if (len != peer_cid_len) {
  340. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  341. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  342. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  343. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  344. }
  345. /* Ignore CID if the user has disabled its use. */
  346. if (ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
  347. /* Leave ssl->handshake->cid_in_use in its default
  348. * value of MBEDTLS_SSL_CID_DISABLED. */
  349. MBEDTLS_SSL_DEBUG_MSG(3, ("Client sent CID extension, but CID disabled"));
  350. return 0;
  351. }
  352. if (peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX) {
  353. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  354. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  355. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
  356. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  357. }
  358. ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
  359. ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
  360. memcpy(ssl->handshake->peer_cid, buf, peer_cid_len);
  361. MBEDTLS_SSL_DEBUG_MSG(3, ("Use of CID extension negotiated"));
  362. MBEDTLS_SSL_DEBUG_BUF(3, "Client CID", buf, peer_cid_len);
  363. return 0;
  364. }
  365. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  366. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  367. MBEDTLS_CHECK_RETURN_CRITICAL
  368. static int ssl_parse_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
  369. const unsigned char *buf,
  370. size_t len)
  371. {
  372. if (len != 0) {
  373. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  374. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  375. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  376. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  377. }
  378. ((void) buf);
  379. if (ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) {
  380. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
  381. }
  382. return 0;
  383. }
  384. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  385. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  386. MBEDTLS_CHECK_RETURN_CRITICAL
  387. static int ssl_parse_extended_ms_ext(mbedtls_ssl_context *ssl,
  388. const unsigned char *buf,
  389. size_t len)
  390. {
  391. if (len != 0) {
  392. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  393. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  394. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  395. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  396. }
  397. ((void) buf);
  398. if (ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
  399. ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  400. }
  401. return 0;
  402. }
  403. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  404. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  405. MBEDTLS_CHECK_RETURN_CRITICAL
  406. static int ssl_parse_session_ticket_ext(mbedtls_ssl_context *ssl,
  407. unsigned char *buf,
  408. size_t len)
  409. {
  410. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  411. mbedtls_ssl_session session;
  412. mbedtls_ssl_session_init(&session);
  413. if (ssl->conf->f_ticket_parse == NULL ||
  414. ssl->conf->f_ticket_write == NULL) {
  415. return 0;
  416. }
  417. /* Remember the client asked us to send a new ticket */
  418. ssl->handshake->new_session_ticket = 1;
  419. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket length: %" MBEDTLS_PRINTF_SIZET, len));
  420. if (len == 0) {
  421. return 0;
  422. }
  423. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  424. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  425. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket rejected: renegotiating"));
  426. return 0;
  427. }
  428. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  429. /*
  430. * Failures are ok: just ignore the ticket and proceed.
  431. */
  432. if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket, &session,
  433. buf, len)) != 0) {
  434. mbedtls_ssl_session_free(&session);
  435. if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
  436. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
  437. } else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
  438. MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
  439. } else {
  440. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_parse", ret);
  441. }
  442. return 0;
  443. }
  444. /*
  445. * Keep the session ID sent by the client, since we MUST send it back to
  446. * inform them we're accepting the ticket (RFC 5077 section 3.4)
  447. */
  448. session.id_len = ssl->session_negotiate->id_len;
  449. memcpy(&session.id, ssl->session_negotiate->id, session.id_len);
  450. mbedtls_ssl_session_free(ssl->session_negotiate);
  451. memcpy(ssl->session_negotiate, &session, sizeof(mbedtls_ssl_session));
  452. /* Zeroize instead of free as we copied the content */
  453. mbedtls_platform_zeroize(&session, sizeof(mbedtls_ssl_session));
  454. MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from ticket"));
  455. ssl->handshake->resume = 1;
  456. /* Don't send a new ticket after all, this one is OK */
  457. ssl->handshake->new_session_ticket = 0;
  458. return 0;
  459. }
  460. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  461. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  462. MBEDTLS_CHECK_RETURN_CRITICAL
  463. static int ssl_parse_use_srtp_ext(mbedtls_ssl_context *ssl,
  464. const unsigned char *buf,
  465. size_t len)
  466. {
  467. mbedtls_ssl_srtp_profile client_protection = MBEDTLS_TLS_SRTP_UNSET;
  468. size_t i, j;
  469. size_t profile_length;
  470. uint16_t mki_length;
  471. /*! 2 bytes for profile length and 1 byte for mki len */
  472. const size_t size_of_lengths = 3;
  473. /* If use_srtp is not configured, just ignore the extension */
  474. if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
  475. (ssl->conf->dtls_srtp_profile_list == NULL) ||
  476. (ssl->conf->dtls_srtp_profile_list_len == 0)) {
  477. return 0;
  478. }
  479. /* RFC5764 section 4.1.1
  480. * uint8 SRTPProtectionProfile[2];
  481. *
  482. * struct {
  483. * SRTPProtectionProfiles SRTPProtectionProfiles;
  484. * opaque srtp_mki<0..255>;
  485. * } UseSRTPData;
  486. * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
  487. */
  488. /*
  489. * Min length is 5: at least one protection profile(2 bytes)
  490. * and length(2 bytes) + srtp_mki length(1 byte)
  491. * Check here that we have at least 2 bytes of protection profiles length
  492. * and one of srtp_mki length
  493. */
  494. if (len < size_of_lengths) {
  495. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  496. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  497. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  498. }
  499. ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
  500. /* first 2 bytes are protection profile length(in bytes) */
  501. profile_length = (buf[0] << 8) | buf[1];
  502. buf += 2;
  503. /* The profile length cannot be bigger than input buffer size - lengths fields */
  504. if (profile_length > len - size_of_lengths ||
  505. profile_length % 2 != 0) { /* profiles are 2 bytes long, so the length must be even */
  506. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  507. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  508. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  509. }
  510. /*
  511. * parse the extension list values are defined in
  512. * http://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
  513. */
  514. for (j = 0; j < profile_length; j += 2) {
  515. uint16_t protection_profile_value = buf[j] << 8 | buf[j + 1];
  516. client_protection = mbedtls_ssl_check_srtp_profile_value(protection_profile_value);
  517. if (client_protection != MBEDTLS_TLS_SRTP_UNSET) {
  518. MBEDTLS_SSL_DEBUG_MSG(3, ("found srtp profile: %s",
  519. mbedtls_ssl_get_srtp_profile_as_string(
  520. client_protection)));
  521. } else {
  522. continue;
  523. }
  524. /* check if suggested profile is in our list */
  525. for (i = 0; i < ssl->conf->dtls_srtp_profile_list_len; i++) {
  526. if (client_protection == ssl->conf->dtls_srtp_profile_list[i]) {
  527. ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
  528. MBEDTLS_SSL_DEBUG_MSG(3, ("selected srtp profile: %s",
  529. mbedtls_ssl_get_srtp_profile_as_string(
  530. client_protection)));
  531. break;
  532. }
  533. }
  534. if (ssl->dtls_srtp_info.chosen_dtls_srtp_profile != MBEDTLS_TLS_SRTP_UNSET) {
  535. break;
  536. }
  537. }
  538. buf += profile_length; /* buf points to the mki length */
  539. mki_length = *buf;
  540. buf++;
  541. if (mki_length > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH ||
  542. mki_length + profile_length + size_of_lengths != len) {
  543. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  544. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  545. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  546. }
  547. /* Parse the mki only if present and mki is supported locally */
  548. if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED &&
  549. mki_length > 0) {
  550. ssl->dtls_srtp_info.mki_len = mki_length;
  551. memcpy(ssl->dtls_srtp_info.mki_value, buf, mki_length);
  552. MBEDTLS_SSL_DEBUG_BUF(3, "using mki", ssl->dtls_srtp_info.mki_value,
  553. ssl->dtls_srtp_info.mki_len);
  554. }
  555. return 0;
  556. }
  557. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  558. /*
  559. * Auxiliary functions for ServerHello parsing and related actions
  560. */
  561. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  562. /*
  563. * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
  564. */
  565. #if defined(MBEDTLS_ECDSA_C)
  566. MBEDTLS_CHECK_RETURN_CRITICAL
  567. static int ssl_check_key_curve(mbedtls_pk_context *pk,
  568. uint16_t *curves_tls_id)
  569. {
  570. uint16_t *curr_tls_id = curves_tls_id;
  571. mbedtls_ecp_group_id grp_id = mbedtls_pk_ec(*pk)->grp.id;
  572. mbedtls_ecp_group_id curr_grp_id;
  573. while (*curr_tls_id != 0) {
  574. curr_grp_id = mbedtls_ssl_get_ecp_group_id_from_tls_id(*curr_tls_id);
  575. if (curr_grp_id == grp_id) {
  576. return 0;
  577. }
  578. curr_tls_id++;
  579. }
  580. return -1;
  581. }
  582. #endif /* MBEDTLS_ECDSA_C */
  583. /*
  584. * Try picking a certificate for this ciphersuite,
  585. * return 0 on success and -1 on failure.
  586. */
  587. MBEDTLS_CHECK_RETURN_CRITICAL
  588. static int ssl_pick_cert(mbedtls_ssl_context *ssl,
  589. const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
  590. {
  591. mbedtls_ssl_key_cert *cur, *list;
  592. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  593. psa_algorithm_t pk_alg =
  594. mbedtls_ssl_get_ciphersuite_sig_pk_psa_alg(ciphersuite_info);
  595. psa_key_usage_t pk_usage =
  596. mbedtls_ssl_get_ciphersuite_sig_pk_psa_usage(ciphersuite_info);
  597. #else
  598. mbedtls_pk_type_t pk_alg =
  599. mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info);
  600. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  601. uint32_t flags;
  602. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  603. if (ssl->handshake->sni_key_cert != NULL) {
  604. list = ssl->handshake->sni_key_cert;
  605. } else
  606. #endif
  607. list = ssl->conf->key_cert;
  608. int pk_alg_is_none = 0;
  609. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  610. pk_alg_is_none = (pk_alg == PSA_ALG_NONE);
  611. #else
  612. pk_alg_is_none = (pk_alg == MBEDTLS_PK_NONE);
  613. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  614. if (pk_alg_is_none) {
  615. return 0;
  616. }
  617. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite requires certificate"));
  618. if (list == NULL) {
  619. MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
  620. return -1;
  621. }
  622. for (cur = list; cur != NULL; cur = cur->next) {
  623. flags = 0;
  624. MBEDTLS_SSL_DEBUG_CRT(3, "candidate certificate chain, certificate",
  625. cur->cert);
  626. int key_type_matches = 0;
  627. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  628. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  629. key_type_matches = ((ssl->conf->f_async_sign_start != NULL ||
  630. ssl->conf->f_async_decrypt_start != NULL ||
  631. mbedtls_pk_can_do_ext(cur->key, pk_alg, pk_usage)) &&
  632. mbedtls_pk_can_do_ext(&cur->cert->pk, pk_alg, pk_usage));
  633. #else
  634. key_type_matches = (
  635. mbedtls_pk_can_do_ext(cur->key, pk_alg, pk_usage));
  636. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  637. #else
  638. key_type_matches = mbedtls_pk_can_do(&cur->cert->pk, pk_alg);
  639. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  640. if (!key_type_matches) {
  641. MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: key type"));
  642. continue;
  643. }
  644. /*
  645. * This avoids sending the client a cert it'll reject based on
  646. * keyUsage or other extensions.
  647. *
  648. * It also allows the user to provision different certificates for
  649. * different uses based on keyUsage, eg if they want to avoid signing
  650. * and decrypting with the same RSA key.
  651. */
  652. if (mbedtls_ssl_check_cert_usage(cur->cert, ciphersuite_info,
  653. MBEDTLS_SSL_IS_SERVER, &flags) != 0) {
  654. MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
  655. "(extended) key usage extension"));
  656. continue;
  657. }
  658. #if defined(MBEDTLS_ECDSA_C)
  659. if (pk_alg == MBEDTLS_PK_ECDSA &&
  660. ssl_check_key_curve(&cur->cert->pk,
  661. ssl->handshake->curves_tls_id) != 0) {
  662. MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: elliptic curve"));
  663. continue;
  664. }
  665. #endif
  666. /* If we get there, we got a winner */
  667. break;
  668. }
  669. /* Do not update ssl->handshake->key_cert unless there is a match */
  670. if (cur != NULL) {
  671. ssl->handshake->key_cert = cur;
  672. MBEDTLS_SSL_DEBUG_CRT(3, "selected certificate chain, certificate",
  673. ssl->handshake->key_cert->cert);
  674. return 0;
  675. }
  676. return -1;
  677. }
  678. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  679. /*
  680. * Check if a given ciphersuite is suitable for use with our config/keys/etc
  681. * Sets ciphersuite_info only if the suite matches.
  682. */
  683. MBEDTLS_CHECK_RETURN_CRITICAL
  684. static int ssl_ciphersuite_match(mbedtls_ssl_context *ssl, int suite_id,
  685. const mbedtls_ssl_ciphersuite_t **ciphersuite_info)
  686. {
  687. const mbedtls_ssl_ciphersuite_t *suite_info;
  688. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  689. mbedtls_pk_type_t sig_type;
  690. #endif
  691. suite_info = mbedtls_ssl_ciphersuite_from_id(suite_id);
  692. if (suite_info == NULL) {
  693. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  694. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  695. }
  696. MBEDTLS_SSL_DEBUG_MSG(3, ("trying ciphersuite: %#04x (%s)",
  697. (unsigned int) suite_id, suite_info->name));
  698. if (suite_info->min_tls_version > ssl->tls_version ||
  699. suite_info->max_tls_version < ssl->tls_version) {
  700. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: version"));
  701. return 0;
  702. }
  703. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  704. if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
  705. (ssl->handshake->cli_exts & MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK) == 0) {
  706. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: ecjpake "
  707. "not configured or ext missing"));
  708. return 0;
  709. }
  710. #endif
  711. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
  712. if (mbedtls_ssl_ciphersuite_uses_ec(suite_info) &&
  713. (ssl->handshake->curves_tls_id == NULL ||
  714. ssl->handshake->curves_tls_id[0] == 0)) {
  715. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: "
  716. "no common elliptic curve"));
  717. return 0;
  718. }
  719. #endif
  720. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  721. /* If the ciphersuite requires a pre-shared key and we don't
  722. * have one, skip it now rather than failing later */
  723. if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
  724. ssl_conf_has_psk_or_cb(ssl->conf) == 0) {
  725. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: no pre-shared key"));
  726. return 0;
  727. }
  728. #endif
  729. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  730. /*
  731. * Final check: if ciphersuite requires us to have a
  732. * certificate/key of a particular type:
  733. * - select the appropriate certificate if we have one, or
  734. * - try the next ciphersuite if we don't
  735. * This must be done last since we modify the key_cert list.
  736. */
  737. if (ssl_pick_cert(ssl, suite_info) != 0) {
  738. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: "
  739. "no suitable certificate"));
  740. return 0;
  741. }
  742. #endif
  743. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  744. /* If the ciphersuite requires signing, check whether
  745. * a suitable hash algorithm is present. */
  746. sig_type = mbedtls_ssl_get_ciphersuite_sig_alg(suite_info);
  747. if (sig_type != MBEDTLS_PK_NONE &&
  748. mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
  749. ssl, mbedtls_ssl_sig_from_pk_alg(sig_type)) == MBEDTLS_SSL_HASH_NONE) {
  750. MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite mismatch: no suitable hash algorithm "
  751. "for signature algorithm %u", (unsigned) sig_type));
  752. return 0;
  753. }
  754. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  755. *ciphersuite_info = suite_info;
  756. return 0;
  757. }
  758. /* This function doesn't alert on errors that happen early during
  759. ClientHello parsing because they might indicate that the client is
  760. not talking SSL/TLS at all and would not understand our alert. */
  761. MBEDTLS_CHECK_RETURN_CRITICAL
  762. static int ssl_parse_client_hello(mbedtls_ssl_context *ssl)
  763. {
  764. int ret, got_common_suite;
  765. size_t i, j;
  766. size_t ciph_offset, comp_offset, ext_offset;
  767. size_t msg_len, ciph_len, sess_len, comp_len, ext_len;
  768. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  769. size_t cookie_offset, cookie_len;
  770. #endif
  771. unsigned char *buf, *p, *ext;
  772. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  773. int renegotiation_info_seen = 0;
  774. #endif
  775. int handshake_failure = 0;
  776. const int *ciphersuites;
  777. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  778. /* If there is no signature-algorithm extension present,
  779. * we need to fall back to the default values for allowed
  780. * signature-hash pairs. */
  781. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  782. int sig_hash_alg_ext_present = 0;
  783. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  784. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
  785. int renegotiating;
  786. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  787. read_record_header:
  788. #endif
  789. /*
  790. * If renegotiating, then the input was read with mbedtls_ssl_read_record(),
  791. * otherwise read it ourselves manually in order to support SSLv2
  792. * ClientHello, which doesn't use the same record layer format.
  793. */
  794. renegotiating = 0;
  795. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  796. renegotiating = (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE);
  797. #endif
  798. if (!renegotiating) {
  799. if ((ret = mbedtls_ssl_fetch_input(ssl, 5)) != 0) {
  800. /* No alert on a read error. */
  801. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
  802. return ret;
  803. }
  804. }
  805. buf = ssl->in_hdr;
  806. MBEDTLS_SSL_DEBUG_BUF(4, "record header", buf, mbedtls_ssl_in_hdr_len(ssl));
  807. /*
  808. * TLS Client Hello
  809. *
  810. * Record layer:
  811. * 0 . 0 message type
  812. * 1 . 2 protocol version
  813. * 3 . 11 DTLS: epoch + record sequence number
  814. * 3 . 4 message length
  815. */
  816. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, message type: %d",
  817. buf[0]));
  818. if (buf[0] != MBEDTLS_SSL_MSG_HANDSHAKE) {
  819. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  820. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  821. }
  822. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, message len.: %d",
  823. (ssl->in_len[0] << 8) | ssl->in_len[1]));
  824. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, protocol version: [%d:%d]",
  825. buf[1], buf[2]));
  826. /* For DTLS if this is the initial handshake, remember the client sequence
  827. * number to use it in our next message (RFC 6347 4.2.1) */
  828. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  829. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM
  830. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  831. && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
  832. #endif
  833. ) {
  834. /* Epoch should be 0 for initial handshakes */
  835. if (ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0) {
  836. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  837. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  838. }
  839. memcpy(&ssl->cur_out_ctr[2], ssl->in_ctr + 2,
  840. sizeof(ssl->cur_out_ctr) - 2);
  841. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  842. if (mbedtls_ssl_dtls_replay_check(ssl) != 0) {
  843. MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record, discarding"));
  844. ssl->next_record_offset = 0;
  845. ssl->in_left = 0;
  846. goto read_record_header;
  847. }
  848. /* No MAC to check yet, so we can update right now */
  849. mbedtls_ssl_dtls_replay_update(ssl);
  850. #endif
  851. }
  852. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  853. msg_len = (ssl->in_len[0] << 8) | ssl->in_len[1];
  854. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  855. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  856. /* Set by mbedtls_ssl_read_record() */
  857. msg_len = ssl->in_hslen;
  858. } else
  859. #endif
  860. {
  861. if (msg_len > MBEDTLS_SSL_IN_CONTENT_LEN) {
  862. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  863. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  864. }
  865. if ((ret = mbedtls_ssl_fetch_input(ssl,
  866. mbedtls_ssl_in_hdr_len(ssl) + msg_len)) != 0) {
  867. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
  868. return ret;
  869. }
  870. /* Done reading this record, get ready for the next one */
  871. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  872. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  873. ssl->next_record_offset = msg_len + mbedtls_ssl_in_hdr_len(ssl);
  874. } else
  875. #endif
  876. ssl->in_left = 0;
  877. }
  878. buf = ssl->in_msg;
  879. MBEDTLS_SSL_DEBUG_BUF(4, "record contents", buf, msg_len);
  880. ret = ssl->handshake->update_checksum(ssl, buf, msg_len);
  881. if (0 != ret) {
  882. MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
  883. return ret;
  884. }
  885. /*
  886. * Handshake layer:
  887. * 0 . 0 handshake type
  888. * 1 . 3 handshake length
  889. * 4 . 5 DTLS only: message sequence number
  890. * 6 . 8 DTLS only: fragment offset
  891. * 9 . 11 DTLS only: fragment length
  892. */
  893. if (msg_len < mbedtls_ssl_hs_hdr_len(ssl)) {
  894. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  895. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  896. }
  897. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, handshake type: %d", buf[0]));
  898. if (buf[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) {
  899. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  900. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  901. }
  902. {
  903. size_t handshake_len = MBEDTLS_GET_UINT24_BE(buf, 1);
  904. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, handshake len.: %u",
  905. (unsigned) handshake_len));
  906. /* The record layer has a record size limit of 2^14 - 1 and
  907. * fragmentation is not supported, so buf[1] should be zero. */
  908. if (buf[1] != 0) {
  909. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message: %u != 0",
  910. (unsigned) buf[1]));
  911. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  912. }
  913. /* We don't support fragmentation of ClientHello (yet?) */
  914. if (msg_len != mbedtls_ssl_hs_hdr_len(ssl) + handshake_len) {
  915. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message: %u != %u + %u",
  916. (unsigned) msg_len,
  917. (unsigned) mbedtls_ssl_hs_hdr_len(ssl),
  918. (unsigned) handshake_len));
  919. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  920. }
  921. }
  922. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  923. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  924. /*
  925. * Copy the client's handshake message_seq on initial handshakes,
  926. * check sequence number on renego.
  927. */
  928. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  929. if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
  930. /* This couldn't be done in ssl_prepare_handshake_record() */
  931. unsigned int cli_msg_seq = (ssl->in_msg[4] << 8) |
  932. ssl->in_msg[5];
  933. if (cli_msg_seq != ssl->handshake->in_msg_seq) {
  934. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message_seq: "
  935. "%u (expected %u)", cli_msg_seq,
  936. ssl->handshake->in_msg_seq));
  937. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  938. }
  939. ssl->handshake->in_msg_seq++;
  940. } else
  941. #endif
  942. {
  943. unsigned int cli_msg_seq = (ssl->in_msg[4] << 8) |
  944. ssl->in_msg[5];
  945. ssl->handshake->out_msg_seq = cli_msg_seq;
  946. ssl->handshake->in_msg_seq = cli_msg_seq + 1;
  947. }
  948. {
  949. /*
  950. * For now we don't support fragmentation, so make sure
  951. * fragment_offset == 0 and fragment_length == length
  952. */
  953. size_t fragment_offset, fragment_length, length;
  954. fragment_offset = MBEDTLS_GET_UINT24_BE(ssl->in_msg, 6);
  955. fragment_length = MBEDTLS_GET_UINT24_BE(ssl->in_msg, 9);
  956. length = MBEDTLS_GET_UINT24_BE(ssl->in_msg, 1);
  957. MBEDTLS_SSL_DEBUG_MSG(
  958. 4, ("fragment_offset=%u fragment_length=%u length=%u",
  959. (unsigned) fragment_offset, (unsigned) fragment_length,
  960. (unsigned) length));
  961. if (fragment_offset != 0 || length != fragment_length) {
  962. MBEDTLS_SSL_DEBUG_MSG(1, ("ClientHello fragmentation not supported"));
  963. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  964. }
  965. }
  966. }
  967. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  968. buf += mbedtls_ssl_hs_hdr_len(ssl);
  969. msg_len -= mbedtls_ssl_hs_hdr_len(ssl);
  970. /*
  971. * ClientHello layer:
  972. * 0 . 1 protocol version
  973. * 2 . 33 random bytes (starting with 4 bytes of Unix time)
  974. * 34 . 35 session id length (1 byte)
  975. * 35 . 34+x session id
  976. * 35+x . 35+x DTLS only: cookie length (1 byte)
  977. * 36+x . .. DTLS only: cookie
  978. * .. . .. ciphersuite list length (2 bytes)
  979. * .. . .. ciphersuite list
  980. * .. . .. compression alg. list length (1 byte)
  981. * .. . .. compression alg. list
  982. * .. . .. extensions length (2 bytes, optional)
  983. * .. . .. extensions (optional)
  984. */
  985. /*
  986. * Minimal length (with everything empty and extensions omitted) is
  987. * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
  988. * read at least up to session id length without worrying.
  989. */
  990. if (msg_len < 38) {
  991. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  992. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  993. }
  994. /*
  995. * Check and save the protocol version
  996. */
  997. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, version", buf, 2);
  998. ssl->tls_version = mbedtls_ssl_read_version(buf, ssl->conf->transport);
  999. ssl->session_negotiate->tls_version = ssl->tls_version;
  1000. if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
  1001. MBEDTLS_SSL_DEBUG_MSG(1, ("server only supports TLS 1.2"));
  1002. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1003. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION);
  1004. return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
  1005. }
  1006. /*
  1007. * Save client random (inc. Unix time)
  1008. */
  1009. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes", buf + 2, 32);
  1010. memcpy(ssl->handshake->randbytes, buf + 2, 32);
  1011. /*
  1012. * Check the session ID length and save session ID
  1013. */
  1014. sess_len = buf[34];
  1015. if (sess_len > sizeof(ssl->session_negotiate->id) ||
  1016. sess_len + 34 + 2 > msg_len) { /* 2 for cipherlist length field */
  1017. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1018. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1019. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1020. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1021. }
  1022. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id", buf + 35, sess_len);
  1023. ssl->session_negotiate->id_len = sess_len;
  1024. memset(ssl->session_negotiate->id, 0,
  1025. sizeof(ssl->session_negotiate->id));
  1026. memcpy(ssl->session_negotiate->id, buf + 35,
  1027. ssl->session_negotiate->id_len);
  1028. /*
  1029. * Check the cookie length and content
  1030. */
  1031. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1032. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1033. cookie_offset = 35 + sess_len;
  1034. cookie_len = buf[cookie_offset];
  1035. if (cookie_offset + 1 + cookie_len + 2 > msg_len) {
  1036. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1037. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1038. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1039. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1040. }
  1041. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
  1042. buf + cookie_offset + 1, cookie_len);
  1043. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  1044. if (ssl->conf->f_cookie_check != NULL
  1045. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1046. && ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE
  1047. #endif
  1048. ) {
  1049. if (ssl->conf->f_cookie_check(ssl->conf->p_cookie,
  1050. buf + cookie_offset + 1, cookie_len,
  1051. ssl->cli_id, ssl->cli_id_len) != 0) {
  1052. MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification failed"));
  1053. ssl->handshake->cookie_verify_result = 1;
  1054. } else {
  1055. MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification passed"));
  1056. ssl->handshake->cookie_verify_result = 0;
  1057. }
  1058. } else
  1059. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  1060. {
  1061. /* We know we didn't send a cookie, so it should be empty */
  1062. if (cookie_len != 0) {
  1063. /* This may be an attacker's probe, so don't send an alert */
  1064. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1065. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1066. }
  1067. MBEDTLS_SSL_DEBUG_MSG(2, ("cookie verification skipped"));
  1068. }
  1069. /*
  1070. * Check the ciphersuitelist length (will be parsed later)
  1071. */
  1072. ciph_offset = cookie_offset + 1 + cookie_len;
  1073. } else
  1074. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1075. ciph_offset = 35 + sess_len;
  1076. ciph_len = (buf[ciph_offset + 0] << 8)
  1077. | (buf[ciph_offset + 1]);
  1078. if (ciph_len < 2 ||
  1079. ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
  1080. (ciph_len % 2) != 0) {
  1081. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1082. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1083. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1084. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1085. }
  1086. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
  1087. buf + ciph_offset + 2, ciph_len);
  1088. /*
  1089. * Check the compression algorithm's length.
  1090. * The list contents are ignored because implementing
  1091. * MBEDTLS_SSL_COMPRESS_NULL is mandatory and is the only
  1092. * option supported by Mbed TLS.
  1093. */
  1094. comp_offset = ciph_offset + 2 + ciph_len;
  1095. comp_len = buf[comp_offset];
  1096. if (comp_len < 1 ||
  1097. comp_len > 16 ||
  1098. comp_len + comp_offset + 1 > msg_len) {
  1099. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1100. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1101. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1102. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1103. }
  1104. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, compression",
  1105. buf + comp_offset + 1, comp_len);
  1106. /*
  1107. * Check the extension length
  1108. */
  1109. ext_offset = comp_offset + 1 + comp_len;
  1110. if (msg_len > ext_offset) {
  1111. if (msg_len < ext_offset + 2) {
  1112. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1113. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1114. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1115. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1116. }
  1117. ext_len = (buf[ext_offset + 0] << 8)
  1118. | (buf[ext_offset + 1]);
  1119. if (msg_len != ext_offset + 2 + ext_len) {
  1120. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1121. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1122. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1123. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1124. }
  1125. } else {
  1126. ext_len = 0;
  1127. }
  1128. ext = buf + ext_offset + 2;
  1129. MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", ext, ext_len);
  1130. while (ext_len != 0) {
  1131. unsigned int ext_id;
  1132. unsigned int ext_size;
  1133. if (ext_len < 4) {
  1134. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1135. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1136. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1137. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1138. }
  1139. ext_id = ((ext[0] << 8) | (ext[1]));
  1140. ext_size = ((ext[2] << 8) | (ext[3]));
  1141. if (ext_size + 4 > ext_len) {
  1142. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  1143. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1144. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
  1145. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  1146. }
  1147. switch (ext_id) {
  1148. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  1149. case MBEDTLS_TLS_EXT_SERVERNAME:
  1150. MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
  1151. ret = mbedtls_ssl_parse_server_name_ext(ssl, ext + 4,
  1152. ext + 4 + ext_size);
  1153. if (ret != 0) {
  1154. return ret;
  1155. }
  1156. break;
  1157. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  1158. case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
  1159. MBEDTLS_SSL_DEBUG_MSG(3, ("found renegotiation extension"));
  1160. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1161. renegotiation_info_seen = 1;
  1162. #endif
  1163. ret = ssl_parse_renegotiation_info(ssl, ext + 4, ext_size);
  1164. if (ret != 0) {
  1165. return ret;
  1166. }
  1167. break;
  1168. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1169. case MBEDTLS_TLS_EXT_SIG_ALG:
  1170. MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
  1171. ret = mbedtls_ssl_parse_sig_alg_ext(ssl, ext + 4, ext + 4 + ext_size);
  1172. if (ret != 0) {
  1173. return ret;
  1174. }
  1175. sig_hash_alg_ext_present = 1;
  1176. break;
  1177. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1178. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  1179. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1180. case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
  1181. MBEDTLS_SSL_DEBUG_MSG(3, ("found supported elliptic curves extension"));
  1182. ret = ssl_parse_supported_groups_ext(ssl, ext + 4, ext_size);
  1183. if (ret != 0) {
  1184. return ret;
  1185. }
  1186. break;
  1187. case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
  1188. MBEDTLS_SSL_DEBUG_MSG(3, ("found supported point formats extension"));
  1189. ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
  1190. ret = ssl_parse_supported_point_formats(ssl, ext + 4, ext_size);
  1191. if (ret != 0) {
  1192. return ret;
  1193. }
  1194. break;
  1195. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  1196. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1197. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1198. case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
  1199. MBEDTLS_SSL_DEBUG_MSG(3, ("found ecjpake kkpp extension"));
  1200. ret = ssl_parse_ecjpake_kkpp(ssl, ext + 4, ext_size);
  1201. if (ret != 0) {
  1202. return ret;
  1203. }
  1204. break;
  1205. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1206. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1207. case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
  1208. MBEDTLS_SSL_DEBUG_MSG(3, ("found max fragment length extension"));
  1209. ret = ssl_parse_max_fragment_length_ext(ssl, ext + 4, ext_size);
  1210. if (ret != 0) {
  1211. return ret;
  1212. }
  1213. break;
  1214. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  1215. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1216. case MBEDTLS_TLS_EXT_CID:
  1217. MBEDTLS_SSL_DEBUG_MSG(3, ("found CID extension"));
  1218. ret = ssl_parse_cid_ext(ssl, ext + 4, ext_size);
  1219. if (ret != 0) {
  1220. return ret;
  1221. }
  1222. break;
  1223. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1224. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1225. case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
  1226. MBEDTLS_SSL_DEBUG_MSG(3, ("found encrypt then mac extension"));
  1227. ret = ssl_parse_encrypt_then_mac_ext(ssl, ext + 4, ext_size);
  1228. if (ret != 0) {
  1229. return ret;
  1230. }
  1231. break;
  1232. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  1233. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1234. case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
  1235. MBEDTLS_SSL_DEBUG_MSG(3, ("found extended master secret extension"));
  1236. ret = ssl_parse_extended_ms_ext(ssl, ext + 4, ext_size);
  1237. if (ret != 0) {
  1238. return ret;
  1239. }
  1240. break;
  1241. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  1242. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1243. case MBEDTLS_TLS_EXT_SESSION_TICKET:
  1244. MBEDTLS_SSL_DEBUG_MSG(3, ("found session ticket extension"));
  1245. ret = ssl_parse_session_ticket_ext(ssl, ext + 4, ext_size);
  1246. if (ret != 0) {
  1247. return ret;
  1248. }
  1249. break;
  1250. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1251. #if defined(MBEDTLS_SSL_ALPN)
  1252. case MBEDTLS_TLS_EXT_ALPN:
  1253. MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
  1254. ret = mbedtls_ssl_parse_alpn_ext(ssl, ext + 4,
  1255. ext + 4 + ext_size);
  1256. if (ret != 0) {
  1257. return ret;
  1258. }
  1259. break;
  1260. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1261. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  1262. case MBEDTLS_TLS_EXT_USE_SRTP:
  1263. MBEDTLS_SSL_DEBUG_MSG(3, ("found use_srtp extension"));
  1264. ret = ssl_parse_use_srtp_ext(ssl, ext + 4, ext_size);
  1265. if (ret != 0) {
  1266. return ret;
  1267. }
  1268. break;
  1269. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  1270. default:
  1271. MBEDTLS_SSL_DEBUG_MSG(3, ("unknown extension found: %u (ignoring)",
  1272. ext_id));
  1273. }
  1274. ext_len -= 4 + ext_size;
  1275. ext += 4 + ext_size;
  1276. }
  1277. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1278. /*
  1279. * Try to fall back to default hash SHA1 if the client
  1280. * hasn't provided any preferred signature-hash combinations.
  1281. */
  1282. if (!sig_hash_alg_ext_present) {
  1283. uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
  1284. const uint16_t default_sig_algs[] = {
  1285. #if defined(MBEDTLS_ECDSA_C)
  1286. MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA,
  1287. MBEDTLS_SSL_HASH_SHA1),
  1288. #endif
  1289. #if defined(MBEDTLS_RSA_C)
  1290. MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA,
  1291. MBEDTLS_SSL_HASH_SHA1),
  1292. #endif
  1293. MBEDTLS_TLS_SIG_NONE
  1294. };
  1295. MBEDTLS_STATIC_ASSERT(sizeof(default_sig_algs) / sizeof(default_sig_algs[0])
  1296. <= MBEDTLS_RECEIVED_SIG_ALGS_SIZE,
  1297. "default_sig_algs is too big");
  1298. memcpy(received_sig_algs, default_sig_algs, sizeof(default_sig_algs));
  1299. }
  1300. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1301. /*
  1302. * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
  1303. */
  1304. for (i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2) {
  1305. if (p[0] == 0 && p[1] == MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO) {
  1306. MBEDTLS_SSL_DEBUG_MSG(3, ("received TLS_EMPTY_RENEGOTIATION_INFO "));
  1307. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1308. if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
  1309. MBEDTLS_SSL_DEBUG_MSG(1, ("received RENEGOTIATION SCSV "
  1310. "during renegotiation"));
  1311. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1312. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1313. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1314. }
  1315. #endif
  1316. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  1317. break;
  1318. }
  1319. }
  1320. /*
  1321. * Renegotiation security checks
  1322. */
  1323. if (ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  1324. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE) {
  1325. MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation, breaking off handshake"));
  1326. handshake_failure = 1;
  1327. }
  1328. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1329. else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1330. ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  1331. renegotiation_info_seen == 0) {
  1332. MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation_info extension missing (secure)"));
  1333. handshake_failure = 1;
  1334. } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1335. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1336. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION) {
  1337. MBEDTLS_SSL_DEBUG_MSG(1, ("legacy renegotiation not allowed"));
  1338. handshake_failure = 1;
  1339. } else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1340. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1341. renegotiation_info_seen == 1) {
  1342. MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation_info extension present (legacy)"));
  1343. handshake_failure = 1;
  1344. }
  1345. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1346. if (handshake_failure == 1) {
  1347. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1348. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1349. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1350. }
  1351. /*
  1352. * Server certification selection (after processing TLS extensions)
  1353. */
  1354. if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
  1355. MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
  1356. return ret;
  1357. }
  1358. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  1359. ssl->handshake->sni_name = NULL;
  1360. ssl->handshake->sni_name_len = 0;
  1361. #endif
  1362. /*
  1363. * Search for a matching ciphersuite
  1364. * (At the end because we need information from the EC-based extensions
  1365. * and certificate from the SNI callback triggered by the SNI extension
  1366. * or certificate from server certificate selection callback.)
  1367. */
  1368. got_common_suite = 0;
  1369. ciphersuites = ssl->conf->ciphersuite_list;
  1370. ciphersuite_info = NULL;
  1371. if (ssl->conf->respect_cli_pref == MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_CLIENT) {
  1372. for (j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2) {
  1373. for (i = 0; ciphersuites[i] != 0; i++) {
  1374. if (MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i]) {
  1375. continue;
  1376. }
  1377. got_common_suite = 1;
  1378. if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
  1379. &ciphersuite_info)) != 0) {
  1380. return ret;
  1381. }
  1382. if (ciphersuite_info != NULL) {
  1383. goto have_ciphersuite;
  1384. }
  1385. }
  1386. }
  1387. } else {
  1388. for (i = 0; ciphersuites[i] != 0; i++) {
  1389. for (j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2) {
  1390. if (MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i]) {
  1391. continue;
  1392. }
  1393. got_common_suite = 1;
  1394. if ((ret = ssl_ciphersuite_match(ssl, ciphersuites[i],
  1395. &ciphersuite_info)) != 0) {
  1396. return ret;
  1397. }
  1398. if (ciphersuite_info != NULL) {
  1399. goto have_ciphersuite;
  1400. }
  1401. }
  1402. }
  1403. }
  1404. if (got_common_suite) {
  1405. MBEDTLS_SSL_DEBUG_MSG(1, ("got ciphersuites in common, "
  1406. "but none of them usable"));
  1407. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1408. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1409. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1410. } else {
  1411. MBEDTLS_SSL_DEBUG_MSG(1, ("got no ciphersuites in common"));
  1412. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1413. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
  1414. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1415. }
  1416. have_ciphersuite:
  1417. MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %s", ciphersuite_info->name));
  1418. ssl->session_negotiate->ciphersuite = ciphersuites[i];
  1419. ssl->handshake->ciphersuite_info = ciphersuite_info;
  1420. ssl->state++;
  1421. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1422. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  1423. mbedtls_ssl_recv_flight_completed(ssl);
  1424. }
  1425. #endif
  1426. /* Debugging-only output for testsuite */
  1427. #if defined(MBEDTLS_DEBUG_C) && \
  1428. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1429. mbedtls_pk_type_t sig_alg = mbedtls_ssl_get_ciphersuite_sig_alg(ciphersuite_info);
  1430. if (sig_alg != MBEDTLS_PK_NONE) {
  1431. unsigned int sig_hash = mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
  1432. ssl, mbedtls_ssl_sig_from_pk_alg(sig_alg));
  1433. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, signature_algorithm ext: %u",
  1434. sig_hash));
  1435. } else {
  1436. MBEDTLS_SSL_DEBUG_MSG(3, ("no hash algorithm for signature algorithm "
  1437. "%u - should not happen", (unsigned) sig_alg));
  1438. }
  1439. #endif
  1440. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
  1441. return 0;
  1442. }
  1443. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1444. static void ssl_write_cid_ext(mbedtls_ssl_context *ssl,
  1445. unsigned char *buf,
  1446. size_t *olen)
  1447. {
  1448. unsigned char *p = buf;
  1449. size_t ext_len;
  1450. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
  1451. *olen = 0;
  1452. /* Skip writing the extension if we don't want to use it or if
  1453. * the client hasn't offered it. */
  1454. if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_DISABLED) {
  1455. return;
  1456. }
  1457. /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
  1458. * which is at most 255, so the increment cannot overflow. */
  1459. if (end < p || (size_t) (end - p) < (unsigned) (ssl->own_cid_len + 5)) {
  1460. MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
  1461. return;
  1462. }
  1463. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding CID extension"));
  1464. /*
  1465. * struct {
  1466. * opaque cid<0..2^8-1>;
  1467. * } ConnectionId;
  1468. */
  1469. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_CID, p, 0);
  1470. p += 2;
  1471. ext_len = (size_t) ssl->own_cid_len + 1;
  1472. MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
  1473. p += 2;
  1474. *p++ = (uint8_t) ssl->own_cid_len;
  1475. memcpy(p, ssl->own_cid, ssl->own_cid_len);
  1476. *olen = ssl->own_cid_len + 5;
  1477. }
  1478. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1479. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
  1480. static void ssl_write_encrypt_then_mac_ext(mbedtls_ssl_context *ssl,
  1481. unsigned char *buf,
  1482. size_t *olen)
  1483. {
  1484. unsigned char *p = buf;
  1485. const mbedtls_ssl_ciphersuite_t *suite = NULL;
  1486. /*
  1487. * RFC 7366: "If a server receives an encrypt-then-MAC request extension
  1488. * from a client and then selects a stream or Authenticated Encryption
  1489. * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
  1490. * encrypt-then-MAC response extension back to the client."
  1491. */
  1492. suite = mbedtls_ssl_ciphersuite_from_id(
  1493. ssl->session_negotiate->ciphersuite);
  1494. if (suite == NULL) {
  1495. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
  1496. } else {
  1497. mbedtls_ssl_mode_t ssl_mode =
  1498. mbedtls_ssl_get_mode_from_ciphersuite(
  1499. ssl->session_negotiate->encrypt_then_mac,
  1500. suite);
  1501. if (ssl_mode != MBEDTLS_SSL_MODE_CBC_ETM) {
  1502. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_DISABLED;
  1503. }
  1504. }
  1505. if (ssl->session_negotiate->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED) {
  1506. *olen = 0;
  1507. return;
  1508. }
  1509. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding encrypt then mac extension"));
  1510. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0);
  1511. p += 2;
  1512. *p++ = 0x00;
  1513. *p++ = 0x00;
  1514. *olen = 4;
  1515. }
  1516. #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
  1517. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1518. static void ssl_write_extended_ms_ext(mbedtls_ssl_context *ssl,
  1519. unsigned char *buf,
  1520. size_t *olen)
  1521. {
  1522. unsigned char *p = buf;
  1523. if (ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED) {
  1524. *olen = 0;
  1525. return;
  1526. }
  1527. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding extended master secret "
  1528. "extension"));
  1529. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0);
  1530. p += 2;
  1531. *p++ = 0x00;
  1532. *p++ = 0x00;
  1533. *olen = 4;
  1534. }
  1535. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  1536. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1537. static void ssl_write_session_ticket_ext(mbedtls_ssl_context *ssl,
  1538. unsigned char *buf,
  1539. size_t *olen)
  1540. {
  1541. unsigned char *p = buf;
  1542. if (ssl->handshake->new_session_ticket == 0) {
  1543. *olen = 0;
  1544. return;
  1545. }
  1546. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding session ticket extension"));
  1547. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0);
  1548. p += 2;
  1549. *p++ = 0x00;
  1550. *p++ = 0x00;
  1551. *olen = 4;
  1552. }
  1553. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1554. static void ssl_write_renegotiation_ext(mbedtls_ssl_context *ssl,
  1555. unsigned char *buf,
  1556. size_t *olen)
  1557. {
  1558. unsigned char *p = buf;
  1559. if (ssl->secure_renegotiation != MBEDTLS_SSL_SECURE_RENEGOTIATION) {
  1560. *olen = 0;
  1561. return;
  1562. }
  1563. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, secure renegotiation extension"));
  1564. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0);
  1565. p += 2;
  1566. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1567. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  1568. *p++ = 0x00;
  1569. *p++ = (ssl->verify_data_len * 2 + 1) & 0xFF;
  1570. *p++ = ssl->verify_data_len * 2 & 0xFF;
  1571. memcpy(p, ssl->peer_verify_data, ssl->verify_data_len);
  1572. p += ssl->verify_data_len;
  1573. memcpy(p, ssl->own_verify_data, ssl->verify_data_len);
  1574. p += ssl->verify_data_len;
  1575. } else
  1576. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1577. {
  1578. *p++ = 0x00;
  1579. *p++ = 0x01;
  1580. *p++ = 0x00;
  1581. }
  1582. *olen = p - buf;
  1583. }
  1584. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1585. static void ssl_write_max_fragment_length_ext(mbedtls_ssl_context *ssl,
  1586. unsigned char *buf,
  1587. size_t *olen)
  1588. {
  1589. unsigned char *p = buf;
  1590. if (ssl->session_negotiate->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE) {
  1591. *olen = 0;
  1592. return;
  1593. }
  1594. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, max_fragment_length extension"));
  1595. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0);
  1596. p += 2;
  1597. *p++ = 0x00;
  1598. *p++ = 1;
  1599. *p++ = ssl->session_negotiate->mfl_code;
  1600. *olen = 5;
  1601. }
  1602. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  1603. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  1604. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1605. static void ssl_write_supported_point_formats_ext(mbedtls_ssl_context *ssl,
  1606. unsigned char *buf,
  1607. size_t *olen)
  1608. {
  1609. unsigned char *p = buf;
  1610. ((void) ssl);
  1611. if ((ssl->handshake->cli_exts &
  1612. MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT) == 0) {
  1613. *olen = 0;
  1614. return;
  1615. }
  1616. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, supported_point_formats extension"));
  1617. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0);
  1618. p += 2;
  1619. *p++ = 0x00;
  1620. *p++ = 2;
  1621. *p++ = 1;
  1622. *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
  1623. *olen = 6;
  1624. }
  1625. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C || MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1626. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1627. static void ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
  1628. unsigned char *buf,
  1629. size_t *olen)
  1630. {
  1631. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1632. unsigned char *p = buf;
  1633. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
  1634. size_t kkpp_len;
  1635. *olen = 0;
  1636. /* Skip costly computation if not needed */
  1637. if (ssl->handshake->ciphersuite_info->key_exchange !=
  1638. MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
  1639. return;
  1640. }
  1641. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, ecjpake kkpp extension"));
  1642. if (end - p < 4) {
  1643. MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
  1644. return;
  1645. }
  1646. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
  1647. p += 2;
  1648. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1649. ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
  1650. p + 2, end - p - 2, &kkpp_len,
  1651. MBEDTLS_ECJPAKE_ROUND_ONE);
  1652. if (ret != 0) {
  1653. psa_destroy_key(ssl->handshake->psa_pake_password);
  1654. psa_pake_abort(&ssl->handshake->psa_pake_ctx);
  1655. MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
  1656. return;
  1657. }
  1658. #else
  1659. ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
  1660. p + 2, end - p - 2, &kkpp_len,
  1661. ssl->conf->f_rng, ssl->conf->p_rng);
  1662. if (ret != 0) {
  1663. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_one", ret);
  1664. return;
  1665. }
  1666. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1667. MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
  1668. p += 2;
  1669. *olen = kkpp_len + 4;
  1670. }
  1671. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1672. #if defined(MBEDTLS_SSL_DTLS_SRTP) && defined(MBEDTLS_SSL_PROTO_DTLS)
  1673. static void ssl_write_use_srtp_ext(mbedtls_ssl_context *ssl,
  1674. unsigned char *buf,
  1675. size_t *olen)
  1676. {
  1677. size_t mki_len = 0, ext_len = 0;
  1678. uint16_t profile_value = 0;
  1679. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
  1680. *olen = 0;
  1681. if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
  1682. (ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET)) {
  1683. return;
  1684. }
  1685. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding use_srtp extension"));
  1686. if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED) {
  1687. mki_len = ssl->dtls_srtp_info.mki_len;
  1688. }
  1689. /* The extension total size is 9 bytes :
  1690. * - 2 bytes for the extension tag
  1691. * - 2 bytes for the total size
  1692. * - 2 bytes for the protection profile length
  1693. * - 2 bytes for the protection profile
  1694. * - 1 byte for the mki length
  1695. * + the actual mki length
  1696. * Check we have enough room in the output buffer */
  1697. if ((size_t) (end - buf) < mki_len + 9) {
  1698. MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
  1699. return;
  1700. }
  1701. /* extension */
  1702. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0);
  1703. /*
  1704. * total length 5 and mki value: only one profile(2 bytes)
  1705. * and length(2 bytes) and srtp_mki )
  1706. */
  1707. ext_len = 5 + mki_len;
  1708. MBEDTLS_PUT_UINT16_BE(ext_len, buf, 2);
  1709. /* protection profile length: 2 */
  1710. buf[4] = 0x00;
  1711. buf[5] = 0x02;
  1712. profile_value = mbedtls_ssl_check_srtp_profile_value(
  1713. ssl->dtls_srtp_info.chosen_dtls_srtp_profile);
  1714. if (profile_value != MBEDTLS_TLS_SRTP_UNSET) {
  1715. MBEDTLS_PUT_UINT16_BE(profile_value, buf, 6);
  1716. } else {
  1717. MBEDTLS_SSL_DEBUG_MSG(1, ("use_srtp extension invalid profile"));
  1718. return;
  1719. }
  1720. buf[8] = mki_len & 0xFF;
  1721. memcpy(&buf[9], ssl->dtls_srtp_info.mki_value, mki_len);
  1722. *olen = 9 + mki_len;
  1723. }
  1724. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  1725. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  1726. MBEDTLS_CHECK_RETURN_CRITICAL
  1727. static int ssl_write_hello_verify_request(mbedtls_ssl_context *ssl)
  1728. {
  1729. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1730. unsigned char *p = ssl->out_msg + 4;
  1731. unsigned char *cookie_len_byte;
  1732. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello verify request"));
  1733. /*
  1734. * struct {
  1735. * ProtocolVersion server_version;
  1736. * opaque cookie<0..2^8-1>;
  1737. * } HelloVerifyRequest;
  1738. */
  1739. /* The RFC is not clear on this point, but sending the actual negotiated
  1740. * version looks like the most interoperable thing to do. */
  1741. mbedtls_ssl_write_version(p, ssl->conf->transport, ssl->tls_version);
  1742. MBEDTLS_SSL_DEBUG_BUF(3, "server version", p, 2);
  1743. p += 2;
  1744. /* If we get here, f_cookie_check is not null */
  1745. if (ssl->conf->f_cookie_write == NULL) {
  1746. MBEDTLS_SSL_DEBUG_MSG(1, ("inconsistent cookie callbacks"));
  1747. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  1748. }
  1749. /* Skip length byte until we know the length */
  1750. cookie_len_byte = p++;
  1751. if ((ret = ssl->conf->f_cookie_write(ssl->conf->p_cookie,
  1752. &p, ssl->out_buf + MBEDTLS_SSL_OUT_BUFFER_LEN,
  1753. ssl->cli_id, ssl->cli_id_len)) != 0) {
  1754. MBEDTLS_SSL_DEBUG_RET(1, "f_cookie_write", ret);
  1755. return ret;
  1756. }
  1757. *cookie_len_byte = (unsigned char) (p - (cookie_len_byte + 1));
  1758. MBEDTLS_SSL_DEBUG_BUF(3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte);
  1759. ssl->out_msglen = p - ssl->out_msg;
  1760. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  1761. ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
  1762. ssl->state = MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
  1763. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  1764. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  1765. return ret;
  1766. }
  1767. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1768. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  1769. (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
  1770. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
  1771. return ret;
  1772. }
  1773. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1774. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello verify request"));
  1775. return 0;
  1776. }
  1777. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  1778. static void ssl_handle_id_based_session_resumption(mbedtls_ssl_context *ssl)
  1779. {
  1780. int ret;
  1781. mbedtls_ssl_session session_tmp;
  1782. mbedtls_ssl_session * const session = ssl->session_negotiate;
  1783. /* Resume is 0 by default, see ssl_handshake_init().
  1784. * It may be already set to 1 by ssl_parse_session_ticket_ext(). */
  1785. if (ssl->handshake->resume == 1) {
  1786. return;
  1787. }
  1788. if (session->id_len == 0) {
  1789. return;
  1790. }
  1791. if (ssl->conf->f_get_cache == NULL) {
  1792. return;
  1793. }
  1794. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1795. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  1796. return;
  1797. }
  1798. #endif
  1799. mbedtls_ssl_session_init(&session_tmp);
  1800. ret = ssl->conf->f_get_cache(ssl->conf->p_cache,
  1801. session->id,
  1802. session->id_len,
  1803. &session_tmp);
  1804. if (ret != 0) {
  1805. goto exit;
  1806. }
  1807. if (session->ciphersuite != session_tmp.ciphersuite) {
  1808. /* Mismatch between cached and negotiated session */
  1809. goto exit;
  1810. }
  1811. /* Move semantics */
  1812. mbedtls_ssl_session_free(session);
  1813. *session = session_tmp;
  1814. memset(&session_tmp, 0, sizeof(session_tmp));
  1815. MBEDTLS_SSL_DEBUG_MSG(3, ("session successfully restored from cache"));
  1816. ssl->handshake->resume = 1;
  1817. exit:
  1818. mbedtls_ssl_session_free(&session_tmp);
  1819. }
  1820. MBEDTLS_CHECK_RETURN_CRITICAL
  1821. static int ssl_write_server_hello(mbedtls_ssl_context *ssl)
  1822. {
  1823. #if defined(MBEDTLS_HAVE_TIME)
  1824. mbedtls_time_t t;
  1825. #endif
  1826. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1827. size_t olen, ext_len = 0, n;
  1828. unsigned char *buf, *p;
  1829. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
  1830. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  1831. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  1832. ssl->handshake->cookie_verify_result != 0) {
  1833. MBEDTLS_SSL_DEBUG_MSG(2, ("client hello was not authenticated"));
  1834. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
  1835. return ssl_write_hello_verify_request(ssl);
  1836. }
  1837. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  1838. if (ssl->conf->f_rng == NULL) {
  1839. MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
  1840. return MBEDTLS_ERR_SSL_NO_RNG;
  1841. }
  1842. /*
  1843. * 0 . 0 handshake type
  1844. * 1 . 3 handshake length
  1845. * 4 . 5 protocol version
  1846. * 6 . 9 UNIX time()
  1847. * 10 . 37 random bytes
  1848. */
  1849. buf = ssl->out_msg;
  1850. p = buf + 4;
  1851. mbedtls_ssl_write_version(p, ssl->conf->transport, ssl->tls_version);
  1852. p += 2;
  1853. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen version: [%d:%d]",
  1854. buf[4], buf[5]));
  1855. #if defined(MBEDTLS_HAVE_TIME)
  1856. t = mbedtls_time(NULL);
  1857. MBEDTLS_PUT_UINT32_BE(t, p, 0);
  1858. p += 4;
  1859. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
  1860. (long long) t));
  1861. #else
  1862. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 4)) != 0) {
  1863. return ret;
  1864. }
  1865. p += 4;
  1866. #endif /* MBEDTLS_HAVE_TIME */
  1867. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, p, 28)) != 0) {
  1868. return ret;
  1869. }
  1870. p += 28;
  1871. memcpy(ssl->handshake->randbytes + 32, buf + 6, 32);
  1872. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", buf + 6, 32);
  1873. ssl_handle_id_based_session_resumption(ssl);
  1874. if (ssl->handshake->resume == 0) {
  1875. /*
  1876. * New session, create a new session id,
  1877. * unless we're about to issue a session ticket
  1878. */
  1879. ssl->state++;
  1880. #if defined(MBEDTLS_HAVE_TIME)
  1881. ssl->session_negotiate->start = mbedtls_time(NULL);
  1882. #endif
  1883. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1884. if (ssl->handshake->new_session_ticket != 0) {
  1885. ssl->session_negotiate->id_len = n = 0;
  1886. memset(ssl->session_negotiate->id, 0, 32);
  1887. } else
  1888. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1889. {
  1890. ssl->session_negotiate->id_len = n = 32;
  1891. if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, ssl->session_negotiate->id,
  1892. n)) != 0) {
  1893. return ret;
  1894. }
  1895. }
  1896. } else {
  1897. /*
  1898. * Resuming a session
  1899. */
  1900. n = ssl->session_negotiate->id_len;
  1901. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  1902. if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
  1903. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
  1904. return ret;
  1905. }
  1906. }
  1907. /*
  1908. * 38 . 38 session id length
  1909. * 39 . 38+n session id
  1910. * 39+n . 40+n chosen ciphersuite
  1911. * 41+n . 41+n chosen compression alg.
  1912. * 42+n . 43+n extensions length
  1913. * 44+n . 43+n+m extensions
  1914. */
  1915. *p++ = (unsigned char) ssl->session_negotiate->id_len;
  1916. memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
  1917. p += ssl->session_negotiate->id_len;
  1918. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n));
  1919. MBEDTLS_SSL_DEBUG_BUF(3, "server hello, session id", buf + 39, n);
  1920. MBEDTLS_SSL_DEBUG_MSG(3, ("%s session has been resumed",
  1921. ssl->handshake->resume ? "a" : "no"));
  1922. MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
  1923. p += 2;
  1924. *p++ = MBEDTLS_BYTE_0(MBEDTLS_SSL_COMPRESS_NULL);
  1925. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: %s",
  1926. mbedtls_ssl_get_ciphersuite_name(ssl->session_negotiate->ciphersuite)));
  1927. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, compress alg.: 0x%02X",
  1928. (unsigned int) MBEDTLS_SSL_COMPRESS_NULL));
  1929. /*
  1930. * First write extensions, then the total length
  1931. */
  1932. ssl_write_renegotiation_ext(ssl, p + 2 + ext_len, &olen);
  1933. ext_len += olen;
  1934. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1935. ssl_write_max_fragment_length_ext(ssl, p + 2 + ext_len, &olen);
  1936. ext_len += olen;
  1937. #endif
  1938. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1939. ssl_write_cid_ext(ssl, p + 2 + ext_len, &olen);
  1940. ext_len += olen;
  1941. #endif
  1942. #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
  1943. ssl_write_encrypt_then_mac_ext(ssl, p + 2 + ext_len, &olen);
  1944. ext_len += olen;
  1945. #endif
  1946. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1947. ssl_write_extended_ms_ext(ssl, p + 2 + ext_len, &olen);
  1948. ext_len += olen;
  1949. #endif
  1950. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1951. ssl_write_session_ticket_ext(ssl, p + 2 + ext_len, &olen);
  1952. ext_len += olen;
  1953. #endif
  1954. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  1955. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1956. const mbedtls_ssl_ciphersuite_t *suite =
  1957. mbedtls_ssl_ciphersuite_from_id(ssl->session_negotiate->ciphersuite);
  1958. if (suite != NULL && mbedtls_ssl_ciphersuite_uses_ec(suite)) {
  1959. ssl_write_supported_point_formats_ext(ssl, p + 2 + ext_len, &olen);
  1960. ext_len += olen;
  1961. }
  1962. #endif
  1963. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1964. ssl_write_ecjpake_kkpp_ext(ssl, p + 2 + ext_len, &olen);
  1965. ext_len += olen;
  1966. #endif
  1967. #if defined(MBEDTLS_SSL_ALPN)
  1968. unsigned char *end = buf + MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
  1969. if ((ret = mbedtls_ssl_write_alpn_ext(ssl, p + 2 + ext_len, end, &olen))
  1970. != 0) {
  1971. return ret;
  1972. }
  1973. ext_len += olen;
  1974. #endif
  1975. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  1976. ssl_write_use_srtp_ext(ssl, p + 2 + ext_len, &olen);
  1977. ext_len += olen;
  1978. #endif
  1979. MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
  1980. ext_len));
  1981. if (ext_len > 0) {
  1982. MBEDTLS_PUT_UINT16_BE(ext_len, p, 0);
  1983. p += 2 + ext_len;
  1984. }
  1985. ssl->out_msglen = p - buf;
  1986. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  1987. ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO;
  1988. ret = mbedtls_ssl_write_handshake_msg(ssl);
  1989. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
  1990. return ret;
  1991. }
  1992. #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
  1993. MBEDTLS_CHECK_RETURN_CRITICAL
  1994. static int ssl_write_certificate_request(mbedtls_ssl_context *ssl)
  1995. {
  1996. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  1997. ssl->handshake->ciphersuite_info;
  1998. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
  1999. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  2000. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
  2001. ssl->state++;
  2002. return 0;
  2003. }
  2004. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2005. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2006. }
  2007. #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  2008. MBEDTLS_CHECK_RETURN_CRITICAL
  2009. static int ssl_write_certificate_request(mbedtls_ssl_context *ssl)
  2010. {
  2011. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2012. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2013. ssl->handshake->ciphersuite_info;
  2014. uint16_t dn_size, total_dn_size; /* excluding length bytes */
  2015. size_t ct_len, sa_len; /* including length bytes */
  2016. unsigned char *buf, *p;
  2017. const unsigned char * const end = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN;
  2018. const mbedtls_x509_crt *crt;
  2019. int authmode;
  2020. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
  2021. ssl->state++;
  2022. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  2023. if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
  2024. authmode = ssl->handshake->sni_authmode;
  2025. } else
  2026. #endif
  2027. authmode = ssl->conf->authmode;
  2028. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info) ||
  2029. authmode == MBEDTLS_SSL_VERIFY_NONE) {
  2030. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
  2031. return 0;
  2032. }
  2033. /*
  2034. * 0 . 0 handshake type
  2035. * 1 . 3 handshake length
  2036. * 4 . 4 cert type count
  2037. * 5 .. m-1 cert types
  2038. * m .. m+1 sig alg length (TLS 1.2 only)
  2039. * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
  2040. * n .. n+1 length of all DNs
  2041. * n+2 .. n+3 length of DN 1
  2042. * n+4 .. ... Distinguished Name #1
  2043. * ... .. ... length of DN 2, etc.
  2044. */
  2045. buf = ssl->out_msg;
  2046. p = buf + 4;
  2047. /*
  2048. * Supported certificate types
  2049. *
  2050. * ClientCertificateType certificate_types<1..2^8-1>;
  2051. * enum { (255) } ClientCertificateType;
  2052. */
  2053. ct_len = 0;
  2054. #if defined(MBEDTLS_RSA_C)
  2055. p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_RSA_SIGN;
  2056. #endif
  2057. #if defined(MBEDTLS_ECDSA_C)
  2058. p[1 + ct_len++] = MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN;
  2059. #endif
  2060. p[0] = (unsigned char) ct_len++;
  2061. p += ct_len;
  2062. sa_len = 0;
  2063. /*
  2064. * Add signature_algorithms for verify (TLS 1.2)
  2065. *
  2066. * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
  2067. *
  2068. * struct {
  2069. * HashAlgorithm hash;
  2070. * SignatureAlgorithm signature;
  2071. * } SignatureAndHashAlgorithm;
  2072. *
  2073. * enum { (255) } HashAlgorithm;
  2074. * enum { (255) } SignatureAlgorithm;
  2075. */
  2076. const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
  2077. if (sig_alg == NULL) {
  2078. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  2079. }
  2080. for (; *sig_alg != MBEDTLS_TLS_SIG_NONE; sig_alg++) {
  2081. unsigned char hash = MBEDTLS_BYTE_1(*sig_alg);
  2082. if (mbedtls_ssl_set_calc_verify_md(ssl, hash)) {
  2083. continue;
  2084. }
  2085. if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
  2086. continue;
  2087. }
  2088. /* Write elements at offsets starting from 1 (offset 0 is for the
  2089. * length). Thus the offset of each element is the length of the
  2090. * partial list including that element. */
  2091. sa_len += 2;
  2092. MBEDTLS_PUT_UINT16_BE(*sig_alg, p, sa_len);
  2093. }
  2094. /* Fill in list length. */
  2095. MBEDTLS_PUT_UINT16_BE(sa_len, p, 0);
  2096. sa_len += 2;
  2097. p += sa_len;
  2098. /*
  2099. * DistinguishedName certificate_authorities<0..2^16-1>;
  2100. * opaque DistinguishedName<1..2^16-1>;
  2101. */
  2102. p += 2;
  2103. total_dn_size = 0;
  2104. if (ssl->conf->cert_req_ca_list == MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED) {
  2105. /* NOTE: If trusted certificates are provisioned
  2106. * via a CA callback (configured through
  2107. * `mbedtls_ssl_conf_ca_cb()`, then the
  2108. * CertificateRequest is currently left empty. */
  2109. #if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
  2110. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  2111. if (ssl->handshake->dn_hints != NULL) {
  2112. crt = ssl->handshake->dn_hints;
  2113. } else
  2114. #endif
  2115. if (ssl->conf->dn_hints != NULL) {
  2116. crt = ssl->conf->dn_hints;
  2117. } else
  2118. #endif
  2119. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  2120. if (ssl->handshake->sni_ca_chain != NULL) {
  2121. crt = ssl->handshake->sni_ca_chain;
  2122. } else
  2123. #endif
  2124. crt = ssl->conf->ca_chain;
  2125. while (crt != NULL && crt->version != 0) {
  2126. /* It follows from RFC 5280 A.1 that this length
  2127. * can be represented in at most 11 bits. */
  2128. dn_size = (uint16_t) crt->subject_raw.len;
  2129. if (end < p || (size_t) (end - p) < 2 + (size_t) dn_size) {
  2130. MBEDTLS_SSL_DEBUG_MSG(1, ("skipping CAs: buffer too short"));
  2131. break;
  2132. }
  2133. MBEDTLS_PUT_UINT16_BE(dn_size, p, 0);
  2134. p += 2;
  2135. memcpy(p, crt->subject_raw.p, dn_size);
  2136. p += dn_size;
  2137. MBEDTLS_SSL_DEBUG_BUF(3, "requested DN", p - dn_size, dn_size);
  2138. total_dn_size += 2 + dn_size;
  2139. crt = crt->next;
  2140. }
  2141. }
  2142. ssl->out_msglen = p - buf;
  2143. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2144. ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST;
  2145. MBEDTLS_PUT_UINT16_BE(total_dn_size, ssl->out_msg, 4 + ct_len + sa_len);
  2146. ret = mbedtls_ssl_write_handshake_msg(ssl);
  2147. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
  2148. return ret;
  2149. }
  2150. #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  2151. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  2152. (defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2153. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED))
  2154. MBEDTLS_CHECK_RETURN_CRITICAL
  2155. static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
  2156. {
  2157. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2158. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  2159. unsigned char buf[
  2160. PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
  2161. psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
  2162. uint16_t tls_id = 0;
  2163. psa_ecc_family_t ecc_family;
  2164. size_t key_len;
  2165. mbedtls_pk_context *pk;
  2166. mbedtls_ecp_keypair *key;
  2167. pk = mbedtls_ssl_own_key(ssl);
  2168. if (pk == NULL) {
  2169. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  2170. }
  2171. switch (mbedtls_pk_get_type(pk)) {
  2172. case MBEDTLS_PK_OPAQUE:
  2173. if (!mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
  2174. return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
  2175. }
  2176. ssl->handshake->ecdh_psa_privkey =
  2177. *((mbedtls_svc_key_id_t *) pk->pk_ctx);
  2178. /* Key should not be destroyed in the TLS library */
  2179. ssl->handshake->ecdh_psa_privkey_is_external = 1;
  2180. status = psa_get_key_attributes(ssl->handshake->ecdh_psa_privkey,
  2181. &key_attributes);
  2182. if (status != PSA_SUCCESS) {
  2183. ssl->handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  2184. return PSA_TO_MBEDTLS_ERR(status);
  2185. }
  2186. ssl->handshake->ecdh_psa_type = psa_get_key_type(&key_attributes);
  2187. ssl->handshake->ecdh_bits = psa_get_key_bits(&key_attributes);
  2188. psa_reset_key_attributes(&key_attributes);
  2189. ret = 0;
  2190. break;
  2191. case MBEDTLS_PK_ECKEY:
  2192. case MBEDTLS_PK_ECKEY_DH:
  2193. case MBEDTLS_PK_ECDSA:
  2194. key = mbedtls_pk_ec(*pk);
  2195. if (key == NULL) {
  2196. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  2197. }
  2198. tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(key->grp.id);
  2199. if (tls_id == 0) {
  2200. /* This elliptic curve is not supported */
  2201. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  2202. }
  2203. /* If the above conversion to TLS ID was fine, then also this one will
  2204. be, so there is no need to check the return value here */
  2205. mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id, &ecc_family,
  2206. &ssl->handshake->ecdh_bits);
  2207. ssl->handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ecc_family);
  2208. key_attributes = psa_key_attributes_init();
  2209. psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
  2210. psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
  2211. psa_set_key_type(&key_attributes,
  2212. PSA_KEY_TYPE_ECC_KEY_PAIR(ssl->handshake->ecdh_psa_type));
  2213. psa_set_key_bits(&key_attributes, ssl->handshake->ecdh_bits);
  2214. key_len = PSA_BITS_TO_BYTES(key->grp.pbits);
  2215. ret = mbedtls_ecp_write_key(key, buf, key_len);
  2216. if (ret != 0) {
  2217. goto cleanup;
  2218. }
  2219. status = psa_import_key(&key_attributes, buf, key_len,
  2220. &ssl->handshake->ecdh_psa_privkey);
  2221. if (status != PSA_SUCCESS) {
  2222. ret = PSA_TO_MBEDTLS_ERR(status);
  2223. goto cleanup;
  2224. }
  2225. ret = 0;
  2226. break;
  2227. default:
  2228. ret = MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
  2229. }
  2230. cleanup:
  2231. mbedtls_platform_zeroize(buf, sizeof(buf));
  2232. return ret;
  2233. }
  2234. #elif defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2235. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  2236. MBEDTLS_CHECK_RETURN_CRITICAL
  2237. static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
  2238. {
  2239. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2240. const mbedtls_pk_context *private_key = mbedtls_ssl_own_key(ssl);
  2241. if (private_key == NULL) {
  2242. MBEDTLS_SSL_DEBUG_MSG(1, ("got no server private key"));
  2243. return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
  2244. }
  2245. if (!mbedtls_pk_can_do(private_key, MBEDTLS_PK_ECKEY)) {
  2246. MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
  2247. return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
  2248. }
  2249. if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx,
  2250. mbedtls_pk_ec(*mbedtls_ssl_own_key(ssl)),
  2251. MBEDTLS_ECDH_OURS)) != 0) {
  2252. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
  2253. return ret;
  2254. }
  2255. return 0;
  2256. }
  2257. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
  2258. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  2259. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
  2260. defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2261. MBEDTLS_CHECK_RETURN_CRITICAL
  2262. static int ssl_resume_server_key_exchange(mbedtls_ssl_context *ssl,
  2263. size_t *signature_len)
  2264. {
  2265. /* Append the signature to ssl->out_msg, leaving 2 bytes for the
  2266. * signature length which will be added in ssl_write_server_key_exchange
  2267. * after the call to ssl_prepare_server_key_exchange.
  2268. * ssl_write_server_key_exchange also takes care of incrementing
  2269. * ssl->out_msglen. */
  2270. unsigned char *sig_start = ssl->out_msg + ssl->out_msglen + 2;
  2271. size_t sig_max_len = (ssl->out_buf + MBEDTLS_SSL_OUT_CONTENT_LEN
  2272. - sig_start);
  2273. int ret = ssl->conf->f_async_resume(ssl,
  2274. sig_start, signature_len, sig_max_len);
  2275. if (ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  2276. ssl->handshake->async_in_progress = 0;
  2277. mbedtls_ssl_set_async_operation_data(ssl, NULL);
  2278. }
  2279. MBEDTLS_SSL_DEBUG_RET(2, "ssl_resume_server_key_exchange", ret);
  2280. return ret;
  2281. }
  2282. #endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
  2283. defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
  2284. /* Prepare the ServerKeyExchange message, up to and including
  2285. * calculating the signature if any, but excluding formatting the
  2286. * signature and sending the message. */
  2287. MBEDTLS_CHECK_RETURN_CRITICAL
  2288. static int ssl_prepare_server_key_exchange(mbedtls_ssl_context *ssl,
  2289. size_t *signature_len)
  2290. {
  2291. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2292. ssl->handshake->ciphersuite_info;
  2293. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED)
  2294. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2295. unsigned char *dig_signed = NULL;
  2296. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
  2297. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PFS_ENABLED */
  2298. (void) ciphersuite_info; /* unused in some configurations */
  2299. #if !defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2300. (void) signature_len;
  2301. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
  2302. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2303. #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
  2304. size_t out_buf_len = ssl->out_buf_len - (ssl->out_msg - ssl->out_buf);
  2305. #else
  2306. size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN - (ssl->out_msg - ssl->out_buf);
  2307. #endif
  2308. #endif
  2309. ssl->out_msglen = 4; /* header (type:1, length:3) to be written later */
  2310. /*
  2311. *
  2312. * Part 1: Provide key exchange parameters for chosen ciphersuite.
  2313. *
  2314. */
  2315. /*
  2316. * - ECJPAKE key exchanges
  2317. */
  2318. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2319. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
  2320. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2321. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2322. unsigned char *out_p = ssl->out_msg + ssl->out_msglen;
  2323. unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
  2324. ssl->out_msglen;
  2325. size_t output_offset = 0;
  2326. size_t output_len = 0;
  2327. /*
  2328. * The first 3 bytes are:
  2329. * [0] MBEDTLS_ECP_TLS_NAMED_CURVE
  2330. * [1, 2] elliptic curve's TLS ID
  2331. *
  2332. * However since we only support secp256r1 for now, we hardcode its
  2333. * TLS ID here
  2334. */
  2335. uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
  2336. MBEDTLS_ECP_DP_SECP256R1);
  2337. if (tls_id == 0) {
  2338. return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2339. }
  2340. *out_p = MBEDTLS_ECP_TLS_NAMED_CURVE;
  2341. MBEDTLS_PUT_UINT16_BE(tls_id, out_p, 1);
  2342. output_offset += 3;
  2343. ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
  2344. out_p + output_offset,
  2345. end_p - out_p - output_offset, &output_len,
  2346. MBEDTLS_ECJPAKE_ROUND_TWO);
  2347. if (ret != 0) {
  2348. psa_destroy_key(ssl->handshake->psa_pake_password);
  2349. psa_pake_abort(&ssl->handshake->psa_pake_ctx);
  2350. MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
  2351. return ret;
  2352. }
  2353. output_offset += output_len;
  2354. ssl->out_msglen += output_offset;
  2355. #else
  2356. size_t len = 0;
  2357. ret = mbedtls_ecjpake_write_round_two(
  2358. &ssl->handshake->ecjpake_ctx,
  2359. ssl->out_msg + ssl->out_msglen,
  2360. MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
  2361. ssl->conf->f_rng, ssl->conf->p_rng);
  2362. if (ret != 0) {
  2363. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
  2364. return ret;
  2365. }
  2366. ssl->out_msglen += len;
  2367. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  2368. }
  2369. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2370. /*
  2371. * For (EC)DHE key exchanges with PSK, parameters are prefixed by support
  2372. * identity hint (RFC 4279, Sec. 3). Until someone needs this feature,
  2373. * we use empty support identity hints here.
  2374. **/
  2375. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
  2376. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  2377. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2378. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
  2379. ssl->out_msg[ssl->out_msglen++] = 0x00;
  2380. ssl->out_msg[ssl->out_msglen++] = 0x00;
  2381. }
  2382. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED ||
  2383. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  2384. /*
  2385. * - DHE key exchanges
  2386. */
  2387. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED)
  2388. if (mbedtls_ssl_ciphersuite_uses_dhe(ciphersuite_info)) {
  2389. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2390. size_t len = 0;
  2391. if (ssl->conf->dhm_P.p == NULL || ssl->conf->dhm_G.p == NULL) {
  2392. MBEDTLS_SSL_DEBUG_MSG(1, ("no DH parameters set"));
  2393. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  2394. }
  2395. /*
  2396. * Ephemeral DH parameters:
  2397. *
  2398. * struct {
  2399. * opaque dh_p<1..2^16-1>;
  2400. * opaque dh_g<1..2^16-1>;
  2401. * opaque dh_Ys<1..2^16-1>;
  2402. * } ServerDHParams;
  2403. */
  2404. if ((ret = mbedtls_dhm_set_group(&ssl->handshake->dhm_ctx,
  2405. &ssl->conf->dhm_P,
  2406. &ssl->conf->dhm_G)) != 0) {
  2407. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_set_group", ret);
  2408. return ret;
  2409. }
  2410. if ((ret = mbedtls_dhm_make_params(
  2411. &ssl->handshake->dhm_ctx,
  2412. (int) mbedtls_dhm_get_len(&ssl->handshake->dhm_ctx),
  2413. ssl->out_msg + ssl->out_msglen, &len,
  2414. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  2415. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_make_params", ret);
  2416. return ret;
  2417. }
  2418. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2419. dig_signed = ssl->out_msg + ssl->out_msglen;
  2420. #endif
  2421. ssl->out_msglen += len;
  2422. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: X ", &ssl->handshake->dhm_ctx.X);
  2423. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: P ", &ssl->handshake->dhm_ctx.P);
  2424. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: G ", &ssl->handshake->dhm_ctx.G);
  2425. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GX", &ssl->handshake->dhm_ctx.GX);
  2426. }
  2427. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_DHE_ENABLED */
  2428. /*
  2429. * - ECDHE key exchanges
  2430. */
  2431. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
  2432. if (mbedtls_ssl_ciphersuite_uses_ecdhe(ciphersuite_info)) {
  2433. /*
  2434. * Ephemeral ECDH parameters:
  2435. *
  2436. * struct {
  2437. * ECParameters curve_params;
  2438. * ECPoint public;
  2439. * } ServerECDHParams;
  2440. */
  2441. uint16_t *curr_tls_id = ssl->handshake->curves_tls_id;
  2442. const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
  2443. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2444. size_t len = 0;
  2445. /* Match our preference list against the offered curves */
  2446. if ((group_list == NULL) || (curr_tls_id == NULL)) {
  2447. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  2448. }
  2449. for (; *group_list != 0; group_list++) {
  2450. for (curr_tls_id = ssl->handshake->curves_tls_id;
  2451. *curr_tls_id != 0; curr_tls_id++) {
  2452. if (*curr_tls_id == *group_list) {
  2453. goto curve_matching_done;
  2454. }
  2455. }
  2456. }
  2457. curve_matching_done:
  2458. if (*curr_tls_id == 0) {
  2459. MBEDTLS_SSL_DEBUG_MSG(1, ("no matching curve for ECDHE"));
  2460. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  2461. }
  2462. MBEDTLS_SSL_DEBUG_MSG(2, ("ECDHE curve: %s",
  2463. mbedtls_ssl_get_curve_name_from_tls_id(*curr_tls_id)));
  2464. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2465. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  2466. psa_key_attributes_t key_attributes;
  2467. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  2468. uint8_t *p = ssl->out_msg + ssl->out_msglen;
  2469. const size_t header_size = 4; // curve_type(1), namedcurve(2),
  2470. // data length(1)
  2471. const size_t data_length_size = 1;
  2472. psa_ecc_family_t ec_psa_family = 0;
  2473. size_t ec_bits = 0;
  2474. MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
  2475. /* Convert EC's TLS ID to PSA key type. */
  2476. if (mbedtls_ssl_get_psa_curve_info_from_tls_id(*curr_tls_id,
  2477. &ec_psa_family,
  2478. &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
  2479. MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid ecc group parse."));
  2480. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  2481. }
  2482. handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ec_psa_family);
  2483. handshake->ecdh_bits = ec_bits;
  2484. key_attributes = psa_key_attributes_init();
  2485. psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
  2486. psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
  2487. psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
  2488. psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
  2489. /*
  2490. * ECParameters curve_params
  2491. *
  2492. * First byte is curve_type, always named_curve
  2493. */
  2494. *p++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
  2495. /*
  2496. * Next two bytes are the namedcurve value
  2497. */
  2498. MBEDTLS_PUT_UINT16_BE(*curr_tls_id, p, 0);
  2499. p += 2;
  2500. /* Generate ECDH private key. */
  2501. status = psa_generate_key(&key_attributes,
  2502. &handshake->ecdh_psa_privkey);
  2503. if (status != PSA_SUCCESS) {
  2504. ret = PSA_TO_MBEDTLS_ERR(status);
  2505. MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
  2506. return ret;
  2507. }
  2508. /*
  2509. * ECPoint public
  2510. *
  2511. * First byte is data length.
  2512. * It will be filled later. p holds now the data length location.
  2513. */
  2514. /* Export the public part of the ECDH private key from PSA.
  2515. * Make one byte space for the length.
  2516. */
  2517. unsigned char *own_pubkey = p + data_length_size;
  2518. size_t own_pubkey_max_len = (size_t) (MBEDTLS_SSL_OUT_CONTENT_LEN
  2519. - (own_pubkey - ssl->out_msg));
  2520. status = psa_export_public_key(handshake->ecdh_psa_privkey,
  2521. own_pubkey, own_pubkey_max_len,
  2522. &len);
  2523. if (status != PSA_SUCCESS) {
  2524. ret = PSA_TO_MBEDTLS_ERR(status);
  2525. MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
  2526. (void) psa_destroy_key(handshake->ecdh_psa_privkey);
  2527. handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  2528. return ret;
  2529. }
  2530. /* Store the length of the exported public key. */
  2531. *p = (uint8_t) len;
  2532. /* Determine full message length. */
  2533. len += header_size;
  2534. #else
  2535. mbedtls_ecp_group_id curr_grp_id =
  2536. mbedtls_ssl_get_ecp_group_id_from_tls_id(*curr_tls_id);
  2537. if ((ret = mbedtls_ecdh_setup(&ssl->handshake->ecdh_ctx,
  2538. curr_grp_id)) != 0) {
  2539. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecp_group_load", ret);
  2540. return ret;
  2541. }
  2542. if ((ret = mbedtls_ecdh_make_params(
  2543. &ssl->handshake->ecdh_ctx, &len,
  2544. ssl->out_msg + ssl->out_msglen,
  2545. MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
  2546. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  2547. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_params", ret);
  2548. return ret;
  2549. }
  2550. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  2551. MBEDTLS_DEBUG_ECDH_Q);
  2552. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  2553. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2554. dig_signed = ssl->out_msg + ssl->out_msglen;
  2555. #endif
  2556. ssl->out_msglen += len;
  2557. }
  2558. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
  2559. /*
  2560. *
  2561. * Part 2: For key exchanges involving the server signing the
  2562. * exchange parameters, compute and add the signature here.
  2563. *
  2564. */
  2565. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2566. if (mbedtls_ssl_ciphersuite_uses_server_signature(ciphersuite_info)) {
  2567. if (dig_signed == NULL) {
  2568. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2569. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2570. }
  2571. size_t dig_signed_len = ssl->out_msg + ssl->out_msglen - dig_signed;
  2572. size_t hashlen = 0;
  2573. unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
  2574. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2575. /*
  2576. * 2.1: Choose hash algorithm:
  2577. * For TLS 1.2, obey signature-hash-algorithm extension
  2578. * to choose appropriate hash.
  2579. */
  2580. mbedtls_pk_type_t sig_alg =
  2581. mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info);
  2582. unsigned int sig_hash =
  2583. mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
  2584. ssl, mbedtls_ssl_sig_from_pk_alg(sig_alg));
  2585. mbedtls_md_type_t md_alg = mbedtls_ssl_md_alg_from_hash(sig_hash);
  2586. /* For TLS 1.2, obey signature-hash-algorithm extension
  2587. * (RFC 5246, Sec. 7.4.1.4.1). */
  2588. if (sig_alg == MBEDTLS_PK_NONE || md_alg == MBEDTLS_MD_NONE) {
  2589. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2590. /* (... because we choose a cipher suite
  2591. * only if there is a matching hash.) */
  2592. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2593. }
  2594. MBEDTLS_SSL_DEBUG_MSG(3, ("pick hash algorithm %u for signing", (unsigned) md_alg));
  2595. /*
  2596. * 2.2: Compute the hash to be signed
  2597. */
  2598. if (md_alg != MBEDTLS_MD_NONE) {
  2599. ret = mbedtls_ssl_get_key_exchange_md_tls1_2(ssl, hash, &hashlen,
  2600. dig_signed,
  2601. dig_signed_len,
  2602. md_alg);
  2603. if (ret != 0) {
  2604. return ret;
  2605. }
  2606. } else {
  2607. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2608. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2609. }
  2610. MBEDTLS_SSL_DEBUG_BUF(3, "parameters hash", hash, hashlen);
  2611. /*
  2612. * 2.3: Compute and add the signature
  2613. */
  2614. /*
  2615. * We need to specify signature and hash algorithm explicitly through
  2616. * a prefix to the signature.
  2617. *
  2618. * struct {
  2619. * HashAlgorithm hash;
  2620. * SignatureAlgorithm signature;
  2621. * } SignatureAndHashAlgorithm;
  2622. *
  2623. * struct {
  2624. * SignatureAndHashAlgorithm algorithm;
  2625. * opaque signature<0..2^16-1>;
  2626. * } DigitallySigned;
  2627. *
  2628. */
  2629. ssl->out_msg[ssl->out_msglen++] = mbedtls_ssl_hash_from_md_alg(md_alg);
  2630. ssl->out_msg[ssl->out_msglen++] = mbedtls_ssl_sig_from_pk_alg(sig_alg);
  2631. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2632. if (ssl->conf->f_async_sign_start != NULL) {
  2633. ret = ssl->conf->f_async_sign_start(ssl,
  2634. mbedtls_ssl_own_cert(ssl),
  2635. md_alg, hash, hashlen);
  2636. switch (ret) {
  2637. case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
  2638. /* act as if f_async_sign was null */
  2639. break;
  2640. case 0:
  2641. ssl->handshake->async_in_progress = 1;
  2642. return ssl_resume_server_key_exchange(ssl, signature_len);
  2643. case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
  2644. ssl->handshake->async_in_progress = 1;
  2645. return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS;
  2646. default:
  2647. MBEDTLS_SSL_DEBUG_RET(1, "f_async_sign_start", ret);
  2648. return ret;
  2649. }
  2650. }
  2651. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2652. if (mbedtls_ssl_own_key(ssl) == NULL) {
  2653. MBEDTLS_SSL_DEBUG_MSG(1, ("got no private key"));
  2654. return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
  2655. }
  2656. /* Append the signature to ssl->out_msg, leaving 2 bytes for the
  2657. * signature length which will be added in ssl_write_server_key_exchange
  2658. * after the call to ssl_prepare_server_key_exchange.
  2659. * ssl_write_server_key_exchange also takes care of incrementing
  2660. * ssl->out_msglen. */
  2661. if ((ret = mbedtls_pk_sign(mbedtls_ssl_own_key(ssl),
  2662. md_alg, hash, hashlen,
  2663. ssl->out_msg + ssl->out_msglen + 2,
  2664. out_buf_len - ssl->out_msglen - 2,
  2665. signature_len,
  2666. ssl->conf->f_rng,
  2667. ssl->conf->p_rng)) != 0) {
  2668. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_sign", ret);
  2669. return ret;
  2670. }
  2671. }
  2672. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
  2673. return 0;
  2674. }
  2675. /* Prepare the ServerKeyExchange message and send it. For ciphersuites
  2676. * that do not include a ServerKeyExchange message, do nothing. Either
  2677. * way, if successful, move on to the next step in the SSL state
  2678. * machine. */
  2679. MBEDTLS_CHECK_RETURN_CRITICAL
  2680. static int ssl_write_server_key_exchange(mbedtls_ssl_context *ssl)
  2681. {
  2682. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2683. size_t signature_len = 0;
  2684. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
  2685. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2686. ssl->handshake->ciphersuite_info;
  2687. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
  2688. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server key exchange"));
  2689. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED)
  2690. /* Extract static ECDH parameters and abort if ServerKeyExchange
  2691. * is not needed. */
  2692. if (mbedtls_ssl_ciphersuite_no_pfs(ciphersuite_info)) {
  2693. /* For suites involving ECDH, extract DH parameters
  2694. * from certificate at this point. */
  2695. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED)
  2696. if (mbedtls_ssl_ciphersuite_uses_ecdh(ciphersuite_info)) {
  2697. ret = ssl_get_ecdh_params_from_cert(ssl);
  2698. if (ret != 0) {
  2699. MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_ecdh_params_from_cert", ret);
  2700. return ret;
  2701. }
  2702. }
  2703. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED */
  2704. /* Key exchanges not involving ephemeral keys don't use
  2705. * ServerKeyExchange, so end here. */
  2706. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write server key exchange"));
  2707. ssl->state++;
  2708. return 0;
  2709. }
  2710. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_NON_PFS_ENABLED */
  2711. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) && \
  2712. defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2713. /* If we have already prepared the message and there is an ongoing
  2714. * signature operation, resume signing. */
  2715. if (ssl->handshake->async_in_progress != 0) {
  2716. MBEDTLS_SSL_DEBUG_MSG(2, ("resuming signature operation"));
  2717. ret = ssl_resume_server_key_exchange(ssl, &signature_len);
  2718. } else
  2719. #endif /* defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) &&
  2720. defined(MBEDTLS_SSL_ASYNC_PRIVATE) */
  2721. {
  2722. /* ServerKeyExchange is needed. Prepare the message. */
  2723. ret = ssl_prepare_server_key_exchange(ssl, &signature_len);
  2724. }
  2725. if (ret != 0) {
  2726. /* If we're starting to write a new message, set ssl->out_msglen
  2727. * to 0. But if we're resuming after an asynchronous message,
  2728. * out_msglen is the amount of data written so far and mst be
  2729. * preserved. */
  2730. if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  2731. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server key exchange (pending)"));
  2732. } else {
  2733. ssl->out_msglen = 0;
  2734. }
  2735. return ret;
  2736. }
  2737. /* If there is a signature, write its length.
  2738. * ssl_prepare_server_key_exchange already wrote the signature
  2739. * itself at its proper place in the output buffer. */
  2740. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2741. if (signature_len != 0) {
  2742. ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1(signature_len);
  2743. ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0(signature_len);
  2744. MBEDTLS_SSL_DEBUG_BUF(3, "my signature",
  2745. ssl->out_msg + ssl->out_msglen,
  2746. signature_len);
  2747. /* Skip over the already-written signature */
  2748. ssl->out_msglen += signature_len;
  2749. }
  2750. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
  2751. /* Add header and send. */
  2752. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2753. ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE;
  2754. ssl->state++;
  2755. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  2756. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  2757. return ret;
  2758. }
  2759. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server key exchange"));
  2760. return 0;
  2761. }
  2762. MBEDTLS_CHECK_RETURN_CRITICAL
  2763. static int ssl_write_server_hello_done(mbedtls_ssl_context *ssl)
  2764. {
  2765. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2766. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello done"));
  2767. ssl->out_msglen = 4;
  2768. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2769. ssl->out_msg[0] = MBEDTLS_SSL_HS_SERVER_HELLO_DONE;
  2770. ssl->state++;
  2771. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2772. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  2773. mbedtls_ssl_send_flight_completed(ssl);
  2774. }
  2775. #endif
  2776. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  2777. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  2778. return ret;
  2779. }
  2780. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2781. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  2782. (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
  2783. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
  2784. return ret;
  2785. }
  2786. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  2787. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello done"));
  2788. return 0;
  2789. }
  2790. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  2791. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  2792. MBEDTLS_CHECK_RETURN_CRITICAL
  2793. static int ssl_parse_client_dh_public(mbedtls_ssl_context *ssl, unsigned char **p,
  2794. const unsigned char *end)
  2795. {
  2796. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2797. size_t n;
  2798. /*
  2799. * Receive G^Y mod P, premaster = (G^Y)^X mod P
  2800. */
  2801. if (*p + 2 > end) {
  2802. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  2803. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2804. }
  2805. n = ((*p)[0] << 8) | (*p)[1];
  2806. *p += 2;
  2807. if (*p + n > end) {
  2808. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  2809. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2810. }
  2811. if ((ret = mbedtls_dhm_read_public(&ssl->handshake->dhm_ctx, *p, n)) != 0) {
  2812. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_read_public", ret);
  2813. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2814. }
  2815. *p += n;
  2816. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: GY", &ssl->handshake->dhm_ctx.GY);
  2817. return ret;
  2818. }
  2819. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  2820. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  2821. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  2822. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  2823. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2824. MBEDTLS_CHECK_RETURN_CRITICAL
  2825. static int ssl_resume_decrypt_pms(mbedtls_ssl_context *ssl,
  2826. unsigned char *peer_pms,
  2827. size_t *peer_pmslen,
  2828. size_t peer_pmssize)
  2829. {
  2830. int ret = ssl->conf->f_async_resume(ssl,
  2831. peer_pms, peer_pmslen, peer_pmssize);
  2832. if (ret != MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  2833. ssl->handshake->async_in_progress = 0;
  2834. mbedtls_ssl_set_async_operation_data(ssl, NULL);
  2835. }
  2836. MBEDTLS_SSL_DEBUG_RET(2, "ssl_decrypt_encrypted_pms", ret);
  2837. return ret;
  2838. }
  2839. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2840. MBEDTLS_CHECK_RETURN_CRITICAL
  2841. static int ssl_decrypt_encrypted_pms(mbedtls_ssl_context *ssl,
  2842. const unsigned char *p,
  2843. const unsigned char *end,
  2844. unsigned char *peer_pms,
  2845. size_t *peer_pmslen,
  2846. size_t peer_pmssize)
  2847. {
  2848. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2849. mbedtls_x509_crt *own_cert = mbedtls_ssl_own_cert(ssl);
  2850. if (own_cert == NULL) {
  2851. MBEDTLS_SSL_DEBUG_MSG(1, ("got no local certificate"));
  2852. return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
  2853. }
  2854. mbedtls_pk_context *public_key = &own_cert->pk;
  2855. mbedtls_pk_context *private_key = mbedtls_ssl_own_key(ssl);
  2856. size_t len = mbedtls_pk_get_len(public_key);
  2857. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2858. /* If we have already started decoding the message and there is an ongoing
  2859. * decryption operation, resume signing. */
  2860. if (ssl->handshake->async_in_progress != 0) {
  2861. MBEDTLS_SSL_DEBUG_MSG(2, ("resuming decryption operation"));
  2862. return ssl_resume_decrypt_pms(ssl,
  2863. peer_pms, peer_pmslen, peer_pmssize);
  2864. }
  2865. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2866. /*
  2867. * Prepare to decrypt the premaster using own private RSA key
  2868. */
  2869. if (p + 2 > end) {
  2870. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  2871. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2872. }
  2873. if (*p++ != MBEDTLS_BYTE_1(len) ||
  2874. *p++ != MBEDTLS_BYTE_0(len)) {
  2875. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  2876. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2877. }
  2878. if (p + len != end) {
  2879. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  2880. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  2881. }
  2882. /*
  2883. * Decrypt the premaster secret
  2884. */
  2885. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2886. if (ssl->conf->f_async_decrypt_start != NULL) {
  2887. ret = ssl->conf->f_async_decrypt_start(ssl,
  2888. mbedtls_ssl_own_cert(ssl),
  2889. p, len);
  2890. switch (ret) {
  2891. case MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH:
  2892. /* act as if f_async_decrypt_start was null */
  2893. break;
  2894. case 0:
  2895. ssl->handshake->async_in_progress = 1;
  2896. return ssl_resume_decrypt_pms(ssl,
  2897. peer_pms,
  2898. peer_pmslen,
  2899. peer_pmssize);
  2900. case MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS:
  2901. ssl->handshake->async_in_progress = 1;
  2902. return MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS;
  2903. default:
  2904. MBEDTLS_SSL_DEBUG_RET(1, "f_async_decrypt_start", ret);
  2905. return ret;
  2906. }
  2907. }
  2908. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2909. if (!mbedtls_pk_can_do(private_key, MBEDTLS_PK_RSA)) {
  2910. MBEDTLS_SSL_DEBUG_MSG(1, ("got no RSA private key"));
  2911. return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
  2912. }
  2913. ret = mbedtls_pk_decrypt(private_key, p, len,
  2914. peer_pms, peer_pmslen, peer_pmssize,
  2915. ssl->conf->f_rng, ssl->conf->p_rng);
  2916. return ret;
  2917. }
  2918. MBEDTLS_CHECK_RETURN_CRITICAL
  2919. static int ssl_parse_encrypted_pms(mbedtls_ssl_context *ssl,
  2920. const unsigned char *p,
  2921. const unsigned char *end,
  2922. size_t pms_offset)
  2923. {
  2924. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2925. unsigned char *pms = ssl->handshake->premaster + pms_offset;
  2926. unsigned char ver[2];
  2927. unsigned char fake_pms[48], peer_pms[48];
  2928. unsigned char mask;
  2929. size_t i, peer_pmslen;
  2930. unsigned int diff;
  2931. /* In case of a failure in decryption, the decryption may write less than
  2932. * 2 bytes of output, but we always read the first two bytes. It doesn't
  2933. * matter in the end because diff will be nonzero in that case due to
  2934. * ret being nonzero, and we only care whether diff is 0.
  2935. * But do initialize peer_pms and peer_pmslen for robustness anyway. This
  2936. * also makes memory analyzers happy (don't access uninitialized memory,
  2937. * even if it's an unsigned char). */
  2938. peer_pms[0] = peer_pms[1] = ~0;
  2939. peer_pmslen = 0;
  2940. ret = ssl_decrypt_encrypted_pms(ssl, p, end,
  2941. peer_pms,
  2942. &peer_pmslen,
  2943. sizeof(peer_pms));
  2944. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2945. if (ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) {
  2946. return ret;
  2947. }
  2948. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2949. mbedtls_ssl_write_version(ver, ssl->conf->transport,
  2950. ssl->session_negotiate->tls_version);
  2951. /* Avoid data-dependent branches while checking for invalid
  2952. * padding, to protect against timing-based Bleichenbacher-type
  2953. * attacks. */
  2954. diff = (unsigned int) ret;
  2955. diff |= peer_pmslen ^ 48;
  2956. diff |= peer_pms[0] ^ ver[0];
  2957. diff |= peer_pms[1] ^ ver[1];
  2958. /* mask = diff ? 0xff : 0x00 using bit operations to avoid branches */
  2959. mask = mbedtls_ct_uint_mask(diff);
  2960. /*
  2961. * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
  2962. * must not cause the connection to end immediately; instead, send a
  2963. * bad_record_mac later in the handshake.
  2964. * To protect against timing-based variants of the attack, we must
  2965. * not have any branch that depends on whether the decryption was
  2966. * successful. In particular, always generate the fake premaster secret,
  2967. * regardless of whether it will ultimately influence the output or not.
  2968. */
  2969. ret = ssl->conf->f_rng(ssl->conf->p_rng, fake_pms, sizeof(fake_pms));
  2970. if (ret != 0) {
  2971. /* It's ok to abort on an RNG failure, since this does not reveal
  2972. * anything about the RSA decryption. */
  2973. return ret;
  2974. }
  2975. #if defined(MBEDTLS_SSL_DEBUG_ALL)
  2976. if (diff != 0) {
  2977. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  2978. }
  2979. #endif
  2980. if (sizeof(ssl->handshake->premaster) < pms_offset ||
  2981. sizeof(ssl->handshake->premaster) - pms_offset < 48) {
  2982. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  2983. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  2984. }
  2985. ssl->handshake->pmslen = 48;
  2986. /* Set pms to either the true or the fake PMS, without
  2987. * data-dependent branches. */
  2988. for (i = 0; i < ssl->handshake->pmslen; i++) {
  2989. pms[i] = (mask & fake_pms[i]) | ((~mask) & peer_pms[i]);
  2990. }
  2991. return 0;
  2992. }
  2993. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
  2994. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  2995. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  2996. MBEDTLS_CHECK_RETURN_CRITICAL
  2997. static int ssl_parse_client_psk_identity(mbedtls_ssl_context *ssl, unsigned char **p,
  2998. const unsigned char *end)
  2999. {
  3000. int ret = 0;
  3001. uint16_t n;
  3002. if (ssl_conf_has_psk_or_cb(ssl->conf) == 0) {
  3003. MBEDTLS_SSL_DEBUG_MSG(1, ("got no pre-shared key"));
  3004. return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
  3005. }
  3006. /*
  3007. * Receive client pre-shared key identity name
  3008. */
  3009. if (end - *p < 2) {
  3010. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3011. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3012. }
  3013. n = ((*p)[0] << 8) | (*p)[1];
  3014. *p += 2;
  3015. if (n == 0 || n > end - *p) {
  3016. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3017. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3018. }
  3019. if (ssl->conf->f_psk != NULL) {
  3020. if (ssl->conf->f_psk(ssl->conf->p_psk, ssl, *p, n) != 0) {
  3021. ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
  3022. }
  3023. } else {
  3024. /* Identity is not a big secret since clients send it in the clear,
  3025. * but treat it carefully anyway, just in case */
  3026. if (n != ssl->conf->psk_identity_len ||
  3027. mbedtls_ct_memcmp(ssl->conf->psk_identity, *p, n) != 0) {
  3028. ret = MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
  3029. }
  3030. }
  3031. if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
  3032. MBEDTLS_SSL_DEBUG_BUF(3, "Unknown PSK identity", *p, n);
  3033. mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3034. MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY);
  3035. return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
  3036. }
  3037. *p += n;
  3038. return 0;
  3039. }
  3040. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  3041. MBEDTLS_CHECK_RETURN_CRITICAL
  3042. static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
  3043. {
  3044. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3045. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  3046. unsigned char *p, *end;
  3047. ciphersuite_info = ssl->handshake->ciphersuite_info;
  3048. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client key exchange"));
  3049. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) && \
  3050. (defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  3051. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED))
  3052. if ((ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  3053. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) &&
  3054. (ssl->handshake->async_in_progress != 0)) {
  3055. /* We've already read a record and there is an asynchronous
  3056. * operation in progress to decrypt it. So skip reading the
  3057. * record. */
  3058. MBEDTLS_SSL_DEBUG_MSG(3, ("will resume decryption of previously-read record"));
  3059. } else
  3060. #endif
  3061. if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
  3062. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
  3063. return ret;
  3064. }
  3065. p = ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl);
  3066. end = ssl->in_msg + ssl->in_hslen;
  3067. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
  3068. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3069. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  3070. }
  3071. if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE) {
  3072. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange message"));
  3073. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  3074. }
  3075. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  3076. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA) {
  3077. if ((ret = ssl_parse_client_dh_public(ssl, &p, end)) != 0) {
  3078. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_dh_public"), ret);
  3079. return ret;
  3080. }
  3081. if (p != end) {
  3082. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
  3083. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3084. }
  3085. if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
  3086. ssl->handshake->premaster,
  3087. MBEDTLS_PREMASTER_SIZE,
  3088. &ssl->handshake->pmslen,
  3089. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  3090. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
  3091. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3092. }
  3093. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
  3094. } else
  3095. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  3096. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  3097. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  3098. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  3099. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  3100. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  3101. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
  3102. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  3103. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
  3104. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3105. size_t data_len = (size_t) (*p++);
  3106. size_t buf_len = (size_t) (end - p);
  3107. psa_status_t status = PSA_ERROR_GENERIC_ERROR;
  3108. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  3109. MBEDTLS_SSL_DEBUG_MSG(1, ("Read the peer's public key."));
  3110. /*
  3111. * We must have at least two bytes (1 for length, at least 1 for data)
  3112. */
  3113. if (buf_len < 2) {
  3114. MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid buffer length"));
  3115. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  3116. }
  3117. if (data_len < 1 || data_len > buf_len) {
  3118. MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid data length"));
  3119. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  3120. }
  3121. /* Store peer's ECDH public key. */
  3122. memcpy(handshake->ecdh_psa_peerkey, p, data_len);
  3123. handshake->ecdh_psa_peerkey_len = data_len;
  3124. /* Compute ECDH shared secret. */
  3125. status = psa_raw_key_agreement(
  3126. PSA_ALG_ECDH, handshake->ecdh_psa_privkey,
  3127. handshake->ecdh_psa_peerkey, handshake->ecdh_psa_peerkey_len,
  3128. handshake->premaster, sizeof(handshake->premaster),
  3129. &handshake->pmslen);
  3130. if (status != PSA_SUCCESS) {
  3131. ret = PSA_TO_MBEDTLS_ERR(status);
  3132. MBEDTLS_SSL_DEBUG_RET(1, "psa_raw_key_agreement", ret);
  3133. if (handshake->ecdh_psa_privkey_is_external == 0) {
  3134. (void) psa_destroy_key(handshake->ecdh_psa_privkey);
  3135. }
  3136. handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  3137. return ret;
  3138. }
  3139. if (handshake->ecdh_psa_privkey_is_external == 0) {
  3140. status = psa_destroy_key(handshake->ecdh_psa_privkey);
  3141. if (status != PSA_SUCCESS) {
  3142. ret = PSA_TO_MBEDTLS_ERR(status);
  3143. MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
  3144. return ret;
  3145. }
  3146. }
  3147. handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  3148. #else
  3149. if ((ret = mbedtls_ecdh_read_public(&ssl->handshake->ecdh_ctx,
  3150. p, end - p)) != 0) {
  3151. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret);
  3152. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3153. }
  3154. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  3155. MBEDTLS_DEBUG_ECDH_QP);
  3156. if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
  3157. &ssl->handshake->pmslen,
  3158. ssl->handshake->premaster,
  3159. MBEDTLS_MPI_MAX_SIZE,
  3160. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  3161. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
  3162. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3163. }
  3164. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  3165. MBEDTLS_DEBUG_ECDH_Z);
  3166. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3167. } else
  3168. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  3169. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  3170. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  3171. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  3172. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  3173. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK) {
  3174. if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
  3175. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
  3176. return ret;
  3177. }
  3178. if (p != end) {
  3179. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
  3180. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3181. }
  3182. #if !defined(MBEDTLS_USE_PSA_CRYPTO)
  3183. if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
  3184. ciphersuite_info->key_exchange)) != 0) {
  3185. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
  3186. return ret;
  3187. }
  3188. #endif /* !MBEDTLS_USE_PSA_CRYPTO */
  3189. } else
  3190. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
  3191. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  3192. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
  3193. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  3194. if (ssl->handshake->async_in_progress != 0) {
  3195. /* There is an asynchronous operation in progress to
  3196. * decrypt the encrypted premaster secret, so skip
  3197. * directly to resuming this operation. */
  3198. MBEDTLS_SSL_DEBUG_MSG(3, ("PSK identity already parsed"));
  3199. /* Update p to skip the PSK identity. ssl_parse_encrypted_pms
  3200. * won't actually use it, but maintain p anyway for robustness. */
  3201. p += ssl->conf->psk_identity_len + 2;
  3202. } else
  3203. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  3204. if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
  3205. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
  3206. return ret;
  3207. }
  3208. if ((ret = ssl_parse_encrypted_pms(ssl, p, end, 2)) != 0) {
  3209. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_encrypted_pms"), ret);
  3210. return ret;
  3211. }
  3212. #if !defined(MBEDTLS_USE_PSA_CRYPTO)
  3213. if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
  3214. ciphersuite_info->key_exchange)) != 0) {
  3215. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
  3216. return ret;
  3217. }
  3218. #endif /* !MBEDTLS_USE_PSA_CRYPTO */
  3219. } else
  3220. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  3221. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  3222. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
  3223. if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
  3224. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
  3225. return ret;
  3226. }
  3227. if ((ret = ssl_parse_client_dh_public(ssl, &p, end)) != 0) {
  3228. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_dh_public"), ret);
  3229. return ret;
  3230. }
  3231. if (p != end) {
  3232. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client key exchange"));
  3233. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3234. }
  3235. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3236. unsigned char *pms = ssl->handshake->premaster;
  3237. unsigned char *pms_end = pms + sizeof(ssl->handshake->premaster);
  3238. size_t pms_len;
  3239. /* Write length only when we know the actual value */
  3240. if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
  3241. pms + 2, pms_end - (pms + 2), &pms_len,
  3242. ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
  3243. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
  3244. return ret;
  3245. }
  3246. MBEDTLS_PUT_UINT16_BE(pms_len, pms, 0);
  3247. pms += 2 + pms_len;
  3248. MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
  3249. #else
  3250. if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
  3251. ciphersuite_info->key_exchange)) != 0) {
  3252. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
  3253. return ret;
  3254. }
  3255. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3256. } else
  3257. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  3258. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  3259. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
  3260. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3261. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  3262. psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
  3263. uint8_t ecpoint_len;
  3264. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  3265. if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
  3266. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
  3267. psa_destroy_key(handshake->ecdh_psa_privkey);
  3268. handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  3269. return ret;
  3270. }
  3271. /* Keep a copy of the peer's public key */
  3272. if (p >= end) {
  3273. psa_destroy_key(handshake->ecdh_psa_privkey);
  3274. handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  3275. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3276. }
  3277. ecpoint_len = *(p++);
  3278. if ((size_t) (end - p) < ecpoint_len) {
  3279. psa_destroy_key(handshake->ecdh_psa_privkey);
  3280. handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  3281. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3282. }
  3283. if (ecpoint_len > sizeof(handshake->ecdh_psa_peerkey)) {
  3284. psa_destroy_key(handshake->ecdh_psa_privkey);
  3285. handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  3286. return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  3287. }
  3288. memcpy(handshake->ecdh_psa_peerkey, p, ecpoint_len);
  3289. handshake->ecdh_psa_peerkey_len = ecpoint_len;
  3290. p += ecpoint_len;
  3291. /* As RFC 5489 section 2, the premaster secret is formed as follows:
  3292. * - a uint16 containing the length (in octets) of the ECDH computation
  3293. * - the octet string produced by the ECDH computation
  3294. * - a uint16 containing the length (in octets) of the PSK
  3295. * - the PSK itself
  3296. */
  3297. unsigned char *psm = ssl->handshake->premaster;
  3298. const unsigned char * const psm_end =
  3299. psm + sizeof(ssl->handshake->premaster);
  3300. /* uint16 to store length (in octets) of the ECDH computation */
  3301. const size_t zlen_size = 2;
  3302. size_t zlen = 0;
  3303. /* Compute ECDH shared secret. */
  3304. status = psa_raw_key_agreement(PSA_ALG_ECDH,
  3305. handshake->ecdh_psa_privkey,
  3306. handshake->ecdh_psa_peerkey,
  3307. handshake->ecdh_psa_peerkey_len,
  3308. psm + zlen_size,
  3309. psm_end - (psm + zlen_size),
  3310. &zlen);
  3311. destruction_status = psa_destroy_key(handshake->ecdh_psa_privkey);
  3312. handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  3313. if (status != PSA_SUCCESS) {
  3314. return PSA_TO_MBEDTLS_ERR(status);
  3315. } else if (destruction_status != PSA_SUCCESS) {
  3316. return PSA_TO_MBEDTLS_ERR(destruction_status);
  3317. }
  3318. /* Write the ECDH computation length before the ECDH computation */
  3319. MBEDTLS_PUT_UINT16_BE(zlen, psm, 0);
  3320. psm += zlen_size + zlen;
  3321. #else /* MBEDTLS_USE_PSA_CRYPTO */
  3322. if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
  3323. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
  3324. return ret;
  3325. }
  3326. if ((ret = mbedtls_ecdh_read_public(&ssl->handshake->ecdh_ctx,
  3327. p, end - p)) != 0) {
  3328. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret);
  3329. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3330. }
  3331. MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
  3332. MBEDTLS_DEBUG_ECDH_QP);
  3333. if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
  3334. ciphersuite_info->key_exchange)) != 0) {
  3335. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
  3336. return ret;
  3337. }
  3338. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3339. } else
  3340. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  3341. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  3342. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA) {
  3343. if ((ret = ssl_parse_encrypted_pms(ssl, p, end, 0)) != 0) {
  3344. MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_parse_encrypted_pms_secret"), ret);
  3345. return ret;
  3346. }
  3347. } else
  3348. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  3349. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  3350. if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
  3351. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3352. if ((ret = mbedtls_psa_ecjpake_read_round(
  3353. &ssl->handshake->psa_pake_ctx, p, end - p,
  3354. MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
  3355. psa_destroy_key(ssl->handshake->psa_pake_password);
  3356. psa_pake_abort(&ssl->handshake->psa_pake_ctx);
  3357. MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
  3358. return ret;
  3359. }
  3360. #else
  3361. ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
  3362. p, end - p);
  3363. if (ret != 0) {
  3364. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
  3365. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3366. }
  3367. ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
  3368. ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
  3369. ssl->conf->f_rng, ssl->conf->p_rng);
  3370. if (ret != 0) {
  3371. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
  3372. return ret;
  3373. }
  3374. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3375. } else
  3376. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  3377. {
  3378. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  3379. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3380. }
  3381. if ((ret = mbedtls_ssl_derive_keys(ssl)) != 0) {
  3382. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_derive_keys", ret);
  3383. return ret;
  3384. }
  3385. ssl->state++;
  3386. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client key exchange"));
  3387. return 0;
  3388. }
  3389. #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
  3390. MBEDTLS_CHECK_RETURN_CRITICAL
  3391. static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
  3392. {
  3393. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  3394. ssl->handshake->ciphersuite_info;
  3395. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
  3396. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  3397. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
  3398. ssl->state++;
  3399. return 0;
  3400. }
  3401. MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
  3402. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3403. }
  3404. #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  3405. MBEDTLS_CHECK_RETURN_CRITICAL
  3406. static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl)
  3407. {
  3408. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3409. size_t i, sig_len;
  3410. unsigned char hash[48];
  3411. unsigned char *hash_start = hash;
  3412. size_t hashlen;
  3413. mbedtls_pk_type_t pk_alg;
  3414. mbedtls_md_type_t md_alg;
  3415. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  3416. ssl->handshake->ciphersuite_info;
  3417. mbedtls_pk_context *peer_pk;
  3418. MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
  3419. if (!mbedtls_ssl_ciphersuite_cert_req_allowed(ciphersuite_info)) {
  3420. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
  3421. ssl->state++;
  3422. return 0;
  3423. }
  3424. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  3425. if (ssl->session_negotiate->peer_cert == NULL) {
  3426. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
  3427. ssl->state++;
  3428. return 0;
  3429. }
  3430. #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  3431. if (ssl->session_negotiate->peer_cert_digest == NULL) {
  3432. MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate verify"));
  3433. ssl->state++;
  3434. return 0;
  3435. }
  3436. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  3437. /* Read the message without adding it to the checksum */
  3438. ret = mbedtls_ssl_read_record(ssl, 0 /* no checksum update */);
  3439. if (0 != ret) {
  3440. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_read_record"), ret);
  3441. return ret;
  3442. }
  3443. ssl->state++;
  3444. /* Process the message contents */
  3445. if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
  3446. ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE_VERIFY) {
  3447. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
  3448. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  3449. }
  3450. i = mbedtls_ssl_hs_hdr_len(ssl);
  3451. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  3452. peer_pk = &ssl->handshake->peer_pubkey;
  3453. #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  3454. if (ssl->session_negotiate->peer_cert == NULL) {
  3455. /* Should never happen */
  3456. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  3457. }
  3458. peer_pk = &ssl->session_negotiate->peer_cert->pk;
  3459. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  3460. /*
  3461. * struct {
  3462. * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
  3463. * opaque signature<0..2^16-1>;
  3464. * } DigitallySigned;
  3465. */
  3466. if (i + 2 > ssl->in_hslen) {
  3467. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
  3468. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3469. }
  3470. /*
  3471. * Hash
  3472. */
  3473. md_alg = mbedtls_ssl_md_alg_from_hash(ssl->in_msg[i]);
  3474. if (md_alg == MBEDTLS_MD_NONE || mbedtls_ssl_set_calc_verify_md(ssl, ssl->in_msg[i])) {
  3475. MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
  3476. " for verify message"));
  3477. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  3478. }
  3479. #if !defined(MBEDTLS_MD_SHA1)
  3480. if (MBEDTLS_MD_SHA1 == md_alg) {
  3481. hash_start += 16;
  3482. }
  3483. #endif
  3484. /* Info from md_alg will be used instead */
  3485. hashlen = 0;
  3486. i++;
  3487. /*
  3488. * Signature
  3489. */
  3490. if ((pk_alg = mbedtls_ssl_pk_alg_from_sig(ssl->in_msg[i]))
  3491. == MBEDTLS_PK_NONE) {
  3492. MBEDTLS_SSL_DEBUG_MSG(1, ("peer not adhering to requested sig_alg"
  3493. " for verify message"));
  3494. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  3495. }
  3496. /*
  3497. * Check the certificate's key type matches the signature alg
  3498. */
  3499. if (!mbedtls_pk_can_do(peer_pk, pk_alg)) {
  3500. MBEDTLS_SSL_DEBUG_MSG(1, ("sig_alg doesn't match cert key"));
  3501. return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
  3502. }
  3503. i++;
  3504. if (i + 2 > ssl->in_hslen) {
  3505. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
  3506. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3507. }
  3508. sig_len = (ssl->in_msg[i] << 8) | ssl->in_msg[i+1];
  3509. i += 2;
  3510. if (i + sig_len != ssl->in_hslen) {
  3511. MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate verify message"));
  3512. return MBEDTLS_ERR_SSL_DECODE_ERROR;
  3513. }
  3514. /* Calculate hash and verify signature */
  3515. {
  3516. size_t dummy_hlen;
  3517. ret = ssl->handshake->calc_verify(ssl, hash, &dummy_hlen);
  3518. if (0 != ret) {
  3519. MBEDTLS_SSL_DEBUG_RET(1, ("calc_verify"), ret);
  3520. return ret;
  3521. }
  3522. }
  3523. if ((ret = mbedtls_pk_verify(peer_pk,
  3524. md_alg, hash_start, hashlen,
  3525. ssl->in_msg + i, sig_len)) != 0) {
  3526. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify", ret);
  3527. return ret;
  3528. }
  3529. ret = mbedtls_ssl_update_handshake_status(ssl);
  3530. if (0 != ret) {
  3531. MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_update_handshake_status"), ret);
  3532. return ret;
  3533. }
  3534. MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
  3535. return ret;
  3536. }
  3537. #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  3538. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3539. MBEDTLS_CHECK_RETURN_CRITICAL
  3540. static int ssl_write_new_session_ticket(mbedtls_ssl_context *ssl)
  3541. {
  3542. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3543. size_t tlen;
  3544. uint32_t lifetime;
  3545. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write new session ticket"));
  3546. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  3547. ssl->out_msg[0] = MBEDTLS_SSL_HS_NEW_SESSION_TICKET;
  3548. /*
  3549. * struct {
  3550. * uint32 ticket_lifetime_hint;
  3551. * opaque ticket<0..2^16-1>;
  3552. * } NewSessionTicket;
  3553. *
  3554. * 4 . 7 ticket_lifetime_hint (0 = unspecified)
  3555. * 8 . 9 ticket_len (n)
  3556. * 10 . 9+n ticket content
  3557. */
  3558. if ((ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
  3559. ssl->session_negotiate,
  3560. ssl->out_msg + 10,
  3561. ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
  3562. &tlen, &lifetime)) != 0) {
  3563. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_ticket_write", ret);
  3564. tlen = 0;
  3565. }
  3566. MBEDTLS_PUT_UINT32_BE(lifetime, ssl->out_msg, 4);
  3567. MBEDTLS_PUT_UINT16_BE(tlen, ssl->out_msg, 8);
  3568. ssl->out_msglen = 10 + tlen;
  3569. /*
  3570. * Morally equivalent to updating ssl->state, but NewSessionTicket and
  3571. * ChangeCipherSpec share the same state.
  3572. */
  3573. ssl->handshake->new_session_ticket = 0;
  3574. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  3575. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  3576. return ret;
  3577. }
  3578. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
  3579. return 0;
  3580. }
  3581. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  3582. /*
  3583. * SSL handshake -- server side -- single step
  3584. */
  3585. int mbedtls_ssl_handshake_server_step(mbedtls_ssl_context *ssl)
  3586. {
  3587. int ret = 0;
  3588. MBEDTLS_SSL_DEBUG_MSG(2, ("server state: %d", ssl->state));
  3589. switch (ssl->state) {
  3590. case MBEDTLS_SSL_HELLO_REQUEST:
  3591. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  3592. break;
  3593. /*
  3594. * <== ClientHello
  3595. */
  3596. case MBEDTLS_SSL_CLIENT_HELLO:
  3597. ret = ssl_parse_client_hello(ssl);
  3598. break;
  3599. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3600. case MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
  3601. return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
  3602. #endif
  3603. /*
  3604. * ==> ServerHello
  3605. * Certificate
  3606. * ( ServerKeyExchange )
  3607. * ( CertificateRequest )
  3608. * ServerHelloDone
  3609. */
  3610. case MBEDTLS_SSL_SERVER_HELLO:
  3611. ret = ssl_write_server_hello(ssl);
  3612. break;
  3613. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  3614. ret = mbedtls_ssl_write_certificate(ssl);
  3615. break;
  3616. case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
  3617. ret = ssl_write_server_key_exchange(ssl);
  3618. break;
  3619. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  3620. ret = ssl_write_certificate_request(ssl);
  3621. break;
  3622. case MBEDTLS_SSL_SERVER_HELLO_DONE:
  3623. ret = ssl_write_server_hello_done(ssl);
  3624. break;
  3625. /*
  3626. * <== ( Certificate/Alert )
  3627. * ClientKeyExchange
  3628. * ( CertificateVerify )
  3629. * ChangeCipherSpec
  3630. * Finished
  3631. */
  3632. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  3633. ret = mbedtls_ssl_parse_certificate(ssl);
  3634. break;
  3635. case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
  3636. ret = ssl_parse_client_key_exchange(ssl);
  3637. break;
  3638. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  3639. ret = ssl_parse_certificate_verify(ssl);
  3640. break;
  3641. case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
  3642. ret = mbedtls_ssl_parse_change_cipher_spec(ssl);
  3643. break;
  3644. case MBEDTLS_SSL_CLIENT_FINISHED:
  3645. ret = mbedtls_ssl_parse_finished(ssl);
  3646. break;
  3647. /*
  3648. * ==> ( NewSessionTicket )
  3649. * ChangeCipherSpec
  3650. * Finished
  3651. */
  3652. case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
  3653. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3654. if (ssl->handshake->new_session_ticket != 0) {
  3655. ret = ssl_write_new_session_ticket(ssl);
  3656. } else
  3657. #endif
  3658. ret = mbedtls_ssl_write_change_cipher_spec(ssl);
  3659. break;
  3660. case MBEDTLS_SSL_SERVER_FINISHED:
  3661. ret = mbedtls_ssl_write_finished(ssl);
  3662. break;
  3663. case MBEDTLS_SSL_FLUSH_BUFFERS:
  3664. MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
  3665. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  3666. break;
  3667. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  3668. mbedtls_ssl_handshake_wrapup(ssl);
  3669. break;
  3670. default:
  3671. MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
  3672. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  3673. }
  3674. return ret;
  3675. }
  3676. void mbedtls_ssl_conf_preference_order(mbedtls_ssl_config *conf, int order)
  3677. {
  3678. conf->respect_cli_pref = order;
  3679. }
  3680. #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_2 */