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.
 
 
 
 
 
 

557 lines
19 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. /* kMaxEarlyDataSkipped is the maximum number of rejected early data bytes that
  122. * will be skipped. Without this limit an attacker could send records at a
  123. * faster rate than we can process and cause trial decryption to loop forever.
  124. * This value should be slightly above kMaxEarlyDataAccepted in tls13_server.c,
  125. * which is measured in plaintext. */
  126. static const size_t kMaxEarlyDataSkipped = 16384;
  127. /* kMaxWarningAlerts is the number of consecutive warning alerts that will be
  128. * processed. */
  129. static const uint8_t kMaxWarningAlerts = 4;
  130. /* ssl_needs_record_splitting returns one if |ssl|'s current outgoing cipher
  131. * state needs record-splitting and zero otherwise. */
  132. static int ssl_needs_record_splitting(const SSL *ssl) {
  133. return ssl->s3->aead_write_ctx != NULL &&
  134. ssl3_protocol_version(ssl) < TLS1_1_VERSION &&
  135. (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 &&
  136. SSL_CIPHER_is_block_cipher(ssl->s3->aead_write_ctx->cipher);
  137. }
  138. static int ssl_uses_short_header(const SSL *ssl,
  139. enum evp_aead_direction_t dir) {
  140. if (!ssl->s3->short_header) {
  141. return 0;
  142. }
  143. if (dir == evp_aead_open) {
  144. return ssl->s3->aead_read_ctx != NULL;
  145. }
  146. return ssl->s3->aead_write_ctx != NULL;
  147. }
  148. int ssl_record_sequence_update(uint8_t *seq, size_t seq_len) {
  149. for (size_t i = seq_len - 1; i < seq_len; i--) {
  150. ++seq[i];
  151. if (seq[i] != 0) {
  152. return 1;
  153. }
  154. }
  155. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  156. return 0;
  157. }
  158. size_t ssl_record_prefix_len(const SSL *ssl) {
  159. size_t header_len;
  160. if (SSL_is_dtls(ssl)) {
  161. header_len = DTLS1_RT_HEADER_LENGTH;
  162. } else if (ssl_uses_short_header(ssl, evp_aead_open)) {
  163. header_len = 2;
  164. } else {
  165. header_len = SSL3_RT_HEADER_LENGTH;
  166. }
  167. return header_len + SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_read_ctx);
  168. }
  169. size_t ssl_seal_align_prefix_len(const SSL *ssl) {
  170. if (SSL_is_dtls(ssl)) {
  171. return DTLS1_RT_HEADER_LENGTH +
  172. SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_write_ctx);
  173. }
  174. size_t header_len;
  175. if (ssl_uses_short_header(ssl, evp_aead_seal)) {
  176. header_len = 2;
  177. } else {
  178. header_len = SSL3_RT_HEADER_LENGTH;
  179. }
  180. size_t ret =
  181. header_len + SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_write_ctx);
  182. if (ssl_needs_record_splitting(ssl)) {
  183. ret += header_len;
  184. ret += ssl_cipher_get_record_split_len(ssl->s3->aead_write_ctx->cipher);
  185. }
  186. return ret;
  187. }
  188. size_t SSL_max_seal_overhead(const SSL *ssl) {
  189. if (SSL_is_dtls(ssl)) {
  190. return dtls_max_seal_overhead(ssl, dtls1_use_current_epoch);
  191. }
  192. size_t ret =
  193. ssl_uses_short_header(ssl, evp_aead_seal) ? 2 : SSL3_RT_HEADER_LENGTH;
  194. ret += SSL_AEAD_CTX_max_overhead(ssl->s3->aead_write_ctx);
  195. /* TLS 1.3 needs an extra byte for the encrypted record type. */
  196. if (ssl->s3->have_version &&
  197. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  198. ret += 1;
  199. }
  200. if (ssl_needs_record_splitting(ssl)) {
  201. ret *= 2;
  202. }
  203. return ret;
  204. }
  205. enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type, CBS *out,
  206. size_t *out_consumed, uint8_t *out_alert,
  207. uint8_t *in, size_t in_len) {
  208. *out_consumed = 0;
  209. CBS cbs;
  210. CBS_init(&cbs, in, in_len);
  211. /* Decode the record header. */
  212. uint8_t type;
  213. uint16_t version, ciphertext_len;
  214. size_t header_len;
  215. if (ssl_uses_short_header(ssl, evp_aead_open)) {
  216. if (!CBS_get_u16(&cbs, &ciphertext_len)) {
  217. *out_consumed = 2;
  218. return ssl_open_record_partial;
  219. }
  220. if ((ciphertext_len & 0x8000) == 0) {
  221. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  222. *out_alert = SSL_AD_DECODE_ERROR;
  223. return ssl_open_record_error;
  224. }
  225. ciphertext_len &= 0x7fff;
  226. type = SSL3_RT_APPLICATION_DATA;
  227. version = TLS1_VERSION;
  228. header_len = 2;
  229. } else {
  230. if (!CBS_get_u8(&cbs, &type) ||
  231. !CBS_get_u16(&cbs, &version) ||
  232. !CBS_get_u16(&cbs, &ciphertext_len)) {
  233. *out_consumed = SSL3_RT_HEADER_LENGTH;
  234. return ssl_open_record_partial;
  235. }
  236. header_len = SSL3_RT_HEADER_LENGTH;
  237. }
  238. int version_ok;
  239. if (ssl->s3->aead_read_ctx == NULL) {
  240. /* Only check the first byte. Enforcing beyond that can prevent decoding
  241. * version negotiation failure alerts. */
  242. version_ok = (version >> 8) == SSL3_VERSION_MAJOR;
  243. } else if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  244. /* Earlier versions of TLS switch the record version. */
  245. version_ok = version == ssl->version;
  246. } else {
  247. /* Starting TLS 1.3, the version field is frozen at {3, 1}. */
  248. version_ok = version == TLS1_VERSION;
  249. }
  250. if (!version_ok) {
  251. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
  252. *out_alert = SSL_AD_PROTOCOL_VERSION;
  253. return ssl_open_record_error;
  254. }
  255. /* Check the ciphertext length. */
  256. if (ciphertext_len > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
  257. OPENSSL_PUT_ERROR(SSL, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
  258. *out_alert = SSL_AD_RECORD_OVERFLOW;
  259. return ssl_open_record_error;
  260. }
  261. /* Extract the body. */
  262. CBS body;
  263. if (!CBS_get_bytes(&cbs, &body, ciphertext_len)) {
  264. *out_consumed = header_len + (size_t)ciphertext_len;
  265. return ssl_open_record_partial;
  266. }
  267. ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HEADER, in, header_len);
  268. *out_consumed = in_len - CBS_len(&cbs);
  269. /* Skip early data received when expecting a second ClientHello if we rejected
  270. * 0RTT. */
  271. if (ssl->s3->skip_early_data &&
  272. ssl->s3->aead_read_ctx == NULL &&
  273. type == SSL3_RT_APPLICATION_DATA) {
  274. goto skipped_data;
  275. }
  276. /* Decrypt the body in-place. */
  277. if (!SSL_AEAD_CTX_open(ssl->s3->aead_read_ctx, out, type, version,
  278. ssl->s3->read_sequence, (uint8_t *)CBS_data(&body),
  279. CBS_len(&body))) {
  280. if (ssl->s3->skip_early_data &&
  281. ssl->s3->aead_read_ctx != NULL) {
  282. ERR_clear_error();
  283. goto skipped_data;
  284. }
  285. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  286. *out_alert = SSL_AD_BAD_RECORD_MAC;
  287. return ssl_open_record_error;
  288. }
  289. ssl->s3->skip_early_data = 0;
  290. if (!ssl_record_sequence_update(ssl->s3->read_sequence, 8)) {
  291. *out_alert = SSL_AD_INTERNAL_ERROR;
  292. return ssl_open_record_error;
  293. }
  294. /* TLS 1.3 hides the record type inside the encrypted data. */
  295. if (ssl->s3->have_version &&
  296. ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
  297. ssl->s3->aead_read_ctx != NULL) {
  298. /* The outer record type is always application_data. */
  299. if (type != SSL3_RT_APPLICATION_DATA) {
  300. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_OUTER_RECORD_TYPE);
  301. *out_alert = SSL_AD_DECODE_ERROR;
  302. return ssl_open_record_error;
  303. }
  304. do {
  305. if (!CBS_get_last_u8(out, &type)) {
  306. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
  307. *out_alert = SSL_AD_DECRYPT_ERROR;
  308. return ssl_open_record_error;
  309. }
  310. } while (type == 0);
  311. }
  312. /* Check the plaintext length. */
  313. if (CBS_len(out) > SSL3_RT_MAX_PLAIN_LENGTH) {
  314. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  315. *out_alert = SSL_AD_RECORD_OVERFLOW;
  316. return ssl_open_record_error;
  317. }
  318. /* Limit the number of consecutive empty records. */
  319. if (CBS_len(out) == 0) {
  320. ssl->s3->empty_record_count++;
  321. if (ssl->s3->empty_record_count > kMaxEmptyRecords) {
  322. OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
  323. *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
  324. return ssl_open_record_error;
  325. }
  326. /* Apart from the limit, empty records are returned up to the caller. This
  327. * allows the caller to reject records of the wrong type. */
  328. } else {
  329. ssl->s3->empty_record_count = 0;
  330. }
  331. if (type == SSL3_RT_ALERT) {
  332. return ssl_process_alert(ssl, out_alert, CBS_data(out), CBS_len(out));
  333. }
  334. ssl->s3->warning_alert_count = 0;
  335. *out_type = type;
  336. return ssl_open_record_success;
  337. skipped_data:
  338. ssl->s3->early_data_skipped += *out_consumed;
  339. if (ssl->s3->early_data_skipped < *out_consumed) {
  340. ssl->s3->early_data_skipped = kMaxEarlyDataSkipped + 1;
  341. }
  342. if (ssl->s3->early_data_skipped > kMaxEarlyDataSkipped) {
  343. OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MUCH_SKIPPED_EARLY_DATA);
  344. *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
  345. return ssl_open_record_error;
  346. }
  347. return ssl_open_record_discard;
  348. }
  349. static int do_seal_record(SSL *ssl, uint8_t *out, size_t *out_len,
  350. size_t max_out, uint8_t type, const uint8_t *in,
  351. size_t in_len) {
  352. assert(!buffers_alias(in, in_len, out, max_out));
  353. const int short_header = ssl_uses_short_header(ssl, evp_aead_seal);
  354. size_t header_len = short_header ? 2 : SSL3_RT_HEADER_LENGTH;
  355. /* TLS 1.3 hides the actual record type inside the encrypted data. */
  356. if (ssl->s3->have_version &&
  357. ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
  358. ssl->s3->aead_write_ctx != NULL) {
  359. if (in_len > in_len + header_len + 1 || max_out < in_len + header_len + 1) {
  360. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  361. return 0;
  362. }
  363. OPENSSL_memcpy(out + header_len, in, in_len);
  364. out[header_len + in_len] = type;
  365. in = out + header_len;
  366. type = SSL3_RT_APPLICATION_DATA;
  367. in_len++;
  368. }
  369. if (max_out < header_len) {
  370. OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
  371. return 0;
  372. }
  373. /* The TLS record-layer version number is meaningless and, starting in
  374. * TLS 1.3, is frozen at TLS 1.0. But for historical reasons, SSL 3.0
  375. * ClientHellos should use SSL 3.0 and pre-TLS-1.3 expects the version
  376. * to change after version negotiation. */
  377. uint16_t wire_version = TLS1_VERSION;
  378. if (ssl->version == SSL3_VERSION ||
  379. (ssl->s3->have_version && ssl3_protocol_version(ssl) < TLS1_3_VERSION)) {
  380. wire_version = ssl->version;
  381. }
  382. /* Write the non-length portions of the header. */
  383. if (!short_header) {
  384. out[0] = type;
  385. out[1] = wire_version >> 8;
  386. out[2] = wire_version & 0xff;
  387. out += 3;
  388. max_out -= 3;
  389. }
  390. /* Write the ciphertext, leaving two bytes for the length. */
  391. size_t ciphertext_len;
  392. if (!SSL_AEAD_CTX_seal(ssl->s3->aead_write_ctx, out + 2, &ciphertext_len,
  393. max_out - 2, type, wire_version,
  394. ssl->s3->write_sequence, in, in_len) ||
  395. !ssl_record_sequence_update(ssl->s3->write_sequence, 8)) {
  396. return 0;
  397. }
  398. /* Fill in the length. */
  399. if (ciphertext_len >= 1 << 15) {
  400. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  401. return 0;
  402. }
  403. out[0] = ciphertext_len >> 8;
  404. out[1] = ciphertext_len & 0xff;
  405. if (short_header) {
  406. out[0] |= 0x80;
  407. }
  408. *out_len = header_len + ciphertext_len;
  409. ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HEADER, out, header_len);
  410. return 1;
  411. }
  412. int tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  413. uint8_t type, const uint8_t *in, size_t in_len) {
  414. if (buffers_alias(in, in_len, out, max_out)) {
  415. OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
  416. return 0;
  417. }
  418. size_t frag_len = 0;
  419. if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 &&
  420. ssl_needs_record_splitting(ssl)) {
  421. if (!do_seal_record(ssl, out, &frag_len, max_out, type, in, 1)) {
  422. return 0;
  423. }
  424. in++;
  425. in_len--;
  426. out += frag_len;
  427. max_out -= frag_len;
  428. assert(!ssl_uses_short_header(ssl, evp_aead_seal));
  429. #if !defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  430. assert(SSL3_RT_HEADER_LENGTH + ssl_cipher_get_record_split_len(
  431. ssl->s3->aead_write_ctx->cipher) ==
  432. frag_len);
  433. #endif
  434. }
  435. if (!do_seal_record(ssl, out, out_len, max_out, type, in, in_len)) {
  436. return 0;
  437. }
  438. *out_len += frag_len;
  439. return 1;
  440. }
  441. enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
  442. const uint8_t *in, size_t in_len) {
  443. /* Alerts records may not contain fragmented or multiple alerts. */
  444. if (in_len != 2) {
  445. *out_alert = SSL_AD_DECODE_ERROR;
  446. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
  447. return ssl_open_record_error;
  448. }
  449. ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_ALERT, in, in_len);
  450. const uint8_t alert_level = in[0];
  451. const uint8_t alert_descr = in[1];
  452. uint16_t alert = (alert_level << 8) | alert_descr;
  453. ssl_do_info_callback(ssl, SSL_CB_READ_ALERT, alert);
  454. if (alert_level == SSL3_AL_WARNING) {
  455. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  456. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  457. return ssl_open_record_close_notify;
  458. }
  459. /* Warning alerts do not exist in TLS 1.3. */
  460. if (ssl->s3->have_version &&
  461. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  462. *out_alert = SSL_AD_DECODE_ERROR;
  463. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
  464. return ssl_open_record_error;
  465. }
  466. ssl->s3->warning_alert_count++;
  467. if (ssl->s3->warning_alert_count > kMaxWarningAlerts) {
  468. *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
  469. OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS);
  470. return ssl_open_record_error;
  471. }
  472. return ssl_open_record_discard;
  473. }
  474. if (alert_level == SSL3_AL_FATAL) {
  475. ssl->s3->recv_shutdown = ssl_shutdown_fatal_alert;
  476. char tmp[16];
  477. OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
  478. BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
  479. ERR_add_error_data(2, "SSL alert number ", tmp);
  480. return ssl_open_record_fatal_alert;
  481. }
  482. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  483. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
  484. return ssl_open_record_error;
  485. }