1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young (eay (at) cryptsoft.com). 6 * The implementation was written so as to conform with Netscapes SSL. 7 * 8 * This library is free for commercial and non-commercial use as long as 9 * the following conditions are aheared to. The following conditions 10 * apply to all code found in this distribution, be it the RC4, RSA, 11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * included with this distribution is covered by the same copyright terms 13 * except that the holder is Tim Hudson (tjh (at) cryptsoft.com). 14 * 15 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * the code are not to be removed. 17 * If this package is used in a product, Eric Young should be given attribution 18 * as the author of the parts of the library used. 19 * This can be in the form of a textual message at program startup or 20 * in documentation (online or textual) provided with the package. 21 * 22 * Redistribution and use in source and binary forms, with or without 23 * modification, are permitted provided that the following conditions 24 * are met: 25 * 1. Redistributions of source code must retain the copyright 26 * notice, this list of conditions and the following disclaimer. 27 * 2. Redistributions in binary form must reproduce the above copyright 28 * notice, this list of conditions and the following disclaimer in the 29 * documentation and/or other materials provided with the distribution. 30 * 3. All advertising materials mentioning features or use of this software 31 * must display the following acknowledgement: 32 * "This product includes cryptographic software written by 33 * Eric Young (eay (at) cryptsoft.com)" 34 * The word 'cryptographic' can be left out if the rouines from the library 35 * being used are not cryptographic related :-). 36 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * the apps directory (application code) you must include an acknowledgement: 38 * "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)" 39 * 40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * SUCH DAMAGE. 51 * 52 * The licence and distribution terms for any publically available version or 53 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * copied and put under another distribution licence 55 * [including the GNU Public Licence.] 56 */ 57 /* ==================================================================== 58 * Copyright 2005 Nokia. All rights reserved. 59 * 60 * The portions of the attached software ("Contribution") is developed by 61 * Nokia Corporation and is licensed pursuant to the OpenSSL open source 62 * license. 63 * 64 * The Contribution, originally written by Mika Kousa and Pasi Eronen of 65 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites 66 * support (see RFC 4279) to OpenSSL. 67 * 68 * No patent licenses or other rights except those expressly stated in 69 * the OpenSSL open source license shall be deemed granted or received 70 * expressly, by implication, estoppel, or otherwise. 71 * 72 * No assurances are provided by Nokia that the Contribution does not 73 * infringe the patent or other intellectual property rights of any third 74 * party or that the license provides you with all the necessary rights 75 * to make use of the Contribution. 76 * 77 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN 78 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA 79 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY 80 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR 81 * OTHERWISE. */ 82 83 #include <limits.h> 84 #include <string.h> 85 86 #include <openssl/bytestring.h> 87 #include <openssl/err.h> 88 #include <openssl/mem.h> 89 #include <openssl/x509.h> 90 91 #include "internal.h" 92 93 94 /* An SSL_SESSION is serialized as the following ASN.1 structure: 95 * 96 * SSLSession ::= SEQUENCE { 97 * version INTEGER (1), -- ignored 98 * sslVersion INTEGER, -- protocol version number 99 * cipher OCTET STRING, -- two bytes long 100 * sessionID OCTET STRING, 101 * masterKey OCTET STRING, 102 * time [1] INTEGER OPTIONAL, -- seconds since UNIX epoch 103 * timeout [2] INTEGER OPTIONAL, -- in seconds 104 * peer [3] Certificate OPTIONAL, 105 * sessionIDContext [4] OCTET STRING OPTIONAL, 106 * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes 107 * hostName [6] OCTET STRING OPTIONAL, 108 * -- from server_name extension 109 * pskIdentity [8] OCTET STRING OPTIONAL, 110 * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only 111 * ticket [10] OCTET STRING OPTIONAL, -- client-only 112 * peerSHA256 [13] OCTET STRING OPTIONAL, 113 * originalHandshakeHash [14] OCTET STRING OPTIONAL, 114 * signedCertTimestampList [15] OCTET STRING OPTIONAL, 115 * -- contents of SCT extension 116 * ocspResponse [16] OCTET STRING OPTIONAL, 117 * -- stapled OCSP response from the server 118 * extendedMasterSecret [17] BOOLEAN OPTIONAL, 119 * } 120 * 121 * Note: historically this serialization has included other optional 122 * fields. Their presense is currently treated as a parse error: 123 * 124 * keyArg [0] IMPLICIT OCTET STRING OPTIONAL, 125 * pskIdentityHint [7] OCTET STRING OPTIONAL, 126 * compressionMethod [11] OCTET STRING OPTIONAL, 127 * srpUsername [12] OCTET STRING OPTIONAL, */ 128 129 static const int kTimeTag = 130 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; 131 static const int kTimeoutTag = 132 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2; 133 static const int kPeerTag = 134 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3; 135 static const int kSessionIDContextTag = 136 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4; 137 static const int kVerifyResultTag = 138 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5; 139 static const int kHostNameTag = 140 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6; 141 static const int kPSKIdentityTag = 142 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8; 143 static const int kTicketLifetimeHintTag = 144 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9; 145 static const int kTicketTag = 146 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10; 147 static const int kPeerSHA256Tag = 148 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13; 149 static const int kOriginalHandshakeHashTag = 150 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14; 151 static const int kSignedCertTimestampListTag = 152 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15; 153 static const int kOCSPResponseTag = 154 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16; 155 static const int kExtendedMasterSecretTag = 156 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17; 157 158 static int SSL_SESSION_to_bytes_full(SSL_SESSION *in, uint8_t **out_data, 159 size_t *out_len, int for_ticket) { 160 CBB cbb, session, child, child2; 161 162 if (in == NULL || in->cipher == NULL) { 163 return 0; 164 } 165 166 if (!CBB_init(&cbb, 0)) { 167 return 0; 168 } 169 170 if (!CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) || 171 !CBB_add_asn1_uint64(&session, SSL_SESSION_ASN1_VERSION) || 172 !CBB_add_asn1_uint64(&session, in->ssl_version) || 173 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || 174 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) || 175 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || 176 /* The session ID is irrelevant for a session ticket. */ 177 !CBB_add_bytes(&child, in->session_id, 178 for_ticket ? 0 : in->session_id_length) || 179 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || 180 !CBB_add_bytes(&child, in->master_key, in->master_key_length)) { 181 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 182 goto err; 183 } 184 185 if (in->time != 0) { 186 if (!CBB_add_asn1(&session, &child, kTimeTag) || 187 !CBB_add_asn1_uint64(&child, in->time)) { 188 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 189 goto err; 190 } 191 } 192 193 if (in->timeout != 0) { 194 if (!CBB_add_asn1(&session, &child, kTimeoutTag) || 195 !CBB_add_asn1_uint64(&child, in->timeout)) { 196 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 197 goto err; 198 } 199 } 200 201 /* The peer certificate is only serialized if the SHA-256 isn't 202 * serialized instead. */ 203 if (in->peer && !in->peer_sha256_valid) { 204 uint8_t *buf; 205 int len = i2d_X509(in->peer, NULL); 206 if (len < 0) { 207 goto err; 208 } 209 if (!CBB_add_asn1(&session, &child, kPeerTag) || 210 !CBB_add_space(&child, &buf, len)) { 211 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 212 goto err; 213 } 214 if (buf != NULL && i2d_X509(in->peer, &buf) < 0) { 215 goto err; 216 } 217 } 218 219 /* Although it is OPTIONAL and usually empty, OpenSSL has 220 * historically always encoded the sid_ctx. */ 221 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) || 222 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || 223 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) { 224 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 225 goto err; 226 } 227 228 if (in->verify_result != X509_V_OK) { 229 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) || 230 !CBB_add_asn1_uint64(&child, in->verify_result)) { 231 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 232 goto err; 233 } 234 } 235 236 if (in->tlsext_hostname) { 237 if (!CBB_add_asn1(&session, &child, kHostNameTag) || 238 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || 239 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname, 240 strlen(in->tlsext_hostname))) { 241 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 242 goto err; 243 } 244 } 245 246 if (in->psk_identity) { 247 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) || 248 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || 249 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity, 250 strlen(in->psk_identity))) { 251 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 252 goto err; 253 } 254 } 255 256 if (in->tlsext_tick_lifetime_hint > 0) { 257 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) || 258 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) { 259 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 260 goto err; 261 } 262 } 263 264 if (in->tlsext_tick) { 265 if (!CBB_add_asn1(&session, &child, kTicketTag) || 266 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || 267 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) { 268 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 269 goto err; 270 } 271 } 272 273 if (in->peer_sha256_valid) { 274 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) || 275 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || 276 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) { 277 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 278 goto err; 279 } 280 } 281 282 if (in->original_handshake_hash_len > 0) { 283 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) || 284 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || 285 !CBB_add_bytes(&child2, in->original_handshake_hash, 286 in->original_handshake_hash_len)) { 287 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 288 goto err; 289 } 290 } 291 292 if (in->tlsext_signed_cert_timestamp_list_length > 0) { 293 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) || 294 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || 295 !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list, 296 in->tlsext_signed_cert_timestamp_list_length)) { 297 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 298 goto err; 299 } 300 } 301 302 if (in->ocsp_response_length > 0) { 303 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) || 304 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || 305 !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) { 306 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 307 goto err; 308 } 309 } 310 311 if (in->extended_master_secret) { 312 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) || 313 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || 314 !CBB_add_u8(&child2, 0xff)) { 315 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 316 goto err; 317 } 318 } 319 320 if (!CBB_finish(&cbb, out_data, out_len)) { 321 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE); 322 goto err; 323 } 324 return 1; 325 326 err: 327 CBB_cleanup(&cbb); 328 return 0; 329 } 330 331 int SSL_SESSION_to_bytes(SSL_SESSION *in, uint8_t **out_data, size_t *out_len) { 332 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0); 333 } 334 335 int SSL_SESSION_to_bytes_for_ticket(SSL_SESSION *in, uint8_t **out_data, 336 size_t *out_len) { 337 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1); 338 } 339 340 int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) { 341 uint8_t *out; 342 size_t len; 343 344 if (!SSL_SESSION_to_bytes(in, &out, &len)) { 345 return -1; 346 } 347 348 if (len > INT_MAX) { 349 OPENSSL_free(out); 350 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_OVERFLOW); 351 return -1; 352 } 353 354 if (pp) { 355 memcpy(*pp, out, len); 356 *pp += len; 357 } 358 OPENSSL_free(out); 359 360 return len; 361 } 362 363 /* d2i_SSL_SESSION_get_string gets an optional ASN.1 OCTET STRING 364 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On 365 * entry, if |*out| is not NULL, it frees the existing contents. If 366 * the element was not found, it sets |*out| to NULL. It returns one 367 * on success, whether or not the element was found, and zero on 368 * decode error. */ 369 static int d2i_SSL_SESSION_get_string(CBS *cbs, char **out, unsigned tag) { 370 CBS value; 371 int present; 372 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) { 373 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 374 return 0; 375 } 376 if (present) { 377 if (CBS_contains_zero_byte(&value)) { 378 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 379 return 0; 380 } 381 if (!CBS_strdup(&value, out)) { 382 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, ERR_R_MALLOC_FAILURE); 383 return 0; 384 } 385 } else { 386 OPENSSL_free(*out); 387 *out = NULL; 388 } 389 return 1; 390 } 391 392 /* d2i_SSL_SESSION_get_string gets an optional ASN.1 OCTET STRING 393 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr| 394 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing 395 * contents. On entry, if the element was not found, it sets 396 * |*out_ptr| to NULL. It returns one on success, whether or not the 397 * element was found, and zero on decode error. */ 398 static int d2i_SSL_SESSION_get_octet_string(CBS *cbs, uint8_t **out_ptr, 399 size_t *out_len, unsigned tag) { 400 CBS value; 401 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) { 402 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 403 return 0; 404 } 405 if (!CBS_stow(&value, out_ptr, out_len)) { 406 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, ERR_R_MALLOC_FAILURE); 407 return 0; 408 } 409 return 1; 410 } 411 412 SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) { 413 SSL_SESSION *ret, *allocated = NULL; 414 CBS cbs, session, cipher, session_id, master_key; 415 CBS peer, sid_ctx, peer_sha256, original_handshake_hash; 416 int has_peer, has_peer_sha256, extended_master_secret; 417 uint64_t version, ssl_version; 418 uint64_t session_time, timeout, verify_result, ticket_lifetime_hint; 419 420 if (a && *a) { 421 ret = *a; 422 } else { 423 ret = allocated = SSL_SESSION_new(); 424 if (allocated == NULL) { 425 goto err; 426 } 427 } 428 429 CBS_init(&cbs, *pp, length); 430 if (!CBS_get_asn1(&cbs, &session, CBS_ASN1_SEQUENCE) || 431 !CBS_get_asn1_uint64(&session, &version) || 432 !CBS_get_asn1_uint64(&session, &ssl_version) || 433 !CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) || 434 !CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) || 435 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) || 436 !CBS_get_optional_asn1_uint64(&session, &session_time, kTimeTag, 437 time(NULL)) || 438 !CBS_get_optional_asn1_uint64(&session, &timeout, kTimeoutTag, 3) || 439 !CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) || 440 !CBS_get_optional_asn1_octet_string(&session, &sid_ctx, NULL, 441 kSessionIDContextTag) || 442 !CBS_get_optional_asn1_uint64(&session, &verify_result, kVerifyResultTag, 443 X509_V_OK)) { 444 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 445 goto err; 446 } 447 if (!d2i_SSL_SESSION_get_string(&session, &ret->tlsext_hostname, 448 kHostNameTag) || 449 !d2i_SSL_SESSION_get_string(&session, &ret->psk_identity, 450 kPSKIdentityTag)) { 451 goto err; 452 } 453 if (!CBS_get_optional_asn1_uint64(&session, &ticket_lifetime_hint, 454 kTicketLifetimeHintTag, 0)) { 455 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 456 goto err; 457 } 458 if (!d2i_SSL_SESSION_get_octet_string(&session, &ret->tlsext_tick, 459 &ret->tlsext_ticklen, kTicketTag)) { 460 goto err; 461 } 462 if (!CBS_get_optional_asn1_octet_string(&session, &peer_sha256, 463 &has_peer_sha256, kPeerSHA256Tag) || 464 !CBS_get_optional_asn1_octet_string(&session, &original_handshake_hash, 465 NULL, kOriginalHandshakeHashTag)) { 466 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 467 goto err; 468 } 469 if (!d2i_SSL_SESSION_get_octet_string( 470 &session, &ret->tlsext_signed_cert_timestamp_list, 471 &ret->tlsext_signed_cert_timestamp_list_length, 472 kSignedCertTimestampListTag) || 473 !d2i_SSL_SESSION_get_octet_string( 474 &session, &ret->ocsp_response, &ret->ocsp_response_length, 475 kOCSPResponseTag)) { 476 goto err; 477 } 478 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret, 479 kExtendedMasterSecretTag, 480 0 /* default to false */) || 481 CBS_len(&session) != 0) { 482 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 483 goto err; 484 } 485 ret->extended_master_secret = extended_master_secret; 486 487 if (version != SSL_SESSION_ASN1_VERSION) { 488 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 489 goto err; 490 } 491 492 /* Only support SSLv3/TLS and DTLS. */ 493 if ((ssl_version >> 8) != SSL3_VERSION_MAJOR && 494 (ssl_version >> 8) != (DTLS1_VERSION >> 8)) { 495 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_UNKNOWN_SSL_VERSION); 496 goto err; 497 } 498 ret->ssl_version = ssl_version; 499 500 uint16_t cipher_value; 501 if (!CBS_get_u16(&cipher, &cipher_value) || CBS_len(&cipher) != 0) { 502 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_CIPHER_CODE_WRONG_LENGTH); 503 goto err; 504 } 505 ret->cipher = SSL_get_cipher_by_value(cipher_value); 506 if (ret->cipher == NULL) { 507 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_UNSUPPORTED_CIPHER); 508 goto err; 509 } 510 511 if (CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH) { 512 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 513 goto err; 514 } 515 memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id)); 516 ret->session_id_length = CBS_len(&session_id); 517 518 if (CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) { 519 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 520 goto err; 521 } 522 memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key)); 523 ret->master_key_length = CBS_len(&master_key); 524 525 if (session_time > LONG_MAX || 526 timeout > LONG_MAX) { 527 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 528 goto err; 529 } 530 ret->time = session_time; 531 ret->timeout = timeout; 532 533 X509_free(ret->peer); 534 ret->peer = NULL; 535 if (has_peer) { 536 const uint8_t *ptr; 537 ptr = CBS_data(&peer); 538 ret->peer = d2i_X509(NULL, &ptr, CBS_len(&peer)); 539 if (ret->peer == NULL) { 540 goto err; 541 } 542 if (ptr != CBS_data(&peer) + CBS_len(&peer)) { 543 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 544 goto err; 545 } 546 } 547 548 if (CBS_len(&sid_ctx) > sizeof(ret->sid_ctx)) { 549 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 550 goto err; 551 } 552 memcpy(ret->sid_ctx, CBS_data(&sid_ctx), CBS_len(&sid_ctx)); 553 ret->sid_ctx_length = CBS_len(&sid_ctx); 554 555 if (verify_result > LONG_MAX || 556 ticket_lifetime_hint > 0xffffffff) { 557 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 558 goto err; 559 } 560 ret->verify_result = verify_result; 561 ret->tlsext_tick_lifetime_hint = ticket_lifetime_hint; 562 563 if (has_peer_sha256) { 564 if (CBS_len(&peer_sha256) != sizeof(ret->peer_sha256)) { 565 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 566 goto err; 567 } 568 memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256)); 569 ret->peer_sha256_valid = 1; 570 } else { 571 ret->peer_sha256_valid = 0; 572 } 573 574 if (CBS_len(&original_handshake_hash) > 575 sizeof(ret->original_handshake_hash)) { 576 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION); 577 goto err; 578 } 579 memcpy(ret->original_handshake_hash, CBS_data(&original_handshake_hash), 580 CBS_len(&original_handshake_hash)); 581 ret->original_handshake_hash_len = CBS_len(&original_handshake_hash); 582 583 if (a) { 584 *a = ret; 585 } 586 *pp = CBS_data(&cbs); 587 return ret; 588 589 err: 590 SSL_SESSION_free(allocated); 591 return NULL; 592 } 593