Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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