Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 109 | #include <openssl/ssl.h> |
| 110 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 111 | #include <assert.h> |
Adam Langley | 87750b4 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 112 | #include <limits.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 113 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 114 | |
| 115 | #include <openssl/buf.h> |
| 116 | #include <openssl/err.h> |
| 117 | #include <openssl/evp.h> |
| 118 | #include <openssl/mem.h> |
| 119 | #include <openssl/rand.h> |
| 120 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 121 | #include "../crypto/internal.h" |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 122 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 123 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 124 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 125 | namespace bssl { |
| 126 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 127 | static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 129 | // ssl3_get_record reads a new input record. On success, it places it in |
| 130 | // |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if |
| 131 | // more data is needed. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 132 | static int ssl3_get_record(SSL *ssl) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | again: |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 134 | switch (ssl->s3->recv_shutdown) { |
| 135 | case ssl_shutdown_none: |
| 136 | break; |
| 137 | case ssl_shutdown_fatal_alert: |
| 138 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 139 | return -1; |
| 140 | case ssl_shutdown_close_notify: |
David Benjamin | 8f73135 | 2016-03-10 01:15:15 -0500 | [diff] [blame] | 141 | return 0; |
David Benjamin | 8f73135 | 2016-03-10 01:15:15 -0500 | [diff] [blame] | 142 | } |
| 143 | |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 144 | CBS body; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 145 | uint8_t type, alert = SSL_AD_DECODE_ERROR; |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 146 | size_t consumed; |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 147 | enum ssl_open_record_t open_ret = |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 148 | tls_open_record(ssl, &type, &body, &consumed, &alert, |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 149 | ssl_read_buffer(ssl), ssl_read_buffer_len(ssl)); |
| 150 | if (open_ret != ssl_open_record_partial) { |
| 151 | ssl_read_buffer_consume(ssl, consumed); |
| 152 | } |
| 153 | switch (open_ret) { |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 154 | case ssl_open_record_partial: { |
| 155 | int read_ret = ssl_read_buffer_extend_to(ssl, consumed); |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 156 | if (read_ret <= 0) { |
| 157 | return read_ret; |
| 158 | } |
| 159 | goto again; |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 160 | } |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame] | 161 | |
David Benjamin | e8703a3 | 2017-07-09 16:17:55 -0400 | [diff] [blame] | 162 | case ssl_open_record_success: { |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 163 | if (CBS_len(&body) > 0xffff) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 164 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 165 | return -1; |
| 166 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 168 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 169 | rr->type = type; |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 170 | rr->length = (uint16_t)CBS_len(&body); |
| 171 | rr->data = (uint8_t *)CBS_data(&body); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 172 | return 1; |
David Benjamin | e8703a3 | 2017-07-09 16:17:55 -0400 | [diff] [blame] | 173 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 174 | |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 175 | case ssl_open_record_discard: |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 176 | goto again; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 177 | |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 178 | case ssl_open_record_close_notify: |
| 179 | return 0; |
| 180 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 181 | case ssl_open_record_error: |
David Benjamin | 751d1a1 | 2017-09-25 13:37:30 -0400 | [diff] [blame^] | 182 | if (alert != 0) { |
| 183 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 184 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 185 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 186 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 187 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 188 | assert(0); |
| 189 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 190 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 191 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 192 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 193 | int ssl3_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf, |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 194 | int len) { |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 195 | assert(ssl_can_write(ssl)); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 196 | assert(!ssl->s3->aead_write_ctx->is_null_cipher()); |
David Benjamin | d7ac143 | 2016-03-10 00:41:25 -0500 | [diff] [blame] | 197 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 198 | *out_needs_handshake = false; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 199 | |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 200 | unsigned tot, n, nw; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 202 | assert(ssl->s3->wnum <= INT_MAX); |
| 203 | tot = ssl->s3->wnum; |
| 204 | ssl->s3->wnum = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 205 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 206 | // Ensure that if we end up with a smaller value of data to write out than |
| 207 | // the the original len from a write which didn't complete for non-blocking |
| 208 | // I/O and also somehow ended up avoiding the check for this in |
| 209 | // ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to |
| 210 | // end up with (len-tot) as a large number that will then promptly send |
| 211 | // beyond the end of the users buffer ... so we trap and report the error in |
| 212 | // a way the user will notice. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 213 | if (len < 0 || (size_t)len < tot) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 214 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 215 | return -1; |
| 216 | } |
Adam Langley | 9611cfc | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 218 | const int is_early_data_write = |
| 219 | !ssl->server && SSL_in_early_data(ssl) && ssl->s3->hs->can_early_write; |
| 220 | |
David Benjamin | 8671c47 | 2017-02-02 15:05:35 -0500 | [diff] [blame] | 221 | n = len - tot; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 222 | for (;;) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 223 | // max contains the maximum number of bytes that we can put into a record. |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 224 | unsigned max = ssl->max_send_fragment; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 225 | if (is_early_data_write && max > ssl->session->ticket_max_early_data - |
| 226 | ssl->s3->hs->early_data_written) { |
| 227 | max = ssl->session->ticket_max_early_data - ssl->s3->hs->early_data_written; |
| 228 | if (max == 0) { |
| 229 | ssl->s3->wnum = tot; |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 230 | ssl->s3->hs->can_early_write = false; |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 231 | *out_needs_handshake = true; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 232 | return -1; |
| 233 | } |
| 234 | } |
| 235 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 236 | if (n > max) { |
| 237 | nw = max; |
| 238 | } else { |
| 239 | nw = n; |
| 240 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 241 | |
David Benjamin | 8671c47 | 2017-02-02 15:05:35 -0500 | [diff] [blame] | 242 | int ret = do_ssl3_write(ssl, SSL3_RT_APPLICATION_DATA, &buf[tot], nw); |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 243 | if (ret <= 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 244 | ssl->s3->wnum = tot; |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 245 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 246 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 247 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 248 | if (is_early_data_write) { |
| 249 | ssl->s3->hs->early_data_written += ret; |
| 250 | } |
| 251 | |
David Benjamin | 8671c47 | 2017-02-02 15:05:35 -0500 | [diff] [blame] | 252 | if (ret == (int)n || (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)) { |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 253 | return tot + ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 254 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 255 | |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 256 | n -= ret; |
| 257 | tot += ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 258 | } |
| 259 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 260 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 261 | static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *buf, |
David Benjamin | 2e0901b | 2015-11-02 17:50:10 -0500 | [diff] [blame] | 262 | unsigned int len) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 263 | if (ssl->s3->wpend_tot > (int)len || |
David Benjamin | c6722cd | 2016-10-24 20:13:20 -0400 | [diff] [blame] | 264 | (!(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) && |
| 265 | ssl->s3->wpend_buf != buf) || |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 266 | ssl->s3->wpend_type != type) { |
David Benjamin | 2e0901b | 2015-11-02 17:50:10 -0500 | [diff] [blame] | 267 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY); |
| 268 | return -1; |
| 269 | } |
| 270 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 271 | int ret = ssl_write_buffer_flush(ssl); |
David Benjamin | 2e0901b | 2015-11-02 17:50:10 -0500 | [diff] [blame] | 272 | if (ret <= 0) { |
| 273 | return ret; |
| 274 | } |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 275 | ssl->s3->wpend_pending = false; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 276 | return ssl->s3->wpend_ret; |
David Benjamin | 2e0901b | 2015-11-02 17:50:10 -0500 | [diff] [blame] | 277 | } |
| 278 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 279 | // do_ssl3_write writes an SSL record of the given type. |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 280 | static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 281 | // If there is still data from the previous record, flush it. |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 282 | if (ssl->s3->wpend_pending) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 283 | return ssl3_write_pending(ssl, type, buf, len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 284 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 285 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 286 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 287 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 288 | return -1; |
| 289 | } |
Adam Langley | c6c8ae8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 290 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 291 | if (len == 0) { |
| 292 | return 0; |
| 293 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 294 | |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 295 | size_t flight_len = 0; |
| 296 | if (ssl->s3->pending_flight != NULL) { |
| 297 | flight_len = |
| 298 | ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset; |
| 299 | } |
| 300 | |
David Benjamin | da86360 | 2016-11-04 15:44:28 -0400 | [diff] [blame] | 301 | size_t max_out = len + SSL_max_seal_overhead(ssl); |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 302 | if (max_out < len || max_out + flight_len < max_out) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 303 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 304 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 305 | } |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 306 | max_out += flight_len; |
| 307 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 308 | uint8_t *out; |
| 309 | size_t ciphertext_len; |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 310 | if (!ssl_write_buffer_init(ssl, &out, max_out)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 311 | return -1; |
| 312 | } |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 313 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 314 | // Add any unflushed handshake data as a prefix. This may be a KeyUpdate |
| 315 | // acknowledgment or 0-RTT key change messages. |pending_flight| must be clear |
| 316 | // when data is added to |write_buffer| or it will be written in the wrong |
| 317 | // order. |
David Benjamin | bbba939 | 2017-04-06 12:54:12 -0400 | [diff] [blame] | 318 | if (ssl->s3->pending_flight != NULL) { |
| 319 | OPENSSL_memcpy( |
| 320 | out, ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset, |
| 321 | flight_len); |
| 322 | BUF_MEM_free(ssl->s3->pending_flight); |
| 323 | ssl->s3->pending_flight = NULL; |
| 324 | ssl->s3->pending_flight_offset = 0; |
| 325 | } |
| 326 | |
| 327 | if (!tls_seal_record(ssl, out + flight_len, &ciphertext_len, |
| 328 | max_out - flight_len, type, buf, len)) { |
| 329 | return -1; |
| 330 | } |
| 331 | ssl_write_buffer_set_len(ssl, flight_len + ciphertext_len); |
| 332 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 333 | // Now that we've made progress on the connection, uncork KeyUpdate |
| 334 | // acknowledgments. |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 335 | ssl->s3->key_update_pending = false; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 336 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 337 | // Memorize arguments so that ssl3_write_pending can detect bad write retries |
| 338 | // later. |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 339 | ssl->s3->wpend_tot = len; |
| 340 | ssl->s3->wpend_buf = buf; |
| 341 | ssl->s3->wpend_type = type; |
| 342 | ssl->s3->wpend_ret = len; |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 343 | ssl->s3->wpend_pending = true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 344 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 345 | // We now just need to write the buffer. |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 346 | return ssl3_write_pending(ssl, type, buf, len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 347 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 348 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 349 | static int consume_record(SSL *ssl, uint8_t *out, int len, int peek) { |
| 350 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 351 | |
| 352 | if (len <= 0) { |
| 353 | return len; |
| 354 | } |
| 355 | |
| 356 | if (len > (int)rr->length) { |
| 357 | len = (int)rr->length; |
| 358 | } |
| 359 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 360 | OPENSSL_memcpy(out, rr->data, len); |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 361 | if (!peek) { |
| 362 | rr->length -= len; |
| 363 | rr->data += len; |
| 364 | if (rr->length == 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 365 | // The record has been consumed, so we may now clear the buffer. |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 366 | ssl_read_buffer_discard(ssl); |
| 367 | } |
| 368 | } |
| 369 | return len; |
| 370 | } |
| 371 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 372 | int ssl3_read_app_data(SSL *ssl, bool *out_got_handshake, uint8_t *buf, int len, |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 373 | int peek) { |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 374 | assert(ssl_can_read(ssl)); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 375 | assert(!ssl->s3->aead_read_ctx->is_null_cipher()); |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 376 | *out_got_handshake = false; |
David Benjamin | 4bea850 | 2016-05-12 09:34:55 -0400 | [diff] [blame] | 377 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 378 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 379 | |
| 380 | for (;;) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 381 | // A previous iteration may have read a partial handshake message. Do not |
| 382 | // allow more app data in that case. |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 383 | int has_hs_data = ssl->init_buf != NULL && ssl->init_buf->length > 0; |
| 384 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 385 | // Get new packet if necessary. |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 386 | if (rr->length == 0 && !has_hs_data) { |
| 387 | int ret = ssl3_get_record(ssl); |
| 388 | if (ret <= 0) { |
| 389 | return ret; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | if (has_hs_data || rr->type == SSL3_RT_HANDSHAKE) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 394 | // If reading 0-RTT data, reject handshake data. 0-RTT data is terminated |
| 395 | // by an alert. |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 396 | if (SSL_in_init(ssl)) { |
| 397 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 398 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 399 | return -1; |
| 400 | } |
| 401 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 402 | // Post-handshake data prior to TLS 1.3 is always renegotiation, which we |
| 403 | // never accept as a server. Otherwise |ssl3_get_message| will send |
| 404 | // |SSL_R_EXCESSIVE_MESSAGE_SIZE|. |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 405 | if (ssl->server && ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
| 406 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION); |
| 407 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 408 | return -1; |
| 409 | } |
| 410 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 411 | // Parse post-handshake handshake messages. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 412 | int ret = ssl3_read_message(ssl); |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 413 | if (ret <= 0) { |
| 414 | return ret; |
| 415 | } |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 416 | *out_got_handshake = true; |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 417 | return -1; |
| 418 | } |
| 419 | |
Alessandro Ghedini | de254b4 | 2017-04-17 19:12:33 +0100 | [diff] [blame] | 420 | const int is_early_data_read = ssl->server && |
| 421 | ssl->s3->hs != NULL && |
| 422 | ssl->s3->hs->can_early_read && |
| 423 | ssl3_protocol_version(ssl) >= TLS1_3_VERSION; |
| 424 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 425 | // Handle the end_of_early_data alert. |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 426 | if (rr->type == SSL3_RT_ALERT && |
David Benjamin | bbfe603 | 2017-03-26 14:55:42 -0500 | [diff] [blame] | 427 | rr->length == 2 && |
| 428 | rr->data[0] == SSL3_AL_WARNING && |
| 429 | rr->data[1] == TLS1_AD_END_OF_EARLY_DATA && |
Alessandro Ghedini | de254b4 | 2017-04-17 19:12:33 +0100 | [diff] [blame] | 430 | is_early_data_read) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 431 | // Consume the record. |
David Benjamin | bbfe603 | 2017-03-26 14:55:42 -0500 | [diff] [blame] | 432 | rr->length = 0; |
| 433 | ssl_read_buffer_discard(ssl); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 434 | // Stop accepting early data. |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 435 | ssl->s3->hs->can_early_read = false; |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 436 | *out_got_handshake = true; |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 437 | return -1; |
| 438 | } |
| 439 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 440 | if (rr->type != SSL3_RT_APPLICATION_DATA) { |
| 441 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 442 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 443 | return -1; |
| 444 | } |
| 445 | |
Alessandro Ghedini | de254b4 | 2017-04-17 19:12:33 +0100 | [diff] [blame] | 446 | if (is_early_data_read) { |
| 447 | if (rr->length > kMaxEarlyDataAccepted - ssl->s3->hs->early_data_read) { |
| 448 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MUCH_READ_EARLY_DATA); |
| 449 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE); |
| 450 | return -1; |
| 451 | } |
| 452 | |
| 453 | ssl->s3->hs->early_data_read += rr->length; |
| 454 | } |
| 455 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 456 | if (rr->length != 0) { |
| 457 | return consume_record(ssl, buf, len, peek); |
| 458 | } |
| 459 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 460 | // Discard empty records and loop again. |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 461 | } |
David Benjamin | a602277 | 2015-05-30 16:22:10 -0400 | [diff] [blame] | 462 | } |
| 463 | |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 464 | int ssl3_read_change_cipher_spec(SSL *ssl) { |
David Benjamin | 585320c | 2016-05-10 20:46:16 -0400 | [diff] [blame] | 465 | SSL3_RECORD *rr = &ssl->s3->rrec; |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 466 | |
David Benjamin | 585320c | 2016-05-10 20:46:16 -0400 | [diff] [blame] | 467 | if (rr->length == 0) { |
| 468 | int ret = ssl3_get_record(ssl); |
| 469 | if (ret <= 0) { |
| 470 | return ret; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) { |
| 475 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 476 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 477 | return -1; |
| 478 | } |
| 479 | |
| 480 | if (rr->length != 1 || rr->data[0] != SSL3_MT_CCS) { |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 481 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
| 482 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 483 | return -1; |
| 484 | } |
| 485 | |
David Benjamin | c027999 | 2016-09-19 20:15:07 -0400 | [diff] [blame] | 486 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, |
| 487 | rr->length); |
David Benjamin | 585320c | 2016-05-10 20:46:16 -0400 | [diff] [blame] | 488 | |
| 489 | rr->length = 0; |
| 490 | ssl_read_buffer_discard(ssl); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 491 | return 1; |
| 492 | } |
| 493 | |
David Benjamin | a602277 | 2015-05-30 16:22:10 -0400 | [diff] [blame] | 494 | void ssl3_read_close_notify(SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 495 | // Read records until an error or close_notify. |
David Benjamin | 4aa4081 | 2016-06-01 19:40:23 -0400 | [diff] [blame] | 496 | while (ssl3_get_record(ssl) > 0) { |
| 497 | ; |
| 498 | } |
David Benjamin | a602277 | 2015-05-30 16:22:10 -0400 | [diff] [blame] | 499 | } |
| 500 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 501 | int ssl3_read_handshake_bytes(SSL *ssl, uint8_t *buf, int len) { |
| 502 | SSL3_RECORD *rr = &ssl->s3->rrec; |
David Benjamin | 397c8e6 | 2016-07-08 14:14:36 -0700 | [diff] [blame] | 503 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 504 | for (;;) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 505 | // Get new packet if necessary. |
David Benjamin | 4cf369b | 2015-08-22 01:35:43 -0400 | [diff] [blame] | 506 | if (rr->length == 0) { |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 507 | int ret = ssl3_get_record(ssl); |
| 508 | if (ret <= 0) { |
| 509 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 513 | // WatchGuard's TLS 1.3 interference bug is very distinctive: they drop the |
| 514 | // ServerHello and send the remaining encrypted application data records |
| 515 | // as-is. This manifests as an application data record when we expect |
| 516 | // handshake. Report a dedicated error code for this case. |
David Benjamin | 5aaaa98 | 2017-06-22 15:03:18 -0400 | [diff] [blame] | 517 | if (!ssl->server && rr->type == SSL3_RT_APPLICATION_DATA && |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 518 | ssl->s3->aead_read_ctx->is_null_cipher()) { |
David Benjamin | 5aaaa98 | 2017-06-22 15:03:18 -0400 | [diff] [blame] | 519 | OPENSSL_PUT_ERROR(SSL, SSL_R_APPLICATION_DATA_INSTEAD_OF_HANDSHAKE); |
| 520 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 521 | return -1; |
| 522 | } |
| 523 | |
Steven Valdez | 4c7f5fa | 2017-10-02 15:53:57 -0400 | [diff] [blame] | 524 | if (rr->type != SSL3_RT_HANDSHAKE) { |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 525 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 526 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
David Benjamin | cd90f3a | 2015-05-21 01:37:55 -0400 | [diff] [blame] | 527 | return -1; |
| 528 | } |
| 529 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 530 | if (rr->length != 0) { |
| 531 | return consume_record(ssl, buf, len, 0 /* consume data */); |
| 532 | } |
| 533 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 534 | // Discard empty records and loop again. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 535 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 536 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 537 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 538 | int ssl3_send_alert(SSL *ssl, int level, int desc) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 539 | // It is illegal to send an alert when we've already sent a closing one. |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 540 | if (ssl->s3->send_shutdown != ssl_shutdown_none) { |
| 541 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 542 | return -1; |
| 543 | } |
| 544 | |
David Benjamin | 0efa759 | 2017-01-27 20:51:33 -0500 | [diff] [blame] | 545 | if (level == SSL3_AL_WARNING && desc == SSL_AD_CLOSE_NOTIFY) { |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 546 | ssl->s3->send_shutdown = ssl_shutdown_close_notify; |
David Benjamin | 0efa759 | 2017-01-27 20:51:33 -0500 | [diff] [blame] | 547 | } else { |
| 548 | assert(level == SSL3_AL_FATAL); |
David Benjamin | 751d1a1 | 2017-09-25 13:37:30 -0400 | [diff] [blame^] | 549 | assert(desc != SSL_AD_CLOSE_NOTIFY); |
David Benjamin | 0efa759 | 2017-01-27 20:51:33 -0500 | [diff] [blame] | 550 | ssl->s3->send_shutdown = ssl_shutdown_fatal_alert; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 551 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 552 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 553 | ssl->s3->alert_dispatch = 1; |
| 554 | ssl->s3->send_alert[0] = level; |
| 555 | ssl->s3->send_alert[1] = desc; |
| 556 | if (!ssl_write_buffer_is_pending(ssl)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 557 | // Nothing is being written out, so the alert may be dispatched |
| 558 | // immediately. |
David Benjamin | f0ee907 | 2016-06-15 17:44:37 -0400 | [diff] [blame] | 559 | return ssl->method->dispatch_alert(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 560 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 561 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 562 | // The alert will be dispatched later. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 563 | return -1; |
| 564 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 565 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 566 | int ssl3_dispatch_alert(SSL *ssl) { |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 567 | int ret = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2); |
| 568 | if (ret <= 0) { |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 569 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 570 | } |
David Benjamin | 0efa759 | 2017-01-27 20:51:33 -0500 | [diff] [blame] | 571 | ssl->s3->alert_dispatch = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 572 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 573 | // If the alert is fatal, flush the BIO now. |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 574 | if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) { |
| 575 | BIO_flush(ssl->wbio); |
| 576 | } |
| 577 | |
David Benjamin | c027999 | 2016-09-19 20:15:07 -0400 | [diff] [blame] | 578 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert, |
| 579 | 2); |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 580 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 581 | int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1]; |
| 582 | ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert); |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 583 | |
| 584 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 585 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 586 | |
| 587 | } // namespace bssl |