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 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 88 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 89 | #include <openssl/err.h> |
Brian Smith | 054e682 | 2015-03-27 21:12:01 -1000 | [diff] [blame] | 90 | #include <openssl/mem.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 91 | #include <openssl/x509.h> |
| 92 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 93 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 94 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 95 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 96 | /* An SSL_SESSION is serialized as the following ASN.1 structure: |
| 97 | * |
| 98 | * SSLSession ::= SEQUENCE { |
| 99 | * version INTEGER (1), -- ignored |
| 100 | * sslVersion INTEGER, -- protocol version number |
| 101 | * cipher OCTET STRING, -- two bytes long |
| 102 | * sessionID OCTET STRING, |
| 103 | * masterKey OCTET STRING, |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 104 | * time [1] INTEGER OPTIONAL, -- seconds since UNIX epoch |
| 105 | * timeout [2] INTEGER OPTIONAL, -- in seconds |
| 106 | * peer [3] Certificate OPTIONAL, |
| 107 | * sessionIDContext [4] OCTET STRING OPTIONAL, |
| 108 | * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes |
| 109 | * hostName [6] OCTET STRING OPTIONAL, |
| 110 | * -- from server_name extension |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 111 | * pskIdentity [8] OCTET STRING OPTIONAL, |
| 112 | * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only |
| 113 | * ticket [10] OCTET STRING OPTIONAL, -- client-only |
| 114 | * peerSHA256 [13] OCTET STRING OPTIONAL, |
| 115 | * originalHandshakeHash [14] OCTET STRING OPTIONAL, |
| 116 | * signedCertTimestampList [15] OCTET STRING OPTIONAL, |
| 117 | * -- contents of SCT extension |
| 118 | * ocspResponse [16] OCTET STRING OPTIONAL, |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 119 | * -- stapled OCSP response from the server |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 120 | * extendedMasterSecret [17] BOOLEAN OPTIONAL, |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 121 | * keyExchangeInfo [18] INTEGER OPTIONAL, |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 122 | * certChain [19] SEQUENCE OF Certificate OPTIONAL, |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 123 | * } |
| 124 | * |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 125 | * Note: historically this serialization has included other optional |
| 126 | * fields. Their presense is currently treated as a parse error: |
| 127 | * |
| 128 | * keyArg [0] IMPLICIT OCTET STRING OPTIONAL, |
| 129 | * pskIdentityHint [7] OCTET STRING OPTIONAL, |
| 130 | * compressionMethod [11] OCTET STRING OPTIONAL, |
| 131 | * srpUsername [12] OCTET STRING OPTIONAL, */ |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 132 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 133 | static const int kTimeTag = |
| 134 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
| 135 | static const int kTimeoutTag = |
| 136 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2; |
| 137 | static const int kPeerTag = |
| 138 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3; |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 139 | static const int kSessionIDContextTag = |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 140 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4; |
| 141 | static const int kVerifyResultTag = |
| 142 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5; |
| 143 | static const int kHostNameTag = |
| 144 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 145 | static const int kPSKIdentityTag = |
| 146 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8; |
| 147 | static const int kTicketLifetimeHintTag = |
| 148 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9; |
| 149 | static const int kTicketTag = |
| 150 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10; |
| 151 | static const int kPeerSHA256Tag = |
| 152 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13; |
| 153 | static const int kOriginalHandshakeHashTag = |
| 154 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14; |
| 155 | static const int kSignedCertTimestampListTag = |
| 156 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15; |
| 157 | static const int kOCSPResponseTag = |
| 158 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 159 | static const int kExtendedMasterSecretTag = |
| 160 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17; |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 161 | static const int kKeyExchangeInfoTag = |
| 162 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 163 | static const int kCertChainTag = |
| 164 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19; |
| 165 | |
| 166 | static int add_X509(CBB *cbb, X509 *x509) { |
| 167 | int len = i2d_X509(x509, NULL); |
| 168 | if (len < 0) { |
| 169 | return 0; |
| 170 | } |
| 171 | uint8_t *buf; |
| 172 | if (!CBB_add_space(cbb, &buf, len)) { |
| 173 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 174 | return 0; |
| 175 | } |
| 176 | if (buf != NULL && i2d_X509(x509, &buf) < 0) { |
| 177 | return 0; |
| 178 | } |
| 179 | return 1; |
| 180 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 181 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 182 | 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] | 183 | size_t *out_len, int for_ticket) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 184 | CBB cbb, session, child, child2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 185 | |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 186 | if (in == NULL || in->cipher == NULL) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 187 | return 0; |
| 188 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 189 | |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 190 | CBB_zero(&cbb); |
| 191 | if (!CBB_init(&cbb, 0) || |
| 192 | !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 193 | !CBB_add_asn1_uint64(&session, SSL_SESSION_ASN1_VERSION) || |
| 194 | !CBB_add_asn1_uint64(&session, in->ssl_version) || |
| 195 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 196 | !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 197 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 198 | /* The session ID is irrelevant for a session ticket. */ |
| 199 | !CBB_add_bytes(&child, in->session_id, |
| 200 | for_ticket ? 0 : in->session_id_length) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 201 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
| 202 | !CBB_add_bytes(&child, in->master_key, in->master_key_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 203 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 204 | goto err; |
| 205 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 206 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 207 | if (in->time != 0) { |
| 208 | if (!CBB_add_asn1(&session, &child, kTimeTag) || |
| 209 | !CBB_add_asn1_uint64(&child, in->time)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 210 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 211 | goto err; |
| 212 | } |
| 213 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 215 | if (in->timeout != 0) { |
| 216 | if (!CBB_add_asn1(&session, &child, kTimeoutTag) || |
| 217 | !CBB_add_asn1_uint64(&child, in->timeout)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 218 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 219 | goto err; |
| 220 | } |
| 221 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 223 | /* The peer certificate is only serialized if the SHA-256 isn't |
| 224 | * serialized instead. */ |
| 225 | if (in->peer && !in->peer_sha256_valid) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 226 | if (!CBB_add_asn1(&session, &child, kPeerTag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 227 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 228 | goto err; |
| 229 | } |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 230 | if (!add_X509(&child, in->peer)) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 231 | goto err; |
| 232 | } |
| 233 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 234 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 235 | /* Although it is OPTIONAL and usually empty, OpenSSL has |
| 236 | * historically always encoded the sid_ctx. */ |
| 237 | if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) || |
| 238 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 239 | !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 240 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 241 | goto err; |
| 242 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 243 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 244 | if (in->verify_result != X509_V_OK) { |
| 245 | if (!CBB_add_asn1(&session, &child, kVerifyResultTag) || |
| 246 | !CBB_add_asn1_uint64(&child, in->verify_result)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 247 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 248 | goto err; |
| 249 | } |
| 250 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 251 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 252 | if (in->tlsext_hostname) { |
| 253 | if (!CBB_add_asn1(&session, &child, kHostNameTag) || |
| 254 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 255 | !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname, |
| 256 | strlen(in->tlsext_hostname))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 257 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 258 | goto err; |
| 259 | } |
| 260 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 261 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 262 | if (in->psk_identity) { |
| 263 | if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) || |
| 264 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 265 | !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity, |
| 266 | strlen(in->psk_identity))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 267 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 268 | goto err; |
| 269 | } |
| 270 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 271 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 272 | if (in->tlsext_tick_lifetime_hint > 0) { |
| 273 | if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) || |
| 274 | !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 275 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 276 | goto err; |
| 277 | } |
| 278 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 279 | |
David Benjamin | 95d3182 | 2015-06-15 19:53:32 -0400 | [diff] [blame] | 280 | if (in->tlsext_tick && !for_ticket) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 281 | if (!CBB_add_asn1(&session, &child, kTicketTag) || |
| 282 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 283 | !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 284 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 285 | goto err; |
| 286 | } |
| 287 | } |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 288 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 289 | if (in->peer_sha256_valid) { |
| 290 | if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) || |
| 291 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 292 | !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 293 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 294 | goto err; |
| 295 | } |
| 296 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 297 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 298 | if (in->original_handshake_hash_len > 0) { |
| 299 | if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) || |
| 300 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 301 | !CBB_add_bytes(&child2, in->original_handshake_hash, |
| 302 | in->original_handshake_hash_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 303 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 304 | goto err; |
| 305 | } |
| 306 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 307 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 308 | if (in->tlsext_signed_cert_timestamp_list_length > 0) { |
| 309 | if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) || |
| 310 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 311 | !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list, |
| 312 | in->tlsext_signed_cert_timestamp_list_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 313 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 314 | goto err; |
| 315 | } |
| 316 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 317 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 318 | if (in->ocsp_response_length > 0) { |
| 319 | if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) || |
| 320 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 321 | !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 322 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 323 | goto err; |
| 324 | } |
| 325 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 326 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 327 | if (in->extended_master_secret) { |
| 328 | if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) || |
| 329 | !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 330 | !CBB_add_u8(&child2, 0xff)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 331 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 332 | goto err; |
| 333 | } |
| 334 | } |
| 335 | |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 336 | if (in->key_exchange_info > 0 && |
| 337 | (!CBB_add_asn1(&session, &child, kKeyExchangeInfoTag) || |
| 338 | !CBB_add_asn1_uint64(&child, in->key_exchange_info))) { |
| 339 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 340 | goto err; |
| 341 | } |
| 342 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 343 | /* The certificate chain is only serialized if the leaf's SHA-256 isn't |
| 344 | * serialized instead. */ |
| 345 | if (in->cert_chain != NULL && !in->peer_sha256_valid) { |
| 346 | if (!CBB_add_asn1(&session, &child, kCertChainTag)) { |
| 347 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 348 | goto err; |
| 349 | } |
| 350 | size_t i; |
| 351 | for (i = 0; i < sk_X509_num(in->cert_chain); i++) { |
| 352 | if (!add_X509(&child, sk_X509_value(in->cert_chain, i))) { |
| 353 | goto err; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 358 | if (!CBB_finish(&cbb, out_data, out_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 359 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 360 | goto err; |
| 361 | } |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 362 | return 1; |
| 363 | |
| 364 | err: |
| 365 | CBB_cleanup(&cbb); |
| 366 | return 0; |
| 367 | } |
| 368 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 369 | int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data, |
| 370 | size_t *out_len) { |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 371 | return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0); |
| 372 | } |
| 373 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 374 | 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] | 375 | size_t *out_len) { |
| 376 | return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1); |
| 377 | } |
| 378 | |
| 379 | int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) { |
| 380 | uint8_t *out; |
| 381 | size_t len; |
| 382 | |
| 383 | if (!SSL_SESSION_to_bytes(in, &out, &len)) { |
| 384 | return -1; |
| 385 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 386 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 387 | if (len > INT_MAX) { |
| 388 | OPENSSL_free(out); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 389 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 390 | return -1; |
| 391 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 393 | if (pp) { |
| 394 | memcpy(*pp, out, len); |
| 395 | *pp += len; |
| 396 | } |
| 397 | OPENSSL_free(out); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 398 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 399 | return len; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 400 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 401 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 402 | /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 403 | * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On |
| 404 | * entry, if |*out| is not NULL, it frees the existing contents. If |
| 405 | * the element was not found, it sets |*out| to NULL. It returns one |
| 406 | * on success, whether or not the element was found, and zero on |
| 407 | * decode error. */ |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 408 | static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 409 | CBS value; |
| 410 | int present; |
| 411 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 412 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | if (present) { |
| 416 | if (CBS_contains_zero_byte(&value)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 417 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | if (!CBS_strdup(&value, out)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 421 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 422 | return 0; |
| 423 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 424 | } else { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 425 | OPENSSL_free(*out); |
| 426 | *out = NULL; |
| 427 | } |
| 428 | return 1; |
| 429 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 430 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 431 | /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 432 | * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr| |
| 433 | * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing |
| 434 | * contents. On entry, if the element was not found, it sets |
| 435 | * |*out_ptr| to NULL. It returns one on success, whether or not the |
| 436 | * element was found, and zero on decode error. */ |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 437 | 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] | 438 | size_t *out_len, unsigned tag) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 439 | CBS value; |
| 440 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 441 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | if (!CBS_stow(&value, out_ptr, out_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 445 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 446 | return 0; |
| 447 | } |
| 448 | return 1; |
| 449 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 450 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 451 | /* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING |
| 452 | * explicitly tagged with |tag| of size at most |max_out|. */ |
| 453 | static int SSL_SESSION_parse_bounded_octet_string( |
| 454 | CBS *cbs, uint8_t *out, unsigned *out_len, unsigned max_out, unsigned tag) { |
| 455 | CBS value; |
| 456 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) || |
| 457 | CBS_len(&value) > max_out) { |
| 458 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 459 | return 0; |
| 460 | } |
| 461 | memcpy(out, CBS_data(&value), CBS_len(&value)); |
| 462 | *out_len = (unsigned)CBS_len(&value); |
| 463 | return 1; |
| 464 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 465 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 466 | static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag, |
| 467 | long default_value) { |
| 468 | uint64_t value; |
| 469 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 470 | (uint64_t)default_value) || |
| 471 | value > LONG_MAX) { |
| 472 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 473 | return 0; |
| 474 | } |
| 475 | *out = (long)value; |
| 476 | return 1; |
| 477 | } |
| 478 | |
| 479 | static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag, |
| 480 | uint32_t default_value) { |
| 481 | uint64_t value; |
| 482 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 483 | (uint64_t)default_value) || |
| 484 | value > 0xffffffff) { |
| 485 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 486 | return 0; |
| 487 | } |
| 488 | *out = (uint32_t)value; |
| 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | static X509 *parse_x509(CBS *cbs) { |
| 493 | const uint8_t *ptr = CBS_data(cbs); |
| 494 | X509 *ret = d2i_X509(NULL, &ptr, CBS_len(cbs)); |
| 495 | if (ret == NULL) { |
| 496 | return NULL; |
| 497 | } |
| 498 | CBS_skip(cbs, ptr - CBS_data(cbs)); |
| 499 | return ret; |
| 500 | } |
| 501 | |
| 502 | static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) { |
| 503 | SSL_SESSION *ret = SSL_SESSION_new(); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 504 | if (ret == NULL) { |
| 505 | goto err; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 506 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 507 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 508 | CBS session; |
| 509 | uint64_t version, ssl_version; |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 510 | if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) || |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 511 | !CBS_get_asn1_uint64(&session, &version) || |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 512 | version != SSL_SESSION_ASN1_VERSION || |
| 513 | !CBS_get_asn1_uint64(&session, &ssl_version)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 514 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 515 | goto err; |
| 516 | } |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 517 | /* Only support SSLv3/TLS and DTLS. */ |
| 518 | if ((ssl_version >> 8) != SSL3_VERSION_MAJOR && |
| 519 | (ssl_version >> 8) != (DTLS1_VERSION >> 8)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 520 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 521 | goto err; |
| 522 | } |
| 523 | ret->ssl_version = ssl_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 524 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 525 | CBS cipher; |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 526 | uint16_t cipher_value; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 527 | if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) || |
| 528 | !CBS_get_u16(&cipher, &cipher_value) || |
| 529 | CBS_len(&cipher) != 0) { |
| 530 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 531 | goto err; |
| 532 | } |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 533 | ret->cipher = SSL_get_cipher_by_value(cipher_value); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 534 | if (ret->cipher == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 535 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 536 | goto err; |
| 537 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 538 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 539 | CBS session_id, master_key; |
| 540 | if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) || |
| 541 | CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH || |
| 542 | !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) || |
| 543 | CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 544 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 545 | goto err; |
| 546 | } |
| 547 | memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id)); |
| 548 | ret->session_id_length = CBS_len(&session_id); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 549 | memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key)); |
| 550 | ret->master_key_length = CBS_len(&master_key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 551 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 552 | if (!SSL_SESSION_parse_long(&session, &ret->time, kTimeTag, time(NULL)) || |
| 553 | !SSL_SESSION_parse_long(&session, &ret->timeout, kTimeoutTag, 3)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 554 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
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 peer; |
| 559 | int has_peer; |
| 560 | if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag)) { |
| 561 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 562 | goto err; |
| 563 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 564 | X509_free(ret->peer); |
| 565 | ret->peer = NULL; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 566 | if (has_peer) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 567 | ret->peer = parse_x509(&peer); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 568 | if (ret->peer == NULL) { |
| 569 | goto err; |
| 570 | } |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 571 | if (CBS_len(&peer) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 572 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 573 | goto err; |
| 574 | } |
| 575 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 576 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 577 | if (!SSL_SESSION_parse_bounded_octet_string( |
| 578 | &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx), |
| 579 | kSessionIDContextTag) || |
| 580 | !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag, |
| 581 | X509_V_OK) || |
| 582 | !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname, |
| 583 | kHostNameTag) || |
| 584 | !SSL_SESSION_parse_string(&session, &ret->psk_identity, |
| 585 | kPSKIdentityTag) || |
| 586 | !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint, |
| 587 | kTicketLifetimeHintTag, 0) || |
| 588 | !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick, |
| 589 | &ret->tlsext_ticklen, kTicketTag)) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 590 | goto err; |
| 591 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 592 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 593 | if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) { |
| 594 | CBS child, peer_sha256; |
| 595 | if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) || |
| 596 | !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) || |
| 597 | CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) || |
| 598 | CBS_len(&child) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 599 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 600 | goto err; |
| 601 | } |
| 602 | memcpy(ret->peer_sha256, CBS_data(&peer_sha256), sizeof(ret->peer_sha256)); |
| 603 | ret->peer_sha256_valid = 1; |
| 604 | } else { |
| 605 | ret->peer_sha256_valid = 0; |
| 606 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 607 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 608 | if (!SSL_SESSION_parse_bounded_octet_string( |
| 609 | &session, ret->original_handshake_hash, |
| 610 | &ret->original_handshake_hash_len, |
| 611 | sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) || |
| 612 | !SSL_SESSION_parse_octet_string( |
| 613 | &session, &ret->tlsext_signed_cert_timestamp_list, |
| 614 | &ret->tlsext_signed_cert_timestamp_list_length, |
| 615 | kSignedCertTimestampListTag) || |
| 616 | !SSL_SESSION_parse_octet_string( |
| 617 | &session, &ret->ocsp_response, &ret->ocsp_response_length, |
| 618 | kOCSPResponseTag)) { |
| 619 | goto err; |
| 620 | } |
| 621 | |
| 622 | int extended_master_secret; |
| 623 | if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret, |
| 624 | kExtendedMasterSecretTag, |
| 625 | 0 /* default to false */)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 626 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 627 | goto err; |
| 628 | } |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 629 | ret->extended_master_secret = !!extended_master_secret; |
| 630 | |
| 631 | if (!SSL_SESSION_parse_u32(&session, &ret->key_exchange_info, |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 632 | kKeyExchangeInfoTag, 0)) { |
| 633 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 634 | goto err; |
| 635 | } |
| 636 | |
| 637 | CBS cert_chain; |
| 638 | int has_cert_chain; |
| 639 | if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain, |
| 640 | kCertChainTag)) { |
| 641 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 642 | goto err; |
| 643 | } |
| 644 | sk_X509_pop_free(ret->cert_chain, X509_free); |
| 645 | ret->cert_chain = NULL; |
| 646 | if (has_cert_chain) { |
| 647 | ret->cert_chain = sk_X509_new_null(); |
| 648 | if (ret->cert_chain == NULL) { |
| 649 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 650 | goto err; |
| 651 | } |
| 652 | while (CBS_len(&cert_chain) > 0) { |
| 653 | X509 *x509 = parse_x509(&cert_chain); |
| 654 | if (x509 == NULL) { |
| 655 | goto err; |
| 656 | } |
| 657 | if (!sk_X509_push(ret->cert_chain, x509)) { |
| 658 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 659 | X509_free(x509); |
| 660 | goto err; |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | if (CBS_len(&session) != 0) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 666 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 667 | goto err; |
| 668 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 669 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 670 | return ret; |
| 671 | |
| 672 | err: |
| 673 | SSL_SESSION_free(ret); |
| 674 | return NULL; |
| 675 | } |
| 676 | |
| 677 | SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) { |
| 678 | CBS cbs; |
| 679 | CBS_init(&cbs, in, in_len); |
| 680 | SSL_SESSION *ret = SSL_SESSION_parse(&cbs); |
| 681 | if (ret == NULL) { |
| 682 | return NULL; |
| 683 | } |
| 684 | if (CBS_len(&cbs) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 685 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 686 | SSL_SESSION_free(ret); |
| 687 | return NULL; |
| 688 | } |
| 689 | return ret; |
| 690 | } |
| 691 | |
| 692 | SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) { |
| 693 | if (length < 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 694 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 695 | return NULL; |
| 696 | } |
| 697 | |
| 698 | CBS cbs; |
| 699 | CBS_init(&cbs, *pp, length); |
| 700 | |
| 701 | SSL_SESSION *ret = SSL_SESSION_parse(&cbs); |
| 702 | if (ret == NULL) { |
| 703 | return NULL; |
| 704 | } |
| 705 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 706 | if (a) { |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 707 | SSL_SESSION_free(*a); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 708 | *a = ret; |
| 709 | } |
| 710 | *pp = CBS_data(&cbs); |
| 711 | return ret; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 712 | } |