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