blob: 0ad4a11bdc43aad0faa3f0be50b4c83cf226a96e [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright 2005 Nokia. All rights reserved.
59 *
60 * The portions of the attached software ("Contribution") is developed by
61 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
62 * license.
63 *
64 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
65 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
66 * support (see RFC 4279) to OpenSSL.
67 *
68 * No patent licenses or other rights except those expressly stated in
69 * the OpenSSL open source license shall be deemed granted or received
70 * expressly, by implication, estoppel, or otherwise.
71 *
72 * No assurances are provided by Nokia that the Contribution does not
73 * infringe the patent or other intellectual property rights of any third
74 * party or that the license provides you with all the necessary rights
75 * to make use of the Contribution.
76 *
77 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
78 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
79 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
80 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
81 * OTHERWISE. */
82
David Benjamin9e4e01e2015-09-15 01:48:04 -040083#include <openssl/ssl.h>
84
David Benjamin83fd6b62014-10-19 04:33:38 -040085#include <limits.h>
David Benjamin89abaea2014-10-19 13:50:18 -040086#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070087
David Benjamin83fd6b62014-10-19 04:33:38 -040088#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -070089#include <openssl/err.h>
Brian Smith054e6822015-03-27 21:12:01 -100090#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -070091#include <openssl/x509.h>
92
David Benjamin2ee94aa2015-04-07 22:38:30 -040093#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070094
Adam Langley95c29f32014-06-20 12:00:00 -070095
David Benjamin83fd6b62014-10-19 04:33:38 -040096/* An SSL_SESSION is serialized as the following ASN.1 structure:
97 *
98 * SSLSession ::= SEQUENCE {
99 * version INTEGER (1), -- ignored
100 * sslVersion INTEGER, -- protocol version number
101 * cipher OCTET STRING, -- two bytes long
102 * sessionID OCTET STRING,
103 * masterKey OCTET STRING,
David Benjamin83fd6b62014-10-19 04:33:38 -0400104 * time [1] INTEGER OPTIONAL, -- seconds since UNIX epoch
105 * timeout [2] INTEGER OPTIONAL, -- in seconds
106 * peer [3] Certificate OPTIONAL,
107 * sessionIDContext [4] OCTET STRING OPTIONAL,
108 * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
109 * hostName [6] OCTET STRING OPTIONAL,
110 * -- from server_name extension
David Benjamin83fd6b62014-10-19 04:33:38 -0400111 * pskIdentity [8] OCTET STRING OPTIONAL,
112 * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
113 * ticket [10] OCTET STRING OPTIONAL, -- client-only
114 * peerSHA256 [13] OCTET STRING OPTIONAL,
115 * originalHandshakeHash [14] OCTET STRING OPTIONAL,
116 * signedCertTimestampList [15] OCTET STRING OPTIONAL,
117 * -- contents of SCT extension
118 * ocspResponse [16] OCTET STRING OPTIONAL,
David Benjamin26416e92015-08-22 16:04:17 -0400119 * -- stapled OCSP response from the server
Adam Langley75712922014-10-10 16:23:43 -0700120 * extendedMasterSecret [17] BOOLEAN OPTIONAL,
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200121 * keyExchangeInfo [18] INTEGER OPTIONAL,
David Benjamin26416e92015-08-22 16:04:17 -0400122 * certChain [19] SEQUENCE OF Certificate OPTIONAL,
David Benjamin83fd6b62014-10-19 04:33:38 -0400123 * }
124 *
David Benjamin688d8df2014-11-02 23:06:42 -0500125 * Note: historically this serialization has included other optional
126 * fields. Their presense is currently treated as a parse error:
127 *
128 * keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
129 * pskIdentityHint [7] OCTET STRING OPTIONAL,
130 * compressionMethod [11] OCTET STRING OPTIONAL,
131 * srpUsername [12] OCTET STRING OPTIONAL, */
David Benjamin83fd6b62014-10-19 04:33:38 -0400132
Adam Langley96c2a282015-06-02 14:16:44 -0700133static const unsigned kVersion = 1;
David Benjamin7f393f72015-10-17 12:36:12 -0400134
David Benjamin83fd6b62014-10-19 04:33:38 -0400135static const int kTimeTag =
136 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
137static const int kTimeoutTag =
138 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
139static const int kPeerTag =
140 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
David Benjamin7f393f72015-10-17 12:36:12 -0400141static const int kSessionIDContextTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400142 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
143static const int kVerifyResultTag =
144 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
145static const int kHostNameTag =
146 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
David Benjamin83fd6b62014-10-19 04:33:38 -0400147static const int kPSKIdentityTag =
148 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
149static const int kTicketLifetimeHintTag =
150 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
151static const int kTicketTag =
152 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
153static const int kPeerSHA256Tag =
154 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
155static const int kOriginalHandshakeHashTag =
156 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
157static const int kSignedCertTimestampListTag =
158 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
159static const int kOCSPResponseTag =
160 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
Adam Langley75712922014-10-10 16:23:43 -0700161static const int kExtendedMasterSecretTag =
162 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200163static const int kKeyExchangeInfoTag =
164 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
David Benjamin26416e92015-08-22 16:04:17 -0400165static const int kCertChainTag =
166 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
167
168static int add_X509(CBB *cbb, X509 *x509) {
169 int len = i2d_X509(x509, NULL);
170 if (len < 0) {
171 return 0;
172 }
173 uint8_t *buf;
174 if (!CBB_add_space(cbb, &buf, len)) {
175 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
176 return 0;
177 }
178 if (buf != NULL && i2d_X509(x509, &buf) < 0) {
179 return 0;
180 }
181 return 1;
182}
Adam Langley95c29f32014-06-20 12:00:00 -0700183
David Benjamin14e2b502015-09-13 14:48:12 -0400184static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400185 size_t *out_len, int for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400186 CBB cbb, session, child, child2;
Adam Langley95c29f32014-06-20 12:00:00 -0700187
David Benjaminf3a8b122014-12-25 23:11:49 -0500188 if (in == NULL || in->cipher == NULL) {
David Benjamin89abaea2014-10-19 13:50:18 -0400189 return 0;
190 }
Adam Langley95c29f32014-06-20 12:00:00 -0700191
David Benjamina8653202015-06-28 01:26:10 -0400192 CBB_zero(&cbb);
193 if (!CBB_init(&cbb, 0) ||
194 !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400195 !CBB_add_asn1_uint64(&session, kVersion) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400196 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
197 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminf3a8b122014-12-25 23:11:49 -0500198 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400199 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjamin3cac4502014-10-21 01:46:30 -0400200 /* The session ID is irrelevant for a session ticket. */
201 !CBB_add_bytes(&child, in->session_id,
202 for_ticket ? 0 : in->session_id_length) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400203 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
204 !CBB_add_bytes(&child, in->master_key, in->master_key_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400205 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400206 goto err;
207 }
Adam Langley95c29f32014-06-20 12:00:00 -0700208
David Benjamin89abaea2014-10-19 13:50:18 -0400209 if (in->time != 0) {
210 if (!CBB_add_asn1(&session, &child, kTimeTag) ||
211 !CBB_add_asn1_uint64(&child, in->time)) {
David Benjamin3570d732015-06-29 00:28:17 -0400212 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400213 goto err;
214 }
215 }
Adam Langley95c29f32014-06-20 12:00:00 -0700216
David Benjamin89abaea2014-10-19 13:50:18 -0400217 if (in->timeout != 0) {
218 if (!CBB_add_asn1(&session, &child, kTimeoutTag) ||
219 !CBB_add_asn1_uint64(&child, in->timeout)) {
David Benjamin3570d732015-06-29 00:28:17 -0400220 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400221 goto err;
222 }
223 }
Adam Langley95c29f32014-06-20 12:00:00 -0700224
David Benjamin89abaea2014-10-19 13:50:18 -0400225 /* The peer certificate is only serialized if the SHA-256 isn't
226 * serialized instead. */
227 if (in->peer && !in->peer_sha256_valid) {
David Benjamin26416e92015-08-22 16:04:17 -0400228 if (!CBB_add_asn1(&session, &child, kPeerTag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400229 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400230 goto err;
231 }
David Benjamin26416e92015-08-22 16:04:17 -0400232 if (!add_X509(&child, in->peer)) {
David Benjamin89abaea2014-10-19 13:50:18 -0400233 goto err;
234 }
235 }
Adam Langley95c29f32014-06-20 12:00:00 -0700236
David Benjamin89abaea2014-10-19 13:50:18 -0400237 /* Although it is OPTIONAL and usually empty, OpenSSL has
238 * historically always encoded the sid_ctx. */
239 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
240 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
241 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400242 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400243 goto err;
244 }
Adam Langley95c29f32014-06-20 12:00:00 -0700245
David Benjamin89abaea2014-10-19 13:50:18 -0400246 if (in->verify_result != X509_V_OK) {
247 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
248 !CBB_add_asn1_uint64(&child, in->verify_result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400249 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400250 goto err;
251 }
252 }
Adam Langley95c29f32014-06-20 12:00:00 -0700253
David Benjamin89abaea2014-10-19 13:50:18 -0400254 if (in->tlsext_hostname) {
255 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
256 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
257 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
258 strlen(in->tlsext_hostname))) {
David Benjamin3570d732015-06-29 00:28:17 -0400259 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400260 goto err;
261 }
262 }
Adam Langley95c29f32014-06-20 12:00:00 -0700263
David Benjamin89abaea2014-10-19 13:50:18 -0400264 if (in->psk_identity) {
265 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
266 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
267 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
268 strlen(in->psk_identity))) {
David Benjamin3570d732015-06-29 00:28:17 -0400269 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400270 goto err;
271 }
272 }
Adam Langley95c29f32014-06-20 12:00:00 -0700273
David Benjamin89abaea2014-10-19 13:50:18 -0400274 if (in->tlsext_tick_lifetime_hint > 0) {
275 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
276 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400277 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400278 goto err;
279 }
280 }
Adam Langley95c29f32014-06-20 12:00:00 -0700281
David Benjamin95d31822015-06-15 19:53:32 -0400282 if (in->tlsext_tick && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400283 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
284 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
285 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -0400286 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400287 goto err;
288 }
289 }
Adam Langley75872532014-06-20 12:00:00 -0700290
David Benjamin89abaea2014-10-19 13:50:18 -0400291 if (in->peer_sha256_valid) {
292 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
293 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
294 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
David Benjamin3570d732015-06-29 00:28:17 -0400295 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400296 goto err;
297 }
298 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700299
David Benjamin89abaea2014-10-19 13:50:18 -0400300 if (in->original_handshake_hash_len > 0) {
301 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
302 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
303 !CBB_add_bytes(&child2, in->original_handshake_hash,
304 in->original_handshake_hash_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400305 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400306 goto err;
307 }
308 }
Adam Langley95c29f32014-06-20 12:00:00 -0700309
David Benjamin89abaea2014-10-19 13:50:18 -0400310 if (in->tlsext_signed_cert_timestamp_list_length > 0) {
311 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
312 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
313 !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
314 in->tlsext_signed_cert_timestamp_list_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400315 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400316 goto err;
317 }
318 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200319
David Benjamin89abaea2014-10-19 13:50:18 -0400320 if (in->ocsp_response_length > 0) {
321 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
322 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
323 !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400324 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400325 goto err;
326 }
327 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400328
Adam Langley75712922014-10-10 16:23:43 -0700329 if (in->extended_master_secret) {
330 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
331 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
332 !CBB_add_u8(&child2, 0xff)) {
David Benjamin3570d732015-06-29 00:28:17 -0400333 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley75712922014-10-10 16:23:43 -0700334 goto err;
335 }
336 }
337
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200338 if (in->key_exchange_info > 0 &&
339 (!CBB_add_asn1(&session, &child, kKeyExchangeInfoTag) ||
340 !CBB_add_asn1_uint64(&child, in->key_exchange_info))) {
341 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
342 goto err;
343 }
344
David Benjamin26416e92015-08-22 16:04:17 -0400345 /* The certificate chain is only serialized if the leaf's SHA-256 isn't
346 * serialized instead. */
347 if (in->cert_chain != NULL && !in->peer_sha256_valid) {
348 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
349 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
350 goto err;
351 }
352 size_t i;
353 for (i = 0; i < sk_X509_num(in->cert_chain); i++) {
354 if (!add_X509(&child, sk_X509_value(in->cert_chain, i))) {
355 goto err;
356 }
357 }
358 }
359
David Benjamin3cac4502014-10-21 01:46:30 -0400360 if (!CBB_finish(&cbb, out_data, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400361 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400362 goto err;
363 }
David Benjamin3cac4502014-10-21 01:46:30 -0400364 return 1;
365
366 err:
367 CBB_cleanup(&cbb);
368 return 0;
369}
370
David Benjamin14e2b502015-09-13 14:48:12 -0400371int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
372 size_t *out_len) {
David Benjamin3cac4502014-10-21 01:46:30 -0400373 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
374}
375
David Benjamin14e2b502015-09-13 14:48:12 -0400376int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400377 size_t *out_len) {
378 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
379}
380
381int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
382 uint8_t *out;
383 size_t len;
384
385 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
386 return -1;
387 }
Adam Langley95c29f32014-06-20 12:00:00 -0700388
David Benjamin89abaea2014-10-19 13:50:18 -0400389 if (len > INT_MAX) {
390 OPENSSL_free(out);
David Benjamin3570d732015-06-29 00:28:17 -0400391 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin89abaea2014-10-19 13:50:18 -0400392 return -1;
393 }
Adam Langley95c29f32014-06-20 12:00:00 -0700394
David Benjamin89abaea2014-10-19 13:50:18 -0400395 if (pp) {
396 memcpy(*pp, out, len);
397 *pp += len;
398 }
399 OPENSSL_free(out);
Adam Langley95c29f32014-06-20 12:00:00 -0700400
David Benjamin89abaea2014-10-19 13:50:18 -0400401 return len;
David Benjamin89abaea2014-10-19 13:50:18 -0400402}
Adam Langley95c29f32014-06-20 12:00:00 -0700403
David Benjaminfd67aa82015-06-15 19:41:48 -0400404/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400405 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
406 * entry, if |*out| is not NULL, it frees the existing contents. If
407 * the element was not found, it sets |*out| to NULL. It returns one
408 * on success, whether or not the element was found, and zero on
409 * decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400410static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400411 CBS value;
412 int present;
413 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
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 (present) {
418 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400419 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400420 return 0;
421 }
422 if (!CBS_strdup(&value, out)) {
David Benjamin3570d732015-06-29 00:28:17 -0400423 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400424 return 0;
425 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400426 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400427 OPENSSL_free(*out);
428 *out = NULL;
429 }
430 return 1;
431}
Adam Langley95c29f32014-06-20 12:00:00 -0700432
David Benjaminfd67aa82015-06-15 19:41:48 -0400433/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400434 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
435 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
436 * contents. On entry, if the element was not found, it sets
437 * |*out_ptr| to NULL. It returns one on success, whether or not the
438 * element was found, and zero on decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400439static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin8be79a32015-08-23 01:37:36 -0400440 size_t *out_len, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400441 CBS value;
442 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400443 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400444 return 0;
445 }
446 if (!CBS_stow(&value, out_ptr, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400447 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400448 return 0;
449 }
450 return 1;
451}
Adam Langley95c29f32014-06-20 12:00:00 -0700452
David Benjamin8be79a32015-08-23 01:37:36 -0400453/* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
454 * explicitly tagged with |tag| of size at most |max_out|. */
455static int SSL_SESSION_parse_bounded_octet_string(
456 CBS *cbs, uint8_t *out, unsigned *out_len, unsigned max_out, unsigned tag) {
457 CBS value;
458 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
459 CBS_len(&value) > max_out) {
460 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
461 return 0;
462 }
463 memcpy(out, CBS_data(&value), CBS_len(&value));
464 *out_len = (unsigned)CBS_len(&value);
465 return 1;
466}
Adam Langley95c29f32014-06-20 12:00:00 -0700467
David Benjamin8be79a32015-08-23 01:37:36 -0400468static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
469 long default_value) {
470 uint64_t value;
471 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
472 (uint64_t)default_value) ||
473 value > LONG_MAX) {
474 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
475 return 0;
476 }
477 *out = (long)value;
478 return 1;
479}
480
481static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
482 uint32_t default_value) {
483 uint64_t value;
484 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
485 (uint64_t)default_value) ||
486 value > 0xffffffff) {
487 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
488 return 0;
489 }
490 *out = (uint32_t)value;
491 return 1;
492}
493
494static X509 *parse_x509(CBS *cbs) {
495 const uint8_t *ptr = CBS_data(cbs);
496 X509 *ret = d2i_X509(NULL, &ptr, CBS_len(cbs));
497 if (ret == NULL) {
498 return NULL;
499 }
500 CBS_skip(cbs, ptr - CBS_data(cbs));
501 return ret;
502}
503
504static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) {
505 SSL_SESSION *ret = SSL_SESSION_new();
David Benjaminfd67aa82015-06-15 19:41:48 -0400506 if (ret == NULL) {
507 goto err;
David Benjamin83fd6b62014-10-19 04:33:38 -0400508 }
Adam Langley95c29f32014-06-20 12:00:00 -0700509
David Benjamin8be79a32015-08-23 01:37:36 -0400510 CBS session;
511 uint64_t version, ssl_version;
David Benjaminfd67aa82015-06-15 19:41:48 -0400512 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400513 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400514 version != kVersion ||
David Benjamin8be79a32015-08-23 01:37:36 -0400515 !CBS_get_asn1_uint64(&session, &ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400516 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400517 goto err;
518 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400519 /* Only support SSLv3/TLS and DTLS. */
520 if ((ssl_version >> 8) != SSL3_VERSION_MAJOR &&
521 (ssl_version >> 8) != (DTLS1_VERSION >> 8)) {
David Benjamin3570d732015-06-29 00:28:17 -0400522 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400523 goto err;
524 }
525 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700526
David Benjamin8be79a32015-08-23 01:37:36 -0400527 CBS cipher;
David Benjaminf3a8b122014-12-25 23:11:49 -0500528 uint16_t cipher_value;
David Benjamin8be79a32015-08-23 01:37:36 -0400529 if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
530 !CBS_get_u16(&cipher, &cipher_value) ||
531 CBS_len(&cipher) != 0) {
532 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400533 goto err;
534 }
David Benjamina1c90a52015-05-30 17:03:14 -0400535 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400536 if (ret->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400537 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin83fd6b62014-10-19 04:33:38 -0400538 goto err;
539 }
Adam Langley95c29f32014-06-20 12:00:00 -0700540
David Benjamin8be79a32015-08-23 01:37:36 -0400541 CBS session_id, master_key;
542 if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
543 CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
544 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
545 CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400546 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400547 goto err;
548 }
549 memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
550 ret->session_id_length = CBS_len(&session_id);
David Benjamin83fd6b62014-10-19 04:33:38 -0400551 memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
552 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700553
David Benjamin8be79a32015-08-23 01:37:36 -0400554 if (!SSL_SESSION_parse_long(&session, &ret->time, kTimeTag, time(NULL)) ||
555 !SSL_SESSION_parse_long(&session, &ret->timeout, kTimeoutTag, 3)) {
David Benjamin3570d732015-06-29 00:28:17 -0400556 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400557 goto err;
558 }
Adam Langley95c29f32014-06-20 12:00:00 -0700559
David Benjamin8be79a32015-08-23 01:37:36 -0400560 CBS peer;
561 int has_peer;
562 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag)) {
563 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
564 goto err;
565 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400566 X509_free(ret->peer);
567 ret->peer = NULL;
David Benjamin83fd6b62014-10-19 04:33:38 -0400568 if (has_peer) {
David Benjamin8be79a32015-08-23 01:37:36 -0400569 ret->peer = parse_x509(&peer);
David Benjamin83fd6b62014-10-19 04:33:38 -0400570 if (ret->peer == NULL) {
571 goto err;
572 }
David Benjamin8be79a32015-08-23 01:37:36 -0400573 if (CBS_len(&peer) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400574 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400575 goto err;
576 }
577 }
Adam Langley95c29f32014-06-20 12:00:00 -0700578
David Benjamin8be79a32015-08-23 01:37:36 -0400579 if (!SSL_SESSION_parse_bounded_octet_string(
580 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
581 kSessionIDContextTag) ||
582 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
583 X509_V_OK) ||
584 !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
585 kHostNameTag) ||
586 !SSL_SESSION_parse_string(&session, &ret->psk_identity,
587 kPSKIdentityTag) ||
588 !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
589 kTicketLifetimeHintTag, 0) ||
590 !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
591 &ret->tlsext_ticklen, kTicketTag)) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400592 goto err;
593 }
Adam Langley95c29f32014-06-20 12:00:00 -0700594
David Benjamin8be79a32015-08-23 01:37:36 -0400595 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
596 CBS child, peer_sha256;
597 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
598 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
599 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
600 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400601 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400602 goto err;
603 }
604 memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256));
605 ret->peer_sha256_valid = 1;
606 } else {
607 ret->peer_sha256_valid = 0;
608 }
Adam Langley95c29f32014-06-20 12:00:00 -0700609
David Benjamin8be79a32015-08-23 01:37:36 -0400610 if (!SSL_SESSION_parse_bounded_octet_string(
611 &session, ret->original_handshake_hash,
612 &ret->original_handshake_hash_len,
613 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
614 !SSL_SESSION_parse_octet_string(
615 &session, &ret->tlsext_signed_cert_timestamp_list,
616 &ret->tlsext_signed_cert_timestamp_list_length,
617 kSignedCertTimestampListTag) ||
618 !SSL_SESSION_parse_octet_string(
619 &session, &ret->ocsp_response, &ret->ocsp_response_length,
620 kOCSPResponseTag)) {
621 goto err;
622 }
623
624 int extended_master_secret;
625 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
626 kExtendedMasterSecretTag,
627 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400628 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400629 goto err;
630 }
David Benjamin8be79a32015-08-23 01:37:36 -0400631 ret->extended_master_secret = !!extended_master_secret;
632
633 if (!SSL_SESSION_parse_u32(&session, &ret->key_exchange_info,
David Benjamin26416e92015-08-22 16:04:17 -0400634 kKeyExchangeInfoTag, 0)) {
635 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
636 goto err;
637 }
638
639 CBS cert_chain;
640 int has_cert_chain;
641 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
642 kCertChainTag)) {
643 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
644 goto err;
645 }
646 sk_X509_pop_free(ret->cert_chain, X509_free);
647 ret->cert_chain = NULL;
648 if (has_cert_chain) {
649 ret->cert_chain = sk_X509_new_null();
650 if (ret->cert_chain == NULL) {
651 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
652 goto err;
653 }
654 while (CBS_len(&cert_chain) > 0) {
655 X509 *x509 = parse_x509(&cert_chain);
656 if (x509 == NULL) {
657 goto err;
658 }
659 if (!sk_X509_push(ret->cert_chain, x509)) {
660 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
661 X509_free(x509);
662 goto err;
663 }
664 }
665 }
666
667 if (CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400668 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
669 goto err;
670 }
Adam Langley95c29f32014-06-20 12:00:00 -0700671
David Benjaminfd67aa82015-06-15 19:41:48 -0400672 return ret;
673
674err:
675 SSL_SESSION_free(ret);
676 return NULL;
677}
678
679SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) {
680 CBS cbs;
681 CBS_init(&cbs, in, in_len);
682 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
683 if (ret == NULL) {
684 return NULL;
685 }
686 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400687 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400688 SSL_SESSION_free(ret);
689 return NULL;
690 }
691 return ret;
692}
693
694SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
695 if (length < 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400696 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjaminfd67aa82015-06-15 19:41:48 -0400697 return NULL;
698 }
699
700 CBS cbs;
701 CBS_init(&cbs, *pp, length);
702
703 SSL_SESSION *ret = SSL_SESSION_parse(&cbs);
704 if (ret == NULL) {
705 return NULL;
706 }
707
David Benjamin83fd6b62014-10-19 04:33:38 -0400708 if (a) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400709 SSL_SESSION_free(*a);
David Benjamin83fd6b62014-10-19 04:33:38 -0400710 *a = ret;
711 }
712 *pp = CBS_data(&cbs);
713 return ret;
David Benjamin83fd6b62014-10-19 04:33:38 -0400714}