ssl_cli.c 121 KB

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