blob: b86615626589f27de65ae05cee74f15962c008b4 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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 Benjamin9e4e01e2015-09-15 01:48:04 -0400112#include <openssl/ssl.h>
113
Adam Langley95c29f32014-06-20 12:00:00 -0700114#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400115#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700116
David Benjamin15aa8952016-05-10 20:51:34 -0400117#include <openssl/bio.h>
David Benjaminc6604172016-06-02 16:38:35 -0400118#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119#include <openssl/mem.h>
120#include <openssl/evp.h>
121#include <openssl/err.h>
122#include <openssl/rand.h>
123
David Benjamin17cf2cb2016-12-13 01:07:13 -0500124#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400125#include "internal.h"
Adam Langley71d8a082014-12-13 16:28:18 -0800126
127
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700128BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -0400129
David Benjamind9229f92017-10-06 17:36:20 -0400130ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out,
131 size_t *out_consumed, uint8_t *out_alert,
132 Span<uint8_t> in) {
David Benjaminc79845c2016-03-10 01:28:00 -0500133 assert(!SSL_in_init(ssl));
David Benjaminaf62d612016-06-01 20:49:50 -0400134
David Benjamind9229f92017-10-06 17:36:20 -0400135 uint8_t type;
136 Span<uint8_t> record;
137 auto ret = dtls_open_record(ssl, &type, &record, out_consumed, out_alert, in);
138 if (ret != ssl_open_record_success) {
139 return ret;
David Benjaminaf62d612016-06-01 20:49:50 -0400140 }
141
David Benjamind9229f92017-10-06 17:36:20 -0400142 if (type == SSL3_RT_HANDSHAKE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400143 // Parse the first fragment header to determine if this is a pre-CCS or
144 // post-CCS handshake record. DTLS resets handshake message numbers on each
145 // handshake, so renegotiations and retransmissions are ambiguous.
David Benjaminaf62d612016-06-01 20:49:50 -0400146 CBS cbs, body;
147 struct hm_header_st msg_hdr;
David Benjamind9229f92017-10-06 17:36:20 -0400148 CBS_init(&cbs, record.data(), record.size());
David Benjaminaf62d612016-06-01 20:49:50 -0400149 if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) {
David Benjaminaf62d612016-06-01 20:49:50 -0400150 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
David Benjamind9229f92017-10-06 17:36:20 -0400151 *out_alert = SSL_AD_DECODE_ERROR;
152 return ssl_open_record_error;
David Benjaminaf62d612016-06-01 20:49:50 -0400153 }
154
David Benjamin02edcd02016-07-27 17:40:37 -0400155 if (msg_hdr.type == SSL3_MT_FINISHED &&
David Benjamin4497e582016-07-27 17:51:49 -0400156 msg_hdr.seq == ssl->d1->handshake_read_seq - 1) {
David Benjaminaf62d612016-06-01 20:49:50 -0400157 if (msg_hdr.frag_off == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400158 // Retransmit our last flight of messages. If the peer sends the second
159 // Finished, they may not have received ours. Only do this for the
160 // first fragment, in case the Finished was fragmented.
David Benjamin9bbdf582017-08-02 19:46:29 -0400161 if (!dtls1_check_timeout_num(ssl)) {
David Benjamind9229f92017-10-06 17:36:20 -0400162 *out_alert = 0; // TODO(davidben): Send an alert?
163 return ssl_open_record_error;
David Benjaminaf62d612016-06-01 20:49:50 -0400164 }
165
David Benjaminaad50db2016-06-23 17:49:12 -0400166 dtls1_retransmit_outgoing_messages(ssl);
David Benjaminaf62d612016-06-01 20:49:50 -0400167 }
David Benjamind9229f92017-10-06 17:36:20 -0400168 return ssl_open_record_discard;
David Benjaminaf62d612016-06-01 20:49:50 -0400169 }
170
David Benjaminc11ea9422017-08-29 16:33:21 -0400171 // Otherwise, this is a pre-CCS handshake message from an unsupported
172 // renegotiation attempt. Fall through to the error path.
David Benjaminaf62d612016-06-01 20:49:50 -0400173 }
174
David Benjamind9229f92017-10-06 17:36:20 -0400175 if (type != SSL3_RT_APPLICATION_DATA) {
David Benjaminaf62d612016-06-01 20:49:50 -0400176 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
David Benjamind9229f92017-10-06 17:36:20 -0400177 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
178 return ssl_open_record_error;
David Benjaminaf62d612016-06-01 20:49:50 -0400179 }
180
David Benjamind9229f92017-10-06 17:36:20 -0400181 if (record.empty()) {
182 return ssl_open_record_discard;
David Benjaminaf62d612016-06-01 20:49:50 -0400183 }
184
David Benjamind9229f92017-10-06 17:36:20 -0400185 *out = record;
186 return ssl_open_record_success;
David Benjamina6022772015-05-30 16:22:10 -0400187}
188
David Benjamin4bd32a82021-05-14 17:06:29 -0400189int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake,
190 size_t *out_bytes_written, Span<const uint8_t> in) {
David Benjamind7ac1432016-03-10 00:41:25 -0500191 assert(!SSL_in_init(ssl));
David Benjamin74795b32017-08-31 15:13:12 -0400192 *out_needs_handshake = false;
Adam Langley71d8a082014-12-13 16:28:18 -0800193
David Benjamine8d07462017-10-12 18:09:20 -0400194 if (ssl->s3->write_shutdown != ssl_shutdown_none) {
195 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
196 return -1;
197 }
198
David Benjamin4bd32a82021-05-14 17:06:29 -0400199 // DTLS does not split the input across records.
200 if (in.size() > SSL3_RT_MAX_PLAIN_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400201 OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
Adam Langley71d8a082014-12-13 16:28:18 -0800202 return -1;
203 }
204
David Benjamin4bd32a82021-05-14 17:06:29 -0400205 if (in.empty()) {
206 *out_bytes_written = 0;
207 return 1;
David Benjamin45d45c12016-06-07 15:20:49 -0400208 }
209
David Benjamin4bd32a82021-05-14 17:06:29 -0400210 int ret = dtls1_write_record(ssl, SSL3_RT_APPLICATION_DATA, in,
David Benjamin45d45c12016-06-07 15:20:49 -0400211 dtls1_use_current_epoch);
212 if (ret <= 0) {
213 return ret;
214 }
David Benjamin4bd32a82021-05-14 17:06:29 -0400215 *out_bytes_written = in.size();
216 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800217}
218
David Benjamin4bd32a82021-05-14 17:06:29 -0400219int dtls1_write_record(SSL *ssl, int type, Span<const uint8_t> in,
David Benjamin45d45c12016-06-07 15:20:49 -0400220 enum dtls1_use_epoch_t use_epoch) {
David Benjaminea712e32017-10-13 16:50:39 -0400221 SSLBuffer *buf = &ssl->s3->write_buffer;
David Benjamin4bd32a82021-05-14 17:06:29 -0400222 assert(in.size() <= SSL3_RT_MAX_PLAIN_LENGTH);
David Benjaminc11ea9422017-08-29 16:33:21 -0400223 // There should never be a pending write buffer in DTLS. One can't write half
224 // a datagram, so the write buffer is always dropped in
225 // |ssl_write_buffer_flush|.
David Benjaminea712e32017-10-13 16:50:39 -0400226 assert(buf->empty());
Adam Langley71d8a082014-12-13 16:28:18 -0800227
David Benjamin4bd32a82021-05-14 17:06:29 -0400228 if (in.size() > SSL3_RT_MAX_PLAIN_LENGTH) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400229 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -0500230 return -1;
231 }
232
David Benjamin31a07792015-03-03 14:20:26 -0500233 size_t ciphertext_len;
David Benjaminea712e32017-10-13 16:50:39 -0400234 if (!buf->EnsureCap(ssl_seal_align_prefix_len(ssl),
David Benjamin4bd32a82021-05-14 17:06:29 -0400235 in.size() + SSL_max_seal_overhead(ssl)) ||
David Benjaminea712e32017-10-13 16:50:39 -0400236 !dtls_seal_record(ssl, buf->remaining().data(), &ciphertext_len,
David Benjamin4bd32a82021-05-14 17:06:29 -0400237 buf->remaining().size(), type, in.data(), in.size(),
238 use_epoch)) {
David Benjaminea712e32017-10-13 16:50:39 -0400239 buf->Clear();
David Benjamin9417b762015-05-08 22:30:06 -0400240 return -1;
241 }
David Benjaminea712e32017-10-13 16:50:39 -0400242 buf->DidWrite(ciphertext_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800243
David Benjamin0d56f882015-12-19 17:05:56 -0500244 int ret = ssl_write_buffer_flush(ssl);
David Benjamin2e0901b2015-11-02 17:50:10 -0500245 if (ret <= 0) {
246 return ret;
247 }
David Benjamin45d45c12016-06-07 15:20:49 -0400248 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800249}
250
David Benjamin0d56f882015-12-19 17:05:56 -0500251int dtls1_dispatch_alert(SSL *ssl) {
David Benjamin4bd32a82021-05-14 17:06:29 -0400252 int ret = dtls1_write_record(ssl, SSL3_RT_ALERT, ssl->s3->send_alert,
David Benjamin45d45c12016-06-07 15:20:49 -0400253 dtls1_use_current_epoch);
David Benjamina8571592016-03-10 01:41:55 -0500254 if (ret <= 0) {
David Benjamina8571592016-03-10 01:41:55 -0500255 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800256 }
David Benjaminee4888c2019-07-21 12:16:52 -0400257 ssl->s3->alert_dispatch = false;
Adam Langley71d8a082014-12-13 16:28:18 -0800258
David Benjaminc11ea9422017-08-29 16:33:21 -0400259 // If the alert is fatal, flush the BIO now.
David Benjamina8571592016-03-10 01:41:55 -0500260 if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
David Benjamin50596f82018-07-02 19:47:27 -0400261 BIO_flush(ssl->wbio.get());
David Benjamina8571592016-03-10 01:41:55 -0500262 }
263
David Benjaminc64d1232017-10-04 18:14:28 -0400264 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert);
David Benjamina8571592016-03-10 01:41:55 -0500265
David Benjamin4e9cc712016-06-01 20:16:03 -0400266 int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
267 ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert);
David Benjamina8571592016-03-10 01:41:55 -0500268
269 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800270}
David Benjamin86e95b82017-07-18 16:34:25 -0400271
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700272BSSL_NAMESPACE_END