blob: 7bcfdd73a547a1fb69cc62a1de2af97d580c9b61 [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
David Benjaminc11ea9422017-08-29 16:33:21 -0400122// pskIdentity [8] OCTET STRING OPTIONAL,
123// ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
124// ticket [10] OCTET STRING OPTIONAL, -- client-only
125// peerSHA256 [13] OCTET STRING OPTIONAL,
126// originalHandshakeHash [14] OCTET STRING OPTIONAL,
127// signedCertTimestampList [15] OCTET STRING OPTIONAL,
128// -- contents of SCT extension
129// ocspResponse [16] OCTET STRING OPTIONAL,
130// -- stapled OCSP response from the server
131// extendedMasterSecret [17] BOOLEAN OPTIONAL,
132// groupID [18] INTEGER OPTIONAL,
133// certChain [19] SEQUENCE OF Certificate OPTIONAL,
134// ticketAgeAdd [21] OCTET STRING OPTIONAL,
135// isServer [22] BOOLEAN DEFAULT TRUE,
136// peerSignatureAlgorithm [23] INTEGER OPTIONAL,
137// ticketMaxEarlyData [24] INTEGER OPTIONAL,
138// authTimeout [25] INTEGER OPTIONAL, -- defaults to timeout
139// earlyALPN [26] OCTET STRING OPTIONAL,
140// }
141//
142// Note: historically this serialization has included other optional
David Benjamina8614602017-09-06 15:40:19 -0400143// fields. Their presence is currently treated as a parse error, except for
144// hostName, which is ignored.
David Benjaminc11ea9422017-08-29 16:33:21 -0400145//
146// keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
David Benjamina8614602017-09-06 15:40:19 -0400147// hostName [6] OCTET STRING OPTIONAL,
David Benjaminc11ea9422017-08-29 16:33:21 -0400148// 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->psk_identity) {
258 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
259 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
260 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
261 strlen(in->psk_identity))) {
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
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700267 if (in->tlsext_tick_lifetime_hint > 0) {
David Benjamin89abaea2014-10-19 13:50:18 -0400268 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700269 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400270 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400271 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400272 }
273 }
Adam Langley95c29f32014-06-20 12:00:00 -0700274
David Benjamin95d31822015-06-15 19:53:32 -0400275 if (in->tlsext_tick && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400276 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
277 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
278 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -0400279 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400280 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400281 }
282 }
Adam Langley75872532014-06-20 12:00:00 -0700283
David Benjamin89abaea2014-10-19 13:50:18 -0400284 if (in->peer_sha256_valid) {
285 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
286 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
287 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
David Benjamin3570d732015-06-29 00:28:17 -0400288 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400289 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400290 }
291 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700292
David Benjamin89abaea2014-10-19 13:50:18 -0400293 if (in->original_handshake_hash_len > 0) {
294 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
295 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
296 !CBB_add_bytes(&child2, in->original_handshake_hash,
297 in->original_handshake_hash_len)) {
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 Langley95c29f32014-06-20 12:00:00 -0700302
David Benjamin8fc2dc02017-08-22 15:07:51 -0700303 if (in->signed_cert_timestamp_list != nullptr) {
David Benjamin89abaea2014-10-19 13:50:18 -0400304 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
305 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
David Benjamin8fc2dc02017-08-22 15:07:51 -0700306 !CBB_add_bytes(&child2,
307 CRYPTO_BUFFER_data(in->signed_cert_timestamp_list),
308 CRYPTO_BUFFER_len(in->signed_cert_timestamp_list))) {
David Benjamin3570d732015-06-29 00:28:17 -0400309 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400310 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400311 }
312 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200313
David Benjamin8fc2dc02017-08-22 15:07:51 -0700314 if (in->ocsp_response != nullptr) {
David Benjamin89abaea2014-10-19 13:50:18 -0400315 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
316 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
David Benjamin8fc2dc02017-08-22 15:07:51 -0700317 !CBB_add_bytes(&child2, CRYPTO_BUFFER_data(in->ocsp_response),
318 CRYPTO_BUFFER_len(in->ocsp_response))) {
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 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400323
Adam Langley75712922014-10-10 16:23:43 -0700324 if (in->extended_master_secret) {
325 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
326 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
327 !CBB_add_u8(&child2, 0xff)) {
David Benjamin3570d732015-06-29 00:28:17 -0400328 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400329 return 0;
Adam Langley75712922014-10-10 16:23:43 -0700330 }
331 }
332
David Benjamin4882a6c2016-12-11 02:48:12 -0500333 if (in->group_id > 0 &&
334 (!CBB_add_asn1(&session, &child, kGroupIDTag) ||
335 !CBB_add_asn1_uint64(&child, in->group_id))) {
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200336 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400337 return 0;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200338 }
339
David Benjaminc11ea9422017-08-29 16:33:21 -0400340 // The certificate chain is only serialized if the leaf's SHA-256 isn't
341 // serialized instead.
Adam Langley68e71242016-12-12 11:06:16 -0800342 if (in->certs != NULL &&
343 !in->peer_sha256_valid &&
344 sk_CRYPTO_BUFFER_num(in->certs) >= 2) {
David Benjamin26416e92015-08-22 16:04:17 -0400345 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
346 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400347 return 0;
David Benjamin26416e92015-08-22 16:04:17 -0400348 }
Adam Langley68e71242016-12-12 11:06:16 -0800349 for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs); i++) {
350 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, i);
351 if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
352 CRYPTO_BUFFER_len(buffer))) {
353 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400354 return 0;
David Benjamin26416e92015-08-22 16:04:17 -0400355 }
356 }
357 }
358
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400359 if (in->ticket_age_add_valid) {
360 if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
361 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
362 !CBB_add_u32(&child2, in->ticket_age_add)) {
363 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400364 return 0;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400365 }
366 }
367
Adam Langley364f7a62016-12-12 10:51:00 -0800368 if (!in->is_server) {
369 if (!CBB_add_asn1(&session, &child, kIsServerTag) ||
370 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
371 !CBB_add_u8(&child2, 0x00)) {
372 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400373 return 0;
Adam Langley364f7a62016-12-12 10:51:00 -0800374 }
375 }
376
David Benjaminf1050fd2016-12-13 20:05:36 -0500377 if (in->peer_signature_algorithm != 0 &&
378 (!CBB_add_asn1(&session, &child, kPeerSignatureAlgorithmTag) ||
379 !CBB_add_asn1_uint64(&child, in->peer_signature_algorithm))) {
380 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400381 return 0;
David Benjaminf1050fd2016-12-13 20:05:36 -0500382 }
383
Steven Valdez08b65f42016-12-07 15:29:45 -0500384 if (in->ticket_max_early_data != 0 &&
385 (!CBB_add_asn1(&session, &child, kTicketMaxEarlyDataTag) ||
386 !CBB_add_asn1_uint64(&child, in->ticket_max_early_data))) {
387 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400388 return 0;
Steven Valdez08b65f42016-12-07 15:29:45 -0500389 }
390
David Benjamin17b30832017-01-28 14:00:32 -0500391 if (in->timeout != in->auth_timeout &&
392 (!CBB_add_asn1(&session, &child, kAuthTimeoutTag) ||
393 !CBB_add_asn1_uint64(&child, in->auth_timeout))) {
394 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400395 return 0;
David Benjamin17b30832017-01-28 14:00:32 -0500396 }
397
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500398 if (in->early_alpn) {
399 if (!CBB_add_asn1(&session, &child, kEarlyALPNTag) ||
400 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
401 !CBB_add_bytes(&child2, (const uint8_t *)in->early_alpn,
402 in->early_alpn_len)) {
403 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400404 return 0;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500405 }
406 }
407
David Benjamin1386aad2017-07-19 23:57:40 -0400408 if (!CBB_finish(cbb.get(), out_data, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400409 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400410 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400411 }
David Benjamin3cac4502014-10-21 01:46:30 -0400412 return 1;
David Benjamin3cac4502014-10-21 01:46:30 -0400413}
414
David Benjaminc11ea9422017-08-29 16:33:21 -0400415// SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
416// explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
417// entry, if |*out| is not NULL, it frees the existing contents. If
418// the element was not found, it sets |*out| to NULL. It returns one
419// on success, whether or not the element was found, and zero on
420// decode error.
David Benjaminfd67aa82015-06-15 19:41:48 -0400421static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400422 CBS value;
423 int present;
424 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400425 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400426 return 0;
427 }
428 if (present) {
429 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400430 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400431 return 0;
432 }
433 if (!CBS_strdup(&value, out)) {
David Benjamin3570d732015-06-29 00:28:17 -0400434 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400435 return 0;
436 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400437 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400438 OPENSSL_free(*out);
439 *out = NULL;
440 }
441 return 1;
442}
Adam Langley95c29f32014-06-20 12:00:00 -0700443
David Benjaminc11ea9422017-08-29 16:33:21 -0400444// SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
445// explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
446// and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
447// contents. On entry, if the element was not found, it sets
448// |*out_ptr| to NULL. It returns one on success, whether or not the
449// element was found, and zero on decode error.
David Benjaminfd67aa82015-06-15 19:41:48 -0400450static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin8be79a32015-08-23 01:37:36 -0400451 size_t *out_len, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400452 CBS value;
453 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400454 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400455 return 0;
456 }
457 if (!CBS_stow(&value, out_ptr, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400458 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400459 return 0;
460 }
461 return 1;
462}
Adam Langley95c29f32014-06-20 12:00:00 -0700463
David Benjamin8fc2dc02017-08-22 15:07:51 -0700464static int SSL_SESSION_parse_crypto_buffer(CBS *cbs, CRYPTO_BUFFER **out,
465 unsigned tag,
466 CRYPTO_BUFFER_POOL *pool) {
467 if (!CBS_peek_asn1_tag(cbs, tag)) {
468 return 1;
469 }
470
471 CBS child, value;
472 if (!CBS_get_asn1(cbs, &child, tag) ||
473 !CBS_get_asn1(&child, &value, CBS_ASN1_OCTETSTRING) ||
474 CBS_len(&child) != 0) {
475 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
476 return 0;
477 }
478 CRYPTO_BUFFER_free(*out);
479 *out = CRYPTO_BUFFER_new_from_CBS(&value, pool);
480 if (*out == nullptr) {
481 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
482 return 0;
483 }
484 return 1;
485}
486
David Benjaminc11ea9422017-08-29 16:33:21 -0400487// SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
488// explicitly tagged with |tag| of size at most |max_out|.
David Benjamin8be79a32015-08-23 01:37:36 -0400489static int SSL_SESSION_parse_bounded_octet_string(
David Benjamin30c4c302016-12-07 22:35:24 -0500490 CBS *cbs, uint8_t *out, uint8_t *out_len, uint8_t max_out, unsigned tag) {
David Benjamin8be79a32015-08-23 01:37:36 -0400491 CBS value;
492 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
493 CBS_len(&value) > max_out) {
494 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
495 return 0;
496 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500497 OPENSSL_memcpy(out, CBS_data(&value), CBS_len(&value));
David Benjamin30c4c302016-12-07 22:35:24 -0500498 *out_len = (uint8_t)CBS_len(&value);
David Benjamin8be79a32015-08-23 01:37:36 -0400499 return 1;
500}
Adam Langley95c29f32014-06-20 12:00:00 -0700501
David Benjamin8be79a32015-08-23 01:37:36 -0400502static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
503 long default_value) {
504 uint64_t value;
505 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
506 (uint64_t)default_value) ||
507 value > LONG_MAX) {
508 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
509 return 0;
510 }
511 *out = (long)value;
512 return 1;
513}
514
515static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
516 uint32_t default_value) {
517 uint64_t value;
518 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
519 (uint64_t)default_value) ||
520 value > 0xffffffff) {
521 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
522 return 0;
523 }
524 *out = (uint32_t)value;
525 return 1;
526}
527
David Benjaminf1050fd2016-12-13 20:05:36 -0500528static int SSL_SESSION_parse_u16(CBS *cbs, uint16_t *out, unsigned tag,
529 uint16_t default_value) {
530 uint64_t value;
531 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
532 (uint64_t)default_value) ||
533 value > 0xffff) {
534 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
535 return 0;
536 }
537 *out = (uint16_t)value;
538 return 1;
539}
540
David Benjamin31b0c9b2017-07-20 14:49:15 -0400541UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs,
542 const SSL_X509_METHOD *x509_method,
543 CRYPTO_BUFFER_POOL *pool) {
544 UniquePtr<SSL_SESSION> ret = ssl_session_new(x509_method);
545 if (!ret) {
546 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400547 }
Adam Langley95c29f32014-06-20 12:00:00 -0700548
David Benjamin8be79a32015-08-23 01:37:36 -0400549 CBS session;
550 uint64_t version, ssl_version;
Steven Valdez8f36c512017-06-20 10:55:02 -0400551 uint16_t unused;
David Benjaminfd67aa82015-06-15 19:41:48 -0400552 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400553 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400554 version != kVersion ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400555 !CBS_get_asn1_uint64(&session, &ssl_version) ||
David Benjaminc11ea9422017-08-29 16:33:21 -0400556 // Require sessions have versions valid in either TLS or DTLS. The session
557 // will not be used by the handshake if not applicable, but, for
558 // simplicity, never parse a session that does not pass
559 // |ssl_protocol_version_from_wire|.
Steven Valdez8f36c512017-06-20 10:55:02 -0400560 ssl_version > UINT16_MAX ||
561 !ssl_protocol_version_from_wire(&unused, ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400562 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400563 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400564 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400565 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700566
David Benjamin8be79a32015-08-23 01:37:36 -0400567 CBS cipher;
David Benjaminf3a8b122014-12-25 23:11:49 -0500568 uint16_t cipher_value;
David Benjamin8be79a32015-08-23 01:37:36 -0400569 if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
570 !CBS_get_u16(&cipher, &cipher_value) ||
571 CBS_len(&cipher) != 0) {
572 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 Benjamina1c90a52015-05-30 17:03:14 -0400575 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400576 if (ret->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400577 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400578 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400579 }
Adam Langley95c29f32014-06-20 12:00:00 -0700580
David Benjamin8be79a32015-08-23 01:37:36 -0400581 CBS session_id, master_key;
582 if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
583 CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
584 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
585 CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400586 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400587 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400588 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500589 OPENSSL_memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
David Benjamin83fd6b62014-10-19 04:33:38 -0400590 ret->session_id_length = CBS_len(&session_id);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500591 OPENSSL_memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
David Benjamin83fd6b62014-10-19 04:33:38 -0400592 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700593
David Benjamin5b7b09c2016-11-03 20:32:10 -0400594 CBS child;
David Benjaminad8f5e12017-02-20 17:00:20 -0500595 uint64_t timeout;
David Benjamin5b7b09c2016-11-03 20:32:10 -0400596 if (!CBS_get_asn1(&session, &child, kTimeTag) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500597 !CBS_get_asn1_uint64(&child, &ret->time) ||
David Benjamin5b7b09c2016-11-03 20:32:10 -0400598 !CBS_get_asn1(&session, &child, kTimeoutTag) ||
599 !CBS_get_asn1_uint64(&child, &timeout) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500600 timeout > UINT32_MAX) {
David Benjamin3570d732015-06-29 00:28:17 -0400601 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400602 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400603 }
Adam Langley95c29f32014-06-20 12:00:00 -0700604
David Benjaminad8f5e12017-02-20 17:00:20 -0500605 ret->timeout = (uint32_t)timeout;
David Benjamin5b7b09c2016-11-03 20:32:10 -0400606
David Benjamin8be79a32015-08-23 01:37:36 -0400607 CBS peer;
608 int has_peer;
Adam Langley68e71242016-12-12 11:06:16 -0800609 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
610 (has_peer && CBS_len(&peer) == 0)) {
David Benjamin8be79a32015-08-23 01:37:36 -0400611 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400612 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400613 }
David Benjaminc11ea9422017-08-29 16:33:21 -0400614 // |peer| is processed with the certificate chain.
Adam Langley95c29f32014-06-20 12:00:00 -0700615
David Benjamin8be79a32015-08-23 01:37:36 -0400616 if (!SSL_SESSION_parse_bounded_octet_string(
617 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
618 kSessionIDContextTag) ||
619 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
David Benjamina8614602017-09-06 15:40:19 -0400620 X509_V_OK)) {
621 return nullptr;
622 }
623
624 // Skip the historical hostName field.
625 CBS unused_hostname;
626 if (!CBS_get_optional_asn1(&session, &unused_hostname, nullptr,
627 kHostNameTag)) {
628 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
629 return nullptr;
630 }
631
632 if (!SSL_SESSION_parse_string(&session, &ret->psk_identity,
David Benjamin8be79a32015-08-23 01:37:36 -0400633 kPSKIdentityTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700634 !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
David Benjamin8be79a32015-08-23 01:37:36 -0400635 kTicketLifetimeHintTag, 0) ||
636 !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
637 &ret->tlsext_ticklen, kTicketTag)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400638 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400639 }
Adam Langley95c29f32014-06-20 12:00:00 -0700640
David Benjamin8be79a32015-08-23 01:37:36 -0400641 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
David Benjamin5b7b09c2016-11-03 20:32:10 -0400642 CBS peer_sha256;
David Benjamin8be79a32015-08-23 01:37:36 -0400643 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
644 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
645 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
646 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400647 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400648 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400649 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500650 OPENSSL_memcpy(ret->peer_sha256, CBS_data(&peer_sha256),
651 sizeof(ret->peer_sha256));
David Benjamin83fd6b62014-10-19 04:33:38 -0400652 ret->peer_sha256_valid = 1;
653 } else {
654 ret->peer_sha256_valid = 0;
655 }
Adam Langley95c29f32014-06-20 12:00:00 -0700656
David Benjamin8be79a32015-08-23 01:37:36 -0400657 if (!SSL_SESSION_parse_bounded_octet_string(
658 &session, ret->original_handshake_hash,
659 &ret->original_handshake_hash_len,
660 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
David Benjamin8fc2dc02017-08-22 15:07:51 -0700661 !SSL_SESSION_parse_crypto_buffer(&session,
662 &ret->signed_cert_timestamp_list,
663 kSignedCertTimestampListTag, pool) ||
664 !SSL_SESSION_parse_crypto_buffer(&session, &ret->ocsp_response,
665 kOCSPResponseTag, pool)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400666 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400667 }
668
669 int extended_master_secret;
670 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
671 kExtendedMasterSecretTag,
672 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400673 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400674 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400675 }
David Benjamin8be79a32015-08-23 01:37:36 -0400676 ret->extended_master_secret = !!extended_master_secret;
677
David Benjamin11fa7032017-04-11 22:13:45 -0400678 if (!SSL_SESSION_parse_u16(&session, &ret->group_id, kGroupIDTag, 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400679 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400680 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400681 }
682
683 CBS cert_chain;
Adam Langley68e71242016-12-12 11:06:16 -0800684 CBS_init(&cert_chain, NULL, 0);
David Benjamin26416e92015-08-22 16:04:17 -0400685 int has_cert_chain;
686 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
Adam Langley68e71242016-12-12 11:06:16 -0800687 kCertChainTag) ||
688 (has_cert_chain && CBS_len(&cert_chain) == 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400689 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400690 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400691 }
Adam Langley68e71242016-12-12 11:06:16 -0800692 if (has_cert_chain && !has_peer) {
693 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400694 return nullptr;
Adam Langley364f7a62016-12-12 10:51:00 -0800695 }
Adam Langley68e71242016-12-12 11:06:16 -0800696 if (has_peer || has_cert_chain) {
697 ret->certs = sk_CRYPTO_BUFFER_new_null();
698 if (ret->certs == NULL) {
David Benjamin26416e92015-08-22 16:04:17 -0400699 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400700 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400701 }
Adam Langley68e71242016-12-12 11:06:16 -0800702
703 if (has_peer) {
David Benjaminee910bf2017-07-25 22:36:00 -0400704 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new_from_CBS(&peer, pool));
705 if (!buffer ||
706 !PushToStack(ret->certs, std::move(buffer))) {
Adam Langleye8509092016-11-07 14:24:33 -0800707 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400708 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400709 }
710 }
Adam Langley68e71242016-12-12 11:06:16 -0800711
712 while (CBS_len(&cert_chain) > 0) {
713 CBS cert;
714 if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) ||
715 CBS_len(&cert) == 0) {
716 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400717 return nullptr;
Adam Langley68e71242016-12-12 11:06:16 -0800718 }
719
Adam Langley46db7af2017-02-01 15:49:37 -0800720 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800721 if (buffer == NULL ||
722 !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
723 CRYPTO_BUFFER_free(buffer);
724 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400725 return nullptr;
Adam Langley68e71242016-12-12 11:06:16 -0800726 }
727 }
728 }
729
David Benjamin31b0c9b2017-07-20 14:49:15 -0400730 if (!x509_method->session_cache_objects(ret.get())) {
Adam Langley68e71242016-12-12 11:06:16 -0800731 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400732 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400733 }
734
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400735 CBS age_add;
736 int age_add_present;
Steven Valdez5b986082016-09-01 12:29:49 -0400737 if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400738 kTicketAgeAddTag) ||
739 (age_add_present &&
740 !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
741 CBS_len(&age_add) != 0) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400742 return nullptr;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400743 }
744 ret->ticket_age_add_valid = age_add_present;
745
Adam Langley364f7a62016-12-12 10:51:00 -0800746 int is_server;
747 if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag,
748 1 /* default to true */)) {
749 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400750 return nullptr;
Adam Langley364f7a62016-12-12 10:51:00 -0800751 }
752 /* TODO: in time we can include |is_server| for servers too, then we can
753 enforce that client and server sessions are never mixed up. */
754
755 ret->is_server = is_server;
756
David Benjaminf1050fd2016-12-13 20:05:36 -0500757 if (!SSL_SESSION_parse_u16(&session, &ret->peer_signature_algorithm,
758 kPeerSignatureAlgorithmTag, 0) ||
Steven Valdez08b65f42016-12-07 15:29:45 -0500759 !SSL_SESSION_parse_u32(&session, &ret->ticket_max_early_data,
760 kTicketMaxEarlyDataTag, 0) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500761 !SSL_SESSION_parse_u32(&session, &ret->auth_timeout, kAuthTimeoutTag,
762 ret->timeout) ||
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500763 !SSL_SESSION_parse_octet_string(&session, &ret->early_alpn,
764 &ret->early_alpn_len, kEarlyALPNTag) ||
David Benjaminf1050fd2016-12-13 20:05:36 -0500765 CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400766 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400767 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400768 }
Adam Langley95c29f32014-06-20 12:00:00 -0700769
David Benjaminfd67aa82015-06-15 19:41:48 -0400770 return ret;
David Benjaminfd67aa82015-06-15 19:41:48 -0400771}
772
David Benjamin86e95b82017-07-18 16:34:25 -0400773} // namespace bssl
774
775using namespace bssl;
776
777int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
778 size_t *out_len) {
779 if (in->not_resumable) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400780 // If the caller has an unresumable session, e.g. if |SSL_get_session| were
781 // called on a TLS 1.3 or False Started connection, serialize with a
782 // placeholder value so it is not accidentally deserialized into a resumable
783 // one.
David Benjamin86e95b82017-07-18 16:34:25 -0400784 static const char kNotResumableSession[] = "NOT RESUMABLE";
785
786 *out_len = strlen(kNotResumableSession);
787 *out_data = (uint8_t *)BUF_memdup(kNotResumableSession, *out_len);
788 if (*out_data == NULL) {
789 return 0;
790 }
791
792 return 1;
793 }
794
795 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
796}
797
798int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
799 size_t *out_len) {
800 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
801}
802
803int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
804 uint8_t *out;
805 size_t len;
806
807 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
808 return -1;
809 }
810
811 if (len > INT_MAX) {
812 OPENSSL_free(out);
813 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
814 return -1;
815 }
816
817 if (pp) {
818 OPENSSL_memcpy(*pp, out, len);
819 *pp += len;
820 }
821 OPENSSL_free(out);
822
823 return len;
824}
825
Adam Langley46db7af2017-02-01 15:49:37 -0800826SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len,
827 const SSL_CTX *ctx) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400828 CBS cbs;
829 CBS_init(&cbs, in, in_len);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400830 UniquePtr<SSL_SESSION> ret =
831 SSL_SESSION_parse(&cbs, ctx->x509_method, ctx->pool);
832 if (!ret) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400833 return NULL;
834 }
835 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400836 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400837 return NULL;
838 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400839 return ret.release();
David Benjaminfd67aa82015-06-15 19:41:48 -0400840}