ssl_cli.c 115 KB

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