2
0

ssl_cli.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648
  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. /* Check that there is enough room for:
  1173. * - 2 bytes of version
  1174. * - 1 byte of cookie_len
  1175. */
  1176. if( mbedtls_ssl_hs_hdr_len( ssl ) + 3 > ssl->in_msglen )
  1177. {
  1178. MBEDTLS_SSL_DEBUG_MSG( 1,
  1179. ( "incoming HelloVerifyRequest message is too short" ) );
  1180. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1181. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1182. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1183. }
  1184. /*
  1185. * struct {
  1186. * ProtocolVersion server_version;
  1187. * opaque cookie<0..2^8-1>;
  1188. * } HelloVerifyRequest;
  1189. */
  1190. MBEDTLS_SSL_DEBUG_BUF( 3, "server version", p, 2 );
  1191. mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, p );
  1192. p += 2;
  1193. /*
  1194. * Since the RFC is not clear on this point, accept DTLS 1.0 (TLS 1.1)
  1195. * even is lower than our min version.
  1196. */
  1197. if( major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
  1198. minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ||
  1199. major_ver > ssl->conf->max_major_ver ||
  1200. minor_ver > ssl->conf->max_minor_ver )
  1201. {
  1202. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server version" ) );
  1203. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1204. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  1205. return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  1206. }
  1207. cookie_len = *p++;
  1208. if( ( ssl->in_msg + ssl->in_msglen ) - p < cookie_len )
  1209. {
  1210. MBEDTLS_SSL_DEBUG_MSG( 1,
  1211. ( "cookie length does not match incoming message size" ) );
  1212. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1213. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1214. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1215. }
  1216. MBEDTLS_SSL_DEBUG_BUF( 3, "cookie", p, cookie_len );
  1217. mbedtls_free( ssl->handshake->verify_cookie );
  1218. ssl->handshake->verify_cookie = mbedtls_calloc( 1, cookie_len );
  1219. if( ssl->handshake->verify_cookie == NULL )
  1220. {
  1221. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", cookie_len ) );
  1222. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  1223. }
  1224. memcpy( ssl->handshake->verify_cookie, p, cookie_len );
  1225. ssl->handshake->verify_cookie_len = cookie_len;
  1226. /* Start over at ClientHello */
  1227. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  1228. mbedtls_ssl_reset_checksum( ssl );
  1229. mbedtls_ssl_recv_flight_completed( ssl );
  1230. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse hello verify request" ) );
  1231. return( 0 );
  1232. }
  1233. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1234. static int ssl_parse_server_hello( mbedtls_ssl_context *ssl )
  1235. {
  1236. int ret, i;
  1237. size_t n;
  1238. size_t ext_len;
  1239. unsigned char *buf, *ext;
  1240. unsigned char comp;
  1241. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1242. int accept_comp;
  1243. #endif
  1244. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1245. int renegotiation_info_seen = 0;
  1246. #endif
  1247. int handshake_failure = 0;
  1248. const mbedtls_ssl_ciphersuite_t *suite_info;
  1249. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
  1250. buf = ssl->in_msg;
  1251. if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
  1252. {
  1253. /* No alert on a read error. */
  1254. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  1255. return( ret );
  1256. }
  1257. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  1258. {
  1259. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1260. if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
  1261. {
  1262. ssl->renego_records_seen++;
  1263. if( ssl->conf->renego_max_records >= 0 &&
  1264. ssl->renego_records_seen > ssl->conf->renego_max_records )
  1265. {
  1266. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
  1267. "but not honored by server" ) );
  1268. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1269. }
  1270. MBEDTLS_SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
  1271. ssl->keep_current_message = 1;
  1272. return( MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
  1273. }
  1274. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1275. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1276. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1277. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  1278. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1279. }
  1280. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1281. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1282. {
  1283. if( buf[0] == MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
  1284. {
  1285. MBEDTLS_SSL_DEBUG_MSG( 2, ( "received hello verify request" ) );
  1286. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
  1287. return( ssl_parse_hello_verify_request( ssl ) );
  1288. }
  1289. else
  1290. {
  1291. /* We made it through the verification process */
  1292. mbedtls_free( ssl->handshake->verify_cookie );
  1293. ssl->handshake->verify_cookie = NULL;
  1294. ssl->handshake->verify_cookie_len = 0;
  1295. }
  1296. }
  1297. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1298. if( ssl->in_hslen < 38 + mbedtls_ssl_hs_hdr_len( ssl ) ||
  1299. buf[0] != MBEDTLS_SSL_HS_SERVER_HELLO )
  1300. {
  1301. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1302. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1303. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1304. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1305. }
  1306. /*
  1307. * 0 . 1 server_version
  1308. * 2 . 33 random (maybe including 4 bytes of Unix time)
  1309. * 34 . 34 session_id length = n
  1310. * 35 . 34+n session_id
  1311. * 35+n . 36+n cipher_suite
  1312. * 37+n . 37+n compression_method
  1313. *
  1314. * 38+n . 39+n extensions length (optional)
  1315. * 40+n . .. extensions
  1316. */
  1317. buf += mbedtls_ssl_hs_hdr_len( ssl );
  1318. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", buf + 0, 2 );
  1319. mbedtls_ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
  1320. ssl->conf->transport, buf + 0 );
  1321. if( ssl->major_ver < ssl->conf->min_major_ver ||
  1322. ssl->minor_ver < ssl->conf->min_minor_ver ||
  1323. ssl->major_ver > ssl->conf->max_major_ver ||
  1324. ssl->minor_ver > ssl->conf->max_minor_ver )
  1325. {
  1326. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server version out of bounds - "
  1327. " min: [%d:%d], server: [%d:%d], max: [%d:%d]",
  1328. ssl->conf->min_major_ver, ssl->conf->min_minor_ver,
  1329. ssl->major_ver, ssl->minor_ver,
  1330. ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) );
  1331. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1332. MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION );
  1333. return( MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  1334. }
  1335. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu",
  1336. ( (uint32_t) buf[2] << 24 ) |
  1337. ( (uint32_t) buf[3] << 16 ) |
  1338. ( (uint32_t) buf[4] << 8 ) |
  1339. ( (uint32_t) buf[5] ) ) );
  1340. memcpy( ssl->handshake->randbytes + 32, buf + 2, 32 );
  1341. n = buf[34];
  1342. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 2, 32 );
  1343. if( n > 32 )
  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. if( ssl->in_hslen > mbedtls_ssl_hs_hdr_len( ssl ) + 39 + n )
  1351. {
  1352. ext_len = ( ( buf[38 + n] << 8 )
  1353. | ( buf[39 + n] ) );
  1354. if( ( ext_len > 0 && ext_len < 4 ) ||
  1355. ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 40 + n + ext_len )
  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. }
  1363. else if( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) + 38 + n )
  1364. {
  1365. ext_len = 0;
  1366. }
  1367. else
  1368. {
  1369. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1370. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1371. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1372. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1373. }
  1374. /* ciphersuite (used later) */
  1375. i = ( buf[35 + n] << 8 ) | buf[36 + n];
  1376. /*
  1377. * Read and check compression
  1378. */
  1379. comp = buf[37 + n];
  1380. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1381. /* See comments in ssl_write_client_hello() */
  1382. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1383. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1384. accept_comp = 0;
  1385. else
  1386. #endif
  1387. accept_comp = 1;
  1388. if( comp != MBEDTLS_SSL_COMPRESS_NULL &&
  1389. ( comp != MBEDTLS_SSL_COMPRESS_DEFLATE || accept_comp == 0 ) )
  1390. #else /* MBEDTLS_ZLIB_SUPPORT */
  1391. if( comp != MBEDTLS_SSL_COMPRESS_NULL )
  1392. #endif/* MBEDTLS_ZLIB_SUPPORT */
  1393. {
  1394. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server hello, bad compression: %d", comp ) );
  1395. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1396. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1397. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  1398. }
  1399. /*
  1400. * Initialize update checksum functions
  1401. */
  1402. ssl->transform_negotiate->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( i );
  1403. if( ssl->transform_negotiate->ciphersuite_info == NULL )
  1404. {
  1405. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
  1406. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1407. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  1408. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  1409. }
  1410. mbedtls_ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
  1411. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
  1412. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, session id", buf + 35, n );
  1413. /*
  1414. * Check if the session can be resumed
  1415. */
  1416. if( ssl->handshake->resume == 0 || n == 0 ||
  1417. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1418. ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
  1419. #endif
  1420. ssl->session_negotiate->ciphersuite != i ||
  1421. ssl->session_negotiate->compression != comp ||
  1422. ssl->session_negotiate->id_len != n ||
  1423. memcmp( ssl->session_negotiate->id, buf + 35, n ) != 0 )
  1424. {
  1425. ssl->state++;
  1426. ssl->handshake->resume = 0;
  1427. #if defined(MBEDTLS_HAVE_TIME)
  1428. ssl->session_negotiate->start = mbedtls_time( NULL );
  1429. #endif
  1430. ssl->session_negotiate->ciphersuite = i;
  1431. ssl->session_negotiate->compression = comp;
  1432. ssl->session_negotiate->id_len = n;
  1433. memcpy( ssl->session_negotiate->id, buf + 35, n );
  1434. }
  1435. else
  1436. {
  1437. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  1438. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  1439. {
  1440. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  1441. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1442. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  1443. return( ret );
  1444. }
  1445. }
  1446. MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
  1447. ssl->handshake->resume ? "a" : "no" ) );
  1448. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %04x", i ) );
  1449. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[37 + n] ) );
  1450. /*
  1451. * Perform cipher suite validation in same way as in ssl_write_client_hello.
  1452. */
  1453. i = 0;
  1454. while( 1 )
  1455. {
  1456. if( ssl->conf->ciphersuite_list[ssl->minor_ver][i] == 0 )
  1457. {
  1458. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1459. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1460. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1461. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1462. }
  1463. if( ssl->conf->ciphersuite_list[ssl->minor_ver][i++] ==
  1464. ssl->session_negotiate->ciphersuite )
  1465. {
  1466. break;
  1467. }
  1468. }
  1469. suite_info = mbedtls_ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
  1470. if( ssl_validate_ciphersuite( suite_info, ssl, ssl->minor_ver, ssl->minor_ver ) != 0 )
  1471. {
  1472. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1473. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1474. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1475. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1476. }
  1477. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", suite_info->name ) );
  1478. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  1479. if( suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA &&
  1480. ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  1481. {
  1482. ssl->handshake->ecrs_enabled = 1;
  1483. }
  1484. #endif
  1485. if( comp != MBEDTLS_SSL_COMPRESS_NULL
  1486. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1487. && comp != MBEDTLS_SSL_COMPRESS_DEFLATE
  1488. #endif
  1489. )
  1490. {
  1491. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1492. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1493. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  1494. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1495. }
  1496. ssl->session_negotiate->compression = comp;
  1497. ext = buf + 40 + n;
  1498. MBEDTLS_SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
  1499. while( ext_len )
  1500. {
  1501. unsigned int ext_id = ( ( ext[0] << 8 )
  1502. | ( ext[1] ) );
  1503. unsigned int ext_size = ( ( ext[2] << 8 )
  1504. | ( ext[3] ) );
  1505. if( ext_size + 4 > ext_len )
  1506. {
  1507. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1508. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1509. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  1510. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1511. }
  1512. switch( ext_id )
  1513. {
  1514. case MBEDTLS_TLS_EXT_RENEGOTIATION_INFO:
  1515. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
  1516. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1517. renegotiation_info_seen = 1;
  1518. #endif
  1519. if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
  1520. ext_size ) ) != 0 )
  1521. return( ret );
  1522. break;
  1523. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1524. case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
  1525. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
  1526. if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
  1527. ext + 4, ext_size ) ) != 0 )
  1528. {
  1529. return( ret );
  1530. }
  1531. break;
  1532. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  1533. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1534. case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
  1535. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
  1536. if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
  1537. ext + 4, ext_size ) ) != 0 )
  1538. {
  1539. return( ret );
  1540. }
  1541. break;
  1542. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  1543. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1544. case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
  1545. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
  1546. if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
  1547. ext + 4, ext_size ) ) != 0 )
  1548. {
  1549. return( ret );
  1550. }
  1551. break;
  1552. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  1553. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1554. case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
  1555. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
  1556. if( ( ret = ssl_parse_extended_ms_ext( ssl,
  1557. ext + 4, ext_size ) ) != 0 )
  1558. {
  1559. return( ret );
  1560. }
  1561. break;
  1562. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  1563. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1564. case MBEDTLS_TLS_EXT_SESSION_TICKET:
  1565. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
  1566. if( ( ret = ssl_parse_session_ticket_ext( ssl,
  1567. ext + 4, ext_size ) ) != 0 )
  1568. {
  1569. return( ret );
  1570. }
  1571. break;
  1572. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  1573. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  1574. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1575. case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
  1576. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
  1577. if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
  1578. ext + 4, ext_size ) ) != 0 )
  1579. {
  1580. return( ret );
  1581. }
  1582. break;
  1583. #endif /* MBEDTLS_ECDH_C || MBEDTLS_ECDSA_C ||
  1584. MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1585. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1586. case MBEDTLS_TLS_EXT_ECJPAKE_KKPP:
  1587. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found ecjpake_kkpp extension" ) );
  1588. if( ( ret = ssl_parse_ecjpake_kkpp( ssl,
  1589. ext + 4, ext_size ) ) != 0 )
  1590. {
  1591. return( ret );
  1592. }
  1593. break;
  1594. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  1595. #if defined(MBEDTLS_SSL_ALPN)
  1596. case MBEDTLS_TLS_EXT_ALPN:
  1597. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
  1598. if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
  1599. return( ret );
  1600. break;
  1601. #endif /* MBEDTLS_SSL_ALPN */
  1602. default:
  1603. MBEDTLS_SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
  1604. ext_id ) );
  1605. }
  1606. ext_len -= 4 + ext_size;
  1607. ext += 4 + ext_size;
  1608. if( ext_len > 0 && ext_len < 4 )
  1609. {
  1610. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  1611. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1612. }
  1613. }
  1614. /*
  1615. * Renegotiation security checks
  1616. */
  1617. if( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1618. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE )
  1619. {
  1620. MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
  1621. handshake_failure = 1;
  1622. }
  1623. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1624. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1625. ssl->secure_renegotiation == MBEDTLS_SSL_SECURE_RENEGOTIATION &&
  1626. renegotiation_info_seen == 0 )
  1627. {
  1628. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
  1629. handshake_failure = 1;
  1630. }
  1631. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1632. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1633. ssl->conf->allow_legacy_renegotiation == MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION )
  1634. {
  1635. MBEDTLS_SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
  1636. handshake_failure = 1;
  1637. }
  1638. else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
  1639. ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
  1640. renegotiation_info_seen == 1 )
  1641. {
  1642. MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
  1643. handshake_failure = 1;
  1644. }
  1645. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1646. if( handshake_failure == 1 )
  1647. {
  1648. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1649. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1650. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO );
  1651. }
  1652. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
  1653. return( 0 );
  1654. }
  1655. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1656. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  1657. static int ssl_parse_server_dh_params( mbedtls_ssl_context *ssl, unsigned char **p,
  1658. unsigned char *end )
  1659. {
  1660. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1661. /*
  1662. * Ephemeral DH parameters:
  1663. *
  1664. * struct {
  1665. * opaque dh_p<1..2^16-1>;
  1666. * opaque dh_g<1..2^16-1>;
  1667. * opaque dh_Ys<1..2^16-1>;
  1668. * } ServerDHParams;
  1669. */
  1670. if( ( ret = mbedtls_dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
  1671. {
  1672. MBEDTLS_SSL_DEBUG_RET( 2, ( "mbedtls_dhm_read_params" ), ret );
  1673. return( ret );
  1674. }
  1675. if( ssl->handshake->dhm_ctx.len * 8 < ssl->conf->dhm_min_bitlen )
  1676. {
  1677. MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHM prime too short: %d < %d",
  1678. ssl->handshake->dhm_ctx.len * 8,
  1679. ssl->conf->dhm_min_bitlen ) );
  1680. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1681. }
  1682. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
  1683. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
  1684. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
  1685. return( ret );
  1686. }
  1687. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1688. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  1689. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1690. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  1691. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  1692. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1693. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1694. static int ssl_check_server_ecdh_params( const mbedtls_ssl_context *ssl )
  1695. {
  1696. const mbedtls_ecp_curve_info *curve_info;
  1697. mbedtls_ecp_group_id grp_id;
  1698. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  1699. grp_id = ssl->handshake->ecdh_ctx.grp.id;
  1700. #else
  1701. grp_id = ssl->handshake->ecdh_ctx.grp_id;
  1702. #endif
  1703. curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
  1704. if( curve_info == NULL )
  1705. {
  1706. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1707. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  1708. }
  1709. MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
  1710. #if defined(MBEDTLS_ECP_C)
  1711. if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
  1712. #else
  1713. if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
  1714. ssl->handshake->ecdh_ctx.grp.nbits > 521 )
  1715. #endif
  1716. return( -1 );
  1717. MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
  1718. MBEDTLS_DEBUG_ECDH_QP );
  1719. return( 0 );
  1720. }
  1721. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1722. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  1723. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  1724. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  1725. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1726. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1727. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  1728. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  1729. static int ssl_parse_server_ecdh_params( mbedtls_ssl_context *ssl,
  1730. unsigned char **p,
  1731. unsigned char *end )
  1732. {
  1733. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1734. /*
  1735. * Ephemeral ECDH parameters:
  1736. *
  1737. * struct {
  1738. * ECParameters curve_params;
  1739. * ECPoint public;
  1740. * } ServerECDHParams;
  1741. */
  1742. if( ( ret = mbedtls_ecdh_read_params( &ssl->handshake->ecdh_ctx,
  1743. (const unsigned char **) p, end ) ) != 0 )
  1744. {
  1745. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_read_params" ), ret );
  1746. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  1747. if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  1748. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  1749. #endif
  1750. return( ret );
  1751. }
  1752. if( ssl_check_server_ecdh_params( ssl ) != 0 )
  1753. {
  1754. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
  1755. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1756. }
  1757. return( ret );
  1758. }
  1759. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1760. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  1761. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  1762. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1763. static int ssl_parse_server_psk_hint( mbedtls_ssl_context *ssl,
  1764. unsigned char **p,
  1765. unsigned char *end )
  1766. {
  1767. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  1768. size_t len;
  1769. ((void) ssl);
  1770. /*
  1771. * PSK parameters:
  1772. *
  1773. * opaque psk_identity_hint<0..2^16-1>;
  1774. */
  1775. if( end - (*p) < 2 )
  1776. {
  1777. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message "
  1778. "(psk_identity_hint length)" ) );
  1779. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1780. }
  1781. len = (*p)[0] << 8 | (*p)[1];
  1782. *p += 2;
  1783. if( end - (*p) < (int) len )
  1784. {
  1785. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message "
  1786. "(psk_identity_hint length)" ) );
  1787. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1788. }
  1789. /*
  1790. * Note: we currently ignore the PKS identity hint, as we only allow one
  1791. * PSK to be provisionned on the client. This could be changed later if
  1792. * someone needs that feature.
  1793. */
  1794. *p += len;
  1795. ret = 0;
  1796. return( ret );
  1797. }
  1798. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  1799. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) || \
  1800. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  1801. /*
  1802. * Generate a pre-master secret and encrypt it with the server's RSA key
  1803. */
  1804. static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl,
  1805. size_t offset, size_t *olen,
  1806. size_t pms_offset )
  1807. {
  1808. int ret;
  1809. size_t len_bytes = ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ? 0 : 2;
  1810. unsigned char *p = ssl->handshake->premaster + pms_offset;
  1811. if( offset + len_bytes > MBEDTLS_SSL_OUT_CONTENT_LEN )
  1812. {
  1813. MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
  1814. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  1815. }
  1816. /*
  1817. * Generate (part of) the pre-master as
  1818. * struct {
  1819. * ProtocolVersion client_version;
  1820. * opaque random[46];
  1821. * } PreMasterSecret;
  1822. */
  1823. mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver,
  1824. ssl->conf->transport, p );
  1825. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, p + 2, 46 ) ) != 0 )
  1826. {
  1827. MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
  1828. return( ret );
  1829. }
  1830. ssl->handshake->pmslen = 48;
  1831. if( ssl->session_negotiate->peer_cert == NULL )
  1832. {
  1833. MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
  1834. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1835. }
  1836. /*
  1837. * Now write it out, encrypted
  1838. */
  1839. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
  1840. MBEDTLS_PK_RSA ) )
  1841. {
  1842. MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
  1843. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  1844. }
  1845. if( ( ret = mbedtls_pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
  1846. p, ssl->handshake->pmslen,
  1847. ssl->out_msg + offset + len_bytes, olen,
  1848. MBEDTLS_SSL_OUT_CONTENT_LEN - offset - len_bytes,
  1849. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  1850. {
  1851. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_rsa_pkcs1_encrypt", ret );
  1852. return( ret );
  1853. }
  1854. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  1855. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1856. if( len_bytes == 2 )
  1857. {
  1858. ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
  1859. ssl->out_msg[offset+1] = (unsigned char)( *olen );
  1860. *olen += 2;
  1861. }
  1862. #endif
  1863. return( 0 );
  1864. }
  1865. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED ||
  1866. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  1867. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  1868. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1869. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1870. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1871. static int ssl_parse_signature_algorithm( mbedtls_ssl_context *ssl,
  1872. unsigned char **p,
  1873. unsigned char *end,
  1874. mbedtls_md_type_t *md_alg,
  1875. mbedtls_pk_type_t *pk_alg )
  1876. {
  1877. ((void) ssl);
  1878. *md_alg = MBEDTLS_MD_NONE;
  1879. *pk_alg = MBEDTLS_PK_NONE;
  1880. /* Only in TLS 1.2 */
  1881. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  1882. {
  1883. return( 0 );
  1884. }
  1885. if( (*p) + 2 > end )
  1886. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1887. /*
  1888. * Get hash algorithm
  1889. */
  1890. if( ( *md_alg = mbedtls_ssl_md_alg_from_hash( (*p)[0] ) ) == MBEDTLS_MD_NONE )
  1891. {
  1892. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Server used unsupported "
  1893. "HashAlgorithm %d", *(p)[0] ) );
  1894. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1895. }
  1896. /*
  1897. * Get signature algorithm
  1898. */
  1899. if( ( *pk_alg = mbedtls_ssl_pk_alg_from_sig( (*p)[1] ) ) == MBEDTLS_PK_NONE )
  1900. {
  1901. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used unsupported "
  1902. "SignatureAlgorithm %d", (*p)[1] ) );
  1903. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1904. }
  1905. /*
  1906. * Check if the hash is acceptable
  1907. */
  1908. if( mbedtls_ssl_check_sig_hash( ssl, *md_alg ) != 0 )
  1909. {
  1910. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server used HashAlgorithm %d that was not offered",
  1911. *(p)[0] ) );
  1912. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1913. }
  1914. MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
  1915. MBEDTLS_SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
  1916. *p += 2;
  1917. return( 0 );
  1918. }
  1919. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1920. MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1921. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  1922. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  1923. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1924. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1925. static int ssl_get_ecdh_params_from_cert( mbedtls_ssl_context *ssl )
  1926. {
  1927. int ret;
  1928. const mbedtls_ecp_keypair *peer_key;
  1929. if( ssl->session_negotiate->peer_cert == NULL )
  1930. {
  1931. MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
  1932. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  1933. }
  1934. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk,
  1935. MBEDTLS_PK_ECKEY ) )
  1936. {
  1937. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
  1938. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  1939. }
  1940. peer_key = mbedtls_pk_ec( ssl->session_negotiate->peer_cert->pk );
  1941. if( ( ret = mbedtls_ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
  1942. MBEDTLS_ECDH_THEIRS ) ) != 0 )
  1943. {
  1944. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_get_params" ), ret );
  1945. return( ret );
  1946. }
  1947. if( ssl_check_server_ecdh_params( ssl ) != 0 )
  1948. {
  1949. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
  1950. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
  1951. }
  1952. return( ret );
  1953. }
  1954. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
  1955. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1956. static int ssl_parse_server_key_exchange( mbedtls_ssl_context *ssl )
  1957. {
  1958. int ret;
  1959. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  1960. ssl->transform_negotiate->ciphersuite_info;
  1961. unsigned char *p = NULL, *end = NULL;
  1962. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
  1963. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  1964. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
  1965. {
  1966. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
  1967. ssl->state++;
  1968. return( 0 );
  1969. }
  1970. ((void) p);
  1971. ((void) end);
  1972. #endif
  1973. #if defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1974. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1975. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  1976. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
  1977. {
  1978. if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
  1979. {
  1980. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
  1981. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  1982. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  1983. return( ret );
  1984. }
  1985. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
  1986. ssl->state++;
  1987. return( 0 );
  1988. }
  1989. ((void) p);
  1990. ((void) end);
  1991. #endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  1992. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1993. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  1994. if( ssl->handshake->ecrs_enabled &&
  1995. ssl->handshake->ecrs_state == ssl_ecrs_ske_start_processing )
  1996. {
  1997. goto start_processing;
  1998. }
  1999. #endif
  2000. if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
  2001. {
  2002. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2003. return( ret );
  2004. }
  2005. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2006. {
  2007. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2008. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2009. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  2010. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2011. }
  2012. /*
  2013. * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
  2014. * doesn't use a psk_identity_hint
  2015. */
  2016. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE )
  2017. {
  2018. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2019. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  2020. {
  2021. /* Current message is probably either
  2022. * CertificateRequest or ServerHelloDone */
  2023. ssl->keep_current_message = 1;
  2024. goto exit;
  2025. }
  2026. MBEDTLS_SSL_DEBUG_MSG( 1, ( "server key exchange message must "
  2027. "not be skipped" ) );
  2028. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2029. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  2030. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2031. }
  2032. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  2033. if( ssl->handshake->ecrs_enabled )
  2034. ssl->handshake->ecrs_state = ssl_ecrs_ske_start_processing;
  2035. start_processing:
  2036. #endif
  2037. p = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  2038. end = ssl->in_msg + ssl->in_hslen;
  2039. MBEDTLS_SSL_DEBUG_BUF( 3, "server key exchange", p, end - p );
  2040. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  2041. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2042. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  2043. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2044. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  2045. {
  2046. if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
  2047. {
  2048. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2049. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2050. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2051. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2052. }
  2053. } /* FALLTROUGH */
  2054. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  2055. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) || \
  2056. defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  2057. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2058. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  2059. ; /* nothing more to do */
  2060. else
  2061. #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED ||
  2062. MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
  2063. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  2064. defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  2065. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA ||
  2066. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  2067. {
  2068. if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
  2069. {
  2070. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2071. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2072. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2073. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2074. }
  2075. }
  2076. else
  2077. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  2078. MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  2079. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2080. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  2081. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  2082. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  2083. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  2084. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA )
  2085. {
  2086. if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
  2087. {
  2088. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2089. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2090. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2091. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2092. }
  2093. }
  2094. else
  2095. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2096. MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  2097. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  2098. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2099. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2100. {
  2101. ret = mbedtls_ecjpake_read_round_two( &ssl->handshake->ecjpake_ctx,
  2102. p, end - p );
  2103. if( ret != 0 )
  2104. {
  2105. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_read_round_two", ret );
  2106. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2107. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2108. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2109. }
  2110. }
  2111. else
  2112. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2113. {
  2114. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2115. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2116. }
  2117. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED)
  2118. if( mbedtls_ssl_ciphersuite_uses_server_signature( ciphersuite_info ) )
  2119. {
  2120. size_t sig_len, hashlen;
  2121. unsigned char hash[64];
  2122. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  2123. mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
  2124. unsigned char *params = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  2125. size_t params_len = p - params;
  2126. void *rs_ctx = NULL;
  2127. /*
  2128. * Handle the digitally-signed structure
  2129. */
  2130. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2131. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  2132. {
  2133. if( ssl_parse_signature_algorithm( ssl, &p, end,
  2134. &md_alg, &pk_alg ) != 0 )
  2135. {
  2136. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2137. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2138. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2139. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2140. }
  2141. if( pk_alg != mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
  2142. {
  2143. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2144. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2145. MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER );
  2146. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2147. }
  2148. }
  2149. else
  2150. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2151. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2152. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2153. if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
  2154. {
  2155. pk_alg = mbedtls_ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
  2156. /* Default hash for ECDSA is SHA-1 */
  2157. if( pk_alg == MBEDTLS_PK_ECDSA && md_alg == MBEDTLS_MD_NONE )
  2158. md_alg = MBEDTLS_MD_SHA1;
  2159. }
  2160. else
  2161. #endif
  2162. {
  2163. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2164. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2165. }
  2166. /*
  2167. * Read signature
  2168. */
  2169. if( p > end - 2 )
  2170. {
  2171. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2172. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2173. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2174. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2175. }
  2176. sig_len = ( p[0] << 8 ) | p[1];
  2177. p += 2;
  2178. if( p != end - sig_len )
  2179. {
  2180. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2181. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2182. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2183. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  2184. }
  2185. MBEDTLS_SSL_DEBUG_BUF( 3, "signature", p, sig_len );
  2186. /*
  2187. * Compute the hash that has been signed
  2188. */
  2189. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2190. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2191. if( md_alg == MBEDTLS_MD_NONE )
  2192. {
  2193. hashlen = 36;
  2194. ret = mbedtls_ssl_get_key_exchange_md_ssl_tls( ssl, hash, params,
  2195. params_len );
  2196. if( ret != 0 )
  2197. return( ret );
  2198. }
  2199. else
  2200. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  2201. MBEDTLS_SSL_PROTO_TLS1_1 */
  2202. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  2203. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2204. if( md_alg != MBEDTLS_MD_NONE )
  2205. {
  2206. ret = mbedtls_ssl_get_key_exchange_md_tls1_2( ssl, hash, &hashlen,
  2207. params, params_len,
  2208. md_alg );
  2209. if( ret != 0 )
  2210. return( ret );
  2211. }
  2212. else
  2213. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  2214. MBEDTLS_SSL_PROTO_TLS1_2 */
  2215. {
  2216. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2217. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2218. }
  2219. MBEDTLS_SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen );
  2220. if( ssl->session_negotiate->peer_cert == NULL )
  2221. {
  2222. MBEDTLS_SSL_DEBUG_MSG( 2, ( "certificate required" ) );
  2223. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2224. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  2225. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2226. }
  2227. /*
  2228. * Verify signature
  2229. */
  2230. if( ! mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
  2231. {
  2232. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  2233. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2234. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE );
  2235. return( MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH );
  2236. }
  2237. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  2238. if( ssl->handshake->ecrs_enabled )
  2239. rs_ctx = &ssl->handshake->ecrs_ctx.pk;
  2240. #endif
  2241. if( ( ret = mbedtls_pk_verify_restartable(
  2242. &ssl->session_negotiate->peer_cert->pk,
  2243. md_alg, hash, hashlen, p, sig_len, rs_ctx ) ) != 0 )
  2244. {
  2245. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  2246. if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
  2247. #endif
  2248. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2249. MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR );
  2250. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify", ret );
  2251. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  2252. if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  2253. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  2254. #endif
  2255. return( ret );
  2256. }
  2257. }
  2258. #endif /* MBEDTLS_KEY_EXCHANGE__WITH_SERVER_SIGNATURE__ENABLED */
  2259. exit:
  2260. ssl->state++;
  2261. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
  2262. return( 0 );
  2263. }
  2264. #if ! defined(MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED)
  2265. static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
  2266. {
  2267. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2268. ssl->transform_negotiate->ciphersuite_info;
  2269. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
  2270. if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
  2271. {
  2272. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
  2273. ssl->state++;
  2274. return( 0 );
  2275. }
  2276. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2277. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2278. }
  2279. #else /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */
  2280. static int ssl_parse_certificate_request( mbedtls_ssl_context *ssl )
  2281. {
  2282. int ret;
  2283. unsigned char *buf;
  2284. size_t n = 0;
  2285. size_t cert_type_len = 0, dn_len = 0;
  2286. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2287. ssl->transform_negotiate->ciphersuite_info;
  2288. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
  2289. if( ! mbedtls_ssl_ciphersuite_cert_req_allowed( ciphersuite_info ) )
  2290. {
  2291. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
  2292. ssl->state++;
  2293. return( 0 );
  2294. }
  2295. if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
  2296. {
  2297. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2298. return( ret );
  2299. }
  2300. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2301. {
  2302. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2303. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2304. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  2305. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2306. }
  2307. ssl->state++;
  2308. ssl->client_auth = ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST );
  2309. MBEDTLS_SSL_DEBUG_MSG( 3, ( "got %s certificate request",
  2310. ssl->client_auth ? "a" : "no" ) );
  2311. if( ssl->client_auth == 0 )
  2312. {
  2313. /* Current message is probably the ServerHelloDone */
  2314. ssl->keep_current_message = 1;
  2315. goto exit;
  2316. }
  2317. /*
  2318. * struct {
  2319. * ClientCertificateType certificate_types<1..2^8-1>;
  2320. * SignatureAndHashAlgorithm
  2321. * supported_signature_algorithms<2^16-1>; -- TLS 1.2 only
  2322. * DistinguishedName certificate_authorities<0..2^16-1>;
  2323. * } CertificateRequest;
  2324. *
  2325. * Since we only support a single certificate on clients, let's just
  2326. * ignore all the information that's supposed to help us pick a
  2327. * certificate.
  2328. *
  2329. * We could check that our certificate matches the request, and bail out
  2330. * if it doesn't, but it's simpler to just send the certificate anyway,
  2331. * and give the server the opportunity to decide if it should terminate
  2332. * the connection when it doesn't like our certificate.
  2333. *
  2334. * Same goes for the hash in TLS 1.2's signature_algorithms: at this
  2335. * point we only have one hash available (see comments in
  2336. * write_certificate_verify), so let's just use what we have.
  2337. *
  2338. * However, we still minimally parse the message to check it is at least
  2339. * superficially sane.
  2340. */
  2341. buf = ssl->in_msg;
  2342. /* certificate_types */
  2343. if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) )
  2344. {
  2345. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2346. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2347. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2348. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  2349. }
  2350. cert_type_len = buf[mbedtls_ssl_hs_hdr_len( ssl )];
  2351. n = cert_type_len;
  2352. /*
  2353. * In the subsequent code there are two paths that read from buf:
  2354. * * the length of the signature algorithms field (if minor version of
  2355. * SSL is 3),
  2356. * * distinguished name length otherwise.
  2357. * Both reach at most the index:
  2358. * ...hdr_len + 2 + n,
  2359. * therefore the buffer length at this point must be greater than that
  2360. * regardless of the actual code path.
  2361. */
  2362. if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n )
  2363. {
  2364. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2365. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2366. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2367. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  2368. }
  2369. /* supported_signature_algorithms */
  2370. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2371. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  2372. {
  2373. size_t sig_alg_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
  2374. | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
  2375. #if defined(MBEDTLS_DEBUG_C)
  2376. unsigned char* sig_alg;
  2377. size_t i;
  2378. #endif
  2379. /*
  2380. * The furthest access in buf is in the loop few lines below:
  2381. * sig_alg[i + 1],
  2382. * where:
  2383. * sig_alg = buf + ...hdr_len + 3 + n,
  2384. * max(i) = sig_alg_len - 1.
  2385. * Therefore the furthest access is:
  2386. * buf[...hdr_len + 3 + n + sig_alg_len - 1 + 1],
  2387. * which reduces to:
  2388. * buf[...hdr_len + 3 + n + sig_alg_len],
  2389. * which is one less than we need the buf to be.
  2390. */
  2391. if( ssl->in_hslen <= mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n + sig_alg_len )
  2392. {
  2393. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2394. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2395. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2396. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  2397. }
  2398. #if defined(MBEDTLS_DEBUG_C)
  2399. sig_alg = buf + mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n;
  2400. for( i = 0; i < sig_alg_len; i += 2 )
  2401. {
  2402. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Supported Signature Algorithm found: %d"
  2403. ",%d", sig_alg[i], sig_alg[i + 1] ) );
  2404. }
  2405. #endif
  2406. n += 2 + sig_alg_len;
  2407. }
  2408. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2409. /* certificate_authorities */
  2410. dn_len = ( ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 1 + n] << 8 )
  2411. | ( buf[mbedtls_ssl_hs_hdr_len( ssl ) + 2 + n] ) );
  2412. n += dn_len;
  2413. if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + 3 + n )
  2414. {
  2415. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  2416. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2417. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2418. return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  2419. }
  2420. exit:
  2421. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
  2422. return( 0 );
  2423. }
  2424. #endif /* MBEDTLS_KEY_EXCHANGE__CERT_REQ_ALLOWED__ENABLED */
  2425. static int ssl_parse_server_hello_done( mbedtls_ssl_context *ssl )
  2426. {
  2427. int ret;
  2428. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
  2429. if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
  2430. {
  2431. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2432. return( ret );
  2433. }
  2434. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2435. {
  2436. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
  2437. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2438. }
  2439. if( ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ||
  2440. ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO_DONE )
  2441. {
  2442. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
  2443. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2444. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2445. return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
  2446. }
  2447. ssl->state++;
  2448. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2449. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  2450. mbedtls_ssl_recv_flight_completed( ssl );
  2451. #endif
  2452. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
  2453. return( 0 );
  2454. }
  2455. static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl )
  2456. {
  2457. int ret;
  2458. size_t i, n;
  2459. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2460. ssl->transform_negotiate->ciphersuite_info;
  2461. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
  2462. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  2463. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_RSA )
  2464. {
  2465. /*
  2466. * DHM key exchange -- send G^X mod P
  2467. */
  2468. n = ssl->handshake->dhm_ctx.len;
  2469. ssl->out_msg[4] = (unsigned char)( n >> 8 );
  2470. ssl->out_msg[5] = (unsigned char)( n );
  2471. i = 6;
  2472. ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
  2473. (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
  2474. &ssl->out_msg[i], n,
  2475. ssl->conf->f_rng, ssl->conf->p_rng );
  2476. if( ret != 0 )
  2477. {
  2478. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
  2479. return( ret );
  2480. }
  2481. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
  2482. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
  2483. if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
  2484. ssl->handshake->premaster,
  2485. MBEDTLS_PREMASTER_SIZE,
  2486. &ssl->handshake->pmslen,
  2487. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  2488. {
  2489. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
  2490. return( ret );
  2491. }
  2492. MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
  2493. }
  2494. else
  2495. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
  2496. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  2497. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  2498. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  2499. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  2500. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_RSA ||
  2501. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
  2502. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
  2503. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA )
  2504. {
  2505. /*
  2506. * ECDH key exchange -- send client public value
  2507. */
  2508. i = 4;
  2509. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  2510. if( ssl->handshake->ecrs_enabled )
  2511. {
  2512. if( ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret )
  2513. goto ecdh_calc_secret;
  2514. mbedtls_ecdh_enable_restart( &ssl->handshake->ecdh_ctx );
  2515. }
  2516. #endif
  2517. ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
  2518. &n,
  2519. &ssl->out_msg[i], 1000,
  2520. ssl->conf->f_rng, ssl->conf->p_rng );
  2521. if( ret != 0 )
  2522. {
  2523. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
  2524. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  2525. if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  2526. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  2527. #endif
  2528. return( ret );
  2529. }
  2530. MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
  2531. MBEDTLS_DEBUG_ECDH_Q );
  2532. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  2533. if( ssl->handshake->ecrs_enabled )
  2534. {
  2535. ssl->handshake->ecrs_n = n;
  2536. ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
  2537. }
  2538. ecdh_calc_secret:
  2539. if( ssl->handshake->ecrs_enabled )
  2540. n = ssl->handshake->ecrs_n;
  2541. #endif
  2542. if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
  2543. &ssl->handshake->pmslen,
  2544. ssl->handshake->premaster,
  2545. MBEDTLS_MPI_MAX_SIZE,
  2546. ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
  2547. {
  2548. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
  2549. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  2550. if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  2551. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  2552. #endif
  2553. return( ret );
  2554. }
  2555. MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
  2556. MBEDTLS_DEBUG_ECDH_Z );
  2557. }
  2558. else
  2559. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  2560. MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  2561. MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  2562. MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  2563. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  2564. if( mbedtls_ssl_ciphersuite_uses_psk( ciphersuite_info ) )
  2565. {
  2566. /*
  2567. * opaque psk_identity<0..2^16-1>;
  2568. */
  2569. if( ssl->conf->psk == NULL || ssl->conf->psk_identity == NULL )
  2570. {
  2571. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for PSK" ) );
  2572. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  2573. }
  2574. i = 4;
  2575. n = ssl->conf->psk_identity_len;
  2576. if( i + 2 + n > MBEDTLS_SSL_OUT_CONTENT_LEN )
  2577. {
  2578. MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity too long or "
  2579. "SSL buffer too short" ) );
  2580. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  2581. }
  2582. ssl->out_msg[i++] = (unsigned char)( n >> 8 );
  2583. ssl->out_msg[i++] = (unsigned char)( n );
  2584. memcpy( ssl->out_msg + i, ssl->conf->psk_identity, ssl->conf->psk_identity_len );
  2585. i += ssl->conf->psk_identity_len;
  2586. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  2587. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK )
  2588. {
  2589. n = 0;
  2590. }
  2591. else
  2592. #endif
  2593. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  2594. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
  2595. {
  2596. if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
  2597. return( ret );
  2598. }
  2599. else
  2600. #endif
  2601. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  2602. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
  2603. {
  2604. /*
  2605. * ClientDiffieHellmanPublic public (DHM send G^X mod P)
  2606. */
  2607. n = ssl->handshake->dhm_ctx.len;
  2608. if( i + 2 + n > MBEDTLS_SSL_OUT_CONTENT_LEN )
  2609. {
  2610. MBEDTLS_SSL_DEBUG_MSG( 1, ( "psk identity or DHM size too long"
  2611. " or SSL buffer too short" ) );
  2612. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  2613. }
  2614. ssl->out_msg[i++] = (unsigned char)( n >> 8 );
  2615. ssl->out_msg[i++] = (unsigned char)( n );
  2616. ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx,
  2617. (int) mbedtls_mpi_size( &ssl->handshake->dhm_ctx.P ),
  2618. &ssl->out_msg[i], n,
  2619. ssl->conf->f_rng, ssl->conf->p_rng );
  2620. if( ret != 0 )
  2621. {
  2622. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_make_public", ret );
  2623. return( ret );
  2624. }
  2625. }
  2626. else
  2627. #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
  2628. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  2629. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
  2630. {
  2631. /*
  2632. * ClientECDiffieHellmanPublic public;
  2633. */
  2634. ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
  2635. &ssl->out_msg[i], MBEDTLS_SSL_OUT_CONTENT_LEN - i,
  2636. ssl->conf->f_rng, ssl->conf->p_rng );
  2637. if( ret != 0 )
  2638. {
  2639. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_make_public", ret );
  2640. return( ret );
  2641. }
  2642. MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
  2643. MBEDTLS_DEBUG_ECDH_Q );
  2644. }
  2645. else
  2646. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  2647. {
  2648. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2649. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2650. }
  2651. if( ( ret = mbedtls_ssl_psk_derive_premaster( ssl,
  2652. ciphersuite_info->key_exchange ) ) != 0 )
  2653. {
  2654. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_psk_derive_premaster", ret );
  2655. return( ret );
  2656. }
  2657. }
  2658. else
  2659. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  2660. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  2661. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA )
  2662. {
  2663. i = 4;
  2664. if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
  2665. return( ret );
  2666. }
  2667. else
  2668. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  2669. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2670. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2671. {
  2672. i = 4;
  2673. ret = mbedtls_ecjpake_write_round_two( &ssl->handshake->ecjpake_ctx,
  2674. ssl->out_msg + i, MBEDTLS_SSL_OUT_CONTENT_LEN - i, &n,
  2675. ssl->conf->f_rng, ssl->conf->p_rng );
  2676. if( ret != 0 )
  2677. {
  2678. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_write_round_two", ret );
  2679. return( ret );
  2680. }
  2681. ret = mbedtls_ecjpake_derive_secret( &ssl->handshake->ecjpake_ctx,
  2682. ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
  2683. ssl->conf->f_rng, ssl->conf->p_rng );
  2684. if( ret != 0 )
  2685. {
  2686. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecjpake_derive_secret", ret );
  2687. return( ret );
  2688. }
  2689. }
  2690. else
  2691. #endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
  2692. {
  2693. ((void) ciphersuite_info);
  2694. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2695. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2696. }
  2697. ssl->out_msglen = i + n;
  2698. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2699. ssl->out_msg[0] = MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE;
  2700. ssl->state++;
  2701. if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
  2702. {
  2703. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
  2704. return( ret );
  2705. }
  2706. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
  2707. return( 0 );
  2708. }
  2709. #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \
  2710. !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
  2711. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
  2712. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
  2713. !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)&& \
  2714. !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  2715. static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
  2716. {
  2717. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2718. ssl->transform_negotiate->ciphersuite_info;
  2719. int ret;
  2720. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
  2721. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  2722. {
  2723. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  2724. return( ret );
  2725. }
  2726. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2727. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  2728. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  2729. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2730. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2731. {
  2732. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  2733. ssl->state++;
  2734. return( 0 );
  2735. }
  2736. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2737. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2738. }
  2739. #else
  2740. static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl )
  2741. {
  2742. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  2743. const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
  2744. ssl->transform_negotiate->ciphersuite_info;
  2745. size_t n = 0, offset = 0;
  2746. unsigned char hash[48];
  2747. unsigned char *hash_start = hash;
  2748. mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
  2749. unsigned int hashlen;
  2750. void *rs_ctx = NULL;
  2751. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
  2752. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  2753. if( ssl->handshake->ecrs_enabled &&
  2754. ssl->handshake->ecrs_state == ssl_ecrs_crt_vrfy_sign )
  2755. {
  2756. goto sign;
  2757. }
  2758. #endif
  2759. if( ( ret = mbedtls_ssl_derive_keys( ssl ) ) != 0 )
  2760. {
  2761. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_derive_keys", ret );
  2762. return( ret );
  2763. }
  2764. if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
  2765. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ||
  2766. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
  2767. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
  2768. ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
  2769. {
  2770. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  2771. ssl->state++;
  2772. return( 0 );
  2773. }
  2774. if( ssl->client_auth == 0 || mbedtls_ssl_own_cert( ssl ) == NULL )
  2775. {
  2776. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  2777. ssl->state++;
  2778. return( 0 );
  2779. }
  2780. if( mbedtls_ssl_own_key( ssl ) == NULL )
  2781. {
  2782. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no private key for certificate" ) );
  2783. return( MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED );
  2784. }
  2785. /*
  2786. * Make a signature of the handshake digests
  2787. */
  2788. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  2789. if( ssl->handshake->ecrs_enabled )
  2790. ssl->handshake->ecrs_state = ssl_ecrs_crt_vrfy_sign;
  2791. sign:
  2792. #endif
  2793. ssl->handshake->calc_verify( ssl, hash );
  2794. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  2795. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  2796. if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
  2797. {
  2798. /*
  2799. * digitally-signed struct {
  2800. * opaque md5_hash[16];
  2801. * opaque sha_hash[20];
  2802. * };
  2803. *
  2804. * md5_hash
  2805. * MD5(handshake_messages);
  2806. *
  2807. * sha_hash
  2808. * SHA(handshake_messages);
  2809. */
  2810. hashlen = 36;
  2811. md_alg = MBEDTLS_MD_NONE;
  2812. /*
  2813. * For ECDSA, default hash is SHA-1 only
  2814. */
  2815. if( mbedtls_pk_can_do( mbedtls_ssl_own_key( ssl ), MBEDTLS_PK_ECDSA ) )
  2816. {
  2817. hash_start += 16;
  2818. hashlen -= 16;
  2819. md_alg = MBEDTLS_MD_SHA1;
  2820. }
  2821. }
  2822. else
  2823. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  2824. MBEDTLS_SSL_PROTO_TLS1_1 */
  2825. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  2826. if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
  2827. {
  2828. /*
  2829. * digitally-signed struct {
  2830. * opaque handshake_messages[handshake_messages_length];
  2831. * };
  2832. *
  2833. * Taking shortcut here. We assume that the server always allows the
  2834. * PRF Hash function and has sent it in the allowed signature
  2835. * algorithms list received in the Certificate Request message.
  2836. *
  2837. * Until we encounter a server that does not, we will take this
  2838. * shortcut.
  2839. *
  2840. * Reason: Otherwise we should have running hashes for SHA512 and SHA224
  2841. * in order to satisfy 'weird' needs from the server side.
  2842. */
  2843. if( ssl->transform_negotiate->ciphersuite_info->mac ==
  2844. MBEDTLS_MD_SHA384 )
  2845. {
  2846. md_alg = MBEDTLS_MD_SHA384;
  2847. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA384;
  2848. }
  2849. else
  2850. {
  2851. md_alg = MBEDTLS_MD_SHA256;
  2852. ssl->out_msg[4] = MBEDTLS_SSL_HASH_SHA256;
  2853. }
  2854. ssl->out_msg[5] = mbedtls_ssl_sig_from_pk( mbedtls_ssl_own_key( ssl ) );
  2855. /* Info from md_alg will be used instead */
  2856. hashlen = 0;
  2857. offset = 2;
  2858. }
  2859. else
  2860. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  2861. {
  2862. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2863. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  2864. }
  2865. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  2866. if( ssl->handshake->ecrs_enabled )
  2867. rs_ctx = &ssl->handshake->ecrs_ctx.pk;
  2868. #endif
  2869. if( ( ret = mbedtls_pk_sign_restartable( mbedtls_ssl_own_key( ssl ),
  2870. md_alg, hash_start, hashlen,
  2871. ssl->out_msg + 6 + offset, &n,
  2872. ssl->conf->f_rng, ssl->conf->p_rng, rs_ctx ) ) != 0 )
  2873. {
  2874. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
  2875. #if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
  2876. if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  2877. ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
  2878. #endif
  2879. return( ret );
  2880. }
  2881. ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
  2882. ssl->out_msg[5 + offset] = (unsigned char)( n );
  2883. ssl->out_msglen = 6 + n + offset;
  2884. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  2885. ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_VERIFY;
  2886. ssl->state++;
  2887. if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
  2888. {
  2889. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
  2890. return( ret );
  2891. }
  2892. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
  2893. return( ret );
  2894. }
  2895. #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED &&
  2896. !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED &&
  2897. !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED &&
  2898. !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
  2899. !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED &&
  2900. !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  2901. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2902. static int ssl_parse_new_session_ticket( mbedtls_ssl_context *ssl )
  2903. {
  2904. int ret;
  2905. uint32_t lifetime;
  2906. size_t ticket_len;
  2907. unsigned char *ticket;
  2908. const unsigned char *msg;
  2909. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
  2910. if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
  2911. {
  2912. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  2913. return( ret );
  2914. }
  2915. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
  2916. {
  2917. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  2918. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2919. MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
  2920. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  2921. }
  2922. /*
  2923. * struct {
  2924. * uint32 ticket_lifetime_hint;
  2925. * opaque ticket<0..2^16-1>;
  2926. * } NewSessionTicket;
  2927. *
  2928. * 0 . 3 ticket_lifetime_hint
  2929. * 4 . 5 ticket_len (n)
  2930. * 6 . 5+n ticket content
  2931. */
  2932. if( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ||
  2933. ssl->in_hslen < 6 + mbedtls_ssl_hs_hdr_len( ssl ) )
  2934. {
  2935. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  2936. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2937. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2938. return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
  2939. }
  2940. msg = ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl );
  2941. lifetime = ( ((uint32_t) msg[0]) << 24 ) | ( msg[1] << 16 ) |
  2942. ( msg[2] << 8 ) | ( msg[3] );
  2943. ticket_len = ( msg[4] << 8 ) | ( msg[5] );
  2944. if( ticket_len + 6 + mbedtls_ssl_hs_hdr_len( ssl ) != ssl->in_hslen )
  2945. {
  2946. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  2947. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2948. MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
  2949. return( MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
  2950. }
  2951. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
  2952. /* We're not waiting for a NewSessionTicket message any more */
  2953. ssl->handshake->new_session_ticket = 0;
  2954. ssl->state = MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC;
  2955. /*
  2956. * Zero-length ticket means the server changed his mind and doesn't want
  2957. * to send a ticket after all, so just forget it
  2958. */
  2959. if( ticket_len == 0 )
  2960. return( 0 );
  2961. mbedtls_platform_zeroize( ssl->session_negotiate->ticket,
  2962. ssl->session_negotiate->ticket_len );
  2963. mbedtls_free( ssl->session_negotiate->ticket );
  2964. ssl->session_negotiate->ticket = NULL;
  2965. ssl->session_negotiate->ticket_len = 0;
  2966. if( ( ticket = mbedtls_calloc( 1, ticket_len ) ) == NULL )
  2967. {
  2968. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ticket alloc failed" ) );
  2969. mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
  2970. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
  2971. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  2972. }
  2973. memcpy( ticket, msg + 6, ticket_len );
  2974. ssl->session_negotiate->ticket = ticket;
  2975. ssl->session_negotiate->ticket_len = ticket_len;
  2976. ssl->session_negotiate->ticket_lifetime = lifetime;
  2977. /*
  2978. * RFC 5077 section 3.4:
  2979. * "If the client receives a session ticket from the server, then it
  2980. * discards any Session ID that was sent in the ServerHello."
  2981. */
  2982. MBEDTLS_SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
  2983. ssl->session_negotiate->id_len = 0;
  2984. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
  2985. return( 0 );
  2986. }
  2987. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  2988. /*
  2989. * SSL handshake -- client side -- single step
  2990. */
  2991. int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl )
  2992. {
  2993. int ret = 0;
  2994. if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL )
  2995. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  2996. MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
  2997. if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
  2998. return( ret );
  2999. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  3000. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  3001. ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
  3002. {
  3003. if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
  3004. return( ret );
  3005. }
  3006. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  3007. /* Change state now, so that it is right in mbedtls_ssl_read_record(), used
  3008. * by DTLS for dropping out-of-sequence ChangeCipherSpec records */
  3009. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3010. if( ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC &&
  3011. ssl->handshake->new_session_ticket != 0 )
  3012. {
  3013. ssl->state = MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET;
  3014. }
  3015. #endif
  3016. switch( ssl->state )
  3017. {
  3018. case MBEDTLS_SSL_HELLO_REQUEST:
  3019. ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
  3020. break;
  3021. /*
  3022. * ==> ClientHello
  3023. */
  3024. case MBEDTLS_SSL_CLIENT_HELLO:
  3025. ret = ssl_write_client_hello( ssl );
  3026. break;
  3027. /*
  3028. * <== ServerHello
  3029. * Certificate
  3030. * ( ServerKeyExchange )
  3031. * ( CertificateRequest )
  3032. * ServerHelloDone
  3033. */
  3034. case MBEDTLS_SSL_SERVER_HELLO:
  3035. ret = ssl_parse_server_hello( ssl );
  3036. break;
  3037. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  3038. ret = mbedtls_ssl_parse_certificate( ssl );
  3039. break;
  3040. case MBEDTLS_SSL_SERVER_KEY_EXCHANGE:
  3041. ret = ssl_parse_server_key_exchange( ssl );
  3042. break;
  3043. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  3044. ret = ssl_parse_certificate_request( ssl );
  3045. break;
  3046. case MBEDTLS_SSL_SERVER_HELLO_DONE:
  3047. ret = ssl_parse_server_hello_done( ssl );
  3048. break;
  3049. /*
  3050. * ==> ( Certificate/Alert )
  3051. * ClientKeyExchange
  3052. * ( CertificateVerify )
  3053. * ChangeCipherSpec
  3054. * Finished
  3055. */
  3056. case MBEDTLS_SSL_CLIENT_CERTIFICATE:
  3057. ret = mbedtls_ssl_write_certificate( ssl );
  3058. break;
  3059. case MBEDTLS_SSL_CLIENT_KEY_EXCHANGE:
  3060. ret = ssl_write_client_key_exchange( ssl );
  3061. break;
  3062. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  3063. ret = ssl_write_certificate_verify( ssl );
  3064. break;
  3065. case MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC:
  3066. ret = mbedtls_ssl_write_change_cipher_spec( ssl );
  3067. break;
  3068. case MBEDTLS_SSL_CLIENT_FINISHED:
  3069. ret = mbedtls_ssl_write_finished( ssl );
  3070. break;
  3071. /*
  3072. * <== ( NewSessionTicket )
  3073. * ChangeCipherSpec
  3074. * Finished
  3075. */
  3076. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3077. case MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET:
  3078. ret = ssl_parse_new_session_ticket( ssl );
  3079. break;
  3080. #endif
  3081. case MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC:
  3082. ret = mbedtls_ssl_parse_change_cipher_spec( ssl );
  3083. break;
  3084. case MBEDTLS_SSL_SERVER_FINISHED:
  3085. ret = mbedtls_ssl_parse_finished( ssl );
  3086. break;
  3087. case MBEDTLS_SSL_FLUSH_BUFFERS:
  3088. MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
  3089. ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
  3090. break;
  3091. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  3092. mbedtls_ssl_handshake_wrapup( ssl );
  3093. break;
  3094. default:
  3095. MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
  3096. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  3097. }
  3098. return( ret );
  3099. }
  3100. #endif /* MBEDTLS_SSL_CLI_C */