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> |
| 110 | #include <errno.h> |
Adam Langley | 87750b4 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 111 | #include <limits.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 112 | #include <stdio.h> |
| 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 | |
| 120 | #include "ssl_locl.h" |
| 121 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 122 | |
| 123 | static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len, |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 124 | char fragment); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 125 | static int ssl3_get_record(SSL *s); |
| 126 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 127 | int ssl3_read_n(SSL *s, int n, int max, int extend) { |
| 128 | /* If |extend| is 0, obtain new n-byte packet; |
| 129 | * if |extend| is 1, increase packet by another n bytes. |
| 130 | * |
| 131 | * The packet will be in the sub-array of |s->s3->rbuf.buf| specified by |
| 132 | * |s->packet| and |s->packet_length|. (If |s->read_ahead| is set, |max| |
| 133 | * bytes may be stored in |rbuf| (plus |s->packet_length| bytes if |extend| |
| 134 | * is one.) */ |
| 135 | int i, len, left; |
| 136 | long align = 0; |
| 137 | uint8_t *pkt; |
| 138 | SSL3_BUFFER *rb; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 139 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 140 | if (n <= 0) { |
| 141 | return n; |
| 142 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 144 | rb = &s->s3->rbuf; |
| 145 | if (rb->buf == NULL && !ssl3_setup_read_buffer(s)) { |
| 146 | return -1; |
| 147 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 148 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 149 | left = rb->left; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 151 | align = (long)rb->buf + SSL3_RT_HEADER_LENGTH; |
| 152 | align = (-align) & (SSL3_ALIGN_PAYLOAD - 1); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 154 | if (!extend) { |
| 155 | /* start with empty packet ... */ |
| 156 | if (left == 0) { |
| 157 | rb->offset = align; |
| 158 | } else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) { |
| 159 | /* check if next packet length is large enough to justify payload |
| 160 | * alignment... */ |
| 161 | pkt = rb->buf + rb->offset; |
| 162 | if (pkt[0] == SSL3_RT_APPLICATION_DATA && (pkt[3] << 8 | pkt[4]) >= 128) { |
| 163 | /* Note that even if packet is corrupted and its length field is |
| 164 | * insane, we can only be led to wrong decision about whether memmove |
| 165 | * will occur or not. Header values has no effect on memmove arguments |
| 166 | * and therefore no buffer overrun can be triggered. */ |
| 167 | memmove(rb->buf + align, pkt, left); |
| 168 | rb->offset = align; |
| 169 | } |
| 170 | } |
| 171 | s->packet = rb->buf + rb->offset; |
| 172 | s->packet_length = 0; |
| 173 | /* ... now we can act as if 'extend' was set */ |
| 174 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 175 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 176 | /* For DTLS/UDP reads should not span multiple packets because the read |
| 177 | * operation returns the whole packet at once (as long as it fits into the |
| 178 | * buffer). */ |
| 179 | if (SSL_IS_DTLS(s) && left > 0 && n > left) { |
| 180 | n = left; |
| 181 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 182 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 183 | /* if there is enough in the buffer from a previous read, take some */ |
| 184 | if (left >= n) { |
| 185 | s->packet_length += n; |
| 186 | rb->left = left - n; |
| 187 | rb->offset += n; |
| 188 | return n; |
| 189 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 190 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 191 | /* else we need to read more data */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 192 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 193 | len = s->packet_length; |
| 194 | pkt = rb->buf + align; |
| 195 | /* Move any available bytes to front of buffer: |len| bytes already pointed |
| 196 | * to by |packet|, |left| extra ones at the end. */ |
| 197 | if (s->packet != pkt) { |
| 198 | /* len > 0 */ |
| 199 | memmove(pkt, s->packet, len + left); |
| 200 | s->packet = pkt; |
| 201 | rb->offset = len + align; |
| 202 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | |
Adam Langley | c4482d6 | 2015-03-09 17:49:31 -0700 | [diff] [blame] | 204 | if (n > (int)(rb->len - rb->offset)) { |
| 205 | OPENSSL_PUT_ERROR(SSL, ssl3_read_n, ERR_R_INTERNAL_ERROR); |
| 206 | return -1; |
| 207 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 208 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 209 | if (!s->read_ahead) { |
| 210 | /* ignore max parameter */ |
| 211 | max = n; |
| 212 | } else { |
| 213 | if (max < n) { |
| 214 | max = n; |
| 215 | } |
| 216 | if (max > (int)(rb->len - rb->offset)) { |
| 217 | max = rb->len - rb->offset; |
| 218 | } |
| 219 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 220 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 221 | while (left < n) { |
| 222 | /* Now we have len+left bytes at the front of s->s3->rbuf.buf and need to |
| 223 | * read in more until we have len+n (up to len+max if possible). */ |
| 224 | ERR_clear_system_error(); |
| 225 | if (s->rbio != NULL) { |
| 226 | s->rwstate = SSL_READING; |
| 227 | i = BIO_read(s->rbio, pkt + len + left, max - left); |
| 228 | } else { |
| 229 | OPENSSL_PUT_ERROR(SSL, ssl3_read_n, SSL_R_READ_BIO_NOT_SET); |
| 230 | i = -1; |
| 231 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 232 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 233 | if (i <= 0) { |
| 234 | rb->left = left; |
| 235 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s) && |
| 236 | len + left == 0) { |
| 237 | ssl3_release_read_buffer(s); |
| 238 | } |
| 239 | return i; |
| 240 | } |
| 241 | left += i; |
| 242 | /* reads should *never* span multiple packets for DTLS because the |
| 243 | * underlying transport protocol is message oriented as opposed to byte |
| 244 | * oriented as in the TLS case. */ |
| 245 | if (SSL_IS_DTLS(s) && n > left) { |
| 246 | n = left; /* makes the while condition false */ |
| 247 | } |
| 248 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 249 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 250 | /* done reading, now the book-keeping */ |
| 251 | rb->offset += n; |
| 252 | rb->left = left - n; |
| 253 | s->packet_length += n; |
| 254 | s->rwstate = SSL_NOTHING; |
| 255 | |
| 256 | return n; |
| 257 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 258 | |
Adam Langley | 48105fa | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 259 | /* MAX_EMPTY_RECORDS defines the number of consecutive, empty records that will |
| 260 | * be processed per call to ssl3_get_record. Without this limit an attacker |
| 261 | * could send empty records at a faster rate than we can process and cause |
| 262 | * ssl3_get_record to loop forever. */ |
| 263 | #define MAX_EMPTY_RECORDS 32 |
| 264 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 265 | /* Call this to get a new input record. It will return <= 0 if more data is |
| 266 | * needed, normally due to an error or non-blocking IO. When it finishes, one |
| 267 | * packet has been decoded and can be found in |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 268 | * ssl->s3->rrec.type - is the type of record |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 269 | * ssl->s3->rrec.data - data |
| 270 | * ssl->s3->rrec.length - number of bytes */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 271 | /* used only by ssl3_read_bytes */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 272 | static int ssl3_get_record(SSL *s) { |
| 273 | int ssl_major, ssl_minor, al; |
David Benjamin | 1e52eca | 2015-01-22 15:33:51 -0500 | [diff] [blame] | 274 | int n, i, ret = -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 275 | SSL3_RECORD *rr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 276 | uint8_t *p; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 277 | short version; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 278 | size_t extra; |
| 279 | unsigned empty_record_count = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 280 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 281 | rr = &s->s3->rrec; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 282 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 283 | if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER) { |
| 284 | extra = SSL3_RT_MAX_EXTRA; |
| 285 | } else { |
| 286 | extra = 0; |
| 287 | } |
| 288 | |
| 289 | if (extra && !s->s3->init_extra) { |
| 290 | /* An application error: SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER set after |
| 291 | * ssl3_setup_buffers() was done */ |
| 292 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, ERR_R_INTERNAL_ERROR); |
| 293 | return -1; |
| 294 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 295 | |
| 296 | again: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 297 | /* check if we have the header */ |
| 298 | if (s->rstate != SSL_ST_READ_BODY || |
| 299 | s->packet_length < SSL3_RT_HEADER_LENGTH) { |
| 300 | n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, s->s3->rbuf.len, 0); |
| 301 | if (n <= 0) { |
| 302 | return n; /* error or non-blocking */ |
| 303 | } |
| 304 | s->rstate = SSL_ST_READ_BODY; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 305 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 306 | p = s->packet; |
| 307 | if (s->msg_callback) { |
| 308 | s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg); |
| 309 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 310 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 311 | /* Pull apart the header into the SSL3_RECORD */ |
| 312 | rr->type = *(p++); |
| 313 | ssl_major = *(p++); |
| 314 | ssl_minor = *(p++); |
| 315 | version = (ssl_major << 8) | ssl_minor; |
| 316 | n2s(p, rr->length); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 317 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 318 | if (s->s3->have_version && version != s->version) { |
| 319 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 320 | al = SSL_AD_PROTOCOL_VERSION; |
| 321 | goto f_err; |
| 322 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 323 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 324 | if ((version >> 8) != SSL3_VERSION_MAJOR) { |
| 325 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER); |
| 326 | goto err; |
| 327 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 328 | |
David Benjamin | 4a3f073 | 2015-04-05 02:21:33 -0400 | [diff] [blame] | 329 | if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 330 | al = SSL_AD_RECORD_OVERFLOW; |
David Benjamin | 4a3f073 | 2015-04-05 02:21:33 -0400 | [diff] [blame] | 331 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_ENCRYPTED_LENGTH_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 332 | goto f_err; |
| 333 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 334 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 335 | /* now s->rstate == SSL_ST_READ_BODY */ |
| 336 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 338 | /* s->rstate == SSL_ST_READ_BODY, get and decode the data */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 339 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 340 | if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) { |
| 341 | /* now s->packet_length == SSL3_RT_HEADER_LENGTH */ |
| 342 | i = rr->length; |
| 343 | n = ssl3_read_n(s, i, i, 1); |
| 344 | if (n <= 0) { |
| 345 | /* Error or non-blocking IO. Now |n| == |rr->length|, and |
| 346 | * |s->packet_length| == |SSL3_RT_HEADER_LENGTH| + |rr->length|. */ |
| 347 | return n; |
| 348 | } |
| 349 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 350 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 351 | s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 352 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 353 | /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, and |
| 354 | * we have that many bytes in s->packet. */ |
| 355 | rr->input = &s->packet[SSL3_RT_HEADER_LENGTH]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 356 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 357 | /* ok, we can now read from |s->packet| data into |rr|. |rr->input| points at |
| 358 | * |rr->length| bytes, which need to be copied into |rr->data| by decryption. |
| 359 | * When the data is 'copied' into the |rr->data| buffer, |rr->input| will be |
| 360 | * pointed at the new buffer. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 361 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 362 | /* We now have - encrypted [ MAC [ compressed [ plain ] ] ] |
| 363 | * rr->length bytes of encrypted compressed stuff. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 364 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 365 | /* decrypt in place in 'rr->input' */ |
| 366 | rr->data = rr->input; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 367 | |
David Benjamin | 1e52eca | 2015-01-22 15:33:51 -0500 | [diff] [blame] | 368 | if (!s->enc_method->enc(s, 0)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 369 | al = SSL_AD_BAD_RECORD_MAC; |
| 370 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, |
| 371 | SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); |
| 372 | goto f_err; |
| 373 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 374 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 375 | if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH + extra) { |
| 376 | al = SSL_AD_RECORD_OVERFLOW; |
| 377 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_DATA_LENGTH_TOO_LONG); |
| 378 | goto f_err; |
| 379 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 380 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 381 | rr->off = 0; |
| 382 | /* So at this point the following is true: |
| 383 | * ssl->s3->rrec.type is the type of record; |
| 384 | * ssl->s3->rrec.length is the number of bytes in the record; |
| 385 | * ssl->s3->rrec.off is the offset to first valid byte; |
| 386 | * ssl->s3->rrec.data is where to take bytes from (increment after use). */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 387 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 388 | /* we have pulled in a full packet so zero things */ |
| 389 | s->packet_length = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 390 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 391 | /* just read a 0 length packet */ |
| 392 | if (rr->length == 0) { |
| 393 | empty_record_count++; |
| 394 | if (empty_record_count > MAX_EMPTY_RECORDS) { |
| 395 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 396 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_TOO_MANY_EMPTY_FRAGMENTS); |
| 397 | goto f_err; |
| 398 | } |
| 399 | goto again; |
| 400 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 401 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 402 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 403 | |
| 404 | f_err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 405 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 406 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 407 | return ret; |
| 408 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 409 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 410 | /* Call this to write data in records of type |type|. It will return <= 0 if |
| 411 | * not all data has been sent or non-blocking IO. */ |
| 412 | int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) { |
| 413 | const uint8_t *buf = buf_; |
| 414 | unsigned int tot, n, nw; |
| 415 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 416 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 417 | s->rwstate = SSL_NOTHING; |
| 418 | assert(s->s3->wnum <= INT_MAX); |
| 419 | tot = s->s3->wnum; |
| 420 | s->s3->wnum = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 421 | |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 422 | 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] | 423 | i = s->handshake_func(s); |
| 424 | if (i < 0) { |
| 425 | return i; |
| 426 | } |
| 427 | if (i == 0) { |
| 428 | OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 429 | return -1; |
| 430 | } |
| 431 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 432 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 433 | /* Ensure that if we end up with a smaller value of data to write out than |
| 434 | * the the original len from a write which didn't complete for non-blocking |
| 435 | * I/O and also somehow ended up avoiding the check for this in |
| 436 | * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to |
| 437 | * end up with (len-tot) as a large number that will then promptly send |
| 438 | * beyond the end of the users buffer ... so we trap and report the error in |
| 439 | * a way the user will notice. */ |
| 440 | if (len < 0 || (size_t)len < tot) { |
| 441 | OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_BAD_LENGTH); |
| 442 | return -1; |
| 443 | } |
Adam Langley | 9611cfc | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 444 | |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 445 | int record_split_done = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 446 | n = (len - tot); |
| 447 | for (;;) { |
| 448 | /* max contains the maximum number of bytes that we can put into a |
| 449 | * record. */ |
| 450 | unsigned max = s->max_send_fragment; |
| 451 | /* fragment is true if do_ssl3_write should send the first byte in its own |
| 452 | * record in order to randomise a CBC IV. */ |
| 453 | int fragment = 0; |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 454 | if (!record_split_done && s->s3->need_record_splitting && |
| 455 | type == SSL3_RT_APPLICATION_DATA) { |
| 456 | /* Only the the first record per write call needs to be split. The |
| 457 | * remaining plaintext was determined before the IV was randomized. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 458 | fragment = 1; |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 459 | record_split_done = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 460 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 461 | if (n > max) { |
| 462 | nw = max; |
| 463 | } else { |
| 464 | nw = n; |
| 465 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 467 | i = do_ssl3_write(s, type, &buf[tot], nw, fragment); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 468 | if (i <= 0) { |
| 469 | s->s3->wnum = tot; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 470 | return i; |
| 471 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 472 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 473 | if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA && |
| 474 | (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 475 | return tot + i; |
| 476 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 477 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 478 | n -= i; |
| 479 | tot += i; |
| 480 | } |
| 481 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 482 | |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 483 | /* ssl3_seal_record seals a new record of type |type| and plaintext |in| and |
| 484 | * writes it to |out|. At most |max_out| bytes will be written. It returns one |
| 485 | * on success and zero on error. On success, |s->s3->wrec| is updated to include |
| 486 | * the new record. */ |
| 487 | static int ssl3_seal_record(SSL *s, uint8_t *out, size_t *out_len, |
| 488 | size_t max_out, uint8_t type, const uint8_t *in, |
| 489 | size_t in_len) { |
| 490 | if (max_out < SSL3_RT_HEADER_LENGTH) { |
| 491 | OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL); |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | out[0] = type; |
| 496 | |
| 497 | /* Some servers hang if initial ClientHello is larger than 256 bytes and |
| 498 | * record version number > TLS 1.0. */ |
| 499 | if (!s->s3->have_version && s->version > SSL3_VERSION) { |
| 500 | out[1] = TLS1_VERSION >> 8; |
| 501 | out[2] = TLS1_VERSION & 0xff; |
| 502 | } else { |
| 503 | out[1] = s->version >> 8; |
| 504 | out[2] = s->version & 0xff; |
| 505 | } |
| 506 | |
| 507 | size_t explicit_nonce_len = 0; |
| 508 | if (s->aead_write_ctx != NULL && |
| 509 | s->aead_write_ctx->variable_nonce_included_in_record) { |
| 510 | explicit_nonce_len = s->aead_write_ctx->variable_nonce_len; |
| 511 | } |
| 512 | size_t max_overhead = 0; |
| 513 | if (s->aead_write_ctx != NULL) { |
| 514 | max_overhead = s->aead_write_ctx->tag_len; |
| 515 | } |
| 516 | |
| 517 | /* Assemble the input for |s->enc_method->enc|. The input is the plaintext |
| 518 | * with |explicit_nonce_len| bytes of space prepended for the explicit |
| 519 | * nonce. The input is copied into |out| and then encrypted in-place to take |
| 520 | * advantage of alignment. |
| 521 | * |
| 522 | * TODO(davidben): |tls1_enc| should accept its inputs and outputs directly |
| 523 | * rather than looking up in |wrec| and friends. The |max_overhead| bounds |
| 524 | * check would also be unnecessary if |max_out| were passed down. */ |
| 525 | SSL3_RECORD *wr = &s->s3->wrec; |
| 526 | size_t plaintext_len = in_len + explicit_nonce_len; |
| 527 | if (plaintext_len < in_len || plaintext_len > INT_MAX || |
| 528 | plaintext_len + max_overhead < plaintext_len) { |
| 529 | OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW); |
| 530 | return 0; |
| 531 | } |
| 532 | if (max_out - SSL3_RT_HEADER_LENGTH < plaintext_len + max_overhead) { |
| 533 | OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL); |
| 534 | return 0; |
| 535 | } |
| 536 | wr->type = type; |
| 537 | wr->input = out + SSL3_RT_HEADER_LENGTH; |
| 538 | wr->data = wr->input; |
| 539 | wr->length = plaintext_len; |
| 540 | memcpy(wr->input + explicit_nonce_len, in, in_len); |
| 541 | |
| 542 | if (!s->enc_method->enc(s, 1)) { |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | /* |wr->length| has now been set to the ciphertext length. */ |
| 547 | if (wr->length >= 1 << 16) { |
| 548 | OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW); |
| 549 | return 0; |
| 550 | } |
| 551 | out[3] = wr->length >> 8; |
| 552 | out[4] = wr->length & 0xff; |
| 553 | *out_len = SSL3_RT_HEADER_LENGTH + (size_t)wr->length; |
| 554 | |
| 555 | if (s->msg_callback) { |
| 556 | s->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out, SSL3_RT_HEADER_LENGTH, |
| 557 | s, s->msg_callback_arg); |
| 558 | } |
| 559 | |
| 560 | return 1; |
| 561 | } |
| 562 | |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 563 | /* do_ssl3_write writes an SSL record of the given type. If |fragment| is 1 |
| 564 | * then it splits the record into a one byte record and a record with the rest |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 565 | * of the data in order to randomise a CBC IV. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 566 | static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len, |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 567 | char fragment) { |
| 568 | SSL3_BUFFER *wb = &s->s3->wbuf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 569 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 570 | /* first check if there is a SSL3_BUFFER still being written out. This will |
| 571 | * happen with non blocking IO */ |
| 572 | if (wb->left != 0) { |
| 573 | return ssl3_write_pending(s, type, buf, len); |
| 574 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 575 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 576 | /* If we have an alert to send, lets send it */ |
| 577 | if (s->s3->alert_dispatch) { |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 578 | int ret = s->method->ssl_dispatch_alert(s); |
| 579 | if (ret <= 0) { |
| 580 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 581 | } |
| 582 | /* if it went, fall through and send more stuff */ |
| 583 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 584 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 585 | if (wb->buf == NULL && !ssl3_setup_write_buffer(s)) { |
| 586 | return -1; |
| 587 | } |
Adam Langley | c6c8ae8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 588 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 589 | if (len == 0) { |
| 590 | return 0; |
| 591 | } |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 592 | if (len == 1) { |
| 593 | /* No sense in fragmenting a one-byte record. */ |
| 594 | fragment = 0; |
| 595 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 596 | |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 597 | /* Align the output so the ciphertext is aligned to |SSL3_ALIGN_PAYLOAD|. */ |
| 598 | uintptr_t align; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 599 | if (fragment) { |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 600 | /* Only CBC-mode ciphers require fragmenting. CBC-mode ciphertext is a |
| 601 | * multiple of the block size which we may assume is aligned. Thus we only |
| 602 | * need to account for a second copy of the record header. */ |
| 603 | align = (uintptr_t)wb->buf + 2 * SSL3_RT_HEADER_LENGTH; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 604 | } else { |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 605 | align = (uintptr_t)wb->buf + SSL3_RT_HEADER_LENGTH; |
| 606 | } |
| 607 | align = (-align) & (SSL3_ALIGN_PAYLOAD - 1); |
| 608 | uint8_t *out = wb->buf + align; |
| 609 | wb->offset = align; |
| 610 | size_t max_out = wb->len - wb->offset; |
| 611 | |
| 612 | const uint8_t *orig_buf = buf; |
| 613 | unsigned int orig_len = len; |
| 614 | size_t fragment_len = 0; |
| 615 | if (fragment) { |
| 616 | /* Write the first byte in its own record as a countermeasure against |
| 617 | * known-IV weaknesses in CBC ciphersuites. (See |
| 618 | * http://www.openssl.org/~bodo/tls-cbc.txt.) */ |
| 619 | if (!ssl3_seal_record(s, out, &fragment_len, max_out, type, buf, 1)) { |
| 620 | return -1; |
| 621 | } |
| 622 | out += fragment_len; |
| 623 | max_out -= fragment_len; |
| 624 | buf++; |
| 625 | len--; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 626 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 627 | |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 628 | assert((((uintptr_t)out + SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1)) |
| 629 | == 0); |
| 630 | size_t ciphertext_len; |
| 631 | if (!ssl3_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) { |
| 632 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 633 | } |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 634 | ciphertext_len += fragment_len; |
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 | /* now let's set up wb */ |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 637 | wb->left = ciphertext_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 638 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 639 | /* memorize arguments so that ssl3_write_pending can detect bad write retries |
| 640 | * later */ |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 641 | s->s3->wpend_tot = orig_len; |
| 642 | s->s3->wpend_buf = orig_buf; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 643 | s->s3->wpend_type = type; |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 644 | s->s3->wpend_ret = orig_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 645 | |
| 646 | /* we now just need to write the buffer */ |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 647 | return ssl3_write_pending(s, type, orig_buf, orig_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 648 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 649 | |
| 650 | /* if s->s3->wbuf.left != 0, we need to call this */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 651 | int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) { |
| 652 | int i; |
| 653 | SSL3_BUFFER *wb = &(s->s3->wbuf); |
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->s3->wpend_tot > (int)len || |
| 656 | (s->s3->wpend_buf != buf && |
| 657 | !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) || |
| 658 | s->s3->wpend_type != type) { |
| 659 | OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BAD_WRITE_RETRY); |
| 660 | return -1; |
| 661 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 662 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 663 | for (;;) { |
| 664 | ERR_clear_system_error(); |
| 665 | if (s->wbio != NULL) { |
| 666 | s->rwstate = SSL_WRITING; |
| 667 | i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]), |
| 668 | (unsigned int)wb->left); |
| 669 | } else { |
| 670 | OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BIO_NOT_SET); |
| 671 | i = -1; |
| 672 | } |
| 673 | if (i == wb->left) { |
| 674 | wb->left = 0; |
| 675 | wb->offset += i; |
| 676 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s)) { |
| 677 | ssl3_release_write_buffer(s); |
| 678 | } |
| 679 | s->rwstate = SSL_NOTHING; |
| 680 | return s->s3->wpend_ret; |
| 681 | } else if (i <= 0) { |
| 682 | if (SSL_IS_DTLS(s)) { |
| 683 | /* For DTLS, just drop it. That's kind of the whole |
| 684 | point in using a datagram service */ |
| 685 | wb->left = 0; |
| 686 | } |
| 687 | return i; |
| 688 | } |
| 689 | wb->offset += i; |
| 690 | wb->left -= i; |
| 691 | } |
| 692 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 693 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 694 | /* ssl3_expect_change_cipher_spec informs the record layer that a |
| 695 | * ChangeCipherSpec record is required at this point. If a Handshake record is |
| 696 | * received before ChangeCipherSpec, the connection will fail. Moreover, if |
| 697 | * there are unprocessed handshake bytes, the handshake will also fail and the |
| 698 | * function returns zero. Otherwise, the function returns one. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 699 | int ssl3_expect_change_cipher_spec(SSL *s) { |
| 700 | if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) { |
| 701 | OPENSSL_PUT_ERROR(SSL, ssl3_expect_change_cipher_spec, |
| 702 | SSL_R_UNPROCESSED_HANDSHAKE_DATA); |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | s->s3->flags |= SSL3_FLAGS_EXPECT_CCS; |
| 707 | return 1; |
| 708 | } |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 709 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 710 | /* Return up to 'len' payload bytes received in 'type' records. |
| 711 | * 'type' is one of the following: |
| 712 | * |
| 713 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) |
| 714 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) |
| 715 | * - 0 (during a shutdown, no data has to be returned) |
| 716 | * |
| 717 | * If we don't have stored data to work from, read a SSL/TLS record first |
| 718 | * (possibly multiple records if we still don't have anything to return). |
| 719 | * |
| 720 | * This function must handle any surprises the peer may have for us, such as |
| 721 | * Alert records (e.g. close_notify), ChangeCipherSpec records (not really |
| 722 | * a surprise, but handled as if it were), or renegotiation requests. |
| 723 | * Also if record payloads contain fragments too small to process, we store |
| 724 | * them until there is enough for the respective protocol (the record protocol |
| 725 | * may use arbitrary fragmentation and even interleaving): |
| 726 | * Change cipher spec protocol |
| 727 | * just 1 byte needed, no need for keeping anything stored |
| 728 | * Alert protocol |
| 729 | * 2 bytes needed (AlertLevel, AlertDescription) |
| 730 | * Handshake protocol |
| 731 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have |
| 732 | * to detect unexpected Client Hello and Hello Request messages |
| 733 | * here, anything else is handled by higher layers |
| 734 | * Application data protocol |
| 735 | * none of our business |
| 736 | */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 737 | 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] | 738 | int al, i, ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 739 | unsigned int n; |
| 740 | SSL3_RECORD *rr; |
| 741 | void (*cb)(const SSL *ssl, int type2, int val) = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 742 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 743 | if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) || |
| 744 | (peek && type != SSL3_RT_APPLICATION_DATA)) { |
| 745 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR); |
| 746 | return -1; |
| 747 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 748 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 749 | if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) { |
| 750 | /* (partially) satisfy request from storage */ |
| 751 | uint8_t *src = s->s3->handshake_fragment; |
| 752 | uint8_t *dst = buf; |
| 753 | unsigned int k; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 754 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 755 | /* peek == 0 */ |
| 756 | n = 0; |
| 757 | while (len > 0 && s->s3->handshake_fragment_len > 0) { |
| 758 | *dst++ = *src++; |
| 759 | len--; |
| 760 | s->s3->handshake_fragment_len--; |
| 761 | n++; |
| 762 | } |
| 763 | /* move any remaining fragment bytes: */ |
| 764 | for (k = 0; k < s->s3->handshake_fragment_len; k++) { |
| 765 | s->s3->handshake_fragment[k] = *src++; |
| 766 | } |
| 767 | return n; |
| 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 | /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 771 | |
David Benjamin | 931ab34 | 2015-02-08 19:46:57 -0500 | [diff] [blame] | 772 | /* This may require multiple iterations. False Start will cause |
| 773 | * |s->handshake_func| to signal success one step early, but the handshake |
| 774 | * must be completely finished before other modes are accepted. |
| 775 | * |
| 776 | * TODO(davidben): Move this check up to a higher level. */ |
| 777 | while (!s->in_handshake && SSL_in_init(s)) { |
| 778 | assert(type == SSL3_RT_APPLICATION_DATA); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 779 | i = s->handshake_func(s); |
| 780 | if (i < 0) { |
| 781 | return i; |
| 782 | } |
| 783 | if (i == 0) { |
| 784 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 785 | return -1; |
| 786 | } |
| 787 | } |
| 788 | |
David Benjamin | e0e7d0d | 2015-02-08 19:33:25 -0500 | [diff] [blame] | 789 | if (s->s3->rbuf.buf == NULL && !ssl3_setup_read_buffer(s)) { |
| 790 | /* TODO(davidben): Is this redundant with the calls in the handshake? */ |
| 791 | return -1; |
| 792 | } |
| 793 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 794 | start: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 795 | s->rwstate = SSL_NOTHING; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 796 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 797 | /* s->s3->rrec.type - is the type of record |
| 798 | * s->s3->rrec.data - data |
| 799 | * s->s3->rrec.off - offset into 'data' for next read |
| 800 | * s->s3->rrec.length - number of bytes. */ |
| 801 | rr = &s->s3->rrec; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 802 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 803 | /* get new packet if necessary */ |
| 804 | if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) { |
| 805 | ret = ssl3_get_record(s); |
| 806 | if (ret <= 0) { |
| 807 | return ret; |
| 808 | } |
| 809 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 810 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 811 | /* we now have a packet which can be read and processed */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 812 | |
David Benjamin | dc3da93 | 2015-03-12 15:09:02 -0400 | [diff] [blame] | 813 | /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by |
| 814 | * ssl3_get_finished. */ |
| 815 | if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE && |
| 816 | rr->type != SSL3_RT_ALERT) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 817 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 818 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, |
| 819 | SSL_R_DATA_BETWEEN_CCS_AND_FINISHED); |
| 820 | goto f_err; |
| 821 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 822 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 823 | /* If we are expecting a ChangeCipherSpec, it is illegal to receive a |
| 824 | * Handshake record. */ |
| 825 | if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) { |
| 826 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 827 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS); |
| 828 | goto f_err; |
| 829 | } |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 830 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 831 | /* If the other end has shut down, throw anything we read away (even in |
| 832 | * 'peek' mode) */ |
| 833 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { |
| 834 | rr->length = 0; |
| 835 | s->rwstate = SSL_NOTHING; |
| 836 | return 0; |
| 837 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 838 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 839 | if (type == rr->type) { |
| 840 | /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */ |
| 841 | /* make sure that we are not getting application data when we are doing a |
| 842 | * handshake for the first time */ |
| 843 | if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA && |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 844 | s->aead_read_ctx == NULL) { |
| 845 | /* TODO(davidben): Is this check redundant with the handshake_func |
| 846 | * check? */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 847 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 848 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE); |
| 849 | goto f_err; |
| 850 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 851 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 852 | if (len <= 0) { |
| 853 | return len; |
| 854 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 855 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 856 | if ((unsigned int)len > rr->length) { |
| 857 | n = rr->length; |
| 858 | } else { |
| 859 | n = (unsigned int)len; |
| 860 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 861 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 862 | memcpy(buf, &(rr->data[rr->off]), n); |
| 863 | if (!peek) { |
| 864 | rr->length -= n; |
| 865 | rr->off += n; |
| 866 | if (rr->length == 0) { |
| 867 | s->rstate = SSL_ST_READ_HEADER; |
| 868 | rr->off = 0; |
| 869 | if (s->mode & SSL_MODE_RELEASE_BUFFERS && s->s3->rbuf.left == 0) { |
| 870 | ssl3_release_read_buffer(s); |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | return n; |
| 876 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 877 | |
| 878 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 879 | /* If we get here, then type != rr->type; if we have a handshake message, |
| 880 | * then it was unexpected (Hello Request or Client Hello). */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 881 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 882 | /* In case of record types for which we have 'fragment' storage, fill that so |
| 883 | * that we can process the data at a fixed place. */ |
Alex Chernyakhovsky | 4cd8c43 | 2014-11-01 19:39:08 -0400 | [diff] [blame] | 884 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 885 | if (rr->type == SSL3_RT_HANDSHAKE) { |
| 886 | const size_t size = sizeof(s->s3->handshake_fragment); |
| 887 | const size_t avail = size - s->s3->handshake_fragment_len; |
| 888 | const size_t todo = (rr->length < avail) ? rr->length : avail; |
| 889 | memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len, |
| 890 | &rr->data[rr->off], todo); |
| 891 | rr->off += todo; |
| 892 | rr->length -= todo; |
| 893 | s->s3->handshake_fragment_len += todo; |
| 894 | if (s->s3->handshake_fragment_len < size) { |
| 895 | goto start; /* fragment was too small */ |
| 896 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 897 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 898 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 899 | /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE; |
| 900 | * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 901 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 902 | /* If we are a client, check for an incoming 'Hello Request': */ |
| 903 | if (!s->server && s->s3->handshake_fragment_len >= 4 && |
| 904 | s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST && |
| 905 | s->session != NULL && s->session->cipher != NULL) { |
| 906 | s->s3->handshake_fragment_len = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 907 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 908 | if (s->s3->handshake_fragment[1] != 0 || |
| 909 | s->s3->handshake_fragment[2] != 0 || |
| 910 | s->s3->handshake_fragment[3] != 0) { |
| 911 | al = SSL_AD_DECODE_ERROR; |
| 912 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST); |
| 913 | goto f_err; |
| 914 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 915 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 916 | if (s->msg_callback) { |
| 917 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, |
| 918 | s->s3->handshake_fragment, 4, s, s->msg_callback_arg); |
| 919 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 920 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 921 | if (SSL_is_init_finished(s) && !s->s3->renegotiate) { |
| 922 | ssl3_renegotiate(s); |
| 923 | if (ssl3_renegotiate_check(s)) { |
| 924 | i = s->handshake_func(s); |
| 925 | if (i < 0) { |
| 926 | return i; |
| 927 | } |
| 928 | if (i == 0) { |
| 929 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 930 | return -1; |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | /* we either finished a handshake or ignored the request, now try again to |
| 935 | * obtain the (application) data we were asked for */ |
| 936 | goto start; |
| 937 | } |
David Benjamin | b4188f0 | 2014-11-01 03:43:48 -0400 | [diff] [blame] | 938 | |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 939 | /* If an alert record, process one alert out of the record. Note that we allow |
| 940 | * a single record to contain multiple alerts. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 941 | if (rr->type == SSL3_RT_ALERT) { |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 942 | /* Alerts may not be fragmented. */ |
| 943 | if (rr->length < 2) { |
| 944 | al = SSL_AD_DECODE_ERROR; |
| 945 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT); |
| 946 | goto f_err; |
| 947 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 948 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 949 | if (s->msg_callback) { |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 950 | 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] | 951 | s->msg_callback_arg); |
| 952 | } |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 953 | const uint8_t alert_level = rr->data[rr->off++]; |
| 954 | const uint8_t alert_descr = rr->data[rr->off++]; |
| 955 | rr->length -= 2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 956 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 957 | if (s->info_callback != NULL) { |
| 958 | cb = s->info_callback; |
| 959 | } else if (s->ctx->info_callback != NULL) { |
| 960 | cb = s->ctx->info_callback; |
| 961 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 962 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 963 | if (cb != NULL) { |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 964 | uint16_t alert = (alert_level << 8) | alert_descr; |
| 965 | cb(s, SSL_CB_READ_ALERT, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 966 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 967 | |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 968 | if (alert_level == SSL3_AL_WARNING) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 969 | s->s3->warn_alert = alert_descr; |
| 970 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) { |
| 971 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
| 972 | return 0; |
| 973 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 974 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 975 | /* This is a warning but we receive it if we requested renegotiation and |
| 976 | * the peer denied it. Terminate with a fatal alert because if |
| 977 | * application tried to renegotiatie it presumably had a good reason and |
| 978 | * expects it to succeed. |
| 979 | * |
| 980 | * In future we might have a renegotiation where we don't care if the |
| 981 | * peer refused it where we carry on. */ |
| 982 | else if (alert_descr == SSL_AD_NO_RENEGOTIATION) { |
| 983 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 984 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION); |
| 985 | goto f_err; |
| 986 | } |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 987 | } else if (alert_level == SSL3_AL_FATAL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 988 | char tmp[16]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 989 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 990 | s->rwstate = SSL_NOTHING; |
| 991 | s->s3->fatal_alert = alert_descr; |
| 992 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, |
| 993 | SSL_AD_REASON_OFFSET + alert_descr); |
| 994 | BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr); |
| 995 | ERR_add_error_data(2, "SSL alert number ", tmp); |
| 996 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
| 997 | SSL_CTX_remove_session(s->ctx, s->session); |
| 998 | return 0; |
| 999 | } else { |
| 1000 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1001 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE); |
| 1002 | goto f_err; |
| 1003 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1004 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1005 | goto start; |
| 1006 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1007 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1008 | if (s->shutdown & SSL_SENT_SHUTDOWN) { |
| 1009 | /* but we have not received a shutdown */ |
| 1010 | s->rwstate = SSL_NOTHING; |
| 1011 | rr->length = 0; |
| 1012 | return 0; |
| 1013 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1014 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1015 | if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) { |
| 1016 | /* 'Change Cipher Spec' is just a single byte, so we know exactly what the |
| 1017 | * record payload has to look like */ |
| 1018 | if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) { |
| 1019 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1020 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
| 1021 | goto f_err; |
| 1022 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1023 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1024 | /* Check we have a cipher to change to */ |
| 1025 | if (s->s3->tmp.new_cipher == NULL) { |
| 1026 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 1027 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY); |
| 1028 | goto f_err; |
| 1029 | } |
Adam Langley | ce7f9ca | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1030 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1031 | if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) { |
| 1032 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 1033 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY); |
| 1034 | goto f_err; |
| 1035 | } |
Adam Langley | ce7f9ca | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1036 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1037 | s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1038 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1039 | rr->length = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1040 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1041 | if (s->msg_callback) { |
| 1042 | s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s, |
| 1043 | s->msg_callback_arg); |
| 1044 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1045 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1046 | s->s3->change_cipher_spec = 1; |
| 1047 | if (!ssl3_do_change_cipher_spec(s)) { |
| 1048 | goto err; |
| 1049 | } else { |
| 1050 | goto start; |
| 1051 | } |
| 1052 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1053 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1054 | /* Unexpected handshake message (Client Hello, or protocol violation) */ |
| 1055 | if (s->s3->handshake_fragment_len >= 4 && !s->in_handshake) { |
| 1056 | if ((s->state & SSL_ST_MASK) == SSL_ST_OK) { |
| 1057 | s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT; |
| 1058 | s->renegotiate = 1; |
| 1059 | s->new_session = 1; |
| 1060 | } |
| 1061 | i = s->handshake_func(s); |
| 1062 | if (i < 0) { |
| 1063 | return i; |
| 1064 | } |
| 1065 | if (i == 0) { |
| 1066 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1067 | return -1; |
| 1068 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1069 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1070 | goto start; |
| 1071 | } |
| 1072 | |
David Benjamin | e820df9 | 2015-02-08 16:06:54 -0500 | [diff] [blame] | 1073 | /* We already handled these. */ |
| 1074 | assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT && |
| 1075 | rr->type != SSL3_RT_HANDSHAKE); |
David Benjamin | ddb9f15 | 2015-02-03 15:44:39 -0500 | [diff] [blame] | 1076 | |
David Benjamin | e820df9 | 2015-02-08 16:06:54 -0500 | [diff] [blame] | 1077 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 1078 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1079 | |
| 1080 | f_err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1081 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1082 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1083 | return -1; |
| 1084 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1085 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1086 | int ssl3_do_change_cipher_spec(SSL *s) { |
| 1087 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1088 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1089 | if (s->state & SSL_ST_ACCEPT) { |
| 1090 | i = SSL3_CHANGE_CIPHER_SERVER_READ; |
| 1091 | } else { |
| 1092 | i = SSL3_CHANGE_CIPHER_CLIENT_READ; |
| 1093 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1094 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1095 | if (s->s3->tmp.key_block == NULL) { |
| 1096 | if (s->session == NULL || s->session->master_key_length == 0) { |
| 1097 | /* might happen if dtls1_read_bytes() calls this */ |
| 1098 | OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec, |
| 1099 | SSL_R_CCS_RECEIVED_EARLY); |
| 1100 | return 0; |
| 1101 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1102 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1103 | s->session->cipher = s->s3->tmp.new_cipher; |
| 1104 | if (!s->enc_method->setup_key_block(s)) { |
| 1105 | return 0; |
| 1106 | } |
| 1107 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1108 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1109 | if (!s->enc_method->change_cipher_state(s, i)) { |
| 1110 | return 0; |
| 1111 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1112 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1113 | return 1; |
| 1114 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1115 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1116 | int ssl3_send_alert(SSL *s, int level, int desc) { |
| 1117 | /* Map tls/ssl alert value to correct one */ |
| 1118 | desc = s->enc_method->alert_value(desc); |
| 1119 | if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) { |
| 1120 | /* SSL 3.0 does not have protocol_version alerts */ |
| 1121 | desc = SSL_AD_HANDSHAKE_FAILURE; |
| 1122 | } |
| 1123 | if (desc < 0) { |
| 1124 | return -1; |
| 1125 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1126 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1127 | /* If a fatal one, remove from cache */ |
| 1128 | if (level == 2 && s->session != NULL) { |
| 1129 | SSL_CTX_remove_session(s->ctx, s->session); |
| 1130 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1131 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1132 | s->s3->alert_dispatch = 1; |
| 1133 | s->s3->send_alert[0] = level; |
| 1134 | s->s3->send_alert[1] = desc; |
| 1135 | if (s->s3->wbuf.left == 0) { |
| 1136 | /* data is still being written out. */ |
| 1137 | return s->method->ssl_dispatch_alert(s); |
| 1138 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1139 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1140 | /* else data is still being written out, we will get written some time in the |
| 1141 | * future */ |
| 1142 | return -1; |
| 1143 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1144 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1145 | int ssl3_dispatch_alert(SSL *s) { |
| 1146 | int i, j; |
| 1147 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1148 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1149 | s->s3->alert_dispatch = 0; |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 1150 | i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1151 | if (i <= 0) { |
| 1152 | s->s3->alert_dispatch = 1; |
| 1153 | } else { |
| 1154 | /* Alert sent to BIO. If it is important, flush it now. If the message |
| 1155 | * does not get sent due to non-blocking IO, we will not worry too much. */ |
| 1156 | if (s->s3->send_alert[0] == SSL3_AL_FATAL) { |
| 1157 | BIO_flush(s->wbio); |
| 1158 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1159 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1160 | if (s->msg_callback) { |
| 1161 | s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s, |
| 1162 | s->msg_callback_arg); |
| 1163 | } |
| 1164 | |
| 1165 | if (s->info_callback != NULL) { |
| 1166 | cb = s->info_callback; |
| 1167 | } else if (s->ctx->info_callback != NULL) { |
| 1168 | cb = s->ctx->info_callback; |
| 1169 | } |
| 1170 | |
| 1171 | if (cb != NULL) { |
| 1172 | j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]; |
| 1173 | cb(s, SSL_CB_WRITE_ALERT, j); |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | return i; |
| 1178 | } |