blob: a1c53d5bd4f4a79a35143d4771458dcce0012ef3 [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 Benjamin86e95b82017-07-18 16:34:25 -0400105namespace bssl {
106
David Benjamin83fd6b62014-10-19 04:33:38 -0400107/* An SSL_SESSION is serialized as the following ASN.1 structure:
108 *
109 * SSLSession ::= SEQUENCE {
David Benjamin7bb88bb2016-11-01 13:59:01 -0400110 * version INTEGER (1), -- session structure version
David Benjamin83fd6b62014-10-19 04:33:38 -0400111 * sslVersion INTEGER, -- protocol version number
112 * cipher OCTET STRING, -- two bytes long
113 * sessionID OCTET STRING,
114 * masterKey OCTET STRING,
David Benjamin5b7b09c2016-11-03 20:32:10 -0400115 * time [1] INTEGER, -- seconds since UNIX epoch
116 * timeout [2] INTEGER, -- in seconds
David Benjamin83fd6b62014-10-19 04:33:38 -0400117 * peer [3] Certificate OPTIONAL,
118 * sessionIDContext [4] OCTET STRING OPTIONAL,
119 * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
120 * hostName [6] OCTET STRING OPTIONAL,
121 * -- from server_name extension
David Benjamin83fd6b62014-10-19 04:33:38 -0400122 * pskIdentity [8] OCTET STRING OPTIONAL,
123 * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
124 * ticket [10] OCTET STRING OPTIONAL, -- client-only
125 * peerSHA256 [13] OCTET STRING OPTIONAL,
126 * originalHandshakeHash [14] OCTET STRING OPTIONAL,
127 * signedCertTimestampList [15] OCTET STRING OPTIONAL,
128 * -- contents of SCT extension
129 * ocspResponse [16] OCTET STRING OPTIONAL,
David Benjamin26416e92015-08-22 16:04:17 -0400130 * -- stapled OCSP response from the server
Adam Langley75712922014-10-10 16:23:43 -0700131 * extendedMasterSecret [17] BOOLEAN OPTIONAL,
David Benjamin4882a6c2016-12-11 02:48:12 -0500132 * groupID [18] INTEGER OPTIONAL,
David Benjamin26416e92015-08-22 16:04:17 -0400133 * certChain [19] SEQUENCE OF Certificate OPTIONAL,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400134 * ticketAgeAdd [21] OCTET STRING OPTIONAL,
Adam Langley364f7a62016-12-12 10:51:00 -0800135 * isServer [22] BOOLEAN DEFAULT TRUE,
David Benjaminf1050fd2016-12-13 20:05:36 -0500136 * peerSignatureAlgorithm [23] INTEGER OPTIONAL,
Steven Valdez08b65f42016-12-07 15:29:45 -0500137 * ticketMaxEarlyData [24] INTEGER OPTIONAL,
David Benjamin17b30832017-01-28 14:00:32 -0500138 * authTimeout [25] INTEGER OPTIONAL, -- defaults to timeout
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500139 * earlyALPN [26] OCTET STRING OPTIONAL,
David Benjamin83fd6b62014-10-19 04:33:38 -0400140 * }
141 *
David Benjamin688d8df2014-11-02 23:06:42 -0500142 * Note: historically this serialization has included other optional
David Benjamin17b30832017-01-28 14:00:32 -0500143 * fields. Their presence is currently treated as a parse error:
David Benjamin688d8df2014-11-02 23:06:42 -0500144 *
145 * keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
146 * pskIdentityHint [7] OCTET STRING OPTIONAL,
147 * compressionMethod [11] OCTET STRING OPTIONAL,
Steven Valdez5b986082016-09-01 12:29:49 -0400148 * srpUsername [12] OCTET STRING OPTIONAL,
149 * ticketFlags [20] INTEGER OPTIONAL,
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800150 */
David Benjamin83fd6b62014-10-19 04:33:38 -0400151
Adam Langley96c2a282015-06-02 14:16:44 -0700152static const unsigned kVersion = 1;
David Benjamin7f393f72015-10-17 12:36:12 -0400153
David Benjamin83fd6b62014-10-19 04:33:38 -0400154static const int kTimeTag =
155 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
156static const int kTimeoutTag =
157 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
158static const int kPeerTag =
159 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
David Benjamin7f393f72015-10-17 12:36:12 -0400160static const int kSessionIDContextTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400161 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
162static const int kVerifyResultTag =
163 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
164static const int kHostNameTag =
165 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
David Benjamin83fd6b62014-10-19 04:33:38 -0400166static const int kPSKIdentityTag =
167 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
168static const int kTicketLifetimeHintTag =
169 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
170static const int kTicketTag =
171 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
172static const int kPeerSHA256Tag =
173 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
174static const int kOriginalHandshakeHashTag =
175 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
176static const int kSignedCertTimestampListTag =
177 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
178static const int kOCSPResponseTag =
179 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
Adam Langley75712922014-10-10 16:23:43 -0700180static const int kExtendedMasterSecretTag =
181 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
David Benjamin4882a6c2016-12-11 02:48:12 -0500182static const int kGroupIDTag =
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200183 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
David Benjamin26416e92015-08-22 16:04:17 -0400184static const int kCertChainTag =
185 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400186static const int kTicketAgeAddTag =
187 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21;
Adam Langley364f7a62016-12-12 10:51:00 -0800188static const int kIsServerTag =
189 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22;
David Benjaminf1050fd2016-12-13 20:05:36 -0500190static const int kPeerSignatureAlgorithmTag =
191 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 23;
Steven Valdez08b65f42016-12-07 15:29:45 -0500192static const int kTicketMaxEarlyDataTag =
193 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 24;
David Benjamin17b30832017-01-28 14:00:32 -0500194static const int kAuthTimeoutTag =
195 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 25;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500196static const int kEarlyALPNTag =
197 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 26;
David Benjamin26416e92015-08-22 16:04:17 -0400198
David Benjamin14e2b502015-09-13 14:48:12 -0400199static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400200 size_t *out_len, int for_ticket) {
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 Benjamin1386aad2017-07-19 23:57:40 -0400205 ScopedCBB cbb;
206 CBB session, child, child2;
207 if (!CBB_init(cbb.get(), 0) ||
208 !CBB_add_asn1(cbb.get(), &session, CBS_ASN1_SEQUENCE) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400209 !CBB_add_asn1_uint64(&session, kVersion) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400210 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
211 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminf3a8b122014-12-25 23:11:49 -0500212 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400213 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjamin3cac4502014-10-21 01:46:30 -0400214 /* The session ID is irrelevant for a session ticket. */
215 !CBB_add_bytes(&child, in->session_id,
216 for_ticket ? 0 : in->session_id_length) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400217 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500218 !CBB_add_bytes(&child, in->master_key, in->master_key_length) ||
219 !CBB_add_asn1(&session, &child, kTimeTag) ||
220 !CBB_add_asn1_uint64(&child, in->time) ||
221 !CBB_add_asn1(&session, &child, kTimeoutTag) ||
David Benjamin5b7b09c2016-11-03 20:32:10 -0400222 !CBB_add_asn1_uint64(&child, in->timeout)) {
223 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400224 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400225 }
Adam Langley95c29f32014-06-20 12:00:00 -0700226
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800227 /* The peer certificate is only serialized if the SHA-256 isn't
228 * serialized instead. */
Adam Langley68e71242016-12-12 11:06:16 -0800229 if (sk_CRYPTO_BUFFER_num(in->certs) > 0 && !in->peer_sha256_valid) {
230 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, 0);
231 if (!CBB_add_asn1(&session, &child, kPeerTag) ||
232 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
233 CRYPTO_BUFFER_len(buffer))) {
David Benjamin3570d732015-06-29 00:28:17 -0400234 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400235 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400236 }
David Benjamin89abaea2014-10-19 13:50:18 -0400237 }
Adam Langley95c29f32014-06-20 12:00:00 -0700238
David Benjamin89abaea2014-10-19 13:50:18 -0400239 /* Although it is OPTIONAL and usually empty, OpenSSL has
240 * historically always encoded the sid_ctx. */
241 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
242 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
243 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400244 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400245 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400246 }
Adam Langley95c29f32014-06-20 12:00:00 -0700247
David Benjamin89abaea2014-10-19 13:50:18 -0400248 if (in->verify_result != X509_V_OK) {
249 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
250 !CBB_add_asn1_uint64(&child, in->verify_result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400251 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400252 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400253 }
254 }
Adam Langley95c29f32014-06-20 12:00:00 -0700255
David Benjamin89abaea2014-10-19 13:50:18 -0400256 if (in->tlsext_hostname) {
257 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
258 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
259 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
260 strlen(in->tlsext_hostname))) {
David Benjamin3570d732015-06-29 00:28:17 -0400261 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400262 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400263 }
264 }
Adam Langley95c29f32014-06-20 12:00:00 -0700265
David Benjamin89abaea2014-10-19 13:50:18 -0400266 if (in->psk_identity) {
267 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
268 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
269 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
270 strlen(in->psk_identity))) {
David Benjamin3570d732015-06-29 00:28:17 -0400271 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400272 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400273 }
274 }
Adam Langley95c29f32014-06-20 12:00:00 -0700275
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700276 if (in->tlsext_tick_lifetime_hint > 0) {
David Benjamin89abaea2014-10-19 13:50:18 -0400277 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700278 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400279 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400280 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400281 }
282 }
Adam Langley95c29f32014-06-20 12:00:00 -0700283
David Benjamin95d31822015-06-15 19:53:32 -0400284 if (in->tlsext_tick && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400285 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
286 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
287 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -0400288 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400289 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400290 }
291 }
Adam Langley75872532014-06-20 12:00:00 -0700292
David Benjamin89abaea2014-10-19 13:50:18 -0400293 if (in->peer_sha256_valid) {
294 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
295 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
296 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
David Benjamin3570d732015-06-29 00:28:17 -0400297 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400298 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400299 }
300 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700301
David Benjamin89abaea2014-10-19 13:50:18 -0400302 if (in->original_handshake_hash_len > 0) {
303 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
304 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
305 !CBB_add_bytes(&child2, in->original_handshake_hash,
306 in->original_handshake_hash_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400307 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400308 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400309 }
310 }
Adam Langley95c29f32014-06-20 12:00:00 -0700311
David Benjamin89abaea2014-10-19 13:50:18 -0400312 if (in->tlsext_signed_cert_timestamp_list_length > 0) {
313 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
314 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
315 !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list,
316 in->tlsext_signed_cert_timestamp_list_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400317 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400318 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400319 }
320 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200321
David Benjamin89abaea2014-10-19 13:50:18 -0400322 if (in->ocsp_response_length > 0) {
323 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
324 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
325 !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400326 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400327 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400328 }
329 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400330
Adam Langley75712922014-10-10 16:23:43 -0700331 if (in->extended_master_secret) {
332 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
333 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
334 !CBB_add_u8(&child2, 0xff)) {
David Benjamin3570d732015-06-29 00:28:17 -0400335 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400336 return 0;
Adam Langley75712922014-10-10 16:23:43 -0700337 }
338 }
339
David Benjamin4882a6c2016-12-11 02:48:12 -0500340 if (in->group_id > 0 &&
341 (!CBB_add_asn1(&session, &child, kGroupIDTag) ||
342 !CBB_add_asn1_uint64(&child, in->group_id))) {
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200343 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400344 return 0;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200345 }
346
David Benjamin26416e92015-08-22 16:04:17 -0400347 /* The certificate chain is only serialized if the leaf's SHA-256 isn't
348 * serialized instead. */
Adam Langley68e71242016-12-12 11:06:16 -0800349 if (in->certs != NULL &&
350 !in->peer_sha256_valid &&
351 sk_CRYPTO_BUFFER_num(in->certs) >= 2) {
David Benjamin26416e92015-08-22 16:04:17 -0400352 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
353 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400354 return 0;
David Benjamin26416e92015-08-22 16:04:17 -0400355 }
Adam Langley68e71242016-12-12 11:06:16 -0800356 for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs); i++) {
357 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, i);
358 if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
359 CRYPTO_BUFFER_len(buffer))) {
360 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400361 return 0;
David Benjamin26416e92015-08-22 16:04:17 -0400362 }
363 }
364 }
365
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400366 if (in->ticket_age_add_valid) {
367 if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
368 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
369 !CBB_add_u32(&child2, in->ticket_age_add)) {
370 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400371 return 0;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400372 }
373 }
374
Adam Langley364f7a62016-12-12 10:51:00 -0800375 if (!in->is_server) {
376 if (!CBB_add_asn1(&session, &child, kIsServerTag) ||
377 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
378 !CBB_add_u8(&child2, 0x00)) {
379 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400380 return 0;
Adam Langley364f7a62016-12-12 10:51:00 -0800381 }
382 }
383
David Benjaminf1050fd2016-12-13 20:05:36 -0500384 if (in->peer_signature_algorithm != 0 &&
385 (!CBB_add_asn1(&session, &child, kPeerSignatureAlgorithmTag) ||
386 !CBB_add_asn1_uint64(&child, in->peer_signature_algorithm))) {
387 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400388 return 0;
David Benjaminf1050fd2016-12-13 20:05:36 -0500389 }
390
Steven Valdez08b65f42016-12-07 15:29:45 -0500391 if (in->ticket_max_early_data != 0 &&
392 (!CBB_add_asn1(&session, &child, kTicketMaxEarlyDataTag) ||
393 !CBB_add_asn1_uint64(&child, in->ticket_max_early_data))) {
394 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400395 return 0;
Steven Valdez08b65f42016-12-07 15:29:45 -0500396 }
397
David Benjamin17b30832017-01-28 14:00:32 -0500398 if (in->timeout != in->auth_timeout &&
399 (!CBB_add_asn1(&session, &child, kAuthTimeoutTag) ||
400 !CBB_add_asn1_uint64(&child, in->auth_timeout))) {
401 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400402 return 0;
David Benjamin17b30832017-01-28 14:00:32 -0500403 }
404
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500405 if (in->early_alpn) {
406 if (!CBB_add_asn1(&session, &child, kEarlyALPNTag) ||
407 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
408 !CBB_add_bytes(&child2, (const uint8_t *)in->early_alpn,
409 in->early_alpn_len)) {
410 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400411 return 0;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500412 }
413 }
414
David Benjamin1386aad2017-07-19 23:57:40 -0400415 if (!CBB_finish(cbb.get(), out_data, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400416 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400417 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400418 }
David Benjamin3cac4502014-10-21 01:46:30 -0400419 return 1;
David Benjamin3cac4502014-10-21 01:46:30 -0400420}
421
David Benjaminfd67aa82015-06-15 19:41:48 -0400422/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400423 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
424 * entry, if |*out| is not NULL, it frees the existing contents. If
425 * the element was not found, it sets |*out| to NULL. It returns one
426 * on success, whether or not the element was found, and zero on
427 * decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400428static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400429 CBS value;
430 int present;
431 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400432 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400433 return 0;
434 }
435 if (present) {
436 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400437 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400438 return 0;
439 }
440 if (!CBS_strdup(&value, out)) {
David Benjamin3570d732015-06-29 00:28:17 -0400441 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400442 return 0;
443 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400444 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400445 OPENSSL_free(*out);
446 *out = NULL;
447 }
448 return 1;
449}
Adam Langley95c29f32014-06-20 12:00:00 -0700450
David Benjaminfd67aa82015-06-15 19:41:48 -0400451/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400452 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
453 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
454 * contents. On entry, if the element was not found, it sets
455 * |*out_ptr| to NULL. It returns one on success, whether or not the
456 * element was found, and zero on decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400457static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin8be79a32015-08-23 01:37:36 -0400458 size_t *out_len, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400459 CBS value;
460 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400461 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400462 return 0;
463 }
464 if (!CBS_stow(&value, out_ptr, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400465 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400466 return 0;
467 }
468 return 1;
469}
Adam Langley95c29f32014-06-20 12:00:00 -0700470
David Benjamin8be79a32015-08-23 01:37:36 -0400471/* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
472 * explicitly tagged with |tag| of size at most |max_out|. */
473static int SSL_SESSION_parse_bounded_octet_string(
David Benjamin30c4c302016-12-07 22:35:24 -0500474 CBS *cbs, uint8_t *out, uint8_t *out_len, uint8_t max_out, unsigned tag) {
David Benjamin8be79a32015-08-23 01:37:36 -0400475 CBS value;
476 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
477 CBS_len(&value) > max_out) {
478 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
479 return 0;
480 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500481 OPENSSL_memcpy(out, CBS_data(&value), CBS_len(&value));
David Benjamin30c4c302016-12-07 22:35:24 -0500482 *out_len = (uint8_t)CBS_len(&value);
David Benjamin8be79a32015-08-23 01:37:36 -0400483 return 1;
484}
Adam Langley95c29f32014-06-20 12:00:00 -0700485
David Benjamin8be79a32015-08-23 01:37:36 -0400486static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
487 long default_value) {
488 uint64_t value;
489 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
490 (uint64_t)default_value) ||
491 value > LONG_MAX) {
492 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
493 return 0;
494 }
495 *out = (long)value;
496 return 1;
497}
498
499static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
500 uint32_t default_value) {
501 uint64_t value;
502 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
503 (uint64_t)default_value) ||
504 value > 0xffffffff) {
505 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
506 return 0;
507 }
508 *out = (uint32_t)value;
509 return 1;
510}
511
David Benjaminf1050fd2016-12-13 20:05:36 -0500512static int SSL_SESSION_parse_u16(CBS *cbs, uint16_t *out, unsigned tag,
513 uint16_t default_value) {
514 uint64_t value;
515 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
516 (uint64_t)default_value) ||
517 value > 0xffff) {
518 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
519 return 0;
520 }
521 *out = (uint16_t)value;
522 return 1;
523}
524
David Benjamin31b0c9b2017-07-20 14:49:15 -0400525UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs,
526 const SSL_X509_METHOD *x509_method,
527 CRYPTO_BUFFER_POOL *pool) {
528 UniquePtr<SSL_SESSION> ret = ssl_session_new(x509_method);
529 if (!ret) {
530 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400531 }
Adam Langley95c29f32014-06-20 12:00:00 -0700532
David Benjamin8be79a32015-08-23 01:37:36 -0400533 CBS session;
534 uint64_t version, ssl_version;
Steven Valdez8f36c512017-06-20 10:55:02 -0400535 uint16_t unused;
David Benjaminfd67aa82015-06-15 19:41:48 -0400536 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400537 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400538 version != kVersion ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400539 !CBS_get_asn1_uint64(&session, &ssl_version) ||
540 /* Require sessions have versions valid in either TLS or DTLS. The session
541 * will not be used by the handshake if not applicable, but, for
542 * simplicity, never parse a session that does not pass
543 * |ssl_protocol_version_from_wire|. */
544 ssl_version > UINT16_MAX ||
545 !ssl_protocol_version_from_wire(&unused, ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400546 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400547 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400548 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400549 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700550
David Benjamin8be79a32015-08-23 01:37:36 -0400551 CBS cipher;
David Benjaminf3a8b122014-12-25 23:11:49 -0500552 uint16_t cipher_value;
David Benjamin8be79a32015-08-23 01:37:36 -0400553 if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
554 !CBS_get_u16(&cipher, &cipher_value) ||
555 CBS_len(&cipher) != 0) {
556 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400557 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400558 }
David Benjamina1c90a52015-05-30 17:03:14 -0400559 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400560 if (ret->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400561 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400562 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400563 }
Adam Langley95c29f32014-06-20 12:00:00 -0700564
David Benjamin8be79a32015-08-23 01:37:36 -0400565 CBS session_id, master_key;
566 if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
567 CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
568 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
569 CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400570 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400571 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400572 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500573 OPENSSL_memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
David Benjamin83fd6b62014-10-19 04:33:38 -0400574 ret->session_id_length = CBS_len(&session_id);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500575 OPENSSL_memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
David Benjamin83fd6b62014-10-19 04:33:38 -0400576 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700577
David Benjamin5b7b09c2016-11-03 20:32:10 -0400578 CBS child;
David Benjaminad8f5e12017-02-20 17:00:20 -0500579 uint64_t timeout;
David Benjamin5b7b09c2016-11-03 20:32:10 -0400580 if (!CBS_get_asn1(&session, &child, kTimeTag) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500581 !CBS_get_asn1_uint64(&child, &ret->time) ||
David Benjamin5b7b09c2016-11-03 20:32:10 -0400582 !CBS_get_asn1(&session, &child, kTimeoutTag) ||
583 !CBS_get_asn1_uint64(&child, &timeout) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500584 timeout > UINT32_MAX) {
David Benjamin3570d732015-06-29 00:28:17 -0400585 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400586 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400587 }
Adam Langley95c29f32014-06-20 12:00:00 -0700588
David Benjaminad8f5e12017-02-20 17:00:20 -0500589 ret->timeout = (uint32_t)timeout;
David Benjamin5b7b09c2016-11-03 20:32:10 -0400590
David Benjamin8be79a32015-08-23 01:37:36 -0400591 CBS peer;
592 int has_peer;
Adam Langley68e71242016-12-12 11:06:16 -0800593 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
594 (has_peer && CBS_len(&peer) == 0)) {
David Benjamin8be79a32015-08-23 01:37:36 -0400595 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400596 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400597 }
Adam Langley68e71242016-12-12 11:06:16 -0800598 /* |peer| is processed with the certificate chain. */
Adam Langley95c29f32014-06-20 12:00:00 -0700599
David Benjamin8be79a32015-08-23 01:37:36 -0400600 if (!SSL_SESSION_parse_bounded_octet_string(
601 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
602 kSessionIDContextTag) ||
603 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
604 X509_V_OK) ||
605 !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
606 kHostNameTag) ||
607 !SSL_SESSION_parse_string(&session, &ret->psk_identity,
608 kPSKIdentityTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700609 !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
David Benjamin8be79a32015-08-23 01:37:36 -0400610 kTicketLifetimeHintTag, 0) ||
611 !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
612 &ret->tlsext_ticklen, kTicketTag)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400613 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400614 }
Adam Langley95c29f32014-06-20 12:00:00 -0700615
David Benjamin8be79a32015-08-23 01:37:36 -0400616 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
David Benjamin5b7b09c2016-11-03 20:32:10 -0400617 CBS peer_sha256;
David Benjamin8be79a32015-08-23 01:37:36 -0400618 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
619 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
620 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
621 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400622 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400623 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400624 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500625 OPENSSL_memcpy(ret->peer_sha256, CBS_data(&peer_sha256),
626 sizeof(ret->peer_sha256));
David Benjamin83fd6b62014-10-19 04:33:38 -0400627 ret->peer_sha256_valid = 1;
628 } else {
629 ret->peer_sha256_valid = 0;
630 }
Adam Langley95c29f32014-06-20 12:00:00 -0700631
David Benjamin8be79a32015-08-23 01:37:36 -0400632 if (!SSL_SESSION_parse_bounded_octet_string(
633 &session, ret->original_handshake_hash,
634 &ret->original_handshake_hash_len,
635 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
636 !SSL_SESSION_parse_octet_string(
637 &session, &ret->tlsext_signed_cert_timestamp_list,
638 &ret->tlsext_signed_cert_timestamp_list_length,
639 kSignedCertTimestampListTag) ||
640 !SSL_SESSION_parse_octet_string(
641 &session, &ret->ocsp_response, &ret->ocsp_response_length,
642 kOCSPResponseTag)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400643 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400644 }
645
646 int extended_master_secret;
647 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
648 kExtendedMasterSecretTag,
649 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400650 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400651 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400652 }
David Benjamin8be79a32015-08-23 01:37:36 -0400653 ret->extended_master_secret = !!extended_master_secret;
654
David Benjamin11fa7032017-04-11 22:13:45 -0400655 if (!SSL_SESSION_parse_u16(&session, &ret->group_id, kGroupIDTag, 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400656 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400657 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400658 }
659
660 CBS cert_chain;
Adam Langley68e71242016-12-12 11:06:16 -0800661 CBS_init(&cert_chain, NULL, 0);
David Benjamin26416e92015-08-22 16:04:17 -0400662 int has_cert_chain;
663 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
Adam Langley68e71242016-12-12 11:06:16 -0800664 kCertChainTag) ||
665 (has_cert_chain && CBS_len(&cert_chain) == 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400666 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400667 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400668 }
Adam Langley68e71242016-12-12 11:06:16 -0800669 if (has_cert_chain && !has_peer) {
670 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400671 return nullptr;
Adam Langley364f7a62016-12-12 10:51:00 -0800672 }
Adam Langley68e71242016-12-12 11:06:16 -0800673 if (has_peer || has_cert_chain) {
674 ret->certs = sk_CRYPTO_BUFFER_new_null();
675 if (ret->certs == NULL) {
David Benjamin26416e92015-08-22 16:04:17 -0400676 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400677 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400678 }
Adam Langley68e71242016-12-12 11:06:16 -0800679
680 if (has_peer) {
Adam Langley46db7af2017-02-01 15:49:37 -0800681 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&peer, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800682 if (buffer == NULL ||
683 !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
684 CRYPTO_BUFFER_free(buffer);
Adam Langleye8509092016-11-07 14:24:33 -0800685 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400686 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400687 }
688 }
Adam Langley68e71242016-12-12 11:06:16 -0800689
690 while (CBS_len(&cert_chain) > 0) {
691 CBS cert;
692 if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) ||
693 CBS_len(&cert) == 0) {
694 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400695 return nullptr;
Adam Langley68e71242016-12-12 11:06:16 -0800696 }
697
Adam Langley46db7af2017-02-01 15:49:37 -0800698 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800699 if (buffer == NULL ||
700 !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
701 CRYPTO_BUFFER_free(buffer);
702 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400703 return nullptr;
Adam Langley68e71242016-12-12 11:06:16 -0800704 }
705 }
706 }
707
David Benjamin31b0c9b2017-07-20 14:49:15 -0400708 if (!x509_method->session_cache_objects(ret.get())) {
Adam Langley68e71242016-12-12 11:06:16 -0800709 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400710 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400711 }
712
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400713 CBS age_add;
714 int age_add_present;
Steven Valdez5b986082016-09-01 12:29:49 -0400715 if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400716 kTicketAgeAddTag) ||
717 (age_add_present &&
718 !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
719 CBS_len(&age_add) != 0) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400720 return nullptr;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400721 }
722 ret->ticket_age_add_valid = age_add_present;
723
Adam Langley364f7a62016-12-12 10:51:00 -0800724 int is_server;
725 if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag,
726 1 /* default to true */)) {
727 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400728 return nullptr;
Adam Langley364f7a62016-12-12 10:51:00 -0800729 }
730 /* TODO: in time we can include |is_server| for servers too, then we can
731 enforce that client and server sessions are never mixed up. */
732
733 ret->is_server = is_server;
734
David Benjaminf1050fd2016-12-13 20:05:36 -0500735 if (!SSL_SESSION_parse_u16(&session, &ret->peer_signature_algorithm,
736 kPeerSignatureAlgorithmTag, 0) ||
Steven Valdez08b65f42016-12-07 15:29:45 -0500737 !SSL_SESSION_parse_u32(&session, &ret->ticket_max_early_data,
738 kTicketMaxEarlyDataTag, 0) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500739 !SSL_SESSION_parse_u32(&session, &ret->auth_timeout, kAuthTimeoutTag,
740 ret->timeout) ||
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500741 !SSL_SESSION_parse_octet_string(&session, &ret->early_alpn,
742 &ret->early_alpn_len, kEarlyALPNTag) ||
David Benjaminf1050fd2016-12-13 20:05:36 -0500743 CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400744 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400745 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400746 }
Adam Langley95c29f32014-06-20 12:00:00 -0700747
David Benjaminfd67aa82015-06-15 19:41:48 -0400748 return ret;
David Benjaminfd67aa82015-06-15 19:41:48 -0400749}
750
David Benjamin86e95b82017-07-18 16:34:25 -0400751} // namespace bssl
752
753using namespace bssl;
754
755int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
756 size_t *out_len) {
757 if (in->not_resumable) {
758 /* If the caller has an unresumable session, e.g. if |SSL_get_session| were
759 * called on a TLS 1.3 or False Started connection, serialize with a
760 * placeholder value so it is not accidentally deserialized into a resumable
761 * one. */
762 static const char kNotResumableSession[] = "NOT RESUMABLE";
763
764 *out_len = strlen(kNotResumableSession);
765 *out_data = (uint8_t *)BUF_memdup(kNotResumableSession, *out_len);
766 if (*out_data == NULL) {
767 return 0;
768 }
769
770 return 1;
771 }
772
773 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
774}
775
776int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
777 size_t *out_len) {
778 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
779}
780
781int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
782 uint8_t *out;
783 size_t len;
784
785 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
786 return -1;
787 }
788
789 if (len > INT_MAX) {
790 OPENSSL_free(out);
791 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
792 return -1;
793 }
794
795 if (pp) {
796 OPENSSL_memcpy(*pp, out, len);
797 *pp += len;
798 }
799 OPENSSL_free(out);
800
801 return len;
802}
803
Adam Langley46db7af2017-02-01 15:49:37 -0800804SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len,
805 const SSL_CTX *ctx) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400806 CBS cbs;
807 CBS_init(&cbs, in, in_len);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400808 UniquePtr<SSL_SESSION> ret =
809 SSL_SESSION_parse(&cbs, ctx->x509_method, ctx->pool);
810 if (!ret) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400811 return NULL;
812 }
813 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400814 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400815 return NULL;
816 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400817 return ret.release();
David Benjaminfd67aa82015-06-15 19:41:48 -0400818}