blob: 39c52ed76c4bf4bc5250f0140f4a141c783a9f60 [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
Steven Valdez84b5c002016-08-25 16:30:58 -040088#include <openssl/buf.h>
David Benjamin83fd6b62014-10-19 04:33:38 -040089#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -070090#include <openssl/err.h>
Brian Smith054e6822015-03-27 21:12:01 -100091#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -070092#include <openssl/x509.h>
93
David Benjamin2ee94aa2015-04-07 22:38:30 -040094#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070095
Adam Langley95c29f32014-06-20 12:00:00 -070096
David Benjamin83fd6b62014-10-19 04:33:38 -040097/* An SSL_SESSION is serialized as the following ASN.1 structure:
98 *
99 * SSLSession ::= SEQUENCE {
David Benjamin7bb88bb2016-11-01 13:59:01 -0400100 * version INTEGER (1), -- session structure version
David Benjamin83fd6b62014-10-19 04:33:38 -0400101 * sslVersion INTEGER, -- protocol version number
102 * cipher OCTET STRING, -- two bytes long
103 * sessionID OCTET STRING,
104 * masterKey OCTET STRING,
David Benjamin5b7b09c2016-11-03 20:32:10 -0400105 * time [1] INTEGER, -- seconds since UNIX epoch
106 * timeout [2] INTEGER, -- in seconds
David Benjamin83fd6b62014-10-19 04:33:38 -0400107 * peer [3] Certificate OPTIONAL,
108 * sessionIDContext [4] OCTET STRING OPTIONAL,
109 * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
110 * hostName [6] OCTET STRING OPTIONAL,
111 * -- from server_name extension
David Benjamin83fd6b62014-10-19 04:33:38 -0400112 * pskIdentity [8] OCTET STRING OPTIONAL,
113 * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
114 * ticket [10] OCTET STRING OPTIONAL, -- client-only
115 * peerSHA256 [13] OCTET STRING OPTIONAL,
116 * originalHandshakeHash [14] OCTET STRING OPTIONAL,
117 * signedCertTimestampList [15] OCTET STRING OPTIONAL,
118 * -- contents of SCT extension
119 * ocspResponse [16] OCTET STRING OPTIONAL,
David Benjamin26416e92015-08-22 16:04:17 -0400120 * -- stapled OCSP response from the server
Adam Langley75712922014-10-10 16:23:43 -0700121 * extendedMasterSecret [17] BOOLEAN OPTIONAL,
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200122 * keyExchangeInfo [18] INTEGER OPTIONAL,
David Benjamin26416e92015-08-22 16:04:17 -0400123 * certChain [19] SEQUENCE OF Certificate OPTIONAL,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400124 * ticketAgeAdd [21] OCTET STRING OPTIONAL,
Adam Langleye8509092016-11-07 14:24:33 -0800125 * x509ChainIncludeLeaf [22] BOOLEAN OPTIONAL,
David Benjamin83fd6b62014-10-19 04:33:38 -0400126 * }
127 *
David Benjamin688d8df2014-11-02 23:06:42 -0500128 * Note: historically this serialization has included other optional
129 * fields. Their presense is currently treated as a parse error:
130 *
131 * keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
132 * pskIdentityHint [7] OCTET STRING OPTIONAL,
133 * compressionMethod [11] OCTET STRING OPTIONAL,
Steven Valdez5b986082016-09-01 12:29:49 -0400134 * srpUsername [12] OCTET STRING OPTIONAL,
135 * ticketFlags [20] INTEGER OPTIONAL,
Adam Langleye8509092016-11-07 14:24:33 -0800136 *
137 *
138 * When serialising, the first element of |certChain| does not duplicate the
139 * contents of |peer|, but that was not always true. When parsing, the returned
140 * |x509_chain| sometimes needs to contain the leaf certificate at the
141 * beginning (clients) and sometimes not (servers). The |x509ChainIncludeLeaf|
142 * value specifies whether this duplication should be done for the in-memory
143 * representation. If not given, the parsing code looks to see whether the
144 * first cert in |certChain| matches the |peer| and assumes that it's parsing
145 * old session data and thus that |x509ChainIncludeLeaf| should be true. */
David Benjamin83fd6b62014-10-19 04:33:38 -0400146
Adam Langley96c2a282015-06-02 14:16:44 -0700147static const unsigned kVersion = 1;
David Benjamin7f393f72015-10-17 12:36:12 -0400148
David Benjamin83fd6b62014-10-19 04:33:38 -0400149static const int kTimeTag =
150 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
151static const int kTimeoutTag =
152 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
153static const int kPeerTag =
154 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
David Benjamin7f393f72015-10-17 12:36:12 -0400155static const int kSessionIDContextTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400156 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
157static const int kVerifyResultTag =
158 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
159static const int kHostNameTag =
160 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
David Benjamin83fd6b62014-10-19 04:33:38 -0400161static const int kPSKIdentityTag =
162 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
163static const int kTicketLifetimeHintTag =
164 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
165static const int kTicketTag =
166 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
167static const int kPeerSHA256Tag =
168 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
169static const int kOriginalHandshakeHashTag =
170 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
171static const int kSignedCertTimestampListTag =
172 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
173static const int kOCSPResponseTag =
174 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
Adam Langley75712922014-10-10 16:23:43 -0700175static const int kExtendedMasterSecretTag =
176 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200177static const int kKeyExchangeInfoTag =
178 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
David Benjamin26416e92015-08-22 16:04:17 -0400179static const int kCertChainTag =
180 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400181static const int kTicketAgeAddTag =
182 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21;
Adam Langleye8509092016-11-07 14:24:33 -0800183static const int kX509ChainIncludeLeafTag =
184 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22;
David Benjamin26416e92015-08-22 16:04:17 -0400185
David Benjamin14e2b502015-09-13 14:48:12 -0400186static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400187 size_t *out_len, int for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400188 CBB cbb, session, child, child2;
Adam Langley95c29f32014-06-20 12:00:00 -0700189
David Benjaminf3a8b122014-12-25 23:11:49 -0500190 if (in == NULL || in->cipher == NULL) {
David Benjamin89abaea2014-10-19 13:50:18 -0400191 return 0;
192 }
Adam Langley95c29f32014-06-20 12:00:00 -0700193
David Benjamina8653202015-06-28 01:26:10 -0400194 CBB_zero(&cbb);
195 if (!CBB_init(&cbb, 0) ||
196 !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400197 !CBB_add_asn1_uint64(&session, kVersion) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400198 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
199 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminf3a8b122014-12-25 23:11:49 -0500200 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400201 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjamin3cac4502014-10-21 01:46:30 -0400202 /* The session ID is irrelevant for a session ticket. */
203 !CBB_add_bytes(&child, in->session_id,
204 for_ticket ? 0 : in->session_id_length) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400205 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
206 !CBB_add_bytes(&child, in->master_key, in->master_key_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400207 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400208 goto err;
209 }
Adam Langley95c29f32014-06-20 12:00:00 -0700210
David Benjamin5b7b09c2016-11-03 20:32:10 -0400211 if (in->time < 0) {
212 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
213 goto err;
David Benjamin89abaea2014-10-19 13:50:18 -0400214 }
Adam Langley95c29f32014-06-20 12:00:00 -0700215
David Benjamin5b7b09c2016-11-03 20:32:10 -0400216 if (!CBB_add_asn1(&session, &child, kTimeTag) ||
217 !CBB_add_asn1_uint64(&child, in->time)) {
218 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
219 goto err;
220 }
David Benjamin0f31ac72016-11-02 23:05:14 -0400221
David Benjamin5b7b09c2016-11-03 20:32:10 -0400222 if (in->timeout < 0) {
223 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
224 goto err;
225 }
226
227 if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
228 !CBB_add_asn1_uint64(&child, in->timeout)) {
229 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
230 goto err;
David Benjamin89abaea2014-10-19 13:50:18 -0400231 }
Adam Langley95c29f32014-06-20 12:00:00 -0700232
Adam Langleye8509092016-11-07 14:24:33 -0800233 if (sk_CRYPTO_BUFFER_num(in->certs) > 0 && !in->peer_sha256_valid) {
234 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, 0);
235 if (!CBB_add_asn1(&session, &child, kPeerTag) ||
236 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
237 CRYPTO_BUFFER_len(buffer))) {
David Benjamin3570d732015-06-29 00:28:17 -0400238 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400239 goto err;
240 }
David Benjamin89abaea2014-10-19 13:50:18 -0400241 }
Adam Langley95c29f32014-06-20 12:00:00 -0700242
David Benjamin89abaea2014-10-19 13:50:18 -0400243 /* Although it is OPTIONAL and usually empty, OpenSSL has
244 * historically always encoded the sid_ctx. */
245 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
246 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
247 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400248 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400249 goto err;
250 }
Adam Langley95c29f32014-06-20 12:00:00 -0700251
David Benjamin89abaea2014-10-19 13:50:18 -0400252 if (in->verify_result != X509_V_OK) {
253 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
254 !CBB_add_asn1_uint64(&child, in->verify_result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400255 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400256 goto err;
257 }
258 }
Adam Langley95c29f32014-06-20 12:00:00 -0700259
David Benjamin89abaea2014-10-19 13:50:18 -0400260 if (in->tlsext_hostname) {
261 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
262 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
263 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
264 strlen(in->tlsext_hostname))) {
David Benjamin3570d732015-06-29 00:28:17 -0400265 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400266 goto err;
267 }
268 }
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjamin89abaea2014-10-19 13:50:18 -0400270 if (in->psk_identity) {
271 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
272 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
273 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
274 strlen(in->psk_identity))) {
David Benjamin3570d732015-06-29 00:28:17 -0400275 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400276 goto err;
277 }
278 }
Adam Langley95c29f32014-06-20 12:00:00 -0700279
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700280 if (in->tlsext_tick_lifetime_hint > 0) {
David Benjamin89abaea2014-10-19 13:50:18 -0400281 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700282 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400283 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400284 goto err;
285 }
286 }
Adam Langley95c29f32014-06-20 12:00:00 -0700287
David Benjamin95d31822015-06-15 19:53:32 -0400288 if (in->tlsext_tick && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400289 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
290 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
291 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -0400292 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400293 goto err;
294 }
295 }
Adam Langley75872532014-06-20 12:00:00 -0700296
David Benjamin89abaea2014-10-19 13:50:18 -0400297 if (in->peer_sha256_valid) {
298 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
299 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
300 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
David Benjamin3570d732015-06-29 00:28:17 -0400301 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400302 goto err;
303 }
304 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700305
David Benjamin89abaea2014-10-19 13:50:18 -0400306 if (in->original_handshake_hash_len > 0) {
307 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
308 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
309 !CBB_add_bytes(&child2, in->original_handshake_hash,
310 in->original_handshake_hash_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400311 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400312 goto err;
313 }
314 }
Adam Langley95c29f32014-06-20 12:00:00 -0700315
David Benjamin89abaea2014-10-19 13:50:18 -0400316 if (in->tlsext_signed_cert_timestamp_list_length > 0) {
317 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
318 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
319 !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
320 in->tlsext_signed_cert_timestamp_list_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400321 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400322 goto err;
323 }
324 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200325
David Benjamin89abaea2014-10-19 13:50:18 -0400326 if (in->ocsp_response_length > 0) {
327 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
328 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
329 !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400330 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400331 goto err;
332 }
333 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400334
Adam Langley75712922014-10-10 16:23:43 -0700335 if (in->extended_master_secret) {
336 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
337 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
338 !CBB_add_u8(&child2, 0xff)) {
David Benjamin3570d732015-06-29 00:28:17 -0400339 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley75712922014-10-10 16:23:43 -0700340 goto err;
341 }
342 }
343
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200344 if (in->key_exchange_info > 0 &&
345 (!CBB_add_asn1(&session, &child, kKeyExchangeInfoTag) ||
346 !CBB_add_asn1_uint64(&child, in->key_exchange_info))) {
347 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
348 goto err;
349 }
350
David Benjamin26416e92015-08-22 16:04:17 -0400351 /* The certificate chain is only serialized if the leaf's SHA-256 isn't
352 * serialized instead. */
Adam Langleye8509092016-11-07 14:24:33 -0800353 if (in->certs != NULL &&
354 !in->peer_sha256_valid &&
355 sk_CRYPTO_BUFFER_num(in->certs) > 1) {
David Benjamin26416e92015-08-22 16:04:17 -0400356 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
357 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
358 goto err;
359 }
Adam Langleye8509092016-11-07 14:24:33 -0800360 for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs); i++) {
361 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, i);
362 if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
363 CRYPTO_BUFFER_len(buffer))) {
364 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin26416e92015-08-22 16:04:17 -0400365 goto err;
366 }
367 }
368 }
369
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400370 if (in->ticket_age_add_valid) {
371 if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
372 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
373 !CBB_add_u32(&child2, in->ticket_age_add)) {
374 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
375 goto err;
376 }
377 }
378
Adam Langleye8509092016-11-07 14:24:33 -0800379 if (in->x509_chain_should_include_leaf) {
380 if (!CBB_add_asn1(&session, &child, kX509ChainIncludeLeafTag) ||
381 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
382 !CBB_add_u8(&child2, 0xff)) {
383 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
384 goto err;
385 }
386 }
387
David Benjamin3cac4502014-10-21 01:46:30 -0400388 if (!CBB_finish(&cbb, out_data, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400389 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400390 goto err;
391 }
David Benjamin3cac4502014-10-21 01:46:30 -0400392 return 1;
393
394 err:
395 CBB_cleanup(&cbb);
396 return 0;
397}
398
David Benjamin14e2b502015-09-13 14:48:12 -0400399int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
400 size_t *out_len) {
Steven Valdez84b5c002016-08-25 16:30:58 -0400401 if (in->not_resumable) {
402 /* If the caller has an unresumable session, e.g. if |SSL_get_session| were
403 * called on a TLS 1.3 or False Started connection, serialize with a
404 * placeholder value so it is not accidentally deserialized into a resumable
405 * one. */
406 static const char kNotResumableSession[] = "NOT RESUMABLE";
407
408 *out_len = strlen(kNotResumableSession);
409 *out_data = BUF_memdup(kNotResumableSession, *out_len);
410 if (*out_data == NULL) {
411 return 0;
412 }
413
414 return 1;
415 }
416
David Benjamin3cac4502014-10-21 01:46:30 -0400417 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
418}
419
David Benjamin14e2b502015-09-13 14:48:12 -0400420int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400421 size_t *out_len) {
422 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
423}
424
425int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
426 uint8_t *out;
427 size_t len;
428
429 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
430 return -1;
431 }
Adam Langley95c29f32014-06-20 12:00:00 -0700432
David Benjamin89abaea2014-10-19 13:50:18 -0400433 if (len > INT_MAX) {
434 OPENSSL_free(out);
David Benjamin3570d732015-06-29 00:28:17 -0400435 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin89abaea2014-10-19 13:50:18 -0400436 return -1;
437 }
Adam Langley95c29f32014-06-20 12:00:00 -0700438
David Benjamin89abaea2014-10-19 13:50:18 -0400439 if (pp) {
440 memcpy(*pp, out, len);
441 *pp += len;
442 }
443 OPENSSL_free(out);
Adam Langley95c29f32014-06-20 12:00:00 -0700444
David Benjamin89abaea2014-10-19 13:50:18 -0400445 return len;
David Benjamin89abaea2014-10-19 13:50:18 -0400446}
Adam Langley95c29f32014-06-20 12:00:00 -0700447
David Benjaminfd67aa82015-06-15 19:41:48 -0400448/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400449 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
450 * entry, if |*out| is not NULL, it frees the existing contents. If
451 * the element was not found, it sets |*out| to NULL. It returns one
452 * on success, whether or not the element was found, and zero on
453 * decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400454static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400455 CBS value;
456 int present;
457 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400458 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400459 return 0;
460 }
461 if (present) {
462 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400463 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400464 return 0;
465 }
466 if (!CBS_strdup(&value, out)) {
David Benjamin3570d732015-06-29 00:28:17 -0400467 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400468 return 0;
469 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400470 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400471 OPENSSL_free(*out);
472 *out = NULL;
473 }
474 return 1;
475}
Adam Langley95c29f32014-06-20 12:00:00 -0700476
David Benjaminfd67aa82015-06-15 19:41:48 -0400477/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400478 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
479 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
480 * contents. On entry, if the element was not found, it sets
481 * |*out_ptr| to NULL. It returns one on success, whether or not the
482 * element was found, and zero on decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400483static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin8be79a32015-08-23 01:37:36 -0400484 size_t *out_len, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400485 CBS value;
486 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400487 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400488 return 0;
489 }
490 if (!CBS_stow(&value, out_ptr, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400491 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400492 return 0;
493 }
494 return 1;
495}
Adam Langley95c29f32014-06-20 12:00:00 -0700496
David Benjamin8be79a32015-08-23 01:37:36 -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|. */
499static 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 }
507 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 Benjamin8be79a32015-08-23 01:37:36 -0400538static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) {
539 SSL_SESSION *ret = SSL_SESSION_new();
David Benjaminfd67aa82015-06-15 19:41:48 -0400540 if (ret == NULL) {
541 goto err;
David Benjamin83fd6b62014-10-19 04:33:38 -0400542 }
Adam Langley95c29f32014-06-20 12:00:00 -0700543
David Benjamin8be79a32015-08-23 01:37:36 -0400544 CBS session;
545 uint64_t version, ssl_version;
David Benjaminfd67aa82015-06-15 19:41:48 -0400546 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400547 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400548 version != kVersion ||
David Benjamin8be79a32015-08-23 01:37:36 -0400549 !CBS_get_asn1_uint64(&session, &ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400550 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400551 goto err;
552 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400553 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700554
David Benjamin8be79a32015-08-23 01:37:36 -0400555 CBS cipher;
David Benjaminf3a8b122014-12-25 23:11:49 -0500556 uint16_t cipher_value;
David Benjamin8be79a32015-08-23 01:37:36 -0400557 if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
558 !CBS_get_u16(&cipher, &cipher_value) ||
559 CBS_len(&cipher) != 0) {
560 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400561 goto err;
562 }
David Benjamina1c90a52015-05-30 17:03:14 -0400563 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400564 if (ret->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400565 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin83fd6b62014-10-19 04:33:38 -0400566 goto err;
567 }
Adam Langley95c29f32014-06-20 12:00:00 -0700568
David Benjamin8be79a32015-08-23 01:37:36 -0400569 CBS session_id, master_key;
570 if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
571 CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
572 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
573 CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400574 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400575 goto err;
576 }
577 memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
578 ret->session_id_length = CBS_len(&session_id);
David Benjamin83fd6b62014-10-19 04:33:38 -0400579 memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
580 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700581
David Benjamin5b7b09c2016-11-03 20:32:10 -0400582 CBS child;
583 uint64_t time, timeout;
584 if (!CBS_get_asn1(&session, &child, kTimeTag) ||
585 !CBS_get_asn1_uint64(&child, &time) ||
586 time > LONG_MAX ||
587 !CBS_get_asn1(&session, &child, kTimeoutTag) ||
588 !CBS_get_asn1_uint64(&child, &timeout) ||
589 timeout > LONG_MAX) {
David Benjamin3570d732015-06-29 00:28:17 -0400590 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400591 goto err;
592 }
Adam Langley95c29f32014-06-20 12:00:00 -0700593
David Benjamin5b7b09c2016-11-03 20:32:10 -0400594 ret->time = (long)time;
595 ret->timeout = (long)timeout;
596
David Benjamin8be79a32015-08-23 01:37:36 -0400597 CBS peer;
598 int has_peer;
Adam Langleye8509092016-11-07 14:24:33 -0800599 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
600 (has_peer && CBS_len(&peer) == 0)) {
David Benjamin8be79a32015-08-23 01:37:36 -0400601 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
602 goto err;
603 }
Adam Langleye8509092016-11-07 14:24:33 -0800604 /* |peer| is processed with the certificate chain. */
Adam Langley95c29f32014-06-20 12:00:00 -0700605
David Benjamin8be79a32015-08-23 01:37:36 -0400606 if (!SSL_SESSION_parse_bounded_octet_string(
607 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
608 kSessionIDContextTag) ||
609 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
610 X509_V_OK) ||
611 !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
612 kHostNameTag) ||
613 !SSL_SESSION_parse_string(&session, &ret->psk_identity,
614 kPSKIdentityTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700615 !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
David Benjamin8be79a32015-08-23 01:37:36 -0400616 kTicketLifetimeHintTag, 0) ||
617 !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
618 &ret->tlsext_ticklen, kTicketTag)) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400619 goto err;
620 }
Adam Langley95c29f32014-06-20 12:00:00 -0700621
David Benjamin8be79a32015-08-23 01:37:36 -0400622 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
David Benjamin5b7b09c2016-11-03 20:32:10 -0400623 CBS peer_sha256;
David Benjamin8be79a32015-08-23 01:37:36 -0400624 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
625 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
626 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
627 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400628 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400629 goto err;
630 }
631 memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
632 ret->peer_sha256_valid = 1;
633 } else {
634 ret->peer_sha256_valid = 0;
635 }
Adam Langley95c29f32014-06-20 12:00:00 -0700636
David Benjamin8be79a32015-08-23 01:37:36 -0400637 if (!SSL_SESSION_parse_bounded_octet_string(
638 &session, ret->original_handshake_hash,
639 &ret->original_handshake_hash_len,
640 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
641 !SSL_SESSION_parse_octet_string(
642 &session, &ret->tlsext_signed_cert_timestamp_list,
643 &ret->tlsext_signed_cert_timestamp_list_length,
644 kSignedCertTimestampListTag) ||
645 !SSL_SESSION_parse_octet_string(
646 &session, &ret->ocsp_response, &ret->ocsp_response_length,
647 kOCSPResponseTag)) {
648 goto err;
649 }
650
651 int extended_master_secret;
652 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
653 kExtendedMasterSecretTag,
654 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400655 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400656 goto err;
657 }
David Benjamin8be79a32015-08-23 01:37:36 -0400658 ret->extended_master_secret = !!extended_master_secret;
659
660 if (!SSL_SESSION_parse_u32(&session, &ret->key_exchange_info,
David Benjamin26416e92015-08-22 16:04:17 -0400661 kKeyExchangeInfoTag, 0)) {
662 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
663 goto err;
664 }
665
666 CBS cert_chain;
Adam Langleye8509092016-11-07 14:24:33 -0800667 CBS_init(&cert_chain, NULL, 0);
David Benjamin26416e92015-08-22 16:04:17 -0400668 int has_cert_chain;
669 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
Adam Langleye8509092016-11-07 14:24:33 -0800670 kCertChainTag) ||
671 (has_cert_chain && CBS_len(&cert_chain) == 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400672 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
673 goto err;
674 }
Adam Langleye8509092016-11-07 14:24:33 -0800675 sk_CRYPTO_BUFFER_pop_free(ret->certs, CRYPTO_BUFFER_free);
676 ret->certs = NULL;
677
678 int x509_chain_should_include_leaf_default = 0;
679
680 if (has_peer || has_cert_chain) {
681 ret->certs = sk_CRYPTO_BUFFER_new_null();
682 if (ret->certs == NULL) {
David Benjamin26416e92015-08-22 16:04:17 -0400683 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
684 goto err;
685 }
Adam Langleye8509092016-11-07 14:24:33 -0800686
687 if (has_peer) {
688 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&peer, NULL);
689 if (buffer == NULL) {
690 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin26416e92015-08-22 16:04:17 -0400691 goto err;
692 }
Adam Langleye8509092016-11-07 14:24:33 -0800693
694 if (!sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
695 CRYPTO_BUFFER_free(buffer);
David Benjamin26416e92015-08-22 16:04:17 -0400696 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleye8509092016-11-07 14:24:33 -0800697 goto err;
698 }
699 }
700
701 size_t chain_i = 0;
702 while (CBS_len(&cert_chain) > 0) {
703 chain_i++;
704
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);
709 goto err;
710 }
711
712 if (has_peer && chain_i == 1) {
713 /* If a certificate was parsed from |peer| then it may, or may not, be
714 * duplicated as the first element of |cert_chain|. */
715 if (CBS_len(&peer) == CBS_len(&cert) &&
716 memcmp(CBS_data(&peer), CBS_data(&cert), CBS_len(&peer)) == 0) {
717 x509_chain_should_include_leaf_default = 1;
718 continue;
719 }
720 }
721
722 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, NULL);
723 if (buffer == NULL) {
724 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
725 goto err;
726 }
727
728 if (!sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
729 CRYPTO_BUFFER_free(buffer);
730 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin26416e92015-08-22 16:04:17 -0400731 goto err;
732 }
733 }
734 }
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) {
743 goto err;
744 }
745 ret->ticket_age_add_valid = age_add_present;
746
Adam Langleye8509092016-11-07 14:24:33 -0800747 int x509_chain_should_include_leaf;
748 if (!CBS_get_optional_asn1_bool(&session, &x509_chain_should_include_leaf,
749 kX509ChainIncludeLeafTag,
750 x509_chain_should_include_leaf_default)) {
751 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
752 goto err;
753 }
754
David Benjamin26416e92015-08-22 16:04:17 -0400755 if (CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400756 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
757 goto err;
758 }
Adam Langley95c29f32014-06-20 12:00:00 -0700759
Adam Langleye8509092016-11-07 14:24:33 -0800760 if (!x509_chain_from_buffers(&ret->x509_chain, ret->certs)) {
761 goto err;
762 }
763
764 ssl_session_set_x509_peer(ret, x509_chain_should_include_leaf);
765
David Benjaminfd67aa82015-06-15 19:41:48 -0400766 return ret;
767
768err:
769 SSL_SESSION_free(ret);
770 return NULL;
771}
772
773SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) {
774 CBS cbs;
775 CBS_init(&cbs, in, in_len);
776 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
777 if (ret == NULL) {
778 return NULL;
779 }
780 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400781 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400782 SSL_SESSION_free(ret);
783 return NULL;
784 }
785 return ret;
786}
787
788SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
789 if (length < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400790 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminfd67aa82015-06-15 19:41:48 -0400791 return NULL;
792 }
793
794 CBS cbs;
795 CBS_init(&cbs, *pp, length);
796
797 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
798 if (ret == NULL) {
799 return NULL;
800 }
801
David Benjamin83fd6b62014-10-19 04:33:38 -0400802 if (a) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400803 SSL_SESSION_free(*a);
David Benjamin83fd6b62014-10-19 04:33:38 -0400804 *a = ret;
805 }
806 *pp = CBS_data(&cbs);
807 return ret;
David Benjamin83fd6b62014-10-19 04:33:38 -0400808}