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.
 
 
 
 
 
 

691 lines
22 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 <limits.h>
  111. #include <stdio.h>
  112. #include <string.h>
  113. #include <openssl/buf.h>
  114. #include <openssl/err.h>
  115. #include <openssl/evp.h>
  116. #include <openssl/mem.h>
  117. #include <openssl/rand.h>
  118. #include "internal.h"
  119. static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len);
  120. /* kMaxWarningAlerts is the number of consecutive warning alerts that will be
  121. * processed. */
  122. static const uint8_t kMaxWarningAlerts = 4;
  123. /* ssl3_get_record reads a new input record. On success, it places it in
  124. * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
  125. * more data is needed. */
  126. static int ssl3_get_record(SSL *ssl) {
  127. int ret;
  128. again:
  129. /* Ensure the buffer is large enough to decrypt in-place. */
  130. ret = ssl_read_buffer_extend_to(ssl, ssl_record_prefix_len(ssl));
  131. if (ret <= 0) {
  132. return ret;
  133. }
  134. assert(ssl_read_buffer_len(ssl) >= ssl_record_prefix_len(ssl));
  135. uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
  136. size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
  137. uint8_t type, alert;
  138. size_t len, consumed;
  139. switch (tls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
  140. ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
  141. case ssl_open_record_success:
  142. ssl_read_buffer_consume(ssl, consumed);
  143. if (len > 0xffff) {
  144. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  145. return -1;
  146. }
  147. SSL3_RECORD *rr = &ssl->s3->rrec;
  148. rr->type = type;
  149. rr->length = (uint16_t)len;
  150. rr->data = out;
  151. return 1;
  152. case ssl_open_record_partial:
  153. ret = ssl_read_buffer_extend_to(ssl, consumed);
  154. if (ret <= 0) {
  155. return ret;
  156. }
  157. goto again;
  158. case ssl_open_record_discard:
  159. ssl_read_buffer_consume(ssl, consumed);
  160. goto again;
  161. case ssl_open_record_error:
  162. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  163. return -1;
  164. }
  165. assert(0);
  166. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  167. return -1;
  168. }
  169. int ssl3_write_app_data(SSL *ssl, const void *buf, int len) {
  170. return ssl3_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len);
  171. }
  172. /* Call this to write data in records of type |type|. It will return <= 0 if
  173. * not all data has been sent or non-blocking IO. */
  174. int ssl3_write_bytes(SSL *ssl, int type, const void *buf_, int len) {
  175. const uint8_t *buf = buf_;
  176. unsigned tot, n, nw;
  177. ssl->rwstate = SSL_NOTHING;
  178. assert(ssl->s3->wnum <= INT_MAX);
  179. tot = ssl->s3->wnum;
  180. ssl->s3->wnum = 0;
  181. if (!ssl->in_handshake && SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
  182. int ret = ssl->handshake_func(ssl);
  183. if (ret < 0) {
  184. return ret;
  185. }
  186. if (ret == 0) {
  187. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  188. return -1;
  189. }
  190. }
  191. /* Ensure that if we end up with a smaller value of data to write out than
  192. * the the original len from a write which didn't complete for non-blocking
  193. * I/O and also somehow ended up avoiding the check for this in
  194. * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
  195. * end up with (len-tot) as a large number that will then promptly send
  196. * beyond the end of the users buffer ... so we trap and report the error in
  197. * a way the user will notice. */
  198. if (len < 0 || (size_t)len < tot) {
  199. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
  200. return -1;
  201. }
  202. n = (len - tot);
  203. for (;;) {
  204. /* max contains the maximum number of bytes that we can put into a
  205. * record. */
  206. unsigned max = ssl->max_send_fragment;
  207. if (n > max) {
  208. nw = max;
  209. } else {
  210. nw = n;
  211. }
  212. int ret = do_ssl3_write(ssl, type, &buf[tot], nw);
  213. if (ret <= 0) {
  214. ssl->s3->wnum = tot;
  215. return ret;
  216. }
  217. if (ret == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
  218. (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
  219. return tot + ret;
  220. }
  221. n -= ret;
  222. tot += ret;
  223. }
  224. }
  225. static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *buf,
  226. unsigned int len) {
  227. if (ssl->s3->wpend_tot > (int)len ||
  228. (ssl->s3->wpend_buf != buf &&
  229. !(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
  230. ssl->s3->wpend_type != type) {
  231. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
  232. return -1;
  233. }
  234. int ret = ssl_write_buffer_flush(ssl);
  235. if (ret <= 0) {
  236. return ret;
  237. }
  238. return ssl->s3->wpend_ret;
  239. }
  240. /* do_ssl3_write writes an SSL record of the given type. */
  241. static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len) {
  242. /* If there is still data from the previous record, flush it. */
  243. if (ssl_write_buffer_is_pending(ssl)) {
  244. return ssl3_write_pending(ssl, type, buf, len);
  245. }
  246. /* If we have an alert to send, lets send it */
  247. if (ssl->s3->alert_dispatch) {
  248. int ret = ssl->method->ssl_dispatch_alert(ssl);
  249. if (ret <= 0) {
  250. return ret;
  251. }
  252. /* if it went, fall through and send more stuff */
  253. }
  254. if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
  255. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  256. return -1;
  257. }
  258. if (len == 0) {
  259. return 0;
  260. }
  261. size_t max_out = len + ssl_max_seal_overhead(ssl);
  262. if (max_out < len) {
  263. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  264. return -1;
  265. }
  266. uint8_t *out;
  267. size_t ciphertext_len;
  268. if (!ssl_write_buffer_init(ssl, &out, max_out) ||
  269. !tls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len)) {
  270. return -1;
  271. }
  272. ssl_write_buffer_set_len(ssl, ciphertext_len);
  273. /* memorize arguments so that ssl3_write_pending can detect bad write retries
  274. * later */
  275. ssl->s3->wpend_tot = len;
  276. ssl->s3->wpend_buf = buf;
  277. ssl->s3->wpend_type = type;
  278. ssl->s3->wpend_ret = len;
  279. /* we now just need to write the buffer */
  280. return ssl3_write_pending(ssl, type, buf, len);
  281. }
  282. int ssl3_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
  283. return ssl3_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
  284. }
  285. int ssl3_read_change_cipher_spec(SSL *ssl) {
  286. uint8_t byte;
  287. int ret = ssl3_read_bytes(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1 /* len */,
  288. 0 /* no peek */);
  289. if (ret <= 0) {
  290. return ret;
  291. }
  292. assert(ret == 1);
  293. if (ssl->s3->rrec.length != 0 || byte != SSL3_MT_CCS) {
  294. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
  295. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  296. return -1;
  297. }
  298. if (ssl->msg_callback != NULL) {
  299. ssl->msg_callback(0, ssl->version, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1,
  300. ssl, ssl->msg_callback_arg);
  301. }
  302. return 1;
  303. }
  304. void ssl3_read_close_notify(SSL *ssl) {
  305. ssl3_read_bytes(ssl, 0, NULL, 0, 0);
  306. }
  307. static int ssl3_can_renegotiate(SSL *ssl) {
  308. switch (ssl->renegotiate_mode) {
  309. case ssl_renegotiate_never:
  310. return 0;
  311. case ssl_renegotiate_once:
  312. return ssl->s3->total_renegotiations == 0;
  313. case ssl_renegotiate_freely:
  314. return 1;
  315. case ssl_renegotiate_ignore:
  316. return 1;
  317. }
  318. assert(0);
  319. return 0;
  320. }
  321. /* Return up to 'len' payload bytes received in 'type' records.
  322. * 'type' is one of the following:
  323. *
  324. * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
  325. * - SSL3_RT_CHANGE_CIPHER_SPEC (when ssl3_read_change_cipher_spec calls us)
  326. * - SSL3_RT_APPLICATION_DATA (when ssl3_read_app_data calls us)
  327. * - 0 (during a shutdown, no data has to be returned)
  328. *
  329. * If we don't have stored data to work from, read a SSL/TLS record first
  330. * (possibly multiple records if we still don't have anything to return).
  331. *
  332. * This function must handle any surprises the peer may have for us, such as
  333. * Alert records (e.g. close_notify) or renegotiation requests. */
  334. int ssl3_read_bytes(SSL *ssl, int type, uint8_t *buf, int len, int peek) {
  335. int al, i, ret;
  336. unsigned int n;
  337. SSL3_RECORD *rr;
  338. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  339. if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE &&
  340. type != SSL3_RT_CHANGE_CIPHER_SPEC) ||
  341. (peek && type != SSL3_RT_APPLICATION_DATA)) {
  342. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  343. return -1;
  344. }
  345. /* This may require multiple iterations. False Start will cause
  346. * |ssl->handshake_func| to signal success one step early, but the handshake
  347. * must be completely finished before other modes are accepted.
  348. *
  349. * TODO(davidben): Move this check up to a higher level. */
  350. while (!ssl->in_handshake && SSL_in_init(ssl)) {
  351. assert(type == SSL3_RT_APPLICATION_DATA);
  352. i = ssl->handshake_func(ssl);
  353. if (i < 0) {
  354. return i;
  355. }
  356. if (i == 0) {
  357. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  358. return -1;
  359. }
  360. }
  361. start:
  362. ssl->rwstate = SSL_NOTHING;
  363. /* ssl->s3->rrec.type - is the type of record
  364. * ssl->s3->rrec.data - data
  365. * ssl->s3->rrec.off - offset into 'data' for next read
  366. * ssl->s3->rrec.length - number of bytes. */
  367. rr = &ssl->s3->rrec;
  368. /* get new packet if necessary */
  369. if (rr->length == 0) {
  370. ret = ssl3_get_record(ssl);
  371. if (ret <= 0) {
  372. return ret;
  373. }
  374. }
  375. /* we now have a packet which can be read and processed */
  376. /* If the other end has shut down, throw anything we read away (even in
  377. * 'peek' mode) */
  378. if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
  379. rr->length = 0;
  380. ssl->rwstate = SSL_NOTHING;
  381. return 0;
  382. }
  383. if (type != 0 && type == rr->type) {
  384. ssl->s3->warning_alert_count = 0;
  385. /* Make sure that we are not getting application data when we are doing a
  386. * handshake for the first time. */
  387. if (SSL_in_init(ssl) && type == SSL3_RT_APPLICATION_DATA &&
  388. ssl->s3->aead_read_ctx == NULL) {
  389. /* TODO(davidben): Is this check redundant with the handshake_func
  390. * check? */
  391. al = SSL_AD_UNEXPECTED_MESSAGE;
  392. OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
  393. goto f_err;
  394. }
  395. /* Discard empty records. */
  396. if (rr->length == 0) {
  397. goto start;
  398. }
  399. if (len <= 0) {
  400. return len;
  401. }
  402. if ((unsigned int)len > rr->length) {
  403. n = rr->length;
  404. } else {
  405. n = (unsigned int)len;
  406. }
  407. memcpy(buf, rr->data, n);
  408. if (!peek) {
  409. rr->length -= n;
  410. rr->data += n;
  411. if (rr->length == 0) {
  412. /* The record has been consumed, so we may now clear the buffer. */
  413. ssl_read_buffer_discard(ssl);
  414. }
  415. }
  416. return n;
  417. }
  418. /* Process unexpected records. */
  419. if (type == SSL3_RT_APPLICATION_DATA && rr->type == SSL3_RT_HANDSHAKE) {
  420. /* If peer renegotiations are disabled, all out-of-order handshake records
  421. * are fatal. Renegotiations as a server are never supported. */
  422. if (ssl->server || !ssl3_can_renegotiate(ssl)) {
  423. al = SSL_AD_NO_RENEGOTIATION;
  424. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  425. goto f_err;
  426. }
  427. /* This must be a HelloRequest, possibly fragmented over multiple records.
  428. * Consume data from the handshake protocol until it is complete. */
  429. static const uint8_t kHelloRequest[] = {SSL3_MT_HELLO_REQUEST, 0, 0, 0};
  430. while (ssl->s3->hello_request_len < sizeof(kHelloRequest)) {
  431. if (rr->length == 0) {
  432. /* Get a new record. */
  433. goto start;
  434. }
  435. if (rr->data[0] != kHelloRequest[ssl->s3->hello_request_len]) {
  436. al = SSL_AD_DECODE_ERROR;
  437. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
  438. goto f_err;
  439. }
  440. rr->data++;
  441. rr->length--;
  442. ssl->s3->hello_request_len++;
  443. }
  444. ssl->s3->hello_request_len = 0;
  445. if (ssl->msg_callback) {
  446. ssl->msg_callback(0, ssl->version, SSL3_RT_HANDSHAKE, kHelloRequest,
  447. sizeof(kHelloRequest), ssl, ssl->msg_callback_arg);
  448. }
  449. if (!SSL_is_init_finished(ssl) || !ssl->s3->initial_handshake_complete) {
  450. /* This cannot happen. If a handshake is in progress, |type| must be
  451. * |SSL3_RT_HANDSHAKE|. */
  452. assert(0);
  453. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  454. goto err;
  455. }
  456. if (ssl->renegotiate_mode == ssl_renegotiate_ignore) {
  457. goto start;
  458. }
  459. /* Renegotiation is only supported at quiescent points in the application
  460. * protocol, namely in HTTPS, just before reading the HTTP response. Require
  461. * the record-layer be idle and avoid complexities of sending a handshake
  462. * record while an application_data record is being written. */
  463. if (ssl_write_buffer_is_pending(ssl)) {
  464. al = SSL_AD_NO_RENEGOTIATION;
  465. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  466. goto f_err;
  467. }
  468. /* Begin a new handshake. */
  469. ssl->s3->total_renegotiations++;
  470. ssl->state = SSL_ST_CONNECT;
  471. i = ssl->handshake_func(ssl);
  472. if (i < 0) {
  473. return i;
  474. }
  475. if (i == 0) {
  476. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  477. return -1;
  478. }
  479. /* The handshake completed synchronously. Continue reading records. */
  480. goto start;
  481. }
  482. /* If an alert record, process one alert out of the record. Note that we allow
  483. * a single record to contain multiple alerts. */
  484. if (rr->type == SSL3_RT_ALERT) {
  485. /* Alerts may not be fragmented. */
  486. if (rr->length < 2) {
  487. al = SSL_AD_DECODE_ERROR;
  488. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
  489. goto f_err;
  490. }
  491. if (ssl->msg_callback) {
  492. ssl->msg_callback(0, ssl->version, SSL3_RT_ALERT, rr->data, 2, ssl,
  493. ssl->msg_callback_arg);
  494. }
  495. const uint8_t alert_level = rr->data[0];
  496. const uint8_t alert_descr = rr->data[1];
  497. rr->length -= 2;
  498. rr->data += 2;
  499. if (ssl->info_callback != NULL) {
  500. cb = ssl->info_callback;
  501. } else if (ssl->ctx->info_callback != NULL) {
  502. cb = ssl->ctx->info_callback;
  503. }
  504. if (cb != NULL) {
  505. uint16_t alert = (alert_level << 8) | alert_descr;
  506. cb(ssl, SSL_CB_READ_ALERT, alert);
  507. }
  508. if (alert_level == SSL3_AL_WARNING) {
  509. ssl->s3->warn_alert = alert_descr;
  510. if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
  511. ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
  512. return 0;
  513. }
  514. /* This is a warning but we receive it if we requested renegotiation and
  515. * the peer denied it. Terminate with a fatal alert because if
  516. * application tried to renegotiatie it presumably had a good reason and
  517. * expects it to succeed.
  518. *
  519. * In future we might have a renegotiation where we don't care if the
  520. * peer refused it where we carry on. */
  521. else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
  522. al = SSL_AD_HANDSHAKE_FAILURE;
  523. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  524. goto f_err;
  525. }
  526. ssl->s3->warning_alert_count++;
  527. if (ssl->s3->warning_alert_count > kMaxWarningAlerts) {
  528. al = SSL_AD_UNEXPECTED_MESSAGE;
  529. OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS);
  530. goto f_err;
  531. }
  532. } else if (alert_level == SSL3_AL_FATAL) {
  533. char tmp[16];
  534. ssl->rwstate = SSL_NOTHING;
  535. ssl->s3->fatal_alert = alert_descr;
  536. OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
  537. BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
  538. ERR_add_error_data(2, "SSL alert number ", tmp);
  539. ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
  540. SSL_CTX_remove_session(ssl->ctx, ssl->session);
  541. return 0;
  542. } else {
  543. al = SSL_AD_ILLEGAL_PARAMETER;
  544. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
  545. goto f_err;
  546. }
  547. goto start;
  548. }
  549. if (ssl->shutdown & SSL_SENT_SHUTDOWN) {
  550. /* close_notify has been sent, so discard all records other than alerts. */
  551. rr->length = 0;
  552. goto start;
  553. }
  554. al = SSL_AD_UNEXPECTED_MESSAGE;
  555. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
  556. f_err:
  557. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  558. err:
  559. return -1;
  560. }
  561. int ssl3_send_alert(SSL *ssl, int level, int desc) {
  562. /* If a fatal one, remove from cache */
  563. if (level == 2 && ssl->session != NULL) {
  564. SSL_CTX_remove_session(ssl->ctx, ssl->session);
  565. }
  566. ssl->s3->alert_dispatch = 1;
  567. ssl->s3->send_alert[0] = level;
  568. ssl->s3->send_alert[1] = desc;
  569. if (!ssl_write_buffer_is_pending(ssl)) {
  570. /* Nothing is being written out, so the alert may be dispatched
  571. * immediately. */
  572. return ssl->method->ssl_dispatch_alert(ssl);
  573. }
  574. /* else data is still being written out, we will get written some time in the
  575. * future */
  576. return -1;
  577. }
  578. int ssl3_dispatch_alert(SSL *ssl) {
  579. ssl->s3->alert_dispatch = 0;
  580. int ret = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2);
  581. if (ret <= 0) {
  582. ssl->s3->alert_dispatch = 1;
  583. return ret;
  584. }
  585. /* If the alert is fatal, flush the BIO now. */
  586. if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
  587. BIO_flush(ssl->wbio);
  588. }
  589. if (ssl->msg_callback != NULL) {
  590. ssl->msg_callback(1 /* write */, ssl->version, SSL3_RT_ALERT,
  591. ssl->s3->send_alert, 2, ssl, ssl->msg_callback_arg);
  592. }
  593. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  594. if (ssl->info_callback != NULL) {
  595. cb = ssl->info_callback;
  596. } else if (ssl->ctx->info_callback != NULL) {
  597. cb = ssl->ctx->info_callback;
  598. }
  599. if (cb != NULL) {
  600. int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
  601. cb(ssl, SSL_CB_WRITE_ALERT, alert);
  602. }
  603. return 1;
  604. }