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