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 | |
| 109 | #include <assert.h> |
Adam Langley | 87750b4 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 110 | #include <limits.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 111 | #include <stdio.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 112 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 113 | |
| 114 | #include <openssl/buf.h> |
| 115 | #include <openssl/err.h> |
| 116 | #include <openssl/evp.h> |
| 117 | #include <openssl/mem.h> |
| 118 | #include <openssl/rand.h> |
| 119 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 120 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 122 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 123 | static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | |
David Benjamin | 24f346d | 2015-06-06 03:28:08 -0400 | [diff] [blame] | 125 | /* kMaxWarningAlerts is the number of consecutive warning alerts that will be |
| 126 | * processed. */ |
| 127 | static const uint8_t kMaxWarningAlerts = 4; |
| 128 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -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. */ |
| 132 | static int ssl3_get_record(SSL *ssl) { |
| 133 | int ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | again: |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 135 | /* Ensure the buffer is large enough to decrypt in-place. */ |
| 136 | ret = ssl_read_buffer_extend_to(ssl, ssl_record_prefix_len(ssl)); |
| 137 | if (ret <= 0) { |
| 138 | return ret; |
| 139 | } |
| 140 | assert(ssl_read_buffer_len(ssl) >= ssl_record_prefix_len(ssl)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 141 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 142 | uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl); |
| 143 | size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl); |
| 144 | uint8_t type, alert; |
| 145 | size_t len, consumed; |
| 146 | switch (tls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out, |
| 147 | ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) { |
| 148 | case ssl_open_record_success: |
| 149 | ssl_read_buffer_consume(ssl, consumed); |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame] | 150 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 151 | if (len > 0xffff) { |
| 152 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 153 | return -1; |
| 154 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 156 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 157 | rr->type = type; |
| 158 | rr->length = (uint16_t)len; |
| 159 | rr->off = 0; |
| 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) { |
| 185 | return ssl3_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len); |
| 186 | } |
| 187 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 188 | /* Call this to write data in records of type |type|. It will return <= 0 if |
| 189 | * not all data has been sent or non-blocking IO. */ |
| 190 | int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) { |
| 191 | const uint8_t *buf = buf_; |
| 192 | unsigned int tot, n, nw; |
| 193 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 194 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 195 | s->rwstate = SSL_NOTHING; |
| 196 | assert(s->s3->wnum <= INT_MAX); |
| 197 | tot = s->s3->wnum; |
| 198 | s->s3->wnum = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 199 | |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 200 | if (!s->in_handshake && SSL_in_init(s) && !SSL_in_false_start(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 201 | i = s->handshake_func(s); |
| 202 | if (i < 0) { |
| 203 | return i; |
| 204 | } |
| 205 | if (i == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 206 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 207 | return -1; |
| 208 | } |
| 209 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 210 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 211 | /* Ensure that if we end up with a smaller value of data to write out than |
| 212 | * the the original len from a write which didn't complete for non-blocking |
| 213 | * I/O and also somehow ended up avoiding the check for this in |
| 214 | * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to |
| 215 | * end up with (len-tot) as a large number that will then promptly send |
| 216 | * beyond the end of the users buffer ... so we trap and report the error in |
| 217 | * a way the user will notice. */ |
| 218 | if (len < 0 || (size_t)len < tot) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 219 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 220 | return -1; |
| 221 | } |
Adam Langley | 9611cfc | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 223 | n = (len - tot); |
| 224 | for (;;) { |
| 225 | /* max contains the maximum number of bytes that we can put into a |
| 226 | * record. */ |
| 227 | unsigned max = s->max_send_fragment; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 228 | if (n > max) { |
| 229 | nw = max; |
| 230 | } else { |
| 231 | nw = n; |
| 232 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 233 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 234 | i = do_ssl3_write(s, type, &buf[tot], nw); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 235 | if (i <= 0) { |
| 236 | s->s3->wnum = tot; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 237 | return i; |
| 238 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 239 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 240 | if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA && |
| 241 | (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 242 | return tot + i; |
| 243 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 244 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 245 | n -= i; |
| 246 | tot += i; |
| 247 | } |
| 248 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 249 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 250 | /* do_ssl3_write writes an SSL record of the given type. */ |
| 251 | static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned len) { |
| 252 | /* If there is still data from the previous record, flush it. */ |
| 253 | if (ssl_write_buffer_is_pending(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 254 | return ssl3_write_pending(s, type, buf, len); |
| 255 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 257 | /* If we have an alert to send, lets send it */ |
| 258 | if (s->s3->alert_dispatch) { |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 259 | int ret = s->method->ssl_dispatch_alert(s); |
| 260 | if (ret <= 0) { |
| 261 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 262 | } |
| 263 | /* if it went, fall through and send more stuff */ |
| 264 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 266 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 267 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 268 | return -1; |
| 269 | } |
Adam Langley | c6c8ae8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 270 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 271 | if (len == 0) { |
| 272 | return 0; |
| 273 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 274 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 275 | size_t max_out = len + ssl_max_seal_overhead(s); |
| 276 | if (max_out < len) { |
| 277 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 278 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 279 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 280 | uint8_t *out; |
| 281 | size_t ciphertext_len; |
| 282 | if (!ssl_write_buffer_init(s, &out, max_out) || |
| 283 | !tls_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) { |
| 284 | return -1; |
| 285 | } |
| 286 | ssl_write_buffer_set_len(s, ciphertext_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 287 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 288 | /* memorize arguments so that ssl3_write_pending can detect bad write retries |
| 289 | * later */ |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 290 | s->s3->wpend_tot = len; |
| 291 | s->s3->wpend_buf = buf; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 292 | s->s3->wpend_type = type; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 293 | s->s3->wpend_ret = len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 294 | |
| 295 | /* we now just need to write the buffer */ |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 296 | return ssl3_write_pending(s, type, buf, len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 297 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 298 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 299 | int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 300 | if (s->s3->wpend_tot > (int)len || |
| 301 | (s->s3->wpend_buf != buf && |
| 302 | !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) || |
| 303 | s->s3->wpend_type != type) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 304 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 305 | return -1; |
| 306 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 307 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 308 | int ret = ssl_write_buffer_flush(s); |
| 309 | if (ret <= 0) { |
| 310 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 311 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 312 | return s->s3->wpend_ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 313 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 314 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 315 | /* ssl3_expect_change_cipher_spec informs the record layer that a |
| 316 | * ChangeCipherSpec record is required at this point. If a Handshake record is |
| 317 | * received before ChangeCipherSpec, the connection will fail. Moreover, if |
| 318 | * there are unprocessed handshake bytes, the handshake will also fail and the |
| 319 | * function returns zero. Otherwise, the function returns one. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 320 | int ssl3_expect_change_cipher_spec(SSL *s) { |
| 321 | if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 322 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNPROCESSED_HANDSHAKE_DATA); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | s->s3->flags |= SSL3_FLAGS_EXPECT_CCS; |
| 327 | return 1; |
| 328 | } |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 329 | |
David Benjamin | a602277 | 2015-05-30 16:22:10 -0400 | [diff] [blame] | 330 | int ssl3_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) { |
| 331 | return ssl3_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek); |
| 332 | } |
| 333 | |
| 334 | void ssl3_read_close_notify(SSL *ssl) { |
| 335 | ssl3_read_bytes(ssl, 0, NULL, 0, 0); |
| 336 | } |
| 337 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 338 | /* Return up to 'len' payload bytes received in 'type' records. |
| 339 | * 'type' is one of the following: |
| 340 | * |
| 341 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) |
| 342 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) |
| 343 | * - 0 (during a shutdown, no data has to be returned) |
| 344 | * |
| 345 | * If we don't have stored data to work from, read a SSL/TLS record first |
| 346 | * (possibly multiple records if we still don't have anything to return). |
| 347 | * |
| 348 | * This function must handle any surprises the peer may have for us, such as |
| 349 | * Alert records (e.g. close_notify), ChangeCipherSpec records (not really |
| 350 | * a surprise, but handled as if it were), or renegotiation requests. |
| 351 | * Also if record payloads contain fragments too small to process, we store |
| 352 | * them until there is enough for the respective protocol (the record protocol |
| 353 | * may use arbitrary fragmentation and even interleaving): |
| 354 | * Change cipher spec protocol |
| 355 | * just 1 byte needed, no need for keeping anything stored |
| 356 | * Alert protocol |
| 357 | * 2 bytes needed (AlertLevel, AlertDescription) |
| 358 | * Handshake protocol |
| 359 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have |
| 360 | * to detect unexpected Client Hello and Hello Request messages |
| 361 | * here, anything else is handled by higher layers |
| 362 | * Application data protocol |
| 363 | * none of our business |
| 364 | */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 365 | int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek) { |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 366 | int al, i, ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 367 | unsigned int n; |
| 368 | SSL3_RECORD *rr; |
| 369 | void (*cb)(const SSL *ssl, int type2, int val) = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 370 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 371 | if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) || |
| 372 | (peek && type != SSL3_RT_APPLICATION_DATA)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 373 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 374 | return -1; |
| 375 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 376 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 377 | if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) { |
| 378 | /* (partially) satisfy request from storage */ |
| 379 | uint8_t *src = s->s3->handshake_fragment; |
| 380 | uint8_t *dst = buf; |
| 381 | unsigned int k; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 382 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 383 | /* peek == 0 */ |
| 384 | n = 0; |
| 385 | while (len > 0 && s->s3->handshake_fragment_len > 0) { |
| 386 | *dst++ = *src++; |
| 387 | len--; |
| 388 | s->s3->handshake_fragment_len--; |
| 389 | n++; |
| 390 | } |
| 391 | /* move any remaining fragment bytes: */ |
| 392 | for (k = 0; k < s->s3->handshake_fragment_len; k++) { |
| 393 | s->s3->handshake_fragment[k] = *src++; |
| 394 | } |
| 395 | return n; |
| 396 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 397 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 398 | /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 399 | |
David Benjamin | 931ab34 | 2015-02-08 19:46:57 -0500 | [diff] [blame] | 400 | /* This may require multiple iterations. False Start will cause |
| 401 | * |s->handshake_func| to signal success one step early, but the handshake |
| 402 | * must be completely finished before other modes are accepted. |
| 403 | * |
| 404 | * TODO(davidben): Move this check up to a higher level. */ |
| 405 | while (!s->in_handshake && SSL_in_init(s)) { |
| 406 | assert(type == SSL3_RT_APPLICATION_DATA); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 407 | i = s->handshake_func(s); |
| 408 | if (i < 0) { |
| 409 | return i; |
| 410 | } |
| 411 | if (i == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 412 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 413 | return -1; |
| 414 | } |
| 415 | } |
| 416 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 417 | start: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 418 | s->rwstate = SSL_NOTHING; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 419 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 420 | /* s->s3->rrec.type - is the type of record |
| 421 | * s->s3->rrec.data - data |
| 422 | * s->s3->rrec.off - offset into 'data' for next read |
| 423 | * s->s3->rrec.length - number of bytes. */ |
| 424 | rr = &s->s3->rrec; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 426 | /* get new packet if necessary */ |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 427 | if (rr->length == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 428 | ret = ssl3_get_record(s); |
| 429 | if (ret <= 0) { |
| 430 | return ret; |
| 431 | } |
| 432 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 433 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 434 | /* we now have a packet which can be read and processed */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 435 | |
David Benjamin | dc3da93 | 2015-03-12 15:09:02 -0400 | [diff] [blame] | 436 | /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by |
| 437 | * ssl3_get_finished. */ |
| 438 | if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE && |
| 439 | rr->type != SSL3_RT_ALERT) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 440 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 441 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 442 | goto f_err; |
| 443 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 444 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 445 | /* If we are expecting a ChangeCipherSpec, it is illegal to receive a |
| 446 | * Handshake record. */ |
| 447 | if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) { |
| 448 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 449 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 450 | goto f_err; |
| 451 | } |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 452 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 453 | /* If the other end has shut down, throw anything we read away (even in |
| 454 | * 'peek' mode) */ |
| 455 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { |
| 456 | rr->length = 0; |
| 457 | s->rwstate = SSL_NOTHING; |
| 458 | return 0; |
| 459 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 460 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 461 | if (type == rr->type) { |
David Benjamin | 24f346d | 2015-06-06 03:28:08 -0400 | [diff] [blame] | 462 | s->s3->warning_alert_count = 0; |
| 463 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 464 | /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */ |
| 465 | /* make sure that we are not getting application data when we are doing a |
| 466 | * handshake for the first time */ |
| 467 | if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA && |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 468 | s->aead_read_ctx == NULL) { |
| 469 | /* TODO(davidben): Is this check redundant with the handshake_func |
| 470 | * check? */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 471 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 472 | OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 473 | goto f_err; |
| 474 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 475 | |
David Benjamin | 4cf369b | 2015-08-22 01:35:43 -0400 | [diff] [blame] | 476 | /* Discard empty records. */ |
| 477 | if (rr->length == 0) { |
| 478 | goto start; |
| 479 | } |
| 480 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 481 | if (len <= 0) { |
| 482 | return len; |
| 483 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 485 | if ((unsigned int)len > rr->length) { |
| 486 | n = rr->length; |
| 487 | } else { |
| 488 | n = (unsigned int)len; |
| 489 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 490 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 491 | memcpy(buf, &(rr->data[rr->off]), n); |
| 492 | if (!peek) { |
| 493 | rr->length -= n; |
| 494 | rr->off += n; |
| 495 | if (rr->length == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 496 | rr->off = 0; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 497 | /* The record has been consumed, so we may now clear the buffer. */ |
| 498 | ssl_read_buffer_discard(s); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
| 502 | return n; |
| 503 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 504 | |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 505 | /* Process unexpected records. */ |
Alex Chernyakhovsky | 4cd8c43 | 2014-11-01 19:39:08 -0400 | [diff] [blame] | 506 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 507 | if (rr->type == SSL3_RT_HANDSHAKE) { |
David Benjamin | b16346b | 2015-04-08 19:16:58 -0400 | [diff] [blame] | 508 | /* If peer renegotiations are disabled, all out-of-order handshake records |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 509 | * are fatal. Renegotiations as a server are never supported. */ |
| 510 | if (!s->accept_peer_renegotiations || s->server) { |
David Benjamin | b16346b | 2015-04-08 19:16:58 -0400 | [diff] [blame] | 511 | al = SSL_AD_NO_RENEGOTIATION; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 512 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
David Benjamin | b16346b | 2015-04-08 19:16:58 -0400 | [diff] [blame] | 513 | goto f_err; |
| 514 | } |
| 515 | |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 516 | /* HelloRequests may be fragmented across multiple records. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 517 | const size_t size = sizeof(s->s3->handshake_fragment); |
| 518 | const size_t avail = size - s->s3->handshake_fragment_len; |
| 519 | const size_t todo = (rr->length < avail) ? rr->length : avail; |
| 520 | memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len, |
| 521 | &rr->data[rr->off], todo); |
| 522 | rr->off += todo; |
| 523 | rr->length -= todo; |
| 524 | s->s3->handshake_fragment_len += todo; |
| 525 | if (s->s3->handshake_fragment_len < size) { |
| 526 | goto start; /* fragment was too small */ |
| 527 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 528 | |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 529 | /* Parse out and consume a HelloRequest. */ |
| 530 | if (s->s3->handshake_fragment[0] != SSL3_MT_HELLO_REQUEST || |
| 531 | s->s3->handshake_fragment[1] != 0 || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 532 | s->s3->handshake_fragment[2] != 0 || |
| 533 | s->s3->handshake_fragment[3] != 0) { |
| 534 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 535 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 536 | goto f_err; |
| 537 | } |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 538 | s->s3->handshake_fragment_len = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 539 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 540 | if (s->msg_callback) { |
| 541 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, |
| 542 | s->s3->handshake_fragment, 4, s, s->msg_callback_arg); |
| 543 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 544 | |
David Benjamin | cd90f3a | 2015-05-21 01:37:55 -0400 | [diff] [blame] | 545 | if (!SSL_is_init_finished(s) || !s->s3->initial_handshake_complete) { |
| 546 | /* This cannot happen. If a handshake is in progress, |type| must be |
| 547 | * |SSL3_RT_HANDSHAKE|. */ |
| 548 | assert(0); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 549 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | cd90f3a | 2015-05-21 01:37:55 -0400 | [diff] [blame] | 550 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 551 | } |
David Benjamin | cd90f3a | 2015-05-21 01:37:55 -0400 | [diff] [blame] | 552 | |
| 553 | /* Renegotiation is only supported at quiescent points in the application |
| 554 | * protocol, namely in HTTPS, just before reading the HTTP response. Require |
| 555 | * the record-layer be idle and avoid complexities of sending a handshake |
| 556 | * record while an application_data record is being written. */ |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 557 | if (ssl_write_buffer_is_pending(s)) { |
David Benjamin | cd90f3a | 2015-05-21 01:37:55 -0400 | [diff] [blame] | 558 | al = SSL_AD_NO_RENEGOTIATION; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 559 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
David Benjamin | cd90f3a | 2015-05-21 01:37:55 -0400 | [diff] [blame] | 560 | goto f_err; |
| 561 | } |
| 562 | |
| 563 | /* Begin a new handshake. */ |
| 564 | s->state = SSL_ST_CONNECT; |
| 565 | i = s->handshake_func(s); |
| 566 | if (i < 0) { |
| 567 | return i; |
| 568 | } |
| 569 | if (i == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 570 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
David Benjamin | cd90f3a | 2015-05-21 01:37:55 -0400 | [diff] [blame] | 571 | return -1; |
| 572 | } |
| 573 | |
| 574 | /* The handshake completed synchronously. Continue reading records. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 575 | goto start; |
| 576 | } |
David Benjamin | b4188f0 | 2014-11-01 03:43:48 -0400 | [diff] [blame] | 577 | |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 578 | /* If an alert record, process one alert out of the record. Note that we allow |
| 579 | * a single record to contain multiple alerts. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 580 | if (rr->type == SSL3_RT_ALERT) { |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 581 | /* Alerts may not be fragmented. */ |
| 582 | if (rr->length < 2) { |
| 583 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 584 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT); |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 585 | goto f_err; |
| 586 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 587 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 588 | if (s->msg_callback) { |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 589 | s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 590 | s->msg_callback_arg); |
| 591 | } |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 592 | const uint8_t alert_level = rr->data[rr->off++]; |
| 593 | const uint8_t alert_descr = rr->data[rr->off++]; |
| 594 | rr->length -= 2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 595 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 596 | if (s->info_callback != NULL) { |
| 597 | cb = s->info_callback; |
| 598 | } else if (s->ctx->info_callback != NULL) { |
| 599 | cb = s->ctx->info_callback; |
| 600 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 601 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 602 | if (cb != NULL) { |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 603 | uint16_t alert = (alert_level << 8) | alert_descr; |
| 604 | cb(s, SSL_CB_READ_ALERT, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 605 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 606 | |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 607 | if (alert_level == SSL3_AL_WARNING) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 608 | s->s3->warn_alert = alert_descr; |
| 609 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) { |
| 610 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
| 611 | return 0; |
| 612 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 613 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 614 | /* This is a warning but we receive it if we requested renegotiation and |
| 615 | * the peer denied it. Terminate with a fatal alert because if |
| 616 | * application tried to renegotiatie it presumably had a good reason and |
| 617 | * expects it to succeed. |
| 618 | * |
| 619 | * In future we might have a renegotiation where we don't care if the |
| 620 | * peer refused it where we carry on. */ |
| 621 | else if (alert_descr == SSL_AD_NO_RENEGOTIATION) { |
| 622 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 623 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 624 | goto f_err; |
| 625 | } |
David Benjamin | 24f346d | 2015-06-06 03:28:08 -0400 | [diff] [blame] | 626 | |
| 627 | s->s3->warning_alert_count++; |
| 628 | if (s->s3->warning_alert_count > kMaxWarningAlerts) { |
| 629 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 630 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS); |
David Benjamin | 24f346d | 2015-06-06 03:28:08 -0400 | [diff] [blame] | 631 | goto f_err; |
| 632 | } |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 633 | } else if (alert_level == SSL3_AL_FATAL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 634 | char tmp[16]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 635 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 636 | s->rwstate = SSL_NOTHING; |
| 637 | s->s3->fatal_alert = alert_descr; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 638 | OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 639 | BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr); |
| 640 | ERR_add_error_data(2, "SSL alert number ", tmp); |
| 641 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
| 642 | SSL_CTX_remove_session(s->ctx, s->session); |
| 643 | return 0; |
| 644 | } else { |
| 645 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 646 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 647 | goto f_err; |
| 648 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 649 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 650 | goto start; |
| 651 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 652 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 653 | if (s->shutdown & SSL_SENT_SHUTDOWN) { |
David Benjamin | aa9361b | 2015-08-29 22:33:16 -0400 | [diff] [blame^] | 654 | /* close_notify has been sent, so discard all records other than alerts. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 655 | rr->length = 0; |
David Benjamin | aa9361b | 2015-08-29 22:33:16 -0400 | [diff] [blame^] | 656 | goto start; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 657 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 658 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 659 | if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) { |
| 660 | /* 'Change Cipher Spec' is just a single byte, so we know exactly what the |
| 661 | * record payload has to look like */ |
| 662 | if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) { |
| 663 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 664 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 665 | goto f_err; |
| 666 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 667 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 668 | /* Check we have a cipher to change to */ |
| 669 | if (s->s3->tmp.new_cipher == NULL) { |
| 670 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 671 | OPENSSL_PUT_ERROR(SSL, SSL_R_CCS_RECEIVED_EARLY); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 672 | goto f_err; |
| 673 | } |
Adam Langley | ce7f9ca | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 674 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 675 | if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) { |
| 676 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 677 | OPENSSL_PUT_ERROR(SSL, SSL_R_CCS_RECEIVED_EARLY); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 678 | goto f_err; |
| 679 | } |
Adam Langley | ce7f9ca | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 680 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 681 | s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 682 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 683 | rr->length = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 684 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 685 | if (s->msg_callback) { |
| 686 | s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s, |
| 687 | s->msg_callback_arg); |
| 688 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 689 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 690 | s->s3->change_cipher_spec = 1; |
| 691 | if (!ssl3_do_change_cipher_spec(s)) { |
| 692 | goto err; |
| 693 | } else { |
| 694 | goto start; |
| 695 | } |
| 696 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 697 | |
David Benjamin | e820df9 | 2015-02-08 16:06:54 -0500 | [diff] [blame] | 698 | /* We already handled these. */ |
| 699 | assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT && |
| 700 | rr->type != SSL3_RT_HANDSHAKE); |
David Benjamin | ddb9f15 | 2015-02-03 15:44:39 -0500 | [diff] [blame] | 701 | |
David Benjamin | e820df9 | 2015-02-08 16:06:54 -0500 | [diff] [blame] | 702 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 703 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 704 | |
| 705 | f_err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 706 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 707 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 708 | return -1; |
| 709 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 710 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 711 | int ssl3_do_change_cipher_spec(SSL *s) { |
| 712 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 713 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 714 | if (s->state & SSL_ST_ACCEPT) { |
| 715 | i = SSL3_CHANGE_CIPHER_SERVER_READ; |
| 716 | } else { |
| 717 | i = SSL3_CHANGE_CIPHER_CLIENT_READ; |
| 718 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 719 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 720 | if (s->s3->tmp.key_block == NULL) { |
| 721 | if (s->session == NULL || s->session->master_key_length == 0) { |
| 722 | /* might happen if dtls1_read_bytes() calls this */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 723 | OPENSSL_PUT_ERROR(SSL, SSL_R_CCS_RECEIVED_EARLY); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 724 | return 0; |
| 725 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 726 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 727 | s->session->cipher = s->s3->tmp.new_cipher; |
| 728 | if (!s->enc_method->setup_key_block(s)) { |
| 729 | return 0; |
| 730 | } |
| 731 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 732 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 733 | if (!s->enc_method->change_cipher_state(s, i)) { |
| 734 | return 0; |
| 735 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 736 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 737 | return 1; |
| 738 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 739 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 740 | int ssl3_send_alert(SSL *s, int level, int desc) { |
| 741 | /* Map tls/ssl alert value to correct one */ |
| 742 | desc = s->enc_method->alert_value(desc); |
| 743 | if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) { |
| 744 | /* SSL 3.0 does not have protocol_version alerts */ |
| 745 | desc = SSL_AD_HANDSHAKE_FAILURE; |
| 746 | } |
| 747 | if (desc < 0) { |
| 748 | return -1; |
| 749 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 750 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 751 | /* If a fatal one, remove from cache */ |
| 752 | if (level == 2 && s->session != NULL) { |
| 753 | SSL_CTX_remove_session(s->ctx, s->session); |
| 754 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 755 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 756 | s->s3->alert_dispatch = 1; |
| 757 | s->s3->send_alert[0] = level; |
| 758 | s->s3->send_alert[1] = desc; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 759 | if (!ssl_write_buffer_is_pending(s)) { |
| 760 | /* Nothing is being written out, so the alert may be dispatched |
| 761 | * immediately. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 762 | return s->method->ssl_dispatch_alert(s); |
| 763 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 764 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 765 | /* else data is still being written out, we will get written some time in the |
| 766 | * future */ |
| 767 | return -1; |
| 768 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 769 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 770 | int ssl3_dispatch_alert(SSL *s) { |
| 771 | int i, j; |
| 772 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 773 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 774 | s->s3->alert_dispatch = 0; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 775 | i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 776 | if (i <= 0) { |
| 777 | s->s3->alert_dispatch = 1; |
| 778 | } else { |
| 779 | /* Alert sent to BIO. If it is important, flush it now. If the message |
| 780 | * does not get sent due to non-blocking IO, we will not worry too much. */ |
| 781 | if (s->s3->send_alert[0] == SSL3_AL_FATAL) { |
| 782 | BIO_flush(s->wbio); |
| 783 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 784 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 785 | if (s->msg_callback) { |
| 786 | s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s, |
| 787 | s->msg_callback_arg); |
| 788 | } |
| 789 | |
| 790 | if (s->info_callback != NULL) { |
| 791 | cb = s->info_callback; |
| 792 | } else if (s->ctx->info_callback != NULL) { |
| 793 | cb = s->ctx->info_callback; |
| 794 | } |
| 795 | |
| 796 | if (cb != NULL) { |
| 797 | j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]; |
| 798 | cb(s, SSL_CB_WRITE_ALERT, j); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | return i; |
| 803 | } |