blob: f81450b43fccbe688c5e3bace317f13c4aaa4ac0 [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 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 * 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,
135 */
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 Benjamin89abaea2014-10-19 13:50:18 -0400199 if (in->time != 0) {
200 if (!CBB_add_asn1(&session, &child, kTimeTag) ||
201 !CBB_add_asn1_uint64(&child, in->time)) {
David Benjamin3570d732015-06-29 00:28:17 -0400202 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400203 goto err;
204 }
205 }
Adam Langley95c29f32014-06-20 12:00:00 -0700206
David Benjamin89abaea2014-10-19 13:50:18 -0400207 if (in->timeout != 0) {
208 if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
209 !CBB_add_asn1_uint64(&child, in->timeout)) {
David Benjamin3570d732015-06-29 00:28:17 -0400210 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400211 goto err;
212 }
213 }
Adam Langley95c29f32014-06-20 12:00:00 -0700214
David Benjamin89abaea2014-10-19 13:50:18 -0400215 /* The peer certificate is only serialized if the SHA-256 isn't
216 * serialized instead. */
217 if (in->peer && !in->peer_sha256_valid) {
David Benjamin26416e92015-08-22 16:04:17 -0400218 if (!CBB_add_asn1(&session, &child, kPeerTag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400219 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400220 goto err;
221 }
David Benjamin75836432016-06-17 18:48:29 -0400222 if (!ssl_add_cert_to_cbb(&child, in->peer)) {
David Benjamin89abaea2014-10-19 13:50:18 -0400223 goto err;
224 }
225 }
Adam Langley95c29f32014-06-20 12:00:00 -0700226
David Benjamin89abaea2014-10-19 13:50:18 -0400227 /* Although it is OPTIONAL and usually empty, OpenSSL has
228 * historically always encoded the sid_ctx. */
229 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
230 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
231 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400232 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400233 goto err;
234 }
Adam Langley95c29f32014-06-20 12:00:00 -0700235
David Benjamin89abaea2014-10-19 13:50:18 -0400236 if (in->verify_result != X509_V_OK) {
237 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
238 !CBB_add_asn1_uint64(&child, in->verify_result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400239 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400240 goto err;
241 }
242 }
Adam Langley95c29f32014-06-20 12:00:00 -0700243
David Benjamin89abaea2014-10-19 13:50:18 -0400244 if (in->tlsext_hostname) {
245 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
246 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
247 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
248 strlen(in->tlsext_hostname))) {
David Benjamin3570d732015-06-29 00:28:17 -0400249 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400250 goto err;
251 }
252 }
Adam Langley95c29f32014-06-20 12:00:00 -0700253
David Benjamin89abaea2014-10-19 13:50:18 -0400254 if (in->psk_identity) {
255 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
256 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
257 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
258 strlen(in->psk_identity))) {
David Benjamin3570d732015-06-29 00:28:17 -0400259 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400260 goto err;
261 }
262 }
Adam Langley95c29f32014-06-20 12:00:00 -0700263
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700264 if (in->tlsext_tick_lifetime_hint > 0) {
David Benjamin89abaea2014-10-19 13:50:18 -0400265 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700266 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400267 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400268 goto err;
269 }
270 }
Adam Langley95c29f32014-06-20 12:00:00 -0700271
David Benjamin95d31822015-06-15 19:53:32 -0400272 if (in->tlsext_tick && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400273 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
274 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
275 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
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 Langley75872532014-06-20 12:00:00 -0700280
David Benjamin89abaea2014-10-19 13:50:18 -0400281 if (in->peer_sha256_valid) {
282 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
283 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
284 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
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 Langley1258b6a2014-06-20 12:00:00 -0700289
David Benjamin89abaea2014-10-19 13:50:18 -0400290 if (in->original_handshake_hash_len > 0) {
291 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
292 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
293 !CBB_add_bytes(&child2, in->original_handshake_hash,
294 in->original_handshake_hash_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400295 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400296 goto err;
297 }
298 }
Adam Langley95c29f32014-06-20 12:00:00 -0700299
David Benjamin89abaea2014-10-19 13:50:18 -0400300 if (in->tlsext_signed_cert_timestamp_list_length > 0) {
301 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
302 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
303 !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
304 in->tlsext_signed_cert_timestamp_list_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400305 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400306 goto err;
307 }
308 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200309
David Benjamin89abaea2014-10-19 13:50:18 -0400310 if (in->ocsp_response_length > 0) {
311 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
312 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
313 !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_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 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400318
Adam Langley75712922014-10-10 16:23:43 -0700319 if (in->extended_master_secret) {
320 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
321 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
322 !CBB_add_u8(&child2, 0xff)) {
David Benjamin3570d732015-06-29 00:28:17 -0400323 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley75712922014-10-10 16:23:43 -0700324 goto err;
325 }
326 }
327
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200328 if (in->key_exchange_info > 0 &&
329 (!CBB_add_asn1(&session, &child, kKeyExchangeInfoTag) ||
330 !CBB_add_asn1_uint64(&child, in->key_exchange_info))) {
331 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
332 goto err;
333 }
334
David Benjamin26416e92015-08-22 16:04:17 -0400335 /* The certificate chain is only serialized if the leaf's SHA-256 isn't
336 * serialized instead. */
337 if (in->cert_chain != NULL && !in->peer_sha256_valid) {
338 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
339 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
340 goto err;
341 }
David Benjamin54091232016-09-05 12:47:25 -0400342 for (size_t i = 0; i < sk_X509_num(in->cert_chain); i++) {
David Benjamin75836432016-06-17 18:48:29 -0400343 if (!ssl_add_cert_to_cbb(&child, sk_X509_value(in->cert_chain, i))) {
David Benjamin26416e92015-08-22 16:04:17 -0400344 goto err;
345 }
346 }
347 }
348
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400349 if (in->ticket_age_add_valid) {
350 if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
351 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
352 !CBB_add_u32(&child2, in->ticket_age_add)) {
353 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
354 goto err;
355 }
356 }
357
David Benjamin3cac4502014-10-21 01:46:30 -0400358 if (!CBB_finish(&cbb, out_data, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400359 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400360 goto err;
361 }
David Benjamin3cac4502014-10-21 01:46:30 -0400362 return 1;
363
364 err:
365 CBB_cleanup(&cbb);
366 return 0;
367}
368
David Benjamin14e2b502015-09-13 14:48:12 -0400369int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
370 size_t *out_len) {
Steven Valdez84b5c002016-08-25 16:30:58 -0400371 if (in->not_resumable) {
372 /* If the caller has an unresumable session, e.g. if |SSL_get_session| were
373 * called on a TLS 1.3 or False Started connection, serialize with a
374 * placeholder value so it is not accidentally deserialized into a resumable
375 * one. */
376 static const char kNotResumableSession[] = "NOT RESUMABLE";
377
378 *out_len = strlen(kNotResumableSession);
379 *out_data = BUF_memdup(kNotResumableSession, *out_len);
380 if (*out_data == NULL) {
381 return 0;
382 }
383
384 return 1;
385 }
386
David Benjamin3cac4502014-10-21 01:46:30 -0400387 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
388}
389
David Benjamin14e2b502015-09-13 14:48:12 -0400390int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400391 size_t *out_len) {
392 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
393}
394
395int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
396 uint8_t *out;
397 size_t len;
398
399 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
400 return -1;
401 }
Adam Langley95c29f32014-06-20 12:00:00 -0700402
David Benjamin89abaea2014-10-19 13:50:18 -0400403 if (len > INT_MAX) {
404 OPENSSL_free(out);
David Benjamin3570d732015-06-29 00:28:17 -0400405 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin89abaea2014-10-19 13:50:18 -0400406 return -1;
407 }
Adam Langley95c29f32014-06-20 12:00:00 -0700408
David Benjamin89abaea2014-10-19 13:50:18 -0400409 if (pp) {
410 memcpy(*pp, out, len);
411 *pp += len;
412 }
413 OPENSSL_free(out);
Adam Langley95c29f32014-06-20 12:00:00 -0700414
David Benjamin89abaea2014-10-19 13:50:18 -0400415 return len;
David Benjamin89abaea2014-10-19 13:50:18 -0400416}
Adam Langley95c29f32014-06-20 12:00:00 -0700417
David Benjaminfd67aa82015-06-15 19:41:48 -0400418/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400419 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
420 * entry, if |*out| is not NULL, it frees the existing contents. If
421 * the element was not found, it sets |*out| to NULL. It returns one
422 * on success, whether or not the element was found, and zero on
423 * decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400424static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400425 CBS value;
426 int present;
427 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400428 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400429 return 0;
430 }
431 if (present) {
432 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400433 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400434 return 0;
435 }
436 if (!CBS_strdup(&value, out)) {
David Benjamin3570d732015-06-29 00:28:17 -0400437 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400438 return 0;
439 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400440 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400441 OPENSSL_free(*out);
442 *out = NULL;
443 }
444 return 1;
445}
Adam Langley95c29f32014-06-20 12:00:00 -0700446
David Benjaminfd67aa82015-06-15 19:41:48 -0400447/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400448 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
449 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
450 * contents. On entry, if the element was not found, it sets
451 * |*out_ptr| to NULL. It returns one on success, whether or not the
452 * element was found, and zero on decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400453static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin8be79a32015-08-23 01:37:36 -0400454 size_t *out_len, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400455 CBS value;
456 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400457 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400458 return 0;
459 }
460 if (!CBS_stow(&value, out_ptr, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400461 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400462 return 0;
463 }
464 return 1;
465}
Adam Langley95c29f32014-06-20 12:00:00 -0700466
David Benjamin8be79a32015-08-23 01:37:36 -0400467/* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
468 * explicitly tagged with |tag| of size at most |max_out|. */
469static int SSL_SESSION_parse_bounded_octet_string(
470 CBS *cbs, uint8_t *out, unsigned *out_len, unsigned max_out, unsigned tag) {
471 CBS value;
472 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
473 CBS_len(&value) > max_out) {
474 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
475 return 0;
476 }
477 memcpy(out, CBS_data(&value), CBS_len(&value));
478 *out_len = (unsigned)CBS_len(&value);
479 return 1;
480}
Adam Langley95c29f32014-06-20 12:00:00 -0700481
David Benjamin8be79a32015-08-23 01:37:36 -0400482static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
483 long default_value) {
484 uint64_t value;
485 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
486 (uint64_t)default_value) ||
487 value > LONG_MAX) {
488 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
489 return 0;
490 }
491 *out = (long)value;
492 return 1;
493}
494
495static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
496 uint32_t default_value) {
497 uint64_t value;
498 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
499 (uint64_t)default_value) ||
500 value > 0xffffffff) {
501 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
502 return 0;
503 }
504 *out = (uint32_t)value;
505 return 1;
506}
507
508static X509 *parse_x509(CBS *cbs) {
David Benjaminaf073652015-11-12 23:09:30 -0800509 if (CBS_len(cbs) > LONG_MAX) {
510 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
511 return NULL;
512 }
David Benjamin8be79a32015-08-23 01:37:36 -0400513 const uint8_t *ptr = CBS_data(cbs);
David Benjaminaf073652015-11-12 23:09:30 -0800514 X509 *ret = d2i_X509(NULL, &ptr, (long)CBS_len(cbs));
David Benjamin8be79a32015-08-23 01:37:36 -0400515 if (ret == NULL) {
516 return NULL;
517 }
518 CBS_skip(cbs, ptr - CBS_data(cbs));
519 return ret;
520}
521
522static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) {
523 SSL_SESSION *ret = SSL_SESSION_new();
David Benjaminfd67aa82015-06-15 19:41:48 -0400524 if (ret == NULL) {
525 goto err;
David Benjamin83fd6b62014-10-19 04:33:38 -0400526 }
Adam Langley95c29f32014-06-20 12:00:00 -0700527
David Benjamin8be79a32015-08-23 01:37:36 -0400528 CBS session;
529 uint64_t version, ssl_version;
David Benjaminfd67aa82015-06-15 19:41:48 -0400530 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400531 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400532 version != kVersion ||
David Benjamin8be79a32015-08-23 01:37:36 -0400533 !CBS_get_asn1_uint64(&session, &ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400534 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400535 goto err;
536 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400537 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700538
David Benjamin8be79a32015-08-23 01:37:36 -0400539 CBS cipher;
David Benjaminf3a8b122014-12-25 23:11:49 -0500540 uint16_t cipher_value;
David Benjamin8be79a32015-08-23 01:37:36 -0400541 if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
542 !CBS_get_u16(&cipher, &cipher_value) ||
543 CBS_len(&cipher) != 0) {
544 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400545 goto err;
546 }
David Benjamina1c90a52015-05-30 17:03:14 -0400547 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400548 if (ret->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400549 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin83fd6b62014-10-19 04:33:38 -0400550 goto err;
551 }
Adam Langley95c29f32014-06-20 12:00:00 -0700552
David Benjamin8be79a32015-08-23 01:37:36 -0400553 CBS session_id, master_key;
554 if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
555 CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
556 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
557 CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400558 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400559 goto err;
560 }
561 memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
562 ret->session_id_length = CBS_len(&session_id);
David Benjamin83fd6b62014-10-19 04:33:38 -0400563 memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
564 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700565
David Benjamin8be79a32015-08-23 01:37:36 -0400566 if (!SSL_SESSION_parse_long(&session, &ret->time, kTimeTag, time(NULL)) ||
567 !SSL_SESSION_parse_long(&session, &ret->timeout, kTimeoutTag, 3)) {
David Benjamin3570d732015-06-29 00:28:17 -0400568 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400569 goto err;
570 }
Adam Langley95c29f32014-06-20 12:00:00 -0700571
David Benjamin8be79a32015-08-23 01:37:36 -0400572 CBS peer;
573 int has_peer;
574 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag)) {
575 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
576 goto err;
577 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400578 X509_free(ret->peer);
579 ret->peer = NULL;
David Benjamin83fd6b62014-10-19 04:33:38 -0400580 if (has_peer) {
David Benjamin8be79a32015-08-23 01:37:36 -0400581 ret->peer = parse_x509(&peer);
David Benjamin83fd6b62014-10-19 04:33:38 -0400582 if (ret->peer == NULL) {
583 goto err;
584 }
David Benjamin8be79a32015-08-23 01:37:36 -0400585 if (CBS_len(&peer) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400586 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400587 goto err;
588 }
589 }
Adam Langley95c29f32014-06-20 12:00:00 -0700590
David Benjamin8be79a32015-08-23 01:37:36 -0400591 if (!SSL_SESSION_parse_bounded_octet_string(
592 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
593 kSessionIDContextTag) ||
594 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
595 X509_V_OK) ||
596 !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
597 kHostNameTag) ||
598 !SSL_SESSION_parse_string(&session, &ret->psk_identity,
599 kPSKIdentityTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700600 !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
David Benjamin8be79a32015-08-23 01:37:36 -0400601 kTicketLifetimeHintTag, 0) ||
602 !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
603 &ret->tlsext_ticklen, kTicketTag)) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400604 goto err;
605 }
Adam Langley95c29f32014-06-20 12:00:00 -0700606
David Benjamin8be79a32015-08-23 01:37:36 -0400607 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
608 CBS child, peer_sha256;
609 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
610 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
611 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
612 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400613 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400614 goto err;
615 }
616 memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
617 ret->peer_sha256_valid = 1;
618 } else {
619 ret->peer_sha256_valid = 0;
620 }
Adam Langley95c29f32014-06-20 12:00:00 -0700621
David Benjamin8be79a32015-08-23 01:37:36 -0400622 if (!SSL_SESSION_parse_bounded_octet_string(
623 &session, ret->original_handshake_hash,
624 &ret->original_handshake_hash_len,
625 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
626 !SSL_SESSION_parse_octet_string(
627 &session, &ret->tlsext_signed_cert_timestamp_list,
628 &ret->tlsext_signed_cert_timestamp_list_length,
629 kSignedCertTimestampListTag) ||
630 !SSL_SESSION_parse_octet_string(
631 &session, &ret->ocsp_response, &ret->ocsp_response_length,
632 kOCSPResponseTag)) {
633 goto err;
634 }
635
636 int extended_master_secret;
637 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
638 kExtendedMasterSecretTag,
639 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400640 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400641 goto err;
642 }
David Benjamin8be79a32015-08-23 01:37:36 -0400643 ret->extended_master_secret = !!extended_master_secret;
644
645 if (!SSL_SESSION_parse_u32(&session, &ret->key_exchange_info,
David Benjamin26416e92015-08-22 16:04:17 -0400646 kKeyExchangeInfoTag, 0)) {
647 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
648 goto err;
649 }
650
651 CBS cert_chain;
652 int has_cert_chain;
653 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
654 kCertChainTag)) {
655 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
656 goto err;
657 }
658 sk_X509_pop_free(ret->cert_chain, X509_free);
659 ret->cert_chain = NULL;
660 if (has_cert_chain) {
661 ret->cert_chain = sk_X509_new_null();
662 if (ret->cert_chain == NULL) {
663 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
664 goto err;
665 }
666 while (CBS_len(&cert_chain) > 0) {
667 X509 *x509 = parse_x509(&cert_chain);
668 if (x509 == NULL) {
669 goto err;
670 }
671 if (!sk_X509_push(ret->cert_chain, x509)) {
672 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
673 X509_free(x509);
674 goto err;
675 }
676 }
677 }
678
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400679 CBS age_add;
680 int age_add_present;
Steven Valdez5b986082016-09-01 12:29:49 -0400681 if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400682 kTicketAgeAddTag) ||
683 (age_add_present &&
684 !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
685 CBS_len(&age_add) != 0) {
686 goto err;
687 }
688 ret->ticket_age_add_valid = age_add_present;
689
David Benjamin26416e92015-08-22 16:04:17 -0400690 if (CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400691 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
692 goto err;
693 }
Adam Langley95c29f32014-06-20 12:00:00 -0700694
David Benjaminfd67aa82015-06-15 19:41:48 -0400695 return ret;
696
697err:
698 SSL_SESSION_free(ret);
699 return NULL;
700}
701
702SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) {
703 CBS cbs;
704 CBS_init(&cbs, in, in_len);
705 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
706 if (ret == NULL) {
707 return NULL;
708 }
709 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400710 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400711 SSL_SESSION_free(ret);
712 return NULL;
713 }
714 return ret;
715}
716
717SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
718 if (length < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400719 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminfd67aa82015-06-15 19:41:48 -0400720 return NULL;
721 }
722
723 CBS cbs;
724 CBS_init(&cbs, *pp, length);
725
726 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
727 if (ret == NULL) {
728 return NULL;
729 }
730
David Benjamin83fd6b62014-10-19 04:33:38 -0400731 if (a) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400732 SSL_SESSION_free(*a);
David Benjamin83fd6b62014-10-19 04:33:38 -0400733 *a = ret;
734 }
735 *pp = CBS_data(&cbs);
736 return ret;
David Benjamin83fd6b62014-10-19 04:33:38 -0400737}