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