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