Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

1830 linhas
60 KiB

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