ssl_srv.c 149 KB

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