ssl_srv.c 148 KB

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