ssl_cli.c 108 KB

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