ssl_srv.c 138 KB

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