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.
 
 
 
 
 
 

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