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 | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame^] | 125 | * isServer [22] BOOLEAN DEFAULT TRUE, |
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 | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 136 | */ |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 137 | |
Adam Langley | 96c2a28 | 2015-06-02 14:16:44 -0700 | [diff] [blame] | 138 | static const unsigned kVersion = 1; |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 139 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 140 | static const int kTimeTag = |
| 141 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
| 142 | static const int kTimeoutTag = |
| 143 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2; |
| 144 | static const int kPeerTag = |
| 145 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3; |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 146 | static const int kSessionIDContextTag = |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 147 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4; |
| 148 | static const int kVerifyResultTag = |
| 149 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5; |
| 150 | static const int kHostNameTag = |
| 151 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 152 | static const int kPSKIdentityTag = |
| 153 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8; |
| 154 | static const int kTicketLifetimeHintTag = |
| 155 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9; |
| 156 | static const int kTicketTag = |
| 157 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10; |
| 158 | static const int kPeerSHA256Tag = |
| 159 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13; |
| 160 | static const int kOriginalHandshakeHashTag = |
| 161 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14; |
| 162 | static const int kSignedCertTimestampListTag = |
| 163 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15; |
| 164 | static const int kOCSPResponseTag = |
| 165 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 166 | static const int kExtendedMasterSecretTag = |
| 167 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17; |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 168 | static const int kKeyExchangeInfoTag = |
| 169 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 170 | static const int kCertChainTag = |
| 171 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 172 | static const int kTicketAgeAddTag = |
| 173 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame^] | 174 | static const int kIsServerTag = |
| 175 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 176 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 177 | 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] | 178 | size_t *out_len, int for_ticket) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 179 | CBB cbb, session, child, child2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 180 | |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 181 | if (in == NULL || in->cipher == NULL) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 182 | return 0; |
| 183 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 185 | CBB_zero(&cbb); |
| 186 | if (!CBB_init(&cbb, 0) || |
| 187 | !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) || |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 188 | !CBB_add_asn1_uint64(&session, kVersion) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 189 | !CBB_add_asn1_uint64(&session, in->ssl_version) || |
| 190 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 191 | !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 192 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 193 | /* The session ID is irrelevant for a session ticket. */ |
| 194 | !CBB_add_bytes(&child, in->session_id, |
| 195 | for_ticket ? 0 : in->session_id_length) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 196 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
| 197 | !CBB_add_bytes(&child, in->master_key, in->master_key_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 198 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 199 | goto err; |
| 200 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 202 | if (in->time < 0) { |
| 203 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 204 | goto err; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 205 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 206 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 207 | if (!CBB_add_asn1(&session, &child, kTimeTag) || |
| 208 | !CBB_add_asn1_uint64(&child, in->time)) { |
| 209 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 210 | goto err; |
| 211 | } |
David Benjamin | 0f31ac7 | 2016-11-02 23:05:14 -0400 | [diff] [blame] | 212 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 213 | if (in->timeout < 0) { |
| 214 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 215 | goto err; |
| 216 | } |
| 217 | |
| 218 | if (!CBB_add_asn1(&session, &child, kTimeoutTag) || |
| 219 | !CBB_add_asn1_uint64(&child, in->timeout)) { |
| 220 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 221 | goto err; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 222 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 223 | |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 224 | /* The peer certificate is only serialized if the SHA-256 isn't |
| 225 | * serialized instead. */ |
| 226 | if (in->x509_peer && !in->peer_sha256_valid) { |
| 227 | if (!CBB_add_asn1(&session, &child, kPeerTag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 228 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 229 | goto err; |
| 230 | } |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 231 | if (!ssl_add_cert_to_cbb(&child, in->x509_peer)) { |
| 232 | goto err; |
| 233 | } |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 234 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 235 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 236 | /* Although it is OPTIONAL and usually empty, OpenSSL has |
| 237 | * historically always encoded the sid_ctx. */ |
| 238 | if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) || |
| 239 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 240 | !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 241 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 242 | goto err; |
| 243 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 244 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 245 | if (in->verify_result != X509_V_OK) { |
| 246 | if (!CBB_add_asn1(&session, &child, kVerifyResultTag) || |
| 247 | !CBB_add_asn1_uint64(&child, in->verify_result)) { |
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 | } |
| 251 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 252 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 253 | if (in->tlsext_hostname) { |
| 254 | if (!CBB_add_asn1(&session, &child, kHostNameTag) || |
| 255 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 256 | !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname, |
| 257 | strlen(in->tlsext_hostname))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 258 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 259 | goto err; |
| 260 | } |
| 261 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 263 | if (in->psk_identity) { |
| 264 | if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) || |
| 265 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 266 | !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity, |
| 267 | strlen(in->psk_identity))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 268 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 269 | goto err; |
| 270 | } |
| 271 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 272 | |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 273 | if (in->tlsext_tick_lifetime_hint > 0) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 274 | if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) || |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 275 | !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 276 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 277 | goto err; |
| 278 | } |
| 279 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 280 | |
David Benjamin | 95d3182 | 2015-06-15 19:53:32 -0400 | [diff] [blame] | 281 | if (in->tlsext_tick && !for_ticket) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 282 | if (!CBB_add_asn1(&session, &child, kTicketTag) || |
| 283 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 284 | !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 285 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 286 | goto err; |
| 287 | } |
| 288 | } |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 289 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 290 | if (in->peer_sha256_valid) { |
| 291 | if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) || |
| 292 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 293 | !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 294 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 295 | goto err; |
| 296 | } |
| 297 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 298 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 299 | if (in->original_handshake_hash_len > 0) { |
| 300 | if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) || |
| 301 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 302 | !CBB_add_bytes(&child2, in->original_handshake_hash, |
| 303 | in->original_handshake_hash_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 304 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 305 | goto err; |
| 306 | } |
| 307 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 308 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 309 | if (in->tlsext_signed_cert_timestamp_list_length > 0) { |
| 310 | if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) || |
| 311 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 312 | !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list, |
| 313 | in->tlsext_signed_cert_timestamp_list_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 314 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 315 | goto err; |
| 316 | } |
| 317 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 318 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 319 | if (in->ocsp_response_length > 0) { |
| 320 | if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) || |
| 321 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 322 | !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 323 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 324 | goto err; |
| 325 | } |
| 326 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 327 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 328 | if (in->extended_master_secret) { |
| 329 | if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) || |
| 330 | !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 331 | !CBB_add_u8(&child2, 0xff)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 332 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 333 | goto err; |
| 334 | } |
| 335 | } |
| 336 | |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 337 | if (in->key_exchange_info > 0 && |
| 338 | (!CBB_add_asn1(&session, &child, kKeyExchangeInfoTag) || |
| 339 | !CBB_add_asn1_uint64(&child, in->key_exchange_info))) { |
| 340 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 341 | goto err; |
| 342 | } |
| 343 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 344 | /* The certificate chain is only serialized if the leaf's SHA-256 isn't |
| 345 | * serialized instead. */ |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame^] | 346 | if (in->x509_chain != NULL && !in->peer_sha256_valid && |
| 347 | sk_X509_num(in->x509_chain) >= 2) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 348 | if (!CBB_add_asn1(&session, &child, kCertChainTag)) { |
| 349 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 350 | goto err; |
| 351 | } |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame^] | 352 | for (size_t i = 1; i < sk_X509_num(in->x509_chain); i++) { |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 353 | if (!ssl_add_cert_to_cbb(&child, sk_X509_value(in->x509_chain, i))) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 354 | goto err; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 359 | if (in->ticket_age_add_valid) { |
| 360 | if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) || |
| 361 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 362 | !CBB_add_u32(&child2, in->ticket_age_add)) { |
| 363 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 364 | goto err; |
| 365 | } |
| 366 | } |
| 367 | |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame^] | 368 | if (!in->is_server) { |
| 369 | if (!CBB_add_asn1(&session, &child, kIsServerTag) || |
| 370 | !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 371 | !CBB_add_u8(&child2, 0x00)) { |
| 372 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 373 | goto err; |
| 374 | } |
| 375 | } |
| 376 | |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 377 | if (!CBB_finish(&cbb, out_data, out_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 378 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 379 | goto err; |
| 380 | } |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 381 | return 1; |
| 382 | |
| 383 | err: |
| 384 | CBB_cleanup(&cbb); |
| 385 | return 0; |
| 386 | } |
| 387 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 388 | int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data, |
| 389 | size_t *out_len) { |
Steven Valdez | 84b5c00 | 2016-08-25 16:30:58 -0400 | [diff] [blame] | 390 | if (in->not_resumable) { |
| 391 | /* If the caller has an unresumable session, e.g. if |SSL_get_session| were |
| 392 | * called on a TLS 1.3 or False Started connection, serialize with a |
| 393 | * placeholder value so it is not accidentally deserialized into a resumable |
| 394 | * one. */ |
| 395 | static const char kNotResumableSession[] = "NOT RESUMABLE"; |
| 396 | |
| 397 | *out_len = strlen(kNotResumableSession); |
| 398 | *out_data = BUF_memdup(kNotResumableSession, *out_len); |
| 399 | if (*out_data == NULL) { |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | return 1; |
| 404 | } |
| 405 | |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 406 | return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0); |
| 407 | } |
| 408 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 409 | 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] | 410 | size_t *out_len) { |
| 411 | return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1); |
| 412 | } |
| 413 | |
| 414 | int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) { |
| 415 | uint8_t *out; |
| 416 | size_t len; |
| 417 | |
| 418 | if (!SSL_SESSION_to_bytes(in, &out, &len)) { |
| 419 | return -1; |
| 420 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 421 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 422 | if (len > INT_MAX) { |
| 423 | OPENSSL_free(out); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 424 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 425 | return -1; |
| 426 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 427 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 428 | if (pp) { |
| 429 | memcpy(*pp, out, len); |
| 430 | *pp += len; |
| 431 | } |
| 432 | OPENSSL_free(out); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 433 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 434 | return len; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 435 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 436 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 437 | /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 438 | * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On |
| 439 | * entry, if |*out| is not NULL, it frees the existing contents. If |
| 440 | * the element was not found, it sets |*out| to NULL. It returns one |
| 441 | * on success, whether or not the element was found, and zero on |
| 442 | * decode error. */ |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 443 | static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 444 | CBS value; |
| 445 | int present; |
| 446 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 447 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | if (present) { |
| 451 | if (CBS_contains_zero_byte(&value)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 452 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 453 | return 0; |
| 454 | } |
| 455 | if (!CBS_strdup(&value, out)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 456 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 457 | return 0; |
| 458 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 459 | } else { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 460 | OPENSSL_free(*out); |
| 461 | *out = NULL; |
| 462 | } |
| 463 | return 1; |
| 464 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 465 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 466 | /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 467 | * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr| |
| 468 | * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing |
| 469 | * contents. On entry, if the element was not found, it sets |
| 470 | * |*out_ptr| to NULL. It returns one on success, whether or not the |
| 471 | * element was found, and zero on decode error. */ |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 472 | 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] | 473 | size_t *out_len, unsigned tag) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 474 | CBS value; |
| 475 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 476 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | if (!CBS_stow(&value, out_ptr, out_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 480 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 481 | return 0; |
| 482 | } |
| 483 | return 1; |
| 484 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 485 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 486 | /* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING |
| 487 | * explicitly tagged with |tag| of size at most |max_out|. */ |
| 488 | static int SSL_SESSION_parse_bounded_octet_string( |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 489 | 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] | 490 | CBS value; |
| 491 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) || |
| 492 | CBS_len(&value) > max_out) { |
| 493 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 494 | return 0; |
| 495 | } |
| 496 | memcpy(out, CBS_data(&value), CBS_len(&value)); |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 497 | *out_len = (uint8_t)CBS_len(&value); |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 498 | return 1; |
| 499 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 500 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 501 | static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag, |
| 502 | long default_value) { |
| 503 | uint64_t value; |
| 504 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 505 | (uint64_t)default_value) || |
| 506 | value > LONG_MAX) { |
| 507 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 508 | return 0; |
| 509 | } |
| 510 | *out = (long)value; |
| 511 | return 1; |
| 512 | } |
| 513 | |
| 514 | static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag, |
| 515 | uint32_t default_value) { |
| 516 | uint64_t value; |
| 517 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 518 | (uint64_t)default_value) || |
| 519 | value > 0xffffffff) { |
| 520 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 521 | return 0; |
| 522 | } |
| 523 | *out = (uint32_t)value; |
| 524 | return 1; |
| 525 | } |
| 526 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 527 | static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) { |
| 528 | SSL_SESSION *ret = SSL_SESSION_new(); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 529 | if (ret == NULL) { |
| 530 | goto err; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 531 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 532 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 533 | CBS session; |
| 534 | uint64_t version, ssl_version; |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 535 | if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) || |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 536 | !CBS_get_asn1_uint64(&session, &version) || |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 537 | version != kVersion || |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 538 | !CBS_get_asn1_uint64(&session, &ssl_version)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 539 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 540 | goto err; |
| 541 | } |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 542 | ret->ssl_version = ssl_version; |
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 cipher; |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 545 | uint16_t cipher_value; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 546 | if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) || |
| 547 | !CBS_get_u16(&cipher, &cipher_value) || |
| 548 | CBS_len(&cipher) != 0) { |
| 549 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 550 | goto err; |
| 551 | } |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 552 | ret->cipher = SSL_get_cipher_by_value(cipher_value); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 553 | if (ret->cipher == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 554 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 555 | goto err; |
| 556 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 557 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 558 | CBS session_id, master_key; |
| 559 | if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) || |
| 560 | CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH || |
| 561 | !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) || |
| 562 | CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 563 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 564 | goto err; |
| 565 | } |
| 566 | memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id)); |
| 567 | ret->session_id_length = CBS_len(&session_id); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 568 | memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key)); |
| 569 | ret->master_key_length = CBS_len(&master_key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 570 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 571 | CBS child; |
| 572 | uint64_t time, timeout; |
| 573 | if (!CBS_get_asn1(&session, &child, kTimeTag) || |
| 574 | !CBS_get_asn1_uint64(&child, &time) || |
| 575 | time > LONG_MAX || |
| 576 | !CBS_get_asn1(&session, &child, kTimeoutTag) || |
| 577 | !CBS_get_asn1_uint64(&child, &timeout) || |
| 578 | timeout > LONG_MAX) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 579 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 580 | goto err; |
| 581 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 582 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 583 | ret->time = (long)time; |
| 584 | ret->timeout = (long)timeout; |
| 585 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 586 | CBS peer; |
| 587 | int has_peer; |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 588 | if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag)) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 589 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 590 | goto err; |
| 591 | } |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 592 | X509_free(ret->x509_peer); |
| 593 | ret->x509_peer = NULL; |
| 594 | if (has_peer) { |
| 595 | ret->x509_peer = ssl_parse_x509(&peer); |
| 596 | if (ret->x509_peer == NULL) { |
| 597 | goto err; |
| 598 | } |
| 599 | if (CBS_len(&peer) != 0) { |
| 600 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 601 | goto err; |
| 602 | } |
| 603 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 604 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 605 | if (!SSL_SESSION_parse_bounded_octet_string( |
| 606 | &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx), |
| 607 | kSessionIDContextTag) || |
| 608 | !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag, |
| 609 | X509_V_OK) || |
| 610 | !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname, |
| 611 | kHostNameTag) || |
| 612 | !SSL_SESSION_parse_string(&session, &ret->psk_identity, |
| 613 | kPSKIdentityTag) || |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 614 | !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint, |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 615 | kTicketLifetimeHintTag, 0) || |
| 616 | !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick, |
| 617 | &ret->tlsext_ticklen, kTicketTag)) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 618 | goto err; |
| 619 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 620 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 621 | if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) { |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 622 | CBS peer_sha256; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 623 | if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) || |
| 624 | !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) || |
| 625 | CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) || |
| 626 | CBS_len(&child) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 627 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 628 | goto err; |
| 629 | } |
| 630 | memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256)); |
| 631 | ret->peer_sha256_valid = 1; |
| 632 | } else { |
| 633 | ret->peer_sha256_valid = 0; |
| 634 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 635 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 636 | if (!SSL_SESSION_parse_bounded_octet_string( |
| 637 | &session, ret->original_handshake_hash, |
| 638 | &ret->original_handshake_hash_len, |
| 639 | sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) || |
| 640 | !SSL_SESSION_parse_octet_string( |
| 641 | &session, &ret->tlsext_signed_cert_timestamp_list, |
| 642 | &ret->tlsext_signed_cert_timestamp_list_length, |
| 643 | kSignedCertTimestampListTag) || |
| 644 | !SSL_SESSION_parse_octet_string( |
| 645 | &session, &ret->ocsp_response, &ret->ocsp_response_length, |
| 646 | kOCSPResponseTag)) { |
| 647 | goto err; |
| 648 | } |
| 649 | |
| 650 | int extended_master_secret; |
| 651 | if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret, |
| 652 | kExtendedMasterSecretTag, |
| 653 | 0 /* default to false */)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 654 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 655 | goto err; |
| 656 | } |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 657 | ret->extended_master_secret = !!extended_master_secret; |
| 658 | |
| 659 | if (!SSL_SESSION_parse_u32(&session, &ret->key_exchange_info, |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 660 | kKeyExchangeInfoTag, 0)) { |
| 661 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 662 | goto err; |
| 663 | } |
| 664 | |
| 665 | CBS cert_chain; |
| 666 | int has_cert_chain; |
| 667 | if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain, |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 668 | kCertChainTag)) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 669 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 670 | goto err; |
| 671 | } |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 672 | sk_X509_pop_free(ret->x509_chain, X509_free); |
| 673 | ret->x509_chain = NULL; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame^] | 674 | if (ret->x509_peer != NULL) { |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 675 | ret->x509_chain = sk_X509_new_null(); |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame^] | 676 | if (ret->x509_chain == NULL || |
| 677 | !sk_X509_push(ret->x509_chain, ret->x509_peer)) { |
| 678 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 679 | goto err; |
| 680 | } |
| 681 | X509_up_ref(ret->x509_peer); |
| 682 | } |
| 683 | if (has_cert_chain) { |
| 684 | if (ret->x509_peer == NULL) { |
| 685 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 686 | goto err; |
| 687 | } |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 688 | if (ret->x509_chain == NULL) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 689 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 690 | goto err; |
| 691 | } |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame] | 692 | while (CBS_len(&cert_chain) > 0) { |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 693 | X509 *x509 = ssl_parse_x509(&cert_chain); |
| 694 | if (x509 == NULL) { |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame] | 695 | goto err; |
| 696 | } |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 697 | if (!sk_X509_push(ret->x509_chain, x509)) { |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame] | 698 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 699 | X509_free(x509); |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 700 | goto err; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 705 | CBS age_add; |
| 706 | int age_add_present; |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 707 | 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] | 708 | kTicketAgeAddTag) || |
| 709 | (age_add_present && |
| 710 | !CBS_get_u32(&age_add, &ret->ticket_age_add)) || |
| 711 | CBS_len(&age_add) != 0) { |
| 712 | goto err; |
| 713 | } |
| 714 | ret->ticket_age_add_valid = age_add_present; |
| 715 | |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame^] | 716 | int is_server; |
| 717 | if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag, |
| 718 | 1 /* default to true */)) { |
| 719 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 720 | goto err; |
| 721 | } |
| 722 | /* TODO: in time we can include |is_server| for servers too, then we can |
| 723 | enforce that client and server sessions are never mixed up. */ |
| 724 | |
| 725 | ret->is_server = is_server; |
| 726 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 727 | if (CBS_len(&session) != 0) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 728 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 729 | goto err; |
| 730 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 731 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 732 | return ret; |
| 733 | |
| 734 | err: |
| 735 | SSL_SESSION_free(ret); |
| 736 | return NULL; |
| 737 | } |
| 738 | |
| 739 | SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) { |
| 740 | CBS cbs; |
| 741 | CBS_init(&cbs, in, in_len); |
| 742 | SSL_SESSION *ret = SSL_SESSION_parse(&cbs); |
| 743 | if (ret == NULL) { |
| 744 | return NULL; |
| 745 | } |
| 746 | if (CBS_len(&cbs) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 747 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 748 | SSL_SESSION_free(ret); |
| 749 | return NULL; |
| 750 | } |
| 751 | return ret; |
| 752 | } |
| 753 | |
| 754 | SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) { |
| 755 | if (length < 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 756 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 757 | return NULL; |
| 758 | } |
| 759 | |
| 760 | CBS cbs; |
| 761 | CBS_init(&cbs, *pp, length); |
| 762 | |
| 763 | SSL_SESSION *ret = SSL_SESSION_parse(&cbs); |
| 764 | if (ret == NULL) { |
| 765 | return NULL; |
| 766 | } |
| 767 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 768 | if (a) { |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 769 | SSL_SESSION_free(*a); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 770 | *a = ret; |
| 771 | } |
| 772 | *pp = CBS_data(&cbs); |
| 773 | return ret; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 774 | } |