You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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