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.
 
 
 
 
 
 

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