Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

823 řádky
27 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-2002 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. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  110. * ECC cipher suite support in OpenSSL originally developed by
  111. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
  112. #include <openssl/ssl.h>
  113. #include <assert.h>
  114. #include <limits.h>
  115. #include <string.h>
  116. #include <openssl/buf.h>
  117. #include <openssl/bytestring.h>
  118. #include <openssl/err.h>
  119. #include <openssl/evp.h>
  120. #include <openssl/mem.h>
  121. #include <openssl/md5.h>
  122. #include <openssl/nid.h>
  123. #include <openssl/rand.h>
  124. #include <openssl/sha.h>
  125. #include <openssl/x509.h>
  126. #include "internal.h"
  127. SSL_HANDSHAKE *ssl_handshake_new(SSL *ssl) {
  128. SSL_HANDSHAKE *hs = OPENSSL_malloc(sizeof(SSL_HANDSHAKE));
  129. if (hs == NULL) {
  130. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  131. return NULL;
  132. }
  133. memset(hs, 0, sizeof(SSL_HANDSHAKE));
  134. hs->ssl = ssl;
  135. hs->wait = ssl_hs_ok;
  136. hs->state = SSL_ST_INIT;
  137. return hs;
  138. }
  139. void ssl_handshake_free(SSL_HANDSHAKE *hs) {
  140. if (hs == NULL) {
  141. return;
  142. }
  143. OPENSSL_cleanse(hs->secret, sizeof(hs->secret));
  144. OPENSSL_cleanse(hs->client_traffic_secret_0,
  145. sizeof(hs->client_traffic_secret_0));
  146. OPENSSL_cleanse(hs->server_traffic_secret_0,
  147. sizeof(hs->server_traffic_secret_0));
  148. SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx);
  149. OPENSSL_free(hs->cookie);
  150. OPENSSL_free(hs->key_share_bytes);
  151. OPENSSL_free(hs->public_key);
  152. OPENSSL_free(hs->peer_sigalgs);
  153. OPENSSL_free(hs->peer_supported_group_list);
  154. OPENSSL_free(hs->peer_key);
  155. OPENSSL_free(hs->server_params);
  156. OPENSSL_free(hs->peer_psk_identity_hint);
  157. sk_X509_NAME_pop_free(hs->ca_names, X509_NAME_free);
  158. OPENSSL_free(hs->certificate_types);
  159. if (hs->key_block != NULL) {
  160. OPENSSL_cleanse(hs->key_block, hs->key_block_len);
  161. OPENSSL_free(hs->key_block);
  162. }
  163. OPENSSL_free(hs->hostname);
  164. EVP_PKEY_free(hs->peer_pubkey);
  165. OPENSSL_free(hs);
  166. }
  167. /* ssl3_do_write sends |ssl->init_buf| in records of type 'type'
  168. * (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC). It returns 1 on success
  169. * and <= 0 on error. */
  170. static int ssl3_do_write(SSL *ssl, int type, const uint8_t *data, size_t len) {
  171. int ret = ssl3_write_bytes(ssl, type, data, len);
  172. if (ret <= 0) {
  173. return ret;
  174. }
  175. /* ssl3_write_bytes writes the data in its entirety. */
  176. assert((size_t)ret == len);
  177. ssl_do_msg_callback(ssl, 1 /* write */, type, data, len);
  178. return 1;
  179. }
  180. int ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
  181. CBB_zero(cbb);
  182. if (ssl->s3->pending_message != NULL) {
  183. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  184. return 0;
  185. }
  186. /* Pick a modest size hint to save most of the |realloc| calls. */
  187. if (!CBB_init(cbb, 64) ||
  188. !CBB_add_u8(cbb, type) ||
  189. !CBB_add_u24_length_prefixed(cbb, body)) {
  190. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  191. return 0;
  192. }
  193. return 1;
  194. }
  195. int ssl3_finish_message(SSL *ssl, CBB *cbb, uint8_t **out_msg,
  196. size_t *out_len) {
  197. if (!CBB_finish(cbb, out_msg, out_len)) {
  198. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  199. return 0;
  200. }
  201. return 1;
  202. }
  203. int ssl3_queue_message(SSL *ssl, uint8_t *msg, size_t len) {
  204. if (ssl->s3->pending_message != NULL ||
  205. len > 0xffffffffu) {
  206. OPENSSL_free(msg);
  207. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  208. return 0;
  209. }
  210. ssl3_update_handshake_hash(ssl, msg, len);
  211. ssl->s3->pending_message = msg;
  212. ssl->s3->pending_message_len = (uint32_t)len;
  213. return 1;
  214. }
  215. int ssl_complete_message(SSL *ssl, CBB *cbb) {
  216. uint8_t *msg;
  217. size_t len;
  218. if (!ssl->method->finish_message(ssl, cbb, &msg, &len) ||
  219. !ssl->method->queue_message(ssl, msg, len)) {
  220. return 0;
  221. }
  222. return 1;
  223. }
  224. int ssl3_write_message(SSL *ssl) {
  225. if (ssl->s3->pending_message == NULL) {
  226. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  227. return 0;
  228. }
  229. int ret = ssl3_do_write(ssl, SSL3_RT_HANDSHAKE, ssl->s3->pending_message,
  230. ssl->s3->pending_message_len);
  231. if (ret <= 0) {
  232. return ret;
  233. }
  234. OPENSSL_free(ssl->s3->pending_message);
  235. ssl->s3->pending_message = NULL;
  236. ssl->s3->pending_message_len = 0;
  237. return 1;
  238. }
  239. int ssl3_send_finished(SSL_HANDSHAKE *hs, int a, int b) {
  240. SSL *const ssl = hs->ssl;
  241. if (hs->state == b) {
  242. return ssl->method->write_message(ssl);
  243. }
  244. uint8_t finished[EVP_MAX_MD_SIZE];
  245. size_t finished_len =
  246. ssl->s3->enc_method->final_finish_mac(ssl, ssl->server, finished);
  247. if (finished_len == 0) {
  248. return 0;
  249. }
  250. /* Log the master secret, if logging is enabled. */
  251. if (!ssl_log_secret(ssl, "CLIENT_RANDOM",
  252. SSL_get_session(ssl)->master_key,
  253. SSL_get_session(ssl)->master_key_length)) {
  254. return 0;
  255. }
  256. /* Copy the Finished so we can use it for renegotiation checks. */
  257. if (ssl->version != SSL3_VERSION) {
  258. if (finished_len > sizeof(ssl->s3->previous_client_finished) ||
  259. finished_len > sizeof(ssl->s3->previous_server_finished)) {
  260. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  261. return -1;
  262. }
  263. if (ssl->server) {
  264. memcpy(ssl->s3->previous_server_finished, finished, finished_len);
  265. ssl->s3->previous_server_finished_len = finished_len;
  266. } else {
  267. memcpy(ssl->s3->previous_client_finished, finished, finished_len);
  268. ssl->s3->previous_client_finished_len = finished_len;
  269. }
  270. }
  271. CBB cbb, body;
  272. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_FINISHED) ||
  273. !CBB_add_bytes(&body, finished, finished_len) ||
  274. !ssl_complete_message(ssl, &cbb)) {
  275. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  276. CBB_cleanup(&cbb);
  277. return -1;
  278. }
  279. hs->state = b;
  280. return ssl->method->write_message(ssl);
  281. }
  282. int ssl3_get_finished(SSL_HANDSHAKE *hs) {
  283. SSL *const ssl = hs->ssl;
  284. int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_FINISHED,
  285. ssl_dont_hash_message);
  286. if (ret <= 0) {
  287. return ret;
  288. }
  289. /* Snapshot the finished hash before incorporating the new message. */
  290. uint8_t finished[EVP_MAX_MD_SIZE];
  291. size_t finished_len =
  292. ssl->s3->enc_method->final_finish_mac(ssl, !ssl->server, finished);
  293. if (finished_len == 0 ||
  294. !ssl_hash_current_message(ssl)) {
  295. return -1;
  296. }
  297. int finished_ok = ssl->init_num == finished_len &&
  298. CRYPTO_memcmp(ssl->init_msg, finished, finished_len) == 0;
  299. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  300. finished_ok = 1;
  301. #endif
  302. if (!finished_ok) {
  303. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  304. OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
  305. return -1;
  306. }
  307. /* Copy the Finished so we can use it for renegotiation checks. */
  308. if (ssl->version != SSL3_VERSION) {
  309. if (finished_len > sizeof(ssl->s3->previous_client_finished) ||
  310. finished_len > sizeof(ssl->s3->previous_server_finished)) {
  311. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  312. return -1;
  313. }
  314. if (ssl->server) {
  315. memcpy(ssl->s3->previous_client_finished, finished, finished_len);
  316. ssl->s3->previous_client_finished_len = finished_len;
  317. } else {
  318. memcpy(ssl->s3->previous_server_finished, finished, finished_len);
  319. ssl->s3->previous_server_finished_len = finished_len;
  320. }
  321. }
  322. return 1;
  323. }
  324. int ssl3_send_change_cipher_spec(SSL *ssl) {
  325. static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
  326. return ssl3_do_write(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec,
  327. sizeof(kChangeCipherSpec));
  328. }
  329. int ssl3_output_cert_chain(SSL *ssl) {
  330. CBB cbb, body;
  331. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CERTIFICATE) ||
  332. !ssl_add_cert_chain(ssl, &body) ||
  333. !ssl_complete_message(ssl, &cbb)) {
  334. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  335. CBB_cleanup(&cbb);
  336. return 0;
  337. }
  338. return 1;
  339. }
  340. size_t ssl_max_handshake_message_len(const SSL *ssl) {
  341. /* kMaxMessageLen is the default maximum message size for handshakes which do
  342. * not accept peer certificate chains. */
  343. static const size_t kMaxMessageLen = 16384;
  344. if (SSL_in_init(ssl)) {
  345. if ((!ssl->server || (ssl->verify_mode & SSL_VERIFY_PEER)) &&
  346. kMaxMessageLen < ssl->max_cert_list) {
  347. return ssl->max_cert_list;
  348. }
  349. return kMaxMessageLen;
  350. }
  351. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  352. /* In TLS 1.2 and below, the largest acceptable post-handshake message is
  353. * a HelloRequest. */
  354. return 0;
  355. }
  356. if (ssl->server) {
  357. /* The largest acceptable post-handshake message for a server is a
  358. * KeyUpdate. We will never initiate post-handshake auth. */
  359. return 0;
  360. }
  361. /* Clients must accept NewSessionTicket and CertificateRequest, so allow the
  362. * default size. */
  363. return kMaxMessageLen;
  364. }
  365. static int extend_handshake_buffer(SSL *ssl, size_t length) {
  366. if (!BUF_MEM_reserve(ssl->init_buf, length)) {
  367. return -1;
  368. }
  369. while (ssl->init_buf->length < length) {
  370. int ret = ssl3_read_handshake_bytes(
  371. ssl, (uint8_t *)ssl->init_buf->data + ssl->init_buf->length,
  372. length - ssl->init_buf->length);
  373. if (ret <= 0) {
  374. return ret;
  375. }
  376. ssl->init_buf->length += (size_t)ret;
  377. }
  378. return 1;
  379. }
  380. static int read_v2_client_hello(SSL *ssl, int *out_is_v2_client_hello) {
  381. /* Read the first 5 bytes, the size of the TLS record header. This is
  382. * sufficient to detect a V2ClientHello and ensures that we never read beyond
  383. * the first record. */
  384. int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH);
  385. if (ret <= 0) {
  386. return ret;
  387. }
  388. const uint8_t *p = ssl_read_buffer(ssl);
  389. /* Some dedicated error codes for protocol mixups should the application wish
  390. * to interpret them differently. (These do not overlap with ClientHello or
  391. * V2ClientHello.) */
  392. if (strncmp("GET ", (const char *)p, 4) == 0 ||
  393. strncmp("POST ", (const char *)p, 5) == 0 ||
  394. strncmp("HEAD ", (const char *)p, 5) == 0 ||
  395. strncmp("PUT ", (const char *)p, 4) == 0) {
  396. OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
  397. return -1;
  398. }
  399. if (strncmp("CONNE", (const char *)p, 5) == 0) {
  400. OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
  401. return -1;
  402. }
  403. if ((p[0] & 0x80) == 0 || p[2] != SSL2_MT_CLIENT_HELLO ||
  404. p[3] != SSL3_VERSION_MAJOR) {
  405. /* Not a V2ClientHello. */
  406. *out_is_v2_client_hello = 0;
  407. return 1;
  408. }
  409. /* Determine the length of the V2ClientHello. */
  410. size_t msg_length = ((p[0] & 0x7f) << 8) | p[1];
  411. if (msg_length > (1024 * 4)) {
  412. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
  413. return -1;
  414. }
  415. if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
  416. /* Reject lengths that are too short early. We have already read
  417. * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
  418. * (invalid) V2ClientHello which would be shorter than that. */
  419. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
  420. return -1;
  421. }
  422. /* Read the remainder of the V2ClientHello. */
  423. ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length);
  424. if (ret <= 0) {
  425. return ret;
  426. }
  427. CBS v2_client_hello;
  428. CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length);
  429. /* The V2ClientHello without the length is incorporated into the handshake
  430. * hash. */
  431. if (!ssl3_update_handshake_hash(ssl, CBS_data(&v2_client_hello),
  432. CBS_len(&v2_client_hello))) {
  433. return -1;
  434. }
  435. ssl_do_msg_callback(ssl, 0 /* read */, 0 /* V2ClientHello */,
  436. CBS_data(&v2_client_hello), CBS_len(&v2_client_hello));
  437. uint8_t msg_type;
  438. uint16_t version, cipher_spec_length, session_id_length, challenge_length;
  439. CBS cipher_specs, session_id, challenge;
  440. if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
  441. !CBS_get_u16(&v2_client_hello, &version) ||
  442. !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
  443. !CBS_get_u16(&v2_client_hello, &session_id_length) ||
  444. !CBS_get_u16(&v2_client_hello, &challenge_length) ||
  445. !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
  446. !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
  447. !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
  448. CBS_len(&v2_client_hello) != 0) {
  449. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  450. return -1;
  451. }
  452. /* msg_type has already been checked. */
  453. assert(msg_type == SSL2_MT_CLIENT_HELLO);
  454. /* The client_random is the V2ClientHello challenge. Truncate or
  455. * left-pad with zeros as needed. */
  456. size_t rand_len = CBS_len(&challenge);
  457. if (rand_len > SSL3_RANDOM_SIZE) {
  458. rand_len = SSL3_RANDOM_SIZE;
  459. }
  460. uint8_t random[SSL3_RANDOM_SIZE];
  461. memset(random, 0, SSL3_RANDOM_SIZE);
  462. memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
  463. rand_len);
  464. /* Write out an equivalent SSLv3 ClientHello. */
  465. size_t max_v3_client_hello = SSL3_HM_HEADER_LENGTH + 2 /* version */ +
  466. SSL3_RANDOM_SIZE + 1 /* session ID length */ +
  467. 2 /* cipher list length */ +
  468. CBS_len(&cipher_specs) / 3 * 2 +
  469. 1 /* compression length */ + 1 /* compression */;
  470. CBB client_hello, hello_body, cipher_suites;
  471. CBB_zero(&client_hello);
  472. if (!BUF_MEM_reserve(ssl->init_buf, max_v3_client_hello) ||
  473. !CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data,
  474. ssl->init_buf->max) ||
  475. !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
  476. !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
  477. !CBB_add_u16(&hello_body, version) ||
  478. !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
  479. /* No session id. */
  480. !CBB_add_u8(&hello_body, 0) ||
  481. !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
  482. CBB_cleanup(&client_hello);
  483. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  484. return -1;
  485. }
  486. /* Copy the cipher suites. */
  487. while (CBS_len(&cipher_specs) > 0) {
  488. uint32_t cipher_spec;
  489. if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
  490. CBB_cleanup(&client_hello);
  491. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  492. return -1;
  493. }
  494. /* Skip SSLv2 ciphers. */
  495. if ((cipher_spec & 0xff0000) != 0) {
  496. continue;
  497. }
  498. if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
  499. CBB_cleanup(&client_hello);
  500. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  501. return -1;
  502. }
  503. }
  504. /* Add the null compression scheme and finish. */
  505. if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) ||
  506. !CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) {
  507. CBB_cleanup(&client_hello);
  508. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  509. return -1;
  510. }
  511. /* Consume and discard the V2ClientHello. */
  512. ssl_read_buffer_consume(ssl, 2 + msg_length);
  513. ssl_read_buffer_discard(ssl);
  514. *out_is_v2_client_hello = 1;
  515. return 1;
  516. }
  517. int ssl3_get_message(SSL *ssl, int msg_type,
  518. enum ssl_hash_message_t hash_message) {
  519. again:
  520. /* Re-create the handshake buffer if needed. */
  521. if (ssl->init_buf == NULL) {
  522. ssl->init_buf = BUF_MEM_new();
  523. if (ssl->init_buf == NULL) {
  524. return -1;
  525. }
  526. }
  527. if (ssl->server && !ssl->s3->v2_hello_done) {
  528. /* Bypass the record layer for the first message to handle V2ClientHello. */
  529. assert(hash_message == ssl_hash_message);
  530. int is_v2_client_hello = 0;
  531. int ret = read_v2_client_hello(ssl, &is_v2_client_hello);
  532. if (ret <= 0) {
  533. return ret;
  534. }
  535. if (is_v2_client_hello) {
  536. /* V2ClientHello is hashed separately. */
  537. hash_message = ssl_dont_hash_message;
  538. }
  539. ssl->s3->v2_hello_done = 1;
  540. }
  541. if (ssl->s3->tmp.reuse_message) {
  542. /* A ssl_dont_hash_message call cannot be combined with reuse_message; the
  543. * ssl_dont_hash_message would have to have been applied to the previous
  544. * call. */
  545. assert(hash_message == ssl_hash_message);
  546. assert(ssl->init_msg != NULL);
  547. ssl->s3->tmp.reuse_message = 0;
  548. hash_message = ssl_dont_hash_message;
  549. } else {
  550. ssl3_release_current_message(ssl, 0 /* don't free buffer */);
  551. }
  552. /* Read the message header, if we haven't yet. */
  553. int ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH);
  554. if (ret <= 0) {
  555. return ret;
  556. }
  557. /* Parse out the length. Cap it so the peer cannot force us to buffer up to
  558. * 2^24 bytes. */
  559. const uint8_t *p = (uint8_t *)ssl->init_buf->data;
  560. size_t msg_len = (((uint32_t)p[1]) << 16) | (((uint32_t)p[2]) << 8) | p[3];
  561. if (msg_len > ssl_max_handshake_message_len(ssl)) {
  562. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  563. OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  564. return -1;
  565. }
  566. /* Read the message body, if we haven't yet. */
  567. ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH + msg_len);
  568. if (ret <= 0) {
  569. return ret;
  570. }
  571. /* We have now received a complete message. */
  572. ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, ssl->init_buf->data,
  573. ssl->init_buf->length);
  574. ssl->s3->tmp.message_type = ((const uint8_t *)ssl->init_buf->data)[0];
  575. ssl->init_msg = (uint8_t*)ssl->init_buf->data + SSL3_HM_HEADER_LENGTH;
  576. ssl->init_num = ssl->init_buf->length - SSL3_HM_HEADER_LENGTH;
  577. /* Ignore stray HelloRequest messages in the handshake before TLS 1.3. Per RFC
  578. * 5246, section 7.4.1.1, the server may send HelloRequest at any time. */
  579. if (!ssl->server && SSL_in_init(ssl) &&
  580. (!ssl->s3->have_version || ssl3_protocol_version(ssl) < TLS1_3_VERSION) &&
  581. ssl->s3->tmp.message_type == SSL3_MT_HELLO_REQUEST &&
  582. ssl->init_num == 0) {
  583. goto again;
  584. }
  585. if (msg_type >= 0 && ssl->s3->tmp.message_type != msg_type) {
  586. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  587. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  588. return -1;
  589. }
  590. /* Feed this message into MAC computation. */
  591. if (hash_message == ssl_hash_message && !ssl_hash_current_message(ssl)) {
  592. return -1;
  593. }
  594. return 1;
  595. }
  596. void ssl3_get_current_message(const SSL *ssl, CBS *out) {
  597. CBS_init(out, (uint8_t *)ssl->init_buf->data, ssl->init_buf->length);
  598. }
  599. int ssl_hash_current_message(SSL *ssl) {
  600. CBS cbs;
  601. ssl->method->get_current_message(ssl, &cbs);
  602. return ssl3_update_handshake_hash(ssl, CBS_data(&cbs), CBS_len(&cbs));
  603. }
  604. void ssl3_release_current_message(SSL *ssl, int free_buffer) {
  605. if (ssl->init_msg != NULL) {
  606. /* |init_buf| never contains data beyond the current message. */
  607. assert(SSL3_HM_HEADER_LENGTH + ssl->init_num == ssl->init_buf->length);
  608. /* Clear the current message. */
  609. ssl->init_msg = NULL;
  610. ssl->init_num = 0;
  611. ssl->init_buf->length = 0;
  612. }
  613. if (free_buffer) {
  614. BUF_MEM_free(ssl->init_buf);
  615. ssl->init_buf = NULL;
  616. }
  617. }
  618. int ssl_verify_alarm_type(long type) {
  619. int al;
  620. switch (type) {
  621. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  622. case X509_V_ERR_UNABLE_TO_GET_CRL:
  623. case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
  624. al = SSL_AD_UNKNOWN_CA;
  625. break;
  626. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  627. case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  628. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  629. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  630. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  631. case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  632. case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  633. case X509_V_ERR_CERT_NOT_YET_VALID:
  634. case X509_V_ERR_CRL_NOT_YET_VALID:
  635. case X509_V_ERR_CERT_UNTRUSTED:
  636. case X509_V_ERR_CERT_REJECTED:
  637. case X509_V_ERR_HOSTNAME_MISMATCH:
  638. case X509_V_ERR_EMAIL_MISMATCH:
  639. case X509_V_ERR_IP_ADDRESS_MISMATCH:
  640. al = SSL_AD_BAD_CERTIFICATE;
  641. break;
  642. case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  643. case X509_V_ERR_CRL_SIGNATURE_FAILURE:
  644. al = SSL_AD_DECRYPT_ERROR;
  645. break;
  646. case X509_V_ERR_CERT_HAS_EXPIRED:
  647. case X509_V_ERR_CRL_HAS_EXPIRED:
  648. al = SSL_AD_CERTIFICATE_EXPIRED;
  649. break;
  650. case X509_V_ERR_CERT_REVOKED:
  651. al = SSL_AD_CERTIFICATE_REVOKED;
  652. break;
  653. case X509_V_ERR_UNSPECIFIED:
  654. case X509_V_ERR_OUT_OF_MEM:
  655. case X509_V_ERR_INVALID_CALL:
  656. case X509_V_ERR_STORE_LOOKUP:
  657. al = SSL_AD_INTERNAL_ERROR;
  658. break;
  659. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  660. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  661. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  662. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  663. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  664. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  665. case X509_V_ERR_INVALID_CA:
  666. al = SSL_AD_UNKNOWN_CA;
  667. break;
  668. case X509_V_ERR_APPLICATION_VERIFICATION:
  669. al = SSL_AD_HANDSHAKE_FAILURE;
  670. break;
  671. case X509_V_ERR_INVALID_PURPOSE:
  672. al = SSL_AD_UNSUPPORTED_CERTIFICATE;
  673. break;
  674. default:
  675. al = SSL_AD_CERTIFICATE_UNKNOWN;
  676. break;
  677. }
  678. return al;
  679. }
  680. int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
  681. const SSL_EXTENSION_TYPE *ext_types,
  682. size_t num_ext_types) {
  683. /* Reset everything. */
  684. for (size_t i = 0; i < num_ext_types; i++) {
  685. *ext_types[i].out_present = 0;
  686. CBS_init(ext_types[i].out_data, NULL, 0);
  687. }
  688. CBS copy = *cbs;
  689. while (CBS_len(&copy) != 0) {
  690. uint16_t type;
  691. CBS data;
  692. if (!CBS_get_u16(&copy, &type) ||
  693. !CBS_get_u16_length_prefixed(&copy, &data)) {
  694. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  695. *out_alert = SSL_AD_DECODE_ERROR;
  696. return 0;
  697. }
  698. const SSL_EXTENSION_TYPE *ext_type = NULL;
  699. for (size_t i = 0; i < num_ext_types; i++) {
  700. if (type == ext_types[i].type) {
  701. ext_type = &ext_types[i];
  702. break;
  703. }
  704. }
  705. if (ext_type == NULL) {
  706. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  707. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  708. return 0;
  709. }
  710. /* Duplicate ext_types are forbidden. */
  711. if (*ext_type->out_present) {
  712. OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION);
  713. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  714. return 0;
  715. }
  716. *ext_type->out_present = 1;
  717. *ext_type->out_data = data;
  718. }
  719. return 1;
  720. }