Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* 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 Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 83 | // 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. |
David Benjamin | e8703a3 | 2017-07-09 16:17:55 -0400 | [diff] [blame] | 86 | #if !defined(__STDC_LIMIT_MACROS) |
| 87 | #define __STDC_LIMIT_MACROS |
| 88 | #endif |
| 89 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 90 | #include <openssl/ssl.h> |
| 91 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 92 | #include <limits.h> |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 93 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 94 | |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 95 | #include <utility> |
| 96 | |
Steven Valdez | 84b5c00 | 2016-08-25 16:30:58 -0400 | [diff] [blame] | 97 | #include <openssl/buf.h> |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 98 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 99 | #include <openssl/err.h> |
Brian Smith | 054e682 | 2015-03-27 21:12:01 -1000 | [diff] [blame] | 100 | #include <openssl/mem.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 101 | #include <openssl/x509.h> |
| 102 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 103 | #include "../crypto/internal.h" |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 104 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 105 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 106 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 107 | namespace bssl { |
| 108 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 109 | // An SSL_SESSION is serialized as the following ASN.1 structure: |
| 110 | // |
| 111 | // SSLSession ::= SEQUENCE { |
| 112 | // version INTEGER (1), -- session structure version |
| 113 | // sslVersion INTEGER, -- protocol version number |
| 114 | // cipher OCTET STRING, -- two bytes long |
| 115 | // sessionID OCTET STRING, |
| 116 | // masterKey OCTET STRING, |
| 117 | // time [1] INTEGER, -- seconds since UNIX epoch |
| 118 | // timeout [2] INTEGER, -- in seconds |
| 119 | // 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 |
| 124 | // 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, |
| 132 | // -- stapled OCSP response from the server |
| 133 | // extendedMasterSecret [17] BOOLEAN OPTIONAL, |
| 134 | // groupID [18] INTEGER OPTIONAL, |
| 135 | // certChain [19] SEQUENCE OF Certificate OPTIONAL, |
| 136 | // ticketAgeAdd [21] OCTET STRING OPTIONAL, |
| 137 | // isServer [22] BOOLEAN DEFAULT TRUE, |
| 138 | // peerSignatureAlgorithm [23] INTEGER OPTIONAL, |
| 139 | // ticketMaxEarlyData [24] INTEGER OPTIONAL, |
| 140 | // authTimeout [25] INTEGER OPTIONAL, -- defaults to timeout |
| 141 | // earlyALPN [26] OCTET STRING OPTIONAL, |
| 142 | // } |
| 143 | // |
| 144 | // Note: historically this serialization has included other optional |
| 145 | // fields. Their presence is currently treated as a parse error: |
| 146 | // |
| 147 | // keyArg [0] IMPLICIT OCTET STRING OPTIONAL, |
| 148 | // pskIdentityHint [7] OCTET STRING OPTIONAL, |
| 149 | // compressionMethod [11] OCTET STRING OPTIONAL, |
| 150 | // srpUsername [12] OCTET STRING OPTIONAL, |
| 151 | // ticketFlags [20] INTEGER OPTIONAL, |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 152 | |
Adam Langley | 96c2a28 | 2015-06-02 14:16:44 -0700 | [diff] [blame] | 153 | static const unsigned kVersion = 1; |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 154 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 155 | static const int kTimeTag = |
| 156 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
| 157 | static const int kTimeoutTag = |
| 158 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2; |
| 159 | static const int kPeerTag = |
| 160 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3; |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 161 | static const int kSessionIDContextTag = |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 162 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4; |
| 163 | static const int kVerifyResultTag = |
| 164 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5; |
| 165 | static const int kHostNameTag = |
| 166 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 167 | static const int kPSKIdentityTag = |
| 168 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8; |
| 169 | static const int kTicketLifetimeHintTag = |
| 170 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9; |
| 171 | static const int kTicketTag = |
| 172 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10; |
| 173 | static const int kPeerSHA256Tag = |
| 174 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13; |
| 175 | static const int kOriginalHandshakeHashTag = |
| 176 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14; |
| 177 | static const int kSignedCertTimestampListTag = |
| 178 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15; |
| 179 | static const int kOCSPResponseTag = |
| 180 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 181 | static const int kExtendedMasterSecretTag = |
| 182 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17; |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 183 | static const int kGroupIDTag = |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 184 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 185 | static const int kCertChainTag = |
| 186 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 187 | static const int kTicketAgeAddTag = |
| 188 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 189 | static const int kIsServerTag = |
| 190 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22; |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 191 | static const int kPeerSignatureAlgorithmTag = |
| 192 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 23; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 193 | static const int kTicketMaxEarlyDataTag = |
| 194 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 24; |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 195 | static const int kAuthTimeoutTag = |
| 196 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 25; |
Steven Valdez | 27a9e6a | 2017-02-14 13:20:40 -0500 | [diff] [blame] | 197 | static const int kEarlyALPNTag = |
| 198 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 26; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 199 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 200 | static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data, |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 201 | size_t *out_len, int for_ticket) { |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 202 | if (in == NULL || in->cipher == NULL) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 203 | return 0; |
| 204 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 205 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 206 | ScopedCBB cbb; |
| 207 | CBB session, child, child2; |
| 208 | if (!CBB_init(cbb.get(), 0) || |
| 209 | !CBB_add_asn1(cbb.get(), &session, CBS_ASN1_SEQUENCE) || |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 210 | !CBB_add_asn1_uint64(&session, kVersion) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 211 | !CBB_add_asn1_uint64(&session, in->ssl_version) || |
| 212 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 213 | !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 214 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 215 | // The session ID is irrelevant for a session ticket. |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 216 | !CBB_add_bytes(&child, in->session_id, |
| 217 | for_ticket ? 0 : in->session_id_length) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 218 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 219 | !CBB_add_bytes(&child, in->master_key, in->master_key_length) || |
| 220 | !CBB_add_asn1(&session, &child, kTimeTag) || |
| 221 | !CBB_add_asn1_uint64(&child, in->time) || |
| 222 | !CBB_add_asn1(&session, &child, kTimeoutTag) || |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 223 | !CBB_add_asn1_uint64(&child, in->timeout)) { |
| 224 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 225 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 226 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 227 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 228 | // The peer certificate is only serialized if the SHA-256 isn't |
| 229 | // serialized instead. |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 230 | if (sk_CRYPTO_BUFFER_num(in->certs) > 0 && !in->peer_sha256_valid) { |
| 231 | const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, 0); |
| 232 | if (!CBB_add_asn1(&session, &child, kPeerTag) || |
| 233 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 234 | CRYPTO_BUFFER_len(buffer))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 235 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 236 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 237 | } |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 238 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 239 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 240 | // Although it is OPTIONAL and usually empty, OpenSSL has |
| 241 | // historically always encoded the sid_ctx. |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 242 | if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) || |
| 243 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 244 | !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 245 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 246 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 247 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 248 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 249 | if (in->verify_result != X509_V_OK) { |
| 250 | if (!CBB_add_asn1(&session, &child, kVerifyResultTag) || |
| 251 | !CBB_add_asn1_uint64(&child, in->verify_result)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 252 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 253 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 254 | } |
| 255 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 257 | if (in->tlsext_hostname) { |
| 258 | if (!CBB_add_asn1(&session, &child, kHostNameTag) || |
| 259 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 260 | !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname, |
| 261 | strlen(in->tlsext_hostname))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 262 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 263 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 264 | } |
| 265 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 266 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 267 | if (in->psk_identity) { |
| 268 | if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) || |
| 269 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 270 | !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity, |
| 271 | strlen(in->psk_identity))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 272 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 273 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 274 | } |
| 275 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 276 | |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 277 | if (in->tlsext_tick_lifetime_hint > 0) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 278 | if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) || |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 279 | !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 280 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 281 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 282 | } |
| 283 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 284 | |
David Benjamin | 95d3182 | 2015-06-15 19:53:32 -0400 | [diff] [blame] | 285 | if (in->tlsext_tick && !for_ticket) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 286 | if (!CBB_add_asn1(&session, &child, kTicketTag) || |
| 287 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 288 | !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 289 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 290 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 291 | } |
| 292 | } |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 293 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 294 | if (in->peer_sha256_valid) { |
| 295 | if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) || |
| 296 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 297 | !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 298 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 299 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 300 | } |
| 301 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 302 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 303 | if (in->original_handshake_hash_len > 0) { |
| 304 | if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) || |
| 305 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 306 | !CBB_add_bytes(&child2, in->original_handshake_hash, |
| 307 | in->original_handshake_hash_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 308 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 309 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 310 | } |
| 311 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 312 | |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 313 | if (in->signed_cert_timestamp_list != nullptr) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 314 | if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) || |
| 315 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 316 | !CBB_add_bytes(&child2, |
| 317 | CRYPTO_BUFFER_data(in->signed_cert_timestamp_list), |
| 318 | CRYPTO_BUFFER_len(in->signed_cert_timestamp_list))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 319 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 320 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 321 | } |
| 322 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 323 | |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 324 | if (in->ocsp_response != nullptr) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 325 | if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) || |
| 326 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 327 | !CBB_add_bytes(&child2, CRYPTO_BUFFER_data(in->ocsp_response), |
| 328 | CRYPTO_BUFFER_len(in->ocsp_response))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 329 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 330 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 331 | } |
| 332 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 333 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 334 | if (in->extended_master_secret) { |
| 335 | if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) || |
| 336 | !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 337 | !CBB_add_u8(&child2, 0xff)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 338 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 339 | return 0; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 343 | if (in->group_id > 0 && |
| 344 | (!CBB_add_asn1(&session, &child, kGroupIDTag) || |
| 345 | !CBB_add_asn1_uint64(&child, in->group_id))) { |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 346 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 347 | return 0; |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 348 | } |
| 349 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 350 | // The certificate chain is only serialized if the leaf's SHA-256 isn't |
| 351 | // serialized instead. |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 352 | if (in->certs != NULL && |
| 353 | !in->peer_sha256_valid && |
| 354 | sk_CRYPTO_BUFFER_num(in->certs) >= 2) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 355 | if (!CBB_add_asn1(&session, &child, kCertChainTag)) { |
| 356 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 357 | return 0; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 358 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 359 | for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs); i++) { |
| 360 | const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, i); |
| 361 | if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 362 | CRYPTO_BUFFER_len(buffer))) { |
| 363 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 364 | return 0; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 369 | if (in->ticket_age_add_valid) { |
| 370 | if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) || |
| 371 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 372 | !CBB_add_u32(&child2, in->ticket_age_add)) { |
| 373 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 374 | return 0; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 378 | if (!in->is_server) { |
| 379 | if (!CBB_add_asn1(&session, &child, kIsServerTag) || |
| 380 | !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 381 | !CBB_add_u8(&child2, 0x00)) { |
| 382 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 383 | return 0; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 387 | if (in->peer_signature_algorithm != 0 && |
| 388 | (!CBB_add_asn1(&session, &child, kPeerSignatureAlgorithmTag) || |
| 389 | !CBB_add_asn1_uint64(&child, in->peer_signature_algorithm))) { |
| 390 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 391 | return 0; |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 392 | } |
| 393 | |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 394 | if (in->ticket_max_early_data != 0 && |
| 395 | (!CBB_add_asn1(&session, &child, kTicketMaxEarlyDataTag) || |
| 396 | !CBB_add_asn1_uint64(&child, in->ticket_max_early_data))) { |
| 397 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 398 | return 0; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 399 | } |
| 400 | |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 401 | if (in->timeout != in->auth_timeout && |
| 402 | (!CBB_add_asn1(&session, &child, kAuthTimeoutTag) || |
| 403 | !CBB_add_asn1_uint64(&child, in->auth_timeout))) { |
| 404 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 405 | return 0; |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 406 | } |
| 407 | |
Steven Valdez | 27a9e6a | 2017-02-14 13:20:40 -0500 | [diff] [blame] | 408 | if (in->early_alpn) { |
| 409 | if (!CBB_add_asn1(&session, &child, kEarlyALPNTag) || |
| 410 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 411 | !CBB_add_bytes(&child2, (const uint8_t *)in->early_alpn, |
| 412 | in->early_alpn_len)) { |
| 413 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 414 | return 0; |
Steven Valdez | 27a9e6a | 2017-02-14 13:20:40 -0500 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 418 | if (!CBB_finish(cbb.get(), out_data, out_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 419 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 420 | return 0; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 421 | } |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 422 | return 1; |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 423 | } |
| 424 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 425 | // SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING |
| 426 | // explicitly tagged with |tag| from |cbs| and saves it in |*out|. On |
| 427 | // entry, if |*out| is not NULL, it frees the existing contents. If |
| 428 | // the element was not found, it sets |*out| to NULL. It returns one |
| 429 | // on success, whether or not the element was found, and zero on |
| 430 | // decode error. |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 431 | static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 432 | CBS value; |
| 433 | int present; |
| 434 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 435 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | if (present) { |
| 439 | if (CBS_contains_zero_byte(&value)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 440 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | if (!CBS_strdup(&value, out)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 444 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 445 | return 0; |
| 446 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 447 | } else { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 448 | OPENSSL_free(*out); |
| 449 | *out = NULL; |
| 450 | } |
| 451 | return 1; |
| 452 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 453 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 454 | // SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING |
| 455 | // explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr| |
| 456 | // and |*out_len|. If |*out_ptr| is not NULL, it frees the existing |
| 457 | // contents. On entry, if the element was not found, it sets |
| 458 | // |*out_ptr| to NULL. It returns one on success, whether or not the |
| 459 | // element was found, and zero on decode error. |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 460 | static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr, |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 461 | size_t *out_len, unsigned tag) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 462 | CBS value; |
| 463 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 464 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | if (!CBS_stow(&value, out_ptr, out_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 468 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | return 1; |
| 472 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 473 | |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 474 | static int SSL_SESSION_parse_crypto_buffer(CBS *cbs, CRYPTO_BUFFER **out, |
| 475 | unsigned tag, |
| 476 | CRYPTO_BUFFER_POOL *pool) { |
| 477 | if (!CBS_peek_asn1_tag(cbs, tag)) { |
| 478 | return 1; |
| 479 | } |
| 480 | |
| 481 | CBS child, value; |
| 482 | if (!CBS_get_asn1(cbs, &child, tag) || |
| 483 | !CBS_get_asn1(&child, &value, CBS_ASN1_OCTETSTRING) || |
| 484 | CBS_len(&child) != 0) { |
| 485 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 486 | return 0; |
| 487 | } |
| 488 | CRYPTO_BUFFER_free(*out); |
| 489 | *out = CRYPTO_BUFFER_new_from_CBS(&value, pool); |
| 490 | if (*out == nullptr) { |
| 491 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 492 | return 0; |
| 493 | } |
| 494 | return 1; |
| 495 | } |
| 496 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 497 | // SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING |
| 498 | // explicitly tagged with |tag| of size at most |max_out|. |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 499 | static int SSL_SESSION_parse_bounded_octet_string( |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 500 | CBS *cbs, uint8_t *out, uint8_t *out_len, uint8_t max_out, unsigned tag) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 501 | CBS value; |
| 502 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) || |
| 503 | CBS_len(&value) > max_out) { |
| 504 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 505 | return 0; |
| 506 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 507 | OPENSSL_memcpy(out, CBS_data(&value), CBS_len(&value)); |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 508 | *out_len = (uint8_t)CBS_len(&value); |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 509 | return 1; |
| 510 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 511 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 512 | static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag, |
| 513 | long default_value) { |
| 514 | uint64_t value; |
| 515 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 516 | (uint64_t)default_value) || |
| 517 | value > LONG_MAX) { |
| 518 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 519 | return 0; |
| 520 | } |
| 521 | *out = (long)value; |
| 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag, |
| 526 | uint32_t default_value) { |
| 527 | uint64_t value; |
| 528 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 529 | (uint64_t)default_value) || |
| 530 | value > 0xffffffff) { |
| 531 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 532 | return 0; |
| 533 | } |
| 534 | *out = (uint32_t)value; |
| 535 | return 1; |
| 536 | } |
| 537 | |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 538 | static int SSL_SESSION_parse_u16(CBS *cbs, uint16_t *out, unsigned tag, |
| 539 | uint16_t default_value) { |
| 540 | uint64_t value; |
| 541 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 542 | (uint64_t)default_value) || |
| 543 | value > 0xffff) { |
| 544 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 545 | return 0; |
| 546 | } |
| 547 | *out = (uint16_t)value; |
| 548 | return 1; |
| 549 | } |
| 550 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 551 | UniquePtr<SSL_SESSION> SSL_SESSION_parse(CBS *cbs, |
| 552 | const SSL_X509_METHOD *x509_method, |
| 553 | CRYPTO_BUFFER_POOL *pool) { |
| 554 | UniquePtr<SSL_SESSION> ret = ssl_session_new(x509_method); |
| 555 | if (!ret) { |
| 556 | return nullptr; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 557 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 558 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 559 | CBS session; |
| 560 | uint64_t version, ssl_version; |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 561 | uint16_t unused; |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 562 | if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) || |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 563 | !CBS_get_asn1_uint64(&session, &version) || |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 564 | version != kVersion || |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 565 | !CBS_get_asn1_uint64(&session, &ssl_version) || |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 566 | // Require sessions have versions valid in either TLS or DTLS. The session |
| 567 | // will not be used by the handshake if not applicable, but, for |
| 568 | // simplicity, never parse a session that does not pass |
| 569 | // |ssl_protocol_version_from_wire|. |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 570 | ssl_version > UINT16_MAX || |
| 571 | !ssl_protocol_version_from_wire(&unused, ssl_version)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 572 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 573 | return nullptr; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 574 | } |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 575 | ret->ssl_version = ssl_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 576 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 577 | CBS cipher; |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 578 | uint16_t cipher_value; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 579 | if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) || |
| 580 | !CBS_get_u16(&cipher, &cipher_value) || |
| 581 | CBS_len(&cipher) != 0) { |
| 582 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 583 | return nullptr; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 584 | } |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 585 | ret->cipher = SSL_get_cipher_by_value(cipher_value); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 586 | if (ret->cipher == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 587 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 588 | return nullptr; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 589 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 590 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 591 | CBS session_id, master_key; |
| 592 | if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) || |
| 593 | CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH || |
| 594 | !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) || |
| 595 | CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 596 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 597 | return nullptr; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 598 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 599 | OPENSSL_memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id)); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 600 | ret->session_id_length = CBS_len(&session_id); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 601 | OPENSSL_memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key)); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 602 | ret->master_key_length = CBS_len(&master_key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 603 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 604 | CBS child; |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 605 | uint64_t timeout; |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 606 | if (!CBS_get_asn1(&session, &child, kTimeTag) || |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 607 | !CBS_get_asn1_uint64(&child, &ret->time) || |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 608 | !CBS_get_asn1(&session, &child, kTimeoutTag) || |
| 609 | !CBS_get_asn1_uint64(&child, &timeout) || |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 610 | timeout > UINT32_MAX) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 611 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 612 | return nullptr; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 613 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 614 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 615 | ret->timeout = (uint32_t)timeout; |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 616 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 617 | CBS peer; |
| 618 | int has_peer; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 619 | if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) || |
| 620 | (has_peer && CBS_len(&peer) == 0)) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 621 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 622 | return nullptr; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 623 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 624 | // |peer| is processed with the certificate chain. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 625 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 626 | if (!SSL_SESSION_parse_bounded_octet_string( |
| 627 | &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx), |
| 628 | kSessionIDContextTag) || |
| 629 | !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag, |
| 630 | X509_V_OK) || |
| 631 | !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname, |
| 632 | kHostNameTag) || |
| 633 | !SSL_SESSION_parse_string(&session, &ret->psk_identity, |
| 634 | kPSKIdentityTag) || |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 635 | !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint, |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 636 | kTicketLifetimeHintTag, 0) || |
| 637 | !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick, |
| 638 | &ret->tlsext_ticklen, kTicketTag)) { |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 639 | return nullptr; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 640 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 641 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 642 | if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) { |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 643 | CBS peer_sha256; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 644 | if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) || |
| 645 | !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) || |
| 646 | CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) || |
| 647 | CBS_len(&child) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 648 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 649 | return nullptr; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 650 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 651 | OPENSSL_memcpy(ret->peer_sha256, CBS_data(&peer_sha256), |
| 652 | sizeof(ret->peer_sha256)); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 653 | ret->peer_sha256_valid = 1; |
| 654 | } else { |
| 655 | ret->peer_sha256_valid = 0; |
| 656 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 657 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 658 | if (!SSL_SESSION_parse_bounded_octet_string( |
| 659 | &session, ret->original_handshake_hash, |
| 660 | &ret->original_handshake_hash_len, |
| 661 | sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) || |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 662 | !SSL_SESSION_parse_crypto_buffer(&session, |
| 663 | &ret->signed_cert_timestamp_list, |
| 664 | kSignedCertTimestampListTag, pool) || |
| 665 | !SSL_SESSION_parse_crypto_buffer(&session, &ret->ocsp_response, |
| 666 | kOCSPResponseTag, pool)) { |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 667 | return nullptr; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | int extended_master_secret; |
| 671 | if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret, |
| 672 | kExtendedMasterSecretTag, |
| 673 | 0 /* default to false */)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 674 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 675 | return nullptr; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 676 | } |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 677 | ret->extended_master_secret = !!extended_master_secret; |
| 678 | |
David Benjamin | 11fa703 | 2017-04-11 22:13:45 -0400 | [diff] [blame] | 679 | if (!SSL_SESSION_parse_u16(&session, &ret->group_id, kGroupIDTag, 0)) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 680 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 681 | return nullptr; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | CBS cert_chain; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 685 | CBS_init(&cert_chain, NULL, 0); |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 686 | int has_cert_chain; |
| 687 | if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain, |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 688 | kCertChainTag) || |
| 689 | (has_cert_chain && CBS_len(&cert_chain) == 0)) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 690 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 691 | return nullptr; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 692 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 693 | if (has_cert_chain && !has_peer) { |
| 694 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 695 | return nullptr; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 696 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 697 | if (has_peer || has_cert_chain) { |
| 698 | ret->certs = sk_CRYPTO_BUFFER_new_null(); |
| 699 | if (ret->certs == NULL) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 700 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 701 | return nullptr; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 702 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 703 | |
| 704 | if (has_peer) { |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 705 | UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new_from_CBS(&peer, pool)); |
| 706 | if (!buffer || |
| 707 | !PushToStack(ret->certs, std::move(buffer))) { |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame] | 708 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 709 | return nullptr; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 710 | } |
| 711 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 712 | |
| 713 | while (CBS_len(&cert_chain) > 0) { |
| 714 | CBS cert; |
| 715 | if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) || |
| 716 | CBS_len(&cert) == 0) { |
| 717 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 718 | return nullptr; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame] | 721 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, pool); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 722 | if (buffer == NULL || |
| 723 | !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) { |
| 724 | CRYPTO_BUFFER_free(buffer); |
| 725 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 726 | return nullptr; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 731 | if (!x509_method->session_cache_objects(ret.get())) { |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 732 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 733 | return nullptr; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 734 | } |
| 735 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 736 | CBS age_add; |
| 737 | int age_add_present; |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 738 | if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present, |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 739 | kTicketAgeAddTag) || |
| 740 | (age_add_present && |
| 741 | !CBS_get_u32(&age_add, &ret->ticket_age_add)) || |
| 742 | CBS_len(&age_add) != 0) { |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 743 | return nullptr; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 744 | } |
| 745 | ret->ticket_age_add_valid = age_add_present; |
| 746 | |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 747 | int is_server; |
| 748 | if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag, |
| 749 | 1 /* default to true */)) { |
| 750 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 751 | return nullptr; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 752 | } |
| 753 | /* TODO: in time we can include |is_server| for servers too, then we can |
| 754 | enforce that client and server sessions are never mixed up. */ |
| 755 | |
| 756 | ret->is_server = is_server; |
| 757 | |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 758 | if (!SSL_SESSION_parse_u16(&session, &ret->peer_signature_algorithm, |
| 759 | kPeerSignatureAlgorithmTag, 0) || |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 760 | !SSL_SESSION_parse_u32(&session, &ret->ticket_max_early_data, |
| 761 | kTicketMaxEarlyDataTag, 0) || |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 762 | !SSL_SESSION_parse_u32(&session, &ret->auth_timeout, kAuthTimeoutTag, |
| 763 | ret->timeout) || |
Steven Valdez | 27a9e6a | 2017-02-14 13:20:40 -0500 | [diff] [blame] | 764 | !SSL_SESSION_parse_octet_string(&session, &ret->early_alpn, |
| 765 | &ret->early_alpn_len, kEarlyALPNTag) || |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 766 | CBS_len(&session) != 0) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 767 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 768 | return nullptr; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 769 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 770 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 771 | return ret; |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 772 | } |
| 773 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 774 | } // namespace bssl |
| 775 | |
| 776 | using namespace bssl; |
| 777 | |
| 778 | int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data, |
| 779 | size_t *out_len) { |
| 780 | if (in->not_resumable) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 781 | // If the caller has an unresumable session, e.g. if |SSL_get_session| were |
| 782 | // called on a TLS 1.3 or False Started connection, serialize with a |
| 783 | // placeholder value so it is not accidentally deserialized into a resumable |
| 784 | // one. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 785 | static const char kNotResumableSession[] = "NOT RESUMABLE"; |
| 786 | |
| 787 | *out_len = strlen(kNotResumableSession); |
| 788 | *out_data = (uint8_t *)BUF_memdup(kNotResumableSession, *out_len); |
| 789 | if (*out_data == NULL) { |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | return 1; |
| 794 | } |
| 795 | |
| 796 | return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0); |
| 797 | } |
| 798 | |
| 799 | int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data, |
| 800 | size_t *out_len) { |
| 801 | return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1); |
| 802 | } |
| 803 | |
| 804 | int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) { |
| 805 | uint8_t *out; |
| 806 | size_t len; |
| 807 | |
| 808 | if (!SSL_SESSION_to_bytes(in, &out, &len)) { |
| 809 | return -1; |
| 810 | } |
| 811 | |
| 812 | if (len > INT_MAX) { |
| 813 | OPENSSL_free(out); |
| 814 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 815 | return -1; |
| 816 | } |
| 817 | |
| 818 | if (pp) { |
| 819 | OPENSSL_memcpy(*pp, out, len); |
| 820 | *pp += len; |
| 821 | } |
| 822 | OPENSSL_free(out); |
| 823 | |
| 824 | return len; |
| 825 | } |
| 826 | |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame] | 827 | SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len, |
| 828 | const SSL_CTX *ctx) { |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 829 | CBS cbs; |
| 830 | CBS_init(&cbs, in, in_len); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 831 | UniquePtr<SSL_SESSION> ret = |
| 832 | SSL_SESSION_parse(&cbs, ctx->x509_method, ctx->pool); |
| 833 | if (!ret) { |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 834 | return NULL; |
| 835 | } |
| 836 | if (CBS_len(&cbs) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 837 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 838 | return NULL; |
| 839 | } |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 840 | return ret.release(); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 841 | } |