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