blob: fa2fbc0f23b1f85941ec9b104839224fa4e11d5d [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 Langley364f7a62016-12-12 10:51:00 -0800125 * isServer [22] BOOLEAN DEFAULT TRUE,
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 Langleyc0fc7a12016-12-09 15:05:34 -0800136 */
David Benjamin83fd6b62014-10-19 04:33:38 -0400137
Adam Langley96c2a282015-06-02 14:16:44 -0700138static const unsigned kVersion = 1;
David Benjamin7f393f72015-10-17 12:36:12 -0400139
David Benjamin83fd6b62014-10-19 04:33:38 -0400140static const int kTimeTag =
141 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
142static const int kTimeoutTag =
143 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
144static const int kPeerTag =
145 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
David Benjamin7f393f72015-10-17 12:36:12 -0400146static const int kSessionIDContextTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400147 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
148static const int kVerifyResultTag =
149 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
150static const int kHostNameTag =
151 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
David Benjamin83fd6b62014-10-19 04:33:38 -0400152static const int kPSKIdentityTag =
153 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
154static const int kTicketLifetimeHintTag =
155 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
156static const int kTicketTag =
157 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
158static const int kPeerSHA256Tag =
159 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
160static const int kOriginalHandshakeHashTag =
161 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
162static const int kSignedCertTimestampListTag =
163 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
164static const int kOCSPResponseTag =
165 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
Adam Langley75712922014-10-10 16:23:43 -0700166static const int kExtendedMasterSecretTag =
167 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200168static const int kKeyExchangeInfoTag =
169 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
David Benjamin26416e92015-08-22 16:04:17 -0400170static const int kCertChainTag =
171 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400172static const int kTicketAgeAddTag =
173 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21;
Adam Langley364f7a62016-12-12 10:51:00 -0800174static const int kIsServerTag =
175 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22;
David Benjamin26416e92015-08-22 16:04:17 -0400176
David Benjamin14e2b502015-09-13 14:48:12 -0400177static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400178 size_t *out_len, int for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400179 CBB cbb, session, child, child2;
Adam Langley95c29f32014-06-20 12:00:00 -0700180
David Benjaminf3a8b122014-12-25 23:11:49 -0500181 if (in == NULL || in->cipher == NULL) {
David Benjamin89abaea2014-10-19 13:50:18 -0400182 return 0;
183 }
Adam Langley95c29f32014-06-20 12:00:00 -0700184
David Benjamina8653202015-06-28 01:26:10 -0400185 CBB_zero(&cbb);
186 if (!CBB_init(&cbb, 0) ||
187 !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400188 !CBB_add_asn1_uint64(&session, kVersion) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400189 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
190 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminf3a8b122014-12-25 23:11:49 -0500191 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400192 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjamin3cac4502014-10-21 01:46:30 -0400193 /* The session ID is irrelevant for a session ticket. */
194 !CBB_add_bytes(&child, in->session_id,
195 for_ticket ? 0 : in->session_id_length) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400196 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
197 !CBB_add_bytes(&child, in->master_key, in->master_key_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400198 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400199 goto err;
200 }
Adam Langley95c29f32014-06-20 12:00:00 -0700201
David Benjamin5b7b09c2016-11-03 20:32:10 -0400202 if (in->time < 0) {
203 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
204 goto err;
David Benjamin89abaea2014-10-19 13:50:18 -0400205 }
Adam Langley95c29f32014-06-20 12:00:00 -0700206
David Benjamin5b7b09c2016-11-03 20:32:10 -0400207 if (!CBB_add_asn1(&session, &child, kTimeTag) ||
208 !CBB_add_asn1_uint64(&child, in->time)) {
209 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
210 goto err;
211 }
David Benjamin0f31ac72016-11-02 23:05:14 -0400212
David Benjamin5b7b09c2016-11-03 20:32:10 -0400213 if (in->timeout < 0) {
214 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
215 goto err;
216 }
217
218 if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
219 !CBB_add_asn1_uint64(&child, in->timeout)) {
220 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
221 goto err;
David Benjamin89abaea2014-10-19 13:50:18 -0400222 }
Adam Langley95c29f32014-06-20 12:00:00 -0700223
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800224 /* The peer certificate is only serialized if the SHA-256 isn't
225 * serialized instead. */
Adam Langley68e71242016-12-12 11:06:16 -0800226 if (sk_CRYPTO_BUFFER_num(in->certs) > 0 && !in->peer_sha256_valid) {
227 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, 0);
228 if (!CBB_add_asn1(&session, &child, kPeerTag) ||
229 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
230 CRYPTO_BUFFER_len(buffer))) {
David Benjamin3570d732015-06-29 00:28:17 -0400231 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400232 goto err;
233 }
David Benjamin89abaea2014-10-19 13:50:18 -0400234 }
Adam Langley95c29f32014-06-20 12:00:00 -0700235
David Benjamin89abaea2014-10-19 13:50:18 -0400236 /* Although it is OPTIONAL and usually empty, OpenSSL has
237 * historically always encoded the sid_ctx. */
238 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
239 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
240 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400241 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400242 goto err;
243 }
Adam Langley95c29f32014-06-20 12:00:00 -0700244
David Benjamin89abaea2014-10-19 13:50:18 -0400245 if (in->verify_result != X509_V_OK) {
246 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
247 !CBB_add_asn1_uint64(&child, in->verify_result)) {
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 }
251 }
Adam Langley95c29f32014-06-20 12:00:00 -0700252
David Benjamin89abaea2014-10-19 13:50:18 -0400253 if (in->tlsext_hostname) {
254 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
255 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
256 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
257 strlen(in->tlsext_hostname))) {
David Benjamin3570d732015-06-29 00:28:17 -0400258 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400259 goto err;
260 }
261 }
Adam Langley95c29f32014-06-20 12:00:00 -0700262
David Benjamin89abaea2014-10-19 13:50:18 -0400263 if (in->psk_identity) {
264 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
265 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
266 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
267 strlen(in->psk_identity))) {
David Benjamin3570d732015-06-29 00:28:17 -0400268 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400269 goto err;
270 }
271 }
Adam Langley95c29f32014-06-20 12:00:00 -0700272
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700273 if (in->tlsext_tick_lifetime_hint > 0) {
David Benjamin89abaea2014-10-19 13:50:18 -0400274 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700275 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400276 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400277 goto err;
278 }
279 }
Adam Langley95c29f32014-06-20 12:00:00 -0700280
David Benjamin95d31822015-06-15 19:53:32 -0400281 if (in->tlsext_tick && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400282 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
283 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
284 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -0400285 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400286 goto err;
287 }
288 }
Adam Langley75872532014-06-20 12:00:00 -0700289
David Benjamin89abaea2014-10-19 13:50:18 -0400290 if (in->peer_sha256_valid) {
291 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
292 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
293 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
David Benjamin3570d732015-06-29 00:28:17 -0400294 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400295 goto err;
296 }
297 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700298
David Benjamin89abaea2014-10-19 13:50:18 -0400299 if (in->original_handshake_hash_len > 0) {
300 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
301 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
302 !CBB_add_bytes(&child2, in->original_handshake_hash,
303 in->original_handshake_hash_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400304 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400305 goto err;
306 }
307 }
Adam Langley95c29f32014-06-20 12:00:00 -0700308
David Benjamin89abaea2014-10-19 13:50:18 -0400309 if (in->tlsext_signed_cert_timestamp_list_length > 0) {
310 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
311 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
312 !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
313 in->tlsext_signed_cert_timestamp_list_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400314 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400315 goto err;
316 }
317 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200318
David Benjamin89abaea2014-10-19 13:50:18 -0400319 if (in->ocsp_response_length > 0) {
320 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
321 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
322 !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400323 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400324 goto err;
325 }
326 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400327
Adam Langley75712922014-10-10 16:23:43 -0700328 if (in->extended_master_secret) {
329 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
330 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
331 !CBB_add_u8(&child2, 0xff)) {
David Benjamin3570d732015-06-29 00:28:17 -0400332 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley75712922014-10-10 16:23:43 -0700333 goto err;
334 }
335 }
336
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200337 if (in->key_exchange_info > 0 &&
338 (!CBB_add_asn1(&session, &child, kKeyExchangeInfoTag) ||
339 !CBB_add_asn1_uint64(&child, in->key_exchange_info))) {
340 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
341 goto err;
342 }
343
David Benjamin26416e92015-08-22 16:04:17 -0400344 /* The certificate chain is only serialized if the leaf's SHA-256 isn't
345 * serialized instead. */
Adam Langley68e71242016-12-12 11:06:16 -0800346 if (in->certs != NULL &&
347 !in->peer_sha256_valid &&
348 sk_CRYPTO_BUFFER_num(in->certs) >= 2) {
David Benjamin26416e92015-08-22 16:04:17 -0400349 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
350 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
351 goto err;
352 }
Adam Langley68e71242016-12-12 11:06:16 -0800353 for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs); i++) {
354 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, i);
355 if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
356 CRYPTO_BUFFER_len(buffer))) {
357 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin26416e92015-08-22 16:04:17 -0400358 goto err;
359 }
360 }
361 }
362
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400363 if (in->ticket_age_add_valid) {
364 if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
365 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
366 !CBB_add_u32(&child2, in->ticket_age_add)) {
367 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
368 goto err;
369 }
370 }
371
Adam Langley364f7a62016-12-12 10:51:00 -0800372 if (!in->is_server) {
373 if (!CBB_add_asn1(&session, &child, kIsServerTag) ||
374 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
375 !CBB_add_u8(&child2, 0x00)) {
376 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
377 goto err;
378 }
379 }
380
David Benjamin3cac4502014-10-21 01:46:30 -0400381 if (!CBB_finish(&cbb, out_data, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400382 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400383 goto err;
384 }
David Benjamin3cac4502014-10-21 01:46:30 -0400385 return 1;
386
387 err:
388 CBB_cleanup(&cbb);
389 return 0;
390}
391
David Benjamin14e2b502015-09-13 14:48:12 -0400392int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
393 size_t *out_len) {
Steven Valdez84b5c002016-08-25 16:30:58 -0400394 if (in->not_resumable) {
395 /* If the caller has an unresumable session, e.g. if |SSL_get_session| were
396 * called on a TLS 1.3 or False Started connection, serialize with a
397 * placeholder value so it is not accidentally deserialized into a resumable
398 * one. */
399 static const char kNotResumableSession[] = "NOT RESUMABLE";
400
401 *out_len = strlen(kNotResumableSession);
402 *out_data = BUF_memdup(kNotResumableSession, *out_len);
403 if (*out_data == NULL) {
404 return 0;
405 }
406
407 return 1;
408 }
409
David Benjamin3cac4502014-10-21 01:46:30 -0400410 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
411}
412
David Benjamin14e2b502015-09-13 14:48:12 -0400413int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400414 size_t *out_len) {
415 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
416}
417
418int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
419 uint8_t *out;
420 size_t len;
421
422 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
423 return -1;
424 }
Adam Langley95c29f32014-06-20 12:00:00 -0700425
David Benjamin89abaea2014-10-19 13:50:18 -0400426 if (len > INT_MAX) {
427 OPENSSL_free(out);
David Benjamin3570d732015-06-29 00:28:17 -0400428 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin89abaea2014-10-19 13:50:18 -0400429 return -1;
430 }
Adam Langley95c29f32014-06-20 12:00:00 -0700431
David Benjamin89abaea2014-10-19 13:50:18 -0400432 if (pp) {
433 memcpy(*pp, out, len);
434 *pp += len;
435 }
436 OPENSSL_free(out);
Adam Langley95c29f32014-06-20 12:00:00 -0700437
David Benjamin89abaea2014-10-19 13:50:18 -0400438 return len;
David Benjamin89abaea2014-10-19 13:50:18 -0400439}
Adam Langley95c29f32014-06-20 12:00:00 -0700440
David Benjaminfd67aa82015-06-15 19:41:48 -0400441/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400442 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
443 * entry, if |*out| is not NULL, it frees the existing contents. If
444 * the element was not found, it sets |*out| to NULL. It returns one
445 * on success, whether or not the element was found, and zero on
446 * decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400447static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400448 CBS value;
449 int present;
450 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400451 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400452 return 0;
453 }
454 if (present) {
455 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400456 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400457 return 0;
458 }
459 if (!CBS_strdup(&value, out)) {
David Benjamin3570d732015-06-29 00:28:17 -0400460 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400461 return 0;
462 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400463 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400464 OPENSSL_free(*out);
465 *out = NULL;
466 }
467 return 1;
468}
Adam Langley95c29f32014-06-20 12:00:00 -0700469
David Benjaminfd67aa82015-06-15 19:41:48 -0400470/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400471 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
472 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
473 * contents. On entry, if the element was not found, it sets
474 * |*out_ptr| to NULL. It returns one on success, whether or not the
475 * element was found, and zero on decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400476static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin8be79a32015-08-23 01:37:36 -0400477 size_t *out_len, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400478 CBS value;
479 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400480 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400481 return 0;
482 }
483 if (!CBS_stow(&value, out_ptr, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400484 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400485 return 0;
486 }
487 return 1;
488}
Adam Langley95c29f32014-06-20 12:00:00 -0700489
David Benjamin8be79a32015-08-23 01:37:36 -0400490/* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
491 * explicitly tagged with |tag| of size at most |max_out|. */
492static int SSL_SESSION_parse_bounded_octet_string(
David Benjamin30c4c302016-12-07 22:35:24 -0500493 CBS *cbs, uint8_t *out, uint8_t *out_len, uint8_t max_out, unsigned tag) {
David Benjamin8be79a32015-08-23 01:37:36 -0400494 CBS value;
495 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
496 CBS_len(&value) > max_out) {
497 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
498 return 0;
499 }
500 memcpy(out, CBS_data(&value), CBS_len(&value));
David Benjamin30c4c302016-12-07 22:35:24 -0500501 *out_len = (uint8_t)CBS_len(&value);
David Benjamin8be79a32015-08-23 01:37:36 -0400502 return 1;
503}
Adam Langley95c29f32014-06-20 12:00:00 -0700504
David Benjamin8be79a32015-08-23 01:37:36 -0400505static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
506 long default_value) {
507 uint64_t value;
508 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
509 (uint64_t)default_value) ||
510 value > LONG_MAX) {
511 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
512 return 0;
513 }
514 *out = (long)value;
515 return 1;
516}
517
518static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
519 uint32_t default_value) {
520 uint64_t value;
521 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
522 (uint64_t)default_value) ||
523 value > 0xffffffff) {
524 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
525 return 0;
526 }
527 *out = (uint32_t)value;
528 return 1;
529}
530
David Benjamin8be79a32015-08-23 01:37:36 -0400531static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) {
532 SSL_SESSION *ret = SSL_SESSION_new();
David Benjaminfd67aa82015-06-15 19:41:48 -0400533 if (ret == NULL) {
534 goto err;
David Benjamin83fd6b62014-10-19 04:33:38 -0400535 }
Adam Langley95c29f32014-06-20 12:00:00 -0700536
David Benjamin8be79a32015-08-23 01:37:36 -0400537 CBS session;
538 uint64_t version, ssl_version;
David Benjaminfd67aa82015-06-15 19:41:48 -0400539 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400540 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400541 version != kVersion ||
David Benjamin8be79a32015-08-23 01:37:36 -0400542 !CBS_get_asn1_uint64(&session, &ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400543 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400544 goto err;
545 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400546 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700547
David Benjamin8be79a32015-08-23 01:37:36 -0400548 CBS cipher;
David Benjaminf3a8b122014-12-25 23:11:49 -0500549 uint16_t cipher_value;
David Benjamin8be79a32015-08-23 01:37:36 -0400550 if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
551 !CBS_get_u16(&cipher, &cipher_value) ||
552 CBS_len(&cipher) != 0) {
553 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400554 goto err;
555 }
David Benjamina1c90a52015-05-30 17:03:14 -0400556 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400557 if (ret->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400558 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin83fd6b62014-10-19 04:33:38 -0400559 goto err;
560 }
Adam Langley95c29f32014-06-20 12:00:00 -0700561
David Benjamin8be79a32015-08-23 01:37:36 -0400562 CBS session_id, master_key;
563 if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
564 CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
565 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
566 CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400567 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400568 goto err;
569 }
570 memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
571 ret->session_id_length = CBS_len(&session_id);
David Benjamin83fd6b62014-10-19 04:33:38 -0400572 memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
573 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700574
David Benjamin5b7b09c2016-11-03 20:32:10 -0400575 CBS child;
576 uint64_t time, timeout;
577 if (!CBS_get_asn1(&session, &child, kTimeTag) ||
578 !CBS_get_asn1_uint64(&child, &time) ||
579 time > LONG_MAX ||
580 !CBS_get_asn1(&session, &child, kTimeoutTag) ||
581 !CBS_get_asn1_uint64(&child, &timeout) ||
582 timeout > LONG_MAX) {
David Benjamin3570d732015-06-29 00:28:17 -0400583 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400584 goto err;
585 }
Adam Langley95c29f32014-06-20 12:00:00 -0700586
David Benjamin5b7b09c2016-11-03 20:32:10 -0400587 ret->time = (long)time;
588 ret->timeout = (long)timeout;
589
David Benjamin8be79a32015-08-23 01:37:36 -0400590 CBS peer;
591 int has_peer;
Adam Langley68e71242016-12-12 11:06:16 -0800592 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
593 (has_peer && CBS_len(&peer) == 0)) {
David Benjamin8be79a32015-08-23 01:37:36 -0400594 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
595 goto err;
596 }
Adam Langley68e71242016-12-12 11:06:16 -0800597 /* |peer| is processed with the certificate chain. */
Adam Langley95c29f32014-06-20 12:00:00 -0700598
David Benjamin8be79a32015-08-23 01:37:36 -0400599 if (!SSL_SESSION_parse_bounded_octet_string(
600 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
601 kSessionIDContextTag) ||
602 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
603 X509_V_OK) ||
604 !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
605 kHostNameTag) ||
606 !SSL_SESSION_parse_string(&session, &ret->psk_identity,
607 kPSKIdentityTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700608 !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
David Benjamin8be79a32015-08-23 01:37:36 -0400609 kTicketLifetimeHintTag, 0) ||
610 !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
611 &ret->tlsext_ticklen, kTicketTag)) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400612 goto err;
613 }
Adam Langley95c29f32014-06-20 12:00:00 -0700614
David Benjamin8be79a32015-08-23 01:37:36 -0400615 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
David Benjamin5b7b09c2016-11-03 20:32:10 -0400616 CBS peer_sha256;
David Benjamin8be79a32015-08-23 01:37:36 -0400617 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
618 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
619 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
620 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400621 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400622 goto err;
623 }
624 memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
625 ret->peer_sha256_valid = 1;
626 } else {
627 ret->peer_sha256_valid = 0;
628 }
Adam Langley95c29f32014-06-20 12:00:00 -0700629
David Benjamin8be79a32015-08-23 01:37:36 -0400630 if (!SSL_SESSION_parse_bounded_octet_string(
631 &session, ret->original_handshake_hash,
632 &ret->original_handshake_hash_len,
633 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
634 !SSL_SESSION_parse_octet_string(
635 &session, &ret->tlsext_signed_cert_timestamp_list,
636 &ret->tlsext_signed_cert_timestamp_list_length,
637 kSignedCertTimestampListTag) ||
638 !SSL_SESSION_parse_octet_string(
639 &session, &ret->ocsp_response, &ret->ocsp_response_length,
640 kOCSPResponseTag)) {
641 goto err;
642 }
643
644 int extended_master_secret;
645 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
646 kExtendedMasterSecretTag,
647 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400648 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400649 goto err;
650 }
David Benjamin8be79a32015-08-23 01:37:36 -0400651 ret->extended_master_secret = !!extended_master_secret;
652
653 if (!SSL_SESSION_parse_u32(&session, &ret->key_exchange_info,
David Benjamin26416e92015-08-22 16:04:17 -0400654 kKeyExchangeInfoTag, 0)) {
655 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
656 goto err;
657 }
658
659 CBS cert_chain;
Adam Langley68e71242016-12-12 11:06:16 -0800660 CBS_init(&cert_chain, NULL, 0);
David Benjamin26416e92015-08-22 16:04:17 -0400661 int has_cert_chain;
662 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
Adam Langley68e71242016-12-12 11:06:16 -0800663 kCertChainTag) ||
664 (has_cert_chain && CBS_len(&cert_chain) == 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400665 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
666 goto err;
667 }
Adam Langley68e71242016-12-12 11:06:16 -0800668 if (has_cert_chain && !has_peer) {
669 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
670 goto err;
Adam Langley364f7a62016-12-12 10:51:00 -0800671 }
Adam Langley68e71242016-12-12 11:06:16 -0800672 if (has_peer || has_cert_chain) {
673 ret->certs = sk_CRYPTO_BUFFER_new_null();
674 if (ret->certs == NULL) {
David Benjamin26416e92015-08-22 16:04:17 -0400675 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
676 goto err;
677 }
Adam Langley68e71242016-12-12 11:06:16 -0800678
679 if (has_peer) {
680 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&peer, NULL);
681 if (buffer == NULL ||
682 !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
683 CRYPTO_BUFFER_free(buffer);
Adam Langleye8509092016-11-07 14:24:33 -0800684 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin26416e92015-08-22 16:04:17 -0400685 goto err;
686 }
687 }
Adam Langley68e71242016-12-12 11:06:16 -0800688
689 while (CBS_len(&cert_chain) > 0) {
690 CBS cert;
691 if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) ||
692 CBS_len(&cert) == 0) {
693 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
694 goto err;
695 }
696
697 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, NULL);
698 if (buffer == NULL ||
699 !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
700 CRYPTO_BUFFER_free(buffer);
701 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
702 goto err;
703 }
704 }
705 }
706
707 if (!ssl_session_x509_cache_objects(ret)) {
708 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
709 goto err;
David Benjamin26416e92015-08-22 16:04:17 -0400710 }
711
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400712 CBS age_add;
713 int age_add_present;
Steven Valdez5b986082016-09-01 12:29:49 -0400714 if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400715 kTicketAgeAddTag) ||
716 (age_add_present &&
717 !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
718 CBS_len(&age_add) != 0) {
719 goto err;
720 }
721 ret->ticket_age_add_valid = age_add_present;
722
Adam Langley364f7a62016-12-12 10:51:00 -0800723 int is_server;
724 if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag,
725 1 /* default to true */)) {
726 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
727 goto err;
728 }
729 /* TODO: in time we can include |is_server| for servers too, then we can
730 enforce that client and server sessions are never mixed up. */
731
732 ret->is_server = is_server;
733
David Benjamin26416e92015-08-22 16:04:17 -0400734 if (CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400735 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
736 goto err;
737 }
Adam Langley95c29f32014-06-20 12:00:00 -0700738
David Benjaminfd67aa82015-06-15 19:41:48 -0400739 return ret;
740
741err:
742 SSL_SESSION_free(ret);
743 return NULL;
744}
745
746SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) {
747 CBS cbs;
748 CBS_init(&cbs, in, in_len);
749 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
750 if (ret == NULL) {
751 return NULL;
752 }
753 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400754 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400755 SSL_SESSION_free(ret);
756 return NULL;
757 }
758 return ret;
759}
760
761SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
762 if (length < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400763 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminfd67aa82015-06-15 19:41:48 -0400764 return NULL;
765 }
766
767 CBS cbs;
768 CBS_init(&cbs, *pp, length);
769
770 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
771 if (ret == NULL) {
772 return NULL;
773 }
774
David Benjamin83fd6b62014-10-19 04:33:38 -0400775 if (a) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400776 SSL_SESSION_free(*a);
David Benjamin83fd6b62014-10-19 04:33:38 -0400777 *a = ret;
778 }
779 *pp = CBS_data(&cbs);
780 return ret;
David Benjamin83fd6b62014-10-19 04:33:38 -0400781}