handshake_client.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. *
  113. * Portions of the attached software ("Contribution") are developed by
  114. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  115. *
  116. * The Contribution is licensed pursuant to the OpenSSL open source
  117. * license provided above.
  118. *
  119. * ECC cipher suite support in OpenSSL originally written by
  120. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  121. *
  122. */
  123. /* ====================================================================
  124. * Copyright 2005 Nokia. All rights reserved.
  125. *
  126. * The portions of the attached software ("Contribution") is developed by
  127. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  128. * license.
  129. *
  130. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  131. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  132. * support (see RFC 4279) to OpenSSL.
  133. *
  134. * No patent licenses or other rights except those expressly stated in
  135. * the OpenSSL open source license shall be deemed granted or received
  136. * expressly, by implication, estoppel, or otherwise.
  137. *
  138. * No assurances are provided by Nokia that the Contribution does not
  139. * infringe the patent or other intellectual property rights of any third
  140. * party or that the license provides you with all the necessary rights
  141. * to make use of the Contribution.
  142. *
  143. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  144. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  145. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  146. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  147. * OTHERWISE.
  148. */
  149. #include <openssl/ssl.h>
  150. #include <assert.h>
  151. #include <string.h>
  152. #include <openssl/aead.h>
  153. #include <openssl/bn.h>
  154. #include <openssl/buf.h>
  155. #include <openssl/bytestring.h>
  156. #include <openssl/dh.h>
  157. #include <openssl/ec_key.h>
  158. #include <openssl/ecdsa.h>
  159. #include <openssl/err.h>
  160. #include <openssl/evp.h>
  161. #include <openssl/md5.h>
  162. #include <openssl/mem.h>
  163. #include <openssl/rand.h>
  164. #include <openssl/x509.h>
  165. #include <openssl/x509v3.h>
  166. #include "../crypto/internal.h"
  167. #include "internal.h"
  168. static int ssl3_send_client_hello(SSL_HANDSHAKE *hs);
  169. static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs);
  170. static int ssl3_get_server_hello(SSL_HANDSHAKE *hs);
  171. static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs);
  172. static int ssl3_get_cert_status(SSL_HANDSHAKE *hs);
  173. static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs);
  174. static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs);
  175. static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs);
  176. static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs);
  177. static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs);
  178. static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs);
  179. static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs);
  180. static int ssl3_send_next_proto(SSL_HANDSHAKE *hs);
  181. static int ssl3_send_channel_id(SSL_HANDSHAKE *hs);
  182. static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs);
  183. int ssl3_connect(SSL_HANDSHAKE *hs) {
  184. SSL *const ssl = hs->ssl;
  185. int ret = -1;
  186. assert(ssl->handshake_func == ssl3_connect);
  187. assert(!ssl->server);
  188. for (;;) {
  189. int state = hs->state;
  190. switch (hs->state) {
  191. case SSL_ST_INIT:
  192. ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
  193. hs->state = SSL3_ST_CW_CLNT_HELLO_A;
  194. break;
  195. case SSL3_ST_CW_CLNT_HELLO_A:
  196. ret = ssl3_send_client_hello(hs);
  197. if (ret <= 0) {
  198. goto end;
  199. }
  200. if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
  201. hs->next_state = SSL3_ST_CR_SRVR_HELLO_A;
  202. } else {
  203. hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
  204. }
  205. hs->state = SSL3_ST_CW_FLUSH;
  206. break;
  207. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
  208. assert(SSL_is_dtls(ssl));
  209. ret = dtls1_get_hello_verify(hs);
  210. if (ret <= 0) {
  211. goto end;
  212. }
  213. if (ssl->d1->send_cookie) {
  214. ssl->method->received_flight(ssl);
  215. hs->state = SSL3_ST_CW_CLNT_HELLO_A;
  216. } else {
  217. hs->state = SSL3_ST_CR_SRVR_HELLO_A;
  218. }
  219. break;
  220. case SSL3_ST_CR_SRVR_HELLO_A:
  221. ret = ssl3_get_server_hello(hs);
  222. if (hs->state == SSL_ST_TLS13) {
  223. break;
  224. }
  225. if (ret <= 0) {
  226. goto end;
  227. }
  228. if (ssl->session != NULL) {
  229. hs->state = SSL3_ST_CR_SESSION_TICKET_A;
  230. } else {
  231. hs->state = SSL3_ST_CR_CERT_A;
  232. }
  233. break;
  234. case SSL3_ST_CR_CERT_A:
  235. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  236. ret = ssl3_get_server_certificate(hs);
  237. if (ret <= 0) {
  238. goto end;
  239. }
  240. }
  241. hs->state = SSL3_ST_CR_CERT_STATUS_A;
  242. break;
  243. case SSL3_ST_CR_CERT_STATUS_A:
  244. if (hs->certificate_status_expected) {
  245. ret = ssl3_get_cert_status(hs);
  246. if (ret <= 0) {
  247. goto end;
  248. }
  249. }
  250. hs->state = SSL3_ST_VERIFY_SERVER_CERT;
  251. break;
  252. case SSL3_ST_VERIFY_SERVER_CERT:
  253. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  254. ret = ssl3_verify_server_cert(hs);
  255. if (ret <= 0) {
  256. goto end;
  257. }
  258. }
  259. hs->state = SSL3_ST_CR_KEY_EXCH_A;
  260. break;
  261. case SSL3_ST_CR_KEY_EXCH_A:
  262. ret = ssl3_get_server_key_exchange(hs);
  263. if (ret <= 0) {
  264. goto end;
  265. }
  266. hs->state = SSL3_ST_CR_CERT_REQ_A;
  267. break;
  268. case SSL3_ST_CR_CERT_REQ_A:
  269. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  270. ret = ssl3_get_certificate_request(hs);
  271. if (ret <= 0) {
  272. goto end;
  273. }
  274. }
  275. hs->state = SSL3_ST_CR_SRVR_DONE_A;
  276. break;
  277. case SSL3_ST_CR_SRVR_DONE_A:
  278. ret = ssl3_get_server_hello_done(hs);
  279. if (ret <= 0) {
  280. goto end;
  281. }
  282. ssl->method->received_flight(ssl);
  283. hs->state = SSL3_ST_CW_CERT_A;
  284. break;
  285. case SSL3_ST_CW_CERT_A:
  286. if (hs->cert_request) {
  287. ret = ssl3_send_client_certificate(hs);
  288. if (ret <= 0) {
  289. goto end;
  290. }
  291. }
  292. hs->state = SSL3_ST_CW_KEY_EXCH_A;
  293. break;
  294. case SSL3_ST_CW_KEY_EXCH_A:
  295. ret = ssl3_send_client_key_exchange(hs);
  296. if (ret <= 0) {
  297. goto end;
  298. }
  299. hs->state = SSL3_ST_CW_CERT_VRFY_A;
  300. break;
  301. case SSL3_ST_CW_CERT_VRFY_A:
  302. case SSL3_ST_CW_CERT_VRFY_B:
  303. if (hs->cert_request && ssl_has_certificate(ssl)) {
  304. ret = ssl3_send_cert_verify(hs);
  305. if (ret <= 0) {
  306. goto end;
  307. }
  308. }
  309. hs->state = SSL3_ST_CW_CHANGE;
  310. break;
  311. case SSL3_ST_CW_CHANGE:
  312. if (!ssl->method->add_change_cipher_spec(ssl) ||
  313. !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  314. ret = -1;
  315. goto end;
  316. }
  317. hs->state = SSL3_ST_CW_NEXT_PROTO_A;
  318. break;
  319. case SSL3_ST_CW_NEXT_PROTO_A:
  320. if (hs->next_proto_neg_seen) {
  321. ret = ssl3_send_next_proto(hs);
  322. if (ret <= 0) {
  323. goto end;
  324. }
  325. }
  326. hs->state = SSL3_ST_CW_CHANNEL_ID_A;
  327. break;
  328. case SSL3_ST_CW_CHANNEL_ID_A:
  329. if (ssl->s3->tlsext_channel_id_valid) {
  330. ret = ssl3_send_channel_id(hs);
  331. if (ret <= 0) {
  332. goto end;
  333. }
  334. }
  335. hs->state = SSL3_ST_CW_FINISHED_A;
  336. break;
  337. case SSL3_ST_CW_FINISHED_A:
  338. ret = ssl3_send_finished(hs);
  339. if (ret <= 0) {
  340. goto end;
  341. }
  342. hs->state = SSL3_ST_CW_FLUSH;
  343. if (ssl->session != NULL) {
  344. hs->next_state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
  345. } else {
  346. /* This is a non-resumption handshake. If it involves ChannelID, then
  347. * record the handshake hashes at this point in the session so that
  348. * any resumption of this session with ChannelID can sign those
  349. * hashes. */
  350. ret = tls1_record_handshake_hashes_for_channel_id(hs);
  351. if (ret <= 0) {
  352. goto end;
  353. }
  354. if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
  355. ssl3_can_false_start(ssl) &&
  356. /* No False Start on renegotiation (would complicate the state
  357. * machine). */
  358. !ssl->s3->initial_handshake_complete) {
  359. hs->next_state = SSL3_ST_FALSE_START;
  360. } else {
  361. hs->next_state = SSL3_ST_CR_SESSION_TICKET_A;
  362. }
  363. }
  364. break;
  365. case SSL3_ST_FALSE_START:
  366. hs->state = SSL3_ST_CR_SESSION_TICKET_A;
  367. hs->in_false_start = 1;
  368. ret = 1;
  369. goto end;
  370. case SSL3_ST_CR_SESSION_TICKET_A:
  371. if (hs->ticket_expected) {
  372. ret = ssl3_get_new_session_ticket(hs);
  373. if (ret <= 0) {
  374. goto end;
  375. }
  376. }
  377. hs->state = SSL3_ST_CR_CHANGE;
  378. break;
  379. case SSL3_ST_CR_CHANGE:
  380. ret = ssl->method->read_change_cipher_spec(ssl);
  381. if (ret <= 0) {
  382. goto end;
  383. }
  384. if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
  385. ret = -1;
  386. goto end;
  387. }
  388. hs->state = SSL3_ST_CR_FINISHED_A;
  389. break;
  390. case SSL3_ST_CR_FINISHED_A:
  391. ret = ssl3_get_finished(hs);
  392. if (ret <= 0) {
  393. goto end;
  394. }
  395. ssl->method->received_flight(ssl);
  396. if (ssl->session != NULL) {
  397. hs->state = SSL3_ST_CW_CHANGE;
  398. } else {
  399. hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
  400. }
  401. break;
  402. case SSL3_ST_CW_FLUSH:
  403. ret = ssl->method->flush_flight(ssl);
  404. if (ret <= 0) {
  405. goto end;
  406. }
  407. hs->state = hs->next_state;
  408. if (hs->state != SSL3_ST_FINISH_CLIENT_HANDSHAKE) {
  409. ssl->method->expect_flight(ssl);
  410. }
  411. break;
  412. case SSL_ST_TLS13:
  413. ret = tls13_handshake(hs);
  414. if (ret <= 0) {
  415. goto end;
  416. }
  417. hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE;
  418. break;
  419. case SSL3_ST_FINISH_CLIENT_HANDSHAKE:
  420. ssl->method->release_current_message(ssl, 1 /* free_buffer */);
  421. SSL_SESSION_free(ssl->s3->established_session);
  422. if (ssl->session != NULL) {
  423. SSL_SESSION_up_ref(ssl->session);
  424. ssl->s3->established_session = ssl->session;
  425. } else {
  426. /* We make a copy of the session in order to maintain the immutability
  427. * of the new established_session due to False Start. The caller may
  428. * have taken a reference to the temporary session. */
  429. ssl->s3->established_session =
  430. SSL_SESSION_dup(hs->new_session, SSL_SESSION_DUP_ALL);
  431. if (ssl->s3->established_session == NULL) {
  432. ret = -1;
  433. goto end;
  434. }
  435. ssl->s3->established_session->not_resumable = 0;
  436. SSL_SESSION_free(hs->new_session);
  437. hs->new_session = NULL;
  438. }
  439. hs->state = SSL_ST_OK;
  440. break;
  441. case SSL_ST_OK: {
  442. const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
  443. ssl->s3->initial_handshake_complete = 1;
  444. if (is_initial_handshake) {
  445. /* Renegotiations do not participate in session resumption. */
  446. ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
  447. }
  448. ret = 1;
  449. ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
  450. goto end;
  451. }
  452. default:
  453. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
  454. ret = -1;
  455. goto end;
  456. }
  457. if (hs->state != state) {
  458. ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
  459. }
  460. }
  461. end:
  462. ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
  463. return ret;
  464. }
  465. uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) {
  466. /* Use the client_random for entropy. This both avoids calling |RAND_bytes| on
  467. * a single byte repeatedly and ensures the values are deterministic. This
  468. * allows the same ClientHello be sent twice for a HelloRetryRequest or the
  469. * same group be advertised in both supported_groups and key_shares. */
  470. uint16_t ret = ssl->s3->client_random[index];
  471. /* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */
  472. ret = (ret & 0xf0) | 0x0a;
  473. ret |= ret << 8;
  474. return ret;
  475. }
  476. /* ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
  477. * disabled algorithms. */
  478. static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
  479. uint32_t *out_mask_k) {
  480. int have_rsa = 0, have_ecdsa = 0;
  481. *out_mask_a = 0;
  482. *out_mask_k = 0;
  483. /* Now go through all signature algorithms seeing if we support any for RSA or
  484. * ECDSA. Do this for all versions not just TLS 1.2. */
  485. const uint16_t *sigalgs;
  486. size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs);
  487. for (size_t i = 0; i < num_sigalgs; i++) {
  488. switch (sigalgs[i]) {
  489. case SSL_SIGN_RSA_PSS_SHA512:
  490. case SSL_SIGN_RSA_PSS_SHA384:
  491. case SSL_SIGN_RSA_PSS_SHA256:
  492. case SSL_SIGN_RSA_PKCS1_SHA512:
  493. case SSL_SIGN_RSA_PKCS1_SHA384:
  494. case SSL_SIGN_RSA_PKCS1_SHA256:
  495. case SSL_SIGN_RSA_PKCS1_SHA1:
  496. have_rsa = 1;
  497. break;
  498. case SSL_SIGN_ECDSA_SECP521R1_SHA512:
  499. case SSL_SIGN_ECDSA_SECP384R1_SHA384:
  500. case SSL_SIGN_ECDSA_SECP256R1_SHA256:
  501. case SSL_SIGN_ECDSA_SHA1:
  502. have_ecdsa = 1;
  503. break;
  504. }
  505. }
  506. /* Disable auth if we don't include any appropriate signature algorithms. */
  507. if (!have_rsa) {
  508. *out_mask_a |= SSL_aRSA;
  509. }
  510. if (!have_ecdsa) {
  511. *out_mask_a |= SSL_aECDSA;
  512. }
  513. /* PSK requires a client callback. */
  514. if (ssl->psk_client_callback == NULL) {
  515. *out_mask_a |= SSL_aPSK;
  516. *out_mask_k |= SSL_kPSK;
  517. }
  518. }
  519. static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
  520. uint16_t min_version,
  521. uint16_t max_version) {
  522. uint32_t mask_a, mask_k;
  523. ssl_get_client_disabled(ssl, &mask_a, &mask_k);
  524. CBB child;
  525. if (!CBB_add_u16_length_prefixed(out, &child)) {
  526. return 0;
  527. }
  528. /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */
  529. if (ssl->ctx->grease_enabled &&
  530. !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
  531. return 0;
  532. }
  533. /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
  534. * hardware support. */
  535. if (max_version >= TLS1_3_VERSION) {
  536. if (!EVP_has_aes_hardware() &&
  537. !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
  538. return 0;
  539. }
  540. if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
  541. !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
  542. return 0;
  543. }
  544. if (EVP_has_aes_hardware() &&
  545. !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
  546. return 0;
  547. }
  548. }
  549. if (min_version < TLS1_3_VERSION) {
  550. STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
  551. int any_enabled = 0;
  552. for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  553. const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
  554. /* Skip disabled ciphers */
  555. if ((cipher->algorithm_mkey & mask_k) ||
  556. (cipher->algorithm_auth & mask_a)) {
  557. continue;
  558. }
  559. if (SSL_CIPHER_get_min_version(cipher) > max_version ||
  560. SSL_CIPHER_get_max_version(cipher) < min_version) {
  561. continue;
  562. }
  563. any_enabled = 1;
  564. if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
  565. return 0;
  566. }
  567. }
  568. /* If all ciphers were disabled, return the error to the caller. */
  569. if (!any_enabled && max_version < TLS1_3_VERSION) {
  570. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
  571. return 0;
  572. }
  573. }
  574. /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
  575. * added. */
  576. if (max_version == SSL3_VERSION &&
  577. !ssl->s3->initial_handshake_complete) {
  578. if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
  579. return 0;
  580. }
  581. }
  582. if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  583. if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  584. return 0;
  585. }
  586. }
  587. return CBB_flush(out);
  588. }
  589. int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
  590. SSL *const ssl = hs->ssl;
  591. uint16_t min_version, max_version;
  592. if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
  593. return 0;
  594. }
  595. CBB cbb, body;
  596. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO)) {
  597. goto err;
  598. }
  599. /* Renegotiations do not participate in session resumption. */
  600. int has_session = ssl->session != NULL &&
  601. !ssl->s3->initial_handshake_complete;
  602. CBB child;
  603. if (!CBB_add_u16(&body, hs->client_version) ||
  604. !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  605. !CBB_add_u8_length_prefixed(&body, &child) ||
  606. (has_session &&
  607. !CBB_add_bytes(&child, ssl->session->session_id,
  608. ssl->session->session_id_length))) {
  609. goto err;
  610. }
  611. if (SSL_is_dtls(ssl)) {
  612. if (!CBB_add_u8_length_prefixed(&body, &child) ||
  613. !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
  614. goto err;
  615. }
  616. }
  617. size_t header_len =
  618. SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
  619. if (!ssl_write_client_cipher_list(ssl, &body, min_version, max_version) ||
  620. !CBB_add_u8(&body, 1 /* one compression method */) ||
  621. !CBB_add_u8(&body, 0 /* null compression */) ||
  622. !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
  623. goto err;
  624. }
  625. uint8_t *msg = NULL;
  626. size_t len;
  627. if (!ssl->method->finish_message(ssl, &cbb, &msg, &len)) {
  628. goto err;
  629. }
  630. /* Now that the length prefixes have been computed, fill in the placeholder
  631. * PSK binder. */
  632. if (hs->needs_psk_binder &&
  633. !tls13_write_psk_binder(hs, msg, len)) {
  634. OPENSSL_free(msg);
  635. goto err;
  636. }
  637. return ssl->method->add_message(ssl, msg, len);
  638. err:
  639. CBB_cleanup(&cbb);
  640. return 0;
  641. }
  642. static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) {
  643. SSL *const ssl = hs->ssl;
  644. /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
  645. * may send multiple ClientHellos if we receive HelloVerifyRequest. */
  646. if (!SSL_TRANSCRIPT_init(&hs->transcript)) {
  647. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  648. return -1;
  649. }
  650. uint16_t min_version, max_version;
  651. if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
  652. return -1;
  653. }
  654. uint16_t max_wire_version = ssl->method->version_to_wire(max_version);
  655. assert(hs->state == SSL3_ST_CW_CLNT_HELLO_A);
  656. if (!ssl->s3->have_version) {
  657. ssl->version = max_wire_version;
  658. }
  659. /* Always advertise the ClientHello version from the original maximum version,
  660. * even on renegotiation. The static RSA key exchange uses this field, and
  661. * some servers fail when it changes across handshakes. */
  662. hs->client_version = max_wire_version;
  663. if (max_version >= TLS1_3_VERSION) {
  664. hs->client_version = ssl->method->version_to_wire(TLS1_2_VERSION);
  665. }
  666. /* If the configured session has expired or was created at a disabled
  667. * version, drop it. */
  668. if (ssl->session != NULL) {
  669. uint16_t session_version;
  670. if (ssl->session->is_server ||
  671. !ssl->method->version_from_wire(&session_version,
  672. ssl->session->ssl_version) ||
  673. (session_version < TLS1_3_VERSION &&
  674. ssl->session->session_id_length == 0) ||
  675. ssl->session->not_resumable ||
  676. !ssl_session_is_time_valid(ssl, ssl->session) ||
  677. session_version < min_version || session_version > max_version) {
  678. ssl_set_session(ssl, NULL);
  679. }
  680. }
  681. /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
  682. * renegerate the client_random. The random must be reused. */
  683. if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
  684. !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
  685. return -1;
  686. }
  687. if (!ssl_write_client_hello(hs)) {
  688. return -1;
  689. }
  690. return 1;
  691. }
  692. static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs) {
  693. SSL *const ssl = hs->ssl;
  694. int al;
  695. CBS hello_verify_request, cookie;
  696. uint16_t server_version;
  697. int ret = ssl->method->ssl_get_message(ssl);
  698. if (ret <= 0) {
  699. return ret;
  700. }
  701. if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  702. ssl->d1->send_cookie = 0;
  703. ssl->s3->tmp.reuse_message = 1;
  704. return 1;
  705. }
  706. CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
  707. if (!CBS_get_u16(&hello_verify_request, &server_version) ||
  708. !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
  709. CBS_len(&hello_verify_request) != 0) {
  710. al = SSL_AD_DECODE_ERROR;
  711. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  712. goto f_err;
  713. }
  714. if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
  715. al = SSL_AD_ILLEGAL_PARAMETER;
  716. goto f_err;
  717. }
  718. OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
  719. ssl->d1->cookie_len = CBS_len(&cookie);
  720. ssl->d1->send_cookie = 1;
  721. return 1;
  722. f_err:
  723. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  724. return -1;
  725. }
  726. static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) {
  727. SSL *const ssl = hs->ssl;
  728. int al = SSL_AD_INTERNAL_ERROR;
  729. CBS server_hello, server_random, session_id;
  730. uint16_t server_wire_version, cipher_suite;
  731. uint8_t compression_method;
  732. int ret = ssl->method->ssl_get_message(ssl);
  733. if (ret <= 0) {
  734. uint32_t err = ERR_peek_error();
  735. if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
  736. ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
  737. /* Add a dedicated error code to the queue for a handshake_failure alert
  738. * in response to ClientHello. This matches NSS's client behavior and
  739. * gives a better error on a (probable) failure to negotiate initial
  740. * parameters. Note: this error code comes after the original one.
  741. *
  742. * See https://crbug.com/446505. */
  743. OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
  744. }
  745. return ret;
  746. }
  747. if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
  748. ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
  749. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  750. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  751. return -1;
  752. }
  753. CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
  754. if (!CBS_get_u16(&server_hello, &server_wire_version)) {
  755. al = SSL_AD_DECODE_ERROR;
  756. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  757. goto f_err;
  758. }
  759. uint16_t min_version, max_version, server_version;
  760. if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
  761. !ssl->method->version_from_wire(&server_version, server_wire_version) ||
  762. server_version < min_version || server_version > max_version) {
  763. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
  764. al = SSL_AD_PROTOCOL_VERSION;
  765. goto f_err;
  766. }
  767. assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
  768. if (!ssl->s3->have_version) {
  769. ssl->version = server_wire_version;
  770. /* At this point, the connection's version is known and ssl->version is
  771. * fixed. Begin enforcing the record-layer version. */
  772. ssl->s3->have_version = 1;
  773. } else if (server_wire_version != ssl->version) {
  774. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  775. al = SSL_AD_PROTOCOL_VERSION;
  776. goto f_err;
  777. }
  778. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  779. hs->state = SSL_ST_TLS13;
  780. hs->do_tls13_handshake = tls13_client_handshake;
  781. return 1;
  782. }
  783. ssl_clear_tls13_state(hs);
  784. if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) {
  785. return -1;
  786. }
  787. if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
  788. !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
  789. CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
  790. !CBS_get_u16(&server_hello, &cipher_suite) ||
  791. !CBS_get_u8(&server_hello, &compression_method)) {
  792. al = SSL_AD_DECODE_ERROR;
  793. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  794. goto f_err;
  795. }
  796. /* Copy over the server random. */
  797. OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
  798. /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
  799. * 1.3 is finalized and we are not implementing a draft version. */
  800. if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
  801. ssl->session->session_id_length != 0 &&
  802. CBS_mem_equal(&session_id, ssl->session->session_id,
  803. ssl->session->session_id_length)) {
  804. ssl->s3->session_reused = 1;
  805. } else {
  806. /* The session wasn't resumed. Create a fresh SSL_SESSION to
  807. * fill out. */
  808. ssl_set_session(ssl, NULL);
  809. if (!ssl_get_new_session(hs, 0 /* client */)) {
  810. goto f_err;
  811. }
  812. /* Note: session_id could be empty. */
  813. hs->new_session->session_id_length = CBS_len(&session_id);
  814. OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
  815. CBS_len(&session_id));
  816. }
  817. const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
  818. if (c == NULL) {
  819. /* unknown cipher */
  820. al = SSL_AD_ILLEGAL_PARAMETER;
  821. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
  822. goto f_err;
  823. }
  824. /* The cipher must be allowed in the selected version and enabled. */
  825. uint32_t mask_a, mask_k;
  826. ssl_get_client_disabled(ssl, &mask_a, &mask_k);
  827. if ((c->algorithm_mkey & mask_k) || (c->algorithm_auth & mask_a) ||
  828. SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
  829. SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) ||
  830. !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) {
  831. al = SSL_AD_ILLEGAL_PARAMETER;
  832. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
  833. goto f_err;
  834. }
  835. if (ssl->session != NULL) {
  836. if (ssl->session->ssl_version != ssl->version) {
  837. al = SSL_AD_ILLEGAL_PARAMETER;
  838. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
  839. goto f_err;
  840. }
  841. if (ssl->session->cipher != c) {
  842. al = SSL_AD_ILLEGAL_PARAMETER;
  843. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  844. goto f_err;
  845. }
  846. if (!ssl_session_is_context_valid(ssl, ssl->session)) {
  847. /* This is actually a client application bug. */
  848. al = SSL_AD_ILLEGAL_PARAMETER;
  849. OPENSSL_PUT_ERROR(SSL,
  850. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  851. goto f_err;
  852. }
  853. } else {
  854. hs->new_session->cipher = c;
  855. }
  856. hs->new_cipher = c;
  857. /* Now that the cipher is known, initialize the handshake hash and hash the
  858. * ServerHello. */
  859. if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl),
  860. c->algorithm_prf) ||
  861. !ssl_hash_current_message(hs)) {
  862. goto f_err;
  863. }
  864. /* If doing a full handshake, the server may request a client certificate
  865. * which requires hashing the handshake transcript. Otherwise, the handshake
  866. * buffer may be released. */
  867. if (ssl->session != NULL ||
  868. !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  869. SSL_TRANSCRIPT_free_buffer(&hs->transcript);
  870. }
  871. /* Only the NULL compression algorithm is supported. */
  872. if (compression_method != 0) {
  873. al = SSL_AD_ILLEGAL_PARAMETER;
  874. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  875. goto f_err;
  876. }
  877. /* TLS extensions */
  878. if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
  879. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  880. goto err;
  881. }
  882. /* There should be nothing left over in the record. */
  883. if (CBS_len(&server_hello) != 0) {
  884. /* wrong packet length */
  885. al = SSL_AD_DECODE_ERROR;
  886. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  887. goto f_err;
  888. }
  889. if (ssl->session != NULL &&
  890. hs->extended_master_secret != ssl->session->extended_master_secret) {
  891. al = SSL_AD_HANDSHAKE_FAILURE;
  892. if (ssl->session->extended_master_secret) {
  893. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  894. } else {
  895. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
  896. }
  897. goto f_err;
  898. }
  899. return 1;
  900. f_err:
  901. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  902. err:
  903. return -1;
  904. }
  905. static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) {
  906. SSL *const ssl = hs->ssl;
  907. int ret = ssl->method->ssl_get_message(ssl);
  908. if (ret <= 0) {
  909. return ret;
  910. }
  911. if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) ||
  912. !ssl_hash_current_message(hs)) {
  913. return -1;
  914. }
  915. CBS cbs;
  916. CBS_init(&cbs, ssl->init_msg, ssl->init_num);
  917. uint8_t alert = SSL_AD_DECODE_ERROR;
  918. sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
  919. EVP_PKEY_free(hs->peer_pubkey);
  920. hs->peer_pubkey = NULL;
  921. hs->new_session->certs = ssl_parse_cert_chain(&alert, &hs->peer_pubkey, NULL,
  922. &cbs, ssl->ctx->pool);
  923. if (hs->new_session->certs == NULL) {
  924. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  925. return -1;
  926. }
  927. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
  928. CBS_len(&cbs) != 0 ||
  929. !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
  930. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  931. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  932. return -1;
  933. }
  934. if (!ssl_check_leaf_certificate(
  935. hs, hs->peer_pubkey,
  936. sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
  937. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  938. return -1;
  939. }
  940. return 1;
  941. }
  942. static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) {
  943. SSL *const ssl = hs->ssl;
  944. int al;
  945. CBS certificate_status, ocsp_response;
  946. uint8_t status_type;
  947. int ret = ssl->method->ssl_get_message(ssl);
  948. if (ret <= 0) {
  949. return ret;
  950. }
  951. if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
  952. /* A server may send status_request in ServerHello and then change
  953. * its mind about sending CertificateStatus. */
  954. ssl->s3->tmp.reuse_message = 1;
  955. return 1;
  956. }
  957. if (!ssl_hash_current_message(hs)) {
  958. return -1;
  959. }
  960. CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
  961. if (!CBS_get_u8(&certificate_status, &status_type) ||
  962. status_type != TLSEXT_STATUSTYPE_ocsp ||
  963. !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
  964. CBS_len(&ocsp_response) == 0 ||
  965. CBS_len(&certificate_status) != 0) {
  966. al = SSL_AD_DECODE_ERROR;
  967. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  968. goto f_err;
  969. }
  970. if (!CBS_stow(&ocsp_response, &hs->new_session->ocsp_response,
  971. &hs->new_session->ocsp_response_length)) {
  972. al = SSL_AD_INTERNAL_ERROR;
  973. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  974. goto f_err;
  975. }
  976. return 1;
  977. f_err:
  978. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  979. return -1;
  980. }
  981. static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs) {
  982. SSL *const ssl = hs->ssl;
  983. if (!ssl_verify_cert_chain(ssl, &hs->new_session->verify_result,
  984. hs->new_session->x509_chain)) {
  985. return -1;
  986. }
  987. return 1;
  988. }
  989. static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) {
  990. SSL *const ssl = hs->ssl;
  991. int al;
  992. DH *dh = NULL;
  993. EC_KEY *ecdh = NULL;
  994. EC_POINT *srvr_ecpoint = NULL;
  995. int ret = ssl->method->ssl_get_message(ssl);
  996. if (ret <= 0) {
  997. return ret;
  998. }
  999. if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
  1000. /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */
  1001. if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
  1002. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  1003. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1004. return -1;
  1005. }
  1006. ssl->s3->tmp.reuse_message = 1;
  1007. return 1;
  1008. }
  1009. if (!ssl_hash_current_message(hs)) {
  1010. return -1;
  1011. }
  1012. /* Retain a copy of the original CBS to compute the signature over. */
  1013. CBS server_key_exchange;
  1014. CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
  1015. CBS server_key_exchange_orig = server_key_exchange;
  1016. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  1017. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  1018. if (alg_a & SSL_aPSK) {
  1019. CBS psk_identity_hint;
  1020. /* Each of the PSK key exchanges begins with a psk_identity_hint. */
  1021. if (!CBS_get_u16_length_prefixed(&server_key_exchange,
  1022. &psk_identity_hint)) {
  1023. al = SSL_AD_DECODE_ERROR;
  1024. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1025. goto f_err;
  1026. }
  1027. /* Store PSK identity hint for later use, hint is used in
  1028. * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
  1029. * identity hint can be as long as the maximum length of a PSK identity.
  1030. * Also do not allow NULL characters; identities are saved as C strings.
  1031. *
  1032. * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
  1033. * a specific identity. */
  1034. if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
  1035. CBS_contains_zero_byte(&psk_identity_hint)) {
  1036. al = SSL_AD_HANDSHAKE_FAILURE;
  1037. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1038. goto f_err;
  1039. }
  1040. /* Save non-empty identity hints as a C string. Empty identity hints we
  1041. * treat as missing. Plain PSK makes it possible to send either no hint
  1042. * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
  1043. * empty hint. Having different capabilities is odd, so we interpret empty
  1044. * and missing as identical. */
  1045. if (CBS_len(&psk_identity_hint) != 0 &&
  1046. !CBS_strdup(&psk_identity_hint, &hs->peer_psk_identity_hint)) {
  1047. al = SSL_AD_INTERNAL_ERROR;
  1048. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1049. goto f_err;
  1050. }
  1051. }
  1052. if (alg_k & SSL_kDHE) {
  1053. CBS dh_p, dh_g, dh_Ys;
  1054. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
  1055. CBS_len(&dh_p) == 0 ||
  1056. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
  1057. CBS_len(&dh_g) == 0 ||
  1058. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
  1059. CBS_len(&dh_Ys) == 0) {
  1060. al = SSL_AD_DECODE_ERROR;
  1061. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1062. goto f_err;
  1063. }
  1064. dh = DH_new();
  1065. if (dh == NULL) {
  1066. goto err;
  1067. }
  1068. dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
  1069. dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
  1070. if (dh->p == NULL || dh->g == NULL) {
  1071. goto err;
  1072. }
  1073. unsigned bits = DH_num_bits(dh);
  1074. if (bits < 1024) {
  1075. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
  1076. goto err;
  1077. } else if (bits > 4096) {
  1078. /* Overly large DHE groups are prohibitively expensive, so enforce a limit
  1079. * to prevent a server from causing us to perform too expensive of a
  1080. * computation. */
  1081. OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
  1082. goto err;
  1083. }
  1084. SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh);
  1085. dh = NULL;
  1086. /* Save the peer public key for later. */
  1087. if (!CBS_stow(&dh_Ys, &hs->peer_key, &hs->peer_key_len)) {
  1088. goto err;
  1089. }
  1090. } else if (alg_k & SSL_kECDHE) {
  1091. /* Parse the server parameters. */
  1092. uint8_t group_type;
  1093. uint16_t group_id;
  1094. CBS point;
  1095. if (!CBS_get_u8(&server_key_exchange, &group_type) ||
  1096. group_type != NAMED_CURVE_TYPE ||
  1097. !CBS_get_u16(&server_key_exchange, &group_id) ||
  1098. !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
  1099. al = SSL_AD_DECODE_ERROR;
  1100. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1101. goto f_err;
  1102. }
  1103. hs->new_session->group_id = group_id;
  1104. /* Ensure the group is consistent with preferences. */
  1105. if (!tls1_check_group_id(ssl, group_id)) {
  1106. al = SSL_AD_ILLEGAL_PARAMETER;
  1107. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  1108. goto f_err;
  1109. }
  1110. /* Initialize ECDH and save the peer public key for later. */
  1111. if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) ||
  1112. !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) {
  1113. goto err;
  1114. }
  1115. } else if (!(alg_k & SSL_kPSK)) {
  1116. al = SSL_AD_UNEXPECTED_MESSAGE;
  1117. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  1118. goto f_err;
  1119. }
  1120. /* At this point, |server_key_exchange| contains the signature, if any, while
  1121. * |server_key_exchange_orig| contains the entire message. From that, derive
  1122. * a CBS containing just the parameter. */
  1123. CBS parameter;
  1124. CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
  1125. CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
  1126. /* ServerKeyExchange should be signed by the server's public key. */
  1127. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  1128. uint16_t signature_algorithm = 0;
  1129. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1130. if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
  1131. al = SSL_AD_DECODE_ERROR;
  1132. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1133. goto f_err;
  1134. }
  1135. if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
  1136. goto f_err;
  1137. }
  1138. hs->new_session->peer_signature_algorithm = signature_algorithm;
  1139. } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) {
  1140. signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
  1141. } else if (hs->peer_pubkey->type == EVP_PKEY_EC) {
  1142. signature_algorithm = SSL_SIGN_ECDSA_SHA1;
  1143. } else {
  1144. al = SSL_AD_UNSUPPORTED_CERTIFICATE;
  1145. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  1146. goto f_err;
  1147. }
  1148. /* The last field in |server_key_exchange| is the signature. */
  1149. CBS signature;
  1150. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
  1151. CBS_len(&server_key_exchange) != 0) {
  1152. al = SSL_AD_DECODE_ERROR;
  1153. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1154. goto f_err;
  1155. }
  1156. CBB transcript;
  1157. uint8_t *transcript_data;
  1158. size_t transcript_len;
  1159. if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
  1160. !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  1161. !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  1162. !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
  1163. !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
  1164. CBB_cleanup(&transcript);
  1165. al = SSL_AD_INTERNAL_ERROR;
  1166. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1167. goto f_err;
  1168. }
  1169. int sig_ok = ssl_public_key_verify(
  1170. ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
  1171. hs->peer_pubkey, transcript_data, transcript_len);
  1172. OPENSSL_free(transcript_data);
  1173. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  1174. sig_ok = 1;
  1175. ERR_clear_error();
  1176. #endif
  1177. if (!sig_ok) {
  1178. /* bad signature */
  1179. al = SSL_AD_DECRYPT_ERROR;
  1180. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  1181. goto f_err;
  1182. }
  1183. } else {
  1184. /* PSK ciphers are the only supported certificate-less ciphers. */
  1185. assert(alg_a == SSL_aPSK);
  1186. if (CBS_len(&server_key_exchange) > 0) {
  1187. al = SSL_AD_DECODE_ERROR;
  1188. OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
  1189. goto f_err;
  1190. }
  1191. }
  1192. return 1;
  1193. f_err:
  1194. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  1195. err:
  1196. DH_free(dh);
  1197. EC_POINT_free(srvr_ecpoint);
  1198. EC_KEY_free(ecdh);
  1199. return -1;
  1200. }
  1201. static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) {
  1202. SSL *const ssl = hs->ssl;
  1203. int msg_ret = ssl->method->ssl_get_message(ssl);
  1204. if (msg_ret <= 0) {
  1205. return msg_ret;
  1206. }
  1207. if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
  1208. ssl->s3->tmp.reuse_message = 1;
  1209. /* If we get here we don't need the handshake buffer as we won't be doing
  1210. * client auth. */
  1211. SSL_TRANSCRIPT_free_buffer(&hs->transcript);
  1212. return 1;
  1213. }
  1214. if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_REQUEST) ||
  1215. !ssl_hash_current_message(hs)) {
  1216. return -1;
  1217. }
  1218. CBS cbs;
  1219. CBS_init(&cbs, ssl->init_msg, ssl->init_num);
  1220. /* Get the certificate types. */
  1221. CBS certificate_types;
  1222. if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
  1223. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1224. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1225. return -1;
  1226. }
  1227. if (!CBS_stow(&certificate_types, &hs->certificate_types,
  1228. &hs->num_certificate_types)) {
  1229. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1230. return -1;
  1231. }
  1232. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1233. CBS supported_signature_algorithms;
  1234. if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
  1235. !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
  1236. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1237. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1238. return -1;
  1239. }
  1240. }
  1241. uint8_t alert = SSL_AD_DECODE_ERROR;
  1242. STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs);
  1243. if (ca_sk == NULL) {
  1244. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  1245. return -1;
  1246. }
  1247. if (CBS_len(&cbs) != 0) {
  1248. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1249. sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
  1250. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1251. return -1;
  1252. }
  1253. hs->cert_request = 1;
  1254. sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free);
  1255. hs->ca_names = ca_sk;
  1256. return 1;
  1257. }
  1258. static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) {
  1259. SSL *const ssl = hs->ssl;
  1260. int ret = ssl->method->ssl_get_message(ssl);
  1261. if (ret <= 0) {
  1262. return ret;
  1263. }
  1264. if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO_DONE) ||
  1265. !ssl_hash_current_message(hs)) {
  1266. return -1;
  1267. }
  1268. /* ServerHelloDone is empty. */
  1269. if (ssl->init_num > 0) {
  1270. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1271. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1272. return -1;
  1273. }
  1274. return 1;
  1275. }
  1276. static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) {
  1277. SSL *const ssl = hs->ssl;
  1278. /* Call cert_cb to update the certificate. */
  1279. if (ssl->cert->cert_cb) {
  1280. int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
  1281. if (ret < 0) {
  1282. ssl->rwstate = SSL_X509_LOOKUP;
  1283. return -1;
  1284. }
  1285. if (ret == 0) {
  1286. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
  1287. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1288. return -1;
  1289. }
  1290. }
  1291. if (!ssl_has_certificate(ssl)) {
  1292. /* Without a client certificate, the handshake buffer may be released. */
  1293. SSL_TRANSCRIPT_free_buffer(&hs->transcript);
  1294. /* In SSL 3.0, the Certificate message is replaced with a warning alert. */
  1295. if (ssl->version == SSL3_VERSION) {
  1296. if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
  1297. SSL_AD_NO_CERTIFICATE)) {
  1298. return -1;
  1299. }
  1300. return 1;
  1301. }
  1302. }
  1303. if (!ssl_auto_chain_if_needed(ssl) ||
  1304. !ssl3_output_cert_chain(ssl)) {
  1305. return -1;
  1306. }
  1307. return 1;
  1308. }
  1309. OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
  1310. SIZE_T_IS_SMALLER_THAN_UNSIGNED);
  1311. static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) {
  1312. SSL *const ssl = hs->ssl;
  1313. uint8_t *pms = NULL;
  1314. size_t pms_len = 0;
  1315. CBB cbb, body;
  1316. if (!ssl->method->init_message(ssl, &cbb, &body,
  1317. SSL3_MT_CLIENT_KEY_EXCHANGE)) {
  1318. goto err;
  1319. }
  1320. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  1321. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  1322. /* If using a PSK key exchange, prepare the pre-shared key. */
  1323. unsigned psk_len = 0;
  1324. uint8_t psk[PSK_MAX_PSK_LEN];
  1325. if (alg_a & SSL_aPSK) {
  1326. if (ssl->psk_client_callback == NULL) {
  1327. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
  1328. goto err;
  1329. }
  1330. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1331. OPENSSL_memset(identity, 0, sizeof(identity));
  1332. psk_len =
  1333. ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity,
  1334. sizeof(identity), psk, sizeof(psk));
  1335. if (psk_len == 0) {
  1336. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1337. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1338. goto err;
  1339. }
  1340. assert(psk_len <= PSK_MAX_PSK_LEN);
  1341. OPENSSL_free(hs->new_session->psk_identity);
  1342. hs->new_session->psk_identity = BUF_strdup(identity);
  1343. if (hs->new_session->psk_identity == NULL) {
  1344. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1345. goto err;
  1346. }
  1347. /* Write out psk_identity. */
  1348. CBB child;
  1349. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1350. !CBB_add_bytes(&child, (const uint8_t *)identity,
  1351. OPENSSL_strnlen(identity, sizeof(identity))) ||
  1352. !CBB_flush(&body)) {
  1353. goto err;
  1354. }
  1355. }
  1356. /* Depending on the key exchange method, compute |pms| and |pms_len|. */
  1357. if (alg_k & SSL_kRSA) {
  1358. pms_len = SSL_MAX_MASTER_KEY_LENGTH;
  1359. pms = OPENSSL_malloc(pms_len);
  1360. if (pms == NULL) {
  1361. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1362. goto err;
  1363. }
  1364. RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey);
  1365. if (rsa == NULL) {
  1366. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1367. goto err;
  1368. }
  1369. pms[0] = hs->client_version >> 8;
  1370. pms[1] = hs->client_version & 0xff;
  1371. if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
  1372. goto err;
  1373. }
  1374. CBB child, *enc_pms = &body;
  1375. size_t enc_pms_len;
  1376. /* In TLS, there is a length prefix. */
  1377. if (ssl->version > SSL3_VERSION) {
  1378. if (!CBB_add_u16_length_prefixed(&body, &child)) {
  1379. goto err;
  1380. }
  1381. enc_pms = &child;
  1382. }
  1383. uint8_t *ptr;
  1384. if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
  1385. !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
  1386. RSA_PKCS1_PADDING) ||
  1387. !CBB_did_write(enc_pms, enc_pms_len) ||
  1388. !CBB_flush(&body)) {
  1389. goto err;
  1390. }
  1391. } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) {
  1392. /* Generate a keypair and serialize the public half. */
  1393. CBB child;
  1394. if (!SSL_ECDH_CTX_add_key(&hs->ecdh_ctx, &body, &child)) {
  1395. goto err;
  1396. }
  1397. /* Compute the premaster. */
  1398. uint8_t alert = SSL_AD_DECODE_ERROR;
  1399. if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert,
  1400. hs->peer_key, hs->peer_key_len)) {
  1401. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  1402. goto err;
  1403. }
  1404. if (!CBB_flush(&body)) {
  1405. goto err;
  1406. }
  1407. /* The key exchange state may now be discarded. */
  1408. SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
  1409. OPENSSL_free(hs->peer_key);
  1410. hs->peer_key = NULL;
  1411. hs->peer_key_len = 0;
  1412. } else if (alg_k & SSL_kPSK) {
  1413. /* For plain PSK, other_secret is a block of 0s with the same length as
  1414. * the pre-shared key. */
  1415. pms_len = psk_len;
  1416. pms = OPENSSL_malloc(pms_len);
  1417. if (pms == NULL) {
  1418. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1419. goto err;
  1420. }
  1421. OPENSSL_memset(pms, 0, pms_len);
  1422. } else {
  1423. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1424. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1425. goto err;
  1426. }
  1427. /* For a PSK cipher suite, other_secret is combined with the pre-shared
  1428. * key. */
  1429. if (alg_a & SSL_aPSK) {
  1430. CBB pms_cbb, child;
  1431. uint8_t *new_pms;
  1432. size_t new_pms_len;
  1433. CBB_zero(&pms_cbb);
  1434. if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
  1435. !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
  1436. !CBB_add_bytes(&child, pms, pms_len) ||
  1437. !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
  1438. !CBB_add_bytes(&child, psk, psk_len) ||
  1439. !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
  1440. CBB_cleanup(&pms_cbb);
  1441. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1442. goto err;
  1443. }
  1444. OPENSSL_cleanse(pms, pms_len);
  1445. OPENSSL_free(pms);
  1446. pms = new_pms;
  1447. pms_len = new_pms_len;
  1448. }
  1449. /* The message must be added to the finished hash before calculating the
  1450. * master secret. */
  1451. if (!ssl_add_message_cbb(ssl, &cbb)) {
  1452. goto err;
  1453. }
  1454. hs->new_session->master_key_length = tls1_generate_master_secret(
  1455. hs, hs->new_session->master_key, pms, pms_len);
  1456. if (hs->new_session->master_key_length == 0) {
  1457. goto err;
  1458. }
  1459. hs->new_session->extended_master_secret = hs->extended_master_secret;
  1460. OPENSSL_cleanse(pms, pms_len);
  1461. OPENSSL_free(pms);
  1462. return 1;
  1463. err:
  1464. CBB_cleanup(&cbb);
  1465. if (pms != NULL) {
  1466. OPENSSL_cleanse(pms, pms_len);
  1467. OPENSSL_free(pms);
  1468. }
  1469. return -1;
  1470. }
  1471. static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) {
  1472. SSL *const ssl = hs->ssl;
  1473. assert(ssl_has_private_key(ssl));
  1474. CBB cbb, body, child;
  1475. if (!ssl->method->init_message(ssl, &cbb, &body,
  1476. SSL3_MT_CERTIFICATE_VERIFY)) {
  1477. goto err;
  1478. }
  1479. uint16_t signature_algorithm;
  1480. if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
  1481. goto err;
  1482. }
  1483. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1484. /* Write out the digest type in TLS 1.2. */
  1485. if (!CBB_add_u16(&body, signature_algorithm)) {
  1486. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1487. goto err;
  1488. }
  1489. }
  1490. /* Set aside space for the signature. */
  1491. const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
  1492. uint8_t *ptr;
  1493. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1494. !CBB_reserve(&child, &ptr, max_sig_len)) {
  1495. goto err;
  1496. }
  1497. size_t sig_len = max_sig_len;
  1498. enum ssl_private_key_result_t sign_result;
  1499. if (hs->state == SSL3_ST_CW_CERT_VRFY_A) {
  1500. /* The SSL3 construction for CertificateVerify does not decompose into a
  1501. * single final digest and signature, and must be special-cased. */
  1502. if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
  1503. if (ssl->cert->key_method != NULL) {
  1504. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
  1505. goto err;
  1506. }
  1507. uint8_t digest[EVP_MAX_MD_SIZE];
  1508. size_t digest_len;
  1509. if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest,
  1510. &digest_len, hs->new_session,
  1511. signature_algorithm)) {
  1512. goto err;
  1513. }
  1514. sign_result = ssl_private_key_success;
  1515. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
  1516. if (pctx == NULL ||
  1517. !EVP_PKEY_sign_init(pctx) ||
  1518. !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
  1519. EVP_PKEY_CTX_free(pctx);
  1520. sign_result = ssl_private_key_failure;
  1521. goto err;
  1522. }
  1523. EVP_PKEY_CTX_free(pctx);
  1524. } else {
  1525. sign_result = ssl_private_key_sign(
  1526. ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
  1527. (const uint8_t *)hs->transcript.buffer->data,
  1528. hs->transcript.buffer->length);
  1529. }
  1530. /* The handshake buffer is no longer necessary. */
  1531. SSL_TRANSCRIPT_free_buffer(&hs->transcript);
  1532. } else {
  1533. assert(hs->state == SSL3_ST_CW_CERT_VRFY_B);
  1534. sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
  1535. }
  1536. switch (sign_result) {
  1537. case ssl_private_key_success:
  1538. break;
  1539. case ssl_private_key_failure:
  1540. goto err;
  1541. case ssl_private_key_retry:
  1542. ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
  1543. hs->state = SSL3_ST_CW_CERT_VRFY_B;
  1544. goto err;
  1545. }
  1546. if (!CBB_did_write(&child, sig_len) ||
  1547. !ssl_add_message_cbb(ssl, &cbb)) {
  1548. goto err;
  1549. }
  1550. return 1;
  1551. err:
  1552. CBB_cleanup(&cbb);
  1553. return -1;
  1554. }
  1555. static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) {
  1556. SSL *const ssl = hs->ssl;
  1557. static const uint8_t kZero[32] = {0};
  1558. size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
  1559. CBB cbb, body, child;
  1560. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
  1561. !CBB_add_u8_length_prefixed(&body, &child) ||
  1562. !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
  1563. ssl->s3->next_proto_negotiated_len) ||
  1564. !CBB_add_u8_length_prefixed(&body, &child) ||
  1565. !CBB_add_bytes(&child, kZero, padding_len) ||
  1566. !ssl_add_message_cbb(ssl, &cbb)) {
  1567. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1568. CBB_cleanup(&cbb);
  1569. return -1;
  1570. }
  1571. return 1;
  1572. }
  1573. static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) {
  1574. SSL *const ssl = hs->ssl;
  1575. if (!ssl_do_channel_id_callback(ssl)) {
  1576. return -1;
  1577. }
  1578. if (ssl->tlsext_channel_id_private == NULL) {
  1579. ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
  1580. return -1;
  1581. }
  1582. CBB cbb, body;
  1583. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
  1584. !tls1_write_channel_id(hs, &body) ||
  1585. !ssl_add_message_cbb(ssl, &cbb)) {
  1586. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1587. CBB_cleanup(&cbb);
  1588. return -1;
  1589. }
  1590. return 1;
  1591. }
  1592. static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) {
  1593. SSL *const ssl = hs->ssl;
  1594. int ret = ssl->method->ssl_get_message(ssl);
  1595. if (ret <= 0) {
  1596. return ret;
  1597. }
  1598. if (!ssl_check_message_type(ssl, SSL3_MT_NEW_SESSION_TICKET) ||
  1599. !ssl_hash_current_message(hs)) {
  1600. return -1;
  1601. }
  1602. CBS new_session_ticket, ticket;
  1603. uint32_t tlsext_tick_lifetime_hint;
  1604. CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
  1605. if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
  1606. !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
  1607. CBS_len(&new_session_ticket) != 0) {
  1608. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1609. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1610. return -1;
  1611. }
  1612. if (CBS_len(&ticket) == 0) {
  1613. /* RFC 5077 allows a server to change its mind and send no ticket after
  1614. * negotiating the extension. The value of |ticket_expected| is checked in
  1615. * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */
  1616. hs->ticket_expected = 0;
  1617. return 1;
  1618. }
  1619. int session_renewed = ssl->session != NULL;
  1620. SSL_SESSION *session = hs->new_session;
  1621. if (session_renewed) {
  1622. /* The server is sending a new ticket for an existing session. Sessions are
  1623. * immutable once established, so duplicate all but the ticket of the
  1624. * existing session. */
  1625. session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
  1626. if (session == NULL) {
  1627. /* This should never happen. */
  1628. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1629. goto err;
  1630. }
  1631. }
  1632. /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */
  1633. ssl_session_rebase_time(ssl, session);
  1634. if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
  1635. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1636. goto err;
  1637. }
  1638. session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
  1639. /* Generate a session ID for this session based on the session ticket. We use
  1640. * the session ID mechanism for detecting ticket resumption. This also fits in
  1641. * with assumptions elsewhere in OpenSSL.*/
  1642. if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
  1643. session->session_id, &session->session_id_length,
  1644. EVP_sha256(), NULL)) {
  1645. goto err;
  1646. }
  1647. if (session_renewed) {
  1648. session->not_resumable = 0;
  1649. SSL_SESSION_free(ssl->session);
  1650. ssl->session = session;
  1651. }
  1652. return 1;
  1653. err:
  1654. if (session_renewed) {
  1655. SSL_SESSION_free(session);
  1656. }
  1657. return -1;
  1658. }