Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [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 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 112 | #include <openssl/ssl.h> |
| 113 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 114 | #include <assert.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 115 | #include <string.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 116 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 117 | #include <openssl/bio.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 118 | #include <openssl/buf.h> |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 119 | #include <openssl/bytestring.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 120 | #include <openssl/mem.h> |
| 121 | #include <openssl/evp.h> |
| 122 | #include <openssl/err.h> |
| 123 | #include <openssl/rand.h> |
| 124 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 125 | #include "../crypto/internal.h" |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 126 | #include "internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 127 | |
| 128 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame^] | 129 | namespace bssl { |
| 130 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 131 | int dtls1_get_record(SSL *ssl) { |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 132 | again: |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 133 | switch (ssl->s3->recv_shutdown) { |
| 134 | case ssl_shutdown_none: |
| 135 | break; |
| 136 | case ssl_shutdown_fatal_alert: |
| 137 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 138 | return -1; |
| 139 | case ssl_shutdown_close_notify: |
| 140 | return 0; |
| 141 | } |
| 142 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 143 | /* Read a new packet if there is no unconsumed one. */ |
| 144 | if (ssl_read_buffer_len(ssl) == 0) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 145 | int read_ret = ssl_read_buffer_extend_to(ssl, 0 /* unused */); |
| 146 | if (read_ret < 0 && dtls1_is_timer_expired(ssl)) { |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 147 | /* Historically, timeouts were handled implicitly if the caller did not |
| 148 | * handle them. |
| 149 | * |
| 150 | * TODO(davidben): This was to support blocking sockets but affected |
| 151 | * non-blocking sockets. Can it be removed? */ |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 152 | int timeout_ret = DTLSv1_handle_timeout(ssl); |
| 153 | if (timeout_ret <= 0) { |
| 154 | return timeout_ret; |
| 155 | } |
| 156 | goto again; |
| 157 | } |
| 158 | if (read_ret <= 0) { |
| 159 | return read_ret; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 160 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 161 | } |
| 162 | assert(ssl_read_buffer_len(ssl) > 0); |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 163 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 164 | CBS body; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 165 | uint8_t type, alert; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 166 | size_t consumed; |
| 167 | enum ssl_open_record_t open_ret = |
| 168 | dtls_open_record(ssl, &type, &body, &consumed, &alert, |
| 169 | ssl_read_buffer(ssl), ssl_read_buffer_len(ssl)); |
| 170 | ssl_read_buffer_consume(ssl, consumed); |
| 171 | switch (open_ret) { |
| 172 | case ssl_open_record_partial: |
| 173 | /* Impossible in DTLS. */ |
| 174 | break; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 175 | |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 176 | case ssl_open_record_success: { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 177 | if (CBS_len(&body) > 0xffff) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 178 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 179 | return -1; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 180 | } |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 181 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 182 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 183 | rr->type = type; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 184 | rr->length = (uint16_t)CBS_len(&body); |
| 185 | rr->data = (uint8_t *)CBS_data(&body); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 186 | return 1; |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 187 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 188 | |
| 189 | case ssl_open_record_discard: |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 190 | goto again; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 191 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 192 | case ssl_open_record_close_notify: |
| 193 | return 0; |
| 194 | |
| 195 | case ssl_open_record_fatal_alert: |
| 196 | return -1; |
| 197 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 198 | case ssl_open_record_error: |
| 199 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 200 | return -1; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 203 | assert(0); |
| 204 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 205 | return -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 206 | } |
| 207 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 208 | int dtls1_read_app_data(SSL *ssl, int *out_got_handshake, uint8_t *buf, int len, |
| 209 | int peek) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 210 | assert(!SSL_in_init(ssl)); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 211 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 212 | *out_got_handshake = 0; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 213 | SSL3_RECORD *rr = &ssl->s3->rrec; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 214 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 215 | again: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 216 | if (rr->length == 0) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 217 | int ret = dtls1_get_record(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 218 | if (ret <= 0) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 219 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 223 | if (rr->type == SSL3_RT_HANDSHAKE) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 224 | /* Parse the first fragment header to determine if this is a pre-CCS or |
| 225 | * post-CCS handshake record. DTLS resets handshake message numbers on each |
| 226 | * handshake, so renegotiations and retransmissions are ambiguous. */ |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 227 | CBS cbs, body; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 228 | struct hm_header_st msg_hdr; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 229 | CBS_init(&cbs, rr->data, rr->length); |
| 230 | if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) { |
| 231 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 232 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD); |
| 233 | return -1; |
| 234 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 235 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 236 | if (msg_hdr.type == SSL3_MT_FINISHED && |
| 237 | msg_hdr.seq == ssl->d1->handshake_read_seq - 1) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 238 | if (msg_hdr.frag_off == 0) { |
| 239 | /* Retransmit our last flight of messages. If the peer sends the second |
| 240 | * Finished, they may not have received ours. Only do this for the |
| 241 | * first fragment, in case the Finished was fragmented. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 242 | if (dtls1_check_timeout_num(ssl) < 0) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 243 | return -1; |
| 244 | } |
| 245 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 246 | dtls1_retransmit_outgoing_messages(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 249 | rr->length = 0; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 250 | goto again; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 251 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 252 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 253 | /* Otherwise, this is a pre-CCS handshake message from an unsupported |
| 254 | * renegotiation attempt. Fall through to the error path. */ |
| 255 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 256 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 257 | if (rr->type != SSL3_RT_APPLICATION_DATA) { |
| 258 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 259 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 260 | return -1; |
| 261 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 262 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 263 | /* Discard empty records. */ |
| 264 | if (rr->length == 0) { |
| 265 | goto again; |
| 266 | } |
| 267 | |
| 268 | if (len <= 0) { |
| 269 | return len; |
| 270 | } |
| 271 | |
| 272 | if ((unsigned)len > rr->length) { |
| 273 | len = rr->length; |
| 274 | } |
| 275 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 276 | OPENSSL_memcpy(buf, rr->data, len); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 277 | if (!peek) { |
| 278 | /* TODO(davidben): Should the record be truncated instead? This is a |
| 279 | * datagram transport. See https://crbug.com/boringssl/65. */ |
| 280 | rr->length -= len; |
| 281 | rr->data += len; |
| 282 | if (rr->length == 0) { |
| 283 | /* The record has been consumed, so we may now clear the buffer. */ |
| 284 | ssl_read_buffer_discard(ssl); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return len; |
| 289 | } |
| 290 | |
| 291 | int dtls1_read_change_cipher_spec(SSL *ssl) { |
| 292 | SSL3_RECORD *rr = &ssl->s3->rrec; |
| 293 | |
| 294 | again: |
| 295 | if (rr->length == 0) { |
| 296 | int ret = dtls1_get_record(ssl); |
| 297 | if (ret <= 0) { |
| 298 | return ret; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* Drop handshake records silently. The epochs match, so this must be a |
| 303 | * retransmit of a message we already received. */ |
| 304 | if (rr->type == SSL3_RT_HANDSHAKE) { |
| 305 | rr->length = 0; |
| 306 | goto again; |
| 307 | } |
| 308 | |
| 309 | /* Other record types are illegal in this epoch. Note all application data |
| 310 | * records come in the encrypted epoch. */ |
| 311 | if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) { |
| 312 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 313 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | if (rr->length != 1 || rr->data[0] != SSL3_MT_CCS) { |
| 318 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
| 319 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 320 | return -1; |
| 321 | } |
| 322 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 323 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, |
| 324 | rr->length); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 325 | |
| 326 | rr->length = 0; |
| 327 | ssl_read_buffer_discard(ssl); |
| 328 | return 1; |
| 329 | } |
| 330 | |
| 331 | void dtls1_read_close_notify(SSL *ssl) { |
| 332 | /* Bidirectional shutdown doesn't make sense for an unordered transport. DTLS |
| 333 | * alerts also aren't delivered reliably, so we may even time out because the |
| 334 | * peer never received our close_notify. Report to the caller that the channel |
| 335 | * has fully shut down. */ |
| 336 | if (ssl->s3->recv_shutdown == ssl_shutdown_none) { |
| 337 | ssl->s3->recv_shutdown = ssl_shutdown_close_notify; |
| 338 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 341 | int dtls1_write_app_data(SSL *ssl, int *out_needs_handshake, const uint8_t *buf, |
| 342 | int len) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 343 | assert(!SSL_in_init(ssl)); |
Robert Sloan | e56da3e | 2017-06-26 08:26:42 -0700 | [diff] [blame] | 344 | *out_needs_handshake = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 345 | |
| 346 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 347 | OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 348 | return -1; |
| 349 | } |
| 350 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 351 | if (len < 0) { |
| 352 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH); |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | if (len == 0) { |
| 357 | return 0; |
| 358 | } |
| 359 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 360 | int ret = dtls1_write_record(ssl, SSL3_RT_APPLICATION_DATA, buf, (size_t)len, |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 361 | dtls1_use_current_epoch); |
| 362 | if (ret <= 0) { |
| 363 | return ret; |
| 364 | } |
| 365 | return len; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 366 | } |
| 367 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 368 | int dtls1_write_record(SSL *ssl, int type, const uint8_t *buf, size_t len, |
| 369 | enum dtls1_use_epoch_t use_epoch) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 370 | assert(len <= SSL3_RT_MAX_PLAIN_LENGTH); |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 371 | /* There should never be a pending write buffer in DTLS. One can't write half |
| 372 | * a datagram, so the write buffer is always dropped in |
| 373 | * |ssl_write_buffer_flush|. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 374 | assert(!ssl_write_buffer_is_pending(ssl)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 375 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 376 | if (len > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 377 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 378 | return -1; |
| 379 | } |
| 380 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 381 | size_t max_out = len + SSL_max_seal_overhead(ssl); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 382 | uint8_t *out; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 383 | size_t ciphertext_len; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 384 | if (!ssl_write_buffer_init(ssl, &out, max_out) || |
| 385 | !dtls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len, |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 386 | use_epoch)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 387 | ssl_write_buffer_clear(ssl); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 388 | return -1; |
| 389 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 390 | ssl_write_buffer_set_len(ssl, ciphertext_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 391 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 392 | int ret = ssl_write_buffer_flush(ssl); |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 393 | if (ret <= 0) { |
| 394 | return ret; |
| 395 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 396 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 399 | int dtls1_dispatch_alert(SSL *ssl) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 400 | int ret = dtls1_write_record(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2, |
| 401 | dtls1_use_current_epoch); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 402 | if (ret <= 0) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 403 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 404 | } |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 405 | ssl->s3->alert_dispatch = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 406 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 407 | /* If the alert is fatal, flush the BIO now. */ |
| 408 | if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) { |
| 409 | BIO_flush(ssl->wbio); |
| 410 | } |
| 411 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 412 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert, |
| 413 | 2); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 414 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 415 | int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1]; |
| 416 | ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 417 | |
| 418 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 419 | } |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame^] | 420 | |
| 421 | } // namespace bssl |