ssl_cli.c 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533
  1. /*
  2. * SSLv3/TLSv1 client-side functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_SSL_CLI_C)
  21. #if defined(MBEDTLS_PLATFORM_C)
  22. #include "mbedtls/platform.h"
  23. #else
  24. #include <stdlib.h>
  25. #define mbedtls_calloc calloc
  26. #define mbedtls_free free
  27. #endif
  28. #include "mbedtls/ssl.h"
  29. #include "mbedtls/ssl_internal.h"
  30. #include "mbedtls/debug.h"
  31. #include "mbedtls/error.h"
  32. #include "mbedtls/constant_time.h"
  33. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  34. #include "mbedtls/psa_util.h"
  35. #include "psa/crypto.h"
  36. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  37. #include <string.h>
  38. #include <stdint.h>
  39. #if defined(MBEDTLS_HAVE_TIME)
  40. #include "mbedtls/platform_time.h"
  41. #endif
  42. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  43. #include "mbedtls/platform_util.h"
  44. #endif
  45. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  46. static int ssl_conf_has_static_psk( mbedtls_ssl_config const *conf )
  47. {
  48. if( conf->psk_identity == NULL ||
  49. conf->psk_identity_len == 0 )
  50. {
  51. return( 0 );
  52. }
  53. if( conf->psk != NULL && conf->psk_len != 0 )
  54. return( 1 );
  55. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  56. if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
  57. return( 1 );
  58. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  59. return( 0 );
  60. }
  61. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  62. static int ssl_conf_has_static_raw_psk( mbedtls_ssl_config const *conf )
  63. {
  64. if( conf->psk_identity == NULL ||
  65. conf->psk_identity_len == 0 )
  66. {
  67. return( 0 );
  68. }
  69. if( conf->psk != NULL && conf->psk_len != 0 )
  70. return( 1 );
  71. return( 0 );
  72. }
  73. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  74. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  75. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  76. static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
  77. unsigned char *buf,
  78. const unsigned char *end,
  79. size_t *olen )
  80. {
  81. unsigned char *p = buf;
  82. size_t hostname_len;
  83. *olen = 0;
  84. if( ssl->hostname == NULL )
  85. return( 0 );
  86. MBEDTLS_SSL_DEBUG_MSG( 3,
  87. ( "client hello, adding server name extension: %s",
  88. ssl->hostname ) );
  89. hostname_len = strlen( ssl->hostname );
  90. MBEDTLS_SSL_CHK_BUF_PTR( p, end, hostname_len + 9 );
  91. /*
  92. * Sect. 3, RFC 6066 (TLS Extensions Definitions)
  93. *
  94. * In order to provide any of the server names, clients MAY include an
  95. * extension of type "server_name" in the (extended) client hello. The
  96. * "extension_data" field of this extension SHALL contain
  97. * "ServerNameList" where:
  98. *
  99. * struct {
  100. * NameType name_type;
  101. * select (name_type) {
  102. * case host_name: HostName;
  103. * } name;
  104. * } ServerName;
  105. *
  106. * enum {
  107. * host_name(0), (255)
  108. * } NameType;
  109. *
  110. * opaque HostName<1..2^16-1>;
  111. *
  112. * struct {
  113. * ServerName server_name_list<1..2^16-1>
  114. * } ServerNameList;
  115. *
  116. */
  117. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 );
  118. p += 2;
  119. MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 );
  120. p += 2;
  121. MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 );
  122. p += 2;
  123. *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME );
  124. MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 );
  125. p += 2;
  126. memcpy( p, ssl->hostname, hostname_len );
  127. *olen = hostname_len + 9;
  128. return( 0 );
  129. }
  130. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  131. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  132. static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
  133. unsigned char *buf,
  134. const unsigned char *end,
  135. size_t *olen )
  136. {
  137. unsigned char *p = buf;
  138. *olen = 0;
  139. /* We're always including an TLS_EMPTY_RENEGOTIATION_INFO_SCSV in the
  140. * initial ClientHello, in which case also adding the renegotiation
  141. * info extension is NOT RECOMMENDED as per RFC 5746 Section 3.4. */
  142. if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  143. return( 0 );
  144. MBEDTLS_SSL_DEBUG_MSG( 3,
  145. ( "client hello, adding renegotiation extension" ) );
  146. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 + ssl->verify_data_len );
  147. /*
  148. * Secure renegotiation
  149. */
  150. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 );
  151. p += 2;
  152. *p++ = 0x00;
  153. *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 );
  154. *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len );
  155. memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
  156. *olen = 5 + ssl->verify_data_len;
  157. return( 0 );
  158. }
  159. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  160. /*
  161. * Only if we handle at least one key exchange that needs signatures.
  162. */
  163. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  164. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  165. static int ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl,
  166. unsigned char *buf,
  167. const unsigned char *end,
  168. size_t *olen )
  169. {
  170. unsigned char *p = buf;
  171. size_t sig_alg_len = 0;
  172. const int *md;
  173. #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)
  174. unsigned char *sig_alg_list = buf + 6;
  175. #endif
  176. *olen = 0;
  177. if( ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  178. return( 0 );
  179. MBEDTLS_SSL_DEBUG_MSG( 3,
  180. ( "client hello, adding signature_algorithms extension" ) );
  181. if( ssl->conf->sig_hashes == NULL )
  182. return( MBEDTLS_ERR_SSL_BAD_CONFIG );
  183. for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
  184. {
  185. #if defined(MBEDTLS_ECDSA_C)
  186. sig_alg_len += 2;
  187. #endif
  188. #if defined(MBEDTLS_RSA_C)
  189. sig_alg_len += 2;
  190. #endif
  191. if( sig_alg_len > MBEDTLS_SSL_MAX_SIG_HASH_ALG_LIST_LEN )
  192. {
  193. MBEDTLS_SSL_DEBUG_MSG( 3,
  194. ( "length in bytes of sig-hash-alg extension too big" ) );
  195. return( MBEDTLS_ERR_SSL_BAD_CONFIG );
  196. }
  197. }
  198. /* Empty signature algorithms list, this is a configuration error. */
  199. if( sig_alg_len == 0 )
  200. return( MBEDTLS_ERR_SSL_BAD_CONFIG );
  201. MBEDTLS_SSL_CHK_BUF_PTR( p, end, sig_alg_len + 6 );
  202. /*
  203. * Prepare signature_algorithms extension (TLS 1.2)
  204. */
  205. sig_alg_len = 0;
  206. for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
  207. {
  208. #if defined(MBEDTLS_ECDSA_C)
  209. sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
  210. sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA;
  211. #endif
  212. #if defined(MBEDTLS_RSA_C)
  213. sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
  214. sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA;
  215. #endif
  216. }
  217. /*
  218. * enum {
  219. * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
  220. * sha512(6), (255)
  221. * } HashAlgorithm;
  222. *
  223. * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
  224. * SignatureAlgorithm;
  225. *
  226. * struct {
  227. * HashAlgorithm hash;
  228. * SignatureAlgorithm signature;
  229. * } SignatureAndHashAlgorithm;
  230. *
  231. * SignatureAndHashAlgorithm
  232. * supported_signature_algorithms<2..2^16-2>;
  233. */
  234. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, p, 0 );
  235. p += 2;
  236. MBEDTLS_PUT_UINT16_BE( sig_alg_len + 2, p, 0 );
  237. p += 2;
  238. MBEDTLS_PUT_UINT16_BE( sig_alg_len, p, 0 );
  239. p += 2;
  240. *olen = 6 + sig_alg_len;
  241. return( 0 );
  242. }
  243. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  244. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  245. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  246. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  247. static int ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl,
  248. unsigned char *buf,
  249. const unsigned char *end,
  250. size_t *olen )
  251. {
  252. unsigned char *p = buf;
  253. unsigned char *elliptic_curve_list = p + 6;
  254. size_t elliptic_curve_len = 0;
  255. const mbedtls_ecp_curve_info *info;
  256. const mbedtls_ecp_group_id *grp_id;
  257. *olen = 0;
  258. MBEDTLS_SSL_DEBUG_MSG( 3,
  259. ( "client hello, adding supported_elliptic_curves extension" ) );
  260. if( ssl->conf->curve_list == NULL )
  261. return( MBEDTLS_ERR_SSL_BAD_CONFIG );
  262. for( grp_id = ssl->conf->curve_list;
  263. *grp_id != MBEDTLS_ECP_DP_NONE;
  264. grp_id++ )
  265. {
  266. info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
  267. if( info == NULL )
  268. {
  269. MBEDTLS_SSL_DEBUG_MSG( 1,
  270. ( "invalid curve in ssl configuration" ) );
  271. return( MBEDTLS_ERR_SSL_BAD_CONFIG );
  272. }
  273. elliptic_curve_len += 2;
  274. if( elliptic_curve_len > MBEDTLS_SSL_MAX_CURVE_LIST_LEN )
  275. {
  276. MBEDTLS_SSL_DEBUG_MSG( 3,
  277. ( "malformed supported_elliptic_curves extension in config" ) );
  278. return( MBEDTLS_ERR_SSL_BAD_CONFIG );
  279. }
  280. }
  281. /* Empty elliptic curve list, this is a configuration error. */
  282. if( elliptic_curve_len == 0 )
  283. return( MBEDTLS_ERR_SSL_BAD_CONFIG );
  284. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + elliptic_curve_len );
  285. elliptic_curve_len = 0;
  286. for( grp_id = ssl->conf->curve_list;
  287. *grp_id != MBEDTLS_ECP_DP_NONE;
  288. grp_id++ )
  289. {
  290. info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
  291. elliptic_curve_list[elliptic_curve_len++] = MBEDTLS_BYTE_1( info->tls_id );
  292. elliptic_curve_list[elliptic_curve_len++] = MBEDTLS_BYTE_0( info->tls_id );
  293. }
  294. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES, p, 0 );
  295. p += 2;
  296. MBEDTLS_PUT_UINT16_BE( elliptic_curve_len + 2, p, 0 );
  297. p += 2;
  298. MBEDTLS_PUT_UINT16_BE( elliptic_curve_len, p, 0 );
  299. p += 2;
  300. *olen = 6 + elliptic_curve_len;
  301. return( 0 );
  302. }
  303. static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
  304. unsigned char *buf,
  305. const unsigned char *end,
  306. size_t *olen )
  307. {
  308. unsigned char *p = buf;
  309. (void) ssl; /* ssl used for debugging only */
  310. *olen = 0;
  311. MBEDTLS_SSL_DEBUG_MSG( 3,
  312. ( "client hello, adding supported_point_formats extension" ) );
  313. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
  314. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 );
  315. p += 2;
  316. *p++ = 0x00;
  317. *p++ = 2;
  318. *p++ = 1;
  319. *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
  320. *olen = 6;
  321. return( 0 );
  322. }
  323. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  324. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  325. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  326. static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
  327. unsigned char *buf,
  328. const unsigned char *end,
  329. size_t *olen )
  330. {
  331. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  332. unsigned char *p = buf;
  333. size_t kkpp_len;
  334. *olen = 0;
  335. /* Skip costly extension if we can't use EC J-PAKE anyway */
  336. if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
  337. return( 0 );
  338. MBEDTLS_SSL_DEBUG_MSG( 3,
  339. ( "client hello, adding ecjpake_kkpp extension" ) );
  340. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
  341. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 );
  342. p += 2;
  343. /*
  344. * We may need to send ClientHello multiple times for Hello verification.
  345. * We don't want to compute fresh values every time (both for performance
  346. * and consistency reasons), so cache the extension content.
  347. */
  348. if( ssl->handshake->ecjpake_cache == NULL ||
  349. ssl->handshake->ecjpake_cache_len == 0 )
  350. {
  351. MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
  352. ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
  353. p + 2, end - p - 2, &kkpp_len,
  354. ssl->conf->f_rng, ssl->conf->p_rng );
  355. if( ret != 0 )
  356. {
  357. MBEDTLS_SSL_DEBUG_RET( 1 ,
  358. "mbedtls_ecjpake_write_round_one", ret );
  359. return( ret );
  360. }
  361. ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
  362. if( ssl->handshake->ecjpake_cache == NULL )
  363. {
  364. MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
  365. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  366. }
  367. memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
  368. ssl->handshake->ecjpake_cache_len = kkpp_len;
  369. }
  370. else
  371. {
  372. MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
  373. kkpp_len = ssl->handshake->ecjpake_cache_len;
  374. MBEDTLS_SSL_CHK_BUF_PTR( p + 2, end, kkpp_len );
  375. memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
  376. }
  377. MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 );
  378. p += 2;
  379. *olen = kkpp_len + 4;
  380. return( 0 );
  381. }
  382. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  383. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  384. static int ssl_write_cid_ext( mbedtls_ssl_context *ssl,
  385. unsigned char *buf,
  386. const unsigned char *end,
  387. size_t *olen )
  388. {
  389. unsigned char *p = buf;
  390. size_t ext_len;
  391. /*
  392. * Quoting draft-ietf-tls-dtls-connection-id-05
  393. * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
  394. *
  395. * struct {
  396. * opaque cid<0..2^8-1>;
  397. * } ConnectionId;
  398. */
  399. *olen = 0;
  400. if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
  401. ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
  402. {
  403. return( 0 );
  404. }
  405. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding CID extension" ) );
  406. /* ssl->own_cid_len is at most MBEDTLS_SSL_CID_IN_LEN_MAX
  407. * which is at most 255, so the increment cannot overflow. */
  408. MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) );
  409. /* Add extension ID + size */
  410. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 );
  411. p += 2;
  412. ext_len = (size_t) ssl->own_cid_len + 1;
  413. MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
  414. p += 2;
  415. *p++ = (uint8_t) ssl->own_cid_len;
  416. memcpy( p, ssl->own_cid, ssl->own_cid_len );
  417. *olen = ssl->own_cid_len + 5;
  418. return( 0 );
  419. }
  420. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  421. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  422. static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
  423. unsigned char *buf,
  424. const unsigned char *end,
  425. size_t *olen )
  426. {
  427. unsigned char *p = buf;
  428. *olen = 0;
  429. if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE )
  430. return( 0 );
  431. MBEDTLS_SSL_DEBUG_MSG( 3,
  432. ( "client hello, adding max_fragment_length extension" ) );
  433. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 );
  434. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 );
  435. p += 2;
  436. *p++ = 0x00;
  437. *p++ = 1;
  438. *p++ = ssl->conf->mfl_code;
  439. *olen = 5;
  440. return( 0 );
  441. }
  442. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  443. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  444. static int ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
  445. unsigned char *buf,
  446. const unsigned char *end,
  447. size_t *olen )
  448. {
  449. unsigned char *p = buf;
  450. *olen = 0;
  451. if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
  452. return( 0 );
  453. MBEDTLS_SSL_DEBUG_MSG( 3,
  454. ( "client hello, adding truncated_hmac extension" ) );
  455. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
  456. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_TRUNCATED_HMAC, p, 0 );
  457. p += 2;
  458. *p++ = 0x00;
  459. *p++ = 0x00;
  460. *olen = 4;
  461. return( 0 );
  462. }
  463. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  464. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  465. static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
  466. unsigned char *buf,
  467. const unsigned char *end,
  468. size_t *olen )
  469. {
  470. unsigned char *p = buf;
  471. *olen = 0;
  472. if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
  473. ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  474. return( 0 );
  475. MBEDTLS_SSL_DEBUG_MSG( 3,
  476. ( "client hello, adding encrypt_then_mac extension" ) );
  477. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
  478. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 );
  479. p += 2;
  480. *p++ = 0x00;
  481. *p++ = 0x00;
  482. *olen = 4;
  483. return( 0 );
  484. }
  485. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  486. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  487. static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
  488. unsigned char *buf,
  489. const unsigned char *end,
  490. size_t *olen )
  491. {
  492. unsigned char *p = buf;
  493. *olen = 0;
  494. if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
  495. ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  496. return( 0 );
  497. MBEDTLS_SSL_DEBUG_MSG( 3,
  498. ( "client hello, adding extended_master_secret extension" ) );
  499. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
  500. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 );
  501. p += 2;
  502. *p++ = 0x00;
  503. *p++ = 0x00;
  504. *olen = 4;
  505. return( 0 );
  506. }
  507. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  508. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  509. static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
  510. unsigned char *buf,
  511. const unsigned char *end,
  512. size_t *olen )
  513. {
  514. unsigned char *p = buf;
  515. size_t tlen = ssl->session_negotiate->ticket_len;
  516. *olen = 0;
  517. if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
  518. return( 0 );
  519. MBEDTLS_SSL_DEBUG_MSG( 3,
  520. ( "client hello, adding session ticket extension" ) );
  521. /* The addition is safe here since the ticket length is 16 bit. */
  522. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen );
  523. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 );
  524. p += 2;
  525. MBEDTLS_PUT_UINT16_BE( tlen, p, 0 );
  526. p += 2;
  527. *olen = 4;
  528. if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
  529. return( 0 );
  530. MBEDTLS_SSL_DEBUG_MSG( 3,
  531. ( "sending session ticket of length %" MBEDTLS_PRINTF_SIZET, tlen ) );
  532. memcpy( p, ssl->session_negotiate->ticket, tlen );
  533. *olen += tlen;
  534. return( 0 );
  535. }
  536. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  537. #if defined(MBEDTLS_SSL_ALPN)
  538. static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
  539. unsigned char *buf,
  540. const unsigned char *end,
  541. size_t *olen )
  542. {
  543. unsigned char *p = buf;
  544. size_t alpnlen = 0;
  545. const char **cur;
  546. *olen = 0;
  547. if( ssl->conf->alpn_list == NULL )
  548. return( 0 );
  549. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
  550. for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
  551. alpnlen += strlen( *cur ) + 1;
  552. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen );
  553. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 );
  554. p += 2;
  555. /*
  556. * opaque ProtocolName<1..2^8-1>;
  557. *
  558. * struct {
  559. * ProtocolName protocol_name_list<2..2^16-1>
  560. * } ProtocolNameList;
  561. */
  562. /* Skip writing extension and list length for now */
  563. p += 4;
  564. for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
  565. {
  566. /*
  567. * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
  568. * protocol names is less than 255.
  569. */
  570. *p = (unsigned char)strlen( *cur );
  571. memcpy( p + 1, *cur, *p );
  572. p += 1 + *p;
  573. }
  574. *olen = p - buf;
  575. /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
  576. MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 );
  577. /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
  578. MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 );
  579. return( 0 );
  580. }
  581. #endif /* MBEDTLS_SSL_ALPN */
  582. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  583. static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl,
  584. unsigned char *buf,
  585. const unsigned char *end,
  586. size_t *olen )
  587. {
  588. unsigned char *p = buf;
  589. size_t protection_profiles_index = 0, ext_len = 0;
  590. uint16_t mki_len = 0, profile_value = 0;
  591. *olen = 0;
  592. if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
  593. ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
  594. ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
  595. {
  596. return( 0 );
  597. }
  598. /* RFC 5764 section 4.1.1
  599. * uint8 SRTPProtectionProfile[2];
  600. *
  601. * struct {
  602. * SRTPProtectionProfiles SRTPProtectionProfiles;
  603. * opaque srtp_mki<0..255>;
  604. * } UseSRTPData;
  605. * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
  606. */
  607. if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
  608. {
  609. mki_len = ssl->dtls_srtp_info.mki_len;
  610. }
  611. /* Extension length = 2 bytes for profiles length,
  612. * ssl->conf->dtls_srtp_profile_list_len * 2 (each profile is 2 bytes length ),
  613. * 1 byte for srtp_mki vector length and the mki_len value
  614. */
  615. ext_len = 2 + 2 * ( ssl->conf->dtls_srtp_profile_list_len ) + 1 + mki_len;
  616. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding use_srtp extension" ) );
  617. /* Check there is room in the buffer for the extension + 4 bytes
  618. * - the extension tag (2 bytes)
  619. * - the extension length (2 bytes)
  620. */
  621. MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 );
  622. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 );
  623. p += 2;
  624. MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
  625. p += 2;
  626. /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */
  627. /* micro-optimization:
  628. * the list size is limited to MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH
  629. * which is lower than 127, so the upper byte of the length is always 0
  630. * For the documentation, the more generic code is left in comments
  631. * *p++ = (unsigned char)( ( ( 2 * ssl->conf->dtls_srtp_profile_list_len )
  632. * >> 8 ) & 0xFF );
  633. */
  634. *p++ = 0;
  635. *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len );
  636. for( protection_profiles_index=0;
  637. protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len;
  638. protection_profiles_index++ )
  639. {
  640. profile_value = mbedtls_ssl_check_srtp_profile_value
  641. ( ssl->conf->dtls_srtp_profile_list[protection_profiles_index] );
  642. if( profile_value != MBEDTLS_TLS_SRTP_UNSET )
  643. {
  644. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x",
  645. profile_value ) );
  646. MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 );
  647. p += 2;
  648. }
  649. else
  650. {
  651. /*
  652. * Note: we shall never arrive here as protection profiles
  653. * is checked by mbedtls_ssl_conf_dtls_srtp_protection_profiles function
  654. */
  655. MBEDTLS_SSL_DEBUG_MSG( 3,
  656. ( "client hello, "
  657. "illegal DTLS-SRTP protection profile %d",
  658. ssl->conf->dtls_srtp_profile_list[protection_profiles_index]
  659. ) );
  660. return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
  661. }
  662. }
  663. *p++ = mki_len & 0xFF;
  664. if( mki_len != 0 )
  665. {
  666. memcpy( p, ssl->dtls_srtp_info.mki_value, mki_len );
  667. /*
  668. * Increment p to point to the current position.
  669. */
  670. p += mki_len;
  671. MBEDTLS_SSL_DEBUG_BUF( 3, "sending mki", ssl->dtls_srtp_info.mki_value,
  672. ssl->dtls_srtp_info.mki_len );
  673. }
  674. /*
  675. * total extension length: extension type (2 bytes)
  676. * + extension length (2 bytes)
  677. * + protection profile length (2 bytes)
  678. * + 2 * number of protection profiles
  679. * + srtp_mki vector length(1 byte)
  680. * + mki value
  681. */
  682. *olen = p - buf;
  683. return( 0 );
  684. }
  685. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  686. /*
  687. * Generate random bytes for ClientHello
  688. */
  689. static int ssl_generate_random( mbedtls_ssl_context *ssl )
  690. {
  691. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  692. unsigned char *p = ssl->handshake->randbytes;
  693. #if defined(MBEDTLS_HAVE_TIME)
  694. mbedtls_time_t t;
  695. #endif
  696. /*
  697. * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
  698. */
  699. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  700. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  701. ssl->handshake->verify_cookie != NULL )
  702. {
  703. return( 0 );
  704. }
  705. #endif
  706. #if defined(MBEDTLS_HAVE_TIME)
  707. t = mbedtls_time( NULL );
  708. MBEDTLS_PUT_UINT32_BE( t, p, 0 );
  709. p += 4;
  710. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
  711. (long long) t ) );
  712. #else
  713. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
  714. return( ret );
  715. p += 4;
  716. #endif /* MBEDTLS_HAVE_TIME */
  717. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
  718. return( ret );
  719. return( 0 );
  720. }
  721. /**
  722. * \brief Validate cipher suite against config in SSL context.
  723. *
  724. * \param suite_info cipher suite to validate
  725. * \param ssl SSL context
  726. * \param min_minor_ver Minimal minor version to accept a cipher suite
  727. * \param max_minor_ver Maximal minor version to accept a cipher suite
  728. *
  729. * \return 0 if valid, else 1
  730. */
  731. static int ssl_validate_ciphersuite(
  732. const mbedtls_ssl_ciphersuite_t * suite_info,
  733. const mbedtls_ssl_context * ssl,
  734. int min_minor_ver, int max_minor_ver )
  735. {
  736. (void) ssl;
  737. if( suite_info == NULL )
  738. return( 1 );
  739. if( suite_info->min_minor_ver > max_minor_ver ||
  740. suite_info->max_minor_ver < min_minor_ver )
  741. return( 1 );
  742. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  743. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  744. ( suite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
  745. return( 1 );
  746. #endif
  747. #if defined(MBEDTLS_ARC4_C)
  748. if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
  749. suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
  750. return( 1 );
  751. #endif
  752. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  753. if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
  754. mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
  755. return( 1 );
  756. #endif
  757. /* Don't suggest PSK-based ciphersuite if no PSK is available. */
  758. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  759. if( mbedtls_ssl_ciphersuite_uses_psk( suite_info ) &&
  760. ssl_conf_has_static_psk( ssl->conf ) == 0 )
  761. {
  762. return( 1 );
  763. }
  764. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  765. return( 0 );
  766. }
  767. static int ssl_write_client_hello( mbedtls_ssl_context *ssl )
  768. {
  769. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  770. size_t i, n, olen, ext_len = 0;
  771. unsigned char *buf;
  772. unsigned char *p, *q;
  773. const unsigned char *end;
  774. unsigned char offer_compress;
  775. const int *ciphersuites;
  776. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  777. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  778. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  779. int uses_ec = 0;
  780. #endif
  781. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
  782. if( ssl->conf->f_rng == NULL )
  783. {
  784. MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
  785. return( MBEDTLS_ERR_SSL_NO_RNG );
  786. }
  787. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  788. if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
  789. #endif
  790. {
  791. ssl->major_ver = ssl->conf->min_major_ver;
  792. ssl->minor_ver = ssl->conf->min_minor_ver;
  793. }
  794. if( ssl->conf->max_major_ver == 0 )
  795. {
  796. MBEDTLS_SSL_DEBUG_MSG( 1,
  797. ( "configured max major version is invalid, consider using mbedtls_ssl_config_defaults()" ) );
  798. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  799. }
  800. buf = ssl->out_msg;
  801. end = buf + MBEDTLS_SSL_OUT_CONTENT_LEN;
  802. /*
  803. * Check if there's enough space for the first part of the ClientHello
  804. * consisting of the 38 bytes described below, the session identifier (at
  805. * most 32 bytes) and its length (1 byte).
  806. *
  807. * Use static upper bounds instead of the actual values
  808. * to allow the compiler to optimize this away.
  809. */
  810. MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );
  811. /*
  812. * The 38 first bytes of the ClientHello:
  813. * 0 . 0 handshake type (written later)
  814. * 1 . 3 handshake length (written later)
  815. * 4 . 5 highest version supported
  816. * 6 . 9 current UNIX time
  817. * 10 . 37 random bytes
  818. *
  819. * The current UNIX time (4 bytes) and following 28 random bytes are written
  820. * by ssl_generate_random() into ssl->handshake->randbytes buffer and then
  821. * copied from there into the output buffer.
  822. */
  823. p = buf + 4;
  824. mbedtls_ssl_write_version( ssl->conf->max_major_ver,
  825. ssl->conf->max_minor_ver,
  826. ssl->conf->transport, p );
  827. p += 2;
  828. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
  829. buf[4], buf[5] ) );
  830. if( ( ret = ssl_generate_random( ssl ) ) != 0 )
  831. {
  832. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
  833. return( ret );
  834. }
  835. memcpy( p, ssl->handshake->randbytes, 32 );
  836. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
  837. p += 32;
  838. /*
  839. * 38 . 38 session id length
  840. * 39 . 39+n session id
  841. * 39+n . 39+n DTLS only: cookie length (1 byte)
  842. * 40+n . .. DTLS only: cookie
  843. * .. . .. ciphersuitelist length (2 bytes)
  844. * .. . .. ciphersuitelist
  845. * .. . .. compression methods length (1 byte)
  846. * .. . .. compression methods
  847. * .. . .. extensions length (2 bytes)
  848. * .. . .. extensions
  849. */
  850. n = ssl->session_negotiate->id_len;
  851. if( n < 16 || n > 32 ||
  852. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  853. ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
  854. #endif
  855. ssl->handshake->resume == 0 )
  856. {
  857. n = 0;
  858. }
  859. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  860. /*
  861. * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
  862. * generate and include a Session ID in the TLS ClientHello."
  863. */
  864. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  865. if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
  866. #endif
  867. {
  868. if( ssl->session_negotiate->ticket != NULL &&
  869. ssl->session_negotiate->ticket_len != 0 )
  870. {
  871. ret = ssl->conf->f_rng( ssl->conf->p_rng,
  872. ssl->session_negotiate->id, 32 );
  873. if( ret != 0 )
  874. return( ret );
  875. ssl->session_negotiate->id_len = n = 32;
  876. }
  877. }
  878. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  879. /*
  880. * The first check of the output buffer size above (
  881. * MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 38 + 1 + 32 );)
  882. * has checked that there is enough space in the output buffer for the
  883. * session identifier length byte and the session identifier (n <= 32).
  884. */
  885. *p++ = (unsigned char) n;
  886. for( i = 0; i < n; i++ )
  887. *p++ = ssl->session_negotiate->id[i];
  888. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
  889. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
  890. /*
  891. * With 'n' being the length of the session identifier
  892. *
  893. * 39+n . 39+n DTLS only: cookie length (1 byte)
  894. * 40+n . .. DTLS only: cookie
  895. * .. . .. ciphersuitelist length (2 bytes)
  896. * .. . .. ciphersuitelist
  897. * .. . .. compression methods length (1 byte)
  898. * .. . .. compression methods
  899. * .. . .. extensions length (2 bytes)
  900. * .. . .. extensions
  901. */
  902. /*
  903. * DTLS cookie
  904. */
  905. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  906. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  907. {
  908. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
  909. if( ssl->handshake->verify_cookie == NULL )
  910. {
  911. MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
  912. *p++ = 0;
  913. }
  914. else
  915. {
  916. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
  917. ssl->handshake->verify_cookie,
  918. ssl->handshake->verify_cookie_len );
  919. *p++ = ssl->handshake->verify_cookie_len;
  920. MBEDTLS_SSL_CHK_BUF_PTR( p, end,
  921. ssl->handshake->verify_cookie_len );
  922. memcpy( p, ssl->handshake->verify_cookie,
  923. ssl->handshake->verify_cookie_len );
  924. p += ssl->handshake->verify_cookie_len;
  925. }
  926. }
  927. #endif
  928. /*
  929. * Ciphersuite list
  930. */
  931. ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
  932. /* Skip writing ciphersuite length for now */
  933. n = 0;
  934. q = p;
  935. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
  936. p += 2;
  937. for( i = 0; ciphersuites[i] != 0; i++ )
  938. {
  939. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
  940. if( ssl_validate_ciphersuite( ciphersuite_info, ssl,
  941. ssl->conf->min_minor_ver,
  942. ssl->conf->max_minor_ver ) != 0 )
  943. continue;
  944. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %#04x (%s)",
  945. (unsigned int)ciphersuites[i], ciphersuite_info->name ) );
  946. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  947. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  948. uses_ec |= mbedtls_ssl_ciphersuite_uses_ec( ciphersuite_info );
  949. #endif
  950. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
  951. n++;
  952. MBEDTLS_PUT_UINT16_BE( ciphersuites[i], p, 0 );
  953. p += 2;
  954. }
  955. MBEDTLS_SSL_DEBUG_MSG( 3,
  956. ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites (excluding SCSVs)", n ) );
  957. /*
  958. * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
  959. */
  960. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  961. if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
  962. #endif
  963. {
  964. MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) );
  965. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
  966. MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 );
  967. p += 2;
  968. n++;
  969. }
  970. /* Some versions of OpenSSL don't handle it correctly if not at end */
  971. #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
  972. if( ssl->conf->fallback == MBEDTLS_SSL_IS_FALLBACK )
  973. {
  974. MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
  975. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
  976. MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_FALLBACK_SCSV_VALUE, p, 0 );
  977. p += 2;
  978. n++;
  979. }
  980. #endif
  981. *q++ = (unsigned char)( n >> 7 );
  982. *q++ = (unsigned char)( n << 1 );
  983. #if defined(MBEDTLS_ZLIB_SUPPORT)
  984. offer_compress = 1;
  985. #else
  986. offer_compress = 0;
  987. #endif
  988. /*
  989. * We don't support compression with DTLS right now: if many records come
  990. * in the same datagram, uncompressing one could overwrite the next one.
  991. * We don't want to add complexity for handling that case unless there is
  992. * an actual need for it.
  993. */
  994. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  995. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  996. offer_compress = 0;
  997. #endif
  998. if( offer_compress )
  999. {
  1000. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
  1001. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
  1002. MBEDTLS_SSL_COMPRESS_DEFLATE,
  1003. MBEDTLS_SSL_COMPRESS_NULL ) );
  1004. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
  1005. *p++ = 2;
  1006. *p++ = MBEDTLS_SSL_COMPRESS_DEFLATE;
  1007. *p++ = MBEDTLS_SSL_COMPRESS_NULL;
  1008. }
  1009. else
  1010. {
  1011. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
  1012. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
  1013. MBEDTLS_SSL_COMPRESS_NULL ) );
  1014. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
  1015. *p++ = 1;
  1016. *p++ = MBEDTLS_SSL_COMPRESS_NULL;
  1017. }
  1018. /* First write extensions, then the total length */
  1019. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
  1020. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  1021. if( ( ret = ssl_write_hostname_ext( ssl, p + 2 + ext_len,
  1022. end, &olen ) ) != 0 )
  1023. {
  1024. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_hostname_ext", ret );
  1025. return( ret );
  1026. }
  1027. ext_len += olen;
  1028. #endif
  1029. /* Note that TLS_EMPTY_RENEGOTIATION_INFO_SCSV is always added
  1030. * even if MBEDTLS_SSL_RENEGOTIATION is not defined. */
  1031. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1032. if( ( ret = ssl_write_renegotiation_ext( ssl, p + 2 + ext_len,
  1033. end, &olen ) ) != 0 )
  1034. {
  1035. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_renegotiation_ext", ret );
  1036. return( ret );
  1037. }
  1038. ext_len += olen;
  1039. #endif
  1040. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  1041. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1042. if( ( ret = ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len,
  1043. end, &olen ) ) != 0 )
  1044. {
  1045. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_signature_algorithms_ext", ret );
  1046. return( ret );
  1047. }
  1048. ext_len += olen;
  1049. #endif
  1050. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  1051. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1052. if( uses_ec )
  1053. {
  1054. if( ( ret = ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len,
  1055. end, &olen ) ) != 0 )
  1056. {
  1057. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_elliptic_curves_ext", ret );
  1058. return( ret );
  1059. }
  1060. ext_len += olen;
  1061. if( ( ret = ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len,
  1062. end, &olen ) ) != 0 )
  1063. {
  1064. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_supported_point_formats_ext", ret );
  1065. return( ret );
  1066. }
  1067. ext_len += olen;
  1068. }
  1069. #endif
  1070. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1071. if( ( ret = ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len,
  1072. end, &olen ) ) != 0 )
  1073. {
  1074. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_ecjpake_kkpp_ext", ret );
  1075. return( ret );
  1076. }
  1077. ext_len += olen;
  1078. #endif
  1079. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1080. if( ( ret = ssl_write_cid_ext( ssl, p + 2 + ext_len, end, &olen ) ) != 0 )
  1081. {
  1082. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_cid_ext", ret );
  1083. return( ret );
  1084. }
  1085. ext_len += olen;
  1086. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1087. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1088. if( ( ret = ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len,
  1089. end, &olen ) ) != 0 )
  1090. {
  1091. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_max_fragment_length_ext", ret );
  1092. return( ret );
  1093. }
  1094. ext_len += olen;
  1095. #endif
  1096. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1097. if( ( ret = ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len,
  1098. end, &olen ) ) != 0 )
  1099. {
  1100. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_truncated_hmac_ext", ret );
  1101. return( ret );
  1102. }
  1103. ext_len += olen;
  1104. #endif
  1105. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1106. if( ( ret = ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len,
  1107. end, &olen ) ) != 0 )
  1108. {
  1109. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_encrypt_then_mac_ext", ret );
  1110. return( ret );
  1111. }
  1112. ext_len += olen;
  1113. #endif
  1114. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1115. if( ( ret = ssl_write_extended_ms_ext( ssl, p + 2 + ext_len,
  1116. end, &olen ) ) != 0 )
  1117. {
  1118. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_extended_ms_ext", ret );
  1119. return( ret );
  1120. }
  1121. ext_len += olen;
  1122. #endif
  1123. #if defined(MBEDTLS_SSL_ALPN)
  1124. if( ( ret = ssl_write_alpn_ext( ssl, p + 2 + ext_len,
  1125. end, &olen ) ) != 0 )
  1126. {
  1127. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_alpn_ext", ret );
  1128. return( ret );
  1129. }
  1130. ext_len += olen;
  1131. #endif
  1132. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  1133. if( ( ret = ssl_write_use_srtp_ext( ssl, p + 2 + ext_len,
  1134. end, &olen ) ) != 0 )
  1135. {
  1136. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_use_srtp_ext", ret );
  1137. return( ret );
  1138. }
  1139. ext_len += olen;
  1140. #endif
  1141. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1142. if( ( ret = ssl_write_session_ticket_ext( ssl, p + 2 + ext_len,
  1143. end, &olen ) ) != 0 )
  1144. {
  1145. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_session_ticket_ext", ret );
  1146. return( ret );
  1147. }
  1148. ext_len += olen;
  1149. #endif
  1150. /* olen unused if all extensions are disabled */
  1151. ((void) olen);
  1152. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET,
  1153. ext_len ) );
  1154. if( ext_len > 0 )
  1155. {
  1156. /* No need to check for space here, because the extension
  1157. * writing functions already took care of that. */
  1158. MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 );
  1159. p += 2 + ext_len;
  1160. }
  1161. ssl->out_msglen = p - buf;
  1162. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  1163. ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO;
  1164. ssl->state++;
  1165. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1166. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1167. mbedtls_ssl_send_flight_completed( ssl );
  1168. #endif
  1169. if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
  1170. {
  1171. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
  1172. return( ret );
  1173. }
  1174. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1175. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  1176. ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
  1177. {
  1178. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
  1179. return( ret );
  1180. }
  1181. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1182. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
  1183. return( 0 );
  1184. }
  1185. static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
  1186. const unsigned char *buf,
  1187. size_t len )
  1188. {
  1189. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1190. if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
  1191. {
  1192. /* Check verify-data in constant-time. The length OTOH is no secret */
  1193. if( len != 1 + ssl->verify_data_len * 2 ||
  1194. buf[0] != ssl->verify_data_len * 2 ||
  1195. mbedtls_ct_memcmp( buf + 1,
  1196. ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
  1197. mbedtls_ct_memcmp( buf + 1 + ssl->verify_data_len,
  1198. ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
  1199. {
  1200. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
  1201. mbedtls_ssl_send_alert_message(
  1202. ssl,
  1203. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1204. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1205. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1206. }
  1207. }
  1208. else
  1209. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1210. {
  1211. if( len != 1 || buf[0] != 0x00 )
  1212. {
  1213. MBEDTLS_SSL_DEBUG_MSG( 1,
  1214. ( "non-zero length renegotiation info" ) );
  1215. mbedtls_ssl_send_alert_message(
  1216. ssl,
  1217. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1218. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1219. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1220. }
  1221. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  1222. }
  1223. return( 0 );
  1224. }
  1225. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1226. static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
  1227. const unsigned char *buf,
  1228. size_t len )
  1229. {
  1230. /*
  1231. * server should use the extension only if we did,
  1232. * and if so the server's value should match ours (and len is always 1)
  1233. */
  1234. if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
  1235. len != 1 ||
  1236. buf[0] != ssl->conf->mfl_code )
  1237. {
  1238. MBEDTLS_SSL_DEBUG_MSG( 1,
  1239. ( "non-matching max fragment length extension" ) );
  1240. mbedtls_ssl_send_alert_message(
  1241. ssl,
  1242. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1243. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1244. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1245. }
  1246. return( 0 );
  1247. }
  1248. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  1249. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1250. static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
  1251. const unsigned char *buf,
  1252. size_t len )
  1253. {
  1254. if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ||
  1255. len != 0 )
  1256. {
  1257. MBEDTLS_SSL_DEBUG_MSG( 1,
  1258. ( "non-matching truncated HMAC extension" ) );
  1259. mbedtls_ssl_send_alert_message(
  1260. ssl,
  1261. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1262. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1263. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1264. }
  1265. ((void) buf);
  1266. ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
  1267. return( 0 );
  1268. }
  1269. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  1270. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1271. static int ssl_parse_cid_ext( mbedtls_ssl_context *ssl,
  1272. const unsigned char *buf,
  1273. size_t len )
  1274. {
  1275. size_t peer_cid_len;
  1276. if( /* CID extension only makes sense in DTLS */
  1277. ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
  1278. /* The server must only send the CID extension if we have offered it. */
  1279. ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED )
  1280. {
  1281. MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension unexpected" ) );
  1282. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1283. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
  1284. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1285. }
  1286. if( len == 0 )
  1287. {
  1288. MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
  1289. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1290. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1291. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1292. }
  1293. peer_cid_len = *buf++;
  1294. len--;
  1295. if( peer_cid_len > MBEDTLS_SSL_CID_OUT_LEN_MAX )
  1296. {
  1297. MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
  1298. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1299. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1300. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1301. }
  1302. if( len != peer_cid_len )
  1303. {
  1304. MBEDTLS_SSL_DEBUG_MSG( 1, ( "CID extension invalid" ) );
  1305. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1306. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1307. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1308. }
  1309. ssl->handshake->cid_in_use = MBEDTLS_SSL_CID_ENABLED;
  1310. ssl->handshake->peer_cid_len = (uint8_t) peer_cid_len;
  1311. memcpy( ssl->handshake->peer_cid, buf, peer_cid_len );
  1312. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use of CID extension negotiated" ) );
  1313. MBEDTLS_SSL_DEBUG_BUF( 3, "Server CID", buf, peer_cid_len );
  1314. return( 0 );
  1315. }
  1316. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1317. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1318. static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
  1319. const unsigned char *buf,
  1320. size_t len )
  1321. {
  1322. if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
  1323. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
  1324. len != 0 )
  1325. {
  1326. MBEDTLS_SSL_DEBUG_MSG( 1,
  1327. ( "non-matching encrypt-then-MAC extension" ) );
  1328. mbedtls_ssl_send_alert_message(
  1329. ssl,
  1330. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1331. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
  1332. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1333. }
  1334. ((void) buf);
  1335. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
  1336. return( 0 );
  1337. }
  1338. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  1339. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1340. static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
  1341. const unsigned char *buf,
  1342. size_t len )
  1343. {
  1344. if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
  1345. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
  1346. len != 0 )
  1347. {
  1348. MBEDTLS_SSL_DEBUG_MSG( 1,
  1349. ( "non-matching extended master secret extension" ) );
  1350. mbedtls_ssl_send_alert_message(
  1351. ssl,
  1352. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1353. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
  1354. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1355. }
  1356. ((void) buf);
  1357. ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  1358. return( 0 );
  1359. }
  1360. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  1361. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1362. static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
  1363. const unsigned char *buf,
  1364. size_t len )
  1365. {
  1366. if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
  1367. len != 0 )
  1368. {
  1369. MBEDTLS_SSL_DEBUG_MSG( 1,
  1370. ( "non-matching session ticket extension" ) );
  1371. mbedtls_ssl_send_alert_message(
  1372. ssl,
  1373. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1374. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
  1375. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1376. }
  1377. ((void) buf);
  1378. ssl->handshake->new_session_ticket = 1;
  1379. return( 0 );
  1380. }
  1381. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1382. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  1383. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1384. static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
  1385. const unsigned char *buf,
  1386. size_t len )
  1387. {
  1388. size_t list_size;
  1389. const unsigned char *p;
  1390. if( len == 0 || (size_t)( buf[0] + 1 ) != len )
  1391. {
  1392. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1393. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1394. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1395. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1396. }
  1397. list_size = buf[0];
  1398. p = buf + 1;
  1399. while( list_size > 0 )
  1400. {
  1401. if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
  1402. p[0] == MBEDTLS_ECP_PF_COMPRESSED )
  1403. {
  1404. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
  1405. ssl->handshake->ecdh_ctx.point_format = p[0];
  1406. #endif
  1407. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1408. ssl->handshake->ecjpake_ctx.point_format = p[0];
  1409. #endif
  1410. MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
  1411. return( 0 );
  1412. }
  1413. list_size--;
  1414. p++;
  1415. }
  1416. MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
  1417. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1418. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1419. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1420. }
  1421. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  1422. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1423. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1424. static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
  1425. const unsigned char *buf,
  1426. size_t len )
  1427. {
  1428. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1429. if( ssl->handshake->ciphersuite_info->key_exchange !=
  1430. MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  1431. {
  1432. MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
  1433. return( 0 );
  1434. }
  1435. /* If we got here, we no longer need our cached extension */
  1436. mbedtls_free( ssl->handshake->ecjpake_cache );
  1437. ssl->handshake->ecjpake_cache = NULL;
  1438. ssl->handshake->ecjpake_cache_len = 0;
  1439. if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
  1440. buf, len ) ) != 0 )
  1441. {
  1442. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
  1443. mbedtls_ssl_send_alert_message(
  1444. ssl,
  1445. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1446. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1447. return( ret );
  1448. }
  1449. return( 0 );
  1450. }
  1451. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1452. #if defined(MBEDTLS_SSL_ALPN)
  1453. static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
  1454. const unsigned char *buf, size_t len )
  1455. {
  1456. size_t list_len, name_len;
  1457. const char **p;
  1458. /* If we didn't send it, the server shouldn't send it */
  1459. if( ssl->conf->alpn_list == NULL )
  1460. {
  1461. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
  1462. mbedtls_ssl_send_alert_message(
  1463. ssl,
  1464. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1465. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT );
  1466. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1467. }
  1468. /*
  1469. * opaque ProtocolName<1..2^8-1>;
  1470. *
  1471. * struct {
  1472. * ProtocolName protocol_name_list<2..2^16-1>
  1473. * } ProtocolNameList;
  1474. *
  1475. * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
  1476. */
  1477. /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
  1478. if( len < 4 )
  1479. {
  1480. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1481. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1482. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1483. }
  1484. list_len = ( buf[0] << 8 ) | buf[1];
  1485. if( list_len != len - 2 )
  1486. {
  1487. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1488. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1489. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1490. }
  1491. name_len = buf[2];
  1492. if( name_len != list_len - 1 )
  1493. {
  1494. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1495. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1496. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1497. }
  1498. /* Check that the server chosen protocol was in our list and save it */
  1499. for( p = ssl->conf->alpn_list; *p != NULL; p++ )
  1500. {
  1501. if( name_len == strlen( *p ) &&
  1502. memcmp( buf + 3, *p, name_len ) == 0 )
  1503. {
  1504. ssl->alpn_chosen = *p;
  1505. return( 0 );
  1506. }
  1507. }
  1508. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
  1509. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1510. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1511. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1512. }
  1513. #endif /* MBEDTLS_SSL_ALPN */
  1514. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  1515. static int ssl_parse_use_srtp_ext( mbedtls_ssl_context *ssl,
  1516. const unsigned char *buf,
  1517. size_t len )
  1518. {
  1519. mbedtls_ssl_srtp_profile server_protection = MBEDTLS_TLS_SRTP_UNSET;
  1520. size_t i, mki_len = 0;
  1521. uint16_t server_protection_profile_value = 0;
  1522. /* If use_srtp is not configured, just ignore the extension */
  1523. if( ( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) ||
  1524. ( ssl->conf->dtls_srtp_profile_list == NULL ) ||
  1525. ( ssl->conf->dtls_srtp_profile_list_len == 0 ) )
  1526. return( 0 );
  1527. /* RFC 5764 section 4.1.1
  1528. * uint8 SRTPProtectionProfile[2];
  1529. *
  1530. * struct {
  1531. * SRTPProtectionProfiles SRTPProtectionProfiles;
  1532. * opaque srtp_mki<0..255>;
  1533. * } UseSRTPData;
  1534. * SRTPProtectionProfile SRTPProtectionProfiles<2..2^16-1>;
  1535. *
  1536. */
  1537. if( ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED )
  1538. {
  1539. mki_len = ssl->dtls_srtp_info.mki_len;
  1540. }
  1541. /*
  1542. * Length is 5 + optional mki_value : one protection profile length (2 bytes)
  1543. * + protection profile (2 bytes)
  1544. * + mki_len(1 byte)
  1545. * and optional srtp_mki
  1546. */
  1547. if( ( len < 5 ) || ( len != ( buf[4] + 5u ) ) )
  1548. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1549. /*
  1550. * get the server protection profile
  1551. */
  1552. /*
  1553. * protection profile length must be 0x0002 as we must have only
  1554. * one protection profile in server Hello
  1555. */
  1556. if( ( buf[0] != 0 ) || ( buf[1] != 2 ) )
  1557. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1558. server_protection_profile_value = ( buf[2] << 8 ) | buf[3];
  1559. server_protection = mbedtls_ssl_check_srtp_profile_value(
  1560. server_protection_profile_value );
  1561. if( server_protection != MBEDTLS_TLS_SRTP_UNSET )
  1562. {
  1563. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found srtp profile: %s",
  1564. mbedtls_ssl_get_srtp_profile_as_string(
  1565. server_protection ) ) );
  1566. }
  1567. ssl->dtls_srtp_info.chosen_dtls_srtp_profile = MBEDTLS_TLS_SRTP_UNSET;
  1568. /*
  1569. * Check we have the server profile in our list
  1570. */
  1571. for( i=0; i < ssl->conf->dtls_srtp_profile_list_len; i++)
  1572. {
  1573. if( server_protection == ssl->conf->dtls_srtp_profile_list[i] )
  1574. {
  1575. ssl->dtls_srtp_info.chosen_dtls_srtp_profile = ssl->conf->dtls_srtp_profile_list[i];
  1576. MBEDTLS_SSL_DEBUG_MSG( 3, ( "selected srtp profile: %s",
  1577. mbedtls_ssl_get_srtp_profile_as_string(
  1578. server_protection ) ) );
  1579. break;
  1580. }
  1581. }
  1582. /* If no match was found : server problem, it shall never answer with incompatible profile */
  1583. if( ssl->dtls_srtp_info.chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET )
  1584. {
  1585. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1586. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1587. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1588. }
  1589. /* If server does not use mki in its reply, make sure the client won't keep
  1590. * one as negotiated */
  1591. if( len == 5 )
  1592. {
  1593. ssl->dtls_srtp_info.mki_len = 0;
  1594. }
  1595. /*
  1596. * RFC5764:
  1597. * If the client detects a nonzero-length MKI in the server's response
  1598. * that is different than the one the client offered, then the client
  1599. * MUST abort the handshake and SHOULD send an invalid_parameter alert.
  1600. */
  1601. if( len > 5 && ( buf[4] != mki_len ||
  1602. ( memcmp( ssl->dtls_srtp_info.mki_value, &buf[5], mki_len ) ) ) )
  1603. {
  1604. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1605. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1606. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1607. }
  1608. #if defined (MBEDTLS_DEBUG_C)
  1609. if( len > 5 )
  1610. {
  1611. MBEDTLS_SSL_DEBUG_BUF( 3, "received mki", ssl->dtls_srtp_info.mki_value,
  1612. ssl->dtls_srtp_info.mki_len );
  1613. }
  1614. #endif
  1615. return( 0 );
  1616. }
  1617. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  1618. /*
  1619. * Parse HelloVerifyRequest. Only called after verifying the HS type.
  1620. */
  1621. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1622. static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
  1623. {
  1624. const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  1625. int major_ver, minor_ver;
  1626. unsigned char cookie_len;
  1627. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
  1628. /* Check that there is enough room for:
  1629. * - 2 bytes of version
  1630. * - 1 byte of cookie_len
  1631. */
  1632. if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
  1633. {
  1634. MBEDTLS_SSL_DEBUG_MSG( 1,
  1635. ( "incoming HelloVerifyRequest message is too short" ) );
  1636. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1637. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1638. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1639. }
  1640. /*
  1641. * struct {
  1642. * ProtocolVersion server_version;
  1643. * opaque cookie<0..2^8-1>;
  1644. * } HelloVerifyRequest;
  1645. */
  1646. MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
  1647. mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
  1648. p += 2;
  1649. /*
  1650. * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
  1651. * even is lower than our min version.
  1652. */
  1653. if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
  1654. minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
  1655. major_ver > ssl->conf->max_major_ver ||
  1656. minor_ver > ssl->conf->max_minor_ver )
  1657. {
  1658. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
  1659. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1660. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  1661. return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  1662. }
  1663. cookie_len = *p++;
  1664. if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
  1665. {
  1666. MBEDTLS_SSL_DEBUG_MSG( 1,
  1667. ( "cookie length does not match incoming message size" ) );
  1668. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1669. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1670. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1671. }
  1672. MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
  1673. mbedtls_free( ssl->handshake->verify_cookie );
  1674. ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
  1675. if( ssl->handshake->verify_cookie == NULL )
  1676. {
  1677. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
  1678. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  1679. }
  1680. memcpy( ssl->handshake->verify_cookie, p, cookie_len );
  1681. ssl->handshake->verify_cookie_len = cookie_len;
  1682. /* Start over at ClientHello */
  1683. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  1684. mbedtls_ssl_reset_checksum( ssl );
  1685. mbedtls_ssl_recv_flight_completed( ssl );
  1686. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
  1687. return( 0 );
  1688. }
  1689. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1690. static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
  1691. {
  1692. int ret, i;
  1693. size_t n;
  1694. size_t ext_len;
  1695. unsigned char *buf, *ext;
  1696. unsigned char comp;
  1697. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1698. int accept_comp;
  1699. #endif
  1700. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1701. int renegotiation_info_seen = 0;
  1702. #endif
  1703. int handshake_failure = 0;
  1704. const mbedtls_ssl_ciphersuite_t *suite_info;
  1705. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
  1706. if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
  1707. {
  1708. /* No alert on a read error. */
  1709. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  1710. return( ret );
  1711. }
  1712. buf = ssl->in_msg;
  1713. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  1714. {
  1715. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1716. if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  1717. {
  1718. ssl->renego_records_seen++;
  1719. if( ssl->conf->renego_max_records >= 0 &&
  1720. ssl->renego_records_seen > ssl->conf->renego_max_records )
  1721. {
  1722. MBEDTLS_SSL_DEBUG_MSG( 1,
  1723. ( "renegotiation requested, but not honored by server" ) );
  1724. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1725. }
  1726. MBEDTLS_SSL_DEBUG_MSG( 1,
  1727. ( "non-handshake message during renegotiation" ) );
  1728. ssl->keep_current_message = 1;
  1729. return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
  1730. }
  1731. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1732. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1733. mbedtls_ssl_send_alert_message(
  1734. ssl,
  1735. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1736. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  1737. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1738. }
  1739. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1740. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1741. {
  1742. if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
  1743. {
  1744. MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
  1745. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
  1746. return( ssl_parse_hello_verify_request( ssl ) );
  1747. }
  1748. else
  1749. {
  1750. /* We made it through the verification process */
  1751. mbedtls_free( ssl->handshake->verify_cookie );
  1752. ssl->handshake->verify_cookie = NULL;
  1753. ssl->handshake->verify_cookie_len = 0;
  1754. }
  1755. }
  1756. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1757. if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
  1758. buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
  1759. {
  1760. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1761. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1762. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1763. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1764. }
  1765. /*
  1766. * 0 . 1 server_version
  1767. * 2 . 33 random (maybe including 4 bytes of Unix time)
  1768. * 34 . 34 session_id length = n
  1769. * 35 . 34+n session_id
  1770. * 35+n . 36+n cipher_suite
  1771. * 37+n . 37+n compression_method
  1772. *
  1773. * 38+n . 39+n extensions length (optional)
  1774. * 40+n . .. extensions
  1775. */
  1776. buf += mbedtls_ssl_hs_hdr_len( ssl );
  1777. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
  1778. mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
  1779. ssl->conf->transport, buf + 0 );
  1780. if( ssl->major_ver < ssl->conf->min_major_ver ||
  1781. ssl->minor_ver < ssl->conf->min_minor_ver ||
  1782. ssl->major_ver > ssl->conf->max_major_ver ||
  1783. ssl->minor_ver > ssl->conf->max_minor_ver )
  1784. {
  1785. MBEDTLS_SSL_DEBUG_MSG( 1,
  1786. ( "server version out of bounds - min: [%d:%d], server: [%d:%d], max: [%d:%d]",
  1787. ssl->conf->min_major_ver,
  1788. ssl->conf->min_minor_ver,
  1789. ssl->major_ver, ssl->minor_ver,
  1790. ssl->conf->max_major_ver,
  1791. ssl->conf->max_minor_ver ) );
  1792. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1793. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  1794. return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  1795. }
  1796. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
  1797. ( (unsigned long) buf[2] << 24 ) |
  1798. ( (unsigned long) buf[3] << 16 ) |
  1799. ( (unsigned long) buf[4] << 8 ) |
  1800. ( (unsigned long) buf[5] ) ) );
  1801. memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
  1802. n = buf[34];
  1803. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
  1804. if( n > 32 )
  1805. {
  1806. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1807. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1808. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1809. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1810. }
  1811. if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
  1812. {
  1813. ext_len = ( ( buf[38 + n] << 8 )
  1814. | ( buf[39 + n] ) );
  1815. if( ( ext_len > 0 && ext_len < 4 ) ||
  1816. ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
  1817. {
  1818. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1819. mbedtls_ssl_send_alert_message(
  1820. ssl,
  1821. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1822. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1823. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1824. }
  1825. }
  1826. else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
  1827. {
  1828. ext_len = 0;
  1829. }
  1830. else
  1831. {
  1832. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1833. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1834. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1835. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1836. }
  1837. /* ciphersuite (used later) */
  1838. i = ( buf[35 + n] << 8 ) | buf[36 + n];
  1839. /*
  1840. * Read and check compression
  1841. */
  1842. comp = buf[37 + n];
  1843. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1844. /* See comments in ssl_write_client_hello() */
  1845. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1846. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1847. accept_comp = 0;
  1848. else
  1849. #endif
  1850. accept_comp = 1;
  1851. if( comp != MBEDTLS_SSL_COMPRESS_NULL &&
  1852. ( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) )
  1853. #else /* MBEDTLS_ZLIB_SUPPORT */
  1854. if( comp != MBEDTLS_SSL_COMPRESS_NULL )
  1855. #endif/* MBEDTLS_ZLIB_SUPPORT */
  1856. {
  1857. MBEDTLS_SSL_DEBUG_MSG( 1,
  1858. ( "server hello, bad compression: %d", comp ) );
  1859. mbedtls_ssl_send_alert_message(
  1860. ssl,
  1861. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1862. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1863. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  1864. }
  1865. /*
  1866. * Initialize update checksum functions
  1867. */
  1868. ssl->handshake->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
  1869. if( ssl->handshake->ciphersuite_info == NULL )
  1870. {
  1871. MBEDTLS_SSL_DEBUG_MSG( 1,
  1872. ( "ciphersuite info for %04x not found", (unsigned int)i ) );
  1873. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1874. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  1875. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  1876. }
  1877. mbedtls_ssl_optimize_checksum( ssl, ssl->handshake->ciphersuite_info );
  1878. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %" MBEDTLS_PRINTF_SIZET, n ) );
  1879. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
  1880. /*
  1881. * Check if the session can be resumed
  1882. */
  1883. if( ssl->handshake->resume == 0 || n == 0 ||
  1884. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1885. ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
  1886. #endif
  1887. ssl->session_negotiate->ciphersuite != i ||
  1888. ssl->session_negotiate->compression != comp ||
  1889. ssl->session_negotiate->id_len != n ||
  1890. memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
  1891. {
  1892. ssl->state++;
  1893. ssl->handshake->resume = 0;
  1894. #if defined(MBEDTLS_HAVE_TIME)
  1895. ssl->session_negotiate->start = mbedtls_time( NULL );
  1896. #endif
  1897. ssl->session_negotiate->ciphersuite = i;
  1898. ssl->session_negotiate->compression = comp;
  1899. ssl->session_negotiate->id_len = n;
  1900. memcpy( ssl->session_negotiate->id, buf + 35, n );
  1901. }
  1902. else
  1903. {
  1904. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  1905. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  1906. {
  1907. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  1908. mbedtls_ssl_send_alert_message(
  1909. ssl,
  1910. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1911. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  1912. return( ret );
  1913. }
  1914. }
  1915. MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
  1916. ssl->handshake->resume ? "a" : "no" ) );
  1917. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", (unsigned) i ) );
  1918. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d",
  1919. buf[37 + n] ) );
  1920. /*
  1921. * Perform cipher suite validation in same way as in ssl_write_client_hello.
  1922. */
  1923. i = 0;
  1924. while( 1 )
  1925. {
  1926. if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 )
  1927. {
  1928. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1929. mbedtls_ssl_send_alert_message(
  1930. ssl,
  1931. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1932. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1933. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1934. }
  1935. if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] ==
  1936. ssl->session_negotiate->ciphersuite )
  1937. {
  1938. break;
  1939. }
  1940. }
  1941. suite_info = mbedtls_ssl_ciphersuite_from_id(
  1942. ssl->session_negotiate->ciphersuite );
  1943. if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver,
  1944. ssl->minor_ver ) != 0 )
  1945. {
  1946. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1947. mbedtls_ssl_send_alert_message(
  1948. ssl,
  1949. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1950. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1951. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1952. }
  1953. MBEDTLS_SSL_DEBUG_MSG( 3,
  1954. ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
  1955. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  1956. if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
  1957. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  1958. {
  1959. ssl->handshake->ecrs_enabled = 1;
  1960. }
  1961. #endif
  1962. if( comp != MBEDTLS_SSL_COMPRESS_NULL
  1963. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1964. && comp != MBEDTLS_SSL_COMPRESS_DEFLATE
  1965. #endif
  1966. )
  1967. {
  1968. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1969. mbedtls_ssl_send_alert_message(
  1970. ssl,
  1971. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1972. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1973. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1974. }
  1975. ssl->session_negotiate->compression = comp;
  1976. ext = buf + 40 + n;
  1977. MBEDTLS_SSL_DEBUG_MSG( 2,
  1978. ( "server hello, total extension length: %" MBEDTLS_PRINTF_SIZET, ext_len ) );
  1979. while( ext_len )
  1980. {
  1981. unsigned int ext_id = ( ( ext[0] << 8 )
  1982. | ( ext[1] ) );
  1983. unsigned int ext_size = ( ( ext[2] << 8 )
  1984. | ( ext[3] ) );
  1985. if( ext_size + 4 > ext_len )
  1986. {
  1987. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1988. mbedtls_ssl_send_alert_message(
  1989. ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1990. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1991. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1992. }
  1993. switch( ext_id )
  1994. {
  1995. case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
  1996. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
  1997. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1998. renegotiation_info_seen = 1;
  1999. #endif
  2000. if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
  2001. ext_size ) ) != 0 )
  2002. return( ret );
  2003. break;
  2004. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2005. case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
  2006. MBEDTLS_SSL_DEBUG_MSG( 3,
  2007. ( "found max_fragment_length extension" ) );
  2008. if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
  2009. ext + 4, ext_size ) ) != 0 )
  2010. {
  2011. return( ret );
  2012. }
  2013. break;
  2014. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  2015. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  2016. case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
  2017. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
  2018. if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
  2019. ext + 4, ext_size ) ) != 0 )
  2020. {
  2021. return( ret );
  2022. }
  2023. break;
  2024. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  2025. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2026. case MBEDTLS_TLS_EXT_CID:
  2027. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found CID extension" ) );
  2028. if( ( ret = ssl_parse_cid_ext( ssl,
  2029. ext + 4,
  2030. ext_size ) ) != 0 )
  2031. {
  2032. return( ret );
  2033. }
  2034. break;
  2035. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2036. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  2037. case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
  2038. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
  2039. if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
  2040. ext + 4, ext_size ) ) != 0 )
  2041. {
  2042. return( ret );
  2043. }
  2044. break;
  2045. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  2046. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  2047. case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
  2048. MBEDTLS_SSL_DEBUG_MSG( 3,
  2049. ( "found extended_master_secret extension" ) );
  2050. if( ( ret = ssl_parse_extended_ms_ext( ssl,
  2051. ext + 4, ext_size ) ) != 0 )
  2052. {
  2053. return( ret );
  2054. }
  2055. break;
  2056. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  2057. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2058. case MBEDTLS_TLS_EXT_SESSION_TICKET:
  2059. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
  2060. if( ( ret = ssl_parse_session_ticket_ext( ssl,
  2061. ext + 4, ext_size ) ) != 0 )
  2062. {
  2063. return( ret );
  2064. }
  2065. break;
  2066. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  2067. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  2068. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2069. case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
  2070. MBEDTLS_SSL_DEBUG_MSG( 3,
  2071. ( "found supported_point_formats extension" ) );
  2072. if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
  2073. ext + 4, ext_size ) ) != 0 )
  2074. {
  2075. return( ret );
  2076. }
  2077. break;
  2078. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  2079. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2080. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2081. case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
  2082. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
  2083. if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
  2084. ext + 4, ext_size ) ) != 0 )
  2085. {
  2086. return( ret );
  2087. }
  2088. break;
  2089. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2090. #if defined(MBEDTLS_SSL_ALPN)
  2091. case MBEDTLS_TLS_EXT_ALPN:
  2092. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
  2093. if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
  2094. return( ret );
  2095. break;
  2096. #endif /* MBEDTLS_SSL_ALPN */
  2097. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  2098. case MBEDTLS_TLS_EXT_USE_SRTP:
  2099. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found use_srtp extension" ) );
  2100. if( ( ret = ssl_parse_use_srtp_ext( ssl, ext + 4, ext_size ) ) != 0 )
  2101. return( ret );
  2102. break;
  2103. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  2104. default:
  2105. MBEDTLS_SSL_DEBUG_MSG( 3,
  2106. ( "unknown extension found: %u (ignoring)", ext_id ) );
  2107. }
  2108. ext_len -= 4 + ext_size;
  2109. ext += 4 + ext_size;
  2110. if( ext_len > 0 && ext_len < 4 )
  2111. {
  2112. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  2113. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  2114. }
  2115. }
  2116. /*
  2117. * Renegotiation security checks
  2118. */
  2119. if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  2120. ssl->conf->allow_legacy_renegotiation ==
  2121. MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
  2122. {
  2123. MBEDTLS_SSL_DEBUG_MSG( 1,
  2124. ( "legacy renegotiation, breaking off handshake" ) );
  2125. handshake_failure = 1;
  2126. }
  2127. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2128. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  2129. ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  2130. renegotiation_info_seen == 0 )
  2131. {
  2132. MBEDTLS_SSL_DEBUG_MSG( 1,
  2133. ( "renegotiation_info extension missing (secure)" ) );
  2134. handshake_failure = 1;
  2135. }
  2136. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  2137. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  2138. ssl->conf->allow_legacy_renegotiation ==
  2139. MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
  2140. {
  2141. MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
  2142. handshake_failure = 1;
  2143. }
  2144. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  2145. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  2146. renegotiation_info_seen == 1 )
  2147. {
  2148. MBEDTLS_SSL_DEBUG_MSG( 1,
  2149. ( "renegotiation_info extension present (legacy)" ) );
  2150. handshake_failure = 1;
  2151. }
  2152. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  2153. if( handshake_failure == 1 )
  2154. {
  2155. mbedtls_ssl_send_alert_message(
  2156. ssl,
  2157. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2158. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  2159. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  2160. }
  2161. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
  2162. return( 0 );
  2163. }
  2164. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  2165. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  2166. static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl,
  2167. unsigned char **p,
  2168. unsigned char *end )
  2169. {
  2170. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2171. size_t dhm_actual_bitlen;
  2172. /*
  2173. * Ephemeral DH parameters:
  2174. *
  2175. * struct {
  2176. * opaque dh_p<1..2^16-1>;
  2177. * opaque dh_g<1..2^16-1>;
  2178. * opaque dh_Ys<1..2^16-1>;
  2179. * } ServerDHParams;
  2180. */
  2181. if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx,
  2182. p, end ) ) != 0 )
  2183. {
  2184. MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
  2185. return( ret );
  2186. }
  2187. dhm_actual_bitlen = mbedtls_mpi_bitlen( &ssl->handshake->dhm_ctx.P );
  2188. if( dhm_actual_bitlen < ssl->conf->dhm_min_bitlen )
  2189. {
  2190. MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %" MBEDTLS_PRINTF_SIZET " < %u",
  2191. dhm_actual_bitlen,
  2192. ssl->conf->dhm_min_bitlen ) );
  2193. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2194. }
  2195. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
  2196. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
  2197. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
  2198. return( ret );
  2199. }
  2200. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  2201. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  2202. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2203. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  2204. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  2205. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2206. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  2207. static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
  2208. {
  2209. const mbedtls_ecp_curve_info *curve_info;
  2210. mbedtls_ecp_group_id grp_id;
  2211. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  2212. grp_id = ssl->handshake->ecdh_ctx.grp.id;
  2213. #else
  2214. grp_id = ssl->handshake->ecdh_ctx.grp_id;
  2215. #endif
  2216. curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
  2217. if( curve_info == NULL )
  2218. {
  2219. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2220. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2221. }
  2222. MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
  2223. #if defined(MBEDTLS_ECP_C)
  2224. if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
  2225. #else
  2226. if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
  2227. ssl->handshake->ecdh_ctx.grp.nbits > 521 )
  2228. #endif
  2229. return( -1 );
  2230. MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
  2231. MBEDTLS_DEBUG_ECDH_QP );
  2232. return( 0 );
  2233. }
  2234. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2235. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  2236. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  2237. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  2238. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  2239. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  2240. ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2241. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
  2242. static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl,
  2243. unsigned char **p,
  2244. unsigned char *end )
  2245. {
  2246. uint16_t tls_id;
  2247. size_t ecdh_bits = 0;
  2248. uint8_t ecpoint_len;
  2249. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  2250. /*
  2251. * Parse ECC group
  2252. */
  2253. if( end - *p < 4 )
  2254. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2255. /* First byte is curve_type; only named_curve is handled */
  2256. if( *(*p)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
  2257. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2258. /* Next two bytes are the namedcurve value */
  2259. tls_id = *(*p)++;
  2260. tls_id <<= 8;
  2261. tls_id |= *(*p)++;
  2262. /* Convert EC group to PSA key type. */
  2263. if( ( handshake->ecdh_psa_type =
  2264. mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 )
  2265. {
  2266. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2267. }
  2268. if( ecdh_bits > 0xffff )
  2269. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2270. handshake->ecdh_bits = (uint16_t) ecdh_bits;
  2271. /*
  2272. * Put peer's ECDH public key in the format understood by PSA.
  2273. */
  2274. ecpoint_len = *(*p)++;
  2275. if( (size_t)( end - *p ) < ecpoint_len )
  2276. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2277. if( mbedtls_psa_tls_ecpoint_to_psa_ec(
  2278. *p, ecpoint_len,
  2279. handshake->ecdh_psa_peerkey,
  2280. sizeof( handshake->ecdh_psa_peerkey ),
  2281. &handshake->ecdh_psa_peerkey_len ) != 0 )
  2282. {
  2283. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  2284. }
  2285. *p += ecpoint_len;
  2286. return( 0 );
  2287. }
  2288. #endif /* MBEDTLS_USE_PSA_CRYPTO &&
  2289. ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2290. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
  2291. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2292. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  2293. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  2294. static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
  2295. unsigned char **p,
  2296. unsigned char *end )
  2297. {
  2298. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2299. /*
  2300. * Ephemeral ECDH parameters:
  2301. *
  2302. * struct {
  2303. * ECParameters curve_params;
  2304. * ECPoint public;
  2305. * } ServerECDHParams;
  2306. */
  2307. if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
  2308. (const unsigned char **) p, end ) ) != 0 )
  2309. {
  2310. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
  2311. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2312. if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  2313. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  2314. #endif
  2315. return( ret );
  2316. }
  2317. if( ssl_check_server_ecdh_params( ssl ) != 0 )
  2318. {
  2319. MBEDTLS_SSL_DEBUG_MSG( 1,
  2320. ( "bad server key exchange message (ECDHE curve)" ) );
  2321. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2322. }
  2323. return( ret );
  2324. }
  2325. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2326. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  2327. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  2328. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  2329. static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
  2330. unsigned char **p,
  2331. unsigned char *end )
  2332. {
  2333. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2334. uint16_t len;
  2335. ((void) ssl);
  2336. /*
  2337. * PSK parameters:
  2338. *
  2339. * opaque psk_identity_hint<0..2^16-1>;
  2340. */
  2341. if( end - (*p) < 2 )
  2342. {
  2343. MBEDTLS_SSL_DEBUG_MSG( 1,
  2344. ( "bad server key exchange message (psk_identity_hint length)" ) );
  2345. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2346. }
  2347. len = (*p)[0] << 8 | (*p)[1];
  2348. *p += 2;
  2349. if( end - (*p) < len )
  2350. {
  2351. MBEDTLS_SSL_DEBUG_MSG( 1,
  2352. ( "bad server key exchange message (psk_identity_hint length)" ) );
  2353. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2354. }
  2355. /*
  2356. * Note: we currently ignore the PKS identity hint, as we only allow one
  2357. * PSK to be provisionned on the client. This could be changed later if
  2358. * someone needs that feature.
  2359. */
  2360. *p += len;
  2361. ret = 0;
  2362. return( ret );
  2363. }
  2364. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  2365. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  2366. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  2367. /*
  2368. * Generate a pre-master secret and encrypt it with the server's RSA key
  2369. */
  2370. static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
  2371. size_t offset, size_t *olen,
  2372. size_t pms_offset )
  2373. {
  2374. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2375. size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2;
  2376. unsigned char *p = ssl->handshake->premaster + pms_offset;
  2377. mbedtls_pk_context * peer_pk;
  2378. if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
  2379. {
  2380. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
  2381. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  2382. }
  2383. /*
  2384. * Generate (part of) the pre-master as
  2385. * struct {
  2386. * ProtocolVersion client_version;
  2387. * opaque random[46];
  2388. * } PreMasterSecret;
  2389. */
  2390. mbedtls_ssl_write_version( ssl->conf->max_major_ver,
  2391. ssl->conf->max_minor_ver,
  2392. ssl->conf->transport, p );
  2393. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
  2394. {
  2395. MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
  2396. return( ret );
  2397. }
  2398. ssl->handshake->pmslen = 48;
  2399. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2400. peer_pk = &ssl->handshake->peer_pubkey;
  2401. #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2402. if( ssl->session_negotiate->peer_cert == NULL )
  2403. {
  2404. /* Should never happen */
  2405. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2406. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2407. }
  2408. peer_pk = &ssl->session_negotiate->peer_cert->pk;
  2409. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2410. /*
  2411. * Now write it out, encrypted
  2412. */
  2413. if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_RSA ) )
  2414. {
  2415. MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
  2416. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  2417. }
  2418. if( ( ret = mbedtls_pk_encrypt( peer_pk,
  2419. p, ssl->handshake->pmslen,
  2420. ssl->out_msg + offset + len_bytes, olen,
  2421. MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
  2422. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  2423. {
  2424. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
  2425. return( ret );
  2426. }
  2427. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  2428. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2429. if( len_bytes == 2 )
  2430. {
  2431. MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset );
  2432. *olen += 2;
  2433. }
  2434. #endif
  2435. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2436. /* We don't need the peer's public key anymore. Free it. */
  2437. mbedtls_pk_free( peer_pk );
  2438. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2439. return( 0 );
  2440. }
  2441. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
  2442. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  2443. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2444. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  2445. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2446. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  2447. static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
  2448. unsigned char **p,
  2449. unsigned char *end,
  2450. mbedtls_md_type_t *md_alg,
  2451. mbedtls_pk_type_t *pk_alg )
  2452. {
  2453. ((void) ssl);
  2454. *md_alg = MBEDTLS_MD_NONE;
  2455. *pk_alg = MBEDTLS_PK_NONE;
  2456. /* Only in TLS 1.2 */
  2457. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  2458. {
  2459. return( 0 );
  2460. }
  2461. if( (*p) + 2 > end )
  2462. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2463. /*
  2464. * Get hash algorithm
  2465. */
  2466. if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) )
  2467. == MBEDTLS_MD_NONE )
  2468. {
  2469. MBEDTLS_SSL_DEBUG_MSG( 1,
  2470. ( "Server used unsupported HashAlgorithm %d", *(p)[0] ) );
  2471. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2472. }
  2473. /*
  2474. * Get signature algorithm
  2475. */
  2476. if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) )
  2477. == MBEDTLS_PK_NONE )
  2478. {
  2479. MBEDTLS_SSL_DEBUG_MSG( 1,
  2480. ( "server used unsupported SignatureAlgorithm %d", (*p)[1] ) );
  2481. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2482. }
  2483. /*
  2484. * Check if the hash is acceptable
  2485. */
  2486. if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 )
  2487. {
  2488. MBEDTLS_SSL_DEBUG_MSG( 1,
  2489. ( "server used HashAlgorithm %d that was not offered", *(p)[0] ) );
  2490. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2491. }
  2492. MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d",
  2493. (*p)[1] ) );
  2494. MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d",
  2495. (*p)[0] ) );
  2496. *p += 2;
  2497. return( 0 );
  2498. }
  2499. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  2500. MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2501. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  2502. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2503. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2504. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  2505. static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
  2506. {
  2507. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2508. const mbedtls_ecp_keypair *peer_key;
  2509. mbedtls_pk_context * peer_pk;
  2510. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2511. peer_pk = &ssl->handshake->peer_pubkey;
  2512. #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2513. if( ssl->session_negotiate->peer_cert == NULL )
  2514. {
  2515. /* Should never happen */
  2516. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2517. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2518. }
  2519. peer_pk = &ssl->session_negotiate->peer_cert->pk;
  2520. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2521. if( ! mbedtls_pk_can_do( peer_pk, MBEDTLS_PK_ECKEY ) )
  2522. {
  2523. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
  2524. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  2525. }
  2526. peer_key = mbedtls_pk_ec( *peer_pk );
  2527. if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
  2528. MBEDTLS_ECDH_THEIRS ) ) != 0 )
  2529. {
  2530. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
  2531. return( ret );
  2532. }
  2533. if( ssl_check_server_ecdh_params( ssl ) != 0 )
  2534. {
  2535. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
  2536. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  2537. }
  2538. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2539. /* We don't need the peer's public key anymore. Free it,
  2540. * so that more RAM is available for upcoming expensive
  2541. * operations like ECDHE. */
  2542. mbedtls_pk_free( peer_pk );
  2543. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2544. return( ret );
  2545. }
  2546. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
  2547. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  2548. static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
  2549. {
  2550. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2551. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2552. ssl->handshake->ciphersuite_info;
  2553. unsigned char *p = NULL, *end = NULL;
  2554. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
  2555. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  2556. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
  2557. {
  2558. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
  2559. ssl->state++;
  2560. return( 0 );
  2561. }
  2562. ((void) p);
  2563. ((void) end);
  2564. #endif
  2565. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2566. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  2567. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  2568. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
  2569. {
  2570. if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
  2571. {
  2572. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
  2573. mbedtls_ssl_send_alert_message(
  2574. ssl,
  2575. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2576. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  2577. return( ret );
  2578. }
  2579. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
  2580. ssl->state++;
  2581. return( 0 );
  2582. }
  2583. ((void) p);
  2584. ((void) end);
  2585. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  2586. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  2587. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2588. if( ssl->handshake->ecrs_enabled &&
  2589. ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
  2590. {
  2591. goto start_processing;
  2592. }
  2593. #endif
  2594. if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
  2595. {
  2596. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2597. return( ret );
  2598. }
  2599. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2600. {
  2601. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2602. mbedtls_ssl_send_alert_message(
  2603. ssl,
  2604. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2605. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  2606. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2607. }
  2608. /*
  2609. * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
  2610. * doesn't use a psk_identity_hint
  2611. */
  2612. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
  2613. {
  2614. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2615. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  2616. {
  2617. /* Current message is probably either
  2618. * CertificateRequest or ServerHelloDone */
  2619. ssl->keep_current_message = 1;
  2620. goto exit;
  2621. }
  2622. MBEDTLS_SSL_DEBUG_MSG( 1,
  2623. ( "server key exchange message must not be skipped" ) );
  2624. mbedtls_ssl_send_alert_message(
  2625. ssl,
  2626. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2627. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  2628. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2629. }
  2630. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2631. if( ssl->handshake->ecrs_enabled )
  2632. ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
  2633. start_processing:
  2634. #endif
  2635. p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  2636. end = ssl->in_msg + ssl->in_hslen;
  2637. MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
  2638. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  2639. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2640. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  2641. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2642. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  2643. {
  2644. if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
  2645. {
  2646. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2647. mbedtls_ssl_send_alert_message(
  2648. ssl,
  2649. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2650. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2651. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2652. }
  2653. } /* FALLTROUGH */
  2654. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  2655. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
  2656. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  2657. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2658. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  2659. ; /* nothing more to do */
  2660. else
  2661. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
  2662. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  2663. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  2664. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  2665. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
  2666. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  2667. {
  2668. if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
  2669. {
  2670. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2671. mbedtls_ssl_send_alert_message(
  2672. ssl,
  2673. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2674. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2675. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2676. }
  2677. }
  2678. else
  2679. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  2680. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  2681. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  2682. ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2683. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
  2684. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  2685. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
  2686. {
  2687. if( ssl_parse_server_ecdh_params_psa( ssl, &p, end ) != 0 )
  2688. {
  2689. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2690. mbedtls_ssl_send_alert_message(
  2691. ssl,
  2692. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2693. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2694. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2695. }
  2696. }
  2697. else
  2698. #endif /* MBEDTLS_USE_PSA_CRYPTO &&
  2699. ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2700. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
  2701. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2702. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  2703. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  2704. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  2705. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  2706. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
  2707. {
  2708. if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
  2709. {
  2710. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2711. mbedtls_ssl_send_alert_message(
  2712. ssl,
  2713. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2714. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2715. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2716. }
  2717. }
  2718. else
  2719. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2720. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  2721. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  2722. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2723. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2724. {
  2725. ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
  2726. p, end - p );
  2727. if( ret != 0 )
  2728. {
  2729. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
  2730. mbedtls_ssl_send_alert_message(
  2731. ssl,
  2732. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2733. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2734. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2735. }
  2736. }
  2737. else
  2738. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2739. {
  2740. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2741. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2742. }
  2743. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
  2744. if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
  2745. {
  2746. size_t sig_len, hashlen;
  2747. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2748. unsigned char hash[PSA_HASH_MAX_SIZE];
  2749. #else
  2750. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  2751. #endif
  2752. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  2753. mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
  2754. unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  2755. size_t params_len = p - params;
  2756. void *rs_ctx = NULL;
  2757. mbedtls_pk_context * peer_pk;
  2758. /*
  2759. * Handle the digitally-signed structure
  2760. */
  2761. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2762. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  2763. {
  2764. if( ssl_parse_signature_algorithm( ssl, &p, end,
  2765. &md_alg, &pk_alg ) != 0 )
  2766. {
  2767. MBEDTLS_SSL_DEBUG_MSG( 1,
  2768. ( "bad server key exchange message" ) );
  2769. mbedtls_ssl_send_alert_message(
  2770. ssl,
  2771. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2772. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2773. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2774. }
  2775. if( pk_alg !=
  2776. mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
  2777. {
  2778. MBEDTLS_SSL_DEBUG_MSG( 1,
  2779. ( "bad server key exchange message" ) );
  2780. mbedtls_ssl_send_alert_message(
  2781. ssl,
  2782. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2783. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2784. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2785. }
  2786. }
  2787. else
  2788. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2789. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2790. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2791. if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
  2792. {
  2793. pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
  2794. /* Default hash for ECDSA is SHA-1 */
  2795. if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE )
  2796. md_alg = MBEDTLS_MD_SHA1;
  2797. }
  2798. else
  2799. #endif
  2800. {
  2801. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2802. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2803. }
  2804. /*
  2805. * Read signature
  2806. */
  2807. if( p > end - 2 )
  2808. {
  2809. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2810. mbedtls_ssl_send_alert_message(
  2811. ssl,
  2812. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2813. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2814. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2815. }
  2816. sig_len = ( p[0] << 8 ) | p[1];
  2817. p += 2;
  2818. if( p != end - sig_len )
  2819. {
  2820. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2821. mbedtls_ssl_send_alert_message(
  2822. ssl,
  2823. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2824. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2825. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2826. }
  2827. MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
  2828. /*
  2829. * Compute the hash that has been signed
  2830. */
  2831. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2832. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2833. if( md_alg == MBEDTLS_MD_NONE )
  2834. {
  2835. hashlen = 36;
  2836. ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, params,
  2837. params_len );
  2838. if( ret != 0 )
  2839. return( ret );
  2840. }
  2841. else
  2842. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  2843. MBEDTLS_SSL_PROTO_TLS1_1 */
  2844. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  2845. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2846. if( md_alg != MBEDTLS_MD_NONE )
  2847. {
  2848. ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
  2849. params, params_len,
  2850. md_alg );
  2851. if( ret != 0 )
  2852. return( ret );
  2853. }
  2854. else
  2855. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  2856. MBEDTLS_SSL_PROTO_TLS1_2 */
  2857. {
  2858. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2859. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2860. }
  2861. MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
  2862. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2863. peer_pk = &ssl->handshake->peer_pubkey;
  2864. #else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2865. if( ssl->session_negotiate->peer_cert == NULL )
  2866. {
  2867. /* Should never happen */
  2868. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2869. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2870. }
  2871. peer_pk = &ssl->session_negotiate->peer_cert->pk;
  2872. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2873. /*
  2874. * Verify signature
  2875. */
  2876. if( !mbedtls_pk_can_do( peer_pk, pk_alg ) )
  2877. {
  2878. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2879. mbedtls_ssl_send_alert_message(
  2880. ssl,
  2881. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2882. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  2883. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  2884. }
  2885. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2886. if( ssl->handshake->ecrs_enabled )
  2887. rs_ctx = &ssl->handshake->ecrs_ctx.pk;
  2888. #endif
  2889. if( ( ret = mbedtls_pk_verify_restartable( peer_pk,
  2890. md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
  2891. {
  2892. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2893. if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
  2894. #endif
  2895. mbedtls_ssl_send_alert_message(
  2896. ssl,
  2897. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2898. MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
  2899. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
  2900. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  2901. if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  2902. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  2903. #endif
  2904. return( ret );
  2905. }
  2906. #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  2907. /* We don't need the peer's public key anymore. Free it,
  2908. * so that more RAM is available for upcoming expensive
  2909. * operations like ECDHE. */
  2910. mbedtls_pk_free( peer_pk );
  2911. #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  2912. }
  2913. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED */
  2914. exit:
  2915. ssl->state++;
  2916. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
  2917. return( 0 );
  2918. }
  2919. #if ! defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
  2920. static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
  2921. {
  2922. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2923. ssl->handshake->ciphersuite_info;
  2924. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
  2925. if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
  2926. {
  2927. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
  2928. ssl->state++;
  2929. return( 0 );
  2930. }
  2931. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2932. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2933. }
  2934. #else /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  2935. static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
  2936. {
  2937. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2938. unsigned char *buf;
  2939. size_t n = 0;
  2940. size_t cert_type_len = 0, dn_len = 0;
  2941. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2942. ssl->handshake->ciphersuite_info;
  2943. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
  2944. if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
  2945. {
  2946. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
  2947. ssl->state++;
  2948. return( 0 );
  2949. }
  2950. if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
  2951. {
  2952. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2953. return( ret );
  2954. }
  2955. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2956. {
  2957. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2958. mbedtls_ssl_send_alert_message(
  2959. ssl,
  2960. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2961. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  2962. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2963. }
  2964. ssl->state++;
  2965. ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
  2966. MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
  2967. ssl->client_auth ? "a" : "no" ) );
  2968. if( ssl->client_auth == 0 )
  2969. {
  2970. /* Current message is probably the ServerHelloDone */
  2971. ssl->keep_current_message = 1;
  2972. goto exit;
  2973. }
  2974. /*
  2975. * struct {
  2976. * ClientCertificateType certificate_types<1..2^8-1>;
  2977. * SignatureAndHashAlgorithm
  2978. * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
  2979. * DistinguishedName certificate_authorities<0..2^16-1>;
  2980. * } CertificateRequest;
  2981. *
  2982. * Since we only support a single certificate on clients, let's just
  2983. * ignore all the information that's supposed to help us pick a
  2984. * certificate.
  2985. *
  2986. * We could check that our certificate matches the request, and bail out
  2987. * if it doesn't, but it's simpler to just send the certificate anyway,
  2988. * and give the server the opportunity to decide if it should terminate
  2989. * the connection when it doesn't like our certificate.
  2990. *
  2991. * Same goes for the hash in TLS 1.2's signature_algorithms: at this
  2992. * point we only have one hash available (see comments in
  2993. * write_certificate_verify), so let's just use what we have.
  2994. *
  2995. * However, we still minimally parse the message to check it is at least
  2996. * superficially sane.
  2997. */
  2998. buf = ssl->in_msg;
  2999. /* certificate_types */
  3000. if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
  3001. {
  3002. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  3003. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3004. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  3005. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  3006. }
  3007. cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
  3008. n = cert_type_len;
  3009. /*
  3010. * In the subsequent code there are two paths that read from buf:
  3011. * * the length of the signature algorithms field (if minor version of
  3012. * SSL is 3),
  3013. * * distinguished name length otherwise.
  3014. * Both reach at most the index:
  3015. * ...hdr_len + 2 + n,
  3016. * therefore the buffer length at this point must be greater than that
  3017. * regardless of the actual code path.
  3018. */
  3019. if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
  3020. {
  3021. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  3022. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3023. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  3024. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  3025. }
  3026. /* supported_signature_algorithms */
  3027. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  3028. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  3029. {
  3030. size_t sig_alg_len =
  3031. ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
  3032. | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
  3033. #if defined(MBEDTLS_DEBUG_C)
  3034. unsigned char* sig_alg;
  3035. size_t i;
  3036. #endif
  3037. /*
  3038. * The furthest access in buf is in the loop few lines below:
  3039. * sig_alg[i + 1],
  3040. * where:
  3041. * sig_alg = buf + ...hdr_len + 3 + n,
  3042. * max(i) = sig_alg_len - 1.
  3043. * Therefore the furthest access is:
  3044. * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
  3045. * which reduces to:
  3046. * buf[...hdr_len + 3 + n + sig_alg_len],
  3047. * which is one less than we need the buf to be.
  3048. */
  3049. if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl )
  3050. + 3 + n + sig_alg_len )
  3051. {
  3052. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  3053. mbedtls_ssl_send_alert_message(
  3054. ssl,
  3055. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3056. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  3057. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  3058. }
  3059. #if defined(MBEDTLS_DEBUG_C)
  3060. sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
  3061. for( i = 0; i < sig_alg_len; i += 2 )
  3062. {
  3063. MBEDTLS_SSL_DEBUG_MSG( 3,
  3064. ( "Supported Signature Algorithm found: %d,%d",
  3065. sig_alg[i], sig_alg[i + 1] ) );
  3066. }
  3067. #endif
  3068. n += 2 + sig_alg_len;
  3069. }
  3070. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  3071. /* certificate_authorities */
  3072. dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
  3073. | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
  3074. n += dn_len;
  3075. if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
  3076. {
  3077. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  3078. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3079. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  3080. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  3081. }
  3082. exit:
  3083. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
  3084. return( 0 );
  3085. }
  3086. #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  3087. static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
  3088. {
  3089. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3090. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
  3091. if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
  3092. {
  3093. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  3094. return( ret );
  3095. }
  3096. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  3097. {
  3098. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
  3099. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  3100. }
  3101. if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
  3102. ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
  3103. {
  3104. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
  3105. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3106. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  3107. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
  3108. }
  3109. ssl->state++;
  3110. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3111. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  3112. mbedtls_ssl_recv_flight_completed( ssl );
  3113. #endif
  3114. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
  3115. return( 0 );
  3116. }
  3117. static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
  3118. {
  3119. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3120. size_t header_len;
  3121. size_t content_len;
  3122. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  3123. ssl->handshake->ciphersuite_info;
  3124. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
  3125. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  3126. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
  3127. {
  3128. /*
  3129. * DHM key exchange -- send G^X mod P
  3130. */
  3131. content_len = ssl->handshake->dhm_ctx.len;
  3132. MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 );
  3133. header_len = 6;
  3134. ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
  3135. (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
  3136. &ssl->out_msg[header_len], content_len,
  3137. ssl->conf->f_rng, ssl->conf->p_rng );
  3138. if( ret != 0 )
  3139. {
  3140. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
  3141. return( ret );
  3142. }
  3143. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
  3144. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
  3145. if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
  3146. ssl->handshake->premaster,
  3147. MBEDTLS_PREMASTER_SIZE,
  3148. &ssl->handshake->pmslen,
  3149. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  3150. {
  3151. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
  3152. return( ret );
  3153. }
  3154. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
  3155. }
  3156. else
  3157. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  3158. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  3159. ( defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  3160. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) )
  3161. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  3162. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
  3163. {
  3164. psa_status_t status;
  3165. psa_key_attributes_t key_attributes;
  3166. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  3167. unsigned char own_pubkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
  3168. size_t own_pubkey_len;
  3169. unsigned char *own_pubkey_ecpoint;
  3170. size_t own_pubkey_ecpoint_len;
  3171. header_len = 4;
  3172. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
  3173. /*
  3174. * Generate EC private key for ECDHE exchange.
  3175. */
  3176. /* The master secret is obtained from the shared ECDH secret by
  3177. * applying the TLS 1.2 PRF with a specific salt and label. While
  3178. * the PSA Crypto API encourages combining key agreement schemes
  3179. * such as ECDH with fixed KDFs such as TLS 1.2 PRF, it does not
  3180. * yet support the provisioning of salt + label to the KDF.
  3181. * For the time being, we therefore need to split the computation
  3182. * of the ECDH secret and the application of the TLS 1.2 PRF. */
  3183. key_attributes = psa_key_attributes_init();
  3184. psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
  3185. psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
  3186. psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
  3187. psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
  3188. /* Generate ECDH private key. */
  3189. status = psa_generate_key( &key_attributes,
  3190. &handshake->ecdh_psa_privkey );
  3191. if( status != PSA_SUCCESS )
  3192. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  3193. /* Export the public part of the ECDH private key from PSA
  3194. * and convert it to ECPoint format used in ClientKeyExchange. */
  3195. status = psa_export_public_key( handshake->ecdh_psa_privkey,
  3196. own_pubkey, sizeof( own_pubkey ),
  3197. &own_pubkey_len );
  3198. if( status != PSA_SUCCESS )
  3199. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  3200. if( mbedtls_psa_tls_psa_ec_to_ecpoint( own_pubkey,
  3201. own_pubkey_len,
  3202. &own_pubkey_ecpoint,
  3203. &own_pubkey_ecpoint_len ) != 0 )
  3204. {
  3205. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  3206. }
  3207. /* Copy ECPoint structure to outgoing message buffer. */
  3208. ssl->out_msg[header_len] = (unsigned char) own_pubkey_ecpoint_len;
  3209. memcpy( ssl->out_msg + header_len + 1,
  3210. own_pubkey_ecpoint, own_pubkey_ecpoint_len );
  3211. content_len = own_pubkey_ecpoint_len + 1;
  3212. /* The ECDH secret is the premaster secret used for key derivation. */
  3213. /* Compute ECDH shared secret. */
  3214. status = psa_raw_key_agreement( PSA_ALG_ECDH,
  3215. handshake->ecdh_psa_privkey,
  3216. handshake->ecdh_psa_peerkey,
  3217. handshake->ecdh_psa_peerkey_len,
  3218. ssl->handshake->premaster,
  3219. sizeof( ssl->handshake->premaster ),
  3220. &ssl->handshake->pmslen );
  3221. if( status != PSA_SUCCESS )
  3222. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  3223. status = psa_destroy_key( handshake->ecdh_psa_privkey );
  3224. if( status != PSA_SUCCESS )
  3225. return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
  3226. handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
  3227. }
  3228. else
  3229. #endif /* MBEDTLS_USE_PSA_CRYPTO &&
  3230. ( MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  3231. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ) */
  3232. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  3233. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  3234. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  3235. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  3236. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  3237. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
  3238. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  3239. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
  3240. {
  3241. /*
  3242. * ECDH key exchange -- send client public value
  3243. */
  3244. header_len = 4;
  3245. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3246. if( ssl->handshake->ecrs_enabled )
  3247. {
  3248. if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
  3249. goto ecdh_calc_secret;
  3250. mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
  3251. }
  3252. #endif
  3253. ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
  3254. &content_len,
  3255. &ssl->out_msg[header_len], 1000,
  3256. ssl->conf->f_rng, ssl->conf->p_rng );
  3257. if( ret != 0 )
  3258. {
  3259. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
  3260. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3261. if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  3262. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  3263. #endif
  3264. return( ret );
  3265. }
  3266. MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
  3267. MBEDTLS_DEBUG_ECDH_Q );
  3268. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3269. if( ssl->handshake->ecrs_enabled )
  3270. {
  3271. ssl->handshake->ecrs_n = content_len;
  3272. ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
  3273. }
  3274. ecdh_calc_secret:
  3275. if( ssl->handshake->ecrs_enabled )
  3276. content_len = ssl->handshake->ecrs_n;
  3277. #endif
  3278. if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
  3279. &ssl->handshake->pmslen,
  3280. ssl->handshake->premaster,
  3281. MBEDTLS_MPI_MAX_SIZE,
  3282. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  3283. {
  3284. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
  3285. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3286. if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  3287. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  3288. #endif
  3289. return( ret );
  3290. }
  3291. MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
  3292. MBEDTLS_DEBUG_ECDH_Z );
  3293. }
  3294. else
  3295. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  3296. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  3297. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  3298. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  3299. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  3300. if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
  3301. {
  3302. /*
  3303. * opaque psk_identity<0..2^16-1>;
  3304. */
  3305. if( ssl_conf_has_static_psk( ssl->conf ) == 0 )
  3306. {
  3307. /* We don't offer PSK suites if we don't have a PSK,
  3308. * and we check that the server's choice is among the
  3309. * ciphersuites we offered, so this should never happen. */
  3310. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  3311. }
  3312. header_len = 4;
  3313. content_len = ssl->conf->psk_identity_len;
  3314. if( header_len + 2 + content_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
  3315. {
  3316. MBEDTLS_SSL_DEBUG_MSG( 1,
  3317. ( "psk identity too long or SSL buffer too short" ) );
  3318. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  3319. }
  3320. ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
  3321. ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
  3322. memcpy( ssl->out_msg + header_len,
  3323. ssl->conf->psk_identity,
  3324. ssl->conf->psk_identity_len );
  3325. header_len += ssl->conf->psk_identity_len;
  3326. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  3327. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
  3328. {
  3329. content_len = 0;
  3330. }
  3331. else
  3332. #endif
  3333. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  3334. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  3335. {
  3336. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3337. /* Opaque PSKs are currently only supported for PSK-only suites. */
  3338. if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
  3339. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  3340. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3341. if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
  3342. &content_len, 2 ) ) != 0 )
  3343. return( ret );
  3344. }
  3345. else
  3346. #endif
  3347. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  3348. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  3349. {
  3350. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3351. /* Opaque PSKs are currently only supported for PSK-only suites. */
  3352. if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
  3353. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  3354. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3355. /*
  3356. * ClientDiffieHellmanPublic public (DHM send G^X mod P)
  3357. */
  3358. content_len = ssl->handshake->dhm_ctx.len;
  3359. if( header_len + 2 + content_len >
  3360. MBEDTLS_SSL_OUT_CONTENT_LEN )
  3361. {
  3362. MBEDTLS_SSL_DEBUG_MSG( 1,
  3363. ( "psk identity or DHM size too long or SSL buffer too short" ) );
  3364. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  3365. }
  3366. ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len );
  3367. ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len );
  3368. ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
  3369. (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
  3370. &ssl->out_msg[header_len], content_len,
  3371. ssl->conf->f_rng, ssl->conf->p_rng );
  3372. if( ret != 0 )
  3373. {
  3374. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
  3375. return( ret );
  3376. }
  3377. }
  3378. else
  3379. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  3380. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  3381. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  3382. {
  3383. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3384. /* Opaque PSKs are currently only supported for PSK-only suites. */
  3385. if( ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
  3386. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  3387. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  3388. /*
  3389. * ClientECDiffieHellmanPublic public;
  3390. */
  3391. ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
  3392. &content_len,
  3393. &ssl->out_msg[header_len],
  3394. MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
  3395. ssl->conf->f_rng, ssl->conf->p_rng );
  3396. if( ret != 0 )
  3397. {
  3398. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
  3399. return( ret );
  3400. }
  3401. MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
  3402. MBEDTLS_DEBUG_ECDH_Q );
  3403. }
  3404. else
  3405. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  3406. {
  3407. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  3408. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  3409. }
  3410. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  3411. defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  3412. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
  3413. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
  3414. ssl_conf_has_static_raw_psk( ssl->conf ) == 0 )
  3415. {
  3416. MBEDTLS_SSL_DEBUG_MSG( 1,
  3417. ( "skip PMS generation for opaque PSK" ) );
  3418. }
  3419. else
  3420. #endif /* MBEDTLS_USE_PSA_CRYPTO &&
  3421. MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
  3422. if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
  3423. ciphersuite_info->key_exchange ) ) != 0 )
  3424. {
  3425. MBEDTLS_SSL_DEBUG_RET( 1,
  3426. "mbedtls_ssl_psk_derive_premaster", ret );
  3427. return( ret );
  3428. }
  3429. }
  3430. else
  3431. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  3432. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  3433. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
  3434. {
  3435. header_len = 4;
  3436. if( ( ret = ssl_write_encrypted_pms( ssl, header_len,
  3437. &content_len, 0 ) ) != 0 )
  3438. return( ret );
  3439. }
  3440. else
  3441. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  3442. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  3443. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  3444. {
  3445. header_len = 4;
  3446. ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
  3447. ssl->out_msg + header_len,
  3448. MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
  3449. &content_len,
  3450. ssl->conf->f_rng, ssl->conf->p_rng );
  3451. if( ret != 0 )
  3452. {
  3453. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
  3454. return( ret );
  3455. }
  3456. ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
  3457. ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
  3458. ssl->conf->f_rng, ssl->conf->p_rng );
  3459. if( ret != 0 )
  3460. {
  3461. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
  3462. return( ret );
  3463. }
  3464. }
  3465. else
  3466. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  3467. {
  3468. ((void) ciphersuite_info);
  3469. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  3470. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  3471. }
  3472. ssl->out_msglen = header_len + content_len;
  3473. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  3474. ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
  3475. ssl->state++;
  3476. if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
  3477. {
  3478. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
  3479. return( ret );
  3480. }
  3481. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
  3482. return( 0 );
  3483. }
  3484. #if !defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
  3485. static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
  3486. {
  3487. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  3488. ssl->handshake->ciphersuite_info;
  3489. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3490. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
  3491. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  3492. {
  3493. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  3494. return( ret );
  3495. }
  3496. if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
  3497. {
  3498. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  3499. ssl->state++;
  3500. return( 0 );
  3501. }
  3502. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  3503. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  3504. }
  3505. #else /* !MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  3506. static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
  3507. {
  3508. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  3509. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  3510. ssl->handshake->ciphersuite_info;
  3511. size_t n = 0, offset = 0;
  3512. unsigned char hash[48];
  3513. unsigned char *hash_start = hash;
  3514. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  3515. size_t hashlen;
  3516. void *rs_ctx = NULL;
  3517. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
  3518. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3519. if( ssl->handshake->ecrs_enabled &&
  3520. ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
  3521. {
  3522. goto sign;
  3523. }
  3524. #endif
  3525. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  3526. {
  3527. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  3528. return( ret );
  3529. }
  3530. if( !mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
  3531. {
  3532. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  3533. ssl->state++;
  3534. return( 0 );
  3535. }
  3536. if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL )
  3537. {
  3538. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  3539. ssl->state++;
  3540. return( 0 );
  3541. }
  3542. if( mbedtls_ssl_own_key( ssl ) == NULL )
  3543. {
  3544. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
  3545. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  3546. }
  3547. /*
  3548. * Make a signature of the handshake digests
  3549. */
  3550. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3551. if( ssl->handshake->ecrs_enabled )
  3552. ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
  3553. sign:
  3554. #endif
  3555. ssl->handshake->calc_verify( ssl, hash, &hashlen );
  3556. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  3557. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  3558. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  3559. {
  3560. /*
  3561. * digitally-signed struct {
  3562. * opaque md5_hash[16];
  3563. * opaque sha_hash[20];
  3564. * };
  3565. *
  3566. * md5_hash
  3567. * MD5(handshake_messages);
  3568. *
  3569. * sha_hash
  3570. * SHA(handshake_messages);
  3571. */
  3572. md_alg = MBEDTLS_MD_NONE;
  3573. /*
  3574. * For ECDSA, default hash is SHA-1 only
  3575. */
  3576. if( mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECDSA ) )
  3577. {
  3578. hash_start += 16;
  3579. hashlen -= 16;
  3580. md_alg = MBEDTLS_MD_SHA1;
  3581. }
  3582. }
  3583. else
  3584. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  3585. MBEDTLS_SSL_PROTO_TLS1_1 */
  3586. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  3587. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  3588. {
  3589. /*
  3590. * digitally-signed struct {
  3591. * opaque handshake_messages[handshake_messages_length];
  3592. * };
  3593. *
  3594. * Taking shortcut here. We assume that the server always allows the
  3595. * PRF Hash function and has sent it in the allowed signature
  3596. * algorithms list received in the Certificate Request message.
  3597. *
  3598. * Until we encounter a server that does not, we will take this
  3599. * shortcut.
  3600. *
  3601. * Reason: Otherwise we should have running hashes for SHA512 and
  3602. * SHA224 in order to satisfy 'weird' needs from the server
  3603. * side.
  3604. */
  3605. if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
  3606. {
  3607. md_alg = MBEDTLS_MD_SHA384;
  3608. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
  3609. }
  3610. else
  3611. {
  3612. md_alg = MBEDTLS_MD_SHA256;
  3613. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
  3614. }
  3615. ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
  3616. /* Info from md_alg will be used instead */
  3617. hashlen = 0;
  3618. offset = 2;
  3619. }
  3620. else
  3621. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  3622. {
  3623. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  3624. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  3625. }
  3626. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3627. if( ssl->handshake->ecrs_enabled )
  3628. rs_ctx = &ssl->handshake->ecrs_ctx.pk;
  3629. #endif
  3630. if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
  3631. md_alg, hash_start, hashlen,
  3632. ssl->out_msg + 6 + offset, &n,
  3633. ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
  3634. {
  3635. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
  3636. #if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
  3637. if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  3638. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  3639. #endif
  3640. return( ret );
  3641. }
  3642. MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 );
  3643. ssl->out_msglen = 6 + n + offset;
  3644. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  3645. ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
  3646. ssl->state++;
  3647. if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
  3648. {
  3649. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
  3650. return( ret );
  3651. }
  3652. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
  3653. return( ret );
  3654. }
  3655. #endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
  3656. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3657. static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
  3658. {
  3659. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  3660. uint32_t lifetime;
  3661. size_t ticket_len;
  3662. unsigned char *ticket;
  3663. const unsigned char *msg;
  3664. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
  3665. if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
  3666. {
  3667. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  3668. return( ret );
  3669. }
  3670. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  3671. {
  3672. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  3673. mbedtls_ssl_send_alert_message(
  3674. ssl,
  3675. MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3676. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  3677. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  3678. }
  3679. /*
  3680. * struct {
  3681. * uint32 ticket_lifetime_hint;
  3682. * opaque ticket<0..2^16-1>;
  3683. * } NewSessionTicket;
  3684. *
  3685. * 0 . 3 ticket_lifetime_hint
  3686. * 4 . 5 ticket_len (n)
  3687. * 6 . 5+n ticket content
  3688. */
  3689. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
  3690. ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
  3691. {
  3692. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  3693. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3694. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  3695. return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
  3696. }
  3697. msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  3698. lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
  3699. ( msg[2] << 8 ) | ( msg[3] );
  3700. ticket_len = ( msg[4] << 8 ) | ( msg[5] );
  3701. if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
  3702. {
  3703. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  3704. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3705. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  3706. return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
  3707. }
  3708. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %" MBEDTLS_PRINTF_SIZET, ticket_len ) );
  3709. /* We're not waiting for a NewSessionTicket message any more */
  3710. ssl->handshake->new_session_ticket = 0;
  3711. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  3712. /*
  3713. * Zero-length ticket means the server changed his mind and doesn't want
  3714. * to send a ticket after all, so just forget it
  3715. */
  3716. if( ticket_len == 0 )
  3717. return( 0 );
  3718. if( ssl->session != NULL && ssl->session->ticket != NULL )
  3719. {
  3720. mbedtls_platform_zeroize( ssl->session->ticket,
  3721. ssl->session->ticket_len );
  3722. mbedtls_free( ssl->session->ticket );
  3723. ssl->session->ticket = NULL;
  3724. ssl->session->ticket_len = 0;
  3725. }
  3726. mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
  3727. ssl->session_negotiate->ticket_len );
  3728. mbedtls_free( ssl->session_negotiate->ticket );
  3729. ssl->session_negotiate->ticket = NULL;
  3730. ssl->session_negotiate->ticket_len = 0;
  3731. if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
  3732. {
  3733. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
  3734. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  3735. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  3736. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  3737. }
  3738. memcpy( ticket, msg + 6, ticket_len );
  3739. ssl->session_negotiate->ticket = ticket;
  3740. ssl->session_negotiate->ticket_len = ticket_len;
  3741. ssl->session_negotiate->ticket_lifetime = lifetime;
  3742. /*
  3743. * RFC 5077 section 3.4:
  3744. * "If the client receives a session ticket from the server, then it
  3745. * discards any Session ID that was sent in the ServerHello."
  3746. */
  3747. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
  3748. ssl->session_negotiate->id_len = 0;
  3749. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
  3750. return( 0 );
  3751. }
  3752. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  3753. /*
  3754. * SSL handshake -- client side -- single step
  3755. */
  3756. int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
  3757. {
  3758. int ret = 0;
  3759. if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
  3760. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3761. MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
  3762. if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
  3763. return( ret );
  3764. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3765. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  3766. ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
  3767. {
  3768. if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
  3769. return( ret );
  3770. }
  3771. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  3772. /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
  3773. * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
  3774. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3775. if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
  3776. ssl->handshake->new_session_ticket != 0 )
  3777. {
  3778. ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
  3779. }
  3780. #endif
  3781. switch( ssl->state )
  3782. {
  3783. case MBEDTLS_SSL_HELLO_REQUEST:
  3784. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  3785. break;
  3786. /*
  3787. * ==> ClientHello
  3788. */
  3789. case MBEDTLS_SSL_CLIENT_HELLO:
  3790. ret = ssl_write_client_hello( ssl );
  3791. break;
  3792. /*
  3793. * <== ServerHello
  3794. * Certificate
  3795. * ( ServerKeyExchange )
  3796. * ( CertificateRequest )
  3797. * ServerHelloDone
  3798. */
  3799. case MBEDTLS_SSL_SERVER_HELLO:
  3800. ret = ssl_parse_server_hello( ssl );
  3801. break;
  3802. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  3803. ret = mbedtls_ssl_parse_certificate( ssl );
  3804. break;
  3805. case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
  3806. ret = ssl_parse_server_key_exchange( ssl );
  3807. break;
  3808. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  3809. ret = ssl_parse_certificate_request( ssl );
  3810. break;
  3811. case MBEDTLS_SSL_SERVER_HELLO_DONE:
  3812. ret = ssl_parse_server_hello_done( ssl );
  3813. break;
  3814. /*
  3815. * ==> ( Certificate/Alert )
  3816. * ClientKeyExchange
  3817. * ( CertificateVerify )
  3818. * ChangeCipherSpec
  3819. * Finished
  3820. */
  3821. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  3822. ret = mbedtls_ssl_write_certificate( ssl );
  3823. break;
  3824. case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
  3825. ret = ssl_write_client_key_exchange( ssl );
  3826. break;
  3827. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  3828. ret = ssl_write_certificate_verify( ssl );
  3829. break;
  3830. case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
  3831. ret = mbedtls_ssl_write_change_cipher_spec( ssl );
  3832. break;
  3833. case MBEDTLS_SSL_CLIENT_FINISHED:
  3834. ret = mbedtls_ssl_write_finished( ssl );
  3835. break;
  3836. /*
  3837. * <== ( NewSessionTicket )
  3838. * ChangeCipherSpec
  3839. * Finished
  3840. */
  3841. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3842. case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
  3843. ret = ssl_parse_new_session_ticket( ssl );
  3844. break;
  3845. #endif
  3846. case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
  3847. ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
  3848. break;
  3849. case MBEDTLS_SSL_SERVER_FINISHED:
  3850. ret = mbedtls_ssl_parse_finished( ssl );
  3851. break;
  3852. case MBEDTLS_SSL_FLUSH_BUFFERS:
  3853. MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
  3854. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  3855. break;
  3856. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  3857. mbedtls_ssl_handshake_wrapup( ssl );
  3858. break;
  3859. default:
  3860. MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
  3861. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3862. }
  3863. return( ret );
  3864. }
  3865. #endif /* MBEDTLS_SSL_CLI_C */