David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [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 | |
| 112 | #include <openssl/ssl.h> |
| 113 | |
| 114 | #include <assert.h> |
| 115 | #include <string.h> |
| 116 | |
| 117 | #include <openssl/bytestring.h> |
| 118 | #include <openssl/err.h> |
| 119 | |
| 120 | #include "internal.h" |
David Benjamin | 95d7a49 | 2016-06-09 16:38:00 -0400 | [diff] [blame] | 121 | #include "../crypto/internal.h" |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 122 | |
| 123 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 124 | namespace bssl { |
| 125 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 126 | // to_u64_be treats |in| as a 8-byte big-endian integer and returns the value as |
| 127 | // a |uint64_t|. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 128 | static uint64_t to_u64_be(const uint8_t in[8]) { |
| 129 | uint64_t ret = 0; |
| 130 | unsigned i; |
| 131 | for (i = 0; i < 8; i++) { |
| 132 | ret <<= 8; |
| 133 | ret |= in[i]; |
| 134 | } |
| 135 | return ret; |
| 136 | } |
| 137 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 138 | // dtls1_bitmap_should_discard returns one if |seq_num| has been seen in |
| 139 | // |bitmap| or is stale. Otherwise it returns zero. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 140 | static int dtls1_bitmap_should_discard(DTLS1_BITMAP *bitmap, |
| 141 | const uint8_t seq_num[8]) { |
| 142 | const unsigned kWindowSize = sizeof(bitmap->map) * 8; |
| 143 | |
| 144 | uint64_t seq_num_u = to_u64_be(seq_num); |
| 145 | if (seq_num_u > bitmap->max_seq_num) { |
| 146 | return 0; |
| 147 | } |
| 148 | uint64_t idx = bitmap->max_seq_num - seq_num_u; |
| 149 | return idx >= kWindowSize || (bitmap->map & (((uint64_t)1) << idx)); |
| 150 | } |
| 151 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 152 | // dtls1_bitmap_record updates |bitmap| to record receipt of sequence number |
| 153 | // |seq_num|. It slides the window forward if needed. It is an error to call |
| 154 | // this function on a stale sequence number. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 155 | static void dtls1_bitmap_record(DTLS1_BITMAP *bitmap, |
| 156 | const uint8_t seq_num[8]) { |
| 157 | const unsigned kWindowSize = sizeof(bitmap->map) * 8; |
| 158 | |
| 159 | uint64_t seq_num_u = to_u64_be(seq_num); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 160 | // Shift the window if necessary. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 161 | if (seq_num_u > bitmap->max_seq_num) { |
| 162 | uint64_t shift = seq_num_u - bitmap->max_seq_num; |
| 163 | if (shift >= kWindowSize) { |
| 164 | bitmap->map = 0; |
| 165 | } else { |
| 166 | bitmap->map <<= shift; |
| 167 | } |
| 168 | bitmap->max_seq_num = seq_num_u; |
| 169 | } |
| 170 | |
| 171 | uint64_t idx = bitmap->max_seq_num - seq_num_u; |
| 172 | if (idx < kWindowSize) { |
| 173 | bitmap->map |= ((uint64_t)1) << idx; |
| 174 | } |
| 175 | } |
| 176 | |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 177 | enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type, |
| 178 | Span<uint8_t> *out, |
| 179 | size_t *out_consumed, |
| 180 | uint8_t *out_alert, Span<uint8_t> in) { |
| 181 | *out_consumed = 0; |
| 182 | if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) { |
| 183 | return ssl_open_record_close_notify; |
| 184 | } |
| 185 | |
David Benjamin | a05d427 | 2017-10-06 19:34:55 -0400 | [diff] [blame] | 186 | if (in.empty()) { |
| 187 | return ssl_open_record_partial; |
| 188 | } |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 189 | |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 190 | CBS cbs = CBS(in); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 191 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 192 | // Decode the record. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 193 | uint8_t type; |
| 194 | uint16_t version; |
| 195 | uint8_t sequence[8]; |
| 196 | CBS body; |
| 197 | if (!CBS_get_u8(&cbs, &type) || |
| 198 | !CBS_get_u16(&cbs, &version) || |
| 199 | !CBS_copy_bytes(&cbs, sequence, 8) || |
| 200 | !CBS_get_u16_length_prefixed(&cbs, &body) || |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 201 | CBS_len(&body) > SSL3_RT_MAX_ENCRYPTED_LENGTH) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 202 | // The record header was incomplete or malformed. Drop the entire packet. |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 203 | *out_consumed = in.size(); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 204 | return ssl_open_record_discard; |
| 205 | } |
| 206 | |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 207 | bool version_ok; |
| 208 | if (ssl->s3->aead_read_ctx->is_null_cipher()) { |
| 209 | // Only check the first byte. Enforcing beyond that can prevent decoding |
| 210 | // version negotiation failure alerts. |
| 211 | version_ok = (version >> 8) == DTLS1_VERSION_MAJOR; |
| 212 | } else { |
| 213 | version_ok = version == ssl->s3->aead_read_ctx->RecordVersion(); |
| 214 | } |
| 215 | |
| 216 | if (!version_ok) { |
| 217 | // The record header was incomplete or malformed. Drop the entire packet. |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 218 | *out_consumed = in.size(); |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 219 | return ssl_open_record_discard; |
| 220 | } |
| 221 | |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 222 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HEADER, |
| 223 | in.subspan(0, DTLS1_RT_HEADER_LENGTH)); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 224 | |
| 225 | uint16_t epoch = (((uint16_t)sequence[0]) << 8) | sequence[1]; |
| 226 | if (epoch != ssl->d1->r_epoch || |
| 227 | dtls1_bitmap_should_discard(&ssl->d1->bitmap, sequence)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 228 | // Drop this record. It's from the wrong epoch or is a replay. Note that if |
| 229 | // |epoch| is the next epoch, the record could be buffered for later. For |
| 230 | // simplicity, drop it and expect retransmit to handle it later; DTLS must |
| 231 | // handle packet loss anyway. |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 232 | *out_consumed = in.size() - CBS_len(&cbs); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 233 | return ssl_open_record_discard; |
| 234 | } |
| 235 | |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 236 | // discard the body in-place. |
| 237 | if (!ssl->s3->aead_read_ctx->Open( |
| 238 | out, type, version, sequence, |
| 239 | MakeSpan(const_cast<uint8_t *>(CBS_data(&body)), CBS_len(&body)))) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 240 | // Bad packets are silently dropped in DTLS. See section 4.2.1 of RFC 6347. |
| 241 | // Clear the error queue of any errors decryption may have added. Drop the |
| 242 | // entire packet as it must not have come from the peer. |
| 243 | // |
| 244 | // TODO(davidben): This doesn't distinguish malloc failures from encryption |
| 245 | // failures. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 246 | ERR_clear_error(); |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 247 | *out_consumed = in.size() - CBS_len(&cbs); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 248 | return ssl_open_record_discard; |
| 249 | } |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 250 | *out_consumed = in.size() - CBS_len(&cbs); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 251 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 252 | // Check the plaintext length. |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 253 | if (out->size() > SSL3_RT_MAX_PLAIN_LENGTH) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 254 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
| 255 | *out_alert = SSL_AD_RECORD_OVERFLOW; |
| 256 | return ssl_open_record_error; |
| 257 | } |
| 258 | |
| 259 | dtls1_bitmap_record(&ssl->d1->bitmap, sequence); |
| 260 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 261 | // TODO(davidben): Limit the number of empty records as in TLS? This is only |
| 262 | // useful if we also limit discarded packets. |
David Benjamin | 4cf369b | 2015-08-22 01:35:43 -0400 | [diff] [blame] | 263 | |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 264 | if (type == SSL3_RT_ALERT) { |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 265 | return ssl_process_alert(ssl, out_alert, *out); |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | ssl->s3->warning_alert_count = 0; |
| 269 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 270 | *out_type = type; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 271 | return ssl_open_record_success; |
| 272 | } |
| 273 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 274 | static const SSLAEADContext *get_write_aead(const SSL *ssl, |
| 275 | enum dtls1_use_epoch_t use_epoch) { |
David Benjamin | a772b16 | 2017-01-24 17:51:33 -0500 | [diff] [blame] | 276 | if (use_epoch == dtls1_use_previous_epoch) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 277 | assert(ssl->d1->w_epoch >= 1); |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 278 | return ssl->d1->last_aead_write_ctx.get(); |
David Benjamin | a772b16 | 2017-01-24 17:51:33 -0500 | [diff] [blame] | 279 | } |
| 280 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 281 | return ssl->s3->aead_write_ctx.get(); |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | size_t dtls_max_seal_overhead(const SSL *ssl, |
| 285 | enum dtls1_use_epoch_t use_epoch) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 286 | return DTLS1_RT_HEADER_LENGTH + get_write_aead(ssl, use_epoch)->MaxOverhead(); |
David Benjamin | 1a999cf | 2017-01-03 10:30:35 -0500 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch) { |
| 290 | return DTLS1_RT_HEADER_LENGTH + |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 291 | get_write_aead(ssl, use_epoch)->ExplicitNonceLen(); |
David Benjamin | a772b16 | 2017-01-24 17:51:33 -0500 | [diff] [blame] | 292 | } |
| 293 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 294 | int dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 295 | uint8_t type, const uint8_t *in, size_t in_len, |
| 296 | enum dtls1_use_epoch_t use_epoch) { |
David Benjamin | a772b16 | 2017-01-24 17:51:33 -0500 | [diff] [blame] | 297 | const size_t prefix = dtls_seal_prefix_len(ssl, use_epoch); |
| 298 | if (buffers_alias(in, in_len, out, max_out) && |
| 299 | (max_out < prefix || out + prefix != in)) { |
David Benjamin | 95d7a49 | 2016-06-09 16:38:00 -0400 | [diff] [blame] | 300 | OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT); |
| 301 | return 0; |
| 302 | } |
| 303 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 304 | // Determine the parameters for the current epoch. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 305 | uint16_t epoch = ssl->d1->w_epoch; |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 306 | SSLAEADContext *aead = ssl->s3->aead_write_ctx.get(); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 307 | uint8_t *seq = ssl->s3->write_sequence; |
| 308 | if (use_epoch == dtls1_use_previous_epoch) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 309 | assert(ssl->d1->w_epoch >= 1); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 310 | epoch = ssl->d1->w_epoch - 1; |
David Benjamin | 800046f | 2017-10-17 01:37:51 -0400 | [diff] [blame] | 311 | aead = ssl->d1->last_aead_write_ctx.get(); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 312 | seq = ssl->d1->last_write_sequence; |
| 313 | } |
| 314 | |
| 315 | if (max_out < DTLS1_RT_HEADER_LENGTH) { |
| 316 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
| 317 | return 0; |
| 318 | } |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 319 | |
| 320 | out[0] = type; |
| 321 | |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 322 | uint16_t record_version = ssl->s3->aead_write_ctx->RecordVersion(); |
| 323 | out[1] = record_version >> 8; |
| 324 | out[2] = record_version & 0xff; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 325 | |
| 326 | out[3] = epoch >> 8; |
| 327 | out[4] = epoch & 0xff; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 328 | OPENSSL_memcpy(&out[5], &seq[2], 6); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 329 | |
| 330 | size_t ciphertext_len; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 331 | if (!aead->Seal(out + DTLS1_RT_HEADER_LENGTH, &ciphertext_len, |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 332 | max_out - DTLS1_RT_HEADER_LENGTH, type, record_version, |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 333 | &out[3] /* seq */, in, in_len) || |
David Benjamin | 1db2156 | 2015-12-25 15:11:39 -0500 | [diff] [blame] | 334 | !ssl_record_sequence_update(&seq[2], 6)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | if (ciphertext_len >= 1 << 16) { |
| 339 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 340 | return 0; |
| 341 | } |
| 342 | out[11] = ciphertext_len >> 8; |
| 343 | out[12] = ciphertext_len & 0xff; |
| 344 | |
| 345 | *out_len = DTLS1_RT_HEADER_LENGTH + ciphertext_len; |
| 346 | |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 347 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HEADER, |
| 348 | MakeSpan(out, DTLS1_RT_HEADER_LENGTH)); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 349 | |
| 350 | return 1; |
| 351 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 352 | |
| 353 | } // namespace bssl |