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