handshake_client.cc 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  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 <limits.h>
  152. #include <string.h>
  153. #include <utility>
  154. #include <openssl/aead.h>
  155. #include <openssl/bn.h>
  156. #include <openssl/buf.h>
  157. #include <openssl/bytestring.h>
  158. #include <openssl/ec_key.h>
  159. #include <openssl/ecdsa.h>
  160. #include <openssl/err.h>
  161. #include <openssl/evp.h>
  162. #include <openssl/md5.h>
  163. #include <openssl/mem.h>
  164. #include <openssl/rand.h>
  165. #include "../crypto/internal.h"
  166. #include "internal.h"
  167. namespace bssl {
  168. enum ssl_client_hs_state_t {
  169. state_start_connect = 0,
  170. state_enter_early_data,
  171. state_read_hello_verify_request,
  172. state_read_server_hello,
  173. state_tls13,
  174. state_read_server_certificate,
  175. state_read_certificate_status,
  176. state_verify_server_certificate,
  177. state_read_server_key_exchange,
  178. state_read_certificate_request,
  179. state_read_server_hello_done,
  180. state_send_client_certificate,
  181. state_send_client_key_exchange,
  182. state_send_client_certificate_verify,
  183. state_send_client_finished,
  184. state_finish_flight,
  185. state_read_session_ticket,
  186. state_process_change_cipher_spec,
  187. state_read_server_finished,
  188. state_finish_client_handshake,
  189. state_done,
  190. };
  191. // ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
  192. // disabled algorithms.
  193. static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a,
  194. uint32_t *out_mask_k) {
  195. *out_mask_a = 0;
  196. *out_mask_k = 0;
  197. // PSK requires a client callback.
  198. if (ssl->psk_client_callback == NULL) {
  199. *out_mask_a |= SSL_aPSK;
  200. *out_mask_k |= SSL_kPSK;
  201. }
  202. }
  203. static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
  204. SSL *const ssl = hs->ssl;
  205. uint32_t mask_a, mask_k;
  206. ssl_get_client_disabled(ssl, &mask_a, &mask_k);
  207. CBB child;
  208. if (!CBB_add_u16_length_prefixed(out, &child)) {
  209. return 0;
  210. }
  211. // Add a fake cipher suite. See draft-davidben-tls-grease-01.
  212. if (ssl->ctx->grease_enabled &&
  213. !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) {
  214. return 0;
  215. }
  216. // Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
  217. // hardware support.
  218. if (hs->max_version >= TLS1_3_VERSION) {
  219. if (!EVP_has_aes_hardware() &&
  220. !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
  221. return 0;
  222. }
  223. if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
  224. !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
  225. return 0;
  226. }
  227. if (EVP_has_aes_hardware() &&
  228. !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
  229. return 0;
  230. }
  231. }
  232. if (hs->min_version < TLS1_3_VERSION) {
  233. int any_enabled = 0;
  234. for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
  235. // Skip disabled ciphers
  236. if ((cipher->algorithm_mkey & mask_k) ||
  237. (cipher->algorithm_auth & mask_a)) {
  238. continue;
  239. }
  240. if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
  241. SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
  242. continue;
  243. }
  244. any_enabled = 1;
  245. if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
  246. return 0;
  247. }
  248. }
  249. // If all ciphers were disabled, return the error to the caller.
  250. if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
  251. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
  252. return 0;
  253. }
  254. }
  255. // For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
  256. // added.
  257. if (hs->max_version == SSL3_VERSION &&
  258. !ssl->s3->initial_handshake_complete) {
  259. if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
  260. return 0;
  261. }
  262. }
  263. if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  264. if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  265. return 0;
  266. }
  267. }
  268. return CBB_flush(out);
  269. }
  270. int ssl_write_client_hello(SSL_HANDSHAKE *hs) {
  271. SSL *const ssl = hs->ssl;
  272. ScopedCBB cbb;
  273. CBB body;
  274. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
  275. return 0;
  276. }
  277. // Renegotiations do not participate in session resumption.
  278. int has_session_id = ssl->session != NULL &&
  279. !ssl->s3->initial_handshake_complete &&
  280. ssl->session->session_id_length > 0;
  281. CBB child;
  282. if (!CBB_add_u16(&body, hs->client_version) ||
  283. !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  284. !CBB_add_u8_length_prefixed(&body, &child)) {
  285. return 0;
  286. }
  287. if (has_session_id) {
  288. if (!CBB_add_bytes(&child, ssl->session->session_id,
  289. ssl->session->session_id_length)) {
  290. return 0;
  291. }
  292. } else {
  293. // In TLS 1.3 experimental encodings, send a fake placeholder session ID
  294. // when we do not otherwise have one to send.
  295. if (hs->max_version >= TLS1_3_VERSION &&
  296. ssl_is_resumption_variant(ssl->tls13_variant) &&
  297. !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
  298. return 0;
  299. }
  300. }
  301. if (SSL_is_dtls(ssl)) {
  302. if (!CBB_add_u8_length_prefixed(&body, &child) ||
  303. !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
  304. return 0;
  305. }
  306. }
  307. size_t header_len =
  308. SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
  309. if (!ssl_write_client_cipher_list(hs, &body) ||
  310. !CBB_add_u8(&body, 1 /* one compression method */) ||
  311. !CBB_add_u8(&body, 0 /* null compression */) ||
  312. !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
  313. return 0;
  314. }
  315. Array<uint8_t> msg;
  316. if (!ssl->method->finish_message(ssl, cbb.get(), &msg)) {
  317. return 0;
  318. }
  319. // Now that the length prefixes have been computed, fill in the placeholder
  320. // PSK binder.
  321. if (hs->needs_psk_binder &&
  322. !tls13_write_psk_binder(hs, msg.data(), msg.size())) {
  323. return 0;
  324. }
  325. return ssl->method->add_message(ssl, std::move(msg));
  326. }
  327. static int parse_server_version(SSL_HANDSHAKE *hs, uint16_t *out,
  328. const SSLMessage &msg) {
  329. SSL *const ssl = hs->ssl;
  330. if (msg.type != SSL3_MT_SERVER_HELLO &&
  331. msg.type != SSL3_MT_HELLO_RETRY_REQUEST) {
  332. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  333. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  334. return 0;
  335. }
  336. CBS server_hello = msg.body;
  337. if (!CBS_get_u16(&server_hello, out)) {
  338. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  339. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  340. return 0;
  341. }
  342. // The server version may also be in the supported_versions extension if
  343. // applicable.
  344. if (msg.type != SSL3_MT_SERVER_HELLO || *out != TLS1_2_VERSION) {
  345. return 1;
  346. }
  347. uint8_t sid_length;
  348. if (!CBS_skip(&server_hello, SSL3_RANDOM_SIZE) ||
  349. !CBS_get_u8(&server_hello, &sid_length) ||
  350. !CBS_skip(&server_hello, sid_length + 2 /* cipher_suite */ +
  351. 1 /* compression_method */)) {
  352. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  353. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  354. return 0;
  355. }
  356. // The extensions block may not be present.
  357. if (CBS_len(&server_hello) == 0) {
  358. return 1;
  359. }
  360. CBS extensions;
  361. if (!CBS_get_u16_length_prefixed(&server_hello, &extensions) ||
  362. CBS_len(&server_hello) != 0) {
  363. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  364. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  365. return 0;
  366. }
  367. bool have_supported_versions;
  368. CBS supported_versions;
  369. const SSL_EXTENSION_TYPE ext_types[] = {
  370. {TLSEXT_TYPE_supported_versions, &have_supported_versions,
  371. &supported_versions},
  372. };
  373. uint8_t alert = SSL_AD_DECODE_ERROR;
  374. if (!ssl_parse_extensions(&extensions, &alert, ext_types,
  375. OPENSSL_ARRAY_SIZE(ext_types),
  376. 1 /* ignore unknown */)) {
  377. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  378. return 0;
  379. }
  380. if (have_supported_versions &&
  381. (!CBS_get_u16(&supported_versions, out) ||
  382. CBS_len(&supported_versions) != 0)) {
  383. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  384. return 0;
  385. }
  386. return 1;
  387. }
  388. static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) {
  389. SSL *const ssl = hs->ssl;
  390. ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
  391. // |session_reused| must be reset in case this is a renegotiation.
  392. ssl->s3->session_reused = false;
  393. // Freeze the version range.
  394. if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
  395. return ssl_hs_error;
  396. }
  397. // SSL 3.0 ClientHellos should use SSL 3.0 not TLS 1.0, for the record-layer
  398. // version.
  399. if (hs->max_version == SSL3_VERSION) {
  400. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(SSL3_VERSION);
  401. }
  402. // Always advertise the ClientHello version from the original maximum version,
  403. // even on renegotiation. The static RSA key exchange uses this field, and
  404. // some servers fail when it changes across handshakes.
  405. if (SSL_is_dtls(hs->ssl)) {
  406. hs->client_version =
  407. hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
  408. } else {
  409. hs->client_version =
  410. hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
  411. }
  412. // If the configured session has expired or was created at a disabled
  413. // version, drop it.
  414. if (ssl->session != NULL) {
  415. if (ssl->session->is_server ||
  416. !ssl_supports_version(hs, ssl->session->ssl_version) ||
  417. (ssl->session->session_id_length == 0 &&
  418. ssl->session->tlsext_ticklen == 0) ||
  419. ssl->session->not_resumable ||
  420. !ssl_session_is_time_valid(ssl, ssl->session)) {
  421. ssl_set_session(ssl, NULL);
  422. }
  423. }
  424. if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
  425. return ssl_hs_error;
  426. }
  427. // Initialize a random session ID for the experimental TLS 1.3 variant
  428. // requiring a session id.
  429. if (ssl_is_resumption_variant(ssl->tls13_variant)) {
  430. hs->session_id_len = sizeof(hs->session_id);
  431. if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
  432. return ssl_hs_error;
  433. }
  434. }
  435. if (!ssl_write_client_hello(hs)) {
  436. return ssl_hs_error;
  437. }
  438. hs->state = state_enter_early_data;
  439. return ssl_hs_flush;
  440. }
  441. static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
  442. SSL *const ssl = hs->ssl;
  443. if (SSL_is_dtls(ssl)) {
  444. hs->state = state_read_hello_verify_request;
  445. return ssl_hs_ok;
  446. }
  447. if (!hs->early_data_offered) {
  448. hs->state = state_read_server_hello;
  449. return ssl_hs_ok;
  450. }
  451. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->session->ssl_version);
  452. if (ssl_is_draft22(ssl->session->ssl_version) &&
  453. !ssl->method->add_change_cipher_spec(ssl)) {
  454. return ssl_hs_error;
  455. }
  456. if (!tls13_init_early_key_schedule(hs, ssl->session->master_key,
  457. ssl->session->master_key_length) ||
  458. !tls13_derive_early_secrets(hs) ||
  459. !tls13_set_traffic_key(ssl, evp_aead_seal, hs->early_traffic_secret,
  460. hs->hash_len)) {
  461. return ssl_hs_error;
  462. }
  463. // Stash the early data session, so connection properties may be queried out
  464. // of it.
  465. hs->in_early_data = true;
  466. SSL_SESSION_up_ref(ssl->session);
  467. hs->early_session.reset(ssl->session);
  468. hs->can_early_write = true;
  469. hs->state = state_read_server_hello;
  470. return ssl_hs_early_return;
  471. }
  472. static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
  473. SSL *const ssl = hs->ssl;
  474. assert(SSL_is_dtls(ssl));
  475. SSLMessage msg;
  476. if (!ssl->method->get_message(ssl, &msg)) {
  477. return ssl_hs_read_message;
  478. }
  479. if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  480. hs->state = state_read_server_hello;
  481. return ssl_hs_ok;
  482. }
  483. CBS hello_verify_request = msg.body, cookie;
  484. uint16_t server_version;
  485. if (!CBS_get_u16(&hello_verify_request, &server_version) ||
  486. !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
  487. CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
  488. CBS_len(&hello_verify_request) != 0) {
  489. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  490. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  491. return ssl_hs_error;
  492. }
  493. OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
  494. ssl->d1->cookie_len = CBS_len(&cookie);
  495. ssl->method->next_message(ssl);
  496. // DTLS resets the handshake buffer after HelloVerifyRequest.
  497. if (!hs->transcript.Init()) {
  498. return ssl_hs_error;
  499. }
  500. if (!ssl_write_client_hello(hs)) {
  501. return ssl_hs_error;
  502. }
  503. hs->state = state_read_server_hello;
  504. return ssl_hs_flush;
  505. }
  506. static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
  507. SSL *const ssl = hs->ssl;
  508. SSLMessage msg;
  509. if (!ssl->method->get_message(ssl, &msg)) {
  510. return ssl_hs_read_server_hello;
  511. }
  512. uint16_t server_version;
  513. if (!parse_server_version(hs, &server_version, msg)) {
  514. return ssl_hs_error;
  515. }
  516. if (!ssl_supports_version(hs, server_version)) {
  517. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
  518. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  519. return ssl_hs_error;
  520. }
  521. assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
  522. if (!ssl->s3->have_version) {
  523. ssl->version = server_version;
  524. // At this point, the connection's version is known and ssl->version is
  525. // fixed. Begin enforcing the record-layer version.
  526. ssl->s3->have_version = true;
  527. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
  528. } else if (server_version != ssl->version) {
  529. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  530. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  531. return ssl_hs_error;
  532. }
  533. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  534. hs->state = state_tls13;
  535. return ssl_hs_ok;
  536. }
  537. // Clear some TLS 1.3 state that no longer needs to be retained.
  538. hs->key_share.reset();
  539. hs->key_share_bytes.Reset();
  540. // A TLS 1.2 server would not know to skip the early data we offered. Report
  541. // an error code sooner. The caller may use this error code to implement the
  542. // fallback described in draft-ietf-tls-tls13-18 appendix C.3.
  543. if (hs->early_data_offered) {
  544. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
  545. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
  546. return ssl_hs_error;
  547. }
  548. if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
  549. return ssl_hs_error;
  550. }
  551. CBS server_hello = msg.body, server_random, session_id;
  552. uint16_t cipher_suite;
  553. uint8_t compression_method;
  554. if (!CBS_skip(&server_hello, 2 /* version */) ||
  555. !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
  556. !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
  557. CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
  558. !CBS_get_u16(&server_hello, &cipher_suite) ||
  559. !CBS_get_u8(&server_hello, &compression_method)) {
  560. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  561. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  562. return ssl_hs_error;
  563. }
  564. // Copy over the server random.
  565. OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
  566. SSL3_RANDOM_SIZE);
  567. // TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
  568. // 1.3 is finalized and we are not implementing a draft version.
  569. if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
  570. ssl->session->session_id_length != 0 &&
  571. CBS_mem_equal(&session_id, ssl->session->session_id,
  572. ssl->session->session_id_length)) {
  573. ssl->s3->session_reused = true;
  574. } else {
  575. // The session wasn't resumed. Create a fresh SSL_SESSION to
  576. // fill out.
  577. ssl_set_session(ssl, NULL);
  578. if (!ssl_get_new_session(hs, 0 /* client */)) {
  579. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  580. return ssl_hs_error;
  581. }
  582. // Note: session_id could be empty.
  583. hs->new_session->session_id_length = CBS_len(&session_id);
  584. OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
  585. CBS_len(&session_id));
  586. }
  587. const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
  588. if (cipher == NULL) {
  589. // unknown cipher
  590. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
  591. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  592. return ssl_hs_error;
  593. }
  594. // The cipher must be allowed in the selected version and enabled.
  595. uint32_t mask_a, mask_k;
  596. ssl_get_client_disabled(ssl, &mask_a, &mask_k);
  597. if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) ||
  598. SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
  599. SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl) ||
  600. !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) {
  601. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
  602. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  603. return ssl_hs_error;
  604. }
  605. if (ssl->session != NULL) {
  606. if (ssl->session->ssl_version != ssl->version) {
  607. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
  608. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  609. return ssl_hs_error;
  610. }
  611. if (ssl->session->cipher != cipher) {
  612. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  613. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  614. return ssl_hs_error;
  615. }
  616. if (!ssl_session_is_context_valid(ssl, ssl->session)) {
  617. // This is actually a client application bug.
  618. OPENSSL_PUT_ERROR(SSL,
  619. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  620. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  621. return ssl_hs_error;
  622. }
  623. } else {
  624. hs->new_session->cipher = cipher;
  625. }
  626. hs->new_cipher = cipher;
  627. // Now that the cipher is known, initialize the handshake hash and hash the
  628. // ServerHello.
  629. if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
  630. !ssl_hash_message(hs, msg)) {
  631. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  632. return ssl_hs_error;
  633. }
  634. // If doing a full handshake, the server may request a client certificate
  635. // which requires hashing the handshake transcript. Otherwise, the handshake
  636. // buffer may be released.
  637. if (ssl->session != NULL ||
  638. !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  639. hs->transcript.FreeBuffer();
  640. }
  641. // Only the NULL compression algorithm is supported.
  642. if (compression_method != 0) {
  643. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  644. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  645. return ssl_hs_error;
  646. }
  647. // TLS extensions
  648. if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
  649. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  650. return ssl_hs_error;
  651. }
  652. // There should be nothing left over in the record.
  653. if (CBS_len(&server_hello) != 0) {
  654. // wrong packet length
  655. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  656. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  657. return ssl_hs_error;
  658. }
  659. if (ssl->session != NULL &&
  660. hs->extended_master_secret != ssl->session->extended_master_secret) {
  661. if (ssl->session->extended_master_secret) {
  662. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  663. } else {
  664. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
  665. }
  666. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  667. return ssl_hs_error;
  668. }
  669. ssl->method->next_message(ssl);
  670. if (ssl->session != NULL) {
  671. hs->state = state_read_session_ticket;
  672. return ssl_hs_ok;
  673. }
  674. hs->state = state_read_server_certificate;
  675. return ssl_hs_ok;
  676. }
  677. static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
  678. enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
  679. if (wait == ssl_hs_ok) {
  680. hs->state = state_finish_client_handshake;
  681. return ssl_hs_ok;
  682. }
  683. return wait;
  684. }
  685. static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
  686. SSL *const ssl = hs->ssl;
  687. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  688. hs->state = state_read_certificate_status;
  689. return ssl_hs_ok;
  690. }
  691. SSLMessage msg;
  692. if (!ssl->method->get_message(ssl, &msg)) {
  693. return ssl_hs_read_message;
  694. }
  695. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
  696. !ssl_hash_message(hs, msg)) {
  697. return ssl_hs_error;
  698. }
  699. CBS body = msg.body;
  700. uint8_t alert = SSL_AD_DECODE_ERROR;
  701. UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
  702. if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey, NULL, &body,
  703. ssl->ctx->pool)) {
  704. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  705. return ssl_hs_error;
  706. }
  707. sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
  708. hs->new_session->certs = chain.release();
  709. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 ||
  710. CBS_len(&body) != 0 ||
  711. !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
  712. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  713. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  714. return ssl_hs_error;
  715. }
  716. if (!ssl_check_leaf_certificate(
  717. hs, hs->peer_pubkey.get(),
  718. sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) {
  719. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  720. return ssl_hs_error;
  721. }
  722. ssl->method->next_message(ssl);
  723. hs->state = state_read_certificate_status;
  724. return ssl_hs_ok;
  725. }
  726. static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
  727. SSL *const ssl = hs->ssl;
  728. if (!hs->certificate_status_expected) {
  729. hs->state = state_verify_server_certificate;
  730. return ssl_hs_ok;
  731. }
  732. SSLMessage msg;
  733. if (!ssl->method->get_message(ssl, &msg)) {
  734. return ssl_hs_read_message;
  735. }
  736. if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
  737. // A server may send status_request in ServerHello and then change its mind
  738. // about sending CertificateStatus.
  739. hs->state = state_verify_server_certificate;
  740. return ssl_hs_ok;
  741. }
  742. if (!ssl_hash_message(hs, msg)) {
  743. return ssl_hs_error;
  744. }
  745. CBS certificate_status = msg.body, ocsp_response;
  746. uint8_t status_type;
  747. if (!CBS_get_u8(&certificate_status, &status_type) ||
  748. status_type != TLSEXT_STATUSTYPE_ocsp ||
  749. !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
  750. CBS_len(&ocsp_response) == 0 ||
  751. CBS_len(&certificate_status) != 0) {
  752. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  753. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  754. return ssl_hs_error;
  755. }
  756. CRYPTO_BUFFER_free(hs->new_session->ocsp_response);
  757. hs->new_session->ocsp_response =
  758. CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool);
  759. if (hs->new_session->ocsp_response == nullptr) {
  760. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  761. return ssl_hs_error;
  762. }
  763. ssl->method->next_message(ssl);
  764. hs->state = state_verify_server_certificate;
  765. return ssl_hs_ok;
  766. }
  767. static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
  768. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  769. hs->state = state_read_server_key_exchange;
  770. return ssl_hs_ok;
  771. }
  772. switch (ssl_verify_peer_cert(hs)) {
  773. case ssl_verify_ok:
  774. break;
  775. case ssl_verify_invalid:
  776. return ssl_hs_error;
  777. case ssl_verify_retry:
  778. hs->state = state_verify_server_certificate;
  779. return ssl_hs_certificate_verify;
  780. }
  781. hs->state = state_read_server_key_exchange;
  782. return ssl_hs_ok;
  783. }
  784. static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
  785. SSL *const ssl = hs->ssl;
  786. SSLMessage msg;
  787. if (!ssl->method->get_message(ssl, &msg)) {
  788. return ssl_hs_read_message;
  789. }
  790. if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
  791. // Some ciphers (pure PSK) have an optional ServerKeyExchange message.
  792. if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
  793. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  794. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  795. return ssl_hs_error;
  796. }
  797. hs->state = state_read_certificate_request;
  798. return ssl_hs_ok;
  799. }
  800. if (!ssl_hash_message(hs, msg)) {
  801. return ssl_hs_error;
  802. }
  803. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  804. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  805. CBS server_key_exchange = msg.body;
  806. if (alg_a & SSL_aPSK) {
  807. CBS psk_identity_hint;
  808. // Each of the PSK key exchanges begins with a psk_identity_hint.
  809. if (!CBS_get_u16_length_prefixed(&server_key_exchange,
  810. &psk_identity_hint)) {
  811. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  812. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  813. return ssl_hs_error;
  814. }
  815. // Store the PSK identity hint for the ClientKeyExchange. Assume that the
  816. // maximum length of a PSK identity hint can be as long as the maximum
  817. // length of a PSK identity. Also do not allow NULL characters; identities
  818. // are saved as C strings.
  819. //
  820. // TODO(davidben): Should invalid hints be ignored? It's a hint rather than
  821. // a specific identity.
  822. if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
  823. CBS_contains_zero_byte(&psk_identity_hint)) {
  824. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  825. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  826. return ssl_hs_error;
  827. }
  828. // Save non-empty identity hints as a C string. Empty identity hints we
  829. // treat as missing. Plain PSK makes it possible to send either no hint
  830. // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
  831. // empty hint. Having different capabilities is odd, so we interpret empty
  832. // and missing as identical.
  833. char *raw = nullptr;
  834. if (CBS_len(&psk_identity_hint) != 0 &&
  835. !CBS_strdup(&psk_identity_hint, &raw)) {
  836. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  837. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  838. return ssl_hs_error;
  839. }
  840. hs->peer_psk_identity_hint.reset(raw);
  841. }
  842. if (alg_k & SSL_kECDHE) {
  843. // Parse the server parameters.
  844. uint8_t group_type;
  845. uint16_t group_id;
  846. CBS point;
  847. if (!CBS_get_u8(&server_key_exchange, &group_type) ||
  848. group_type != NAMED_CURVE_TYPE ||
  849. !CBS_get_u16(&server_key_exchange, &group_id) ||
  850. !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
  851. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  852. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  853. return ssl_hs_error;
  854. }
  855. hs->new_session->group_id = group_id;
  856. // Ensure the group is consistent with preferences.
  857. if (!tls1_check_group_id(ssl, group_id)) {
  858. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  859. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  860. return ssl_hs_error;
  861. }
  862. // Initialize ECDH and save the peer public key for later.
  863. hs->key_share = SSLKeyShare::Create(group_id);
  864. if (!hs->key_share ||
  865. !hs->peer_key.CopyFrom(point)) {
  866. return ssl_hs_error;
  867. }
  868. } else if (!(alg_k & SSL_kPSK)) {
  869. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  870. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  871. return ssl_hs_error;
  872. }
  873. // At this point, |server_key_exchange| contains the signature, if any, while
  874. // |msg.body| contains the entire message. From that, derive a CBS containing
  875. // just the parameter.
  876. CBS parameter;
  877. CBS_init(&parameter, CBS_data(&msg.body),
  878. CBS_len(&msg.body) - CBS_len(&server_key_exchange));
  879. // ServerKeyExchange should be signed by the server's public key.
  880. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  881. uint16_t signature_algorithm = 0;
  882. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  883. if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
  884. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  885. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  886. return ssl_hs_error;
  887. }
  888. uint8_t alert = SSL_AD_DECODE_ERROR;
  889. if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
  890. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  891. return ssl_hs_error;
  892. }
  893. hs->new_session->peer_signature_algorithm = signature_algorithm;
  894. } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
  895. hs->peer_pubkey.get())) {
  896. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  897. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
  898. return ssl_hs_error;
  899. }
  900. // The last field in |server_key_exchange| is the signature.
  901. CBS signature;
  902. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
  903. CBS_len(&server_key_exchange) != 0) {
  904. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  905. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  906. return ssl_hs_error;
  907. }
  908. ScopedCBB transcript;
  909. Array<uint8_t> transcript_data;
  910. if (!CBB_init(transcript.get(),
  911. 2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
  912. !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
  913. SSL3_RANDOM_SIZE) ||
  914. !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
  915. SSL3_RANDOM_SIZE) ||
  916. !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
  917. CBS_len(&parameter)) ||
  918. !CBBFinishArray(transcript.get(), &transcript_data)) {
  919. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  920. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  921. return ssl_hs_error;
  922. }
  923. bool sig_ok = ssl_public_key_verify(ssl, signature, signature_algorithm,
  924. hs->peer_pubkey.get(), transcript_data);
  925. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  926. sig_ok = true;
  927. ERR_clear_error();
  928. #endif
  929. if (!sig_ok) {
  930. // bad signature
  931. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  932. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  933. return ssl_hs_error;
  934. }
  935. } else {
  936. // PSK ciphers are the only supported certificate-less ciphers.
  937. assert(alg_a == SSL_aPSK);
  938. if (CBS_len(&server_key_exchange) > 0) {
  939. OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
  940. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  941. return ssl_hs_error;
  942. }
  943. }
  944. ssl->method->next_message(ssl);
  945. hs->state = state_read_certificate_request;
  946. return ssl_hs_ok;
  947. }
  948. static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
  949. SSL *const ssl = hs->ssl;
  950. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  951. hs->state = state_read_server_hello_done;
  952. return ssl_hs_ok;
  953. }
  954. SSLMessage msg;
  955. if (!ssl->method->get_message(ssl, &msg)) {
  956. return ssl_hs_read_message;
  957. }
  958. if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
  959. // If we get here we don't need the handshake buffer as we won't be doing
  960. // client auth.
  961. hs->transcript.FreeBuffer();
  962. hs->state = state_read_server_hello_done;
  963. return ssl_hs_ok;
  964. }
  965. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
  966. !ssl_hash_message(hs, msg)) {
  967. return ssl_hs_error;
  968. }
  969. // Get the certificate types.
  970. CBS body = msg.body, certificate_types;
  971. if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
  972. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  973. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  974. return ssl_hs_error;
  975. }
  976. if (!hs->certificate_types.CopyFrom(certificate_types)) {
  977. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  978. return ssl_hs_error;
  979. }
  980. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  981. CBS supported_signature_algorithms;
  982. if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
  983. !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
  984. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  985. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  986. return ssl_hs_error;
  987. }
  988. }
  989. uint8_t alert = SSL_AD_DECODE_ERROR;
  990. UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
  991. ssl_parse_client_CA_list(ssl, &alert, &body);
  992. if (!ca_names) {
  993. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  994. return ssl_hs_error;
  995. }
  996. if (CBS_len(&body) != 0) {
  997. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  998. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  999. return ssl_hs_error;
  1000. }
  1001. hs->cert_request = true;
  1002. hs->ca_names = std::move(ca_names);
  1003. ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
  1004. ssl->method->next_message(ssl);
  1005. hs->state = state_read_server_hello_done;
  1006. return ssl_hs_ok;
  1007. }
  1008. static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
  1009. SSL *const ssl = hs->ssl;
  1010. SSLMessage msg;
  1011. if (!ssl->method->get_message(ssl, &msg)) {
  1012. return ssl_hs_read_message;
  1013. }
  1014. if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
  1015. !ssl_hash_message(hs, msg)) {
  1016. return ssl_hs_error;
  1017. }
  1018. // ServerHelloDone is empty.
  1019. if (CBS_len(&msg.body) != 0) {
  1020. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1021. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1022. return ssl_hs_error;
  1023. }
  1024. ssl->method->next_message(ssl);
  1025. hs->state = state_send_client_certificate;
  1026. return ssl_hs_ok;
  1027. }
  1028. static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
  1029. SSL *const ssl = hs->ssl;
  1030. // The peer didn't request a certificate.
  1031. if (!hs->cert_request) {
  1032. hs->state = state_send_client_key_exchange;
  1033. return ssl_hs_ok;
  1034. }
  1035. // Call cert_cb to update the certificate.
  1036. if (ssl->cert->cert_cb != NULL) {
  1037. int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
  1038. if (rv == 0) {
  1039. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1040. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
  1041. return ssl_hs_error;
  1042. }
  1043. if (rv < 0) {
  1044. hs->state = state_send_client_certificate;
  1045. return ssl_hs_x509_lookup;
  1046. }
  1047. }
  1048. if (!ssl_has_certificate(ssl)) {
  1049. // Without a client certificate, the handshake buffer may be released.
  1050. hs->transcript.FreeBuffer();
  1051. // In SSL 3.0, the Certificate message is replaced with a warning alert.
  1052. if (ssl->version == SSL3_VERSION) {
  1053. if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING,
  1054. SSL_AD_NO_CERTIFICATE)) {
  1055. return ssl_hs_error;
  1056. }
  1057. hs->state = state_send_client_key_exchange;
  1058. return ssl_hs_ok;
  1059. }
  1060. }
  1061. if (!ssl_on_certificate_selected(hs) ||
  1062. !ssl_output_cert_chain(ssl)) {
  1063. return ssl_hs_error;
  1064. }
  1065. hs->state = state_send_client_key_exchange;
  1066. return ssl_hs_ok;
  1067. }
  1068. static_assert(sizeof(size_t) >= sizeof(unsigned),
  1069. "size_t is smaller than unsigned");
  1070. static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
  1071. SSL *const ssl = hs->ssl;
  1072. ScopedCBB cbb;
  1073. CBB body;
  1074. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1075. SSL3_MT_CLIENT_KEY_EXCHANGE)) {
  1076. return ssl_hs_error;
  1077. }
  1078. Array<uint8_t> pms;
  1079. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  1080. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  1081. // If using a PSK key exchange, prepare the pre-shared key.
  1082. unsigned psk_len = 0;
  1083. uint8_t psk[PSK_MAX_PSK_LEN];
  1084. if (alg_a & SSL_aPSK) {
  1085. if (ssl->psk_client_callback == NULL) {
  1086. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
  1087. return ssl_hs_error;
  1088. }
  1089. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1090. OPENSSL_memset(identity, 0, sizeof(identity));
  1091. psk_len =
  1092. ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint.get(),
  1093. identity, sizeof(identity), psk, sizeof(psk));
  1094. if (psk_len == 0) {
  1095. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1096. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1097. return ssl_hs_error;
  1098. }
  1099. assert(psk_len <= PSK_MAX_PSK_LEN);
  1100. OPENSSL_free(hs->new_session->psk_identity);
  1101. hs->new_session->psk_identity = BUF_strdup(identity);
  1102. if (hs->new_session->psk_identity == NULL) {
  1103. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1104. return ssl_hs_error;
  1105. }
  1106. // Write out psk_identity.
  1107. CBB child;
  1108. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1109. !CBB_add_bytes(&child, (const uint8_t *)identity,
  1110. OPENSSL_strnlen(identity, sizeof(identity))) ||
  1111. !CBB_flush(&body)) {
  1112. return ssl_hs_error;
  1113. }
  1114. }
  1115. // Depending on the key exchange method, compute |pms|.
  1116. if (alg_k & SSL_kRSA) {
  1117. if (!pms.Init(SSL_MAX_MASTER_KEY_LENGTH)) {
  1118. return ssl_hs_error;
  1119. }
  1120. RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
  1121. if (rsa == NULL) {
  1122. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1123. return ssl_hs_error;
  1124. }
  1125. pms[0] = hs->client_version >> 8;
  1126. pms[1] = hs->client_version & 0xff;
  1127. if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
  1128. return ssl_hs_error;
  1129. }
  1130. CBB child, *enc_pms = &body;
  1131. size_t enc_pms_len;
  1132. // In TLS, there is a length prefix.
  1133. if (ssl->version > SSL3_VERSION) {
  1134. if (!CBB_add_u16_length_prefixed(&body, &child)) {
  1135. return ssl_hs_error;
  1136. }
  1137. enc_pms = &child;
  1138. }
  1139. uint8_t *ptr;
  1140. if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
  1141. !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms.data(),
  1142. pms.size(), RSA_PKCS1_PADDING) ||
  1143. !CBB_did_write(enc_pms, enc_pms_len) ||
  1144. !CBB_flush(&body)) {
  1145. return ssl_hs_error;
  1146. }
  1147. } else if (alg_k & SSL_kECDHE) {
  1148. // Generate a keypair and serialize the public half.
  1149. CBB child;
  1150. if (!CBB_add_u8_length_prefixed(&body, &child)) {
  1151. return ssl_hs_error;
  1152. }
  1153. // Compute the premaster.
  1154. uint8_t alert = SSL_AD_DECODE_ERROR;
  1155. if (!hs->key_share->Accept(&child, &pms, &alert, hs->peer_key)) {
  1156. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1157. return ssl_hs_error;
  1158. }
  1159. if (!CBB_flush(&body)) {
  1160. return ssl_hs_error;
  1161. }
  1162. // The key exchange state may now be discarded.
  1163. hs->key_share.reset();
  1164. hs->peer_key.Reset();
  1165. } else if (alg_k & SSL_kPSK) {
  1166. // For plain PSK, other_secret is a block of 0s with the same length as
  1167. // the pre-shared key.
  1168. if (!pms.Init(psk_len)) {
  1169. return ssl_hs_error;
  1170. }
  1171. OPENSSL_memset(pms.data(), 0, pms.size());
  1172. } else {
  1173. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1174. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1175. return ssl_hs_error;
  1176. }
  1177. // For a PSK cipher suite, other_secret is combined with the pre-shared
  1178. // key.
  1179. if (alg_a & SSL_aPSK) {
  1180. ScopedCBB pms_cbb;
  1181. CBB child;
  1182. if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms.size()) ||
  1183. !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
  1184. !CBB_add_bytes(&child, pms.data(), pms.size()) ||
  1185. !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
  1186. !CBB_add_bytes(&child, psk, psk_len) ||
  1187. !CBBFinishArray(pms_cbb.get(), &pms)) {
  1188. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1189. return ssl_hs_error;
  1190. }
  1191. }
  1192. // The message must be added to the finished hash before calculating the
  1193. // master secret.
  1194. if (!ssl_add_message_cbb(ssl, cbb.get())) {
  1195. return ssl_hs_error;
  1196. }
  1197. hs->new_session->master_key_length =
  1198. tls1_generate_master_secret(hs, hs->new_session->master_key, pms);
  1199. if (hs->new_session->master_key_length == 0) {
  1200. return ssl_hs_error;
  1201. }
  1202. hs->new_session->extended_master_secret = hs->extended_master_secret;
  1203. hs->state = state_send_client_certificate_verify;
  1204. return ssl_hs_ok;
  1205. }
  1206. static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
  1207. SSL *const ssl = hs->ssl;
  1208. if (!hs->cert_request || !ssl_has_certificate(ssl)) {
  1209. hs->state = state_send_client_finished;
  1210. return ssl_hs_ok;
  1211. }
  1212. assert(ssl_has_private_key(ssl));
  1213. ScopedCBB cbb;
  1214. CBB body, child;
  1215. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1216. SSL3_MT_CERTIFICATE_VERIFY)) {
  1217. return ssl_hs_error;
  1218. }
  1219. uint16_t signature_algorithm;
  1220. if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
  1221. return ssl_hs_error;
  1222. }
  1223. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  1224. // Write out the digest type in TLS 1.2.
  1225. if (!CBB_add_u16(&body, signature_algorithm)) {
  1226. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1227. return ssl_hs_error;
  1228. }
  1229. }
  1230. // Set aside space for the signature.
  1231. const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
  1232. uint8_t *ptr;
  1233. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1234. !CBB_reserve(&child, &ptr, max_sig_len)) {
  1235. return ssl_hs_error;
  1236. }
  1237. size_t sig_len = max_sig_len;
  1238. // The SSL3 construction for CertificateVerify does not decompose into a
  1239. // single final digest and signature, and must be special-cased.
  1240. if (ssl_protocol_version(ssl) == SSL3_VERSION) {
  1241. if (ssl->cert->key_method != NULL) {
  1242. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
  1243. return ssl_hs_error;
  1244. }
  1245. uint8_t digest[EVP_MAX_MD_SIZE];
  1246. size_t digest_len;
  1247. if (!hs->transcript.GetSSL3CertVerifyHash(
  1248. digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
  1249. return ssl_hs_error;
  1250. }
  1251. UniquePtr<EVP_PKEY_CTX> pctx(EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL));
  1252. if (!pctx ||
  1253. !EVP_PKEY_sign_init(pctx.get()) ||
  1254. !EVP_PKEY_sign(pctx.get(), ptr, &sig_len, digest, digest_len)) {
  1255. return ssl_hs_error;
  1256. }
  1257. } else {
  1258. switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
  1259. signature_algorithm,
  1260. hs->transcript.buffer())) {
  1261. case ssl_private_key_success:
  1262. break;
  1263. case ssl_private_key_failure:
  1264. return ssl_hs_error;
  1265. case ssl_private_key_retry:
  1266. hs->state = state_send_client_certificate_verify;
  1267. return ssl_hs_private_key_operation;
  1268. }
  1269. }
  1270. if (!CBB_did_write(&child, sig_len) ||
  1271. !ssl_add_message_cbb(ssl, cbb.get())) {
  1272. return ssl_hs_error;
  1273. }
  1274. // The handshake buffer is no longer necessary.
  1275. hs->transcript.FreeBuffer();
  1276. hs->state = state_send_client_finished;
  1277. return ssl_hs_ok;
  1278. }
  1279. static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
  1280. SSL *const ssl = hs->ssl;
  1281. // Resolve Channel ID first, before any non-idempotent operations.
  1282. if (ssl->s3->tlsext_channel_id_valid) {
  1283. if (!ssl_do_channel_id_callback(ssl)) {
  1284. return ssl_hs_error;
  1285. }
  1286. if (ssl->tlsext_channel_id_private == NULL) {
  1287. hs->state = state_send_client_finished;
  1288. return ssl_hs_channel_id_lookup;
  1289. }
  1290. }
  1291. if (!ssl->method->add_change_cipher_spec(ssl) ||
  1292. !tls1_change_cipher_state(hs, evp_aead_seal)) {
  1293. return ssl_hs_error;
  1294. }
  1295. if (hs->next_proto_neg_seen) {
  1296. static const uint8_t kZero[32] = {0};
  1297. size_t padding_len =
  1298. 32 - ((ssl->s3->next_proto_negotiated.size() + 2) % 32);
  1299. ScopedCBB cbb;
  1300. CBB body, child;
  1301. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
  1302. !CBB_add_u8_length_prefixed(&body, &child) ||
  1303. !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated.data(),
  1304. ssl->s3->next_proto_negotiated.size()) ||
  1305. !CBB_add_u8_length_prefixed(&body, &child) ||
  1306. !CBB_add_bytes(&child, kZero, padding_len) ||
  1307. !ssl_add_message_cbb(ssl, cbb.get())) {
  1308. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1309. return ssl_hs_error;
  1310. }
  1311. }
  1312. if (ssl->s3->tlsext_channel_id_valid) {
  1313. ScopedCBB cbb;
  1314. CBB body;
  1315. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
  1316. !tls1_write_channel_id(hs, &body) ||
  1317. !ssl_add_message_cbb(ssl, cbb.get())) {
  1318. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1319. return ssl_hs_error;
  1320. }
  1321. }
  1322. if (!ssl_send_finished(hs)) {
  1323. return ssl_hs_error;
  1324. }
  1325. hs->state = state_finish_flight;
  1326. return ssl_hs_flush;
  1327. }
  1328. static bool can_false_start(const SSL_HANDSHAKE *hs) {
  1329. SSL *const ssl = hs->ssl;
  1330. // False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN.
  1331. return !SSL_is_dtls(ssl) &&
  1332. SSL_version(ssl) == TLS1_2_VERSION &&
  1333. (!ssl->s3->alpn_selected.empty() ||
  1334. !ssl->s3->next_proto_negotiated.empty()) &&
  1335. hs->new_cipher->algorithm_mkey == SSL_kECDHE &&
  1336. hs->new_cipher->algorithm_mac == SSL_AEAD;
  1337. }
  1338. static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
  1339. SSL *const ssl = hs->ssl;
  1340. if (ssl->session != NULL) {
  1341. hs->state = state_finish_client_handshake;
  1342. return ssl_hs_ok;
  1343. }
  1344. // This is a full handshake. If it involves ChannelID, then record the
  1345. // handshake hashes at this point in the session so that any resumption of
  1346. // this session with ChannelID can sign those hashes.
  1347. if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
  1348. return ssl_hs_error;
  1349. }
  1350. hs->state = state_read_session_ticket;
  1351. if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
  1352. can_false_start(hs) &&
  1353. // No False Start on renegotiation (would complicate the state machine).
  1354. !ssl->s3->initial_handshake_complete) {
  1355. hs->in_false_start = true;
  1356. hs->can_early_write = true;
  1357. return ssl_hs_early_return;
  1358. }
  1359. return ssl_hs_ok;
  1360. }
  1361. static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
  1362. SSL *const ssl = hs->ssl;
  1363. if (!hs->ticket_expected) {
  1364. hs->state = state_process_change_cipher_spec;
  1365. return ssl_hs_read_change_cipher_spec;
  1366. }
  1367. SSLMessage msg;
  1368. if (!ssl->method->get_message(ssl, &msg)) {
  1369. return ssl_hs_read_message;
  1370. }
  1371. if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
  1372. !ssl_hash_message(hs, msg)) {
  1373. return ssl_hs_error;
  1374. }
  1375. CBS new_session_ticket = msg.body, ticket;
  1376. uint32_t tlsext_tick_lifetime_hint;
  1377. if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
  1378. !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
  1379. CBS_len(&new_session_ticket) != 0) {
  1380. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1381. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1382. return ssl_hs_error;
  1383. }
  1384. if (CBS_len(&ticket) == 0) {
  1385. // RFC 5077 allows a server to change its mind and send no ticket after
  1386. // negotiating the extension. The value of |ticket_expected| is checked in
  1387. // |ssl_update_cache| so is cleared here to avoid an unnecessary update.
  1388. hs->ticket_expected = false;
  1389. ssl->method->next_message(ssl);
  1390. hs->state = state_process_change_cipher_spec;
  1391. return ssl_hs_read_change_cipher_spec;
  1392. }
  1393. SSL_SESSION *session = hs->new_session.get();
  1394. UniquePtr<SSL_SESSION> renewed_session;
  1395. if (ssl->session != NULL) {
  1396. // The server is sending a new ticket for an existing session. Sessions are
  1397. // immutable once established, so duplicate all but the ticket of the
  1398. // existing session.
  1399. renewed_session =
  1400. SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
  1401. if (!renewed_session) {
  1402. // This should never happen.
  1403. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1404. return ssl_hs_error;
  1405. }
  1406. session = renewed_session.get();
  1407. }
  1408. // |tlsext_tick_lifetime_hint| is measured from when the ticket was issued.
  1409. ssl_session_rebase_time(ssl, session);
  1410. if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
  1411. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1412. return ssl_hs_error;
  1413. }
  1414. session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
  1415. // Generate a session ID for this session based on the session ticket. We use
  1416. // the session ID mechanism for detecting ticket resumption. This also fits in
  1417. // with assumptions elsewhere in OpenSSL.
  1418. if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
  1419. session->session_id, &session->session_id_length,
  1420. EVP_sha256(), NULL)) {
  1421. return ssl_hs_error;
  1422. }
  1423. if (renewed_session) {
  1424. session->not_resumable = 0;
  1425. SSL_SESSION_free(ssl->session);
  1426. ssl->session = renewed_session.release();
  1427. }
  1428. ssl->method->next_message(ssl);
  1429. hs->state = state_process_change_cipher_spec;
  1430. return ssl_hs_read_change_cipher_spec;
  1431. }
  1432. static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1433. if (!tls1_change_cipher_state(hs, evp_aead_open)) {
  1434. return ssl_hs_error;
  1435. }
  1436. hs->state = state_read_server_finished;
  1437. return ssl_hs_ok;
  1438. }
  1439. static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
  1440. SSL *const ssl = hs->ssl;
  1441. enum ssl_hs_wait_t wait = ssl_get_finished(hs);
  1442. if (wait != ssl_hs_ok) {
  1443. return wait;
  1444. }
  1445. if (ssl->session != NULL) {
  1446. hs->state = state_send_client_finished;
  1447. return ssl_hs_ok;
  1448. }
  1449. hs->state = state_finish_client_handshake;
  1450. return ssl_hs_ok;
  1451. }
  1452. static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
  1453. SSL *const ssl = hs->ssl;
  1454. ssl->method->on_handshake_complete(ssl);
  1455. if (ssl->session != NULL) {
  1456. SSL_SESSION_up_ref(ssl->session);
  1457. ssl->s3->established_session.reset(ssl->session);
  1458. } else {
  1459. // We make a copy of the session in order to maintain the immutability
  1460. // of the new established_session due to False Start. The caller may
  1461. // have taken a reference to the temporary session.
  1462. ssl->s3->established_session =
  1463. SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL);
  1464. if (!ssl->s3->established_session) {
  1465. return ssl_hs_error;
  1466. }
  1467. // Renegotiations do not participate in session resumption.
  1468. if (!ssl->s3->initial_handshake_complete) {
  1469. ssl->s3->established_session->not_resumable = 0;
  1470. }
  1471. hs->new_session.reset();
  1472. }
  1473. hs->handshake_finalized = true;
  1474. ssl->s3->initial_handshake_complete = true;
  1475. ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
  1476. hs->state = state_done;
  1477. return ssl_hs_ok;
  1478. }
  1479. enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
  1480. while (hs->state != state_done) {
  1481. enum ssl_hs_wait_t ret = ssl_hs_error;
  1482. enum ssl_client_hs_state_t state =
  1483. static_cast<enum ssl_client_hs_state_t>(hs->state);
  1484. switch (state) {
  1485. case state_start_connect:
  1486. ret = do_start_connect(hs);
  1487. break;
  1488. case state_enter_early_data:
  1489. ret = do_enter_early_data(hs);
  1490. break;
  1491. case state_read_hello_verify_request:
  1492. ret = do_read_hello_verify_request(hs);
  1493. break;
  1494. case state_read_server_hello:
  1495. ret = do_read_server_hello(hs);
  1496. break;
  1497. case state_tls13:
  1498. ret = do_tls13(hs);
  1499. break;
  1500. case state_read_server_certificate:
  1501. ret = do_read_server_certificate(hs);
  1502. break;
  1503. case state_read_certificate_status:
  1504. ret = do_read_certificate_status(hs);
  1505. break;
  1506. case state_verify_server_certificate:
  1507. ret = do_verify_server_certificate(hs);
  1508. break;
  1509. case state_read_server_key_exchange:
  1510. ret = do_read_server_key_exchange(hs);
  1511. break;
  1512. case state_read_certificate_request:
  1513. ret = do_read_certificate_request(hs);
  1514. break;
  1515. case state_read_server_hello_done:
  1516. ret = do_read_server_hello_done(hs);
  1517. break;
  1518. case state_send_client_certificate:
  1519. ret = do_send_client_certificate(hs);
  1520. break;
  1521. case state_send_client_key_exchange:
  1522. ret = do_send_client_key_exchange(hs);
  1523. break;
  1524. case state_send_client_certificate_verify:
  1525. ret = do_send_client_certificate_verify(hs);
  1526. break;
  1527. case state_send_client_finished:
  1528. ret = do_send_client_finished(hs);
  1529. break;
  1530. case state_finish_flight:
  1531. ret = do_finish_flight(hs);
  1532. break;
  1533. case state_read_session_ticket:
  1534. ret = do_read_session_ticket(hs);
  1535. break;
  1536. case state_process_change_cipher_spec:
  1537. ret = do_process_change_cipher_spec(hs);
  1538. break;
  1539. case state_read_server_finished:
  1540. ret = do_read_server_finished(hs);
  1541. break;
  1542. case state_finish_client_handshake:
  1543. ret = do_finish_client_handshake(hs);
  1544. break;
  1545. case state_done:
  1546. ret = ssl_hs_ok;
  1547. break;
  1548. }
  1549. if (hs->state != state) {
  1550. ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
  1551. }
  1552. if (ret != ssl_hs_ok) {
  1553. return ret;
  1554. }
  1555. }
  1556. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
  1557. return ssl_hs_ok;
  1558. }
  1559. const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
  1560. enum ssl_client_hs_state_t state =
  1561. static_cast<enum ssl_client_hs_state_t>(hs->state);
  1562. switch (state) {
  1563. case state_start_connect:
  1564. return "TLS client start_connect";
  1565. case state_enter_early_data:
  1566. return "TLS client enter_early_data";
  1567. case state_read_hello_verify_request:
  1568. return "TLS client read_hello_verify_request";
  1569. case state_read_server_hello:
  1570. return "TLS client read_server_hello";
  1571. case state_tls13:
  1572. return tls13_client_handshake_state(hs);
  1573. case state_read_server_certificate:
  1574. return "TLS client read_server_certificate";
  1575. case state_read_certificate_status:
  1576. return "TLS client read_certificate_status";
  1577. case state_verify_server_certificate:
  1578. return "TLS client verify_server_certificate";
  1579. case state_read_server_key_exchange:
  1580. return "TLS client read_server_key_exchange";
  1581. case state_read_certificate_request:
  1582. return "TLS client read_certificate_request";
  1583. case state_read_server_hello_done:
  1584. return "TLS client read_server_hello_done";
  1585. case state_send_client_certificate:
  1586. return "TLS client send_client_certificate";
  1587. case state_send_client_key_exchange:
  1588. return "TLS client send_client_key_exchange";
  1589. case state_send_client_certificate_verify:
  1590. return "TLS client send_client_certificate_verify";
  1591. case state_send_client_finished:
  1592. return "TLS client send_client_finished";
  1593. case state_finish_flight:
  1594. return "TLS client finish_flight";
  1595. case state_read_session_ticket:
  1596. return "TLS client read_session_ticket";
  1597. case state_process_change_cipher_spec:
  1598. return "TLS client process_change_cipher_spec";
  1599. case state_read_server_finished:
  1600. return "TLS client read_server_finished";
  1601. case state_finish_client_handshake:
  1602. return "TLS client finish_client_handshake";
  1603. case state_done:
  1604. return "TLS client done";
  1605. }
  1606. return "TLS client unknown";
  1607. }
  1608. }