ssl_cli.c 117 KB

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