handshake_client.cc 61 KB

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