ssl_cli.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408
  1. /*
  2. * SSLv3/TLSv1 client-side functions
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_SSL_CLI_C)
  29. #if defined(MBEDTLS_PLATFORM_C)
  30. #include "mbedtls/platform.h"
  31. #else
  32. #include <stdlib.h>
  33. #define mbedtls_calloc calloc
  34. #define mbedtls_free free
  35. #endif
  36. #include "mbedtls/debug.h"
  37. #include "mbedtls/ssl.h"
  38. #include "mbedtls/ssl_internal.h"
  39. #include <string.h>
  40. #include <stdint.h>
  41. #if defined(MBEDTLS_HAVE_TIME)
  42. #include "mbedtls/platform_time.h"
  43. #endif
  44. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  45. /* Implementation that should never be optimized out by the compiler */
  46. static void mbedtls_zeroize( void *v, size_t n ) {
  47. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  48. }
  49. #endif
  50. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  51. static void ssl_write_hostname_ext( mbedtls_ssl_context *ssl,
  52. unsigned char *buf,
  53. size_t *olen )
  54. {
  55. unsigned char *p = buf;
  56. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  57. size_t hostname_len;
  58. *olen = 0;
  59. if( ssl->hostname == NULL )
  60. return;
  61. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
  62. ssl->hostname ) );
  63. hostname_len = strlen( ssl->hostname );
  64. if( end < p || (size_t)( end - p ) < hostname_len + 9 )
  65. {
  66. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  67. return;
  68. }
  69. /*
  70. * struct {
  71. * NameType name_type;
  72. * select (name_type) {
  73. * case host_name: HostName;
  74. * } name;
  75. * } ServerName;
  76. *
  77. * enum {
  78. * host_name(0), (255)
  79. * } NameType;
  80. *
  81. * opaque HostName<1..2^16-1>;
  82. *
  83. * struct {
  84. * ServerName server_name_list<1..2^16-1>
  85. * } ServerNameList;
  86. */
  87. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
  88. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME ) & 0xFF );
  89. *p++ = (unsigned char)( ( (hostname_len + 5) >> 8 ) & 0xFF );
  90. *p++ = (unsigned char)( ( (hostname_len + 5) ) & 0xFF );
  91. *p++ = (unsigned char)( ( (hostname_len + 3) >> 8 ) & 0xFF );
  92. *p++ = (unsigned char)( ( (hostname_len + 3) ) & 0xFF );
  93. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
  94. *p++ = (unsigned char)( ( hostname_len >> 8 ) & 0xFF );
  95. *p++ = (unsigned char)( ( hostname_len ) & 0xFF );
  96. memcpy( p, ssl->hostname, hostname_len );
  97. *olen = hostname_len + 9;
  98. }
  99. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  100. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  101. static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl,
  102. unsigned char *buf,
  103. size_t *olen )
  104. {
  105. unsigned char *p = buf;
  106. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  107. *olen = 0;
  108. if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  109. return;
  110. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
  111. if( end < p || (size_t)( end - p ) < 5 + ssl->verify_data_len )
  112. {
  113. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  114. return;
  115. }
  116. /*
  117. * Secure renegotiation
  118. */
  119. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
  120. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
  121. *p++ = 0x00;
  122. *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
  123. *p++ = ssl->verify_data_len & 0xFF;
  124. memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
  125. *olen = 5 + ssl->verify_data_len;
  126. }
  127. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  128. /*
  129. * Only if we handle at least one key exchange that needs signatures.
  130. */
  131. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  132. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  133. static void ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl,
  134. unsigned char *buf,
  135. size_t *olen )
  136. {
  137. unsigned char *p = buf;
  138. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  139. size_t sig_alg_len = 0;
  140. const int *md;
  141. #if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)
  142. unsigned char *sig_alg_list = buf + 6;
  143. #endif
  144. *olen = 0;
  145. if( ssl->conf->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  146. return;
  147. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
  148. for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
  149. {
  150. #if defined(MBEDTLS_ECDSA_C)
  151. sig_alg_len += 2;
  152. #endif
  153. #if defined(MBEDTLS_RSA_C)
  154. sig_alg_len += 2;
  155. #endif
  156. }
  157. if( end < p || (size_t)( end - p ) < sig_alg_len + 6 )
  158. {
  159. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  160. return;
  161. }
  162. /*
  163. * Prepare signature_algorithms extension (TLS 1.2)
  164. */
  165. sig_alg_len = 0;
  166. for( md = ssl->conf->sig_hashes; *md != MBEDTLS_MD_NONE; md++ )
  167. {
  168. #if defined(MBEDTLS_ECDSA_C)
  169. sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
  170. sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_ECDSA;
  171. #endif
  172. #if defined(MBEDTLS_RSA_C)
  173. sig_alg_list[sig_alg_len++] = mbedtls_ssl_hash_from_md_alg( *md );
  174. sig_alg_list[sig_alg_len++] = MBEDTLS_SSL_SIG_RSA;
  175. #endif
  176. }
  177. /*
  178. * enum {
  179. * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
  180. * sha512(6), (255)
  181. * } HashAlgorithm;
  182. *
  183. * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
  184. * SignatureAlgorithm;
  185. *
  186. * struct {
  187. * HashAlgorithm hash;
  188. * SignatureAlgorithm signature;
  189. * } SignatureAndHashAlgorithm;
  190. *
  191. * SignatureAndHashAlgorithm
  192. * supported_signature_algorithms<2..2^16-2>;
  193. */
  194. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
  195. *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG ) & 0xFF );
  196. *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
  197. *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
  198. *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
  199. *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
  200. *olen = 6 + sig_alg_len;
  201. }
  202. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  203. MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  204. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  205. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  206. static void ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl,
  207. unsigned char *buf,
  208. size_t *olen )
  209. {
  210. unsigned char *p = buf;
  211. const unsigned char *end = ssl->out_msg + MBEDTLS_SSL_MAX_CONTENT_LEN;
  212. unsigned char *elliptic_curve_list = p + 6;
  213. size_t elliptic_curve_len = 0;
  214. const mbedtls_ecp_curve_info *info;
  215. #if defined(MBEDTLS_ECP_C)
  216. const mbedtls_ecp_group_id *grp_id;
  217. #else
  218. ((void) ssl);
  219. #endif
  220. *olen = 0;
  221. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
  222. #if defined(MBEDTLS_ECP_C)
  223. for( grp_id = ssl->conf->curve_list; *grp_id != MBEDTLS_ECP_DP_NONE; grp_id++ )
  224. {
  225. info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
  226. #else
  227. for( info = mbedtls_ecp_curve_list(); info->grp_id != MBEDTLS_ECP_DP_NONE; info++ )
  228. {
  229. #endif
  230. if( info == NULL )
  231. {
  232. MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid curve in ssl configuration" ) );
  233. return;
  234. }
  235. elliptic_curve_len += 2;
  236. }
  237. if( end < p || (size_t)( end - p ) < 6 + elliptic_curve_len )
  238. {
  239. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
  240. return;
  241. }
  242. elliptic_curve_len = 0;
  243. #if defined(MBEDTLS_ECP_C)
  244. for( grp_id = ssl->conf->curve_list; *grp_id != MBEDTLS_ECP_DP_NONE; grp_id++ )
  245. {
  246. info = mbedtls_ecp_curve_info_from_grp_id( *grp_id );
  247. #else
  248. for( info = mbedtls_ecp_curve_list(); info->grp_id != MBEDTLS_ECP_DP_NONE; info++ )
  249. {
  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. int ret;
  858. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  859. if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE )
  860. {
  861. /* Check verify-data in constant-time. The length OTOH is no secret */
  862. if( len != 1 + ssl->verify_data_len * 2 ||
  863. buf[0] != ssl->verify_data_len * 2 ||
  864. mbedtls_ssl_safer_memcmp( buf + 1,
  865. ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
  866. mbedtls_ssl_safer_memcmp( buf + 1 + ssl->verify_data_len,
  867. ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
  868. {
  869. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
  870. if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
  871. return( ret );
  872. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  873. }
  874. }
  875. else
  876. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  877. {
  878. if( len != 1 || buf[0] != 0x00 )
  879. {
  880. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
  881. if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
  882. return( ret );
  883. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  884. }
  885. ssl->secure_renegotiation = MBEDTLS_SSL_SECURE_RENEGOTIATION;
  886. }
  887. return( 0 );
  888. }
  889. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  890. static int ssl_parse_max_fragment_length_ext( mbedtls_ssl_context *ssl,
  891. const unsigned char *buf,
  892. size_t len )
  893. {
  894. /*
  895. * server should use the extension only if we did,
  896. * and if so the server's value should match ours (and len is always 1)
  897. */
  898. if( ssl->conf->mfl_code == MBEDTLS_SSL_MAX_FRAG_LEN_NONE ||
  899. len != 1 ||
  900. buf[0] != ssl->conf->mfl_code )
  901. {
  902. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  903. }
  904. return( 0 );
  905. }
  906. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  907. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  908. static int ssl_parse_truncated_hmac_ext( mbedtls_ssl_context *ssl,
  909. const unsigned char *buf,
  910. size_t len )
  911. {
  912. if( ssl->conf->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_DISABLED ||
  913. len != 0 )
  914. {
  915. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  916. }
  917. ((void) buf);
  918. ssl->session_negotiate->trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED;
  919. return( 0 );
  920. }
  921. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  922. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  923. static int ssl_parse_encrypt_then_mac_ext( mbedtls_ssl_context *ssl,
  924. const unsigned char *buf,
  925. size_t len )
  926. {
  927. if( ssl->conf->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED ||
  928. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
  929. len != 0 )
  930. {
  931. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  932. }
  933. ((void) buf);
  934. ssl->session_negotiate->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
  935. return( 0 );
  936. }
  937. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  938. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  939. static int ssl_parse_extended_ms_ext( mbedtls_ssl_context *ssl,
  940. const unsigned char *buf,
  941. size_t len )
  942. {
  943. if( ssl->conf->extended_ms == MBEDTLS_SSL_EXTENDED_MS_DISABLED ||
  944. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
  945. len != 0 )
  946. {
  947. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  948. }
  949. ((void) buf);
  950. ssl->handshake->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  951. return( 0 );
  952. }
  953. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  954. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  955. static int ssl_parse_session_ticket_ext( mbedtls_ssl_context *ssl,
  956. const unsigned char *buf,
  957. size_t len )
  958. {
  959. if( ssl->conf->session_tickets == MBEDTLS_SSL_SESSION_TICKETS_DISABLED ||
  960. len != 0 )
  961. {
  962. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  963. }
  964. ((void) buf);
  965. ssl->handshake->new_session_ticket = 1;
  966. return( 0 );
  967. }
  968. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  969. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  970. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  971. static int ssl_parse_supported_point_formats_ext( mbedtls_ssl_context *ssl,
  972. const unsigned char *buf,
  973. size_t len )
  974. {
  975. size_t list_size;
  976. const unsigned char *p;
  977. list_size = buf[0];
  978. if( list_size + 1 != len )
  979. {
  980. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  981. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  982. }
  983. p = buf + 1;
  984. while( list_size > 0 )
  985. {
  986. if( p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
  987. p[0] == MBEDTLS_ECP_PF_COMPRESSED )
  988. {
  989. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C)
  990. ssl->handshake->ecdh_ctx.point_format = p[0];
  991. #endif
  992. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  993. ssl->handshake->ecjpake_ctx.point_format = p[0];
  994. #endif
  995. MBEDTLS_SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
  996. return( 0 );
  997. }
  998. list_size--;
  999. p++;
  1000. }
  1001. MBEDTLS_SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
  1002. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1003. }
  1004. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  1005. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1006. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1007. static int ssl_parse_ecjpake_kkpp( mbedtls_ssl_context *ssl,
  1008. const unsigned char *buf,
  1009. size_t len )
  1010. {
  1011. int ret;
  1012. if( ssl->transform_negotiate->ciphersuite_info->key_exchange !=
  1013. MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  1014. {
  1015. MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip ecjpake kkpp extension" ) );
  1016. return( 0 );
  1017. }
  1018. /* If we got here, we no longer need our cached extension */
  1019. mbedtls_free( ssl->handshake->ecjpake_cache );
  1020. ssl->handshake->ecjpake_cache = NULL;
  1021. ssl->handshake->ecjpake_cache_len = 0;
  1022. if( ( ret = mbedtls_ecjpake_read_round_one( &ssl->handshake->ecjpake_ctx,
  1023. buf, len ) ) != 0 )
  1024. {
  1025. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_one", ret );
  1026. return( ret );
  1027. }
  1028. return( 0 );
  1029. }
  1030. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1031. #if defined(MBEDTLS_SSL_ALPN)
  1032. static int ssl_parse_alpn_ext( mbedtls_ssl_context *ssl,
  1033. const unsigned char *buf, size_t len )
  1034. {
  1035. size_t list_len, name_len;
  1036. const char **p;
  1037. /* If we didn't send it, the server shouldn't send it */
  1038. if( ssl->conf->alpn_list == NULL )
  1039. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1040. /*
  1041. * opaque ProtocolName<1..2^8-1>;
  1042. *
  1043. * struct {
  1044. * ProtocolName protocol_name_list<2..2^16-1>
  1045. * } ProtocolNameList;
  1046. *
  1047. * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
  1048. */
  1049. /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
  1050. if( len < 4 )
  1051. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1052. list_len = ( buf[0] << 8 ) | buf[1];
  1053. if( list_len != len - 2 )
  1054. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1055. name_len = buf[2];
  1056. if( name_len != list_len - 1 )
  1057. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1058. /* Check that the server chosen protocol was in our list and save it */
  1059. for( p = ssl->conf->alpn_list; *p != NULL; p++ )
  1060. {
  1061. if( name_len == strlen( *p ) &&
  1062. memcmp( buf + 3, *p, name_len ) == 0 )
  1063. {
  1064. ssl->alpn_chosen = *p;
  1065. return( 0 );
  1066. }
  1067. }
  1068. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1069. }
  1070. #endif /* MBEDTLS_SSL_ALPN */
  1071. /*
  1072. * Parse HelloVerifyRequest. Only called after verifying the HS type.
  1073. */
  1074. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1075. static int ssl_parse_hello_verify_request( mbedtls_ssl_context *ssl )
  1076. {
  1077. const unsigned char *p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  1078. int major_ver, minor_ver;
  1079. unsigned char cookie_len;
  1080. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse hello verify request" ) );
  1081. /*
  1082. * struct {
  1083. * ProtocolVersion server_version;
  1084. * opaque cookie<0..2^8-1>;
  1085. * } HelloVerifyRequest;
  1086. */
  1087. MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
  1088. mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
  1089. p += 2;
  1090. /*
  1091. * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
  1092. * even is lower than our min version.
  1093. */
  1094. if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
  1095. minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
  1096. major_ver > ssl->conf->max_major_ver ||
  1097. minor_ver > ssl->conf->max_minor_ver )
  1098. {
  1099. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
  1100. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1101. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  1102. return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  1103. }
  1104. cookie_len = *p++;
  1105. MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
  1106. if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
  1107. {
  1108. MBEDTLS_SSL_DEBUG_MSG( 1,
  1109. ( "cookie length does not match incoming message size" ) );
  1110. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1111. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1112. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1113. }
  1114. mbedtls_free( ssl->handshake->verify_cookie );
  1115. ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
  1116. if( ssl->handshake->verify_cookie == NULL )
  1117. {
  1118. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
  1119. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  1120. }
  1121. memcpy( ssl->handshake->verify_cookie, p, cookie_len );
  1122. ssl->handshake->verify_cookie_len = cookie_len;
  1123. /* Start over at ClientHello */
  1124. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  1125. mbedtls_ssl_reset_checksum( ssl );
  1126. mbedtls_ssl_recv_flight_completed( ssl );
  1127. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
  1128. return( 0 );
  1129. }
  1130. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1131. static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
  1132. {
  1133. int ret, i;
  1134. size_t n;
  1135. size_t ext_len;
  1136. unsigned char *buf, *ext;
  1137. unsigned char comp;
  1138. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1139. int accept_comp;
  1140. #endif
  1141. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1142. int renegotiation_info_seen = 0;
  1143. #endif
  1144. int handshake_failure = 0;
  1145. const mbedtls_ssl_ciphersuite_t *suite_info;
  1146. #if defined(MBEDTLS_DEBUG_C)
  1147. uint32_t t;
  1148. #endif
  1149. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
  1150. buf = ssl->in_msg;
  1151. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  1152. {
  1153. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  1154. return( ret );
  1155. }
  1156. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  1157. {
  1158. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1159. if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  1160. {
  1161. ssl->renego_records_seen++;
  1162. if( ssl->conf->renego_max_records >= 0 &&
  1163. ssl->renego_records_seen > ssl->conf->renego_max_records )
  1164. {
  1165. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
  1166. "but not honored by server" ) );
  1167. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1168. }
  1169. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
  1170. return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
  1171. }
  1172. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1173. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1174. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1175. }
  1176. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1177. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1178. {
  1179. if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
  1180. {
  1181. MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
  1182. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
  1183. return( ssl_parse_hello_verify_request( ssl ) );
  1184. }
  1185. else
  1186. {
  1187. /* We made it through the verification process */
  1188. mbedtls_free( ssl->handshake->verify_cookie );
  1189. ssl->handshake->verify_cookie = NULL;
  1190. ssl->handshake->verify_cookie_len = 0;
  1191. }
  1192. }
  1193. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1194. if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
  1195. buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
  1196. {
  1197. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1198. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1199. }
  1200. /*
  1201. * 0 . 1 server_version
  1202. * 2 . 33 random (maybe including 4 bytes of Unix time)
  1203. * 34 . 34 session_id length = n
  1204. * 35 . 34+n session_id
  1205. * 35+n . 36+n cipher_suite
  1206. * 37+n . 37+n compression_method
  1207. *
  1208. * 38+n . 39+n extensions length (optional)
  1209. * 40+n . .. extensions
  1210. */
  1211. buf += mbedtls_ssl_hs_hdr_len( ssl );
  1212. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
  1213. mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
  1214. ssl->conf->transport, buf + 0 );
  1215. if( ssl->major_ver < ssl->conf->min_major_ver ||
  1216. ssl->minor_ver < ssl->conf->min_minor_ver ||
  1217. ssl->major_ver > ssl->conf->max_major_ver ||
  1218. ssl->minor_ver > ssl->conf->max_minor_ver )
  1219. {
  1220. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
  1221. " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
  1222. ssl->conf->min_major_ver, ssl->conf->min_minor_ver,
  1223. ssl->major_ver, ssl->minor_ver,
  1224. ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) );
  1225. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1226. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  1227. return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  1228. }
  1229. #if defined(MBEDTLS_DEBUG_C)
  1230. t = ( (uint32_t) buf[2] << 24 )
  1231. | ( (uint32_t) buf[3] << 16 )
  1232. | ( (uint32_t) buf[4] << 8 )
  1233. | ( (uint32_t) buf[5] );
  1234. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
  1235. #endif
  1236. memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
  1237. n = buf[34];
  1238. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
  1239. if( n > 32 )
  1240. {
  1241. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1242. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1243. }
  1244. if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
  1245. {
  1246. ext_len = ( ( buf[38 + n] << 8 )
  1247. | ( buf[39 + n] ) );
  1248. if( ( ext_len > 0 && ext_len < 4 ) ||
  1249. ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
  1250. {
  1251. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1252. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1253. }
  1254. }
  1255. else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
  1256. {
  1257. ext_len = 0;
  1258. }
  1259. else
  1260. {
  1261. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1262. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1263. }
  1264. /* ciphersuite (used later) */
  1265. i = ( buf[35 + n] << 8 ) | buf[36 + n];
  1266. /*
  1267. * Read and check compression
  1268. */
  1269. comp = buf[37 + n];
  1270. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1271. /* See comments in ssl_write_client_hello() */
  1272. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1273. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1274. accept_comp = 0;
  1275. else
  1276. #endif
  1277. accept_comp = 1;
  1278. if( comp != MBEDTLS_SSL_COMPRESS_NULL &&
  1279. ( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) )
  1280. #else /* MBEDTLS_ZLIB_SUPPORT */
  1281. if( comp != MBEDTLS_SSL_COMPRESS_NULL )
  1282. #endif/* MBEDTLS_ZLIB_SUPPORT */
  1283. {
  1284. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
  1285. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  1286. }
  1287. /*
  1288. * Initialize update checksum functions
  1289. */
  1290. ssl->transform_negotiate->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
  1291. if( ssl->transform_negotiate->ciphersuite_info == NULL )
  1292. {
  1293. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
  1294. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  1295. }
  1296. mbedtls_ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
  1297. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
  1298. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
  1299. /*
  1300. * Check if the session can be resumed
  1301. */
  1302. if( ssl->handshake->resume == 0 || n == 0 ||
  1303. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1304. ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
  1305. #endif
  1306. ssl->session_negotiate->ciphersuite != i ||
  1307. ssl->session_negotiate->compression != comp ||
  1308. ssl->session_negotiate->id_len != n ||
  1309. memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
  1310. {
  1311. ssl->state++;
  1312. ssl->handshake->resume = 0;
  1313. #if defined(MBEDTLS_HAVE_TIME)
  1314. ssl->session_negotiate->start = mbedtls_time( NULL );
  1315. #endif
  1316. ssl->session_negotiate->ciphersuite = i;
  1317. ssl->session_negotiate->compression = comp;
  1318. ssl->session_negotiate->id_len = n;
  1319. memcpy( ssl->session_negotiate->id, buf + 35, n );
  1320. }
  1321. else
  1322. {
  1323. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  1324. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  1325. {
  1326. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  1327. return( ret );
  1328. }
  1329. }
  1330. MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
  1331. ssl->handshake->resume ? "a" : "no" ) );
  1332. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", i ) );
  1333. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) );
  1334. suite_info = mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
  1335. if( suite_info == NULL
  1336. #if defined(MBEDTLS_ARC4_C)
  1337. || ( ssl->conf->arc4_disabled &&
  1338. suite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
  1339. #endif
  1340. )
  1341. {
  1342. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1343. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1344. }
  1345. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
  1346. i = 0;
  1347. while( 1 )
  1348. {
  1349. if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 )
  1350. {
  1351. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1352. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1353. }
  1354. if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] ==
  1355. ssl->session_negotiate->ciphersuite )
  1356. {
  1357. break;
  1358. }
  1359. }
  1360. if( comp != MBEDTLS_SSL_COMPRESS_NULL
  1361. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1362. && comp != MBEDTLS_SSL_COMPRESS_DEFLATE
  1363. #endif
  1364. )
  1365. {
  1366. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1367. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1368. }
  1369. ssl->session_negotiate->compression = comp;
  1370. ext = buf + 40 + n;
  1371. MBEDTLS_SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
  1372. while( ext_len )
  1373. {
  1374. unsigned int ext_id = ( ( ext[0] << 8 )
  1375. | ( ext[1] ) );
  1376. unsigned int ext_size = ( ( ext[2] << 8 )
  1377. | ( ext[3] ) );
  1378. if( ext_size + 4 > ext_len )
  1379. {
  1380. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1381. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1382. }
  1383. switch( ext_id )
  1384. {
  1385. case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
  1386. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
  1387. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1388. renegotiation_info_seen = 1;
  1389. #endif
  1390. if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
  1391. ext_size ) ) != 0 )
  1392. return( ret );
  1393. break;
  1394. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1395. case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
  1396. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
  1397. if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
  1398. ext + 4, ext_size ) ) != 0 )
  1399. {
  1400. return( ret );
  1401. }
  1402. break;
  1403. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  1404. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1405. case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
  1406. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
  1407. if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
  1408. ext + 4, ext_size ) ) != 0 )
  1409. {
  1410. return( ret );
  1411. }
  1412. break;
  1413. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  1414. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1415. case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
  1416. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
  1417. if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
  1418. ext + 4, ext_size ) ) != 0 )
  1419. {
  1420. return( ret );
  1421. }
  1422. break;
  1423. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  1424. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1425. case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
  1426. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
  1427. if( ( ret = ssl_parse_extended_ms_ext( ssl,
  1428. ext + 4, ext_size ) ) != 0 )
  1429. {
  1430. return( ret );
  1431. }
  1432. break;
  1433. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  1434. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1435. case MBEDTLS_TLS_EXT_SESSION_TICKET:
  1436. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
  1437. if( ( ret = ssl_parse_session_ticket_ext( ssl,
  1438. ext + 4, ext_size ) ) != 0 )
  1439. {
  1440. return( ret );
  1441. }
  1442. break;
  1443. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1444. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  1445. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1446. case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
  1447. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
  1448. if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
  1449. ext + 4, ext_size ) ) != 0 )
  1450. {
  1451. return( ret );
  1452. }
  1453. break;
  1454. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  1455. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1456. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1457. case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
  1458. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
  1459. if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
  1460. ext + 4, ext_size ) ) != 0 )
  1461. {
  1462. return( ret );
  1463. }
  1464. break;
  1465. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1466. #if defined(MBEDTLS_SSL_ALPN)
  1467. case MBEDTLS_TLS_EXT_ALPN:
  1468. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
  1469. if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
  1470. return( ret );
  1471. break;
  1472. #endif /* MBEDTLS_SSL_ALPN */
  1473. default:
  1474. MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
  1475. ext_id ) );
  1476. }
  1477. ext_len -= 4 + ext_size;
  1478. ext += 4 + ext_size;
  1479. if( ext_len > 0 && ext_len < 4 )
  1480. {
  1481. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1482. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1483. }
  1484. }
  1485. /*
  1486. * Renegotiation security checks
  1487. */
  1488. if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1489. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
  1490. {
  1491. MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
  1492. handshake_failure = 1;
  1493. }
  1494. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1495. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1496. ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  1497. renegotiation_info_seen == 0 )
  1498. {
  1499. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
  1500. handshake_failure = 1;
  1501. }
  1502. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1503. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1504. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
  1505. {
  1506. MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
  1507. handshake_failure = 1;
  1508. }
  1509. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1510. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1511. renegotiation_info_seen == 1 )
  1512. {
  1513. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
  1514. handshake_failure = 1;
  1515. }
  1516. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1517. if( handshake_failure == 1 )
  1518. {
  1519. if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
  1520. return( ret );
  1521. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1522. }
  1523. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
  1524. return( 0 );
  1525. }
  1526. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1527. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  1528. static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl, unsigned char **p,
  1529. unsigned char *end )
  1530. {
  1531. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1532. /*
  1533. * Ephemeral DH parameters:
  1534. *
  1535. * struct {
  1536. * opaque dh_p<1..2^16-1>;
  1537. * opaque dh_g<1..2^16-1>;
  1538. * opaque dh_Ys<1..2^16-1>;
  1539. * } ServerDHParams;
  1540. */
  1541. if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
  1542. {
  1543. MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
  1544. return( ret );
  1545. }
  1546. if( ssl->handshake->dhm_ctx.len * 8 < ssl->conf->dhm_min_bitlen )
  1547. {
  1548. MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %d < %d",
  1549. ssl->handshake->dhm_ctx.len * 8,
  1550. ssl->conf->dhm_min_bitlen ) );
  1551. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1552. }
  1553. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
  1554. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
  1555. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
  1556. return( ret );
  1557. }
  1558. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1559. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  1560. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1561. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  1562. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  1563. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1564. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1565. static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
  1566. {
  1567. const mbedtls_ecp_curve_info *curve_info;
  1568. curve_info = mbedtls_ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
  1569. if( curve_info == NULL )
  1570. {
  1571. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1572. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  1573. }
  1574. MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
  1575. #if defined(MBEDTLS_ECP_C)
  1576. if( mbedtls_ssl_check_curve( ssl, ssl->handshake->ecdh_ctx.grp.id ) != 0 )
  1577. #else
  1578. if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
  1579. ssl->handshake->ecdh_ctx.grp.nbits > 521 )
  1580. #endif
  1581. return( -1 );
  1582. MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
  1583. return( 0 );
  1584. }
  1585. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1586. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  1587. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  1588. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  1589. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1590. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1591. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  1592. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  1593. static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
  1594. unsigned char **p,
  1595. unsigned char *end )
  1596. {
  1597. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1598. /*
  1599. * Ephemeral ECDH parameters:
  1600. *
  1601. * struct {
  1602. * ECParameters curve_params;
  1603. * ECPoint public;
  1604. * } ServerECDHParams;
  1605. */
  1606. if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
  1607. (const unsigned char **) p, end ) ) != 0 )
  1608. {
  1609. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
  1610. return( ret );
  1611. }
  1612. if( ssl_check_server_ecdh_params( ssl ) != 0 )
  1613. {
  1614. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
  1615. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1616. }
  1617. return( ret );
  1618. }
  1619. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1620. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  1621. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  1622. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1623. static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
  1624. unsigned char **p,
  1625. unsigned char *end )
  1626. {
  1627. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1628. size_t len;
  1629. ((void) ssl);
  1630. /*
  1631. * PSK parameters:
  1632. *
  1633. * opaque psk_identity_hint<0..2^16-1>;
  1634. */
  1635. len = (*p)[0] << 8 | (*p)[1];
  1636. *p += 2;
  1637. if( (*p) + len > end )
  1638. {
  1639. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
  1640. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1641. }
  1642. /*
  1643. * Note: we currently ignore the PKS identity hint, as we only allow one
  1644. * PSK to be provisionned on the client. This could be changed later if
  1645. * someone needs that feature.
  1646. */
  1647. *p += len;
  1648. ret = 0;
  1649. return( ret );
  1650. }
  1651. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  1652. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  1653. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  1654. /*
  1655. * Generate a pre-master secret and encrypt it with the server's RSA key
  1656. */
  1657. static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
  1658. size_t offset, size_t *olen,
  1659. size_t pms_offset )
  1660. {
  1661. int ret;
  1662. size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2;
  1663. unsigned char *p = ssl->handshake->premaster + pms_offset;
  1664. if( offset + len_bytes > MBEDTLS_SSL_MAX_CONTENT_LEN )
  1665. {
  1666. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
  1667. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  1668. }
  1669. /*
  1670. * Generate (part of) the pre-master as
  1671. * struct {
  1672. * ProtocolVersion client_version;
  1673. * opaque random[46];
  1674. * } PreMasterSecret;
  1675. */
  1676. mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
  1677. ssl->conf->transport, p );
  1678. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
  1679. {
  1680. MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
  1681. return( ret );
  1682. }
  1683. ssl->handshake->pmslen = 48;
  1684. if( ssl->session_negotiate->peer_cert == NULL )
  1685. {
  1686. MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
  1687. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1688. }
  1689. /*
  1690. * Now write it out, encrypted
  1691. */
  1692. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
  1693. MBEDTLS_PK_RSA ) )
  1694. {
  1695. MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
  1696. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  1697. }
  1698. if( ( ret = mbedtls_pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
  1699. p, ssl->handshake->pmslen,
  1700. ssl->out_msg + offset + len_bytes, olen,
  1701. MBEDTLS_SSL_MAX_CONTENT_LEN - offset - len_bytes,
  1702. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  1703. {
  1704. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
  1705. return( ret );
  1706. }
  1707. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  1708. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1709. if( len_bytes == 2 )
  1710. {
  1711. ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
  1712. ssl->out_msg[offset+1] = (unsigned char)( *olen );
  1713. *olen += 2;
  1714. }
  1715. #endif
  1716. return( 0 );
  1717. }
  1718. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
  1719. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  1720. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1721. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1722. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1723. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1724. static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
  1725. unsigned char **p,
  1726. unsigned char *end,
  1727. mbedtls_md_type_t *md_alg,
  1728. mbedtls_pk_type_t *pk_alg )
  1729. {
  1730. ((void) ssl);
  1731. *md_alg = MBEDTLS_MD_NONE;
  1732. *pk_alg = MBEDTLS_PK_NONE;
  1733. /* Only in TLS 1.2 */
  1734. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  1735. {
  1736. return( 0 );
  1737. }
  1738. if( (*p) + 2 > end )
  1739. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1740. /*
  1741. * Get hash algorithm
  1742. */
  1743. if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) ) == MBEDTLS_MD_NONE )
  1744. {
  1745. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server used unsupported "
  1746. "HashAlgorithm %d", *(p)[0] ) );
  1747. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1748. }
  1749. /*
  1750. * Get signature algorithm
  1751. */
  1752. if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) ) == MBEDTLS_PK_NONE )
  1753. {
  1754. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used unsupported "
  1755. "SignatureAlgorithm %d", (*p)[1] ) );
  1756. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1757. }
  1758. /*
  1759. * Check if the hash is acceptable
  1760. */
  1761. if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 )
  1762. {
  1763. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used HashAlgorithm "
  1764. "that was not offered" ) );
  1765. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1766. }
  1767. MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
  1768. MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
  1769. *p += 2;
  1770. return( 0 );
  1771. }
  1772. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1773. MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1774. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  1775. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1776. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1777. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1778. static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
  1779. {
  1780. int ret;
  1781. const mbedtls_ecp_keypair *peer_key;
  1782. if( ssl->session_negotiate->peer_cert == NULL )
  1783. {
  1784. MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
  1785. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1786. }
  1787. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
  1788. MBEDTLS_PK_ECKEY ) )
  1789. {
  1790. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
  1791. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  1792. }
  1793. peer_key = mbedtls_pk_ec( ssl->session_negotiate->peer_cert->pk );
  1794. if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
  1795. MBEDTLS_ECDH_THEIRS ) ) != 0 )
  1796. {
  1797. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
  1798. return( ret );
  1799. }
  1800. if( ssl_check_server_ecdh_params( ssl ) != 0 )
  1801. {
  1802. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
  1803. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  1804. }
  1805. return( ret );
  1806. }
  1807. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
  1808. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1809. static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
  1810. {
  1811. int ret;
  1812. const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  1813. unsigned char *p, *end;
  1814. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
  1815. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  1816. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
  1817. {
  1818. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
  1819. ssl->state++;
  1820. return( 0 );
  1821. }
  1822. ((void) p);
  1823. ((void) end);
  1824. #endif
  1825. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1826. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1827. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  1828. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
  1829. {
  1830. if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
  1831. {
  1832. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
  1833. return( ret );
  1834. }
  1835. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
  1836. ssl->state++;
  1837. return( 0 );
  1838. }
  1839. ((void) p);
  1840. ((void) end);
  1841. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  1842. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1843. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  1844. {
  1845. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  1846. return( ret );
  1847. }
  1848. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  1849. {
  1850. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1851. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1852. }
  1853. /*
  1854. * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
  1855. * doesn't use a psk_identity_hint
  1856. */
  1857. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
  1858. {
  1859. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  1860. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  1861. {
  1862. ssl->record_read = 1;
  1863. goto exit;
  1864. }
  1865. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1866. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1867. }
  1868. p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  1869. end = ssl->in_msg + ssl->in_hslen;
  1870. MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
  1871. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1872. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  1873. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  1874. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  1875. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  1876. {
  1877. if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
  1878. {
  1879. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1880. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1881. }
  1882. } /* FALLTROUGH */
  1883. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  1884. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
  1885. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  1886. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  1887. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  1888. ; /* nothing more to do */
  1889. else
  1890. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
  1891. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  1892. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1893. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  1894. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
  1895. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  1896. {
  1897. if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
  1898. {
  1899. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1900. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1901. }
  1902. }
  1903. else
  1904. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1905. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  1906. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1907. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  1908. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1909. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  1910. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  1911. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
  1912. {
  1913. if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
  1914. {
  1915. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1916. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1917. }
  1918. }
  1919. else
  1920. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1921. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  1922. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  1923. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1924. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  1925. {
  1926. ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
  1927. p, end - p );
  1928. if( ret != 0 )
  1929. {
  1930. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
  1931. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1932. }
  1933. }
  1934. else
  1935. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1936. {
  1937. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1938. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  1939. }
  1940. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1941. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1942. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1943. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
  1944. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  1945. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
  1946. {
  1947. size_t sig_len, hashlen;
  1948. unsigned char hash[64];
  1949. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  1950. mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
  1951. unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  1952. size_t params_len = p - params;
  1953. /*
  1954. * Handle the digitally-signed structure
  1955. */
  1956. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1957. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  1958. {
  1959. if( ssl_parse_signature_algorithm( ssl, &p, end,
  1960. &md_alg, &pk_alg ) != 0 )
  1961. {
  1962. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1963. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1964. }
  1965. if( pk_alg != mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
  1966. {
  1967. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1968. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1969. }
  1970. }
  1971. else
  1972. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1973. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  1974. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  1975. if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
  1976. {
  1977. pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
  1978. /* Default hash for ECDSA is SHA-1 */
  1979. if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE )
  1980. md_alg = MBEDTLS_MD_SHA1;
  1981. }
  1982. else
  1983. #endif
  1984. {
  1985. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1986. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  1987. }
  1988. /*
  1989. * Read signature
  1990. */
  1991. sig_len = ( p[0] << 8 ) | p[1];
  1992. p += 2;
  1993. if( end != p + sig_len )
  1994. {
  1995. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1996. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1997. }
  1998. MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
  1999. /*
  2000. * Compute the hash that has been signed
  2001. */
  2002. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2003. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2004. if( md_alg == MBEDTLS_MD_NONE )
  2005. {
  2006. mbedtls_md5_context mbedtls_md5;
  2007. mbedtls_sha1_context mbedtls_sha1;
  2008. mbedtls_md5_init( &mbedtls_md5 );
  2009. mbedtls_sha1_init( &mbedtls_sha1 );
  2010. hashlen = 36;
  2011. /*
  2012. * digitally-signed struct {
  2013. * opaque md5_hash[16];
  2014. * opaque sha_hash[20];
  2015. * };
  2016. *
  2017. * md5_hash
  2018. * MD5(ClientHello.random + ServerHello.random
  2019. * + ServerParams);
  2020. * sha_hash
  2021. * SHA(ClientHello.random + ServerHello.random
  2022. * + ServerParams);
  2023. */
  2024. mbedtls_md5_starts( &mbedtls_md5 );
  2025. mbedtls_md5_update( &mbedtls_md5, ssl->handshake->randbytes, 64 );
  2026. mbedtls_md5_update( &mbedtls_md5, params, params_len );
  2027. mbedtls_md5_finish( &mbedtls_md5, hash );
  2028. mbedtls_sha1_starts( &mbedtls_sha1 );
  2029. mbedtls_sha1_update( &mbedtls_sha1, ssl->handshake->randbytes, 64 );
  2030. mbedtls_sha1_update( &mbedtls_sha1, params, params_len );
  2031. mbedtls_sha1_finish( &mbedtls_sha1, hash + 16 );
  2032. mbedtls_md5_free( &mbedtls_md5 );
  2033. mbedtls_sha1_free( &mbedtls_sha1 );
  2034. }
  2035. else
  2036. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  2037. MBEDTLS_SSL_PROTO_TLS1_1 */
  2038. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  2039. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2040. if( md_alg != MBEDTLS_MD_NONE )
  2041. {
  2042. mbedtls_md_context_t ctx;
  2043. mbedtls_md_init( &ctx );
  2044. /* Info from md_alg will be used instead */
  2045. hashlen = 0;
  2046. /*
  2047. * digitally-signed struct {
  2048. * opaque client_random[32];
  2049. * opaque server_random[32];
  2050. * ServerDHParams params;
  2051. * };
  2052. */
  2053. if( ( ret = mbedtls_md_setup( &ctx,
  2054. mbedtls_md_info_from_type( md_alg ), 0 ) ) != 0 )
  2055. {
  2056. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
  2057. return( ret );
  2058. }
  2059. mbedtls_md_starts( &ctx );
  2060. mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 );
  2061. mbedtls_md_update( &ctx, params, params_len );
  2062. mbedtls_md_finish( &ctx, hash );
  2063. mbedtls_md_free( &ctx );
  2064. }
  2065. else
  2066. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  2067. MBEDTLS_SSL_PROTO_TLS1_2 */
  2068. {
  2069. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2070. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2071. }
  2072. MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
  2073. (unsigned int) ( mbedtls_md_get_size( mbedtls_md_info_from_type( md_alg ) ) ) );
  2074. if( ssl->session_negotiate->peer_cert == NULL )
  2075. {
  2076. MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
  2077. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2078. }
  2079. /*
  2080. * Verify signature
  2081. */
  2082. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
  2083. {
  2084. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2085. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  2086. }
  2087. if( ( ret = mbedtls_pk_verify( &ssl->session_negotiate->peer_cert->pk,
  2088. md_alg, hash, hashlen, p, sig_len ) ) != 0 )
  2089. {
  2090. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
  2091. return( ret );
  2092. }
  2093. }
  2094. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  2095. MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2096. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  2097. exit:
  2098. ssl->state++;
  2099. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
  2100. return( 0 );
  2101. }
  2102. #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
  2103. !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
  2104. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
  2105. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
  2106. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
  2107. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  2108. static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
  2109. {
  2110. const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  2111. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
  2112. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2113. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  2114. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2115. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  2116. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2117. {
  2118. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
  2119. ssl->state++;
  2120. return( 0 );
  2121. }
  2122. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2123. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2124. }
  2125. #else
  2126. static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
  2127. {
  2128. int ret;
  2129. unsigned char *buf;
  2130. size_t n = 0;
  2131. size_t cert_type_len = 0, dn_len = 0;
  2132. const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  2133. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
  2134. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2135. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  2136. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2137. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  2138. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2139. {
  2140. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
  2141. ssl->state++;
  2142. return( 0 );
  2143. }
  2144. if( ssl->record_read == 0 )
  2145. {
  2146. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  2147. {
  2148. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2149. return( ret );
  2150. }
  2151. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2152. {
  2153. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2154. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2155. }
  2156. ssl->record_read = 1;
  2157. }
  2158. ssl->client_auth = 0;
  2159. ssl->state++;
  2160. if( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST )
  2161. ssl->client_auth++;
  2162. MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
  2163. ssl->client_auth ? "a" : "no" ) );
  2164. if( ssl->client_auth == 0 )
  2165. goto exit;
  2166. ssl->record_read = 0;
  2167. /*
  2168. * struct {
  2169. * ClientCertificateType certificate_types<1..2^8-1>;
  2170. * SignatureAndHashAlgorithm
  2171. * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
  2172. * DistinguishedName certificate_authorities<0..2^16-1>;
  2173. * } CertificateRequest;
  2174. *
  2175. * Since we only support a single certificate on clients, let's just
  2176. * ignore all the information that's supposed to help us pick a
  2177. * certificate.
  2178. *
  2179. * We could check that our certificate matches the request, and bail out
  2180. * if it doesn't, but it's simpler to just send the certificate anyway,
  2181. * and give the server the opportunity to decide if it should terminate
  2182. * the connection when it doesn't like our certificate.
  2183. *
  2184. * Same goes for the hash in TLS 1.2's signature_algorithms: at this
  2185. * point we only have one hash available (see comments in
  2186. * write_certificate_verify), so let's just use what we have.
  2187. *
  2188. * However, we still minimally parse the message to check it is at least
  2189. * superficially sane.
  2190. */
  2191. buf = ssl->in_msg;
  2192. /* certificate_types */
  2193. cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
  2194. n = cert_type_len;
  2195. if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
  2196. {
  2197. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2198. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  2199. }
  2200. /* supported_signature_algorithms */
  2201. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2202. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  2203. {
  2204. size_t sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
  2205. | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
  2206. #if defined(MBEDTLS_DEBUG_C)
  2207. unsigned char* sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
  2208. size_t i;
  2209. for( i = 0; i < sig_alg_len; i += 2 )
  2210. {
  2211. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Signature Algorithm found: %d,%d", sig_alg[i], sig_alg[i + 1] ) );
  2212. }
  2213. #endif
  2214. n += 2 + sig_alg_len;
  2215. if( ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
  2216. {
  2217. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2218. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  2219. }
  2220. }
  2221. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2222. /* certificate_authorities */
  2223. dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
  2224. | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
  2225. n += dn_len;
  2226. if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
  2227. {
  2228. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2229. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  2230. }
  2231. exit:
  2232. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
  2233. return( 0 );
  2234. }
  2235. #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
  2236. !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
  2237. !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
  2238. !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
  2239. !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
  2240. !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  2241. static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
  2242. {
  2243. int ret;
  2244. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
  2245. if( ssl->record_read == 0 )
  2246. {
  2247. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  2248. {
  2249. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2250. return( ret );
  2251. }
  2252. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2253. {
  2254. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
  2255. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2256. }
  2257. }
  2258. ssl->record_read = 0;
  2259. if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
  2260. ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
  2261. {
  2262. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
  2263. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
  2264. }
  2265. ssl->state++;
  2266. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2267. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  2268. mbedtls_ssl_recv_flight_completed( ssl );
  2269. #endif
  2270. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
  2271. return( 0 );
  2272. }
  2273. static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
  2274. {
  2275. int ret;
  2276. size_t i, n;
  2277. const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  2278. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
  2279. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  2280. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
  2281. {
  2282. /*
  2283. * DHM key exchange -- send G^X mod P
  2284. */
  2285. n = ssl->handshake->dhm_ctx.len;
  2286. ssl->out_msg[4] = (unsigned char)( n >> 8 );
  2287. ssl->out_msg[5] = (unsigned char)( n );
  2288. i = 6;
  2289. ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
  2290. (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
  2291. &ssl->out_msg[i], n,
  2292. ssl->conf->f_rng, ssl->conf->p_rng );
  2293. if( ret != 0 )
  2294. {
  2295. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
  2296. return( ret );
  2297. }
  2298. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
  2299. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
  2300. if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
  2301. ssl->handshake->premaster,
  2302. MBEDTLS_PREMASTER_SIZE,
  2303. &ssl->handshake->pmslen,
  2304. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  2305. {
  2306. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
  2307. return( ret );
  2308. }
  2309. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
  2310. }
  2311. else
  2312. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  2313. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2314. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  2315. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2316. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  2317. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  2318. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
  2319. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  2320. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
  2321. {
  2322. /*
  2323. * ECDH key exchange -- send client public value
  2324. */
  2325. i = 4;
  2326. ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
  2327. &n,
  2328. &ssl->out_msg[i], 1000,
  2329. ssl->conf->f_rng, ssl->conf->p_rng );
  2330. if( ret != 0 )
  2331. {
  2332. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
  2333. return( ret );
  2334. }
  2335. MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
  2336. if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
  2337. &ssl->handshake->pmslen,
  2338. ssl->handshake->premaster,
  2339. MBEDTLS_MPI_MAX_SIZE,
  2340. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  2341. {
  2342. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
  2343. return( ret );
  2344. }
  2345. MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
  2346. }
  2347. else
  2348. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2349. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  2350. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  2351. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  2352. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  2353. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2354. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  2355. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2356. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  2357. {
  2358. /*
  2359. * opaque psk_identity<0..2^16-1>;
  2360. */
  2361. if( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL )
  2362. {
  2363. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for PSK" ) );
  2364. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  2365. }
  2366. i = 4;
  2367. n = ssl->conf->psk_identity_len;
  2368. if( i + 2 + n > MBEDTLS_SSL_MAX_CONTENT_LEN )
  2369. {
  2370. MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity too long or "
  2371. "SSL buffer too short" ) );
  2372. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  2373. }
  2374. ssl->out_msg[i++] = (unsigned char)( n >> 8 );
  2375. ssl->out_msg[i++] = (unsigned char)( n );
  2376. memcpy( ssl->out_msg + i, ssl->conf->psk_identity, ssl->conf->psk_identity_len );
  2377. i += ssl->conf->psk_identity_len;
  2378. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  2379. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
  2380. {
  2381. n = 0;
  2382. }
  2383. else
  2384. #endif
  2385. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  2386. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  2387. {
  2388. if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
  2389. return( ret );
  2390. }
  2391. else
  2392. #endif
  2393. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  2394. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  2395. {
  2396. /*
  2397. * ClientDiffieHellmanPublic public (DHM send G^X mod P)
  2398. */
  2399. n = ssl->handshake->dhm_ctx.len;
  2400. if( i + 2 + n > MBEDTLS_SSL_MAX_CONTENT_LEN )
  2401. {
  2402. MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity or DHM size too long"
  2403. " or SSL buffer too short" ) );
  2404. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  2405. }
  2406. ssl->out_msg[i++] = (unsigned char)( n >> 8 );
  2407. ssl->out_msg[i++] = (unsigned char)( n );
  2408. ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
  2409. (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
  2410. &ssl->out_msg[i], n,
  2411. ssl->conf->f_rng, ssl->conf->p_rng );
  2412. if( ret != 0 )
  2413. {
  2414. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
  2415. return( ret );
  2416. }
  2417. }
  2418. else
  2419. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  2420. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  2421. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  2422. {
  2423. /*
  2424. * ClientECDiffieHellmanPublic public;
  2425. */
  2426. ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
  2427. &ssl->out_msg[i], MBEDTLS_SSL_MAX_CONTENT_LEN - i,
  2428. ssl->conf->f_rng, ssl->conf->p_rng );
  2429. if( ret != 0 )
  2430. {
  2431. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
  2432. return( ret );
  2433. }
  2434. MBEDTLS_SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
  2435. }
  2436. else
  2437. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  2438. {
  2439. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2440. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2441. }
  2442. if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
  2443. ciphersuite_info->key_exchange ) ) != 0 )
  2444. {
  2445. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
  2446. return( ret );
  2447. }
  2448. }
  2449. else
  2450. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  2451. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  2452. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
  2453. {
  2454. i = 4;
  2455. if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
  2456. return( ret );
  2457. }
  2458. else
  2459. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  2460. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2461. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2462. {
  2463. i = 4;
  2464. ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
  2465. ssl->out_msg + i, MBEDTLS_SSL_MAX_CONTENT_LEN - i, &n,
  2466. ssl->conf->f_rng, ssl->conf->p_rng );
  2467. if( ret != 0 )
  2468. {
  2469. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
  2470. return( ret );
  2471. }
  2472. ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
  2473. ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
  2474. ssl->conf->f_rng, ssl->conf->p_rng );
  2475. if( ret != 0 )
  2476. {
  2477. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
  2478. return( ret );
  2479. }
  2480. }
  2481. else
  2482. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  2483. {
  2484. ((void) ciphersuite_info);
  2485. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2486. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2487. }
  2488. ssl->out_msglen = i + n;
  2489. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2490. ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
  2491. ssl->state++;
  2492. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  2493. {
  2494. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  2495. return( ret );
  2496. }
  2497. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
  2498. return( 0 );
  2499. }
  2500. #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
  2501. !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
  2502. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
  2503. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
  2504. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
  2505. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  2506. static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
  2507. {
  2508. const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  2509. int ret;
  2510. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
  2511. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  2512. {
  2513. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  2514. return( ret );
  2515. }
  2516. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2517. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  2518. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  2519. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2520. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2521. {
  2522. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  2523. ssl->state++;
  2524. return( 0 );
  2525. }
  2526. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2527. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2528. }
  2529. #else
  2530. static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
  2531. {
  2532. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2533. const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  2534. size_t n = 0, offset = 0;
  2535. unsigned char hash[48];
  2536. unsigned char *hash_start = hash;
  2537. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  2538. unsigned int hashlen;
  2539. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
  2540. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  2541. {
  2542. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  2543. return( ret );
  2544. }
  2545. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2546. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  2547. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  2548. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2549. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2550. {
  2551. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  2552. ssl->state++;
  2553. return( 0 );
  2554. }
  2555. if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL )
  2556. {
  2557. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  2558. ssl->state++;
  2559. return( 0 );
  2560. }
  2561. if( mbedtls_ssl_own_key( ssl ) == NULL )
  2562. {
  2563. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
  2564. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  2565. }
  2566. /*
  2567. * Make an RSA signature of the handshake digests
  2568. */
  2569. ssl->handshake->calc_verify( ssl, hash );
  2570. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2571. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2572. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  2573. {
  2574. /*
  2575. * digitally-signed struct {
  2576. * opaque md5_hash[16];
  2577. * opaque sha_hash[20];
  2578. * };
  2579. *
  2580. * md5_hash
  2581. * MD5(handshake_messages);
  2582. *
  2583. * sha_hash
  2584. * SHA(handshake_messages);
  2585. */
  2586. hashlen = 36;
  2587. md_alg = MBEDTLS_MD_NONE;
  2588. /*
  2589. * For ECDSA, default hash is SHA-1 only
  2590. */
  2591. if( mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECDSA ) )
  2592. {
  2593. hash_start += 16;
  2594. hashlen -= 16;
  2595. md_alg = MBEDTLS_MD_SHA1;
  2596. }
  2597. }
  2598. else
  2599. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  2600. MBEDTLS_SSL_PROTO_TLS1_1 */
  2601. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2602. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  2603. {
  2604. /*
  2605. * digitally-signed struct {
  2606. * opaque handshake_messages[handshake_messages_length];
  2607. * };
  2608. *
  2609. * Taking shortcut here. We assume that the server always allows the
  2610. * PRF Hash function and has sent it in the allowed signature
  2611. * algorithms list received in the Certificate Request message.
  2612. *
  2613. * Until we encounter a server that does not, we will take this
  2614. * shortcut.
  2615. *
  2616. * Reason: Otherwise we should have running hashes for SHA512 and SHA224
  2617. * in order to satisfy 'weird' needs from the server side.
  2618. */
  2619. if( ssl->transform_negotiate->ciphersuite_info->mac ==
  2620. MBEDTLS_MD_SHA384 )
  2621. {
  2622. md_alg = MBEDTLS_MD_SHA384;
  2623. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
  2624. }
  2625. else
  2626. {
  2627. md_alg = MBEDTLS_MD_SHA256;
  2628. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
  2629. }
  2630. ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
  2631. /* Info from md_alg will be used instead */
  2632. hashlen = 0;
  2633. offset = 2;
  2634. }
  2635. else
  2636. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2637. {
  2638. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2639. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2640. }
  2641. if( ( ret = mbedtls_pk_sign( mbedtls_ssl_own_key( ssl ), md_alg, hash_start, hashlen,
  2642. ssl->out_msg + 6 + offset, &n,
  2643. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  2644. {
  2645. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
  2646. return( ret );
  2647. }
  2648. ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
  2649. ssl->out_msg[5 + offset] = (unsigned char)( n );
  2650. ssl->out_msglen = 6 + n + offset;
  2651. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2652. ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
  2653. ssl->state++;
  2654. if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 )
  2655. {
  2656. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
  2657. return( ret );
  2658. }
  2659. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
  2660. return( ret );
  2661. }
  2662. #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
  2663. !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
  2664. !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
  2665. !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
  2666. !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
  2667. !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  2668. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2669. static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
  2670. {
  2671. int ret;
  2672. uint32_t lifetime;
  2673. size_t ticket_len;
  2674. unsigned char *ticket;
  2675. const unsigned char *msg;
  2676. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
  2677. if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 )
  2678. {
  2679. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2680. return( ret );
  2681. }
  2682. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2683. {
  2684. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  2685. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2686. }
  2687. /*
  2688. * struct {
  2689. * uint32 ticket_lifetime_hint;
  2690. * opaque ticket<0..2^16-1>;
  2691. * } NewSessionTicket;
  2692. *
  2693. * 0 . 3 ticket_lifetime_hint
  2694. * 4 . 5 ticket_len (n)
  2695. * 6 . 5+n ticket content
  2696. */
  2697. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
  2698. ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
  2699. {
  2700. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  2701. return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
  2702. }
  2703. msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  2704. lifetime = ( msg[0] << 24 ) | ( msg[1] << 16 ) |
  2705. ( msg[2] << 8 ) | ( msg[3] );
  2706. ticket_len = ( msg[4] << 8 ) | ( msg[5] );
  2707. if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
  2708. {
  2709. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  2710. return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
  2711. }
  2712. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
  2713. /* We're not waiting for a NewSessionTicket message any more */
  2714. ssl->handshake->new_session_ticket = 0;
  2715. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  2716. /*
  2717. * Zero-length ticket means the server changed his mind and doesn't want
  2718. * to send a ticket after all, so just forget it
  2719. */
  2720. if( ticket_len == 0 )
  2721. return( 0 );
  2722. mbedtls_zeroize( ssl->session_negotiate->ticket,
  2723. ssl->session_negotiate->ticket_len );
  2724. mbedtls_free( ssl->session_negotiate->ticket );
  2725. ssl->session_negotiate->ticket = NULL;
  2726. ssl->session_negotiate->ticket_len = 0;
  2727. if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
  2728. {
  2729. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
  2730. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  2731. }
  2732. memcpy( ticket, msg + 6, ticket_len );
  2733. ssl->session_negotiate->ticket = ticket;
  2734. ssl->session_negotiate->ticket_len = ticket_len;
  2735. ssl->session_negotiate->ticket_lifetime = lifetime;
  2736. /*
  2737. * RFC 5077 section 3.4:
  2738. * "If the client receives a session ticket from the server, then it
  2739. * discards any Session ID that was sent in the ServerHello."
  2740. */
  2741. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
  2742. ssl->session_negotiate->id_len = 0;
  2743. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
  2744. return( 0 );
  2745. }
  2746. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  2747. /*
  2748. * SSL handshake -- client side -- single step
  2749. */
  2750. int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
  2751. {
  2752. int ret = 0;
  2753. if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
  2754. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  2755. MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
  2756. if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
  2757. return( ret );
  2758. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2759. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  2760. ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
  2761. {
  2762. if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
  2763. return( ret );
  2764. }
  2765. #endif
  2766. /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
  2767. * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
  2768. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2769. if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
  2770. ssl->handshake->new_session_ticket != 0 )
  2771. {
  2772. ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
  2773. }
  2774. #endif
  2775. switch( ssl->state )
  2776. {
  2777. case MBEDTLS_SSL_HELLO_REQUEST:
  2778. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  2779. break;
  2780. /*
  2781. * ==> ClientHello
  2782. */
  2783. case MBEDTLS_SSL_CLIENT_HELLO:
  2784. ret = ssl_write_client_hello( ssl );
  2785. break;
  2786. /*
  2787. * <== ServerHello
  2788. * Certificate
  2789. * ( ServerKeyExchange )
  2790. * ( CertificateRequest )
  2791. * ServerHelloDone
  2792. */
  2793. case MBEDTLS_SSL_SERVER_HELLO:
  2794. ret = ssl_parse_server_hello( ssl );
  2795. break;
  2796. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  2797. ret = mbedtls_ssl_parse_certificate( ssl );
  2798. break;
  2799. case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
  2800. ret = ssl_parse_server_key_exchange( ssl );
  2801. break;
  2802. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  2803. ret = ssl_parse_certificate_request( ssl );
  2804. break;
  2805. case MBEDTLS_SSL_SERVER_HELLO_DONE:
  2806. ret = ssl_parse_server_hello_done( ssl );
  2807. break;
  2808. /*
  2809. * ==> ( Certificate/Alert )
  2810. * ClientKeyExchange
  2811. * ( CertificateVerify )
  2812. * ChangeCipherSpec
  2813. * Finished
  2814. */
  2815. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  2816. ret = mbedtls_ssl_write_certificate( ssl );
  2817. break;
  2818. case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
  2819. ret = ssl_write_client_key_exchange( ssl );
  2820. break;
  2821. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  2822. ret = ssl_write_certificate_verify( ssl );
  2823. break;
  2824. case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
  2825. ret = mbedtls_ssl_write_change_cipher_spec( ssl );
  2826. break;
  2827. case MBEDTLS_SSL_CLIENT_FINISHED:
  2828. ret = mbedtls_ssl_write_finished( ssl );
  2829. break;
  2830. /*
  2831. * <== ( NewSessionTicket )
  2832. * ChangeCipherSpec
  2833. * Finished
  2834. */
  2835. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2836. case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
  2837. ret = ssl_parse_new_session_ticket( ssl );
  2838. break;
  2839. #endif
  2840. case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
  2841. ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
  2842. break;
  2843. case MBEDTLS_SSL_SERVER_FINISHED:
  2844. ret = mbedtls_ssl_parse_finished( ssl );
  2845. break;
  2846. case MBEDTLS_SSL_FLUSH_BUFFERS:
  2847. MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
  2848. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  2849. break;
  2850. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  2851. mbedtls_ssl_handshake_wrapup( ssl );
  2852. break;
  2853. default:
  2854. MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
  2855. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  2856. }
  2857. return( ret );
  2858. }
  2859. #endif /* MBEDTLS_SSL_CLI_C */