| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include <openssl/ssl.h> |
| 5 |
|
| 6 |
#include <openssl/bytestring.h> |
| 7 |
#include <openssl/err.h> |
| 8 |
#include <openssl/mem.h> |
| 9 |
|
| 10 |
#include "internal.h" |
| 11 |
|
| 12 |
BSSL_NAMESPACE_BEGIN |
| 13 |
|
| 14 |
bool ssl_transfer_supported(const SSL *in) { |
| 15 |
if (in == NULL) { |
| 16 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_UNSUPPORTED); |
| 17 |
return false; |
| 18 |
} |
| 19 |
|
| 20 |
// An SSL connection can't be serialized by current implementation under some |
| 21 |
// conditions 0) It's not server SSL. 1) It's a DTLS connection. 2) It uses |
| 22 |
// QUIC 3) Its SSL_SESSION isn't serializable. 4) Handshake hasn't finished |
| 23 |
// yet. 5) TLS version is not supported(currently, only TLS 1.2 & TLS 1.3 are |
| 24 |
// supported). 6) Write is not in clean state(|SSL_write| should finish the |
| 25 |
// |in| write, no pending writes). 7) A handshake message is not in a clean |
| 26 |
// state. 8) ssl shutdown state is not ssl_shutdown_none. |
| 27 |
if (!SSL_is_server(in) || // (0) |
| 28 |
SSL_is_dtls(in) || // (1) |
| 29 |
in->quic_method != nullptr || // (2) |
| 30 |
!in->s3 || // (3) |
| 31 |
!in->s3->established_session || SSL_in_init(in) || // (4) |
| 32 |
!(in->version == TLS1_2_VERSION || |
| 33 |
in->version == TLS1_3_VERSION) || // (5) |
| 34 |
in->s3->unreported_bytes_written > 0 || // (6) |
| 35 |
in->s3->pending_write.size() > 0 || |
| 36 |
in->s3->pending_flight_offset > 0 || // (7) |
| 37 |
in->s3->read_shutdown != ssl_shutdown_none || // (8) |
| 38 |
in->s3->write_shutdown != ssl_shutdown_none || |
| 39 |
in->is_suspended_state) { |
| 40 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_UNSUPPORTED); |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
BSSL_NAMESPACE_END |
| 48 |
|
| 49 |
using namespace bssl; |
| 50 |
|
| 51 |
// SSL3_STATE_parse_octet_string gets an optional ASN.1 OCTET STRING explicitly |
| 52 |
// tagged with |tag| from |cbs| and stows it in |*out|. It returns one on |
| 53 |
// success, whether or not the element was found, and zero on decode error. |
| 54 |
static bool SSL3_STATE_parse_octet_string(CBS *cbs, Array<uint8_t> *out, |
| 55 |
unsigned tag) { |
| 56 |
CBS value; |
| 57 |
if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) { |
| 58 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 59 |
return false; |
| 60 |
} |
| 61 |
return out->CopyFrom(value); |
| 62 |
} |
| 63 |
|
| 64 |
// parse_optional_string gets an optional ASN.1 OCTET STRING explicitly |
| 65 |
// tagged with |tag| from |cbs| and saves it in |*out|. If the element was not |
| 66 |
// found, it sets |*out| to NULL. It returns one on success, whether or not the |
| 67 |
// element was found, and zero on decode error. |
| 68 |
static int parse_optional_string(CBS *cbs, UniquePtr<char> *out, unsigned tag, |
| 69 |
int reason) { |
| 70 |
CBS value; |
| 71 |
int present; |
| 72 |
if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) { |
| 73 |
OPENSSL_PUT_ERROR(SSL, reason); |
| 74 |
return 0; |
| 75 |
} |
| 76 |
if (present) { |
| 77 |
if (CBS_contains_zero_byte(&value)) { |
| 78 |
OPENSSL_PUT_ERROR(SSL, reason); |
| 79 |
return 0; |
| 80 |
} |
| 81 |
char *raw = nullptr; |
| 82 |
if (!CBS_strdup(&value, &raw)) { |
| 83 |
return 0; |
| 84 |
} |
| 85 |
out->reset(raw); |
| 86 |
} else { |
| 87 |
out->reset(); |
| 88 |
} |
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
// SSL3_STATE_get_optional_octet_string requires |dst| has |target_len| |
| 93 |
// space to store the |tag| data from the |cbs|. |
| 94 |
static bool SSL3_STATE_get_optional_octet_string(CBS *cbs, void *dst, |
| 95 |
unsigned tag, |
| 96 |
size_t target_len) { |
| 97 |
int present; |
| 98 |
CBS value; |
| 99 |
if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) { |
| 100 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 101 |
return false; |
| 102 |
} |
| 103 |
if (!present) { |
| 104 |
return true; |
| 105 |
} |
| 106 |
size_t data_len = CBS_len(&value); |
| 107 |
if (target_len > 0 && data_len != target_len) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
OPENSSL_memcpy(dst, CBS_data(&value), data_len); |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
// SSL3_STATE serialization. |
| 115 |
|
| 116 |
enum SSL3_STATE_SERDE_VERSION { |
| 117 |
SSL3_STATE_SERDE_VERSION_ONE = 1, |
| 118 |
SSL3_STATE_SERDE_VERSION_TWO = 2, |
| 119 |
SSL3_STATE_SERDE_VERSION_THREE = 3 |
| 120 |
}; |
| 121 |
|
| 122 |
static const unsigned kS3EstablishedSessionTag = |
| 123 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0; |
| 124 |
static const unsigned kS3SessionReusedTag = |
| 125 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
| 126 |
static const unsigned kS3HostNameTag = |
| 127 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2; |
| 128 |
static const unsigned kS3ALPNSelectedTag = |
| 129 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3; |
| 130 |
static const unsigned kS3NextProtoNegotiatedTag = |
| 131 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4; |
| 132 |
static const unsigned kS3ChannelIdValidTag = |
| 133 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5; |
| 134 |
static const unsigned kS3ChannelIdTag = |
| 135 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6; |
| 136 |
static const unsigned kS3SendConnectionBindingTag = |
| 137 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 7; |
| 138 |
static const unsigned kS3PendingAppDataTag = |
| 139 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8; |
| 140 |
static const unsigned kS3ReadBufferTag = |
| 141 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9; |
| 142 |
static const unsigned kS3NotResumableTag = |
| 143 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10; |
| 144 |
static const unsigned kS3EarlyDataSkippedTag = |
| 145 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 11; |
| 146 |
static const unsigned kS3DelegatedCredentialUsedTag = |
| 147 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 12; |
| 148 |
static const unsigned kS3EarlyDataAcceptedTag = |
| 149 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13; |
| 150 |
static const unsigned kS3UsedHelloRetryRequestTag = |
| 151 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14; |
| 152 |
static const unsigned kS3TicketAgeSkewTag = |
| 153 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15; |
| 154 |
static const unsigned kS3WriteTrafficSecretTag = |
| 155 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16; |
| 156 |
static const unsigned kS3WriteTrafficSecretLenTag = |
| 157 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17; |
| 158 |
static const unsigned kS3ReadTrafficSecretTag = |
| 159 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18; |
| 160 |
static const unsigned kS3ReadTrafficSecretLenTag = |
| 161 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19; |
| 162 |
static const unsigned kS3ExporterSecretTag = |
| 163 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 20; |
| 164 |
static const unsigned kS3ExporterSecretLenTag = |
| 165 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21; |
| 166 |
static const unsigned kS3HandshakeBufferTag = |
| 167 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22; |
| 168 |
static const unsigned kS3EchStatusTag = |
| 169 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 23; |
| 170 |
static const unsigned kS3PendingHsDataTag = |
| 171 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 24; |
| 172 |
static const unsigned kS3PendingFlightTag = |
| 173 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 25; |
| 174 |
static const unsigned kS3AeadReadCtxTag = |
| 175 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 26; |
| 176 |
static const unsigned kS3AeadWriteCtxTag = |
| 177 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 27; |
| 178 |
static const unsigned kS3KeyUpdatePending = |
| 179 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 28; |
| 180 |
|
| 181 |
static int serialize_buf_mem(bssl::UniquePtr<BUF_MEM> &buf, CBS_ASN1_TAG tag, |
| 182 |
CBB &cbb) { |
| 183 |
CBB child; |
| 184 |
if (!CBB_add_asn1(&cbb, &child, tag) || |
| 185 |
!CBB_add_asn1_octet_string(&child, reinterpret_cast<uint8_t *>(buf->data), |
| 186 |
buf->length) || |
| 187 |
!CBB_flush(&cbb)) { |
| 188 |
return 0; |
| 189 |
} |
| 190 |
return 1; |
| 191 |
} |
| 192 |
|
| 193 |
static int deserialize_buf_mem(bssl::UniquePtr<BUF_MEM> &buf, int &present, |
| 194 |
CBS_ASN1_TAG tag, CBS &cbs) { |
| 195 |
CBS child; |
| 196 |
|
| 197 |
if (!CBS_get_optional_asn1_octet_string(&cbs, &child, &present, tag)) { |
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
buf.reset(BUF_MEM_new()); |
| 202 |
if (!BUF_MEM_append(buf.get(), CBS_data(&child), CBS_len(&child))) { |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
return 1; |
| 207 |
} |
| 208 |
|
| 209 |
// *** EXPERIMENTAL — DO NOT USE WITHOUT CHECKING *** |
| 210 |
// These SSL3_STATE serialization functions are developed to support SSL |
| 211 |
// transfer. |
| 212 |
|
| 213 |
// ssl3_state_to_bytes serializes |in| to bytes stored in |cbb|. |
| 214 |
// It returns one on success and zero on failure. |
| 215 |
// |
| 216 |
// SSL3_STATE ASN.1 structure: see ssl_transfer_asn1.cc |
| 217 |
static int SSL3_STATE_to_bytes(SSL3_STATE *in, uint16_t protocol_version, |
| 218 |
CBB *cbb) { |
| 219 |
if (in == NULL || cbb == NULL) { |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
CBB s3, child; |
| 224 |
if (!CBB_add_asn1(cbb, &s3, CBS_ASN1_SEQUENCE) || |
| 225 |
!CBB_add_asn1_uint64(&s3, SSL3_STATE_SERDE_VERSION_THREE) || |
| 226 |
!CBB_add_asn1_octet_string(&s3, in->read_sequence, TLS_SEQ_NUM_SIZE) || |
| 227 |
!CBB_add_asn1_octet_string(&s3, in->write_sequence, TLS_SEQ_NUM_SIZE) || |
| 228 |
!CBB_add_asn1_octet_string(&s3, in->server_random, SSL3_RANDOM_SIZE) || |
| 229 |
!CBB_add_asn1_octet_string(&s3, in->client_random, SSL3_RANDOM_SIZE) || |
| 230 |
!CBB_add_asn1_octet_string(&s3, in->send_alert, SSL3_SEND_ALERT_SIZE) || |
| 231 |
!CBB_add_asn1_int64(&s3, in->rwstate) || |
| 232 |
!CBB_add_asn1_int64(&s3, in->early_data_reason) || |
| 233 |
!CBB_add_asn1_octet_string(&s3, in->previous_client_finished, |
| 234 |
in->previous_client_finished_len) || |
| 235 |
!CBB_add_asn1_uint64(&s3, in->previous_client_finished_len) || |
| 236 |
!CBB_add_asn1_octet_string(&s3, in->previous_server_finished, |
| 237 |
in->previous_server_finished_len) || |
| 238 |
!CBB_add_asn1_uint64(&s3, in->previous_server_finished_len) || |
| 239 |
!CBB_add_asn1_uint64(&s3, in->empty_record_count) || |
| 240 |
!CBB_add_asn1_uint64(&s3, in->warning_alert_count) || |
| 241 |
!CBB_add_asn1_uint64(&s3, in->total_renegotiations)) { |
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
if (!CBB_add_asn1(&s3, &child, kS3EstablishedSessionTag) || |
| 246 |
!ssl_session_serialize(in->established_session.get(), &child)) { |
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
if (in->session_reused) { |
| 251 |
if (!CBB_add_asn1(&s3, &child, kS3SessionReusedTag) || |
| 252 |
!CBB_add_asn1_bool(&child, true)) { |
| 253 |
return 0; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
if (in->hostname) { |
| 258 |
if (!CBB_add_asn1(&s3, &child, kS3HostNameTag) || |
| 259 |
!CBB_add_asn1_octet_string(&child, |
| 260 |
(const uint8_t *)(in->hostname.get()), |
| 261 |
strlen(in->hostname.get()))) { |
| 262 |
return 0; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if (!in->alpn_selected.empty()) { |
| 267 |
if (!CBB_add_asn1(&s3, &child, kS3ALPNSelectedTag) || |
| 268 |
!CBB_add_asn1_octet_string(&child, in->alpn_selected.data(), |
| 269 |
in->alpn_selected.size())) { |
| 270 |
return 0; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
if (!in->next_proto_negotiated.empty()) { |
| 275 |
if (!CBB_add_asn1(&s3, &child, kS3NextProtoNegotiatedTag) || |
| 276 |
!CBB_add_asn1_octet_string(&child, in->next_proto_negotiated.data(), |
| 277 |
in->next_proto_negotiated.size())) { |
| 278 |
return 0; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
if (in->channel_id_valid) { |
| 283 |
if (!CBB_add_asn1(&s3, &child, kS3ChannelIdValidTag) || |
| 284 |
!CBB_add_asn1_bool(&child, true)) { |
| 285 |
return 0; |
| 286 |
} |
| 287 |
if (!CBB_add_asn1(&s3, &child, kS3ChannelIdTag) || |
| 288 |
!CBB_add_asn1_octet_string(&child, in->channel_id, |
| 289 |
SSL3_CHANNEL_ID_SIZE)) { |
| 290 |
return 0; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
if (in->send_connection_binding) { |
| 295 |
if (!CBB_add_asn1(&s3, &child, kS3SendConnectionBindingTag) || |
| 296 |
!CBB_add_asn1_bool(&child, true)) { |
| 297 |
return 0; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
if (!in->pending_app_data.empty()) { |
| 302 |
if (!CBB_add_asn1(&s3, &child, kS3PendingAppDataTag) || |
| 303 |
!in->read_buffer.SerializeBufferView(child, in->pending_app_data)) { |
| 304 |
return 0; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
if (!in->pending_app_data.empty() || !in->read_buffer.empty()) { |
| 309 |
if (!CBB_add_asn1(&s3, &child, kS3ReadBufferTag) || |
| 310 |
!in->read_buffer.DoSerialization(child)) { |
| 311 |
return 0; |
| 312 |
} |
| 313 |
} |
| 314 |
// serialization of |not_resumable| is not added in |ssl_session_serialize| |
| 315 |
// because the function is used to serialize a session for resumption. |
| 316 |
if (in->established_session.get()->not_resumable) { |
| 317 |
if (!CBB_add_asn1(&s3, &child, kS3NotResumableTag) || |
| 318 |
!CBB_add_asn1_bool(&child, true)) { |
| 319 |
return 0; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
// Version 2 and higher extensions starts below, all which are optional as they are |
| 324 |
// TLS 1.3 specific. |
| 325 |
if (protocol_version >= TLS1_3_VERSION) { |
| 326 |
if (!CBB_add_asn1(&s3, &child, kS3EarlyDataSkippedTag) || |
| 327 |
!CBB_add_asn1_uint64(&child, in->early_data_skipped)) { |
| 328 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 329 |
return 0; |
| 330 |
} |
| 331 |
|
| 332 |
if (!CBB_add_asn1(&s3, &child, kS3DelegatedCredentialUsedTag) || |
| 333 |
!CBB_add_asn1_bool(&child, in->delegated_credential_used)) { |
| 334 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 335 |
return 0; |
| 336 |
} |
| 337 |
|
| 338 |
if (!CBB_add_asn1(&s3, &child, kS3EarlyDataAcceptedTag) || |
| 339 |
!CBB_add_asn1_bool(&child, in->early_data_accepted)) { |
| 340 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
if (!CBB_add_asn1(&s3, &child, kS3UsedHelloRetryRequestTag) || |
| 345 |
!CBB_add_asn1_bool(&child, in->used_hello_retry_request)) { |
| 346 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
if (!CBB_add_asn1(&s3, &child, kS3TicketAgeSkewTag) || |
| 351 |
!CBB_add_asn1_int64(&child, in->ticket_age_skew)) { |
| 352 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 353 |
return 0; |
| 354 |
} |
| 355 |
|
| 356 |
if (!CBB_add_asn1(&s3, &child, kS3WriteTrafficSecretTag) || |
| 357 |
!CBB_add_asn1_octet_string(&child, in->write_traffic_secret, |
| 358 |
in->write_traffic_secret_len)) { |
| 359 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 360 |
return 0; |
| 361 |
} |
| 362 |
|
| 363 |
if (!CBB_add_asn1(&s3, &child, kS3WriteTrafficSecretLenTag) || |
| 364 |
!CBB_add_asn1_uint64(&child, in->write_traffic_secret_len)) { |
| 365 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
if (!CBB_add_asn1(&s3, &child, kS3ReadTrafficSecretTag) || |
| 370 |
!CBB_add_asn1_octet_string(&child, in->read_traffic_secret, |
| 371 |
in->read_traffic_secret_len)) { |
| 372 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 373 |
return 0; |
| 374 |
} |
| 375 |
|
| 376 |
if (!CBB_add_asn1(&s3, &child, kS3ReadTrafficSecretLenTag) || |
| 377 |
!CBB_add_asn1_uint64(&child, in->read_traffic_secret_len)) { |
| 378 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
if (!CBB_add_asn1(&s3, &child, kS3ExporterSecretTag) || |
| 383 |
!CBB_add_asn1_octet_string(&child, in->exporter_secret, |
| 384 |
in->exporter_secret_len)) { |
| 385 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 386 |
return 0; |
| 387 |
} |
| 388 |
|
| 389 |
if (!CBB_add_asn1(&s3, &child, kS3ExporterSecretLenTag) || |
| 390 |
!CBB_add_asn1_uint64(&child, in->exporter_secret_len)) { |
| 391 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
if (in->hs_buf && in->hs_buf->length > 0) { |
| 396 |
if (!CBB_add_asn1(&s3, &child, kS3HandshakeBufferTag) || |
| 397 |
!CBB_add_asn1_octet_string( |
| 398 |
&child, reinterpret_cast<uint8_t *>(in->hs_buf->data), |
| 399 |
in->hs_buf->length)) { |
| 400 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 401 |
return 0; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
if (in->ech_status) { |
| 406 |
if (!CBB_add_asn1(&s3, &child, kS3EchStatusTag) || |
| 407 |
!CBB_add_asn1_uint64(&child, in->ech_status)) { |
| 408 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 409 |
return 0; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
if (in->pending_hs_data && |
| 414 |
(in->pending_hs_data->length > 0 || in->pending_hs_data->max > 0)) { |
| 415 |
if (!serialize_buf_mem(in->pending_hs_data, kS3PendingHsDataTag, s3)) { |
| 416 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 417 |
return 0; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
if (in->pending_flight && |
| 422 |
(in->pending_flight->length > 0 || in->pending_flight->max > 0)) { |
| 423 |
if (!serialize_buf_mem(in->pending_flight, kS3PendingFlightTag, s3)) { |
| 424 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 425 |
return 0; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
if (in->aead_read_ctx) { |
| 430 |
if (!CBB_add_asn1(&s3, &child, kS3AeadReadCtxTag) || |
| 431 |
!in->aead_read_ctx.get()->SerializeState(&child)) { |
| 432 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 433 |
return 0; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
if (in->aead_write_ctx) { |
| 438 |
if (!CBB_add_asn1(&s3, &child, kS3AeadWriteCtxTag) || |
| 439 |
!in->aead_write_ctx.get()->SerializeState(&child)) { |
| 440 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 441 |
return 0; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
if (!CBB_add_asn1(&s3, &child, kS3KeyUpdatePending) || |
| 446 |
!CBB_add_asn1_int64(&child, in->key_update_pending)) { |
| 447 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 448 |
return 0; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
return CBB_flush(cbb); |
| 453 |
} |
| 454 |
|
| 455 |
static int SSL3_STATE_parse_session(CBS *cbs, UniquePtr<SSL_SESSION> *out, |
| 456 |
const SSL_CTX *ctx) { |
| 457 |
CBS value; |
| 458 |
int present; |
| 459 |
if (!CBS_get_optional_asn1(cbs, &value, &present, kS3EstablishedSessionTag)) { |
| 460 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 461 |
return 0; |
| 462 |
} |
| 463 |
if (present) { |
| 464 |
UniquePtr<SSL_SESSION> ptr = |
| 465 |
SSL_SESSION_parse(&value, ctx->x509_method, ctx->pool); |
| 466 |
if (!ptr) { |
| 467 |
return 0; |
| 468 |
} |
| 469 |
out->reset(ptr.release()); |
| 470 |
return 1; |
| 471 |
} else { |
| 472 |
// session should exist because ssl transfer only supports SSL completes |
| 473 |
// handshake. |
| 474 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 475 |
out->reset(); |
| 476 |
return 0; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
// SSL3_STATE_from_bytes recovers SSL3_STATE from |cbs|. |
| 481 |
// |ssl| is used because |tls1_configure_aead| is used to recover |
| 482 |
// |aead_read_ctx| and |aead_write_ctx|. |
| 483 |
static int SSL3_STATE_from_bytes(SSL *ssl, CBS *cbs, const SSL_CTX *ctx) { |
| 484 |
// We expect the caller to have configured |ssl| with the protocol |
| 485 |
// version prior to calling us. |
| 486 |
uint16_t protocol_version; |
| 487 |
if (!ssl_protocol_version_from_wire(&protocol_version, ssl->version)) { |
| 488 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 489 |
return 0; |
| 490 |
} |
| 491 |
bool is_tls13 = protocol_version >= TLS1_3_VERSION; |
| 492 |
|
| 493 |
SSL3_STATE *out = ssl->s3; |
| 494 |
CBS s3, read_seq, write_seq, server_random, client_random, send_alert, |
| 495 |
pending_app_data, read_buffer; |
| 496 |
CBS previous_client_finished, previous_server_finished; |
| 497 |
int session_reused, channel_id_valid, send_connection_binding, not_resumable; |
| 498 |
uint64_t serde_version, early_data_reason, previous_client_finished_len, |
| 499 |
previous_server_finished_len; |
| 500 |
uint64_t empty_record_count, warning_alert_count, total_renegotiations; |
| 501 |
int64_t rwstate; |
| 502 |
int pending_app_data_present, read_buffer_present; |
| 503 |
if (!CBS_get_asn1(cbs, &s3, CBS_ASN1_SEQUENCE) || |
| 504 |
!CBS_get_asn1_uint64(&s3, &serde_version) || |
| 505 |
serde_version > SSL3_STATE_SERDE_VERSION_THREE || |
| 506 |
(is_tls13 && serde_version < SSL3_STATE_SERDE_VERSION_TWO) || |
| 507 |
!CBS_get_asn1(&s3, &read_seq, CBS_ASN1_OCTETSTRING) || |
| 508 |
CBS_len(&read_seq) != TLS_SEQ_NUM_SIZE || |
| 509 |
!CBS_get_asn1(&s3, &write_seq, CBS_ASN1_OCTETSTRING) || |
| 510 |
CBS_len(&write_seq) != TLS_SEQ_NUM_SIZE || |
| 511 |
!CBS_get_asn1(&s3, &server_random, CBS_ASN1_OCTETSTRING) || |
| 512 |
CBS_len(&server_random) != SSL3_RANDOM_SIZE || |
| 513 |
!CBS_get_asn1(&s3, &client_random, CBS_ASN1_OCTETSTRING) || |
| 514 |
CBS_len(&client_random) != SSL3_RANDOM_SIZE || |
| 515 |
!CBS_get_asn1(&s3, &send_alert, CBS_ASN1_OCTETSTRING) || |
| 516 |
CBS_len(&send_alert) != SSL3_SEND_ALERT_SIZE || |
| 517 |
!CBS_get_asn1_int64(&s3, &rwstate) || |
| 518 |
!CBS_get_asn1_uint64(&s3, &early_data_reason) || |
| 519 |
early_data_reason > ssl_early_data_reason_max_value || |
| 520 |
!CBS_get_asn1(&s3, &previous_client_finished, CBS_ASN1_OCTETSTRING) || |
| 521 |
!CBS_get_asn1_uint64(&s3, &previous_client_finished_len) || |
| 522 |
previous_client_finished_len > PREV_FINISHED_MAX_SIZE || |
| 523 |
(serde_version >= SSL3_STATE_SERDE_VERSION_THREE && |
| 524 |
CBS_len(&previous_client_finished) != previous_client_finished_len) || |
| 525 |
(serde_version < SSL3_STATE_SERDE_VERSION_THREE && |
| 526 |
CBS_len(&previous_client_finished) > PREV_FINISHED_MAX_SIZE) || |
| 527 |
!CBS_get_asn1(&s3, &previous_server_finished, CBS_ASN1_OCTETSTRING) || |
| 528 |
!CBS_get_asn1_uint64(&s3, &previous_server_finished_len) || |
| 529 |
previous_server_finished_len > PREV_FINISHED_MAX_SIZE || |
| 530 |
(serde_version >= SSL3_STATE_SERDE_VERSION_THREE && |
| 531 |
CBS_len(&previous_server_finished) != previous_server_finished_len) || |
| 532 |
(serde_version < SSL3_STATE_SERDE_VERSION_THREE && |
| 533 |
CBS_len(&previous_server_finished) > PREV_FINISHED_MAX_SIZE) || |
| 534 |
!CBS_get_asn1_uint64(&s3, &empty_record_count) || |
| 535 |
!CBS_get_asn1_uint64(&s3, &warning_alert_count) || |
| 536 |
!CBS_get_asn1_uint64(&s3, &total_renegotiations) || |
| 537 |
!SSL3_STATE_parse_session(&s3, &(out->established_session), ctx) || |
| 538 |
!CBS_get_optional_asn1_bool(&s3, &session_reused, kS3SessionReusedTag, |
| 539 |
0 /* default to false */) || |
| 540 |
!parse_optional_string(&s3, &(out->hostname), kS3HostNameTag, |
| 541 |
SSL_R_SERIALIZATION_INVALID_SSL3_STATE) || |
| 542 |
!SSL3_STATE_parse_octet_string(&s3, &(out->alpn_selected), |
| 543 |
kS3ALPNSelectedTag) || |
| 544 |
!SSL3_STATE_parse_octet_string(&s3, &(out->next_proto_negotiated), |
| 545 |
kS3NextProtoNegotiatedTag) || |
| 546 |
!CBS_get_optional_asn1_bool(&s3, &channel_id_valid, kS3ChannelIdValidTag, |
| 547 |
0 /* default to false */) || |
| 548 |
!SSL3_STATE_get_optional_octet_string( |
| 549 |
&s3, out->channel_id, kS3ChannelIdTag, SSL3_CHANNEL_ID_SIZE) || |
| 550 |
!CBS_get_optional_asn1_bool(&s3, &send_connection_binding, |
| 551 |
kS3SendConnectionBindingTag, |
| 552 |
0 /* default to false */) || |
| 553 |
!CBS_get_optional_asn1(&s3, &pending_app_data, &pending_app_data_present, |
| 554 |
kS3PendingAppDataTag) || |
| 555 |
!CBS_get_optional_asn1(&s3, &read_buffer, &read_buffer_present, |
| 556 |
kS3ReadBufferTag) || |
| 557 |
!CBS_get_optional_asn1_bool(&s3, ¬_resumable, kS3NotResumableTag, |
| 558 |
0 /* default to false */)) { |
| 559 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 560 |
return 0; |
| 561 |
} |
| 562 |
|
| 563 |
// We should have no more data at this point if we are deserializing v1 |
| 564 |
// encoding. |
| 565 |
if (serde_version < SSL3_STATE_SERDE_VERSION_TWO && CBS_len(&s3) > 0) { |
| 566 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 567 |
return 0; |
| 568 |
} |
| 569 |
|
| 570 |
uint64_t early_data_skipped; |
| 571 |
if (!CBS_get_optional_asn1_uint64(&s3, &early_data_skipped, |
| 572 |
kS3EarlyDataSkippedTag, |
| 573 |
0 /* default to 0 */) || |
| 574 |
early_data_skipped > UINT16_MAX) { |
| 575 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 576 |
return 0; |
| 577 |
} |
| 578 |
out->early_data_skipped = static_cast<uint16_t>(early_data_skipped); |
| 579 |
|
| 580 |
int delegated_credential_used; |
| 581 |
if (!CBS_get_optional_asn1_bool(&s3, &delegated_credential_used, |
| 582 |
kS3DelegatedCredentialUsedTag, 0)) { |
| 583 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 584 |
return 0; |
| 585 |
} |
| 586 |
out->delegated_credential_used = delegated_credential_used != 0; |
| 587 |
|
| 588 |
int early_data_accepted; |
| 589 |
if (!CBS_get_optional_asn1_bool(&s3, &early_data_accepted, |
| 590 |
kS3EarlyDataAcceptedTag, 0)) { |
| 591 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 592 |
return 0; |
| 593 |
} |
| 594 |
out->early_data_accepted = early_data_accepted != 0; |
| 595 |
|
| 596 |
int used_hello_retry_request; |
| 597 |
if (!CBS_get_optional_asn1_bool(&s3, &used_hello_retry_request, |
| 598 |
kS3UsedHelloRetryRequestTag, 0)) { |
| 599 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 600 |
return 0; |
| 601 |
} |
| 602 |
out->used_hello_retry_request = used_hello_retry_request != 0; |
| 603 |
|
| 604 |
int64_t ticket_age_skew; |
| 605 |
if (!CBS_get_optional_asn1_int64(&s3, &ticket_age_skew, kS3TicketAgeSkewTag, |
| 606 |
0) || |
| 607 |
ticket_age_skew > INT32_MAX || ticket_age_skew < INT32_MIN) { |
| 608 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 609 |
return 0; |
| 610 |
} |
| 611 |
out->ticket_age_skew = static_cast<int32_t>(ticket_age_skew); |
| 612 |
|
| 613 |
CBS write_traffic_secret; |
| 614 |
int write_traffic_secret_present; |
| 615 |
if (!CBS_get_optional_asn1_octet_string(&s3, &write_traffic_secret, |
| 616 |
&write_traffic_secret_present, |
| 617 |
kS3WriteTrafficSecretTag)) { |
| 618 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 619 |
return 0; |
| 620 |
} |
| 621 |
|
| 622 |
uint64_t write_traffic_secret_len; |
| 623 |
if (!CBS_get_optional_asn1_uint64(&s3, &write_traffic_secret_len, |
| 624 |
kS3WriteTrafficSecretLenTag, 0)) { |
| 625 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 626 |
return 0; |
| 627 |
} |
| 628 |
|
| 629 |
CBS read_traffic_secret; |
| 630 |
int read_traffic_secret_present; |
| 631 |
if (!CBS_get_optional_asn1_octet_string(&s3, &read_traffic_secret, |
| 632 |
&read_traffic_secret_present, |
| 633 |
kS3ReadTrafficSecretTag)) { |
| 634 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 635 |
return 0; |
| 636 |
} |
| 637 |
|
| 638 |
uint64_t read_traffic_secret_len; |
| 639 |
if (!CBS_get_optional_asn1_uint64(&s3, &read_traffic_secret_len, |
| 640 |
kS3ReadTrafficSecretLenTag, 0)) { |
| 641 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
CBS exporter_secret; |
| 646 |
int exporter_secret_present; |
| 647 |
if (!CBS_get_optional_asn1_octet_string(&s3, &exporter_secret, |
| 648 |
&exporter_secret_present, |
| 649 |
kS3ExporterSecretTag)) { |
| 650 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 651 |
return 0; |
| 652 |
} |
| 653 |
|
| 654 |
uint64_t exporter_secret_len; |
| 655 |
if (!CBS_get_optional_asn1_uint64(&s3, &exporter_secret_len, |
| 656 |
kS3ExporterSecretLenTag, 0)) { |
| 657 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 658 |
return 0; |
| 659 |
} |
| 660 |
|
| 661 |
CBS hs_buf; |
| 662 |
int hs_buf_present; |
| 663 |
if (!CBS_get_optional_asn1_octet_string(&s3, &hs_buf, &hs_buf_present, |
| 664 |
kS3HandshakeBufferTag)) { |
| 665 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 666 |
return 0; |
| 667 |
} |
| 668 |
|
| 669 |
uint64_t ech_status; |
| 670 |
if (!CBS_get_optional_asn1_uint64(&s3, &ech_status, kS3EchStatusTag, |
| 671 |
ssl_ech_none)) { |
| 672 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 673 |
return 0; |
| 674 |
} |
| 675 |
|
| 676 |
bssl::UniquePtr<BUF_MEM> pending_hs_data; |
| 677 |
int pending_hs_data_present; |
| 678 |
|
| 679 |
if (!deserialize_buf_mem(pending_hs_data, pending_hs_data_present, |
| 680 |
kS3PendingHsDataTag, s3)) { |
| 681 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 682 |
return 0; |
| 683 |
} |
| 684 |
|
| 685 |
bssl::UniquePtr<BUF_MEM> pending_flight; |
| 686 |
int pending_flight_present; |
| 687 |
if (!deserialize_buf_mem(pending_flight, pending_flight_present, |
| 688 |
kS3PendingFlightTag, s3)) { |
| 689 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 690 |
return 0; |
| 691 |
} |
| 692 |
|
| 693 |
CBS aead_read_ctx; |
| 694 |
int aead_read_ctx_present; |
| 695 |
if (!CBS_get_optional_asn1(&s3, &aead_read_ctx, &aead_read_ctx_present, |
| 696 |
kS3AeadReadCtxTag)) { |
| 697 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 698 |
return 0; |
| 699 |
} |
| 700 |
|
| 701 |
CBS aead_write_ctx; |
| 702 |
int aead_write_ctx_present; |
| 703 |
if (!CBS_get_optional_asn1(&s3, &aead_write_ctx, &aead_write_ctx_present, |
| 704 |
kS3AeadWriteCtxTag)) { |
| 705 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 706 |
return 0; |
| 707 |
} |
| 708 |
|
| 709 |
int64_t key_update_pending = SSL_KEY_UPDATE_NONE; |
| 710 |
if (!CBS_get_optional_asn1_int64(&s3, &key_update_pending, |
| 711 |
kS3KeyUpdatePending, SSL_KEY_UPDATE_NONE)) { |
| 712 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 713 |
return 0; |
| 714 |
} |
| 715 |
|
| 716 |
if (is_tls13) { |
| 717 |
if (!write_traffic_secret_present || !write_traffic_secret_len || |
| 718 |
write_traffic_secret_len > UINT8_MAX || |
| 719 |
write_traffic_secret_len > SSL_MAX_MD_SIZE || |
| 720 |
(serde_version >= SSL3_STATE_SERDE_VERSION_THREE && |
| 721 |
CBS_len(&write_traffic_secret) != write_traffic_secret_len) || |
| 722 |
(serde_version < SSL3_STATE_SERDE_VERSION_THREE && |
| 723 |
CBS_len(&write_traffic_secret) != SSL_MAX_MD_SIZE) || |
| 724 |
!read_traffic_secret_present || !read_traffic_secret_len || |
| 725 |
read_traffic_secret_len > UINT8_MAX || |
| 726 |
read_traffic_secret_len > SSL_MAX_MD_SIZE || |
| 727 |
(serde_version >= SSL3_STATE_SERDE_VERSION_THREE && |
| 728 |
CBS_len(&read_traffic_secret) != read_traffic_secret_len) || |
| 729 |
(serde_version < SSL3_STATE_SERDE_VERSION_THREE && |
| 730 |
CBS_len(&read_traffic_secret) != SSL_MAX_MD_SIZE) || |
| 731 |
!exporter_secret_present || !exporter_secret_len || |
| 732 |
exporter_secret_len > UINT8_MAX || |
| 733 |
exporter_secret_len > SSL_MAX_MD_SIZE || |
| 734 |
(serde_version >= SSL3_STATE_SERDE_VERSION_THREE && |
| 735 |
CBS_len(&exporter_secret) != exporter_secret_len) || |
| 736 |
(serde_version < SSL3_STATE_SERDE_VERSION_THREE && |
| 737 |
CBS_len(&exporter_secret) != SSL_MAX_MD_SIZE) || |
| 738 |
ech_status > ssl_ech_rejected || !aead_read_ctx_present || |
| 739 |
!aead_write_ctx_present || |
| 740 |
!(key_update_pending == SSL_KEY_UPDATE_NONE || |
| 741 |
key_update_pending == SSL_KEY_UPDATE_NOT_REQUESTED || |
| 742 |
key_update_pending == SSL_KEY_UPDATE_REQUESTED)) { |
| 743 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 744 |
return 0; |
| 745 |
} |
| 746 |
|
| 747 |
OPENSSL_memcpy(out->exporter_secret, CBS_data(&exporter_secret), |
| 748 |
exporter_secret_len); |
| 749 |
out->exporter_secret_len = exporter_secret_len; |
| 750 |
|
| 751 |
out->hs_buf.reset(BUF_MEM_new()); |
| 752 |
if (hs_buf_present) { |
| 753 |
if (!BUF_MEM_append(out->hs_buf.get(), CBS_data(&hs_buf), |
| 754 |
CBS_len(&hs_buf))) { |
| 755 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 756 |
return 0; |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
out->ech_status = static_cast<ssl_ech_status_t>(ech_status); |
| 761 |
} else if (write_traffic_secret_present || write_traffic_secret_len || |
| 762 |
read_traffic_secret_present || read_traffic_secret_len || |
| 763 |
exporter_secret_present || exporter_secret_len || hs_buf_present || |
| 764 |
ech_status || pending_hs_data_present || pending_flight_present) { |
| 765 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 766 |
return 0; |
| 767 |
} |
| 768 |
|
| 769 |
if (read_buffer_present && |
| 770 |
!out->read_buffer.DoDeserialization(read_buffer)) { |
| 771 |
return 0; |
| 772 |
} |
| 773 |
// If |pending_app_data_size| is not zero, it needs to point to |read_buffer|. |
| 774 |
if (pending_app_data_present) { |
| 775 |
if (!read_buffer_present || !out->read_buffer.DeserializeBufferView( |
| 776 |
pending_app_data, out->pending_app_data)) { |
| 777 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 778 |
return 0; |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
if (CBS_len(&s3)) { |
| 783 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 784 |
return 0; |
| 785 |
} |
| 786 |
|
| 787 |
// Initialize some states before call |tls1_configure_aead|. |
| 788 |
// Below comment is copied from |SSL_do_handshake|. |
| 789 |
// Destroy the handshake object if the handshake has completely finished. |
| 790 |
out->hs.reset(); |
| 791 |
// have_version is true if the connection's final version is known. Otherwise |
| 792 |
// the version has not been negotiated yet. |
| 793 |
out->have_version = true; |
| 794 |
OPENSSL_memcpy(out->server_random, CBS_data(&server_random), |
| 795 |
SSL3_RANDOM_SIZE); |
| 796 |
OPENSSL_memcpy(out->client_random, CBS_data(&client_random), |
| 797 |
SSL3_RANDOM_SIZE); |
| 798 |
SSL_SESSION *session = out->established_session.get(); |
| 799 |
if (is_tls13) { |
| 800 |
if (!tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal, |
| 801 |
session, |
| 802 |
MakeSpan(CBS_data(&write_traffic_secret), |
| 803 |
write_traffic_secret_len))) { |
| 804 |
return 0; |
| 805 |
} |
| 806 |
if (!tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_open, |
| 807 |
session, |
| 808 |
MakeSpan(CBS_data(&read_traffic_secret), |
| 809 |
read_traffic_secret_len))) { |
| 810 |
return 0; |
| 811 |
} |
| 812 |
|
| 813 |
// !! We set pending_hs_data and pending_hs_flight after as to avoid |
| 814 |
// tls13_set_traffic_key from trying to flush their contents if they are not |
| 815 |
// empty !! |
| 816 |
|
| 817 |
if (pending_hs_data_present) { |
| 818 |
out->pending_hs_data = std::move(pending_hs_data); |
| 819 |
} else { |
| 820 |
out->pending_hs_data.reset(BUF_MEM_new()); |
| 821 |
} |
| 822 |
|
| 823 |
if (pending_flight_present) { |
| 824 |
out->pending_flight = std::move(pending_flight); |
| 825 |
} else { |
| 826 |
out->pending_flight.reset(BUF_MEM_new()); |
| 827 |
} |
| 828 |
} else { |
| 829 |
// the impl of |SSL_serialize_handback|, which only fetch IV when it's |
| 830 |
// TLS 1. |
| 831 |
Array<uint8_t> key_block1, key_block2; |
| 832 |
if (!tls1_configure_aead(ssl, evp_aead_seal, &key_block1, session, {})) { |
| 833 |
return 0; |
| 834 |
} |
| 835 |
if (!tls1_configure_aead(ssl, evp_aead_open, &key_block2, session, {})) { |
| 836 |
return 0; |
| 837 |
} |
| 838 |
} |
| 839 |
if (aead_read_ctx_present && |
| 840 |
!out->aead_read_ctx->DeserializeState(&aead_read_ctx)) { |
| 841 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 842 |
return 0; |
| 843 |
} |
| 844 |
if (aead_write_ctx_present && |
| 845 |
!out->aead_write_ctx->DeserializeState(&aead_write_ctx)) { |
| 846 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL3_STATE); |
| 847 |
return 0; |
| 848 |
} |
| 849 |
// read_sequence & write_sequence must be set AFTER setting the traffic keys, |
| 850 |
// otherwise would be reset back to zero. |
| 851 |
OPENSSL_memcpy(out->read_sequence, CBS_data(&read_seq), TLS_SEQ_NUM_SIZE); |
| 852 |
OPENSSL_memcpy(out->write_sequence, CBS_data(&write_seq), TLS_SEQ_NUM_SIZE); |
| 853 |
OPENSSL_memcpy(out->send_alert, CBS_data(&send_alert), SSL3_SEND_ALERT_SIZE); |
| 854 |
OPENSSL_memcpy(out->previous_client_finished, |
| 855 |
CBS_data(&previous_client_finished), previous_client_finished_len); |
| 856 |
OPENSSL_memcpy(out->previous_server_finished, |
| 857 |
CBS_data(&previous_server_finished), previous_server_finished_len); |
| 858 |
out->early_data_reason = |
| 859 |
static_cast<ssl_early_data_reason_t>(early_data_reason); |
| 860 |
out->rwstate = rwstate; |
| 861 |
out->session_reused = !!session_reused; |
| 862 |
if (out->session_reused) { |
| 863 |
ssl->session = UpRef(out->established_session); |
| 864 |
} |
| 865 |
out->channel_id_valid = !!channel_id_valid; |
| 866 |
out->previous_client_finished_len = previous_client_finished_len; |
| 867 |
out->previous_server_finished_len = previous_server_finished_len; |
| 868 |
out->v2_hello_done = true; |
| 869 |
out->initial_handshake_complete = true; |
| 870 |
out->empty_record_count = empty_record_count; |
| 871 |
out->warning_alert_count = warning_alert_count; |
| 872 |
out->total_renegotiations = total_renegotiations; |
| 873 |
out->send_connection_binding = !!send_connection_binding; |
| 874 |
out->established_session.get()->not_resumable = !!not_resumable; |
| 875 |
out->key_update_pending = key_update_pending; |
| 876 |
return 1; |
| 877 |
} |
| 878 |
|
| 879 |
// SSL_CONFIG serialization. |
| 880 |
|
| 881 |
static const unsigned kSSLConfigVersion = 2; |
| 882 |
|
| 883 |
static const unsigned kSSLConfigOcspStaplingEnabledTag = |
| 884 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0; |
| 885 |
|
| 886 |
static const unsigned kSSLConfigJdk11WorkaroundTag = |
| 887 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
| 888 |
|
| 889 |
static const unsigned kSSLConfigConfMaxVersionUseDefault = |
| 890 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2; |
| 891 |
|
| 892 |
static const unsigned kSSLConfigConfMinVersionUseDefault = |
| 893 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3; |
| 894 |
|
| 895 |
// *** EXPERIMENTAL — DO NOT USE WITHOUT CHECKING *** |
| 896 |
// These SSL_CONFIG serialization functions are developed to support SSL |
| 897 |
// transfer. Most fields of SSL_CONFIG are not used after handshake completes. |
| 898 |
// It only encodes some fields needed by SSL_*_getter functions. |
| 899 |
|
| 900 |
// SSL_CONFIG_to_bytes serializes |in| to bytes stored in |cbb|. |
| 901 |
// It returns one on success and zero on failure. |
| 902 |
// |
| 903 |
// SSL_CONFIG ASN.1 structure: see ssl_transfer_asn1.cc |
| 904 |
static int SSL_CONFIG_to_bytes(SSL_CONFIG *in, CBB *cbb) { |
| 905 |
if (in == NULL || cbb == NULL) { |
| 906 |
return 0; |
| 907 |
} |
| 908 |
|
| 909 |
CBB config, child; |
| 910 |
if (!CBB_add_asn1(cbb, &config, CBS_ASN1_SEQUENCE) || |
| 911 |
!CBB_add_asn1_uint64(&config, kSSLConfigVersion) || |
| 912 |
!CBB_add_asn1_uint64(&config, in->conf_max_version) || |
| 913 |
!CBB_add_asn1_uint64(&config, in->conf_min_version)) { |
| 914 |
return 0; |
| 915 |
} |
| 916 |
|
| 917 |
if (in->ocsp_stapling_enabled) { |
| 918 |
if (!CBB_add_asn1(&config, &child, kSSLConfigOcspStaplingEnabledTag) || |
| 919 |
!CBB_add_asn1_bool(&child, true)) { |
| 920 |
return 0; |
| 921 |
} |
| 922 |
} |
| 923 |
if (in->jdk11_workaround) { |
| 924 |
if (!CBB_add_asn1(&config, &child, kSSLConfigJdk11WorkaroundTag) || |
| 925 |
!CBB_add_asn1_bool(&child, true)) { |
| 926 |
return 0; |
| 927 |
} |
| 928 |
} |
| 929 |
if (in->conf_max_version_use_default) { |
| 930 |
if (!CBB_add_asn1(&config, &child, kSSLConfigConfMaxVersionUseDefault) || |
| 931 |
!CBB_add_asn1_bool(&child, true)) { |
| 932 |
return 0; |
| 933 |
} |
| 934 |
} |
| 935 |
if (in->conf_min_version_use_default) { |
| 936 |
if (!CBB_add_asn1(&config, &child, kSSLConfigConfMinVersionUseDefault) || |
| 937 |
!CBB_add_asn1_bool(&child, true)) { |
| 938 |
return 0; |
| 939 |
} |
| 940 |
} |
| 941 |
return CBB_flush(cbb); |
| 942 |
} |
| 943 |
|
| 944 |
static int SSL_CONFIG_from_bytes(SSL_CONFIG *out, CBS *cbs) { |
| 945 |
CBS config; |
| 946 |
int ocsp_stapling_enabled, jdk11_workaround; |
| 947 |
int conf_max_version_use_default, conf_min_version_use_default; |
| 948 |
uint64_t version, conf_max_version, conf_min_version; |
| 949 |
if (!CBS_get_asn1(cbs, &config, CBS_ASN1_SEQUENCE) || |
| 950 |
!CBS_get_asn1_uint64(&config, &version) || version > kSSLConfigVersion || |
| 951 |
!CBS_get_asn1_uint64(&config, &conf_max_version) || |
| 952 |
!CBS_get_asn1_uint64(&config, &conf_min_version) || |
| 953 |
!CBS_get_optional_asn1_bool(&config, &ocsp_stapling_enabled, |
| 954 |
kSSLConfigOcspStaplingEnabledTag, |
| 955 |
0 /* default to false */) || |
| 956 |
!CBS_get_optional_asn1_bool(&config, &jdk11_workaround, |
| 957 |
kSSLConfigJdk11WorkaroundTag, |
| 958 |
0 /* default to false */)) { |
| 959 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_CONFIG); |
| 960 |
return 0; |
| 961 |
} |
| 962 |
|
| 963 |
bool is_atleast_v2 = (version >= 2); |
| 964 |
|
| 965 |
if (!is_atleast_v2 && CBS_len(&config) != 0) { |
| 966 |
// We are parsing version 1 of config format, but there is more data to |
| 967 |
// parse than expected. That is, the input is not v1 formatted. |
| 968 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_CONFIG); |
| 969 |
return 0; |
| 970 |
} |
| 971 |
|
| 972 |
if (!CBS_get_optional_asn1_bool(&config, &conf_max_version_use_default, |
| 973 |
kSSLConfigConfMaxVersionUseDefault, |
| 974 |
1 /* default to true */) || |
| 975 |
!CBS_get_optional_asn1_bool(&config, &conf_min_version_use_default, |
| 976 |
kSSLConfigConfMinVersionUseDefault, |
| 977 |
1 /* default to true */) || |
| 978 |
CBS_len(&config) != 0) { |
| 979 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_CONFIG); |
| 980 |
return 0; |
| 981 |
} |
| 982 |
|
| 983 |
out->conf_max_version = conf_max_version; |
| 984 |
out->conf_min_version = conf_min_version; |
| 985 |
out->conf_max_version_use_default = !!conf_max_version_use_default; |
| 986 |
out->conf_min_version_use_default = !!conf_min_version_use_default; |
| 987 |
out->ocsp_stapling_enabled = !!ocsp_stapling_enabled; |
| 988 |
out->jdk11_workaround = !!jdk11_workaround; |
| 989 |
// handoff will always be the normal state(false) after handshake completes. |
| 990 |
out->handoff = false; |
| 991 |
// shed_handshake_config will always be false if config can be encoded(not |
| 992 |
// sheded). |
| 993 |
out->shed_handshake_config = false; |
| 994 |
return 1; |
| 995 |
} |
| 996 |
|
| 997 |
// SSL serialization. |
| 998 |
|
| 999 |
// Serialized SSL data version for forward compatibility |
| 1000 |
#define SSL_SERIAL_VERSION 1 |
| 1001 |
|
| 1002 |
static const unsigned kSSLQuietShutdownTag = |
| 1003 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0; |
| 1004 |
static const unsigned kSSLConfigTag = |
| 1005 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
| 1006 |
static const unsigned kSSLVerifyResultTag = |
| 1007 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2; |
| 1008 |
|
| 1009 |
// Parse serialized SSL connection binary |
| 1010 |
// |
| 1011 |
// SSL_to_bytes_full serializes |in| to bytes stored in |cbb|. |
| 1012 |
// It returns one on success and zero on failure. |
| 1013 |
// |
| 1014 |
// SSL ASN.1 structure: see ssl_transfer_asn1.cc |
| 1015 |
static int SSL_to_bytes_full(const SSL *in, CBB *cbb) { |
| 1016 |
CBB ssl, child; |
| 1017 |
|
| 1018 |
if (!CBB_add_asn1(cbb, &ssl, CBS_ASN1_SEQUENCE) || |
| 1019 |
!CBB_add_asn1_uint64(&ssl, SSL_SERIAL_VERSION) || |
| 1020 |
// FIXME add hash of SSL_CTX |
| 1021 |
!CBB_add_asn1_uint64(&ssl, in->version) || |
| 1022 |
!CBB_add_asn1_uint64(&ssl, in->max_send_fragment) || |
| 1023 |
!SSL3_STATE_to_bytes(in->s3, ssl_protocol_version(in), &ssl) || |
| 1024 |
!CBB_add_asn1_uint64(&ssl, in->mode) || |
| 1025 |
!CBB_add_asn1_uint64(&ssl, in->options)) { |
| 1026 |
return 0; |
| 1027 |
} |
| 1028 |
|
| 1029 |
if (in->quiet_shutdown) { |
| 1030 |
if (!CBB_add_asn1(&ssl, &child, kSSLQuietShutdownTag) || |
| 1031 |
!CBB_add_asn1_bool(&child, true)) { |
| 1032 |
return 0; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
if (in->config) { |
| 1037 |
if (!CBB_add_asn1(&ssl, &child, kSSLConfigTag) || |
| 1038 |
!SSL_CONFIG_to_bytes(in->config.get(), &child)) { |
| 1039 |
return 0; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
if (!CBB_add_asn1(&ssl, &child, kSSLVerifyResultTag) || |
| 1044 |
!CBB_add_asn1_int64(&child, in->verify_result)) { |
| 1045 |
return 0; |
| 1046 |
} |
| 1047 |
|
| 1048 |
return CBB_flush(cbb); |
| 1049 |
} |
| 1050 |
|
| 1051 |
static int SSL_parse(SSL *ssl, CBS *cbs, SSL_CTX *ctx) { |
| 1052 |
CBS ssl_cbs, ssl_config; |
| 1053 |
uint64_t ssl_serial_ver, version, max_send_fragment, mode, options; |
| 1054 |
int64_t verify_result = X509_V_ERR_INVALID_CALL; |
| 1055 |
int quiet_shutdown; |
| 1056 |
int ssl_config_present = 0; |
| 1057 |
|
| 1058 |
if (!CBS_get_asn1(cbs, &ssl_cbs, CBS_ASN1_SEQUENCE) || CBS_len(cbs) != 0) { |
| 1059 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL); |
| 1060 |
return 0; |
| 1061 |
} |
| 1062 |
|
| 1063 |
if (!CBS_get_asn1_uint64(&ssl_cbs, &ssl_serial_ver) || |
| 1064 |
ssl_serial_ver > SSL_SERIAL_VERSION) { |
| 1065 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SERDE_VERSION); |
| 1066 |
return 0; |
| 1067 |
} |
| 1068 |
|
| 1069 |
// FIXME add hash of SSL_CTX |
| 1070 |
// This TODO is actually a part of SSL DER struct revisit. |
| 1071 |
if (!CBS_get_asn1_uint64(&ssl_cbs, &version) || |
| 1072 |
!CBS_get_asn1_uint64(&ssl_cbs, &max_send_fragment)) { |
| 1073 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL); |
| 1074 |
return 0; |
| 1075 |
} |
| 1076 |
|
| 1077 |
ssl->version = version; |
| 1078 |
ssl->max_send_fragment = max_send_fragment; |
| 1079 |
|
| 1080 |
SSL_set_accept_state(ssl); |
| 1081 |
|
| 1082 |
// This is called separately to avoid overriding error code. |
| 1083 |
// This function expects ssl->version to have been set, otherwise will error! |
| 1084 |
if (!SSL3_STATE_from_bytes(ssl, &ssl_cbs, ssl->ctx.get())) { |
| 1085 |
return 0; |
| 1086 |
} |
| 1087 |
|
| 1088 |
if (!CBS_get_asn1_uint64(&ssl_cbs, &mode) || |
| 1089 |
!CBS_get_asn1_uint64(&ssl_cbs, &options)) { |
| 1090 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL); |
| 1091 |
return 0; |
| 1092 |
} |
| 1093 |
ssl->mode = mode; |
| 1094 |
ssl->options = options; |
| 1095 |
|
| 1096 |
if (!CBS_get_optional_asn1_bool(&ssl_cbs, &quiet_shutdown, |
| 1097 |
kSSLQuietShutdownTag, |
| 1098 |
0 /* default to false */)) { |
| 1099 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL); |
| 1100 |
return 0; |
| 1101 |
} |
| 1102 |
ssl->quiet_shutdown = !!quiet_shutdown; |
| 1103 |
|
| 1104 |
if (!CBS_get_optional_asn1(&ssl_cbs, &ssl_config, &ssl_config_present, |
| 1105 |
kSSLConfigTag)) { |
| 1106 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL); |
| 1107 |
return 0; |
| 1108 |
} |
| 1109 |
|
| 1110 |
if (ssl_config_present) { |
| 1111 |
if (!SSL_CONFIG_from_bytes(ssl->config.get(), &ssl_config)) { |
| 1112 |
return 0; |
| 1113 |
} |
| 1114 |
} else { |
| 1115 |
ssl->config.reset(); |
| 1116 |
} |
| 1117 |
|
| 1118 |
if (!CBS_get_optional_asn1_int64( |
| 1119 |
&ssl_cbs, &verify_result, kSSLVerifyResultTag, |
| 1120 |
ssl->s3->established_session->verify_result)) { |
| 1121 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL); |
| 1122 |
return 0; |
| 1123 |
} |
| 1124 |
|
| 1125 |
if (CBS_len(&ssl_cbs) != 0) { |
| 1126 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL); |
| 1127 |
return 0; |
| 1128 |
} |
| 1129 |
|
| 1130 |
ssl->verify_result = verify_result; |
| 1131 |
ssl->is_suspended_state = false; |
| 1132 |
|
| 1133 |
return 1; |
| 1134 |
} |
| 1135 |
|
| 1136 |
SSL *SSL_from_bytes(const uint8_t *in, size_t in_len, SSL_CTX *ctx) { |
| 1137 |
if (!in || !in_len || !ctx) { |
| 1138 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_UNSUPPORTED); |
| 1139 |
return NULL; |
| 1140 |
} |
| 1141 |
|
| 1142 |
SSL *ssl = SSL_new(ctx); |
| 1143 |
if (ssl == NULL) { |
| 1144 |
return NULL; |
| 1145 |
} |
| 1146 |
UniquePtr<SSL> ret(ssl); |
| 1147 |
|
| 1148 |
CBS cbs, seq; |
| 1149 |
CBS_init(&cbs, in, in_len); |
| 1150 |
if (!CBS_get_asn1(&cbs, &seq, CBS_ASN1_SEQUENCE) || (CBS_len(&cbs) != 0)) { |
| 1151 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL); |
| 1152 |
return NULL; |
| 1153 |
} |
| 1154 |
|
| 1155 |
// Restore SSL part |
| 1156 |
if (!SSL_parse(ret.get(), &seq, ctx)) { |
| 1157 |
return NULL; |
| 1158 |
} |
| 1159 |
|
| 1160 |
return ret.release(); |
| 1161 |
} |
| 1162 |
|
| 1163 |
int SSL_to_bytes(const SSL *in, uint8_t **out_data, size_t *out_len) { |
| 1164 |
if (!ssl_transfer_supported(in)) { |
| 1165 |
return 0; |
| 1166 |
} |
| 1167 |
|
| 1168 |
ScopedCBB cbb; |
| 1169 |
|
| 1170 |
CBB seq; |
| 1171 |
if (!CBB_init(cbb.get(), 1024) || |
| 1172 |
!CBB_add_asn1(cbb.get(), &seq, CBS_ASN1_SEQUENCE) || |
| 1173 |
!SSL_to_bytes_full(in, &seq)) { |
| 1174 |
return 0; |
| 1175 |
} |
| 1176 |
|
| 1177 |
const_cast<SSL *>(in)->is_suspended_state = true; |
| 1178 |
|
| 1179 |
return CBB_finish(cbb.get(), out_data, out_len); |
| 1180 |
} |