blob: 709e15be62c85f18a962eba544675d445a331db2 [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 Benjamin83fd6b62014-10-19 04:33:38 -040083#include <limits.h>
David Benjamin89abaea2014-10-19 13:50:18 -040084#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070085
David Benjamin83fd6b62014-10-19 04:33:38 -040086#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -070087#include <openssl/err.h>
Brian Smith054e6822015-03-27 21:12:01 -100088#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -070089#include <openssl/x509.h>
90
David Benjamin2ee94aa2015-04-07 22:38:30 -040091#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070092
Adam Langley95c29f32014-06-20 12:00:00 -070093
David Benjamin83fd6b62014-10-19 04:33:38 -040094/* An SSL_SESSION is serialized as the following ASN.1 structure:
95 *
96 * SSLSession ::= SEQUENCE {
97 * version INTEGER (1), -- ignored
98 * sslVersion INTEGER, -- protocol version number
99 * cipher OCTET STRING, -- two bytes long
100 * sessionID OCTET STRING,
101 * masterKey OCTET STRING,
David Benjamin83fd6b62014-10-19 04:33:38 -0400102 * time [1] INTEGER OPTIONAL, -- seconds since UNIX epoch
103 * timeout [2] INTEGER OPTIONAL, -- in seconds
104 * peer [3] Certificate OPTIONAL,
105 * sessionIDContext [4] OCTET STRING OPTIONAL,
106 * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
107 * hostName [6] OCTET STRING OPTIONAL,
108 * -- from server_name extension
David Benjamin83fd6b62014-10-19 04:33:38 -0400109 * pskIdentity [8] OCTET STRING OPTIONAL,
110 * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
111 * ticket [10] OCTET STRING OPTIONAL, -- client-only
112 * peerSHA256 [13] OCTET STRING OPTIONAL,
113 * originalHandshakeHash [14] OCTET STRING OPTIONAL,
114 * signedCertTimestampList [15] OCTET STRING OPTIONAL,
115 * -- contents of SCT extension
116 * ocspResponse [16] OCTET STRING OPTIONAL,
117 * -- stapled OCSP response from the server
Adam Langley75712922014-10-10 16:23:43 -0700118 * extendedMasterSecret [17] BOOLEAN OPTIONAL,
David Benjamin83fd6b62014-10-19 04:33:38 -0400119 * }
120 *
David Benjamin688d8df2014-11-02 23:06:42 -0500121 * Note: historically this serialization has included other optional
122 * fields. Their presense is currently treated as a parse error:
123 *
124 * keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
125 * pskIdentityHint [7] OCTET STRING OPTIONAL,
126 * compressionMethod [11] OCTET STRING OPTIONAL,
127 * srpUsername [12] OCTET STRING OPTIONAL, */
David Benjamin83fd6b62014-10-19 04:33:38 -0400128
David Benjamin83fd6b62014-10-19 04:33:38 -0400129static const int kTimeTag =
130 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
131static const int kTimeoutTag =
132 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
133static const int kPeerTag =
134 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
David Benjamin3cac4502014-10-21 01:46:30 -0400135 static const int kSessionIDContextTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400136 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
137static const int kVerifyResultTag =
138 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
139static const int kHostNameTag =
140 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
David Benjamin83fd6b62014-10-19 04:33:38 -0400141static const int kPSKIdentityTag =
142 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
143static const int kTicketLifetimeHintTag =
144 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
145static const int kTicketTag =
146 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
147static const int kPeerSHA256Tag =
148 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
149static const int kOriginalHandshakeHashTag =
150 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
151static const int kSignedCertTimestampListTag =
152 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
153static const int kOCSPResponseTag =
154 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
Adam Langley75712922014-10-10 16:23:43 -0700155static const int kExtendedMasterSecretTag =
156 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
Adam Langley95c29f32014-06-20 12:00:00 -0700157
David Benjamin3cac4502014-10-21 01:46:30 -0400158static int SSL_SESSION_to_bytes_full(SSL_SESSION *in, uint8_t **out_data,
159 size_t *out_len, int for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400160 CBB cbb, session, child, child2;
Adam Langley95c29f32014-06-20 12:00:00 -0700161
David Benjaminf3a8b122014-12-25 23:11:49 -0500162 if (in == NULL || in->cipher == NULL) {
David Benjamin89abaea2014-10-19 13:50:18 -0400163 return 0;
164 }
Adam Langley95c29f32014-06-20 12:00:00 -0700165
David Benjamin89abaea2014-10-19 13:50:18 -0400166 if (!CBB_init(&cbb, 0)) {
167 return 0;
168 }
Adam Langley95c29f32014-06-20 12:00:00 -0700169
David Benjamin89abaea2014-10-19 13:50:18 -0400170 if (!CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
171 !CBB_add_asn1_uint64(&session, SSL_SESSION_ASN1_VERSION) ||
172 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
173 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminf3a8b122014-12-25 23:11:49 -0500174 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400175 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjamin3cac4502014-10-21 01:46:30 -0400176 /* The session ID is irrelevant for a session ticket. */
177 !CBB_add_bytes(&child, in->session_id,
178 for_ticket ? 0 : in->session_id_length) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400179 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
180 !CBB_add_bytes(&child, in->master_key, in->master_key_length)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100181 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400182 goto err;
183 }
Adam Langley95c29f32014-06-20 12:00:00 -0700184
David Benjamin89abaea2014-10-19 13:50:18 -0400185 if (in->time != 0) {
186 if (!CBB_add_asn1(&session, &child, kTimeTag) ||
187 !CBB_add_asn1_uint64(&child, in->time)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100188 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400189 goto err;
190 }
191 }
Adam Langley95c29f32014-06-20 12:00:00 -0700192
David Benjamin89abaea2014-10-19 13:50:18 -0400193 if (in->timeout != 0) {
194 if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
195 !CBB_add_asn1_uint64(&child, in->timeout)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100196 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400197 goto err;
198 }
199 }
Adam Langley95c29f32014-06-20 12:00:00 -0700200
David Benjamin89abaea2014-10-19 13:50:18 -0400201 /* The peer certificate is only serialized if the SHA-256 isn't
202 * serialized instead. */
203 if (in->peer && !in->peer_sha256_valid) {
204 uint8_t *buf;
205 int len = i2d_X509(in->peer, NULL);
206 if (len < 0) {
207 goto err;
208 }
209 if (!CBB_add_asn1(&session, &child, kPeerTag) ||
210 !CBB_add_space(&child, &buf, len)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100211 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400212 goto err;
213 }
214 if (buf != NULL && i2d_X509(in->peer, &buf) < 0) {
215 goto err;
216 }
217 }
Adam Langley95c29f32014-06-20 12:00:00 -0700218
David Benjamin89abaea2014-10-19 13:50:18 -0400219 /* Although it is OPTIONAL and usually empty, OpenSSL has
220 * historically always encoded the sid_ctx. */
221 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
222 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
223 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100224 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400225 goto err;
226 }
Adam Langley95c29f32014-06-20 12:00:00 -0700227
David Benjamin89abaea2014-10-19 13:50:18 -0400228 if (in->verify_result != X509_V_OK) {
229 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
230 !CBB_add_asn1_uint64(&child, in->verify_result)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100231 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400232 goto err;
233 }
234 }
Adam Langley95c29f32014-06-20 12:00:00 -0700235
David Benjamin89abaea2014-10-19 13:50:18 -0400236 if (in->tlsext_hostname) {
237 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
238 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
239 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
240 strlen(in->tlsext_hostname))) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100241 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400242 goto err;
243 }
244 }
Adam Langley95c29f32014-06-20 12:00:00 -0700245
David Benjamin89abaea2014-10-19 13:50:18 -0400246 if (in->psk_identity) {
247 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
248 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
249 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
250 strlen(in->psk_identity))) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100251 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400252 goto err;
253 }
254 }
Adam Langley95c29f32014-06-20 12:00:00 -0700255
David Benjamin89abaea2014-10-19 13:50:18 -0400256 if (in->tlsext_tick_lifetime_hint > 0) {
257 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
258 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100259 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400260 goto err;
261 }
262 }
Adam Langley95c29f32014-06-20 12:00:00 -0700263
David Benjamin89abaea2014-10-19 13:50:18 -0400264 if (in->tlsext_tick) {
265 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
266 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
267 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100268 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400269 goto err;
270 }
271 }
Adam Langley75872532014-06-20 12:00:00 -0700272
David Benjamin89abaea2014-10-19 13:50:18 -0400273 if (in->peer_sha256_valid) {
274 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
275 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
276 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100277 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400278 goto err;
279 }
280 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700281
David Benjamin89abaea2014-10-19 13:50:18 -0400282 if (in->original_handshake_hash_len > 0) {
283 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
284 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
285 !CBB_add_bytes(&child2, in->original_handshake_hash,
286 in->original_handshake_hash_len)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100287 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400288 goto err;
289 }
290 }
Adam Langley95c29f32014-06-20 12:00:00 -0700291
David Benjamin89abaea2014-10-19 13:50:18 -0400292 if (in->tlsext_signed_cert_timestamp_list_length > 0) {
293 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
294 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
295 !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
296 in->tlsext_signed_cert_timestamp_list_length)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100297 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400298 goto err;
299 }
300 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200301
David Benjamin89abaea2014-10-19 13:50:18 -0400302 if (in->ocsp_response_length > 0) {
303 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
304 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
305 !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100306 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400307 goto err;
308 }
309 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400310
Adam Langley75712922014-10-10 16:23:43 -0700311 if (in->extended_master_secret) {
312 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
313 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
314 !CBB_add_u8(&child2, 0xff)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100315 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
Adam Langley75712922014-10-10 16:23:43 -0700316 goto err;
317 }
318 }
319
David Benjamin3cac4502014-10-21 01:46:30 -0400320 if (!CBB_finish(&cbb, out_data, out_len)) {
HÃ¥vard Mollandab2479a2015-03-20 13:15:39 +0100321 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_to_bytes_full, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400322 goto err;
323 }
David Benjamin3cac4502014-10-21 01:46:30 -0400324 return 1;
325
326 err:
327 CBB_cleanup(&cbb);
328 return 0;
329}
330
331int SSL_SESSION_to_bytes(SSL_SESSION *in, uint8_t **out_data, size_t *out_len) {
332 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
333}
334
335int SSL_SESSION_to_bytes_for_ticket(SSL_SESSION *in, uint8_t **out_data,
336 size_t *out_len) {
337 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
338}
339
340int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
341 uint8_t *out;
342 size_t len;
343
344 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
345 return -1;
346 }
Adam Langley95c29f32014-06-20 12:00:00 -0700347
David Benjamin89abaea2014-10-19 13:50:18 -0400348 if (len > INT_MAX) {
349 OPENSSL_free(out);
350 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_OVERFLOW);
351 return -1;
352 }
Adam Langley95c29f32014-06-20 12:00:00 -0700353
David Benjamin89abaea2014-10-19 13:50:18 -0400354 if (pp) {
355 memcpy(*pp, out, len);
356 *pp += len;
357 }
358 OPENSSL_free(out);
Adam Langley95c29f32014-06-20 12:00:00 -0700359
David Benjamin89abaea2014-10-19 13:50:18 -0400360 return len;
David Benjamin89abaea2014-10-19 13:50:18 -0400361}
Adam Langley95c29f32014-06-20 12:00:00 -0700362
David Benjaminfd67aa82015-06-15 19:41:48 -0400363/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400364 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
365 * entry, if |*out| is not NULL, it frees the existing contents. If
366 * the element was not found, it sets |*out| to NULL. It returns one
367 * on success, whether or not the element was found, and zero on
368 * decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400369static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400370 CBS value;
371 int present;
372 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400373 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse_string, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400374 return 0;
375 }
376 if (present) {
377 if (CBS_contains_zero_byte(&value)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400378 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse_string,
379 SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400380 return 0;
381 }
382 if (!CBS_strdup(&value, out)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400383 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse_string, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400384 return 0;
385 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400386 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400387 OPENSSL_free(*out);
388 *out = NULL;
389 }
390 return 1;
391}
Adam Langley95c29f32014-06-20 12:00:00 -0700392
David Benjaminfd67aa82015-06-15 19:41:48 -0400393/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400394 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
395 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
396 * contents. On entry, if the element was not found, it sets
397 * |*out_ptr| to NULL. It returns one on success, whether or not the
398 * element was found, and zero on decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400399static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin83fd6b62014-10-19 04:33:38 -0400400 size_t *out_len, unsigned tag) {
401 CBS value;
402 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400403 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse_octet_string,
404 SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400405 return 0;
406 }
407 if (!CBS_stow(&value, out_ptr, out_len)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400408 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse_octet_string,
409 ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400410 return 0;
411 }
412 return 1;
413}
Adam Langley95c29f32014-06-20 12:00:00 -0700414
David Benjaminfd67aa82015-06-15 19:41:48 -0400415static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) {
416 SSL_SESSION *ret = NULL;
417 CBS session, cipher, session_id, master_key;
David Benjamin7001a7f2014-10-26 00:03:08 -0400418 CBS peer, sid_ctx, peer_sha256, original_handshake_hash;
419 int has_peer, has_peer_sha256, extended_master_secret;
David Benjamin83fd6b62014-10-19 04:33:38 -0400420 uint64_t version, ssl_version;
421 uint64_t session_time, timeout, verify_result, ticket_lifetime_hint;
Adam Langley95c29f32014-06-20 12:00:00 -0700422
David Benjaminfd67aa82015-06-15 19:41:48 -0400423 ret = SSL_SESSION_new();
424 if (ret == NULL) {
425 goto err;
David Benjamin83fd6b62014-10-19 04:33:38 -0400426 }
Adam Langley95c29f32014-06-20 12:00:00 -0700427
David Benjaminfd67aa82015-06-15 19:41:48 -0400428 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400429 !CBS_get_asn1_uint64(&session, &version) ||
430 !CBS_get_asn1_uint64(&session, &ssl_version) ||
431 !CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
432 !CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
433 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400434 !CBS_get_optional_asn1_uint64(&session, &session_time, kTimeTag,
435 time(NULL)) ||
436 !CBS_get_optional_asn1_uint64(&session, &timeout, kTimeoutTag, 3) ||
437 !CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
438 !CBS_get_optional_asn1_octet_string(&session, &sid_ctx, NULL,
439 kSessionIDContextTag) ||
440 !CBS_get_optional_asn1_uint64(&session, &verify_result, kVerifyResultTag,
441 X509_V_OK)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400442 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400443 goto err;
444 }
David Benjaminfd67aa82015-06-15 19:41:48 -0400445 if (!SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
446 kHostNameTag) ||
447 !SSL_SESSION_parse_string(&session, &ret->psk_identity,
448 kPSKIdentityTag)) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400449 goto err;
450 }
451 if (!CBS_get_optional_asn1_uint64(&session, &ticket_lifetime_hint,
452 kTicketLifetimeHintTag, 0)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400453 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400454 goto err;
455 }
David Benjaminfd67aa82015-06-15 19:41:48 -0400456 if (!SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
David Benjamin83fd6b62014-10-19 04:33:38 -0400457 &ret->tlsext_ticklen, kTicketTag)) {
458 goto err;
459 }
460 if (!CBS_get_optional_asn1_octet_string(&session, &peer_sha256,
461 &has_peer_sha256, kPeerSHA256Tag) ||
462 !CBS_get_optional_asn1_octet_string(&session, &original_handshake_hash,
463 NULL, kOriginalHandshakeHashTag)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400464 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400465 goto err;
466 }
David Benjaminfd67aa82015-06-15 19:41:48 -0400467 if (!SSL_SESSION_parse_octet_string(
David Benjamin83fd6b62014-10-19 04:33:38 -0400468 &session, &ret->tlsext_signed_cert_timestamp_list,
469 &ret->tlsext_signed_cert_timestamp_list_length,
470 kSignedCertTimestampListTag) ||
David Benjaminfd67aa82015-06-15 19:41:48 -0400471 !SSL_SESSION_parse_octet_string(
David Benjamin83fd6b62014-10-19 04:33:38 -0400472 &session, &ret->ocsp_response, &ret->ocsp_response_length,
473 kOCSPResponseTag)) {
474 goto err;
475 }
Adam Langley75712922014-10-10 16:23:43 -0700476 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
477 kExtendedMasterSecretTag,
David Benjaminf297e022015-05-28 19:55:29 -0400478 0 /* default to false */) ||
479 CBS_len(&session) != 0) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400480 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
Adam Langley75712922014-10-10 16:23:43 -0700481 goto err;
482 }
483 ret->extended_master_secret = extended_master_secret;
Adam Langley95c29f32014-06-20 12:00:00 -0700484
David Benjamin338e0672015-05-28 20:00:08 -0400485 if (version != SSL_SESSION_ASN1_VERSION) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400486 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
David Benjamin338e0672015-05-28 20:00:08 -0400487 goto err;
488 }
Adam Langley95c29f32014-06-20 12:00:00 -0700489
David Benjamin83fd6b62014-10-19 04:33:38 -0400490 /* Only support SSLv3/TLS and DTLS. */
491 if ((ssl_version >> 8) != SSL3_VERSION_MAJOR &&
492 (ssl_version >> 8) != (DTLS1_VERSION >> 8)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400493 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_UNKNOWN_SSL_VERSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400494 goto err;
495 }
496 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700497
David Benjaminf3a8b122014-12-25 23:11:49 -0500498 uint16_t cipher_value;
499 if (!CBS_get_u16(&cipher, &cipher_value) || CBS_len(&cipher) != 0) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400500 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_CIPHER_CODE_WRONG_LENGTH);
David Benjamin83fd6b62014-10-19 04:33:38 -0400501 goto err;
502 }
David Benjamina1c90a52015-05-30 17:03:14 -0400503 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400504 if (ret->cipher == NULL) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400505 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin83fd6b62014-10-19 04:33:38 -0400506 goto err;
507 }
Adam Langley95c29f32014-06-20 12:00:00 -0700508
David Benjamin83fd6b62014-10-19 04:33:38 -0400509 if (CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400510 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400511 goto err;
512 }
513 memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
514 ret->session_id_length = CBS_len(&session_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700515
David Benjamin83fd6b62014-10-19 04:33:38 -0400516 if (CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400517 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400518 goto err;
519 }
520 memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
521 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700522
David Benjamin83fd6b62014-10-19 04:33:38 -0400523 if (session_time > LONG_MAX ||
524 timeout > LONG_MAX) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400525 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400526 goto err;
527 }
528 ret->time = session_time;
529 ret->timeout = timeout;
Adam Langley95c29f32014-06-20 12:00:00 -0700530
David Benjamin2755a3e2015-04-22 16:17:58 -0400531 X509_free(ret->peer);
532 ret->peer = NULL;
David Benjamin83fd6b62014-10-19 04:33:38 -0400533 if (has_peer) {
534 const uint8_t *ptr;
535 ptr = CBS_data(&peer);
536 ret->peer = d2i_X509(NULL, &ptr, CBS_len(&peer));
537 if (ret->peer == NULL) {
538 goto err;
539 }
540 if (ptr != CBS_data(&peer) + CBS_len(&peer)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400541 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400542 goto err;
543 }
544 }
Adam Langley95c29f32014-06-20 12:00:00 -0700545
David Benjamin83fd6b62014-10-19 04:33:38 -0400546 if (CBS_len(&sid_ctx) > sizeof(ret->sid_ctx)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400547 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400548 goto err;
549 }
550 memcpy(ret->sid_ctx, CBS_data(&sid_ctx), CBS_len(&sid_ctx));
551 ret->sid_ctx_length = CBS_len(&sid_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700552
David Benjamin83fd6b62014-10-19 04:33:38 -0400553 if (verify_result > LONG_MAX ||
554 ticket_lifetime_hint > 0xffffffff) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400555 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400556 goto err;
557 }
558 ret->verify_result = verify_result;
559 ret->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
Adam Langley95c29f32014-06-20 12:00:00 -0700560
David Benjamin83fd6b62014-10-19 04:33:38 -0400561 if (has_peer_sha256) {
562 if (CBS_len(&peer_sha256) != sizeof(ret->peer_sha256)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400563 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400564 goto err;
565 }
566 memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
567 ret->peer_sha256_valid = 1;
568 } else {
569 ret->peer_sha256_valid = 0;
570 }
Adam Langley95c29f32014-06-20 12:00:00 -0700571
David Benjamin83fd6b62014-10-19 04:33:38 -0400572 if (CBS_len(&original_handshake_hash) >
573 sizeof(ret->original_handshake_hash)) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400574 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_parse, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400575 goto err;
576 }
577 memcpy(ret->original_handshake_hash, CBS_data(&original_handshake_hash),
578 CBS_len(&original_handshake_hash));
579 ret->original_handshake_hash_len = CBS_len(&original_handshake_hash);
Adam Langley95c29f32014-06-20 12:00:00 -0700580
David Benjaminfd67aa82015-06-15 19:41:48 -0400581 return ret;
582
583err:
584 SSL_SESSION_free(ret);
585 return NULL;
586}
587
588SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) {
589 CBS cbs;
590 CBS_init(&cbs, in, in_len);
591 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
592 if (ret == NULL) {
593 return NULL;
594 }
595 if (CBS_len(&cbs) != 0) {
596 OPENSSL_PUT_ERROR(SSL, SSL_SESSION_from_bytes, SSL_R_INVALID_SSL_SESSION);
597 SSL_SESSION_free(ret);
598 return NULL;
599 }
600 return ret;
601}
602
603SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
604 if (length < 0) {
605 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, ERR_R_INTERNAL_ERROR);
606 return NULL;
607 }
608
609 CBS cbs;
610 CBS_init(&cbs, *pp, length);
611
612 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
613 if (ret == NULL) {
614 return NULL;
615 }
616
David Benjamin83fd6b62014-10-19 04:33:38 -0400617 if (a) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400618 SSL_SESSION_free(*a);
David Benjamin83fd6b62014-10-19 04:33:38 -0400619 *a = ret;
620 }
621 *pp = CBS_data(&cbs);
622 return ret;
David Benjamin83fd6b62014-10-19 04:33:38 -0400623}