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 | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 83 | #include <openssl/ssl.h> |
| 84 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 85 | #include <limits.h> |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 86 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 87 | |
Steven Valdez | 84b5c00 | 2016-08-25 16:30:58 -0400 | [diff] [blame] | 88 | #include <openssl/buf.h> |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 89 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 90 | #include <openssl/err.h> |
Brian Smith | 054e682 | 2015-03-27 21:12:01 -1000 | [diff] [blame] | 91 | #include <openssl/mem.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 92 | #include <openssl/x509.h> |
| 93 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 94 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 95 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 96 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 97 | /* An SSL_SESSION is serialized as the following ASN.1 structure: |
| 98 | * |
| 99 | * SSLSession ::= SEQUENCE { |
David Benjamin | 7bb88bb | 2016-11-01 13:59:01 -0400 | [diff] [blame] | 100 | * version INTEGER (1), -- session structure version |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 101 | * sslVersion INTEGER, -- protocol version number |
| 102 | * cipher OCTET STRING, -- two bytes long |
| 103 | * sessionID OCTET STRING, |
| 104 | * masterKey OCTET STRING, |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 105 | * time [1] INTEGER, -- seconds since UNIX epoch |
| 106 | * timeout [2] INTEGER, -- in seconds |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 107 | * peer [3] Certificate OPTIONAL, |
| 108 | * sessionIDContext [4] OCTET STRING OPTIONAL, |
| 109 | * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes |
| 110 | * hostName [6] OCTET STRING OPTIONAL, |
| 111 | * -- from server_name extension |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 112 | * pskIdentity [8] OCTET STRING OPTIONAL, |
| 113 | * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only |
| 114 | * ticket [10] OCTET STRING OPTIONAL, -- client-only |
| 115 | * peerSHA256 [13] OCTET STRING OPTIONAL, |
| 116 | * originalHandshakeHash [14] OCTET STRING OPTIONAL, |
| 117 | * signedCertTimestampList [15] OCTET STRING OPTIONAL, |
| 118 | * -- contents of SCT extension |
| 119 | * ocspResponse [16] OCTET STRING OPTIONAL, |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 120 | * -- stapled OCSP response from the server |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 121 | * extendedMasterSecret [17] BOOLEAN OPTIONAL, |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 122 | * keyExchangeInfo [18] INTEGER OPTIONAL, |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 123 | * certChain [19] SEQUENCE OF Certificate OPTIONAL, |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 124 | * ticketAgeAdd [21] OCTET STRING OPTIONAL, |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 125 | * x509ChainIncludeLeaf [22] BOOLEAN OPTIONAL, |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 126 | * } |
| 127 | * |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 128 | * Note: historically this serialization has included other optional |
| 129 | * fields. Their presense is currently treated as a parse error: |
| 130 | * |
| 131 | * keyArg [0] IMPLICIT OCTET STRING OPTIONAL, |
| 132 | * pskIdentityHint [7] OCTET STRING OPTIONAL, |
| 133 | * compressionMethod [11] OCTET STRING OPTIONAL, |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 134 | * srpUsername [12] OCTET STRING OPTIONAL, |
| 135 | * ticketFlags [20] INTEGER OPTIONAL, |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 136 | * |
| 137 | * |
| 138 | * When serialising, the first element of |certChain| does not duplicate the |
| 139 | * contents of |peer|, but that was not always true. When parsing, the returned |
| 140 | * |x509_chain| sometimes needs to contain the leaf certificate at the |
| 141 | * beginning (clients) and sometimes not (servers). The |x509ChainIncludeLeaf| |
| 142 | * value specifies whether this duplication should be done for the in-memory |
| 143 | * representation. If not given, the parsing code looks to see whether the |
| 144 | * first cert in |certChain| matches the |peer| and assumes that it's parsing |
| 145 | * old session data and thus that |x509ChainIncludeLeaf| should be true. */ |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 146 | |
Adam Langley | 96c2a28 | 2015-06-02 14:16:44 -0700 | [diff] [blame] | 147 | static const unsigned kVersion = 1; |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 148 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 149 | static const int kTimeTag = |
| 150 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
| 151 | static const int kTimeoutTag = |
| 152 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2; |
| 153 | static const int kPeerTag = |
| 154 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3; |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 155 | static const int kSessionIDContextTag = |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 156 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4; |
| 157 | static const int kVerifyResultTag = |
| 158 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5; |
| 159 | static const int kHostNameTag = |
| 160 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 161 | static const int kPSKIdentityTag = |
| 162 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8; |
| 163 | static const int kTicketLifetimeHintTag = |
| 164 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9; |
| 165 | static const int kTicketTag = |
| 166 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10; |
| 167 | static const int kPeerSHA256Tag = |
| 168 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13; |
| 169 | static const int kOriginalHandshakeHashTag = |
| 170 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14; |
| 171 | static const int kSignedCertTimestampListTag = |
| 172 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15; |
| 173 | static const int kOCSPResponseTag = |
| 174 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 175 | static const int kExtendedMasterSecretTag = |
| 176 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17; |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 177 | static const int kKeyExchangeInfoTag = |
| 178 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 179 | static const int kCertChainTag = |
| 180 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 181 | static const int kTicketAgeAddTag = |
| 182 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21; |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 183 | static const int kX509ChainIncludeLeafTag = |
| 184 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 185 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 186 | 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] | 187 | size_t *out_len, int for_ticket) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 188 | CBB cbb, session, child, child2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 189 | |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 190 | if (in == NULL || in->cipher == NULL) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 191 | return 0; |
| 192 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 194 | CBB_zero(&cbb); |
| 195 | if (!CBB_init(&cbb, 0) || |
| 196 | !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) || |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 197 | !CBB_add_asn1_uint64(&session, kVersion) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 198 | !CBB_add_asn1_uint64(&session, in->ssl_version) || |
| 199 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 200 | !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 201 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 202 | /* The session ID is irrelevant for a session ticket. */ |
| 203 | !CBB_add_bytes(&child, in->session_id, |
| 204 | for_ticket ? 0 : in->session_id_length) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 205 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
| 206 | !CBB_add_bytes(&child, in->master_key, in->master_key_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 207 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 208 | goto err; |
| 209 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 210 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 211 | if (in->time < 0) { |
| 212 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 213 | goto err; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 214 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 215 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 216 | if (!CBB_add_asn1(&session, &child, kTimeTag) || |
| 217 | !CBB_add_asn1_uint64(&child, in->time)) { |
| 218 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 219 | goto err; |
| 220 | } |
David Benjamin | 0f31ac7 | 2016-11-02 23:05:14 -0400 | [diff] [blame] | 221 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 222 | if (in->timeout < 0) { |
| 223 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 224 | goto err; |
| 225 | } |
| 226 | |
| 227 | if (!CBB_add_asn1(&session, &child, kTimeoutTag) || |
| 228 | !CBB_add_asn1_uint64(&child, in->timeout)) { |
| 229 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 230 | goto err; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 231 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 232 | |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 233 | if (sk_CRYPTO_BUFFER_num(in->certs) > 0 && !in->peer_sha256_valid) { |
| 234 | const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, 0); |
| 235 | if (!CBB_add_asn1(&session, &child, kPeerTag) || |
| 236 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 237 | CRYPTO_BUFFER_len(buffer))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 238 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 239 | goto err; |
| 240 | } |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 241 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 242 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 243 | /* Although it is OPTIONAL and usually empty, OpenSSL has |
| 244 | * historically always encoded the sid_ctx. */ |
| 245 | if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) || |
| 246 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 247 | !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 248 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 249 | goto err; |
| 250 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 251 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 252 | if (in->verify_result != X509_V_OK) { |
| 253 | if (!CBB_add_asn1(&session, &child, kVerifyResultTag) || |
| 254 | !CBB_add_asn1_uint64(&child, in->verify_result)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 255 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 256 | goto err; |
| 257 | } |
| 258 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 259 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 260 | if (in->tlsext_hostname) { |
| 261 | if (!CBB_add_asn1(&session, &child, kHostNameTag) || |
| 262 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 263 | !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname, |
| 264 | strlen(in->tlsext_hostname))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 265 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 266 | goto err; |
| 267 | } |
| 268 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 269 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 270 | if (in->psk_identity) { |
| 271 | if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) || |
| 272 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 273 | !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity, |
| 274 | strlen(in->psk_identity))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 275 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 276 | goto err; |
| 277 | } |
| 278 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 279 | |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 280 | if (in->tlsext_tick_lifetime_hint > 0) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 281 | if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) || |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 282 | !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 283 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 284 | goto err; |
| 285 | } |
| 286 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 287 | |
David Benjamin | 95d3182 | 2015-06-15 19:53:32 -0400 | [diff] [blame] | 288 | if (in->tlsext_tick && !for_ticket) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 289 | if (!CBB_add_asn1(&session, &child, kTicketTag) || |
| 290 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 291 | !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 292 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 293 | goto err; |
| 294 | } |
| 295 | } |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 296 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 297 | if (in->peer_sha256_valid) { |
| 298 | if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) || |
| 299 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 300 | !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 301 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 302 | goto err; |
| 303 | } |
| 304 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 305 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 306 | if (in->original_handshake_hash_len > 0) { |
| 307 | if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) || |
| 308 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 309 | !CBB_add_bytes(&child2, in->original_handshake_hash, |
| 310 | in->original_handshake_hash_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 311 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 312 | goto err; |
| 313 | } |
| 314 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 316 | if (in->tlsext_signed_cert_timestamp_list_length > 0) { |
| 317 | if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) || |
| 318 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 319 | !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list, |
| 320 | in->tlsext_signed_cert_timestamp_list_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 321 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 322 | goto err; |
| 323 | } |
| 324 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 325 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 326 | if (in->ocsp_response_length > 0) { |
| 327 | if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) || |
| 328 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 329 | !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 330 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 331 | goto err; |
| 332 | } |
| 333 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 334 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 335 | 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 Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 339 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 340 | goto err; |
| 341 | } |
| 342 | } |
| 343 | |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 344 | if (in->key_exchange_info > 0 && |
| 345 | (!CBB_add_asn1(&session, &child, kKeyExchangeInfoTag) || |
| 346 | !CBB_add_asn1_uint64(&child, in->key_exchange_info))) { |
| 347 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 348 | goto err; |
| 349 | } |
| 350 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 351 | /* The certificate chain is only serialized if the leaf's SHA-256 isn't |
| 352 | * serialized instead. */ |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 353 | if (in->certs != NULL && |
| 354 | !in->peer_sha256_valid && |
| 355 | sk_CRYPTO_BUFFER_num(in->certs) > 1) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 356 | if (!CBB_add_asn1(&session, &child, kCertChainTag)) { |
| 357 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 358 | goto err; |
| 359 | } |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 360 | 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 Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 365 | goto err; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 370 | 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); |
| 375 | goto err; |
| 376 | } |
| 377 | } |
| 378 | |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 379 | if (in->x509_chain_should_include_leaf) { |
| 380 | if (!CBB_add_asn1(&session, &child, kX509ChainIncludeLeafTag) || |
| 381 | !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 382 | !CBB_add_u8(&child2, 0xff)) { |
| 383 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 384 | goto err; |
| 385 | } |
| 386 | } |
| 387 | |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 388 | if (!CBB_finish(&cbb, out_data, out_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 389 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 390 | goto err; |
| 391 | } |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 392 | return 1; |
| 393 | |
| 394 | err: |
| 395 | CBB_cleanup(&cbb); |
| 396 | return 0; |
| 397 | } |
| 398 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 399 | int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data, |
| 400 | size_t *out_len) { |
Steven Valdez | 84b5c00 | 2016-08-25 16:30:58 -0400 | [diff] [blame] | 401 | if (in->not_resumable) { |
| 402 | /* If the caller has an unresumable session, e.g. if |SSL_get_session| were |
| 403 | * called on a TLS 1.3 or False Started connection, serialize with a |
| 404 | * placeholder value so it is not accidentally deserialized into a resumable |
| 405 | * one. */ |
| 406 | static const char kNotResumableSession[] = "NOT RESUMABLE"; |
| 407 | |
| 408 | *out_len = strlen(kNotResumableSession); |
| 409 | *out_data = BUF_memdup(kNotResumableSession, *out_len); |
| 410 | if (*out_data == NULL) { |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | return 1; |
| 415 | } |
| 416 | |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 417 | return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0); |
| 418 | } |
| 419 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 420 | int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data, |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 421 | size_t *out_len) { |
| 422 | return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1); |
| 423 | } |
| 424 | |
| 425 | int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) { |
| 426 | uint8_t *out; |
| 427 | size_t len; |
| 428 | |
| 429 | if (!SSL_SESSION_to_bytes(in, &out, &len)) { |
| 430 | return -1; |
| 431 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 432 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 433 | if (len > INT_MAX) { |
| 434 | OPENSSL_free(out); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 435 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 436 | return -1; |
| 437 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 438 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 439 | if (pp) { |
| 440 | memcpy(*pp, out, len); |
| 441 | *pp += len; |
| 442 | } |
| 443 | OPENSSL_free(out); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 444 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 445 | return len; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 446 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 447 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 448 | /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 449 | * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On |
| 450 | * entry, if |*out| is not NULL, it frees the existing contents. If |
| 451 | * the element was not found, it sets |*out| to NULL. It returns one |
| 452 | * on success, whether or not the element was found, and zero on |
| 453 | * decode error. */ |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 454 | static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 455 | CBS value; |
| 456 | int present; |
| 457 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 458 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | if (present) { |
| 462 | if (CBS_contains_zero_byte(&value)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 463 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 464 | return 0; |
| 465 | } |
| 466 | if (!CBS_strdup(&value, out)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 467 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 468 | return 0; |
| 469 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 470 | } else { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 471 | OPENSSL_free(*out); |
| 472 | *out = NULL; |
| 473 | } |
| 474 | return 1; |
| 475 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 476 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 477 | /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 478 | * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr| |
| 479 | * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing |
| 480 | * contents. On entry, if the element was not found, it sets |
| 481 | * |*out_ptr| to NULL. It returns one on success, whether or not the |
| 482 | * element was found, and zero on decode error. */ |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 483 | 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] | 484 | size_t *out_len, unsigned tag) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 485 | CBS value; |
| 486 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 487 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 488 | return 0; |
| 489 | } |
| 490 | if (!CBS_stow(&value, out_ptr, out_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 491 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | return 1; |
| 495 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 496 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -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|. */ |
| 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 | } |
| 507 | 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 | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 538 | static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) { |
| 539 | SSL_SESSION *ret = SSL_SESSION_new(); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 540 | if (ret == NULL) { |
| 541 | goto err; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 542 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 543 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 544 | CBS session; |
| 545 | uint64_t version, ssl_version; |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 546 | if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) || |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 547 | !CBS_get_asn1_uint64(&session, &version) || |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 548 | version != kVersion || |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 549 | !CBS_get_asn1_uint64(&session, &ssl_version)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 550 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 551 | goto err; |
| 552 | } |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 553 | ret->ssl_version = ssl_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 554 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 555 | CBS cipher; |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 556 | uint16_t cipher_value; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 557 | if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) || |
| 558 | !CBS_get_u16(&cipher, &cipher_value) || |
| 559 | CBS_len(&cipher) != 0) { |
| 560 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 561 | goto err; |
| 562 | } |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 563 | ret->cipher = SSL_get_cipher_by_value(cipher_value); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 564 | if (ret->cipher == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 565 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 566 | goto err; |
| 567 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 568 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 569 | CBS session_id, master_key; |
| 570 | if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) || |
| 571 | CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH || |
| 572 | !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) || |
| 573 | CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 574 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 575 | goto err; |
| 576 | } |
| 577 | memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id)); |
| 578 | ret->session_id_length = CBS_len(&session_id); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 579 | memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key)); |
| 580 | ret->master_key_length = CBS_len(&master_key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 581 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 582 | CBS child; |
| 583 | uint64_t time, timeout; |
| 584 | if (!CBS_get_asn1(&session, &child, kTimeTag) || |
| 585 | !CBS_get_asn1_uint64(&child, &time) || |
| 586 | time > LONG_MAX || |
| 587 | !CBS_get_asn1(&session, &child, kTimeoutTag) || |
| 588 | !CBS_get_asn1_uint64(&child, &timeout) || |
| 589 | timeout > LONG_MAX) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 590 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 591 | goto err; |
| 592 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 593 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 594 | ret->time = (long)time; |
| 595 | ret->timeout = (long)timeout; |
| 596 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 597 | CBS peer; |
| 598 | int has_peer; |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 599 | if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) || |
| 600 | (has_peer && CBS_len(&peer) == 0)) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 601 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 602 | goto err; |
| 603 | } |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 604 | /* |peer| is processed with the certificate chain. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 605 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 606 | if (!SSL_SESSION_parse_bounded_octet_string( |
| 607 | &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx), |
| 608 | kSessionIDContextTag) || |
| 609 | !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag, |
| 610 | X509_V_OK) || |
| 611 | !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname, |
| 612 | kHostNameTag) || |
| 613 | !SSL_SESSION_parse_string(&session, &ret->psk_identity, |
| 614 | kPSKIdentityTag) || |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 615 | !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint, |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 616 | kTicketLifetimeHintTag, 0) || |
| 617 | !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick, |
| 618 | &ret->tlsext_ticklen, kTicketTag)) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 619 | goto err; |
| 620 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 621 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 622 | if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) { |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 623 | CBS peer_sha256; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 624 | if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) || |
| 625 | !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) || |
| 626 | CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) || |
| 627 | CBS_len(&child) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 628 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 629 | goto err; |
| 630 | } |
| 631 | memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256)); |
| 632 | ret->peer_sha256_valid = 1; |
| 633 | } else { |
| 634 | ret->peer_sha256_valid = 0; |
| 635 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 636 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 637 | if (!SSL_SESSION_parse_bounded_octet_string( |
| 638 | &session, ret->original_handshake_hash, |
| 639 | &ret->original_handshake_hash_len, |
| 640 | sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) || |
| 641 | !SSL_SESSION_parse_octet_string( |
| 642 | &session, &ret->tlsext_signed_cert_timestamp_list, |
| 643 | &ret->tlsext_signed_cert_timestamp_list_length, |
| 644 | kSignedCertTimestampListTag) || |
| 645 | !SSL_SESSION_parse_octet_string( |
| 646 | &session, &ret->ocsp_response, &ret->ocsp_response_length, |
| 647 | kOCSPResponseTag)) { |
| 648 | goto err; |
| 649 | } |
| 650 | |
| 651 | int extended_master_secret; |
| 652 | if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret, |
| 653 | kExtendedMasterSecretTag, |
| 654 | 0 /* default to false */)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 655 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 656 | goto err; |
| 657 | } |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 658 | ret->extended_master_secret = !!extended_master_secret; |
| 659 | |
| 660 | if (!SSL_SESSION_parse_u32(&session, &ret->key_exchange_info, |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 661 | kKeyExchangeInfoTag, 0)) { |
| 662 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 663 | goto err; |
| 664 | } |
| 665 | |
| 666 | CBS cert_chain; |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 667 | CBS_init(&cert_chain, NULL, 0); |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 668 | int has_cert_chain; |
| 669 | if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain, |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 670 | kCertChainTag) || |
| 671 | (has_cert_chain && CBS_len(&cert_chain) == 0)) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 672 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 673 | goto err; |
| 674 | } |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 675 | sk_CRYPTO_BUFFER_pop_free(ret->certs, CRYPTO_BUFFER_free); |
| 676 | ret->certs = NULL; |
| 677 | |
| 678 | int x509_chain_should_include_leaf_default = 0; |
| 679 | |
| 680 | if (has_peer || has_cert_chain) { |
| 681 | ret->certs = sk_CRYPTO_BUFFER_new_null(); |
| 682 | if (ret->certs == NULL) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 683 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 684 | goto err; |
| 685 | } |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 686 | |
| 687 | if (has_peer) { |
| 688 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&peer, NULL); |
| 689 | if (buffer == NULL) { |
| 690 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 691 | goto err; |
| 692 | } |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 693 | |
| 694 | if (!sk_CRYPTO_BUFFER_push(ret->certs, buffer)) { |
| 695 | CRYPTO_BUFFER_free(buffer); |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 696 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 697 | goto err; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | size_t chain_i = 0; |
| 702 | while (CBS_len(&cert_chain) > 0) { |
| 703 | chain_i++; |
| 704 | |
| 705 | CBS cert; |
| 706 | if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) || |
| 707 | CBS_len(&cert) == 0) { |
| 708 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 709 | goto err; |
| 710 | } |
| 711 | |
| 712 | if (has_peer && chain_i == 1) { |
| 713 | /* If a certificate was parsed from |peer| then it may, or may not, be |
| 714 | * duplicated as the first element of |cert_chain|. */ |
| 715 | if (CBS_len(&peer) == CBS_len(&cert) && |
| 716 | memcmp(CBS_data(&peer), CBS_data(&cert), CBS_len(&peer)) == 0) { |
| 717 | x509_chain_should_include_leaf_default = 1; |
| 718 | continue; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, NULL); |
| 723 | if (buffer == NULL) { |
| 724 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 725 | goto err; |
| 726 | } |
| 727 | |
| 728 | if (!sk_CRYPTO_BUFFER_push(ret->certs, buffer)) { |
| 729 | CRYPTO_BUFFER_free(buffer); |
| 730 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 731 | goto err; |
| 732 | } |
| 733 | } |
| 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) { |
| 743 | goto err; |
| 744 | } |
| 745 | ret->ticket_age_add_valid = age_add_present; |
| 746 | |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 747 | int x509_chain_should_include_leaf; |
| 748 | if (!CBS_get_optional_asn1_bool(&session, &x509_chain_should_include_leaf, |
| 749 | kX509ChainIncludeLeafTag, |
| 750 | x509_chain_should_include_leaf_default)) { |
| 751 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 752 | goto err; |
| 753 | } |
| 754 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 755 | if (CBS_len(&session) != 0) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 756 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 757 | goto err; |
| 758 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 759 | |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame^] | 760 | if (!x509_chain_from_buffers(&ret->x509_chain, ret->certs)) { |
| 761 | goto err; |
| 762 | } |
| 763 | |
| 764 | ssl_session_set_x509_peer(ret, x509_chain_should_include_leaf); |
| 765 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 766 | return ret; |
| 767 | |
| 768 | err: |
| 769 | SSL_SESSION_free(ret); |
| 770 | return NULL; |
| 771 | } |
| 772 | |
| 773 | SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) { |
| 774 | CBS cbs; |
| 775 | CBS_init(&cbs, in, in_len); |
| 776 | SSL_SESSION *ret = SSL_SESSION_parse(&cbs); |
| 777 | if (ret == NULL) { |
| 778 | return NULL; |
| 779 | } |
| 780 | if (CBS_len(&cbs) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 781 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 782 | SSL_SESSION_free(ret); |
| 783 | return NULL; |
| 784 | } |
| 785 | return ret; |
| 786 | } |
| 787 | |
| 788 | SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) { |
| 789 | if (length < 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 790 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 791 | return NULL; |
| 792 | } |
| 793 | |
| 794 | CBS cbs; |
| 795 | CBS_init(&cbs, *pp, length); |
| 796 | |
| 797 | SSL_SESSION *ret = SSL_SESSION_parse(&cbs); |
| 798 | if (ret == NULL) { |
| 799 | return NULL; |
| 800 | } |
| 801 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 802 | if (a) { |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 803 | SSL_SESSION_free(*a); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 804 | *a = ret; |
| 805 | } |
| 806 | *pp = CBS_data(&cbs); |
| 807 | return ret; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 808 | } |