Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

656 Zeilen
23 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. BSSL_NAMESPACE_BEGIN
  128. static bool add_record_to_flight(SSL *ssl, uint8_t type,
  129. Span<const uint8_t> in) {
  130. // The caller should have flushed |pending_hs_data| first.
  131. assert(!ssl->s3->pending_hs_data);
  132. // We'll never add a flight while in the process of writing it out.
  133. assert(ssl->s3->pending_flight_offset == 0);
  134. if (ssl->s3->pending_flight == nullptr) {
  135. ssl->s3->pending_flight.reset(BUF_MEM_new());
  136. if (ssl->s3->pending_flight == nullptr) {
  137. return false;
  138. }
  139. }
  140. size_t max_out = in.size() + SSL_max_seal_overhead(ssl);
  141. size_t new_cap = ssl->s3->pending_flight->length + max_out;
  142. if (max_out < in.size() || new_cap < max_out) {
  143. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  144. return false;
  145. }
  146. size_t len;
  147. if (!BUF_MEM_reserve(ssl->s3->pending_flight.get(), new_cap) ||
  148. !tls_seal_record(ssl,
  149. (uint8_t *)ssl->s3->pending_flight->data +
  150. ssl->s3->pending_flight->length,
  151. &len, max_out, type, in.data(), in.size())) {
  152. return false;
  153. }
  154. ssl->s3->pending_flight->length += len;
  155. return true;
  156. }
  157. bool ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) {
  158. // Pick a modest size hint to save most of the |realloc| calls.
  159. if (!CBB_init(cbb, 64) ||
  160. !CBB_add_u8(cbb, type) ||
  161. !CBB_add_u24_length_prefixed(cbb, body)) {
  162. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  163. CBB_cleanup(cbb);
  164. return false;
  165. }
  166. return true;
  167. }
  168. bool ssl3_finish_message(SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg) {
  169. return CBBFinishArray(cbb, out_msg);
  170. }
  171. bool ssl3_add_message(SSL *ssl, Array<uint8_t> msg) {
  172. // Pack handshake data into the minimal number of records. This avoids
  173. // unnecessary encryption overhead, notably in TLS 1.3 where we send several
  174. // encrypted messages in a row. For now, we do not do this for the null
  175. // cipher. The benefit is smaller and there is a risk of breaking buggy
  176. // implementations.
  177. //
  178. // TODO(davidben): See if we can do this uniformly.
  179. Span<const uint8_t> rest = msg;
  180. if (ssl->quic_method == nullptr &&
  181. ssl->s3->aead_write_ctx->is_null_cipher()) {
  182. while (!rest.empty()) {
  183. Span<const uint8_t> chunk = rest.subspan(0, ssl->max_send_fragment);
  184. rest = rest.subspan(chunk.size());
  185. if (!add_record_to_flight(ssl, SSL3_RT_HANDSHAKE, chunk)) {
  186. return false;
  187. }
  188. }
  189. } else {
  190. while (!rest.empty()) {
  191. // Flush if |pending_hs_data| is full.
  192. if (ssl->s3->pending_hs_data &&
  193. ssl->s3->pending_hs_data->length >= ssl->max_send_fragment &&
  194. !tls_flush_pending_hs_data(ssl)) {
  195. return false;
  196. }
  197. size_t pending_len =
  198. ssl->s3->pending_hs_data ? ssl->s3->pending_hs_data->length : 0;
  199. Span<const uint8_t> chunk =
  200. rest.subspan(0, ssl->max_send_fragment - pending_len);
  201. assert(!chunk.empty());
  202. rest = rest.subspan(chunk.size());
  203. if (!ssl->s3->pending_hs_data) {
  204. ssl->s3->pending_hs_data.reset(BUF_MEM_new());
  205. }
  206. if (!ssl->s3->pending_hs_data ||
  207. !BUF_MEM_append(ssl->s3->pending_hs_data.get(), chunk.data(),
  208. chunk.size())) {
  209. return false;
  210. }
  211. }
  212. }
  213. ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, msg);
  214. // TODO(svaldez): Move this up a layer to fix abstraction for SSLTranscript on
  215. // hs.
  216. if (ssl->s3->hs != NULL &&
  217. !ssl->s3->hs->transcript.Update(msg)) {
  218. return false;
  219. }
  220. return true;
  221. }
  222. bool tls_flush_pending_hs_data(SSL *ssl) {
  223. if (!ssl->s3->pending_hs_data || ssl->s3->pending_hs_data->length == 0) {
  224. return true;
  225. }
  226. UniquePtr<BUF_MEM> pending_hs_data = std::move(ssl->s3->pending_hs_data);
  227. auto data =
  228. MakeConstSpan(reinterpret_cast<const uint8_t *>(pending_hs_data->data),
  229. pending_hs_data->length);
  230. if (ssl->quic_method) {
  231. if (!ssl->quic_method->add_handshake_data(ssl, ssl->s3->write_level,
  232. data.data(), data.size())) {
  233. OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR);
  234. return false;
  235. }
  236. return true;
  237. }
  238. return add_record_to_flight(ssl, SSL3_RT_HANDSHAKE, data);
  239. }
  240. bool ssl3_add_change_cipher_spec(SSL *ssl) {
  241. static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS};
  242. if (!tls_flush_pending_hs_data(ssl)) {
  243. return false;
  244. }
  245. if (!ssl->quic_method &&
  246. !add_record_to_flight(ssl, SSL3_RT_CHANGE_CIPHER_SPEC,
  247. kChangeCipherSpec)) {
  248. return false;
  249. }
  250. ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC,
  251. kChangeCipherSpec);
  252. return true;
  253. }
  254. int ssl3_flush_flight(SSL *ssl) {
  255. if (!tls_flush_pending_hs_data(ssl)) {
  256. return -1;
  257. }
  258. if (ssl->quic_method) {
  259. if (ssl->s3->write_shutdown != ssl_shutdown_none) {
  260. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  261. return -1;
  262. }
  263. if (!ssl->quic_method->flush_flight(ssl)) {
  264. OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR);
  265. return -1;
  266. }
  267. }
  268. if (ssl->s3->pending_flight == nullptr) {
  269. return 1;
  270. }
  271. if (ssl->s3->write_shutdown != ssl_shutdown_none) {
  272. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  273. return -1;
  274. }
  275. static_assert(INT_MAX <= 0xffffffff, "int is larger than 32 bits");
  276. if (ssl->s3->pending_flight->length > INT_MAX) {
  277. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  278. return -1;
  279. }
  280. // If there is pending data in the write buffer, it must be flushed out before
  281. // any new data in pending_flight.
  282. if (!ssl->s3->write_buffer.empty()) {
  283. int ret = ssl_write_buffer_flush(ssl);
  284. if (ret <= 0) {
  285. ssl->s3->rwstate = SSL_WRITING;
  286. return ret;
  287. }
  288. }
  289. // Write the pending flight.
  290. while (ssl->s3->pending_flight_offset < ssl->s3->pending_flight->length) {
  291. int ret = BIO_write(
  292. ssl->wbio.get(),
  293. ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset,
  294. ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset);
  295. if (ret <= 0) {
  296. ssl->s3->rwstate = SSL_WRITING;
  297. return ret;
  298. }
  299. ssl->s3->pending_flight_offset += ret;
  300. }
  301. if (BIO_flush(ssl->wbio.get()) <= 0) {
  302. ssl->s3->rwstate = SSL_WRITING;
  303. return -1;
  304. }
  305. ssl->s3->pending_flight.reset();
  306. ssl->s3->pending_flight_offset = 0;
  307. return 1;
  308. }
  309. static ssl_open_record_t read_v2_client_hello(SSL *ssl, size_t *out_consumed,
  310. Span<const uint8_t> in) {
  311. *out_consumed = 0;
  312. assert(in.size() >= SSL3_RT_HEADER_LENGTH);
  313. // Determine the length of the V2ClientHello.
  314. size_t msg_length = ((in[0] & 0x7f) << 8) | in[1];
  315. if (msg_length > (1024 * 4)) {
  316. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
  317. return ssl_open_record_error;
  318. }
  319. if (msg_length < SSL3_RT_HEADER_LENGTH - 2) {
  320. // Reject lengths that are too short early. We have already read
  321. // |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an
  322. // (invalid) V2ClientHello which would be shorter than that.
  323. OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH);
  324. return ssl_open_record_error;
  325. }
  326. // Ask for the remainder of the V2ClientHello.
  327. if (in.size() < 2 + msg_length) {
  328. *out_consumed = 2 + msg_length;
  329. return ssl_open_record_partial;
  330. }
  331. CBS v2_client_hello = CBS(ssl->s3->read_buffer.span().subspan(2, msg_length));
  332. // The V2ClientHello without the length is incorporated into the handshake
  333. // hash. This is only ever called at the start of the handshake, so hs is
  334. // guaranteed to be non-NULL.
  335. if (!ssl->s3->hs->transcript.Update(v2_client_hello)) {
  336. return ssl_open_record_error;
  337. }
  338. ssl_do_msg_callback(ssl, 0 /* read */, 0 /* V2ClientHello */,
  339. v2_client_hello);
  340. uint8_t msg_type;
  341. uint16_t version, cipher_spec_length, session_id_length, challenge_length;
  342. CBS cipher_specs, session_id, challenge;
  343. if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
  344. !CBS_get_u16(&v2_client_hello, &version) ||
  345. !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
  346. !CBS_get_u16(&v2_client_hello, &session_id_length) ||
  347. !CBS_get_u16(&v2_client_hello, &challenge_length) ||
  348. !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
  349. !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
  350. !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
  351. CBS_len(&v2_client_hello) != 0) {
  352. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  353. return ssl_open_record_error;
  354. }
  355. // msg_type has already been checked.
  356. assert(msg_type == SSL2_MT_CLIENT_HELLO);
  357. // The client_random is the V2ClientHello challenge. Truncate or left-pad with
  358. // zeros as needed.
  359. size_t rand_len = CBS_len(&challenge);
  360. if (rand_len > SSL3_RANDOM_SIZE) {
  361. rand_len = SSL3_RANDOM_SIZE;
  362. }
  363. uint8_t random[SSL3_RANDOM_SIZE];
  364. OPENSSL_memset(random, 0, SSL3_RANDOM_SIZE);
  365. OPENSSL_memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge),
  366. rand_len);
  367. // Write out an equivalent TLS ClientHello directly to the handshake buffer.
  368. size_t max_v3_client_hello = SSL3_HM_HEADER_LENGTH + 2 /* version */ +
  369. SSL3_RANDOM_SIZE + 1 /* session ID length */ +
  370. 2 /* cipher list length */ +
  371. CBS_len(&cipher_specs) / 3 * 2 +
  372. 1 /* compression length */ + 1 /* compression */;
  373. ScopedCBB client_hello;
  374. CBB hello_body, cipher_suites;
  375. if (!ssl->s3->hs_buf) {
  376. ssl->s3->hs_buf.reset(BUF_MEM_new());
  377. }
  378. if (!ssl->s3->hs_buf ||
  379. !BUF_MEM_reserve(ssl->s3->hs_buf.get(), max_v3_client_hello) ||
  380. !CBB_init_fixed(client_hello.get(), (uint8_t *)ssl->s3->hs_buf->data,
  381. ssl->s3->hs_buf->max) ||
  382. !CBB_add_u8(client_hello.get(), SSL3_MT_CLIENT_HELLO) ||
  383. !CBB_add_u24_length_prefixed(client_hello.get(), &hello_body) ||
  384. !CBB_add_u16(&hello_body, version) ||
  385. !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
  386. // No session id.
  387. !CBB_add_u8(&hello_body, 0) ||
  388. !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) {
  389. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  390. return ssl_open_record_error;
  391. }
  392. // Copy the cipher suites.
  393. while (CBS_len(&cipher_specs) > 0) {
  394. uint32_t cipher_spec;
  395. if (!CBS_get_u24(&cipher_specs, &cipher_spec)) {
  396. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  397. return ssl_open_record_error;
  398. }
  399. // Skip SSLv2 ciphers.
  400. if ((cipher_spec & 0xff0000) != 0) {
  401. continue;
  402. }
  403. if (!CBB_add_u16(&cipher_suites, cipher_spec)) {
  404. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  405. return ssl_open_record_error;
  406. }
  407. }
  408. // Add the null compression scheme and finish.
  409. if (!CBB_add_u8(&hello_body, 1) ||
  410. !CBB_add_u8(&hello_body, 0) ||
  411. !CBB_finish(client_hello.get(), NULL, &ssl->s3->hs_buf->length)) {
  412. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  413. return ssl_open_record_error;
  414. }
  415. *out_consumed = 2 + msg_length;
  416. ssl->s3->is_v2_hello = true;
  417. return ssl_open_record_success;
  418. }
  419. static bool parse_message(const SSL *ssl, SSLMessage *out,
  420. size_t *out_bytes_needed) {
  421. if (!ssl->s3->hs_buf) {
  422. *out_bytes_needed = 4;
  423. return false;
  424. }
  425. CBS cbs;
  426. uint32_t len;
  427. CBS_init(&cbs, reinterpret_cast<const uint8_t *>(ssl->s3->hs_buf->data),
  428. ssl->s3->hs_buf->length);
  429. if (!CBS_get_u8(&cbs, &out->type) ||
  430. !CBS_get_u24(&cbs, &len)) {
  431. *out_bytes_needed = 4;
  432. return false;
  433. }
  434. if (!CBS_get_bytes(&cbs, &out->body, len)) {
  435. *out_bytes_needed = 4 + len;
  436. return false;
  437. }
  438. CBS_init(&out->raw, reinterpret_cast<const uint8_t *>(ssl->s3->hs_buf->data),
  439. 4 + len);
  440. out->is_v2_hello = ssl->s3->is_v2_hello;
  441. return true;
  442. }
  443. bool ssl3_get_message(SSL *ssl, SSLMessage *out) {
  444. size_t unused;
  445. if (!parse_message(ssl, out, &unused)) {
  446. return false;
  447. }
  448. if (!ssl->s3->has_message) {
  449. if (!out->is_v2_hello) {
  450. ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, out->raw);
  451. }
  452. ssl->s3->has_message = true;
  453. }
  454. return true;
  455. }
  456. bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert) {
  457. // If there is a complete message, the caller must have consumed it first.
  458. SSLMessage msg;
  459. size_t bytes_needed;
  460. if (parse_message(ssl, &msg, &bytes_needed)) {
  461. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  462. *out_alert = SSL_AD_INTERNAL_ERROR;
  463. return false;
  464. }
  465. // Enforce the limit so the peer cannot force us to buffer 16MB.
  466. if (bytes_needed > 4 + ssl_max_handshake_message_len(ssl)) {
  467. OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE);
  468. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  469. return false;
  470. }
  471. return true;
  472. }
  473. bool tls_has_unprocessed_handshake_data(const SSL *ssl) {
  474. size_t msg_len = 0;
  475. if (ssl->s3->has_message) {
  476. SSLMessage msg;
  477. size_t unused;
  478. if (parse_message(ssl, &msg, &unused)) {
  479. msg_len = CBS_len(&msg.raw);
  480. }
  481. }
  482. return ssl->s3->hs_buf && ssl->s3->hs_buf->length > msg_len;
  483. }
  484. bool tls_append_handshake_data(SSL *ssl, Span<const uint8_t> data) {
  485. // Re-create the handshake buffer if needed.
  486. if (!ssl->s3->hs_buf) {
  487. ssl->s3->hs_buf.reset(BUF_MEM_new());
  488. }
  489. return ssl->s3->hs_buf &&
  490. BUF_MEM_append(ssl->s3->hs_buf.get(), data.data(), data.size());
  491. }
  492. ssl_open_record_t ssl3_open_handshake(SSL *ssl, size_t *out_consumed,
  493. uint8_t *out_alert, Span<uint8_t> in) {
  494. *out_consumed = 0;
  495. // Bypass the record layer for the first message to handle V2ClientHello.
  496. if (ssl->server && !ssl->s3->v2_hello_done) {
  497. // Ask for the first 5 bytes, the size of the TLS record header. This is
  498. // sufficient to detect a V2ClientHello and ensures that we never read
  499. // beyond the first record.
  500. if (in.size() < SSL3_RT_HEADER_LENGTH) {
  501. *out_consumed = SSL3_RT_HEADER_LENGTH;
  502. return ssl_open_record_partial;
  503. }
  504. // Some dedicated error codes for protocol mixups should the application
  505. // wish to interpret them differently. (These do not overlap with
  506. // ClientHello or V2ClientHello.)
  507. const char *str = reinterpret_cast<const char*>(in.data());
  508. if (strncmp("GET ", str, 4) == 0 ||
  509. strncmp("POST ", str, 5) == 0 ||
  510. strncmp("HEAD ", str, 5) == 0 ||
  511. strncmp("PUT ", str, 4) == 0) {
  512. OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST);
  513. *out_alert = 0;
  514. return ssl_open_record_error;
  515. }
  516. if (strncmp("CONNE", str, 5) == 0) {
  517. OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST);
  518. *out_alert = 0;
  519. return ssl_open_record_error;
  520. }
  521. // Check for a V2ClientHello.
  522. if ((in[0] & 0x80) != 0 && in[2] == SSL2_MT_CLIENT_HELLO &&
  523. in[3] == SSL3_VERSION_MAJOR) {
  524. auto ret = read_v2_client_hello(ssl, out_consumed, in);
  525. if (ret == ssl_open_record_error) {
  526. *out_alert = 0;
  527. } else if (ret == ssl_open_record_success) {
  528. ssl->s3->v2_hello_done = true;
  529. }
  530. return ret;
  531. }
  532. ssl->s3->v2_hello_done = true;
  533. }
  534. uint8_t type;
  535. Span<uint8_t> body;
  536. auto ret = tls_open_record(ssl, &type, &body, out_consumed, out_alert, in);
  537. if (ret != ssl_open_record_success) {
  538. return ret;
  539. }
  540. // WatchGuard's TLS 1.3 interference bug is very distinctive: they drop the
  541. // ServerHello and send the remaining encrypted application data records
  542. // as-is. This manifests as an application data record when we expect
  543. // handshake. Report a dedicated error code for this case.
  544. if (!ssl->server && type == SSL3_RT_APPLICATION_DATA &&
  545. ssl->s3->aead_read_ctx->is_null_cipher()) {
  546. OPENSSL_PUT_ERROR(SSL, SSL_R_APPLICATION_DATA_INSTEAD_OF_HANDSHAKE);
  547. *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
  548. return ssl_open_record_error;
  549. }
  550. if (type != SSL3_RT_HANDSHAKE) {
  551. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  552. *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
  553. return ssl_open_record_error;
  554. }
  555. // Append the entire handshake record to the buffer.
  556. if (!tls_append_handshake_data(ssl, body)) {
  557. *out_alert = SSL_AD_INTERNAL_ERROR;
  558. return ssl_open_record_error;
  559. }
  560. return ssl_open_record_success;
  561. }
  562. void ssl3_next_message(SSL *ssl) {
  563. SSLMessage msg;
  564. if (!ssl3_get_message(ssl, &msg) ||
  565. !ssl->s3->hs_buf ||
  566. ssl->s3->hs_buf->length < CBS_len(&msg.raw)) {
  567. assert(0);
  568. return;
  569. }
  570. OPENSSL_memmove(ssl->s3->hs_buf->data,
  571. ssl->s3->hs_buf->data + CBS_len(&msg.raw),
  572. ssl->s3->hs_buf->length - CBS_len(&msg.raw));
  573. ssl->s3->hs_buf->length -= CBS_len(&msg.raw);
  574. ssl->s3->is_v2_hello = false;
  575. ssl->s3->has_message = false;
  576. // Post-handshake messages are rare, so release the buffer after every
  577. // message. During the handshake, |on_handshake_complete| will release it.
  578. if (!SSL_in_init(ssl) && ssl->s3->hs_buf->length == 0) {
  579. ssl->s3->hs_buf.reset();
  580. }
  581. }
  582. BSSL_NAMESPACE_END