blob: 73908db1e1acdc86afb7767dd9a08e174b03e756 [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 Benjaminc11ea9422017-08-29 16:33:21 -040083// Per C99, various stdint.h macros are unavailable in C++ unless some macros
84// are defined. C++11 overruled this decision, but older Android NDKs still
85// require it.
David Benjamine8703a32017-07-09 16:17:55 -040086#if !defined(__STDC_LIMIT_MACROS)
87#define __STDC_LIMIT_MACROS
88#endif
89
David Benjamin9e4e01e2015-09-15 01:48:04 -040090#include <openssl/ssl.h>
91
David Benjamin83fd6b62014-10-19 04:33:38 -040092#include <limits.h>
David Benjamin89abaea2014-10-19 13:50:18 -040093#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070094
David Benjaminee910bf2017-07-25 22:36:00 -040095#include <utility>
96
Steven Valdez84b5c002016-08-25 16:30:58 -040097#include <openssl/buf.h>
David Benjamin83fd6b62014-10-19 04:33:38 -040098#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -070099#include <openssl/err.h>
Brian Smith054e6822015-03-27 21:12:01 -1000100#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700101#include <openssl/x509.h>
102
David Benjamin17cf2cb2016-12-13 01:07:13 -0500103#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400104#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700105
Adam Langley95c29f32014-06-20 12:00:00 -0700106
David Benjamin86e95b82017-07-18 16:34:25 -0400107namespace bssl {
108
David Benjaminc11ea9422017-08-29 16:33:21 -0400109// An SSL_SESSION is serialized as the following ASN.1 structure:
110//
111// SSLSession ::= SEQUENCE {
112// version INTEGER (1), -- session structure version
113// sslVersion INTEGER, -- protocol version number
114// cipher OCTET STRING, -- two bytes long
115// sessionID OCTET STRING,
116// masterKey OCTET STRING,
117// time [1] INTEGER, -- seconds since UNIX epoch
118// timeout [2] INTEGER, -- in seconds
119// peer [3] Certificate OPTIONAL,
120// sessionIDContext [4] OCTET STRING OPTIONAL,
121// verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
122// hostName [6] OCTET STRING OPTIONAL,
123// -- from server_name extension
124// pskIdentity [8] OCTET STRING OPTIONAL,
125// ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
126// ticket [10] OCTET STRING OPTIONAL, -- client-only
127// peerSHA256 [13] OCTET STRING OPTIONAL,
128// originalHandshakeHash [14] OCTET STRING OPTIONAL,
129// signedCertTimestampList [15] OCTET STRING OPTIONAL,
130// -- contents of SCT extension
131// ocspResponse [16] OCTET STRING OPTIONAL,
132// -- stapled OCSP response from the server
133// extendedMasterSecret [17] BOOLEAN OPTIONAL,
134// groupID [18] INTEGER OPTIONAL,
135// certChain [19] SEQUENCE OF Certificate OPTIONAL,
136// ticketAgeAdd [21] OCTET STRING OPTIONAL,
137// isServer [22] BOOLEAN DEFAULT TRUE,
138// peerSignatureAlgorithm [23] INTEGER OPTIONAL,
139// ticketMaxEarlyData [24] INTEGER OPTIONAL,
140// authTimeout [25] INTEGER OPTIONAL, -- defaults to timeout
141// earlyALPN [26] OCTET STRING OPTIONAL,
142// }
143//
144// Note: historically this serialization has included other optional
145// fields. Their presence is currently treated as a parse error:
146//
147// keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
148// pskIdentityHint [7] OCTET STRING OPTIONAL,
149// compressionMethod [11] OCTET STRING OPTIONAL,
150// srpUsername [12] OCTET STRING OPTIONAL,
151// ticketFlags [20] INTEGER OPTIONAL,
David Benjamin83fd6b62014-10-19 04:33:38 -0400152
Adam Langley96c2a282015-06-02 14:16:44 -0700153static const unsigned kVersion = 1;
David Benjamin7f393f72015-10-17 12:36:12 -0400154
David Benjamin83fd6b62014-10-19 04:33:38 -0400155static const int kTimeTag =
156 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
157static const int kTimeoutTag =
158 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
159static const int kPeerTag =
160 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
David Benjamin7f393f72015-10-17 12:36:12 -0400161static const int kSessionIDContextTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400162 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
163static const int kVerifyResultTag =
164 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
165static const int kHostNameTag =
166 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
David Benjamin83fd6b62014-10-19 04:33:38 -0400167static const int kPSKIdentityTag =
168 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
169static const int kTicketLifetimeHintTag =
170 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
171static const int kTicketTag =
172 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
173static const int kPeerSHA256Tag =
174 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
175static const int kOriginalHandshakeHashTag =
176 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
177static const int kSignedCertTimestampListTag =
178 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
179static const int kOCSPResponseTag =
180 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
Adam Langley75712922014-10-10 16:23:43 -0700181static const int kExtendedMasterSecretTag =
182 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
David Benjamin4882a6c2016-12-11 02:48:12 -0500183static const int kGroupIDTag =
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200184 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
David Benjamin26416e92015-08-22 16:04:17 -0400185static const int kCertChainTag =
186 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400187static const int kTicketAgeAddTag =
188 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21;
Adam Langley364f7a62016-12-12 10:51:00 -0800189static const int kIsServerTag =
190 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22;
David Benjaminf1050fd2016-12-13 20:05:36 -0500191static const int kPeerSignatureAlgorithmTag =
192 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 23;
Steven Valdez08b65f42016-12-07 15:29:45 -0500193static const int kTicketMaxEarlyDataTag =
194 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 24;
David Benjamin17b30832017-01-28 14:00:32 -0500195static const int kAuthTimeoutTag =
196 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 25;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500197static const int kEarlyALPNTag =
198 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 26;
David Benjamin26416e92015-08-22 16:04:17 -0400199
David Benjamin14e2b502015-09-13 14:48:12 -0400200static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400201 size_t *out_len, int for_ticket) {
David Benjaminf3a8b122014-12-25 23:11:49 -0500202 if (in == NULL || in->cipher == NULL) {
David Benjamin89abaea2014-10-19 13:50:18 -0400203 return 0;
204 }
Adam Langley95c29f32014-06-20 12:00:00 -0700205
David Benjamin1386aad2017-07-19 23:57:40 -0400206 ScopedCBB cbb;
207 CBB session, child, child2;
208 if (!CBB_init(cbb.get(), 0) ||
209 !CBB_add_asn1(cbb.get(), &session, CBS_ASN1_SEQUENCE) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400210 !CBB_add_asn1_uint64(&session, kVersion) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400211 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
212 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminf3a8b122014-12-25 23:11:49 -0500213 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400214 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400215 // The session ID is irrelevant for a session ticket.
David Benjamin3cac4502014-10-21 01:46:30 -0400216 !CBB_add_bytes(&child, in->session_id,
217 for_ticket ? 0 : in->session_id_length) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400218 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500219 !CBB_add_bytes(&child, in->master_key, in->master_key_length) ||
220 !CBB_add_asn1(&session, &child, kTimeTag) ||
221 !CBB_add_asn1_uint64(&child, in->time) ||
222 !CBB_add_asn1(&session, &child, kTimeoutTag) ||
David Benjamin5b7b09c2016-11-03 20:32:10 -0400223 !CBB_add_asn1_uint64(&child, in->timeout)) {
224 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400225 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400226 }
Adam Langley95c29f32014-06-20 12:00:00 -0700227
David Benjaminc11ea9422017-08-29 16:33:21 -0400228 // The peer certificate is only serialized if the SHA-256 isn't
229 // serialized instead.
Adam Langley68e71242016-12-12 11:06:16 -0800230 if (sk_CRYPTO_BUFFER_num(in->certs) > 0 && !in->peer_sha256_valid) {
231 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, 0);
232 if (!CBB_add_asn1(&session, &child, kPeerTag) ||
233 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
234 CRYPTO_BUFFER_len(buffer))) {
David Benjamin3570d732015-06-29 00:28:17 -0400235 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
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) ||
243 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
244 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400245 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400246 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400247 }
Adam Langley95c29f32014-06-20 12:00:00 -0700248
David Benjamin89abaea2014-10-19 13:50:18 -0400249 if (in->verify_result != X509_V_OK) {
250 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
251 !CBB_add_asn1_uint64(&child, in->verify_result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400252 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400253 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400254 }
255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
David Benjamin89abaea2014-10-19 13:50:18 -0400257 if (in->tlsext_hostname) {
258 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
259 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
260 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
261 strlen(in->tlsext_hostname))) {
David Benjamin3570d732015-06-29 00:28:17 -0400262 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400263 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400264 }
265 }
Adam Langley95c29f32014-06-20 12:00:00 -0700266
David Benjamin89abaea2014-10-19 13:50:18 -0400267 if (in->psk_identity) {
268 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
269 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
270 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
271 strlen(in->psk_identity))) {
David Benjamin3570d732015-06-29 00:28:17 -0400272 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400273 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400274 }
275 }
Adam Langley95c29f32014-06-20 12:00:00 -0700276
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700277 if (in->tlsext_tick_lifetime_hint > 0) {
David Benjamin89abaea2014-10-19 13:50:18 -0400278 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700279 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400280 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400281 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400282 }
283 }
Adam Langley95c29f32014-06-20 12:00:00 -0700284
David Benjamin95d31822015-06-15 19:53:32 -0400285 if (in->tlsext_tick && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400286 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
287 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
288 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -0400289 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400290 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400291 }
292 }
Adam Langley75872532014-06-20 12:00:00 -0700293
David Benjamin89abaea2014-10-19 13:50:18 -0400294 if (in->peer_sha256_valid) {
295 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
296 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
297 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
David Benjamin3570d732015-06-29 00:28:17 -0400298 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400299 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400300 }
301 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700302
David Benjamin89abaea2014-10-19 13:50:18 -0400303 if (in->original_handshake_hash_len > 0) {
304 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
305 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
306 !CBB_add_bytes(&child2, in->original_handshake_hash,
307 in->original_handshake_hash_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400308 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400309 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400310 }
311 }
Adam Langley95c29f32014-06-20 12:00:00 -0700312
David Benjamin8fc2dc02017-08-22 15:07:51 -0700313 if (in->signed_cert_timestamp_list != nullptr) {
David Benjamin89abaea2014-10-19 13:50:18 -0400314 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
315 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
David Benjamin8fc2dc02017-08-22 15:07:51 -0700316 !CBB_add_bytes(&child2,
317 CRYPTO_BUFFER_data(in->signed_cert_timestamp_list),
318 CRYPTO_BUFFER_len(in->signed_cert_timestamp_list))) {
David Benjamin3570d732015-06-29 00:28:17 -0400319 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400320 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400321 }
322 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200323
David Benjamin8fc2dc02017-08-22 15:07:51 -0700324 if (in->ocsp_response != nullptr) {
David Benjamin89abaea2014-10-19 13:50:18 -0400325 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
326 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
David Benjamin8fc2dc02017-08-22 15:07:51 -0700327 !CBB_add_bytes(&child2, CRYPTO_BUFFER_data(in->ocsp_response),
328 CRYPTO_BUFFER_len(in->ocsp_response))) {
David Benjamin3570d732015-06-29 00:28:17 -0400329 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400330 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400331 }
332 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400333
Adam Langley75712922014-10-10 16:23:43 -0700334 if (in->extended_master_secret) {
335 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
336 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
337 !CBB_add_u8(&child2, 0xff)) {
David Benjamin3570d732015-06-29 00:28:17 -0400338 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400339 return 0;
Adam Langley75712922014-10-10 16:23:43 -0700340 }
341 }
342
David Benjamin4882a6c2016-12-11 02:48:12 -0500343 if (in->group_id > 0 &&
344 (!CBB_add_asn1(&session, &child, kGroupIDTag) ||
345 !CBB_add_asn1_uint64(&child, in->group_id))) {
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200346 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400347 return 0;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200348 }
349
David Benjaminc11ea9422017-08-29 16:33:21 -0400350 // The certificate chain is only serialized if the leaf's SHA-256 isn't
351 // serialized instead.
Adam Langley68e71242016-12-12 11:06:16 -0800352 if (in->certs != NULL &&
353 !in->peer_sha256_valid &&
354 sk_CRYPTO_BUFFER_num(in->certs) >= 2) {
David Benjamin26416e92015-08-22 16:04:17 -0400355 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
356 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400357 return 0;
David Benjamin26416e92015-08-22 16:04:17 -0400358 }
Adam Langley68e71242016-12-12 11:06:16 -0800359 for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs); i++) {
360 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, i);
361 if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
362 CRYPTO_BUFFER_len(buffer))) {
363 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400364 return 0;
David Benjamin26416e92015-08-22 16:04:17 -0400365 }
366 }
367 }
368
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400369 if (in->ticket_age_add_valid) {
370 if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
371 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
372 !CBB_add_u32(&child2, in->ticket_age_add)) {
373 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400374 return 0;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400375 }
376 }
377
Adam Langley364f7a62016-12-12 10:51:00 -0800378 if (!in->is_server) {
379 if (!CBB_add_asn1(&session, &child, kIsServerTag) ||
380 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
381 !CBB_add_u8(&child2, 0x00)) {
382 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400383 return 0;
Adam Langley364f7a62016-12-12 10:51:00 -0800384 }
385 }
386
David Benjaminf1050fd2016-12-13 20:05:36 -0500387 if (in->peer_signature_algorithm != 0 &&
388 (!CBB_add_asn1(&session, &child, kPeerSignatureAlgorithmTag) ||
389 !CBB_add_asn1_uint64(&child, in->peer_signature_algorithm))) {
390 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400391 return 0;
David Benjaminf1050fd2016-12-13 20:05:36 -0500392 }
393
Steven Valdez08b65f42016-12-07 15:29:45 -0500394 if (in->ticket_max_early_data != 0 &&
395 (!CBB_add_asn1(&session, &child, kTicketMaxEarlyDataTag) ||
396 !CBB_add_asn1_uint64(&child, in->ticket_max_early_data))) {
397 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400398 return 0;
Steven Valdez08b65f42016-12-07 15:29:45 -0500399 }
400
David Benjamin17b30832017-01-28 14:00:32 -0500401 if (in->timeout != in->auth_timeout &&
402 (!CBB_add_asn1(&session, &child, kAuthTimeoutTag) ||
403 !CBB_add_asn1_uint64(&child, in->auth_timeout))) {
404 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400405 return 0;
David Benjamin17b30832017-01-28 14:00:32 -0500406 }
407
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500408 if (in->early_alpn) {
409 if (!CBB_add_asn1(&session, &child, kEarlyALPNTag) ||
410 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
411 !CBB_add_bytes(&child2, (const uint8_t *)in->early_alpn,
412 in->early_alpn_len)) {
413 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400414 return 0;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500415 }
416 }
417
David Benjamin1386aad2017-07-19 23:57:40 -0400418 if (!CBB_finish(cbb.get(), out_data, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400419 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400420 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400421 }
David Benjamin3cac4502014-10-21 01:46:30 -0400422 return 1;
David Benjamin3cac4502014-10-21 01:46:30 -0400423}
424
David Benjaminc11ea9422017-08-29 16:33:21 -0400425// SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
426// explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
427// entry, if |*out| is not NULL, it frees the existing contents. If
428// the element was not found, it sets |*out| to NULL. It returns one
429// on success, whether or not the element was found, and zero on
430// decode error.
David Benjaminfd67aa82015-06-15 19:41:48 -0400431static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400432 CBS value;
433 int present;
434 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400435 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400436 return 0;
437 }
438 if (present) {
439 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400440 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400441 return 0;
442 }
443 if (!CBS_strdup(&value, out)) {
David Benjamin3570d732015-06-29 00:28:17 -0400444 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400445 return 0;
446 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400447 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400448 OPENSSL_free(*out);
449 *out = NULL;
450 }
451 return 1;
452}
Adam Langley95c29f32014-06-20 12:00:00 -0700453
David Benjaminc11ea9422017-08-29 16:33:21 -0400454// SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
455// explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
456// and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
457// contents. On entry, if the element was not found, it sets
458// |*out_ptr| to NULL. It returns one on success, whether or not the
459// element was found, and zero on decode error.
David Benjaminfd67aa82015-06-15 19:41:48 -0400460static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin8be79a32015-08-23 01:37:36 -0400461 size_t *out_len, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400462 CBS value;
463 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400464 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400465 return 0;
466 }
467 if (!CBS_stow(&value, out_ptr, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400468 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400469 return 0;
470 }
471 return 1;
472}
Adam Langley95c29f32014-06-20 12:00:00 -0700473
David Benjamin8fc2dc02017-08-22 15:07:51 -0700474static int SSL_SESSION_parse_crypto_buffer(CBS *cbs, CRYPTO_BUFFER **out,
475 unsigned tag,
476 CRYPTO_BUFFER_POOL *pool) {
477 if (!CBS_peek_asn1_tag(cbs, tag)) {
478 return 1;
479 }
480
481 CBS child, value;
482 if (!CBS_get_asn1(cbs, &child, tag) ||
483 !CBS_get_asn1(&child, &value, CBS_ASN1_OCTETSTRING) ||
484 CBS_len(&child) != 0) {
485 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
486 return 0;
487 }
488 CRYPTO_BUFFER_free(*out);
489 *out = CRYPTO_BUFFER_new_from_CBS(&value, pool);
490 if (*out == nullptr) {
491 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
492 return 0;
493 }
494 return 1;
495}
496
David Benjaminc11ea9422017-08-29 16:33:21 -0400497// SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
498// explicitly tagged with |tag| of size at most |max_out|.
David Benjamin8be79a32015-08-23 01:37:36 -0400499static int SSL_SESSION_parse_bounded_octet_string(
David Benjamin30c4c302016-12-07 22:35:24 -0500500 CBS *cbs, uint8_t *out, uint8_t *out_len, uint8_t max_out, unsigned tag) {
David Benjamin8be79a32015-08-23 01:37:36 -0400501 CBS value;
502 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
503 CBS_len(&value) > max_out) {
504 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
505 return 0;
506 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500507 OPENSSL_memcpy(out, CBS_data(&value), CBS_len(&value));
David Benjamin30c4c302016-12-07 22:35:24 -0500508 *out_len = (uint8_t)CBS_len(&value);
David Benjamin8be79a32015-08-23 01:37:36 -0400509 return 1;
510}
Adam Langley95c29f32014-06-20 12:00:00 -0700511
David Benjamin8be79a32015-08-23 01:37:36 -0400512static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
513 long default_value) {
514 uint64_t value;
515 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
516 (uint64_t)default_value) ||
517 value > LONG_MAX) {
518 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
519 return 0;
520 }
521 *out = (long)value;
522 return 1;
523}
524
525static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
526 uint32_t default_value) {
527 uint64_t value;
528 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
529 (uint64_t)default_value) ||
530 value > 0xffffffff) {
531 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
532 return 0;
533 }
534 *out = (uint32_t)value;
535 return 1;
536}
537
David Benjaminf1050fd2016-12-13 20:05:36 -0500538static int SSL_SESSION_parse_u16(CBS *cbs, uint16_t *out, unsigned tag,
539 uint16_t default_value) {
540 uint64_t value;
541 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
542 (uint64_t)default_value) ||
543 value > 0xffff) {
544 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
545 return 0;
546 }
547 *out = (uint16_t)value;
548 return 1;
549}
550
David Benjamin31b0c9b2017-07-20 14:49:15 -0400551UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs,
552 const SSL_X509_METHOD *x509_method,
553 CRYPTO_BUFFER_POOL *pool) {
554 UniquePtr<SSL_SESSION> ret = ssl_session_new(x509_method);
555 if (!ret) {
556 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400557 }
Adam Langley95c29f32014-06-20 12:00:00 -0700558
David Benjamin8be79a32015-08-23 01:37:36 -0400559 CBS session;
560 uint64_t version, ssl_version;
Steven Valdez8f36c512017-06-20 10:55:02 -0400561 uint16_t unused;
David Benjaminfd67aa82015-06-15 19:41:48 -0400562 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400563 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400564 version != kVersion ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400565 !CBS_get_asn1_uint64(&session, &ssl_version) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400566 // Require sessions have versions valid in either TLS or DTLS. The session
567 // will not be used by the handshake if not applicable, but, for
568 // simplicity, never parse a session that does not pass
569 // |ssl_protocol_version_from_wire|.
Steven Valdez8f36c512017-06-20 10:55:02 -0400570 ssl_version > UINT16_MAX ||
571 !ssl_protocol_version_from_wire(&unused, ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400572 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400573 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400574 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400575 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700576
David Benjamin8be79a32015-08-23 01:37:36 -0400577 CBS cipher;
David Benjaminf3a8b122014-12-25 23:11:49 -0500578 uint16_t cipher_value;
David Benjamin8be79a32015-08-23 01:37:36 -0400579 if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
580 !CBS_get_u16(&cipher, &cipher_value) ||
581 CBS_len(&cipher) != 0) {
582 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400583 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400584 }
David Benjamina1c90a52015-05-30 17:03:14 -0400585 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400586 if (ret->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400587 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400588 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400589 }
Adam Langley95c29f32014-06-20 12:00:00 -0700590
David Benjamin8be79a32015-08-23 01:37:36 -0400591 CBS session_id, master_key;
592 if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
593 CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
594 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
595 CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
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 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500599 OPENSSL_memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
David Benjamin83fd6b62014-10-19 04:33:38 -0400600 ret->session_id_length = CBS_len(&session_id);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500601 OPENSSL_memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
David Benjamin83fd6b62014-10-19 04:33:38 -0400602 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700603
David Benjamin5b7b09c2016-11-03 20:32:10 -0400604 CBS child;
David Benjaminad8f5e12017-02-20 17:00:20 -0500605 uint64_t timeout;
David Benjamin5b7b09c2016-11-03 20:32:10 -0400606 if (!CBS_get_asn1(&session, &child, kTimeTag) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500607 !CBS_get_asn1_uint64(&child, &ret->time) ||
David Benjamin5b7b09c2016-11-03 20:32:10 -0400608 !CBS_get_asn1(&session, &child, kTimeoutTag) ||
609 !CBS_get_asn1_uint64(&child, &timeout) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500610 timeout > UINT32_MAX) {
David Benjamin3570d732015-06-29 00:28:17 -0400611 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400612 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400613 }
Adam Langley95c29f32014-06-20 12:00:00 -0700614
David Benjaminad8f5e12017-02-20 17:00:20 -0500615 ret->timeout = (uint32_t)timeout;
David Benjamin5b7b09c2016-11-03 20:32:10 -0400616
David Benjamin8be79a32015-08-23 01:37:36 -0400617 CBS peer;
618 int has_peer;
Adam Langley68e71242016-12-12 11:06:16 -0800619 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
620 (has_peer && CBS_len(&peer) == 0)) {
David Benjamin8be79a32015-08-23 01:37:36 -0400621 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400622 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400623 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400624 // |peer| is processed with the certificate chain.
Adam Langley95c29f32014-06-20 12:00:00 -0700625
David Benjamin8be79a32015-08-23 01:37:36 -0400626 if (!SSL_SESSION_parse_bounded_octet_string(
627 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
628 kSessionIDContextTag) ||
629 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
630 X509_V_OK) ||
631 !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
632 kHostNameTag) ||
633 !SSL_SESSION_parse_string(&session, &ret->psk_identity,
634 kPSKIdentityTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700635 !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
David Benjamin8be79a32015-08-23 01:37:36 -0400636 kTicketLifetimeHintTag, 0) ||
637 !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
638 &ret->tlsext_ticklen, kTicketTag)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400639 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400640 }
Adam Langley95c29f32014-06-20 12:00:00 -0700641
David Benjamin8be79a32015-08-23 01:37:36 -0400642 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
David Benjamin5b7b09c2016-11-03 20:32:10 -0400643 CBS peer_sha256;
David Benjamin8be79a32015-08-23 01:37:36 -0400644 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
645 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
646 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
647 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400648 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400649 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400650 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500651 OPENSSL_memcpy(ret->peer_sha256, CBS_data(&peer_sha256),
652 sizeof(ret->peer_sha256));
David Benjamin83fd6b62014-10-19 04:33:38 -0400653 ret->peer_sha256_valid = 1;
654 } else {
655 ret->peer_sha256_valid = 0;
656 }
Adam Langley95c29f32014-06-20 12:00:00 -0700657
David Benjamin8be79a32015-08-23 01:37:36 -0400658 if (!SSL_SESSION_parse_bounded_octet_string(
659 &session, ret->original_handshake_hash,
660 &ret->original_handshake_hash_len,
661 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
David Benjamin8fc2dc02017-08-22 15:07:51 -0700662 !SSL_SESSION_parse_crypto_buffer(&session,
663 &ret->signed_cert_timestamp_list,
664 kSignedCertTimestampListTag, pool) ||
665 !SSL_SESSION_parse_crypto_buffer(&session, &ret->ocsp_response,
666 kOCSPResponseTag, pool)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400667 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400668 }
669
670 int extended_master_secret;
671 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
672 kExtendedMasterSecretTag,
673 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400674 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400675 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400676 }
David Benjamin8be79a32015-08-23 01:37:36 -0400677 ret->extended_master_secret = !!extended_master_secret;
678
David Benjamin11fa7032017-04-11 22:13:45 -0400679 if (!SSL_SESSION_parse_u16(&session, &ret->group_id, kGroupIDTag, 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400680 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400681 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400682 }
683
684 CBS cert_chain;
Adam Langley68e71242016-12-12 11:06:16 -0800685 CBS_init(&cert_chain, NULL, 0);
David Benjamin26416e92015-08-22 16:04:17 -0400686 int has_cert_chain;
687 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
Adam Langley68e71242016-12-12 11:06:16 -0800688 kCertChainTag) ||
689 (has_cert_chain && CBS_len(&cert_chain) == 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400690 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400691 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400692 }
Adam Langley68e71242016-12-12 11:06:16 -0800693 if (has_cert_chain && !has_peer) {
694 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400695 return nullptr;
Adam Langley364f7a62016-12-12 10:51:00 -0800696 }
Adam Langley68e71242016-12-12 11:06:16 -0800697 if (has_peer || has_cert_chain) {
698 ret->certs = sk_CRYPTO_BUFFER_new_null();
699 if (ret->certs == NULL) {
David Benjamin26416e92015-08-22 16:04:17 -0400700 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400701 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400702 }
Adam Langley68e71242016-12-12 11:06:16 -0800703
704 if (has_peer) {
David Benjaminee910bf2017-07-25 22:36:00 -0400705 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new_from_CBS(&peer, pool));
706 if (!buffer ||
707 !PushToStack(ret->certs, std::move(buffer))) {
Adam Langleye8509092016-11-07 14:24:33 -0800708 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400709 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400710 }
711 }
Adam Langley68e71242016-12-12 11:06:16 -0800712
713 while (CBS_len(&cert_chain) > 0) {
714 CBS cert;
715 if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) ||
716 CBS_len(&cert) == 0) {
717 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400718 return nullptr;
Adam Langley68e71242016-12-12 11:06:16 -0800719 }
720
Adam Langley46db7af2017-02-01 15:49:37 -0800721 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800722 if (buffer == NULL ||
723 !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
724 CRYPTO_BUFFER_free(buffer);
725 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400726 return nullptr;
Adam Langley68e71242016-12-12 11:06:16 -0800727 }
728 }
729 }
730
David Benjamin31b0c9b2017-07-20 14:49:15 -0400731 if (!x509_method->session_cache_objects(ret.get())) {
Adam Langley68e71242016-12-12 11:06:16 -0800732 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400733 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400734 }
735
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400736 CBS age_add;
737 int age_add_present;
Steven Valdez5b986082016-09-01 12:29:49 -0400738 if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400739 kTicketAgeAddTag) ||
740 (age_add_present &&
741 !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
742 CBS_len(&age_add) != 0) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400743 return nullptr;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400744 }
745 ret->ticket_age_add_valid = age_add_present;
746
Adam Langley364f7a62016-12-12 10:51:00 -0800747 int is_server;
748 if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag,
749 1 /* default to true */)) {
750 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400751 return nullptr;
Adam Langley364f7a62016-12-12 10:51:00 -0800752 }
753 /* TODO: in time we can include |is_server| for servers too, then we can
754 enforce that client and server sessions are never mixed up. */
755
756 ret->is_server = is_server;
757
David Benjaminf1050fd2016-12-13 20:05:36 -0500758 if (!SSL_SESSION_parse_u16(&session, &ret->peer_signature_algorithm,
759 kPeerSignatureAlgorithmTag, 0) ||
Steven Valdez08b65f42016-12-07 15:29:45 -0500760 !SSL_SESSION_parse_u32(&session, &ret->ticket_max_early_data,
761 kTicketMaxEarlyDataTag, 0) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500762 !SSL_SESSION_parse_u32(&session, &ret->auth_timeout, kAuthTimeoutTag,
763 ret->timeout) ||
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500764 !SSL_SESSION_parse_octet_string(&session, &ret->early_alpn,
765 &ret->early_alpn_len, kEarlyALPNTag) ||
David Benjaminf1050fd2016-12-13 20:05:36 -0500766 CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400767 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400768 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400769 }
Adam Langley95c29f32014-06-20 12:00:00 -0700770
David Benjaminfd67aa82015-06-15 19:41:48 -0400771 return ret;
David Benjaminfd67aa82015-06-15 19:41:48 -0400772}
773
David Benjamin86e95b82017-07-18 16:34:25 -0400774} // namespace bssl
775
776using namespace bssl;
777
778int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
779 size_t *out_len) {
780 if (in->not_resumable) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400781 // If the caller has an unresumable session, e.g. if |SSL_get_session| were
782 // called on a TLS 1.3 or False Started connection, serialize with a
783 // placeholder value so it is not accidentally deserialized into a resumable
784 // one.
David Benjamin86e95b82017-07-18 16:34:25 -0400785 static const char kNotResumableSession[] = "NOT RESUMABLE";
786
787 *out_len = strlen(kNotResumableSession);
788 *out_data = (uint8_t *)BUF_memdup(kNotResumableSession, *out_len);
789 if (*out_data == NULL) {
790 return 0;
791 }
792
793 return 1;
794 }
795
796 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
797}
798
799int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
800 size_t *out_len) {
801 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
802}
803
804int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
805 uint8_t *out;
806 size_t len;
807
808 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
809 return -1;
810 }
811
812 if (len > INT_MAX) {
813 OPENSSL_free(out);
814 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
815 return -1;
816 }
817
818 if (pp) {
819 OPENSSL_memcpy(*pp, out, len);
820 *pp += len;
821 }
822 OPENSSL_free(out);
823
824 return len;
825}
826
Adam Langley46db7af2017-02-01 15:49:37 -0800827SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len,
828 const SSL_CTX *ctx) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400829 CBS cbs;
830 CBS_init(&cbs, in, in_len);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400831 UniquePtr<SSL_SESSION> ret =
832 SSL_SESSION_parse(&cbs, ctx->x509_method, ctx->pool);
833 if (!ret) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400834 return NULL;
835 }
836 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400837 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400838 return NULL;
839 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400840 return ret.release();
David Benjaminfd67aa82015-06-15 19:41:48 -0400841}