ssl_cli.c 109 KB

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