ssl_cli.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  1. /*
  2. * SSLv3/TLSv1 client-side functions
  3. *
  4. * Copyright (C) 2006-2014, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #if !defined(POLARSSL_CONFIG_FILE)
  26. #include "polarssl/config.h"
  27. #else
  28. #include POLARSSL_CONFIG_FILE
  29. #endif
  30. #if defined(POLARSSL_SSL_CLI_C)
  31. #include "polarssl/debug.h"
  32. #include "polarssl/ssl.h"
  33. #if defined(POLARSSL_PLATFORM_C)
  34. #include "polarssl/platform.h"
  35. #else
  36. #define polarssl_malloc malloc
  37. #define polarssl_free free
  38. #endif
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
  42. #include <basetsd.h>
  43. typedef UINT32 uint32_t;
  44. #else
  45. #include <inttypes.h>
  46. #endif
  47. #if defined(POLARSSL_HAVE_TIME)
  48. #include <time.h>
  49. #endif
  50. #if defined(POLARSSL_SSL_SESSION_TICKETS)
  51. /* Implementation that should never be optimized out by the compiler */
  52. static void polarssl_zeroize( void *v, size_t n ) {
  53. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  54. }
  55. #endif
  56. #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
  57. static void ssl_write_hostname_ext( ssl_context *ssl,
  58. unsigned char *buf,
  59. size_t *olen )
  60. {
  61. unsigned char *p = buf;
  62. *olen = 0;
  63. if( ssl->hostname == NULL )
  64. return;
  65. SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
  66. ssl->hostname ) );
  67. /*
  68. * struct {
  69. * NameType name_type;
  70. * select (name_type) {
  71. * case host_name: HostName;
  72. * } name;
  73. * } ServerName;
  74. *
  75. * enum {
  76. * host_name(0), (255)
  77. * } NameType;
  78. *
  79. * opaque HostName<1..2^16-1>;
  80. *
  81. * struct {
  82. * ServerName server_name_list<1..2^16-1>
  83. * } ServerNameList;
  84. */
  85. *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
  86. *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
  87. *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
  88. *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
  89. *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
  90. *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
  91. *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
  92. *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
  93. *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
  94. memcpy( p, ssl->hostname, ssl->hostname_len );
  95. *olen = ssl->hostname_len + 9;
  96. }
  97. #endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
  98. static void ssl_write_renegotiation_ext( ssl_context *ssl,
  99. unsigned char *buf,
  100. size_t *olen )
  101. {
  102. unsigned char *p = buf;
  103. *olen = 0;
  104. if( ssl->renegotiation != SSL_RENEGOTIATION )
  105. return;
  106. SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
  107. /*
  108. * Secure renegotiation
  109. */
  110. *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
  111. *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
  112. *p++ = 0x00;
  113. *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
  114. *p++ = ssl->verify_data_len & 0xFF;
  115. memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
  116. *olen = 5 + ssl->verify_data_len;
  117. }
  118. #if defined(POLARSSL_SSL_PROTO_TLS1_2)
  119. static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
  120. unsigned char *buf,
  121. size_t *olen )
  122. {
  123. unsigned char *p = buf;
  124. size_t sig_alg_len = 0;
  125. #if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
  126. unsigned char *sig_alg_list = buf + 6;
  127. #endif
  128. *olen = 0;
  129. if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
  130. return;
  131. SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
  132. /*
  133. * Prepare signature_algorithms extension (TLS 1.2)
  134. */
  135. #if defined(POLARSSL_RSA_C)
  136. #if defined(POLARSSL_SHA512_C)
  137. sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
  138. sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
  139. sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
  140. sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
  141. #endif
  142. #if defined(POLARSSL_SHA256_C)
  143. sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
  144. sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
  145. sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
  146. sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
  147. #endif
  148. #if defined(POLARSSL_SHA1_C)
  149. sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
  150. sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
  151. #endif
  152. #if defined(POLARSSL_MD5_C)
  153. sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
  154. sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
  155. #endif
  156. #endif /* POLARSSL_RSA_C */
  157. #if defined(POLARSSL_ECDSA_C)
  158. #if defined(POLARSSL_SHA512_C)
  159. sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
  160. sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
  161. sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
  162. sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
  163. #endif
  164. #if defined(POLARSSL_SHA256_C)
  165. sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
  166. sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
  167. sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
  168. sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
  169. #endif
  170. #if defined(POLARSSL_SHA1_C)
  171. sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
  172. sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
  173. #endif
  174. #if defined(POLARSSL_MD5_C)
  175. sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
  176. sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
  177. #endif
  178. #endif /* POLARSSL_ECDSA_C */
  179. /*
  180. * enum {
  181. * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
  182. * sha512(6), (255)
  183. * } HashAlgorithm;
  184. *
  185. * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
  186. * SignatureAlgorithm;
  187. *
  188. * struct {
  189. * HashAlgorithm hash;
  190. * SignatureAlgorithm signature;
  191. * } SignatureAndHashAlgorithm;
  192. *
  193. * SignatureAndHashAlgorithm
  194. * supported_signature_algorithms<2..2^16-2>;
  195. */
  196. *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
  197. *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
  198. *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
  199. *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
  200. *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
  201. *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
  202. *olen = 6 + sig_alg_len;
  203. }
  204. #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
  205. #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
  206. static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
  207. unsigned char *buf,
  208. size_t *olen )
  209. {
  210. unsigned char *p = buf;
  211. unsigned char *elliptic_curve_list = p + 6;
  212. size_t elliptic_curve_len = 0;
  213. const ecp_curve_info *info;
  214. #if defined(POLARSSL_SSL_SET_CURVES)
  215. const ecp_group_id *grp_id;
  216. #else
  217. ((void) ssl);
  218. #endif
  219. *olen = 0;
  220. SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
  221. #if defined(POLARSSL_SSL_SET_CURVES)
  222. for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
  223. {
  224. info = ecp_curve_info_from_grp_id( *grp_id );
  225. #else
  226. for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
  227. {
  228. #endif
  229. elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
  230. elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
  231. }
  232. if( elliptic_curve_len == 0 )
  233. return;
  234. *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
  235. *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
  236. *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
  237. *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
  238. *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
  239. *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
  240. *olen = 6 + elliptic_curve_len;
  241. }
  242. static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
  243. unsigned char *buf,
  244. size_t *olen )
  245. {
  246. unsigned char *p = buf;
  247. ((void) ssl);
  248. *olen = 0;
  249. SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
  250. *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
  251. *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
  252. *p++ = 0x00;
  253. *p++ = 2;
  254. *p++ = 1;
  255. *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
  256. *olen = 6;
  257. }
  258. #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
  259. #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
  260. static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
  261. unsigned char *buf,
  262. size_t *olen )
  263. {
  264. unsigned char *p = buf;
  265. if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ) {
  266. *olen = 0;
  267. return;
  268. }
  269. SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
  270. *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
  271. *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
  272. *p++ = 0x00;
  273. *p++ = 1;
  274. *p++ = ssl->mfl_code;
  275. *olen = 5;
  276. }
  277. #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
  278. #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
  279. static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
  280. unsigned char *buf, size_t *olen )
  281. {
  282. unsigned char *p = buf;
  283. if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
  284. {
  285. *olen = 0;
  286. return;
  287. }
  288. SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
  289. *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
  290. *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
  291. *p++ = 0x00;
  292. *p++ = 0x00;
  293. *olen = 4;
  294. }
  295. #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
  296. #if defined(POLARSSL_SSL_SESSION_TICKETS)
  297. static void ssl_write_session_ticket_ext( ssl_context *ssl,
  298. unsigned char *buf, size_t *olen )
  299. {
  300. unsigned char *p = buf;
  301. size_t tlen = ssl->session_negotiate->ticket_len;
  302. if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
  303. {
  304. *olen = 0;
  305. return;
  306. }
  307. SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
  308. *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
  309. *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
  310. *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
  311. *p++ = (unsigned char)( ( tlen ) & 0xFF );
  312. *olen = 4;
  313. if( ssl->session_negotiate->ticket == NULL ||
  314. ssl->session_negotiate->ticket_len == 0 )
  315. {
  316. return;
  317. }
  318. SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
  319. memcpy( p, ssl->session_negotiate->ticket, tlen );
  320. *olen += tlen;
  321. }
  322. #endif /* POLARSSL_SSL_SESSION_TICKETS */
  323. #if defined(POLARSSL_SSL_ALPN)
  324. static void ssl_write_alpn_ext( ssl_context *ssl,
  325. unsigned char *buf, size_t *olen )
  326. {
  327. unsigned char *p = buf;
  328. const char **cur;
  329. if( ssl->alpn_list == NULL )
  330. {
  331. *olen = 0;
  332. return;
  333. }
  334. SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
  335. *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
  336. *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
  337. /*
  338. * opaque ProtocolName<1..2^8-1>;
  339. *
  340. * struct {
  341. * ProtocolName protocol_name_list<2..2^16-1>
  342. * } ProtocolNameList;
  343. */
  344. /* Skip writing extension and list length for now */
  345. p += 4;
  346. for( cur = ssl->alpn_list; *cur != NULL; cur++ )
  347. {
  348. *p = (unsigned char)( strlen( *cur ) & 0xFF );
  349. memcpy( p + 1, *cur, *p );
  350. p += 1 + *p;
  351. }
  352. *olen = p - buf;
  353. /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
  354. buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
  355. buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
  356. /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
  357. buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
  358. buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
  359. }
  360. #endif /* POLARSSL_SSL_ALPN */
  361. static int ssl_write_client_hello( ssl_context *ssl )
  362. {
  363. int ret;
  364. size_t i, n, olen, ext_len = 0;
  365. unsigned char *buf;
  366. unsigned char *p, *q;
  367. #if defined(POLARSSL_HAVE_TIME)
  368. time_t t;
  369. #endif
  370. const int *ciphersuites;
  371. const ssl_ciphersuite_t *ciphersuite_info;
  372. SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
  373. if( ssl->f_rng == NULL )
  374. {
  375. SSL_DEBUG_MSG( 1, ( "no RNG provided") );
  376. return( POLARSSL_ERR_SSL_NO_RNG );
  377. }
  378. if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
  379. {
  380. ssl->major_ver = ssl->min_major_ver;
  381. ssl->minor_ver = ssl->min_minor_ver;
  382. }
  383. if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
  384. {
  385. ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
  386. ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
  387. }
  388. /*
  389. * 0 . 0 handshake type
  390. * 1 . 3 handshake length
  391. * 4 . 5 highest version supported
  392. * 6 . 9 current UNIX time
  393. * 10 . 37 random bytes
  394. */
  395. buf = ssl->out_msg;
  396. p = buf + 4;
  397. *p++ = (unsigned char) ssl->max_major_ver;
  398. *p++ = (unsigned char) ssl->max_minor_ver;
  399. SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
  400. buf[4], buf[5] ) );
  401. #if defined(POLARSSL_HAVE_TIME)
  402. t = time( NULL );
  403. *p++ = (unsigned char)( t >> 24 );
  404. *p++ = (unsigned char)( t >> 16 );
  405. *p++ = (unsigned char)( t >> 8 );
  406. *p++ = (unsigned char)( t );
  407. SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
  408. #else
  409. if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
  410. return( ret );
  411. p += 4;
  412. #endif /* POLARSSL_HAVE_TIME */
  413. if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
  414. return( ret );
  415. p += 28;
  416. memcpy( ssl->handshake->randbytes, buf + 6, 32 );
  417. SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
  418. /*
  419. * 38 . 38 session id length
  420. * 39 . 39+n session id
  421. * 40+n . 41+n ciphersuitelist length
  422. * 42+n . .. ciphersuitelist
  423. * .. . .. compression methods length
  424. * .. . .. compression methods
  425. * .. . .. extensions length
  426. * .. . .. extensions
  427. */
  428. n = ssl->session_negotiate->length;
  429. if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE || n < 16 || n > 32 ||
  430. ssl->handshake->resume == 0 )
  431. {
  432. n = 0;
  433. }
  434. #if defined(POLARSSL_SSL_SESSION_TICKETS)
  435. /*
  436. * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
  437. * generate and include a Session ID in the TLS ClientHello."
  438. */
  439. if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
  440. ssl->session_negotiate->ticket != NULL &&
  441. ssl->session_negotiate->ticket_len != 0 )
  442. {
  443. ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
  444. if( ret != 0 )
  445. return( ret );
  446. ssl->session_negotiate->length = n = 32;
  447. }
  448. #endif /* POLARSSL_SSL_SESSION_TICKETS */
  449. *p++ = (unsigned char) n;
  450. for( i = 0; i < n; i++ )
  451. *p++ = ssl->session_negotiate->id[i];
  452. SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
  453. SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
  454. ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
  455. n = 0;
  456. q = p;
  457. // Skip writing ciphersuite length for now
  458. p += 2;
  459. /*
  460. * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
  461. */
  462. if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
  463. {
  464. *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
  465. *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
  466. n++;
  467. }
  468. for( i = 0; ciphersuites[i] != 0; i++ )
  469. {
  470. ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
  471. if( ciphersuite_info == NULL )
  472. continue;
  473. if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
  474. ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
  475. continue;
  476. SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
  477. ciphersuites[i] ) );
  478. n++;
  479. *p++ = (unsigned char)( ciphersuites[i] >> 8 );
  480. *p++ = (unsigned char)( ciphersuites[i] );
  481. }
  482. *q++ = (unsigned char)( n >> 7 );
  483. *q++ = (unsigned char)( n << 1 );
  484. SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
  485. #if defined(POLARSSL_ZLIB_SUPPORT)
  486. SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
  487. SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
  488. SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
  489. *p++ = 2;
  490. *p++ = SSL_COMPRESS_DEFLATE;
  491. *p++ = SSL_COMPRESS_NULL;
  492. #else
  493. SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
  494. SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
  495. *p++ = 1;
  496. *p++ = SSL_COMPRESS_NULL;
  497. #endif /* POLARSSL_ZLIB_SUPPORT */
  498. // First write extensions, then the total length
  499. //
  500. #if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
  501. ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
  502. ext_len += olen;
  503. #endif
  504. ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
  505. ext_len += olen;
  506. #if defined(POLARSSL_SSL_PROTO_TLS1_2)
  507. ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
  508. ext_len += olen;
  509. #endif
  510. #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
  511. ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
  512. ext_len += olen;
  513. ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
  514. ext_len += olen;
  515. #endif
  516. #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
  517. ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
  518. ext_len += olen;
  519. #endif
  520. #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
  521. ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
  522. ext_len += olen;
  523. #endif
  524. #if defined(POLARSSL_SSL_SESSION_TICKETS)
  525. ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
  526. ext_len += olen;
  527. #endif
  528. #if defined(POLARSSL_SSL_ALPN)
  529. ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
  530. ext_len += olen;
  531. #endif
  532. SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
  533. ext_len ) );
  534. if( ext_len > 0 )
  535. {
  536. *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
  537. *p++ = (unsigned char)( ( ext_len ) & 0xFF );
  538. p += ext_len;
  539. }
  540. ssl->out_msglen = p - buf;
  541. ssl->out_msgtype = SSL_MSG_HANDSHAKE;
  542. ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
  543. ssl->state++;
  544. if( ( ret = ssl_write_record( ssl ) ) != 0 )
  545. {
  546. SSL_DEBUG_RET( 1, "ssl_write_record", ret );
  547. return( ret );
  548. }
  549. SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
  550. return( 0 );
  551. }
  552. static int ssl_parse_renegotiation_info( ssl_context *ssl,
  553. const unsigned char *buf,
  554. size_t len )
  555. {
  556. int ret;
  557. if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
  558. {
  559. if( len != 1 || buf[0] != 0x0 )
  560. {
  561. SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
  562. if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
  563. return( ret );
  564. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  565. }
  566. ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
  567. }
  568. else
  569. {
  570. /* Check verify-data in constant-time. The length OTOH is no secret */
  571. if( len != 1 + ssl->verify_data_len * 2 ||
  572. buf[0] != ssl->verify_data_len * 2 ||
  573. safer_memcmp( buf + 1,
  574. ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
  575. safer_memcmp( buf + 1 + ssl->verify_data_len,
  576. ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
  577. {
  578. SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
  579. if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
  580. return( ret );
  581. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  582. }
  583. }
  584. return( 0 );
  585. }
  586. #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
  587. static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
  588. const unsigned char *buf,
  589. size_t len )
  590. {
  591. /*
  592. * server should use the extension only if we did,
  593. * and if so the server's value should match ours (and len is always 1)
  594. */
  595. if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
  596. len != 1 ||
  597. buf[0] != ssl->mfl_code )
  598. {
  599. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  600. }
  601. return( 0 );
  602. }
  603. #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
  604. #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
  605. static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
  606. const unsigned char *buf,
  607. size_t len )
  608. {
  609. if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
  610. len != 0 )
  611. {
  612. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  613. }
  614. ((void) buf);
  615. ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
  616. return( 0 );
  617. }
  618. #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
  619. #if defined(POLARSSL_SSL_SESSION_TICKETS)
  620. static int ssl_parse_session_ticket_ext( ssl_context *ssl,
  621. const unsigned char *buf,
  622. size_t len )
  623. {
  624. if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
  625. len != 0 )
  626. {
  627. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  628. }
  629. ((void) buf);
  630. ssl->handshake->new_session_ticket = 1;
  631. return( 0 );
  632. }
  633. #endif /* POLARSSL_SSL_SESSION_TICKETS */
  634. #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
  635. static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
  636. const unsigned char *buf,
  637. size_t len )
  638. {
  639. size_t list_size;
  640. const unsigned char *p;
  641. list_size = buf[0];
  642. if( list_size + 1 != len )
  643. {
  644. SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  645. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  646. }
  647. p = buf + 1;
  648. while( list_size > 0 )
  649. {
  650. if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
  651. p[0] == POLARSSL_ECP_PF_COMPRESSED )
  652. {
  653. ssl->handshake->ecdh_ctx.point_format = p[0];
  654. SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
  655. return( 0 );
  656. }
  657. list_size--;
  658. p++;
  659. }
  660. SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
  661. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  662. }
  663. #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
  664. #if defined(POLARSSL_SSL_ALPN)
  665. static int ssl_parse_alpn_ext( ssl_context *ssl,
  666. const unsigned char *buf, size_t len )
  667. {
  668. size_t list_len, name_len;
  669. const char **p;
  670. /* If we didn't send it, the server shouldn't send it */
  671. if( ssl->alpn_list == NULL )
  672. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  673. /*
  674. * opaque ProtocolName<1..2^8-1>;
  675. *
  676. * struct {
  677. * ProtocolName protocol_name_list<2..2^16-1>
  678. * } ProtocolNameList;
  679. *
  680. * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
  681. */
  682. /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
  683. if( len < 4 )
  684. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  685. list_len = ( buf[0] << 8 ) | buf[1];
  686. if( list_len != len - 2 )
  687. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  688. name_len = buf[2];
  689. if( name_len != list_len - 1 )
  690. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  691. /* Check that the server chosen protocol was in our list and save it */
  692. for( p = ssl->alpn_list; *p != NULL; p++ )
  693. {
  694. if( name_len == strlen( *p ) &&
  695. memcmp( buf + 3, *p, name_len ) == 0 )
  696. {
  697. ssl->alpn_chosen = *p;
  698. return( 0 );
  699. }
  700. }
  701. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  702. }
  703. #endif /* POLARSSL_SSL_ALPN */
  704. static int ssl_parse_server_hello( ssl_context *ssl )
  705. {
  706. int ret, i, comp;
  707. size_t n;
  708. size_t ext_len = 0;
  709. unsigned char *buf, *ext;
  710. int renegotiation_info_seen = 0;
  711. int handshake_failure = 0;
  712. #if defined(POLARSSL_DEBUG_C)
  713. uint32_t t;
  714. #endif
  715. SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
  716. /*
  717. * 0 . 0 handshake type
  718. * 1 . 3 handshake length
  719. * 4 . 5 protocol version
  720. * 6 . 9 UNIX time()
  721. * 10 . 37 random bytes
  722. */
  723. buf = ssl->in_msg;
  724. if( ( ret = ssl_read_record( ssl ) ) != 0 )
  725. {
  726. SSL_DEBUG_RET( 1, "ssl_read_record", ret );
  727. return( ret );
  728. }
  729. if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
  730. {
  731. SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  732. return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
  733. }
  734. SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
  735. buf[4], buf[5] ) );
  736. if( ssl->in_hslen < 42 ||
  737. buf[0] != SSL_HS_SERVER_HELLO ||
  738. buf[4] != SSL_MAJOR_VERSION_3 )
  739. {
  740. SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  741. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  742. }
  743. if( buf[5] > ssl->max_minor_ver )
  744. {
  745. SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  746. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  747. }
  748. ssl->minor_ver = buf[5];
  749. if( ssl->minor_ver < ssl->min_minor_ver )
  750. {
  751. SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
  752. " [%d:%d] < [%d:%d]", ssl->major_ver,
  753. ssl->minor_ver, buf[4], buf[5] ) );
  754. ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
  755. SSL_ALERT_MSG_PROTOCOL_VERSION );
  756. return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
  757. }
  758. #if defined(POLARSSL_DEBUG_C)
  759. t = ( (uint32_t) buf[6] << 24 )
  760. | ( (uint32_t) buf[7] << 16 )
  761. | ( (uint32_t) buf[8] << 8 )
  762. | ( (uint32_t) buf[9] );
  763. SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
  764. #endif
  765. memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
  766. n = buf[38];
  767. SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
  768. if( n > 32 )
  769. {
  770. SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  771. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  772. }
  773. /*
  774. * 38 . 38 session id length
  775. * 39 . 38+n session id
  776. * 39+n . 40+n chosen ciphersuite
  777. * 41+n . 41+n chosen compression alg.
  778. * 42+n . 43+n extensions length
  779. * 44+n . 44+n+m extensions
  780. */
  781. if( ssl->in_hslen > 42 + n )
  782. {
  783. ext_len = ( ( buf[42 + n] << 8 )
  784. | ( buf[43 + n] ) );
  785. if( ( ext_len > 0 && ext_len < 4 ) ||
  786. ssl->in_hslen != 44 + n + ext_len )
  787. {
  788. SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  789. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  790. }
  791. }
  792. i = ( buf[39 + n] << 8 ) | buf[40 + n];
  793. comp = buf[41 + n];
  794. /*
  795. * Initialize update checksum functions
  796. */
  797. ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
  798. if( ssl->transform_negotiate->ciphersuite_info == NULL )
  799. {
  800. SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
  801. return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
  802. }
  803. ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
  804. SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
  805. SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
  806. /*
  807. * Check if the session can be resumed
  808. */
  809. if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
  810. ssl->handshake->resume == 0 || n == 0 ||
  811. ssl->session_negotiate->ciphersuite != i ||
  812. ssl->session_negotiate->compression != comp ||
  813. ssl->session_negotiate->length != n ||
  814. memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
  815. {
  816. ssl->state++;
  817. ssl->handshake->resume = 0;
  818. #if defined(POLARSSL_HAVE_TIME)
  819. ssl->session_negotiate->start = time( NULL );
  820. #endif
  821. ssl->session_negotiate->ciphersuite = i;
  822. ssl->session_negotiate->compression = comp;
  823. ssl->session_negotiate->length = n;
  824. memcpy( ssl->session_negotiate->id, buf + 39, n );
  825. }
  826. else
  827. {
  828. ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
  829. if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
  830. {
  831. SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
  832. return( ret );
  833. }
  834. }
  835. SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
  836. ssl->handshake->resume ? "a" : "no" ) );
  837. SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
  838. SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
  839. i = 0;
  840. while( 1 )
  841. {
  842. if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
  843. {
  844. SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  845. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  846. }
  847. if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
  848. ssl->session_negotiate->ciphersuite )
  849. {
  850. break;
  851. }
  852. }
  853. if( comp != SSL_COMPRESS_NULL
  854. #if defined(POLARSSL_ZLIB_SUPPORT)
  855. && comp != SSL_COMPRESS_DEFLATE
  856. #endif
  857. )
  858. {
  859. SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  860. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  861. }
  862. ssl->session_negotiate->compression = comp;
  863. ext = buf + 44 + n;
  864. SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
  865. while( ext_len )
  866. {
  867. unsigned int ext_id = ( ( ext[0] << 8 )
  868. | ( ext[1] ) );
  869. unsigned int ext_size = ( ( ext[2] << 8 )
  870. | ( ext[3] ) );
  871. if( ext_size + 4 > ext_len )
  872. {
  873. SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  874. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  875. }
  876. switch( ext_id )
  877. {
  878. case TLS_EXT_RENEGOTIATION_INFO:
  879. SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
  880. renegotiation_info_seen = 1;
  881. if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
  882. ext_size ) ) != 0 )
  883. return( ret );
  884. break;
  885. #if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
  886. case TLS_EXT_MAX_FRAGMENT_LENGTH:
  887. SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
  888. if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
  889. ext + 4, ext_size ) ) != 0 )
  890. {
  891. return( ret );
  892. }
  893. break;
  894. #endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
  895. #if defined(POLARSSL_SSL_TRUNCATED_HMAC)
  896. case TLS_EXT_TRUNCATED_HMAC:
  897. SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
  898. if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
  899. ext + 4, ext_size ) ) != 0 )
  900. {
  901. return( ret );
  902. }
  903. break;
  904. #endif /* POLARSSL_SSL_TRUNCATED_HMAC */
  905. #if defined(POLARSSL_SSL_SESSION_TICKETS)
  906. case TLS_EXT_SESSION_TICKET:
  907. SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
  908. if( ( ret = ssl_parse_session_ticket_ext( ssl,
  909. ext + 4, ext_size ) ) != 0 )
  910. {
  911. return( ret );
  912. }
  913. break;
  914. #endif /* POLARSSL_SSL_SESSION_TICKETS */
  915. #if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
  916. case TLS_EXT_SUPPORTED_POINT_FORMATS:
  917. SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
  918. if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
  919. ext + 4, ext_size ) ) != 0 )
  920. {
  921. return( ret );
  922. }
  923. break;
  924. #endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
  925. #if defined(POLARSSL_SSL_ALPN)
  926. case TLS_EXT_ALPN:
  927. SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
  928. if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
  929. return( ret );
  930. break;
  931. #endif /* POLARSSL_SSL_ALPN */
  932. default:
  933. SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
  934. ext_id ) );
  935. }
  936. ext_len -= 4 + ext_size;
  937. ext += 4 + ext_size;
  938. if( ext_len > 0 && ext_len < 4 )
  939. {
  940. SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
  941. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  942. }
  943. }
  944. /*
  945. * Renegotiation security checks
  946. */
  947. if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
  948. ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
  949. {
  950. SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
  951. handshake_failure = 1;
  952. }
  953. else if( ssl->renegotiation == SSL_RENEGOTIATION &&
  954. ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
  955. renegotiation_info_seen == 0 )
  956. {
  957. SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
  958. handshake_failure = 1;
  959. }
  960. else if( ssl->renegotiation == SSL_RENEGOTIATION &&
  961. ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
  962. ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
  963. {
  964. SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
  965. handshake_failure = 1;
  966. }
  967. else if( ssl->renegotiation == SSL_RENEGOTIATION &&
  968. ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
  969. renegotiation_info_seen == 1 )
  970. {
  971. SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
  972. handshake_failure = 1;
  973. }
  974. if( handshake_failure == 1 )
  975. {
  976. if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
  977. return( ret );
  978. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
  979. }
  980. SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
  981. return( 0 );
  982. }
  983. #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  984. defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
  985. static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
  986. unsigned char *end )
  987. {
  988. int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
  989. /*
  990. * Ephemeral DH parameters:
  991. *
  992. * struct {
  993. * opaque dh_p<1..2^16-1>;
  994. * opaque dh_g<1..2^16-1>;
  995. * opaque dh_Ys<1..2^16-1>;
  996. * } ServerDHParams;
  997. */
  998. if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
  999. {
  1000. SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
  1001. return( ret );
  1002. }
  1003. if( ssl->handshake->dhm_ctx.len < 64 ||
  1004. ssl->handshake->dhm_ctx.len > 512 )
  1005. {
  1006. SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
  1007. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1008. }
  1009. SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
  1010. SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
  1011. SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
  1012. return( ret );
  1013. }
  1014. #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1015. POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
  1016. #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1017. defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  1018. defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  1019. defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1020. defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1021. static int ssl_check_server_ecdh_params( const ssl_context *ssl )
  1022. {
  1023. const ecp_curve_info *curve_info;
  1024. curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
  1025. if( curve_info == NULL )
  1026. {
  1027. SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1028. return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
  1029. }
  1030. SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
  1031. #if defined(POLARSSL_SSL_ECP_SET_CURVES)
  1032. if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
  1033. #else
  1034. if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
  1035. ssl->handshake->ecdh_ctx.grp.nbits > 521 )
  1036. #endif
  1037. return( -1 );
  1038. SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
  1039. return( 0 );
  1040. }
  1041. #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1042. POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  1043. POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  1044. POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  1045. POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1046. #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1047. defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  1048. defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  1049. static int ssl_parse_server_ecdh_params( ssl_context *ssl,
  1050. unsigned char **p,
  1051. unsigned char *end )
  1052. {
  1053. int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
  1054. /*
  1055. * Ephemeral ECDH parameters:
  1056. *
  1057. * struct {
  1058. * ECParameters curve_params;
  1059. * ECPoint public;
  1060. * } ServerECDHParams;
  1061. */
  1062. if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
  1063. (const unsigned char **) p, end ) ) != 0 )
  1064. {
  1065. SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
  1066. return( ret );
  1067. }
  1068. if( ssl_check_server_ecdh_params( ssl ) != 0 )
  1069. {
  1070. SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
  1071. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1072. }
  1073. return( ret );
  1074. }
  1075. #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1076. POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  1077. POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  1078. #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1079. static int ssl_parse_server_psk_hint( ssl_context *ssl,
  1080. unsigned char **p,
  1081. unsigned char *end )
  1082. {
  1083. int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
  1084. size_t len;
  1085. ((void) ssl);
  1086. /*
  1087. * PSK parameters:
  1088. *
  1089. * opaque psk_identity_hint<0..2^16-1>;
  1090. */
  1091. len = (*p)[0] << 8 | (*p)[1];
  1092. *p += 2;
  1093. if( (*p) + len > end )
  1094. {
  1095. SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
  1096. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1097. }
  1098. // TODO: Retrieve PSK identity hint and callback to app
  1099. //
  1100. *p += len;
  1101. ret = 0;
  1102. return( ret );
  1103. }
  1104. #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
  1105. #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
  1106. defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
  1107. /*
  1108. * Generate a pre-master secret and encrypt it with the server's RSA key
  1109. */
  1110. static int ssl_write_encrypted_pms( ssl_context *ssl,
  1111. size_t offset, size_t *olen,
  1112. size_t pms_offset )
  1113. {
  1114. int ret;
  1115. size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
  1116. unsigned char *p = ssl->handshake->premaster + pms_offset;
  1117. /*
  1118. * Generate (part of) the pre-master as
  1119. * struct {
  1120. * ProtocolVersion client_version;
  1121. * opaque random[46];
  1122. * } PreMasterSecret;
  1123. */
  1124. p[0] = (unsigned char) ssl->max_major_ver;
  1125. p[1] = (unsigned char) ssl->max_minor_ver;
  1126. if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
  1127. {
  1128. SSL_DEBUG_RET( 1, "f_rng", ret );
  1129. return( ret );
  1130. }
  1131. ssl->handshake->pmslen = 48;
  1132. /*
  1133. * Now write it out, encrypted
  1134. */
  1135. if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
  1136. POLARSSL_PK_RSA ) )
  1137. {
  1138. SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
  1139. return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
  1140. }
  1141. if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
  1142. p, ssl->handshake->pmslen,
  1143. ssl->out_msg + offset + len_bytes, olen,
  1144. SSL_MAX_CONTENT_LEN - offset - len_bytes,
  1145. ssl->f_rng, ssl->p_rng ) ) != 0 )
  1146. {
  1147. SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
  1148. return( ret );
  1149. }
  1150. #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
  1151. defined(POLARSSL_SSL_PROTO_TLS1_2)
  1152. if( len_bytes == 2 )
  1153. {
  1154. ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
  1155. ssl->out_msg[offset+1] = (unsigned char)( *olen );
  1156. *olen += 2;
  1157. }
  1158. #endif
  1159. return( 0 );
  1160. }
  1161. #endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
  1162. POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
  1163. #if defined(POLARSSL_SSL_PROTO_TLS1_2)
  1164. #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1165. defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1166. defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1167. static int ssl_parse_signature_algorithm( ssl_context *ssl,
  1168. unsigned char **p,
  1169. unsigned char *end,
  1170. md_type_t *md_alg,
  1171. pk_type_t *pk_alg )
  1172. {
  1173. ((void) ssl);
  1174. *md_alg = POLARSSL_MD_NONE;
  1175. *pk_alg = POLARSSL_PK_NONE;
  1176. /* Only in TLS 1.2 */
  1177. if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
  1178. {
  1179. return( 0 );
  1180. }
  1181. if( (*p) + 2 > end )
  1182. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1183. /*
  1184. * Get hash algorithm
  1185. */
  1186. if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
  1187. {
  1188. SSL_DEBUG_MSG( 2, ( "Server used unsupported "
  1189. "HashAlgorithm %d", *(p)[0] ) );
  1190. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1191. }
  1192. /*
  1193. * Get signature algorithm
  1194. */
  1195. if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
  1196. {
  1197. SSL_DEBUG_MSG( 2, ( "server used unsupported "
  1198. "SignatureAlgorithm %d", (*p)[1] ) );
  1199. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1200. }
  1201. SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
  1202. SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
  1203. *p += 2;
  1204. return( 0 );
  1205. }
  1206. #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1207. POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1208. POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  1209. #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
  1210. #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1211. defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1212. static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
  1213. {
  1214. int ret;
  1215. const ecp_keypair *peer_key;
  1216. if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
  1217. POLARSSL_PK_ECKEY ) )
  1218. {
  1219. SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
  1220. return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
  1221. }
  1222. peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
  1223. if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
  1224. POLARSSL_ECDH_THEIRS ) ) != 0 )
  1225. {
  1226. SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
  1227. return( ret );
  1228. }
  1229. if( ssl_check_server_ecdh_params( ssl ) != 0 )
  1230. {
  1231. SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
  1232. return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
  1233. }
  1234. return( ret );
  1235. }
  1236. #endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
  1237. POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1238. static int ssl_parse_server_key_exchange( ssl_context *ssl )
  1239. {
  1240. int ret;
  1241. const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  1242. unsigned char *p, *end;
  1243. #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1244. defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1245. defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1246. size_t sig_len, params_len;
  1247. unsigned char hash[64];
  1248. md_type_t md_alg = POLARSSL_MD_NONE;
  1249. size_t hashlen;
  1250. pk_type_t pk_alg = POLARSSL_PK_NONE;
  1251. #endif
  1252. SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
  1253. #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
  1254. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
  1255. {
  1256. SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
  1257. ssl->state++;
  1258. return( 0 );
  1259. }
  1260. ((void) p);
  1261. ((void) end);
  1262. #endif
  1263. #if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1264. defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1265. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
  1266. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
  1267. {
  1268. if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
  1269. {
  1270. SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
  1271. return( ret );
  1272. }
  1273. SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
  1274. ssl->state++;
  1275. return( 0 );
  1276. }
  1277. ((void) p);
  1278. ((void) end);
  1279. #endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  1280. POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1281. if( ( ret = ssl_read_record( ssl ) ) != 0 )
  1282. {
  1283. SSL_DEBUG_RET( 1, "ssl_read_record", ret );
  1284. return( ret );
  1285. }
  1286. if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
  1287. {
  1288. SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1289. return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
  1290. }
  1291. /*
  1292. * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
  1293. * doesn't use a psk_identity_hint
  1294. */
  1295. if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
  1296. {
  1297. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
  1298. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
  1299. {
  1300. ssl->record_read = 1;
  1301. goto exit;
  1302. }
  1303. SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1304. return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
  1305. }
  1306. p = ssl->in_msg + 4;
  1307. end = ssl->in_msg + ssl->in_hslen;
  1308. SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
  1309. #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1310. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
  1311. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
  1312. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
  1313. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
  1314. {
  1315. if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
  1316. {
  1317. SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1318. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1319. }
  1320. } /* FALLTROUGH */
  1321. #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
  1322. #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
  1323. defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
  1324. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
  1325. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
  1326. ; /* nothing more to do */
  1327. else
  1328. #endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
  1329. POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
  1330. #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1331. defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
  1332. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
  1333. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
  1334. {
  1335. if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
  1336. {
  1337. SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1338. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1339. }
  1340. }
  1341. else
  1342. #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1343. POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
  1344. #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1345. defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
  1346. defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1347. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
  1348. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
  1349. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
  1350. {
  1351. if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
  1352. {
  1353. SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1354. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1355. }
  1356. }
  1357. else
  1358. #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1359. POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
  1360. POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  1361. {
  1362. SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1363. return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
  1364. }
  1365. #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
  1366. defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1367. defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1368. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
  1369. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
  1370. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
  1371. {
  1372. params_len = p - ( ssl->in_msg + 4 );
  1373. /*
  1374. * Handle the digitally-signed structure
  1375. */
  1376. #if defined(POLARSSL_SSL_PROTO_TLS1_2)
  1377. if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
  1378. {
  1379. if( ssl_parse_signature_algorithm( ssl, &p, end,
  1380. &md_alg, &pk_alg ) != 0 )
  1381. {
  1382. SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1383. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1384. }
  1385. if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
  1386. {
  1387. SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1388. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1389. }
  1390. }
  1391. else
  1392. #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
  1393. #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
  1394. defined(POLARSSL_SSL_PROTO_TLS1_1)
  1395. if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
  1396. {
  1397. pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
  1398. /* Default hash for ECDSA is SHA-1 */
  1399. if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
  1400. md_alg = POLARSSL_MD_SHA1;
  1401. }
  1402. else
  1403. #endif
  1404. {
  1405. SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1406. return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
  1407. }
  1408. /*
  1409. * Read signature
  1410. */
  1411. sig_len = ( p[0] << 8 ) | p[1];
  1412. p += 2;
  1413. if( end != p + sig_len )
  1414. {
  1415. SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1416. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
  1417. }
  1418. SSL_DEBUG_BUF( 3, "signature", p, sig_len );
  1419. /*
  1420. * Compute the hash that has been signed
  1421. */
  1422. #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
  1423. defined(POLARSSL_SSL_PROTO_TLS1_1)
  1424. if( md_alg == POLARSSL_MD_NONE )
  1425. {
  1426. md5_context md5;
  1427. sha1_context sha1;
  1428. md5_init( &md5 );
  1429. sha1_init( &sha1 );
  1430. hashlen = 36;
  1431. /*
  1432. * digitally-signed struct {
  1433. * opaque md5_hash[16];
  1434. * opaque sha_hash[20];
  1435. * };
  1436. *
  1437. * md5_hash
  1438. * MD5(ClientHello.random + ServerHello.random
  1439. * + ServerParams);
  1440. * sha_hash
  1441. * SHA(ClientHello.random + ServerHello.random
  1442. * + ServerParams);
  1443. */
  1444. md5_starts( &md5 );
  1445. md5_update( &md5, ssl->handshake->randbytes, 64 );
  1446. md5_update( &md5, ssl->in_msg + 4, params_len );
  1447. md5_finish( &md5, hash );
  1448. sha1_starts( &sha1 );
  1449. sha1_update( &sha1, ssl->handshake->randbytes, 64 );
  1450. sha1_update( &sha1, ssl->in_msg + 4, params_len );
  1451. sha1_finish( &sha1, hash + 16 );
  1452. md5_free( &md5 );
  1453. sha1_free( &sha1 );
  1454. }
  1455. else
  1456. #endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
  1457. POLARSSL_SSL_PROTO_TLS1_1 */
  1458. #if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
  1459. defined(POLARSSL_SSL_PROTO_TLS1_2)
  1460. if( md_alg != POLARSSL_MD_NONE )
  1461. {
  1462. md_context_t ctx;
  1463. md_init( &ctx );
  1464. /* Info from md_alg will be used instead */
  1465. hashlen = 0;
  1466. /*
  1467. * digitally-signed struct {
  1468. * opaque client_random[32];
  1469. * opaque server_random[32];
  1470. * ServerDHParams params;
  1471. * };
  1472. */
  1473. if( ( ret = md_init_ctx( &ctx,
  1474. md_info_from_type( md_alg ) ) ) != 0 )
  1475. {
  1476. SSL_DEBUG_RET( 1, "md_init_ctx", ret );
  1477. return( ret );
  1478. }
  1479. md_starts( &ctx );
  1480. md_update( &ctx, ssl->handshake->randbytes, 64 );
  1481. md_update( &ctx, ssl->in_msg + 4, params_len );
  1482. md_finish( &ctx, hash );
  1483. md_free( &ctx );
  1484. }
  1485. else
  1486. #endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
  1487. POLARSSL_SSL_PROTO_TLS1_2 */
  1488. {
  1489. SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1490. return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
  1491. }
  1492. SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
  1493. (unsigned int) ( md_info_from_type( md_alg ) )->size );
  1494. /*
  1495. * Verify signature
  1496. */
  1497. if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
  1498. {
  1499. SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
  1500. return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
  1501. }
  1502. if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
  1503. md_alg, hash, hashlen, p, sig_len ) ) != 0 )
  1504. {
  1505. SSL_DEBUG_RET( 1, "pk_verify", ret );
  1506. return( ret );
  1507. }
  1508. }
  1509. #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
  1510. POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1511. POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  1512. exit:
  1513. ssl->state++;
  1514. SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
  1515. return( 0 );
  1516. }
  1517. #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
  1518. !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
  1519. !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
  1520. !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1521. static int ssl_parse_certificate_request( ssl_context *ssl )
  1522. {
  1523. const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  1524. SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
  1525. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
  1526. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
  1527. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
  1528. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
  1529. {
  1530. SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
  1531. ssl->state++;
  1532. return( 0 );
  1533. }
  1534. SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1535. return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
  1536. }
  1537. #else
  1538. static int ssl_parse_certificate_request( ssl_context *ssl )
  1539. {
  1540. int ret;
  1541. unsigned char *buf, *p;
  1542. size_t n = 0, m = 0;
  1543. size_t cert_type_len = 0, dn_len = 0;
  1544. const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  1545. SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
  1546. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
  1547. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
  1548. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
  1549. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
  1550. {
  1551. SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
  1552. ssl->state++;
  1553. return( 0 );
  1554. }
  1555. /*
  1556. * 0 . 0 handshake type
  1557. * 1 . 3 handshake length
  1558. * 4 . 4 cert type count
  1559. * 5 .. m-1 cert types
  1560. * m .. m+1 sig alg length (TLS 1.2 only)
  1561. * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
  1562. * n .. n+1 length of all DNs
  1563. * n+2 .. n+3 length of DN 1
  1564. * n+4 .. ... Distinguished Name #1
  1565. * ... .. ... length of DN 2, etc.
  1566. */
  1567. if( ssl->record_read == 0 )
  1568. {
  1569. if( ( ret = ssl_read_record( ssl ) ) != 0 )
  1570. {
  1571. SSL_DEBUG_RET( 1, "ssl_read_record", ret );
  1572. return( ret );
  1573. }
  1574. if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
  1575. {
  1576. SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  1577. return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
  1578. }
  1579. ssl->record_read = 1;
  1580. }
  1581. ssl->client_auth = 0;
  1582. ssl->state++;
  1583. if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
  1584. ssl->client_auth++;
  1585. SSL_DEBUG_MSG( 3, ( "got %s certificate request",
  1586. ssl->client_auth ? "a" : "no" ) );
  1587. if( ssl->client_auth == 0 )
  1588. goto exit;
  1589. ssl->record_read = 0;
  1590. // TODO: handshake_failure alert for an anonymous server to request
  1591. // client authentication
  1592. buf = ssl->in_msg;
  1593. // Retrieve cert types
  1594. //
  1595. cert_type_len = buf[4];
  1596. n = cert_type_len;
  1597. if( ssl->in_hslen < 6 + n )
  1598. {
  1599. SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  1600. return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  1601. }
  1602. p = buf + 5;
  1603. while( cert_type_len > 0 )
  1604. {
  1605. #if defined(POLARSSL_RSA_C)
  1606. if( *p == SSL_CERT_TYPE_RSA_SIGN &&
  1607. pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
  1608. {
  1609. ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
  1610. break;
  1611. }
  1612. else
  1613. #endif
  1614. #if defined(POLARSSL_ECDSA_C)
  1615. if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
  1616. pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
  1617. {
  1618. ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
  1619. break;
  1620. }
  1621. else
  1622. #endif
  1623. {
  1624. ; /* Unsupported cert type, ignore */
  1625. }
  1626. cert_type_len--;
  1627. p++;
  1628. }
  1629. #if defined(POLARSSL_SSL_PROTO_TLS1_2)
  1630. if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
  1631. {
  1632. /* Ignored, see comments about hash in write_certificate_verify */
  1633. // TODO: should check the signature part against our pk_key though
  1634. size_t sig_alg_len = ( ( buf[5 + n] << 8 )
  1635. | ( buf[6 + n] ) );
  1636. p = buf + 7 + n;
  1637. m += 2;
  1638. n += sig_alg_len;
  1639. if( ssl->in_hslen < 6 + n )
  1640. {
  1641. SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  1642. return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  1643. }
  1644. }
  1645. #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
  1646. /* Ignore certificate_authorities, we only have one cert anyway */
  1647. // TODO: should not send cert if no CA matches
  1648. dn_len = ( ( buf[5 + m + n] << 8 )
  1649. | ( buf[6 + m + n] ) );
  1650. n += dn_len;
  1651. if( ssl->in_hslen != 7 + m + n )
  1652. {
  1653. SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
  1654. return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
  1655. }
  1656. exit:
  1657. SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
  1658. return( 0 );
  1659. }
  1660. #endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
  1661. !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
  1662. !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
  1663. !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  1664. static int ssl_parse_server_hello_done( ssl_context *ssl )
  1665. {
  1666. int ret;
  1667. SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
  1668. if( ssl->record_read == 0 )
  1669. {
  1670. if( ( ret = ssl_read_record( ssl ) ) != 0 )
  1671. {
  1672. SSL_DEBUG_RET( 1, "ssl_read_record", ret );
  1673. return( ret );
  1674. }
  1675. if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
  1676. {
  1677. SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
  1678. return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
  1679. }
  1680. }
  1681. ssl->record_read = 0;
  1682. if( ssl->in_hslen != 4 ||
  1683. ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
  1684. {
  1685. SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
  1686. return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
  1687. }
  1688. ssl->state++;
  1689. SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
  1690. return( 0 );
  1691. }
  1692. static int ssl_write_client_key_exchange( ssl_context *ssl )
  1693. {
  1694. int ret;
  1695. size_t i, n;
  1696. const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  1697. SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
  1698. #if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
  1699. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
  1700. {
  1701. /*
  1702. * DHM key exchange -- send G^X mod P
  1703. */
  1704. n = ssl->handshake->dhm_ctx.len;
  1705. ssl->out_msg[4] = (unsigned char)( n >> 8 );
  1706. ssl->out_msg[5] = (unsigned char)( n );
  1707. i = 6;
  1708. ret = dhm_make_public( &ssl->handshake->dhm_ctx,
  1709. (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
  1710. &ssl->out_msg[i], n,
  1711. ssl->f_rng, ssl->p_rng );
  1712. if( ret != 0 )
  1713. {
  1714. SSL_DEBUG_RET( 1, "dhm_make_public", ret );
  1715. return( ret );
  1716. }
  1717. SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
  1718. SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
  1719. ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
  1720. if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
  1721. ssl->handshake->premaster,
  1722. &ssl->handshake->pmslen,
  1723. ssl->f_rng, ssl->p_rng ) ) != 0 )
  1724. {
  1725. SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
  1726. return( ret );
  1727. }
  1728. SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
  1729. }
  1730. else
  1731. #endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
  1732. #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  1733. defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  1734. defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  1735. defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  1736. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
  1737. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
  1738. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
  1739. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
  1740. {
  1741. /*
  1742. * ECDH key exchange -- send client public value
  1743. */
  1744. i = 4;
  1745. ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
  1746. &n,
  1747. &ssl->out_msg[i], 1000,
  1748. ssl->f_rng, ssl->p_rng );
  1749. if( ret != 0 )
  1750. {
  1751. SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
  1752. return( ret );
  1753. }
  1754. SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
  1755. if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
  1756. &ssl->handshake->pmslen,
  1757. ssl->handshake->premaster,
  1758. POLARSSL_MPI_MAX_SIZE,
  1759. ssl->f_rng, ssl->p_rng ) ) != 0 )
  1760. {
  1761. SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
  1762. return( ret );
  1763. }
  1764. SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
  1765. }
  1766. else
  1767. #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
  1768. POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
  1769. POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
  1770. POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
  1771. #if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1772. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
  1773. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
  1774. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
  1775. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
  1776. {
  1777. /*
  1778. * opaque psk_identity<0..2^16-1>;
  1779. */
  1780. if( ssl->psk == NULL || ssl->psk_identity == NULL )
  1781. return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
  1782. i = 4;
  1783. n = ssl->psk_identity_len;
  1784. ssl->out_msg[i++] = (unsigned char)( n >> 8 );
  1785. ssl->out_msg[i++] = (unsigned char)( n );
  1786. memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
  1787. i += ssl->psk_identity_len;
  1788. #if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
  1789. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
  1790. {
  1791. n = 0;
  1792. }
  1793. else
  1794. #endif
  1795. #if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
  1796. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
  1797. {
  1798. if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
  1799. return( ret );
  1800. }
  1801. else
  1802. #endif
  1803. #if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
  1804. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
  1805. {
  1806. /*
  1807. * ClientDiffieHellmanPublic public (DHM send G^X mod P)
  1808. */
  1809. n = ssl->handshake->dhm_ctx.len;
  1810. ssl->out_msg[i++] = (unsigned char)( n >> 8 );
  1811. ssl->out_msg[i++] = (unsigned char)( n );
  1812. ret = dhm_make_public( &ssl->handshake->dhm_ctx,
  1813. (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
  1814. &ssl->out_msg[i], n,
  1815. ssl->f_rng, ssl->p_rng );
  1816. if( ret != 0 )
  1817. {
  1818. SSL_DEBUG_RET( 1, "dhm_make_public", ret );
  1819. return( ret );
  1820. }
  1821. }
  1822. else
  1823. #endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
  1824. #if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  1825. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
  1826. {
  1827. /*
  1828. * ClientECDiffieHellmanPublic public;
  1829. */
  1830. ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
  1831. &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
  1832. ssl->f_rng, ssl->p_rng );
  1833. if( ret != 0 )
  1834. {
  1835. SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
  1836. return( ret );
  1837. }
  1838. SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
  1839. }
  1840. else
  1841. #endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
  1842. {
  1843. SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1844. return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
  1845. }
  1846. if( ( ret = ssl_psk_derive_premaster( ssl,
  1847. ciphersuite_info->key_exchange ) ) != 0 )
  1848. {
  1849. SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
  1850. return( ret );
  1851. }
  1852. }
  1853. else
  1854. #endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
  1855. #if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
  1856. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
  1857. {
  1858. i = 4;
  1859. if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
  1860. return( ret );
  1861. }
  1862. else
  1863. #endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
  1864. {
  1865. ((void) ciphersuite_info);
  1866. SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1867. return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
  1868. }
  1869. if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
  1870. {
  1871. SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
  1872. return( ret );
  1873. }
  1874. ssl->out_msglen = i + n;
  1875. ssl->out_msgtype = SSL_MSG_HANDSHAKE;
  1876. ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
  1877. ssl->state++;
  1878. if( ( ret = ssl_write_record( ssl ) ) != 0 )
  1879. {
  1880. SSL_DEBUG_RET( 1, "ssl_write_record", ret );
  1881. return( ret );
  1882. }
  1883. SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
  1884. return( 0 );
  1885. }
  1886. #if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
  1887. !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
  1888. !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
  1889. !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  1890. static int ssl_write_certificate_verify( ssl_context *ssl )
  1891. {
  1892. const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  1893. SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
  1894. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
  1895. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
  1896. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
  1897. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
  1898. {
  1899. SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  1900. ssl->state++;
  1901. return( 0 );
  1902. }
  1903. SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  1904. return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
  1905. }
  1906. #else
  1907. static int ssl_write_certificate_verify( ssl_context *ssl )
  1908. {
  1909. int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
  1910. const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
  1911. size_t n = 0, offset = 0;
  1912. unsigned char hash[48];
  1913. unsigned char *hash_start = hash;
  1914. md_type_t md_alg = POLARSSL_MD_NONE;
  1915. unsigned int hashlen;
  1916. SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
  1917. if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
  1918. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
  1919. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
  1920. ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
  1921. {
  1922. SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  1923. ssl->state++;
  1924. return( 0 );
  1925. }
  1926. if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
  1927. {
  1928. SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
  1929. ssl->state++;
  1930. return( 0 );
  1931. }
  1932. if( ssl_own_key( ssl ) == NULL )
  1933. {
  1934. SSL_DEBUG_MSG( 1, ( "got no private key" ) );
  1935. return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
  1936. }
  1937. /*
  1938. * Make an RSA signature of the handshake digests
  1939. */
  1940. ssl->handshake->calc_verify( ssl, hash );
  1941. #if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
  1942. defined(POLARSSL_SSL_PROTO_TLS1_1)
  1943. if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
  1944. {
  1945. /*
  1946. * digitally-signed struct {
  1947. * opaque md5_hash[16];
  1948. * opaque sha_hash[20];
  1949. * };
  1950. *
  1951. * md5_hash
  1952. * MD5(handshake_messages);
  1953. *
  1954. * sha_hash
  1955. * SHA(handshake_messages);
  1956. */
  1957. hashlen = 36;
  1958. md_alg = POLARSSL_MD_NONE;
  1959. /*
  1960. * For ECDSA, default hash is SHA-1 only
  1961. */
  1962. if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
  1963. {
  1964. hash_start += 16;
  1965. hashlen -= 16;
  1966. md_alg = POLARSSL_MD_SHA1;
  1967. }
  1968. }
  1969. else
  1970. #endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
  1971. POLARSSL_SSL_PROTO_TLS1_1 */
  1972. #if defined(POLARSSL_SSL_PROTO_TLS1_2)
  1973. if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
  1974. {
  1975. /*
  1976. * digitally-signed struct {
  1977. * opaque handshake_messages[handshake_messages_length];
  1978. * };
  1979. *
  1980. * Taking shortcut here. We assume that the server always allows the
  1981. * PRF Hash function and has sent it in the allowed signature
  1982. * algorithms list received in the Certificate Request message.
  1983. *
  1984. * Until we encounter a server that does not, we will take this
  1985. * shortcut.
  1986. *
  1987. * Reason: Otherwise we should have running hashes for SHA512 and SHA224
  1988. * in order to satisfy 'weird' needs from the server side.
  1989. */
  1990. if( ssl->transform_negotiate->ciphersuite_info->mac ==
  1991. POLARSSL_MD_SHA384 )
  1992. {
  1993. md_alg = POLARSSL_MD_SHA384;
  1994. ssl->out_msg[4] = SSL_HASH_SHA384;
  1995. }
  1996. else
  1997. {
  1998. md_alg = POLARSSL_MD_SHA256;
  1999. ssl->out_msg[4] = SSL_HASH_SHA256;
  2000. }
  2001. ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
  2002. /* Info from md_alg will be used instead */
  2003. hashlen = 0;
  2004. offset = 2;
  2005. }
  2006. else
  2007. #endif /* POLARSSL_SSL_PROTO_TLS1_2 */
  2008. {
  2009. SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  2010. return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
  2011. }
  2012. if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
  2013. ssl->out_msg + 6 + offset, &n,
  2014. ssl->f_rng, ssl->p_rng ) ) != 0 )
  2015. {
  2016. SSL_DEBUG_RET( 1, "pk_sign", ret );
  2017. return( ret );
  2018. }
  2019. ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
  2020. ssl->out_msg[5 + offset] = (unsigned char)( n );
  2021. ssl->out_msglen = 6 + n + offset;
  2022. ssl->out_msgtype = SSL_MSG_HANDSHAKE;
  2023. ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
  2024. ssl->state++;
  2025. if( ( ret = ssl_write_record( ssl ) ) != 0 )
  2026. {
  2027. SSL_DEBUG_RET( 1, "ssl_write_record", ret );
  2028. return( ret );
  2029. }
  2030. SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
  2031. return( ret );
  2032. }
  2033. #endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
  2034. !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
  2035. !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
  2036. #if defined(POLARSSL_SSL_SESSION_TICKETS)
  2037. static int ssl_parse_new_session_ticket( ssl_context *ssl )
  2038. {
  2039. int ret;
  2040. uint32_t lifetime;
  2041. size_t ticket_len;
  2042. unsigned char *ticket;
  2043. SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
  2044. if( ( ret = ssl_read_record( ssl ) ) != 0 )
  2045. {
  2046. SSL_DEBUG_RET( 1, "ssl_read_record", ret );
  2047. return( ret );
  2048. }
  2049. if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
  2050. {
  2051. SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  2052. return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
  2053. }
  2054. /*
  2055. * struct {
  2056. * uint32 ticket_lifetime_hint;
  2057. * opaque ticket<0..2^16-1>;
  2058. * } NewSessionTicket;
  2059. *
  2060. * 0 . 0 handshake message type
  2061. * 1 . 3 handshake message length
  2062. * 4 . 7 ticket_lifetime_hint
  2063. * 8 . 9 ticket_len (n)
  2064. * 10 . 9+n ticket content
  2065. */
  2066. if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
  2067. ssl->in_hslen < 10 )
  2068. {
  2069. SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  2070. return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
  2071. }
  2072. lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
  2073. ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
  2074. ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
  2075. if( ticket_len + 10 != ssl->in_hslen )
  2076. {
  2077. SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
  2078. return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
  2079. }
  2080. SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
  2081. /* We're not waiting for a NewSessionTicket message any more */
  2082. ssl->handshake->new_session_ticket = 0;
  2083. /*
  2084. * Zero-length ticket means the server changed his mind and doesn't want
  2085. * to send a ticket after all, so just forget it
  2086. */
  2087. if( ticket_len == 0 )
  2088. return( 0 );
  2089. polarssl_zeroize( ssl->session_negotiate->ticket,
  2090. ssl->session_negotiate->ticket_len );
  2091. polarssl_free( ssl->session_negotiate->ticket );
  2092. ssl->session_negotiate->ticket = NULL;
  2093. ssl->session_negotiate->ticket_len = 0;
  2094. if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
  2095. {
  2096. SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
  2097. return( POLARSSL_ERR_SSL_MALLOC_FAILED );
  2098. }
  2099. memcpy( ticket, ssl->in_msg + 10, ticket_len );
  2100. ssl->session_negotiate->ticket = ticket;
  2101. ssl->session_negotiate->ticket_len = ticket_len;
  2102. ssl->session_negotiate->ticket_lifetime = lifetime;
  2103. /*
  2104. * RFC 5077 section 3.4:
  2105. * "If the client receives a session ticket from the server, then it
  2106. * discards any Session ID that was sent in the ServerHello."
  2107. */
  2108. SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
  2109. ssl->session_negotiate->length = 0;
  2110. SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
  2111. return( 0 );
  2112. }
  2113. #endif /* POLARSSL_SSL_SESSION_TICKETS */
  2114. /*
  2115. * SSL handshake -- client side -- single step
  2116. */
  2117. int ssl_handshake_client_step( ssl_context *ssl )
  2118. {
  2119. int ret = 0;
  2120. if( ssl->state == SSL_HANDSHAKE_OVER )
  2121. return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
  2122. SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
  2123. if( ( ret = ssl_flush_output( ssl ) ) != 0 )
  2124. return( ret );
  2125. switch( ssl->state )
  2126. {
  2127. case SSL_HELLO_REQUEST:
  2128. ssl->state = SSL_CLIENT_HELLO;
  2129. break;
  2130. /*
  2131. * ==> ClientHello
  2132. */
  2133. case SSL_CLIENT_HELLO:
  2134. ret = ssl_write_client_hello( ssl );
  2135. break;
  2136. /*
  2137. * <== ServerHello
  2138. * Certificate
  2139. * ( ServerKeyExchange )
  2140. * ( CertificateRequest )
  2141. * ServerHelloDone
  2142. */
  2143. case SSL_SERVER_HELLO:
  2144. ret = ssl_parse_server_hello( ssl );
  2145. break;
  2146. case SSL_SERVER_CERTIFICATE:
  2147. ret = ssl_parse_certificate( ssl );
  2148. break;
  2149. case SSL_SERVER_KEY_EXCHANGE:
  2150. ret = ssl_parse_server_key_exchange( ssl );
  2151. break;
  2152. case SSL_CERTIFICATE_REQUEST:
  2153. ret = ssl_parse_certificate_request( ssl );
  2154. break;
  2155. case SSL_SERVER_HELLO_DONE:
  2156. ret = ssl_parse_server_hello_done( ssl );
  2157. break;
  2158. /*
  2159. * ==> ( Certificate/Alert )
  2160. * ClientKeyExchange
  2161. * ( CertificateVerify )
  2162. * ChangeCipherSpec
  2163. * Finished
  2164. */
  2165. case SSL_CLIENT_CERTIFICATE:
  2166. ret = ssl_write_certificate( ssl );
  2167. break;
  2168. case SSL_CLIENT_KEY_EXCHANGE:
  2169. ret = ssl_write_client_key_exchange( ssl );
  2170. break;
  2171. case SSL_CERTIFICATE_VERIFY:
  2172. ret = ssl_write_certificate_verify( ssl );
  2173. break;
  2174. case SSL_CLIENT_CHANGE_CIPHER_SPEC:
  2175. ret = ssl_write_change_cipher_spec( ssl );
  2176. break;
  2177. case SSL_CLIENT_FINISHED:
  2178. ret = ssl_write_finished( ssl );
  2179. break;
  2180. /*
  2181. * <== ( NewSessionTicket )
  2182. * ChangeCipherSpec
  2183. * Finished
  2184. */
  2185. case SSL_SERVER_CHANGE_CIPHER_SPEC:
  2186. #if defined(POLARSSL_SSL_SESSION_TICKETS)
  2187. if( ssl->handshake->new_session_ticket != 0 )
  2188. ret = ssl_parse_new_session_ticket( ssl );
  2189. else
  2190. #endif
  2191. ret = ssl_parse_change_cipher_spec( ssl );
  2192. break;
  2193. case SSL_SERVER_FINISHED:
  2194. ret = ssl_parse_finished( ssl );
  2195. break;
  2196. case SSL_FLUSH_BUFFERS:
  2197. SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
  2198. ssl->state = SSL_HANDSHAKE_WRAPUP;
  2199. break;
  2200. case SSL_HANDSHAKE_WRAPUP:
  2201. ssl_handshake_wrapup( ssl );
  2202. break;
  2203. default:
  2204. SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
  2205. return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
  2206. }
  2207. return( ret );
  2208. }
  2209. #endif /* POLARSSL_SSL_CLI_C */