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