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