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