blob: b87c795447067873fb072f28bda52fe62c558b22 [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
David Benjaminee910bf2017-07-25 22:36:00 -040095#include <utility>
96
Steven Valdez84b5c002016-08-25 16:30:58 -040097#include <openssl/buf.h>
David Benjamin83fd6b62014-10-19 04:33:38 -040098#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -070099#include <openssl/err.h>
Brian Smith054e6822015-03-27 21:12:01 -1000100#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700101#include <openssl/x509.h>
102
David Benjamin17cf2cb2016-12-13 01:07:13 -0500103#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400104#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700105
Adam Langley95c29f32014-06-20 12:00:00 -0700106
David Benjamin86e95b82017-07-18 16:34:25 -0400107namespace bssl {
108
David Benjamin83fd6b62014-10-19 04:33:38 -0400109/* An SSL_SESSION is serialized as the following ASN.1 structure:
110 *
111 * SSLSession ::= SEQUENCE {
David Benjamin7bb88bb2016-11-01 13:59:01 -0400112 * version INTEGER (1), -- session structure version
David Benjamin83fd6b62014-10-19 04:33:38 -0400113 * sslVersion INTEGER, -- protocol version number
114 * cipher OCTET STRING, -- two bytes long
115 * sessionID OCTET STRING,
116 * masterKey OCTET STRING,
David Benjamin5b7b09c2016-11-03 20:32:10 -0400117 * time [1] INTEGER, -- seconds since UNIX epoch
118 * timeout [2] INTEGER, -- in seconds
David Benjamin83fd6b62014-10-19 04:33:38 -0400119 * peer [3] Certificate OPTIONAL,
120 * sessionIDContext [4] OCTET STRING OPTIONAL,
121 * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes
122 * hostName [6] OCTET STRING OPTIONAL,
123 * -- from server_name extension
David Benjamin83fd6b62014-10-19 04:33:38 -0400124 * pskIdentity [8] OCTET STRING OPTIONAL,
125 * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only
126 * ticket [10] OCTET STRING OPTIONAL, -- client-only
127 * peerSHA256 [13] OCTET STRING OPTIONAL,
128 * originalHandshakeHash [14] OCTET STRING OPTIONAL,
129 * signedCertTimestampList [15] OCTET STRING OPTIONAL,
130 * -- contents of SCT extension
131 * ocspResponse [16] OCTET STRING OPTIONAL,
David Benjamin26416e92015-08-22 16:04:17 -0400132 * -- stapled OCSP response from the server
Adam Langley75712922014-10-10 16:23:43 -0700133 * extendedMasterSecret [17] BOOLEAN OPTIONAL,
David Benjamin4882a6c2016-12-11 02:48:12 -0500134 * groupID [18] INTEGER OPTIONAL,
David Benjamin26416e92015-08-22 16:04:17 -0400135 * certChain [19] SEQUENCE OF Certificate OPTIONAL,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400136 * ticketAgeAdd [21] OCTET STRING OPTIONAL,
Adam Langley364f7a62016-12-12 10:51:00 -0800137 * isServer [22] BOOLEAN DEFAULT TRUE,
David Benjaminf1050fd2016-12-13 20:05:36 -0500138 * peerSignatureAlgorithm [23] INTEGER OPTIONAL,
Steven Valdez08b65f42016-12-07 15:29:45 -0500139 * ticketMaxEarlyData [24] INTEGER OPTIONAL,
David Benjamin17b30832017-01-28 14:00:32 -0500140 * authTimeout [25] INTEGER OPTIONAL, -- defaults to timeout
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500141 * earlyALPN [26] OCTET STRING OPTIONAL,
David Benjamin83fd6b62014-10-19 04:33:38 -0400142 * }
143 *
David Benjamin688d8df2014-11-02 23:06:42 -0500144 * Note: historically this serialization has included other optional
David Benjamin17b30832017-01-28 14:00:32 -0500145 * fields. Their presence is currently treated as a parse error:
David Benjamin688d8df2014-11-02 23:06:42 -0500146 *
147 * keyArg [0] IMPLICIT OCTET STRING OPTIONAL,
148 * pskIdentityHint [7] OCTET STRING OPTIONAL,
149 * compressionMethod [11] OCTET STRING OPTIONAL,
Steven Valdez5b986082016-09-01 12:29:49 -0400150 * srpUsername [12] OCTET STRING OPTIONAL,
151 * ticketFlags [20] INTEGER OPTIONAL,
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800152 */
David Benjamin83fd6b62014-10-19 04:33:38 -0400153
Adam Langley96c2a282015-06-02 14:16:44 -0700154static const unsigned kVersion = 1;
David Benjamin7f393f72015-10-17 12:36:12 -0400155
David Benjamin83fd6b62014-10-19 04:33:38 -0400156static const int kTimeTag =
157 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
158static const int kTimeoutTag =
159 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2;
160static const int kPeerTag =
161 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3;
David Benjamin7f393f72015-10-17 12:36:12 -0400162static const int kSessionIDContextTag =
David Benjamin83fd6b62014-10-19 04:33:38 -0400163 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4;
164static const int kVerifyResultTag =
165 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5;
166static const int kHostNameTag =
167 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6;
David Benjamin83fd6b62014-10-19 04:33:38 -0400168static const int kPSKIdentityTag =
169 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8;
170static const int kTicketLifetimeHintTag =
171 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9;
172static const int kTicketTag =
173 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10;
174static const int kPeerSHA256Tag =
175 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13;
176static const int kOriginalHandshakeHashTag =
177 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14;
178static const int kSignedCertTimestampListTag =
179 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15;
180static const int kOCSPResponseTag =
181 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16;
Adam Langley75712922014-10-10 16:23:43 -0700182static const int kExtendedMasterSecretTag =
183 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17;
David Benjamin4882a6c2016-12-11 02:48:12 -0500184static const int kGroupIDTag =
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200185 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18;
David Benjamin26416e92015-08-22 16:04:17 -0400186static const int kCertChainTag =
187 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400188static const int kTicketAgeAddTag =
189 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21;
Adam Langley364f7a62016-12-12 10:51:00 -0800190static const int kIsServerTag =
191 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22;
David Benjaminf1050fd2016-12-13 20:05:36 -0500192static const int kPeerSignatureAlgorithmTag =
193 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 23;
Steven Valdez08b65f42016-12-07 15:29:45 -0500194static const int kTicketMaxEarlyDataTag =
195 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 24;
David Benjamin17b30832017-01-28 14:00:32 -0500196static const int kAuthTimeoutTag =
197 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 25;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500198static const int kEarlyALPNTag =
199 CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 26;
David Benjamin26416e92015-08-22 16:04:17 -0400200
David Benjamin14e2b502015-09-13 14:48:12 -0400201static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data,
David Benjamin3cac4502014-10-21 01:46:30 -0400202 size_t *out_len, int for_ticket) {
David Benjaminf3a8b122014-12-25 23:11:49 -0500203 if (in == NULL || in->cipher == NULL) {
David Benjamin89abaea2014-10-19 13:50:18 -0400204 return 0;
205 }
Adam Langley95c29f32014-06-20 12:00:00 -0700206
David Benjamin1386aad2017-07-19 23:57:40 -0400207 ScopedCBB cbb;
208 CBB session, child, child2;
209 if (!CBB_init(cbb.get(), 0) ||
210 !CBB_add_asn1(cbb.get(), &session, CBS_ASN1_SEQUENCE) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400211 !CBB_add_asn1_uint64(&session, kVersion) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400212 !CBB_add_asn1_uint64(&session, in->ssl_version) ||
213 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminf3a8b122014-12-25 23:11:49 -0500214 !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400215 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjamin3cac4502014-10-21 01:46:30 -0400216 /* The session ID is irrelevant for a session ticket. */
217 !CBB_add_bytes(&child, in->session_id,
218 for_ticket ? 0 : in->session_id_length) ||
David Benjamin89abaea2014-10-19 13:50:18 -0400219 !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500220 !CBB_add_bytes(&child, in->master_key, in->master_key_length) ||
221 !CBB_add_asn1(&session, &child, kTimeTag) ||
222 !CBB_add_asn1_uint64(&child, in->time) ||
223 !CBB_add_asn1(&session, &child, kTimeoutTag) ||
David Benjamin5b7b09c2016-11-03 20:32:10 -0400224 !CBB_add_asn1_uint64(&child, in->timeout)) {
225 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400226 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400227 }
Adam Langley95c29f32014-06-20 12:00:00 -0700228
Adam Langleyc0fc7a12016-12-09 15:05:34 -0800229 /* The peer certificate is only serialized if the SHA-256 isn't
230 * serialized instead. */
Adam Langley68e71242016-12-12 11:06:16 -0800231 if (sk_CRYPTO_BUFFER_num(in->certs) > 0 && !in->peer_sha256_valid) {
232 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, 0);
233 if (!CBB_add_asn1(&session, &child, kPeerTag) ||
234 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
235 CRYPTO_BUFFER_len(buffer))) {
David Benjamin3570d732015-06-29 00:28:17 -0400236 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400237 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400238 }
David Benjamin89abaea2014-10-19 13:50:18 -0400239 }
Adam Langley95c29f32014-06-20 12:00:00 -0700240
David Benjamin89abaea2014-10-19 13:50:18 -0400241 /* Although it is OPTIONAL and usually empty, OpenSSL has
242 * historically always encoded the sid_ctx. */
243 if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) ||
244 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
245 !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) {
David Benjamin3570d732015-06-29 00:28:17 -0400246 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400247 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400248 }
Adam Langley95c29f32014-06-20 12:00:00 -0700249
David Benjamin89abaea2014-10-19 13:50:18 -0400250 if (in->verify_result != X509_V_OK) {
251 if (!CBB_add_asn1(&session, &child, kVerifyResultTag) ||
252 !CBB_add_asn1_uint64(&child, in->verify_result)) {
David Benjamin3570d732015-06-29 00:28:17 -0400253 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400254 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400255 }
256 }
Adam Langley95c29f32014-06-20 12:00:00 -0700257
David Benjamin89abaea2014-10-19 13:50:18 -0400258 if (in->tlsext_hostname) {
259 if (!CBB_add_asn1(&session, &child, kHostNameTag) ||
260 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
261 !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname,
262 strlen(in->tlsext_hostname))) {
David Benjamin3570d732015-06-29 00:28:17 -0400263 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400264 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400265 }
266 }
Adam Langley95c29f32014-06-20 12:00:00 -0700267
David Benjamin89abaea2014-10-19 13:50:18 -0400268 if (in->psk_identity) {
269 if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) ||
270 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
271 !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity,
272 strlen(in->psk_identity))) {
David Benjamin3570d732015-06-29 00:28:17 -0400273 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400274 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400275 }
276 }
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700278 if (in->tlsext_tick_lifetime_hint > 0) {
David Benjamin89abaea2014-10-19 13:50:18 -0400279 if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700280 !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) {
David Benjamin3570d732015-06-29 00:28:17 -0400281 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400282 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400283 }
284 }
Adam Langley95c29f32014-06-20 12:00:00 -0700285
David Benjamin95d31822015-06-15 19:53:32 -0400286 if (in->tlsext_tick && !for_ticket) {
David Benjamin89abaea2014-10-19 13:50:18 -0400287 if (!CBB_add_asn1(&session, &child, kTicketTag) ||
288 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
289 !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) {
David Benjamin3570d732015-06-29 00:28:17 -0400290 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400291 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400292 }
293 }
Adam Langley75872532014-06-20 12:00:00 -0700294
David Benjamin89abaea2014-10-19 13:50:18 -0400295 if (in->peer_sha256_valid) {
296 if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) ||
297 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
298 !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) {
David Benjamin3570d732015-06-29 00:28:17 -0400299 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400300 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400301 }
302 }
Adam Langley1258b6a2014-06-20 12:00:00 -0700303
David Benjamin89abaea2014-10-19 13:50:18 -0400304 if (in->original_handshake_hash_len > 0) {
305 if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) ||
306 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
307 !CBB_add_bytes(&child2, in->original_handshake_hash,
308 in->original_handshake_hash_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400309 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400310 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400311 }
312 }
Adam Langley95c29f32014-06-20 12:00:00 -0700313
David Benjamin8fc2dc02017-08-22 15:07:51 -0700314 if (in->signed_cert_timestamp_list != nullptr) {
David Benjamin89abaea2014-10-19 13:50:18 -0400315 if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) ||
316 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
David Benjamin8fc2dc02017-08-22 15:07:51 -0700317 !CBB_add_bytes(&child2,
318 CRYPTO_BUFFER_data(in->signed_cert_timestamp_list),
319 CRYPTO_BUFFER_len(in->signed_cert_timestamp_list))) {
David Benjamin3570d732015-06-29 00:28:17 -0400320 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400321 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400322 }
323 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200324
David Benjamin8fc2dc02017-08-22 15:07:51 -0700325 if (in->ocsp_response != nullptr) {
David Benjamin89abaea2014-10-19 13:50:18 -0400326 if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) ||
327 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
David Benjamin8fc2dc02017-08-22 15:07:51 -0700328 !CBB_add_bytes(&child2, CRYPTO_BUFFER_data(in->ocsp_response),
329 CRYPTO_BUFFER_len(in->ocsp_response))) {
David Benjamin3570d732015-06-29 00:28:17 -0400330 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400331 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400332 }
333 }
David Benjamin6c7aed02014-08-27 16:42:38 -0400334
Adam Langley75712922014-10-10 16:23:43 -0700335 if (in->extended_master_secret) {
336 if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) ||
337 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
338 !CBB_add_u8(&child2, 0xff)) {
David Benjamin3570d732015-06-29 00:28:17 -0400339 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400340 return 0;
Adam Langley75712922014-10-10 16:23:43 -0700341 }
342 }
343
David Benjamin4882a6c2016-12-11 02:48:12 -0500344 if (in->group_id > 0 &&
345 (!CBB_add_asn1(&session, &child, kGroupIDTag) ||
346 !CBB_add_asn1_uint64(&child, in->group_id))) {
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200347 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400348 return 0;
Sigbjorn Vik2b23d242015-06-29 15:07:26 +0200349 }
350
David Benjamin26416e92015-08-22 16:04:17 -0400351 /* The certificate chain is only serialized if the leaf's SHA-256 isn't
352 * serialized instead. */
Adam Langley68e71242016-12-12 11:06:16 -0800353 if (in->certs != NULL &&
354 !in->peer_sha256_valid &&
355 sk_CRYPTO_BUFFER_num(in->certs) >= 2) {
David Benjamin26416e92015-08-22 16:04:17 -0400356 if (!CBB_add_asn1(&session, &child, kCertChainTag)) {
357 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400358 return 0;
David Benjamin26416e92015-08-22 16:04:17 -0400359 }
Adam Langley68e71242016-12-12 11:06:16 -0800360 for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs); i++) {
361 const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, i);
362 if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer),
363 CRYPTO_BUFFER_len(buffer))) {
364 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400365 return 0;
David Benjamin26416e92015-08-22 16:04:17 -0400366 }
367 }
368 }
369
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400370 if (in->ticket_age_add_valid) {
371 if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) ||
372 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
373 !CBB_add_u32(&child2, in->ticket_age_add)) {
374 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400375 return 0;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400376 }
377 }
378
Adam Langley364f7a62016-12-12 10:51:00 -0800379 if (!in->is_server) {
380 if (!CBB_add_asn1(&session, &child, kIsServerTag) ||
381 !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
382 !CBB_add_u8(&child2, 0x00)) {
383 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400384 return 0;
Adam Langley364f7a62016-12-12 10:51:00 -0800385 }
386 }
387
David Benjaminf1050fd2016-12-13 20:05:36 -0500388 if (in->peer_signature_algorithm != 0 &&
389 (!CBB_add_asn1(&session, &child, kPeerSignatureAlgorithmTag) ||
390 !CBB_add_asn1_uint64(&child, in->peer_signature_algorithm))) {
391 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400392 return 0;
David Benjaminf1050fd2016-12-13 20:05:36 -0500393 }
394
Steven Valdez08b65f42016-12-07 15:29:45 -0500395 if (in->ticket_max_early_data != 0 &&
396 (!CBB_add_asn1(&session, &child, kTicketMaxEarlyDataTag) ||
397 !CBB_add_asn1_uint64(&child, in->ticket_max_early_data))) {
398 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400399 return 0;
Steven Valdez08b65f42016-12-07 15:29:45 -0500400 }
401
David Benjamin17b30832017-01-28 14:00:32 -0500402 if (in->timeout != in->auth_timeout &&
403 (!CBB_add_asn1(&session, &child, kAuthTimeoutTag) ||
404 !CBB_add_asn1_uint64(&child, in->auth_timeout))) {
405 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400406 return 0;
David Benjamin17b30832017-01-28 14:00:32 -0500407 }
408
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500409 if (in->early_alpn) {
410 if (!CBB_add_asn1(&session, &child, kEarlyALPNTag) ||
411 !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) ||
412 !CBB_add_bytes(&child2, (const uint8_t *)in->early_alpn,
413 in->early_alpn_len)) {
414 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400415 return 0;
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500416 }
417 }
418
David Benjamin1386aad2017-07-19 23:57:40 -0400419 if (!CBB_finish(cbb.get(), out_data, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400420 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin1386aad2017-07-19 23:57:40 -0400421 return 0;
David Benjamin89abaea2014-10-19 13:50:18 -0400422 }
David Benjamin3cac4502014-10-21 01:46:30 -0400423 return 1;
David Benjamin3cac4502014-10-21 01:46:30 -0400424}
425
David Benjaminfd67aa82015-06-15 19:41:48 -0400426/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400427 * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On
428 * entry, if |*out| is not NULL, it frees the existing contents. If
429 * the element was not found, it sets |*out| to NULL. It returns one
430 * on success, whether or not the element was found, and zero on
431 * decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400432static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400433 CBS value;
434 int present;
435 if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400436 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400437 return 0;
438 }
439 if (present) {
440 if (CBS_contains_zero_byte(&value)) {
David Benjamin3570d732015-06-29 00:28:17 -0400441 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400442 return 0;
443 }
444 if (!CBS_strdup(&value, out)) {
David Benjamin3570d732015-06-29 00:28:17 -0400445 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400446 return 0;
447 }
David Benjamin2755a3e2015-04-22 16:17:58 -0400448 } else {
David Benjamin83fd6b62014-10-19 04:33:38 -0400449 OPENSSL_free(*out);
450 *out = NULL;
451 }
452 return 1;
453}
Adam Langley95c29f32014-06-20 12:00:00 -0700454
David Benjaminfd67aa82015-06-15 19:41:48 -0400455/* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING
David Benjamin83fd6b62014-10-19 04:33:38 -0400456 * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr|
457 * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing
458 * contents. On entry, if the element was not found, it sets
459 * |*out_ptr| to NULL. It returns one on success, whether or not the
460 * element was found, and zero on decode error. */
David Benjaminfd67aa82015-06-15 19:41:48 -0400461static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr,
David Benjamin8be79a32015-08-23 01:37:36 -0400462 size_t *out_len, unsigned tag) {
David Benjamin83fd6b62014-10-19 04:33:38 -0400463 CBS value;
464 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) {
David Benjamin3570d732015-06-29 00:28:17 -0400465 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin83fd6b62014-10-19 04:33:38 -0400466 return 0;
467 }
468 if (!CBS_stow(&value, out_ptr, out_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400469 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin83fd6b62014-10-19 04:33:38 -0400470 return 0;
471 }
472 return 1;
473}
Adam Langley95c29f32014-06-20 12:00:00 -0700474
David Benjamin8fc2dc02017-08-22 15:07:51 -0700475static int SSL_SESSION_parse_crypto_buffer(CBS *cbs, CRYPTO_BUFFER **out,
476 unsigned tag,
477 CRYPTO_BUFFER_POOL *pool) {
478 if (!CBS_peek_asn1_tag(cbs, tag)) {
479 return 1;
480 }
481
482 CBS child, value;
483 if (!CBS_get_asn1(cbs, &child, tag) ||
484 !CBS_get_asn1(&child, &value, CBS_ASN1_OCTETSTRING) ||
485 CBS_len(&child) != 0) {
486 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
487 return 0;
488 }
489 CRYPTO_BUFFER_free(*out);
490 *out = CRYPTO_BUFFER_new_from_CBS(&value, pool);
491 if (*out == nullptr) {
492 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
493 return 0;
494 }
495 return 1;
496}
497
David Benjamin8be79a32015-08-23 01:37:36 -0400498/* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING
499 * explicitly tagged with |tag| of size at most |max_out|. */
500static int SSL_SESSION_parse_bounded_octet_string(
David Benjamin30c4c302016-12-07 22:35:24 -0500501 CBS *cbs, uint8_t *out, uint8_t *out_len, uint8_t max_out, unsigned tag) {
David Benjamin8be79a32015-08-23 01:37:36 -0400502 CBS value;
503 if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) ||
504 CBS_len(&value) > max_out) {
505 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
506 return 0;
507 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500508 OPENSSL_memcpy(out, CBS_data(&value), CBS_len(&value));
David Benjamin30c4c302016-12-07 22:35:24 -0500509 *out_len = (uint8_t)CBS_len(&value);
David Benjamin8be79a32015-08-23 01:37:36 -0400510 return 1;
511}
Adam Langley95c29f32014-06-20 12:00:00 -0700512
David Benjamin8be79a32015-08-23 01:37:36 -0400513static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag,
514 long default_value) {
515 uint64_t value;
516 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
517 (uint64_t)default_value) ||
518 value > LONG_MAX) {
519 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
520 return 0;
521 }
522 *out = (long)value;
523 return 1;
524}
525
526static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag,
527 uint32_t default_value) {
528 uint64_t value;
529 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
530 (uint64_t)default_value) ||
531 value > 0xffffffff) {
532 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
533 return 0;
534 }
535 *out = (uint32_t)value;
536 return 1;
537}
538
David Benjaminf1050fd2016-12-13 20:05:36 -0500539static int SSL_SESSION_parse_u16(CBS *cbs, uint16_t *out, unsigned tag,
540 uint16_t default_value) {
541 uint64_t value;
542 if (!CBS_get_optional_asn1_uint64(cbs, &value, tag,
543 (uint64_t)default_value) ||
544 value > 0xffff) {
545 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
546 return 0;
547 }
548 *out = (uint16_t)value;
549 return 1;
550}
551
David Benjamin31b0c9b2017-07-20 14:49:15 -0400552UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs,
553 const SSL_X509_METHOD *x509_method,
554 CRYPTO_BUFFER_POOL *pool) {
555 UniquePtr<SSL_SESSION> ret = ssl_session_new(x509_method);
556 if (!ret) {
557 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400558 }
Adam Langley95c29f32014-06-20 12:00:00 -0700559
David Benjamin8be79a32015-08-23 01:37:36 -0400560 CBS session;
561 uint64_t version, ssl_version;
Steven Valdez8f36c512017-06-20 10:55:02 -0400562 uint16_t unused;
David Benjaminfd67aa82015-06-15 19:41:48 -0400563 if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) ||
David Benjamin83fd6b62014-10-19 04:33:38 -0400564 !CBS_get_asn1_uint64(&session, &version) ||
David Benjamin7f393f72015-10-17 12:36:12 -0400565 version != kVersion ||
Steven Valdez8f36c512017-06-20 10:55:02 -0400566 !CBS_get_asn1_uint64(&session, &ssl_version) ||
567 /* Require sessions have versions valid in either TLS or DTLS. The session
568 * will not be used by the handshake if not applicable, but, for
569 * simplicity, never parse a session that does not pass
570 * |ssl_protocol_version_from_wire|. */
571 ssl_version > UINT16_MAX ||
572 !ssl_protocol_version_from_wire(&unused, ssl_version)) {
David Benjamin3570d732015-06-29 00:28:17 -0400573 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400574 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400575 }
David Benjamin83fd6b62014-10-19 04:33:38 -0400576 ret->ssl_version = ssl_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700577
David Benjamin8be79a32015-08-23 01:37:36 -0400578 CBS cipher;
David Benjaminf3a8b122014-12-25 23:11:49 -0500579 uint16_t cipher_value;
David Benjamin8be79a32015-08-23 01:37:36 -0400580 if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) ||
581 !CBS_get_u16(&cipher, &cipher_value) ||
582 CBS_len(&cipher) != 0) {
583 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400584 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400585 }
David Benjamina1c90a52015-05-30 17:03:14 -0400586 ret->cipher = SSL_get_cipher_by_value(cipher_value);
David Benjamin83fd6b62014-10-19 04:33:38 -0400587 if (ret->cipher == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400588 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400589 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400590 }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
David Benjamin8be79a32015-08-23 01:37:36 -0400592 CBS session_id, master_key;
593 if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) ||
594 CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH ||
595 !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) ||
596 CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400597 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400598 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400599 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500600 OPENSSL_memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id));
David Benjamin83fd6b62014-10-19 04:33:38 -0400601 ret->session_id_length = CBS_len(&session_id);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500602 OPENSSL_memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key));
David Benjamin83fd6b62014-10-19 04:33:38 -0400603 ret->master_key_length = CBS_len(&master_key);
Adam Langley95c29f32014-06-20 12:00:00 -0700604
David Benjamin5b7b09c2016-11-03 20:32:10 -0400605 CBS child;
David Benjaminad8f5e12017-02-20 17:00:20 -0500606 uint64_t timeout;
David Benjamin5b7b09c2016-11-03 20:32:10 -0400607 if (!CBS_get_asn1(&session, &child, kTimeTag) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500608 !CBS_get_asn1_uint64(&child, &ret->time) ||
David Benjamin5b7b09c2016-11-03 20:32:10 -0400609 !CBS_get_asn1(&session, &child, kTimeoutTag) ||
610 !CBS_get_asn1_uint64(&child, &timeout) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500611 timeout > UINT32_MAX) {
David Benjamin3570d732015-06-29 00:28:17 -0400612 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
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 Benjaminad8f5e12017-02-20 17:00:20 -0500616 ret->timeout = (uint32_t)timeout;
David Benjamin5b7b09c2016-11-03 20:32:10 -0400617
David Benjamin8be79a32015-08-23 01:37:36 -0400618 CBS peer;
619 int has_peer;
Adam Langley68e71242016-12-12 11:06:16 -0800620 if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) ||
621 (has_peer && CBS_len(&peer) == 0)) {
David Benjamin8be79a32015-08-23 01:37:36 -0400622 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400623 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400624 }
Adam Langley68e71242016-12-12 11:06:16 -0800625 /* |peer| is processed with the certificate chain. */
Adam Langley95c29f32014-06-20 12:00:00 -0700626
David Benjamin8be79a32015-08-23 01:37:36 -0400627 if (!SSL_SESSION_parse_bounded_octet_string(
628 &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx),
629 kSessionIDContextTag) ||
630 !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag,
631 X509_V_OK) ||
632 !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname,
633 kHostNameTag) ||
634 !SSL_SESSION_parse_string(&session, &ret->psk_identity,
635 kPSKIdentityTag) ||
Martin Kreichgauerbaafa4a2016-08-09 10:18:40 -0700636 !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint,
David Benjamin8be79a32015-08-23 01:37:36 -0400637 kTicketLifetimeHintTag, 0) ||
638 !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick,
639 &ret->tlsext_ticklen, kTicketTag)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400640 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400641 }
Adam Langley95c29f32014-06-20 12:00:00 -0700642
David Benjamin8be79a32015-08-23 01:37:36 -0400643 if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) {
David Benjamin5b7b09c2016-11-03 20:32:10 -0400644 CBS peer_sha256;
David Benjamin8be79a32015-08-23 01:37:36 -0400645 if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) ||
646 !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) ||
647 CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) ||
648 CBS_len(&child) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400649 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400650 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400651 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500652 OPENSSL_memcpy(ret->peer_sha256, CBS_data(&peer_sha256),
653 sizeof(ret->peer_sha256));
David Benjamin83fd6b62014-10-19 04:33:38 -0400654 ret->peer_sha256_valid = 1;
655 } else {
656 ret->peer_sha256_valid = 0;
657 }
Adam Langley95c29f32014-06-20 12:00:00 -0700658
David Benjamin8be79a32015-08-23 01:37:36 -0400659 if (!SSL_SESSION_parse_bounded_octet_string(
660 &session, ret->original_handshake_hash,
661 &ret->original_handshake_hash_len,
662 sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) ||
David Benjamin8fc2dc02017-08-22 15:07:51 -0700663 !SSL_SESSION_parse_crypto_buffer(&session,
664 &ret->signed_cert_timestamp_list,
665 kSignedCertTimestampListTag, pool) ||
666 !SSL_SESSION_parse_crypto_buffer(&session, &ret->ocsp_response,
667 kOCSPResponseTag, pool)) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400668 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400669 }
670
671 int extended_master_secret;
672 if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret,
673 kExtendedMasterSecretTag,
674 0 /* default to false */)) {
David Benjamin3570d732015-06-29 00:28:17 -0400675 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400676 return nullptr;
David Benjamin83fd6b62014-10-19 04:33:38 -0400677 }
David Benjamin8be79a32015-08-23 01:37:36 -0400678 ret->extended_master_secret = !!extended_master_secret;
679
David Benjamin11fa7032017-04-11 22:13:45 -0400680 if (!SSL_SESSION_parse_u16(&session, &ret->group_id, kGroupIDTag, 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400681 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400682 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400683 }
684
685 CBS cert_chain;
Adam Langley68e71242016-12-12 11:06:16 -0800686 CBS_init(&cert_chain, NULL, 0);
David Benjamin26416e92015-08-22 16:04:17 -0400687 int has_cert_chain;
688 if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain,
Adam Langley68e71242016-12-12 11:06:16 -0800689 kCertChainTag) ||
690 (has_cert_chain && CBS_len(&cert_chain) == 0)) {
David Benjamin26416e92015-08-22 16:04:17 -0400691 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400692 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400693 }
Adam Langley68e71242016-12-12 11:06:16 -0800694 if (has_cert_chain && !has_peer) {
695 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400696 return nullptr;
Adam Langley364f7a62016-12-12 10:51:00 -0800697 }
Adam Langley68e71242016-12-12 11:06:16 -0800698 if (has_peer || has_cert_chain) {
699 ret->certs = sk_CRYPTO_BUFFER_new_null();
700 if (ret->certs == NULL) {
David Benjamin26416e92015-08-22 16:04:17 -0400701 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400702 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400703 }
Adam Langley68e71242016-12-12 11:06:16 -0800704
705 if (has_peer) {
David Benjaminee910bf2017-07-25 22:36:00 -0400706 UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new_from_CBS(&peer, pool));
707 if (!buffer ||
708 !PushToStack(ret->certs, std::move(buffer))) {
Adam Langleye8509092016-11-07 14:24:33 -0800709 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400710 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400711 }
712 }
Adam Langley68e71242016-12-12 11:06:16 -0800713
714 while (CBS_len(&cert_chain) > 0) {
715 CBS cert;
716 if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) ||
717 CBS_len(&cert) == 0) {
718 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400719 return nullptr;
Adam Langley68e71242016-12-12 11:06:16 -0800720 }
721
Adam Langley46db7af2017-02-01 15:49:37 -0800722 CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, pool);
Adam Langley68e71242016-12-12 11:06:16 -0800723 if (buffer == NULL ||
724 !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) {
725 CRYPTO_BUFFER_free(buffer);
726 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400727 return nullptr;
Adam Langley68e71242016-12-12 11:06:16 -0800728 }
729 }
730 }
731
David Benjamin31b0c9b2017-07-20 14:49:15 -0400732 if (!x509_method->session_cache_objects(ret.get())) {
Adam Langley68e71242016-12-12 11:06:16 -0800733 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400734 return nullptr;
David Benjamin26416e92015-08-22 16:04:17 -0400735 }
736
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400737 CBS age_add;
738 int age_add_present;
Steven Valdez5b986082016-09-01 12:29:49 -0400739 if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present,
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400740 kTicketAgeAddTag) ||
741 (age_add_present &&
742 !CBS_get_u32(&age_add, &ret->ticket_age_add)) ||
743 CBS_len(&age_add) != 0) {
David Benjamin31b0c9b2017-07-20 14:49:15 -0400744 return nullptr;
Steven Valdez1e6f11a2016-07-27 11:10:52 -0400745 }
746 ret->ticket_age_add_valid = age_add_present;
747
Adam Langley364f7a62016-12-12 10:51:00 -0800748 int is_server;
749 if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag,
750 1 /* default to true */)) {
751 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400752 return nullptr;
Adam Langley364f7a62016-12-12 10:51:00 -0800753 }
754 /* TODO: in time we can include |is_server| for servers too, then we can
755 enforce that client and server sessions are never mixed up. */
756
757 ret->is_server = is_server;
758
David Benjaminf1050fd2016-12-13 20:05:36 -0500759 if (!SSL_SESSION_parse_u16(&session, &ret->peer_signature_algorithm,
760 kPeerSignatureAlgorithmTag, 0) ||
Steven Valdez08b65f42016-12-07 15:29:45 -0500761 !SSL_SESSION_parse_u32(&session, &ret->ticket_max_early_data,
762 kTicketMaxEarlyDataTag, 0) ||
David Benjaminad8f5e12017-02-20 17:00:20 -0500763 !SSL_SESSION_parse_u32(&session, &ret->auth_timeout, kAuthTimeoutTag,
764 ret->timeout) ||
Steven Valdez27a9e6a2017-02-14 13:20:40 -0500765 !SSL_SESSION_parse_octet_string(&session, &ret->early_alpn,
766 &ret->early_alpn_len, kEarlyALPNTag) ||
David Benjaminf1050fd2016-12-13 20:05:36 -0500767 CBS_len(&session) != 0) {
David Benjamin8be79a32015-08-23 01:37:36 -0400768 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400769 return nullptr;
David Benjamin8be79a32015-08-23 01:37:36 -0400770 }
Adam Langley95c29f32014-06-20 12:00:00 -0700771
David Benjaminfd67aa82015-06-15 19:41:48 -0400772 return ret;
David Benjaminfd67aa82015-06-15 19:41:48 -0400773}
774
David Benjamin86e95b82017-07-18 16:34:25 -0400775} // namespace bssl
776
777using namespace bssl;
778
779int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data,
780 size_t *out_len) {
781 if (in->not_resumable) {
782 /* If the caller has an unresumable session, e.g. if |SSL_get_session| were
783 * called on a TLS 1.3 or False Started connection, serialize with a
784 * placeholder value so it is not accidentally deserialized into a resumable
785 * one. */
786 static const char kNotResumableSession[] = "NOT RESUMABLE";
787
788 *out_len = strlen(kNotResumableSession);
789 *out_data = (uint8_t *)BUF_memdup(kNotResumableSession, *out_len);
790 if (*out_data == NULL) {
791 return 0;
792 }
793
794 return 1;
795 }
796
797 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0);
798}
799
800int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data,
801 size_t *out_len) {
802 return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1);
803}
804
805int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) {
806 uint8_t *out;
807 size_t len;
808
809 if (!SSL_SESSION_to_bytes(in, &out, &len)) {
810 return -1;
811 }
812
813 if (len > INT_MAX) {
814 OPENSSL_free(out);
815 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
816 return -1;
817 }
818
819 if (pp) {
820 OPENSSL_memcpy(*pp, out, len);
821 *pp += len;
822 }
823 OPENSSL_free(out);
824
825 return len;
826}
827
Adam Langley46db7af2017-02-01 15:49:37 -0800828SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len,
829 const SSL_CTX *ctx) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400830 CBS cbs;
831 CBS_init(&cbs, in, in_len);
David Benjamin31b0c9b2017-07-20 14:49:15 -0400832 UniquePtr<SSL_SESSION> ret =
833 SSL_SESSION_parse(&cbs, ctx->x509_method, ctx->pool);
834 if (!ret) {
David Benjaminfd67aa82015-06-15 19:41:48 -0400835 return NULL;
836 }
837 if (CBS_len(&cbs) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400838 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION);
David Benjaminfd67aa82015-06-15 19:41:48 -0400839 return NULL;
840 }
David Benjamin31b0c9b2017-07-20 14:49:15 -0400841 return ret.release();
David Benjaminfd67aa82015-06-15 19:41:48 -0400842}