ssl_srv.c 160 KB

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