blob: aab6052c5fa8f5746cf09d84f42253478f72899e [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,
David Benjamin83fd6b62014-10-19 04:33:38 -0400125 * }
126 *
David Benjamin688d8df2014-11-02 23:06:42 -0500127 * Note: historically this serialization has included other optional
128 * fields. Their presense is currently treated as a parse error:
129 *
130 * keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
131 * pskIdentityHint [7] OCTET STRING OPTIONAL,
132 * compressionMethod [11] OCTET STRING OPTIONAL,
Steven Valdez5b986082016-09-01 12:29:49 -0400133 * srpUsername [12] OCTET STRING OPTIONAL,
134 * ticketFlags [20] INTEGER OPTIONAL,
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800135 */
David Benjamin83fd6b62014-10-19 04:33:38 -0400136
Adam Langley96c2a282015-06-02 14:16:44 -0700137static const unsigned kVersion = 1;
David Benjamin7f393f72015-10-17 12:36:12 -0400138
David Benjamin83fd6b62014-10-19 04:33:38 -0400139static const int kTimeTag =
140 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
141static const int kTimeoutTag =
142 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
143static const int kPeerTag =
144 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
David Benjamin7f393f72015-10-17 12:36:12 -0400145static const int kSessionIDContextTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400146 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
147static const int kVerifyResultTag =
148 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
149static const int kHostNameTag =
150 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
David Benjamin83fd6b62014-10-19 04:33:38 -0400151static const int kPSKIdentityTag =
152 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
153static const int kTicketLifetimeHintTag =
154 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
155static const int kTicketTag =
156 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
157static const int kPeerSHA256Tag =
158 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
159static const int kOriginalHandshakeHashTag =
160 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
161static const int kSignedCertTimestampListTag =
162 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
163static const int kOCSPResponseTag =
164 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
Adam Langley75712922014-10-10 16:23:43 -0700165static const int kExtendedMasterSecretTag =
166 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200167static const int kKeyExchangeInfoTag =
168 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
David Benjamin26416e92015-08-22 16:04:17 -0400169static const int kCertChainTag =
170 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400171static const int kTicketAgeAddTag =
172 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21;
David Benjamin26416e92015-08-22 16:04:17 -0400173
David Benjamin14e2b502015-09-13 14:48:12 -0400174static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400175 size_t *out_len, int for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400176 CBB cbb, session, child, child2;
Adam Langley95c29f32014-06-20 12:00:00 -0700177
David Benjaminf3a8b122014-12-25 23:11:49 -0500178 if (in == NULL || in->cipher == NULL) {
David Benjamin89abaea2014-10-19 13:50:18 -0400179 return 0;
180 }
Adam Langley95c29f32014-06-20 12:00:00 -0700181
David Benjamina8653202015-06-28 01:26:10 -0400182 CBB_zero(&cbb);
183 if (!CBB_init(&cbb, 0) ||
184 !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400185 !CBB_add_asn1_uint64(&session, kVersion) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400186 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
187 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminf3a8b122014-12-25 23:11:49 -0500188 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400189 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjamin3cac4502014-10-21 01:46:30 -0400190 /* The session ID is irrelevant for a session ticket. */
191 !CBB_add_bytes(&child, in->session_id,
192 for_ticket ? 0 : in->session_id_length) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400193 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
194 !CBB_add_bytes(&child, in->master_key, in->master_key_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400195 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400196 goto err;
197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjamin5b7b09c2016-11-03 20:32:10 -0400199 if (in->time < 0) {
200 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
201 goto err;
David Benjamin89abaea2014-10-19 13:50:18 -0400202 }
Adam Langley95c29f32014-06-20 12:00:00 -0700203
David Benjamin5b7b09c2016-11-03 20:32:10 -0400204 if (!CBB_add_asn1(&session, &child, kTimeTag) ||
205 !CBB_add_asn1_uint64(&child, in->time)) {
206 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
207 goto err;
208 }
David Benjamin0f31ac72016-11-02 23:05:14 -0400209
David Benjamin5b7b09c2016-11-03 20:32:10 -0400210 if (in->timeout < 0) {
211 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
212 goto err;
213 }
214
215 if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
216 !CBB_add_asn1_uint64(&child, in->timeout)) {
217 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
218 goto err;
David Benjamin89abaea2014-10-19 13:50:18 -0400219 }
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800221 /* The peer certificate is only serialized if the SHA-256 isn't
222 * serialized instead. */
223 if (in->x509_peer && !in->peer_sha256_valid) {
224 if (!CBB_add_asn1(&session, &child, kPeerTag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400225 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400226 goto err;
227 }
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800228 if (!ssl_add_cert_to_cbb(&child, in->x509_peer)) {
229 goto err;
230 }
David Benjamin89abaea2014-10-19 13:50:18 -0400231 }
Adam Langley95c29f32014-06-20 12:00:00 -0700232
David Benjamin89abaea2014-10-19 13:50:18 -0400233 /* Although it is OPTIONAL and usually empty, OpenSSL has
234 * historically always encoded the sid_ctx. */
235 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
236 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
237 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
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 }
Adam Langley95c29f32014-06-20 12:00:00 -0700241
David Benjamin89abaea2014-10-19 13:50:18 -0400242 if (in->verify_result != X509_V_OK) {
243 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
244 !CBB_add_asn1_uint64(&child, in->verify_result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400245 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400246 goto err;
247 }
248 }
Adam Langley95c29f32014-06-20 12:00:00 -0700249
David Benjamin89abaea2014-10-19 13:50:18 -0400250 if (in->tlsext_hostname) {
251 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
252 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
253 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
254 strlen(in->tlsext_hostname))) {
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->psk_identity) {
261 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
262 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
263 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
264 strlen(in->psk_identity))) {
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
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700270 if (in->tlsext_tick_lifetime_hint > 0) {
David Benjamin89abaea2014-10-19 13:50:18 -0400271 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700272 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400273 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400274 goto err;
275 }
276 }
Adam Langley95c29f32014-06-20 12:00:00 -0700277
David Benjamin95d31822015-06-15 19:53:32 -0400278 if (in->tlsext_tick && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400279 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
280 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
281 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -0400282 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400283 goto err;
284 }
285 }
Adam Langley75872532014-06-20 12:00:00 -0700286
David Benjamin89abaea2014-10-19 13:50:18 -0400287 if (in->peer_sha256_valid) {
288 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
289 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
290 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
David Benjamin3570d732015-06-29 00:28:17 -0400291 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400292 goto err;
293 }
294 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700295
David Benjamin89abaea2014-10-19 13:50:18 -0400296 if (in->original_handshake_hash_len > 0) {
297 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
298 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
299 !CBB_add_bytes(&child2, in->original_handshake_hash,
300 in->original_handshake_hash_len)) {
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 Langley95c29f32014-06-20 12:00:00 -0700305
David Benjamin89abaea2014-10-19 13:50:18 -0400306 if (in->tlsext_signed_cert_timestamp_list_length > 0) {
307 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
308 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
309 !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
310 in->tlsext_signed_cert_timestamp_list_length)) {
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 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200315
David Benjamin89abaea2014-10-19 13:50:18 -0400316 if (in->ocsp_response_length > 0) {
317 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
318 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
319 !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400320 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400321 goto err;
322 }
323 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400324
Adam Langley75712922014-10-10 16:23:43 -0700325 if (in->extended_master_secret) {
326 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
327 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
328 !CBB_add_u8(&child2, 0xff)) {
David Benjamin3570d732015-06-29 00:28:17 -0400329 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley75712922014-10-10 16:23:43 -0700330 goto err;
331 }
332 }
333
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200334 if (in->key_exchange_info > 0 &&
335 (!CBB_add_asn1(&session, &child, kKeyExchangeInfoTag) ||
336 !CBB_add_asn1_uint64(&child, in->key_exchange_info))) {
337 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
338 goto err;
339 }
340
David Benjamin26416e92015-08-22 16:04:17 -0400341 /* The certificate chain is only serialized if the leaf's SHA-256 isn't
342 * serialized instead. */
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800343 if (in->x509_chain != NULL && !in->peer_sha256_valid) {
David Benjamin26416e92015-08-22 16:04:17 -0400344 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
345 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
346 goto err;
347 }
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800348 for (size_t i = 0; i < sk_X509_num(in->x509_chain); i++) {
349 if (!ssl_add_cert_to_cbb(&child, sk_X509_value(in->x509_chain, i))) {
David Benjamin26416e92015-08-22 16:04:17 -0400350 goto err;
351 }
352 }
353 }
354
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400355 if (in->ticket_age_add_valid) {
356 if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
357 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
358 !CBB_add_u32(&child2, in->ticket_age_add)) {
359 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
360 goto err;
361 }
362 }
363
David Benjamin3cac4502014-10-21 01:46:30 -0400364 if (!CBB_finish(&cbb, out_data, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400365 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400366 goto err;
367 }
David Benjamin3cac4502014-10-21 01:46:30 -0400368 return 1;
369
370 err:
371 CBB_cleanup(&cbb);
372 return 0;
373}
374
David Benjamin14e2b502015-09-13 14:48:12 -0400375int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
376 size_t *out_len) {
Steven Valdez84b5c002016-08-25 16:30:58 -0400377 if (in->not_resumable) {
378 /* If the caller has an unresumable session, e.g. if |SSL_get_session| were
379 * called on a TLS 1.3 or False Started connection, serialize with a
380 * placeholder value so it is not accidentally deserialized into a resumable
381 * one. */
382 static const char kNotResumableSession[] = "NOT RESUMABLE";
383
384 *out_len = strlen(kNotResumableSession);
385 *out_data = BUF_memdup(kNotResumableSession, *out_len);
386 if (*out_data == NULL) {
387 return 0;
388 }
389
390 return 1;
391 }
392
David Benjamin3cac4502014-10-21 01:46:30 -0400393 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
394}
395
David Benjamin14e2b502015-09-13 14:48:12 -0400396int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400397 size_t *out_len) {
398 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
399}
400
401int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
402 uint8_t *out;
403 size_t len;
404
405 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
406 return -1;
407 }
Adam Langley95c29f32014-06-20 12:00:00 -0700408
David Benjamin89abaea2014-10-19 13:50:18 -0400409 if (len > INT_MAX) {
410 OPENSSL_free(out);
David Benjamin3570d732015-06-29 00:28:17 -0400411 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin89abaea2014-10-19 13:50:18 -0400412 return -1;
413 }
Adam Langley95c29f32014-06-20 12:00:00 -0700414
David Benjamin89abaea2014-10-19 13:50:18 -0400415 if (pp) {
416 memcpy(*pp, out, len);
417 *pp += len;
418 }
419 OPENSSL_free(out);
Adam Langley95c29f32014-06-20 12:00:00 -0700420
David Benjamin89abaea2014-10-19 13:50:18 -0400421 return len;
David Benjamin89abaea2014-10-19 13:50:18 -0400422}
Adam Langley95c29f32014-06-20 12:00:00 -0700423
David Benjaminfd67aa82015-06-15 19:41:48 -0400424/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400425 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
426 * entry, if |*out| is not NULL, it frees the existing contents. If
427 * the element was not found, it sets |*out| to NULL. It returns one
428 * on success, whether or not the element was found, and zero on
429 * decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400430static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400431 CBS value;
432 int present;
433 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400434 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400435 return 0;
436 }
437 if (present) {
438 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400439 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400440 return 0;
441 }
442 if (!CBS_strdup(&value, out)) {
David Benjamin3570d732015-06-29 00:28:17 -0400443 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400444 return 0;
445 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400446 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400447 OPENSSL_free(*out);
448 *out = NULL;
449 }
450 return 1;
451}
Adam Langley95c29f32014-06-20 12:00:00 -0700452
David Benjaminfd67aa82015-06-15 19:41:48 -0400453/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400454 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
455 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
456 * contents. On entry, if the element was not found, it sets
457 * |*out_ptr| to NULL. It returns one on success, whether or not the
458 * element was found, and zero on decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400459static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin8be79a32015-08-23 01:37:36 -0400460 size_t *out_len, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400461 CBS value;
462 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
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_stow(&value, out_ptr, out_len)) {
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 }
470 return 1;
471}
Adam Langley95c29f32014-06-20 12:00:00 -0700472
David Benjamin8be79a32015-08-23 01:37:36 -0400473/* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
474 * explicitly tagged with |tag| of size at most |max_out|. */
475static int SSL_SESSION_parse_bounded_octet_string(
David Benjamin30c4c302016-12-07 22:35:24 -0500476 CBS *cbs, uint8_t *out, uint8_t *out_len, uint8_t max_out, unsigned tag) {
David Benjamin8be79a32015-08-23 01:37:36 -0400477 CBS value;
478 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
479 CBS_len(&value) > max_out) {
480 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
481 return 0;
482 }
483 memcpy(out, CBS_data(&value), CBS_len(&value));
David Benjamin30c4c302016-12-07 22:35:24 -0500484 *out_len = (uint8_t)CBS_len(&value);
David Benjamin8be79a32015-08-23 01:37:36 -0400485 return 1;
486}
Adam Langley95c29f32014-06-20 12:00:00 -0700487
David Benjamin8be79a32015-08-23 01:37:36 -0400488static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
489 long default_value) {
490 uint64_t value;
491 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
492 (uint64_t)default_value) ||
493 value > LONG_MAX) {
494 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
495 return 0;
496 }
497 *out = (long)value;
498 return 1;
499}
500
501static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
502 uint32_t default_value) {
503 uint64_t value;
504 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
505 (uint64_t)default_value) ||
506 value > 0xffffffff) {
507 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
508 return 0;
509 }
510 *out = (uint32_t)value;
511 return 1;
512}
513
David Benjamin8be79a32015-08-23 01:37:36 -0400514static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) {
515 SSL_SESSION *ret = SSL_SESSION_new();
David Benjaminfd67aa82015-06-15 19:41:48 -0400516 if (ret == NULL) {
517 goto err;
David Benjamin83fd6b62014-10-19 04:33:38 -0400518 }
Adam Langley95c29f32014-06-20 12:00:00 -0700519
David Benjamin8be79a32015-08-23 01:37:36 -0400520 CBS session;
521 uint64_t version, ssl_version;
David Benjaminfd67aa82015-06-15 19:41:48 -0400522 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400523 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400524 version != kVersion ||
David Benjamin8be79a32015-08-23 01:37:36 -0400525 !CBS_get_asn1_uint64(&session, &ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400526 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400527 goto err;
528 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400529 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700530
David Benjamin8be79a32015-08-23 01:37:36 -0400531 CBS cipher;
David Benjaminf3a8b122014-12-25 23:11:49 -0500532 uint16_t cipher_value;
David Benjamin8be79a32015-08-23 01:37:36 -0400533 if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
534 !CBS_get_u16(&cipher, &cipher_value) ||
535 CBS_len(&cipher) != 0) {
536 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400537 goto err;
538 }
David Benjamina1c90a52015-05-30 17:03:14 -0400539 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400540 if (ret->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400541 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin83fd6b62014-10-19 04:33:38 -0400542 goto err;
543 }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
David Benjamin8be79a32015-08-23 01:37:36 -0400545 CBS session_id, master_key;
546 if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
547 CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
548 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
549 CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
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 }
553 memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
554 ret->session_id_length = CBS_len(&session_id);
David Benjamin83fd6b62014-10-19 04:33:38 -0400555 memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
556 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700557
David Benjamin5b7b09c2016-11-03 20:32:10 -0400558 CBS child;
559 uint64_t time, timeout;
560 if (!CBS_get_asn1(&session, &child, kTimeTag) ||
561 !CBS_get_asn1_uint64(&child, &time) ||
562 time > LONG_MAX ||
563 !CBS_get_asn1(&session, &child, kTimeoutTag) ||
564 !CBS_get_asn1_uint64(&child, &timeout) ||
565 timeout > LONG_MAX) {
David Benjamin3570d732015-06-29 00:28:17 -0400566 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400567 goto err;
568 }
Adam Langley95c29f32014-06-20 12:00:00 -0700569
David Benjamin5b7b09c2016-11-03 20:32:10 -0400570 ret->time = (long)time;
571 ret->timeout = (long)timeout;
572
David Benjamin8be79a32015-08-23 01:37:36 -0400573 CBS peer;
574 int has_peer;
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800575 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag)) {
David Benjamin8be79a32015-08-23 01:37:36 -0400576 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
577 goto err;
578 }
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800579 X509_free(ret->x509_peer);
580 ret->x509_peer = NULL;
581 if (has_peer) {
582 ret->x509_peer = ssl_parse_x509(&peer);
583 if (ret->x509_peer == NULL) {
584 goto err;
585 }
586 if (CBS_len(&peer) != 0) {
587 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
588 goto err;
589 }
590 }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
David Benjamin8be79a32015-08-23 01:37:36 -0400592 if (!SSL_SESSION_parse_bounded_octet_string(
593 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
594 kSessionIDContextTag) ||
595 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
596 X509_V_OK) ||
597 !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
598 kHostNameTag) ||
599 !SSL_SESSION_parse_string(&session, &ret->psk_identity,
600 kPSKIdentityTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700601 !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
David Benjamin8be79a32015-08-23 01:37:36 -0400602 kTicketLifetimeHintTag, 0) ||
603 !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
604 &ret->tlsext_ticklen, kTicketTag)) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400605 goto err;
606 }
Adam Langley95c29f32014-06-20 12:00:00 -0700607
David Benjamin8be79a32015-08-23 01:37:36 -0400608 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
David Benjamin5b7b09c2016-11-03 20:32:10 -0400609 CBS peer_sha256;
David Benjamin8be79a32015-08-23 01:37:36 -0400610 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
611 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
612 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
613 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400614 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400615 goto err;
616 }
617 memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
618 ret->peer_sha256_valid = 1;
619 } else {
620 ret->peer_sha256_valid = 0;
621 }
Adam Langley95c29f32014-06-20 12:00:00 -0700622
David Benjamin8be79a32015-08-23 01:37:36 -0400623 if (!SSL_SESSION_parse_bounded_octet_string(
624 &session, ret->original_handshake_hash,
625 &ret->original_handshake_hash_len,
626 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
627 !SSL_SESSION_parse_octet_string(
628 &session, &ret->tlsext_signed_cert_timestamp_list,
629 &ret->tlsext_signed_cert_timestamp_list_length,
630 kSignedCertTimestampListTag) ||
631 !SSL_SESSION_parse_octet_string(
632 &session, &ret->ocsp_response, &ret->ocsp_response_length,
633 kOCSPResponseTag)) {
634 goto err;
635 }
636
637 int extended_master_secret;
638 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
639 kExtendedMasterSecretTag,
640 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400641 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400642 goto err;
643 }
David Benjamin8be79a32015-08-23 01:37:36 -0400644 ret->extended_master_secret = !!extended_master_secret;
645
646 if (!SSL_SESSION_parse_u32(&session, &ret->key_exchange_info,
David Benjamin26416e92015-08-22 16:04:17 -0400647 kKeyExchangeInfoTag, 0)) {
648 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
649 goto err;
650 }
651
652 CBS cert_chain;
653 int has_cert_chain;
654 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800655 kCertChainTag)) {
David Benjamin26416e92015-08-22 16:04:17 -0400656 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
657 goto err;
658 }
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800659 sk_X509_pop_free(ret->x509_chain, X509_free);
660 ret->x509_chain = NULL;
661 if (has_cert_chain) {
662 ret->x509_chain = sk_X509_new_null();
663 if (ret->x509_chain == NULL) {
David Benjamin26416e92015-08-22 16:04:17 -0400664 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
665 goto err;
666 }
Adam Langleye8509092016-11-07 14:24:33 -0800667 while (CBS_len(&cert_chain) > 0) {
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800668 X509 *x509 = ssl_parse_x509(&cert_chain);
669 if (x509 == NULL) {
Adam Langleye8509092016-11-07 14:24:33 -0800670 goto err;
671 }
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800672 if (!sk_X509_push(ret->x509_chain, x509)) {
Adam Langleye8509092016-11-07 14:24:33 -0800673 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800674 X509_free(x509);
David Benjamin26416e92015-08-22 16:04:17 -0400675 goto err;
676 }
677 }
678 }
679
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400680 CBS age_add;
681 int age_add_present;
Steven Valdez5b986082016-09-01 12:29:49 -0400682 if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400683 kTicketAgeAddTag) ||
684 (age_add_present &&
685 !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
686 CBS_len(&age_add) != 0) {
687 goto err;
688 }
689 ret->ticket_age_add_valid = age_add_present;
690
David Benjamin26416e92015-08-22 16:04:17 -0400691 if (CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400692 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
693 goto err;
694 }
Adam Langley95c29f32014-06-20 12:00:00 -0700695
David Benjaminfd67aa82015-06-15 19:41:48 -0400696 return ret;
697
698err:
699 SSL_SESSION_free(ret);
700 return NULL;
701}
702
703SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) {
704 CBS cbs;
705 CBS_init(&cbs, in, in_len);
706 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
707 if (ret == NULL) {
708 return NULL;
709 }
710 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400711 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400712 SSL_SESSION_free(ret);
713 return NULL;
714 }
715 return ret;
716}
717
718SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
719 if (length < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400720 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminfd67aa82015-06-15 19:41:48 -0400721 return NULL;
722 }
723
724 CBS cbs;
725 CBS_init(&cbs, *pp, length);
726
727 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
728 if (ret == NULL) {
729 return NULL;
730 }
731
David Benjamin83fd6b62014-10-19 04:33:38 -0400732 if (a) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400733 SSL_SESSION_free(*a);
David Benjamin83fd6b62014-10-19 04:33:38 -0400734 *a = ret;
735 }
736 *pp = CBS_data(&cbs);
737 return ret;
David Benjamin83fd6b62014-10-19 04:33:38 -0400738}