Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). */ |
| 108 | |
| 109 | #include <assert.h> |
Adam Langley | 87750b4 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 110 | #include <limits.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 111 | #include <stdio.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 112 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 113 | |
| 114 | #include <openssl/buf.h> |
| 115 | #include <openssl/err.h> |
| 116 | #include <openssl/evp.h> |
| 117 | #include <openssl/mem.h> |
| 118 | #include <openssl/rand.h> |
| 119 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 120 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 122 | |
| 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 | |
David Benjamin | b1f5bca | 2015-05-08 22:54:02 -0400 | [diff] [blame] | 127 | int ssl3_read_n(SSL *s, int n, int extend) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 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 |
David Benjamin | b1f5bca | 2015-05-08 22:54:02 -0400 | [diff] [blame] | 132 | * |s->packet| and |s->packet_length|. (If |s->read_ahead| is set and |extend| |
| 133 | * is 0, additional bytes may be read into |rbuf|, up to the size of the |
| 134 | * buffer.) */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 135 | int i, len, left; |
David Benjamin | ef4962f | 2015-04-06 20:31:44 -0400 | [diff] [blame] | 136 | uintptr_t align = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 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 | |
David Benjamin | ef4962f | 2015-04-06 20:31:44 -0400 | [diff] [blame] | 151 | align = (uintptr_t)rb->buf + SSL3_RT_HEADER_LENGTH; |
| 152 | align = (0 - 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 | |
David Benjamin | b1f5bca | 2015-05-08 22:54:02 -0400 | [diff] [blame] | 209 | int max = n; |
| 210 | if (s->read_ahead && !extend) { |
| 211 | max = rb->len - rb->offset; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 212 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 213 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 214 | while (left < n) { |
| 215 | /* Now we have len+left bytes at the front of s->s3->rbuf.buf and need to |
| 216 | * read in more until we have len+n (up to len+max if possible). */ |
| 217 | ERR_clear_system_error(); |
| 218 | if (s->rbio != NULL) { |
| 219 | s->rwstate = SSL_READING; |
| 220 | i = BIO_read(s->rbio, pkt + len + left, max - left); |
| 221 | } else { |
| 222 | OPENSSL_PUT_ERROR(SSL, ssl3_read_n, SSL_R_READ_BIO_NOT_SET); |
| 223 | i = -1; |
| 224 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 225 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 226 | if (i <= 0) { |
| 227 | rb->left = left; |
David Benjamin | 9417b76 | 2015-05-08 22:30:06 -0400 | [diff] [blame] | 228 | if (len + left == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 229 | ssl3_release_read_buffer(s); |
| 230 | } |
| 231 | return i; |
| 232 | } |
| 233 | left += i; |
| 234 | /* reads should *never* span multiple packets for DTLS because the |
| 235 | * underlying transport protocol is message oriented as opposed to byte |
| 236 | * oriented as in the TLS case. */ |
| 237 | if (SSL_IS_DTLS(s) && n > left) { |
| 238 | n = left; /* makes the while condition false */ |
| 239 | } |
| 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 | /* done reading, now the book-keeping */ |
| 243 | rb->offset += n; |
| 244 | rb->left = left - n; |
| 245 | s->packet_length += n; |
| 246 | s->rwstate = SSL_NOTHING; |
| 247 | |
| 248 | return n; |
| 249 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 250 | |
Adam Langley | 48105fa | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 251 | /* MAX_EMPTY_RECORDS defines the number of consecutive, empty records that will |
| 252 | * be processed per call to ssl3_get_record. Without this limit an attacker |
| 253 | * could send empty records at a faster rate than we can process and cause |
| 254 | * ssl3_get_record to loop forever. */ |
| 255 | #define MAX_EMPTY_RECORDS 32 |
| 256 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 257 | /* Call this to get a new input record. It will return <= 0 if more data is |
| 258 | * needed, normally due to an error or non-blocking IO. When it finishes, one |
| 259 | * packet has been decoded and can be found in |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 260 | * ssl->s3->rrec.type - is the type of record |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 261 | * ssl->s3->rrec.data - data |
| 262 | * ssl->s3->rrec.length - number of bytes */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | /* used only by ssl3_read_bytes */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 264 | static int ssl3_get_record(SSL *s) { |
| 265 | int ssl_major, ssl_minor, al; |
David Benjamin | 1e52eca | 2015-01-22 15:33:51 -0500 | [diff] [blame] | 266 | int n, i, ret = -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 267 | SSL3_RECORD *rr; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 268 | uint8_t *p; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 269 | short version; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 270 | size_t extra; |
| 271 | unsigned empty_record_count = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 272 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 273 | rr = &s->s3->rrec; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 274 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 275 | again: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 276 | /* check if we have the header */ |
| 277 | if (s->rstate != SSL_ST_READ_BODY || |
| 278 | s->packet_length < SSL3_RT_HEADER_LENGTH) { |
David Benjamin | b1f5bca | 2015-05-08 22:54:02 -0400 | [diff] [blame] | 279 | n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, 0); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 280 | if (n <= 0) { |
| 281 | return n; /* error or non-blocking */ |
| 282 | } |
| 283 | s->rstate = SSL_ST_READ_BODY; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 284 | |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame^] | 285 | /* Some bytes were read, so the read buffer must be existant and |
| 286 | * |s->s3->init_extra| is defined. */ |
| 287 | assert(s->s3->rbuf.buf != NULL); |
| 288 | extra = s->s3->init_extra ? SSL3_RT_MAX_EXTRA : 0; |
| 289 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 290 | p = s->packet; |
| 291 | if (s->msg_callback) { |
| 292 | s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg); |
| 293 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 294 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 295 | /* Pull apart the header into the SSL3_RECORD */ |
| 296 | rr->type = *(p++); |
| 297 | ssl_major = *(p++); |
| 298 | ssl_minor = *(p++); |
| 299 | version = (ssl_major << 8) | ssl_minor; |
| 300 | n2s(p, rr->length); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 301 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 302 | if (s->s3->have_version && version != s->version) { |
| 303 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 304 | al = SSL_AD_PROTOCOL_VERSION; |
| 305 | goto f_err; |
| 306 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 307 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 308 | if ((version >> 8) != SSL3_VERSION_MAJOR) { |
| 309 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER); |
| 310 | goto err; |
| 311 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 312 | |
David Benjamin | 4a3f073 | 2015-04-05 02:21:33 -0400 | [diff] [blame] | 313 | if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 314 | al = SSL_AD_RECORD_OVERFLOW; |
David Benjamin | 4a3f073 | 2015-04-05 02:21:33 -0400 | [diff] [blame] | 315 | 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] | 316 | goto f_err; |
| 317 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 318 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 319 | /* now s->rstate == SSL_ST_READ_BODY */ |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame^] | 320 | } else { |
| 321 | /* |packet_length| is non-zero and |s->rstate| is |SSL_ST_READ_BODY|. The |
| 322 | * read buffer must be existant and |s->s3->init_extra| is defined. */ |
| 323 | assert(s->s3->rbuf.buf != NULL); |
| 324 | extra = s->s3->init_extra ? SSL3_RT_MAX_EXTRA : 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 325 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 326 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 327 | /* s->rstate == SSL_ST_READ_BODY, get and decode the data */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 328 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 329 | if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) { |
| 330 | /* now s->packet_length == SSL3_RT_HEADER_LENGTH */ |
| 331 | i = rr->length; |
David Benjamin | b1f5bca | 2015-05-08 22:54:02 -0400 | [diff] [blame] | 332 | n = ssl3_read_n(s, i, 1); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 333 | if (n <= 0) { |
| 334 | /* Error or non-blocking IO. Now |n| == |rr->length|, and |
| 335 | * |s->packet_length| == |SSL3_RT_HEADER_LENGTH| + |rr->length|. */ |
| 336 | return n; |
| 337 | } |
| 338 | } |
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 | s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 341 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 342 | /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, and |
| 343 | * we have that many bytes in s->packet. */ |
| 344 | rr->input = &s->packet[SSL3_RT_HEADER_LENGTH]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 345 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 346 | /* ok, we can now read from |s->packet| data into |rr|. |rr->input| points at |
| 347 | * |rr->length| bytes, which need to be copied into |rr->data| by decryption. |
| 348 | * When the data is 'copied' into the |rr->data| buffer, |rr->input| will be |
| 349 | * pointed at the new buffer. */ |
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 | /* We now have - encrypted [ MAC [ compressed [ plain ] ] ] |
| 352 | * rr->length bytes of encrypted compressed stuff. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 353 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 354 | /* decrypt in place in 'rr->input' */ |
| 355 | rr->data = rr->input; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 356 | |
David Benjamin | 1e52eca | 2015-01-22 15:33:51 -0500 | [diff] [blame] | 357 | if (!s->enc_method->enc(s, 0)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 358 | al = SSL_AD_BAD_RECORD_MAC; |
| 359 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, |
| 360 | SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); |
| 361 | goto f_err; |
| 362 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 363 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 364 | if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH + extra) { |
| 365 | al = SSL_AD_RECORD_OVERFLOW; |
| 366 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_DATA_LENGTH_TOO_LONG); |
| 367 | goto f_err; |
| 368 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 369 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 370 | rr->off = 0; |
| 371 | /* So at this point the following is true: |
| 372 | * ssl->s3->rrec.type is the type of record; |
| 373 | * ssl->s3->rrec.length is the number of bytes in the record; |
| 374 | * ssl->s3->rrec.off is the offset to first valid byte; |
| 375 | * 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] | 376 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 377 | /* we have pulled in a full packet so zero things */ |
| 378 | s->packet_length = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 379 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 380 | /* just read a 0 length packet */ |
| 381 | if (rr->length == 0) { |
| 382 | empty_record_count++; |
| 383 | if (empty_record_count > MAX_EMPTY_RECORDS) { |
| 384 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 385 | OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_TOO_MANY_EMPTY_FRAGMENTS); |
| 386 | goto f_err; |
| 387 | } |
| 388 | goto again; |
| 389 | } |
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 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | |
| 393 | f_err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 394 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 395 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 396 | return ret; |
| 397 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 398 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 399 | /* Call this to write data in records of type |type|. It will return <= 0 if |
| 400 | * not all data has been sent or non-blocking IO. */ |
| 401 | int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) { |
| 402 | const uint8_t *buf = buf_; |
| 403 | unsigned int tot, n, nw; |
| 404 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 406 | s->rwstate = SSL_NOTHING; |
| 407 | assert(s->s3->wnum <= INT_MAX); |
| 408 | tot = s->s3->wnum; |
| 409 | s->s3->wnum = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 410 | |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 411 | 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] | 412 | i = s->handshake_func(s); |
| 413 | if (i < 0) { |
| 414 | return i; |
| 415 | } |
| 416 | if (i == 0) { |
| 417 | OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 418 | return -1; |
| 419 | } |
| 420 | } |
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 | /* Ensure that if we end up with a smaller value of data to write out than |
| 423 | * the the original len from a write which didn't complete for non-blocking |
| 424 | * I/O and also somehow ended up avoiding the check for this in |
| 425 | * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to |
| 426 | * end up with (len-tot) as a large number that will then promptly send |
| 427 | * beyond the end of the users buffer ... so we trap and report the error in |
| 428 | * a way the user will notice. */ |
| 429 | if (len < 0 || (size_t)len < tot) { |
| 430 | OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_BAD_LENGTH); |
| 431 | return -1; |
| 432 | } |
Adam Langley | 9611cfc | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 433 | |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 434 | int record_split_done = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 435 | n = (len - tot); |
| 436 | for (;;) { |
| 437 | /* max contains the maximum number of bytes that we can put into a |
| 438 | * record. */ |
| 439 | unsigned max = s->max_send_fragment; |
| 440 | /* fragment is true if do_ssl3_write should send the first byte in its own |
| 441 | * record in order to randomise a CBC IV. */ |
| 442 | int fragment = 0; |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 443 | if (!record_split_done && s->s3->need_record_splitting && |
| 444 | type == SSL3_RT_APPLICATION_DATA) { |
| 445 | /* Only the the first record per write call needs to be split. The |
| 446 | * remaining plaintext was determined before the IV was randomized. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 447 | fragment = 1; |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 448 | record_split_done = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 449 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 450 | if (n > max) { |
| 451 | nw = max; |
| 452 | } else { |
| 453 | nw = n; |
| 454 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 455 | |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 456 | i = do_ssl3_write(s, type, &buf[tot], nw, fragment); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 457 | if (i <= 0) { |
| 458 | s->s3->wnum = tot; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 459 | return i; |
| 460 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 461 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 462 | if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA && |
| 463 | (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 464 | return tot + i; |
| 465 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 467 | n -= i; |
| 468 | tot += i; |
| 469 | } |
| 470 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 471 | |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 472 | /* ssl3_seal_record seals a new record of type |type| and plaintext |in| and |
| 473 | * writes it to |out|. At most |max_out| bytes will be written. It returns one |
| 474 | * on success and zero on error. On success, |s->s3->wrec| is updated to include |
| 475 | * the new record. */ |
| 476 | static int ssl3_seal_record(SSL *s, uint8_t *out, size_t *out_len, |
| 477 | size_t max_out, uint8_t type, const uint8_t *in, |
| 478 | size_t in_len) { |
| 479 | if (max_out < SSL3_RT_HEADER_LENGTH) { |
| 480 | OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL); |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | out[0] = type; |
| 485 | |
| 486 | /* Some servers hang if initial ClientHello is larger than 256 bytes and |
| 487 | * record version number > TLS 1.0. */ |
| 488 | if (!s->s3->have_version && s->version > SSL3_VERSION) { |
| 489 | out[1] = TLS1_VERSION >> 8; |
| 490 | out[2] = TLS1_VERSION & 0xff; |
| 491 | } else { |
| 492 | out[1] = s->version >> 8; |
| 493 | out[2] = s->version & 0xff; |
| 494 | } |
| 495 | |
| 496 | size_t explicit_nonce_len = 0; |
| 497 | if (s->aead_write_ctx != NULL && |
| 498 | s->aead_write_ctx->variable_nonce_included_in_record) { |
| 499 | explicit_nonce_len = s->aead_write_ctx->variable_nonce_len; |
| 500 | } |
| 501 | size_t max_overhead = 0; |
| 502 | if (s->aead_write_ctx != NULL) { |
| 503 | max_overhead = s->aead_write_ctx->tag_len; |
| 504 | } |
| 505 | |
| 506 | /* Assemble the input for |s->enc_method->enc|. The input is the plaintext |
| 507 | * with |explicit_nonce_len| bytes of space prepended for the explicit |
| 508 | * nonce. The input is copied into |out| and then encrypted in-place to take |
| 509 | * advantage of alignment. |
| 510 | * |
| 511 | * TODO(davidben): |tls1_enc| should accept its inputs and outputs directly |
| 512 | * rather than looking up in |wrec| and friends. The |max_overhead| bounds |
| 513 | * check would also be unnecessary if |max_out| were passed down. */ |
| 514 | SSL3_RECORD *wr = &s->s3->wrec; |
| 515 | size_t plaintext_len = in_len + explicit_nonce_len; |
| 516 | if (plaintext_len < in_len || plaintext_len > INT_MAX || |
| 517 | plaintext_len + max_overhead < plaintext_len) { |
| 518 | OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW); |
| 519 | return 0; |
| 520 | } |
| 521 | if (max_out - SSL3_RT_HEADER_LENGTH < plaintext_len + max_overhead) { |
| 522 | OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL); |
| 523 | return 0; |
| 524 | } |
| 525 | wr->type = type; |
| 526 | wr->input = out + SSL3_RT_HEADER_LENGTH; |
| 527 | wr->data = wr->input; |
| 528 | wr->length = plaintext_len; |
| 529 | memcpy(wr->input + explicit_nonce_len, in, in_len); |
| 530 | |
| 531 | if (!s->enc_method->enc(s, 1)) { |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /* |wr->length| has now been set to the ciphertext length. */ |
| 536 | if (wr->length >= 1 << 16) { |
| 537 | OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW); |
| 538 | return 0; |
| 539 | } |
| 540 | out[3] = wr->length >> 8; |
| 541 | out[4] = wr->length & 0xff; |
| 542 | *out_len = SSL3_RT_HEADER_LENGTH + (size_t)wr->length; |
| 543 | |
| 544 | if (s->msg_callback) { |
| 545 | s->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out, SSL3_RT_HEADER_LENGTH, |
| 546 | s, s->msg_callback_arg); |
| 547 | } |
| 548 | |
| 549 | return 1; |
| 550 | } |
| 551 | |
Adam Langley | d493d52 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 552 | /* do_ssl3_write writes an SSL record of the given type. If |fragment| is 1 |
| 553 | * 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] | 554 | * of the data in order to randomise a CBC IV. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 555 | 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] | 556 | char fragment) { |
| 557 | SSL3_BUFFER *wb = &s->s3->wbuf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 558 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 559 | /* first check if there is a SSL3_BUFFER still being written out. This will |
| 560 | * happen with non blocking IO */ |
| 561 | if (wb->left != 0) { |
| 562 | return ssl3_write_pending(s, type, buf, len); |
| 563 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 564 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 565 | /* If we have an alert to send, lets send it */ |
| 566 | if (s->s3->alert_dispatch) { |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 567 | int ret = s->method->ssl_dispatch_alert(s); |
| 568 | if (ret <= 0) { |
| 569 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 570 | } |
| 571 | /* if it went, fall through and send more stuff */ |
| 572 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 573 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 574 | if (wb->buf == NULL && !ssl3_setup_write_buffer(s)) { |
| 575 | return -1; |
| 576 | } |
Adam Langley | c6c8ae8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 577 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 578 | if (len == 0) { |
| 579 | return 0; |
| 580 | } |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 581 | if (len == 1) { |
| 582 | /* No sense in fragmenting a one-byte record. */ |
| 583 | fragment = 0; |
| 584 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 585 | |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 586 | /* Align the output so the ciphertext is aligned to |SSL3_ALIGN_PAYLOAD|. */ |
| 587 | uintptr_t align; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 588 | if (fragment) { |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 589 | /* Only CBC-mode ciphers require fragmenting. CBC-mode ciphertext is a |
| 590 | * multiple of the block size which we may assume is aligned. Thus we only |
| 591 | * need to account for a second copy of the record header. */ |
| 592 | align = (uintptr_t)wb->buf + 2 * SSL3_RT_HEADER_LENGTH; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 593 | } else { |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 594 | align = (uintptr_t)wb->buf + SSL3_RT_HEADER_LENGTH; |
| 595 | } |
David Benjamin | ef4962f | 2015-04-06 20:31:44 -0400 | [diff] [blame] | 596 | align = (0 - align) & (SSL3_ALIGN_PAYLOAD - 1); |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 597 | uint8_t *out = wb->buf + align; |
| 598 | wb->offset = align; |
| 599 | size_t max_out = wb->len - wb->offset; |
| 600 | |
| 601 | const uint8_t *orig_buf = buf; |
| 602 | unsigned int orig_len = len; |
| 603 | size_t fragment_len = 0; |
| 604 | if (fragment) { |
| 605 | /* Write the first byte in its own record as a countermeasure against |
| 606 | * known-IV weaknesses in CBC ciphersuites. (See |
| 607 | * http://www.openssl.org/~bodo/tls-cbc.txt.) */ |
| 608 | if (!ssl3_seal_record(s, out, &fragment_len, max_out, type, buf, 1)) { |
| 609 | return -1; |
| 610 | } |
| 611 | out += fragment_len; |
| 612 | max_out -= fragment_len; |
| 613 | buf++; |
| 614 | len--; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 615 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 616 | |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 617 | assert((((uintptr_t)out + SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1)) |
| 618 | == 0); |
| 619 | size_t ciphertext_len; |
| 620 | if (!ssl3_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) { |
| 621 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 622 | } |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 623 | ciphertext_len += fragment_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 624 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 625 | /* now let's set up wb */ |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 626 | wb->left = ciphertext_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 627 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 628 | /* memorize arguments so that ssl3_write_pending can detect bad write retries |
| 629 | * later */ |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 630 | s->s3->wpend_tot = orig_len; |
| 631 | s->s3->wpend_buf = orig_buf; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 632 | s->s3->wpend_type = type; |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 633 | s->s3->wpend_ret = orig_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 634 | |
| 635 | /* we now just need to write the buffer */ |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 636 | return ssl3_write_pending(s, type, orig_buf, orig_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 637 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 638 | |
| 639 | /* if s->s3->wbuf.left != 0, we need to call this */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 640 | int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) { |
| 641 | int i; |
| 642 | SSL3_BUFFER *wb = &(s->s3->wbuf); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 643 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 644 | if (s->s3->wpend_tot > (int)len || |
| 645 | (s->s3->wpend_buf != buf && |
| 646 | !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) || |
| 647 | s->s3->wpend_type != type) { |
| 648 | OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BAD_WRITE_RETRY); |
| 649 | return -1; |
| 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 | for (;;) { |
| 653 | ERR_clear_system_error(); |
| 654 | if (s->wbio != NULL) { |
| 655 | s->rwstate = SSL_WRITING; |
| 656 | i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]), |
| 657 | (unsigned int)wb->left); |
| 658 | } else { |
| 659 | OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BIO_NOT_SET); |
| 660 | i = -1; |
| 661 | } |
| 662 | if (i == wb->left) { |
| 663 | wb->left = 0; |
| 664 | wb->offset += i; |
David Benjamin | 9417b76 | 2015-05-08 22:30:06 -0400 | [diff] [blame] | 665 | ssl3_release_write_buffer(s); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 666 | s->rwstate = SSL_NOTHING; |
| 667 | return s->s3->wpend_ret; |
| 668 | } else if (i <= 0) { |
| 669 | if (SSL_IS_DTLS(s)) { |
David Benjamin | aebefed | 2015-05-08 22:20:04 -0400 | [diff] [blame] | 670 | /* For DTLS, just drop it. That's kind of the whole point in |
| 671 | * using a datagram service */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 672 | wb->left = 0; |
| 673 | } |
| 674 | return i; |
| 675 | } |
David Benjamin | 883e49f | 2015-04-05 12:19:14 -0400 | [diff] [blame] | 676 | /* TODO(davidben): This codepath is used in DTLS, but the write |
| 677 | * payload may not split across packets. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 678 | wb->offset += i; |
| 679 | wb->left -= i; |
| 680 | } |
| 681 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 682 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 683 | /* ssl3_expect_change_cipher_spec informs the record layer that a |
| 684 | * ChangeCipherSpec record is required at this point. If a Handshake record is |
| 685 | * received before ChangeCipherSpec, the connection will fail. Moreover, if |
| 686 | * there are unprocessed handshake bytes, the handshake will also fail and the |
| 687 | * function returns zero. Otherwise, the function returns one. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 688 | int ssl3_expect_change_cipher_spec(SSL *s) { |
| 689 | if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) { |
| 690 | OPENSSL_PUT_ERROR(SSL, ssl3_expect_change_cipher_spec, |
| 691 | SSL_R_UNPROCESSED_HANDSHAKE_DATA); |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | s->s3->flags |= SSL3_FLAGS_EXPECT_CCS; |
| 696 | return 1; |
| 697 | } |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 698 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 699 | /* Return up to 'len' payload bytes received in 'type' records. |
| 700 | * 'type' is one of the following: |
| 701 | * |
| 702 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) |
| 703 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) |
| 704 | * - 0 (during a shutdown, no data has to be returned) |
| 705 | * |
| 706 | * If we don't have stored data to work from, read a SSL/TLS record first |
| 707 | * (possibly multiple records if we still don't have anything to return). |
| 708 | * |
| 709 | * This function must handle any surprises the peer may have for us, such as |
| 710 | * Alert records (e.g. close_notify), ChangeCipherSpec records (not really |
| 711 | * a surprise, but handled as if it were), or renegotiation requests. |
| 712 | * Also if record payloads contain fragments too small to process, we store |
| 713 | * them until there is enough for the respective protocol (the record protocol |
| 714 | * may use arbitrary fragmentation and even interleaving): |
| 715 | * Change cipher spec protocol |
| 716 | * just 1 byte needed, no need for keeping anything stored |
| 717 | * Alert protocol |
| 718 | * 2 bytes needed (AlertLevel, AlertDescription) |
| 719 | * Handshake protocol |
| 720 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have |
| 721 | * to detect unexpected Client Hello and Hello Request messages |
| 722 | * here, anything else is handled by higher layers |
| 723 | * Application data protocol |
| 724 | * none of our business |
| 725 | */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 726 | 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] | 727 | int al, i, ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 728 | unsigned int n; |
| 729 | SSL3_RECORD *rr; |
| 730 | void (*cb)(const SSL *ssl, int type2, int val) = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 731 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 732 | if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) || |
| 733 | (peek && type != SSL3_RT_APPLICATION_DATA)) { |
| 734 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR); |
| 735 | return -1; |
| 736 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 737 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 738 | if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) { |
| 739 | /* (partially) satisfy request from storage */ |
| 740 | uint8_t *src = s->s3->handshake_fragment; |
| 741 | uint8_t *dst = buf; |
| 742 | unsigned int k; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 743 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 744 | /* peek == 0 */ |
| 745 | n = 0; |
| 746 | while (len > 0 && s->s3->handshake_fragment_len > 0) { |
| 747 | *dst++ = *src++; |
| 748 | len--; |
| 749 | s->s3->handshake_fragment_len--; |
| 750 | n++; |
| 751 | } |
| 752 | /* move any remaining fragment bytes: */ |
| 753 | for (k = 0; k < s->s3->handshake_fragment_len; k++) { |
| 754 | s->s3->handshake_fragment[k] = *src++; |
| 755 | } |
| 756 | return n; |
| 757 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 758 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 759 | /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 760 | |
David Benjamin | 931ab34 | 2015-02-08 19:46:57 -0500 | [diff] [blame] | 761 | /* This may require multiple iterations. False Start will cause |
| 762 | * |s->handshake_func| to signal success one step early, but the handshake |
| 763 | * must be completely finished before other modes are accepted. |
| 764 | * |
| 765 | * TODO(davidben): Move this check up to a higher level. */ |
| 766 | while (!s->in_handshake && SSL_in_init(s)) { |
| 767 | assert(type == SSL3_RT_APPLICATION_DATA); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 768 | i = s->handshake_func(s); |
| 769 | if (i < 0) { |
| 770 | return i; |
| 771 | } |
| 772 | if (i == 0) { |
| 773 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 774 | return -1; |
| 775 | } |
| 776 | } |
| 777 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 778 | start: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 779 | s->rwstate = SSL_NOTHING; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 780 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 781 | /* s->s3->rrec.type - is the type of record |
| 782 | * s->s3->rrec.data - data |
| 783 | * s->s3->rrec.off - offset into 'data' for next read |
| 784 | * s->s3->rrec.length - number of bytes. */ |
| 785 | rr = &s->s3->rrec; |
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 | /* get new packet if necessary */ |
| 788 | if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) { |
| 789 | ret = ssl3_get_record(s); |
| 790 | if (ret <= 0) { |
| 791 | return ret; |
| 792 | } |
| 793 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 794 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 795 | /* we now have a packet which can be read and processed */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 796 | |
David Benjamin | dc3da93 | 2015-03-12 15:09:02 -0400 | [diff] [blame] | 797 | /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by |
| 798 | * ssl3_get_finished. */ |
| 799 | if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE && |
| 800 | rr->type != SSL3_RT_ALERT) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 801 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 802 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, |
| 803 | SSL_R_DATA_BETWEEN_CCS_AND_FINISHED); |
| 804 | goto f_err; |
| 805 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 806 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 807 | /* If we are expecting a ChangeCipherSpec, it is illegal to receive a |
| 808 | * Handshake record. */ |
| 809 | if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) { |
| 810 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 811 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS); |
| 812 | goto f_err; |
| 813 | } |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 814 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 815 | /* If the other end has shut down, throw anything we read away (even in |
| 816 | * 'peek' mode) */ |
| 817 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { |
| 818 | rr->length = 0; |
| 819 | s->rwstate = SSL_NOTHING; |
| 820 | return 0; |
| 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 (type == rr->type) { |
| 824 | /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */ |
| 825 | /* make sure that we are not getting application data when we are doing a |
| 826 | * handshake for the first time */ |
| 827 | if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA && |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 828 | s->aead_read_ctx == NULL) { |
| 829 | /* TODO(davidben): Is this check redundant with the handshake_func |
| 830 | * check? */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 831 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 832 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE); |
| 833 | goto f_err; |
| 834 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 835 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 836 | if (len <= 0) { |
| 837 | return len; |
| 838 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 839 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 840 | if ((unsigned int)len > rr->length) { |
| 841 | n = rr->length; |
| 842 | } else { |
| 843 | n = (unsigned int)len; |
| 844 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 845 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 846 | memcpy(buf, &(rr->data[rr->off]), n); |
| 847 | if (!peek) { |
| 848 | rr->length -= n; |
| 849 | rr->off += n; |
| 850 | if (rr->length == 0) { |
| 851 | s->rstate = SSL_ST_READ_HEADER; |
| 852 | rr->off = 0; |
David Benjamin | aebefed | 2015-05-08 22:20:04 -0400 | [diff] [blame] | 853 | if (s->s3->rbuf.left == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 854 | ssl3_release_read_buffer(s); |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | return n; |
| 860 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 861 | |
| 862 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 863 | /* If we get here, then type != rr->type; if we have a handshake message, |
| 864 | * then it was unexpected (Hello Request or Client Hello). */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 865 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 866 | /* In case of record types for which we have 'fragment' storage, fill that so |
| 867 | * that we can process the data at a fixed place. */ |
Alex Chernyakhovsky | 4cd8c43 | 2014-11-01 19:39:08 -0400 | [diff] [blame] | 868 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 869 | if (rr->type == SSL3_RT_HANDSHAKE) { |
David Benjamin | b16346b | 2015-04-08 19:16:58 -0400 | [diff] [blame] | 870 | /* If peer renegotiations are disabled, all out-of-order handshake records |
| 871 | * are fatal. */ |
| 872 | if (s->reject_peer_renegotiations) { |
| 873 | al = SSL_AD_NO_RENEGOTIATION; |
| 874 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION); |
| 875 | goto f_err; |
| 876 | } |
| 877 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 878 | const size_t size = sizeof(s->s3->handshake_fragment); |
| 879 | const size_t avail = size - s->s3->handshake_fragment_len; |
| 880 | const size_t todo = (rr->length < avail) ? rr->length : avail; |
| 881 | memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len, |
| 882 | &rr->data[rr->off], todo); |
| 883 | rr->off += todo; |
| 884 | rr->length -= todo; |
| 885 | s->s3->handshake_fragment_len += todo; |
| 886 | if (s->s3->handshake_fragment_len < size) { |
| 887 | goto start; /* fragment was too small */ |
| 888 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 889 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 890 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 891 | /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE; |
| 892 | * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 893 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 894 | /* If we are a client, check for an incoming 'Hello Request': */ |
| 895 | if (!s->server && s->s3->handshake_fragment_len >= 4 && |
| 896 | s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST && |
| 897 | s->session != NULL && s->session->cipher != NULL) { |
| 898 | s->s3->handshake_fragment_len = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 899 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 900 | if (s->s3->handshake_fragment[1] != 0 || |
| 901 | s->s3->handshake_fragment[2] != 0 || |
| 902 | s->s3->handshake_fragment[3] != 0) { |
| 903 | al = SSL_AD_DECODE_ERROR; |
| 904 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST); |
| 905 | goto f_err; |
| 906 | } |
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->msg_callback) { |
| 909 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, |
| 910 | s->s3->handshake_fragment, 4, s, s->msg_callback_arg); |
| 911 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 912 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 913 | if (SSL_is_init_finished(s) && !s->s3->renegotiate) { |
| 914 | ssl3_renegotiate(s); |
| 915 | if (ssl3_renegotiate_check(s)) { |
| 916 | i = s->handshake_func(s); |
| 917 | if (i < 0) { |
| 918 | return i; |
| 919 | } |
| 920 | if (i == 0) { |
| 921 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 922 | return -1; |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | /* we either finished a handshake or ignored the request, now try again to |
| 927 | * obtain the (application) data we were asked for */ |
| 928 | goto start; |
| 929 | } |
David Benjamin | b4188f0 | 2014-11-01 03:43:48 -0400 | [diff] [blame] | 930 | |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 931 | /* If an alert record, process one alert out of the record. Note that we allow |
| 932 | * a single record to contain multiple alerts. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 933 | if (rr->type == SSL3_RT_ALERT) { |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 934 | /* Alerts may not be fragmented. */ |
| 935 | if (rr->length < 2) { |
| 936 | al = SSL_AD_DECODE_ERROR; |
| 937 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT); |
| 938 | goto f_err; |
| 939 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 940 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 941 | if (s->msg_callback) { |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 942 | 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] | 943 | s->msg_callback_arg); |
| 944 | } |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 945 | const uint8_t alert_level = rr->data[rr->off++]; |
| 946 | const uint8_t alert_descr = rr->data[rr->off++]; |
| 947 | rr->length -= 2; |
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->info_callback != NULL) { |
| 950 | cb = s->info_callback; |
| 951 | } else if (s->ctx->info_callback != NULL) { |
| 952 | cb = s->ctx->info_callback; |
| 953 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 954 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 955 | if (cb != NULL) { |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 956 | uint16_t alert = (alert_level << 8) | alert_descr; |
| 957 | cb(s, SSL_CB_READ_ALERT, alert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 958 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 959 | |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 960 | if (alert_level == SSL3_AL_WARNING) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 961 | s->s3->warn_alert = alert_descr; |
| 962 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) { |
| 963 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
| 964 | return 0; |
| 965 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 966 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 967 | /* This is a warning but we receive it if we requested renegotiation and |
| 968 | * the peer denied it. Terminate with a fatal alert because if |
| 969 | * application tried to renegotiatie it presumably had a good reason and |
| 970 | * expects it to succeed. |
| 971 | * |
| 972 | * In future we might have a renegotiation where we don't care if the |
| 973 | * peer refused it where we carry on. */ |
| 974 | else if (alert_descr == SSL_AD_NO_RENEGOTIATION) { |
| 975 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 976 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION); |
| 977 | goto f_err; |
| 978 | } |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 979 | } else if (alert_level == SSL3_AL_FATAL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 980 | char tmp[16]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 981 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 982 | s->rwstate = SSL_NOTHING; |
| 983 | s->s3->fatal_alert = alert_descr; |
| 984 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, |
| 985 | SSL_AD_REASON_OFFSET + alert_descr); |
| 986 | BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr); |
| 987 | ERR_add_error_data(2, "SSL alert number ", tmp); |
| 988 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
| 989 | SSL_CTX_remove_session(s->ctx, s->session); |
| 990 | return 0; |
| 991 | } else { |
| 992 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 993 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE); |
| 994 | goto f_err; |
| 995 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 996 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 997 | goto start; |
| 998 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 999 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1000 | if (s->shutdown & SSL_SENT_SHUTDOWN) { |
| 1001 | /* but we have not received a shutdown */ |
| 1002 | s->rwstate = SSL_NOTHING; |
| 1003 | rr->length = 0; |
| 1004 | return 0; |
| 1005 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1006 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1007 | if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) { |
| 1008 | /* 'Change Cipher Spec' is just a single byte, so we know exactly what the |
| 1009 | * record payload has to look like */ |
| 1010 | if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) { |
| 1011 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1012 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
| 1013 | goto f_err; |
| 1014 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1015 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1016 | /* Check we have a cipher to change to */ |
| 1017 | if (s->s3->tmp.new_cipher == NULL) { |
| 1018 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 1019 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY); |
| 1020 | goto f_err; |
| 1021 | } |
Adam Langley | ce7f9ca | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1022 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1023 | if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) { |
| 1024 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 1025 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY); |
| 1026 | goto f_err; |
| 1027 | } |
Adam Langley | ce7f9ca | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1028 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1029 | s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1030 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1031 | rr->length = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1032 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1033 | if (s->msg_callback) { |
| 1034 | s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s, |
| 1035 | s->msg_callback_arg); |
| 1036 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1037 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1038 | s->s3->change_cipher_spec = 1; |
| 1039 | if (!ssl3_do_change_cipher_spec(s)) { |
| 1040 | goto err; |
| 1041 | } else { |
| 1042 | goto start; |
| 1043 | } |
| 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 | /* Unexpected handshake message (Client Hello, or protocol violation) */ |
| 1047 | if (s->s3->handshake_fragment_len >= 4 && !s->in_handshake) { |
| 1048 | if ((s->state & SSL_ST_MASK) == SSL_ST_OK) { |
| 1049 | s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT; |
| 1050 | s->renegotiate = 1; |
| 1051 | s->new_session = 1; |
| 1052 | } |
| 1053 | i = s->handshake_func(s); |
| 1054 | if (i < 0) { |
| 1055 | return i; |
| 1056 | } |
| 1057 | if (i == 0) { |
| 1058 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1059 | return -1; |
| 1060 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1061 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1062 | goto start; |
| 1063 | } |
| 1064 | |
David Benjamin | e820df9 | 2015-02-08 16:06:54 -0500 | [diff] [blame] | 1065 | /* We already handled these. */ |
| 1066 | assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT && |
| 1067 | rr->type != SSL3_RT_HANDSHAKE); |
David Benjamin | ddb9f15 | 2015-02-03 15:44:39 -0500 | [diff] [blame] | 1068 | |
David Benjamin | e820df9 | 2015-02-08 16:06:54 -0500 | [diff] [blame] | 1069 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 1070 | OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1071 | |
| 1072 | f_err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1073 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1074 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1075 | return -1; |
| 1076 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1077 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1078 | int ssl3_do_change_cipher_spec(SSL *s) { |
| 1079 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1080 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1081 | if (s->state & SSL_ST_ACCEPT) { |
| 1082 | i = SSL3_CHANGE_CIPHER_SERVER_READ; |
| 1083 | } else { |
| 1084 | i = SSL3_CHANGE_CIPHER_CLIENT_READ; |
| 1085 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1086 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1087 | if (s->s3->tmp.key_block == NULL) { |
| 1088 | if (s->session == NULL || s->session->master_key_length == 0) { |
| 1089 | /* might happen if dtls1_read_bytes() calls this */ |
| 1090 | OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec, |
| 1091 | SSL_R_CCS_RECEIVED_EARLY); |
| 1092 | return 0; |
| 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 | s->session->cipher = s->s3->tmp.new_cipher; |
| 1096 | if (!s->enc_method->setup_key_block(s)) { |
| 1097 | return 0; |
| 1098 | } |
| 1099 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1100 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1101 | if (!s->enc_method->change_cipher_state(s, i)) { |
| 1102 | return 0; |
| 1103 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1104 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1105 | return 1; |
| 1106 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1107 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1108 | int ssl3_send_alert(SSL *s, int level, int desc) { |
| 1109 | /* Map tls/ssl alert value to correct one */ |
| 1110 | desc = s->enc_method->alert_value(desc); |
| 1111 | if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) { |
| 1112 | /* SSL 3.0 does not have protocol_version alerts */ |
| 1113 | desc = SSL_AD_HANDSHAKE_FAILURE; |
| 1114 | } |
| 1115 | if (desc < 0) { |
| 1116 | return -1; |
| 1117 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1118 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1119 | /* If a fatal one, remove from cache */ |
| 1120 | if (level == 2 && s->session != NULL) { |
| 1121 | SSL_CTX_remove_session(s->ctx, s->session); |
| 1122 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1123 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1124 | s->s3->alert_dispatch = 1; |
| 1125 | s->s3->send_alert[0] = level; |
| 1126 | s->s3->send_alert[1] = desc; |
| 1127 | if (s->s3->wbuf.left == 0) { |
| 1128 | /* data is still being written out. */ |
| 1129 | return s->method->ssl_dispatch_alert(s); |
| 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 | /* else data is still being written out, we will get written some time in the |
| 1133 | * future */ |
| 1134 | return -1; |
| 1135 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1136 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1137 | int ssl3_dispatch_alert(SSL *s) { |
| 1138 | int i, j; |
| 1139 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1140 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1141 | s->s3->alert_dispatch = 0; |
David Benjamin | 9faafda | 2015-04-04 19:23:10 -0400 | [diff] [blame] | 1142 | 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] | 1143 | if (i <= 0) { |
| 1144 | s->s3->alert_dispatch = 1; |
| 1145 | } else { |
| 1146 | /* Alert sent to BIO. If it is important, flush it now. If the message |
| 1147 | * does not get sent due to non-blocking IO, we will not worry too much. */ |
| 1148 | if (s->s3->send_alert[0] == SSL3_AL_FATAL) { |
| 1149 | BIO_flush(s->wbio); |
| 1150 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1151 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1152 | if (s->msg_callback) { |
| 1153 | s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s, |
| 1154 | s->msg_callback_arg); |
| 1155 | } |
| 1156 | |
| 1157 | if (s->info_callback != NULL) { |
| 1158 | cb = s->info_callback; |
| 1159 | } else if (s->ctx->info_callback != NULL) { |
| 1160 | cb = s->ctx->info_callback; |
| 1161 | } |
| 1162 | |
| 1163 | if (cb != NULL) { |
| 1164 | j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]; |
| 1165 | cb(s, SSL_CB_WRITE_ALERT, j); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | return i; |
| 1170 | } |