blob: 3311246c6f6e6c661d33ad2f7ada0cc36d6142cd [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@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@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@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@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
David Benjamin9e4e01e2015-09-15 01:48:04 -040083#include <openssl/ssl.h>
84
David Benjamin83fd6b62014-10-19 04:33:38 -040085#include <limits.h>
David Benjamin89abaea2014-10-19 13:50:18 -040086#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070087
David Benjaminee910bf2017-07-25 22:36:00 -040088#include <utility>
89
David Benjamin83fd6b62014-10-19 04:33:38 -040090#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -070091#include <openssl/err.h>
Brian Smith054e6822015-03-27 21:12:01 -100092#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -070093#include <openssl/x509.h>
94
David Benjamin17cf2cb2016-12-13 01:07:13 -050095#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -040096#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070097
Adam Langley95c29f32014-06-20 12:00:00 -070098
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070099BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -0400100
David Benjaminc11ea9422017-08-29 16:33:21 -0400101// An SSL_SESSION is serialized as the following ASN.1 structure:
102//
103// SSLSession ::= SEQUENCE {
104// version INTEGER (1), -- session structure version
105// sslVersion INTEGER, -- protocol version number
106// cipher OCTET STRING, -- two bytes long
107// sessionID OCTET STRING,
David Benjamin5351c8b2020-11-19 00:25:29 -0500108// secret OCTET STRING,
David Benjaminc11ea9422017-08-29 16:33:21 -0400109// time [1] INTEGER, -- seconds since UNIX epoch
110// timeout [2] INTEGER, -- in seconds
111// peer [3] Certificate OPTIONAL,
112// sessionIDContext [4] OCTET STRING OPTIONAL,
113// verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
David Benjaminc11ea9422017-08-29 16:33:21 -0400114// pskIdentity [8] OCTET STRING OPTIONAL,
115// ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
116// ticket [10] OCTET STRING OPTIONAL, -- client-only
117// peerSHA256 [13] OCTET STRING OPTIONAL,
118// originalHandshakeHash [14] OCTET STRING OPTIONAL,
119// signedCertTimestampList [15] OCTET STRING OPTIONAL,
120// -- contents of SCT extension
121// ocspResponse [16] OCTET STRING OPTIONAL,
122// -- stapled OCSP response from the server
123// extendedMasterSecret [17] BOOLEAN OPTIONAL,
124// groupID [18] INTEGER OPTIONAL,
125// certChain [19] SEQUENCE OF Certificate OPTIONAL,
126// ticketAgeAdd [21] OCTET STRING OPTIONAL,
127// isServer [22] BOOLEAN DEFAULT TRUE,
128// peerSignatureAlgorithm [23] INTEGER OPTIONAL,
129// ticketMaxEarlyData [24] INTEGER OPTIONAL,
130// authTimeout [25] INTEGER OPTIONAL, -- defaults to timeout
131// earlyALPN [26] OCTET STRING OPTIONAL,
Nick Harper6bfd25c2020-03-30 17:15:19 -0700132// isQuic [27] BOOLEAN OPTIONAL,
Nick Harper7c522992020-04-30 14:15:49 -0700133// quicEarlyDataHash [28] OCTET STRING OPTIONAL,
Steven Valdez51607f12020-08-05 10:46:05 -0400134// localALPS [29] OCTET STRING OPTIONAL,
135// peerALPS [30] OCTET STRING OPTIONAL,
136// -- Either both or none of localALPS and peerALPS must be present. If both
137// -- are present, earlyALPN must be present and non-empty.
David Benjaminc11ea9422017-08-29 16:33:21 -0400138// }
139//
140// Note: historically this serialization has included other optional
David Benjamina8614602017-09-06 15:40:19 -0400141// fields. Their presence is currently treated as a parse error, except for
142// hostName, which is ignored.
David Benjaminc11ea9422017-08-29 16:33:21 -0400143//
144// keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
David Benjamina8614602017-09-06 15:40:19 -0400145// hostName [6] OCTET STRING OPTIONAL,
David Benjaminc11ea9422017-08-29 16:33:21 -0400146// pskIdentityHint [7] OCTET STRING OPTIONAL,
147// compressionMethod [11] OCTET STRING OPTIONAL,
148// srpUsername [12] OCTET STRING OPTIONAL,
149// ticketFlags [20] INTEGER OPTIONAL,
David Benjamin83fd6b62014-10-19 04:33:38 -0400150
Adam Langley96c2a282015-06-02 14:16:44 -0700151static const unsigned kVersion = 1;
David Benjamin7f393f72015-10-17 12:36:12 -0400152
David Benjamina1dffbf2022-10-25 16:29:43 -0400153static const CBS_ASN1_TAG kTimeTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400154 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
David Benjamina1dffbf2022-10-25 16:29:43 -0400155static const CBS_ASN1_TAG kTimeoutTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400156 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
David Benjamina1dffbf2022-10-25 16:29:43 -0400157static const CBS_ASN1_TAG kPeerTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400158 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
David Benjamina1dffbf2022-10-25 16:29:43 -0400159static const CBS_ASN1_TAG kSessionIDContextTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400160 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
David Benjamina1dffbf2022-10-25 16:29:43 -0400161static const CBS_ASN1_TAG kVerifyResultTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400162 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
David Benjamina1dffbf2022-10-25 16:29:43 -0400163static const CBS_ASN1_TAG kHostNameTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400164 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
David Benjamina1dffbf2022-10-25 16:29:43 -0400165static const CBS_ASN1_TAG kPSKIdentityTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400166 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
David Benjamina1dffbf2022-10-25 16:29:43 -0400167static const CBS_ASN1_TAG kTicketLifetimeHintTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400168 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
David Benjamina1dffbf2022-10-25 16:29:43 -0400169static const CBS_ASN1_TAG kTicketTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400170 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
David Benjamina1dffbf2022-10-25 16:29:43 -0400171static const CBS_ASN1_TAG kPeerSHA256Tag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400172 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
David Benjamina1dffbf2022-10-25 16:29:43 -0400173static const CBS_ASN1_TAG kOriginalHandshakeHashTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400174 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
David Benjamina1dffbf2022-10-25 16:29:43 -0400175static const CBS_ASN1_TAG kSignedCertTimestampListTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400176 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
David Benjamina1dffbf2022-10-25 16:29:43 -0400177static const CBS_ASN1_TAG kOCSPResponseTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400178 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
David Benjamina1dffbf2022-10-25 16:29:43 -0400179static const CBS_ASN1_TAG kExtendedMasterSecretTag =
Adam Langley75712922014-10-10 16:23:43 -0700180 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
David Benjamina1dffbf2022-10-25 16:29:43 -0400181static const CBS_ASN1_TAG kGroupIDTag =
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200182 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
David Benjamina1dffbf2022-10-25 16:29:43 -0400183static const CBS_ASN1_TAG kCertChainTag =
David Benjamin26416e92015-08-22 16:04:17 -0400184 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
David Benjamina1dffbf2022-10-25 16:29:43 -0400185static const CBS_ASN1_TAG kTicketAgeAddTag =
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400186 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21;
David Benjamina1dffbf2022-10-25 16:29:43 -0400187static const CBS_ASN1_TAG kIsServerTag =
Adam Langley364f7a62016-12-12 10:51:00 -0800188 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22;
David Benjamina1dffbf2022-10-25 16:29:43 -0400189static const CBS_ASN1_TAG kPeerSignatureAlgorithmTag =
David Benjaminf1050fd2016-12-13 20:05:36 -0500190 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 23;
David Benjamina1dffbf2022-10-25 16:29:43 -0400191static const CBS_ASN1_TAG kTicketMaxEarlyDataTag =
Steven Valdez08b65f42016-12-07 15:29:45 -0500192 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 24;
David Benjamina1dffbf2022-10-25 16:29:43 -0400193static const CBS_ASN1_TAG kAuthTimeoutTag =
David Benjamin17b30832017-01-28 14:00:32 -0500194 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 25;
David Benjamina1dffbf2022-10-25 16:29:43 -0400195static const CBS_ASN1_TAG kEarlyALPNTag =
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500196 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 26;
David Benjamina1dffbf2022-10-25 16:29:43 -0400197static const CBS_ASN1_TAG kIsQuicTag =
Nick Harper6bfd25c2020-03-30 17:15:19 -0700198 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 27;
David Benjamina1dffbf2022-10-25 16:29:43 -0400199static const CBS_ASN1_TAG kQuicEarlyDataContextTag =
Nick Harper7c522992020-04-30 14:15:49 -0700200 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 28;
David Benjamina1dffbf2022-10-25 16:29:43 -0400201static const CBS_ASN1_TAG kLocalALPSTag =
Steven Valdez51607f12020-08-05 10:46:05 -0400202 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 29;
David Benjamina1dffbf2022-10-25 16:29:43 -0400203static const CBS_ASN1_TAG kPeerALPSTag =
Steven Valdez51607f12020-08-05 10:46:05 -0400204 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 30;
David Benjamin26416e92015-08-22 16:04:17 -0400205
Adam Langleye8d24392018-01-25 15:49:37 -0800206static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, CBB *cbb,
207 int for_ticket) {
David Benjaminf3a8b122014-12-25 23:11:49 -0500208 if (in == NULL || in->cipher == NULL) {
David Benjamin89abaea2014-10-19 13:50:18 -0400209 return 0;
210 }
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjamin1386aad2017-07-19 23:57:40 -0400212 CBB session, child, child2;
Adam Langleye8d24392018-01-25 15:49:37 -0800213 if (!CBB_add_asn1(cbb, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400214 !CBB_add_asn1_uint64(&session, kVersion) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400215 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
216 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminf3a8b122014-12-25 23:11:49 -0500217 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400218 // The session ID is irrelevant for a session ticket.
Adam Langleyc61b5772018-01-25 15:37:46 -0800219 !CBB_add_asn1_octet_string(&session, in->session_id,
220 for_ticket ? 0 : in->session_id_length) ||
David Benjamin5351c8b2020-11-19 00:25:29 -0500221 !CBB_add_asn1_octet_string(&session, in->secret, in->secret_length) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500222 !CBB_add_asn1(&session, &child, kTimeTag) ||
223 !CBB_add_asn1_uint64(&child, in->time) ||
224 !CBB_add_asn1(&session, &child, kTimeoutTag) ||
David Benjamin5b7b09c2016-11-03 20:32:10 -0400225 !CBB_add_asn1_uint64(&child, in->timeout)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400226 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400227 }
Adam Langley95c29f32014-06-20 12:00:00 -0700228
David Benjaminc11ea9422017-08-29 16:33:21 -0400229 // The peer certificate is only serialized if the SHA-256 isn't
230 // serialized instead.
David Benjaminbfdd1a92018-06-29 16:26:38 -0400231 if (sk_CRYPTO_BUFFER_num(in->certs.get()) > 0 && !in->peer_sha256_valid) {
232 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs.get(), 0);
Adam Langley68e71242016-12-12 11:06:16 -0800233 if (!CBB_add_asn1(&session, &child, kPeerTag) ||
234 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
235 CRYPTO_BUFFER_len(buffer))) {
David Benjamin1386aad2017-07-19 23:57:40 -0400236 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400237 }
David Benjamin89abaea2014-10-19 13:50:18 -0400238 }
Adam Langley95c29f32014-06-20 12:00:00 -0700239
David Benjaminc11ea9422017-08-29 16:33:21 -0400240 // Although it is OPTIONAL and usually empty, OpenSSL has
241 // historically always encoded the sid_ctx.
David Benjamin89abaea2014-10-19 13:50:18 -0400242 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
Adam Langleyc61b5772018-01-25 15:37:46 -0800243 !CBB_add_asn1_octet_string(&child, in->sid_ctx, in->sid_ctx_length)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400244 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400245 }
Adam Langley95c29f32014-06-20 12:00:00 -0700246
David Benjamin89abaea2014-10-19 13:50:18 -0400247 if (in->verify_result != X509_V_OK) {
248 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
249 !CBB_add_asn1_uint64(&child, in->verify_result)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400250 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400251 }
252 }
Adam Langley95c29f32014-06-20 12:00:00 -0700253
David Benjamin89abaea2014-10-19 13:50:18 -0400254 if (in->psk_identity) {
255 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
David Benjaminbfdd1a92018-06-29 16:26:38 -0400256 !CBB_add_asn1_octet_string(&child,
257 (const uint8_t *)in->psk_identity.get(),
258 strlen(in->psk_identity.get()))) {
David Benjamin1386aad2017-07-19 23:57:40 -0400259 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400260 }
261 }
Adam Langley95c29f32014-06-20 12:00:00 -0700262
David Benjaminbfdd1a92018-06-29 16:26:38 -0400263 if (in->ticket_lifetime_hint > 0) {
David Benjamin89abaea2014-10-19 13:50:18 -0400264 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
David Benjaminbfdd1a92018-06-29 16:26:38 -0400265 !CBB_add_asn1_uint64(&child, in->ticket_lifetime_hint)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400266 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400267 }
268 }
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjaminbfdd1a92018-06-29 16:26:38 -0400270 if (!in->ticket.empty() && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400271 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
David Benjaminbfdd1a92018-06-29 16:26:38 -0400272 !CBB_add_asn1_octet_string(&child, in->ticket.data(),
273 in->ticket.size())) {
David Benjamin1386aad2017-07-19 23:57:40 -0400274 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400275 }
276 }
Adam Langley75872532014-06-20 12:00:00 -0700277
David Benjamin89abaea2014-10-19 13:50:18 -0400278 if (in->peer_sha256_valid) {
279 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
Adam Langleyc61b5772018-01-25 15:37:46 -0800280 !CBB_add_asn1_octet_string(&child, in->peer_sha256,
281 sizeof(in->peer_sha256))) {
David Benjamin1386aad2017-07-19 23:57:40 -0400282 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400283 }
284 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700285
David Benjamin89abaea2014-10-19 13:50:18 -0400286 if (in->original_handshake_hash_len > 0) {
287 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
Adam Langleyc61b5772018-01-25 15:37:46 -0800288 !CBB_add_asn1_octet_string(&child, in->original_handshake_hash,
289 in->original_handshake_hash_len)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400290 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400291 }
292 }
Adam Langley95c29f32014-06-20 12:00:00 -0700293
David Benjamin8fc2dc02017-08-22 15:07:51 -0700294 if (in->signed_cert_timestamp_list != nullptr) {
David Benjamin89abaea2014-10-19 13:50:18 -0400295 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
Adam Langleyc61b5772018-01-25 15:37:46 -0800296 !CBB_add_asn1_octet_string(
David Benjaminbfdd1a92018-06-29 16:26:38 -0400297 &child, CRYPTO_BUFFER_data(in->signed_cert_timestamp_list.get()),
298 CRYPTO_BUFFER_len(in->signed_cert_timestamp_list.get()))) {
David Benjamin1386aad2017-07-19 23:57:40 -0400299 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400300 }
301 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200302
David Benjamin8fc2dc02017-08-22 15:07:51 -0700303 if (in->ocsp_response != nullptr) {
David Benjamin89abaea2014-10-19 13:50:18 -0400304 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
David Benjaminbfdd1a92018-06-29 16:26:38 -0400305 !CBB_add_asn1_octet_string(
306 &child, CRYPTO_BUFFER_data(in->ocsp_response.get()),
307 CRYPTO_BUFFER_len(in->ocsp_response.get()))) {
David Benjamin1386aad2017-07-19 23:57:40 -0400308 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400309 }
310 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400311
Adam Langley75712922014-10-10 16:23:43 -0700312 if (in->extended_master_secret) {
313 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
Adam Langleyc61b5772018-01-25 15:37:46 -0800314 !CBB_add_asn1_bool(&child, true)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400315 return 0;
Adam Langley75712922014-10-10 16:23:43 -0700316 }
317 }
318
David Benjamin4882a6c2016-12-11 02:48:12 -0500319 if (in->group_id > 0 &&
320 (!CBB_add_asn1(&session, &child, kGroupIDTag) ||
321 !CBB_add_asn1_uint64(&child, in->group_id))) {
David Benjamin1386aad2017-07-19 23:57:40 -0400322 return 0;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200323 }
324
David Benjaminc11ea9422017-08-29 16:33:21 -0400325 // The certificate chain is only serialized if the leaf's SHA-256 isn't
326 // serialized instead.
Adam Langley68e71242016-12-12 11:06:16 -0800327 if (in->certs != NULL &&
328 !in->peer_sha256_valid &&
David Benjaminbfdd1a92018-06-29 16:26:38 -0400329 sk_CRYPTO_BUFFER_num(in->certs.get()) >= 2) {
David Benjamin26416e92015-08-22 16:04:17 -0400330 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400331 return 0;
David Benjamin26416e92015-08-22 16:04:17 -0400332 }
David Benjaminbfdd1a92018-06-29 16:26:38 -0400333 for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs.get()); i++) {
334 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs.get(), i);
Adam Langley68e71242016-12-12 11:06:16 -0800335 if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
336 CRYPTO_BUFFER_len(buffer))) {
David Benjamin1386aad2017-07-19 23:57:40 -0400337 return 0;
David Benjamin26416e92015-08-22 16:04:17 -0400338 }
339 }
340 }
341
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400342 if (in->ticket_age_add_valid) {
343 if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
344 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
345 !CBB_add_u32(&child2, in->ticket_age_add)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400346 return 0;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400347 }
348 }
349
Adam Langley364f7a62016-12-12 10:51:00 -0800350 if (!in->is_server) {
351 if (!CBB_add_asn1(&session, &child, kIsServerTag) ||
Adam Langleyc61b5772018-01-25 15:37:46 -0800352 !CBB_add_asn1_bool(&child, false)) {
David Benjamin1386aad2017-07-19 23:57:40 -0400353 return 0;
Adam Langley364f7a62016-12-12 10:51:00 -0800354 }
355 }
356
David Benjaminf1050fd2016-12-13 20:05:36 -0500357 if (in->peer_signature_algorithm != 0 &&
358 (!CBB_add_asn1(&session, &child, kPeerSignatureAlgorithmTag) ||
359 !CBB_add_asn1_uint64(&child, in->peer_signature_algorithm))) {
David Benjamin1386aad2017-07-19 23:57:40 -0400360 return 0;
David Benjaminf1050fd2016-12-13 20:05:36 -0500361 }
362
Steven Valdez08b65f42016-12-07 15:29:45 -0500363 if (in->ticket_max_early_data != 0 &&
364 (!CBB_add_asn1(&session, &child, kTicketMaxEarlyDataTag) ||
365 !CBB_add_asn1_uint64(&child, in->ticket_max_early_data))) {
David Benjamin1386aad2017-07-19 23:57:40 -0400366 return 0;
Steven Valdez08b65f42016-12-07 15:29:45 -0500367 }
368
David Benjamin17b30832017-01-28 14:00:32 -0500369 if (in->timeout != in->auth_timeout &&
370 (!CBB_add_asn1(&session, &child, kAuthTimeoutTag) ||
371 !CBB_add_asn1_uint64(&child, in->auth_timeout))) {
David Benjamin1386aad2017-07-19 23:57:40 -0400372 return 0;
David Benjamin17b30832017-01-28 14:00:32 -0500373 }
374
David Benjaminbfdd1a92018-06-29 16:26:38 -0400375 if (!in->early_alpn.empty()) {
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500376 if (!CBB_add_asn1(&session, &child, kEarlyALPNTag) ||
David Benjaminbfdd1a92018-06-29 16:26:38 -0400377 !CBB_add_asn1_octet_string(&child, in->early_alpn.data(),
378 in->early_alpn.size())) {
David Benjamin1386aad2017-07-19 23:57:40 -0400379 return 0;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500380 }
381 }
382
Nick Harper6bfd25c2020-03-30 17:15:19 -0700383 if (in->is_quic) {
384 if (!CBB_add_asn1(&session, &child, kIsQuicTag) ||
385 !CBB_add_asn1_bool(&child, true)) {
Nick Harper6bfd25c2020-03-30 17:15:19 -0700386 return 0;
387 }
388 }
389
Nick Harper85194322020-05-20 16:59:29 -0700390 if (!in->quic_early_data_context.empty()) {
391 if (!CBB_add_asn1(&session, &child, kQuicEarlyDataContextTag) ||
392 !CBB_add_asn1_octet_string(&child, in->quic_early_data_context.data(),
393 in->quic_early_data_context.size())) {
Nick Harper7c522992020-04-30 14:15:49 -0700394 return 0;
395 }
396 }
Nick Harper6bfd25c2020-03-30 17:15:19 -0700397
Steven Valdez51607f12020-08-05 10:46:05 -0400398 if (in->has_application_settings) {
399 if (!CBB_add_asn1(&session, &child, kLocalALPSTag) ||
400 !CBB_add_asn1_octet_string(&child,
401 in->local_application_settings.data(),
402 in->local_application_settings.size()) ||
403 !CBB_add_asn1(&session, &child, kPeerALPSTag) ||
404 !CBB_add_asn1_octet_string(&child, in->peer_application_settings.data(),
405 in->peer_application_settings.size())) {
Steven Valdez51607f12020-08-05 10:46:05 -0400406 return 0;
407 }
408 }
409
Adam Langleye8d24392018-01-25 15:49:37 -0800410 return CBB_flush(cbb);
David Benjamin3cac4502014-10-21 01:46:30 -0400411}
412
David Benjaminbfdd1a92018-06-29 16:26:38 -0400413// SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING explicitly
414// tagged with |tag| from |cbs| and saves it in |*out|. If the element was not
415// found, it sets |*out| to NULL. It returns one on success, whether or not the
416// element was found, and zero on decode error.
David Benjamina1dffbf2022-10-25 16:29:43 -0400417static int SSL_SESSION_parse_string(CBS *cbs, UniquePtr<char> *out,
418 CBS_ASN1_TAG tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400419 CBS value;
420 int present;
421 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400422 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400423 return 0;
424 }
425 if (present) {
426 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400427 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400428 return 0;
429 }
David Benjaminbfdd1a92018-06-29 16:26:38 -0400430 char *raw = nullptr;
431 if (!CBS_strdup(&value, &raw)) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400432 return 0;
433 }
David Benjaminbfdd1a92018-06-29 16:26:38 -0400434 out->reset(raw);
David Benjamin2755a3e2015-04-22 16:17:58 -0400435 } else {
David Benjaminbfdd1a92018-06-29 16:26:38 -0400436 out->reset();
David Benjamin83fd6b62014-10-19 04:33:38 -0400437 }
438 return 1;
439}
Adam Langley95c29f32014-06-20 12:00:00 -0700440
David Benjaminbfdd1a92018-06-29 16:26:38 -0400441// SSL_SESSION_parse_octet_string gets an optional ASN.1 OCTET STRING explicitly
442// tagged with |tag| from |cbs| and stows it in |*out|. It returns one on
443// success, whether or not the element was found, and zero on decode error.
444static bool SSL_SESSION_parse_octet_string(CBS *cbs, Array<uint8_t> *out,
David Benjamina1dffbf2022-10-25 16:29:43 -0400445 CBS_ASN1_TAG tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400446 CBS value;
447 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400448 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminbfdd1a92018-06-29 16:26:38 -0400449 return false;
David Benjamin83fd6b62014-10-19 04:33:38 -0400450 }
David Benjaminbfdd1a92018-06-29 16:26:38 -0400451 return out->CopyFrom(value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400452}
Adam Langley95c29f32014-06-20 12:00:00 -0700453
David Benjaminbfdd1a92018-06-29 16:26:38 -0400454static int SSL_SESSION_parse_crypto_buffer(CBS *cbs,
455 UniquePtr<CRYPTO_BUFFER> *out,
David Benjamina1dffbf2022-10-25 16:29:43 -0400456 CBS_ASN1_TAG tag,
David Benjamin8fc2dc02017-08-22 15:07:51 -0700457 CRYPTO_BUFFER_POOL *pool) {
458 if (!CBS_peek_asn1_tag(cbs, tag)) {
459 return 1;
460 }
461
462 CBS child, value;
463 if (!CBS_get_asn1(cbs, &child, tag) ||
464 !CBS_get_asn1(&child, &value, CBS_ASN1_OCTETSTRING) ||
465 CBS_len(&child) != 0) {
466 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
467 return 0;
468 }
David Benjaminbfdd1a92018-06-29 16:26:38 -0400469 out->reset(CRYPTO_BUFFER_new_from_CBS(&value, pool));
David Benjamin8fc2dc02017-08-22 15:07:51 -0700470 if (*out == nullptr) {
David Benjamin8fc2dc02017-08-22 15:07:51 -0700471 return 0;
472 }
473 return 1;
474}
475
David Benjaminc11ea9422017-08-29 16:33:21 -0400476// SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
477// explicitly tagged with |tag| of size at most |max_out|.
David Benjamina1dffbf2022-10-25 16:29:43 -0400478static int SSL_SESSION_parse_bounded_octet_string(CBS *cbs, uint8_t *out,
479 uint8_t *out_len,
480 uint8_t max_out,
481 CBS_ASN1_TAG tag) {
David Benjamin8be79a32015-08-23 01:37:36 -0400482 CBS value;
483 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
484 CBS_len(&value) > max_out) {
485 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
486 return 0;
487 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500488 OPENSSL_memcpy(out, CBS_data(&value), CBS_len(&value));
David Benjamin44a389a2022-12-10 00:46:54 -0500489 *out_len = static_cast<uint8_t>(CBS_len(&value));
David Benjamin8be79a32015-08-23 01:37:36 -0400490 return 1;
491}
Adam Langley95c29f32014-06-20 12:00:00 -0700492
David Benjamina1dffbf2022-10-25 16:29:43 -0400493static int SSL_SESSION_parse_long(CBS *cbs, long *out, CBS_ASN1_TAG tag,
David Benjamin8be79a32015-08-23 01:37:36 -0400494 long default_value) {
495 uint64_t value;
496 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
497 (uint64_t)default_value) ||
498 value > LONG_MAX) {
499 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
500 return 0;
501 }
502 *out = (long)value;
503 return 1;
504}
505
David Benjamina1dffbf2022-10-25 16:29:43 -0400506static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, CBS_ASN1_TAG tag,
David Benjamin8be79a32015-08-23 01:37:36 -0400507 uint32_t default_value) {
508 uint64_t value;
509 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
510 (uint64_t)default_value) ||
511 value > 0xffffffff) {
512 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
513 return 0;
514 }
515 *out = (uint32_t)value;
516 return 1;
517}
518
David Benjamina1dffbf2022-10-25 16:29:43 -0400519static int SSL_SESSION_parse_u16(CBS *cbs, uint16_t *out, CBS_ASN1_TAG tag,
David Benjaminf1050fd2016-12-13 20:05:36 -0500520 uint16_t default_value) {
521 uint64_t value;
522 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
523 (uint64_t)default_value) ||
524 value > 0xffff) {
525 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
526 return 0;
527 }
528 *out = (uint16_t)value;
529 return 1;
530}
531
David Benjamin31b0c9b2017-07-20 14:49:15 -0400532UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs,
533 const SSL_X509_METHOD *x509_method,
534 CRYPTO_BUFFER_POOL *pool) {
535 UniquePtr<SSL_SESSION> ret = ssl_session_new(x509_method);
536 if (!ret) {
537 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400538 }
Adam Langley95c29f32014-06-20 12:00:00 -0700539
David Benjamin8be79a32015-08-23 01:37:36 -0400540 CBS session;
541 uint64_t version, ssl_version;
Steven Valdez8f36c512017-06-20 10:55:02 -0400542 uint16_t unused;
David Benjaminfd67aa82015-06-15 19:41:48 -0400543 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400544 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400545 version != kVersion ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400546 !CBS_get_asn1_uint64(&session, &ssl_version) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400547 // Require sessions have versions valid in either TLS or DTLS. The session
548 // will not be used by the handshake if not applicable, but, for
549 // simplicity, never parse a session that does not pass
550 // |ssl_protocol_version_from_wire|.
Steven Valdez8f36c512017-06-20 10:55:02 -0400551 ssl_version > UINT16_MAX ||
552 !ssl_protocol_version_from_wire(&unused, ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400553 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400554 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400555 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400556 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700557
David Benjamin8be79a32015-08-23 01:37:36 -0400558 CBS cipher;
David Benjaminf3a8b122014-12-25 23:11:49 -0500559 uint16_t cipher_value;
David Benjamin8be79a32015-08-23 01:37:36 -0400560 if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
561 !CBS_get_u16(&cipher, &cipher_value) ||
562 CBS_len(&cipher) != 0) {
563 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400564 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400565 }
David Benjamina1c90a52015-05-30 17:03:14 -0400566 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400567 if (ret->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400568 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400569 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400570 }
Adam Langley95c29f32014-06-20 12:00:00 -0700571
David Benjamin5351c8b2020-11-19 00:25:29 -0500572 CBS session_id, secret;
David Benjamin8be79a32015-08-23 01:37:36 -0400573 if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
574 CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
David Benjamin5351c8b2020-11-19 00:25:29 -0500575 !CBS_get_asn1(&session, &secret, CBS_ASN1_OCTETSTRING) ||
576 CBS_len(&secret) > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400577 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400578 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400579 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500580 OPENSSL_memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
David Benjamin44a389a2022-12-10 00:46:54 -0500581 static_assert(SSL3_MAX_SSL_SESSION_ID_LENGTH <= UINT8_MAX,
582 "max session ID is too large");
583 ret->session_id_length = static_cast<uint8_t>(CBS_len(&session_id));
David Benjamin5351c8b2020-11-19 00:25:29 -0500584 OPENSSL_memcpy(ret->secret, CBS_data(&secret), CBS_len(&secret));
David Benjamin44a389a2022-12-10 00:46:54 -0500585 static_assert(SSL_MAX_MASTER_KEY_LENGTH <= UINT8_MAX,
586 "max secret is too large");
587 ret->secret_length = static_cast<uint8_t>(CBS_len(&secret));
Adam Langley95c29f32014-06-20 12:00:00 -0700588
David Benjamin5b7b09c2016-11-03 20:32:10 -0400589 CBS child;
David Benjaminad8f5e12017-02-20 17:00:20 -0500590 uint64_t timeout;
David Benjamin5b7b09c2016-11-03 20:32:10 -0400591 if (!CBS_get_asn1(&session, &child, kTimeTag) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500592 !CBS_get_asn1_uint64(&child, &ret->time) ||
David Benjamin5b7b09c2016-11-03 20:32:10 -0400593 !CBS_get_asn1(&session, &child, kTimeoutTag) ||
594 !CBS_get_asn1_uint64(&child, &timeout) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500595 timeout > UINT32_MAX) {
David Benjamin3570d732015-06-29 00:28:17 -0400596 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400597 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400598 }
Adam Langley95c29f32014-06-20 12:00:00 -0700599
David Benjaminad8f5e12017-02-20 17:00:20 -0500600 ret->timeout = (uint32_t)timeout;
David Benjamin5b7b09c2016-11-03 20:32:10 -0400601
David Benjamin8be79a32015-08-23 01:37:36 -0400602 CBS peer;
603 int has_peer;
Adam Langley68e71242016-12-12 11:06:16 -0800604 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
605 (has_peer && CBS_len(&peer) == 0)) {
David Benjamin8be79a32015-08-23 01:37:36 -0400606 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400607 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400608 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400609 // |peer| is processed with the certificate chain.
Adam Langley95c29f32014-06-20 12:00:00 -0700610
David Benjamin8be79a32015-08-23 01:37:36 -0400611 if (!SSL_SESSION_parse_bounded_octet_string(
612 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
613 kSessionIDContextTag) ||
614 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
David Benjamina8614602017-09-06 15:40:19 -0400615 X509_V_OK)) {
616 return nullptr;
617 }
618
619 // Skip the historical hostName field.
620 CBS unused_hostname;
621 if (!CBS_get_optional_asn1(&session, &unused_hostname, nullptr,
622 kHostNameTag)) {
623 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
624 return nullptr;
625 }
626
627 if (!SSL_SESSION_parse_string(&session, &ret->psk_identity,
David Benjamin8be79a32015-08-23 01:37:36 -0400628 kPSKIdentityTag) ||
David Benjaminbfdd1a92018-06-29 16:26:38 -0400629 !SSL_SESSION_parse_u32(&session, &ret->ticket_lifetime_hint,
David Benjamin8be79a32015-08-23 01:37:36 -0400630 kTicketLifetimeHintTag, 0) ||
David Benjaminbfdd1a92018-06-29 16:26:38 -0400631 !SSL_SESSION_parse_octet_string(&session, &ret->ticket, kTicketTag)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400632 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400633 }
Adam Langley95c29f32014-06-20 12:00:00 -0700634
David Benjamin8be79a32015-08-23 01:37:36 -0400635 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
David Benjamin5b7b09c2016-11-03 20:32:10 -0400636 CBS peer_sha256;
David Benjamin8be79a32015-08-23 01:37:36 -0400637 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
638 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
639 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
640 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400641 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400642 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400643 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500644 OPENSSL_memcpy(ret->peer_sha256, CBS_data(&peer_sha256),
645 sizeof(ret->peer_sha256));
Anton Bikineev50e7ea52022-01-23 22:35:48 +0100646 ret->peer_sha256_valid = true;
David Benjamin83fd6b62014-10-19 04:33:38 -0400647 } else {
Anton Bikineev50e7ea52022-01-23 22:35:48 +0100648 ret->peer_sha256_valid = false;
David Benjamin83fd6b62014-10-19 04:33:38 -0400649 }
Adam Langley95c29f32014-06-20 12:00:00 -0700650
David Benjamin8be79a32015-08-23 01:37:36 -0400651 if (!SSL_SESSION_parse_bounded_octet_string(
652 &session, ret->original_handshake_hash,
653 &ret->original_handshake_hash_len,
654 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
David Benjamin8fc2dc02017-08-22 15:07:51 -0700655 !SSL_SESSION_parse_crypto_buffer(&session,
656 &ret->signed_cert_timestamp_list,
657 kSignedCertTimestampListTag, pool) ||
658 !SSL_SESSION_parse_crypto_buffer(&session, &ret->ocsp_response,
659 kOCSPResponseTag, pool)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400660 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400661 }
662
663 int extended_master_secret;
664 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
665 kExtendedMasterSecretTag,
666 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400667 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400668 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400669 }
David Benjamin8be79a32015-08-23 01:37:36 -0400670 ret->extended_master_secret = !!extended_master_secret;
671
David Benjamin11fa7032017-04-11 22:13:45 -0400672 if (!SSL_SESSION_parse_u16(&session, &ret->group_id, kGroupIDTag, 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400673 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400674 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400675 }
676
677 CBS cert_chain;
Adam Langley68e71242016-12-12 11:06:16 -0800678 CBS_init(&cert_chain, NULL, 0);
David Benjamin26416e92015-08-22 16:04:17 -0400679 int has_cert_chain;
680 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
Adam Langley68e71242016-12-12 11:06:16 -0800681 kCertChainTag) ||
682 (has_cert_chain && CBS_len(&cert_chain) == 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400683 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400684 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400685 }
Adam Langley68e71242016-12-12 11:06:16 -0800686 if (has_cert_chain && !has_peer) {
687 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400688 return nullptr;
Adam Langley364f7a62016-12-12 10:51:00 -0800689 }
Adam Langley68e71242016-12-12 11:06:16 -0800690 if (has_peer || has_cert_chain) {
David Benjaminbfdd1a92018-06-29 16:26:38 -0400691 ret->certs.reset(sk_CRYPTO_BUFFER_new_null());
692 if (ret->certs == nullptr) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400693 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400694 }
Adam Langley68e71242016-12-12 11:06:16 -0800695
696 if (has_peer) {
David Benjaminee910bf2017-07-25 22:36:00 -0400697 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new_from_CBS(&peer, pool));
698 if (!buffer ||
David Benjaminbfdd1a92018-06-29 16:26:38 -0400699 !PushToStack(ret->certs.get(), std::move(buffer))) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400700 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400701 }
702 }
Adam Langley68e71242016-12-12 11:06:16 -0800703
704 while (CBS_len(&cert_chain) > 0) {
705 CBS cert;
706 if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) ||
707 CBS_len(&cert) == 0) {
708 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400709 return nullptr;
Adam Langley68e71242016-12-12 11:06:16 -0800710 }
711
David Benjamin2908dd12018-06-29 17:46:42 -0400712 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new_from_CBS(&cert, pool));
713 if (buffer == nullptr ||
David Benjaminbfdd1a92018-06-29 16:26:38 -0400714 !PushToStack(ret->certs.get(), std::move(buffer))) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400715 return nullptr;
Adam Langley68e71242016-12-12 11:06:16 -0800716 }
717 }
718 }
719
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400720 CBS age_add;
721 int age_add_present;
Steven Valdez5b986082016-09-01 12:29:49 -0400722 if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400723 kTicketAgeAddTag) ||
724 (age_add_present &&
725 !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
726 CBS_len(&age_add) != 0) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400727 return nullptr;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400728 }
David Benjamina3a71e92018-06-29 13:24:45 -0400729 ret->ticket_age_add_valid = age_add_present != 0;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400730
Adam Langley364f7a62016-12-12 10:51:00 -0800731 int is_server;
732 if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag,
733 1 /* default to true */)) {
734 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400735 return nullptr;
Adam Langley364f7a62016-12-12 10:51:00 -0800736 }
737 /* TODO: in time we can include |is_server| for servers too, then we can
738 enforce that client and server sessions are never mixed up. */
739
740 ret->is_server = is_server;
741
Nick Harper6bfd25c2020-03-30 17:15:19 -0700742 int is_quic;
David Benjaminf1050fd2016-12-13 20:05:36 -0500743 if (!SSL_SESSION_parse_u16(&session, &ret->peer_signature_algorithm,
744 kPeerSignatureAlgorithmTag, 0) ||
Steven Valdez08b65f42016-12-07 15:29:45 -0500745 !SSL_SESSION_parse_u32(&session, &ret->ticket_max_early_data,
746 kTicketMaxEarlyDataTag, 0) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500747 !SSL_SESSION_parse_u32(&session, &ret->auth_timeout, kAuthTimeoutTag,
748 ret->timeout) ||
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500749 !SSL_SESSION_parse_octet_string(&session, &ret->early_alpn,
David Benjaminbfdd1a92018-06-29 16:26:38 -0400750 kEarlyALPNTag) ||
Nick Harper6bfd25c2020-03-30 17:15:19 -0700751 !CBS_get_optional_asn1_bool(&session, &is_quic, kIsQuicTag,
752 /*default_value=*/false) ||
Nick Harper85194322020-05-20 16:59:29 -0700753 !SSL_SESSION_parse_octet_string(&session, &ret->quic_early_data_context,
Steven Valdez51607f12020-08-05 10:46:05 -0400754 kQuicEarlyDataContextTag)) {
755 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
756 return nullptr;
757 }
758
759 CBS settings;
760 int has_local_alps, has_peer_alps;
761 if (!CBS_get_optional_asn1_octet_string(&session, &settings, &has_local_alps,
762 kLocalALPSTag) ||
763 !ret->local_application_settings.CopyFrom(settings) ||
764 !CBS_get_optional_asn1_octet_string(&session, &settings, &has_peer_alps,
765 kPeerALPSTag) ||
766 !ret->peer_application_settings.CopyFrom(settings) ||
David Benjaminf1050fd2016-12-13 20:05:36 -0500767 CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400768 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400769 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400770 }
Nick Harper6bfd25c2020-03-30 17:15:19 -0700771 ret->is_quic = is_quic;
Adam Langley95c29f32014-06-20 12:00:00 -0700772
Steven Valdez51607f12020-08-05 10:46:05 -0400773 // The two ALPS values and ALPN must be consistent.
774 if (has_local_alps != has_peer_alps ||
775 (has_local_alps && ret->early_alpn.empty())) {
776 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
777 return nullptr;
778 }
779 ret->has_application_settings = has_local_alps;
780
David Benjamin4cce9552018-12-13 12:20:54 -0600781 if (!x509_method->session_cache_objects(ret.get())) {
782 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
783 return nullptr;
784 }
785
David Benjaminfd67aa82015-06-15 19:41:48 -0400786 return ret;
David Benjaminfd67aa82015-06-15 19:41:48 -0400787}
788
David Benjamin6e723e52023-03-29 19:28:46 +0900789bool ssl_session_serialize(const SSL_SESSION *in, CBB *cbb) {
Adam Langleye8d24392018-01-25 15:49:37 -0800790 return SSL_SESSION_to_bytes_full(in, cbb, 0);
791}
792
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700793BSSL_NAMESPACE_END
David Benjamin86e95b82017-07-18 16:34:25 -0400794
795using namespace bssl;
796
797int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
798 size_t *out_len) {
799 if (in->not_resumable) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400800 // If the caller has an unresumable session, e.g. if |SSL_get_session| were
801 // called on a TLS 1.3 or False Started connection, serialize with a
802 // placeholder value so it is not accidentally deserialized into a resumable
803 // one.
David Benjamin86e95b82017-07-18 16:34:25 -0400804 static const char kNotResumableSession[] = "NOT RESUMABLE";
805
806 *out_len = strlen(kNotResumableSession);
David Benjamin3ba95862019-10-21 16:14:33 -0400807 *out_data = (uint8_t *)OPENSSL_memdup(kNotResumableSession, *out_len);
David Benjamin86e95b82017-07-18 16:34:25 -0400808 if (*out_data == NULL) {
809 return 0;
810 }
811
812 return 1;
813 }
814
Adam Langleye8d24392018-01-25 15:49:37 -0800815 ScopedCBB cbb;
816 if (!CBB_init(cbb.get(), 256) ||
817 !SSL_SESSION_to_bytes_full(in, cbb.get(), 0) ||
818 !CBB_finish(cbb.get(), out_data, out_len)) {
819 return 0;
820 }
821
822 return 1;
David Benjamin86e95b82017-07-18 16:34:25 -0400823}
824
825int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
826 size_t *out_len) {
Adam Langleye8d24392018-01-25 15:49:37 -0800827 ScopedCBB cbb;
828 if (!CBB_init(cbb.get(), 256) ||
829 !SSL_SESSION_to_bytes_full(in, cbb.get(), 1) ||
830 !CBB_finish(cbb.get(), out_data, out_len)) {
831 return 0;
832 }
833
834 return 1;
David Benjamin86e95b82017-07-18 16:34:25 -0400835}
836
837int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
838 uint8_t *out;
839 size_t len;
840
841 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
842 return -1;
843 }
844
845 if (len > INT_MAX) {
846 OPENSSL_free(out);
847 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
848 return -1;
849 }
850
851 if (pp) {
852 OPENSSL_memcpy(*pp, out, len);
853 *pp += len;
854 }
855 OPENSSL_free(out);
856
857 return len;
858}
859
Adam Langley46db7af2017-02-01 15:49:37 -0800860SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len,
861 const SSL_CTX *ctx) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400862 CBS cbs;
863 CBS_init(&cbs, in, in_len);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400864 UniquePtr<SSL_SESSION> ret =
865 SSL_SESSION_parse(&cbs, ctx->x509_method, ctx->pool);
866 if (!ret) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400867 return NULL;
868 }
869 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400870 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400871 return NULL;
872 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400873 return ret.release();
David Benjaminfd67aa82015-06-15 19:41:48 -0400874}