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.
 
 
 
 
 
 

456 lines
17 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 <openssl/mem.h>
  114. #include "internal.h"
  115. #include "../crypto/internal.h"
  116. /* kMaxEmptyRecords is the number of consecutive, empty records that will be
  117. * processed. Without this limit an attacker could send empty records at a
  118. * faster rate than we can process and cause record processing to loop
  119. * forever. */
  120. static const uint8_t kMaxEmptyRecords = 32;
  121. /* kMaxWarningAlerts is the number of consecutive warning alerts that will be
  122. * processed. */
  123. static const uint8_t kMaxWarningAlerts = 4;
  124. /* ssl_needs_record_splitting returns one if |ssl|'s current outgoing cipher
  125. * state needs record-splitting and zero otherwise. */
  126. static int ssl_needs_record_splitting(const SSL *ssl) {
  127. return ssl->s3->aead_write_ctx != NULL &&
  128. ssl3_protocol_version(ssl) < TLS1_1_VERSION &&
  129. (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 &&
  130. SSL_CIPHER_is_block_cipher(ssl->s3->aead_write_ctx->cipher);
  131. }
  132. int ssl_record_sequence_update(uint8_t *seq, size_t seq_len) {
  133. size_t i;
  134. for (i = seq_len - 1; i < seq_len; i--) {
  135. ++seq[i];
  136. if (seq[i] != 0) {
  137. return 1;
  138. }
  139. }
  140. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  141. return 0;
  142. }
  143. size_t ssl_record_prefix_len(const SSL *ssl) {
  144. if (SSL_IS_DTLS(ssl)) {
  145. return DTLS1_RT_HEADER_LENGTH +
  146. SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_read_ctx);
  147. } else {
  148. return SSL3_RT_HEADER_LENGTH +
  149. SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_read_ctx);
  150. }
  151. }
  152. size_t ssl_seal_align_prefix_len(const SSL *ssl) {
  153. if (SSL_IS_DTLS(ssl)) {
  154. return DTLS1_RT_HEADER_LENGTH +
  155. SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_write_ctx);
  156. } else {
  157. size_t ret = SSL3_RT_HEADER_LENGTH +
  158. SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_write_ctx);
  159. if (ssl_needs_record_splitting(ssl)) {
  160. ret += SSL3_RT_HEADER_LENGTH;
  161. ret += ssl_cipher_get_record_split_len(ssl->s3->aead_write_ctx->cipher);
  162. }
  163. return ret;
  164. }
  165. }
  166. size_t ssl_max_seal_overhead(const SSL *ssl) {
  167. size_t ret = SSL_AEAD_CTX_max_overhead(ssl->s3->aead_write_ctx);
  168. if (SSL_IS_DTLS(ssl)) {
  169. ret += DTLS1_RT_HEADER_LENGTH;
  170. } else {
  171. ret += SSL3_RT_HEADER_LENGTH;
  172. }
  173. /* TLS 1.3 needs an extra byte for the encrypted record type. */
  174. if (ssl->s3->have_version &&
  175. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  176. ret += 1;
  177. }
  178. if (!SSL_IS_DTLS(ssl) && ssl_needs_record_splitting(ssl)) {
  179. ret *= 2;
  180. }
  181. return ret;
  182. }
  183. enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type, CBS *out,
  184. size_t *out_consumed, uint8_t *out_alert,
  185. uint8_t *in, size_t in_len) {
  186. *out_consumed = 0;
  187. CBS cbs;
  188. CBS_init(&cbs, in, in_len);
  189. /* Decode the record header. */
  190. uint8_t type;
  191. uint16_t version, ciphertext_len;
  192. if (!CBS_get_u8(&cbs, &type) ||
  193. !CBS_get_u16(&cbs, &version) ||
  194. !CBS_get_u16(&cbs, &ciphertext_len)) {
  195. *out_consumed = SSL3_RT_HEADER_LENGTH;
  196. return ssl_open_record_partial;
  197. }
  198. /* Check that the major version in the record matches. As of TLS 1.3, the
  199. * minor version is no longer checked. */
  200. if ((version >> 8) != SSL3_VERSION_MAJOR) {
  201. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
  202. *out_alert = SSL_AD_PROTOCOL_VERSION;
  203. return ssl_open_record_error;
  204. }
  205. /* Check the ciphertext length. */
  206. if (ciphertext_len > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  207. OPENSSL_PUT_ERROR(SSL, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  208. *out_alert = SSL_AD_RECORD_OVERFLOW;
  209. return ssl_open_record_error;
  210. }
  211. /* Extract the body. */
  212. CBS body;
  213. if (!CBS_get_bytes(&cbs, &body, ciphertext_len)) {
  214. *out_consumed = SSL3_RT_HEADER_LENGTH + (size_t)ciphertext_len;
  215. return ssl_open_record_partial;
  216. }
  217. ssl_do_msg_callback(ssl, 0 /* read */, 0, SSL3_RT_HEADER, in,
  218. SSL3_RT_HEADER_LENGTH);
  219. /* Decrypt the body in-place. */
  220. if (!SSL_AEAD_CTX_open(ssl->s3->aead_read_ctx, out, type, version,
  221. ssl->s3->read_sequence, (uint8_t *)CBS_data(&body),
  222. CBS_len(&body))) {
  223. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  224. *out_alert = SSL_AD_BAD_RECORD_MAC;
  225. return ssl_open_record_error;
  226. }
  227. *out_consumed = in_len - CBS_len(&cbs);
  228. if (!ssl_record_sequence_update(ssl->s3->read_sequence, 8)) {
  229. *out_alert = SSL_AD_INTERNAL_ERROR;
  230. return ssl_open_record_error;
  231. }
  232. /* TLS 1.3 hides the record type inside the encrypted data. */
  233. if (ssl->s3->have_version &&
  234. ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
  235. ssl->s3->aead_read_ctx != NULL) {
  236. /* The outer record type is always application_data. */
  237. if (type != SSL3_RT_APPLICATION_DATA) {
  238. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_OUTER_RECORD_TYPE);
  239. *out_alert = SSL_AD_DECODE_ERROR;
  240. return ssl_open_record_error;
  241. }
  242. do {
  243. if (!CBS_get_last_u8(out, &type)) {
  244. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  245. *out_alert = SSL_AD_DECRYPT_ERROR;
  246. return ssl_open_record_error;
  247. }
  248. } while (type == 0);
  249. }
  250. /* Check the plaintext length. */
  251. if (CBS_len(out) > SSL3_RT_MAX_PLAIN_LENGTH) {
  252. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  253. *out_alert = SSL_AD_RECORD_OVERFLOW;
  254. return ssl_open_record_error;
  255. }
  256. /* Limit the number of consecutive empty records. */
  257. if (CBS_len(out) == 0) {
  258. ssl->s3->empty_record_count++;
  259. if (ssl->s3->empty_record_count > kMaxEmptyRecords) {
  260. OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
  261. *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
  262. return ssl_open_record_error;
  263. }
  264. /* Apart from the limit, empty records are returned up to the caller. This
  265. * allows the caller to reject records of the wrong type. */
  266. } else {
  267. ssl->s3->empty_record_count = 0;
  268. }
  269. if (type == SSL3_RT_ALERT) {
  270. return ssl_process_alert(ssl, out_alert, CBS_data(out), CBS_len(out));
  271. }
  272. ssl->s3->warning_alert_count = 0;
  273. *out_type = type;
  274. return ssl_open_record_success;
  275. }
  276. static int do_seal_record(SSL *ssl, uint8_t *out, size_t *out_len,
  277. size_t max_out, uint8_t type, const uint8_t *in,
  278. size_t in_len) {
  279. if (max_out < SSL3_RT_HEADER_LENGTH) {
  280. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  281. return 0;
  282. }
  283. /* Either |in| and |out| don't alias or |in| aligns with the
  284. * ciphertext. |tls_seal_record| forbids aliasing, but TLS 1.3 aliases them
  285. * internally. */
  286. assert(!buffers_alias(in, in_len, out, max_out) ||
  287. in ==
  288. out + SSL3_RT_HEADER_LENGTH +
  289. SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_write_ctx));
  290. out[0] = type;
  291. /* The TLS record-layer version number is meaningless and, starting in
  292. * TLS 1.3, is frozen at TLS 1.0. But for historical reasons, SSL 3.0
  293. * ClientHellos should use SSL 3.0 and pre-TLS-1.3 expects the version
  294. * to change after version negotiation. */
  295. uint16_t wire_version = TLS1_VERSION;
  296. if (ssl->version == SSL3_VERSION ||
  297. (ssl->s3->have_version && ssl3_protocol_version(ssl) < TLS1_3_VERSION)) {
  298. wire_version = ssl->version;
  299. }
  300. out[1] = wire_version >> 8;
  301. out[2] = wire_version & 0xff;
  302. size_t ciphertext_len;
  303. if (!SSL_AEAD_CTX_seal(ssl->s3->aead_write_ctx, out + SSL3_RT_HEADER_LENGTH,
  304. &ciphertext_len, max_out - SSL3_RT_HEADER_LENGTH,
  305. type, wire_version, ssl->s3->write_sequence, in,
  306. in_len) ||
  307. !ssl_record_sequence_update(ssl->s3->write_sequence, 8)) {
  308. return 0;
  309. }
  310. if (ciphertext_len >= 1 << 16) {
  311. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  312. return 0;
  313. }
  314. out[3] = ciphertext_len >> 8;
  315. out[4] = ciphertext_len & 0xff;
  316. *out_len = SSL3_RT_HEADER_LENGTH + ciphertext_len;
  317. ssl_do_msg_callback(ssl, 1 /* write */, 0, SSL3_RT_HEADER, out,
  318. SSL3_RT_HEADER_LENGTH);
  319. return 1;
  320. }
  321. int tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  322. uint8_t type, const uint8_t *in, size_t in_len) {
  323. if (buffers_alias(in, in_len, out, max_out)) {
  324. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  325. return 0;
  326. }
  327. size_t frag_len = 0;
  328. /* TLS 1.3 hides the actual record type inside the encrypted data. */
  329. if (ssl->s3->have_version &&
  330. ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
  331. ssl->s3->aead_write_ctx != NULL) {
  332. size_t padding = SSL3_RT_HEADER_LENGTH + 1;
  333. if (in_len > in_len + padding || max_out < in_len + padding) {
  334. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  335. return 0;
  336. }
  337. memmove(out + SSL3_RT_HEADER_LENGTH, in, in_len);
  338. out[SSL3_RT_HEADER_LENGTH + in_len] = type;
  339. in = out + SSL3_RT_HEADER_LENGTH;
  340. type = SSL3_RT_APPLICATION_DATA;
  341. in_len++;
  342. }
  343. if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 &&
  344. ssl_needs_record_splitting(ssl)) {
  345. if (!do_seal_record(ssl, out, &frag_len, max_out, type, in, 1)) {
  346. return 0;
  347. }
  348. in++;
  349. in_len--;
  350. out += frag_len;
  351. max_out -= frag_len;
  352. #if !defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  353. assert(SSL3_RT_HEADER_LENGTH + ssl_cipher_get_record_split_len(
  354. ssl->s3->aead_write_ctx->cipher) ==
  355. frag_len);
  356. #endif
  357. }
  358. if (!do_seal_record(ssl, out, out_len, max_out, type, in, in_len)) {
  359. return 0;
  360. }
  361. *out_len += frag_len;
  362. return 1;
  363. }
  364. enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
  365. const uint8_t *in, size_t in_len) {
  366. /* Alerts records may not contain fragmented or multiple alerts. */
  367. if (in_len != 2) {
  368. *out_alert = SSL_AD_DECODE_ERROR;
  369. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
  370. return ssl_open_record_error;
  371. }
  372. ssl_do_msg_callback(ssl, 0 /* read */, ssl->version, SSL3_RT_ALERT, in, in_len);
  373. const uint8_t alert_level = in[0];
  374. const uint8_t alert_descr = in[1];
  375. uint16_t alert = (alert_level << 8) | alert_descr;
  376. ssl_do_info_callback(ssl, SSL_CB_READ_ALERT, alert);
  377. if (alert_level == SSL3_AL_WARNING) {
  378. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  379. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  380. return ssl_open_record_close_notify;
  381. }
  382. ssl->s3->warning_alert_count++;
  383. if (ssl->s3->warning_alert_count > kMaxWarningAlerts) {
  384. *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
  385. OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS);
  386. return ssl_open_record_error;
  387. }
  388. return ssl_open_record_discard;
  389. }
  390. if (alert_level == SSL3_AL_FATAL) {
  391. ssl->s3->recv_shutdown = ssl_shutdown_fatal_alert;
  392. SSL_CTX_remove_session(ssl->ctx, ssl->session);
  393. char tmp[16];
  394. OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
  395. BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
  396. ERR_add_error_data(2, "SSL alert number ", tmp);
  397. return ssl_open_record_fatal_alert;
  398. }
  399. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  400. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
  401. return ssl_open_record_error;
  402. }