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