handshake_server.cc 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  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. #include <openssl/ssl.h>
  149. #include <assert.h>
  150. #include <string.h>
  151. #include <openssl/bn.h>
  152. #include <openssl/buf.h>
  153. #include <openssl/bytestring.h>
  154. #include <openssl/cipher.h>
  155. #include <openssl/ec.h>
  156. #include <openssl/ecdsa.h>
  157. #include <openssl/err.h>
  158. #include <openssl/evp.h>
  159. #include <openssl/hmac.h>
  160. #include <openssl/md5.h>
  161. #include <openssl/mem.h>
  162. #include <openssl/nid.h>
  163. #include <openssl/rand.h>
  164. #include <openssl/x509.h>
  165. #include "internal.h"
  166. #include "../crypto/internal.h"
  167. namespace bssl {
  168. enum ssl_server_hs_state_t {
  169. state_start_accept = 0,
  170. state_read_client_hello,
  171. state_select_certificate,
  172. state_tls13,
  173. state_select_parameters,
  174. state_send_server_hello,
  175. state_send_server_certificate,
  176. state_send_server_key_exchange,
  177. state_send_server_hello_done,
  178. state_read_client_certificate,
  179. state_verify_client_certificate,
  180. state_read_client_key_exchange,
  181. state_read_client_certificate_verify,
  182. state_read_change_cipher_spec,
  183. state_process_change_cipher_spec,
  184. state_read_next_proto,
  185. state_read_channel_id,
  186. state_read_client_finished,
  187. state_send_server_finished,
  188. state_finish_server_handshake,
  189. state_done,
  190. };
  191. int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
  192. uint16_t id) {
  193. CBS cipher_suites;
  194. CBS_init(&cipher_suites, client_hello->cipher_suites,
  195. client_hello->cipher_suites_len);
  196. while (CBS_len(&cipher_suites) > 0) {
  197. uint16_t got_id;
  198. if (!CBS_get_u16(&cipher_suites, &got_id)) {
  199. return 0;
  200. }
  201. if (got_id == id) {
  202. return 1;
  203. }
  204. }
  205. return 0;
  206. }
  207. static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  208. const SSL_CLIENT_HELLO *client_hello) {
  209. SSL *const ssl = hs->ssl;
  210. assert(!ssl->s3->have_version);
  211. CBS supported_versions, versions;
  212. if (ssl_client_hello_get_extension(client_hello, &supported_versions,
  213. TLSEXT_TYPE_supported_versions)) {
  214. if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
  215. CBS_len(&supported_versions) != 0 ||
  216. CBS_len(&versions) == 0) {
  217. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  218. *out_alert = SSL_AD_DECODE_ERROR;
  219. return 0;
  220. }
  221. } else {
  222. // Convert the ClientHello version to an equivalent supported_versions
  223. // extension.
  224. static const uint8_t kTLSVersions[] = {
  225. 0x03, 0x03, // TLS 1.2
  226. 0x03, 0x02, // TLS 1.1
  227. 0x03, 0x01, // TLS 1
  228. 0x03, 0x00, // SSL 3
  229. };
  230. static const uint8_t kDTLSVersions[] = {
  231. 0xfe, 0xfd, // DTLS 1.2
  232. 0xfe, 0xff, // DTLS 1.0
  233. };
  234. size_t versions_len = 0;
  235. if (SSL_is_dtls(ssl)) {
  236. if (client_hello->version <= DTLS1_2_VERSION) {
  237. versions_len = 4;
  238. } else if (client_hello->version <= DTLS1_VERSION) {
  239. versions_len = 2;
  240. }
  241. CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len,
  242. versions_len);
  243. } else {
  244. if (client_hello->version >= TLS1_2_VERSION) {
  245. versions_len = 8;
  246. } else if (client_hello->version >= TLS1_1_VERSION) {
  247. versions_len = 6;
  248. } else if (client_hello->version >= TLS1_VERSION) {
  249. versions_len = 4;
  250. } else if (client_hello->version >= SSL3_VERSION) {
  251. versions_len = 2;
  252. }
  253. CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len,
  254. versions_len);
  255. }
  256. }
  257. if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) {
  258. return 0;
  259. }
  260. // At this point, the connection's version is known and |ssl->version| is
  261. // fixed. Begin enforcing the record-layer version.
  262. ssl->s3->have_version = true;
  263. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
  264. // Handle FALLBACK_SCSV.
  265. if (ssl_client_cipher_list_contains_cipher(client_hello,
  266. SSL3_CK_FALLBACK_SCSV & 0xffff) &&
  267. ssl_protocol_version(ssl) < hs->max_version) {
  268. OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
  269. *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
  270. return 0;
  271. }
  272. return 1;
  273. }
  274. static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
  275. const SSL_CLIENT_HELLO *client_hello) {
  276. CBS cipher_suites;
  277. CBS_init(&cipher_suites, client_hello->cipher_suites,
  278. client_hello->cipher_suites_len);
  279. UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
  280. if (!sk) {
  281. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  282. return nullptr;
  283. }
  284. while (CBS_len(&cipher_suites) > 0) {
  285. uint16_t cipher_suite;
  286. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  287. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  288. return nullptr;
  289. }
  290. const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
  291. if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
  292. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  293. return nullptr;
  294. }
  295. }
  296. return sk;
  297. }
  298. // ssl_get_compatible_server_ciphers determines the key exchange and
  299. // authentication cipher suite masks compatible with the server configuration
  300. // and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
  301. // exchange mask and |*out_mask_a| to the authentication mask.
  302. static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
  303. uint32_t *out_mask_k,
  304. uint32_t *out_mask_a) {
  305. SSL *const ssl = hs->ssl;
  306. uint32_t mask_k = 0;
  307. uint32_t mask_a = 0;
  308. if (ssl_has_certificate(ssl)) {
  309. mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get());
  310. if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) {
  311. mask_k |= SSL_kRSA;
  312. }
  313. }
  314. // Check for a shared group to consider ECDHE ciphers.
  315. uint16_t unused;
  316. if (tls1_get_shared_group(hs, &unused)) {
  317. mask_k |= SSL_kECDHE;
  318. }
  319. // PSK requires a server callback.
  320. if (ssl->psk_server_callback != NULL) {
  321. mask_k |= SSL_kPSK;
  322. mask_a |= SSL_aPSK;
  323. }
  324. *out_mask_k = mask_k;
  325. *out_mask_a = mask_a;
  326. }
  327. static const SSL_CIPHER *ssl3_choose_cipher(
  328. SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
  329. const struct ssl_cipher_preference_list_st *server_pref) {
  330. SSL *const ssl = hs->ssl;
  331. STACK_OF(SSL_CIPHER) *prio, *allow;
  332. // in_group_flags will either be NULL, or will point to an array of bytes
  333. // which indicate equal-preference groups in the |prio| stack. See the
  334. // comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
  335. // struct.
  336. const uint8_t *in_group_flags;
  337. // group_min contains the minimal index so far found in a group, or -1 if no
  338. // such value exists yet.
  339. int group_min = -1;
  340. UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
  341. ssl_parse_client_cipher_list(client_hello);
  342. if (!client_pref) {
  343. return nullptr;
  344. }
  345. if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  346. prio = server_pref->ciphers;
  347. in_group_flags = server_pref->in_group_flags;
  348. allow = client_pref.get();
  349. } else {
  350. prio = client_pref.get();
  351. in_group_flags = NULL;
  352. allow = server_pref->ciphers;
  353. }
  354. uint32_t mask_k, mask_a;
  355. ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
  356. for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
  357. const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
  358. size_t cipher_index;
  359. if (// Check if the cipher is supported for the current version.
  360. SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) &&
  361. ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
  362. // Check the cipher is supported for the server configuration.
  363. (c->algorithm_mkey & mask_k) &&
  364. (c->algorithm_auth & mask_a) &&
  365. // Check the cipher is in the |allow| list.
  366. sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
  367. if (in_group_flags != NULL && in_group_flags[i] == 1) {
  368. // This element of |prio| is in a group. Update the minimum index found
  369. // so far and continue looking.
  370. if (group_min == -1 || (size_t)group_min > cipher_index) {
  371. group_min = cipher_index;
  372. }
  373. } else {
  374. if (group_min != -1 && (size_t)group_min < cipher_index) {
  375. cipher_index = group_min;
  376. }
  377. return sk_SSL_CIPHER_value(allow, cipher_index);
  378. }
  379. }
  380. if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
  381. // We are about to leave a group, but we found a match in it, so that's
  382. // our answer.
  383. return sk_SSL_CIPHER_value(allow, group_min);
  384. }
  385. }
  386. return nullptr;
  387. }
  388. static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) {
  389. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1);
  390. hs->state = state_read_client_hello;
  391. return ssl_hs_ok;
  392. }
  393. static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) {
  394. SSL *const ssl = hs->ssl;
  395. SSLMessage msg;
  396. if (!ssl->method->get_message(ssl, &msg)) {
  397. return ssl_hs_read_message;
  398. }
  399. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
  400. return ssl_hs_error;
  401. }
  402. SSL_CLIENT_HELLO client_hello;
  403. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  404. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  405. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  406. return ssl_hs_error;
  407. }
  408. // Run the early callback.
  409. if (ssl->ctx->select_certificate_cb != NULL) {
  410. switch (ssl->ctx->select_certificate_cb(&client_hello)) {
  411. case ssl_select_cert_retry:
  412. return ssl_hs_certificate_selection_pending;
  413. case ssl_select_cert_error:
  414. // Connection rejected.
  415. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  416. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  417. return ssl_hs_error;
  418. default:
  419. /* fallthrough */;
  420. }
  421. }
  422. // Freeze the version range after the early callback.
  423. if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
  424. return ssl_hs_error;
  425. }
  426. uint8_t alert = SSL_AD_DECODE_ERROR;
  427. if (!negotiate_version(hs, &alert, &client_hello)) {
  428. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  429. return ssl_hs_error;
  430. }
  431. hs->client_version = client_hello.version;
  432. if (client_hello.random_len != SSL3_RANDOM_SIZE) {
  433. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  434. return ssl_hs_error;
  435. }
  436. OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
  437. client_hello.random_len);
  438. // Only null compression is supported. TLS 1.3 further requires the peer
  439. // advertise no other compression.
  440. if (OPENSSL_memchr(client_hello.compression_methods, 0,
  441. client_hello.compression_methods_len) == NULL ||
  442. (ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
  443. client_hello.compression_methods_len != 1)) {
  444. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
  445. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  446. return ssl_hs_error;
  447. }
  448. // TLS extensions.
  449. if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
  450. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  451. return ssl_hs_error;
  452. }
  453. hs->state = state_select_certificate;
  454. return ssl_hs_ok;
  455. }
  456. static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
  457. SSL *const ssl = hs->ssl;
  458. SSLMessage msg;
  459. if (!ssl->method->get_message(ssl, &msg)) {
  460. return ssl_hs_read_message;
  461. }
  462. // Call |cert_cb| to update server certificates if required.
  463. if (ssl->cert->cert_cb != NULL) {
  464. int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
  465. if (rv == 0) {
  466. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
  467. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  468. return ssl_hs_error;
  469. }
  470. if (rv < 0) {
  471. return ssl_hs_x509_lookup;
  472. }
  473. }
  474. if (!ssl_on_certificate_selected(hs)) {
  475. return ssl_hs_error;
  476. }
  477. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  478. // Jump to the TLS 1.3 state machine.
  479. hs->state = state_tls13;
  480. return ssl_hs_ok;
  481. }
  482. SSL_CLIENT_HELLO client_hello;
  483. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  484. return ssl_hs_error;
  485. }
  486. // Negotiate the cipher suite. This must be done after |cert_cb| so the
  487. // certificate is finalized.
  488. hs->new_cipher =
  489. ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
  490. if (hs->new_cipher == NULL) {
  491. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
  492. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  493. return ssl_hs_error;
  494. }
  495. hs->state = state_select_parameters;
  496. return ssl_hs_ok;
  497. }
  498. static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
  499. enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
  500. if (wait == ssl_hs_ok) {
  501. hs->state = state_finish_server_handshake;
  502. return ssl_hs_ok;
  503. }
  504. return wait;
  505. }
  506. static enum ssl_hs_wait_t do_select_parameters(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_message;
  511. }
  512. SSL_CLIENT_HELLO client_hello;
  513. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  514. return ssl_hs_error;
  515. }
  516. // Determine whether we are doing session resumption.
  517. UniquePtr<SSL_SESSION> session;
  518. bool tickets_supported = false, renew_ticket = false;
  519. enum ssl_hs_wait_t wait = ssl_get_prev_session(
  520. ssl, &session, &tickets_supported, &renew_ticket, &client_hello);
  521. if (wait != ssl_hs_ok) {
  522. return wait;
  523. }
  524. if (session) {
  525. if (session->extended_master_secret && !hs->extended_master_secret) {
  526. // A ClientHello without EMS that attempts to resume a session with EMS
  527. // is fatal to the connection.
  528. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  529. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  530. return ssl_hs_error;
  531. }
  532. if (!ssl_session_is_resumable(hs, session.get()) ||
  533. // If the client offers the EMS extension, but the previous session
  534. // didn't use it, then negotiate a new session.
  535. hs->extended_master_secret != session->extended_master_secret) {
  536. session.reset();
  537. }
  538. }
  539. if (session) {
  540. // Use the old session.
  541. hs->ticket_expected = renew_ticket;
  542. ssl->session = session.release();
  543. ssl->s3->session_reused = true;
  544. } else {
  545. hs->ticket_expected = tickets_supported;
  546. ssl_set_session(ssl, NULL);
  547. if (!ssl_get_new_session(hs, 1 /* server */)) {
  548. return ssl_hs_error;
  549. }
  550. // Clear the session ID if we want the session to be single-use.
  551. if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
  552. hs->new_session->session_id_length = 0;
  553. }
  554. }
  555. if (ssl->ctx->dos_protection_cb != NULL &&
  556. ssl->ctx->dos_protection_cb(&client_hello) == 0) {
  557. // Connection rejected for DOS reasons.
  558. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  559. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  560. return ssl_hs_error;
  561. }
  562. if (ssl->session == NULL) {
  563. hs->new_session->cipher = hs->new_cipher;
  564. // Determine whether to request a client certificate.
  565. hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
  566. // Only request a certificate if Channel ID isn't negotiated.
  567. if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
  568. ssl->s3->tlsext_channel_id_valid) {
  569. hs->cert_request = false;
  570. }
  571. // CertificateRequest may only be sent in certificate-based ciphers.
  572. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  573. hs->cert_request = false;
  574. }
  575. if (!hs->cert_request) {
  576. // OpenSSL returns X509_V_OK when no certificates are requested. This is
  577. // classed by them as a bug, but it's assumed by at least NGINX.
  578. hs->new_session->verify_result = X509_V_OK;
  579. }
  580. }
  581. // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
  582. // deferred. Complete it now.
  583. uint8_t alert = SSL_AD_DECODE_ERROR;
  584. if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
  585. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  586. return ssl_hs_error;
  587. }
  588. // Now that all parameters are known, initialize the handshake hash and hash
  589. // the ClientHello.
  590. if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
  591. !ssl_hash_message(hs, msg)) {
  592. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  593. return ssl_hs_error;
  594. }
  595. // Release the handshake buffer if client authentication isn't required.
  596. if (!hs->cert_request) {
  597. hs->transcript.FreeBuffer();
  598. }
  599. ssl->method->next_message(ssl);
  600. hs->state = state_send_server_hello;
  601. return ssl_hs_ok;
  602. }
  603. static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
  604. SSL *const ssl = hs->ssl;
  605. // We only accept ChannelIDs on connections with ECDHE in order to avoid a
  606. // known attack while we fix ChannelID itself.
  607. if (ssl->s3->tlsext_channel_id_valid &&
  608. (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
  609. ssl->s3->tlsext_channel_id_valid = false;
  610. }
  611. // If this is a resumption and the original handshake didn't support
  612. // ChannelID then we didn't record the original handshake hashes in the
  613. // session and so cannot resume with ChannelIDs.
  614. if (ssl->session != NULL &&
  615. ssl->session->original_handshake_hash_len == 0) {
  616. ssl->s3->tlsext_channel_id_valid = false;
  617. }
  618. struct OPENSSL_timeval now;
  619. ssl_get_current_time(ssl, &now);
  620. ssl->s3->server_random[0] = now.tv_sec >> 24;
  621. ssl->s3->server_random[1] = now.tv_sec >> 16;
  622. ssl->s3->server_random[2] = now.tv_sec >> 8;
  623. ssl->s3->server_random[3] = now.tv_sec;
  624. if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
  625. return ssl_hs_error;
  626. }
  627. // TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
  628. // 1.3 is finalized and we are not implementing a draft version.
  629. const SSL_SESSION *session = hs->new_session.get();
  630. if (ssl->session != NULL) {
  631. session = ssl->session;
  632. }
  633. ScopedCBB cbb;
  634. CBB body, session_id;
  635. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
  636. !CBB_add_u16(&body, ssl->version) ||
  637. !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  638. !CBB_add_u8_length_prefixed(&body, &session_id) ||
  639. !CBB_add_bytes(&session_id, session->session_id,
  640. session->session_id_length) ||
  641. !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
  642. !CBB_add_u8(&body, 0 /* no compression */) ||
  643. !ssl_add_serverhello_tlsext(hs, &body) ||
  644. !ssl_add_message_cbb(ssl, cbb.get())) {
  645. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  646. return ssl_hs_error;
  647. }
  648. if (ssl->session != NULL) {
  649. hs->state = state_send_server_finished;
  650. } else {
  651. hs->state = state_send_server_certificate;
  652. }
  653. return ssl_hs_ok;
  654. }
  655. static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
  656. SSL *const ssl = hs->ssl;
  657. ScopedCBB cbb;
  658. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  659. if (!ssl_has_certificate(ssl)) {
  660. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  661. return ssl_hs_error;
  662. }
  663. if (!ssl_output_cert_chain(ssl)) {
  664. return ssl_hs_error;
  665. }
  666. if (hs->certificate_status_expected) {
  667. CBB body, ocsp_response;
  668. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  669. SSL3_MT_CERTIFICATE_STATUS) ||
  670. !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
  671. !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
  672. !CBB_add_bytes(&ocsp_response,
  673. CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
  674. CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
  675. !ssl_add_message_cbb(ssl, cbb.get())) {
  676. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  677. return ssl_hs_error;
  678. }
  679. }
  680. }
  681. // Assemble ServerKeyExchange parameters if needed.
  682. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  683. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  684. if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
  685. ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
  686. // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
  687. // the client and server randoms for the signing transcript.
  688. CBB child;
  689. if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
  690. !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  691. !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
  692. return ssl_hs_error;
  693. }
  694. // PSK ciphers begin with an identity hint.
  695. if (alg_a & SSL_aPSK) {
  696. size_t len =
  697. (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
  698. if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
  699. !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
  700. len)) {
  701. return ssl_hs_error;
  702. }
  703. }
  704. if (alg_k & SSL_kECDHE) {
  705. // Determine the group to use.
  706. uint16_t group_id;
  707. if (!tls1_get_shared_group(hs, &group_id)) {
  708. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  709. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  710. return ssl_hs_error;
  711. }
  712. hs->new_session->group_id = group_id;
  713. // Set up ECDH, generate a key, and emit the public half.
  714. hs->key_share = SSLKeyShare::Create(group_id);
  715. if (!hs->key_share ||
  716. !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
  717. !CBB_add_u16(cbb.get(), group_id) ||
  718. !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
  719. !hs->key_share->Offer(&child)) {
  720. return ssl_hs_error;
  721. }
  722. } else {
  723. assert(alg_k & SSL_kPSK);
  724. }
  725. if (!CBBFinishArray(cbb.get(), &hs->server_params)) {
  726. return ssl_hs_error;
  727. }
  728. }
  729. hs->state = state_send_server_key_exchange;
  730. return ssl_hs_ok;
  731. }
  732. static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
  733. SSL *const ssl = hs->ssl;
  734. if (hs->server_params.size() == 0) {
  735. hs->state = state_send_server_hello_done;
  736. return ssl_hs_ok;
  737. }
  738. ScopedCBB cbb;
  739. CBB body, child;
  740. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  741. SSL3_MT_SERVER_KEY_EXCHANGE) ||
  742. // |hs->server_params| contains a prefix for signing.
  743. hs->server_params.size() < 2 * SSL3_RANDOM_SIZE ||
  744. !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE,
  745. hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) {
  746. return ssl_hs_error;
  747. }
  748. // Add a signature.
  749. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  750. if (!ssl_has_private_key(ssl)) {
  751. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  752. return ssl_hs_error;
  753. }
  754. // Determine the signature algorithm.
  755. uint16_t signature_algorithm;
  756. if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
  757. return ssl_hs_error;
  758. }
  759. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  760. if (!CBB_add_u16(&body, signature_algorithm)) {
  761. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  762. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  763. return ssl_hs_error;
  764. }
  765. }
  766. // Add space for the signature.
  767. const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
  768. uint8_t *ptr;
  769. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  770. !CBB_reserve(&child, &ptr, max_sig_len)) {
  771. return ssl_hs_error;
  772. }
  773. size_t sig_len;
  774. switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
  775. signature_algorithm, hs->server_params)) {
  776. case ssl_private_key_success:
  777. if (!CBB_did_write(&child, sig_len)) {
  778. return ssl_hs_error;
  779. }
  780. break;
  781. case ssl_private_key_failure:
  782. return ssl_hs_error;
  783. case ssl_private_key_retry:
  784. return ssl_hs_private_key_operation;
  785. }
  786. }
  787. if (!ssl_add_message_cbb(ssl, cbb.get())) {
  788. return ssl_hs_error;
  789. }
  790. hs->server_params.Reset();
  791. hs->state = state_send_server_hello_done;
  792. return ssl_hs_ok;
  793. }
  794. static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
  795. SSL *const ssl = hs->ssl;
  796. ScopedCBB cbb;
  797. CBB body;
  798. if (hs->cert_request) {
  799. CBB cert_types, sigalgs_cbb;
  800. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  801. SSL3_MT_CERTIFICATE_REQUEST) ||
  802. !CBB_add_u8_length_prefixed(&body, &cert_types) ||
  803. !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
  804. (ssl_protocol_version(ssl) >= TLS1_VERSION &&
  805. !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN)) ||
  806. (ssl_protocol_version(ssl) >= TLS1_2_VERSION &&
  807. (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
  808. !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb))) ||
  809. !ssl_add_client_CA_list(ssl, &body) ||
  810. !ssl_add_message_cbb(ssl, cbb.get())) {
  811. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  812. return ssl_hs_error;
  813. }
  814. }
  815. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  816. SSL3_MT_SERVER_HELLO_DONE) ||
  817. !ssl_add_message_cbb(ssl, cbb.get())) {
  818. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  819. return ssl_hs_error;
  820. }
  821. hs->state = state_read_client_certificate;
  822. return ssl_hs_flush;
  823. }
  824. static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
  825. SSL *const ssl = hs->ssl;
  826. if (!hs->cert_request) {
  827. hs->state = state_verify_client_certificate;
  828. return ssl_hs_ok;
  829. }
  830. SSLMessage msg;
  831. if (!ssl->method->get_message(ssl, &msg)) {
  832. return ssl_hs_read_message;
  833. }
  834. if (msg.type != SSL3_MT_CERTIFICATE) {
  835. if (ssl->version == SSL3_VERSION &&
  836. msg.type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  837. // In SSL 3.0, the Certificate message is omitted to signal no
  838. // certificate.
  839. if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  840. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  841. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  842. return ssl_hs_error;
  843. }
  844. // OpenSSL returns X509_V_OK when no certificates are received. This is
  845. // classed by them as a bug, but it's assumed by at least NGINX.
  846. hs->new_session->verify_result = X509_V_OK;
  847. hs->state = state_verify_client_certificate;
  848. return ssl_hs_ok;
  849. }
  850. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  851. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  852. return ssl_hs_error;
  853. }
  854. if (!ssl_hash_message(hs, msg)) {
  855. return ssl_hs_error;
  856. }
  857. CBS certificate_msg = msg.body;
  858. uint8_t alert = SSL_AD_DECODE_ERROR;
  859. UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
  860. if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey,
  861. ssl->retain_only_sha256_of_client_certs
  862. ? hs->new_session->peer_sha256
  863. : NULL,
  864. &certificate_msg, ssl->ctx->pool)) {
  865. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  866. return ssl_hs_error;
  867. }
  868. sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
  869. hs->new_session->certs = chain.release();
  870. if (CBS_len(&certificate_msg) != 0 ||
  871. !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
  872. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  873. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  874. return ssl_hs_error;
  875. }
  876. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
  877. // No client certificate so the handshake buffer may be discarded.
  878. hs->transcript.FreeBuffer();
  879. // In SSL 3.0, sending no certificate is signaled by omitting the
  880. // Certificate message.
  881. if (ssl->version == SSL3_VERSION) {
  882. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
  883. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  884. return ssl_hs_error;
  885. }
  886. if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  887. // Fail for TLS only if we required a certificate
  888. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  889. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  890. return ssl_hs_error;
  891. }
  892. // OpenSSL returns X509_V_OK when no certificates are received. This is
  893. // classed by them as a bug, but it's assumed by at least NGINX.
  894. hs->new_session->verify_result = X509_V_OK;
  895. } else if (ssl->retain_only_sha256_of_client_certs) {
  896. // The hash will have been filled in.
  897. hs->new_session->peer_sha256_valid = 1;
  898. }
  899. ssl->method->next_message(ssl);
  900. hs->state = state_verify_client_certificate;
  901. return ssl_hs_ok;
  902. }
  903. static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
  904. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) > 0) {
  905. switch (ssl_verify_peer_cert(hs)) {
  906. case ssl_verify_ok:
  907. break;
  908. case ssl_verify_invalid:
  909. return ssl_hs_error;
  910. case ssl_verify_retry:
  911. return ssl_hs_certificate_verify;
  912. }
  913. }
  914. hs->state = state_read_client_key_exchange;
  915. return ssl_hs_ok;
  916. }
  917. static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
  918. SSL *const ssl = hs->ssl;
  919. SSLMessage msg;
  920. if (!ssl->method->get_message(ssl, &msg)) {
  921. return ssl_hs_read_message;
  922. }
  923. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
  924. return ssl_hs_error;
  925. }
  926. CBS client_key_exchange = msg.body;
  927. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  928. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  929. // If using a PSK key exchange, parse the PSK identity.
  930. if (alg_a & SSL_aPSK) {
  931. CBS psk_identity;
  932. // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
  933. // then this is the only field in the message.
  934. if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
  935. ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
  936. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  937. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  938. return ssl_hs_error;
  939. }
  940. if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
  941. CBS_contains_zero_byte(&psk_identity)) {
  942. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  943. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  944. return ssl_hs_error;
  945. }
  946. if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
  947. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  948. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  949. return ssl_hs_error;
  950. }
  951. }
  952. // Depending on the key exchange method, compute |premaster_secret|.
  953. Array<uint8_t> premaster_secret;
  954. if (alg_k & SSL_kRSA) {
  955. CBS encrypted_premaster_secret;
  956. if (ssl->version > SSL3_VERSION) {
  957. if (!CBS_get_u16_length_prefixed(&client_key_exchange,
  958. &encrypted_premaster_secret) ||
  959. CBS_len(&client_key_exchange) != 0) {
  960. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  961. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  962. return ssl_hs_error;
  963. }
  964. } else {
  965. encrypted_premaster_secret = client_key_exchange;
  966. }
  967. // Allocate a buffer large enough for an RSA decryption.
  968. Array<uint8_t> decrypt_buf;
  969. if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) {
  970. return ssl_hs_error;
  971. }
  972. // Decrypt with no padding. PKCS#1 padding will be removed as part of the
  973. // timing-sensitive code below.
  974. size_t decrypt_len;
  975. switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len,
  976. decrypt_buf.size(),
  977. encrypted_premaster_secret)) {
  978. case ssl_private_key_success:
  979. break;
  980. case ssl_private_key_failure:
  981. return ssl_hs_error;
  982. case ssl_private_key_retry:
  983. return ssl_hs_private_key_operation;
  984. }
  985. if (decrypt_len != decrypt_buf.size()) {
  986. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  987. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  988. return ssl_hs_error;
  989. }
  990. // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
  991. // section 7.4.7.1.
  992. if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) ||
  993. !RAND_bytes(premaster_secret.data(), premaster_secret.size())) {
  994. return ssl_hs_error;
  995. }
  996. // The smallest padded premaster is 11 bytes of overhead. Small keys are
  997. // publicly invalid.
  998. if (decrypt_len < 11 + premaster_secret.size()) {
  999. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  1000. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  1001. return ssl_hs_error;
  1002. }
  1003. // Check the padding. See RFC 3447, section 7.2.2.
  1004. size_t padding_len = decrypt_len - premaster_secret.size();
  1005. uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
  1006. constant_time_eq_int_8(decrypt_buf[1], 2);
  1007. for (size_t i = 2; i < padding_len - 1; i++) {
  1008. good &= ~constant_time_is_zero_8(decrypt_buf[i]);
  1009. }
  1010. good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
  1011. // The premaster secret must begin with |client_version|. This too must be
  1012. // checked in constant time (http://eprint.iacr.org/2003/052/).
  1013. good &= constant_time_eq_8(decrypt_buf[padding_len],
  1014. (unsigned)(hs->client_version >> 8));
  1015. good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
  1016. (unsigned)(hs->client_version & 0xff));
  1017. // Select, in constant time, either the decrypted premaster or the random
  1018. // premaster based on |good|.
  1019. for (size_t i = 0; i < premaster_secret.size(); i++) {
  1020. premaster_secret[i] = constant_time_select_8(
  1021. good, decrypt_buf[padding_len + i], premaster_secret[i]);
  1022. }
  1023. } else if (alg_k & SSL_kECDHE) {
  1024. // Parse the ClientKeyExchange.
  1025. CBS peer_key;
  1026. if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
  1027. CBS_len(&client_key_exchange) != 0) {
  1028. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1029. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1030. return ssl_hs_error;
  1031. }
  1032. // Compute the premaster.
  1033. uint8_t alert = SSL_AD_DECODE_ERROR;
  1034. if (!hs->key_share->Finish(&premaster_secret, &alert, peer_key)) {
  1035. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1036. return ssl_hs_error;
  1037. }
  1038. // The key exchange state may now be discarded.
  1039. hs->key_share.reset();
  1040. } else if (!(alg_k & SSL_kPSK)) {
  1041. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1042. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1043. return ssl_hs_error;
  1044. }
  1045. // For a PSK cipher suite, the actual pre-master secret is combined with the
  1046. // pre-shared key.
  1047. if (alg_a & SSL_aPSK) {
  1048. if (ssl->psk_server_callback == NULL) {
  1049. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1050. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1051. return ssl_hs_error;
  1052. }
  1053. // Look up the key for the identity.
  1054. uint8_t psk[PSK_MAX_PSK_LEN];
  1055. unsigned psk_len = ssl->psk_server_callback(
  1056. ssl, hs->new_session->psk_identity, psk, sizeof(psk));
  1057. if (psk_len > PSK_MAX_PSK_LEN) {
  1058. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1059. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1060. return ssl_hs_error;
  1061. } else if (psk_len == 0) {
  1062. // PSK related to the given identity not found.
  1063. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1064. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
  1065. return ssl_hs_error;
  1066. }
  1067. if (alg_k & SSL_kPSK) {
  1068. // In plain PSK, other_secret is a block of 0s with the same length as the
  1069. // pre-shared key.
  1070. if (!premaster_secret.Init(psk_len)) {
  1071. return ssl_hs_error;
  1072. }
  1073. OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size());
  1074. }
  1075. ScopedCBB new_premaster;
  1076. CBB child;
  1077. if (!CBB_init(new_premaster.get(),
  1078. 2 + psk_len + 2 + premaster_secret.size()) ||
  1079. !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
  1080. !CBB_add_bytes(&child, premaster_secret.data(),
  1081. premaster_secret.size()) ||
  1082. !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
  1083. !CBB_add_bytes(&child, psk, psk_len) ||
  1084. !CBBFinishArray(new_premaster.get(), &premaster_secret)) {
  1085. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1086. return ssl_hs_error;
  1087. }
  1088. }
  1089. if (!ssl_hash_message(hs, msg)) {
  1090. return ssl_hs_error;
  1091. }
  1092. // Compute the master secret.
  1093. hs->new_session->master_key_length = tls1_generate_master_secret(
  1094. hs, hs->new_session->master_key, premaster_secret);
  1095. if (hs->new_session->master_key_length == 0) {
  1096. return ssl_hs_error;
  1097. }
  1098. hs->new_session->extended_master_secret = hs->extended_master_secret;
  1099. ssl->method->next_message(ssl);
  1100. hs->state = state_read_client_certificate_verify;
  1101. return ssl_hs_ok;
  1102. }
  1103. static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
  1104. SSL *const ssl = hs->ssl;
  1105. // Only RSA and ECDSA client certificates are supported, so a
  1106. // CertificateVerify is required if and only if there's a client certificate.
  1107. if (!hs->peer_pubkey) {
  1108. hs->transcript.FreeBuffer();
  1109. hs->state = state_read_change_cipher_spec;
  1110. return ssl_hs_ok;
  1111. }
  1112. SSLMessage msg;
  1113. if (!ssl->method->get_message(ssl, &msg)) {
  1114. return ssl_hs_read_message;
  1115. }
  1116. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
  1117. return ssl_hs_error;
  1118. }
  1119. CBS certificate_verify = msg.body, signature;
  1120. // Determine the signature algorithm.
  1121. uint16_t signature_algorithm = 0;
  1122. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  1123. if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
  1124. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1125. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1126. return ssl_hs_error;
  1127. }
  1128. uint8_t alert = SSL_AD_DECODE_ERROR;
  1129. if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
  1130. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1131. return ssl_hs_error;
  1132. }
  1133. hs->new_session->peer_signature_algorithm = signature_algorithm;
  1134. } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
  1135. hs->peer_pubkey.get())) {
  1136. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  1137. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
  1138. return ssl_hs_error;
  1139. }
  1140. // Parse and verify the signature.
  1141. if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
  1142. CBS_len(&certificate_verify) != 0) {
  1143. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1144. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1145. return ssl_hs_error;
  1146. }
  1147. bool sig_ok;
  1148. // The SSL3 construction for CertificateVerify does not decompose into a
  1149. // single final digest and signature, and must be special-cased.
  1150. if (ssl_protocol_version(ssl) == SSL3_VERSION) {
  1151. uint8_t digest[EVP_MAX_MD_SIZE];
  1152. size_t digest_len;
  1153. if (!hs->transcript.GetSSL3CertVerifyHash(
  1154. digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
  1155. return ssl_hs_error;
  1156. }
  1157. UniquePtr<EVP_PKEY_CTX> pctx(
  1158. EVP_PKEY_CTX_new(hs->peer_pubkey.get(), nullptr));
  1159. sig_ok = pctx &&
  1160. EVP_PKEY_verify_init(pctx.get()) &&
  1161. EVP_PKEY_verify(pctx.get(), CBS_data(&signature),
  1162. CBS_len(&signature), digest, digest_len);
  1163. } else {
  1164. sig_ok =
  1165. ssl_public_key_verify(ssl, signature, signature_algorithm,
  1166. hs->peer_pubkey.get(), hs->transcript.buffer());
  1167. }
  1168. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  1169. sig_ok = true;
  1170. ERR_clear_error();
  1171. #endif
  1172. if (!sig_ok) {
  1173. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  1174. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  1175. return ssl_hs_error;
  1176. }
  1177. // The handshake buffer is no longer necessary, and we may hash the current
  1178. // message.
  1179. hs->transcript.FreeBuffer();
  1180. if (!ssl_hash_message(hs, msg)) {
  1181. return ssl_hs_error;
  1182. }
  1183. ssl->method->next_message(ssl);
  1184. hs->state = state_read_change_cipher_spec;
  1185. return ssl_hs_ok;
  1186. }
  1187. static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1188. hs->state = state_process_change_cipher_spec;
  1189. return ssl_hs_read_change_cipher_spec;
  1190. }
  1191. static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1192. if (!tls1_change_cipher_state(hs, evp_aead_open)) {
  1193. return ssl_hs_error;
  1194. }
  1195. hs->state = state_read_next_proto;
  1196. return ssl_hs_ok;
  1197. }
  1198. static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
  1199. SSL *const ssl = hs->ssl;
  1200. if (!hs->next_proto_neg_seen) {
  1201. hs->state = state_read_channel_id;
  1202. return ssl_hs_ok;
  1203. }
  1204. SSLMessage msg;
  1205. if (!ssl->method->get_message(ssl, &msg)) {
  1206. return ssl_hs_read_message;
  1207. }
  1208. if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
  1209. !ssl_hash_message(hs, msg)) {
  1210. return ssl_hs_error;
  1211. }
  1212. CBS next_protocol = msg.body, selected_protocol, padding;
  1213. if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
  1214. !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
  1215. CBS_len(&next_protocol) != 0) {
  1216. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1217. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1218. return ssl_hs_error;
  1219. }
  1220. if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) {
  1221. return ssl_hs_error;
  1222. }
  1223. ssl->method->next_message(ssl);
  1224. hs->state = state_read_channel_id;
  1225. return ssl_hs_ok;
  1226. }
  1227. static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
  1228. SSL *const ssl = hs->ssl;
  1229. if (!ssl->s3->tlsext_channel_id_valid) {
  1230. hs->state = state_read_client_finished;
  1231. return ssl_hs_ok;
  1232. }
  1233. SSLMessage msg;
  1234. if (!ssl->method->get_message(ssl, &msg)) {
  1235. return ssl_hs_read_message;
  1236. }
  1237. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
  1238. !tls1_verify_channel_id(hs, msg) ||
  1239. !ssl_hash_message(hs, msg)) {
  1240. return ssl_hs_error;
  1241. }
  1242. ssl->method->next_message(ssl);
  1243. hs->state = state_read_client_finished;
  1244. return ssl_hs_ok;
  1245. }
  1246. static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
  1247. SSL *const ssl = hs->ssl;
  1248. enum ssl_hs_wait_t wait = ssl_get_finished(hs);
  1249. if (wait != ssl_hs_ok) {
  1250. return wait;
  1251. }
  1252. if (ssl->session != NULL) {
  1253. hs->state = state_finish_server_handshake;
  1254. } else {
  1255. hs->state = state_send_server_finished;
  1256. }
  1257. // If this is a full handshake with ChannelID then record the handshake
  1258. // hashes in |hs->new_session| in case we need them to verify a
  1259. // ChannelID signature on a resumption of this session in the future.
  1260. if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid &&
  1261. !tls1_record_handshake_hashes_for_channel_id(hs)) {
  1262. return ssl_hs_error;
  1263. }
  1264. return ssl_hs_ok;
  1265. }
  1266. static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
  1267. SSL *const ssl = hs->ssl;
  1268. if (hs->ticket_expected) {
  1269. const SSL_SESSION *session;
  1270. UniquePtr<SSL_SESSION> session_copy;
  1271. if (ssl->session == NULL) {
  1272. // Fix the timeout to measure from the ticket issuance time.
  1273. ssl_session_rebase_time(ssl, hs->new_session.get());
  1274. session = hs->new_session.get();
  1275. } else {
  1276. // We are renewing an existing session. Duplicate the session to adjust
  1277. // the timeout.
  1278. session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
  1279. if (!session_copy) {
  1280. return ssl_hs_error;
  1281. }
  1282. ssl_session_rebase_time(ssl, session_copy.get());
  1283. session = session_copy.get();
  1284. }
  1285. ScopedCBB cbb;
  1286. CBB body, ticket;
  1287. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1288. SSL3_MT_NEW_SESSION_TICKET) ||
  1289. !CBB_add_u32(&body, session->timeout) ||
  1290. !CBB_add_u16_length_prefixed(&body, &ticket) ||
  1291. !ssl_encrypt_ticket(ssl, &ticket, session) ||
  1292. !ssl_add_message_cbb(ssl, cbb.get())) {
  1293. return ssl_hs_error;
  1294. }
  1295. }
  1296. if (!ssl->method->add_change_cipher_spec(ssl) ||
  1297. !tls1_change_cipher_state(hs, evp_aead_seal) ||
  1298. !ssl_send_finished(hs)) {
  1299. return ssl_hs_error;
  1300. }
  1301. if (ssl->session != NULL) {
  1302. hs->state = state_read_change_cipher_spec;
  1303. } else {
  1304. hs->state = state_finish_server_handshake;
  1305. }
  1306. return ssl_hs_flush;
  1307. }
  1308. static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
  1309. SSL *const ssl = hs->ssl;
  1310. ssl->method->on_handshake_complete(ssl);
  1311. // If we aren't retaining peer certificates then we can discard it now.
  1312. if (hs->new_session != NULL && ssl->retain_only_sha256_of_client_certs) {
  1313. sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
  1314. hs->new_session->certs = NULL;
  1315. ssl->ctx->x509_method->session_clear(hs->new_session.get());
  1316. }
  1317. if (ssl->session != NULL) {
  1318. SSL_SESSION_up_ref(ssl->session);
  1319. ssl->s3->established_session.reset(ssl->session);
  1320. } else {
  1321. ssl->s3->established_session = std::move(hs->new_session);
  1322. ssl->s3->established_session->not_resumable = 0;
  1323. }
  1324. hs->handshake_finalized = true;
  1325. ssl->s3->initial_handshake_complete = true;
  1326. ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
  1327. hs->state = state_done;
  1328. return ssl_hs_ok;
  1329. }
  1330. enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
  1331. while (hs->state != state_done) {
  1332. enum ssl_hs_wait_t ret = ssl_hs_error;
  1333. enum ssl_server_hs_state_t state =
  1334. static_cast<enum ssl_server_hs_state_t>(hs->state);
  1335. switch (state) {
  1336. case state_start_accept:
  1337. ret = do_start_accept(hs);
  1338. break;
  1339. case state_read_client_hello:
  1340. ret = do_read_client_hello(hs);
  1341. break;
  1342. case state_select_certificate:
  1343. ret = do_select_certificate(hs);
  1344. break;
  1345. case state_tls13:
  1346. ret = do_tls13(hs);
  1347. break;
  1348. case state_select_parameters:
  1349. ret = do_select_parameters(hs);
  1350. break;
  1351. case state_send_server_hello:
  1352. ret = do_send_server_hello(hs);
  1353. break;
  1354. case state_send_server_certificate:
  1355. ret = do_send_server_certificate(hs);
  1356. break;
  1357. case state_send_server_key_exchange:
  1358. ret = do_send_server_key_exchange(hs);
  1359. break;
  1360. case state_send_server_hello_done:
  1361. ret = do_send_server_hello_done(hs);
  1362. break;
  1363. case state_read_client_certificate:
  1364. ret = do_read_client_certificate(hs);
  1365. break;
  1366. case state_verify_client_certificate:
  1367. ret = do_verify_client_certificate(hs);
  1368. break;
  1369. case state_read_client_key_exchange:
  1370. ret = do_read_client_key_exchange(hs);
  1371. break;
  1372. case state_read_client_certificate_verify:
  1373. ret = do_read_client_certificate_verify(hs);
  1374. break;
  1375. case state_read_change_cipher_spec:
  1376. ret = do_read_change_cipher_spec(hs);
  1377. break;
  1378. case state_process_change_cipher_spec:
  1379. ret = do_process_change_cipher_spec(hs);
  1380. break;
  1381. case state_read_next_proto:
  1382. ret = do_read_next_proto(hs);
  1383. break;
  1384. case state_read_channel_id:
  1385. ret = do_read_channel_id(hs);
  1386. break;
  1387. case state_read_client_finished:
  1388. ret = do_read_client_finished(hs);
  1389. break;
  1390. case state_send_server_finished:
  1391. ret = do_send_server_finished(hs);
  1392. break;
  1393. case state_finish_server_handshake:
  1394. ret = do_finish_server_handshake(hs);
  1395. break;
  1396. case state_done:
  1397. ret = ssl_hs_ok;
  1398. break;
  1399. }
  1400. if (hs->state != state) {
  1401. ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
  1402. }
  1403. if (ret != ssl_hs_ok) {
  1404. return ret;
  1405. }
  1406. }
  1407. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
  1408. return ssl_hs_ok;
  1409. }
  1410. const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
  1411. enum ssl_server_hs_state_t state =
  1412. static_cast<enum ssl_server_hs_state_t>(hs->state);
  1413. switch (state) {
  1414. case state_start_accept:
  1415. return "TLS server start_accept";
  1416. case state_read_client_hello:
  1417. return "TLS server read_client_hello";
  1418. case state_select_certificate:
  1419. return "TLS server select_certificate";
  1420. case state_tls13:
  1421. return tls13_server_handshake_state(hs);
  1422. case state_select_parameters:
  1423. return "TLS server select_parameters";
  1424. case state_send_server_hello:
  1425. return "TLS server send_server_hello";
  1426. case state_send_server_certificate:
  1427. return "TLS server send_server_certificate";
  1428. case state_send_server_key_exchange:
  1429. return "TLS server send_server_key_exchange";
  1430. case state_send_server_hello_done:
  1431. return "TLS server send_server_hello_done";
  1432. case state_read_client_certificate:
  1433. return "TLS server read_client_certificate";
  1434. case state_verify_client_certificate:
  1435. return "TLS server verify_client_certificate";
  1436. case state_read_client_key_exchange:
  1437. return "TLS server read_client_key_exchange";
  1438. case state_read_client_certificate_verify:
  1439. return "TLS server read_client_certificate_verify";
  1440. case state_read_change_cipher_spec:
  1441. return "TLS server read_change_cipher_spec";
  1442. case state_process_change_cipher_spec:
  1443. return "TLS server process_change_cipher_spec";
  1444. case state_read_next_proto:
  1445. return "TLS server read_next_proto";
  1446. case state_read_channel_id:
  1447. return "TLS server read_channel_id";
  1448. case state_read_client_finished:
  1449. return "TLS server read_client_finished";
  1450. case state_send_server_finished:
  1451. return "TLS server send_server_finished";
  1452. case state_finish_server_handshake:
  1453. return "TLS server finish_server_handshake";
  1454. case state_done:
  1455. return "TLS server done";
  1456. }
  1457. return "TLS server unknown";
  1458. }
  1459. }