blob: bfa064f8d995cddc31bd13d0eb9c7e63043df755 [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>
Adam Langley95c29f32014-06-20 12:00:00 -070088#include <openssl/x509.h>
89
90#include "ssl_locl.h"
91
Adam Langley95c29f32014-06-20 12:00:00 -070092
David Benjamin83fd6b62014-10-19 04:33:38 -040093/* An SSL_SESSION is serialized as the following ASN.1 structure:
94 *
95 * SSLSession ::= SEQUENCE {
96 * version INTEGER (1), -- ignored
97 * sslVersion INTEGER, -- protocol version number
98 * cipher OCTET STRING, -- two bytes long
99 * sessionID OCTET STRING,
100 * masterKey OCTET STRING,
David Benjamin83fd6b62014-10-19 04:33:38 -0400101 * time [1] INTEGER OPTIONAL, -- seconds since UNIX epoch
102 * timeout [2] INTEGER OPTIONAL, -- in seconds
103 * peer [3] Certificate OPTIONAL,
104 * sessionIDContext [4] OCTET STRING OPTIONAL,
105 * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
106 * hostName [6] OCTET STRING OPTIONAL,
107 * -- from server_name extension
108 * pskIdentityHint [7] OCTET STRING OPTIONAL,
109 * 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 *
121 * Note: When the relevant features were #ifdef'd out, support for
122 * parsing compressionMethod [11] and srpUsername [12] was lost. */
123
David Benjamin83fd6b62014-10-19 04:33:38 -0400124static const int kTimeTag =
125 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
126static const int kTimeoutTag =
127 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
128static const int kPeerTag =
129 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
130static const int kSessionIDContextTag =
131 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
132static const int kVerifyResultTag =
133 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
134static const int kHostNameTag =
135 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
136static const int kPSKIdentityHintTag =
137 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 7;
138static const int kPSKIdentityTag =
139 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
140static const int kTicketLifetimeHintTag =
141 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
142static const int kTicketTag =
143 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
144static const int kPeerSHA256Tag =
145 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
146static const int kOriginalHandshakeHashTag =
147 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
148static const int kSignedCertTimestampListTag =
149 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
150static const int kOCSPResponseTag =
151 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
Adam Langley75712922014-10-10 16:23:43 -0700152static const int kExtendedMasterSecretTag =
153 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamin89abaea2014-10-19 13:50:18 -0400155int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
156 CBB cbb, session, child, child2;
157 uint16_t cipher_id;
158 uint8_t *out;
159 size_t len;
Adam Langley95c29f32014-06-20 12:00:00 -0700160
David Benjamin89abaea2014-10-19 13:50:18 -0400161 if (in == NULL || (in->cipher == NULL && in->cipher_id == 0)) {
162 return 0;
163 }
Adam Langley95c29f32014-06-20 12:00:00 -0700164
David Benjamin89abaea2014-10-19 13:50:18 -0400165 if (!CBB_init(&cbb, 0)) {
166 return 0;
167 }
Adam Langley95c29f32014-06-20 12:00:00 -0700168
David Benjamin89abaea2014-10-19 13:50:18 -0400169 if (in->cipher == NULL) {
170 cipher_id = in->cipher_id & 0xffff;
171 } else {
172 cipher_id = in->cipher->id & 0xffff;
173 }
Adam Langley95c29f32014-06-20 12:00:00 -0700174
David Benjamin89abaea2014-10-19 13:50:18 -0400175 if (!CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
176 !CBB_add_asn1_uint64(&session, SSL_SESSION_ASN1_VERSION) ||
177 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
178 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
179 !CBB_add_u16(&child, cipher_id) ||
180 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
181 !CBB_add_bytes(&child, in->session_id, in->session_id_length) ||
182 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
183 !CBB_add_bytes(&child, in->master_key, in->master_key_length)) {
184 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
185 goto err;
186 }
Adam Langley95c29f32014-06-20 12:00:00 -0700187
David Benjamin89abaea2014-10-19 13:50:18 -0400188 if (in->time != 0) {
189 if (!CBB_add_asn1(&session, &child, kTimeTag) ||
190 !CBB_add_asn1_uint64(&child, in->time)) {
191 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
192 goto err;
193 }
194 }
Adam Langley95c29f32014-06-20 12:00:00 -0700195
David Benjamin89abaea2014-10-19 13:50:18 -0400196 if (in->timeout != 0) {
197 if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
198 !CBB_add_asn1_uint64(&child, in->timeout)) {
199 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
200 goto err;
201 }
202 }
Adam Langley95c29f32014-06-20 12:00:00 -0700203
David Benjamin89abaea2014-10-19 13:50:18 -0400204 /* The peer certificate is only serialized if the SHA-256 isn't
205 * serialized instead. */
206 if (in->peer && !in->peer_sha256_valid) {
207 uint8_t *buf;
208 int len = i2d_X509(in->peer, NULL);
209 if (len < 0) {
210 goto err;
211 }
212 if (!CBB_add_asn1(&session, &child, kPeerTag) ||
213 !CBB_add_space(&child, &buf, len)) {
214 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
215 goto err;
216 }
217 if (buf != NULL && i2d_X509(in->peer, &buf) < 0) {
218 goto err;
219 }
220 }
Adam Langley95c29f32014-06-20 12:00:00 -0700221
David Benjamin89abaea2014-10-19 13:50:18 -0400222 /* Although it is OPTIONAL and usually empty, OpenSSL has
223 * historically always encoded the sid_ctx. */
224 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
225 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
226 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
227 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
228 goto err;
229 }
Adam Langley95c29f32014-06-20 12:00:00 -0700230
David Benjamin89abaea2014-10-19 13:50:18 -0400231 if (in->verify_result != X509_V_OK) {
232 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
233 !CBB_add_asn1_uint64(&child, in->verify_result)) {
234 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
235 goto err;
236 }
237 }
Adam Langley95c29f32014-06-20 12:00:00 -0700238
David Benjamin89abaea2014-10-19 13:50:18 -0400239 if (in->tlsext_hostname) {
240 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
241 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
242 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
243 strlen(in->tlsext_hostname))) {
244 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
245 goto err;
246 }
247 }
Adam Langley95c29f32014-06-20 12:00:00 -0700248
David Benjamin89abaea2014-10-19 13:50:18 -0400249 if (in->psk_identity_hint) {
250 if (!CBB_add_asn1(&session, &child, kPSKIdentityHintTag) ||
251 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
252 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity_hint,
253 strlen(in->psk_identity_hint))) {
254 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
255 goto err;
256 }
257 }
Adam Langley95c29f32014-06-20 12:00:00 -0700258
David Benjamin89abaea2014-10-19 13:50:18 -0400259 if (in->psk_identity) {
260 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
261 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
262 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
263 strlen(in->psk_identity))) {
264 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
265 goto err;
266 }
267 }
Adam Langley95c29f32014-06-20 12:00:00 -0700268
David Benjamin89abaea2014-10-19 13:50:18 -0400269 if (in->tlsext_tick_lifetime_hint > 0) {
270 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
271 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
272 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
273 goto err;
274 }
275 }
Adam Langley95c29f32014-06-20 12:00:00 -0700276
David Benjamin89abaea2014-10-19 13:50:18 -0400277 if (in->tlsext_tick) {
278 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
279 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
280 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
281 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
282 goto err;
283 }
284 }
Adam Langley75872532014-06-20 12:00:00 -0700285
David Benjamin89abaea2014-10-19 13:50:18 -0400286 if (in->peer_sha256_valid) {
287 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
288 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
289 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
290 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
291 goto err;
292 }
293 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700294
David Benjamin89abaea2014-10-19 13:50:18 -0400295 if (in->original_handshake_hash_len > 0) {
296 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
297 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
298 !CBB_add_bytes(&child2, in->original_handshake_hash,
299 in->original_handshake_hash_len)) {
300 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
301 goto err;
302 }
303 }
Adam Langley95c29f32014-06-20 12:00:00 -0700304
David Benjamin89abaea2014-10-19 13:50:18 -0400305 if (in->tlsext_signed_cert_timestamp_list_length > 0) {
306 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
307 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
308 !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
309 in->tlsext_signed_cert_timestamp_list_length)) {
310 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
311 goto err;
312 }
313 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200314
David Benjamin89abaea2014-10-19 13:50:18 -0400315 if (in->ocsp_response_length > 0) {
316 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
317 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
318 !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
319 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
320 goto err;
321 }
322 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400323
Adam Langley75712922014-10-10 16:23:43 -0700324 if (in->extended_master_secret) {
325 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
326 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
327 !CBB_add_u8(&child2, 0xff)) {
328 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
329 goto err;
330 }
331 }
332
David Benjamin89abaea2014-10-19 13:50:18 -0400333 if (!CBB_finish(&cbb, &out, &len)) {
334 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_MALLOC_FAILURE);
335 goto err;
336 }
Adam Langley95c29f32014-06-20 12:00:00 -0700337
David Benjamin89abaea2014-10-19 13:50:18 -0400338 if (len > INT_MAX) {
339 OPENSSL_free(out);
340 OPENSSL_PUT_ERROR(SSL, i2d_SSL_SESSION, ERR_R_OVERFLOW);
341 return -1;
342 }
Adam Langley95c29f32014-06-20 12:00:00 -0700343
David Benjamin89abaea2014-10-19 13:50:18 -0400344 /* TODO(davidben): Provide a safer API and deprecate this one. */
345 if (pp) {
346 memcpy(*pp, out, len);
347 *pp += len;
348 }
349 OPENSSL_free(out);
Adam Langley95c29f32014-06-20 12:00:00 -0700350
David Benjamin89abaea2014-10-19 13:50:18 -0400351 return len;
Adam Langley1258b6a2014-06-20 12:00:00 -0700352
David Benjamin89abaea2014-10-19 13:50:18 -0400353err:
354 CBB_cleanup(&cbb);
355 return -1;
356}
Adam Langley95c29f32014-06-20 12:00:00 -0700357
David Benjamin83fd6b62014-10-19 04:33:38 -0400358/* d2i_SSL_SESSION_get_string gets an optional ASN.1 OCTET STRING
359 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
360 * entry, if |*out| is not NULL, it frees the existing contents. If
361 * the element was not found, it sets |*out| to NULL. It returns one
362 * on success, whether or not the element was found, and zero on
363 * decode error. */
364static int d2i_SSL_SESSION_get_string(CBS *cbs, char **out, unsigned tag) {
365 CBS value;
366 int present;
367 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
368 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
369 return 0;
370 }
371 if (present) {
372 if (CBS_contains_zero_byte(&value)) {
373 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
374 return 0;
375 }
376 if (!CBS_strdup(&value, out)) {
377 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, ERR_R_MALLOC_FAILURE);
378 return 0;
379 }
380 } else if (*out) {
381 OPENSSL_free(*out);
382 *out = NULL;
383 }
384 return 1;
385}
Adam Langley95c29f32014-06-20 12:00:00 -0700386
David Benjamin83fd6b62014-10-19 04:33:38 -0400387/* d2i_SSL_SESSION_get_string gets an optional ASN.1 OCTET STRING
388 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
389 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
390 * contents. On entry, if the element was not found, it sets
391 * |*out_ptr| to NULL. It returns one on success, whether or not the
392 * element was found, and zero on decode error. */
393static int d2i_SSL_SESSION_get_octet_string(CBS *cbs, uint8_t **out_ptr,
394 size_t *out_len, unsigned tag) {
395 CBS value;
396 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
397 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
398 return 0;
399 }
400 if (!CBS_stow(&value, out_ptr, out_len)) {
401 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, ERR_R_MALLOC_FAILURE);
402 return 0;
403 }
404 return 1;
405}
Adam Langley95c29f32014-06-20 12:00:00 -0700406
David Benjamin83fd6b62014-10-19 04:33:38 -0400407SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
408 SSL_SESSION *ret = NULL;
409 CBS cbs, session, cipher, session_id, master_key;
David Benjamin7001a7f2014-10-26 00:03:08 -0400410 CBS peer, sid_ctx, peer_sha256, original_handshake_hash;
411 int has_peer, has_peer_sha256, extended_master_secret;
David Benjamin83fd6b62014-10-19 04:33:38 -0400412 uint64_t version, ssl_version;
413 uint64_t session_time, timeout, verify_result, ticket_lifetime_hint;
Adam Langley95c29f32014-06-20 12:00:00 -0700414
David Benjamin83fd6b62014-10-19 04:33:38 -0400415 if (a && *a) {
416 ret = *a;
417 } else {
418 ret = SSL_SESSION_new();
419 if (ret == NULL) {
420 goto err;
421 }
422 }
Adam Langley95c29f32014-06-20 12:00:00 -0700423
David Benjamin83fd6b62014-10-19 04:33:38 -0400424 CBS_init(&cbs, *pp, length);
425 if (!CBS_get_asn1(&cbs, &session, CBS_ASN1_SEQUENCE) ||
426 !CBS_get_asn1_uint64(&session, &version) ||
427 !CBS_get_asn1_uint64(&session, &ssl_version) ||
428 !CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
429 !CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
430 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400431 !CBS_get_optional_asn1_uint64(&session, &session_time, kTimeTag,
432 time(NULL)) ||
433 !CBS_get_optional_asn1_uint64(&session, &timeout, kTimeoutTag, 3) ||
434 !CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
435 !CBS_get_optional_asn1_octet_string(&session, &sid_ctx, NULL,
436 kSessionIDContextTag) ||
437 !CBS_get_optional_asn1_uint64(&session, &verify_result, kVerifyResultTag,
438 X509_V_OK)) {
439 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
440 goto err;
441 }
442 if (!d2i_SSL_SESSION_get_string(&session, &ret->tlsext_hostname,
443 kHostNameTag) ||
444 !d2i_SSL_SESSION_get_string(&session, &ret->psk_identity_hint,
445 kPSKIdentityHintTag) ||
446 !d2i_SSL_SESSION_get_string(&session, &ret->psk_identity,
447 kPSKIdentityTag)) {
448 goto err;
449 }
450 if (!CBS_get_optional_asn1_uint64(&session, &ticket_lifetime_hint,
451 kTicketLifetimeHintTag, 0)) {
452 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
453 goto err;
454 }
455 if (!d2i_SSL_SESSION_get_octet_string(&session, &ret->tlsext_tick,
456 &ret->tlsext_ticklen, kTicketTag)) {
457 goto err;
458 }
459 if (!CBS_get_optional_asn1_octet_string(&session, &peer_sha256,
460 &has_peer_sha256, kPeerSHA256Tag) ||
461 !CBS_get_optional_asn1_octet_string(&session, &original_handshake_hash,
462 NULL, kOriginalHandshakeHashTag)) {
463 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
464 goto err;
465 }
466 if (!d2i_SSL_SESSION_get_octet_string(
467 &session, &ret->tlsext_signed_cert_timestamp_list,
468 &ret->tlsext_signed_cert_timestamp_list_length,
469 kSignedCertTimestampListTag) ||
470 !d2i_SSL_SESSION_get_octet_string(
471 &session, &ret->ocsp_response, &ret->ocsp_response_length,
472 kOCSPResponseTag)) {
473 goto err;
474 }
Adam Langley75712922014-10-10 16:23:43 -0700475 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
476 kExtendedMasterSecretTag,
477 0 /* default to false */)) {
478 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
479 goto err;
480 }
481 ret->extended_master_secret = extended_master_secret;
Adam Langley95c29f32014-06-20 12:00:00 -0700482
David Benjamin83fd6b62014-10-19 04:33:38 -0400483 /* Ignore |version|. The structure version number is ignored. */
Adam Langley95c29f32014-06-20 12:00:00 -0700484
David Benjamin83fd6b62014-10-19 04:33:38 -0400485 /* Only support SSLv3/TLS and DTLS. */
486 if ((ssl_version >> 8) != SSL3_VERSION_MAJOR &&
487 (ssl_version >> 8) != (DTLS1_VERSION >> 8)) {
488 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_UNKNOWN_SSL_VERSION);
489 goto err;
490 }
491 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700492
David Benjamin83fd6b62014-10-19 04:33:38 -0400493 if (CBS_len(&cipher) != 2) {
494 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_CIPHER_CODE_WRONG_LENGTH);
495 goto err;
496 }
497 ret->cipher_id =
498 0x03000000L | (CBS_data(&cipher)[0] << 8L) | CBS_data(&cipher)[1];
499 ret->cipher = ssl3_get_cipher_by_value(ret->cipher_id & 0xffff);
500 if (ret->cipher == NULL) {
501 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_UNSUPPORTED_CIPHER);
502 goto err;
503 }
Adam Langley95c29f32014-06-20 12:00:00 -0700504
David Benjamin83fd6b62014-10-19 04:33:38 -0400505 if (CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH) {
506 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
507 goto err;
508 }
509 memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
510 ret->session_id_length = CBS_len(&session_id);
Adam Langley95c29f32014-06-20 12:00:00 -0700511
David Benjamin83fd6b62014-10-19 04:33:38 -0400512 if (CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
513 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
514 goto err;
515 }
516 memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
517 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700518
David Benjamin83fd6b62014-10-19 04:33:38 -0400519 if (session_time > LONG_MAX ||
520 timeout > LONG_MAX) {
521 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
522 goto err;
523 }
524 ret->time = session_time;
525 ret->timeout = timeout;
Adam Langley95c29f32014-06-20 12:00:00 -0700526
David Benjamin83fd6b62014-10-19 04:33:38 -0400527 if (ret->peer != NULL) {
528 X509_free(ret->peer);
529 ret->peer = NULL;
530 }
531 if (has_peer) {
532 const uint8_t *ptr;
533 ptr = CBS_data(&peer);
534 ret->peer = d2i_X509(NULL, &ptr, CBS_len(&peer));
535 if (ret->peer == NULL) {
536 goto err;
537 }
538 if (ptr != CBS_data(&peer) + CBS_len(&peer)) {
539 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
540 goto err;
541 }
542 }
Adam Langley95c29f32014-06-20 12:00:00 -0700543
David Benjamin83fd6b62014-10-19 04:33:38 -0400544 if (CBS_len(&sid_ctx) > sizeof(ret->sid_ctx)) {
545 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
546 goto err;
547 }
548 memcpy(ret->sid_ctx, CBS_data(&sid_ctx), CBS_len(&sid_ctx));
549 ret->sid_ctx_length = CBS_len(&sid_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700550
David Benjamin83fd6b62014-10-19 04:33:38 -0400551 if (verify_result > LONG_MAX ||
552 ticket_lifetime_hint > 0xffffffff) {
553 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
554 goto err;
555 }
556 ret->verify_result = verify_result;
557 ret->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
Adam Langley95c29f32014-06-20 12:00:00 -0700558
David Benjamin83fd6b62014-10-19 04:33:38 -0400559 if (has_peer_sha256) {
560 if (CBS_len(&peer_sha256) != sizeof(ret->peer_sha256)) {
561 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
562 goto err;
563 }
564 memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
565 ret->peer_sha256_valid = 1;
566 } else {
567 ret->peer_sha256_valid = 0;
568 }
Adam Langley95c29f32014-06-20 12:00:00 -0700569
David Benjamin83fd6b62014-10-19 04:33:38 -0400570 if (CBS_len(&original_handshake_hash) >
571 sizeof(ret->original_handshake_hash)) {
572 OPENSSL_PUT_ERROR(SSL, d2i_SSL_SESSION, SSL_R_INVALID_SSL_SESSION);
573 goto err;
574 }
575 memcpy(ret->original_handshake_hash, CBS_data(&original_handshake_hash),
576 CBS_len(&original_handshake_hash));
577 ret->original_handshake_hash_len = CBS_len(&original_handshake_hash);
Adam Langley95c29f32014-06-20 12:00:00 -0700578
David Benjamin83fd6b62014-10-19 04:33:38 -0400579 if (a) {
580 *a = ret;
581 }
582 *pp = CBS_data(&cbs);
583 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700584
David Benjamin83fd6b62014-10-19 04:33:38 -0400585err:
586 if (a && *a != ret) {
587 SSL_SESSION_free(ret);
588 }
589 return NULL;
590}