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.
 
 
 
 
 
 

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