blob: 2fd7f129f7aae598a59dead9fbefe14b3640959b [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,
David Benjamin26416e92015-08-22 16:04:17 -0400117 * -- stapled OCSP response from the server
Adam Langley75712922014-10-10 16:23:43 -0700118 * extendedMasterSecret [17] BOOLEAN OPTIONAL,
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200119 * keyExchangeInfo [18] INTEGER OPTIONAL,
David Benjamin26416e92015-08-22 16:04:17 -0400120 * certChain [19] SEQUENCE OF Certificate OPTIONAL,
David Benjamin83fd6b62014-10-19 04:33:38 -0400121 * }
122 *
David Benjamin688d8df2014-11-02 23:06:42 -0500123 * Note: historically this serialization has included other optional
124 * fields. Their presense is currently treated as a parse error:
125 *
126 * keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
127 * pskIdentityHint [7] OCTET STRING OPTIONAL,
128 * compressionMethod [11] OCTET STRING OPTIONAL,
129 * srpUsername [12] OCTET STRING OPTIONAL, */
David Benjamin83fd6b62014-10-19 04:33:38 -0400130
David Benjamin83fd6b62014-10-19 04:33:38 -0400131static const int kTimeTag =
132 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
133static const int kTimeoutTag =
134 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
135static const int kPeerTag =
136 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
David Benjamin3cac4502014-10-21 01:46:30 -0400137 static const int kSessionIDContextTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400138 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
139static const int kVerifyResultTag =
140 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
141static const int kHostNameTag =
142 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
David Benjamin83fd6b62014-10-19 04:33:38 -0400143static const int kPSKIdentityTag =
144 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
145static const int kTicketLifetimeHintTag =
146 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
147static const int kTicketTag =
148 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
149static const int kPeerSHA256Tag =
150 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
151static const int kOriginalHandshakeHashTag =
152 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
153static const int kSignedCertTimestampListTag =
154 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
155static const int kOCSPResponseTag =
156 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
Adam Langley75712922014-10-10 16:23:43 -0700157static const int kExtendedMasterSecretTag =
158 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200159static const int kKeyExchangeInfoTag =
160 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
David Benjamin26416e92015-08-22 16:04:17 -0400161static const int kCertChainTag =
162 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
163
164static int add_X509(CBB *cbb, X509 *x509) {
165 int len = i2d_X509(x509, NULL);
166 if (len < 0) {
167 return 0;
168 }
169 uint8_t *buf;
170 if (!CBB_add_space(cbb, &buf, len)) {
171 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
172 return 0;
173 }
174 if (buf != NULL && i2d_X509(x509, &buf) < 0) {
175 return 0;
176 }
177 return 1;
178}
Adam Langley95c29f32014-06-20 12:00:00 -0700179
David Benjamin3cac4502014-10-21 01:46:30 -0400180static int SSL_SESSION_to_bytes_full(SSL_SESSION *in, uint8_t **out_data,
181 size_t *out_len, int for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400182 CBB cbb, session, child, child2;
Adam Langley95c29f32014-06-20 12:00:00 -0700183
David Benjaminf3a8b122014-12-25 23:11:49 -0500184 if (in == NULL || in->cipher == NULL) {
David Benjamin89abaea2014-10-19 13:50:18 -0400185 return 0;
186 }
Adam Langley95c29f32014-06-20 12:00:00 -0700187
David Benjamina8653202015-06-28 01:26:10 -0400188 CBB_zero(&cbb);
189 if (!CBB_init(&cbb, 0) ||
190 !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400191 !CBB_add_asn1_uint64(&session, SSL_SESSION_ASN1_VERSION) ||
192 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
193 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminf3a8b122014-12-25 23:11:49 -0500194 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400195 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjamin3cac4502014-10-21 01:46:30 -0400196 /* The session ID is irrelevant for a session ticket. */
197 !CBB_add_bytes(&child, in->session_id,
198 for_ticket ? 0 : in->session_id_length) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400199 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
200 !CBB_add_bytes(&child, in->master_key, in->master_key_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400201 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400202 goto err;
203 }
Adam Langley95c29f32014-06-20 12:00:00 -0700204
David Benjamin89abaea2014-10-19 13:50:18 -0400205 if (in->time != 0) {
206 if (!CBB_add_asn1(&session, &child, kTimeTag) ||
207 !CBB_add_asn1_uint64(&child, in->time)) {
David Benjamin3570d732015-06-29 00:28:17 -0400208 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400209 goto err;
210 }
211 }
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjamin89abaea2014-10-19 13:50:18 -0400213 if (in->timeout != 0) {
214 if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
215 !CBB_add_asn1_uint64(&child, in->timeout)) {
David Benjamin3570d732015-06-29 00:28:17 -0400216 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400217 goto err;
218 }
219 }
Adam Langley95c29f32014-06-20 12:00:00 -0700220
David Benjamin89abaea2014-10-19 13:50:18 -0400221 /* The peer certificate is only serialized if the SHA-256 isn't
222 * serialized instead. */
223 if (in->peer && !in->peer_sha256_valid) {
David Benjamin26416e92015-08-22 16:04:17 -0400224 if (!CBB_add_asn1(&session, &child, kPeerTag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400225 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400226 goto err;
227 }
David Benjamin26416e92015-08-22 16:04:17 -0400228 if (!add_X509(&child, in->peer)) {
David Benjamin89abaea2014-10-19 13:50:18 -0400229 goto err;
230 }
231 }
Adam Langley95c29f32014-06-20 12:00:00 -0700232
David Benjamin89abaea2014-10-19 13:50:18 -0400233 /* Although it is OPTIONAL and usually empty, OpenSSL has
234 * historically always encoded the sid_ctx. */
235 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
236 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
237 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400238 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400239 goto err;
240 }
Adam Langley95c29f32014-06-20 12:00:00 -0700241
David Benjamin89abaea2014-10-19 13:50:18 -0400242 if (in->verify_result != X509_V_OK) {
243 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
244 !CBB_add_asn1_uint64(&child, in->verify_result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400245 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400246 goto err;
247 }
248 }
Adam Langley95c29f32014-06-20 12:00:00 -0700249
David Benjamin89abaea2014-10-19 13:50:18 -0400250 if (in->tlsext_hostname) {
251 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
252 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
253 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
254 strlen(in->tlsext_hostname))) {
David Benjamin3570d732015-06-29 00:28:17 -0400255 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400256 goto err;
257 }
258 }
Adam Langley95c29f32014-06-20 12:00:00 -0700259
David Benjamin89abaea2014-10-19 13:50:18 -0400260 if (in->psk_identity) {
261 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
262 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
263 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
264 strlen(in->psk_identity))) {
David Benjamin3570d732015-06-29 00:28:17 -0400265 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400266 goto err;
267 }
268 }
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjamin89abaea2014-10-19 13:50:18 -0400270 if (in->tlsext_tick_lifetime_hint > 0) {
271 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
272 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400273 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400274 goto err;
275 }
276 }
Adam Langley95c29f32014-06-20 12:00:00 -0700277
David Benjamin95d31822015-06-15 19:53:32 -0400278 if (in->tlsext_tick && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400279 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
280 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
281 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -0400282 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400283 goto err;
284 }
285 }
Adam Langley75872532014-06-20 12:00:00 -0700286
David Benjamin89abaea2014-10-19 13:50:18 -0400287 if (in->peer_sha256_valid) {
288 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
289 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
290 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
David Benjamin3570d732015-06-29 00:28:17 -0400291 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400292 goto err;
293 }
294 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700295
David Benjamin89abaea2014-10-19 13:50:18 -0400296 if (in->original_handshake_hash_len > 0) {
297 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
298 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
299 !CBB_add_bytes(&child2, in->original_handshake_hash,
300 in->original_handshake_hash_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400301 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400302 goto err;
303 }
304 }
Adam Langley95c29f32014-06-20 12:00:00 -0700305
David Benjamin89abaea2014-10-19 13:50:18 -0400306 if (in->tlsext_signed_cert_timestamp_list_length > 0) {
307 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
308 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
309 !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
310 in->tlsext_signed_cert_timestamp_list_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400311 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400312 goto err;
313 }
314 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200315
David Benjamin89abaea2014-10-19 13:50:18 -0400316 if (in->ocsp_response_length > 0) {
317 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
318 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
319 !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400320 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400321 goto err;
322 }
323 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400324
Adam Langley75712922014-10-10 16:23:43 -0700325 if (in->extended_master_secret) {
326 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
327 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
328 !CBB_add_u8(&child2, 0xff)) {
David Benjamin3570d732015-06-29 00:28:17 -0400329 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley75712922014-10-10 16:23:43 -0700330 goto err;
331 }
332 }
333
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200334 if (in->key_exchange_info > 0 &&
335 (!CBB_add_asn1(&session, &child, kKeyExchangeInfoTag) ||
336 !CBB_add_asn1_uint64(&child, in->key_exchange_info))) {
337 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
338 goto err;
339 }
340
David Benjamin26416e92015-08-22 16:04:17 -0400341 /* The certificate chain is only serialized if the leaf's SHA-256 isn't
342 * serialized instead. */
343 if (in->cert_chain != NULL && !in->peer_sha256_valid) {
344 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
345 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
346 goto err;
347 }
348 size_t i;
349 for (i = 0; i < sk_X509_num(in->cert_chain); i++) {
350 if (!add_X509(&child, sk_X509_value(in->cert_chain, i))) {
351 goto err;
352 }
353 }
354 }
355
David Benjamin3cac4502014-10-21 01:46:30 -0400356 if (!CBB_finish(&cbb, out_data, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400357 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400358 goto err;
359 }
David Benjamin3cac4502014-10-21 01:46:30 -0400360 return 1;
361
362 err:
363 CBB_cleanup(&cbb);
364 return 0;
365}
366
367int SSL_SESSION_to_bytes(SSL_SESSION *in, uint8_t **out_data, size_t *out_len) {
368 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
369}
370
371int SSL_SESSION_to_bytes_for_ticket(SSL_SESSION *in, uint8_t **out_data,
372 size_t *out_len) {
373 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
374}
375
376int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
377 uint8_t *out;
378 size_t len;
379
380 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
381 return -1;
382 }
Adam Langley95c29f32014-06-20 12:00:00 -0700383
David Benjamin89abaea2014-10-19 13:50:18 -0400384 if (len > INT_MAX) {
385 OPENSSL_free(out);
David Benjamin3570d732015-06-29 00:28:17 -0400386 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin89abaea2014-10-19 13:50:18 -0400387 return -1;
388 }
Adam Langley95c29f32014-06-20 12:00:00 -0700389
David Benjamin89abaea2014-10-19 13:50:18 -0400390 if (pp) {
391 memcpy(*pp, out, len);
392 *pp += len;
393 }
394 OPENSSL_free(out);
Adam Langley95c29f32014-06-20 12:00:00 -0700395
David Benjamin89abaea2014-10-19 13:50:18 -0400396 return len;
David Benjamin89abaea2014-10-19 13:50:18 -0400397}
Adam Langley95c29f32014-06-20 12:00:00 -0700398
David Benjaminfd67aa82015-06-15 19:41:48 -0400399/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400400 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
401 * entry, if |*out| is not NULL, it frees the existing contents. If
402 * the element was not found, it sets |*out| to NULL. It returns one
403 * on success, whether or not the element was found, and zero on
404 * decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400405static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400406 CBS value;
407 int present;
408 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400409 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400410 return 0;
411 }
412 if (present) {
413 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400414 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400415 return 0;
416 }
417 if (!CBS_strdup(&value, out)) {
David Benjamin3570d732015-06-29 00:28:17 -0400418 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400419 return 0;
420 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400421 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400422 OPENSSL_free(*out);
423 *out = NULL;
424 }
425 return 1;
426}
Adam Langley95c29f32014-06-20 12:00:00 -0700427
David Benjaminfd67aa82015-06-15 19:41:48 -0400428/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400429 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
430 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
431 * contents. On entry, if the element was not found, it sets
432 * |*out_ptr| to NULL. It returns one on success, whether or not the
433 * element was found, and zero on decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400434static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin8be79a32015-08-23 01:37:36 -0400435 size_t *out_len, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400436 CBS value;
437 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400438 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400439 return 0;
440 }
441 if (!CBS_stow(&value, out_ptr, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400442 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400443 return 0;
444 }
445 return 1;
446}
Adam Langley95c29f32014-06-20 12:00:00 -0700447
David Benjamin8be79a32015-08-23 01:37:36 -0400448/* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
449 * explicitly tagged with |tag| of size at most |max_out|. */
450static int SSL_SESSION_parse_bounded_octet_string(
451 CBS *cbs, uint8_t *out, unsigned *out_len, unsigned max_out, unsigned tag) {
452 CBS value;
453 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
454 CBS_len(&value) > max_out) {
455 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
456 return 0;
457 }
458 memcpy(out, CBS_data(&value), CBS_len(&value));
459 *out_len = (unsigned)CBS_len(&value);
460 return 1;
461}
Adam Langley95c29f32014-06-20 12:00:00 -0700462
David Benjamin8be79a32015-08-23 01:37:36 -0400463static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
464 long default_value) {
465 uint64_t value;
466 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
467 (uint64_t)default_value) ||
468 value > LONG_MAX) {
469 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
470 return 0;
471 }
472 *out = (long)value;
473 return 1;
474}
475
476static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
477 uint32_t default_value) {
478 uint64_t value;
479 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
480 (uint64_t)default_value) ||
481 value > 0xffffffff) {
482 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
483 return 0;
484 }
485 *out = (uint32_t)value;
486 return 1;
487}
488
489static X509 *parse_x509(CBS *cbs) {
490 const uint8_t *ptr = CBS_data(cbs);
491 X509 *ret = d2i_X509(NULL, &ptr, CBS_len(cbs));
492 if (ret == NULL) {
493 return NULL;
494 }
495 CBS_skip(cbs, ptr - CBS_data(cbs));
496 return ret;
497}
498
499static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) {
500 SSL_SESSION *ret = SSL_SESSION_new();
David Benjaminfd67aa82015-06-15 19:41:48 -0400501 if (ret == NULL) {
502 goto err;
David Benjamin83fd6b62014-10-19 04:33:38 -0400503 }
Adam Langley95c29f32014-06-20 12:00:00 -0700504
David Benjamin8be79a32015-08-23 01:37:36 -0400505 CBS session;
506 uint64_t version, ssl_version;
David Benjaminfd67aa82015-06-15 19:41:48 -0400507 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400508 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin8be79a32015-08-23 01:37:36 -0400509 version != SSL_SESSION_ASN1_VERSION ||
510 !CBS_get_asn1_uint64(&session, &ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400511 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400512 goto err;
513 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400514 /* Only support SSLv3/TLS and DTLS. */
515 if ((ssl_version >> 8) != SSL3_VERSION_MAJOR &&
516 (ssl_version >> 8) != (DTLS1_VERSION >> 8)) {
David Benjamin3570d732015-06-29 00:28:17 -0400517 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400518 goto err;
519 }
520 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700521
David Benjamin8be79a32015-08-23 01:37:36 -0400522 CBS cipher;
David Benjaminf3a8b122014-12-25 23:11:49 -0500523 uint16_t cipher_value;
David Benjamin8be79a32015-08-23 01:37:36 -0400524 if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
525 !CBS_get_u16(&cipher, &cipher_value) ||
526 CBS_len(&cipher) != 0) {
527 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400528 goto err;
529 }
David Benjamina1c90a52015-05-30 17:03:14 -0400530 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400531 if (ret->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400532 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin83fd6b62014-10-19 04:33:38 -0400533 goto err;
534 }
Adam Langley95c29f32014-06-20 12:00:00 -0700535
David Benjamin8be79a32015-08-23 01:37:36 -0400536 CBS session_id, master_key;
537 if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
538 CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
539 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
540 CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400541 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400542 goto err;
543 }
544 memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
545 ret->session_id_length = CBS_len(&session_id);
David Benjamin83fd6b62014-10-19 04:33:38 -0400546 memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
547 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700548
David Benjamin8be79a32015-08-23 01:37:36 -0400549 if (!SSL_SESSION_parse_long(&session, &ret->time, kTimeTag, time(NULL)) ||
550 !SSL_SESSION_parse_long(&session, &ret->timeout, kTimeoutTag, 3)) {
David Benjamin3570d732015-06-29 00:28:17 -0400551 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400552 goto err;
553 }
Adam Langley95c29f32014-06-20 12:00:00 -0700554
David Benjamin8be79a32015-08-23 01:37:36 -0400555 CBS peer;
556 int has_peer;
557 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag)) {
558 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
559 goto err;
560 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400561 X509_free(ret->peer);
562 ret->peer = NULL;
David Benjamin83fd6b62014-10-19 04:33:38 -0400563 if (has_peer) {
David Benjamin8be79a32015-08-23 01:37:36 -0400564 ret->peer = parse_x509(&peer);
David Benjamin83fd6b62014-10-19 04:33:38 -0400565 if (ret->peer == NULL) {
566 goto err;
567 }
David Benjamin8be79a32015-08-23 01:37:36 -0400568 if (CBS_len(&peer) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400569 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400570 goto err;
571 }
572 }
Adam Langley95c29f32014-06-20 12:00:00 -0700573
David Benjamin8be79a32015-08-23 01:37:36 -0400574 if (!SSL_SESSION_parse_bounded_octet_string(
575 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
576 kSessionIDContextTag) ||
577 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
578 X509_V_OK) ||
579 !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
580 kHostNameTag) ||
581 !SSL_SESSION_parse_string(&session, &ret->psk_identity,
582 kPSKIdentityTag) ||
583 !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
584 kTicketLifetimeHintTag, 0) ||
585 !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
586 &ret->tlsext_ticklen, kTicketTag)) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400587 goto err;
588 }
Adam Langley95c29f32014-06-20 12:00:00 -0700589
David Benjamin8be79a32015-08-23 01:37:36 -0400590 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
591 CBS child, peer_sha256;
592 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
593 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
594 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
595 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400596 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400597 goto err;
598 }
599 memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
600 ret->peer_sha256_valid = 1;
601 } else {
602 ret->peer_sha256_valid = 0;
603 }
Adam Langley95c29f32014-06-20 12:00:00 -0700604
David Benjamin8be79a32015-08-23 01:37:36 -0400605 if (!SSL_SESSION_parse_bounded_octet_string(
606 &session, ret->original_handshake_hash,
607 &ret->original_handshake_hash_len,
608 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
609 !SSL_SESSION_parse_octet_string(
610 &session, &ret->tlsext_signed_cert_timestamp_list,
611 &ret->tlsext_signed_cert_timestamp_list_length,
612 kSignedCertTimestampListTag) ||
613 !SSL_SESSION_parse_octet_string(
614 &session, &ret->ocsp_response, &ret->ocsp_response_length,
615 kOCSPResponseTag)) {
616 goto err;
617 }
618
619 int extended_master_secret;
620 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
621 kExtendedMasterSecretTag,
622 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400623 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400624 goto err;
625 }
David Benjamin8be79a32015-08-23 01:37:36 -0400626 ret->extended_master_secret = !!extended_master_secret;
627
628 if (!SSL_SESSION_parse_u32(&session, &ret->key_exchange_info,
David Benjamin26416e92015-08-22 16:04:17 -0400629 kKeyExchangeInfoTag, 0)) {
630 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
631 goto err;
632 }
633
634 CBS cert_chain;
635 int has_cert_chain;
636 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
637 kCertChainTag)) {
638 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
639 goto err;
640 }
641 sk_X509_pop_free(ret->cert_chain, X509_free);
642 ret->cert_chain = NULL;
643 if (has_cert_chain) {
644 ret->cert_chain = sk_X509_new_null();
645 if (ret->cert_chain == NULL) {
646 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
647 goto err;
648 }
649 while (CBS_len(&cert_chain) > 0) {
650 X509 *x509 = parse_x509(&cert_chain);
651 if (x509 == NULL) {
652 goto err;
653 }
654 if (!sk_X509_push(ret->cert_chain, x509)) {
655 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
656 X509_free(x509);
657 goto err;
658 }
659 }
660 }
661
662 if (CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400663 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
664 goto err;
665 }
Adam Langley95c29f32014-06-20 12:00:00 -0700666
David Benjaminfd67aa82015-06-15 19:41:48 -0400667 return ret;
668
669err:
670 SSL_SESSION_free(ret);
671 return NULL;
672}
673
674SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) {
675 CBS cbs;
676 CBS_init(&cbs, in, in_len);
677 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
678 if (ret == NULL) {
679 return NULL;
680 }
681 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400682 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400683 SSL_SESSION_free(ret);
684 return NULL;
685 }
686 return ret;
687}
688
689SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
690 if (length < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400691 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminfd67aa82015-06-15 19:41:48 -0400692 return NULL;
693 }
694
695 CBS cbs;
696 CBS_init(&cbs, *pp, length);
697
698 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
699 if (ret == NULL) {
700 return NULL;
701 }
702
David Benjamin83fd6b62014-10-19 04:33:38 -0400703 if (a) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400704 SSL_SESSION_free(*a);
David Benjamin83fd6b62014-10-19 04:33:38 -0400705 *a = ret;
706 }
707 *pp = CBS_data(&cbs);
708 return ret;
David Benjamin83fd6b62014-10-19 04:33:38 -0400709}