Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

382 linhas
14 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. #include <openssl/ssl.h>
  109. #include <assert.h>
  110. #include <string.h>
  111. #include <openssl/bytestring.h>
  112. #include <openssl/err.h>
  113. #include "internal.h"
  114. /* kMaxEmptyRecords is the number of consecutive, empty records that will be
  115. * processed. Without this limit an attacker could send empty records at a
  116. * faster rate than we can process and cause record processing to loop
  117. * forever. */
  118. static const uint8_t kMaxEmptyRecords = 32;
  119. /* ssl_needs_record_splitting returns one if |ssl|'s current outgoing cipher
  120. * state needs record-splitting and zero otherwise. */
  121. static int ssl_needs_record_splitting(const SSL *ssl) {
  122. return ssl->s3->aead_write_ctx != NULL &&
  123. ssl3_protocol_version(ssl) < TLS1_1_VERSION &&
  124. (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 &&
  125. SSL_CIPHER_is_block_cipher(ssl->s3->aead_write_ctx->cipher);
  126. }
  127. int ssl_record_sequence_update(uint8_t *seq, size_t seq_len) {
  128. size_t i;
  129. for (i = seq_len - 1; i < seq_len; i--) {
  130. ++seq[i];
  131. if (seq[i] != 0) {
  132. return 1;
  133. }
  134. }
  135. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  136. return 0;
  137. }
  138. size_t ssl_record_prefix_len(const SSL *ssl) {
  139. if (SSL_IS_DTLS(ssl)) {
  140. return DTLS1_RT_HEADER_LENGTH +
  141. SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_read_ctx);
  142. } else {
  143. return SSL3_RT_HEADER_LENGTH +
  144. SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_read_ctx);
  145. }
  146. }
  147. size_t ssl_seal_prefix_len(const SSL *ssl) {
  148. if (SSL_IS_DTLS(ssl)) {
  149. return DTLS1_RT_HEADER_LENGTH +
  150. SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_write_ctx);
  151. } else {
  152. size_t ret = SSL3_RT_HEADER_LENGTH +
  153. SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_write_ctx);
  154. if (ssl_needs_record_splitting(ssl)) {
  155. ret += SSL3_RT_HEADER_LENGTH;
  156. ret += ssl_cipher_get_record_split_len(ssl->s3->aead_write_ctx->cipher);
  157. }
  158. return ret;
  159. }
  160. }
  161. size_t ssl_max_seal_overhead(const SSL *ssl) {
  162. if (SSL_IS_DTLS(ssl)) {
  163. return DTLS1_RT_HEADER_LENGTH +
  164. SSL_AEAD_CTX_max_overhead(ssl->s3->aead_write_ctx);
  165. } else {
  166. size_t ret = SSL3_RT_HEADER_LENGTH +
  167. SSL_AEAD_CTX_max_overhead(ssl->s3->aead_write_ctx);
  168. if (ssl_needs_record_splitting(ssl)) {
  169. ret *= 2;
  170. }
  171. return ret;
  172. }
  173. }
  174. enum ssl_open_record_t tls_open_record(
  175. SSL *ssl, uint8_t *out_type, uint8_t *out, size_t *out_len,
  176. size_t *out_consumed, uint8_t *out_alert, size_t max_out, const uint8_t *in,
  177. size_t in_len) {
  178. CBS cbs;
  179. CBS_init(&cbs, in, in_len);
  180. /* Decode the record header. */
  181. uint8_t type;
  182. uint16_t version, ciphertext_len;
  183. if (!CBS_get_u8(&cbs, &type) ||
  184. !CBS_get_u16(&cbs, &version) ||
  185. !CBS_get_u16(&cbs, &ciphertext_len)) {
  186. *out_consumed = SSL3_RT_HEADER_LENGTH;
  187. return ssl_open_record_partial;
  188. }
  189. /* Check the version. */
  190. if ((ssl->s3->have_version && version != ssl->version) ||
  191. (version >> 8) != SSL3_VERSION_MAJOR) {
  192. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
  193. *out_alert = SSL_AD_PROTOCOL_VERSION;
  194. return ssl_open_record_error;
  195. }
  196. /* Check the ciphertext length. */
  197. if (ciphertext_len > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  198. OPENSSL_PUT_ERROR(SSL, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  199. *out_alert = SSL_AD_RECORD_OVERFLOW;
  200. return ssl_open_record_error;
  201. }
  202. /* Extract the body. */
  203. CBS body;
  204. if (!CBS_get_bytes(&cbs, &body, ciphertext_len)) {
  205. *out_consumed = SSL3_RT_HEADER_LENGTH + (size_t)ciphertext_len;
  206. return ssl_open_record_partial;
  207. }
  208. if (ssl->msg_callback != NULL) {
  209. ssl->msg_callback(0 /* read */, 0, SSL3_RT_HEADER, in,
  210. SSL3_RT_HEADER_LENGTH, ssl, ssl->msg_callback_arg);
  211. }
  212. /* Decrypt the body. */
  213. size_t plaintext_len;
  214. if (!SSL_AEAD_CTX_open(ssl->s3->aead_read_ctx, out, &plaintext_len, max_out,
  215. type, version, ssl->s3->read_sequence, CBS_data(&body),
  216. CBS_len(&body))) {
  217. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  218. *out_alert = SSL_AD_BAD_RECORD_MAC;
  219. return ssl_open_record_error;
  220. }
  221. if (!ssl_record_sequence_update(ssl->s3->read_sequence, 8)) {
  222. *out_alert = SSL_AD_INTERNAL_ERROR;
  223. return ssl_open_record_error;
  224. }
  225. /* Check the plaintext length. */
  226. if (plaintext_len > SSL3_RT_MAX_PLAIN_LENGTH) {
  227. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  228. *out_alert = SSL_AD_RECORD_OVERFLOW;
  229. return ssl_open_record_error;
  230. }
  231. /* Limit the number of consecutive empty records. */
  232. if (plaintext_len == 0) {
  233. ssl->s3->empty_record_count++;
  234. if (ssl->s3->empty_record_count > kMaxEmptyRecords) {
  235. OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
  236. *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
  237. return ssl_open_record_error;
  238. }
  239. /* Apart from the limit, empty records are returned up to the caller. This
  240. * allows the caller to reject records of the wrong type. */
  241. } else {
  242. ssl->s3->empty_record_count = 0;
  243. }
  244. *out_type = type;
  245. *out_len = plaintext_len;
  246. *out_consumed = in_len - CBS_len(&cbs);
  247. return ssl_open_record_success;
  248. }
  249. static int do_seal_record(SSL *ssl, uint8_t *out, size_t *out_len,
  250. size_t max_out, uint8_t type, const uint8_t *in,
  251. size_t in_len) {
  252. if (max_out < SSL3_RT_HEADER_LENGTH) {
  253. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  254. return 0;
  255. }
  256. /* Check the record header does not alias any part of the input.
  257. * |SSL_AEAD_CTX_seal| will internally enforce other aliasing requirements. */
  258. if (in < out + SSL3_RT_HEADER_LENGTH && out < in + in_len) {
  259. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  260. return 0;
  261. }
  262. out[0] = type;
  263. /* Some servers hang if initial ClientHello is larger than 256 bytes and
  264. * record version number > TLS 1.0. */
  265. uint16_t wire_version = ssl->version;
  266. if (!ssl->s3->have_version && ssl->version > SSL3_VERSION) {
  267. wire_version = TLS1_VERSION;
  268. }
  269. out[1] = wire_version >> 8;
  270. out[2] = wire_version & 0xff;
  271. size_t ciphertext_len;
  272. if (!SSL_AEAD_CTX_seal(ssl->s3->aead_write_ctx, out + SSL3_RT_HEADER_LENGTH,
  273. &ciphertext_len, max_out - SSL3_RT_HEADER_LENGTH,
  274. type, wire_version, ssl->s3->write_sequence, in,
  275. in_len) ||
  276. !ssl_record_sequence_update(ssl->s3->write_sequence, 8)) {
  277. return 0;
  278. }
  279. if (ciphertext_len >= 1 << 16) {
  280. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  281. return 0;
  282. }
  283. out[3] = ciphertext_len >> 8;
  284. out[4] = ciphertext_len & 0xff;
  285. *out_len = SSL3_RT_HEADER_LENGTH + ciphertext_len;
  286. if (ssl->msg_callback) {
  287. ssl->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out,
  288. SSL3_RT_HEADER_LENGTH, ssl, ssl->msg_callback_arg);
  289. }
  290. return 1;
  291. }
  292. int tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  293. uint8_t type, const uint8_t *in, size_t in_len) {
  294. size_t frag_len = 0;
  295. if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 &&
  296. ssl_needs_record_splitting(ssl)) {
  297. /* |do_seal_record| will notice if it clobbers |in[0]|, but not if it
  298. * aliases the rest of |in|. */
  299. if (in + 1 <= out && out < in + in_len) {
  300. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  301. return 0;
  302. }
  303. /* Ensure |do_seal_record| does not write beyond |in[0]|. */
  304. size_t frag_max_out = max_out;
  305. if (out <= in + 1 && in + 1 < out + frag_max_out) {
  306. frag_max_out = (size_t)(in + 1 - out);
  307. }
  308. if (!do_seal_record(ssl, out, &frag_len, frag_max_out, type, in, 1)) {
  309. return 0;
  310. }
  311. in++;
  312. in_len--;
  313. out += frag_len;
  314. max_out -= frag_len;
  315. #if !defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  316. assert(SSL3_RT_HEADER_LENGTH + ssl_cipher_get_record_split_len(
  317. ssl->s3->aead_write_ctx->cipher) ==
  318. frag_len);
  319. #endif
  320. }
  321. if (!do_seal_record(ssl, out, out_len, max_out, type, in, in_len)) {
  322. return 0;
  323. }
  324. *out_len += frag_len;
  325. return 1;
  326. }
  327. void ssl_set_read_state(SSL *ssl, SSL_AEAD_CTX *aead_ctx) {
  328. if (SSL_IS_DTLS(ssl)) {
  329. ssl->d1->r_epoch++;
  330. memset(&ssl->d1->bitmap, 0, sizeof(ssl->d1->bitmap));
  331. }
  332. memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence));
  333. SSL_AEAD_CTX_free(ssl->s3->aead_read_ctx);
  334. ssl->s3->aead_read_ctx = aead_ctx;
  335. }
  336. void ssl_set_write_state(SSL *ssl, SSL_AEAD_CTX *aead_ctx) {
  337. if (SSL_IS_DTLS(ssl)) {
  338. ssl->d1->w_epoch++;
  339. memcpy(ssl->d1->last_write_sequence, ssl->s3->write_sequence,
  340. sizeof(ssl->s3->write_sequence));
  341. }
  342. memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence));
  343. SSL_AEAD_CTX_free(ssl->s3->aead_write_ctx);
  344. ssl->s3->aead_write_ctx = aead_ctx;
  345. }