| 1 |
// Copyright (c) 2015, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/ssl.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <limits.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include <openssl/bio.h> |
| 12 |
#include <openssl/err.h> |
| 13 |
#include <openssl/mem.h> |
| 14 |
|
| 15 |
#include "../crypto/internal.h" |
| 16 |
#include "internal.h" |
| 17 |
|
| 18 |
|
| 19 |
BSSL_NAMESPACE_BEGIN |
| 20 |
|
| 21 |
// BIO uses int instead of size_t. No lengths will exceed SSLBUFFER_MAX_CAPACITY |
| 22 |
// (uint16_t), so this will not overflow. |
| 23 |
static_assert(SSLBUFFER_MAX_CAPACITY <= INT_MAX, |
| 24 |
"uint16_t does not fit in int"); |
| 25 |
|
| 26 |
static_assert((SSL3_ALIGN_PAYLOAD & (SSL3_ALIGN_PAYLOAD - 1)) == 0, |
| 27 |
"SSL3_ALIGN_PAYLOAD must be a power of 2"); |
| 28 |
|
| 29 |
static OPENSSL_INLINE size_t compute_buffer_size(size_t capacity) { |
| 30 |
return capacity + SSL3_ALIGN_PAYLOAD - 1; |
| 31 |
} |
| 32 |
|
| 33 |
static OPENSSL_INLINE size_t compute_buffer_offset(size_t header_len, |
| 34 |
uint8_t *buffer) { |
| 35 |
return (0 - header_len - (uintptr_t)buffer) & (SSL3_ALIGN_PAYLOAD - 1); |
| 36 |
} |
| 37 |
|
| 38 |
void SSLBuffer::Clear() { |
| 39 |
if (buf_allocated_) { |
| 40 |
free(buf_); // Allocated with malloc(). |
| 41 |
} |
| 42 |
buf_ = nullptr; |
| 43 |
buf_allocated_ = false; |
| 44 |
buf_cap_ = 0; |
| 45 |
buf_size_ = 0; |
| 46 |
header_len_ = 0; |
| 47 |
offset_ = 0; |
| 48 |
size_ = 0; |
| 49 |
cap_ = 0; |
| 50 |
max_serialization_version_ = kSSLBufferMaxSerDeVersion; |
| 51 |
} |
| 52 |
|
| 53 |
bool SSLBuffer::EnsureCap(size_t header_len, size_t new_cap) { |
| 54 |
if (new_cap > SSLBUFFER_MAX_CAPACITY) { |
| 55 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
if ((size_t)cap_ >= new_cap) { |
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
uint8_t *new_buf; |
| 64 |
bool new_buf_allocated; |
| 65 |
size_t new_offset; |
| 66 |
if (new_cap <= sizeof(inline_buf_)) { |
| 67 |
// This function is called twice per TLS record, first for the five-byte |
| 68 |
// header. To avoid allocating twice, use an inline buffer for short inputs. |
| 69 |
new_buf = inline_buf_; |
| 70 |
new_buf_allocated = false; |
| 71 |
new_offset = 0; |
| 72 |
buf_size_ = sizeof(inline_buf_); |
| 73 |
} else { |
| 74 |
// Add up to |SSL3_ALIGN_PAYLOAD| - 1 bytes of slack for alignment. |
| 75 |
// |
| 76 |
// Since this buffer gets allocated quite frequently and doesn't contain any |
| 77 |
// sensitive data, we allocate with malloc rather than |OPENSSL_malloc| and |
| 78 |
// avoid zeroing on free. |
| 79 |
buf_size_ = compute_buffer_size(new_cap); |
| 80 |
new_buf = (uint8_t *)malloc(buf_size_); |
| 81 |
if (new_buf == NULL) { |
| 82 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 83 |
return false; |
| 84 |
} |
| 85 |
new_buf_allocated = true; |
| 86 |
// Offset the buffer such that the record body is aligned. |
| 87 |
new_offset = compute_buffer_offset(header_len, new_buf); |
| 88 |
} |
| 89 |
|
| 90 |
// Note if the both old and new buffer are inline, the source and destination |
| 91 |
// may alias. |
| 92 |
OPENSSL_memmove(new_buf + new_offset, buf_ + offset_, size_); |
| 93 |
|
| 94 |
if (buf_allocated_) { |
| 95 |
free(buf_); // Allocated with malloc(). |
| 96 |
} |
| 97 |
|
| 98 |
buf_ = new_buf; |
| 99 |
buf_allocated_ = new_buf_allocated; |
| 100 |
header_len_ = header_len; |
| 101 |
offset_ = new_offset; |
| 102 |
buf_cap_ = cap_ = new_cap; |
| 103 |
max_serialization_version_ = kSSLBufferMaxSerDeVersion; |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
void SSLBuffer::DidWrite(size_t new_size) { |
| 108 |
if (new_size > cap() - size()) { |
| 109 |
abort(); |
| 110 |
} |
| 111 |
size_ += new_size; |
| 112 |
} |
| 113 |
|
| 114 |
void SSLBuffer::Consume(size_t len) { |
| 115 |
if (len > (size_t)size_) { |
| 116 |
abort(); |
| 117 |
} |
| 118 |
offset_ += (int)len; |
| 119 |
size_ -= (int)len; |
| 120 |
cap_ -= (int)len; |
| 121 |
} |
| 122 |
|
| 123 |
void SSLBuffer::DiscardConsumed() { |
| 124 |
if (size_ == 0) { |
| 125 |
Clear(); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// An SSLBuffer is serialized as the following ASN.1 structure: |
| 130 |
// |
| 131 |
// -- The V2 Serialization |
| 132 |
// SSLBuffer ::= SEQUENCE { |
| 133 |
// version INTEGER (2), -- SSLBuffer structure |
| 134 |
// bufAllocated BOOLEAN, |
| 135 |
// bufferCapacity INTEGER, |
| 136 |
// headerLength INTEGER, |
| 137 |
// relativeOffset INTEGER, |
| 138 |
// remainingCapacity INTEGER, |
| 139 |
// buf OCTET STRING, |
| 140 |
// } |
| 141 |
// |
| 142 |
// -- The V1 Serialization |
| 143 |
// SSLBuffer ::= SEQUENCE { |
| 144 |
// version INTEGER (1), -- SSLBuffer structure |
| 145 |
// bufAllocated BOOLEAN, |
| 146 |
// offset INTEGER, |
| 147 |
// size INTEGER, |
| 148 |
// cap INTEGER, |
| 149 |
// buf OCTET STRING, |
| 150 |
// } |
| 151 |
|
| 152 |
bool SSLBuffer::DoSerializationV1(CBB &seq) { |
| 153 |
if (!CBB_add_asn1_uint64(&seq, SSL_BUFFER_SERDE_VERSION_ONE) || |
| 154 |
!CBB_add_asn1_bool(&seq, (buf_allocated_ ? 1 : 0)) || |
| 155 |
!CBB_add_asn1_uint64(&seq, offset_) || |
| 156 |
!CBB_add_asn1_uint64(&seq, size_) || !CBB_add_asn1_uint64(&seq, cap_) || |
| 157 |
(buf_allocated_ && !CBB_add_asn1_octet_string(&seq, buf_, buf_size_)) || |
| 158 |
(!buf_allocated_ && |
| 159 |
!CBB_add_asn1_octet_string(&seq, inline_buf_, SSL3_RT_HEADER_LENGTH))) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
bool SSLBuffer::DoSerializationV2(CBB &seq) { |
| 166 |
size_t align_offset = 0; |
| 167 |
if (buf_allocated_) { |
| 168 |
align_offset = compute_buffer_offset(header_len_, buf_); |
| 169 |
} |
| 170 |
|
| 171 |
size_t rel_offset = offset_ - align_offset; |
| 172 |
|
| 173 |
if (!CBB_add_asn1_uint64(&seq, SSL_BUFFER_SERDE_VERSION_TWO) || |
| 174 |
!CBB_add_asn1_bool(&seq, (buf_allocated_ ? 1 : 0)) || |
| 175 |
!CBB_add_asn1_uint64(&seq, buf_cap_) || |
| 176 |
!CBB_add_asn1_uint64(&seq, header_len_) || |
| 177 |
!CBB_add_asn1_uint64(&seq, rel_offset) || |
| 178 |
!CBB_add_asn1_uint64(&seq, cap_) || |
| 179 |
(buf_allocated_ && !CBB_add_asn1_octet_string(&seq, buf_ + align_offset, |
| 180 |
rel_offset + size_)) || |
| 181 |
(!buf_allocated_ && |
| 182 |
!CBB_add_asn1_octet_string(&seq, inline_buf_ + align_offset, |
| 183 |
rel_offset + size_))) { |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
return true; |
| 188 |
} |
| 189 |
|
| 190 |
bool SSLBuffer::DoSerialization(CBB &cbb) { |
| 191 |
CBB seq; |
| 192 |
|
| 193 |
if (!CBB_add_asn1(&cbb, &seq, CBS_ASN1_SEQUENCE)) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
switch (max_serialization_version_) { |
| 198 |
case SSL_BUFFER_SERDE_VERSION_TWO: |
| 199 |
if (!DoSerializationV2(seq)) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
break; |
| 203 |
case SSL_BUFFER_SERDE_VERSION_ONE: |
| 204 |
if (!DoSerializationV1(seq)) { |
| 205 |
return false; |
| 206 |
} |
| 207 |
break; |
| 208 |
default: |
| 209 |
// Only possible with a programming error |
| 210 |
abort(); |
| 211 |
} |
| 212 |
|
| 213 |
return CBB_flush(&cbb) == 1; |
| 214 |
} |
| 215 |
|
| 216 |
bool SSLBuffer::ValidateBuffersState() { |
| 217 |
const uint8_t *start_ptr = buf_; |
| 218 |
const uint8_t *end_ptr = buf_ + buf_size_; |
| 219 |
const uint8_t *data_start_ = buf_ + offset_; |
| 220 |
const uint8_t *data_end_ptr = data_start_ + size_; |
| 221 |
const uint8_t *remaining_ptr = data_end_ptr + (cap_ - size_); |
| 222 |
if (data_start_ > end_ptr || data_start_ < start_ptr || |
| 223 |
data_end_ptr > end_ptr || data_end_ptr < start_ptr || size_ > cap_ || |
| 224 |
remaining_ptr > end_ptr || remaining_ptr < start_ptr) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
return true; |
| 228 |
} |
| 229 |
|
| 230 |
bool SSLBuffer::DoDeserialization(CBS &cbs) { |
| 231 |
CBS seq; |
| 232 |
uint64_t version = 0; |
| 233 |
if (!CBS_get_asn1(&cbs, &seq, CBS_ASN1_SEQUENCE) || |
| 234 |
!CBS_get_asn1_uint64(&seq, &version) || |
| 235 |
version > kSSLBufferMaxSerDeVersion) { |
| 236 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
switch (version) { |
| 241 |
case SSL_BUFFER_SERDE_VERSION_TWO: |
| 242 |
return DoDeserializationV2(seq); |
| 243 |
case SSL_BUFFER_SERDE_VERSION_ONE: |
| 244 |
return DoDeserializationV1(seq); |
| 245 |
default: |
| 246 |
return false; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
bool SSLBuffer::DoDeserializationV1(CBS &cbs) { |
| 251 |
CBS buf; |
| 252 |
int buf_allocated_int = 0; |
| 253 |
uint64_t offset = 0, size = 0, cap = 0; |
| 254 |
if (!CBS_get_asn1_bool(&cbs, &buf_allocated_int) || |
| 255 |
!CBS_get_asn1_uint64(&cbs, &offset) || |
| 256 |
!CBS_get_asn1_uint64(&cbs, &size) || !CBS_get_asn1_uint64(&cbs, &cap) || |
| 257 |
!CBS_get_asn1(&cbs, &buf, CBS_ASN1_OCTETSTRING) || CBS_len(&cbs) != 0) { |
| 258 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
if (offset > INT_MAX || size > INT_MAX || cap > INT_MAX) { |
| 263 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
// In the event that deserialization happens into an existing buffer. |
| 268 |
Clear(); |
| 269 |
|
| 270 |
bool buf_allocated = !!buf_allocated_int; |
| 271 |
if (buf_allocated) { |
| 272 |
// When buf_allocated, CBS_len(&buf) should be larger than |
| 273 |
// sizeof(inline_buf_). This is ensured in |EnsureCap|. |
| 274 |
if (CBS_len(&buf) <= sizeof(inline_buf_)) { |
| 275 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 276 |
return false; |
| 277 |
} |
| 278 |
buf_allocated_ = true; |
| 279 |
buf_ = (uint8_t *)malloc(CBS_len(&buf)); |
| 280 |
if (buf_ == NULL) { |
| 281 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 282 |
return false; |
| 283 |
} |
| 284 |
buf_size_ = CBS_len(&buf); |
| 285 |
OPENSSL_memcpy(buf_, CBS_data(&buf), CBS_len(&buf)); |
| 286 |
} else { |
| 287 |
if (CBS_len(&buf) != sizeof(inline_buf_)) { |
| 288 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 289 |
return false; |
| 290 |
} |
| 291 |
buf_allocated_ = false; |
| 292 |
buf_ = inline_buf_; |
| 293 |
buf_size_ = sizeof(inline_buf_); |
| 294 |
OPENSSL_memcpy(inline_buf_, CBS_data(&buf), CBS_len(&buf)); |
| 295 |
} |
| 296 |
buf_cap_ = header_len_ = 0; // V1 was lossy :( |
| 297 |
offset_ = (int)offset; |
| 298 |
size_ = (int)size; |
| 299 |
cap_ = (int)cap; |
| 300 |
// As we restored from a V1 format we can only serialize as V1 until the next |
| 301 |
// |EnsureCap| call. |
| 302 |
max_serialization_version_ = SSL_BUFFER_SERDE_VERSION_ONE; |
| 303 |
|
| 304 |
// Final sanity check |
| 305 |
if(!ValidateBuffersState()) { |
| 306 |
Clear(); |
| 307 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
return true; |
| 312 |
} |
| 313 |
|
| 314 |
bool SSLBuffer::DoDeserializationV2(CBS &cbs) { |
| 315 |
CBS buf; |
| 316 |
int buf_allocated_int = 0; |
| 317 |
uint64_t header_len = 0, buf_cap = 0, rel_offset = 0, cap = 0; |
| 318 |
size_t align_offset = 0; |
| 319 |
if (!CBS_get_asn1_bool(&cbs, &buf_allocated_int) || |
| 320 |
!CBS_get_asn1_uint64(&cbs, &buf_cap) || |
| 321 |
!CBS_get_asn1_uint64(&cbs, &header_len) || |
| 322 |
!CBS_get_asn1_uint64(&cbs, &rel_offset) || |
| 323 |
!CBS_get_asn1_uint64(&cbs, &cap) || |
| 324 |
!CBS_get_asn1(&cbs, &buf, CBS_ASN1_OCTETSTRING) || CBS_len(&cbs) != 0) { |
| 325 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
if (buf_cap > SSLBUFFER_MAX_CAPACITY || rel_offset > INT_MAX || |
| 330 |
rel_offset > CBS_len(&buf) || cap > INT_MAX) { |
| 331 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 332 |
return false; |
| 333 |
} |
| 334 |
|
| 335 |
// In the event that deserialization happens into an existing buffer. |
| 336 |
Clear(); |
| 337 |
|
| 338 |
bool buf_allocated = !!buf_allocated_int; |
| 339 |
if (buf_allocated) { |
| 340 |
buf_allocated_ = true; |
| 341 |
buf_size_ = compute_buffer_size(buf_cap); |
| 342 |
buf_ = (uint8_t *)malloc(buf_size_); |
| 343 |
if (buf_ == NULL) { |
| 344 |
Clear(); |
| 345 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 346 |
return false; |
| 347 |
} |
| 348 |
align_offset = compute_buffer_offset(header_len, buf_); |
| 349 |
if (rel_offset > INT_MAX - align_offset || |
| 350 |
CBS_len(&buf) - rel_offset > INT_MAX || |
| 351 |
CBS_len(&buf) > buf_size_ - align_offset) { |
| 352 |
Clear(); |
| 353 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 354 |
return false; |
| 355 |
} |
| 356 |
OPENSSL_memcpy(buf_ + align_offset, CBS_data(&buf), CBS_len(&buf)); |
| 357 |
} else { |
| 358 |
buf_allocated_ = false; |
| 359 |
buf_ = inline_buf_; |
| 360 |
buf_size_ = sizeof(inline_buf_); |
| 361 |
// We could relax this and just allocate a in-memory buffer with correct |
| 362 |
// alignment. But this value is not configurable (unlike |
| 363 |
// SSL3_ALIGN_PAYLOAD), so we can adjust this in the future if we modify |
| 364 |
// sizeof(inline_buf_); |
| 365 |
if (CBS_len(&buf) > sizeof(inline_buf_) || buf_cap > sizeof(inline_buf_)) { |
| 366 |
Clear(); |
| 367 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 368 |
return false; |
| 369 |
} |
| 370 |
OPENSSL_memcpy(buf_, CBS_data(&buf), CBS_len(&buf)); |
| 371 |
} |
| 372 |
buf_cap_ = (size_t)buf_cap; |
| 373 |
header_len_ = (size_t)header_len; |
| 374 |
offset_ = (int)(align_offset + rel_offset); |
| 375 |
size_ = (int)(CBS_len(&buf) - rel_offset); |
| 376 |
cap_ = (int)cap; |
| 377 |
max_serialization_version_ = SSL_BUFFER_SERDE_VERSION_TWO; |
| 378 |
|
| 379 |
// Final sanity check |
| 380 |
if (!ValidateBuffersState()) { |
| 381 |
Clear(); |
| 382 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 383 |
return false; |
| 384 |
} |
| 385 |
|
| 386 |
return true; |
| 387 |
} |
| 388 |
|
| 389 |
static const unsigned kBufferViewOffsetFromDataPtr = |
| 390 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0; |
| 391 |
|
| 392 |
bool SSLBuffer::SerializeBufferView(CBB &cbb, Span<uint8_t> &view) { |
| 393 |
// View must be a span that points into this buffer. |
| 394 |
if (!buf_ptr() || !view.data() || |
| 395 |
view.data() < |
| 396 |
buf_ptr() || // does the start of the view fall before the buffer |
| 397 |
(view.data() + view.size()) < |
| 398 |
buf_ptr() || // does the end of the view fall before the buffer |
| 399 |
(buf_ptr() + buf_size()) < |
| 400 |
view.data() || // does the start of the view fall after the end of |
| 401 |
// the buffer |
| 402 |
(buf_ptr() + buf_size()) < |
| 403 |
(view.data() + view.size()) // does the end of of the view fall after |
| 404 |
// the end of the buffer |
| 405 |
) { |
| 406 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 407 |
return false; |
| 408 |
} |
| 409 |
|
| 410 |
// Important: The offset should not be relative to |
| 411 |
// |buf_ptr()|, as the alignment can change on restore. |
| 412 |
// It should be relative to |data()|, which can mean it may |
| 413 |
// currently point towards data before the current offset and thus be |
| 414 |
// negative. |
| 415 |
// |
| 416 |
// Ideally for simplicity we would make this relative to read buffer's |
| 417 |
// alignment offset, but we might not know this in the V1 case if the buffer |
| 418 |
// had been restored from a V1 format :\. So this is the better option at this |
| 419 |
// time. |
| 420 |
ptrdiff_t offset = view.data() - data(); |
| 421 |
|
| 422 |
CBB child, child2; |
| 423 |
if (!CBB_add_asn1(&cbb, &child, CBS_ASN1_SEQUENCE) || |
| 424 |
!CBB_add_asn1(&child, &child2, kBufferViewOffsetFromDataPtr) || |
| 425 |
!CBB_add_asn1_int64(&child2, offset) || |
| 426 |
!CBB_add_asn1_uint64(&child, view.size())) { |
| 427 |
return false; |
| 428 |
} |
| 429 |
|
| 430 |
return CBB_flush(&cbb) == 1; |
| 431 |
} |
| 432 |
|
| 433 |
static bool deserialize_buffer_view_from_buf_ptr_offset(CBS &cbs, |
| 434 |
SSLBuffer *buffer, |
| 435 |
Span<uint8_t> &view) { |
| 436 |
if (!buffer) { |
| 437 |
abort(); |
| 438 |
} |
| 439 |
uint64_t offset = 0, size = 0; |
| 440 |
if (!CBS_get_asn1_uint64(&cbs, &offset) || |
| 441 |
!CBS_get_asn1_uint64(&cbs, &size)) { |
| 442 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 443 |
return 0; |
| 444 |
} |
| 445 |
uint8_t *view_ptr = buffer->buf_ptr() + offset; |
| 446 |
if (view_ptr < buffer->buf_ptr() || // does the start of the view fall before |
| 447 |
// the buffer |
| 448 |
view_ptr > (buffer->buf_ptr() + |
| 449 |
buffer->buf_size()) || // does the the start of the view fall |
| 450 |
// after the end of the buffer |
| 451 |
(view_ptr + size) < |
| 452 |
buffer->buf_ptr() || // does the end of the view fall |
| 453 |
// before the start of the buffer |
| 454 |
(view_ptr + size) > (buffer->buf_ptr() + |
| 455 |
buffer->buf_size()) // does the end of the view fall |
| 456 |
// after the end of the buffer |
| 457 |
|
| 458 |
) { |
| 459 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 460 |
return false; |
| 461 |
} |
| 462 |
view = MakeSpan(view_ptr, size); |
| 463 |
return true; |
| 464 |
} |
| 465 |
|
| 466 |
static int fits_in_ptrdiff_int64(int64_t x) { |
| 467 |
#if PTRDIFF_MAX > INT64_MAX || PTRDIFF_MAX == INT64_MAX |
| 468 |
// ptrdiff_t is equal to or wider than int64_t — all int64_t fit |
| 469 |
(void)x; |
| 470 |
return 1; |
| 471 |
#else |
| 472 |
// ptrdiff_t is narrower than int64_t — cast to int64_t for safe compare |
| 473 |
return x >= (int64_t)PTRDIFF_MIN && x <= (int64_t)PTRDIFF_MAX; |
| 474 |
#endif |
| 475 |
} |
| 476 |
|
| 477 |
static bool deserialize_buffer_view_from_data_offset(CBS &cbs, |
| 478 |
SSLBuffer *buffer, |
| 479 |
Span<uint8_t> &view) { |
| 480 |
if (!buffer) { |
| 481 |
abort(); |
| 482 |
} |
| 483 |
CBS child; |
| 484 |
int64_t offset = 0; |
| 485 |
uint64_t size = 0; |
| 486 |
if (!CBS_get_asn1(&cbs, &child, kBufferViewOffsetFromDataPtr) || |
| 487 |
!CBS_get_asn1_int64(&child, &offset) || |
| 488 |
!CBS_get_asn1_uint64(&cbs, &size)) { |
| 489 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 490 |
return 0; |
| 491 |
} |
| 492 |
if (!fits_in_ptrdiff_int64(offset)) { |
| 493 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 494 |
return 0; |
| 495 |
} |
| 496 |
uint8_t *view_ptr = buffer->data() + (ptrdiff_t)offset; |
| 497 |
if (view_ptr < |
| 498 |
buffer->buf_ptr() || // does the view start fall before the buffer |
| 499 |
view_ptr > |
| 500 |
(buffer->buf_ptr() + |
| 501 |
buffer->buf_size()) || // does the view start fall after the buffer |
| 502 |
(view_ptr + size) < buffer->buf_ptr() || // does the end of the view fall |
| 503 |
// before the buffer start |
| 504 |
(view_ptr + size) > (buffer->buf_ptr() + |
| 505 |
buffer->buf_size() // does the end of the view fall |
| 506 |
// after the end of the buffer |
| 507 |
)) { |
| 508 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 509 |
return 0; |
| 510 |
} |
| 511 |
view = MakeSpan(view_ptr, size); |
| 512 |
|
| 513 |
return true; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
bool SSLBuffer::DeserializeBufferView(CBS &cbs, Span<uint8_t> &view) { |
| 518 |
CBS app_seq; |
| 519 |
|
| 520 |
if (!CBS_get_asn1(&cbs, &app_seq, CBS_ASN1_SEQUENCE)) { |
| 521 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 522 |
return false; |
| 523 |
} |
| 524 |
|
| 525 |
if (CBS_peek_asn1_tag(&app_seq, CBS_ASN1_INTEGER)) { |
| 526 |
return deserialize_buffer_view_from_buf_ptr_offset(app_seq, this, view); |
| 527 |
} else if (CBS_peek_asn1_tag(&app_seq, kBufferViewOffsetFromDataPtr)) { |
| 528 |
return deserialize_buffer_view_from_data_offset(app_seq, this, view); |
| 529 |
} else { |
| 530 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_BUFFER); |
| 531 |
return false; |
| 532 |
} |
| 533 |
|
| 534 |
return true; |
| 535 |
} |
| 536 |
|
| 537 |
static int dtls_read_buffer_next_packet(SSL *ssl) { |
| 538 |
SSLBuffer *buf = &ssl->s3->read_buffer; |
| 539 |
|
| 540 |
if (!buf->empty()) { |
| 541 |
// It is an error to call |dtls_read_buffer_extend| when the read buffer is |
| 542 |
// not empty. |
| 543 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 544 |
return -1; |
| 545 |
} |
| 546 |
|
| 547 |
// Read a single packet from |ssl->rbio|. |buf->cap()| must fit in an int. |
| 548 |
int ret = |
| 549 |
BIO_read(ssl->rbio.get(), buf->data(), static_cast<int>(buf->cap())); |
| 550 |
if (ret <= 0) { |
| 551 |
ssl->s3->rwstate = SSL_ERROR_WANT_READ; |
| 552 |
return ret; |
| 553 |
} |
| 554 |
buf->DidWrite(static_cast<size_t>(ret)); |
| 555 |
return 1; |
| 556 |
} |
| 557 |
|
| 558 |
static int tls_read_buffer_extend_to(SSL *ssl, size_t len) { |
| 559 |
SSLBuffer *buf = &ssl->s3->read_buffer; |
| 560 |
|
| 561 |
if (len > buf->cap()) { |
| 562 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
| 563 |
return -1; |
| 564 |
} |
| 565 |
|
| 566 |
// Read until the target length is reached. |
| 567 |
while (buf->size() < len) { |
| 568 |
// The amount of data to read is bounded by |buf->cap|, which must fit in an |
| 569 |
// int. |
| 570 |
|
| 571 |
// If enable_read_ahead we want to attempt to fill the entire buffer, but |
| 572 |
// the while loop will bail once we have at least the requested len amount |
| 573 |
// of data. If not enable_read_ahead, only read as much to get to len bytes, |
| 574 |
// at this point we know len is less than the overall size of the buffer. |
| 575 |
assert(buf->cap() >= buf->size()); |
| 576 |
size_t read_amount = |
| 577 |
ssl->enable_read_ahead ? buf->cap() - buf->size() : len - buf->size(); |
| 578 |
assert(read_amount <= buf->cap() - buf->size()); |
| 579 |
int ret = BIO_read(ssl->rbio.get(), buf->data() + buf->size(), |
| 580 |
static_cast<int>(read_amount)); |
| 581 |
if (ret <= 0) { |
| 582 |
ssl->s3->rwstate = SSL_ERROR_WANT_READ; |
| 583 |
return ret; |
| 584 |
} |
| 585 |
buf->DidWrite(static_cast<size_t>(ret)); |
| 586 |
} |
| 587 |
|
| 588 |
return 1; |
| 589 |
} |
| 590 |
|
| 591 |
int ssl_read_buffer_extend_to(SSL *ssl, size_t len) { |
| 592 |
// |ssl_read_buffer_extend_to| implicitly discards any consumed data. |
| 593 |
ssl->s3->read_buffer.DiscardConsumed(); |
| 594 |
size_t buffer_size = len; |
| 595 |
if (SSL_is_dtls(ssl)) { |
| 596 |
static_assert(DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <= |
| 597 |
SSLBUFFER_MAX_CAPACITY, |
| 598 |
"DTLS read buffer is too large"); |
| 599 |
|
| 600 |
// The |len| parameter is ignored in DTLS. |
| 601 |
len = DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH; |
| 602 |
buffer_size = len; |
| 603 |
} else { |
| 604 |
if (ssl->ctx.get()->enable_read_ahead) { |
| 605 |
// If we're reading ahead allocate read_ahead_buffer size or the requested |
| 606 |
// len whichever is bigger |
| 607 |
buffer_size = std::max(len, ssl->read_ahead_buffer_size); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
if (!ssl->s3->read_buffer.EnsureCap(ssl_record_prefix_len(ssl), |
| 612 |
buffer_size)) { |
| 613 |
return -1; |
| 614 |
} |
| 615 |
|
| 616 |
if (ssl->rbio == nullptr) { |
| 617 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET); |
| 618 |
return -1; |
| 619 |
} |
| 620 |
|
| 621 |
int ret; |
| 622 |
if (SSL_is_dtls(ssl)) { |
| 623 |
// |len| is ignored for a datagram transport. |
| 624 |
ret = dtls_read_buffer_next_packet(ssl); |
| 625 |
} else { |
| 626 |
ret = tls_read_buffer_extend_to(ssl, len); |
| 627 |
} |
| 628 |
|
| 629 |
if (ret <= 0) { |
| 630 |
// If the buffer was empty originally and remained empty after attempting to |
| 631 |
// extend it, release the buffer until the next attempt. |
| 632 |
ssl->s3->read_buffer.DiscardConsumed(); |
| 633 |
} |
| 634 |
return ret; |
| 635 |
} |
| 636 |
|
| 637 |
int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret, |
| 638 |
size_t consumed, uint8_t alert) { |
| 639 |
*out_retry = false; |
| 640 |
if (ret != ssl_open_record_partial) { |
| 641 |
ssl->s3->read_buffer.Consume(consumed); |
| 642 |
} |
| 643 |
if (ret != ssl_open_record_success) { |
| 644 |
// Nothing was returned to the caller, so discard anything marked consumed. |
| 645 |
ssl->s3->read_buffer.DiscardConsumed(); |
| 646 |
} |
| 647 |
switch (ret) { |
| 648 |
case ssl_open_record_success: |
| 649 |
return 1; |
| 650 |
|
| 651 |
case ssl_open_record_partial: { |
| 652 |
int read_ret = ssl_read_buffer_extend_to(ssl, consumed); |
| 653 |
if (read_ret <= 0) { |
| 654 |
return read_ret; |
| 655 |
} |
| 656 |
*out_retry = true; |
| 657 |
return 1; |
| 658 |
} |
| 659 |
|
| 660 |
case ssl_open_record_discard: |
| 661 |
*out_retry = true; |
| 662 |
return 1; |
| 663 |
|
| 664 |
case ssl_open_record_close_notify: |
| 665 |
ssl->s3->rwstate = SSL_ERROR_ZERO_RETURN; |
| 666 |
return 0; |
| 667 |
|
| 668 |
case ssl_open_record_error: |
| 669 |
if (alert != 0) { |
| 670 |
ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 671 |
} |
| 672 |
return -1; |
| 673 |
} |
| 674 |
assert(0); |
| 675 |
return -1; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
static_assert(SSL3_RT_HEADER_LENGTH * 2 + |
| 680 |
SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD * 2 + |
| 681 |
SSL3_RT_MAX_PLAIN_LENGTH <= |
| 682 |
SSLBUFFER_MAX_CAPACITY, |
| 683 |
"maximum TLS write buffer is too large"); |
| 684 |
|
| 685 |
static_assert(DTLS1_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + |
| 686 |
SSL3_RT_MAX_PLAIN_LENGTH <= |
| 687 |
SSLBUFFER_MAX_CAPACITY, |
| 688 |
"maximum DTLS write buffer is too large"); |
| 689 |
|
| 690 |
static int tls_write_buffer_flush(SSL *ssl) { |
| 691 |
SSLBuffer *buf = &ssl->s3->write_buffer; |
| 692 |
|
| 693 |
while (!buf->empty()) { |
| 694 |
int ret = BIO_write(ssl->wbio.get(), buf->data(), buf->size()); |
| 695 |
if (ret <= 0) { |
| 696 |
ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; |
| 697 |
return ret; |
| 698 |
} |
| 699 |
buf->Consume(static_cast<size_t>(ret)); |
| 700 |
} |
| 701 |
buf->Clear(); |
| 702 |
return 1; |
| 703 |
} |
| 704 |
|
| 705 |
static int dtls_write_buffer_flush(SSL *ssl) { |
| 706 |
SSLBuffer *buf = &ssl->s3->write_buffer; |
| 707 |
if (buf->empty()) { |
| 708 |
return 1; |
| 709 |
} |
| 710 |
|
| 711 |
int ret = BIO_write(ssl->wbio.get(), buf->data(), buf->size()); |
| 712 |
if (ret <= 0) { |
| 713 |
ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; |
| 714 |
// If the write failed, drop the write buffer anyway. Datagram transports |
| 715 |
// can't write half a packet, so the caller is expected to retry from the |
| 716 |
// top. |
| 717 |
buf->Clear(); |
| 718 |
return ret; |
| 719 |
} |
| 720 |
buf->Clear(); |
| 721 |
return 1; |
| 722 |
} |
| 723 |
|
| 724 |
int ssl_write_buffer_flush(SSL *ssl) { |
| 725 |
if (ssl->wbio == nullptr) { |
| 726 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET); |
| 727 |
return -1; |
| 728 |
} |
| 729 |
|
| 730 |
if (SSL_is_dtls(ssl)) { |
| 731 |
return dtls_write_buffer_flush(ssl); |
| 732 |
} else { |
| 733 |
return tls_write_buffer_flush(ssl); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
BSSL_NAMESPACE_END |