Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright 2005 Nokia. All rights reserved. |
| 59 | * |
| 60 | * The portions of the attached software ("Contribution") is developed by |
| 61 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 62 | * license. |
| 63 | * |
| 64 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 65 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 66 | * support (see RFC 4279) to OpenSSL. |
| 67 | * |
| 68 | * No patent licenses or other rights except those expressly stated in |
| 69 | * the OpenSSL open source license shall be deemed granted or received |
| 70 | * expressly, by implication, estoppel, or otherwise. |
| 71 | * |
| 72 | * No assurances are provided by Nokia that the Contribution does not |
| 73 | * infringe the patent or other intellectual property rights of any third |
| 74 | * party or that the license provides you with all the necessary rights |
| 75 | * to make use of the Contribution. |
| 76 | * |
| 77 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 78 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 79 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 80 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 81 | * OTHERWISE. */ |
| 82 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 83 | #include <openssl/ssl.h> |
| 84 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 85 | #include <limits.h> |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 86 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 87 | |
Steven Valdez | 84b5c00 | 2016-08-25 16:30:58 -0400 | [diff] [blame] | 88 | #include <openssl/buf.h> |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 89 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 90 | #include <openssl/err.h> |
Brian Smith | 054e682 | 2015-03-27 21:12:01 -1000 | [diff] [blame] | 91 | #include <openssl/mem.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 92 | #include <openssl/x509.h> |
| 93 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 94 | #include "../crypto/internal.h" |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 95 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 96 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 97 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 98 | /* An SSL_SESSION is serialized as the following ASN.1 structure: |
| 99 | * |
| 100 | * SSLSession ::= SEQUENCE { |
David Benjamin | 7bb88bb | 2016-11-01 13:59:01 -0400 | [diff] [blame] | 101 | * version INTEGER (1), -- session structure version |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 102 | * sslVersion INTEGER, -- protocol version number |
| 103 | * cipher OCTET STRING, -- two bytes long |
| 104 | * sessionID OCTET STRING, |
| 105 | * masterKey OCTET STRING, |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 106 | * time [1] INTEGER, -- seconds since UNIX epoch |
| 107 | * timeout [2] INTEGER, -- in seconds |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 108 | * peer [3] Certificate OPTIONAL, |
| 109 | * sessionIDContext [4] OCTET STRING OPTIONAL, |
| 110 | * verifyResult [5] INTEGER OPTIONAL, -- one of X509_V_* codes |
| 111 | * hostName [6] OCTET STRING OPTIONAL, |
| 112 | * -- from server_name extension |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 113 | * pskIdentity [8] OCTET STRING OPTIONAL, |
| 114 | * ticketLifetimeHint [9] INTEGER OPTIONAL, -- client-only |
| 115 | * ticket [10] OCTET STRING OPTIONAL, -- client-only |
| 116 | * peerSHA256 [13] OCTET STRING OPTIONAL, |
| 117 | * originalHandshakeHash [14] OCTET STRING OPTIONAL, |
| 118 | * signedCertTimestampList [15] OCTET STRING OPTIONAL, |
| 119 | * -- contents of SCT extension |
| 120 | * ocspResponse [16] OCTET STRING OPTIONAL, |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 121 | * -- stapled OCSP response from the server |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 122 | * extendedMasterSecret [17] BOOLEAN OPTIONAL, |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 123 | * groupID [18] INTEGER OPTIONAL, |
| 124 | * -- For historical reasons, for legacy DHE or |
| 125 | * -- static RSA ciphers, this field contains |
| 126 | * -- another value to be discarded. |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 127 | * certChain [19] SEQUENCE OF Certificate OPTIONAL, |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 128 | * ticketAgeAdd [21] OCTET STRING OPTIONAL, |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 129 | * isServer [22] BOOLEAN DEFAULT TRUE, |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 130 | * peerSignatureAlgorithm [23] INTEGER OPTIONAL, |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 131 | * ticketMaxEarlyData [24] INTEGER OPTIONAL, |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 132 | * } |
| 133 | * |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 134 | * Note: historically this serialization has included other optional |
| 135 | * fields. Their presense is currently treated as a parse error: |
| 136 | * |
| 137 | * keyArg [0] IMPLICIT OCTET STRING OPTIONAL, |
| 138 | * pskIdentityHint [7] OCTET STRING OPTIONAL, |
| 139 | * compressionMethod [11] OCTET STRING OPTIONAL, |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 140 | * srpUsername [12] OCTET STRING OPTIONAL, |
| 141 | * ticketFlags [20] INTEGER OPTIONAL, |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 142 | */ |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 143 | |
Adam Langley | 96c2a28 | 2015-06-02 14:16:44 -0700 | [diff] [blame] | 144 | static const unsigned kVersion = 1; |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 145 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 146 | static const int kTimeTag = |
| 147 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
| 148 | static const int kTimeoutTag = |
| 149 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2; |
| 150 | static const int kPeerTag = |
| 151 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3; |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 152 | static const int kSessionIDContextTag = |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 153 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4; |
| 154 | static const int kVerifyResultTag = |
| 155 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5; |
| 156 | static const int kHostNameTag = |
| 157 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 158 | static const int kPSKIdentityTag = |
| 159 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8; |
| 160 | static const int kTicketLifetimeHintTag = |
| 161 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9; |
| 162 | static const int kTicketTag = |
| 163 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10; |
| 164 | static const int kPeerSHA256Tag = |
| 165 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13; |
| 166 | static const int kOriginalHandshakeHashTag = |
| 167 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14; |
| 168 | static const int kSignedCertTimestampListTag = |
| 169 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15; |
| 170 | static const int kOCSPResponseTag = |
| 171 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 172 | static const int kExtendedMasterSecretTag = |
| 173 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17; |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 174 | static const int kGroupIDTag = |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 175 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 176 | static const int kCertChainTag = |
| 177 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 178 | static const int kTicketAgeAddTag = |
| 179 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 180 | static const int kIsServerTag = |
| 181 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22; |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 182 | static const int kPeerSignatureAlgorithmTag = |
| 183 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 23; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 184 | static const int kTicketMaxEarlyDataTag = |
| 185 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 24; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 186 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 187 | static int SSL_SESSION_to_bytes_full(const SSL_SESSION *in, uint8_t **out_data, |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 188 | size_t *out_len, int for_ticket) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 189 | CBB cbb, session, child, child2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 190 | |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 191 | if (in == NULL || in->cipher == NULL) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 192 | return 0; |
| 193 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 194 | |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 195 | CBB_zero(&cbb); |
| 196 | if (!CBB_init(&cbb, 0) || |
| 197 | !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) || |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 198 | !CBB_add_asn1_uint64(&session, kVersion) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 199 | !CBB_add_asn1_uint64(&session, in->ssl_version) || |
| 200 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 201 | !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 202 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 203 | /* The session ID is irrelevant for a session ticket. */ |
| 204 | !CBB_add_bytes(&child, in->session_id, |
| 205 | for_ticket ? 0 : in->session_id_length) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 206 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
| 207 | !CBB_add_bytes(&child, in->master_key, in->master_key_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 208 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 209 | goto err; |
| 210 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 212 | if (in->time < 0) { |
| 213 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 214 | goto err; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 215 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 216 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 217 | if (!CBB_add_asn1(&session, &child, kTimeTag) || |
| 218 | !CBB_add_asn1_uint64(&child, in->time)) { |
| 219 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 220 | goto err; |
| 221 | } |
David Benjamin | 0f31ac7 | 2016-11-02 23:05:14 -0400 | [diff] [blame] | 222 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 223 | if (in->timeout < 0) { |
| 224 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 225 | goto err; |
| 226 | } |
| 227 | |
| 228 | if (!CBB_add_asn1(&session, &child, kTimeoutTag) || |
| 229 | !CBB_add_asn1_uint64(&child, in->timeout)) { |
| 230 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 231 | goto err; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 232 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 233 | |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 234 | /* The peer certificate is only serialized if the SHA-256 isn't |
| 235 | * serialized instead. */ |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 236 | if (sk_CRYPTO_BUFFER_num(in->certs) > 0 && !in->peer_sha256_valid) { |
| 237 | const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, 0); |
| 238 | if (!CBB_add_asn1(&session, &child, kPeerTag) || |
| 239 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 240 | CRYPTO_BUFFER_len(buffer))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 241 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 242 | goto err; |
| 243 | } |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 244 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 245 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 246 | /* Although it is OPTIONAL and usually empty, OpenSSL has |
| 247 | * historically always encoded the sid_ctx. */ |
| 248 | if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) || |
| 249 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 250 | !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 251 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 252 | goto err; |
| 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->verify_result != X509_V_OK) { |
| 256 | if (!CBB_add_asn1(&session, &child, kVerifyResultTag) || |
| 257 | !CBB_add_asn1_uint64(&child, in->verify_result)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 258 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 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_hostname) { |
| 264 | if (!CBB_add_asn1(&session, &child, kHostNameTag) || |
| 265 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 266 | !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname, |
| 267 | strlen(in->tlsext_hostname))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 268 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 269 | goto err; |
| 270 | } |
| 271 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 272 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 273 | if (in->psk_identity) { |
| 274 | if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) || |
| 275 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 276 | !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity, |
| 277 | strlen(in->psk_identity))) { |
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 | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 282 | |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 283 | if (in->tlsext_tick_lifetime_hint > 0) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 284 | if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) || |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 285 | !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 286 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 287 | goto err; |
| 288 | } |
| 289 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 290 | |
David Benjamin | 95d3182 | 2015-06-15 19:53:32 -0400 | [diff] [blame] | 291 | if (in->tlsext_tick && !for_ticket) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 292 | if (!CBB_add_asn1(&session, &child, kTicketTag) || |
| 293 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 294 | !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) { |
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 | } |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 300 | if (in->peer_sha256_valid) { |
| 301 | if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) || |
| 302 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 303 | !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) { |
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 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 308 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 309 | if (in->original_handshake_hash_len > 0) { |
| 310 | if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) || |
| 311 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 312 | !CBB_add_bytes(&child2, in->original_handshake_hash, |
| 313 | in->original_handshake_hash_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 314 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 315 | goto err; |
| 316 | } |
| 317 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 318 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 319 | if (in->tlsext_signed_cert_timestamp_list_length > 0) { |
| 320 | if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) || |
| 321 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 322 | !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list, |
| 323 | in->tlsext_signed_cert_timestamp_list_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 324 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 325 | goto err; |
| 326 | } |
| 327 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 328 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 329 | if (in->ocsp_response_length > 0) { |
| 330 | if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) || |
| 331 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 332 | !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 333 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 334 | goto err; |
| 335 | } |
| 336 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 337 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 338 | if (in->extended_master_secret) { |
| 339 | if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) || |
| 340 | !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 341 | !CBB_add_u8(&child2, 0xff)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 342 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 343 | goto err; |
| 344 | } |
| 345 | } |
| 346 | |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 347 | if (in->group_id > 0 && |
| 348 | (!CBB_add_asn1(&session, &child, kGroupIDTag) || |
| 349 | !CBB_add_asn1_uint64(&child, in->group_id))) { |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 350 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 351 | goto err; |
| 352 | } |
| 353 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 354 | /* The certificate chain is only serialized if the leaf's SHA-256 isn't |
| 355 | * serialized instead. */ |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 356 | if (in->certs != NULL && |
| 357 | !in->peer_sha256_valid && |
| 358 | sk_CRYPTO_BUFFER_num(in->certs) >= 2) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 359 | if (!CBB_add_asn1(&session, &child, kCertChainTag)) { |
| 360 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 361 | goto err; |
| 362 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 363 | for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs); i++) { |
| 364 | const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, i); |
| 365 | if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 366 | CRYPTO_BUFFER_len(buffer))) { |
| 367 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 368 | goto err; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 373 | if (in->ticket_age_add_valid) { |
| 374 | if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) || |
| 375 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 376 | !CBB_add_u32(&child2, in->ticket_age_add)) { |
| 377 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 378 | goto err; |
| 379 | } |
| 380 | } |
| 381 | |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 382 | if (!in->is_server) { |
| 383 | if (!CBB_add_asn1(&session, &child, kIsServerTag) || |
| 384 | !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 385 | !CBB_add_u8(&child2, 0x00)) { |
| 386 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 387 | goto err; |
| 388 | } |
| 389 | } |
| 390 | |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 391 | if (in->peer_signature_algorithm != 0 && |
| 392 | (!CBB_add_asn1(&session, &child, kPeerSignatureAlgorithmTag) || |
| 393 | !CBB_add_asn1_uint64(&child, in->peer_signature_algorithm))) { |
| 394 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 395 | goto err; |
| 396 | } |
| 397 | |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 398 | if (in->ticket_max_early_data != 0 && |
| 399 | (!CBB_add_asn1(&session, &child, kTicketMaxEarlyDataTag) || |
| 400 | !CBB_add_asn1_uint64(&child, in->ticket_max_early_data))) { |
| 401 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 402 | goto err; |
| 403 | } |
| 404 | |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 405 | if (!CBB_finish(&cbb, out_data, out_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 406 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 407 | goto err; |
| 408 | } |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 409 | return 1; |
| 410 | |
| 411 | err: |
| 412 | CBB_cleanup(&cbb); |
| 413 | return 0; |
| 414 | } |
| 415 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 416 | int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data, |
| 417 | size_t *out_len) { |
Steven Valdez | 84b5c00 | 2016-08-25 16:30:58 -0400 | [diff] [blame] | 418 | if (in->not_resumable) { |
| 419 | /* If the caller has an unresumable session, e.g. if |SSL_get_session| were |
| 420 | * called on a TLS 1.3 or False Started connection, serialize with a |
| 421 | * placeholder value so it is not accidentally deserialized into a resumable |
| 422 | * one. */ |
| 423 | static const char kNotResumableSession[] = "NOT RESUMABLE"; |
| 424 | |
| 425 | *out_len = strlen(kNotResumableSession); |
| 426 | *out_data = BUF_memdup(kNotResumableSession, *out_len); |
| 427 | if (*out_data == NULL) { |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | return 1; |
| 432 | } |
| 433 | |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 434 | return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0); |
| 435 | } |
| 436 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 437 | int SSL_SESSION_to_bytes_for_ticket(const SSL_SESSION *in, uint8_t **out_data, |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 438 | size_t *out_len) { |
| 439 | return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1); |
| 440 | } |
| 441 | |
| 442 | int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) { |
| 443 | uint8_t *out; |
| 444 | size_t len; |
| 445 | |
| 446 | if (!SSL_SESSION_to_bytes(in, &out, &len)) { |
| 447 | return -1; |
| 448 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 449 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 450 | if (len > INT_MAX) { |
| 451 | OPENSSL_free(out); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 452 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 453 | return -1; |
| 454 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 455 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 456 | if (pp) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 457 | OPENSSL_memcpy(*pp, out, len); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 458 | *pp += len; |
| 459 | } |
| 460 | OPENSSL_free(out); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 461 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 462 | return len; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 463 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 464 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 465 | /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 466 | * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On |
| 467 | * entry, if |*out| is not NULL, it frees the existing contents. If |
| 468 | * the element was not found, it sets |*out| to NULL. It returns one |
| 469 | * on success, whether or not the element was found, and zero on |
| 470 | * decode error. */ |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 471 | static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 472 | CBS value; |
| 473 | int present; |
| 474 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 475 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | if (present) { |
| 479 | if (CBS_contains_zero_byte(&value)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 480 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 481 | return 0; |
| 482 | } |
| 483 | if (!CBS_strdup(&value, out)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 484 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 485 | return 0; |
| 486 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 487 | } else { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 488 | OPENSSL_free(*out); |
| 489 | *out = NULL; |
| 490 | } |
| 491 | return 1; |
| 492 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 493 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 494 | /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 495 | * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr| |
| 496 | * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing |
| 497 | * contents. On entry, if the element was not found, it sets |
| 498 | * |*out_ptr| to NULL. It returns one on success, whether or not the |
| 499 | * element was found, and zero on decode error. */ |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 500 | static int SSL_SESSION_parse_octet_string(CBS *cbs, uint8_t **out_ptr, |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 501 | size_t *out_len, unsigned tag) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 502 | CBS value; |
| 503 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 504 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | if (!CBS_stow(&value, out_ptr, out_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 508 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | return 1; |
| 512 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 513 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 514 | /* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING |
| 515 | * explicitly tagged with |tag| of size at most |max_out|. */ |
| 516 | static int SSL_SESSION_parse_bounded_octet_string( |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 517 | CBS *cbs, uint8_t *out, uint8_t *out_len, uint8_t max_out, unsigned tag) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 518 | CBS value; |
| 519 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) || |
| 520 | CBS_len(&value) > max_out) { |
| 521 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 522 | return 0; |
| 523 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 524 | OPENSSL_memcpy(out, CBS_data(&value), CBS_len(&value)); |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 525 | *out_len = (uint8_t)CBS_len(&value); |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 526 | return 1; |
| 527 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 528 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 529 | static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag, |
| 530 | long default_value) { |
| 531 | uint64_t value; |
| 532 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 533 | (uint64_t)default_value) || |
| 534 | value > LONG_MAX) { |
| 535 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 536 | return 0; |
| 537 | } |
| 538 | *out = (long)value; |
| 539 | return 1; |
| 540 | } |
| 541 | |
| 542 | static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag, |
| 543 | uint32_t default_value) { |
| 544 | uint64_t value; |
| 545 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 546 | (uint64_t)default_value) || |
| 547 | value > 0xffffffff) { |
| 548 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 549 | return 0; |
| 550 | } |
| 551 | *out = (uint32_t)value; |
| 552 | return 1; |
| 553 | } |
| 554 | |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 555 | static int SSL_SESSION_parse_u16(CBS *cbs, uint16_t *out, unsigned tag, |
| 556 | uint16_t default_value) { |
| 557 | uint64_t value; |
| 558 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 559 | (uint64_t)default_value) || |
| 560 | value > 0xffff) { |
| 561 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 562 | return 0; |
| 563 | } |
| 564 | *out = (uint16_t)value; |
| 565 | return 1; |
| 566 | } |
| 567 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 568 | static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) { |
| 569 | SSL_SESSION *ret = SSL_SESSION_new(); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 570 | if (ret == NULL) { |
| 571 | goto err; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 572 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 573 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 574 | CBS session; |
| 575 | uint64_t version, ssl_version; |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 576 | if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) || |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 577 | !CBS_get_asn1_uint64(&session, &version) || |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 578 | version != kVersion || |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 579 | !CBS_get_asn1_uint64(&session, &ssl_version)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 580 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 581 | goto err; |
| 582 | } |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 583 | ret->ssl_version = ssl_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 584 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 585 | CBS cipher; |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 586 | uint16_t cipher_value; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 587 | if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) || |
| 588 | !CBS_get_u16(&cipher, &cipher_value) || |
| 589 | CBS_len(&cipher) != 0) { |
| 590 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 591 | goto err; |
| 592 | } |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 593 | ret->cipher = SSL_get_cipher_by_value(cipher_value); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 594 | if (ret->cipher == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 595 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 596 | goto err; |
| 597 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 598 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 599 | CBS session_id, master_key; |
| 600 | if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) || |
| 601 | CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH || |
| 602 | !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) || |
| 603 | CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 604 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 605 | goto err; |
| 606 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 607 | OPENSSL_memcpy(ret->session_id, CBS_data(&session_id), CBS_len(&session_id)); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 608 | ret->session_id_length = CBS_len(&session_id); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 609 | OPENSSL_memcpy(ret->master_key, CBS_data(&master_key), CBS_len(&master_key)); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 610 | ret->master_key_length = CBS_len(&master_key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 611 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 612 | CBS child; |
| 613 | uint64_t time, timeout; |
| 614 | if (!CBS_get_asn1(&session, &child, kTimeTag) || |
| 615 | !CBS_get_asn1_uint64(&child, &time) || |
| 616 | time > LONG_MAX || |
| 617 | !CBS_get_asn1(&session, &child, kTimeoutTag) || |
| 618 | !CBS_get_asn1_uint64(&child, &timeout) || |
| 619 | timeout > LONG_MAX) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 620 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 621 | goto err; |
| 622 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 623 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 624 | ret->time = (long)time; |
| 625 | ret->timeout = (long)timeout; |
| 626 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 627 | CBS peer; |
| 628 | int has_peer; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 629 | if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) || |
| 630 | (has_peer && CBS_len(&peer) == 0)) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 631 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 632 | goto err; |
| 633 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 634 | /* |peer| is processed with the certificate chain. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 635 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 636 | if (!SSL_SESSION_parse_bounded_octet_string( |
| 637 | &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx), |
| 638 | kSessionIDContextTag) || |
| 639 | !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag, |
| 640 | X509_V_OK) || |
| 641 | !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname, |
| 642 | kHostNameTag) || |
| 643 | !SSL_SESSION_parse_string(&session, &ret->psk_identity, |
| 644 | kPSKIdentityTag) || |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 645 | !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint, |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 646 | kTicketLifetimeHintTag, 0) || |
| 647 | !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick, |
| 648 | &ret->tlsext_ticklen, kTicketTag)) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 649 | goto err; |
| 650 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 651 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 652 | if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) { |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 653 | CBS peer_sha256; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 654 | if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) || |
| 655 | !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) || |
| 656 | CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) || |
| 657 | CBS_len(&child) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 658 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 659 | goto err; |
| 660 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 661 | OPENSSL_memcpy(ret->peer_sha256, CBS_data(&peer_sha256), |
| 662 | sizeof(ret->peer_sha256)); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 663 | ret->peer_sha256_valid = 1; |
| 664 | } else { |
| 665 | ret->peer_sha256_valid = 0; |
| 666 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 667 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 668 | if (!SSL_SESSION_parse_bounded_octet_string( |
| 669 | &session, ret->original_handshake_hash, |
| 670 | &ret->original_handshake_hash_len, |
| 671 | sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) || |
| 672 | !SSL_SESSION_parse_octet_string( |
| 673 | &session, &ret->tlsext_signed_cert_timestamp_list, |
| 674 | &ret->tlsext_signed_cert_timestamp_list_length, |
| 675 | kSignedCertTimestampListTag) || |
| 676 | !SSL_SESSION_parse_octet_string( |
| 677 | &session, &ret->ocsp_response, &ret->ocsp_response_length, |
| 678 | kOCSPResponseTag)) { |
| 679 | goto err; |
| 680 | } |
| 681 | |
| 682 | int extended_master_secret; |
| 683 | if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret, |
| 684 | kExtendedMasterSecretTag, |
| 685 | 0 /* default to false */)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 686 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 687 | goto err; |
| 688 | } |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 689 | ret->extended_master_secret = !!extended_master_secret; |
| 690 | |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 691 | uint32_t value; |
| 692 | if (!SSL_SESSION_parse_u32(&session, &value, kGroupIDTag, 0)) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 693 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 694 | goto err; |
| 695 | } |
| 696 | |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 697 | /* Historically, the group_id field was used for key-exchange-specific |
| 698 | * information. Discard all but the group ID. */ |
| 699 | if (ret->cipher->algorithm_mkey & (SSL_kRSA | SSL_kDHE)) { |
| 700 | value = 0; |
| 701 | } |
| 702 | |
| 703 | if (value > 0xffff) { |
| 704 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 705 | goto err; |
| 706 | } |
| 707 | ret->group_id = (uint16_t)value; |
| 708 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 709 | CBS cert_chain; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 710 | CBS_init(&cert_chain, NULL, 0); |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 711 | int has_cert_chain; |
| 712 | if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain, |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 713 | kCertChainTag) || |
| 714 | (has_cert_chain && CBS_len(&cert_chain) == 0)) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 715 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 716 | goto err; |
| 717 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 718 | if (has_cert_chain && !has_peer) { |
| 719 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 720 | goto err; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 721 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 722 | if (has_peer || has_cert_chain) { |
| 723 | ret->certs = sk_CRYPTO_BUFFER_new_null(); |
| 724 | if (ret->certs == NULL) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 725 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 726 | goto err; |
| 727 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 728 | |
| 729 | if (has_peer) { |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 730 | /* TODO(agl): this should use the |SSL_CTX|'s pool. */ |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 731 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&peer, NULL); |
| 732 | if (buffer == NULL || |
| 733 | !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) { |
| 734 | CRYPTO_BUFFER_free(buffer); |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame] | 735 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 736 | goto err; |
| 737 | } |
| 738 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 739 | |
| 740 | while (CBS_len(&cert_chain) > 0) { |
| 741 | CBS cert; |
| 742 | if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) || |
| 743 | CBS_len(&cert) == 0) { |
| 744 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 745 | goto err; |
| 746 | } |
| 747 | |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 748 | /* TODO(agl): this should use the |SSL_CTX|'s pool. */ |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 749 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, NULL); |
| 750 | if (buffer == NULL || |
| 751 | !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) { |
| 752 | CRYPTO_BUFFER_free(buffer); |
| 753 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 754 | goto err; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | if (!ssl_session_x509_cache_objects(ret)) { |
| 760 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 761 | goto err; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 762 | } |
| 763 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 764 | CBS age_add; |
| 765 | int age_add_present; |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 766 | if (!CBS_get_optional_asn1_octet_string(&session, &age_add, &age_add_present, |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 767 | kTicketAgeAddTag) || |
| 768 | (age_add_present && |
| 769 | !CBS_get_u32(&age_add, &ret->ticket_age_add)) || |
| 770 | CBS_len(&age_add) != 0) { |
| 771 | goto err; |
| 772 | } |
| 773 | ret->ticket_age_add_valid = age_add_present; |
| 774 | |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 775 | int is_server; |
| 776 | if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag, |
| 777 | 1 /* default to true */)) { |
| 778 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 779 | goto err; |
| 780 | } |
| 781 | /* TODO: in time we can include |is_server| for servers too, then we can |
| 782 | enforce that client and server sessions are never mixed up. */ |
| 783 | |
| 784 | ret->is_server = is_server; |
| 785 | |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 786 | if (!SSL_SESSION_parse_u16(&session, &ret->peer_signature_algorithm, |
| 787 | kPeerSignatureAlgorithmTag, 0) || |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 788 | !SSL_SESSION_parse_u32(&session, &ret->ticket_max_early_data, |
| 789 | kTicketMaxEarlyDataTag, 0) || |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 790 | CBS_len(&session) != 0) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 791 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 792 | goto err; |
| 793 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 794 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 795 | return ret; |
| 796 | |
| 797 | err: |
| 798 | SSL_SESSION_free(ret); |
| 799 | return NULL; |
| 800 | } |
| 801 | |
| 802 | SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) { |
| 803 | CBS cbs; |
| 804 | CBS_init(&cbs, in, in_len); |
| 805 | SSL_SESSION *ret = SSL_SESSION_parse(&cbs); |
| 806 | if (ret == NULL) { |
| 807 | return NULL; |
| 808 | } |
| 809 | if (CBS_len(&cbs) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 810 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 811 | SSL_SESSION_free(ret); |
| 812 | return NULL; |
| 813 | } |
| 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) { |
| 818 | if (length < 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 819 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 820 | return NULL; |
| 821 | } |
| 822 | |
| 823 | CBS cbs; |
| 824 | CBS_init(&cbs, *pp, length); |
| 825 | |
| 826 | SSL_SESSION *ret = SSL_SESSION_parse(&cbs); |
| 827 | if (ret == NULL) { |
| 828 | return NULL; |
| 829 | } |
| 830 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 831 | if (a) { |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 832 | SSL_SESSION_free(*a); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 833 | *a = ret; |
| 834 | } |
| 835 | *pp = CBS_data(&cbs); |
| 836 | return ret; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 837 | } |