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 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 112 | #include <openssl/ssl.h> |
| 113 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 114 | #include <assert.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 115 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 116 | |
David Benjamin | 15aa895 | 2016-05-10 20:51:34 -0400 | [diff] [blame] | 117 | #include <openssl/bio.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 118 | #include <openssl/buf.h> |
| 119 | #include <openssl/mem.h> |
| 120 | #include <openssl/evp.h> |
| 121 | #include <openssl/err.h> |
| 122 | #include <openssl/rand.h> |
| 123 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 124 | #include "internal.h" |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 125 | |
| 126 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 127 | static int do_dtls1_write(SSL *ssl, int type, const uint8_t *buf, |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 128 | unsigned int len, enum dtls1_use_epoch_t use_epoch); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 129 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 130 | /* dtls1_get_record reads a new input record. On success, it places it in |
| 131 | * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if |
| 132 | * more data is needed. */ |
| 133 | static int dtls1_get_record(SSL *ssl) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | again: |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 135 | switch (ssl->s3->recv_shutdown) { |
| 136 | case ssl_shutdown_none: |
| 137 | break; |
| 138 | case ssl_shutdown_fatal_alert: |
| 139 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 140 | return -1; |
| 141 | case ssl_shutdown_close_notify: |
David Benjamin | 8f73135 | 2016-03-10 01:15:15 -0500 | [diff] [blame] | 142 | return 0; |
David Benjamin | 8f73135 | 2016-03-10 01:15:15 -0500 | [diff] [blame] | 143 | } |
| 144 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 145 | /* Read a new packet if there is no unconsumed one. */ |
| 146 | if (ssl_read_buffer_len(ssl) == 0) { |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 147 | int read_ret = ssl_read_buffer_extend_to(ssl, 0 /* unused */); |
| 148 | if (read_ret < 0 && dtls1_is_timer_expired(ssl)) { |
David Benjamin | 15aa895 | 2016-05-10 20:51:34 -0400 | [diff] [blame] | 149 | /* For blocking BIOs, retransmits must be handled internally. */ |
| 150 | int timeout_ret = DTLSv1_handle_timeout(ssl); |
| 151 | if (timeout_ret <= 0) { |
| 152 | return timeout_ret; |
| 153 | } |
| 154 | goto again; |
| 155 | } |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 156 | if (read_ret <= 0) { |
| 157 | return read_ret; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 158 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 159 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 160 | assert(ssl_read_buffer_len(ssl) > 0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 161 | |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 162 | CBS body; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 163 | uint8_t type, alert; |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 164 | size_t consumed; |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 165 | enum ssl_open_record_t open_ret = |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 166 | dtls_open_record(ssl, &type, &body, &consumed, &alert, |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 167 | ssl_read_buffer(ssl), ssl_read_buffer_len(ssl)); |
| 168 | ssl_read_buffer_consume(ssl, consumed); |
| 169 | switch (open_ret) { |
| 170 | case ssl_open_record_partial: |
| 171 | /* Impossible in DTLS. */ |
| 172 | break; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 173 | |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 174 | case ssl_open_record_success: |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 175 | if (CBS_len(&body) > 0xffff) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 176 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 181 | rr->type = type; |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 182 | rr->length = (uint16_t)CBS_len(&body); |
| 183 | rr->data = (uint8_t *)CBS_data(&body); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 184 | return 1; |
| 185 | |
| 186 | case ssl_open_record_discard: |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 187 | goto again; |
| 188 | |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 189 | case ssl_open_record_close_notify: |
| 190 | return 0; |
| 191 | |
| 192 | case ssl_open_record_fatal_alert: |
| 193 | return -1; |
| 194 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 195 | case ssl_open_record_error: |
| 196 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 197 | return -1; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 198 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 199 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 200 | assert(0); |
| 201 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 202 | return -1; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 203 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 204 | |
David Benjamin | a602277 | 2015-05-30 16:22:10 -0400 | [diff] [blame] | 205 | int dtls1_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) { |
David Benjamin | c79845c | 2016-03-10 01:28:00 -0500 | [diff] [blame] | 206 | assert(!SSL_in_init(ssl)); |
David Benjamin | a602277 | 2015-05-30 16:22:10 -0400 | [diff] [blame] | 207 | return dtls1_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek); |
| 208 | } |
| 209 | |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 210 | int dtls1_read_change_cipher_spec(SSL *ssl) { |
David Benjamin | 585320c | 2016-05-10 20:46:16 -0400 | [diff] [blame^] | 211 | SSL3_RECORD *rr = &ssl->s3->rrec; |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 212 | |
David Benjamin | 585320c | 2016-05-10 20:46:16 -0400 | [diff] [blame^] | 213 | again: |
| 214 | if (rr->length == 0) { |
| 215 | int ret = dtls1_get_record(ssl); |
| 216 | if (ret <= 0) { |
| 217 | return ret; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* Drop handshake records silently. The epochs match, so this must be a |
| 222 | * retransmit of a message we already received. */ |
| 223 | if (rr->type == SSL3_RT_HANDSHAKE) { |
| 224 | rr->length = 0; |
| 225 | goto again; |
| 226 | } |
| 227 | |
| 228 | /* Other record types are illegal in this epoch. Note all application data |
| 229 | * records come in the encrypted epoch. */ |
| 230 | if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) { |
| 231 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 232 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | if (rr->length != 1 || rr->data[0] != SSL3_MT_CCS) { |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 237 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
| 238 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 239 | return -1; |
| 240 | } |
| 241 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 242 | ssl_do_msg_callback(ssl, 0 /* read */, ssl->version, |
David Benjamin | 585320c | 2016-05-10 20:46:16 -0400 | [diff] [blame^] | 243 | SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, rr->length); |
| 244 | |
| 245 | rr->length = 0; |
| 246 | ssl_read_buffer_discard(ssl); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 247 | return 1; |
| 248 | } |
| 249 | |
David Benjamin | a602277 | 2015-05-30 16:22:10 -0400 | [diff] [blame] | 250 | void dtls1_read_close_notify(SSL *ssl) { |
David Benjamin | e9cb2ec | 2015-08-22 11:31:33 -0400 | [diff] [blame] | 251 | /* Bidirectional shutdown doesn't make sense for an unordered transport. DTLS |
| 252 | * alerts also aren't delivered reliably, so we may even time out because the |
| 253 | * peer never received our close_notify. Report to the caller that the channel |
| 254 | * has fully shut down. */ |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 255 | if (ssl->s3->recv_shutdown == ssl_shutdown_none) { |
| 256 | ssl->s3->recv_shutdown = ssl_shutdown_close_notify; |
| 257 | } |
David Benjamin | a602277 | 2015-05-30 16:22:10 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 260 | /* Return up to 'len' payload bytes received in 'type' records. |
| 261 | * 'type' is one of the following: |
| 262 | * |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 263 | * - SSL3_RT_HANDSHAKE (when dtls1_get_message calls us) |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 264 | * - SSL3_RT_APPLICATION_DATA (when dtls1_read_app_data calls us) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | * |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 266 | * If we don't have stored data to work from, read a DTLS record first (possibly |
| 267 | * multiple records if we still don't have anything to return). |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 268 | * |
| 269 | * This function must handle any surprises the peer may have for us, such as |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 270 | * Alert records (e.g. close_notify) and out of records. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 271 | int dtls1_read_bytes(SSL *ssl, int type, unsigned char *buf, int len, int peek) { |
David Benjamin | c79845c | 2016-03-10 01:28:00 -0500 | [diff] [blame] | 272 | int al, ret; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 273 | unsigned int n; |
| 274 | SSL3_RECORD *rr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 275 | |
David Benjamin | 585320c | 2016-05-10 20:46:16 -0400 | [diff] [blame^] | 276 | if ((type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) || |
David Benjamin | e9cb2ec | 2015-08-22 11:31:33 -0400 | [diff] [blame] | 277 | (peek && type != SSL3_RT_APPLICATION_DATA)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 278 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 279 | return -1; |
| 280 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 281 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 282 | start: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 283 | /* ssl->s3->rrec.type - is the type of record |
| 284 | * ssl->s3->rrec.data - data |
| 285 | * ssl->s3->rrec.off - offset into 'data' for next read |
| 286 | * ssl->s3->rrec.length - number of bytes. */ |
| 287 | rr = &ssl->s3->rrec; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 288 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 289 | /* get new packet if necessary */ |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 290 | if (rr->length == 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 291 | ret = dtls1_get_record(ssl); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 292 | if (ret <= 0) { |
David Benjamin | 15aa895 | 2016-05-10 20:51:34 -0400 | [diff] [blame] | 293 | return ret; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 296 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 297 | /* we now have a packet which can be read and processed */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 298 | |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 299 | if (type == rr->type) { |
David Benjamin | 4cf369b | 2015-08-22 01:35:43 -0400 | [diff] [blame] | 300 | /* Discard empty records. */ |
| 301 | if (rr->length == 0) { |
| 302 | goto start; |
| 303 | } |
| 304 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 305 | if (len <= 0) { |
| 306 | return len; |
| 307 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 308 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 309 | if ((unsigned int)len > rr->length) { |
| 310 | n = rr->length; |
| 311 | } else { |
| 312 | n = (unsigned int)len; |
| 313 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 314 | |
David Benjamin | 7fc0100 | 2015-12-06 15:48:22 -0500 | [diff] [blame] | 315 | memcpy(buf, rr->data, n); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 316 | if (!peek) { |
| 317 | rr->length -= n; |
David Benjamin | 7fc0100 | 2015-12-06 15:48:22 -0500 | [diff] [blame] | 318 | rr->data += n; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 319 | if (rr->length == 0) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 320 | /* The record has been consumed, so we may now clear the buffer. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 321 | ssl_read_buffer_discard(ssl); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 322 | } |
| 323 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 324 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 325 | return n; |
| 326 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 327 | |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 328 | /* If we get here, then type != rr->type. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 329 | |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 330 | /* Cross-epoch records are discarded, but we may receive out-of-order |
| 331 | * application data between ChangeCipherSpec and Finished or a ChangeCipherSpec |
| 332 | * before the appropriate point in the handshake. Those must be silently |
| 333 | * discarded. |
| 334 | * |
| 335 | * However, only allow the out-of-order records in the correct epoch. |
| 336 | * Application data must come in the encrypted epoch, and ChangeCipherSpec in |
| 337 | * the unencrypted epoch (we never renegotiate). Other cases fall through and |
| 338 | * fail with a fatal error. */ |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 339 | if ((rr->type == SSL3_RT_APPLICATION_DATA && |
| 340 | ssl->s3->aead_read_ctx != NULL) || |
| 341 | (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC && |
| 342 | ssl->s3->aead_read_ctx == NULL)) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 343 | rr->length = 0; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 344 | goto start; |
| 345 | } |
| 346 | |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 347 | if (rr->type == SSL3_RT_HANDSHAKE) { |
David Benjamin | 585320c | 2016-05-10 20:46:16 -0400 | [diff] [blame^] | 348 | assert(type == SSL3_RT_APPLICATION_DATA); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 349 | /* Parse the first fragment header to determine if this is a pre-CCS or |
| 350 | * post-CCS handshake record. DTLS resets handshake message numbers on each |
| 351 | * handshake, so renegotiations and retransmissions are ambiguous. */ |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 352 | if (rr->length < DTLS1_HM_HEADER_LENGTH) { |
| 353 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 354 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD); |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 355 | goto f_err; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 356 | } |
David Benjamin | 0ea8dda | 2015-01-31 20:33:40 -0500 | [diff] [blame] | 357 | struct hm_header_st msg_hdr; |
David Benjamin | 7fc0100 | 2015-12-06 15:48:22 -0500 | [diff] [blame] | 358 | dtls1_get_message_header(rr->data, &msg_hdr); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 359 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 360 | if (msg_hdr.type == SSL3_MT_FINISHED) { |
David Benjamin | 7eaab4c | 2015-03-02 19:01:16 -0500 | [diff] [blame] | 361 | if (msg_hdr.frag_off == 0) { |
| 362 | /* Retransmit our last flight of messages. If the peer sends the second |
| 363 | * Finished, they may not have received ours. Only do this for the |
| 364 | * first fragment, in case the Finished was fragmented. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 365 | if (dtls1_check_timeout_num(ssl) < 0) { |
David Benjamin | 7eaab4c | 2015-03-02 19:01:16 -0500 | [diff] [blame] | 366 | return -1; |
| 367 | } |
| 368 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 369 | dtls1_retransmit_buffered_messages(ssl); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 372 | rr->length = 0; |
| 373 | goto start; |
| 374 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 375 | |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 376 | /* Otherwise, this is a pre-CCS handshake message from an unsupported |
| 377 | * renegotiation attempt. Fall through to the error path. */ |
| 378 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 379 | |
David Benjamin | ddb9f15 | 2015-02-03 15:44:39 -0500 | [diff] [blame] | 380 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 381 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 382 | |
| 383 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 384 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 385 | return -1; |
| 386 | } |
| 387 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 388 | int dtls1_write_app_data(SSL *ssl, const void *buf_, int len) { |
David Benjamin | d7ac143 | 2016-03-10 00:41:25 -0500 | [diff] [blame] | 389 | assert(!SSL_in_init(ssl)); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 390 | |
| 391 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 392 | OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 393 | return -1; |
| 394 | } |
| 395 | |
David Benjamin | d7ac143 | 2016-03-10 00:41:25 -0500 | [diff] [blame] | 396 | return dtls1_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf_, len, |
| 397 | dtls1_use_current_epoch); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 400 | /* Call this to write data in records of type 'type' It will return <= 0 if not |
| 401 | * all data has been sent or non-blocking IO. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 402 | int dtls1_write_bytes(SSL *ssl, int type, const void *buf, int len, |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 403 | enum dtls1_use_epoch_t use_epoch) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 404 | assert(len <= SSL3_RT_MAX_PLAIN_LENGTH); |
David Benjamin | d7ac143 | 2016-03-10 00:41:25 -0500 | [diff] [blame] | 405 | return do_dtls1_write(ssl, type, buf, len, use_epoch); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 406 | } |
| 407 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 408 | static int do_dtls1_write(SSL *ssl, int type, const uint8_t *buf, |
David Benjamin | 3e3090d | 2015-04-05 12:48:30 -0400 | [diff] [blame] | 409 | unsigned int len, enum dtls1_use_epoch_t use_epoch) { |
David Benjamin | 2e0901b | 2015-11-02 17:50:10 -0500 | [diff] [blame] | 410 | /* There should never be a pending write buffer in DTLS. One can't write half |
| 411 | * a datagram, so the write buffer is always dropped in |
| 412 | * |ssl_write_buffer_flush|. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 413 | assert(!ssl_write_buffer_is_pending(ssl)); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 414 | |
| 415 | /* If we have an alert to send, lets send it */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 416 | if (ssl->s3->alert_dispatch) { |
| 417 | int ret = ssl->method->ssl_dispatch_alert(ssl); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 418 | if (ret <= 0) { |
| 419 | return ret; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 420 | } |
| 421 | /* if it went, fall through and send more stuff */ |
| 422 | } |
| 423 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 424 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 425 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 426 | return -1; |
| 427 | } |
| 428 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 429 | if (len == 0) { |
| 430 | return 0; |
| 431 | } |
| 432 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 433 | size_t max_out = len + ssl_max_seal_overhead(ssl); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 434 | uint8_t *out; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 435 | size_t ciphertext_len; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 436 | if (!ssl_write_buffer_init(ssl, &out, max_out) || |
| 437 | !dtls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len, |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 438 | use_epoch)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 439 | ssl_write_buffer_clear(ssl); |
David Benjamin | 9417b76 | 2015-05-08 22:30:06 -0400 | [diff] [blame] | 440 | return -1; |
| 441 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 442 | ssl_write_buffer_set_len(ssl, ciphertext_len); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 443 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 444 | int ret = ssl_write_buffer_flush(ssl); |
David Benjamin | 2e0901b | 2015-11-02 17:50:10 -0500 | [diff] [blame] | 445 | if (ret <= 0) { |
| 446 | return ret; |
| 447 | } |
| 448 | return (int)len; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 449 | } |
| 450 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 451 | int dtls1_dispatch_alert(SSL *ssl) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 452 | ssl->s3->alert_dispatch = 0; |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 453 | int ret = do_dtls1_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2, |
| 454 | dtls1_use_current_epoch); |
| 455 | if (ret <= 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 456 | ssl->s3->alert_dispatch = 1; |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 457 | return ret; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 458 | } |
| 459 | |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 460 | /* If the alert is fatal, flush the BIO now. */ |
| 461 | if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) { |
| 462 | BIO_flush(ssl->wbio); |
| 463 | } |
| 464 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 465 | ssl_do_msg_callback(ssl, 1 /* write */, ssl->version, SSL3_RT_ALERT, |
| 466 | ssl->s3->send_alert, 2); |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 467 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 468 | int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1]; |
| 469 | ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert); |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 470 | |
| 471 | return 1; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 472 | } |