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 (c) 1998-2006 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2005 Nokia. All rights reserved. |
| 112 | * |
| 113 | * The portions of the attached software ("Contribution") is developed by |
| 114 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 115 | * license. |
| 116 | * |
| 117 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 118 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 119 | * support (see RFC 4279) to OpenSSL. |
| 120 | * |
| 121 | * No patent licenses or other rights except those expressly stated in |
| 122 | * the OpenSSL open source license shall be deemed granted or received |
| 123 | * expressly, by implication, estoppel, or otherwise. |
| 124 | * |
| 125 | * No assurances are provided by Nokia that the Contribution does not |
| 126 | * infringe the patent or other intellectual property rights of any third |
| 127 | * party or that the license provides you with all the necessary rights |
| 128 | * to make use of the Contribution. |
| 129 | * |
| 130 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 131 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 132 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 133 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 134 | * OTHERWISE. */ |
| 135 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 136 | #include <openssl/ssl.h> |
| 137 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 138 | #include <assert.h> |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 139 | #include <stdlib.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 140 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 141 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 142 | #include <utility> |
| 143 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | #include <openssl/err.h> |
David Benjamin | 9a4876e | 2017-09-15 18:22:43 -0400 | [diff] [blame] | 145 | #include <openssl/hmac.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 146 | #include <openssl/lhash.h> |
| 147 | #include <openssl/mem.h> |
| 148 | #include <openssl/rand.h> |
| 149 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 150 | #include "internal.h" |
David Benjamin | 546f1a5 | 2015-04-15 16:46:09 -0400 | [diff] [blame] | 151 | #include "../crypto/internal.h" |
| 152 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 154 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 155 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 156 | // The address of this is a magic value, a pointer to which is returned by |
| 157 | // SSL_magic_pending_session_ptr(). It allows a session callback to indicate |
| 158 | // that it needs to asynchronously fetch session information. |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 159 | static const char g_pending_session_magic = 0; |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 160 | |
David Benjamin | aa58513 | 2015-06-29 23:36:17 -0400 | [diff] [blame] | 161 | static CRYPTO_EX_DATA_CLASS g_ex_data_class = |
| 162 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 163 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 164 | static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session); |
| 165 | static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 166 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 167 | UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 168 | return MakeUnique<SSL_SESSION>(x509_method); |
David Benjamin | abb6c1c | 2015-09-13 14:46:26 -0400 | [diff] [blame] | 169 | } |
| 170 | |
David Benjamin | 58150ed | 2018-06-29 23:58:43 -0400 | [diff] [blame] | 171 | uint32_t ssl_hash_session_id(Span<const uint8_t> session_id) { |
| 172 | // Take the first four bytes of |session_id|. Session IDs are generated by the |
| 173 | // server randomly, so we can assume even using the first four bytes results |
| 174 | // in a good distribution. |
| 175 | uint8_t tmp_storage[sizeof(uint32_t)]; |
| 176 | if (session_id.size() < sizeof(tmp_storage)) { |
| 177 | OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage)); |
| 178 | OPENSSL_memcpy(tmp_storage, session_id.data(), session_id.size()); |
| 179 | session_id = tmp_storage; |
| 180 | } |
| 181 | |
| 182 | uint32_t hash = |
| 183 | ((uint32_t)session_id[0]) | |
| 184 | ((uint32_t)session_id[1] << 8) | |
| 185 | ((uint32_t)session_id[2] << 16) | |
| 186 | ((uint32_t)session_id[3] << 24); |
| 187 | |
| 188 | return hash; |
| 189 | } |
| 190 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 191 | UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session, int dup_flags) { |
| 192 | UniquePtr<SSL_SESSION> new_session = ssl_session_new(session->x509_method); |
| 193 | if (!new_session) { |
| 194 | return nullptr; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 195 | } |
| 196 | |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 197 | new_session->is_server = session->is_server; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 198 | new_session->ssl_version = session->ssl_version; |
Nick Harper | 6bfd25c | 2020-03-30 17:15:19 -0700 | [diff] [blame] | 199 | new_session->is_quic = session->is_quic; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 200 | new_session->sid_ctx_length = session->sid_ctx_length; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 201 | OPENSSL_memcpy(new_session->sid_ctx, session->sid_ctx, session->sid_ctx_length); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 202 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 203 | // Copy the key material. |
David Benjamin | 5351c8b | 2020-11-19 00:25:29 -0500 | [diff] [blame] | 204 | new_session->secret_length = session->secret_length; |
| 205 | OPENSSL_memcpy(new_session->secret, session->secret, session->secret_length); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 206 | new_session->cipher = session->cipher; |
| 207 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 208 | // Copy authentication state. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 209 | if (session->psk_identity != nullptr) { |
David Benjamin | 3ba9586 | 2019-10-21 16:14:33 -0400 | [diff] [blame] | 210 | new_session->psk_identity.reset( |
| 211 | OPENSSL_strdup(session->psk_identity.get())); |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 212 | if (new_session->psk_identity == nullptr) { |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 213 | return nullptr; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 214 | } |
| 215 | } |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 216 | if (session->certs != nullptr) { |
David Benjamin | df8a55b | 2023-01-04 15:52:36 -0800 | [diff] [blame] | 217 | auto buf_up_ref = [](const CRYPTO_BUFFER *buf) { |
| 218 | CRYPTO_BUFFER_up_ref(const_cast<CRYPTO_BUFFER *>(buf)); |
| 219 | return const_cast<CRYPTO_BUFFER*>(buf); |
David Benjamin | 2908dd1 | 2018-06-29 17:46:42 -0400 | [diff] [blame] | 220 | }; |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 221 | new_session->certs.reset(sk_CRYPTO_BUFFER_deep_copy( |
| 222 | session->certs.get(), buf_up_ref, CRYPTO_BUFFER_free)); |
| 223 | if (new_session->certs == nullptr) { |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 224 | return nullptr; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 225 | } |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 226 | } |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame] | 227 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 228 | if (!session->x509_method->session_dup(new_session.get(), session)) { |
| 229 | return nullptr; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 230 | } |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame] | 231 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 232 | new_session->verify_result = session->verify_result; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 233 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 234 | new_session->ocsp_response = UpRef(session->ocsp_response); |
| 235 | new_session->signed_cert_timestamp_list = |
| 236 | UpRef(session->signed_cert_timestamp_list); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 237 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 238 | OPENSSL_memcpy(new_session->peer_sha256, session->peer_sha256, |
| 239 | SHA256_DIGEST_LENGTH); |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 240 | new_session->peer_sha256_valid = session->peer_sha256_valid; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 241 | |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 242 | new_session->peer_signature_algorithm = session->peer_signature_algorithm; |
| 243 | |
David Benjamin | 01a9057 | 2016-09-22 00:11:43 -0400 | [diff] [blame] | 244 | new_session->timeout = session->timeout; |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 245 | new_session->auth_timeout = session->auth_timeout; |
David Benjamin | 01a9057 | 2016-09-22 00:11:43 -0400 | [diff] [blame] | 246 | new_session->time = session->time; |
| 247 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 248 | // Copy non-authentication connection properties. |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 249 | if (dup_flags & SSL_SESSION_INCLUDE_NONAUTH) { |
| 250 | new_session->session_id_length = session->session_id_length; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 251 | OPENSSL_memcpy(new_session->session_id, session->session_id, |
| 252 | session->session_id_length); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 253 | |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 254 | new_session->group_id = session->group_id; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 255 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 256 | OPENSSL_memcpy(new_session->original_handshake_hash, |
| 257 | session->original_handshake_hash, |
| 258 | session->original_handshake_hash_len); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 259 | new_session->original_handshake_hash_len = |
| 260 | session->original_handshake_hash_len; |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 261 | new_session->ticket_lifetime_hint = session->ticket_lifetime_hint; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 262 | new_session->ticket_age_add = session->ticket_age_add; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 263 | new_session->ticket_max_early_data = session->ticket_max_early_data; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 264 | new_session->extended_master_secret = session->extended_master_secret; |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 265 | new_session->has_application_settings = session->has_application_settings; |
Steven Valdez | 27a9e6a | 2017-02-14 13:20:40 -0500 | [diff] [blame] | 266 | |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 267 | if (!new_session->early_alpn.CopyFrom(session->early_alpn) || |
| 268 | !new_session->quic_early_data_context.CopyFrom( |
| 269 | session->quic_early_data_context) || |
| 270 | !new_session->local_application_settings.CopyFrom( |
| 271 | session->local_application_settings) || |
| 272 | !new_session->peer_application_settings.CopyFrom( |
| 273 | session->peer_application_settings)) { |
Nick Harper | 7c52299 | 2020-04-30 14:15:49 -0700 | [diff] [blame] | 274 | return nullptr; |
| 275 | } |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 276 | } |
| 277 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 278 | // Copy the ticket. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 279 | if (dup_flags & SSL_SESSION_INCLUDE_TICKET && |
| 280 | !new_session->ticket.CopyFrom(session->ticket)) { |
| 281 | return nullptr; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 282 | } |
| 283 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 284 | // The new_session does not get a copy of the ex_data. |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 285 | |
David Benjamin | a3a71e9 | 2018-06-29 13:24:45 -0400 | [diff] [blame] | 286 | new_session->not_resumable = true; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 287 | return new_session; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 288 | } |
| 289 | |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 290 | void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session) { |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 291 | struct OPENSSL_timeval now; |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 292 | ssl_get_current_time(ssl, &now); |
| 293 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 294 | // To avoid overflows and underflows, if we've gone back in time, update the |
| 295 | // time, but mark the session expired. |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 296 | if (session->time > now.tv_sec) { |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 297 | session->time = now.tv_sec; |
| 298 | session->timeout = 0; |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 299 | session->auth_timeout = 0; |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 300 | return; |
| 301 | } |
| 302 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 303 | // Adjust the session time and timeouts. If the session has already expired, |
| 304 | // clamp the timeouts at zero. |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 305 | uint64_t delta = now.tv_sec - session->time; |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 306 | session->time = now.tv_sec; |
| 307 | if (session->timeout < delta) { |
| 308 | session->timeout = 0; |
| 309 | } else { |
| 310 | session->timeout -= delta; |
| 311 | } |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 312 | if (session->auth_timeout < delta) { |
| 313 | session->auth_timeout = 0; |
| 314 | } else { |
| 315 | session->auth_timeout -= delta; |
| 316 | } |
| 317 | } |
| 318 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 319 | void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session, |
| 320 | uint32_t timeout) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 321 | // Rebase the timestamp relative to the current time so |timeout| is measured |
| 322 | // correctly. |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 323 | ssl_session_rebase_time(ssl, session); |
| 324 | |
| 325 | if (session->timeout > timeout) { |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | session->timeout = timeout; |
| 330 | if (session->timeout > session->auth_timeout) { |
| 331 | session->timeout = session->auth_timeout; |
| 332 | } |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 333 | } |
| 334 | |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 335 | uint16_t ssl_session_protocol_version(const SSL_SESSION *session) { |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 336 | uint16_t ret; |
| 337 | if (!ssl_protocol_version_from_wire(&ret, session->ssl_version)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 338 | // An |SSL_SESSION| will never have an invalid version. This is enforced by |
| 339 | // the parser. |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 340 | assert(0); |
| 341 | return 0; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 342 | } |
| 343 | |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 344 | return ret; |
| 345 | } |
| 346 | |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 347 | const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session) { |
| 348 | return ssl_get_handshake_digest(ssl_session_protocol_version(session), |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 349 | session->cipher); |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 350 | } |
| 351 | |
David Benjamin | 962b375 | 2021-05-10 15:17:18 -0400 | [diff] [blame] | 352 | bool ssl_get_new_session(SSL_HANDSHAKE *hs) { |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 353 | SSL *const ssl = hs->ssl; |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 354 | if (ssl->mode & SSL_MODE_NO_SESSION_CREATION) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 355 | OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_MAY_NOT_BE_CREATED); |
David Benjamin | 962b375 | 2021-05-10 15:17:18 -0400 | [diff] [blame] | 356 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 357 | } |
| 358 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 359 | UniquePtr<SSL_SESSION> session = ssl_session_new(ssl->ctx->x509_method); |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 360 | if (session == NULL) { |
David Benjamin | 962b375 | 2021-05-10 15:17:18 -0400 | [diff] [blame] | 361 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 362 | } |
| 363 | |
David Benjamin | 962b375 | 2021-05-10 15:17:18 -0400 | [diff] [blame] | 364 | session->is_server = ssl->server; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 365 | session->ssl_version = ssl->version; |
Nick Harper | 6bfd25c | 2020-03-30 17:15:19 -0700 | [diff] [blame] | 366 | session->is_quic = ssl->quic_method != nullptr; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 367 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 368 | // Fill in the time from the |SSL_CTX|'s clock. |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 369 | struct OPENSSL_timeval now; |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame] | 370 | ssl_get_current_time(ssl, &now); |
| 371 | session->time = now.tv_sec; |
| 372 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 373 | uint16_t version = ssl_protocol_version(ssl); |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 374 | if (version >= TLS1_3_VERSION) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 375 | // TLS 1.3 uses tickets as authenticators, so we are willing to use them for |
| 376 | // longer. |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 377 | session->timeout = ssl->session_ctx->session_psk_dhe_timeout; |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 378 | session->auth_timeout = SSL_DEFAULT_SESSION_AUTH_TIMEOUT; |
| 379 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 380 | // TLS 1.2 resumption does not incorporate new key material, so we use a |
| 381 | // much shorter timeout. |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 382 | session->timeout = ssl->session_ctx->session_timeout; |
| 383 | session->auth_timeout = ssl->session_ctx->session_timeout; |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 384 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 385 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 386 | if (hs->config->cert->sid_ctx_length > sizeof(session->sid_ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 387 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 962b375 | 2021-05-10 15:17:18 -0400 | [diff] [blame] | 388 | return false; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 389 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 390 | OPENSSL_memcpy(session->sid_ctx, hs->config->cert->sid_ctx, |
| 391 | hs->config->cert->sid_ctx_length); |
| 392 | session->sid_ctx_length = hs->config->cert->sid_ctx_length; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 393 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 394 | // The session is marked not resumable until it is completely filled in. |
David Benjamin | a3a71e9 | 2018-06-29 13:24:45 -0400 | [diff] [blame] | 395 | session->not_resumable = true; |
Adam Langley | 9498e74 | 2016-07-18 10:17:16 -0700 | [diff] [blame] | 396 | session->verify_result = X509_V_ERR_INVALID_CALL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 397 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 398 | hs->new_session = std::move(session); |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 399 | ssl_set_session(ssl, NULL); |
David Benjamin | 962b375 | 2021-05-10 15:17:18 -0400 | [diff] [blame] | 400 | return true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 401 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 403 | bool ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx) { |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 404 | OPENSSL_timeval now; |
| 405 | ssl_ctx_get_current_time(ctx, &now); |
| 406 | { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 407 | // Avoid acquiring a write lock in the common case (i.e. a non-default key |
| 408 | // is used or the default keys have not expired yet). |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 409 | MutexReadLock lock(&ctx->lock); |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 410 | if (ctx->ticket_key_current && |
| 411 | (ctx->ticket_key_current->next_rotation_tv_sec == 0 || |
| 412 | ctx->ticket_key_current->next_rotation_tv_sec > now.tv_sec) && |
| 413 | (!ctx->ticket_key_prev || |
| 414 | ctx->ticket_key_prev->next_rotation_tv_sec > now.tv_sec)) { |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 415 | return true; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | MutexWriteLock lock(&ctx->lock); |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 420 | if (!ctx->ticket_key_current || |
| 421 | (ctx->ticket_key_current->next_rotation_tv_sec != 0 && |
| 422 | ctx->ticket_key_current->next_rotation_tv_sec <= now.tv_sec)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 423 | // The current key has not been initialized or it is expired. |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 424 | auto new_key = bssl::MakeUnique<TicketKey>(); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 425 | if (!new_key) { |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 426 | return false; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 427 | } |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 428 | RAND_bytes(new_key->name, 16); |
| 429 | RAND_bytes(new_key->hmac_key, 16); |
| 430 | RAND_bytes(new_key->aes_key, 16); |
| 431 | new_key->next_rotation_tv_sec = |
| 432 | now.tv_sec + SSL_DEFAULT_TICKET_KEY_ROTATION_INTERVAL; |
| 433 | if (ctx->ticket_key_current) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 434 | // The current key expired. Rotate it to prev and bump up its rotation |
| 435 | // timestamp. Note that even with the new rotation time it may still be |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 436 | // expired and get dropped below. |
| 437 | ctx->ticket_key_current->next_rotation_tv_sec += |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 438 | SSL_DEFAULT_TICKET_KEY_ROTATION_INTERVAL; |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 439 | ctx->ticket_key_prev = std::move(ctx->ticket_key_current); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 440 | } |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 441 | ctx->ticket_key_current = std::move(new_key); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 442 | } |
| 443 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 444 | // Drop an expired prev key. |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 445 | if (ctx->ticket_key_prev && |
| 446 | ctx->ticket_key_prev->next_rotation_tv_sec <= now.tv_sec) { |
| 447 | ctx->ticket_key_prev.reset(); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 448 | } |
| 449 | |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 450 | return true; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 453 | static int ssl_encrypt_ticket_with_cipher_ctx(SSL_HANDSHAKE *hs, CBB *out, |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 454 | const uint8_t *session_buf, |
| 455 | size_t session_len) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 456 | ScopedEVP_CIPHER_CTX ctx; |
| 457 | ScopedHMAC_CTX hctx; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 458 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 459 | // If the session is too long, emit a dummy value rather than abort the |
| 460 | // connection. |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 461 | static const size_t kMaxTicketOverhead = |
| 462 | 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE; |
| 463 | if (session_len > 0xffff - kMaxTicketOverhead) { |
| 464 | static const char kTicketPlaceholder[] = "TICKET TOO LARGE"; |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 465 | return CBB_add_bytes(out, (const uint8_t *)kTicketPlaceholder, |
| 466 | strlen(kTicketPlaceholder)); |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 467 | } |
| 468 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 469 | // Initialize HMAC and cipher contexts. If callback present it does all the |
| 470 | // work otherwise use generated values from parent ctx. |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 471 | SSL_CTX *tctx = hs->ssl->session_ctx.get(); |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 472 | uint8_t iv[EVP_MAX_IV_LENGTH]; |
| 473 | uint8_t key_name[16]; |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 474 | if (tctx->ticket_key_cb != NULL) { |
| 475 | if (tctx->ticket_key_cb(hs->ssl, key_name, iv, ctx.get(), hctx.get(), |
| 476 | 1 /* encrypt */) < 0) { |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 477 | return 0; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 478 | } |
| 479 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 480 | // Rotate ticket key if necessary. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 481 | if (!ssl_ctx_rotate_ticket_encryption_key(tctx)) { |
| 482 | return 0; |
| 483 | } |
| 484 | MutexReadLock lock(&tctx->lock); |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 485 | if (!RAND_bytes(iv, 16) || |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 486 | !EVP_EncryptInit_ex(ctx.get(), EVP_aes_128_cbc(), NULL, |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 487 | tctx->ticket_key_current->aes_key, iv) || |
| 488 | !HMAC_Init_ex(hctx.get(), tctx->ticket_key_current->hmac_key, 16, |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 489 | tlsext_tick_md(), NULL)) { |
| 490 | return 0; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 491 | } |
David Benjamin | 7bb0fbf | 2018-07-03 13:55:42 -0400 | [diff] [blame] | 492 | OPENSSL_memcpy(key_name, tctx->ticket_key_current->name, 16); |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | uint8_t *ptr; |
| 496 | if (!CBB_add_bytes(out, key_name, 16) || |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 497 | !CBB_add_bytes(out, iv, EVP_CIPHER_CTX_iv_length(ctx.get())) || |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 498 | !CBB_reserve(out, &ptr, session_len + EVP_MAX_BLOCK_LENGTH)) { |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 499 | return 0; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 500 | } |
| 501 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 502 | size_t total = 0; |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 503 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 504 | OPENSSL_memcpy(ptr, session_buf, session_len); |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 505 | total = session_len; |
| 506 | #else |
| 507 | int len; |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 508 | if (!EVP_EncryptUpdate(ctx.get(), ptr + total, &len, session_buf, session_len)) { |
| 509 | return 0; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 510 | } |
| 511 | total += len; |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 512 | if (!EVP_EncryptFinal_ex(ctx.get(), ptr + total, &len)) { |
| 513 | return 0; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 514 | } |
| 515 | total += len; |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 516 | #endif |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 517 | if (!CBB_did_write(out, total)) { |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 518 | return 0; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | unsigned hlen; |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 522 | if (!HMAC_Update(hctx.get(), CBB_data(out), CBB_len(out)) || |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 523 | !CBB_reserve(out, &ptr, EVP_MAX_MD_SIZE) || |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 524 | !HMAC_Final(hctx.get(), ptr, &hlen) || |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 525 | !CBB_did_write(out, hlen)) { |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 526 | return 0; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 527 | } |
| 528 | |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 529 | return 1; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 530 | } |
| 531 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 532 | static int ssl_encrypt_ticket_with_method(SSL_HANDSHAKE *hs, CBB *out, |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 533 | const uint8_t *session_buf, |
| 534 | size_t session_len) { |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 535 | SSL *const ssl = hs->ssl; |
| 536 | const SSL_TICKET_AEAD_METHOD *method = ssl->session_ctx->ticket_aead_method; |
| 537 | const size_t max_overhead = method->max_overhead(ssl); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 538 | const size_t max_out = session_len + max_overhead; |
| 539 | if (max_out < max_overhead) { |
| 540 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | uint8_t *ptr; |
| 545 | if (!CBB_reserve(out, &ptr, max_out)) { |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | size_t out_len; |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 550 | if (!method->seal(ssl, ptr, &out_len, max_out, session_buf, |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 551 | session_len)) { |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 552 | OPENSSL_PUT_ERROR(SSL, SSL_R_TICKET_ENCRYPTION_FAILED); |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | if (!CBB_did_write(out, out_len)) { |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | return 1; |
| 561 | } |
| 562 | |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 563 | bool ssl_encrypt_ticket(SSL_HANDSHAKE *hs, CBB *out, |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 564 | const SSL_SESSION *session) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 565 | // Serialize the SSL_SESSION to be encoded into the ticket. |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 566 | uint8_t *session_buf = nullptr; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 567 | size_t session_len; |
| 568 | if (!SSL_SESSION_to_bytes_for_ticket(session, &session_buf, &session_len)) { |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 569 | return false; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 570 | } |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 571 | bssl::UniquePtr<uint8_t> free_session_buf(session_buf); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 572 | |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 573 | if (hs->ssl->session_ctx->ticket_aead_method) { |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 574 | return ssl_encrypt_ticket_with_method(hs, out, session_buf, session_len); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 575 | } else { |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 576 | return ssl_encrypt_ticket_with_cipher_ctx(hs, out, session_buf, |
| 577 | session_len); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 578 | } |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 579 | } |
| 580 | |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 581 | bool ssl_session_is_context_valid(const SSL_HANDSHAKE *hs, |
| 582 | const SSL_SESSION *session) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 583 | if (session == NULL) { |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 584 | return false; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 585 | } |
| 586 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 587 | return session->sid_ctx_length == hs->config->cert->sid_ctx_length && |
| 588 | OPENSSL_memcmp(session->sid_ctx, hs->config->cert->sid_ctx, |
| 589 | hs->config->cert->sid_ctx_length) == 0; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 590 | } |
| 591 | |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 592 | bool ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 593 | if (session == NULL) { |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 594 | return false; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 595 | } |
| 596 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 597 | struct OPENSSL_timeval now; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 598 | ssl_get_current_time(ssl, &now); |
David Benjamin | 1b22f85 | 2016-10-27 16:36:32 -0400 | [diff] [blame] | 599 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 600 | // Reject tickets from the future to avoid underflow. |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 601 | if (now.tv_sec < session->time) { |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 602 | return false; |
David Benjamin | 1b22f85 | 2016-10-27 16:36:32 -0400 | [diff] [blame] | 603 | } |
| 604 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 605 | return session->timeout > now.tv_sec - session->time; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 606 | } |
| 607 | |
David Benjamin | 6e723e5 | 2023-03-29 19:28:46 +0900 | [diff] [blame] | 608 | bool ssl_session_is_resumable(const SSL_HANDSHAKE *hs, |
| 609 | const SSL_SESSION *session) { |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 610 | const SSL *const ssl = hs->ssl; |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 611 | return ssl_session_is_context_valid(hs, session) && |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 612 | // The session must have been created by the same type of end point as |
| 613 | // we're now using it with. |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 614 | ssl->server == session->is_server && |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 615 | // The session must not be expired. |
David Benjamin | 75f9914 | 2016-11-12 12:36:06 +0900 | [diff] [blame] | 616 | ssl_session_is_time_valid(ssl, session) && |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 617 | // Only resume if the session's version matches the negotiated |
| 618 | // version. |
David Benjamin | 75f9914 | 2016-11-12 12:36:06 +0900 | [diff] [blame] | 619 | ssl->version == session->ssl_version && |
David Benjamin | 754d4c9 | 2020-02-11 16:27:21 -0500 | [diff] [blame] | 620 | // Only resume if the session's cipher matches the negotiated one. This |
| 621 | // is stricter than necessary for TLS 1.3, which allows cross-cipher |
| 622 | // resumption if the PRF hashes match. We require an exact match for |
| 623 | // simplicity. If loosening this, the 0-RTT accept logic must be |
| 624 | // updated to check the cipher. |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 625 | hs->new_cipher == session->cipher && |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 626 | // If the session contains a client certificate (either the full |
| 627 | // certificate or just the hash) then require that the form of the |
| 628 | // certificate matches the current configuration. |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 629 | ((sk_CRYPTO_BUFFER_num(session->certs.get()) == 0 && |
Adam Langley | 46db7af | 2017-02-01 15:49:37 -0800 | [diff] [blame] | 630 | !session->peer_sha256_valid) || |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame] | 631 | session->peer_sha256_valid == |
Nick Harper | 6bfd25c | 2020-03-30 17:15:19 -0700 | [diff] [blame] | 632 | hs->config->retain_only_sha256_of_client_certs) && |
| 633 | // Only resume if the underlying transport protocol hasn't changed. |
| 634 | // This is to prevent cross-protocol resumption between QUIC and TCP. |
| 635 | (hs->ssl->quic_method != nullptr) == session->is_quic; |
David Benjamin | 75f9914 | 2016-11-12 12:36:06 +0900 | [diff] [blame] | 636 | } |
| 637 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 638 | // ssl_lookup_session looks up |session_id| in the session cache and sets |
| 639 | // |*out_session| to an |SSL_SESSION| object if found. |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 640 | static enum ssl_hs_wait_t ssl_lookup_session( |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 641 | SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session, |
David Benjamin | 58150ed | 2018-06-29 23:58:43 -0400 | [diff] [blame] | 642 | Span<const uint8_t> session_id) { |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 643 | SSL *const ssl = hs->ssl; |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 644 | out_session->reset(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 645 | |
David Benjamin | 58150ed | 2018-06-29 23:58:43 -0400 | [diff] [blame] | 646 | if (session_id.empty() || session_id.size() > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 647 | return ssl_hs_ok; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 648 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 649 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 650 | UniquePtr<SSL_SESSION> session; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 651 | // Try the internal cache, if it exists. |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 652 | if (!(ssl->session_ctx->session_cache_mode & |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 653 | SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) { |
David Benjamin | 58150ed | 2018-06-29 23:58:43 -0400 | [diff] [blame] | 654 | uint32_t hash = ssl_hash_session_id(session_id); |
| 655 | auto cmp = [](const void *key, const SSL_SESSION *sess) -> int { |
| 656 | Span<const uint8_t> key_id = |
| 657 | *reinterpret_cast<const Span<const uint8_t> *>(key); |
| 658 | Span<const uint8_t> sess_id = |
| 659 | MakeConstSpan(sess->session_id, sess->session_id_length); |
| 660 | return key_id == sess_id ? 0 : 1; |
| 661 | }; |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 662 | MutexReadLock lock(&ssl->session_ctx->lock); |
David Benjamin | 58150ed | 2018-06-29 23:58:43 -0400 | [diff] [blame] | 663 | // |lh_SSL_SESSION_retrieve_key| returns a non-owning pointer. |
| 664 | session = UpRef(lh_SSL_SESSION_retrieve_key(ssl->session_ctx->sessions, |
| 665 | &session_id, hash, cmp)); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 666 | // TODO(davidben): This should probably move it to the front of the list. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 667 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 668 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 669 | // Fall back to the external cache, if it exists. |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 670 | if (!session && ssl->session_ctx->get_session_cb != nullptr) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 671 | int copy = 1; |
David Benjamin | 58150ed | 2018-06-29 23:58:43 -0400 | [diff] [blame] | 672 | session.reset(ssl->session_ctx->get_session_cb(ssl, session_id.data(), |
| 673 | session_id.size(), ©)); |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 674 | if (!session) { |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 675 | return ssl_hs_ok; |
Adam Langley | 81f4335 | 2016-08-26 09:22:56 -0700 | [diff] [blame] | 676 | } |
| 677 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 678 | if (session.get() == SSL_magic_pending_session_ptr()) { |
| 679 | session.release(); // This pointer is not actually owned. |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 680 | return ssl_hs_pending_session; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 681 | } |
| 682 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 683 | // Increment reference count now if the session callback asks us to do so |
| 684 | // (note that if the session structures returned by the callback are shared |
| 685 | // between threads, it must handle the reference count itself [i.e. copy == |
| 686 | // 0], or things won't be thread-safe). |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 687 | if (copy) { |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 688 | SSL_SESSION_up_ref(session.get()); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 689 | } |
| 690 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 691 | // Add the externally cached session to the internal cache if necessary. |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 692 | if (!(ssl->session_ctx->session_cache_mode & |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 693 | SSL_SESS_CACHE_NO_INTERNAL_STORE)) { |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 694 | SSL_CTX_add_session(ssl->session_ctx.get(), session.get()); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 695 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 696 | } |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 697 | |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 698 | if (session && !ssl_session_is_time_valid(ssl, session.get())) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 699 | // The session was from the cache, so remove it. |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 700 | SSL_CTX_remove_session(ssl->session_ctx.get(), session.get()); |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 701 | session.reset(); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 702 | } |
| 703 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 704 | *out_session = std::move(session); |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 705 | return ssl_hs_ok; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 708 | enum ssl_hs_wait_t ssl_get_prev_session(SSL_HANDSHAKE *hs, |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 709 | UniquePtr<SSL_SESSION> *out_session, |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 710 | bool *out_tickets_supported, |
| 711 | bool *out_renew_ticket, |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 712 | const SSL_CLIENT_HELLO *client_hello) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 713 | // This is used only by servers. |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 714 | assert(hs->ssl->server); |
David Benjamin | 15868b3 | 2017-09-26 17:46:58 -0400 | [diff] [blame] | 715 | UniquePtr<SSL_SESSION> session; |
| 716 | bool renew_ticket = false; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 717 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 718 | // If tickets are disabled, always behave as if no tickets are present. |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 719 | CBS ticket; |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 720 | const bool tickets_supported = |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 721 | !(SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) && |
David Benjamin | 2865567 | 2018-07-18 23:23:25 -0400 | [diff] [blame] | 722 | ssl_client_hello_get_extension(client_hello, &ticket, |
| 723 | TLSEXT_TYPE_session_ticket); |
| 724 | if (tickets_supported && CBS_len(&ticket) != 0) { |
| 725 | switch (ssl_process_ticket(hs, &session, &renew_ticket, ticket, |
| 726 | MakeConstSpan(client_hello->session_id, |
| 727 | client_hello->session_id_len))) { |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 728 | case ssl_ticket_aead_success: |
| 729 | break; |
| 730 | case ssl_ticket_aead_ignore_ticket: |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 731 | assert(!session); |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 732 | break; |
| 733 | case ssl_ticket_aead_error: |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 734 | return ssl_hs_error; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 735 | case ssl_ticket_aead_retry: |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 736 | return ssl_hs_pending_ticket; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 737 | } |
| 738 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 739 | // The client didn't send a ticket, so the session ID is a real ID. |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 740 | enum ssl_hs_wait_t lookup_ret = ssl_lookup_session( |
David Benjamin | 58150ed | 2018-06-29 23:58:43 -0400 | [diff] [blame] | 741 | hs, &session, |
| 742 | MakeConstSpan(client_hello->session_id, client_hello->session_id_len)); |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 743 | if (lookup_ret != ssl_hs_ok) { |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 744 | return lookup_ret; |
| 745 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 746 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 747 | |
David Benjamin | 37af90f | 2017-07-29 01:42:16 -0400 | [diff] [blame] | 748 | *out_session = std::move(session); |
David Benjamin | 75f9914 | 2016-11-12 12:36:06 +0900 | [diff] [blame] | 749 | *out_tickets_supported = tickets_supported; |
| 750 | *out_renew_ticket = renew_ticket; |
Steven Valdez | 398085b | 2017-08-29 13:25:56 -0400 | [diff] [blame] | 751 | return ssl_hs_ok; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 752 | } |
| 753 | |
David Benjamin | a10017c | 2021-06-16 16:00:13 -0400 | [diff] [blame] | 754 | static bool remove_session(SSL_CTX *ctx, SSL_SESSION *session, bool lock) { |
| 755 | if (session == nullptr || session->session_id_length == 0) { |
| 756 | return false; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 757 | } |
| 758 | |
David Benjamin | a10017c | 2021-06-16 16:00:13 -0400 | [diff] [blame] | 759 | if (lock) { |
| 760 | CRYPTO_MUTEX_lock_write(&ctx->lock); |
| 761 | } |
| 762 | |
| 763 | SSL_SESSION *found_session = lh_SSL_SESSION_retrieve(ctx->sessions, session); |
| 764 | bool found = found_session == session; |
| 765 | if (found) { |
| 766 | found_session = lh_SSL_SESSION_delete(ctx->sessions, session); |
| 767 | SSL_SESSION_list_remove(ctx, session); |
| 768 | } |
| 769 | |
| 770 | if (lock) { |
| 771 | CRYPTO_MUTEX_unlock_write(&ctx->lock); |
| 772 | } |
| 773 | |
| 774 | if (found) { |
| 775 | // TODO(https://crbug.com/boringssl/251): Callbacks should not be called |
| 776 | // under a lock. |
| 777 | if (ctx->remove_session_cb != nullptr) { |
| 778 | ctx->remove_session_cb(ctx, found_session); |
| 779 | } |
| 780 | SSL_SESSION_free(found_session); |
| 781 | } |
| 782 | |
| 783 | return found; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | void ssl_set_session(SSL *ssl, SSL_SESSION *session) { |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 787 | if (ssl->session.get() == session) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 788 | return; |
| 789 | } |
| 790 | |
David Benjamin | 50596f8 | 2018-07-02 19:47:27 -0400 | [diff] [blame] | 791 | ssl->session = UpRef(session); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 792 | } |
| 793 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 794 | // locked by SSL_CTX in the calling function |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 795 | static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session) { |
| 796 | if (session->next == NULL || session->prev == NULL) { |
| 797 | return; |
| 798 | } |
| 799 | |
| 800 | if (session->next == (SSL_SESSION *)&ctx->session_cache_tail) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 801 | // last element in list |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 802 | if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 803 | // only one element in list |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 804 | ctx->session_cache_head = NULL; |
| 805 | ctx->session_cache_tail = NULL; |
| 806 | } else { |
| 807 | ctx->session_cache_tail = session->prev; |
| 808 | session->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail); |
| 809 | } |
| 810 | } else { |
| 811 | if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 812 | // first element in list |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 813 | ctx->session_cache_head = session->next; |
| 814 | session->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 815 | } else { // middle of list |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 816 | session->next->prev = session->prev; |
| 817 | session->prev->next = session->next; |
| 818 | } |
| 819 | } |
| 820 | session->prev = session->next = NULL; |
| 821 | } |
| 822 | |
| 823 | static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session) { |
| 824 | if (session->next != NULL && session->prev != NULL) { |
| 825 | SSL_SESSION_list_remove(ctx, session); |
| 826 | } |
| 827 | |
| 828 | if (ctx->session_cache_head == NULL) { |
| 829 | ctx->session_cache_head = session; |
| 830 | ctx->session_cache_tail = session; |
| 831 | session->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
| 832 | session->next = (SSL_SESSION *)&(ctx->session_cache_tail); |
| 833 | } else { |
| 834 | session->next = ctx->session_cache_head; |
| 835 | session->next->prev = session; |
| 836 | session->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
| 837 | ctx->session_cache_head = session; |
| 838 | } |
| 839 | } |
| 840 | |
David Benjamin | a10017c | 2021-06-16 16:00:13 -0400 | [diff] [blame] | 841 | static bool add_session_locked(SSL_CTX *ctx, UniquePtr<SSL_SESSION> session) { |
| 842 | SSL_SESSION *new_session = session.get(); |
| 843 | SSL_SESSION *old_session; |
| 844 | if (!lh_SSL_SESSION_insert(ctx->sessions, &old_session, new_session)) { |
| 845 | return false; |
| 846 | } |
| 847 | // |ctx->sessions| took ownership of |new_session| and gave us back a |
| 848 | // reference to |old_session|. (|old_session| may be the same as |
| 849 | // |new_session|, in which case we traded identical references with |
| 850 | // |ctx->sessions|.) |
| 851 | session.release(); |
| 852 | session.reset(old_session); |
| 853 | |
| 854 | if (old_session != nullptr) { |
| 855 | if (old_session == new_session) { |
| 856 | // |session| was already in the cache. There are no linked list pointers |
| 857 | // to update. |
| 858 | return false; |
| 859 | } |
| 860 | |
| 861 | // There was a session ID collision. |old_session| was replaced with |
| 862 | // |session| in the hash table, so |old_session| must be removed from the |
| 863 | // linked list to match. |
| 864 | SSL_SESSION_list_remove(ctx, old_session); |
| 865 | } |
| 866 | |
| 867 | // This does not increment the reference count. Although |session| is inserted |
| 868 | // into two structures (a doubly-linked list and the hash table), |ctx| only |
| 869 | // takes one reference. |
| 870 | SSL_SESSION_list_add(ctx, new_session); |
| 871 | |
| 872 | // Enforce any cache size limits. |
| 873 | if (SSL_CTX_sess_get_cache_size(ctx) > 0) { |
| 874 | while (lh_SSL_SESSION_num_items(ctx->sessions) > |
| 875 | SSL_CTX_sess_get_cache_size(ctx)) { |
| 876 | if (!remove_session(ctx, ctx->session_cache_tail, |
| 877 | /*lock=*/false)) { |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | return true; |
| 884 | } |
| 885 | |
| 886 | void ssl_update_cache(SSL *ssl) { |
| 887 | SSL_CTX *ctx = ssl->session_ctx.get(); |
| 888 | SSL_SESSION *session = ssl->s3->established_session.get(); |
| 889 | int mode = SSL_is_server(ssl) ? SSL_SESS_CACHE_SERVER : SSL_SESS_CACHE_CLIENT; |
| 890 | if (!SSL_SESSION_is_resumable(session) || |
| 891 | (ctx->session_cache_mode & mode) != mode) { |
| 892 | return; |
| 893 | } |
| 894 | |
| 895 | // Clients never use the internal session cache. |
| 896 | if (ssl->server && |
| 897 | !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE)) { |
| 898 | UniquePtr<SSL_SESSION> ref = UpRef(session); |
| 899 | bool remove_expired_sessions = false; |
| 900 | { |
| 901 | MutexWriteLock lock(&ctx->lock); |
| 902 | add_session_locked(ctx, std::move(ref)); |
| 903 | |
| 904 | if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) { |
| 905 | // Automatically flush the internal session cache every 255 connections. |
| 906 | ctx->handshakes_since_cache_flush++; |
| 907 | if (ctx->handshakes_since_cache_flush >= 255) { |
| 908 | remove_expired_sessions = true; |
| 909 | ctx->handshakes_since_cache_flush = 0; |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | if (remove_expired_sessions) { |
| 915 | // |SSL_CTX_flush_sessions| takes the lock we just released. We could |
| 916 | // merge the critical sections, but we'd then call user code under a |
| 917 | // lock, or compute |now| earlier, even when not flushing. |
| 918 | OPENSSL_timeval now; |
| 919 | ssl_get_current_time(ssl, &now); |
| 920 | SSL_CTX_flush_sessions(ctx, now.tv_sec); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | if (ctx->new_session_cb != nullptr) { |
| 925 | UniquePtr<SSL_SESSION> ref = UpRef(session); |
| 926 | if (ctx->new_session_cb(ssl, ref.get())) { |
| 927 | // |new_session_cb|'s return value signals whether it took ownership. |
| 928 | ref.release(); |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 933 | BSSL_NAMESPACE_END |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 934 | |
| 935 | using namespace bssl; |
| 936 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 937 | ssl_session_st::ssl_session_st(const SSL_X509_METHOD *method) |
| 938 | : x509_method(method), |
| 939 | extended_master_secret(false), |
| 940 | peer_sha256_valid(false), |
| 941 | not_resumable(false), |
| 942 | ticket_age_add_valid(false), |
Nick Harper | 6bfd25c | 2020-03-30 17:15:19 -0700 | [diff] [blame] | 943 | is_server(false), |
Steven Valdez | 51607f1 | 2020-08-05 10:46:05 -0400 | [diff] [blame] | 944 | is_quic(false), |
| 945 | has_application_settings(false) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 946 | CRYPTO_new_ex_data(&ex_data); |
| 947 | time = ::time(nullptr); |
| 948 | } |
| 949 | |
| 950 | ssl_session_st::~ssl_session_st() { |
| 951 | CRYPTO_free_ex_data(&g_ex_data_class, this, &ex_data); |
| 952 | x509_method->session_clear(this); |
| 953 | } |
| 954 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 955 | SSL_SESSION *SSL_SESSION_new(const SSL_CTX *ctx) { |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 956 | return ssl_session_new(ctx->x509_method).release(); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | int SSL_SESSION_up_ref(SSL_SESSION *session) { |
| 960 | CRYPTO_refcount_inc(&session->references); |
| 961 | return 1; |
| 962 | } |
| 963 | |
| 964 | void SSL_SESSION_free(SSL_SESSION *session) { |
| 965 | if (session == NULL || |
| 966 | !CRYPTO_refcount_dec_and_test_zero(&session->references)) { |
| 967 | return; |
| 968 | } |
| 969 | |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 970 | session->~ssl_session_st(); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 971 | OPENSSL_free(session); |
| 972 | } |
| 973 | |
| 974 | const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *session, |
| 975 | unsigned *out_len) { |
| 976 | if (out_len != NULL) { |
| 977 | *out_len = session->session_id_length; |
| 978 | } |
| 979 | return session->session_id; |
| 980 | } |
| 981 | |
David Benjamin | caf8ddd | 2018-06-03 16:25:55 -0400 | [diff] [blame] | 982 | int SSL_SESSION_set1_id(SSL_SESSION *session, const uint8_t *sid, |
| 983 | size_t sid_len) { |
| 984 | if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 985 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_TOO_LONG); |
| 986 | return 0; |
| 987 | } |
| 988 | |
| 989 | // Use memmove in case someone passes in the output of |SSL_SESSION_get_id|. |
| 990 | OPENSSL_memmove(session->session_id, sid, sid_len); |
| 991 | session->session_id_length = sid_len; |
| 992 | return 1; |
| 993 | } |
| 994 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 995 | uint32_t SSL_SESSION_get_timeout(const SSL_SESSION *session) { |
| 996 | return session->timeout; |
| 997 | } |
| 998 | |
| 999 | uint64_t SSL_SESSION_get_time(const SSL_SESSION *session) { |
| 1000 | if (session == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1001 | // NULL should crash, but silently accept it here for compatibility. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1002 | return 0; |
| 1003 | } |
| 1004 | return session->time; |
| 1005 | } |
| 1006 | |
| 1007 | X509 *SSL_SESSION_get0_peer(const SSL_SESSION *session) { |
| 1008 | return session->x509_peer; |
| 1009 | } |
| 1010 | |
David Benjamin | 855dabc | 2018-04-25 17:34:24 -0400 | [diff] [blame] | 1011 | const STACK_OF(CRYPTO_BUFFER) * |
| 1012 | SSL_SESSION_get0_peer_certificates(const SSL_SESSION *session) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 1013 | return session->certs.get(); |
David Benjamin | 855dabc | 2018-04-25 17:34:24 -0400 | [diff] [blame] | 1014 | } |
| 1015 | |
David Benjamin | 5b220ee | 2018-05-11 00:54:42 -0400 | [diff] [blame] | 1016 | void SSL_SESSION_get0_signed_cert_timestamp_list(const SSL_SESSION *session, |
| 1017 | const uint8_t **out, |
| 1018 | size_t *out_len) { |
| 1019 | if (session->signed_cert_timestamp_list) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 1020 | *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list.get()); |
| 1021 | *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list.get()); |
David Benjamin | 5b220ee | 2018-05-11 00:54:42 -0400 | [diff] [blame] | 1022 | } else { |
| 1023 | *out = nullptr; |
| 1024 | *out_len = 0; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | void SSL_SESSION_get0_ocsp_response(const SSL_SESSION *session, |
| 1029 | const uint8_t **out, size_t *out_len) { |
| 1030 | if (session->ocsp_response) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 1031 | *out = CRYPTO_BUFFER_data(session->ocsp_response.get()); |
| 1032 | *out_len = CRYPTO_BUFFER_len(session->ocsp_response.get()); |
David Benjamin | 5b220ee | 2018-05-11 00:54:42 -0400 | [diff] [blame] | 1033 | } else { |
| 1034 | *out = nullptr; |
| 1035 | *out_len = 0; |
| 1036 | } |
| 1037 | } |
| 1038 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1039 | size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, uint8_t *out, |
| 1040 | size_t max_out) { |
David Benjamin | 5351c8b | 2020-11-19 00:25:29 -0500 | [diff] [blame] | 1041 | // TODO(davidben): Fix secret_length's type and remove these casts. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1042 | if (max_out == 0) { |
David Benjamin | 5351c8b | 2020-11-19 00:25:29 -0500 | [diff] [blame] | 1043 | return (size_t)session->secret_length; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1044 | } |
David Benjamin | 5351c8b | 2020-11-19 00:25:29 -0500 | [diff] [blame] | 1045 | if (max_out > (size_t)session->secret_length) { |
| 1046 | max_out = (size_t)session->secret_length; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1047 | } |
David Benjamin | 5351c8b | 2020-11-19 00:25:29 -0500 | [diff] [blame] | 1048 | OPENSSL_memcpy(out, session->secret, max_out); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1049 | return max_out; |
| 1050 | } |
| 1051 | |
| 1052 | uint64_t SSL_SESSION_set_time(SSL_SESSION *session, uint64_t time) { |
| 1053 | if (session == NULL) { |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | session->time = time; |
| 1058 | return time; |
| 1059 | } |
| 1060 | |
| 1061 | uint32_t SSL_SESSION_set_timeout(SSL_SESSION *session, uint32_t timeout) { |
| 1062 | if (session == NULL) { |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
| 1066 | session->timeout = timeout; |
| 1067 | session->auth_timeout = timeout; |
| 1068 | return 1; |
| 1069 | } |
| 1070 | |
David Benjamin | 3b2ff02 | 2018-06-07 13:34:05 -0400 | [diff] [blame] | 1071 | const uint8_t *SSL_SESSION_get0_id_context(const SSL_SESSION *session, |
| 1072 | unsigned *out_len) { |
| 1073 | if (out_len != NULL) { |
| 1074 | *out_len = session->sid_ctx_length; |
| 1075 | } |
| 1076 | return session->sid_ctx; |
| 1077 | } |
| 1078 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1079 | int SSL_SESSION_set1_id_context(SSL_SESSION *session, const uint8_t *sid_ctx, |
| 1080 | size_t sid_ctx_len) { |
| 1081 | if (sid_ctx_len > sizeof(session->sid_ctx)) { |
| 1082 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 1086 | static_assert(sizeof(session->sid_ctx) < 256, "sid_ctx_len does not fit"); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1087 | session->sid_ctx_length = (uint8_t)sid_ctx_len; |
| 1088 | OPENSSL_memcpy(session->sid_ctx, sid_ctx, sid_ctx_len); |
| 1089 | |
| 1090 | return 1; |
| 1091 | } |
| 1092 | |
David Benjamin | e9c7b1c | 2017-09-28 14:12:52 -0400 | [diff] [blame] | 1093 | int SSL_SESSION_should_be_single_use(const SSL_SESSION *session) { |
David Benjamin | a4bafd3 | 2017-10-03 15:06:29 -0400 | [diff] [blame] | 1094 | return ssl_session_protocol_version(session) >= TLS1_3_VERSION; |
David Benjamin | e9c7b1c | 2017-09-28 14:12:52 -0400 | [diff] [blame] | 1095 | } |
| 1096 | |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 1097 | int SSL_SESSION_is_resumable(const SSL_SESSION *session) { |
David Benjamin | 71a3b82 | 2021-05-10 15:57:45 -0400 | [diff] [blame] | 1098 | return !session->not_resumable && |
| 1099 | (session->session_id_length != 0 || !session->ticket.empty()); |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | int SSL_SESSION_has_ticket(const SSL_SESSION *session) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 1103 | return !session->ticket.empty(); |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | void SSL_SESSION_get0_ticket(const SSL_SESSION *session, |
| 1107 | const uint8_t **out_ticket, size_t *out_len) { |
| 1108 | if (out_ticket != nullptr) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 1109 | *out_ticket = session->ticket.data(); |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 1110 | } |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 1111 | *out_len = session->ticket.size(); |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 1112 | } |
| 1113 | |
David Benjamin | 02de7bd | 2018-05-08 18:13:54 -0400 | [diff] [blame] | 1114 | int SSL_SESSION_set_ticket(SSL_SESSION *session, const uint8_t *ticket, |
| 1115 | size_t ticket_len) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 1116 | return session->ticket.CopyFrom(MakeConstSpan(ticket, ticket_len)); |
David Benjamin | 02de7bd | 2018-05-08 18:13:54 -0400 | [diff] [blame] | 1117 | } |
| 1118 | |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 1119 | uint32_t SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *session) { |
David Benjamin | bfdd1a9 | 2018-06-29 16:26:38 -0400 | [diff] [blame] | 1120 | return session->ticket_lifetime_hint; |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 1121 | } |
| 1122 | |
David Benjamin | b8b1a9d | 2018-04-12 18:39:28 -0400 | [diff] [blame] | 1123 | const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *session) { |
| 1124 | return session->cipher; |
| 1125 | } |
| 1126 | |
David Benjamin | 02de7bd | 2018-05-08 18:13:54 -0400 | [diff] [blame] | 1127 | int SSL_SESSION_has_peer_sha256(const SSL_SESSION *session) { |
| 1128 | return session->peer_sha256_valid; |
| 1129 | } |
| 1130 | |
| 1131 | void SSL_SESSION_get0_peer_sha256(const SSL_SESSION *session, |
| 1132 | const uint8_t **out_ptr, size_t *out_len) { |
| 1133 | if (session->peer_sha256_valid) { |
| 1134 | *out_ptr = session->peer_sha256; |
| 1135 | *out_len = sizeof(session->peer_sha256); |
| 1136 | } else { |
| 1137 | *out_ptr = nullptr; |
| 1138 | *out_len = 0; |
| 1139 | } |
| 1140 | } |
| 1141 | |
Steven Valdez | 5274cea | 2019-05-08 11:34:57 -0400 | [diff] [blame] | 1142 | int SSL_SESSION_early_data_capable(const SSL_SESSION *session) { |
| 1143 | return ssl_session_protocol_version(session) >= TLS1_3_VERSION && |
| 1144 | session->ticket_max_early_data != 0; |
| 1145 | } |
| 1146 | |
David Benjamin | f9e0cda | 2020-03-23 18:29:09 -0400 | [diff] [blame] | 1147 | SSL_SESSION *SSL_SESSION_copy_without_early_data(SSL_SESSION *session) { |
| 1148 | if (!SSL_SESSION_early_data_capable(session)) { |
| 1149 | return UpRef(session).release(); |
| 1150 | } |
| 1151 | |
| 1152 | bssl::UniquePtr<SSL_SESSION> copy = |
| 1153 | SSL_SESSION_dup(session, SSL_SESSION_DUP_ALL); |
| 1154 | if (!copy) { |
| 1155 | return nullptr; |
| 1156 | } |
| 1157 | |
| 1158 | copy->ticket_max_early_data = 0; |
| 1159 | // Copied sessions are non-resumable until they're completely filled in. |
| 1160 | copy->not_resumable = session->not_resumable; |
| 1161 | assert(!SSL_SESSION_early_data_capable(copy.get())); |
| 1162 | return copy.release(); |
| 1163 | } |
| 1164 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1165 | SSL_SESSION *SSL_magic_pending_session_ptr(void) { |
| 1166 | return (SSL_SESSION *)&g_pending_session_magic; |
| 1167 | } |
| 1168 | |
| 1169 | SSL_SESSION *SSL_get_session(const SSL *ssl) { |
David Benjamin | 10fef97 | 2022-08-30 11:38:51 -0400 | [diff] [blame] | 1170 | // Once the initially handshake completes, we return the most recently |
| 1171 | // established session. In particular, if there is a pending renegotiation, we |
| 1172 | // do not return information about it until it completes. |
| 1173 | // |
| 1174 | // Code in the handshake must either use |hs->new_session| (if updating a |
| 1175 | // partial session) or |ssl_handshake_session| (if trying to query properties |
| 1176 | // consistently across TLS 1.2 resumption and other handshakes). |
| 1177 | if (ssl->s3->established_session != nullptr) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1178 | return ssl->s3->established_session.get(); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1179 | } |
David Benjamin | 10fef97 | 2022-08-30 11:38:51 -0400 | [diff] [blame] | 1180 | |
| 1181 | // Otherwise, we must be in the initial handshake. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1182 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
David Benjamin | 10fef97 | 2022-08-30 11:38:51 -0400 | [diff] [blame] | 1183 | assert(hs != nullptr); |
| 1184 | assert(!ssl->s3->initial_handshake_complete); |
| 1185 | |
| 1186 | // Return the 0-RTT session, if in the 0-RTT state. While the handshake has |
| 1187 | // not actually completed, the public accessors all report properties as if |
| 1188 | // it has. |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1189 | if (hs->early_session) { |
| 1190 | return hs->early_session.get(); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1191 | } |
David Benjamin | 10fef97 | 2022-08-30 11:38:51 -0400 | [diff] [blame] | 1192 | |
| 1193 | // Otherwise, return the partial session. |
| 1194 | return (SSL_SESSION *)ssl_handshake_session(hs); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | SSL_SESSION *SSL_get1_session(SSL *ssl) { |
| 1198 | SSL_SESSION *ret = SSL_get_session(ssl); |
| 1199 | if (ret != NULL) { |
| 1200 | SSL_SESSION_up_ref(ret); |
| 1201 | } |
| 1202 | return ret; |
| 1203 | } |
| 1204 | |
| 1205 | int SSL_SESSION_get_ex_new_index(long argl, void *argp, |
| 1206 | CRYPTO_EX_unused *unused, |
| 1207 | CRYPTO_EX_dup *dup_unused, |
| 1208 | CRYPTO_EX_free *free_func) { |
| 1209 | int index; |
| 1210 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, |
| 1211 | free_func)) { |
| 1212 | return -1; |
| 1213 | } |
| 1214 | return index; |
| 1215 | } |
| 1216 | |
| 1217 | int SSL_SESSION_set_ex_data(SSL_SESSION *session, int idx, void *arg) { |
| 1218 | return CRYPTO_set_ex_data(&session->ex_data, idx, arg); |
| 1219 | } |
| 1220 | |
| 1221 | void *SSL_SESSION_get_ex_data(const SSL_SESSION *session, int idx) { |
| 1222 | return CRYPTO_get_ex_data(&session->ex_data, idx); |
| 1223 | } |
| 1224 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1225 | int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *session) { |
David Benjamin | 2908dd1 | 2018-06-29 17:46:42 -0400 | [diff] [blame] | 1226 | UniquePtr<SSL_SESSION> owned_session = UpRef(session); |
David Benjamin | 21fa684 | 2017-09-27 17:28:10 -0400 | [diff] [blame] | 1227 | MutexWriteLock lock(&ctx->lock); |
David Benjamin | a10017c | 2021-06-16 16:00:13 -0400 | [diff] [blame] | 1228 | return add_session_locked(ctx, std::move(owned_session)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1229 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1230 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1231 | int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *session) { |
David Benjamin | a10017c | 2021-06-16 16:00:13 -0400 | [diff] [blame] | 1232 | return remove_session(ctx, session, /*lock=*/true); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1233 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1234 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1235 | int SSL_set_session(SSL *ssl, SSL_SESSION *session) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1236 | // SSL_set_session may only be called before the handshake has started. |
David Benjamin | 9e766d7 | 2017-02-10 23:14:17 -0500 | [diff] [blame] | 1237 | if (ssl->s3->initial_handshake_complete || |
| 1238 | ssl->s3->hs == NULL || |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1239 | ssl->s3->hs->state != 0) { |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 1240 | abort(); |
| 1241 | } |
| 1242 | |
| 1243 | ssl_set_session(ssl, session); |
| 1244 | return 1; |
| 1245 | } |
| 1246 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 1247 | uint32_t SSL_CTX_set_timeout(SSL_CTX *ctx, uint32_t timeout) { |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1248 | if (ctx == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1249 | return 0; |
| 1250 | } |
| 1251 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1252 | // Historically, zero was treated as |SSL_DEFAULT_SESSION_TIMEOUT|. |
David Benjamin | 0b1bb12 | 2017-01-28 14:32:53 -0500 | [diff] [blame] | 1253 | if (timeout == 0) { |
| 1254 | timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
| 1255 | } |
| 1256 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 1257 | uint32_t old_timeout = ctx->session_timeout; |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1258 | ctx->session_timeout = timeout; |
| 1259 | return old_timeout; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1260 | } |
| 1261 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 1262 | uint32_t SSL_CTX_get_timeout(const SSL_CTX *ctx) { |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1263 | if (ctx == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
| 1266 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1267 | return ctx->session_timeout; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 1270 | void SSL_CTX_set_session_psk_dhe_timeout(SSL_CTX *ctx, uint32_t timeout) { |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 1271 | ctx->session_psk_dhe_timeout = timeout; |
| 1272 | } |
| 1273 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1274 | typedef struct timeout_param_st { |
| 1275 | SSL_CTX *ctx; |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 1276 | uint64_t time; |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 1277 | LHASH_OF(SSL_SESSION) *cache; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1278 | } TIMEOUT_PARAM; |
| 1279 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1280 | static void timeout_doall_arg(SSL_SESSION *session, void *void_param) { |
David Benjamin | 0238d8f | 2017-07-12 17:35:14 -0400 | [diff] [blame] | 1281 | TIMEOUT_PARAM *param = reinterpret_cast<TIMEOUT_PARAM *>(void_param); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1282 | |
| 1283 | if (param->time == 0 || |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 1284 | session->time + session->timeout < session->time || |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1285 | param->time > (session->time + session->timeout)) { |
David Benjamin | a10017c | 2021-06-16 16:00:13 -0400 | [diff] [blame] | 1286 | // TODO(davidben): This can probably just call |remove_session|. |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1287 | (void) lh_SSL_SESSION_delete(param->cache, session); |
| 1288 | SSL_SESSION_list_remove(param->ctx, session); |
David Benjamin | a10017c | 2021-06-16 16:00:13 -0400 | [diff] [blame] | 1289 | // TODO(https://crbug.com/boringssl/251): Callbacks should not be called |
| 1290 | // under a lock. |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1291 | if (param->ctx->remove_session_cb != NULL) { |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1292 | param->ctx->remove_session_cb(param->ctx, session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1293 | } |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1294 | SSL_SESSION_free(session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 1298 | void SSL_CTX_flush_sessions(SSL_CTX *ctx, uint64_t time) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1299 | TIMEOUT_PARAM tp; |
| 1300 | |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 1301 | tp.ctx = ctx; |
| 1302 | tp.cache = ctx->sessions; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1303 | if (tp.cache == NULL) { |
| 1304 | return; |
| 1305 | } |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1306 | tp.time = time; |
David Benjamin | 21fa684 | 2017-09-27 17:28:10 -0400 | [diff] [blame] | 1307 | MutexWriteLock lock(&ctx->lock); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1308 | lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1311 | void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1312 | int (*cb)(SSL *ssl, SSL_SESSION *session)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1313 | ctx->new_session_cb = cb; |
| 1314 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1315 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1316 | int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *session) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1317 | return ctx->new_session_cb; |
| 1318 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1319 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1320 | void SSL_CTX_sess_set_remove_cb( |
| 1321 | SSL_CTX *ctx, void (*cb)(SSL_CTX *ctx, SSL_SESSION *session)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1322 | ctx->remove_session_cb = cb; |
| 1323 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1324 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1325 | void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx, |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 1326 | SSL_SESSION *session) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1327 | return ctx->remove_session_cb; |
| 1328 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1329 | |
| 1330 | void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 1331 | SSL_SESSION *(*cb)(SSL *ssl, const uint8_t *id, |
| 1332 | int id_len, int *out_copy)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1333 | ctx->get_session_cb = cb; |
| 1334 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1335 | |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 1336 | SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, |
| 1337 | const uint8_t *id, |
| 1338 | int id_len, |
| 1339 | int *out_copy) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1340 | return ctx->get_session_cb; |
| 1341 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1342 | |
David Benjamin | 8217024 | 2015-10-17 22:51:17 -0400 | [diff] [blame] | 1343 | void SSL_CTX_set_info_callback( |
| 1344 | SSL_CTX *ctx, void (*cb)(const SSL *ssl, int type, int value)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1345 | ctx->info_callback = cb; |
| 1346 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1347 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1348 | void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type, |
David Benjamin | 8217024 | 2015-10-17 22:51:17 -0400 | [diff] [blame] | 1349 | int value) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1350 | return ctx->info_callback; |
| 1351 | } |