ssl_srv.c 160 KB

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