| 1 |
// DTLS implementation written by Nagendra Modadugu ([email protected]) for the OpenSSL project 2005. |
| 2 |
// Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 4 |
// SPDX-License-Identifier: Apache-2.0 |
| 5 |
|
| 6 |
#include <openssl/ssl.h> |
| 7 |
|
| 8 |
#include <assert.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include <openssl/bytestring.h> |
| 12 |
#include <openssl/err.h> |
| 13 |
|
| 14 |
#include "internal.h" |
| 15 |
#include "../crypto/internal.h" |
| 16 |
|
| 17 |
|
| 18 |
BSSL_NAMESPACE_BEGIN |
| 19 |
|
| 20 |
// to_u64_be treats |in| as a 8-byte big-endian integer and returns the value as |
| 21 |
// a |uint64_t|. |
| 22 |
static uint64_t to_u64_be(const uint8_t in[8]) { |
| 23 |
uint64_t ret = 0; |
| 24 |
unsigned i; |
| 25 |
for (i = 0; i < 8; i++) { |
| 26 |
ret <<= 8; |
| 27 |
ret |= in[i]; |
| 28 |
} |
| 29 |
return ret; |
| 30 |
} |
| 31 |
|
| 32 |
// dtls1_bitmap_should_discard returns one if |seq_num| has been seen in |
| 33 |
// |bitmap| or is stale. Otherwise it returns zero. |
| 34 |
static bool dtls1_bitmap_should_discard(DTLS1_BITMAP *bitmap, |
| 35 |
const uint8_t seq_num[8]) { |
| 36 |
const size_t kWindowSize = bitmap->map.size(); |
| 37 |
|
| 38 |
uint64_t seq_num_u = to_u64_be(seq_num); |
| 39 |
if (seq_num_u > bitmap->max_seq_num) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
uint64_t idx = bitmap->max_seq_num - seq_num_u; |
| 43 |
return idx >= kWindowSize || bitmap->map[idx]; |
| 44 |
} |
| 45 |
|
| 46 |
// dtls1_bitmap_record updates |bitmap| to record receipt of sequence number |
| 47 |
// |seq_num|. It slides the window forward if needed. It is an error to call |
| 48 |
// this function on a stale sequence number. |
| 49 |
static void dtls1_bitmap_record(DTLS1_BITMAP *bitmap, |
| 50 |
const uint8_t seq_num[8]) { |
| 51 |
const size_t kWindowSize = bitmap->map.size(); |
| 52 |
|
| 53 |
uint64_t seq_num_u = to_u64_be(seq_num); |
| 54 |
// Shift the window if necessary. |
| 55 |
if (seq_num_u > bitmap->max_seq_num) { |
| 56 |
uint64_t shift = seq_num_u - bitmap->max_seq_num; |
| 57 |
if (shift >= kWindowSize) { |
| 58 |
bitmap->map.reset(); |
| 59 |
} else { |
| 60 |
bitmap->map <<= shift; |
| 61 |
} |
| 62 |
bitmap->max_seq_num = seq_num_u; |
| 63 |
} |
| 64 |
|
| 65 |
uint64_t idx = bitmap->max_seq_num - seq_num_u; |
| 66 |
if (idx < kWindowSize) { |
| 67 |
bitmap->map[idx] = true; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type, |
| 72 |
Span<uint8_t> *out, |
| 73 |
size_t *out_consumed, |
| 74 |
uint8_t *out_alert, Span<uint8_t> in) { |
| 75 |
*out_consumed = 0; |
| 76 |
if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) { |
| 77 |
return ssl_open_record_close_notify; |
| 78 |
} |
| 79 |
|
| 80 |
if (in.empty()) { |
| 81 |
return ssl_open_record_partial; |
| 82 |
} |
| 83 |
|
| 84 |
CBS cbs = CBS(in); |
| 85 |
|
| 86 |
// Decode the record. |
| 87 |
uint8_t type; |
| 88 |
uint16_t version; |
| 89 |
uint8_t sequence[8]; |
| 90 |
CBS body; |
| 91 |
if (!CBS_get_u8(&cbs, &type) || |
| 92 |
!CBS_get_u16(&cbs, &version) || |
| 93 |
!CBS_copy_bytes(&cbs, sequence, 8) || |
| 94 |
!CBS_get_u16_length_prefixed(&cbs, &body) || |
| 95 |
CBS_len(&body) > SSL3_RT_MAX_ENCRYPTED_LENGTH) { |
| 96 |
// The record header was incomplete or malformed. Drop the entire packet. |
| 97 |
*out_consumed = in.size(); |
| 98 |
return ssl_open_record_discard; |
| 99 |
} |
| 100 |
|
| 101 |
bool version_ok; |
| 102 |
if (ssl->s3->aead_read_ctx->is_null_cipher()) { |
| 103 |
// Only check the first byte. Enforcing beyond that can prevent decoding |
| 104 |
// version negotiation failure alerts. |
| 105 |
version_ok = (version >> 8) == DTLS1_VERSION_MAJOR; |
| 106 |
} else { |
| 107 |
version_ok = version == ssl->s3->aead_read_ctx->RecordVersion(); |
| 108 |
} |
| 109 |
|
| 110 |
if (!version_ok) { |
| 111 |
// The record header was incomplete or malformed. Drop the entire packet. |
| 112 |
*out_consumed = in.size(); |
| 113 |
return ssl_open_record_discard; |
| 114 |
} |
| 115 |
|
| 116 |
Span<const uint8_t> header = in.subspan(0, DTLS1_RT_HEADER_LENGTH); |
| 117 |
ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HEADER, header); |
| 118 |
|
| 119 |
uint16_t epoch = (((uint16_t)sequence[0]) << 8) | sequence[1]; |
| 120 |
if (epoch != ssl->d1->r_epoch || |
| 121 |
dtls1_bitmap_should_discard(&ssl->d1->bitmap, sequence)) { |
| 122 |
// Drop this record. It's from the wrong epoch or is a replay. Note that if |
| 123 |
// |epoch| is the next epoch, the record could be buffered for later. For |
| 124 |
// simplicity, drop it and expect retransmit to handle it later; DTLS must |
| 125 |
// handle packet loss anyway. |
| 126 |
*out_consumed = in.size() - CBS_len(&cbs); |
| 127 |
return ssl_open_record_discard; |
| 128 |
} |
| 129 |
|
| 130 |
// discard the body in-place. |
| 131 |
if (!ssl->s3->aead_read_ctx->Open( |
| 132 |
out, type, version, sequence, header, |
| 133 |
MakeSpan(const_cast<uint8_t *>(CBS_data(&body)), CBS_len(&body)))) { |
| 134 |
// Bad packets are silently dropped in DTLS. See section 4.2.1 of RFC 6347. |
| 135 |
// Clear the error queue of any errors decryption may have added. Drop the |
| 136 |
// entire packet as it must not have come from the peer. |
| 137 |
// |
| 138 |
// TODO(davidben): This doesn't distinguish malloc failures from encryption |
| 139 |
// failures. |
| 140 |
ERR_clear_error(); |
| 141 |
*out_consumed = in.size() - CBS_len(&cbs); |
| 142 |
return ssl_open_record_discard; |
| 143 |
} |
| 144 |
*out_consumed = in.size() - CBS_len(&cbs); |
| 145 |
|
| 146 |
// Check the plaintext length. |
| 147 |
if (out->size() > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 148 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
| 149 |
*out_alert = SSL_AD_RECORD_OVERFLOW; |
| 150 |
return ssl_open_record_error; |
| 151 |
} |
| 152 |
|
| 153 |
dtls1_bitmap_record(&ssl->d1->bitmap, sequence); |
| 154 |
|
| 155 |
// TODO(davidben): Limit the number of empty records as in TLS? This is only |
| 156 |
// useful if we also limit discarded packets. |
| 157 |
|
| 158 |
if (type == SSL3_RT_ALERT) { |
| 159 |
return ssl_process_alert(ssl, out_alert, *out); |
| 160 |
} |
| 161 |
|
| 162 |
ssl->s3->warning_alert_count = 0; |
| 163 |
|
| 164 |
*out_type = type; |
| 165 |
return ssl_open_record_success; |
| 166 |
} |
| 167 |
|
| 168 |
static const SSLAEADContext *get_write_aead(const SSL *ssl, |
| 169 |
enum dtls1_use_epoch_t use_epoch) { |
| 170 |
if (use_epoch == dtls1_use_previous_epoch) { |
| 171 |
assert(ssl->d1->w_epoch >= 1); |
| 172 |
return ssl->d1->last_aead_write_ctx.get(); |
| 173 |
} |
| 174 |
|
| 175 |
return ssl->s3->aead_write_ctx.get(); |
| 176 |
} |
| 177 |
|
| 178 |
size_t dtls_max_seal_overhead(const SSL *ssl, |
| 179 |
enum dtls1_use_epoch_t use_epoch) { |
| 180 |
return DTLS1_RT_HEADER_LENGTH + get_write_aead(ssl, use_epoch)->MaxOverhead(); |
| 181 |
} |
| 182 |
|
| 183 |
size_t dtls_seal_prefix_len(const SSL *ssl, enum dtls1_use_epoch_t use_epoch) { |
| 184 |
return DTLS1_RT_HEADER_LENGTH + |
| 185 |
get_write_aead(ssl, use_epoch)->ExplicitNonceLen(); |
| 186 |
} |
| 187 |
|
| 188 |
bool dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 189 |
uint8_t type, const uint8_t *in, size_t in_len, |
| 190 |
enum dtls1_use_epoch_t use_epoch) { |
| 191 |
const size_t prefix = dtls_seal_prefix_len(ssl, use_epoch); |
| 192 |
if (buffers_alias(in, in_len, out, max_out) && |
| 193 |
(max_out < prefix || out + prefix != in)) { |
| 194 |
OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT); |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
// Determine the parameters for the current epoch. |
| 199 |
uint16_t epoch = ssl->d1->w_epoch; |
| 200 |
SSLAEADContext *aead = ssl->s3->aead_write_ctx.get(); |
| 201 |
uint8_t *seq = ssl->s3->write_sequence; |
| 202 |
if (use_epoch == dtls1_use_previous_epoch) { |
| 203 |
assert(ssl->d1->w_epoch >= 1); |
| 204 |
epoch = ssl->d1->w_epoch - 1; |
| 205 |
aead = ssl->d1->last_aead_write_ctx.get(); |
| 206 |
seq = ssl->d1->last_write_sequence; |
| 207 |
} |
| 208 |
|
| 209 |
if (max_out < DTLS1_RT_HEADER_LENGTH) { |
| 210 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
out[0] = type; |
| 215 |
|
| 216 |
uint16_t record_version = ssl->s3->aead_write_ctx->RecordVersion(); |
| 217 |
out[1] = record_version >> 8; |
| 218 |
out[2] = record_version & 0xff; |
| 219 |
|
| 220 |
out[3] = epoch >> 8; |
| 221 |
out[4] = epoch & 0xff; |
| 222 |
OPENSSL_memcpy(&out[5], &seq[2], 6); |
| 223 |
|
| 224 |
size_t ciphertext_len; |
| 225 |
if (!aead->CiphertextLen(&ciphertext_len, in_len, 0)) { |
| 226 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); |
| 227 |
return false; |
| 228 |
} |
| 229 |
out[11] = ciphertext_len >> 8; |
| 230 |
out[12] = ciphertext_len & 0xff; |
| 231 |
Span<const uint8_t> header = MakeConstSpan(out, DTLS1_RT_HEADER_LENGTH); |
| 232 |
|
| 233 |
size_t len_copy; |
| 234 |
if (!aead->Seal(out + DTLS1_RT_HEADER_LENGTH, &len_copy, |
| 235 |
max_out - DTLS1_RT_HEADER_LENGTH, type, record_version, |
| 236 |
&out[3] /* seq */, header, in, in_len) || |
| 237 |
!ssl_record_sequence_update(&seq[2], 6)) { |
| 238 |
return false; |
| 239 |
} |
| 240 |
assert(ciphertext_len == len_copy); |
| 241 |
|
| 242 |
*out_len = DTLS1_RT_HEADER_LENGTH + ciphertext_len; |
| 243 |
ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HEADER, header); |
| 244 |
return true; |
| 245 |
} |
| 246 |
|
| 247 |
BSSL_NAMESPACE_END |