ssl_cli.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489
  1. /*
  2. * SSLv3/TLSv1 client-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_CLI_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. #include <stdint.h>
  39. #if defined(MBEDTLS_HAVE_TIME)
  40. #include "mbedtls/platform_time.h"
  41. #endif
  42. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  43. /* Implementation that should never be optimized out by the compiler */
  44. static void mbedtls_zeroize( void *v, size_t n ) {
  45. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  46. }
  47. #endif
  48. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  49. static void ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
  50. unsigned char *buf,
  51. size_t *olen )
  52. {
  53. unsigned char *p = buf;
  54. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  55. size_t hostname_len;
  56. *olen = 0;
  57. if( ssl->hostname == NULL )
  58. return;
  59. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
  60. ssl->hostname ) );
  61. hostname_len = strlen( ssl->hostname );
  62. if( end < p || (size_t)( end - p ) < hostname_len + 9 )
  63. {
  64. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  65. return;
  66. }
  67. /*
  68. * struct {
  69. * NameType name_type;
  70. * select (name_type) {
  71. * case host_name: HostName;
  72. * } name;
  73. * } ServerName;
  74. *
  75. * enum {
  76. * host_name(0), (255)
  77. * } NameType;
  78. *
  79. * opaque HostName<1..2^16-1>;
  80. *
  81. * struct {
  82. * ServerName server_name_list<1..2^16-1>
  83. * } ServerNameList;
  84. */
  85. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
  86. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME ) & 0xFF );
  87. *p++ = (unsigned char)( ( (hostname_len + 5) >> 8 ) & 0xFF );
  88. *p++ = (unsigned char)( ( (hostname_len + 5) ) & 0xFF );
  89. *p++ = (unsigned char)( ( (hostname_len + 3) >> 8 ) & 0xFF );
  90. *p++ = (unsigned char)( ( (hostname_len + 3) ) & 0xFF );
  91. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
  92. *p++ = (unsigned char)( ( hostname_len >> 8 ) & 0xFF );
  93. *p++ = (unsigned char)( ( hostname_len ) & 0xFF );
  94. memcpy( p, ssl->hostname, hostname_len );
  95. *olen = hostname_len + 9;
  96. }
  97. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  98. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  99. static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
  100. unsigned char *buf,
  101. size_t *olen )
  102. {
  103. unsigned char *p = buf;
  104. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  105. *olen = 0;
  106. if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  107. return;
  108. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
  109. if( end < p || (size_t)( end - p ) < 5 + ssl->verify_data_len )
  110. {
  111. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  112. return;
  113. }
  114. /*
  115. * Secure renegotiation
  116. */
  117. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
  118. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
  119. *p++ = 0x00;
  120. *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
  121. *p++ = ssl->verify_data_len & 0xFF;
  122. memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
  123. *olen = 5 + ssl->verify_data_len;
  124. }
  125. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  126. /*
  127. * Only if we handle at least one key exchange that needs signatures.
  128. */
  129. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  130. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  131. static void ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl,
  132. unsigned char *buf,
  133. size_t *olen )
  134. {
  135. unsigned char *p = buf;
  136. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  137. size_t sig_alg_len = 0;
  138. const int *md;
  139. #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)
  140. unsigned char *sig_alg_list = buf + 6;
  141. #endif
  142. *olen = 0;
  143. if( ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  144. return;
  145. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
  146. for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
  147. {
  148. #if defined(MBEDTLS_ECDSA_C)
  149. sig_alg_len += 2;
  150. #endif
  151. #if defined(MBEDTLS_RSA_C)
  152. sig_alg_len += 2;
  153. #endif
  154. }
  155. if( end < p || (size_t)( end - p ) < sig_alg_len + 6 )
  156. {
  157. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  158. return;
  159. }
  160. /*
  161. * Prepare signature_algorithms extension (TLS 1.2)
  162. */
  163. sig_alg_len = 0;
  164. for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
  165. {
  166. #if defined(MBEDTLS_ECDSA_C)
  167. sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
  168. sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA;
  169. #endif
  170. #if defined(MBEDTLS_RSA_C)
  171. sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
  172. sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA;
  173. #endif
  174. }
  175. /*
  176. * enum {
  177. * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
  178. * sha512(6), (255)
  179. * } HashAlgorithm;
  180. *
  181. * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
  182. * SignatureAlgorithm;
  183. *
  184. * struct {
  185. * HashAlgorithm hash;
  186. * SignatureAlgorithm signature;
  187. * } SignatureAndHashAlgorithm;
  188. *
  189. * SignatureAndHashAlgorithm
  190. * supported_signature_algorithms<2..2^16-2>;
  191. */
  192. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
  193. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG ) & 0xFF );
  194. *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
  195. *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
  196. *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
  197. *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
  198. *olen = 6 + sig_alg_len;
  199. }
  200. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  201. MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  202. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  203. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  204. static void ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl,
  205. unsigned char *buf,
  206. size_t *olen )
  207. {
  208. unsigned char *p = buf;
  209. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  210. unsigned char *elliptic_curve_list = p + 6;
  211. size_t elliptic_curve_len = 0;
  212. const mbedtls_ecp_curve_info *info;
  213. #if defined(MBEDTLS_ECP_C)
  214. const mbedtls_ecp_group_id *grp_id;
  215. #else
  216. ((void) ssl);
  217. #endif
  218. *olen = 0;
  219. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
  220. #if defined(MBEDTLS_ECP_C)
  221. for( grp_id = ssl->conf->curve_list; *grp_id != MBEDTLS_ECP_DP_NONE; grp_id++ )
  222. #else
  223. for( info = mbedtls_ecp_curve_list(); info->grp_id != MBEDTLS_ECP_DP_NONE; info++ )
  224. #endif
  225. {
  226. #if defined(MBEDTLS_ECP_C)
  227. info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
  228. #endif
  229. if( info == NULL )
  230. {
  231. MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid curve in ssl configuration" ) );
  232. return;
  233. }
  234. elliptic_curve_len += 2;
  235. }
  236. if( end < p || (size_t)( end - p ) < 6 + elliptic_curve_len )
  237. {
  238. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  239. return;
  240. }
  241. elliptic_curve_len = 0;
  242. #if defined(MBEDTLS_ECP_C)
  243. for( grp_id = ssl->conf->curve_list; *grp_id != MBEDTLS_ECP_DP_NONE; grp_id++ )
  244. #else
  245. for( info = mbedtls_ecp_curve_list(); info->grp_id != MBEDTLS_ECP_DP_NONE; info++ )
  246. #endif
  247. {
  248. #if defined(MBEDTLS_ECP_C)
  249. info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
  250. #endif
  251. elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
  252. elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
  253. }
  254. if( elliptic_curve_len == 0 )
  255. return;
  256. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
  257. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
  258. *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
  259. *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
  260. *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
  261. *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
  262. *olen = 6 + elliptic_curve_len;
  263. }
  264. static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl,
  265. unsigned char *buf,
  266. size_t *olen )
  267. {
  268. unsigned char *p = buf;
  269. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  270. *olen = 0;
  271. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
  272. if( end < p || (size_t)( end - p ) < 6 )
  273. {
  274. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  275. return;
  276. }
  277. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
  278. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
  279. *p++ = 0x00;
  280. *p++ = 2;
  281. *p++ = 1;
  282. *p++ = MBEDTLS_ECP_PF_UNCOMPRESSED;
  283. *olen = 6;
  284. }
  285. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  286. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  287. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  288. static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl,
  289. unsigned char *buf,
  290. size_t *olen )
  291. {
  292. int ret;
  293. unsigned char *p = buf;
  294. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  295. size_t kkpp_len;
  296. *olen = 0;
  297. /* Skip costly extension if we can't use EC J-PAKE anyway */
  298. if( mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
  299. return;
  300. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding ecjpake_kkpp extension" ) );
  301. if( end - p < 4 )
  302. {
  303. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  304. return;
  305. }
  306. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF );
  307. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF );
  308. /*
  309. * We may need to send ClientHello multiple times for Hello verification.
  310. * We don't want to compute fresh values every time (both for performance
  311. * and consistency reasons), so cache the extension content.
  312. */
  313. if( ssl->handshake->ecjpake_cache == NULL ||
  314. ssl->handshake->ecjpake_cache_len == 0 )
  315. {
  316. MBEDTLS_SSL_DEBUG_MSG( 3, ( "generating new ecjpake parameters" ) );
  317. ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx,
  318. p + 2, end - p - 2, &kkpp_len,
  319. ssl->conf->f_rng, ssl->conf->p_rng );
  320. if( ret != 0 )
  321. {
  322. MBEDTLS_SSL_DEBUG_RET( 1 , "mbedtls_ecjpake_write_round_one", ret );
  323. return;
  324. }
  325. ssl->handshake->ecjpake_cache = mbedtls_calloc( 1, kkpp_len );
  326. if( ssl->handshake->ecjpake_cache == NULL )
  327. {
  328. MBEDTLS_SSL_DEBUG_MSG( 1, ( "allocation failed" ) );
  329. return;
  330. }
  331. memcpy( ssl->handshake->ecjpake_cache, p + 2, kkpp_len );
  332. ssl->handshake->ecjpake_cache_len = kkpp_len;
  333. }
  334. else
  335. {
  336. MBEDTLS_SSL_DEBUG_MSG( 3, ( "re-using cached ecjpake parameters" ) );
  337. kkpp_len = ssl->handshake->ecjpake_cache_len;
  338. if( (size_t)( end - p - 2 ) < kkpp_len )
  339. {
  340. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  341. return;
  342. }
  343. memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len );
  344. }
  345. *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF );
  346. *p++ = (unsigned char)( ( kkpp_len ) & 0xFF );
  347. *olen = kkpp_len + 4;
  348. }
  349. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  350. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  351. static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl,
  352. unsigned char *buf,
  353. size_t *olen )
  354. {
  355. unsigned char *p = buf;
  356. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  357. *olen = 0;
  358. if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ) {
  359. return;
  360. }
  361. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
  362. if( end < p || (size_t)( end - p ) < 5 )
  363. {
  364. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  365. return;
  366. }
  367. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
  368. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
  369. *p++ = 0x00;
  370. *p++ = 1;
  371. *p++ = ssl->conf->mfl_code;
  372. *olen = 5;
  373. }
  374. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  375. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  376. static void ssl_write_truncated_hmac_ext( mbedtls_ssl_context *ssl,
  377. unsigned char *buf, size_t *olen )
  378. {
  379. unsigned char *p = buf;
  380. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  381. *olen = 0;
  382. if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED )
  383. {
  384. return;
  385. }
  386. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
  387. if( end < p || (size_t)( end - p ) < 4 )
  388. {
  389. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  390. return;
  391. }
  392. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
  393. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
  394. *p++ = 0x00;
  395. *p++ = 0x00;
  396. *olen = 4;
  397. }
  398. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  399. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  400. static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
  401. unsigned char *buf, size_t *olen )
  402. {
  403. unsigned char *p = buf;
  404. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  405. *olen = 0;
  406. if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
  407. ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  408. {
  409. return;
  410. }
  411. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
  412. "extension" ) );
  413. if( end < p || (size_t)( end - p ) < 4 )
  414. {
  415. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  416. return;
  417. }
  418. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
  419. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
  420. *p++ = 0x00;
  421. *p++ = 0x00;
  422. *olen = 4;
  423. }
  424. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  425. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  426. static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl,
  427. unsigned char *buf, size_t *olen )
  428. {
  429. unsigned char *p = buf;
  430. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  431. *olen = 0;
  432. if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
  433. ssl->conf->max_minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
  434. {
  435. return;
  436. }
  437. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
  438. "extension" ) );
  439. if( end < p || (size_t)( end - p ) < 4 )
  440. {
  441. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  442. return;
  443. }
  444. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
  445. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
  446. *p++ = 0x00;
  447. *p++ = 0x00;
  448. *olen = 4;
  449. }
  450. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  451. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  452. static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl,
  453. unsigned char *buf, size_t *olen )
  454. {
  455. unsigned char *p = buf;
  456. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  457. size_t tlen = ssl->session_negotiate->ticket_len;
  458. *olen = 0;
  459. if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED )
  460. {
  461. return;
  462. }
  463. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
  464. if( end < p || (size_t)( end - p ) < 4 + tlen )
  465. {
  466. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  467. return;
  468. }
  469. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
  470. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF );
  471. *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
  472. *p++ = (unsigned char)( ( tlen ) & 0xFF );
  473. *olen = 4;
  474. if( ssl->session_negotiate->ticket == NULL || tlen == 0 )
  475. {
  476. return;
  477. }
  478. MBEDTLS_SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
  479. memcpy( p, ssl->session_negotiate->ticket, tlen );
  480. *olen += tlen;
  481. }
  482. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  483. #if defined(MBEDTLS_SSL_ALPN)
  484. static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl,
  485. unsigned char *buf, size_t *olen )
  486. {
  487. unsigned char *p = buf;
  488. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  489. size_t alpnlen = 0;
  490. const char **cur;
  491. *olen = 0;
  492. if( ssl->conf->alpn_list == NULL )
  493. {
  494. return;
  495. }
  496. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
  497. for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
  498. alpnlen += (unsigned char)( strlen( *cur ) & 0xFF ) + 1;
  499. if( end < p || (size_t)( end - p ) < 6 + alpnlen )
  500. {
  501. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  502. return;
  503. }
  504. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF );
  505. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF );
  506. /*
  507. * opaque ProtocolName<1..2^8-1>;
  508. *
  509. * struct {
  510. * ProtocolName protocol_name_list<2..2^16-1>
  511. * } ProtocolNameList;
  512. */
  513. /* Skip writing extension and list length for now */
  514. p += 4;
  515. for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
  516. {
  517. *p = (unsigned char)( strlen( *cur ) & 0xFF );
  518. memcpy( p + 1, *cur, *p );
  519. p += 1 + *p;
  520. }
  521. *olen = p - buf;
  522. /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
  523. buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
  524. buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
  525. /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
  526. buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
  527. buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
  528. }
  529. #endif /* MBEDTLS_SSL_ALPN */
  530. /*
  531. * Generate random bytes for ClientHello
  532. */
  533. static int ssl_generate_random( mbedtls_ssl_context *ssl )
  534. {
  535. int ret;
  536. unsigned char *p = ssl->handshake->randbytes;
  537. #if defined(MBEDTLS_HAVE_TIME)
  538. mbedtls_time_t t;
  539. #endif
  540. /*
  541. * When responding to a verify request, MUST reuse random (RFC 6347 4.2.1)
  542. */
  543. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  544. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  545. ssl->handshake->verify_cookie != NULL )
  546. {
  547. return( 0 );
  548. }
  549. #endif
  550. #if defined(MBEDTLS_HAVE_TIME)
  551. t = mbedtls_time( NULL );
  552. *p++ = (unsigned char)( t >> 24 );
  553. *p++ = (unsigned char)( t >> 16 );
  554. *p++ = (unsigned char)( t >> 8 );
  555. *p++ = (unsigned char)( t );
  556. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
  557. #else
  558. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 4 ) ) != 0 )
  559. return( ret );
  560. p += 4;
  561. #endif /* MBEDTLS_HAVE_TIME */
  562. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p, 28 ) ) != 0 )
  563. return( ret );
  564. return( 0 );
  565. }
  566. static int ssl_write_client_hello( mbedtls_ssl_context *ssl )
  567. {
  568. int ret;
  569. size_t i, n, olen, ext_len = 0;
  570. unsigned char *buf;
  571. unsigned char *p, *q;
  572. unsigned char offer_compress;
  573. const int *ciphersuites;
  574. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  575. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
  576. if( ssl->conf->f_rng == NULL )
  577. {
  578. MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided") );
  579. return( MBEDTLS_ERR_SSL_NO_RNG );
  580. }
  581. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  582. if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
  583. #endif
  584. {
  585. ssl->major_ver = ssl->conf->min_major_ver;
  586. ssl->minor_ver = ssl->conf->min_minor_ver;
  587. }
  588. if( ssl->conf->max_major_ver == 0 )
  589. {
  590. MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, "
  591. "consider using mbedtls_ssl_config_defaults()" ) );
  592. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  593. }
  594. /*
  595. * 0 . 0 handshake type
  596. * 1 . 3 handshake length
  597. * 4 . 5 highest version supported
  598. * 6 . 9 current UNIX time
  599. * 10 . 37 random bytes
  600. */
  601. buf = ssl->out_msg;
  602. p = buf + 4;
  603. mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
  604. ssl->conf->transport, p );
  605. p += 2;
  606. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
  607. buf[4], buf[5] ) );
  608. if( ( ret = ssl_generate_random( ssl ) ) != 0 )
  609. {
  610. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret );
  611. return( ret );
  612. }
  613. memcpy( p, ssl->handshake->randbytes, 32 );
  614. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", p, 32 );
  615. p += 32;
  616. /*
  617. * 38 . 38 session id length
  618. * 39 . 39+n session id
  619. * 39+n . 39+n DTLS only: cookie length (1 byte)
  620. * 40+n . .. DTSL only: cookie
  621. * .. . .. ciphersuitelist length (2 bytes)
  622. * .. . .. ciphersuitelist
  623. * .. . .. compression methods length (1 byte)
  624. * .. . .. compression methods
  625. * .. . .. extensions length (2 bytes)
  626. * .. . .. extensions
  627. */
  628. n = ssl->session_negotiate->id_len;
  629. if( n < 16 || n > 32 ||
  630. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  631. ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
  632. #endif
  633. ssl->handshake->resume == 0 )
  634. {
  635. n = 0;
  636. }
  637. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  638. /*
  639. * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
  640. * generate and include a Session ID in the TLS ClientHello."
  641. */
  642. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  643. if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
  644. #endif
  645. {
  646. if( ssl->session_negotiate->ticket != NULL &&
  647. ssl->session_negotiate->ticket_len != 0 )
  648. {
  649. ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->session_negotiate->id, 32 );
  650. if( ret != 0 )
  651. return( ret );
  652. ssl->session_negotiate->id_len = n = 32;
  653. }
  654. }
  655. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  656. *p++ = (unsigned char) n;
  657. for( i = 0; i < n; i++ )
  658. *p++ = ssl->session_negotiate->id[i];
  659. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
  660. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
  661. /*
  662. * DTLS cookie
  663. */
  664. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  665. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  666. {
  667. if( ssl->handshake->verify_cookie == NULL )
  668. {
  669. MBEDTLS_SSL_DEBUG_MSG( 3, ( "no verify cookie to send" ) );
  670. *p++ = 0;
  671. }
  672. else
  673. {
  674. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, cookie",
  675. ssl->handshake->verify_cookie,
  676. ssl->handshake->verify_cookie_len );
  677. *p++ = ssl->handshake->verify_cookie_len;
  678. memcpy( p, ssl->handshake->verify_cookie,
  679. ssl->handshake->verify_cookie_len );
  680. p += ssl->handshake->verify_cookie_len;
  681. }
  682. }
  683. #endif
  684. /*
  685. * Ciphersuite list
  686. */
  687. ciphersuites = ssl->conf->ciphersuite_list[ssl->minor_ver];
  688. /* Skip writing ciphersuite length for now */
  689. n = 0;
  690. q = p;
  691. p += 2;
  692. for( i = 0; ciphersuites[i] != 0; i++ )
  693. {
  694. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] );
  695. if( ciphersuite_info == NULL )
  696. continue;
  697. if( ciphersuite_info->min_minor_ver > ssl->conf->max_minor_ver ||
  698. ciphersuite_info->max_minor_ver < ssl->conf->min_minor_ver )
  699. continue;
  700. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  701. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  702. ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_NODTLS ) )
  703. continue;
  704. #endif
  705. #if defined(MBEDTLS_ARC4_C)
  706. if( ssl->conf->arc4_disabled == MBEDTLS_SSL_ARC4_DISABLED &&
  707. ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
  708. continue;
  709. #endif
  710. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  711. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
  712. mbedtls_ecjpake_check( &ssl->handshake->ecjpake_ctx ) != 0 )
  713. continue;
  714. #endif
  715. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x",
  716. ciphersuites[i] ) );
  717. n++;
  718. *p++ = (unsigned char)( ciphersuites[i] >> 8 );
  719. *p++ = (unsigned char)( ciphersuites[i] );
  720. }
  721. /*
  722. * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
  723. */
  724. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  725. if( ssl->renego_status == MBEDTLS_SSL_INITIAL_HANDSHAKE )
  726. #endif
  727. {
  728. *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
  729. *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO );
  730. n++;
  731. }
  732. /* Some versions of OpenSSL don't handle it correctly if not at end */
  733. #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
  734. if( ssl->conf->fallback == MBEDTLS_SSL_IS_FALLBACK )
  735. {
  736. MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
  737. *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE >> 8 );
  738. *p++ = (unsigned char)( MBEDTLS_SSL_FALLBACK_SCSV_VALUE );
  739. n++;
  740. }
  741. #endif
  742. *q++ = (unsigned char)( n >> 7 );
  743. *q++ = (unsigned char)( n << 1 );
  744. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
  745. #if defined(MBEDTLS_ZLIB_SUPPORT)
  746. offer_compress = 1;
  747. #else
  748. offer_compress = 0;
  749. #endif
  750. /*
  751. * We don't support compression with DTLS right now: is many records come
  752. * in the same datagram, uncompressing one could overwrite the next one.
  753. * We don't want to add complexity for handling that case unless there is
  754. * an actual need for it.
  755. */
  756. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  757. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  758. offer_compress = 0;
  759. #endif
  760. if( offer_compress )
  761. {
  762. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
  763. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
  764. MBEDTLS_SSL_COMPRESS_DEFLATE, MBEDTLS_SSL_COMPRESS_NULL ) );
  765. *p++ = 2;
  766. *p++ = MBEDTLS_SSL_COMPRESS_DEFLATE;
  767. *p++ = MBEDTLS_SSL_COMPRESS_NULL;
  768. }
  769. else
  770. {
  771. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
  772. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d",
  773. MBEDTLS_SSL_COMPRESS_NULL ) );
  774. *p++ = 1;
  775. *p++ = MBEDTLS_SSL_COMPRESS_NULL;
  776. }
  777. // First write extensions, then the total length
  778. //
  779. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  780. ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
  781. ext_len += olen;
  782. #endif
  783. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  784. ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
  785. ext_len += olen;
  786. #endif
  787. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  788. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  789. ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
  790. ext_len += olen;
  791. #endif
  792. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  793. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  794. ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
  795. ext_len += olen;
  796. ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
  797. ext_len += olen;
  798. #endif
  799. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  800. ssl_write_ecjpake_kkpp_ext( ssl, p + 2 + ext_len, &olen );
  801. ext_len += olen;
  802. #endif
  803. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  804. ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
  805. ext_len += olen;
  806. #endif
  807. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  808. ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
  809. ext_len += olen;
  810. #endif
  811. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  812. ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
  813. ext_len += olen;
  814. #endif
  815. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  816. ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
  817. ext_len += olen;
  818. #endif
  819. #if defined(MBEDTLS_SSL_ALPN)
  820. ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
  821. ext_len += olen;
  822. #endif
  823. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  824. ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
  825. ext_len += olen;
  826. #endif
  827. /* olen unused if all extensions are disabled */
  828. ((void) olen);
  829. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
  830. ext_len ) );
  831. if( ext_len > 0 )
  832. {
  833. *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
  834. *p++ = (unsigned char)( ( ext_len ) & 0xFF );
  835. p += ext_len;
  836. }
  837. ssl->out_msglen = p - buf;
  838. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  839. ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_HELLO;
  840. ssl->state++;
  841. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  842. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  843. mbedtls_ssl_send_flight_completed( ssl );
  844. #endif
  845. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  846. {
  847. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  848. return( ret );
  849. }
  850. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
  851. return( 0 );
  852. }
  853. static int ssl_parse_renegotiation_info( mbedtls_ssl_context *ssl,
  854. const unsigned char *buf,
  855. size_t len )
  856. {
  857. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  858. if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
  859. {
  860. /* Check verify-data in constant-time. The length OTOH is no secret */
  861. if( len != 1 + ssl->verify_data_len * 2 ||
  862. buf[0] != ssl->verify_data_len * 2 ||
  863. mbedtls_ssl_safer_memcmp( buf + 1,
  864. ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
  865. mbedtls_ssl_safer_memcmp( buf + 1 + ssl->verify_data_len,
  866. ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
  867. {
  868. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
  869. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  870. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  871. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  872. }
  873. }
  874. else
  875. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  876. {
  877. if( len != 1 || buf[0] != 0x00 )
  878. {
  879. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
  880. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  881. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  882. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  883. }
  884. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  885. }
  886. return( 0 );
  887. }
  888. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  889. static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
  890. const unsigned char *buf,
  891. size_t len )
  892. {
  893. /*
  894. * server should use the extension only if we did,
  895. * and if so the server's value should match ours (and len is always 1)
  896. */
  897. if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
  898. len != 1 ||
  899. buf[0] != ssl->conf->mfl_code )
  900. {
  901. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching max fragment length extension" ) );
  902. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  903. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  904. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  905. }
  906. return( 0 );
  907. }
  908. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  909. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  910. static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
  911. const unsigned char *buf,
  912. size_t len )
  913. {
  914. if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ||
  915. len != 0 )
  916. {
  917. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching truncated HMAC extension" ) );
  918. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  919. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  920. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  921. }
  922. ((void) buf);
  923. ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
  924. return( 0 );
  925. }
  926. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  927. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  928. static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
  929. const unsigned char *buf,
  930. size_t len )
  931. {
  932. if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
  933. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
  934. len != 0 )
  935. {
  936. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching encrypt-then-MAC extension" ) );
  937. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  938. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  939. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  940. }
  941. ((void) buf);
  942. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
  943. return( 0 );
  944. }
  945. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  946. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  947. static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
  948. const unsigned char *buf,
  949. size_t len )
  950. {
  951. if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
  952. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
  953. len != 0 )
  954. {
  955. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching extended master secret extension" ) );
  956. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  957. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  958. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  959. }
  960. ((void) buf);
  961. ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  962. return( 0 );
  963. }
  964. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  965. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  966. static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
  967. const unsigned char *buf,
  968. size_t len )
  969. {
  970. if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
  971. len != 0 )
  972. {
  973. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching session ticket extension" ) );
  974. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  975. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  976. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  977. }
  978. ((void) buf);
  979. ssl->handshake->new_session_ticket = 1;
  980. return( 0 );
  981. }
  982. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  983. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  984. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  985. static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
  986. const unsigned char *buf,
  987. size_t len )
  988. {
  989. size_t list_size;
  990. const unsigned char *p;
  991. list_size = buf[0];
  992. if( list_size + 1 != len )
  993. {
  994. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  995. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  996. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  997. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  998. }
  999. p = buf + 1;
  1000. while( list_size > 0 )
  1001. {
  1002. if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
  1003. p[0] == MBEDTLS_ECP_PF_COMPRESSED )
  1004. {
  1005. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
  1006. ssl->handshake->ecdh_ctx.point_format = p[0];
  1007. #endif
  1008. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1009. ssl->handshake->ecjpake_ctx.point_format = p[0];
  1010. #endif
  1011. MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
  1012. return( 0 );
  1013. }
  1014. list_size--;
  1015. p++;
  1016. }
  1017. MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
  1018. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1019. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1020. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1021. }
  1022. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  1023. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1024. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1025. static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
  1026. const unsigned char *buf,
  1027. size_t len )
  1028. {
  1029. int ret;
  1030. if( ssl->transform_negotiate->ciphersuite_info->key_exchange !=
  1031. MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  1032. {
  1033. MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
  1034. return( 0 );
  1035. }
  1036. /* If we got here, we no longer need our cached extension */
  1037. mbedtls_free( ssl->handshake->ecjpake_cache );
  1038. ssl->handshake->ecjpake_cache = NULL;
  1039. ssl->handshake->ecjpake_cache_len = 0;
  1040. if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
  1041. buf, len ) ) != 0 )
  1042. {
  1043. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
  1044. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1045. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1046. return( ret );
  1047. }
  1048. return( 0 );
  1049. }
  1050. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1051. #if defined(MBEDTLS_SSL_ALPN)
  1052. static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
  1053. const unsigned char *buf, size_t len )
  1054. {
  1055. size_t list_len, name_len;
  1056. const char **p;
  1057. /* If we didn't send it, the server shouldn't send it */
  1058. if( ssl->conf->alpn_list == NULL )
  1059. {
  1060. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching ALPN extension" ) );
  1061. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1062. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1063. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1064. }
  1065. /*
  1066. * opaque ProtocolName<1..2^8-1>;
  1067. *
  1068. * struct {
  1069. * ProtocolName protocol_name_list<2..2^16-1>
  1070. * } ProtocolNameList;
  1071. *
  1072. * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
  1073. */
  1074. /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
  1075. if( len < 4 )
  1076. {
  1077. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1078. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1079. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1080. }
  1081. list_len = ( buf[0] << 8 ) | buf[1];
  1082. if( list_len != len - 2 )
  1083. {
  1084. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1085. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1086. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1087. }
  1088. name_len = buf[2];
  1089. if( name_len != list_len - 1 )
  1090. {
  1091. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1092. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1093. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1094. }
  1095. /* Check that the server chosen protocol was in our list and save it */
  1096. for( p = ssl->conf->alpn_list; *p != NULL; p++ )
  1097. {
  1098. if( name_len == strlen( *p ) &&
  1099. memcmp( buf + 3, *p, name_len ) == 0 )
  1100. {
  1101. ssl->alpn_chosen = *p;
  1102. return( 0 );
  1103. }
  1104. }
  1105. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ALPN extension: no matching protocol" ) );
  1106. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1107. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1108. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1109. }
  1110. #endif /* MBEDTLS_SSL_ALPN */
  1111. /*
  1112. * Parse HelloVerifyRequest. Only called after verifying the HS type.
  1113. */
  1114. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1115. static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
  1116. {
  1117. const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  1118. int major_ver, minor_ver;
  1119. unsigned char cookie_len;
  1120. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
  1121. /*
  1122. * struct {
  1123. * ProtocolVersion server_version;
  1124. * opaque cookie<0..2^8-1>;
  1125. * } HelloVerifyRequest;
  1126. */
  1127. MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
  1128. mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
  1129. p += 2;
  1130. /*
  1131. * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
  1132. * even is lower than our min version.
  1133. */
  1134. if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
  1135. minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
  1136. major_ver > ssl->conf->max_major_ver ||
  1137. minor_ver > ssl->conf->max_minor_ver )
  1138. {
  1139. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
  1140. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1141. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  1142. return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  1143. }
  1144. cookie_len = *p++;
  1145. MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
  1146. if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
  1147. {
  1148. MBEDTLS_SSL_DEBUG_MSG( 1,
  1149. ( "cookie length does not match incoming message size" ) );
  1150. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1151. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1152. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1153. }
  1154. mbedtls_free( ssl->handshake->verify_cookie );
  1155. ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
  1156. if( ssl->handshake->verify_cookie == NULL )
  1157. {
  1158. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
  1159. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  1160. }
  1161. memcpy( ssl->handshake->verify_cookie, p, cookie_len );
  1162. ssl->handshake->verify_cookie_len = cookie_len;
  1163. /* Start over at ClientHello */
  1164. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  1165. mbedtls_ssl_reset_checksum( ssl );
  1166. mbedtls_ssl_recv_flight_completed( ssl );
  1167. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
  1168. return( 0 );
  1169. }
  1170. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1171. static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
  1172. {
  1173. int ret, i;
  1174. size_t n;
  1175. size_t ext_len;
  1176. unsigned char *buf, *ext;
  1177. unsigned char comp;
  1178. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1179. int accept_comp;
  1180. #endif
  1181. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1182. int renegotiation_info_seen = 0;
  1183. #endif
  1184. int handshake_failure = 0;
  1185. const mbedtls_ssl_ciphersuite_t *suite_info;
  1186. #if defined(MBEDTLS_DEBUG_C)
  1187. uint32_t t;
  1188. #endif
  1189. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
  1190. buf = ssl->in_msg;
  1191. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  1192. {
  1193. /* No alert on a read error. */
  1194. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  1195. return( ret );
  1196. }
  1197. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  1198. {
  1199. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1200. if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  1201. {
  1202. ssl->renego_records_seen++;
  1203. if( ssl->conf->renego_max_records >= 0 &&
  1204. ssl->renego_records_seen > ssl->conf->renego_max_records )
  1205. {
  1206. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
  1207. "but not honored by server" ) );
  1208. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1209. }
  1210. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
  1211. ssl->keep_current_message = 1;
  1212. return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
  1213. }
  1214. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1215. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1216. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1217. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  1218. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1219. }
  1220. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1221. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1222. {
  1223. if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
  1224. {
  1225. MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
  1226. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
  1227. return( ssl_parse_hello_verify_request( ssl ) );
  1228. }
  1229. else
  1230. {
  1231. /* We made it through the verification process */
  1232. mbedtls_free( ssl->handshake->verify_cookie );
  1233. ssl->handshake->verify_cookie = NULL;
  1234. ssl->handshake->verify_cookie_len = 0;
  1235. }
  1236. }
  1237. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1238. if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
  1239. buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
  1240. {
  1241. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1242. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1243. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1244. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1245. }
  1246. /*
  1247. * 0 . 1 server_version
  1248. * 2 . 33 random (maybe including 4 bytes of Unix time)
  1249. * 34 . 34 session_id length = n
  1250. * 35 . 34+n session_id
  1251. * 35+n . 36+n cipher_suite
  1252. * 37+n . 37+n compression_method
  1253. *
  1254. * 38+n . 39+n extensions length (optional)
  1255. * 40+n . .. extensions
  1256. */
  1257. buf += mbedtls_ssl_hs_hdr_len( ssl );
  1258. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
  1259. mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
  1260. ssl->conf->transport, buf + 0 );
  1261. if( ssl->major_ver < ssl->conf->min_major_ver ||
  1262. ssl->minor_ver < ssl->conf->min_minor_ver ||
  1263. ssl->major_ver > ssl->conf->max_major_ver ||
  1264. ssl->minor_ver > ssl->conf->max_minor_ver )
  1265. {
  1266. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
  1267. " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
  1268. ssl->conf->min_major_ver, ssl->conf->min_minor_ver,
  1269. ssl->major_ver, ssl->minor_ver,
  1270. ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) );
  1271. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1272. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  1273. return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  1274. }
  1275. #if defined(MBEDTLS_DEBUG_C)
  1276. t = ( (uint32_t) buf[2] << 24 )
  1277. | ( (uint32_t) buf[3] << 16 )
  1278. | ( (uint32_t) buf[4] << 8 )
  1279. | ( (uint32_t) buf[5] );
  1280. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
  1281. #endif
  1282. memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
  1283. n = buf[34];
  1284. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
  1285. if( n > 32 )
  1286. {
  1287. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1288. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1289. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1290. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1291. }
  1292. if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
  1293. {
  1294. ext_len = ( ( buf[38 + n] << 8 )
  1295. | ( buf[39 + n] ) );
  1296. if( ( ext_len > 0 && ext_len < 4 ) ||
  1297. ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
  1298. {
  1299. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1300. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1301. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1302. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1303. }
  1304. }
  1305. else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
  1306. {
  1307. ext_len = 0;
  1308. }
  1309. else
  1310. {
  1311. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1312. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1313. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1314. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1315. }
  1316. /* ciphersuite (used later) */
  1317. i = ( buf[35 + n] << 8 ) | buf[36 + n];
  1318. /*
  1319. * Read and check compression
  1320. */
  1321. comp = buf[37 + n];
  1322. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1323. /* See comments in ssl_write_client_hello() */
  1324. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1325. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1326. accept_comp = 0;
  1327. else
  1328. #endif
  1329. accept_comp = 1;
  1330. if( comp != MBEDTLS_SSL_COMPRESS_NULL &&
  1331. ( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) )
  1332. #else /* MBEDTLS_ZLIB_SUPPORT */
  1333. if( comp != MBEDTLS_SSL_COMPRESS_NULL )
  1334. #endif/* MBEDTLS_ZLIB_SUPPORT */
  1335. {
  1336. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
  1337. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1338. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1339. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  1340. }
  1341. /*
  1342. * Initialize update checksum functions
  1343. */
  1344. ssl->transform_negotiate->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
  1345. if( ssl->transform_negotiate->ciphersuite_info == NULL )
  1346. {
  1347. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
  1348. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1349. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  1350. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  1351. }
  1352. mbedtls_ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
  1353. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
  1354. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
  1355. /*
  1356. * Check if the session can be resumed
  1357. */
  1358. if( ssl->handshake->resume == 0 || n == 0 ||
  1359. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1360. ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
  1361. #endif
  1362. ssl->session_negotiate->ciphersuite != i ||
  1363. ssl->session_negotiate->compression != comp ||
  1364. ssl->session_negotiate->id_len != n ||
  1365. memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
  1366. {
  1367. ssl->state++;
  1368. ssl->handshake->resume = 0;
  1369. #if defined(MBEDTLS_HAVE_TIME)
  1370. ssl->session_negotiate->start = mbedtls_time( NULL );
  1371. #endif
  1372. ssl->session_negotiate->ciphersuite = i;
  1373. ssl->session_negotiate->compression = comp;
  1374. ssl->session_negotiate->id_len = n;
  1375. memcpy( ssl->session_negotiate->id, buf + 35, n );
  1376. }
  1377. else
  1378. {
  1379. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  1380. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  1381. {
  1382. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  1383. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1384. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  1385. return( ret );
  1386. }
  1387. }
  1388. MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
  1389. ssl->handshake->resume ? "a" : "no" ) );
  1390. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", i ) );
  1391. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) );
  1392. suite_info = mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
  1393. if( suite_info == NULL
  1394. #if defined(MBEDTLS_ARC4_C)
  1395. || ( ssl->conf->arc4_disabled &&
  1396. suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
  1397. #endif
  1398. )
  1399. {
  1400. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1401. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1402. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1403. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1404. }
  1405. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
  1406. i = 0;
  1407. while( 1 )
  1408. {
  1409. if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 )
  1410. {
  1411. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1412. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1413. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1414. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1415. }
  1416. if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] ==
  1417. ssl->session_negotiate->ciphersuite )
  1418. {
  1419. break;
  1420. }
  1421. }
  1422. if( comp != MBEDTLS_SSL_COMPRESS_NULL
  1423. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1424. && comp != MBEDTLS_SSL_COMPRESS_DEFLATE
  1425. #endif
  1426. )
  1427. {
  1428. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1429. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1430. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1431. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1432. }
  1433. ssl->session_negotiate->compression = comp;
  1434. ext = buf + 40 + n;
  1435. MBEDTLS_SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
  1436. while( ext_len )
  1437. {
  1438. unsigned int ext_id = ( ( ext[0] << 8 )
  1439. | ( ext[1] ) );
  1440. unsigned int ext_size = ( ( ext[2] << 8 )
  1441. | ( ext[3] ) );
  1442. if( ext_size + 4 > ext_len )
  1443. {
  1444. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1445. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1446. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1447. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1448. }
  1449. switch( ext_id )
  1450. {
  1451. case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
  1452. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
  1453. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1454. renegotiation_info_seen = 1;
  1455. #endif
  1456. if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
  1457. ext_size ) ) != 0 )
  1458. return( ret );
  1459. break;
  1460. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1461. case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
  1462. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
  1463. if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
  1464. ext + 4, ext_size ) ) != 0 )
  1465. {
  1466. return( ret );
  1467. }
  1468. break;
  1469. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  1470. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1471. case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
  1472. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
  1473. if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
  1474. ext + 4, ext_size ) ) != 0 )
  1475. {
  1476. return( ret );
  1477. }
  1478. break;
  1479. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  1480. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1481. case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
  1482. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
  1483. if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
  1484. ext + 4, ext_size ) ) != 0 )
  1485. {
  1486. return( ret );
  1487. }
  1488. break;
  1489. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  1490. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1491. case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
  1492. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
  1493. if( ( ret = ssl_parse_extended_ms_ext( ssl,
  1494. ext + 4, ext_size ) ) != 0 )
  1495. {
  1496. return( ret );
  1497. }
  1498. break;
  1499. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  1500. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1501. case MBEDTLS_TLS_EXT_SESSION_TICKET:
  1502. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
  1503. if( ( ret = ssl_parse_session_ticket_ext( ssl,
  1504. ext + 4, ext_size ) ) != 0 )
  1505. {
  1506. return( ret );
  1507. }
  1508. break;
  1509. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1510. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  1511. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1512. case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
  1513. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
  1514. if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
  1515. ext + 4, ext_size ) ) != 0 )
  1516. {
  1517. return( ret );
  1518. }
  1519. break;
  1520. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  1521. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1522. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1523. case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
  1524. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
  1525. if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
  1526. ext + 4, ext_size ) ) != 0 )
  1527. {
  1528. return( ret );
  1529. }
  1530. break;
  1531. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1532. #if defined(MBEDTLS_SSL_ALPN)
  1533. case MBEDTLS_TLS_EXT_ALPN:
  1534. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
  1535. if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
  1536. return( ret );
  1537. break;
  1538. #endif /* MBEDTLS_SSL_ALPN */
  1539. default:
  1540. MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
  1541. ext_id ) );
  1542. }
  1543. ext_len -= 4 + ext_size;
  1544. ext += 4 + ext_size;
  1545. if( ext_len > 0 && ext_len < 4 )
  1546. {
  1547. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1548. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1549. }
  1550. }
  1551. /*
  1552. * Renegotiation security checks
  1553. */
  1554. if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1555. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
  1556. {
  1557. MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
  1558. handshake_failure = 1;
  1559. }
  1560. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1561. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1562. ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  1563. renegotiation_info_seen == 0 )
  1564. {
  1565. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
  1566. handshake_failure = 1;
  1567. }
  1568. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1569. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1570. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
  1571. {
  1572. MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
  1573. handshake_failure = 1;
  1574. }
  1575. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1576. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1577. renegotiation_info_seen == 1 )
  1578. {
  1579. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
  1580. handshake_failure = 1;
  1581. }
  1582. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1583. if( handshake_failure == 1 )
  1584. {
  1585. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1586. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1587. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1588. }
  1589. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
  1590. return( 0 );
  1591. }
  1592. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1593. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  1594. static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl, unsigned char **p,
  1595. unsigned char *end )
  1596. {
  1597. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1598. /*
  1599. * Ephemeral DH parameters:
  1600. *
  1601. * struct {
  1602. * opaque dh_p<1..2^16-1>;
  1603. * opaque dh_g<1..2^16-1>;
  1604. * opaque dh_Ys<1..2^16-1>;
  1605. * } ServerDHParams;
  1606. */
  1607. if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
  1608. {
  1609. MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
  1610. return( ret );
  1611. }
  1612. if( ssl->handshake->dhm_ctx.len * 8 < ssl->conf->dhm_min_bitlen )
  1613. {
  1614. MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %d < %d",
  1615. ssl->handshake->dhm_ctx.len * 8,
  1616. ssl->conf->dhm_min_bitlen ) );
  1617. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1618. }
  1619. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
  1620. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
  1621. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
  1622. return( ret );
  1623. }
  1624. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1625. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  1626. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1627. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  1628. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  1629. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1630. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1631. static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
  1632. {
  1633. const mbedtls_ecp_curve_info *curve_info;
  1634. curve_info = mbedtls_ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
  1635. if( curve_info == NULL )
  1636. {
  1637. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1638. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  1639. }
  1640. MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
  1641. #if defined(MBEDTLS_ECP_C)
  1642. if( mbedtls_ssl_check_curve( ssl, ssl->handshake->ecdh_ctx.grp.id ) != 0 )
  1643. #else
  1644. if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
  1645. ssl->handshake->ecdh_ctx.grp.nbits > 521 )
  1646. #endif
  1647. return( -1 );
  1648. MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
  1649. return( 0 );
  1650. }
  1651. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1652. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  1653. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  1654. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  1655. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1656. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1657. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  1658. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  1659. static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
  1660. unsigned char **p,
  1661. unsigned char *end )
  1662. {
  1663. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1664. /*
  1665. * Ephemeral ECDH parameters:
  1666. *
  1667. * struct {
  1668. * ECParameters curve_params;
  1669. * ECPoint public;
  1670. * } ServerECDHParams;
  1671. */
  1672. if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
  1673. (const unsigned char **) p, end ) ) != 0 )
  1674. {
  1675. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
  1676. return( ret );
  1677. }
  1678. if( ssl_check_server_ecdh_params( ssl ) != 0 )
  1679. {
  1680. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
  1681. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1682. }
  1683. return( ret );
  1684. }
  1685. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1686. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  1687. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  1688. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1689. static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
  1690. unsigned char **p,
  1691. unsigned char *end )
  1692. {
  1693. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1694. size_t len;
  1695. ((void) ssl);
  1696. /*
  1697. * PSK parameters:
  1698. *
  1699. * opaque psk_identity_hint<0..2^16-1>;
  1700. */
  1701. len = (*p)[0] << 8 | (*p)[1];
  1702. *p += 2;
  1703. if( (*p) + len > end )
  1704. {
  1705. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message "
  1706. "(psk_identity_hint length)" ) );
  1707. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1708. }
  1709. /*
  1710. * Note: we currently ignore the PKS identity hint, as we only allow one
  1711. * PSK to be provisionned on the client. This could be changed later if
  1712. * someone needs that feature.
  1713. */
  1714. *p += len;
  1715. ret = 0;
  1716. return( ret );
  1717. }
  1718. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  1719. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  1720. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  1721. /*
  1722. * Generate a pre-master secret and encrypt it with the server's RSA key
  1723. */
  1724. static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
  1725. size_t offset, size_t *olen,
  1726. size_t pms_offset )
  1727. {
  1728. int ret;
  1729. size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2;
  1730. unsigned char *p = ssl->handshake->premaster + pms_offset;
  1731. if( offset + len_bytes > MBEDTLS_SSL_MAX_CONTENT_LEN )
  1732. {
  1733. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
  1734. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  1735. }
  1736. /*
  1737. * Generate (part of) the pre-master as
  1738. * struct {
  1739. * ProtocolVersion client_version;
  1740. * opaque random[46];
  1741. * } PreMasterSecret;
  1742. */
  1743. mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
  1744. ssl->conf->transport, p );
  1745. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
  1746. {
  1747. MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
  1748. return( ret );
  1749. }
  1750. ssl->handshake->pmslen = 48;
  1751. if( ssl->session_negotiate->peer_cert == NULL )
  1752. {
  1753. MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
  1754. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1755. }
  1756. /*
  1757. * Now write it out, encrypted
  1758. */
  1759. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
  1760. MBEDTLS_PK_RSA ) )
  1761. {
  1762. MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
  1763. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  1764. }
  1765. if( ( ret = mbedtls_pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
  1766. p, ssl->handshake->pmslen,
  1767. ssl->out_msg + offset + len_bytes, olen,
  1768. MBEDTLS_SSL_MAX_CONTENT_LEN - offset - len_bytes,
  1769. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  1770. {
  1771. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
  1772. return( ret );
  1773. }
  1774. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  1775. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1776. if( len_bytes == 2 )
  1777. {
  1778. ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
  1779. ssl->out_msg[offset+1] = (unsigned char)( *olen );
  1780. *olen += 2;
  1781. }
  1782. #endif
  1783. return( 0 );
  1784. }
  1785. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
  1786. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  1787. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1788. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1789. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1790. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1791. static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
  1792. unsigned char **p,
  1793. unsigned char *end,
  1794. mbedtls_md_type_t *md_alg,
  1795. mbedtls_pk_type_t *pk_alg )
  1796. {
  1797. ((void) ssl);
  1798. *md_alg = MBEDTLS_MD_NONE;
  1799. *pk_alg = MBEDTLS_PK_NONE;
  1800. /* Only in TLS 1.2 */
  1801. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  1802. {
  1803. return( 0 );
  1804. }
  1805. if( (*p) + 2 > end )
  1806. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1807. /*
  1808. * Get hash algorithm
  1809. */
  1810. if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) ) == MBEDTLS_MD_NONE )
  1811. {
  1812. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server used unsupported "
  1813. "HashAlgorithm %d", *(p)[0] ) );
  1814. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1815. }
  1816. /*
  1817. * Get signature algorithm
  1818. */
  1819. if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) ) == MBEDTLS_PK_NONE )
  1820. {
  1821. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used unsupported "
  1822. "SignatureAlgorithm %d", (*p)[1] ) );
  1823. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1824. }
  1825. /*
  1826. * Check if the hash is acceptable
  1827. */
  1828. if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 )
  1829. {
  1830. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used HashAlgorithm %d that was not offered",
  1831. *(p)[0] ) );
  1832. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1833. }
  1834. MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
  1835. MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
  1836. *p += 2;
  1837. return( 0 );
  1838. }
  1839. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1840. MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1841. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  1842. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1843. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1844. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1845. static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
  1846. {
  1847. int ret;
  1848. const mbedtls_ecp_keypair *peer_key;
  1849. if( ssl->session_negotiate->peer_cert == NULL )
  1850. {
  1851. MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
  1852. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1853. }
  1854. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
  1855. MBEDTLS_PK_ECKEY ) )
  1856. {
  1857. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
  1858. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  1859. }
  1860. peer_key = mbedtls_pk_ec( ssl->session_negotiate->peer_cert->pk );
  1861. if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
  1862. MBEDTLS_ECDH_THEIRS ) ) != 0 )
  1863. {
  1864. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
  1865. return( ret );
  1866. }
  1867. if( ssl_check_server_ecdh_params( ssl ) != 0 )
  1868. {
  1869. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
  1870. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  1871. }
  1872. return( ret );
  1873. }
  1874. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
  1875. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1876. static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
  1877. {
  1878. int ret;
  1879. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  1880. ssl->transform_negotiate->ciphersuite_info;
  1881. unsigned char *p, *end;
  1882. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
  1883. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  1884. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
  1885. {
  1886. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
  1887. ssl->state++;
  1888. return( 0 );
  1889. }
  1890. ((void) p);
  1891. ((void) end);
  1892. #endif
  1893. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1894. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1895. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  1896. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
  1897. {
  1898. if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
  1899. {
  1900. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
  1901. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1902. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1903. return( ret );
  1904. }
  1905. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
  1906. ssl->state++;
  1907. return( 0 );
  1908. }
  1909. ((void) p);
  1910. ((void) end);
  1911. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  1912. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1913. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  1914. {
  1915. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  1916. return( ret );
  1917. }
  1918. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  1919. {
  1920. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1921. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1922. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  1923. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1924. }
  1925. /*
  1926. * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
  1927. * doesn't use a psk_identity_hint
  1928. */
  1929. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
  1930. {
  1931. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  1932. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  1933. {
  1934. /* Current message is probably either
  1935. * CertificateRequest or ServerHelloDone */
  1936. ssl->keep_current_message = 1;
  1937. goto exit;
  1938. }
  1939. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key exchange message must "
  1940. "not be skipped" ) );
  1941. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1942. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  1943. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1944. }
  1945. p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  1946. end = ssl->in_msg + ssl->in_hslen;
  1947. MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
  1948. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1949. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  1950. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  1951. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  1952. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  1953. {
  1954. if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
  1955. {
  1956. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1957. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1958. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1959. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1960. }
  1961. } /* FALLTROUGH */
  1962. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  1963. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
  1964. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  1965. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  1966. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  1967. ; /* nothing more to do */
  1968. else
  1969. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
  1970. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  1971. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1972. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  1973. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
  1974. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  1975. {
  1976. if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
  1977. {
  1978. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1979. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1980. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1981. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1982. }
  1983. }
  1984. else
  1985. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1986. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  1987. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1988. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  1989. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1990. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  1991. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  1992. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
  1993. {
  1994. if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
  1995. {
  1996. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1997. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1998. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1999. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2000. }
  2001. }
  2002. else
  2003. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2004. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  2005. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  2006. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2007. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2008. {
  2009. ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
  2010. p, end - p );
  2011. if( ret != 0 )
  2012. {
  2013. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
  2014. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2015. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2016. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2017. }
  2018. }
  2019. else
  2020. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2021. {
  2022. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2023. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2024. }
  2025. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
  2026. if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
  2027. {
  2028. size_t sig_len, hashlen;
  2029. unsigned char hash[64];
  2030. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  2031. mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
  2032. unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  2033. size_t params_len = p - params;
  2034. /*
  2035. * Handle the digitally-signed structure
  2036. */
  2037. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2038. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  2039. {
  2040. if( ssl_parse_signature_algorithm( ssl, &p, end,
  2041. &md_alg, &pk_alg ) != 0 )
  2042. {
  2043. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2044. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2045. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2046. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2047. }
  2048. if( pk_alg != mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
  2049. {
  2050. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2051. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2052. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2053. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2054. }
  2055. }
  2056. else
  2057. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2058. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2059. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2060. if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
  2061. {
  2062. pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
  2063. /* Default hash for ECDSA is SHA-1 */
  2064. if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE )
  2065. md_alg = MBEDTLS_MD_SHA1;
  2066. }
  2067. else
  2068. #endif
  2069. {
  2070. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2071. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2072. }
  2073. /*
  2074. * Read signature
  2075. */
  2076. sig_len = ( p[0] << 8 ) | p[1];
  2077. p += 2;
  2078. if( end != p + sig_len )
  2079. {
  2080. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2081. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2082. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2083. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2084. }
  2085. MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
  2086. /*
  2087. * Compute the hash that has been signed
  2088. */
  2089. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2090. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2091. if( md_alg == MBEDTLS_MD_NONE )
  2092. {
  2093. mbedtls_md5_context mbedtls_md5;
  2094. mbedtls_sha1_context mbedtls_sha1;
  2095. mbedtls_md5_init( &mbedtls_md5 );
  2096. mbedtls_sha1_init( &mbedtls_sha1 );
  2097. hashlen = 36;
  2098. /*
  2099. * digitally-signed struct {
  2100. * opaque md5_hash[16];
  2101. * opaque sha_hash[20];
  2102. * };
  2103. *
  2104. * md5_hash
  2105. * MD5(ClientHello.random + ServerHello.random
  2106. * + ServerParams);
  2107. * sha_hash
  2108. * SHA(ClientHello.random + ServerHello.random
  2109. * + ServerParams);
  2110. */
  2111. mbedtls_md5_starts( &mbedtls_md5 );
  2112. mbedtls_md5_update( &mbedtls_md5, ssl->handshake->randbytes, 64 );
  2113. mbedtls_md5_update( &mbedtls_md5, params, params_len );
  2114. mbedtls_md5_finish( &mbedtls_md5, hash );
  2115. mbedtls_sha1_starts( &mbedtls_sha1 );
  2116. mbedtls_sha1_update( &mbedtls_sha1, ssl->handshake->randbytes, 64 );
  2117. mbedtls_sha1_update( &mbedtls_sha1, params, params_len );
  2118. mbedtls_sha1_finish( &mbedtls_sha1, hash + 16 );
  2119. mbedtls_md5_free( &mbedtls_md5 );
  2120. mbedtls_sha1_free( &mbedtls_sha1 );
  2121. }
  2122. else
  2123. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  2124. MBEDTLS_SSL_PROTO_TLS1_1 */
  2125. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  2126. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2127. if( md_alg != MBEDTLS_MD_NONE )
  2128. {
  2129. mbedtls_md_context_t ctx;
  2130. mbedtls_md_init( &ctx );
  2131. /* Info from md_alg will be used instead */
  2132. hashlen = 0;
  2133. /*
  2134. * digitally-signed struct {
  2135. * opaque client_random[32];
  2136. * opaque server_random[32];
  2137. * ServerDHParams params;
  2138. * };
  2139. */
  2140. if( ( ret = mbedtls_md_setup( &ctx,
  2141. mbedtls_md_info_from_type( md_alg ), 0 ) ) != 0 )
  2142. {
  2143. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
  2144. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2145. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  2146. return( ret );
  2147. }
  2148. mbedtls_md_starts( &ctx );
  2149. mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 );
  2150. mbedtls_md_update( &ctx, params, params_len );
  2151. mbedtls_md_finish( &ctx, hash );
  2152. mbedtls_md_free( &ctx );
  2153. }
  2154. else
  2155. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  2156. MBEDTLS_SSL_PROTO_TLS1_2 */
  2157. {
  2158. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2159. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2160. }
  2161. MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
  2162. (unsigned int) ( mbedtls_md_get_size( mbedtls_md_info_from_type( md_alg ) ) ) );
  2163. if( ssl->session_negotiate->peer_cert == NULL )
  2164. {
  2165. MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
  2166. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2167. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  2168. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2169. }
  2170. /*
  2171. * Verify signature
  2172. */
  2173. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
  2174. {
  2175. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2176. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2177. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  2178. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  2179. }
  2180. if( ( ret = mbedtls_pk_verify( &ssl->session_negotiate->peer_cert->pk,
  2181. md_alg, hash, hashlen, p, sig_len ) ) != 0 )
  2182. {
  2183. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2184. MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
  2185. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
  2186. return( ret );
  2187. }
  2188. }
  2189. #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */
  2190. exit:
  2191. ssl->state++;
  2192. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
  2193. return( 0 );
  2194. }
  2195. #if ! defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED)
  2196. static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
  2197. {
  2198. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2199. ssl->transform_negotiate->ciphersuite_info;
  2200. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
  2201. if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
  2202. {
  2203. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
  2204. ssl->state++;
  2205. return( 0 );
  2206. }
  2207. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2208. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2209. }
  2210. #else /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */
  2211. static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
  2212. {
  2213. int ret;
  2214. unsigned char *buf;
  2215. size_t n = 0;
  2216. size_t cert_type_len = 0, dn_len = 0;
  2217. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2218. ssl->transform_negotiate->ciphersuite_info;
  2219. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
  2220. if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
  2221. {
  2222. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
  2223. ssl->state++;
  2224. return( 0 );
  2225. }
  2226. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  2227. {
  2228. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2229. return( ret );
  2230. }
  2231. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2232. {
  2233. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2234. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2235. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  2236. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2237. }
  2238. ssl->state++;
  2239. ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
  2240. MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
  2241. ssl->client_auth ? "a" : "no" ) );
  2242. if( ssl->client_auth == 0 )
  2243. {
  2244. /* Current message is probably the ServerHelloDone */
  2245. ssl->keep_current_message = 1;
  2246. goto exit;
  2247. }
  2248. /*
  2249. * struct {
  2250. * ClientCertificateType certificate_types<1..2^8-1>;
  2251. * SignatureAndHashAlgorithm
  2252. * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
  2253. * DistinguishedName certificate_authorities<0..2^16-1>;
  2254. * } CertificateRequest;
  2255. *
  2256. * Since we only support a single certificate on clients, let's just
  2257. * ignore all the information that's supposed to help us pick a
  2258. * certificate.
  2259. *
  2260. * We could check that our certificate matches the request, and bail out
  2261. * if it doesn't, but it's simpler to just send the certificate anyway,
  2262. * and give the server the opportunity to decide if it should terminate
  2263. * the connection when it doesn't like our certificate.
  2264. *
  2265. * Same goes for the hash in TLS 1.2's signature_algorithms: at this
  2266. * point we only have one hash available (see comments in
  2267. * write_certificate_verify), so let's just use what we have.
  2268. *
  2269. * However, we still minimally parse the message to check it is at least
  2270. * superficially sane.
  2271. */
  2272. buf = ssl->in_msg;
  2273. /* certificate_types */
  2274. cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
  2275. n = cert_type_len;
  2276. if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
  2277. {
  2278. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2279. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2280. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2281. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  2282. }
  2283. /* supported_signature_algorithms */
  2284. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2285. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  2286. {
  2287. size_t sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
  2288. | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
  2289. #if defined(MBEDTLS_DEBUG_C)
  2290. unsigned char* sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
  2291. size_t i;
  2292. for( i = 0; i < sig_alg_len; i += 2 )
  2293. {
  2294. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Signature Algorithm found: %d"
  2295. ",%d", sig_alg[i], sig_alg[i + 1] ) );
  2296. }
  2297. #endif
  2298. n += 2 + sig_alg_len;
  2299. if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
  2300. {
  2301. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2302. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2303. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2304. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  2305. }
  2306. }
  2307. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2308. /* certificate_authorities */
  2309. dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
  2310. | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
  2311. n += dn_len;
  2312. if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
  2313. {
  2314. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2315. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2316. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2317. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  2318. }
  2319. exit:
  2320. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
  2321. return( 0 );
  2322. }
  2323. #endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */
  2324. static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
  2325. {
  2326. int ret;
  2327. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
  2328. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  2329. {
  2330. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2331. return( ret );
  2332. }
  2333. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2334. {
  2335. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
  2336. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2337. }
  2338. if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
  2339. ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
  2340. {
  2341. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
  2342. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2343. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2344. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
  2345. }
  2346. ssl->state++;
  2347. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2348. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  2349. mbedtls_ssl_recv_flight_completed( ssl );
  2350. #endif
  2351. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
  2352. return( 0 );
  2353. }
  2354. static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
  2355. {
  2356. int ret;
  2357. size_t i, n;
  2358. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2359. ssl->transform_negotiate->ciphersuite_info;
  2360. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
  2361. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  2362. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
  2363. {
  2364. /*
  2365. * DHM key exchange -- send G^X mod P
  2366. */
  2367. n = ssl->handshake->dhm_ctx.len;
  2368. ssl->out_msg[4] = (unsigned char)( n >> 8 );
  2369. ssl->out_msg[5] = (unsigned char)( n );
  2370. i = 6;
  2371. ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
  2372. (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
  2373. &ssl->out_msg[i], n,
  2374. ssl->conf->f_rng, ssl->conf->p_rng );
  2375. if( ret != 0 )
  2376. {
  2377. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
  2378. return( ret );
  2379. }
  2380. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
  2381. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
  2382. if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
  2383. ssl->handshake->premaster,
  2384. MBEDTLS_PREMASTER_SIZE,
  2385. &ssl->handshake->pmslen,
  2386. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  2387. {
  2388. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
  2389. return( ret );
  2390. }
  2391. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
  2392. }
  2393. else
  2394. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  2395. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2396. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  2397. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2398. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  2399. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  2400. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
  2401. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  2402. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
  2403. {
  2404. /*
  2405. * ECDH key exchange -- send client public value
  2406. */
  2407. i = 4;
  2408. ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
  2409. &n,
  2410. &ssl->out_msg[i], 1000,
  2411. ssl->conf->f_rng, ssl->conf->p_rng );
  2412. if( ret != 0 )
  2413. {
  2414. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
  2415. return( ret );
  2416. }
  2417. MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
  2418. if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
  2419. &ssl->handshake->pmslen,
  2420. ssl->handshake->premaster,
  2421. MBEDTLS_MPI_MAX_SIZE,
  2422. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  2423. {
  2424. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
  2425. return( ret );
  2426. }
  2427. MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
  2428. }
  2429. else
  2430. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2431. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  2432. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  2433. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  2434. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  2435. if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
  2436. {
  2437. /*
  2438. * opaque psk_identity<0..2^16-1>;
  2439. */
  2440. if( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL )
  2441. {
  2442. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for PSK" ) );
  2443. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  2444. }
  2445. i = 4;
  2446. n = ssl->conf->psk_identity_len;
  2447. if( i + 2 + n > MBEDTLS_SSL_MAX_CONTENT_LEN )
  2448. {
  2449. MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity too long or "
  2450. "SSL buffer too short" ) );
  2451. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  2452. }
  2453. ssl->out_msg[i++] = (unsigned char)( n >> 8 );
  2454. ssl->out_msg[i++] = (unsigned char)( n );
  2455. memcpy( ssl->out_msg + i, ssl->conf->psk_identity, ssl->conf->psk_identity_len );
  2456. i += ssl->conf->psk_identity_len;
  2457. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  2458. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
  2459. {
  2460. n = 0;
  2461. }
  2462. else
  2463. #endif
  2464. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  2465. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  2466. {
  2467. if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
  2468. return( ret );
  2469. }
  2470. else
  2471. #endif
  2472. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  2473. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  2474. {
  2475. /*
  2476. * ClientDiffieHellmanPublic public (DHM send G^X mod P)
  2477. */
  2478. n = ssl->handshake->dhm_ctx.len;
  2479. if( i + 2 + n > MBEDTLS_SSL_MAX_CONTENT_LEN )
  2480. {
  2481. MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity or DHM size too long"
  2482. " or SSL buffer too short" ) );
  2483. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  2484. }
  2485. ssl->out_msg[i++] = (unsigned char)( n >> 8 );
  2486. ssl->out_msg[i++] = (unsigned char)( n );
  2487. ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
  2488. (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
  2489. &ssl->out_msg[i], n,
  2490. ssl->conf->f_rng, ssl->conf->p_rng );
  2491. if( ret != 0 )
  2492. {
  2493. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
  2494. return( ret );
  2495. }
  2496. }
  2497. else
  2498. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  2499. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  2500. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  2501. {
  2502. /*
  2503. * ClientECDiffieHellmanPublic public;
  2504. */
  2505. ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
  2506. &ssl->out_msg[i], MBEDTLS_SSL_MAX_CONTENT_LEN - i,
  2507. ssl->conf->f_rng, ssl->conf->p_rng );
  2508. if( ret != 0 )
  2509. {
  2510. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
  2511. return( ret );
  2512. }
  2513. MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
  2514. }
  2515. else
  2516. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  2517. {
  2518. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2519. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2520. }
  2521. if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
  2522. ciphersuite_info->key_exchange ) ) != 0 )
  2523. {
  2524. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
  2525. return( ret );
  2526. }
  2527. }
  2528. else
  2529. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  2530. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  2531. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
  2532. {
  2533. i = 4;
  2534. if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
  2535. return( ret );
  2536. }
  2537. else
  2538. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  2539. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2540. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2541. {
  2542. i = 4;
  2543. ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
  2544. ssl->out_msg + i, MBEDTLS_SSL_MAX_CONTENT_LEN - i, &n,
  2545. ssl->conf->f_rng, ssl->conf->p_rng );
  2546. if( ret != 0 )
  2547. {
  2548. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
  2549. return( ret );
  2550. }
  2551. ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
  2552. ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
  2553. ssl->conf->f_rng, ssl->conf->p_rng );
  2554. if( ret != 0 )
  2555. {
  2556. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
  2557. return( ret );
  2558. }
  2559. }
  2560. else
  2561. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  2562. {
  2563. ((void) ciphersuite_info);
  2564. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2565. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2566. }
  2567. ssl->out_msglen = i + n;
  2568. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2569. ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
  2570. ssl->state++;
  2571. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  2572. {
  2573. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  2574. return( ret );
  2575. }
  2576. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
  2577. return( 0 );
  2578. }
  2579. #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
  2580. !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
  2581. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
  2582. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
  2583. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
  2584. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  2585. static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
  2586. {
  2587. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2588. ssl->transform_negotiate->ciphersuite_info;
  2589. int ret;
  2590. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
  2591. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  2592. {
  2593. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  2594. return( ret );
  2595. }
  2596. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2597. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  2598. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  2599. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2600. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2601. {
  2602. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  2603. ssl->state++;
  2604. return( 0 );
  2605. }
  2606. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2607. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2608. }
  2609. #else
  2610. static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
  2611. {
  2612. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2613. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2614. ssl->transform_negotiate->ciphersuite_info;
  2615. size_t n = 0, offset = 0;
  2616. unsigned char hash[48];
  2617. unsigned char *hash_start = hash;
  2618. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  2619. unsigned int hashlen;
  2620. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
  2621. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  2622. {
  2623. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  2624. return( ret );
  2625. }
  2626. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2627. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  2628. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  2629. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2630. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2631. {
  2632. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  2633. ssl->state++;
  2634. return( 0 );
  2635. }
  2636. if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL )
  2637. {
  2638. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  2639. ssl->state++;
  2640. return( 0 );
  2641. }
  2642. if( mbedtls_ssl_own_key( ssl ) == NULL )
  2643. {
  2644. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
  2645. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  2646. }
  2647. /*
  2648. * Make an RSA signature of the handshake digests
  2649. */
  2650. ssl->handshake->calc_verify( ssl, hash );
  2651. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2652. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2653. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  2654. {
  2655. /*
  2656. * digitally-signed struct {
  2657. * opaque md5_hash[16];
  2658. * opaque sha_hash[20];
  2659. * };
  2660. *
  2661. * md5_hash
  2662. * MD5(handshake_messages);
  2663. *
  2664. * sha_hash
  2665. * SHA(handshake_messages);
  2666. */
  2667. hashlen = 36;
  2668. md_alg = MBEDTLS_MD_NONE;
  2669. /*
  2670. * For ECDSA, default hash is SHA-1 only
  2671. */
  2672. if( mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECDSA ) )
  2673. {
  2674. hash_start += 16;
  2675. hashlen -= 16;
  2676. md_alg = MBEDTLS_MD_SHA1;
  2677. }
  2678. }
  2679. else
  2680. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  2681. MBEDTLS_SSL_PROTO_TLS1_1 */
  2682. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2683. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  2684. {
  2685. /*
  2686. * digitally-signed struct {
  2687. * opaque handshake_messages[handshake_messages_length];
  2688. * };
  2689. *
  2690. * Taking shortcut here. We assume that the server always allows the
  2691. * PRF Hash function and has sent it in the allowed signature
  2692. * algorithms list received in the Certificate Request message.
  2693. *
  2694. * Until we encounter a server that does not, we will take this
  2695. * shortcut.
  2696. *
  2697. * Reason: Otherwise we should have running hashes for SHA512 and SHA224
  2698. * in order to satisfy 'weird' needs from the server side.
  2699. */
  2700. if( ssl->transform_negotiate->ciphersuite_info->mac ==
  2701. MBEDTLS_MD_SHA384 )
  2702. {
  2703. md_alg = MBEDTLS_MD_SHA384;
  2704. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
  2705. }
  2706. else
  2707. {
  2708. md_alg = MBEDTLS_MD_SHA256;
  2709. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
  2710. }
  2711. ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
  2712. /* Info from md_alg will be used instead */
  2713. hashlen = 0;
  2714. offset = 2;
  2715. }
  2716. else
  2717. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2718. {
  2719. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2720. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2721. }
  2722. if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), md_alg, hash_start, hashlen,
  2723. ssl->out_msg + 6 + offset, &n,
  2724. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  2725. {
  2726. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
  2727. return( ret );
  2728. }
  2729. ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
  2730. ssl->out_msg[5 + offset] = (unsigned char)( n );
  2731. ssl->out_msglen = 6 + n + offset;
  2732. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2733. ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
  2734. ssl->state++;
  2735. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  2736. {
  2737. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  2738. return( ret );
  2739. }
  2740. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
  2741. return( ret );
  2742. }
  2743. #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
  2744. !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
  2745. !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
  2746. !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
  2747. !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
  2748. !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  2749. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2750. static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
  2751. {
  2752. int ret;
  2753. uint32_t lifetime;
  2754. size_t ticket_len;
  2755. unsigned char *ticket;
  2756. const unsigned char *msg;
  2757. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
  2758. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  2759. {
  2760. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2761. return( ret );
  2762. }
  2763. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2764. {
  2765. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  2766. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2767. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  2768. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2769. }
  2770. /*
  2771. * struct {
  2772. * uint32 ticket_lifetime_hint;
  2773. * opaque ticket<0..2^16-1>;
  2774. * } NewSessionTicket;
  2775. *
  2776. * 0 . 3 ticket_lifetime_hint
  2777. * 4 . 5 ticket_len (n)
  2778. * 6 . 5+n ticket content
  2779. */
  2780. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
  2781. ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
  2782. {
  2783. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  2784. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2785. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2786. return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
  2787. }
  2788. msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  2789. lifetime = ( msg[0] << 24 ) | ( msg[1] << 16 ) |
  2790. ( msg[2] << 8 ) | ( msg[3] );
  2791. ticket_len = ( msg[4] << 8 ) | ( msg[5] );
  2792. if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
  2793. {
  2794. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  2795. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2796. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2797. return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
  2798. }
  2799. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
  2800. /* We're not waiting for a NewSessionTicket message any more */
  2801. ssl->handshake->new_session_ticket = 0;
  2802. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  2803. /*
  2804. * Zero-length ticket means the server changed his mind and doesn't want
  2805. * to send a ticket after all, so just forget it
  2806. */
  2807. if( ticket_len == 0 )
  2808. return( 0 );
  2809. mbedtls_zeroize( ssl->session_negotiate->ticket,
  2810. ssl->session_negotiate->ticket_len );
  2811. mbedtls_free( ssl->session_negotiate->ticket );
  2812. ssl->session_negotiate->ticket = NULL;
  2813. ssl->session_negotiate->ticket_len = 0;
  2814. if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
  2815. {
  2816. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
  2817. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2818. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  2819. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  2820. }
  2821. memcpy( ticket, msg + 6, ticket_len );
  2822. ssl->session_negotiate->ticket = ticket;
  2823. ssl->session_negotiate->ticket_len = ticket_len;
  2824. ssl->session_negotiate->ticket_lifetime = lifetime;
  2825. /*
  2826. * RFC 5077 section 3.4:
  2827. * "If the client receives a session ticket from the server, then it
  2828. * discards any Session ID that was sent in the ServerHello."
  2829. */
  2830. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
  2831. ssl->session_negotiate->id_len = 0;
  2832. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
  2833. return( 0 );
  2834. }
  2835. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  2836. /*
  2837. * SSL handshake -- client side -- single step
  2838. */
  2839. int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
  2840. {
  2841. int ret = 0;
  2842. if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
  2843. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  2844. MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
  2845. if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
  2846. return( ret );
  2847. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2848. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  2849. ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
  2850. {
  2851. if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
  2852. return( ret );
  2853. }
  2854. #endif
  2855. /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
  2856. * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
  2857. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2858. if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
  2859. ssl->handshake->new_session_ticket != 0 )
  2860. {
  2861. ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
  2862. }
  2863. #endif
  2864. switch( ssl->state )
  2865. {
  2866. case MBEDTLS_SSL_HELLO_REQUEST:
  2867. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  2868. break;
  2869. /*
  2870. * ==> ClientHello
  2871. */
  2872. case MBEDTLS_SSL_CLIENT_HELLO:
  2873. ret = ssl_write_client_hello( ssl );
  2874. break;
  2875. /*
  2876. * <== ServerHello
  2877. * Certificate
  2878. * ( ServerKeyExchange )
  2879. * ( CertificateRequest )
  2880. * ServerHelloDone
  2881. */
  2882. case MBEDTLS_SSL_SERVER_HELLO:
  2883. ret = ssl_parse_server_hello( ssl );
  2884. break;
  2885. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  2886. ret = mbedtls_ssl_parse_certificate( ssl );
  2887. break;
  2888. case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
  2889. ret = ssl_parse_server_key_exchange( ssl );
  2890. break;
  2891. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  2892. ret = ssl_parse_certificate_request( ssl );
  2893. break;
  2894. case MBEDTLS_SSL_SERVER_HELLO_DONE:
  2895. ret = ssl_parse_server_hello_done( ssl );
  2896. break;
  2897. /*
  2898. * ==> ( Certificate/Alert )
  2899. * ClientKeyExchange
  2900. * ( CertificateVerify )
  2901. * ChangeCipherSpec
  2902. * Finished
  2903. */
  2904. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  2905. ret = mbedtls_ssl_write_certificate( ssl );
  2906. break;
  2907. case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
  2908. ret = ssl_write_client_key_exchange( ssl );
  2909. break;
  2910. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  2911. ret = ssl_write_certificate_verify( ssl );
  2912. break;
  2913. case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
  2914. ret = mbedtls_ssl_write_change_cipher_spec( ssl );
  2915. break;
  2916. case MBEDTLS_SSL_CLIENT_FINISHED:
  2917. ret = mbedtls_ssl_write_finished( ssl );
  2918. break;
  2919. /*
  2920. * <== ( NewSessionTicket )
  2921. * ChangeCipherSpec
  2922. * Finished
  2923. */
  2924. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2925. case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
  2926. ret = ssl_parse_new_session_ticket( ssl );
  2927. break;
  2928. #endif
  2929. case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
  2930. ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
  2931. break;
  2932. case MBEDTLS_SSL_SERVER_FINISHED:
  2933. ret = mbedtls_ssl_parse_finished( ssl );
  2934. break;
  2935. case MBEDTLS_SSL_FLUSH_BUFFERS:
  2936. MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
  2937. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  2938. break;
  2939. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  2940. mbedtls_ssl_handshake_wrapup( ssl );
  2941. break;
  2942. default:
  2943. MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
  2944. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  2945. }
  2946. return( ret );
  2947. }
  2948. #endif /* MBEDTLS_SSL_CLI_C */