blob: 4409f351284da97324b1fcab84aaa6652834201b [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 {
100 * version INTEGER (1), -- ignored
101 * sslVersion INTEGER, -- protocol version number
102 * cipher OCTET STRING, -- two bytes long
103 * sessionID OCTET STRING,
104 * masterKey OCTET STRING,
David Benjamin83fd6b62014-10-19 04:33:38 -0400105 * time [1] INTEGER OPTIONAL, -- seconds since UNIX epoch
106 * timeout [2] INTEGER OPTIONAL, -- in seconds
107 * 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 * ticketFlags [20] INTEGER OPTIONAL,
125 * ticketAgeAdd [21] OCTET STRING 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,
134 * srpUsername [12] OCTET STRING OPTIONAL, */
David Benjamin83fd6b62014-10-19 04:33:38 -0400135
Adam Langley96c2a282015-06-02 14:16:44 -0700136static const unsigned kVersion = 1;
David Benjamin7f393f72015-10-17 12:36:12 -0400137
David Benjamin83fd6b62014-10-19 04:33:38 -0400138static const int kTimeTag =
139 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
140static const int kTimeoutTag =
141 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
142static const int kPeerTag =
143 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
David Benjamin7f393f72015-10-17 12:36:12 -0400144static const int kSessionIDContextTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400145 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
146static const int kVerifyResultTag =
147 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
148static const int kHostNameTag =
149 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
David Benjamin83fd6b62014-10-19 04:33:38 -0400150static const int kPSKIdentityTag =
151 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
152static const int kTicketLifetimeHintTag =
153 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
154static const int kTicketTag =
155 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
156static const int kPeerSHA256Tag =
157 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
158static const int kOriginalHandshakeHashTag =
159 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
160static const int kSignedCertTimestampListTag =
161 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
162static const int kOCSPResponseTag =
163 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
Adam Langley75712922014-10-10 16:23:43 -0700164static const int kExtendedMasterSecretTag =
165 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200166static const int kKeyExchangeInfoTag =
167 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
David Benjamin26416e92015-08-22 16:04:17 -0400168static const int kCertChainTag =
169 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400170static const int kTicketFlagsTag =
171 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 20;
172static const int kTicketAgeAddTag =
173 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21;
David Benjamin26416e92015-08-22 16:04:17 -0400174
David Benjamin14e2b502015-09-13 14:48:12 -0400175static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400176 size_t *out_len, int for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400177 CBB cbb, session, child, child2;
Adam Langley95c29f32014-06-20 12:00:00 -0700178
David Benjaminf3a8b122014-12-25 23:11:49 -0500179 if (in == NULL || in->cipher == NULL) {
David Benjamin89abaea2014-10-19 13:50:18 -0400180 return 0;
181 }
Adam Langley95c29f32014-06-20 12:00:00 -0700182
David Benjamina8653202015-06-28 01:26:10 -0400183 CBB_zero(&cbb);
184 if (!CBB_init(&cbb, 0) ||
185 !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400186 !CBB_add_asn1_uint64(&session, kVersion) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400187 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
188 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminf3a8b122014-12-25 23:11:49 -0500189 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400190 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjamin3cac4502014-10-21 01:46:30 -0400191 /* The session ID is irrelevant for a session ticket. */
192 !CBB_add_bytes(&child, in->session_id,
193 for_ticket ? 0 : in->session_id_length) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400194 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
195 !CBB_add_bytes(&child, in->master_key, in->master_key_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400196 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400197 goto err;
198 }
Adam Langley95c29f32014-06-20 12:00:00 -0700199
David Benjamin89abaea2014-10-19 13:50:18 -0400200 if (in->time != 0) {
201 if (!CBB_add_asn1(&session, &child, kTimeTag) ||
202 !CBB_add_asn1_uint64(&child, in->time)) {
David Benjamin3570d732015-06-29 00:28:17 -0400203 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400204 goto err;
205 }
206 }
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin89abaea2014-10-19 13:50:18 -0400208 if (in->timeout != 0) {
209 if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
210 !CBB_add_asn1_uint64(&child, in->timeout)) {
David Benjamin3570d732015-06-29 00:28:17 -0400211 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400212 goto err;
213 }
214 }
Adam Langley95c29f32014-06-20 12:00:00 -0700215
David Benjamin89abaea2014-10-19 13:50:18 -0400216 /* The peer certificate is only serialized if the SHA-256 isn't
217 * serialized instead. */
218 if (in->peer && !in->peer_sha256_valid) {
David Benjamin26416e92015-08-22 16:04:17 -0400219 if (!CBB_add_asn1(&session, &child, kPeerTag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400220 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400221 goto err;
222 }
David Benjamin75836432016-06-17 18:48:29 -0400223 if (!ssl_add_cert_to_cbb(&child, in->peer)) {
David Benjamin89abaea2014-10-19 13:50:18 -0400224 goto err;
225 }
226 }
Adam Langley95c29f32014-06-20 12:00:00 -0700227
David Benjamin89abaea2014-10-19 13:50:18 -0400228 /* Although it is OPTIONAL and usually empty, OpenSSL has
229 * historically always encoded the sid_ctx. */
230 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
231 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
232 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400233 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400234 goto err;
235 }
Adam Langley95c29f32014-06-20 12:00:00 -0700236
David Benjamin89abaea2014-10-19 13:50:18 -0400237 if (in->verify_result != X509_V_OK) {
238 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
239 !CBB_add_asn1_uint64(&child, in->verify_result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400240 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400241 goto err;
242 }
243 }
Adam Langley95c29f32014-06-20 12:00:00 -0700244
David Benjamin89abaea2014-10-19 13:50:18 -0400245 if (in->tlsext_hostname) {
246 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
247 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
248 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
249 strlen(in->tlsext_hostname))) {
David Benjamin3570d732015-06-29 00:28:17 -0400250 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400251 goto err;
252 }
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
David Benjamin89abaea2014-10-19 13:50:18 -0400255 if (in->psk_identity) {
256 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
257 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
258 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
259 strlen(in->psk_identity))) {
David Benjamin3570d732015-06-29 00:28:17 -0400260 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400261 goto err;
262 }
263 }
Adam Langley95c29f32014-06-20 12:00:00 -0700264
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700265 if (in->tlsext_tick_lifetime_hint > 0) {
David Benjamin89abaea2014-10-19 13:50:18 -0400266 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700267 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
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
David Benjamin95d31822015-06-15 19:53:32 -0400273 if (in->tlsext_tick && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400274 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
275 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
276 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -0400277 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400278 goto err;
279 }
280 }
Adam Langley75872532014-06-20 12:00:00 -0700281
David Benjamin89abaea2014-10-19 13:50:18 -0400282 if (in->peer_sha256_valid) {
283 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
284 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
285 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
David Benjamin3570d732015-06-29 00:28:17 -0400286 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400287 goto err;
288 }
289 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700290
David Benjamin89abaea2014-10-19 13:50:18 -0400291 if (in->original_handshake_hash_len > 0) {
292 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
293 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
294 !CBB_add_bytes(&child2, in->original_handshake_hash,
295 in->original_handshake_hash_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400296 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400297 goto err;
298 }
299 }
Adam Langley95c29f32014-06-20 12:00:00 -0700300
David Benjamin89abaea2014-10-19 13:50:18 -0400301 if (in->tlsext_signed_cert_timestamp_list_length > 0) {
302 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
303 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
304 !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
305 in->tlsext_signed_cert_timestamp_list_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400306 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400307 goto err;
308 }
309 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200310
David Benjamin89abaea2014-10-19 13:50:18 -0400311 if (in->ocsp_response_length > 0) {
312 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
313 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
314 !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400315 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400316 goto err;
317 }
318 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400319
Adam Langley75712922014-10-10 16:23:43 -0700320 if (in->extended_master_secret) {
321 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
322 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
323 !CBB_add_u8(&child2, 0xff)) {
David Benjamin3570d732015-06-29 00:28:17 -0400324 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley75712922014-10-10 16:23:43 -0700325 goto err;
326 }
327 }
328
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200329 if (in->key_exchange_info > 0 &&
330 (!CBB_add_asn1(&session, &child, kKeyExchangeInfoTag) ||
331 !CBB_add_asn1_uint64(&child, in->key_exchange_info))) {
332 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
333 goto err;
334 }
335
David Benjamin26416e92015-08-22 16:04:17 -0400336 /* The certificate chain is only serialized if the leaf's SHA-256 isn't
337 * serialized instead. */
338 if (in->cert_chain != NULL && !in->peer_sha256_valid) {
339 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
340 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
341 goto err;
342 }
343 size_t i;
344 for (i = 0; i < sk_X509_num(in->cert_chain); i++) {
David Benjamin75836432016-06-17 18:48:29 -0400345 if (!ssl_add_cert_to_cbb(&child, sk_X509_value(in->cert_chain, i))) {
David Benjamin26416e92015-08-22 16:04:17 -0400346 goto err;
347 }
348 }
349 }
350
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400351 if (in->ticket_flags > 0) {
352 if (!CBB_add_asn1(&session, &child, kTicketFlagsTag) ||
353 !CBB_add_asn1_uint64(&child, in->ticket_flags)) {
354 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
355 goto err;
356 }
357 }
358
359 if (in->ticket_age_add_valid) {
360 if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
361 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
362 !CBB_add_u32(&child2, in->ticket_age_add)) {
363 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
364 goto err;
365 }
366 }
367
David Benjamin3cac4502014-10-21 01:46:30 -0400368 if (!CBB_finish(&cbb, out_data, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400369 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400370 goto err;
371 }
David Benjamin3cac4502014-10-21 01:46:30 -0400372 return 1;
373
374 err:
375 CBB_cleanup(&cbb);
376 return 0;
377}
378
David Benjamin14e2b502015-09-13 14:48:12 -0400379int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
380 size_t *out_len) {
Steven Valdez84b5c002016-08-25 16:30:58 -0400381 if (in->not_resumable) {
382 /* If the caller has an unresumable session, e.g. if |SSL_get_session| were
383 * called on a TLS 1.3 or False Started connection, serialize with a
384 * placeholder value so it is not accidentally deserialized into a resumable
385 * one. */
386 static const char kNotResumableSession[] = "NOT RESUMABLE";
387
388 *out_len = strlen(kNotResumableSession);
389 *out_data = BUF_memdup(kNotResumableSession, *out_len);
390 if (*out_data == NULL) {
391 return 0;
392 }
393
394 return 1;
395 }
396
David Benjamin3cac4502014-10-21 01:46:30 -0400397 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
398}
399
David Benjamin14e2b502015-09-13 14:48:12 -0400400int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400401 size_t *out_len) {
402 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
403}
404
405int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
406 uint8_t *out;
407 size_t len;
408
409 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
410 return -1;
411 }
Adam Langley95c29f32014-06-20 12:00:00 -0700412
David Benjamin89abaea2014-10-19 13:50:18 -0400413 if (len > INT_MAX) {
414 OPENSSL_free(out);
David Benjamin3570d732015-06-29 00:28:17 -0400415 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin89abaea2014-10-19 13:50:18 -0400416 return -1;
417 }
Adam Langley95c29f32014-06-20 12:00:00 -0700418
David Benjamin89abaea2014-10-19 13:50:18 -0400419 if (pp) {
420 memcpy(*pp, out, len);
421 *pp += len;
422 }
423 OPENSSL_free(out);
Adam Langley95c29f32014-06-20 12:00:00 -0700424
David Benjamin89abaea2014-10-19 13:50:18 -0400425 return len;
David Benjamin89abaea2014-10-19 13:50:18 -0400426}
Adam Langley95c29f32014-06-20 12:00:00 -0700427
David Benjaminfd67aa82015-06-15 19:41:48 -0400428/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400429 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
430 * entry, if |*out| is not NULL, it frees the existing contents. If
431 * the element was not found, it sets |*out| to NULL. It returns one
432 * on success, whether or not the element was found, and zero on
433 * decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400434static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400435 CBS value;
436 int present;
437 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400438 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400439 return 0;
440 }
441 if (present) {
442 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400443 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400444 return 0;
445 }
446 if (!CBS_strdup(&value, out)) {
David Benjamin3570d732015-06-29 00:28:17 -0400447 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400448 return 0;
449 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400450 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400451 OPENSSL_free(*out);
452 *out = NULL;
453 }
454 return 1;
455}
Adam Langley95c29f32014-06-20 12:00:00 -0700456
David Benjaminfd67aa82015-06-15 19:41:48 -0400457/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400458 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
459 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
460 * contents. On entry, if the element was not found, it sets
461 * |*out_ptr| to NULL. It returns one on success, whether or not the
462 * element was found, and zero on decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400463static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin8be79a32015-08-23 01:37:36 -0400464 size_t *out_len, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400465 CBS value;
466 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400467 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400468 return 0;
469 }
470 if (!CBS_stow(&value, out_ptr, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400471 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400472 return 0;
473 }
474 return 1;
475}
Adam Langley95c29f32014-06-20 12:00:00 -0700476
David Benjamin8be79a32015-08-23 01:37:36 -0400477/* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
478 * explicitly tagged with |tag| of size at most |max_out|. */
479static int SSL_SESSION_parse_bounded_octet_string(
480 CBS *cbs, uint8_t *out, unsigned *out_len, unsigned max_out, unsigned tag) {
481 CBS value;
482 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
483 CBS_len(&value) > max_out) {
484 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
485 return 0;
486 }
487 memcpy(out, CBS_data(&value), CBS_len(&value));
488 *out_len = (unsigned)CBS_len(&value);
489 return 1;
490}
Adam Langley95c29f32014-06-20 12:00:00 -0700491
David Benjamin8be79a32015-08-23 01:37:36 -0400492static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
493 long default_value) {
494 uint64_t value;
495 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
496 (uint64_t)default_value) ||
497 value > LONG_MAX) {
498 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
499 return 0;
500 }
501 *out = (long)value;
502 return 1;
503}
504
505static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
506 uint32_t default_value) {
507 uint64_t value;
508 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
509 (uint64_t)default_value) ||
510 value > 0xffffffff) {
511 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
512 return 0;
513 }
514 *out = (uint32_t)value;
515 return 1;
516}
517
518static X509 *parse_x509(CBS *cbs) {
David Benjaminaf073652015-11-12 23:09:30 -0800519 if (CBS_len(cbs) > LONG_MAX) {
520 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
521 return NULL;
522 }
David Benjamin8be79a32015-08-23 01:37:36 -0400523 const uint8_t *ptr = CBS_data(cbs);
David Benjaminaf073652015-11-12 23:09:30 -0800524 X509 *ret = d2i_X509(NULL, &ptr, (long)CBS_len(cbs));
David Benjamin8be79a32015-08-23 01:37:36 -0400525 if (ret == NULL) {
526 return NULL;
527 }
528 CBS_skip(cbs, ptr - CBS_data(cbs));
529 return ret;
530}
531
532static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) {
533 SSL_SESSION *ret = SSL_SESSION_new();
David Benjaminfd67aa82015-06-15 19:41:48 -0400534 if (ret == NULL) {
535 goto err;
David Benjamin83fd6b62014-10-19 04:33:38 -0400536 }
Adam Langley95c29f32014-06-20 12:00:00 -0700537
David Benjamin8be79a32015-08-23 01:37:36 -0400538 CBS session;
539 uint64_t version, ssl_version;
David Benjaminfd67aa82015-06-15 19:41:48 -0400540 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400541 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400542 version != kVersion ||
David Benjamin8be79a32015-08-23 01:37:36 -0400543 !CBS_get_asn1_uint64(&session, &ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400544 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400545 goto err;
546 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400547 /* Only support SSLv3/TLS and DTLS. */
548 if ((ssl_version >> 8) != SSL3_VERSION_MAJOR &&
549 (ssl_version >> 8) != (DTLS1_VERSION >> 8)) {
David Benjamin3570d732015-06-29 00:28:17 -0400550 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400551 goto err;
552 }
553 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 Benjamin8be79a32015-08-23 01:37:36 -0400582 if (!SSL_SESSION_parse_long(&session, &ret->time, kTimeTag, time(NULL)) ||
583 !SSL_SESSION_parse_long(&session, &ret->timeout, kTimeoutTag, 3)) {
David Benjamin3570d732015-06-29 00:28:17 -0400584 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400585 goto err;
586 }
Adam Langley95c29f32014-06-20 12:00:00 -0700587
David Benjamin8be79a32015-08-23 01:37:36 -0400588 CBS peer;
589 int has_peer;
590 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag)) {
591 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
592 goto err;
593 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400594 X509_free(ret->peer);
595 ret->peer = NULL;
David Benjamin83fd6b62014-10-19 04:33:38 -0400596 if (has_peer) {
David Benjamin8be79a32015-08-23 01:37:36 -0400597 ret->peer = parse_x509(&peer);
David Benjamin83fd6b62014-10-19 04:33:38 -0400598 if (ret->peer == NULL) {
599 goto err;
600 }
David Benjamin8be79a32015-08-23 01:37:36 -0400601 if (CBS_len(&peer) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400602 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400603 goto err;
604 }
605 }
Adam Langley95c29f32014-06-20 12:00:00 -0700606
David Benjamin8be79a32015-08-23 01:37:36 -0400607 if (!SSL_SESSION_parse_bounded_octet_string(
608 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
609 kSessionIDContextTag) ||
610 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
611 X509_V_OK) ||
612 !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
613 kHostNameTag) ||
614 !SSL_SESSION_parse_string(&session, &ret->psk_identity,
615 kPSKIdentityTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700616 !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
David Benjamin8be79a32015-08-23 01:37:36 -0400617 kTicketLifetimeHintTag, 0) ||
618 !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
619 &ret->tlsext_ticklen, kTicketTag)) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400620 goto err;
621 }
Adam Langley95c29f32014-06-20 12:00:00 -0700622
David Benjamin8be79a32015-08-23 01:37:36 -0400623 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
624 CBS child, peer_sha256;
625 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
626 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
627 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
628 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400629 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400630 goto err;
631 }
632 memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
633 ret->peer_sha256_valid = 1;
634 } else {
635 ret->peer_sha256_valid = 0;
636 }
Adam Langley95c29f32014-06-20 12:00:00 -0700637
David Benjamin8be79a32015-08-23 01:37:36 -0400638 if (!SSL_SESSION_parse_bounded_octet_string(
639 &session, ret->original_handshake_hash,
640 &ret->original_handshake_hash_len,
641 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
642 !SSL_SESSION_parse_octet_string(
643 &session, &ret->tlsext_signed_cert_timestamp_list,
644 &ret->tlsext_signed_cert_timestamp_list_length,
645 kSignedCertTimestampListTag) ||
646 !SSL_SESSION_parse_octet_string(
647 &session, &ret->ocsp_response, &ret->ocsp_response_length,
648 kOCSPResponseTag)) {
649 goto err;
650 }
651
652 int extended_master_secret;
653 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
654 kExtendedMasterSecretTag,
655 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400656 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400657 goto err;
658 }
David Benjamin8be79a32015-08-23 01:37:36 -0400659 ret->extended_master_secret = !!extended_master_secret;
660
661 if (!SSL_SESSION_parse_u32(&session, &ret->key_exchange_info,
David Benjamin26416e92015-08-22 16:04:17 -0400662 kKeyExchangeInfoTag, 0)) {
663 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
664 goto err;
665 }
666
667 CBS cert_chain;
668 int has_cert_chain;
669 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
670 kCertChainTag)) {
671 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
672 goto err;
673 }
674 sk_X509_pop_free(ret->cert_chain, X509_free);
675 ret->cert_chain = NULL;
676 if (has_cert_chain) {
677 ret->cert_chain = sk_X509_new_null();
678 if (ret->cert_chain == NULL) {
679 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
680 goto err;
681 }
682 while (CBS_len(&cert_chain) > 0) {
683 X509 *x509 = parse_x509(&cert_chain);
684 if (x509 == NULL) {
685 goto err;
686 }
687 if (!sk_X509_push(ret->cert_chain, x509)) {
688 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
689 X509_free(x509);
690 goto err;
691 }
692 }
693 }
694
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400695 CBS age_add;
696 int age_add_present;
697 if (!SSL_SESSION_parse_u32(&session, &ret->ticket_flags,
698 kTicketFlagsTag, 0) ||
699 !CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
700 kTicketAgeAddTag) ||
701 (age_add_present &&
702 !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
703 CBS_len(&age_add) != 0) {
704 goto err;
705 }
706 ret->ticket_age_add_valid = age_add_present;
707
David Benjamin26416e92015-08-22 16:04:17 -0400708 if (CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400709 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
710 goto err;
711 }
Adam Langley95c29f32014-06-20 12:00:00 -0700712
David Benjaminfd67aa82015-06-15 19:41:48 -0400713 return ret;
714
715err:
716 SSL_SESSION_free(ret);
717 return NULL;
718}
719
720SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) {
721 CBS cbs;
722 CBS_init(&cbs, in, in_len);
723 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
724 if (ret == NULL) {
725 return NULL;
726 }
727 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400728 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400729 SSL_SESSION_free(ret);
730 return NULL;
731 }
732 return ret;
733}
734
735SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
736 if (length < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400737 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminfd67aa82015-06-15 19:41:48 -0400738 return NULL;
739 }
740
741 CBS cbs;
742 CBS_init(&cbs, *pp, length);
743
744 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
745 if (ret == NULL) {
746 return NULL;
747 }
748
David Benjamin83fd6b62014-10-19 04:33:38 -0400749 if (a) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400750 SSL_SESSION_free(*a);
David Benjamin83fd6b62014-10-19 04:33:38 -0400751 *a = ret;
752 }
753 *pp = CBS_data(&cbs);
754 return ret;
David Benjamin83fd6b62014-10-19 04:33:38 -0400755}