ssl_srv.c 127 KB

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