| 1 |
|
| 2 |
// Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 4 |
// DTLS implementation written by Nagendra Modadugu ([email protected]) for the OpenSSL project 2005. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
#include <openssl/ssl.h> |
| 8 |
|
| 9 |
#include <assert.h> |
| 10 |
#include <limits.h> |
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
#include <openssl/err.h> |
| 14 |
#include <openssl/evp.h> |
| 15 |
#include <openssl/mem.h> |
| 16 |
#include <openssl/rand.h> |
| 17 |
#include <openssl/bio.h> |
| 18 |
|
| 19 |
#include "../crypto/internal.h" |
| 20 |
#include "internal.h" |
| 21 |
|
| 22 |
|
| 23 |
BSSL_NAMESPACE_BEGIN |
| 24 |
|
| 25 |
// TODO(davidben): 28 comes from the size of IP + UDP header. Is this reasonable |
| 26 |
// for these values? Notably, why is kMinMTU a function of the transport |
| 27 |
// protocol's overhead rather than, say, what's needed to hold a minimally-sized |
| 28 |
// handshake fragment plus protocol overhead. |
| 29 |
|
| 30 |
// kMinMTU is the minimum acceptable MTU value. |
| 31 |
static const unsigned int kMinMTU = 256 - 28; |
| 32 |
|
| 33 |
// kDefaultMTU is the default MTU value to use if neither the user nor |
| 34 |
// the underlying BIO supplies one. |
| 35 |
static const unsigned int kDefaultMTU = 1500 - 28; |
| 36 |
|
| 37 |
|
| 38 |
// Receiving handshake messages. |
| 39 |
|
| 40 |
hm_fragment::~hm_fragment() { |
| 41 |
OPENSSL_free(data); |
| 42 |
OPENSSL_free(reassembly); |
| 43 |
} |
| 44 |
|
| 45 |
static UniquePtr<hm_fragment> dtls1_hm_fragment_new( |
| 46 |
const struct hm_header_st *msg_hdr) { |
| 47 |
ScopedCBB cbb; |
| 48 |
UniquePtr<hm_fragment> frag = MakeUnique<hm_fragment>(); |
| 49 |
if (!frag) { |
| 50 |
return nullptr; |
| 51 |
} |
| 52 |
frag->type = msg_hdr->type; |
| 53 |
frag->seq = msg_hdr->seq; |
| 54 |
frag->msg_len = msg_hdr->msg_len; |
| 55 |
|
| 56 |
// Allocate space for the reassembled message and fill in the header. |
| 57 |
frag->data = |
| 58 |
(uint8_t *)OPENSSL_malloc(DTLS1_HM_HEADER_LENGTH + msg_hdr->msg_len); |
| 59 |
if (frag->data == NULL) { |
| 60 |
return nullptr; |
| 61 |
} |
| 62 |
|
| 63 |
if (!CBB_init_fixed(cbb.get(), frag->data, DTLS1_HM_HEADER_LENGTH) || |
| 64 |
!CBB_add_u8(cbb.get(), msg_hdr->type) || |
| 65 |
!CBB_add_u24(cbb.get(), msg_hdr->msg_len) || |
| 66 |
!CBB_add_u16(cbb.get(), msg_hdr->seq) || |
| 67 |
!CBB_add_u24(cbb.get(), 0 /* frag_off */) || |
| 68 |
!CBB_add_u24(cbb.get(), msg_hdr->msg_len) || |
| 69 |
!CBB_finish(cbb.get(), NULL, NULL)) { |
| 70 |
return nullptr; |
| 71 |
} |
| 72 |
|
| 73 |
// If the handshake message is empty, |frag->reassembly| is NULL. |
| 74 |
if (msg_hdr->msg_len > 0) { |
| 75 |
// Initialize reassembly bitmask. |
| 76 |
if (msg_hdr->msg_len + 7 < msg_hdr->msg_len) { |
| 77 |
OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 78 |
return nullptr; |
| 79 |
} |
| 80 |
size_t bitmask_len = (msg_hdr->msg_len + 7) / 8; |
| 81 |
frag->reassembly = (uint8_t *)OPENSSL_zalloc(bitmask_len); |
| 82 |
if (frag->reassembly == NULL) { |
| 83 |
return nullptr; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return frag; |
| 88 |
} |
| 89 |
|
| 90 |
// bit_range returns a |uint8_t| with bits |start|, inclusive, to |end|, |
| 91 |
// exclusive, set. |
| 92 |
static uint8_t bit_range(size_t start, size_t end) { |
| 93 |
return (uint8_t)(~((1u << start) - 1) & ((1u << end) - 1)); |
| 94 |
} |
| 95 |
|
| 96 |
// dtls1_hm_fragment_mark marks bytes |start|, inclusive, to |end|, exclusive, |
| 97 |
// as received in |frag|. If |frag| becomes complete, it clears |
| 98 |
// |frag->reassembly|. The range must be within the bounds of |frag|'s message |
| 99 |
// and |frag->reassembly| must not be NULL. |
| 100 |
static void dtls1_hm_fragment_mark(hm_fragment *frag, size_t start, |
| 101 |
size_t end) { |
| 102 |
size_t msg_len = frag->msg_len; |
| 103 |
|
| 104 |
if (frag->reassembly == NULL || start > end || end > msg_len) { |
| 105 |
assert(0); |
| 106 |
return; |
| 107 |
} |
| 108 |
// A zero-length message will never have a pending reassembly. |
| 109 |
assert(msg_len > 0); |
| 110 |
|
| 111 |
if (start == end) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
if ((start >> 3) == (end >> 3)) { |
| 116 |
frag->reassembly[start >> 3] |= bit_range(start & 7, end & 7); |
| 117 |
} else { |
| 118 |
frag->reassembly[start >> 3] |= bit_range(start & 7, 8); |
| 119 |
for (size_t i = (start >> 3) + 1; i < (end >> 3); i++) { |
| 120 |
frag->reassembly[i] = 0xff; |
| 121 |
} |
| 122 |
if ((end & 7) != 0) { |
| 123 |
frag->reassembly[end >> 3] |= bit_range(0, end & 7); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// Check if the fragment is complete. |
| 128 |
for (size_t i = 0; i < (msg_len >> 3); i++) { |
| 129 |
if (frag->reassembly[i] != 0xff) { |
| 130 |
return; |
| 131 |
} |
| 132 |
} |
| 133 |
if ((msg_len & 7) != 0 && |
| 134 |
frag->reassembly[msg_len >> 3] != bit_range(0, msg_len & 7)) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
OPENSSL_free(frag->reassembly); |
| 139 |
frag->reassembly = NULL; |
| 140 |
} |
| 141 |
|
| 142 |
// dtls1_is_current_message_complete returns whether the current handshake |
| 143 |
// message is complete. |
| 144 |
static bool dtls1_is_current_message_complete(const SSL *ssl) { |
| 145 |
size_t idx = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT; |
| 146 |
hm_fragment *frag = ssl->d1->incoming_messages[idx].get(); |
| 147 |
return frag != NULL && frag->reassembly == NULL; |
| 148 |
} |
| 149 |
|
| 150 |
// dtls1_get_incoming_message returns the incoming message corresponding to |
| 151 |
// |msg_hdr|. If none exists, it creates a new one and inserts it in the |
| 152 |
// queue. Otherwise, it checks |msg_hdr| is consistent with the existing one. It |
| 153 |
// returns NULL on failure. The caller does not take ownership of the result. |
| 154 |
static hm_fragment *dtls1_get_incoming_message( |
| 155 |
SSL *ssl, uint8_t *out_alert, const struct hm_header_st *msg_hdr) { |
| 156 |
if (msg_hdr->seq < ssl->d1->handshake_read_seq || |
| 157 |
msg_hdr->seq - ssl->d1->handshake_read_seq >= SSL_MAX_HANDSHAKE_FLIGHT) { |
| 158 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 159 |
return NULL; |
| 160 |
} |
| 161 |
|
| 162 |
size_t idx = msg_hdr->seq % SSL_MAX_HANDSHAKE_FLIGHT; |
| 163 |
hm_fragment *frag = ssl->d1->incoming_messages[idx].get(); |
| 164 |
if (frag != NULL) { |
| 165 |
assert(frag->seq == msg_hdr->seq); |
| 166 |
// The new fragment must be compatible with the previous fragments from this |
| 167 |
// message. |
| 168 |
if (frag->type != msg_hdr->type || |
| 169 |
frag->msg_len != msg_hdr->msg_len) { |
| 170 |
OPENSSL_PUT_ERROR(SSL, SSL_R_FRAGMENT_MISMATCH); |
| 171 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
return frag; |
| 175 |
} |
| 176 |
|
| 177 |
// This is the first fragment from this message. |
| 178 |
ssl->d1->incoming_messages[idx] = dtls1_hm_fragment_new(msg_hdr); |
| 179 |
if (!ssl->d1->incoming_messages[idx]) { |
| 180 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
return ssl->d1->incoming_messages[idx].get(); |
| 184 |
} |
| 185 |
|
| 186 |
ssl_open_record_t dtls1_open_handshake(SSL *ssl, size_t *out_consumed, |
| 187 |
uint8_t *out_alert, Span<uint8_t> in) { |
| 188 |
uint8_t type; |
| 189 |
Span<uint8_t> record; |
| 190 |
auto ret = dtls_open_record(ssl, &type, &record, out_consumed, out_alert, in); |
| 191 |
if (ret != ssl_open_record_success) { |
| 192 |
return ret; |
| 193 |
} |
| 194 |
|
| 195 |
switch (type) { |
| 196 |
case SSL3_RT_APPLICATION_DATA: |
| 197 |
// Unencrypted application data records are always illegal. |
| 198 |
if (ssl->s3->aead_read_ctx->is_null_cipher()) { |
| 199 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 200 |
*out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 201 |
return ssl_open_record_error; |
| 202 |
} |
| 203 |
|
| 204 |
// Out-of-order application data may be received between ChangeCipherSpec |
| 205 |
// and finished. Discard it. |
| 206 |
return ssl_open_record_discard; |
| 207 |
|
| 208 |
case SSL3_RT_CHANGE_CIPHER_SPEC: |
| 209 |
// We do not support renegotiation, so encrypted ChangeCipherSpec records |
| 210 |
// are illegal. |
| 211 |
if (!ssl->s3->aead_read_ctx->is_null_cipher()) { |
| 212 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 213 |
*out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 214 |
return ssl_open_record_error; |
| 215 |
} |
| 216 |
|
| 217 |
if (record.size() != 1u || record[0] != SSL3_MT_CCS) { |
| 218 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC); |
| 219 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 220 |
return ssl_open_record_error; |
| 221 |
} |
| 222 |
|
| 223 |
// Flag the ChangeCipherSpec for later. |
| 224 |
ssl->d1->has_change_cipher_spec = true; |
| 225 |
ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, |
| 226 |
record); |
| 227 |
return ssl_open_record_success; |
| 228 |
|
| 229 |
case SSL3_RT_HANDSHAKE: |
| 230 |
// Break out to main processing. |
| 231 |
break; |
| 232 |
|
| 233 |
default: |
| 234 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 235 |
*out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 236 |
return ssl_open_record_error; |
| 237 |
} |
| 238 |
|
| 239 |
CBS cbs; |
| 240 |
CBS_init(&cbs, record.data(), record.size()); |
| 241 |
while (CBS_len(&cbs) > 0) { |
| 242 |
// Read a handshake fragment. |
| 243 |
struct hm_header_st msg_hdr; |
| 244 |
CBS body; |
| 245 |
if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) { |
| 246 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD); |
| 247 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 248 |
return ssl_open_record_error; |
| 249 |
} |
| 250 |
|
| 251 |
const size_t frag_off = msg_hdr.frag_off; |
| 252 |
const size_t frag_len = msg_hdr.frag_len; |
| 253 |
const size_t msg_len = msg_hdr.msg_len; |
| 254 |
if (frag_off > msg_len || frag_off + frag_len < frag_off || |
| 255 |
frag_off + frag_len > msg_len || |
| 256 |
msg_len > ssl_max_handshake_message_len(ssl)) { |
| 257 |
OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 258 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 259 |
return ssl_open_record_error; |
| 260 |
} |
| 261 |
|
| 262 |
// The encrypted epoch in DTLS has only one handshake message. |
| 263 |
if (ssl->d1->r_epoch == 1 && msg_hdr.seq != ssl->d1->handshake_read_seq) { |
| 264 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 265 |
*out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 266 |
return ssl_open_record_error; |
| 267 |
} |
| 268 |
|
| 269 |
if (msg_hdr.seq < ssl->d1->handshake_read_seq || |
| 270 |
msg_hdr.seq - ssl->d1->handshake_read_seq >= SSL_MAX_HANDSHAKE_FLIGHT) { |
| 271 |
// Ignore fragments from the past, or ones too far in the future. |
| 272 |
continue; |
| 273 |
} |
| 274 |
|
| 275 |
hm_fragment *frag = dtls1_get_incoming_message(ssl, out_alert, &msg_hdr); |
| 276 |
if (frag == NULL) { |
| 277 |
return ssl_open_record_error; |
| 278 |
} |
| 279 |
assert(frag->msg_len == msg_len); |
| 280 |
|
| 281 |
if (frag->reassembly == NULL) { |
| 282 |
// The message is already assembled. |
| 283 |
continue; |
| 284 |
} |
| 285 |
assert(msg_len > 0); |
| 286 |
|
| 287 |
// Copy the body into the fragment. |
| 288 |
OPENSSL_memcpy(frag->data + DTLS1_HM_HEADER_LENGTH + frag_off, |
| 289 |
CBS_data(&body), CBS_len(&body)); |
| 290 |
dtls1_hm_fragment_mark(frag, frag_off, frag_off + frag_len); |
| 291 |
} |
| 292 |
|
| 293 |
return ssl_open_record_success; |
| 294 |
} |
| 295 |
|
| 296 |
bool dtls1_get_message(const SSL *ssl, SSLMessage *out) { |
| 297 |
if (!dtls1_is_current_message_complete(ssl)) { |
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
size_t idx = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT; |
| 302 |
hm_fragment *frag = ssl->d1->incoming_messages[idx].get(); |
| 303 |
out->type = frag->type; |
| 304 |
CBS_init(&out->body, frag->data + DTLS1_HM_HEADER_LENGTH, frag->msg_len); |
| 305 |
CBS_init(&out->raw, frag->data, DTLS1_HM_HEADER_LENGTH + frag->msg_len); |
| 306 |
out->is_v2_hello = false; |
| 307 |
if (!ssl->s3->has_message) { |
| 308 |
ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, out->raw); |
| 309 |
ssl->s3->has_message = true; |
| 310 |
} |
| 311 |
return true; |
| 312 |
} |
| 313 |
|
| 314 |
void dtls1_next_message(SSL *ssl) { |
| 315 |
assert(ssl->s3->has_message); |
| 316 |
assert(dtls1_is_current_message_complete(ssl)); |
| 317 |
size_t index = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT; |
| 318 |
ssl->d1->incoming_messages[index].reset(); |
| 319 |
ssl->d1->handshake_read_seq++; |
| 320 |
ssl->s3->has_message = false; |
| 321 |
// If we previously sent a flight, mark it as having a reply, so |
| 322 |
// |on_handshake_complete| can manage post-handshake retransmission. |
| 323 |
if (ssl->d1->outgoing_messages_complete) { |
| 324 |
ssl->d1->flight_has_reply = true; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
bool dtls_has_unprocessed_handshake_data(const SSL *ssl) { |
| 329 |
size_t current = ssl->d1->handshake_read_seq % SSL_MAX_HANDSHAKE_FLIGHT; |
| 330 |
for (size_t i = 0; i < SSL_MAX_HANDSHAKE_FLIGHT; i++) { |
| 331 |
// Skip the current message. |
| 332 |
if (ssl->s3->has_message && i == current) { |
| 333 |
assert(dtls1_is_current_message_complete(ssl)); |
| 334 |
continue; |
| 335 |
} |
| 336 |
if (ssl->d1->incoming_messages[i] != nullptr) { |
| 337 |
return true; |
| 338 |
} |
| 339 |
} |
| 340 |
return false; |
| 341 |
} |
| 342 |
|
| 343 |
bool dtls1_parse_fragment(CBS *cbs, struct hm_header_st *out_hdr, |
| 344 |
CBS *out_body) { |
| 345 |
OPENSSL_memset(out_hdr, 0x00, sizeof(struct hm_header_st)); |
| 346 |
|
| 347 |
if (!CBS_get_u8(cbs, &out_hdr->type) || |
| 348 |
!CBS_get_u24(cbs, &out_hdr->msg_len) || |
| 349 |
!CBS_get_u16(cbs, &out_hdr->seq) || |
| 350 |
!CBS_get_u24(cbs, &out_hdr->frag_off) || |
| 351 |
!CBS_get_u24(cbs, &out_hdr->frag_len) || |
| 352 |
!CBS_get_bytes(cbs, out_body, out_hdr->frag_len)) { |
| 353 |
return false; |
| 354 |
} |
| 355 |
|
| 356 |
return true; |
| 357 |
} |
| 358 |
|
| 359 |
ssl_open_record_t dtls1_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
| 360 |
uint8_t *out_alert, |
| 361 |
Span<uint8_t> in) { |
| 362 |
if (!ssl->d1->has_change_cipher_spec) { |
| 363 |
// dtls1_open_handshake processes both handshake and ChangeCipherSpec. |
| 364 |
auto ret = dtls1_open_handshake(ssl, out_consumed, out_alert, in); |
| 365 |
if (ret != ssl_open_record_success) { |
| 366 |
return ret; |
| 367 |
} |
| 368 |
} |
| 369 |
if (ssl->d1->has_change_cipher_spec) { |
| 370 |
ssl->d1->has_change_cipher_spec = false; |
| 371 |
return ssl_open_record_success; |
| 372 |
} |
| 373 |
return ssl_open_record_discard; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
// Sending handshake messages. |
| 378 |
|
| 379 |
void DTLS_OUTGOING_MESSAGE::Clear() { data.Reset(); } |
| 380 |
|
| 381 |
void dtls_clear_outgoing_messages(SSL *ssl) { |
| 382 |
for (size_t i = 0; i < ssl->d1->outgoing_messages_len; i++) { |
| 383 |
ssl->d1->outgoing_messages[i].Clear(); |
| 384 |
} |
| 385 |
ssl->d1->outgoing_messages_len = 0; |
| 386 |
ssl->d1->outgoing_written = 0; |
| 387 |
ssl->d1->outgoing_offset = 0; |
| 388 |
ssl->d1->outgoing_messages_complete = false; |
| 389 |
ssl->d1->flight_has_reply = false; |
| 390 |
} |
| 391 |
|
| 392 |
bool dtls1_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type) { |
| 393 |
// Pick a modest size hint to save most of the |realloc| calls. |
| 394 |
if (!CBB_init(cbb, 64) || |
| 395 |
!CBB_add_u8(cbb, type) || |
| 396 |
!CBB_add_u24(cbb, 0 /* length (filled in later) */) || |
| 397 |
!CBB_add_u16(cbb, ssl->d1->handshake_write_seq) || |
| 398 |
!CBB_add_u24(cbb, 0 /* offset */) || |
| 399 |
!CBB_add_u24_length_prefixed(cbb, body)) { |
| 400 |
return false; |
| 401 |
} |
| 402 |
|
| 403 |
return true; |
| 404 |
} |
| 405 |
|
| 406 |
bool dtls1_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg) { |
| 407 |
if (!CBBFinishArray(cbb, out_msg) || |
| 408 |
out_msg->size() < DTLS1_HM_HEADER_LENGTH) { |
| 409 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 410 |
return false; |
| 411 |
} |
| 412 |
|
| 413 |
// Fix up the header. Copy the fragment length into the total message |
| 414 |
// length. |
| 415 |
OPENSSL_memcpy(out_msg->data() + 1, |
| 416 |
out_msg->data() + DTLS1_HM_HEADER_LENGTH - 3, 3); |
| 417 |
return true; |
| 418 |
} |
| 419 |
|
| 420 |
// ssl_size_t_greater_than_32_bits returns whether |v| exceeds the bounds of a |
| 421 |
// 32-bit value. The obvious thing doesn't work because, in some 32-bit build |
| 422 |
// configurations, the compiler warns that the test is always false and breaks |
| 423 |
// the build. |
| 424 |
static bool ssl_size_t_greater_than_32_bits(size_t v) { |
| 425 |
#if defined(OPENSSL_64_BIT) |
| 426 |
return v > 0xffffffff; |
| 427 |
#elif defined(OPENSSL_32_BIT) |
| 428 |
return false; |
| 429 |
#else |
| 430 |
#error "Building for neither 32- nor 64-bits." |
| 431 |
#endif |
| 432 |
} |
| 433 |
|
| 434 |
// add_outgoing adds a new handshake message or ChangeCipherSpec to the current |
| 435 |
// outgoing flight. It returns true on success and false on error. |
| 436 |
static bool add_outgoing(SSL *ssl, bool is_ccs, Array<uint8_t> data) { |
| 437 |
if (ssl->d1->outgoing_messages_complete) { |
| 438 |
// If we've begun writing a new flight, we received the peer flight. Discard |
| 439 |
// the timer and the our flight. |
| 440 |
dtls1_stop_timer(ssl); |
| 441 |
dtls_clear_outgoing_messages(ssl); |
| 442 |
} |
| 443 |
|
| 444 |
static_assert(SSL_MAX_HANDSHAKE_FLIGHT < |
| 445 |
(1 << 8 * sizeof(ssl->d1->outgoing_messages_len)), |
| 446 |
"outgoing_messages_len is too small"); |
| 447 |
if (ssl->d1->outgoing_messages_len >= SSL_MAX_HANDSHAKE_FLIGHT || |
| 448 |
ssl_size_t_greater_than_32_bits(data.size())) { |
| 449 |
assert(false); |
| 450 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 451 |
return false; |
| 452 |
} |
| 453 |
|
| 454 |
if (!is_ccs) { |
| 455 |
// TODO(svaldez): Move this up a layer to fix abstraction for SSLTranscript |
| 456 |
// on hs. |
| 457 |
if (ssl->s3->hs != NULL && |
| 458 |
!ssl->s3->hs->transcript.Update(data)) { |
| 459 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 460 |
return false; |
| 461 |
} |
| 462 |
ssl->d1->handshake_write_seq++; |
| 463 |
} |
| 464 |
|
| 465 |
DTLS_OUTGOING_MESSAGE *msg = |
| 466 |
&ssl->d1->outgoing_messages[ssl->d1->outgoing_messages_len]; |
| 467 |
msg->data = std::move(data); |
| 468 |
msg->epoch = ssl->d1->w_epoch; |
| 469 |
msg->is_ccs = is_ccs; |
| 470 |
|
| 471 |
ssl->d1->outgoing_messages_len++; |
| 472 |
return true; |
| 473 |
} |
| 474 |
|
| 475 |
bool dtls1_add_message(SSL *ssl, Array<uint8_t> data) { |
| 476 |
return add_outgoing(ssl, false /* handshake */, std::move(data)); |
| 477 |
} |
| 478 |
|
| 479 |
bool dtls1_add_change_cipher_spec(SSL *ssl) { |
| 480 |
return add_outgoing(ssl, true /* ChangeCipherSpec */, Array<uint8_t>()); |
| 481 |
} |
| 482 |
|
| 483 |
// dtls1_update_mtu updates the current MTU from the BIO, ensuring it is above |
| 484 |
// the minimum. |
| 485 |
static void dtls1_update_mtu(SSL *ssl) { |
| 486 |
// TODO(davidben): No consumer implements |BIO_CTRL_DGRAM_SET_MTU| and the |
| 487 |
// only |BIO_CTRL_DGRAM_QUERY_MTU| implementation could use |
| 488 |
// |SSL_set_mtu|. Does this need to be so complex? |
| 489 |
if (ssl->d1->mtu < dtls1_min_mtu() && |
| 490 |
!(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
| 491 |
long const mtu = BIO_ctrl(ssl->wbio.get(), BIO_CTRL_DGRAM_QUERY_MTU, 0, nullptr); |
| 492 |
if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) { |
| 493 |
ssl->d1->mtu = (unsigned)mtu; |
| 494 |
} else { |
| 495 |
ssl->d1->mtu = kDefaultMTU; |
| 496 |
if (1 != BIO_ctrl(ssl->wbio.get(), BIO_CTRL_DGRAM_SET_MTU, ssl->d1->mtu, nullptr)) { |
| 497 |
// Ignore failure: BIO_CTRL_DGRAM_SET_MTU isn't supported |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
// The MTU should be above the minimum now. |
| 503 |
assert(ssl->d1->mtu >= dtls1_min_mtu()); |
| 504 |
} |
| 505 |
|
| 506 |
enum seal_result_t { |
| 507 |
seal_error, |
| 508 |
seal_no_progress, |
| 509 |
seal_partial, |
| 510 |
seal_success, |
| 511 |
}; |
| 512 |
|
| 513 |
// seal_next_message seals |msg|, which must be the next message, to |out|. If |
| 514 |
// progress was made, it returns |seal_partial| or |seal_success| and sets |
| 515 |
// |*out_len| to the number of bytes written. |
| 516 |
static enum seal_result_t seal_next_message(SSL *ssl, uint8_t *out, |
| 517 |
size_t *out_len, size_t max_out, |
| 518 |
const DTLS_OUTGOING_MESSAGE *msg) { |
| 519 |
assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len); |
| 520 |
assert(msg == &ssl->d1->outgoing_messages[ssl->d1->outgoing_written]); |
| 521 |
|
| 522 |
enum dtls1_use_epoch_t use_epoch = dtls1_use_current_epoch; |
| 523 |
if (ssl->d1->w_epoch >= 1 && msg->epoch == ssl->d1->w_epoch - 1) { |
| 524 |
use_epoch = dtls1_use_previous_epoch; |
| 525 |
} else if (msg->epoch != ssl->d1->w_epoch) { |
| 526 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 527 |
return seal_error; |
| 528 |
} |
| 529 |
|
| 530 |
size_t overhead = dtls_max_seal_overhead(ssl, use_epoch); |
| 531 |
size_t prefix = dtls_seal_prefix_len(ssl, use_epoch); |
| 532 |
|
| 533 |
if (msg->is_ccs) { |
| 534 |
// Check there is room for the ChangeCipherSpec. |
| 535 |
static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS}; |
| 536 |
if (max_out < sizeof(kChangeCipherSpec) + overhead) { |
| 537 |
return seal_no_progress; |
| 538 |
} |
| 539 |
|
| 540 |
if (!dtls_seal_record(ssl, out, out_len, max_out, |
| 541 |
SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec, |
| 542 |
sizeof(kChangeCipherSpec), use_epoch)) { |
| 543 |
return seal_error; |
| 544 |
} |
| 545 |
|
| 546 |
ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC, |
| 547 |
kChangeCipherSpec); |
| 548 |
return seal_success; |
| 549 |
} |
| 550 |
|
| 551 |
// DTLS messages are serialized as a single fragment in |msg|. |
| 552 |
CBS cbs, body; |
| 553 |
struct hm_header_st hdr; |
| 554 |
CBS_init(&cbs, msg->data.data(), msg->data.size()); |
| 555 |
if (!dtls1_parse_fragment(&cbs, &hdr, &body) || |
| 556 |
hdr.frag_off != 0 || |
| 557 |
hdr.frag_len != CBS_len(&body) || |
| 558 |
hdr.msg_len != CBS_len(&body) || |
| 559 |
!CBS_skip(&body, ssl->d1->outgoing_offset) || |
| 560 |
CBS_len(&cbs) != 0) { |
| 561 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 562 |
return seal_error; |
| 563 |
} |
| 564 |
|
| 565 |
// Determine how much progress can be made. |
| 566 |
if (max_out < DTLS1_HM_HEADER_LENGTH + 1 + overhead || max_out < prefix) { |
| 567 |
return seal_no_progress; |
| 568 |
} |
| 569 |
size_t todo = CBS_len(&body); |
| 570 |
if (todo > max_out - DTLS1_HM_HEADER_LENGTH - overhead) { |
| 571 |
todo = max_out - DTLS1_HM_HEADER_LENGTH - overhead; |
| 572 |
} |
| 573 |
|
| 574 |
// Assemble a fragment, to be sealed in-place. |
| 575 |
ScopedCBB cbb; |
| 576 |
CBB child; |
| 577 |
uint8_t *frag = out + prefix; |
| 578 |
size_t max_frag = max_out - prefix, frag_len; |
| 579 |
if (!CBB_init_fixed(cbb.get(), frag, max_frag) || |
| 580 |
!CBB_add_u8(cbb.get(), hdr.type) || |
| 581 |
!CBB_add_u24(cbb.get(), hdr.msg_len) || |
| 582 |
!CBB_add_u16(cbb.get(), hdr.seq) || |
| 583 |
!CBB_add_u24(cbb.get(), ssl->d1->outgoing_offset) || |
| 584 |
!CBB_add_u24_length_prefixed(cbb.get(), &child) || |
| 585 |
!CBB_add_bytes(&child, CBS_data(&body), todo) || |
| 586 |
!CBB_finish(cbb.get(), NULL, &frag_len)) { |
| 587 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 588 |
return seal_error; |
| 589 |
} |
| 590 |
|
| 591 |
ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, |
| 592 |
MakeSpan(frag, frag_len)); |
| 593 |
|
| 594 |
if (!dtls_seal_record(ssl, out, out_len, max_out, SSL3_RT_HANDSHAKE, |
| 595 |
out + prefix, frag_len, use_epoch)) { |
| 596 |
return seal_error; |
| 597 |
} |
| 598 |
|
| 599 |
if (todo == CBS_len(&body)) { |
| 600 |
// The next message is complete. |
| 601 |
ssl->d1->outgoing_offset = 0; |
| 602 |
return seal_success; |
| 603 |
} |
| 604 |
|
| 605 |
ssl->d1->outgoing_offset += todo; |
| 606 |
return seal_partial; |
| 607 |
} |
| 608 |
|
| 609 |
// seal_next_packet writes as much of the next flight as possible to |out| and |
| 610 |
// advances |ssl->d1->outgoing_written| and |ssl->d1->outgoing_offset| as |
| 611 |
// appropriate. |
| 612 |
static bool seal_next_packet(SSL *ssl, uint8_t *out, size_t *out_len, |
| 613 |
size_t max_out) { |
| 614 |
bool made_progress = false; |
| 615 |
size_t total = 0; |
| 616 |
assert(ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len); |
| 617 |
for (; ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len; |
| 618 |
ssl->d1->outgoing_written++) { |
| 619 |
const DTLS_OUTGOING_MESSAGE *msg = |
| 620 |
&ssl->d1->outgoing_messages[ssl->d1->outgoing_written]; |
| 621 |
size_t len; |
| 622 |
enum seal_result_t ret = seal_next_message(ssl, out, &len, max_out, msg); |
| 623 |
switch (ret) { |
| 624 |
case seal_error: |
| 625 |
return false; |
| 626 |
|
| 627 |
case seal_no_progress: |
| 628 |
goto packet_full; |
| 629 |
|
| 630 |
case seal_partial: |
| 631 |
case seal_success: |
| 632 |
out += len; |
| 633 |
max_out -= len; |
| 634 |
total += len; |
| 635 |
made_progress = true; |
| 636 |
|
| 637 |
if (ret == seal_partial) { |
| 638 |
goto packet_full; |
| 639 |
} |
| 640 |
break; |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
packet_full: |
| 645 |
// The MTU was too small to make any progress. |
| 646 |
if (!made_progress) { |
| 647 |
OPENSSL_PUT_ERROR(SSL, SSL_R_MTU_TOO_SMALL); |
| 648 |
return false; |
| 649 |
} |
| 650 |
|
| 651 |
*out_len = total; |
| 652 |
return true; |
| 653 |
} |
| 654 |
|
| 655 |
static int send_flight(SSL *ssl) { |
| 656 |
if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 657 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 658 |
return -1; |
| 659 |
} |
| 660 |
|
| 661 |
if (ssl->wbio == nullptr) { |
| 662 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET); |
| 663 |
return -1; |
| 664 |
} |
| 665 |
|
| 666 |
dtls1_update_mtu(ssl); |
| 667 |
|
| 668 |
Array<uint8_t> packet; |
| 669 |
if (!packet.Init(ssl->d1->mtu)) { |
| 670 |
return -1; |
| 671 |
} |
| 672 |
|
| 673 |
while (ssl->d1->outgoing_written < ssl->d1->outgoing_messages_len) { |
| 674 |
uint8_t old_written = ssl->d1->outgoing_written; |
| 675 |
uint32_t old_offset = ssl->d1->outgoing_offset; |
| 676 |
|
| 677 |
size_t packet_len; |
| 678 |
if (!seal_next_packet(ssl, packet.data(), &packet_len, packet.size())) { |
| 679 |
return -1; |
| 680 |
} |
| 681 |
|
| 682 |
int bio_ret = BIO_write(ssl->wbio.get(), packet.data(), packet_len); |
| 683 |
if (bio_ret <= 0) { |
| 684 |
// Retry this packet the next time around. |
| 685 |
ssl->d1->outgoing_written = old_written; |
| 686 |
ssl->d1->outgoing_offset = old_offset; |
| 687 |
ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; |
| 688 |
return bio_ret; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
if (BIO_flush(ssl->wbio.get()) <= 0) { |
| 693 |
ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; |
| 694 |
return -1; |
| 695 |
} |
| 696 |
|
| 697 |
return 1; |
| 698 |
} |
| 699 |
|
| 700 |
int dtls1_flush_flight(SSL *ssl) { |
| 701 |
ssl->d1->outgoing_messages_complete = true; |
| 702 |
// Start the retransmission timer for the next flight (if any). |
| 703 |
dtls1_start_timer(ssl); |
| 704 |
return send_flight(ssl); |
| 705 |
} |
| 706 |
|
| 707 |
int dtls1_retransmit_outgoing_messages(SSL *ssl) { |
| 708 |
// Rewind to the start of the flight and write it again. |
| 709 |
// |
| 710 |
// TODO(davidben): This does not allow retransmits to be resumed on |
| 711 |
// non-blocking write. |
| 712 |
ssl->d1->outgoing_written = 0; |
| 713 |
ssl->d1->outgoing_offset = 0; |
| 714 |
|
| 715 |
return send_flight(ssl); |
| 716 |
} |
| 717 |
|
| 718 |
unsigned int dtls1_min_mtu(void) { |
| 719 |
return kMinMTU; |
| 720 |
} |
| 721 |
|
| 722 |
BSSL_NAMESPACE_END |