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