blob: 1d6140e9adba1c8524f00af88be9cc1cb3cabdb3 [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 Benjamine8703a32017-07-09 16:17:55 -040083/* Per C99, various stdint.h macros are unavailable in C++ unless some macros
84 * are defined. C++11 overruled this decision, but older Android NDKs still
85 * require it. */
86#if !defined(__STDC_LIMIT_MACROS)
87#define __STDC_LIMIT_MACROS
88#endif
89
David Benjamin9e4e01e2015-09-15 01:48:04 -040090#include <openssl/ssl.h>
91
David Benjamin83fd6b62014-10-19 04:33:38 -040092#include <limits.h>
David Benjamin89abaea2014-10-19 13:50:18 -040093#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070094
Steven Valdez84b5c002016-08-25 16:30:58 -040095#include <openssl/buf.h>
David Benjamin83fd6b62014-10-19 04:33:38 -040096#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -070097#include <openssl/err.h>
Brian Smith054e6822015-03-27 21:12:01 -100098#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -070099#include <openssl/x509.h>
100
David Benjamin17cf2cb2016-12-13 01:07:13 -0500101#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400102#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700103
Adam Langley95c29f32014-06-20 12:00:00 -0700104
David Benjamin83fd6b62014-10-19 04:33:38 -0400105/* An SSL_SESSION is serialized as the following ASN.1 structure:
106 *
107 * SSLSession ::= SEQUENCE {
David Benjamin7bb88bb2016-11-01 13:59:01 -0400108 * version INTEGER (1), -- session structure version
David Benjamin83fd6b62014-10-19 04:33:38 -0400109 * sslVersion INTEGER, -- protocol version number
110 * cipher OCTET STRING, -- two bytes long
111 * sessionID OCTET STRING,
112 * masterKey OCTET STRING,
David Benjamin5b7b09c2016-11-03 20:32:10 -0400113 * time [1] INTEGER, -- seconds since UNIX epoch
114 * timeout [2] INTEGER, -- in seconds
David Benjamin83fd6b62014-10-19 04:33:38 -0400115 * peer [3] Certificate OPTIONAL,
116 * sessionIDContext [4] OCTET STRING OPTIONAL,
117 * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
118 * hostName [6] OCTET STRING OPTIONAL,
119 * -- from server_name extension
David Benjamin83fd6b62014-10-19 04:33:38 -0400120 * pskIdentity [8] OCTET STRING OPTIONAL,
121 * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
122 * ticket [10] OCTET STRING OPTIONAL, -- client-only
123 * peerSHA256 [13] OCTET STRING OPTIONAL,
124 * originalHandshakeHash [14] OCTET STRING OPTIONAL,
125 * signedCertTimestampList [15] OCTET STRING OPTIONAL,
126 * -- contents of SCT extension
127 * ocspResponse [16] OCTET STRING OPTIONAL,
David Benjamin26416e92015-08-22 16:04:17 -0400128 * -- stapled OCSP response from the server
Adam Langley75712922014-10-10 16:23:43 -0700129 * extendedMasterSecret [17] BOOLEAN OPTIONAL,
David Benjamin4882a6c2016-12-11 02:48:12 -0500130 * groupID [18] INTEGER OPTIONAL,
David Benjamin26416e92015-08-22 16:04:17 -0400131 * certChain [19] SEQUENCE OF Certificate OPTIONAL,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400132 * ticketAgeAdd [21] OCTET STRING OPTIONAL,
Adam Langley364f7a62016-12-12 10:51:00 -0800133 * isServer [22] BOOLEAN DEFAULT TRUE,
David Benjaminf1050fd2016-12-13 20:05:36 -0500134 * peerSignatureAlgorithm [23] INTEGER OPTIONAL,
Steven Valdez08b65f42016-12-07 15:29:45 -0500135 * ticketMaxEarlyData [24] INTEGER OPTIONAL,
David Benjamin17b30832017-01-28 14:00:32 -0500136 * authTimeout [25] INTEGER OPTIONAL, -- defaults to timeout
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500137 * earlyALPN [26] OCTET STRING OPTIONAL,
David Benjamin83fd6b62014-10-19 04:33:38 -0400138 * }
139 *
David Benjamin688d8df2014-11-02 23:06:42 -0500140 * Note: historically this serialization has included other optional
David Benjamin17b30832017-01-28 14:00:32 -0500141 * fields. Their presence is currently treated as a parse error:
David Benjamin688d8df2014-11-02 23:06:42 -0500142 *
143 * keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
144 * pskIdentityHint [7] OCTET STRING OPTIONAL,
145 * compressionMethod [11] OCTET STRING OPTIONAL,
Steven Valdez5b986082016-09-01 12:29:49 -0400146 * srpUsername [12] OCTET STRING OPTIONAL,
147 * ticketFlags [20] INTEGER OPTIONAL,
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800148 */
David Benjamin83fd6b62014-10-19 04:33:38 -0400149
Adam Langley96c2a282015-06-02 14:16:44 -0700150static const unsigned kVersion = 1;
David Benjamin7f393f72015-10-17 12:36:12 -0400151
David Benjamin83fd6b62014-10-19 04:33:38 -0400152static const int kTimeTag =
153 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
154static const int kTimeoutTag =
155 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
156static const int kPeerTag =
157 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
David Benjamin7f393f72015-10-17 12:36:12 -0400158static const int kSessionIDContextTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400159 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
160static const int kVerifyResultTag =
161 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
162static const int kHostNameTag =
163 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
David Benjamin83fd6b62014-10-19 04:33:38 -0400164static const int kPSKIdentityTag =
165 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
166static const int kTicketLifetimeHintTag =
167 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
168static const int kTicketTag =
169 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
170static const int kPeerSHA256Tag =
171 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
172static const int kOriginalHandshakeHashTag =
173 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
174static const int kSignedCertTimestampListTag =
175 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
176static const int kOCSPResponseTag =
177 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
Adam Langley75712922014-10-10 16:23:43 -0700178static const int kExtendedMasterSecretTag =
179 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
David Benjamin4882a6c2016-12-11 02:48:12 -0500180static const int kGroupIDTag =
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200181 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
David Benjamin26416e92015-08-22 16:04:17 -0400182static const int kCertChainTag =
183 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400184static const int kTicketAgeAddTag =
185 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21;
Adam Langley364f7a62016-12-12 10:51:00 -0800186static const int kIsServerTag =
187 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22;
David Benjaminf1050fd2016-12-13 20:05:36 -0500188static const int kPeerSignatureAlgorithmTag =
189 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 23;
Steven Valdez08b65f42016-12-07 15:29:45 -0500190static const int kTicketMaxEarlyDataTag =
191 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 24;
David Benjamin17b30832017-01-28 14:00:32 -0500192static const int kAuthTimeoutTag =
193 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 25;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500194static const int kEarlyALPNTag =
195 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 26;
David Benjamin26416e92015-08-22 16:04:17 -0400196
David Benjamin14e2b502015-09-13 14:48:12 -0400197static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400198 size_t *out_len, int for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400199 CBB cbb, session, child, child2;
Adam Langley95c29f32014-06-20 12:00:00 -0700200
David Benjaminf3a8b122014-12-25 23:11:49 -0500201 if (in == NULL || in->cipher == NULL) {
David Benjamin89abaea2014-10-19 13:50:18 -0400202 return 0;
203 }
Adam Langley95c29f32014-06-20 12:00:00 -0700204
David Benjamina8653202015-06-28 01:26:10 -0400205 CBB_zero(&cbb);
206 if (!CBB_init(&cbb, 0) ||
207 !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400208 !CBB_add_asn1_uint64(&session, kVersion) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400209 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
210 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminf3a8b122014-12-25 23:11:49 -0500211 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400212 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjamin3cac4502014-10-21 01:46:30 -0400213 /* The session ID is irrelevant for a session ticket. */
214 !CBB_add_bytes(&child, in->session_id,
215 for_ticket ? 0 : in->session_id_length) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400216 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500217 !CBB_add_bytes(&child, in->master_key, in->master_key_length) ||
218 !CBB_add_asn1(&session, &child, kTimeTag) ||
219 !CBB_add_asn1_uint64(&child, in->time) ||
220 !CBB_add_asn1(&session, &child, kTimeoutTag) ||
David Benjamin5b7b09c2016-11-03 20:32:10 -0400221 !CBB_add_asn1_uint64(&child, in->timeout)) {
222 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
223 goto err;
David Benjamin89abaea2014-10-19 13:50:18 -0400224 }
Adam Langley95c29f32014-06-20 12:00:00 -0700225
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800226 /* The peer certificate is only serialized if the SHA-256 isn't
227 * serialized instead. */
Adam Langley68e71242016-12-12 11:06:16 -0800228 if (sk_CRYPTO_BUFFER_num(in->certs) > 0 && !in->peer_sha256_valid) {
229 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, 0);
230 if (!CBB_add_asn1(&session, &child, kPeerTag) ||
231 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
232 CRYPTO_BUFFER_len(buffer))) {
David Benjamin3570d732015-06-29 00:28:17 -0400233 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400234 goto err;
235 }
David Benjamin89abaea2014-10-19 13:50:18 -0400236 }
Adam Langley95c29f32014-06-20 12:00:00 -0700237
David Benjamin89abaea2014-10-19 13:50:18 -0400238 /* Although it is OPTIONAL and usually empty, OpenSSL has
239 * historically always encoded the sid_ctx. */
240 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
241 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
242 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400243 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400244 goto err;
245 }
Adam Langley95c29f32014-06-20 12:00:00 -0700246
David Benjamin89abaea2014-10-19 13:50:18 -0400247 if (in->verify_result != X509_V_OK) {
248 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
249 !CBB_add_asn1_uint64(&child, in->verify_result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400250 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400251 goto err;
252 }
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
David Benjamin89abaea2014-10-19 13:50:18 -0400255 if (in->tlsext_hostname) {
256 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
257 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
258 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
259 strlen(in->tlsext_hostname))) {
David Benjamin3570d732015-06-29 00:28:17 -0400260 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400261 goto err;
262 }
263 }
Adam Langley95c29f32014-06-20 12:00:00 -0700264
David Benjamin89abaea2014-10-19 13:50:18 -0400265 if (in->psk_identity) {
266 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
267 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
268 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
269 strlen(in->psk_identity))) {
David Benjamin3570d732015-06-29 00:28:17 -0400270 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400271 goto err;
272 }
273 }
Adam Langley95c29f32014-06-20 12:00:00 -0700274
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700275 if (in->tlsext_tick_lifetime_hint > 0) {
David Benjamin89abaea2014-10-19 13:50:18 -0400276 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700277 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400278 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400279 goto err;
280 }
281 }
Adam Langley95c29f32014-06-20 12:00:00 -0700282
David Benjamin95d31822015-06-15 19:53:32 -0400283 if (in->tlsext_tick && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400284 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
285 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
286 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -0400287 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400288 goto err;
289 }
290 }
Adam Langley75872532014-06-20 12:00:00 -0700291
David Benjamin89abaea2014-10-19 13:50:18 -0400292 if (in->peer_sha256_valid) {
293 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
294 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
295 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
David Benjamin3570d732015-06-29 00:28:17 -0400296 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400297 goto err;
298 }
299 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700300
David Benjamin89abaea2014-10-19 13:50:18 -0400301 if (in->original_handshake_hash_len > 0) {
302 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
303 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
304 !CBB_add_bytes(&child2, in->original_handshake_hash,
305 in->original_handshake_hash_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400306 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400307 goto err;
308 }
309 }
Adam Langley95c29f32014-06-20 12:00:00 -0700310
David Benjamin89abaea2014-10-19 13:50:18 -0400311 if (in->tlsext_signed_cert_timestamp_list_length > 0) {
312 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
313 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
314 !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
315 in->tlsext_signed_cert_timestamp_list_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400316 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400317 goto err;
318 }
319 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200320
David Benjamin89abaea2014-10-19 13:50:18 -0400321 if (in->ocsp_response_length > 0) {
322 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
323 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
324 !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400325 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400326 goto err;
327 }
328 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400329
Adam Langley75712922014-10-10 16:23:43 -0700330 if (in->extended_master_secret) {
331 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
332 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
333 !CBB_add_u8(&child2, 0xff)) {
David Benjamin3570d732015-06-29 00:28:17 -0400334 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langley75712922014-10-10 16:23:43 -0700335 goto err;
336 }
337 }
338
David Benjamin4882a6c2016-12-11 02:48:12 -0500339 if (in->group_id > 0 &&
340 (!CBB_add_asn1(&session, &child, kGroupIDTag) ||
341 !CBB_add_asn1_uint64(&child, in->group_id))) {
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200342 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
343 goto err;
344 }
345
David Benjamin26416e92015-08-22 16:04:17 -0400346 /* The certificate chain is only serialized if the leaf's SHA-256 isn't
347 * serialized instead. */
Adam Langley68e71242016-12-12 11:06:16 -0800348 if (in->certs != NULL &&
349 !in->peer_sha256_valid &&
350 sk_CRYPTO_BUFFER_num(in->certs) >= 2) {
David Benjamin26416e92015-08-22 16:04:17 -0400351 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
352 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
353 goto err;
354 }
Adam Langley68e71242016-12-12 11:06:16 -0800355 for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs); i++) {
356 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, i);
357 if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
358 CRYPTO_BUFFER_len(buffer))) {
359 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin26416e92015-08-22 16:04:17 -0400360 goto err;
361 }
362 }
363 }
364
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400365 if (in->ticket_age_add_valid) {
366 if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
367 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
368 !CBB_add_u32(&child2, in->ticket_age_add)) {
369 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
370 goto err;
371 }
372 }
373
Adam Langley364f7a62016-12-12 10:51:00 -0800374 if (!in->is_server) {
375 if (!CBB_add_asn1(&session, &child, kIsServerTag) ||
376 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
377 !CBB_add_u8(&child2, 0x00)) {
378 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
379 goto err;
380 }
381 }
382
David Benjaminf1050fd2016-12-13 20:05:36 -0500383 if (in->peer_signature_algorithm != 0 &&
384 (!CBB_add_asn1(&session, &child, kPeerSignatureAlgorithmTag) ||
385 !CBB_add_asn1_uint64(&child, in->peer_signature_algorithm))) {
386 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
387 goto err;
388 }
389
Steven Valdez08b65f42016-12-07 15:29:45 -0500390 if (in->ticket_max_early_data != 0 &&
391 (!CBB_add_asn1(&session, &child, kTicketMaxEarlyDataTag) ||
392 !CBB_add_asn1_uint64(&child, in->ticket_max_early_data))) {
393 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
394 goto err;
395 }
396
David Benjamin17b30832017-01-28 14:00:32 -0500397 if (in->timeout != in->auth_timeout &&
398 (!CBB_add_asn1(&session, &child, kAuthTimeoutTag) ||
399 !CBB_add_asn1_uint64(&child, in->auth_timeout))) {
400 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
401 goto err;
402 }
403
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500404 if (in->early_alpn) {
405 if (!CBB_add_asn1(&session, &child, kEarlyALPNTag) ||
406 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
407 !CBB_add_bytes(&child2, (const uint8_t *)in->early_alpn,
408 in->early_alpn_len)) {
409 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
410 goto err;
411 }
412 }
413
David Benjamin3cac4502014-10-21 01:46:30 -0400414 if (!CBB_finish(&cbb, out_data, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400415 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin89abaea2014-10-19 13:50:18 -0400416 goto err;
417 }
David Benjamin3cac4502014-10-21 01:46:30 -0400418 return 1;
419
420 err:
421 CBB_cleanup(&cbb);
422 return 0;
423}
424
David Benjamin14e2b502015-09-13 14:48:12 -0400425int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
426 size_t *out_len) {
Steven Valdez84b5c002016-08-25 16:30:58 -0400427 if (in->not_resumable) {
428 /* If the caller has an unresumable session, e.g. if |SSL_get_session| were
429 * called on a TLS 1.3 or False Started connection, serialize with a
430 * placeholder value so it is not accidentally deserialized into a resumable
431 * one. */
432 static const char kNotResumableSession[] = "NOT RESUMABLE";
433
434 *out_len = strlen(kNotResumableSession);
David Benjamine8703a32017-07-09 16:17:55 -0400435 *out_data = (uint8_t *)BUF_memdup(kNotResumableSession, *out_len);
Steven Valdez84b5c002016-08-25 16:30:58 -0400436 if (*out_data == NULL) {
437 return 0;
438 }
439
440 return 1;
441 }
442
David Benjamin3cac4502014-10-21 01:46:30 -0400443 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
444}
445
David Benjamin14e2b502015-09-13 14:48:12 -0400446int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400447 size_t *out_len) {
448 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
449}
450
451int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
452 uint8_t *out;
453 size_t len;
454
455 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
456 return -1;
457 }
Adam Langley95c29f32014-06-20 12:00:00 -0700458
David Benjamin89abaea2014-10-19 13:50:18 -0400459 if (len > INT_MAX) {
460 OPENSSL_free(out);
David Benjamin3570d732015-06-29 00:28:17 -0400461 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin89abaea2014-10-19 13:50:18 -0400462 return -1;
463 }
Adam Langley95c29f32014-06-20 12:00:00 -0700464
David Benjamin89abaea2014-10-19 13:50:18 -0400465 if (pp) {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500466 OPENSSL_memcpy(*pp, out, len);
David Benjamin89abaea2014-10-19 13:50:18 -0400467 *pp += len;
468 }
469 OPENSSL_free(out);
Adam Langley95c29f32014-06-20 12:00:00 -0700470
David Benjamin89abaea2014-10-19 13:50:18 -0400471 return len;
David Benjamin89abaea2014-10-19 13:50:18 -0400472}
Adam Langley95c29f32014-06-20 12:00:00 -0700473
David Benjaminfd67aa82015-06-15 19:41:48 -0400474/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400475 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
476 * entry, if |*out| is not NULL, it frees the existing contents. If
477 * the element was not found, it sets |*out| to NULL. It returns one
478 * on success, whether or not the element was found, and zero on
479 * decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400480static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400481 CBS value;
482 int present;
483 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400484 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400485 return 0;
486 }
487 if (present) {
488 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400489 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400490 return 0;
491 }
492 if (!CBS_strdup(&value, out)) {
David Benjamin3570d732015-06-29 00:28:17 -0400493 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400494 return 0;
495 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400496 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400497 OPENSSL_free(*out);
498 *out = NULL;
499 }
500 return 1;
501}
Adam Langley95c29f32014-06-20 12:00:00 -0700502
David Benjaminfd67aa82015-06-15 19:41:48 -0400503/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400504 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
505 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
506 * contents. On entry, if the element was not found, it sets
507 * |*out_ptr| to NULL. It returns one on success, whether or not the
508 * element was found, and zero on decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400509static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin8be79a32015-08-23 01:37:36 -0400510 size_t *out_len, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400511 CBS value;
512 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400513 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400514 return 0;
515 }
516 if (!CBS_stow(&value, out_ptr, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400517 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400518 return 0;
519 }
520 return 1;
521}
Adam Langley95c29f32014-06-20 12:00:00 -0700522
David Benjamin8be79a32015-08-23 01:37:36 -0400523/* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
524 * explicitly tagged with |tag| of size at most |max_out|. */
525static int SSL_SESSION_parse_bounded_octet_string(
David Benjamin30c4c302016-12-07 22:35:24 -0500526 CBS *cbs, uint8_t *out, uint8_t *out_len, uint8_t max_out, unsigned tag) {
David Benjamin8be79a32015-08-23 01:37:36 -0400527 CBS value;
528 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
529 CBS_len(&value) > max_out) {
530 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
531 return 0;
532 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500533 OPENSSL_memcpy(out, CBS_data(&value), CBS_len(&value));
David Benjamin30c4c302016-12-07 22:35:24 -0500534 *out_len = (uint8_t)CBS_len(&value);
David Benjamin8be79a32015-08-23 01:37:36 -0400535 return 1;
536}
Adam Langley95c29f32014-06-20 12:00:00 -0700537
David Benjamin8be79a32015-08-23 01:37:36 -0400538static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
539 long default_value) {
540 uint64_t value;
541 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
542 (uint64_t)default_value) ||
543 value > LONG_MAX) {
544 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
545 return 0;
546 }
547 *out = (long)value;
548 return 1;
549}
550
551static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
552 uint32_t default_value) {
553 uint64_t value;
554 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
555 (uint64_t)default_value) ||
556 value > 0xffffffff) {
557 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
558 return 0;
559 }
560 *out = (uint32_t)value;
561 return 1;
562}
563
David Benjaminf1050fd2016-12-13 20:05:36 -0500564static int SSL_SESSION_parse_u16(CBS *cbs, uint16_t *out, unsigned tag,
565 uint16_t default_value) {
566 uint64_t value;
567 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
568 (uint64_t)default_value) ||
569 value > 0xffff) {
570 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
571 return 0;
572 }
573 *out = (uint16_t)value;
574 return 1;
575}
576
Adam Langley46db7af2017-02-01 15:49:37 -0800577SSL_SESSION *SSL_SESSION_parse(CBS *cbs, const SSL_X509_METHOD *x509_method,
578 CRYPTO_BUFFER_POOL *pool) {
579 SSL_SESSION *ret = ssl_session_new(x509_method);
David Benjaminfd67aa82015-06-15 19:41:48 -0400580 if (ret == NULL) {
581 goto err;
David Benjamin83fd6b62014-10-19 04:33:38 -0400582 }
Adam Langley95c29f32014-06-20 12:00:00 -0700583
David Benjamin8be79a32015-08-23 01:37:36 -0400584 CBS session;
585 uint64_t version, ssl_version;
Steven Valdez8f36c512017-06-20 10:55:02 -0400586 uint16_t unused;
David Benjaminfd67aa82015-06-15 19:41:48 -0400587 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400588 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400589 version != kVersion ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400590 !CBS_get_asn1_uint64(&session, &ssl_version) ||
591 /* Require sessions have versions valid in either TLS or DTLS. The session
592 * will not be used by the handshake if not applicable, but, for
593 * simplicity, never parse a session that does not pass
594 * |ssl_protocol_version_from_wire|. */
595 ssl_version > UINT16_MAX ||
596 !ssl_protocol_version_from_wire(&unused, ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400597 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400598 goto err;
599 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400600 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700601
David Benjamin8be79a32015-08-23 01:37:36 -0400602 CBS cipher;
David Benjaminf3a8b122014-12-25 23:11:49 -0500603 uint16_t cipher_value;
David Benjamin8be79a32015-08-23 01:37:36 -0400604 if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
605 !CBS_get_u16(&cipher, &cipher_value) ||
606 CBS_len(&cipher) != 0) {
607 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400608 goto err;
609 }
David Benjamina1c90a52015-05-30 17:03:14 -0400610 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400611 if (ret->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400612 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin83fd6b62014-10-19 04:33:38 -0400613 goto err;
614 }
Adam Langley95c29f32014-06-20 12:00:00 -0700615
David Benjamin8be79a32015-08-23 01:37:36 -0400616 CBS session_id, master_key;
617 if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
618 CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
619 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
620 CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400621 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400622 goto err;
623 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500624 OPENSSL_memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
David Benjamin83fd6b62014-10-19 04:33:38 -0400625 ret->session_id_length = CBS_len(&session_id);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500626 OPENSSL_memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
David Benjamin83fd6b62014-10-19 04:33:38 -0400627 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700628
David Benjamin5b7b09c2016-11-03 20:32:10 -0400629 CBS child;
David Benjaminad8f5e12017-02-20 17:00:20 -0500630 uint64_t timeout;
David Benjamin5b7b09c2016-11-03 20:32:10 -0400631 if (!CBS_get_asn1(&session, &child, kTimeTag) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500632 !CBS_get_asn1_uint64(&child, &ret->time) ||
David Benjamin5b7b09c2016-11-03 20:32:10 -0400633 !CBS_get_asn1(&session, &child, kTimeoutTag) ||
634 !CBS_get_asn1_uint64(&child, &timeout) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500635 timeout > UINT32_MAX) {
David Benjamin3570d732015-06-29 00:28:17 -0400636 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400637 goto err;
638 }
Adam Langley95c29f32014-06-20 12:00:00 -0700639
David Benjaminad8f5e12017-02-20 17:00:20 -0500640 ret->timeout = (uint32_t)timeout;
David Benjamin5b7b09c2016-11-03 20:32:10 -0400641
David Benjamin8be79a32015-08-23 01:37:36 -0400642 CBS peer;
643 int has_peer;
Adam Langley68e71242016-12-12 11:06:16 -0800644 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
645 (has_peer && CBS_len(&peer) == 0)) {
David Benjamin8be79a32015-08-23 01:37:36 -0400646 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
647 goto err;
648 }
Adam Langley68e71242016-12-12 11:06:16 -0800649 /* |peer| is processed with the certificate chain. */
Adam Langley95c29f32014-06-20 12:00:00 -0700650
David Benjamin8be79a32015-08-23 01:37:36 -0400651 if (!SSL_SESSION_parse_bounded_octet_string(
652 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
653 kSessionIDContextTag) ||
654 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
655 X509_V_OK) ||
656 !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
657 kHostNameTag) ||
658 !SSL_SESSION_parse_string(&session, &ret->psk_identity,
659 kPSKIdentityTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700660 !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
David Benjamin8be79a32015-08-23 01:37:36 -0400661 kTicketLifetimeHintTag, 0) ||
662 !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
663 &ret->tlsext_ticklen, kTicketTag)) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400664 goto err;
665 }
Adam Langley95c29f32014-06-20 12:00:00 -0700666
David Benjamin8be79a32015-08-23 01:37:36 -0400667 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
David Benjamin5b7b09c2016-11-03 20:32:10 -0400668 CBS peer_sha256;
David Benjamin8be79a32015-08-23 01:37:36 -0400669 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
670 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
671 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
672 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400673 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400674 goto err;
675 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500676 OPENSSL_memcpy(ret->peer_sha256, CBS_data(&peer_sha256),
677 sizeof(ret->peer_sha256));
David Benjamin83fd6b62014-10-19 04:33:38 -0400678 ret->peer_sha256_valid = 1;
679 } else {
680 ret->peer_sha256_valid = 0;
681 }
Adam Langley95c29f32014-06-20 12:00:00 -0700682
David Benjamin8be79a32015-08-23 01:37:36 -0400683 if (!SSL_SESSION_parse_bounded_octet_string(
684 &session, ret->original_handshake_hash,
685 &ret->original_handshake_hash_len,
686 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
687 !SSL_SESSION_parse_octet_string(
688 &session, &ret->tlsext_signed_cert_timestamp_list,
689 &ret->tlsext_signed_cert_timestamp_list_length,
690 kSignedCertTimestampListTag) ||
691 !SSL_SESSION_parse_octet_string(
692 &session, &ret->ocsp_response, &ret->ocsp_response_length,
693 kOCSPResponseTag)) {
694 goto err;
695 }
696
697 int extended_master_secret;
698 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
699 kExtendedMasterSecretTag,
700 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400701 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400702 goto err;
703 }
David Benjamin8be79a32015-08-23 01:37:36 -0400704 ret->extended_master_secret = !!extended_master_secret;
705
David Benjamin11fa7032017-04-11 22:13:45 -0400706 if (!SSL_SESSION_parse_u16(&session, &ret->group_id, kGroupIDTag, 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400707 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
708 goto err;
709 }
710
711 CBS cert_chain;
Adam Langley68e71242016-12-12 11:06:16 -0800712 CBS_init(&cert_chain, NULL, 0);
David Benjamin26416e92015-08-22 16:04:17 -0400713 int has_cert_chain;
714 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
Adam Langley68e71242016-12-12 11:06:16 -0800715 kCertChainTag) ||
716 (has_cert_chain && CBS_len(&cert_chain) == 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400717 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
718 goto err;
719 }
Adam Langley68e71242016-12-12 11:06:16 -0800720 if (has_cert_chain && !has_peer) {
721 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
722 goto err;
Adam Langley364f7a62016-12-12 10:51:00 -0800723 }
Adam Langley68e71242016-12-12 11:06:16 -0800724 if (has_peer || has_cert_chain) {
725 ret->certs = sk_CRYPTO_BUFFER_new_null();
726 if (ret->certs == NULL) {
David Benjamin26416e92015-08-22 16:04:17 -0400727 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
728 goto err;
729 }
Adam Langley68e71242016-12-12 11:06:16 -0800730
731 if (has_peer) {
Adam Langleyd519bf62016-12-12 11:16:44 -0800732 /* TODO(agl): this should use the |SSL_CTX|'s pool. */
Adam Langley46db7af2017-02-01 15:49:37 -0800733 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&peer, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800734 if (buffer == NULL ||
735 !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
736 CRYPTO_BUFFER_free(buffer);
Adam Langleye8509092016-11-07 14:24:33 -0800737 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin26416e92015-08-22 16:04:17 -0400738 goto err;
739 }
740 }
Adam Langley68e71242016-12-12 11:06:16 -0800741
742 while (CBS_len(&cert_chain) > 0) {
743 CBS cert;
744 if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) ||
745 CBS_len(&cert) == 0) {
746 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
747 goto err;
748 }
749
Adam Langleyd519bf62016-12-12 11:16:44 -0800750 /* TODO(agl): this should use the |SSL_CTX|'s pool. */
Adam Langley46db7af2017-02-01 15:49:37 -0800751 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800752 if (buffer == NULL ||
753 !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
754 CRYPTO_BUFFER_free(buffer);
755 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
756 goto err;
757 }
758 }
759 }
760
Adam Langley46db7af2017-02-01 15:49:37 -0800761 if (!x509_method->session_cache_objects(ret)) {
Adam Langley68e71242016-12-12 11:06:16 -0800762 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
763 goto err;
David Benjamin26416e92015-08-22 16:04:17 -0400764 }
765
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400766 CBS age_add;
767 int age_add_present;
Steven Valdez5b986082016-09-01 12:29:49 -0400768 if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400769 kTicketAgeAddTag) ||
770 (age_add_present &&
771 !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
772 CBS_len(&age_add) != 0) {
773 goto err;
774 }
775 ret->ticket_age_add_valid = age_add_present;
776
Adam Langley364f7a62016-12-12 10:51:00 -0800777 int is_server;
778 if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag,
779 1 /* default to true */)) {
780 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
781 goto err;
782 }
783 /* TODO: in time we can include |is_server| for servers too, then we can
784 enforce that client and server sessions are never mixed up. */
785
786 ret->is_server = is_server;
787
David Benjaminf1050fd2016-12-13 20:05:36 -0500788 if (!SSL_SESSION_parse_u16(&session, &ret->peer_signature_algorithm,
789 kPeerSignatureAlgorithmTag, 0) ||
Steven Valdez08b65f42016-12-07 15:29:45 -0500790 !SSL_SESSION_parse_u32(&session, &ret->ticket_max_early_data,
791 kTicketMaxEarlyDataTag, 0) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500792 !SSL_SESSION_parse_u32(&session, &ret->auth_timeout, kAuthTimeoutTag,
793 ret->timeout) ||
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500794 !SSL_SESSION_parse_octet_string(&session, &ret->early_alpn,
795 &ret->early_alpn_len, kEarlyALPNTag) ||
David Benjaminf1050fd2016-12-13 20:05:36 -0500796 CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400797 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
798 goto err;
799 }
Adam Langley95c29f32014-06-20 12:00:00 -0700800
David Benjaminfd67aa82015-06-15 19:41:48 -0400801 return ret;
802
803err:
804 SSL_SESSION_free(ret);
805 return NULL;
806}
807
Adam Langley46db7af2017-02-01 15:49:37 -0800808SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len,
809 const SSL_CTX *ctx) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400810 CBS cbs;
811 CBS_init(&cbs, in, in_len);
Adam Langley46db7af2017-02-01 15:49:37 -0800812 SSL_SESSION *ret = SSL_SESSION_parse(&cbs, ctx->x509_method, ctx->pool);
David Benjaminfd67aa82015-06-15 19:41:48 -0400813 if (ret == NULL) {
814 return NULL;
815 }
816 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400817 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400818 SSL_SESSION_free(ret);
819 return NULL;
820 }
821 return ret;
822}