Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* DTLS implementation written by Nagendra Modadugu |
| 2 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. */ |
| 3 | /* ==================================================================== |
| 4 | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * |
| 18 | * 3. All advertising materials mentioning features or use of this |
| 19 | * software must display the following acknowledgment: |
| 20 | * "This product includes software developed by the OpenSSL Project |
| 21 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 22 | * |
| 23 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 24 | * endorse or promote products derived from this software without |
| 25 | * prior written permission. For written permission, please contact |
| 26 | * openssl-core@openssl.org. |
| 27 | * |
| 28 | * 5. Products derived from this software may not be called "OpenSSL" |
| 29 | * nor may "OpenSSL" appear in their names without prior written |
| 30 | * permission of the OpenSSL Project. |
| 31 | * |
| 32 | * 6. Redistributions of any form whatsoever must retain the following |
| 33 | * acknowledgment: |
| 34 | * "This product includes software developed by the OpenSSL Project |
| 35 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 36 | * |
| 37 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 38 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 39 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 40 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 41 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 42 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 43 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 44 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 45 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 46 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 48 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 49 | * ==================================================================== |
| 50 | * |
| 51 | * This product includes cryptographic software written by Eric Young |
| 52 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 53 | * Hudson (tjh@cryptsoft.com). |
| 54 | * |
| 55 | */ |
| 56 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 57 | * All rights reserved. |
| 58 | * |
| 59 | * This package is an SSL implementation written |
| 60 | * by Eric Young (eay@cryptsoft.com). |
| 61 | * The implementation was written so as to conform with Netscapes SSL. |
| 62 | * |
| 63 | * This library is free for commercial and non-commercial use as long as |
| 64 | * the following conditions are aheared to. The following conditions |
| 65 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 66 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 67 | * included with this distribution is covered by the same copyright terms |
| 68 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 69 | * |
| 70 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 71 | * the code are not to be removed. |
| 72 | * If this package is used in a product, Eric Young should be given attribution |
| 73 | * as the author of the parts of the library used. |
| 74 | * This can be in the form of a textual message at program startup or |
| 75 | * in documentation (online or textual) provided with the package. |
| 76 | * |
| 77 | * Redistribution and use in source and binary forms, with or without |
| 78 | * modification, are permitted provided that the following conditions |
| 79 | * are met: |
| 80 | * 1. Redistributions of source code must retain the copyright |
| 81 | * notice, this list of conditions and the following disclaimer. |
| 82 | * 2. Redistributions in binary form must reproduce the above copyright |
| 83 | * notice, this list of conditions and the following disclaimer in the |
| 84 | * documentation and/or other materials provided with the distribution. |
| 85 | * 3. All advertising materials mentioning features or use of this software |
| 86 | * must display the following acknowledgement: |
| 87 | * "This product includes cryptographic software written by |
| 88 | * Eric Young (eay@cryptsoft.com)" |
| 89 | * The word 'cryptographic' can be left out if the rouines from the library |
| 90 | * being used are not cryptographic related :-). |
| 91 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 92 | * the apps directory (application code) you must include an acknowledgement: |
| 93 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 94 | * |
| 95 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 96 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 97 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 98 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 99 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 100 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 101 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 102 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 103 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 104 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 105 | * SUCH DAMAGE. |
| 106 | * |
| 107 | * The licence and distribution terms for any publically available version or |
| 108 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 109 | * copied and put under another distribution licence |
| 110 | * [including the GNU Public Licence.] */ |
| 111 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 112 | #include <assert.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 113 | #include <stdio.h> |
| 114 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 115 | |
| 116 | #include <openssl/buf.h> |
| 117 | #include <openssl/mem.h> |
| 118 | #include <openssl/evp.h> |
| 119 | #include <openssl/err.h> |
| 120 | #include <openssl/rand.h> |
| 121 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 122 | #include "internal.h" |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 123 | |
| 124 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 125 | static int do_dtls1_write(SSL *s, int type, const uint8_t *buf, |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 126 | unsigned int len, enum dtls1_use_epoch_t use_epoch); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 127 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 128 | /* dtls1_get_record reads a new input record. On success, it places it in |
| 129 | * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if |
| 130 | * more data is needed. */ |
| 131 | static int dtls1_get_record(SSL *ssl) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 132 | again: |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 133 | /* Read a new packet if there is no unconsumed one. */ |
| 134 | if (ssl_read_buffer_len(ssl) == 0) { |
| 135 | int ret = ssl_read_buffer_extend_to(ssl, 0 /* unused */); |
| 136 | if (ret <= 0) { |
| 137 | return ret; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 138 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 139 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 140 | assert(ssl_read_buffer_len(ssl) > 0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 141 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 142 | /* Ensure the packet is large enough to decrypt in-place. */ |
| 143 | if (ssl_read_buffer_len(ssl) < ssl_record_prefix_len(ssl)) { |
| 144 | ssl_read_buffer_clear(ssl); |
David Benjamin | 0afbcc0 | 2015-04-05 04:06:20 -0400 | [diff] [blame] | 145 | goto again; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 146 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 147 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 148 | uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl); |
| 149 | size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl); |
| 150 | uint8_t type, alert; |
| 151 | size_t len, consumed; |
| 152 | switch (dtls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out, |
| 153 | ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) { |
| 154 | case ssl_open_record_success: |
| 155 | ssl_read_buffer_consume(ssl, consumed); |
| 156 | |
| 157 | /* Discard empty records. |
| 158 | * TODO(davidben): This logic should be moved to a higher level. See |
| 159 | * https://crbug.com/521840. |
| 160 | * TODO(davidben): Limit the number of empty records as in TLS? This is |
| 161 | * useful if we also limit discarded packets. */ |
| 162 | if (len == 0) { |
| 163 | goto again; |
| 164 | } |
| 165 | |
| 166 | if (len > 0xffff) { |
| 167 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 172 | rr->type = type; |
| 173 | rr->length = (uint16_t)len; |
| 174 | rr->off = 0; |
| 175 | rr->data = out; |
| 176 | return 1; |
| 177 | |
| 178 | case ssl_open_record_discard: |
| 179 | ssl_read_buffer_consume(ssl, consumed); |
| 180 | goto again; |
| 181 | |
| 182 | case ssl_open_record_error: |
| 183 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 184 | return -1; |
| 185 | |
| 186 | case ssl_open_record_partial: |
| 187 | /* Impossible in DTLS. */ |
| 188 | break; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 189 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 190 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 191 | assert(0); |
| 192 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 193 | return -1; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 194 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 195 | |
David Benjamin | a602277 | 2015-05-30 16:22:10 -0400 | [diff] [blame] | 196 | int dtls1_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) { |
| 197 | return dtls1_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek); |
| 198 | } |
| 199 | |
| 200 | void dtls1_read_close_notify(SSL *ssl) { |
| 201 | dtls1_read_bytes(ssl, 0, NULL, 0, 0); |
| 202 | } |
| 203 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 204 | /* Return up to 'len' payload bytes received in 'type' records. |
| 205 | * 'type' is one of the following: |
| 206 | * |
| 207 | * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) |
| 208 | * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) |
| 209 | * - 0 (during a shutdown, no data has to be returned) |
| 210 | * |
| 211 | * If we don't have stored data to work from, read a SSL/TLS record first |
| 212 | * (possibly multiple records if we still don't have anything to return). |
| 213 | * |
| 214 | * This function must handle any surprises the peer may have for us, such as |
| 215 | * Alert records (e.g. close_notify), ChangeCipherSpec records (not really |
| 216 | * a surprise, but handled as if it were), or renegotiation requests. |
| 217 | * Also if record payloads contain fragments too small to process, we store |
| 218 | * them until there is enough for the respective protocol (the record protocol |
| 219 | * may use arbitrary fragmentation and even interleaving): |
| 220 | * Change cipher spec protocol |
| 221 | * just 1 byte needed, no need for keeping anything stored |
| 222 | * Alert protocol |
| 223 | * 2 bytes needed (AlertLevel, AlertDescription) |
| 224 | * Handshake protocol |
| 225 | * 4 bytes needed (HandshakeType, uint24 length) -- we just have |
| 226 | * to detect unexpected Client Hello and Hello Request messages |
| 227 | * here, anything else is handled by higher layers |
| 228 | * Application data protocol |
| 229 | * none of our business |
| 230 | */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 231 | int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) { |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 232 | int al, i, ret; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 233 | unsigned int n; |
| 234 | SSL3_RECORD *rr; |
| 235 | void (*cb)(const SSL *ssl, int type2, int val) = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 236 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 237 | /* XXX: check what the second '&& type' is about */ |
| 238 | if ((type && (type != SSL3_RT_APPLICATION_DATA) && |
| 239 | (type != SSL3_RT_HANDSHAKE) && type) || |
| 240 | (peek && (type != SSL3_RT_APPLICATION_DATA))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 241 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 242 | return -1; |
| 243 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 244 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 245 | if (!s->in_handshake && SSL_in_init(s)) { |
| 246 | /* type == SSL3_RT_APPLICATION_DATA */ |
| 247 | i = s->handshake_func(s); |
| 248 | if (i < 0) { |
| 249 | return i; |
| 250 | } |
| 251 | if (i == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 252 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 253 | return -1; |
| 254 | } |
| 255 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | |
| 257 | start: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 258 | s->rwstate = SSL_NOTHING; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 259 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 260 | /* s->s3->rrec.type - is the type of record |
| 261 | * s->s3->rrec.data - data |
| 262 | * s->s3->rrec.off - offset into 'data' for next read |
| 263 | * s->s3->rrec.length - number of bytes. */ |
| 264 | rr = &s->s3->rrec; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 266 | /* Check for timeout */ |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 267 | if (DTLSv1_handle_timeout(s) > 0) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 268 | goto start; |
| 269 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 270 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 271 | /* get new packet if necessary */ |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 272 | if (rr->length == 0) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 273 | ret = dtls1_get_record(s); |
| 274 | if (ret <= 0) { |
| 275 | ret = dtls1_read_failed(s, ret); |
| 276 | /* anything other than a timeout is an error */ |
| 277 | if (ret <= 0) { |
| 278 | return ret; |
| 279 | } else { |
| 280 | goto start; |
| 281 | } |
| 282 | } |
| 283 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 284 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 285 | /* we now have a packet which can be read and processed */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 286 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 287 | /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by |
| 288 | * ssl3_get_finished. */ |
David Benjamin | dc3da93 | 2015-03-12 15:09:02 -0400 | [diff] [blame] | 289 | if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE && |
| 290 | rr->type != SSL3_RT_ALERT) { |
| 291 | /* We now have an unexpected record between CCS and Finished. Most likely |
David Benjamin | 4417d05 | 2015-04-05 04:17:25 -0400 | [diff] [blame] | 292 | * the packets were reordered on their way. DTLS is unreliable, so drop the |
| 293 | * packet and expect the peer to retransmit. */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 294 | rr->length = 0; |
| 295 | goto start; |
| 296 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 297 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 298 | /* If the other end has shut down, throw anything we read away (even in |
| 299 | * 'peek' mode) */ |
| 300 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) { |
| 301 | rr->length = 0; |
| 302 | s->rwstate = SSL_NOTHING; |
| 303 | return 0; |
| 304 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 305 | |
| 306 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 307 | if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */ |
| 308 | /* make sure that we are not getting application data when we |
| 309 | * are doing a handshake for the first time */ |
| 310 | if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) && |
David Benjamin | b8a56f1 | 2014-12-23 11:41:02 -0500 | [diff] [blame] | 311 | (s->aead_read_ctx == NULL)) { |
| 312 | /* TODO(davidben): Is this check redundant with the handshake_func |
| 313 | * check? */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 314 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 315 | OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 316 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 319 | if (len <= 0) { |
| 320 | return len; |
| 321 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 322 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 323 | if ((unsigned int)len > rr->length) { |
| 324 | n = rr->length; |
| 325 | } else { |
| 326 | n = (unsigned int)len; |
| 327 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 328 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 329 | memcpy(buf, &(rr->data[rr->off]), n); |
| 330 | if (!peek) { |
| 331 | rr->length -= n; |
| 332 | rr->off += n; |
| 333 | if (rr->length == 0) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 334 | rr->off = 0; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 335 | /* The record has been consumed, so we may now clear the buffer. */ |
| 336 | ssl_read_buffer_discard(s); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 337 | } |
| 338 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 339 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 340 | return n; |
| 341 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 342 | |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 343 | /* If we get here, then type != rr->type. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 344 | |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 345 | /* If an alert record, process one alert out of the record. Note that we allow |
| 346 | * a single record to contain multiple alerts. */ |
| 347 | if (rr->type == SSL3_RT_ALERT) { |
| 348 | /* Alerts may not be fragmented. */ |
| 349 | if (rr->length < 2) { |
| 350 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 351 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT); |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 352 | goto f_err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 353 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 354 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 355 | if (s->msg_callback) { |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 356 | s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s, |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 357 | s->msg_callback_arg); |
| 358 | } |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 359 | const uint8_t alert_level = rr->data[rr->off++]; |
| 360 | const uint8_t alert_descr = rr->data[rr->off++]; |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 361 | rr->length -= 2; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 362 | |
| 363 | if (s->info_callback != NULL) { |
| 364 | cb = s->info_callback; |
| 365 | } else if (s->ctx->info_callback != NULL) { |
| 366 | cb = s->ctx->info_callback; |
| 367 | } |
| 368 | |
| 369 | if (cb != NULL) { |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 370 | uint16_t alert = (alert_level << 8) | alert_descr; |
| 371 | cb(s, SSL_CB_READ_ALERT, alert); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 372 | } |
| 373 | |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 374 | if (alert_level == SSL3_AL_WARNING) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 375 | s->s3->warn_alert = alert_descr; |
| 376 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) { |
| 377 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
| 378 | return 0; |
| 379 | } |
David Benjamin | 86058a2 | 2015-02-22 13:07:21 -0500 | [diff] [blame] | 380 | } else if (alert_level == SSL3_AL_FATAL) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 381 | char tmp[16]; |
| 382 | |
| 383 | s->rwstate = SSL_NOTHING; |
| 384 | s->s3->fatal_alert = alert_descr; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 385 | OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 386 | BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr); |
| 387 | ERR_add_error_data(2, "SSL alert number ", tmp); |
| 388 | s->shutdown |= SSL_RECEIVED_SHUTDOWN; |
| 389 | SSL_CTX_remove_session(s->ctx, s->session); |
| 390 | return 0; |
| 391 | } else { |
| 392 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 393 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 394 | goto f_err; |
| 395 | } |
| 396 | |
| 397 | goto start; |
| 398 | } |
| 399 | |
| 400 | if (s->shutdown & SSL_SENT_SHUTDOWN) { |
| 401 | /* but we have not received a shutdown */ |
| 402 | s->rwstate = SSL_NOTHING; |
| 403 | rr->length = 0; |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) { |
David Benjamin | f7f0f3a | 2015-03-02 21:09:31 -0500 | [diff] [blame] | 408 | /* 'Change Cipher Spec' is just a single byte, so we know exactly what the |
| 409 | * record payload has to look like */ |
| 410 | if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 411 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 412 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 413 | goto f_err; |
| 414 | } |
| 415 | |
| 416 | rr->length = 0; |
| 417 | |
| 418 | if (s->msg_callback) { |
| 419 | s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s, |
| 420 | s->msg_callback_arg); |
| 421 | } |
| 422 | |
| 423 | /* We can't process a CCS now, because previous handshake |
| 424 | * messages are still missing, so just drop it. |
| 425 | */ |
| 426 | if (!s->d1->change_cipher_spec_ok) { |
| 427 | goto start; |
| 428 | } |
| 429 | |
| 430 | s->d1->change_cipher_spec_ok = 0; |
| 431 | |
| 432 | s->s3->change_cipher_spec = 1; |
| 433 | if (!ssl3_do_change_cipher_spec(s)) { |
| 434 | goto err; |
| 435 | } |
| 436 | |
| 437 | /* do this whenever CCS is processed */ |
| 438 | dtls1_reset_seq_numbers(s, SSL3_CC_READ); |
| 439 | |
| 440 | goto start; |
| 441 | } |
| 442 | |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 443 | /* Unexpected handshake message. It may be a retransmitted Finished (the only |
| 444 | * post-CCS message). Otherwise, it's a pre-CCS handshake message from an |
| 445 | * unsupported renegotiation attempt. */ |
| 446 | if (rr->type == SSL3_RT_HANDSHAKE && !s->in_handshake) { |
| 447 | if (rr->length < DTLS1_HM_HEADER_LENGTH) { |
| 448 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 449 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD); |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 450 | goto f_err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 451 | } |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 452 | struct hm_header_st msg_hdr; |
| 453 | dtls1_get_message_header(&rr->data[rr->off], &msg_hdr); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 454 | |
David Benjamin | 7eaab4c | 2015-03-02 19:01:16 -0500 | [diff] [blame] | 455 | /* Ignore a stray Finished from the previous handshake. */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 456 | if (msg_hdr.type == SSL3_MT_FINISHED) { |
David Benjamin | 7eaab4c | 2015-03-02 19:01:16 -0500 | [diff] [blame] | 457 | if (msg_hdr.frag_off == 0) { |
| 458 | /* Retransmit our last flight of messages. If the peer sends the second |
| 459 | * Finished, they may not have received ours. Only do this for the |
| 460 | * first fragment, in case the Finished was fragmented. */ |
| 461 | if (dtls1_check_timeout_num(s) < 0) { |
| 462 | return -1; |
| 463 | } |
| 464 | |
| 465 | dtls1_retransmit_buffered_messages(s); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 468 | rr->length = 0; |
| 469 | goto start; |
| 470 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 471 | } |
| 472 | |
David Benjamin | ddb9f15 | 2015-02-03 15:44:39 -0500 | [diff] [blame] | 473 | /* We already handled these. */ |
| 474 | assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 475 | |
David Benjamin | ddb9f15 | 2015-02-03 15:44:39 -0500 | [diff] [blame] | 476 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 477 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 478 | |
| 479 | f_err: |
| 480 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 481 | err: |
| 482 | return -1; |
| 483 | } |
| 484 | |
David Benjamin | c933a47 | 2015-05-30 16:14:58 -0400 | [diff] [blame] | 485 | int dtls1_write_app_data(SSL *s, const void *buf_, int len) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 486 | int i; |
| 487 | |
| 488 | if (SSL_in_init(s) && !s->in_handshake) { |
| 489 | i = s->handshake_func(s); |
| 490 | if (i < 0) { |
| 491 | return i; |
| 492 | } |
| 493 | if (i == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 494 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 495 | return -1; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 500 | OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 501 | return -1; |
| 502 | } |
| 503 | |
David Benjamin | c933a47 | 2015-05-30 16:14:58 -0400 | [diff] [blame] | 504 | i = dtls1_write_bytes(s, SSL3_RT_APPLICATION_DATA, buf_, len, |
| 505 | dtls1_use_current_epoch); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 506 | return i; |
| 507 | } |
| 508 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 509 | /* Call this to write data in records of type 'type' It will return <= 0 if not |
| 510 | * all data has been sent or non-blocking IO. */ |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 511 | int dtls1_write_bytes(SSL *s, int type, const void *buf, int len, |
| 512 | enum dtls1_use_epoch_t use_epoch) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 513 | int i; |
| 514 | |
| 515 | assert(len <= SSL3_RT_MAX_PLAIN_LENGTH); |
| 516 | s->rwstate = SSL_NOTHING; |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 517 | i = do_dtls1_write(s, type, buf, len, use_epoch); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 518 | return i; |
| 519 | } |
| 520 | |
| 521 | static int do_dtls1_write(SSL *s, int type, const uint8_t *buf, |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 522 | unsigned int len, enum dtls1_use_epoch_t use_epoch) { |
David Benjamin | 883e49f | 2015-04-05 12:19:14 -0400 | [diff] [blame] | 523 | /* ssl3_write_pending drops the write if |BIO_write| fails in DTLS, so there |
| 524 | * is never pending data. */ |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 525 | assert(!ssl_write_buffer_is_pending(s)); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 526 | |
| 527 | /* If we have an alert to send, lets send it */ |
| 528 | if (s->s3->alert_dispatch) { |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 529 | int ret = s->method->ssl_dispatch_alert(s); |
| 530 | if (ret <= 0) { |
| 531 | return ret; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 532 | } |
| 533 | /* if it went, fall through and send more stuff */ |
| 534 | } |
| 535 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 536 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 537 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 538 | return -1; |
| 539 | } |
| 540 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 541 | if (len == 0) { |
| 542 | return 0; |
| 543 | } |
| 544 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 545 | size_t max_out = len + ssl_max_seal_overhead(s); |
| 546 | uint8_t *out; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 547 | size_t ciphertext_len; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 548 | if (!ssl_write_buffer_init(s, &out, max_out) || |
| 549 | !dtls_seal_record(s, out, &ciphertext_len, max_out, type, buf, len, |
| 550 | use_epoch)) { |
David Benjamin | 9417b76 | 2015-05-08 22:30:06 -0400 | [diff] [blame] | 551 | return -1; |
| 552 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame^] | 553 | ssl_write_buffer_set_len(s, ciphertext_len); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 554 | |
| 555 | /* memorize arguments so that ssl3_write_pending can detect bad write retries |
| 556 | * later */ |
| 557 | s->s3->wpend_tot = len; |
| 558 | s->s3->wpend_buf = buf; |
| 559 | s->s3->wpend_type = type; |
| 560 | s->s3->wpend_ret = len; |
| 561 | |
| 562 | /* we now just need to write the buffer */ |
| 563 | return ssl3_write_pending(s, type, buf, len); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 566 | int dtls1_dispatch_alert(SSL *s) { |
| 567 | int i, j; |
| 568 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
| 569 | uint8_t buf[DTLS1_AL_HEADER_LENGTH]; |
| 570 | uint8_t *ptr = &buf[0]; |
| 571 | |
| 572 | s->s3->alert_dispatch = 0; |
| 573 | |
| 574 | memset(buf, 0x00, sizeof(buf)); |
| 575 | *ptr++ = s->s3->send_alert[0]; |
| 576 | *ptr++ = s->s3->send_alert[1]; |
| 577 | |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 578 | i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), |
| 579 | dtls1_use_current_epoch); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 580 | if (i <= 0) { |
| 581 | s->s3->alert_dispatch = 1; |
| 582 | } else { |
| 583 | if (s->s3->send_alert[0] == SSL3_AL_FATAL) { |
| 584 | (void)BIO_flush(s->wbio); |
| 585 | } |
| 586 | |
| 587 | if (s->msg_callback) { |
| 588 | s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s, |
| 589 | s->msg_callback_arg); |
| 590 | } |
| 591 | |
| 592 | if (s->info_callback != NULL) { |
| 593 | cb = s->info_callback; |
| 594 | } else if (s->ctx->info_callback != NULL) { |
| 595 | cb = s->ctx->info_callback; |
| 596 | } |
| 597 | |
| 598 | if (cb != NULL) { |
| 599 | j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1]; |
| 600 | cb(s, SSL_CB_WRITE_ALERT, j); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | return i; |
| 605 | } |
| 606 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 607 | void dtls1_reset_seq_numbers(SSL *s, int rw) { |
| 608 | uint8_t *seq; |
| 609 | unsigned int seq_bytes = sizeof(s->s3->read_sequence); |
| 610 | |
| 611 | if (rw & SSL3_CC_READ) { |
| 612 | seq = s->s3->read_sequence; |
| 613 | s->d1->r_epoch++; |
David Benjamin | 0afbcc0 | 2015-04-05 04:06:20 -0400 | [diff] [blame] | 614 | memset(&s->d1->bitmap, 0, sizeof(DTLS1_BITMAP)); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 615 | } else { |
| 616 | seq = s->s3->write_sequence; |
| 617 | memcpy(s->d1->last_write_sequence, seq, sizeof(s->s3->write_sequence)); |
| 618 | s->d1->w_epoch++; |
| 619 | } |
| 620 | |
| 621 | memset(seq, 0x00, seq_bytes); |
| 622 | } |