| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright 2005 Nokia. All rights reserved. |
| 3 |
// |
| 4 |
// The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 5 |
// Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 6 |
// support (see RFC 4279) to OpenSSL. |
| 7 |
// |
| 8 |
// SPDX-License-Identifier: Apache-2.0 |
| 9 |
|
| 10 |
#include <openssl/ssl.h> |
| 11 |
|
| 12 |
#include <limits.h> |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
#include <utility> |
| 16 |
|
| 17 |
#include <openssl/bytestring.h> |
| 18 |
#include <openssl/err.h> |
| 19 |
#include <openssl/mem.h> |
| 20 |
#include <openssl/x509.h> |
| 21 |
|
| 22 |
#include "../crypto/bytestring/internal.h" |
| 23 |
#include "../crypto/internal.h" |
| 24 |
#include "internal.h" |
| 25 |
|
| 26 |
|
| 27 |
BSSL_NAMESPACE_BEGIN |
| 28 |
|
| 29 |
// An SSL_SESSION is serialized as the following ASN.1 structure: |
| 30 |
// |
| 31 |
// SSLSession ::= SEQUENCE { |
| 32 |
// version INTEGER (1), -- session structure version |
| 33 |
// sslVersion INTEGER, -- protocol version number |
| 34 |
// cipher OCTET STRING, -- two bytes long |
| 35 |
// sessionID OCTET STRING, |
| 36 |
// secret OCTET STRING, |
| 37 |
// time [1] INTEGER, -- seconds since UNIX epoch |
| 38 |
// timeout [2] INTEGER, -- in seconds |
| 39 |
// peer [3] Certificate OPTIONAL, |
| 40 |
// sessionIDContext [4] OCTET STRING OPTIONAL, |
| 41 |
// verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes |
| 42 |
// pskIdentity [8] OCTET STRING OPTIONAL, |
| 43 |
// ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only |
| 44 |
// ticket [10] OCTET STRING OPTIONAL, -- client-only |
| 45 |
// peerSHA256 [13] OCTET STRING OPTIONAL, |
| 46 |
// originalHandshakeHash [14] OCTET STRING OPTIONAL, |
| 47 |
// signedCertTimestampList [15] OCTET STRING OPTIONAL, |
| 48 |
// -- contents of SCT extension |
| 49 |
// ocspResponse [16] OCTET STRING OPTIONAL, |
| 50 |
// -- stapled OCSP response from the server |
| 51 |
// extendedMasterSecret [17] BOOLEAN OPTIONAL, |
| 52 |
// groupID [18] INTEGER OPTIONAL, |
| 53 |
// certChain [19] SEQUENCE OF Certificate OPTIONAL, |
| 54 |
// ticketAgeAdd [21] OCTET STRING OPTIONAL, |
| 55 |
// isServer [22] BOOLEAN DEFAULT TRUE, |
| 56 |
// peerSignatureAlgorithm [23] INTEGER OPTIONAL, |
| 57 |
// ticketMaxEarlyData [24] INTEGER OPTIONAL, |
| 58 |
// authTimeout [25] INTEGER OPTIONAL, -- defaults to timeout |
| 59 |
// earlyALPN [26] OCTET STRING OPTIONAL, |
| 60 |
// isQuic [27] BOOLEAN OPTIONAL, |
| 61 |
// quicEarlyDataHash [28] OCTET STRING OPTIONAL, |
| 62 |
// localALPS [29] OCTET STRING OPTIONAL, |
| 63 |
// peerALPS [30] OCTET STRING OPTIONAL, |
| 64 |
// -- Either both or none of localALPS and peerALPS must be present. If both |
| 65 |
// -- are present, earlyALPN must be present and non-empty. |
| 66 |
// } |
| 67 |
// |
| 68 |
// Note: historically this serialization has included other optional |
| 69 |
// fields. Their presence is currently treated as a parse error, except for |
| 70 |
// hostName, which is ignored. |
| 71 |
// |
| 72 |
// keyArg [0] IMPLICIT OCTET STRING OPTIONAL, |
| 73 |
// hostName [6] OCTET STRING OPTIONAL, |
| 74 |
// pskIdentityHint [7] OCTET STRING OPTIONAL, |
| 75 |
// compressionMethod [11] OCTET STRING OPTIONAL, |
| 76 |
// srpUsername [12] OCTET STRING OPTIONAL, |
| 77 |
// ticketFlags [20] INTEGER OPTIONAL, |
| 78 |
|
| 79 |
static const unsigned kVersion = 1; |
| 80 |
|
| 81 |
static const CBS_ASN1_TAG kTimeTag = |
| 82 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
| 83 |
static const CBS_ASN1_TAG kTimeoutTag = |
| 84 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2; |
| 85 |
static const CBS_ASN1_TAG kPeerTag = |
| 86 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3; |
| 87 |
static const CBS_ASN1_TAG kSessionIDContextTag = |
| 88 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4; |
| 89 |
static const CBS_ASN1_TAG kVerifyResultTag = |
| 90 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5; |
| 91 |
static const CBS_ASN1_TAG kHostNameTag = |
| 92 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6; |
| 93 |
static const CBS_ASN1_TAG kPSKIdentityTag = |
| 94 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8; |
| 95 |
static const CBS_ASN1_TAG kTicketLifetimeHintTag = |
| 96 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9; |
| 97 |
static const CBS_ASN1_TAG kTicketTag = |
| 98 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10; |
| 99 |
static const CBS_ASN1_TAG kPeerSHA256Tag = |
| 100 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13; |
| 101 |
static const CBS_ASN1_TAG kOriginalHandshakeHashTag = |
| 102 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14; |
| 103 |
static const CBS_ASN1_TAG kSignedCertTimestampListTag = |
| 104 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15; |
| 105 |
static const CBS_ASN1_TAG kOCSPResponseTag = |
| 106 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16; |
| 107 |
static const CBS_ASN1_TAG kExtendedMasterSecretTag = |
| 108 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17; |
| 109 |
static const CBS_ASN1_TAG kGroupIDTag = |
| 110 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18; |
| 111 |
static const CBS_ASN1_TAG kCertChainTag = |
| 112 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19; |
| 113 |
static const CBS_ASN1_TAG kTicketAgeAddTag = |
| 114 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21; |
| 115 |
static const CBS_ASN1_TAG kIsServerTag = |
| 116 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22; |
| 117 |
static const CBS_ASN1_TAG kPeerSignatureAlgorithmTag = |
| 118 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 23; |
| 119 |
static const CBS_ASN1_TAG kTicketMaxEarlyDataTag = |
| 120 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 24; |
| 121 |
static const CBS_ASN1_TAG kAuthTimeoutTag = |
| 122 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 25; |
| 123 |
static const CBS_ASN1_TAG kEarlyALPNTag = |
| 124 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 26; |
| 125 |
static const CBS_ASN1_TAG kIsQuicTag = |
| 126 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 27; |
| 127 |
static const CBS_ASN1_TAG kQuicEarlyDataContextTag = |
| 128 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 28; |
| 129 |
static const CBS_ASN1_TAG kLocalALPSTag = |
| 130 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 29; |
| 131 |
static const CBS_ASN1_TAG kPeerALPSTag = |
| 132 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 30; |
| 133 |
|
| 134 |
static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, CBB *cbb, |
| 135 |
int for_ticket) { |
| 136 |
if (in == NULL || in->cipher == NULL) { |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
CBB session, child, child2; |
| 141 |
if (!CBB_add_asn1(cbb, &session, CBS_ASN1_SEQUENCE) || |
| 142 |
!CBB_add_asn1_uint64(&session, kVersion) || |
| 143 |
!CBB_add_asn1_uint64(&session, in->ssl_version) || |
| 144 |
!CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
| 145 |
!CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) || |
| 146 |
// The session ID is irrelevant for a session ticket. |
| 147 |
!CBB_add_asn1_octet_string(&session, in->session_id, |
| 148 |
for_ticket ? 0 : in->session_id_length) || |
| 149 |
!CBB_add_asn1_octet_string(&session, in->secret, in->secret_length) || |
| 150 |
!CBB_add_asn1(&session, &child, kTimeTag) || |
| 151 |
!CBB_add_asn1_uint64(&child, in->time) || |
| 152 |
!CBB_add_asn1(&session, &child, kTimeoutTag) || |
| 153 |
!CBB_add_asn1_uint64(&child, in->timeout)) { |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
// The peer certificate is only serialized if the SHA-256 isn't |
| 158 |
// serialized instead. |
| 159 |
if (sk_CRYPTO_BUFFER_num(in->certs.get()) > 0 && !in->peer_sha256_valid) { |
| 160 |
const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs.get(), 0); |
| 161 |
if (!CBB_add_asn1(&session, &child, kPeerTag) || |
| 162 |
!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 163 |
CRYPTO_BUFFER_len(buffer))) { |
| 164 |
return 0; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// Although it is OPTIONAL and usually empty, OpenSSL has |
| 169 |
// historically always encoded the sid_ctx. |
| 170 |
if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) || |
| 171 |
!CBB_add_asn1_octet_string(&child, in->sid_ctx, in->sid_ctx_length)) { |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
if (in->verify_result != X509_V_OK) { |
| 176 |
if (!CBB_add_asn1(&session, &child, kVerifyResultTag) || |
| 177 |
!CBB_add_asn1_uint64(&child, in->verify_result)) { |
| 178 |
return 0; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if (in->psk_identity) { |
| 183 |
if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) || |
| 184 |
!CBB_add_asn1_octet_string(&child, |
| 185 |
(const uint8_t *)in->psk_identity.get(), |
| 186 |
strlen(in->psk_identity.get()))) { |
| 187 |
return 0; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
if (in->ticket_lifetime_hint > 0) { |
| 192 |
if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) || |
| 193 |
!CBB_add_asn1_uint64(&child, in->ticket_lifetime_hint)) { |
| 194 |
return 0; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if (!in->ticket.empty() && !for_ticket) { |
| 199 |
if (!CBB_add_asn1(&session, &child, kTicketTag) || |
| 200 |
!CBB_add_asn1_octet_string(&child, in->ticket.data(), |
| 201 |
in->ticket.size())) { |
| 202 |
return 0; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if (in->peer_sha256_valid) { |
| 207 |
if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) || |
| 208 |
!CBB_add_asn1_octet_string(&child, in->peer_sha256, |
| 209 |
sizeof(in->peer_sha256))) { |
| 210 |
return 0; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if (in->original_handshake_hash_len > 0) { |
| 215 |
if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) || |
| 216 |
!CBB_add_asn1_octet_string(&child, in->original_handshake_hash, |
| 217 |
in->original_handshake_hash_len)) { |
| 218 |
return 0; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (in->signed_cert_timestamp_list != nullptr) { |
| 223 |
if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) || |
| 224 |
!CBB_add_asn1_octet_string( |
| 225 |
&child, CRYPTO_BUFFER_data(in->signed_cert_timestamp_list.get()), |
| 226 |
CRYPTO_BUFFER_len(in->signed_cert_timestamp_list.get()))) { |
| 227 |
return 0; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
if (in->ocsp_response != nullptr) { |
| 232 |
if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) || |
| 233 |
!CBB_add_asn1_octet_string( |
| 234 |
&child, CRYPTO_BUFFER_data(in->ocsp_response.get()), |
| 235 |
CRYPTO_BUFFER_len(in->ocsp_response.get()))) { |
| 236 |
return 0; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if (in->extended_master_secret) { |
| 241 |
if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) || |
| 242 |
!CBB_add_asn1_bool(&child, true)) { |
| 243 |
return 0; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if (in->group_id > 0 && |
| 248 |
(!CBB_add_asn1(&session, &child, kGroupIDTag) || |
| 249 |
!CBB_add_asn1_uint64(&child, in->group_id))) { |
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
// The certificate chain is only serialized if the leaf's SHA-256 isn't |
| 254 |
// serialized instead. |
| 255 |
if (in->certs != NULL && |
| 256 |
!in->peer_sha256_valid && |
| 257 |
sk_CRYPTO_BUFFER_num(in->certs.get()) >= 2) { |
| 258 |
if (!CBB_add_asn1(&session, &child, kCertChainTag)) { |
| 259 |
return 0; |
| 260 |
} |
| 261 |
for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs.get()); i++) { |
| 262 |
const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs.get(), i); |
| 263 |
if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 264 |
CRYPTO_BUFFER_len(buffer))) { |
| 265 |
return 0; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
if (in->ticket_age_add_valid) { |
| 271 |
if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) || |
| 272 |
!CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 273 |
!CBB_add_u32(&child2, in->ticket_age_add)) { |
| 274 |
return 0; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if (!in->is_server) { |
| 279 |
if (!CBB_add_asn1(&session, &child, kIsServerTag) || |
| 280 |
!CBB_add_asn1_bool(&child, false)) { |
| 281 |
return 0; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
if (in->peer_signature_algorithm != 0 && |
| 286 |
(!CBB_add_asn1(&session, &child, kPeerSignatureAlgorithmTag) || |
| 287 |
!CBB_add_asn1_uint64(&child, in->peer_signature_algorithm))) { |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
if (in->ticket_max_early_data != 0 && |
| 292 |
(!CBB_add_asn1(&session, &child, kTicketMaxEarlyDataTag) || |
| 293 |
!CBB_add_asn1_uint64(&child, in->ticket_max_early_data))) { |
| 294 |
return 0; |
| 295 |
} |
| 296 |
|
| 297 |
if (in->timeout != in->auth_timeout && |
| 298 |
(!CBB_add_asn1(&session, &child, kAuthTimeoutTag) || |
| 299 |
!CBB_add_asn1_uint64(&child, in->auth_timeout))) { |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
if (!in->early_alpn.empty()) { |
| 304 |
if (!CBB_add_asn1(&session, &child, kEarlyALPNTag) || |
| 305 |
!CBB_add_asn1_octet_string(&child, in->early_alpn.data(), |
| 306 |
in->early_alpn.size())) { |
| 307 |
return 0; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
if (in->is_quic) { |
| 312 |
if (!CBB_add_asn1(&session, &child, kIsQuicTag) || |
| 313 |
!CBB_add_asn1_bool(&child, true)) { |
| 314 |
return 0; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
if (!in->quic_early_data_context.empty()) { |
| 319 |
if (!CBB_add_asn1(&session, &child, kQuicEarlyDataContextTag) || |
| 320 |
!CBB_add_asn1_octet_string(&child, in->quic_early_data_context.data(), |
| 321 |
in->quic_early_data_context.size())) { |
| 322 |
return 0; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
if (in->has_application_settings) { |
| 327 |
if (!CBB_add_asn1(&session, &child, kLocalALPSTag) || |
| 328 |
!CBB_add_asn1_octet_string(&child, |
| 329 |
in->local_application_settings.data(), |
| 330 |
in->local_application_settings.size()) || |
| 331 |
!CBB_add_asn1(&session, &child, kPeerALPSTag) || |
| 332 |
!CBB_add_asn1_octet_string(&child, in->peer_application_settings.data(), |
| 333 |
in->peer_application_settings.size())) { |
| 334 |
return 0; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
return CBB_flush(cbb); |
| 339 |
} |
| 340 |
|
| 341 |
// SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING explicitly |
| 342 |
// tagged with |tag| from |cbs| and saves it in |*out|. If the element was not |
| 343 |
// found, it sets |*out| to NULL. It returns one on success, whether or not the |
| 344 |
// element was found, and zero on decode error. |
| 345 |
static int SSL_SESSION_parse_string(CBS *cbs, UniquePtr<char> *out, |
| 346 |
CBS_ASN1_TAG tag) { |
| 347 |
CBS value; |
| 348 |
int present; |
| 349 |
if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) { |
| 350 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 351 |
return 0; |
| 352 |
} |
| 353 |
if (present) { |
| 354 |
if (CBS_contains_zero_byte(&value)) { |
| 355 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 356 |
return 0; |
| 357 |
} |
| 358 |
char *raw = nullptr; |
| 359 |
if (!CBS_strdup(&value, &raw)) { |
| 360 |
return 0; |
| 361 |
} |
| 362 |
out->reset(raw); |
| 363 |
} else { |
| 364 |
out->reset(); |
| 365 |
} |
| 366 |
return 1; |
| 367 |
} |
| 368 |
|
| 369 |
// SSL_SESSION_parse_octet_string gets an optional ASN.1 OCTET STRING explicitly |
| 370 |
// tagged with |tag| from |cbs| and stows it in |*out|. It returns one on |
| 371 |
// success, whether or not the element was found, and zero on decode error. |
| 372 |
static bool SSL_SESSION_parse_octet_string(CBS *cbs, Array<uint8_t> *out, |
| 373 |
CBS_ASN1_TAG tag) { |
| 374 |
CBS value; |
| 375 |
if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) { |
| 376 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 377 |
return false; |
| 378 |
} |
| 379 |
return out->CopyFrom(value); |
| 380 |
} |
| 381 |
|
| 382 |
static int SSL_SESSION_parse_crypto_buffer(CBS *cbs, |
| 383 |
UniquePtr<CRYPTO_BUFFER> *out, |
| 384 |
CBS_ASN1_TAG tag, |
| 385 |
CRYPTO_BUFFER_POOL *pool) { |
| 386 |
if (!CBS_peek_asn1_tag(cbs, tag)) { |
| 387 |
return 1; |
| 388 |
} |
| 389 |
|
| 390 |
CBS child, value; |
| 391 |
if (!CBS_get_asn1(cbs, &child, tag) || |
| 392 |
!CBS_get_asn1(&child, &value, CBS_ASN1_OCTETSTRING) || |
| 393 |
CBS_len(&child) != 0) { |
| 394 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 395 |
return 0; |
| 396 |
} |
| 397 |
out->reset(CRYPTO_BUFFER_new_from_CBS(&value, pool)); |
| 398 |
if (*out == nullptr) { |
| 399 |
return 0; |
| 400 |
} |
| 401 |
return 1; |
| 402 |
} |
| 403 |
|
| 404 |
// SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING |
| 405 |
// explicitly tagged with |tag| of size at most |max_out|. |
| 406 |
static int SSL_SESSION_parse_bounded_octet_string(CBS *cbs, uint8_t *out, |
| 407 |
uint8_t *out_len, |
| 408 |
uint8_t max_out, |
| 409 |
CBS_ASN1_TAG tag) { |
| 410 |
CBS value; |
| 411 |
if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) || |
| 412 |
CBS_len(&value) > max_out) { |
| 413 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 414 |
return 0; |
| 415 |
} |
| 416 |
OPENSSL_memcpy(out, CBS_data(&value), CBS_len(&value)); |
| 417 |
*out_len = static_cast<uint8_t>(CBS_len(&value)); |
| 418 |
return 1; |
| 419 |
} |
| 420 |
|
| 421 |
static int SSL_SESSION_parse_long(CBS *cbs, long *out, CBS_ASN1_TAG tag, |
| 422 |
long default_value) { |
| 423 |
uint64_t value; |
| 424 |
if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 425 |
(uint64_t)default_value) || |
| 426 |
value > LONG_MAX) { |
| 427 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 428 |
return 0; |
| 429 |
} |
| 430 |
*out = (long)value; |
| 431 |
return 1; |
| 432 |
} |
| 433 |
|
| 434 |
static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, CBS_ASN1_TAG tag, |
| 435 |
uint32_t default_value) { |
| 436 |
uint64_t value; |
| 437 |
if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 438 |
(uint64_t)default_value) || |
| 439 |
value > 0xffffffff) { |
| 440 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 441 |
return 0; |
| 442 |
} |
| 443 |
*out = (uint32_t)value; |
| 444 |
return 1; |
| 445 |
} |
| 446 |
|
| 447 |
static int SSL_SESSION_parse_u16(CBS *cbs, uint16_t *out, CBS_ASN1_TAG tag, |
| 448 |
uint16_t default_value) { |
| 449 |
uint64_t value; |
| 450 |
if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 451 |
(uint64_t)default_value) || |
| 452 |
value > 0xffff) { |
| 453 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 454 |
return 0; |
| 455 |
} |
| 456 |
*out = (uint16_t)value; |
| 457 |
return 1; |
| 458 |
} |
| 459 |
|
| 460 |
UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs, |
| 461 |
const SSL_X509_METHOD *x509_method, |
| 462 |
CRYPTO_BUFFER_POOL *pool) { |
| 463 |
UniquePtr<SSL_SESSION> ret = ssl_session_new(x509_method); |
| 464 |
if (!ret) { |
| 465 |
return nullptr; |
| 466 |
} |
| 467 |
|
| 468 |
CBS session; |
| 469 |
uint64_t version, ssl_version; |
| 470 |
uint16_t unused; |
| 471 |
if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) || |
| 472 |
!CBS_get_asn1_uint64(&session, &version) || |
| 473 |
version != kVersion || |
| 474 |
!CBS_get_asn1_uint64(&session, &ssl_version) || |
| 475 |
// Require sessions have versions valid in either TLS or DTLS. The session |
| 476 |
// will not be used by the handshake if not applicable, but, for |
| 477 |
// simplicity, never parse a session that does not pass |
| 478 |
// |ssl_protocol_version_from_wire|. |
| 479 |
ssl_version > UINT16_MAX || |
| 480 |
!ssl_protocol_version_from_wire(&unused, ssl_version)) { |
| 481 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 482 |
return nullptr; |
| 483 |
} |
| 484 |
ret->ssl_version = ssl_version; |
| 485 |
|
| 486 |
CBS cipher; |
| 487 |
uint16_t cipher_value; |
| 488 |
if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) || |
| 489 |
!CBS_get_u16(&cipher, &cipher_value) || |
| 490 |
CBS_len(&cipher) != 0) { |
| 491 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 492 |
return nullptr; |
| 493 |
} |
| 494 |
ret->cipher = SSL_get_cipher_by_value(cipher_value); |
| 495 |
if (ret->cipher == NULL) { |
| 496 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER); |
| 497 |
return nullptr; |
| 498 |
} |
| 499 |
|
| 500 |
CBS session_id, secret; |
| 501 |
if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) || |
| 502 |
CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH || |
| 503 |
!CBS_get_asn1(&session, &secret, CBS_ASN1_OCTETSTRING) || |
| 504 |
CBS_len(&secret) > SSL_MAX_MASTER_KEY_LENGTH) { |
| 505 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 506 |
return nullptr; |
| 507 |
} |
| 508 |
OPENSSL_memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id)); |
| 509 |
static_assert(SSL3_MAX_SSL_SESSION_ID_LENGTH <= UINT8_MAX, |
| 510 |
"max session ID is too large"); |
| 511 |
ret->session_id_length = static_cast<uint8_t>(CBS_len(&session_id)); |
| 512 |
OPENSSL_memcpy(ret->secret, CBS_data(&secret), CBS_len(&secret)); |
| 513 |
static_assert(SSL_MAX_MASTER_KEY_LENGTH <= UINT8_MAX, |
| 514 |
"max secret is too large"); |
| 515 |
ret->secret_length = static_cast<uint8_t>(CBS_len(&secret)); |
| 516 |
|
| 517 |
CBS child; |
| 518 |
uint64_t timeout; |
| 519 |
if (!CBS_get_asn1(&session, &child, kTimeTag) || |
| 520 |
!CBS_get_asn1_uint64(&child, &ret->time) || |
| 521 |
!CBS_get_asn1(&session, &child, kTimeoutTag) || |
| 522 |
!CBS_get_asn1_uint64(&child, &timeout) || |
| 523 |
timeout > UINT32_MAX) { |
| 524 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 525 |
return nullptr; |
| 526 |
} |
| 527 |
|
| 528 |
ret->timeout = (uint32_t)timeout; |
| 529 |
|
| 530 |
CBS peer; |
| 531 |
int has_peer; |
| 532 |
if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) || |
| 533 |
(has_peer && CBS_len(&peer) == 0)) { |
| 534 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 535 |
return nullptr; |
| 536 |
} |
| 537 |
// |peer| is processed with the certificate chain. |
| 538 |
|
| 539 |
if (!SSL_SESSION_parse_bounded_octet_string( |
| 540 |
&session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx), |
| 541 |
kSessionIDContextTag) || |
| 542 |
!SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag, |
| 543 |
X509_V_OK)) { |
| 544 |
return nullptr; |
| 545 |
} |
| 546 |
|
| 547 |
// Skip the historical hostName field. |
| 548 |
CBS unused_hostname; |
| 549 |
if (!CBS_get_optional_asn1(&session, &unused_hostname, nullptr, |
| 550 |
kHostNameTag)) { |
| 551 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 552 |
return nullptr; |
| 553 |
} |
| 554 |
|
| 555 |
if (!SSL_SESSION_parse_string(&session, &ret->psk_identity, |
| 556 |
kPSKIdentityTag) || |
| 557 |
!SSL_SESSION_parse_u32(&session, &ret->ticket_lifetime_hint, |
| 558 |
kTicketLifetimeHintTag, 0) || |
| 559 |
!SSL_SESSION_parse_octet_string(&session, &ret->ticket, kTicketTag)) { |
| 560 |
return nullptr; |
| 561 |
} |
| 562 |
|
| 563 |
if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) { |
| 564 |
CBS peer_sha256; |
| 565 |
if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) || |
| 566 |
!CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) || |
| 567 |
CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) || |
| 568 |
CBS_len(&child) != 0) { |
| 569 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 570 |
return nullptr; |
| 571 |
} |
| 572 |
OPENSSL_memcpy(ret->peer_sha256, CBS_data(&peer_sha256), |
| 573 |
sizeof(ret->peer_sha256)); |
| 574 |
ret->peer_sha256_valid = true; |
| 575 |
} else { |
| 576 |
ret->peer_sha256_valid = false; |
| 577 |
} |
| 578 |
|
| 579 |
if (!SSL_SESSION_parse_bounded_octet_string( |
| 580 |
&session, ret->original_handshake_hash, |
| 581 |
&ret->original_handshake_hash_len, |
| 582 |
sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) || |
| 583 |
!SSL_SESSION_parse_crypto_buffer(&session, |
| 584 |
&ret->signed_cert_timestamp_list, |
| 585 |
kSignedCertTimestampListTag, pool) || |
| 586 |
!SSL_SESSION_parse_crypto_buffer(&session, &ret->ocsp_response, |
| 587 |
kOCSPResponseTag, pool)) { |
| 588 |
return nullptr; |
| 589 |
} |
| 590 |
|
| 591 |
int extended_master_secret; |
| 592 |
if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret, |
| 593 |
kExtendedMasterSecretTag, |
| 594 |
0 /* default to false */)) { |
| 595 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 596 |
return nullptr; |
| 597 |
} |
| 598 |
ret->extended_master_secret = !!extended_master_secret; |
| 599 |
|
| 600 |
if (!SSL_SESSION_parse_u16(&session, &ret->group_id, kGroupIDTag, 0)) { |
| 601 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 602 |
return nullptr; |
| 603 |
} |
| 604 |
|
| 605 |
CBS cert_chain; |
| 606 |
CBS_init(&cert_chain, NULL, 0); |
| 607 |
int has_cert_chain; |
| 608 |
if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain, |
| 609 |
kCertChainTag) || |
| 610 |
(has_cert_chain && CBS_len(&cert_chain) == 0)) { |
| 611 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 612 |
return nullptr; |
| 613 |
} |
| 614 |
if (has_cert_chain && !has_peer) { |
| 615 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 616 |
return nullptr; |
| 617 |
} |
| 618 |
if (has_peer || has_cert_chain) { |
| 619 |
ret->certs.reset(sk_CRYPTO_BUFFER_new_null()); |
| 620 |
if (ret->certs == nullptr) { |
| 621 |
return nullptr; |
| 622 |
} |
| 623 |
|
| 624 |
if (has_peer) { |
| 625 |
UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new_from_CBS(&peer, pool)); |
| 626 |
if (!buffer || |
| 627 |
!PushToStack(ret->certs.get(), std::move(buffer))) { |
| 628 |
return nullptr; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
while (CBS_len(&cert_chain) > 0) { |
| 633 |
CBS cert; |
| 634 |
if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) || |
| 635 |
CBS_len(&cert) == 0) { |
| 636 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 637 |
return nullptr; |
| 638 |
} |
| 639 |
|
| 640 |
UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new_from_CBS(&cert, pool)); |
| 641 |
if (buffer == nullptr || |
| 642 |
!PushToStack(ret->certs.get(), std::move(buffer))) { |
| 643 |
return nullptr; |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
CBS age_add; |
| 649 |
int age_add_present; |
| 650 |
if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present, |
| 651 |
kTicketAgeAddTag) || |
| 652 |
(age_add_present && |
| 653 |
!CBS_get_u32(&age_add, &ret->ticket_age_add)) || |
| 654 |
CBS_len(&age_add) != 0) { |
| 655 |
return nullptr; |
| 656 |
} |
| 657 |
ret->ticket_age_add_valid = age_add_present != 0; |
| 658 |
|
| 659 |
int is_server; |
| 660 |
if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag, |
| 661 |
1 /* default to true */)) { |
| 662 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 663 |
return nullptr; |
| 664 |
} |
| 665 |
/* TODO: in time we can include |is_server| for servers too, then we can |
| 666 |
enforce that client and server sessions are never mixed up. */ |
| 667 |
|
| 668 |
ret->is_server = is_server; |
| 669 |
|
| 670 |
int is_quic; |
| 671 |
if (!SSL_SESSION_parse_u16(&session, &ret->peer_signature_algorithm, |
| 672 |
kPeerSignatureAlgorithmTag, 0) || |
| 673 |
!SSL_SESSION_parse_u32(&session, &ret->ticket_max_early_data, |
| 674 |
kTicketMaxEarlyDataTag, 0) || |
| 675 |
!SSL_SESSION_parse_u32(&session, &ret->auth_timeout, kAuthTimeoutTag, |
| 676 |
ret->timeout) || |
| 677 |
!SSL_SESSION_parse_octet_string(&session, &ret->early_alpn, |
| 678 |
kEarlyALPNTag) || |
| 679 |
!CBS_get_optional_asn1_bool(&session, &is_quic, kIsQuicTag, |
| 680 |
/*default_value=*/false) || |
| 681 |
!SSL_SESSION_parse_octet_string(&session, &ret->quic_early_data_context, |
| 682 |
kQuicEarlyDataContextTag)) { |
| 683 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 684 |
return nullptr; |
| 685 |
} |
| 686 |
|
| 687 |
CBS settings; |
| 688 |
int has_local_alps, has_peer_alps; |
| 689 |
if (!CBS_get_optional_asn1_octet_string(&session, &settings, &has_local_alps, |
| 690 |
kLocalALPSTag) || |
| 691 |
!ret->local_application_settings.CopyFrom(settings) || |
| 692 |
!CBS_get_optional_asn1_octet_string(&session, &settings, &has_peer_alps, |
| 693 |
kPeerALPSTag) || |
| 694 |
!ret->peer_application_settings.CopyFrom(settings) || |
| 695 |
CBS_len(&session) != 0) { |
| 696 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 697 |
return nullptr; |
| 698 |
} |
| 699 |
ret->is_quic = is_quic; |
| 700 |
|
| 701 |
// The two ALPS values and ALPN must be consistent. |
| 702 |
if (has_local_alps != has_peer_alps || |
| 703 |
(has_local_alps && ret->early_alpn.empty())) { |
| 704 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 705 |
return nullptr; |
| 706 |
} |
| 707 |
ret->has_application_settings = has_local_alps; |
| 708 |
|
| 709 |
if (!x509_method->session_cache_objects(ret.get())) { |
| 710 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 711 |
return nullptr; |
| 712 |
} |
| 713 |
|
| 714 |
return ret; |
| 715 |
} |
| 716 |
|
| 717 |
bool ssl_session_serialize(const SSL_SESSION *in, CBB *cbb) { |
| 718 |
return SSL_SESSION_to_bytes_full(in, cbb, 0); |
| 719 |
} |
| 720 |
|
| 721 |
BSSL_NAMESPACE_END |
| 722 |
|
| 723 |
using namespace bssl; |
| 724 |
|
| 725 |
int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data, |
| 726 |
size_t *out_len) { |
| 727 |
if (in->not_resumable) { |
| 728 |
// If the caller has an unresumable session, e.g. if |SSL_get_session| were |
| 729 |
// called on a TLS 1.3 or False Started connection, serialize with a |
| 730 |
// placeholder value so it is not accidentally deserialized into a resumable |
| 731 |
// one. |
| 732 |
static const char kNotResumableSession[] = "NOT RESUMABLE"; |
| 733 |
|
| 734 |
*out_len = strlen(kNotResumableSession); |
| 735 |
*out_data = (uint8_t *)OPENSSL_memdup(kNotResumableSession, *out_len); |
| 736 |
if (*out_data == NULL) { |
| 737 |
return 0; |
| 738 |
} |
| 739 |
|
| 740 |
return 1; |
| 741 |
} |
| 742 |
|
| 743 |
ScopedCBB cbb; |
| 744 |
if (!CBB_init(cbb.get(), 256) || |
| 745 |
!SSL_SESSION_to_bytes_full(in, cbb.get(), 0) || |
| 746 |
!CBB_finish(cbb.get(), out_data, out_len)) { |
| 747 |
return 0; |
| 748 |
} |
| 749 |
|
| 750 |
return 1; |
| 751 |
} |
| 752 |
|
| 753 |
int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data, |
| 754 |
size_t *out_len) { |
| 755 |
ScopedCBB cbb; |
| 756 |
if (!CBB_init(cbb.get(), 256) || |
| 757 |
!SSL_SESSION_to_bytes_full(in, cbb.get(), 1) || |
| 758 |
!CBB_finish(cbb.get(), out_data, out_len)) { |
| 759 |
return 0; |
| 760 |
} |
| 761 |
|
| 762 |
return 1; |
| 763 |
} |
| 764 |
|
| 765 |
int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) { |
| 766 |
uint8_t *out; |
| 767 |
size_t len; |
| 768 |
|
| 769 |
if (!SSL_SESSION_to_bytes(in, &out, &len)) { |
| 770 |
return -1; |
| 771 |
} |
| 772 |
bssl::UniquePtr<uint8_t> free_out(out); |
| 773 |
|
| 774 |
ScopedCBB cbb; |
| 775 |
if (!CBB_init(cbb.get(), 256) || !CBB_add_bytes(cbb.get(), out, len)) { |
| 776 |
return 0; |
| 777 |
} |
| 778 |
|
| 779 |
return CBB_finish_i2d(cbb.get(), pp); |
| 780 |
} |
| 781 |
|
| 782 |
SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len, |
| 783 |
const SSL_CTX *ctx) { |
| 784 |
CBS cbs; |
| 785 |
CBS_init(&cbs, in, in_len); |
| 786 |
UniquePtr<SSL_SESSION> ret = |
| 787 |
SSL_SESSION_parse(&cbs, ctx->x509_method, ctx->pool); |
| 788 |
if (!ret) { |
| 789 |
return NULL; |
| 790 |
} |
| 791 |
if (CBS_len(&cbs) != 0) { |
| 792 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 793 |
return NULL; |
| 794 |
} |
| 795 |
return ret.release(); |
| 796 |
} |