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