ssl_srv.c 150 KB

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