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 | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 132 | * authTimeout [25] INTEGER OPTIONAL, -- defaults to timeout |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 133 | * } |
| 134 | * |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 135 | * Note: historically this serialization has included other optional |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 136 | * fields. Their presence is currently treated as a parse error: |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 137 | * |
| 138 | * keyArg [0] IMPLICIT OCTET STRING OPTIONAL, |
| 139 | * pskIdentityHint [7] OCTET STRING OPTIONAL, |
| 140 | * compressionMethod [11] OCTET STRING OPTIONAL, |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 141 | * srpUsername [12] OCTET STRING OPTIONAL, |
| 142 | * ticketFlags [20] INTEGER OPTIONAL, |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 143 | */ |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 144 | |
Adam Langley | 96c2a28 | 2015-06-02 14:16:44 -0700 | [diff] [blame] | 145 | static const unsigned kVersion = 1; |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 146 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 147 | static const int kTimeTag = |
| 148 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
| 149 | static const int kTimeoutTag = |
| 150 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2; |
| 151 | static const int kPeerTag = |
| 152 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 3; |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 153 | static const int kSessionIDContextTag = |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 154 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 4; |
| 155 | static const int kVerifyResultTag = |
| 156 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 5; |
| 157 | static const int kHostNameTag = |
| 158 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 6; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 159 | static const int kPSKIdentityTag = |
| 160 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 8; |
| 161 | static const int kTicketLifetimeHintTag = |
| 162 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 9; |
| 163 | static const int kTicketTag = |
| 164 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 10; |
| 165 | static const int kPeerSHA256Tag = |
| 166 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 13; |
| 167 | static const int kOriginalHandshakeHashTag = |
| 168 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 14; |
| 169 | static const int kSignedCertTimestampListTag = |
| 170 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 15; |
| 171 | static const int kOCSPResponseTag = |
| 172 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 16; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 173 | static const int kExtendedMasterSecretTag = |
| 174 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 17; |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 175 | static const int kGroupIDTag = |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 176 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 18; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 177 | static const int kCertChainTag = |
| 178 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 19; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 179 | static const int kTicketAgeAddTag = |
| 180 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 21; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 181 | static const int kIsServerTag = |
| 182 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 22; |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 183 | static const int kPeerSignatureAlgorithmTag = |
| 184 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 23; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 185 | static const int kTicketMaxEarlyDataTag = |
| 186 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 24; |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 187 | static const int kAuthTimeoutTag = |
| 188 | CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 25; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 189 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 190 | 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] | 191 | size_t *out_len, int for_ticket) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 192 | CBB cbb, session, child, child2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 194 | if (in == NULL || in->cipher == NULL) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 195 | return 0; |
| 196 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 198 | CBB_zero(&cbb); |
| 199 | if (!CBB_init(&cbb, 0) || |
| 200 | !CBB_add_asn1(&cbb, &session, CBS_ASN1_SEQUENCE) || |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 201 | !CBB_add_asn1_uint64(&session, kVersion) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 202 | !CBB_add_asn1_uint64(&session, in->ssl_version) || |
| 203 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 204 | !CBB_add_u16(&child, (uint16_t)(in->cipher->id & 0xffff)) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 205 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 206 | /* The session ID is irrelevant for a session ticket. */ |
| 207 | !CBB_add_bytes(&child, in->session_id, |
| 208 | for_ticket ? 0 : in->session_id_length) || |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 209 | !CBB_add_asn1(&session, &child, CBS_ASN1_OCTETSTRING) || |
| 210 | !CBB_add_bytes(&child, in->master_key, in->master_key_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 211 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 212 | goto err; |
| 213 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 215 | if (in->time < 0) { |
| 216 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 217 | goto err; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 218 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 219 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 220 | if (!CBB_add_asn1(&session, &child, kTimeTag) || |
| 221 | !CBB_add_asn1_uint64(&child, in->time)) { |
| 222 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 223 | goto err; |
| 224 | } |
David Benjamin | 0f31ac7 | 2016-11-02 23:05:14 -0400 | [diff] [blame] | 225 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 226 | if (in->timeout < 0) { |
| 227 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 228 | goto err; |
| 229 | } |
| 230 | |
| 231 | if (!CBB_add_asn1(&session, &child, kTimeoutTag) || |
| 232 | !CBB_add_asn1_uint64(&child, in->timeout)) { |
| 233 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 234 | goto err; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 235 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 236 | |
Adam Langley | c0fc7a1 | 2016-12-09 15:05:34 -0800 | [diff] [blame] | 237 | /* The peer certificate is only serialized if the SHA-256 isn't |
| 238 | * serialized instead. */ |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 239 | if (sk_CRYPTO_BUFFER_num(in->certs) > 0 && !in->peer_sha256_valid) { |
| 240 | const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, 0); |
| 241 | if (!CBB_add_asn1(&session, &child, kPeerTag) || |
| 242 | !CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 243 | CRYPTO_BUFFER_len(buffer))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 244 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 245 | goto err; |
| 246 | } |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 247 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 248 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 249 | /* Although it is OPTIONAL and usually empty, OpenSSL has |
| 250 | * historically always encoded the sid_ctx. */ |
| 251 | if (!CBB_add_asn1(&session, &child, kSessionIDContextTag) || |
| 252 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 253 | !CBB_add_bytes(&child2, in->sid_ctx, in->sid_ctx_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 254 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 255 | goto err; |
| 256 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 257 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 258 | if (in->verify_result != X509_V_OK) { |
| 259 | if (!CBB_add_asn1(&session, &child, kVerifyResultTag) || |
| 260 | !CBB_add_asn1_uint64(&child, in->verify_result)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 261 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 262 | goto err; |
| 263 | } |
| 264 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 266 | if (in->tlsext_hostname) { |
| 267 | if (!CBB_add_asn1(&session, &child, kHostNameTag) || |
| 268 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 269 | !CBB_add_bytes(&child2, (const uint8_t *)in->tlsext_hostname, |
| 270 | strlen(in->tlsext_hostname))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 271 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 272 | goto err; |
| 273 | } |
| 274 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 275 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 276 | if (in->psk_identity) { |
| 277 | if (!CBB_add_asn1(&session, &child, kPSKIdentityTag) || |
| 278 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 279 | !CBB_add_bytes(&child2, (const uint8_t *)in->psk_identity, |
| 280 | strlen(in->psk_identity))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 281 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 282 | goto err; |
| 283 | } |
| 284 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 285 | |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 286 | if (in->tlsext_tick_lifetime_hint > 0) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 287 | if (!CBB_add_asn1(&session, &child, kTicketLifetimeHintTag) || |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 288 | !CBB_add_asn1_uint64(&child, in->tlsext_tick_lifetime_hint)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 289 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 290 | goto err; |
| 291 | } |
| 292 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 293 | |
David Benjamin | 95d3182 | 2015-06-15 19:53:32 -0400 | [diff] [blame] | 294 | if (in->tlsext_tick && !for_ticket) { |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 295 | if (!CBB_add_asn1(&session, &child, kTicketTag) || |
| 296 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 297 | !CBB_add_bytes(&child2, in->tlsext_tick, in->tlsext_ticklen)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 298 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 299 | goto err; |
| 300 | } |
| 301 | } |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 302 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 303 | if (in->peer_sha256_valid) { |
| 304 | if (!CBB_add_asn1(&session, &child, kPeerSHA256Tag) || |
| 305 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 306 | !CBB_add_bytes(&child2, in->peer_sha256, sizeof(in->peer_sha256))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 307 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 308 | goto err; |
| 309 | } |
| 310 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 311 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 312 | if (in->original_handshake_hash_len > 0) { |
| 313 | if (!CBB_add_asn1(&session, &child, kOriginalHandshakeHashTag) || |
| 314 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 315 | !CBB_add_bytes(&child2, in->original_handshake_hash, |
| 316 | in->original_handshake_hash_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 317 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 318 | goto err; |
| 319 | } |
| 320 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 321 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 322 | if (in->tlsext_signed_cert_timestamp_list_length > 0) { |
| 323 | if (!CBB_add_asn1(&session, &child, kSignedCertTimestampListTag) || |
| 324 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 325 | !CBB_add_bytes(&child2, in->tlsext_signed_cert_timestamp_list, |
| 326 | in->tlsext_signed_cert_timestamp_list_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 327 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 328 | goto err; |
| 329 | } |
| 330 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 331 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 332 | if (in->ocsp_response_length > 0) { |
| 333 | if (!CBB_add_asn1(&session, &child, kOCSPResponseTag) || |
| 334 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 335 | !CBB_add_bytes(&child2, in->ocsp_response, in->ocsp_response_length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 336 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 337 | goto err; |
| 338 | } |
| 339 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 340 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 341 | if (in->extended_master_secret) { |
| 342 | if (!CBB_add_asn1(&session, &child, kExtendedMasterSecretTag) || |
| 343 | !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 344 | !CBB_add_u8(&child2, 0xff)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 345 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 346 | goto err; |
| 347 | } |
| 348 | } |
| 349 | |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 350 | if (in->group_id > 0 && |
| 351 | (!CBB_add_asn1(&session, &child, kGroupIDTag) || |
| 352 | !CBB_add_asn1_uint64(&child, in->group_id))) { |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 353 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 354 | goto err; |
| 355 | } |
| 356 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 357 | /* The certificate chain is only serialized if the leaf's SHA-256 isn't |
| 358 | * serialized instead. */ |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 359 | if (in->certs != NULL && |
| 360 | !in->peer_sha256_valid && |
| 361 | sk_CRYPTO_BUFFER_num(in->certs) >= 2) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 362 | if (!CBB_add_asn1(&session, &child, kCertChainTag)) { |
| 363 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 364 | goto err; |
| 365 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 366 | for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(in->certs); i++) { |
| 367 | const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(in->certs, i); |
| 368 | if (!CBB_add_bytes(&child, CRYPTO_BUFFER_data(buffer), |
| 369 | CRYPTO_BUFFER_len(buffer))) { |
| 370 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 371 | goto err; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 376 | if (in->ticket_age_add_valid) { |
| 377 | if (!CBB_add_asn1(&session, &child, kTicketAgeAddTag) || |
| 378 | !CBB_add_asn1(&child, &child2, CBS_ASN1_OCTETSTRING) || |
| 379 | !CBB_add_u32(&child2, in->ticket_age_add)) { |
| 380 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 381 | goto err; |
| 382 | } |
| 383 | } |
| 384 | |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 385 | if (!in->is_server) { |
| 386 | if (!CBB_add_asn1(&session, &child, kIsServerTag) || |
| 387 | !CBB_add_asn1(&child, &child2, CBS_ASN1_BOOLEAN) || |
| 388 | !CBB_add_u8(&child2, 0x00)) { |
| 389 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 390 | goto err; |
| 391 | } |
| 392 | } |
| 393 | |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 394 | if (in->peer_signature_algorithm != 0 && |
| 395 | (!CBB_add_asn1(&session, &child, kPeerSignatureAlgorithmTag) || |
| 396 | !CBB_add_asn1_uint64(&child, in->peer_signature_algorithm))) { |
| 397 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 398 | goto err; |
| 399 | } |
| 400 | |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 401 | if (in->ticket_max_early_data != 0 && |
| 402 | (!CBB_add_asn1(&session, &child, kTicketMaxEarlyDataTag) || |
| 403 | !CBB_add_asn1_uint64(&child, in->ticket_max_early_data))) { |
| 404 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 405 | goto err; |
| 406 | } |
| 407 | |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 408 | if (in->timeout != in->auth_timeout && |
| 409 | (!CBB_add_asn1(&session, &child, kAuthTimeoutTag) || |
| 410 | !CBB_add_asn1_uint64(&child, in->auth_timeout))) { |
| 411 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 412 | goto err; |
| 413 | } |
| 414 | |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 415 | if (!CBB_finish(&cbb, out_data, out_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 416 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 417 | goto err; |
| 418 | } |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 419 | return 1; |
| 420 | |
| 421 | err: |
| 422 | CBB_cleanup(&cbb); |
| 423 | return 0; |
| 424 | } |
| 425 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 426 | int SSL_SESSION_to_bytes(const SSL_SESSION *in, uint8_t **out_data, |
| 427 | size_t *out_len) { |
Steven Valdez | 84b5c00 | 2016-08-25 16:30:58 -0400 | [diff] [blame] | 428 | if (in->not_resumable) { |
| 429 | /* If the caller has an unresumable session, e.g. if |SSL_get_session| were |
| 430 | * called on a TLS 1.3 or False Started connection, serialize with a |
| 431 | * placeholder value so it is not accidentally deserialized into a resumable |
| 432 | * one. */ |
| 433 | static const char kNotResumableSession[] = "NOT RESUMABLE"; |
| 434 | |
| 435 | *out_len = strlen(kNotResumableSession); |
| 436 | *out_data = BUF_memdup(kNotResumableSession, *out_len); |
| 437 | if (*out_data == NULL) { |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | return 1; |
| 442 | } |
| 443 | |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 444 | return SSL_SESSION_to_bytes_full(in, out_data, out_len, 0); |
| 445 | } |
| 446 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 447 | 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] | 448 | size_t *out_len) { |
| 449 | return SSL_SESSION_to_bytes_full(in, out_data, out_len, 1); |
| 450 | } |
| 451 | |
| 452 | int i2d_SSL_SESSION(SSL_SESSION *in, uint8_t **pp) { |
| 453 | uint8_t *out; |
| 454 | size_t len; |
| 455 | |
| 456 | if (!SSL_SESSION_to_bytes(in, &out, &len)) { |
| 457 | return -1; |
| 458 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 459 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 460 | if (len > INT_MAX) { |
| 461 | OPENSSL_free(out); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 462 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 463 | return -1; |
| 464 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 465 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 466 | if (pp) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 467 | OPENSSL_memcpy(*pp, out, len); |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 468 | *pp += len; |
| 469 | } |
| 470 | OPENSSL_free(out); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 471 | |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 472 | return len; |
David Benjamin | 89abaea | 2014-10-19 13:50:18 -0400 | [diff] [blame] | 473 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 474 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 475 | /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 476 | * explicitly tagged with |tag| from |cbs| and saves it in |*out|. On |
| 477 | * entry, if |*out| is not NULL, it frees the existing contents. If |
| 478 | * the element was not found, it sets |*out| to NULL. It returns one |
| 479 | * on success, whether or not the element was found, and zero on |
| 480 | * decode error. */ |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 481 | static int SSL_SESSION_parse_string(CBS *cbs, char **out, unsigned tag) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 482 | CBS value; |
| 483 | int present; |
| 484 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, &present, tag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 485 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | if (present) { |
| 489 | if (CBS_contains_zero_byte(&value)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 490 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | if (!CBS_strdup(&value, out)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 494 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 495 | return 0; |
| 496 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 497 | } else { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 498 | OPENSSL_free(*out); |
| 499 | *out = NULL; |
| 500 | } |
| 501 | return 1; |
| 502 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 503 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 504 | /* SSL_SESSION_parse_string gets an optional ASN.1 OCTET STRING |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 505 | * explicitly tagged with |tag| from |cbs| and stows it in |*out_ptr| |
| 506 | * and |*out_len|. If |*out_ptr| is not NULL, it frees the existing |
| 507 | * contents. On entry, if the element was not found, it sets |
| 508 | * |*out_ptr| to NULL. It returns one on success, whether or not the |
| 509 | * element was found, and zero on decode error. */ |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 510 | 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] | 511 | size_t *out_len, unsigned tag) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 512 | CBS value; |
| 513 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 514 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 515 | return 0; |
| 516 | } |
| 517 | if (!CBS_stow(&value, out_ptr, out_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 518 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | return 1; |
| 522 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 523 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 524 | /* SSL_SESSION_parse_bounded_octet_string parses an optional ASN.1 OCTET STRING |
| 525 | * explicitly tagged with |tag| of size at most |max_out|. */ |
| 526 | static int SSL_SESSION_parse_bounded_octet_string( |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 527 | 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] | 528 | CBS value; |
| 529 | if (!CBS_get_optional_asn1_octet_string(cbs, &value, NULL, tag) || |
| 530 | CBS_len(&value) > max_out) { |
| 531 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 532 | return 0; |
| 533 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 534 | OPENSSL_memcpy(out, CBS_data(&value), CBS_len(&value)); |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 535 | *out_len = (uint8_t)CBS_len(&value); |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 536 | return 1; |
| 537 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 538 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 539 | static int SSL_SESSION_parse_long(CBS *cbs, long *out, unsigned tag, |
| 540 | long default_value) { |
| 541 | uint64_t value; |
| 542 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 543 | (uint64_t)default_value) || |
| 544 | value > LONG_MAX) { |
| 545 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 546 | return 0; |
| 547 | } |
| 548 | *out = (long)value; |
| 549 | return 1; |
| 550 | } |
| 551 | |
| 552 | static int SSL_SESSION_parse_u32(CBS *cbs, uint32_t *out, unsigned tag, |
| 553 | uint32_t default_value) { |
| 554 | uint64_t value; |
| 555 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 556 | (uint64_t)default_value) || |
| 557 | value > 0xffffffff) { |
| 558 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 559 | return 0; |
| 560 | } |
| 561 | *out = (uint32_t)value; |
| 562 | return 1; |
| 563 | } |
| 564 | |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 565 | static int SSL_SESSION_parse_u16(CBS *cbs, uint16_t *out, unsigned tag, |
| 566 | uint16_t default_value) { |
| 567 | uint64_t value; |
| 568 | if (!CBS_get_optional_asn1_uint64(cbs, &value, tag, |
| 569 | (uint64_t)default_value) || |
| 570 | value > 0xffff) { |
| 571 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 572 | return 0; |
| 573 | } |
| 574 | *out = (uint16_t)value; |
| 575 | return 1; |
| 576 | } |
| 577 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 578 | static SSL_SESSION *SSL_SESSION_parse(CBS *cbs) { |
| 579 | SSL_SESSION *ret = SSL_SESSION_new(); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 580 | if (ret == NULL) { |
| 581 | goto err; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 582 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 583 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 584 | CBS session; |
| 585 | uint64_t version, ssl_version; |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 586 | if (!CBS_get_asn1(cbs, &session, CBS_ASN1_SEQUENCE) || |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 587 | !CBS_get_asn1_uint64(&session, &version) || |
David Benjamin | 7f393f7 | 2015-10-17 12:36:12 -0400 | [diff] [blame] | 588 | version != kVersion || |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 589 | !CBS_get_asn1_uint64(&session, &ssl_version)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 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 | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 593 | ret->ssl_version = ssl_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 594 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 595 | CBS cipher; |
David Benjamin | f3a8b12 | 2014-12-25 23:11:49 -0500 | [diff] [blame] | 596 | uint16_t cipher_value; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 597 | if (!CBS_get_asn1(&session, &cipher, CBS_ASN1_OCTETSTRING) || |
| 598 | !CBS_get_u16(&cipher, &cipher_value) || |
| 599 | CBS_len(&cipher) != 0) { |
| 600 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 601 | goto err; |
| 602 | } |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 603 | ret->cipher = SSL_get_cipher_by_value(cipher_value); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 604 | if (ret->cipher == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 605 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_CIPHER); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 606 | goto err; |
| 607 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 608 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 609 | CBS session_id, master_key; |
| 610 | if (!CBS_get_asn1(&session, &session_id, CBS_ASN1_OCTETSTRING) || |
| 611 | CBS_len(&session_id) > SSL3_MAX_SSL_SESSION_ID_LENGTH || |
| 612 | !CBS_get_asn1(&session, &master_key, CBS_ASN1_OCTETSTRING) || |
| 613 | CBS_len(&master_key) > SSL_MAX_MASTER_KEY_LENGTH) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 614 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 615 | goto err; |
| 616 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 617 | 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] | 618 | ret->session_id_length = CBS_len(&session_id); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 619 | 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] | 620 | ret->master_key_length = CBS_len(&master_key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 621 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 622 | CBS child; |
| 623 | uint64_t time, timeout; |
| 624 | if (!CBS_get_asn1(&session, &child, kTimeTag) || |
| 625 | !CBS_get_asn1_uint64(&child, &time) || |
| 626 | time > LONG_MAX || |
| 627 | !CBS_get_asn1(&session, &child, kTimeoutTag) || |
| 628 | !CBS_get_asn1_uint64(&child, &timeout) || |
| 629 | timeout > LONG_MAX) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 630 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 631 | goto err; |
| 632 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 633 | |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 634 | ret->time = (long)time; |
| 635 | ret->timeout = (long)timeout; |
| 636 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 637 | CBS peer; |
| 638 | int has_peer; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 639 | if (!CBS_get_optional_asn1(&session, &peer, &has_peer, kPeerTag) || |
| 640 | (has_peer && CBS_len(&peer) == 0)) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 641 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 642 | goto err; |
| 643 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 644 | /* |peer| is processed with the certificate chain. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 645 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 646 | if (!SSL_SESSION_parse_bounded_octet_string( |
| 647 | &session, ret->sid_ctx, &ret->sid_ctx_length, sizeof(ret->sid_ctx), |
| 648 | kSessionIDContextTag) || |
| 649 | !SSL_SESSION_parse_long(&session, &ret->verify_result, kVerifyResultTag, |
| 650 | X509_V_OK) || |
| 651 | !SSL_SESSION_parse_string(&session, &ret->tlsext_hostname, |
| 652 | kHostNameTag) || |
| 653 | !SSL_SESSION_parse_string(&session, &ret->psk_identity, |
| 654 | kPSKIdentityTag) || |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 655 | !SSL_SESSION_parse_u32(&session, &ret->tlsext_tick_lifetime_hint, |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 656 | kTicketLifetimeHintTag, 0) || |
| 657 | !SSL_SESSION_parse_octet_string(&session, &ret->tlsext_tick, |
| 658 | &ret->tlsext_ticklen, kTicketTag)) { |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 659 | goto err; |
| 660 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 661 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 662 | if (CBS_peek_asn1_tag(&session, kPeerSHA256Tag)) { |
David Benjamin | 5b7b09c | 2016-11-03 20:32:10 -0400 | [diff] [blame] | 663 | CBS peer_sha256; |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 664 | if (!CBS_get_asn1(&session, &child, kPeerSHA256Tag) || |
| 665 | !CBS_get_asn1(&child, &peer_sha256, CBS_ASN1_OCTETSTRING) || |
| 666 | CBS_len(&peer_sha256) != sizeof(ret->peer_sha256) || |
| 667 | CBS_len(&child) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 668 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 669 | goto err; |
| 670 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 671 | OPENSSL_memcpy(ret->peer_sha256, CBS_data(&peer_sha256), |
| 672 | sizeof(ret->peer_sha256)); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 673 | ret->peer_sha256_valid = 1; |
| 674 | } else { |
| 675 | ret->peer_sha256_valid = 0; |
| 676 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 677 | |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 678 | if (!SSL_SESSION_parse_bounded_octet_string( |
| 679 | &session, ret->original_handshake_hash, |
| 680 | &ret->original_handshake_hash_len, |
| 681 | sizeof(ret->original_handshake_hash), kOriginalHandshakeHashTag) || |
| 682 | !SSL_SESSION_parse_octet_string( |
| 683 | &session, &ret->tlsext_signed_cert_timestamp_list, |
| 684 | &ret->tlsext_signed_cert_timestamp_list_length, |
| 685 | kSignedCertTimestampListTag) || |
| 686 | !SSL_SESSION_parse_octet_string( |
| 687 | &session, &ret->ocsp_response, &ret->ocsp_response_length, |
| 688 | kOCSPResponseTag)) { |
| 689 | goto err; |
| 690 | } |
| 691 | |
| 692 | int extended_master_secret; |
| 693 | if (!CBS_get_optional_asn1_bool(&session, &extended_master_secret, |
| 694 | kExtendedMasterSecretTag, |
| 695 | 0 /* default to false */)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 696 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 697 | goto err; |
| 698 | } |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 699 | ret->extended_master_secret = !!extended_master_secret; |
| 700 | |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 701 | uint32_t value; |
| 702 | if (!SSL_SESSION_parse_u32(&session, &value, kGroupIDTag, 0)) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 703 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 704 | goto err; |
| 705 | } |
| 706 | |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 707 | /* Historically, the group_id field was used for key-exchange-specific |
| 708 | * information. Discard all but the group ID. */ |
| 709 | if (ret->cipher->algorithm_mkey & (SSL_kRSA | SSL_kDHE)) { |
| 710 | value = 0; |
| 711 | } |
| 712 | |
| 713 | if (value > 0xffff) { |
| 714 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 715 | goto err; |
| 716 | } |
| 717 | ret->group_id = (uint16_t)value; |
| 718 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 719 | CBS cert_chain; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 720 | CBS_init(&cert_chain, NULL, 0); |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 721 | int has_cert_chain; |
| 722 | if (!CBS_get_optional_asn1(&session, &cert_chain, &has_cert_chain, |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 723 | kCertChainTag) || |
| 724 | (has_cert_chain && CBS_len(&cert_chain) == 0)) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 725 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 726 | goto err; |
| 727 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 728 | if (has_cert_chain && !has_peer) { |
| 729 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 730 | goto err; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 731 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 732 | if (has_peer || has_cert_chain) { |
| 733 | ret->certs = sk_CRYPTO_BUFFER_new_null(); |
| 734 | if (ret->certs == NULL) { |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 735 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 736 | goto err; |
| 737 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 738 | |
| 739 | if (has_peer) { |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 740 | /* TODO(agl): this should use the |SSL_CTX|'s pool. */ |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 741 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&peer, NULL); |
| 742 | if (buffer == NULL || |
| 743 | !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) { |
| 744 | CRYPTO_BUFFER_free(buffer); |
Adam Langley | e850909 | 2016-11-07 14:24:33 -0800 | [diff] [blame] | 745 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 746 | goto err; |
| 747 | } |
| 748 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 749 | |
| 750 | while (CBS_len(&cert_chain) > 0) { |
| 751 | CBS cert; |
| 752 | if (!CBS_get_any_asn1_element(&cert_chain, &cert, NULL, NULL) || |
| 753 | CBS_len(&cert) == 0) { |
| 754 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 755 | goto err; |
| 756 | } |
| 757 | |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 758 | /* TODO(agl): this should use the |SSL_CTX|'s pool. */ |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 759 | CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new_from_CBS(&cert, NULL); |
| 760 | if (buffer == NULL || |
| 761 | !sk_CRYPTO_BUFFER_push(ret->certs, buffer)) { |
| 762 | CRYPTO_BUFFER_free(buffer); |
| 763 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 764 | goto err; |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if (!ssl_session_x509_cache_objects(ret)) { |
| 770 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 771 | goto err; |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 772 | } |
| 773 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 774 | CBS age_add; |
| 775 | int age_add_present; |
Steven Valdez | 5b98608 | 2016-09-01 12:29:49 -0400 | [diff] [blame] | 776 | 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] | 777 | kTicketAgeAddTag) || |
| 778 | (age_add_present && |
| 779 | !CBS_get_u32(&age_add, &ret->ticket_age_add)) || |
| 780 | CBS_len(&age_add) != 0) { |
| 781 | goto err; |
| 782 | } |
| 783 | ret->ticket_age_add_valid = age_add_present; |
| 784 | |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 785 | int is_server; |
| 786 | if (!CBS_get_optional_asn1_bool(&session, &is_server, kIsServerTag, |
| 787 | 1 /* default to true */)) { |
| 788 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 789 | goto err; |
| 790 | } |
| 791 | /* TODO: in time we can include |is_server| for servers too, then we can |
| 792 | enforce that client and server sessions are never mixed up. */ |
| 793 | |
| 794 | ret->is_server = is_server; |
| 795 | |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 796 | if (!SSL_SESSION_parse_u16(&session, &ret->peer_signature_algorithm, |
| 797 | kPeerSignatureAlgorithmTag, 0) || |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 798 | !SSL_SESSION_parse_u32(&session, &ret->ticket_max_early_data, |
| 799 | kTicketMaxEarlyDataTag, 0) || |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 800 | !SSL_SESSION_parse_long(&session, &ret->auth_timeout, kAuthTimeoutTag, |
| 801 | ret->timeout) || |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 802 | CBS_len(&session) != 0) { |
David Benjamin | 8be79a3 | 2015-08-23 01:37:36 -0400 | [diff] [blame] | 803 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
| 804 | goto err; |
| 805 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 806 | |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 807 | return ret; |
| 808 | |
| 809 | err: |
| 810 | SSL_SESSION_free(ret); |
| 811 | return NULL; |
| 812 | } |
| 813 | |
| 814 | SSL_SESSION *SSL_SESSION_from_bytes(const uint8_t *in, size_t in_len) { |
| 815 | CBS cbs; |
| 816 | CBS_init(&cbs, in, in_len); |
| 817 | SSL_SESSION *ret = SSL_SESSION_parse(&cbs); |
| 818 | if (ret == NULL) { |
| 819 | return NULL; |
| 820 | } |
| 821 | if (CBS_len(&cbs) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 822 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SSL_SESSION); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 823 | SSL_SESSION_free(ret); |
| 824 | return NULL; |
| 825 | } |
| 826 | return ret; |
| 827 | } |
| 828 | |
| 829 | SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) { |
| 830 | if (length < 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 831 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 832 | return NULL; |
| 833 | } |
| 834 | |
| 835 | CBS cbs; |
| 836 | CBS_init(&cbs, *pp, length); |
| 837 | |
| 838 | SSL_SESSION *ret = SSL_SESSION_parse(&cbs); |
| 839 | if (ret == NULL) { |
| 840 | return NULL; |
| 841 | } |
| 842 | |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 843 | if (a) { |
David Benjamin | fd67aa8 | 2015-06-15 19:41:48 -0400 | [diff] [blame] | 844 | SSL_SESSION_free(*a); |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 845 | *a = ret; |
| 846 | } |
| 847 | *pp = CBS_data(&cbs); |
| 848 | return ret; |
David Benjamin | 83fd6b6 | 2014-10-19 04:33:38 -0400 | [diff] [blame] | 849 | } |