Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [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 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 136 | #include <openssl/ssl.h> |
| 137 | |
| 138 | #include <assert.h> |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 139 | #include <stdlib.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 140 | #include <string.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 141 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 142 | #include <utility> |
| 143 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 144 | #include <openssl/err.h> |
Robert Sloan | db4251a | 2017-09-18 09:38:15 -0700 | [diff] [blame] | 145 | #include <openssl/hmac.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 146 | #include <openssl/lhash.h> |
| 147 | #include <openssl/mem.h> |
| 148 | #include <openssl/rand.h> |
| 149 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 150 | #include "internal.h" |
| 151 | #include "../crypto/internal.h" |
| 152 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 153 | |
Robert Sloan | 726e9d1 | 2018-09-11 11:45:04 -0700 | [diff] [blame] | 154 | BSSL_NAMESPACE_BEGIN |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 155 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [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. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 159 | static const char g_pending_session_magic = 0; |
| 160 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 161 | static CRYPTO_EX_DATA_CLASS g_ex_data_class = |
| 162 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 163 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [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); |
| 166 | static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock); |
| 167 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 168 | UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 169 | return MakeUnique<SSL_SESSION>(x509_method); |
| 170 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 171 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 172 | uint32_t ssl_hash_session_id(Span<const uint8_t> session_id) { |
| 173 | // Take the first four bytes of |session_id|. Session IDs are generated by the |
| 174 | // server randomly, so we can assume even using the first four bytes results |
| 175 | // in a good distribution. |
| 176 | uint8_t tmp_storage[sizeof(uint32_t)]; |
| 177 | if (session_id.size() < sizeof(tmp_storage)) { |
| 178 | OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage)); |
| 179 | OPENSSL_memcpy(tmp_storage, session_id.data(), session_id.size()); |
| 180 | session_id = tmp_storage; |
| 181 | } |
| 182 | |
| 183 | uint32_t hash = |
| 184 | ((uint32_t)session_id[0]) | |
| 185 | ((uint32_t)session_id[1] << 8) | |
| 186 | ((uint32_t)session_id[2] << 16) | |
| 187 | ((uint32_t)session_id[3] << 24); |
| 188 | |
| 189 | return hash; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 192 | UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session, int dup_flags) { |
| 193 | UniquePtr<SSL_SESSION> new_session = ssl_session_new(session->x509_method); |
| 194 | if (!new_session) { |
| 195 | return nullptr; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 196 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 197 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 198 | new_session->is_server = session->is_server; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 199 | new_session->ssl_version = session->ssl_version; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 200 | new_session->sid_ctx_length = session->sid_ctx_length; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 201 | OPENSSL_memcpy(new_session->sid_ctx, session->sid_ctx, session->sid_ctx_length); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 202 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 203 | // Copy the key material. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 204 | new_session->master_key_length = session->master_key_length; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 205 | OPENSSL_memcpy(new_session->master_key, session->master_key, |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 206 | session->master_key_length); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 207 | new_session->cipher = session->cipher; |
| 208 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 209 | // Copy authentication state. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 210 | if (session->psk_identity != nullptr) { |
| 211 | new_session->psk_identity.reset(BUF_strdup(session->psk_identity.get())); |
| 212 | if (new_session->psk_identity == nullptr) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 213 | return nullptr; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 214 | } |
| 215 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 216 | if (session->certs != nullptr) { |
| 217 | auto buf_up_ref = [](CRYPTO_BUFFER *buf) { |
| 218 | CRYPTO_BUFFER_up_ref(buf); |
| 219 | return buf; |
| 220 | }; |
| 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) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 224 | return nullptr; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 225 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 226 | } |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 227 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 228 | if (!session->x509_method->session_dup(new_session.get(), session)) { |
| 229 | return nullptr; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 230 | } |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 231 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 232 | new_session->verify_result = session->verify_result; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 233 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [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); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 237 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 238 | OPENSSL_memcpy(new_session->peer_sha256, session->peer_sha256, |
| 239 | SHA256_DIGEST_LENGTH); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 240 | new_session->peer_sha256_valid = session->peer_sha256_valid; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 241 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 242 | new_session->peer_signature_algorithm = session->peer_signature_algorithm; |
| 243 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 244 | new_session->timeout = session->timeout; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 245 | new_session->auth_timeout = session->auth_timeout; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 246 | new_session->time = session->time; |
| 247 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 248 | // Copy non-authentication connection properties. |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 249 | if (dup_flags & SSL_SESSION_INCLUDE_NONAUTH) { |
| 250 | new_session->session_id_length = session->session_id_length; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 251 | OPENSSL_memcpy(new_session->session_id, session->session_id, |
| 252 | session->session_id_length); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 253 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 254 | new_session->group_id = session->group_id; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 255 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 256 | OPENSSL_memcpy(new_session->original_handshake_hash, |
| 257 | session->original_handshake_hash, |
| 258 | session->original_handshake_hash_len); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 259 | new_session->original_handshake_hash_len = |
| 260 | session->original_handshake_hash_len; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 261 | new_session->ticket_lifetime_hint = session->ticket_lifetime_hint; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 262 | new_session->ticket_age_add = session->ticket_age_add; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 263 | new_session->ticket_max_early_data = session->ticket_max_early_data; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 264 | new_session->extended_master_secret = session->extended_master_secret; |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 265 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 266 | if (!new_session->early_alpn.CopyFrom(session->early_alpn)) { |
| 267 | return nullptr; |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 268 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 269 | } |
| 270 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 271 | // Copy the ticket. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 272 | if (dup_flags & SSL_SESSION_INCLUDE_TICKET && |
| 273 | !new_session->ticket.CopyFrom(session->ticket)) { |
| 274 | return nullptr; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 277 | // The new_session does not get a copy of the ex_data. |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 278 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 279 | new_session->not_resumable = true; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 280 | return new_session; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 281 | } |
| 282 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 283 | void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session) { |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 284 | struct OPENSSL_timeval now; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 285 | ssl_get_current_time(ssl, &now); |
| 286 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 287 | // To avoid overflows and underflows, if we've gone back in time, update the |
| 288 | // time, but mark the session expired. |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 289 | if (session->time > now.tv_sec) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 290 | session->time = now.tv_sec; |
| 291 | session->timeout = 0; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 292 | session->auth_timeout = 0; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 296 | // Adjust the session time and timeouts. If the session has already expired, |
| 297 | // clamp the timeouts at zero. |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 298 | uint64_t delta = now.tv_sec - session->time; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 299 | session->time = now.tv_sec; |
| 300 | if (session->timeout < delta) { |
| 301 | session->timeout = 0; |
| 302 | } else { |
| 303 | session->timeout -= delta; |
| 304 | } |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 305 | if (session->auth_timeout < delta) { |
| 306 | session->auth_timeout = 0; |
| 307 | } else { |
| 308 | session->auth_timeout -= delta; |
| 309 | } |
| 310 | } |
| 311 | |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 312 | void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session, |
| 313 | uint32_t timeout) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 314 | // Rebase the timestamp relative to the current time so |timeout| is measured |
| 315 | // correctly. |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 316 | ssl_session_rebase_time(ssl, session); |
| 317 | |
| 318 | if (session->timeout > timeout) { |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | session->timeout = timeout; |
| 323 | if (session->timeout > session->auth_timeout) { |
| 324 | session->timeout = session->auth_timeout; |
| 325 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 326 | } |
| 327 | |
Robert Sloan | ae1abf9 | 2017-10-05 12:50:08 -0700 | [diff] [blame] | 328 | uint16_t ssl_session_protocol_version(const SSL_SESSION *session) { |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 329 | uint16_t ret; |
| 330 | if (!ssl_protocol_version_from_wire(&ret, session->ssl_version)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 331 | // An |SSL_SESSION| will never have an invalid version. This is enforced by |
| 332 | // the parser. |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 333 | assert(0); |
| 334 | return 0; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Robert Sloan | f6200e7 | 2017-07-10 08:09:18 -0700 | [diff] [blame] | 337 | return ret; |
| 338 | } |
| 339 | |
Robert Sloan | ae1abf9 | 2017-10-05 12:50:08 -0700 | [diff] [blame] | 340 | const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session) { |
| 341 | return ssl_get_handshake_digest(ssl_session_protocol_version(session), |
Robert Sloan | 8437709 | 2017-08-14 09:33:19 -0700 | [diff] [blame] | 342 | session->cipher); |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 343 | } |
| 344 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 345 | int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server) { |
| 346 | SSL *const ssl = hs->ssl; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 347 | if (ssl->mode & SSL_MODE_NO_SESSION_CREATION) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 348 | OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_MAY_NOT_BE_CREATED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 352 | UniquePtr<SSL_SESSION> session = ssl_session_new(ssl->ctx->x509_method); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 353 | if (session == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 357 | session->is_server = is_server; |
| 358 | session->ssl_version = ssl->version; |
| 359 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 360 | // Fill in the time from the |SSL_CTX|'s clock. |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 361 | struct OPENSSL_timeval now; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 362 | ssl_get_current_time(ssl, &now); |
| 363 | session->time = now.tv_sec; |
| 364 | |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame] | 365 | uint16_t version = ssl_protocol_version(ssl); |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 366 | if (version >= TLS1_3_VERSION) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 367 | // TLS 1.3 uses tickets as authenticators, so we are willing to use them for |
| 368 | // longer. |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 369 | session->timeout = ssl->session_ctx->session_psk_dhe_timeout; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 370 | session->auth_timeout = SSL_DEFAULT_SESSION_AUTH_TIMEOUT; |
| 371 | } else { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 372 | // TLS 1.2 resumption does not incorporate new key material, so we use a |
| 373 | // much shorter timeout. |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 374 | session->timeout = ssl->session_ctx->session_timeout; |
| 375 | session->auth_timeout = ssl->session_ctx->session_timeout; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 376 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 377 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 378 | if (is_server) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 379 | if (hs->ticket_expected || version >= TLS1_3_VERSION) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 380 | // Don't set session IDs for sessions resumed with tickets. This will keep |
| 381 | // them out of the session cache. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 382 | session->session_id_length = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 383 | } else { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 384 | session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; |
| 385 | if (!RAND_bytes(session->session_id, session->session_id_length)) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 386 | return 0; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 387 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 388 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 389 | } else { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 390 | session->session_id_length = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 393 | if (hs->config->cert->sid_ctx_length > sizeof(session->sid_ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 394 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 395 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 396 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 397 | OPENSSL_memcpy(session->sid_ctx, hs->config->cert->sid_ctx, |
| 398 | hs->config->cert->sid_ctx_length); |
| 399 | session->sid_ctx_length = hs->config->cert->sid_ctx_length; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 400 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 401 | // The session is marked not resumable until it is completely filled in. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 402 | session->not_resumable = true; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 403 | session->verify_result = X509_V_ERR_INVALID_CALL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 404 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 405 | hs->new_session = std::move(session); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 406 | ssl_set_session(ssl, NULL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 407 | return 1; |
| 408 | } |
| 409 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 410 | int ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx) { |
| 411 | OPENSSL_timeval now; |
| 412 | ssl_ctx_get_current_time(ctx, &now); |
| 413 | { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 414 | // Avoid acquiring a write lock in the common case (i.e. a non-default key |
| 415 | // is used or the default keys have not expired yet). |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 416 | MutexReadLock lock(&ctx->lock); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 417 | if (ctx->ticket_key_current && |
| 418 | (ctx->ticket_key_current->next_rotation_tv_sec == 0 || |
| 419 | ctx->ticket_key_current->next_rotation_tv_sec > now.tv_sec) && |
| 420 | (!ctx->ticket_key_prev || |
| 421 | ctx->ticket_key_prev->next_rotation_tv_sec > now.tv_sec)) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 422 | return 1; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | MutexWriteLock lock(&ctx->lock); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 427 | if (!ctx->ticket_key_current || |
| 428 | (ctx->ticket_key_current->next_rotation_tv_sec != 0 && |
| 429 | ctx->ticket_key_current->next_rotation_tv_sec <= now.tv_sec)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 430 | // The current key has not been initialized or it is expired. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 431 | auto new_key = bssl::MakeUnique<TicketKey>(); |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 432 | if (!new_key) { |
| 433 | return 0; |
| 434 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 435 | RAND_bytes(new_key->name, 16); |
| 436 | RAND_bytes(new_key->hmac_key, 16); |
| 437 | RAND_bytes(new_key->aes_key, 16); |
| 438 | new_key->next_rotation_tv_sec = |
| 439 | now.tv_sec + SSL_DEFAULT_TICKET_KEY_ROTATION_INTERVAL; |
| 440 | if (ctx->ticket_key_current) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 441 | // The current key expired. Rotate it to prev and bump up its rotation |
| 442 | // timestamp. Note that even with the new rotation time it may still be |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 443 | // expired and get dropped below. |
| 444 | ctx->ticket_key_current->next_rotation_tv_sec += |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 445 | SSL_DEFAULT_TICKET_KEY_ROTATION_INTERVAL; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 446 | ctx->ticket_key_prev = std::move(ctx->ticket_key_current); |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 447 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 448 | ctx->ticket_key_current = std::move(new_key); |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 451 | // Drop an expired prev key. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 452 | if (ctx->ticket_key_prev && |
| 453 | ctx->ticket_key_prev->next_rotation_tv_sec <= now.tv_sec) { |
| 454 | ctx->ticket_key_prev.reset(); |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | return 1; |
| 458 | } |
| 459 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 460 | static int ssl_encrypt_ticket_with_cipher_ctx(SSL_HANDSHAKE *hs, CBB *out, |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 461 | const uint8_t *session_buf, |
| 462 | size_t session_len) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 463 | ScopedEVP_CIPHER_CTX ctx; |
| 464 | ScopedHMAC_CTX hctx; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 465 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 466 | // If the session is too long, emit a dummy value rather than abort the |
| 467 | // connection. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 468 | static const size_t kMaxTicketOverhead = |
| 469 | 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE; |
| 470 | if (session_len > 0xffff - kMaxTicketOverhead) { |
| 471 | static const char kTicketPlaceholder[] = "TICKET TOO LARGE"; |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 472 | return CBB_add_bytes(out, (const uint8_t *)kTicketPlaceholder, |
| 473 | strlen(kTicketPlaceholder)); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 474 | } |
| 475 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 476 | // Initialize HMAC and cipher contexts. If callback present it does all the |
| 477 | // work otherwise use generated values from parent ctx. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 478 | SSL_CTX *tctx = hs->ssl->session_ctx.get(); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 479 | uint8_t iv[EVP_MAX_IV_LENGTH]; |
| 480 | uint8_t key_name[16]; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 481 | if (tctx->ticket_key_cb != NULL) { |
| 482 | if (tctx->ticket_key_cb(hs->ssl, key_name, iv, ctx.get(), hctx.get(), |
| 483 | 1 /* encrypt */) < 0) { |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 484 | return 0; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 485 | } |
| 486 | } else { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 487 | // Rotate ticket key if necessary. |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 488 | if (!ssl_ctx_rotate_ticket_encryption_key(tctx)) { |
| 489 | return 0; |
| 490 | } |
| 491 | MutexReadLock lock(&tctx->lock); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 492 | if (!RAND_bytes(iv, 16) || |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 493 | !EVP_EncryptInit_ex(ctx.get(), EVP_aes_128_cbc(), NULL, |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 494 | tctx->ticket_key_current->aes_key, iv) || |
| 495 | !HMAC_Init_ex(hctx.get(), tctx->ticket_key_current->hmac_key, 16, |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 496 | tlsext_tick_md(), NULL)) { |
| 497 | return 0; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 498 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 499 | OPENSSL_memcpy(key_name, tctx->ticket_key_current->name, 16); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | uint8_t *ptr; |
| 503 | if (!CBB_add_bytes(out, key_name, 16) || |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 504 | !CBB_add_bytes(out, iv, EVP_CIPHER_CTX_iv_length(ctx.get())) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 505 | !CBB_reserve(out, &ptr, session_len + EVP_MAX_BLOCK_LENGTH)) { |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 506 | return 0; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 507 | } |
| 508 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 509 | size_t total = 0; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 510 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 511 | OPENSSL_memcpy(ptr, session_buf, session_len); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 512 | total = session_len; |
| 513 | #else |
| 514 | int len; |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 515 | if (!EVP_EncryptUpdate(ctx.get(), ptr + total, &len, session_buf, session_len)) { |
| 516 | return 0; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 517 | } |
| 518 | total += len; |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 519 | if (!EVP_EncryptFinal_ex(ctx.get(), ptr + total, &len)) { |
| 520 | return 0; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 521 | } |
| 522 | total += len; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 523 | #endif |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 524 | if (!CBB_did_write(out, total)) { |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 525 | return 0; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | unsigned hlen; |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 529 | if (!HMAC_Update(hctx.get(), CBB_data(out), CBB_len(out)) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 530 | !CBB_reserve(out, &ptr, EVP_MAX_MD_SIZE) || |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 531 | !HMAC_Final(hctx.get(), ptr, &hlen) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 532 | !CBB_did_write(out, hlen)) { |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 533 | return 0; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 534 | } |
| 535 | |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 536 | return 1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 537 | } |
| 538 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 539 | static int ssl_encrypt_ticket_with_method(SSL_HANDSHAKE *hs, CBB *out, |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 540 | const uint8_t *session_buf, |
| 541 | size_t session_len) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 542 | SSL *const ssl = hs->ssl; |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 543 | const SSL_TICKET_AEAD_METHOD *method = ssl->session_ctx->ticket_aead_method; |
| 544 | const size_t max_overhead = method->max_overhead(ssl); |
| 545 | const size_t max_out = session_len + max_overhead; |
| 546 | if (max_out < max_overhead) { |
| 547 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | uint8_t *ptr; |
| 552 | if (!CBB_reserve(out, &ptr, max_out)) { |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | size_t out_len; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 557 | if (!method->seal(ssl, ptr, &out_len, max_out, session_buf, |
| 558 | session_len)) { |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 559 | OPENSSL_PUT_ERROR(SSL, SSL_R_TICKET_ENCRYPTION_FAILED); |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | if (!CBB_did_write(out, out_len)) { |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | return 1; |
| 568 | } |
| 569 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 570 | int ssl_encrypt_ticket(SSL_HANDSHAKE *hs, CBB *out, |
| 571 | const SSL_SESSION *session) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 572 | // Serialize the SSL_SESSION to be encoded into the ticket. |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 573 | uint8_t *session_buf = NULL; |
| 574 | size_t session_len; |
| 575 | if (!SSL_SESSION_to_bytes_for_ticket(session, &session_buf, &session_len)) { |
| 576 | return -1; |
| 577 | } |
| 578 | |
| 579 | int ret = 0; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 580 | if (hs->ssl->session_ctx->ticket_aead_method) { |
| 581 | ret = ssl_encrypt_ticket_with_method(hs, out, session_buf, session_len); |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 582 | } else { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 583 | ret = ssl_encrypt_ticket_with_cipher_ctx(hs, out, session_buf, session_len); |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | OPENSSL_free(session_buf); |
| 587 | return ret; |
| 588 | } |
| 589 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 590 | int ssl_session_is_context_valid(const SSL_HANDSHAKE *hs, |
| 591 | const SSL_SESSION *session) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 592 | if (session == NULL) { |
| 593 | return 0; |
| 594 | } |
| 595 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 596 | return session->sid_ctx_length == hs->config->cert->sid_ctx_length && |
| 597 | OPENSSL_memcmp(session->sid_ctx, hs->config->cert->sid_ctx, |
| 598 | hs->config->cert->sid_ctx_length) == 0; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session) { |
| 602 | if (session == NULL) { |
| 603 | return 0; |
| 604 | } |
| 605 | |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 606 | struct OPENSSL_timeval now; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 607 | ssl_get_current_time(ssl, &now); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 608 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 609 | // Reject tickets from the future to avoid underflow. |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 610 | if (now.tv_sec < session->time) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 614 | return session->timeout > now.tv_sec - session->time; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 615 | } |
| 616 | |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 617 | int ssl_session_is_resumable(const SSL_HANDSHAKE *hs, |
| 618 | const SSL_SESSION *session) { |
| 619 | const SSL *const ssl = hs->ssl; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 620 | return ssl_session_is_context_valid(hs, session) && |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 621 | // The session must have been created by the same type of end point as |
| 622 | // we're now using it with. |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 623 | ssl->server == session->is_server && |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 624 | // The session must not be expired. |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 625 | ssl_session_is_time_valid(ssl, session) && |
| 626 | /* Only resume if the session's version matches the negotiated |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 627 | * version. */ |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 628 | ssl->version == session->ssl_version && |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 629 | // Only resume if the session's cipher matches the negotiated one. |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 630 | hs->new_cipher == session->cipher && |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 631 | // If the session contains a client certificate (either the full |
| 632 | // certificate or just the hash) then require that the form of the |
| 633 | // certificate matches the current configuration. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 634 | ((sk_CRYPTO_BUFFER_num(session->certs.get()) == 0 && |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame] | 635 | !session->peer_sha256_valid) || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 636 | session->peer_sha256_valid == |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 637 | hs->config->retain_only_sha256_of_client_certs); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 638 | } |
| 639 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 640 | // ssl_lookup_session looks up |session_id| in the session cache and sets |
| 641 | // |*out_session| to an |SSL_SESSION| object if found. |
| 642 | static enum ssl_hs_wait_t ssl_lookup_session( |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 643 | SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session, |
| 644 | Span<const uint8_t> session_id) { |
| 645 | SSL *const ssl = hs->ssl; |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 646 | out_session->reset(); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 647 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 648 | if (session_id.empty() || session_id.size() > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 649 | return ssl_hs_ok; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 652 | UniquePtr<SSL_SESSION> session; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 653 | // Try the internal cache, if it exists. |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 654 | if (!(ssl->session_ctx->session_cache_mode & |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 655 | SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 656 | uint32_t hash = ssl_hash_session_id(session_id); |
| 657 | auto cmp = [](const void *key, const SSL_SESSION *sess) -> int { |
| 658 | Span<const uint8_t> key_id = |
| 659 | *reinterpret_cast<const Span<const uint8_t> *>(key); |
| 660 | Span<const uint8_t> sess_id = |
| 661 | MakeConstSpan(sess->session_id, sess->session_id_length); |
| 662 | return key_id == sess_id ? 0 : 1; |
| 663 | }; |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 664 | MutexReadLock lock(&ssl->session_ctx->lock); |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 665 | // |lh_SSL_SESSION_retrieve_key| returns a non-owning pointer. |
| 666 | session = UpRef(lh_SSL_SESSION_retrieve_key(ssl->session_ctx->sessions, |
| 667 | &session_id, hash, cmp)); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 668 | // TODO(davidben): This should probably move it to the front of the list. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 671 | // Fall back to the external cache, if it exists. |
Robert Sloan | 5cbb5c8 | 2018-04-24 11:35:46 -0700 | [diff] [blame] | 672 | if (!session && ssl->session_ctx->get_session_cb != nullptr) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 673 | int copy = 1; |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 674 | session.reset(ssl->session_ctx->get_session_cb(ssl, session_id.data(), |
| 675 | session_id.size(), ©)); |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 676 | if (!session) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 677 | return ssl_hs_ok; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 678 | } |
| 679 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 680 | if (session.get() == SSL_magic_pending_session_ptr()) { |
| 681 | session.release(); // This pointer is not actually owned. |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 682 | return ssl_hs_pending_session; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 683 | } |
| 684 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 685 | // Increment reference count now if the session callback asks us to do so |
| 686 | // (note that if the session structures returned by the callback are shared |
| 687 | // between threads, it must handle the reference count itself [i.e. copy == |
| 688 | // 0], or things won't be thread-safe). |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 689 | if (copy) { |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 690 | SSL_SESSION_up_ref(session.get()); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 691 | } |
| 692 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 693 | // Add the externally cached session to the internal cache if necessary. |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 694 | if (!(ssl->session_ctx->session_cache_mode & |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 695 | SSL_SESS_CACHE_NO_INTERNAL_STORE)) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 696 | SSL_CTX_add_session(ssl->session_ctx.get(), session.get()); |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 697 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 698 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 699 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 700 | if (session && !ssl_session_is_time_valid(ssl, session.get())) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 701 | // The session was from the cache, so remove it. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 702 | SSL_CTX_remove_session(ssl->session_ctx.get(), session.get()); |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 703 | session.reset(); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 704 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 705 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 706 | *out_session = std::move(session); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 707 | return ssl_hs_ok; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 710 | enum ssl_hs_wait_t ssl_get_prev_session(SSL_HANDSHAKE *hs, |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 711 | UniquePtr<SSL_SESSION> *out_session, |
| 712 | bool *out_tickets_supported, |
| 713 | bool *out_renew_ticket, |
| 714 | const SSL_CLIENT_HELLO *client_hello) { |
| 715 | // This is used only by servers. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 716 | assert(hs->ssl->server); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 717 | UniquePtr<SSL_SESSION> session; |
| 718 | bool renew_ticket = false; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 719 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 720 | // If tickets are disabled, always behave as if no tickets are present. |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 721 | CBS ticket; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 722 | const bool tickets_supported = |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 723 | !(SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) && |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 724 | ssl_client_hello_get_extension(client_hello, &ticket, |
| 725 | TLSEXT_TYPE_session_ticket); |
| 726 | if (tickets_supported && CBS_len(&ticket) != 0) { |
| 727 | switch (ssl_process_ticket(hs, &session, &renew_ticket, ticket, |
| 728 | MakeConstSpan(client_hello->session_id, |
| 729 | client_hello->session_id_len))) { |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 730 | case ssl_ticket_aead_success: |
| 731 | break; |
| 732 | case ssl_ticket_aead_ignore_ticket: |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 733 | assert(!session); |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 734 | break; |
| 735 | case ssl_ticket_aead_error: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 736 | return ssl_hs_error; |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 737 | case ssl_ticket_aead_retry: |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 738 | return ssl_hs_pending_ticket; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 739 | } |
| 740 | } else { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 741 | // The client didn't send a ticket, so the session ID is a real ID. |
| 742 | enum ssl_hs_wait_t lookup_ret = ssl_lookup_session( |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 743 | hs, &session, |
| 744 | MakeConstSpan(client_hello->session_id, client_hello->session_id_len)); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 745 | if (lookup_ret != ssl_hs_ok) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 746 | return lookup_ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 747 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 750 | *out_session = std::move(session); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 751 | *out_tickets_supported = tickets_supported; |
| 752 | *out_renew_ticket = renew_ticket; |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 753 | return ssl_hs_ok; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 754 | } |
| 755 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 756 | static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock) { |
| 757 | int ret = 0; |
| 758 | |
| 759 | if (session != NULL && session->session_id_length != 0) { |
| 760 | if (lock) { |
| 761 | CRYPTO_MUTEX_lock_write(&ctx->lock); |
| 762 | } |
| 763 | SSL_SESSION *found_session = lh_SSL_SESSION_retrieve(ctx->sessions, |
| 764 | session); |
| 765 | if (found_session == session) { |
| 766 | ret = 1; |
| 767 | found_session = lh_SSL_SESSION_delete(ctx->sessions, session); |
| 768 | SSL_SESSION_list_remove(ctx, session); |
| 769 | } |
| 770 | |
| 771 | if (lock) { |
| 772 | CRYPTO_MUTEX_unlock_write(&ctx->lock); |
| 773 | } |
| 774 | |
| 775 | if (ret) { |
| 776 | if (ctx->remove_session_cb != NULL) { |
| 777 | ctx->remove_session_cb(ctx, found_session); |
| 778 | } |
| 779 | SSL_SESSION_free(found_session); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | return ret; |
| 784 | } |
| 785 | |
| 786 | void ssl_set_session(SSL *ssl, SSL_SESSION *session) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 787 | if (ssl->session.get() == session) { |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 788 | return; |
| 789 | } |
| 790 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 791 | ssl->session = UpRef(session); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 794 | // locked by SSL_CTX in the calling function |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [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) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 801 | // last element in list |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 802 | if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 803 | // only one element in list |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [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) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 812 | // first element in list |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 813 | ctx->session_cache_head = session->next; |
| 814 | session->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 815 | } else { // middle of list |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [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 | |
Robert Sloan | 726e9d1 | 2018-09-11 11:45:04 -0700 | [diff] [blame] | 841 | BSSL_NAMESPACE_END |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 842 | |
| 843 | using namespace bssl; |
| 844 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 845 | ssl_session_st::ssl_session_st(const SSL_X509_METHOD *method) |
| 846 | : x509_method(method), |
| 847 | extended_master_secret(false), |
| 848 | peer_sha256_valid(false), |
| 849 | not_resumable(false), |
| 850 | ticket_age_add_valid(false), |
| 851 | is_server(false) { |
| 852 | CRYPTO_new_ex_data(&ex_data); |
| 853 | time = ::time(nullptr); |
| 854 | } |
| 855 | |
| 856 | ssl_session_st::~ssl_session_st() { |
| 857 | CRYPTO_free_ex_data(&g_ex_data_class, this, &ex_data); |
| 858 | x509_method->session_clear(this); |
| 859 | } |
| 860 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 861 | SSL_SESSION *SSL_SESSION_new(const SSL_CTX *ctx) { |
| 862 | return ssl_session_new(ctx->x509_method).release(); |
| 863 | } |
| 864 | |
| 865 | int SSL_SESSION_up_ref(SSL_SESSION *session) { |
| 866 | CRYPTO_refcount_inc(&session->references); |
| 867 | return 1; |
| 868 | } |
| 869 | |
| 870 | void SSL_SESSION_free(SSL_SESSION *session) { |
| 871 | if (session == NULL || |
| 872 | !CRYPTO_refcount_dec_and_test_zero(&session->references)) { |
| 873 | return; |
| 874 | } |
| 875 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 876 | session->~ssl_session_st(); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 877 | OPENSSL_free(session); |
| 878 | } |
| 879 | |
| 880 | const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *session, |
| 881 | unsigned *out_len) { |
| 882 | if (out_len != NULL) { |
| 883 | *out_len = session->session_id_length; |
| 884 | } |
| 885 | return session->session_id; |
| 886 | } |
| 887 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 888 | int SSL_SESSION_set1_id(SSL_SESSION *session, const uint8_t *sid, |
| 889 | size_t sid_len) { |
| 890 | if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 891 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_TOO_LONG); |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | // Use memmove in case someone passes in the output of |SSL_SESSION_get_id|. |
| 896 | OPENSSL_memmove(session->session_id, sid, sid_len); |
| 897 | session->session_id_length = sid_len; |
| 898 | return 1; |
| 899 | } |
| 900 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 901 | uint32_t SSL_SESSION_get_timeout(const SSL_SESSION *session) { |
| 902 | return session->timeout; |
| 903 | } |
| 904 | |
| 905 | uint64_t SSL_SESSION_get_time(const SSL_SESSION *session) { |
| 906 | if (session == NULL) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 907 | // NULL should crash, but silently accept it here for compatibility. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 908 | return 0; |
| 909 | } |
| 910 | return session->time; |
| 911 | } |
| 912 | |
| 913 | X509 *SSL_SESSION_get0_peer(const SSL_SESSION *session) { |
| 914 | return session->x509_peer; |
| 915 | } |
| 916 | |
Robert Sloan | c6ebb28 | 2018-04-30 10:10:26 -0700 | [diff] [blame] | 917 | const STACK_OF(CRYPTO_BUFFER) * |
| 918 | SSL_SESSION_get0_peer_certificates(const SSL_SESSION *session) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 919 | return session->certs.get(); |
| 920 | } |
| 921 | |
| 922 | void SSL_SESSION_get0_signed_cert_timestamp_list(const SSL_SESSION *session, |
| 923 | const uint8_t **out, |
| 924 | size_t *out_len) { |
| 925 | if (session->signed_cert_timestamp_list) { |
| 926 | *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list.get()); |
| 927 | *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list.get()); |
| 928 | } else { |
| 929 | *out = nullptr; |
| 930 | *out_len = 0; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | void SSL_SESSION_get0_ocsp_response(const SSL_SESSION *session, |
| 935 | const uint8_t **out, size_t *out_len) { |
| 936 | if (session->ocsp_response) { |
| 937 | *out = CRYPTO_BUFFER_data(session->ocsp_response.get()); |
| 938 | *out_len = CRYPTO_BUFFER_len(session->ocsp_response.get()); |
| 939 | } else { |
| 940 | *out = nullptr; |
| 941 | *out_len = 0; |
| 942 | } |
Robert Sloan | c6ebb28 | 2018-04-30 10:10:26 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 945 | size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, uint8_t *out, |
| 946 | size_t max_out) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 947 | // TODO(davidben): Fix master_key_length's type and remove these casts. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 948 | if (max_out == 0) { |
| 949 | return (size_t)session->master_key_length; |
| 950 | } |
| 951 | if (max_out > (size_t)session->master_key_length) { |
| 952 | max_out = (size_t)session->master_key_length; |
| 953 | } |
| 954 | OPENSSL_memcpy(out, session->master_key, max_out); |
| 955 | return max_out; |
| 956 | } |
| 957 | |
| 958 | uint64_t SSL_SESSION_set_time(SSL_SESSION *session, uint64_t time) { |
| 959 | if (session == NULL) { |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | session->time = time; |
| 964 | return time; |
| 965 | } |
| 966 | |
| 967 | uint32_t SSL_SESSION_set_timeout(SSL_SESSION *session, uint32_t timeout) { |
| 968 | if (session == NULL) { |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | session->timeout = timeout; |
| 973 | session->auth_timeout = timeout; |
| 974 | return 1; |
| 975 | } |
| 976 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 977 | const uint8_t *SSL_SESSION_get0_id_context(const SSL_SESSION *session, |
| 978 | unsigned *out_len) { |
| 979 | if (out_len != NULL) { |
| 980 | *out_len = session->sid_ctx_length; |
| 981 | } |
| 982 | return session->sid_ctx; |
| 983 | } |
| 984 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 985 | int SSL_SESSION_set1_id_context(SSL_SESSION *session, const uint8_t *sid_ctx, |
| 986 | size_t sid_ctx_len) { |
| 987 | if (sid_ctx_len > sizeof(session->sid_ctx)) { |
| 988 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
| 989 | return 0; |
| 990 | } |
| 991 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 992 | static_assert(sizeof(session->sid_ctx) < 256, "sid_ctx_len does not fit"); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 993 | session->sid_ctx_length = (uint8_t)sid_ctx_len; |
| 994 | OPENSSL_memcpy(session->sid_ctx, sid_ctx, sid_ctx_len); |
| 995 | |
| 996 | return 1; |
| 997 | } |
| 998 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 999 | int SSL_SESSION_should_be_single_use(const SSL_SESSION *session) { |
Robert Sloan | ae1abf9 | 2017-10-05 12:50:08 -0700 | [diff] [blame] | 1000 | return ssl_session_protocol_version(session) >= TLS1_3_VERSION; |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | int SSL_SESSION_is_resumable(const SSL_SESSION *session) { |
| 1004 | return !session->not_resumable; |
| 1005 | } |
| 1006 | |
| 1007 | int SSL_SESSION_has_ticket(const SSL_SESSION *session) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1008 | return !session->ticket.empty(); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | void SSL_SESSION_get0_ticket(const SSL_SESSION *session, |
| 1012 | const uint8_t **out_ticket, size_t *out_len) { |
| 1013 | if (out_ticket != nullptr) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1014 | *out_ticket = session->ticket.data(); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1015 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1016 | *out_len = session->ticket.size(); |
| 1017 | } |
| 1018 | |
| 1019 | int SSL_SESSION_set_ticket(SSL_SESSION *session, const uint8_t *ticket, |
| 1020 | size_t ticket_len) { |
| 1021 | return session->ticket.CopyFrom(MakeConstSpan(ticket, ticket_len)); |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | uint32_t SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *session) { |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1025 | return session->ticket_lifetime_hint; |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Robert Sloan | 15c0b35 | 2018-04-16 08:36:46 -0700 | [diff] [blame] | 1028 | const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *session) { |
| 1029 | return session->cipher; |
| 1030 | } |
| 1031 | |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1032 | int SSL_SESSION_has_peer_sha256(const SSL_SESSION *session) { |
| 1033 | return session->peer_sha256_valid; |
| 1034 | } |
| 1035 | |
| 1036 | void SSL_SESSION_get0_peer_sha256(const SSL_SESSION *session, |
| 1037 | const uint8_t **out_ptr, size_t *out_len) { |
| 1038 | if (session->peer_sha256_valid) { |
| 1039 | *out_ptr = session->peer_sha256; |
| 1040 | *out_len = sizeof(session->peer_sha256); |
| 1041 | } else { |
| 1042 | *out_ptr = nullptr; |
| 1043 | *out_len = 0; |
| 1044 | } |
| 1045 | } |
| 1046 | |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1047 | SSL_SESSION *SSL_magic_pending_session_ptr(void) { |
| 1048 | return (SSL_SESSION *)&g_pending_session_magic; |
| 1049 | } |
| 1050 | |
| 1051 | SSL_SESSION *SSL_get_session(const SSL *ssl) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1052 | // Once the handshake completes we return the established session. Otherwise |
| 1053 | // we return the intermediate session, either |session| (for resumption) or |
| 1054 | // |new_session| if doing a full handshake. |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1055 | if (!SSL_in_init(ssl)) { |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 1056 | return ssl->s3->established_session.get(); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1057 | } |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 1058 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1059 | if (hs->early_session) { |
| 1060 | return hs->early_session.get(); |
| 1061 | } |
| 1062 | if (hs->new_session) { |
| 1063 | return hs->new_session.get(); |
| 1064 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1065 | return ssl->session.get(); |
Robert Sloan | b6d070c | 2017-07-24 08:40:01 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | SSL_SESSION *SSL_get1_session(SSL *ssl) { |
| 1069 | SSL_SESSION *ret = SSL_get_session(ssl); |
| 1070 | if (ret != NULL) { |
| 1071 | SSL_SESSION_up_ref(ret); |
| 1072 | } |
| 1073 | return ret; |
| 1074 | } |
| 1075 | |
| 1076 | int SSL_SESSION_get_ex_new_index(long argl, void *argp, |
| 1077 | CRYPTO_EX_unused *unused, |
| 1078 | CRYPTO_EX_dup *dup_unused, |
| 1079 | CRYPTO_EX_free *free_func) { |
| 1080 | int index; |
| 1081 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, |
| 1082 | free_func)) { |
| 1083 | return -1; |
| 1084 | } |
| 1085 | return index; |
| 1086 | } |
| 1087 | |
| 1088 | int SSL_SESSION_set_ex_data(SSL_SESSION *session, int idx, void *arg) { |
| 1089 | return CRYPTO_set_ex_data(&session->ex_data, idx, arg); |
| 1090 | } |
| 1091 | |
| 1092 | void *SSL_SESSION_get_ex_data(const SSL_SESSION *session, int idx) { |
| 1093 | return CRYPTO_get_ex_data(&session->ex_data, idx); |
| 1094 | } |
| 1095 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1096 | int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *session) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1097 | // Although |session| is inserted into two structures (a doubly-linked list |
| 1098 | // and the hash table), |ctx| only takes one reference. |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 1099 | UniquePtr<SSL_SESSION> owned_session = UpRef(session); |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1100 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1101 | SSL_SESSION *old_session; |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1102 | MutexWriteLock lock(&ctx->lock); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1103 | if (!lh_SSL_SESSION_insert(ctx->sessions, &old_session, session)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1104 | return 0; |
| 1105 | } |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1106 | // |ctx->sessions| took ownership of |session| and gave us back a reference to |
| 1107 | // |old_session|. (|old_session| may be the same as |session|, in which case |
| 1108 | // we traded identical references with |ctx->sessions|.) |
| 1109 | owned_session.release(); |
| 1110 | owned_session.reset(old_session); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1111 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1112 | if (old_session != NULL) { |
| 1113 | if (old_session == session) { |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1114 | // |session| was already in the cache. There are no linked list pointers |
| 1115 | // to update. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1116 | return 0; |
| 1117 | } |
| 1118 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1119 | // There was a session ID collision. |old_session| was replaced with |
| 1120 | // |session| in the hash table, so |old_session| must be removed from the |
| 1121 | // linked list to match. |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1122 | SSL_SESSION_list_remove(ctx, old_session); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1125 | SSL_SESSION_list_add(ctx, session); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1126 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1127 | // Enforce any cache size limits. |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1128 | if (SSL_CTX_sess_get_cache_size(ctx) > 0) { |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1129 | while (lh_SSL_SESSION_num_items(ctx->sessions) > |
| 1130 | SSL_CTX_sess_get_cache_size(ctx)) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1131 | if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) { |
| 1132 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1137 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1140 | int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *session) { |
| 1141 | return remove_session_lock(ctx, session, 1); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1144 | int SSL_set_session(SSL *ssl, SSL_SESSION *session) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1145 | // SSL_set_session may only be called before the handshake has started. |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 1146 | if (ssl->s3->initial_handshake_complete || |
| 1147 | ssl->s3->hs == NULL || |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1148 | ssl->s3->hs->state != 0) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1149 | abort(); |
| 1150 | } |
| 1151 | |
| 1152 | ssl_set_session(ssl, session); |
| 1153 | return 1; |
| 1154 | } |
| 1155 | |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 1156 | uint32_t SSL_CTX_set_timeout(SSL_CTX *ctx, uint32_t timeout) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1157 | if (ctx == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1158 | return 0; |
| 1159 | } |
| 1160 | |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1161 | // Historically, zero was treated as |SSL_DEFAULT_SESSION_TIMEOUT|. |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1162 | if (timeout == 0) { |
| 1163 | timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
| 1164 | } |
| 1165 | |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 1166 | uint32_t old_timeout = ctx->session_timeout; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1167 | ctx->session_timeout = timeout; |
| 1168 | return old_timeout; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 1171 | uint32_t SSL_CTX_get_timeout(const SSL_CTX *ctx) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1172 | if (ctx == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1173 | return 0; |
| 1174 | } |
| 1175 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1176 | return ctx->session_timeout; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 1179 | void SSL_CTX_set_session_psk_dhe_timeout(SSL_CTX *ctx, uint32_t timeout) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1180 | ctx->session_psk_dhe_timeout = timeout; |
| 1181 | } |
| 1182 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1183 | typedef struct timeout_param_st { |
| 1184 | SSL_CTX *ctx; |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 1185 | uint64_t time; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1186 | LHASH_OF(SSL_SESSION) *cache; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1187 | } TIMEOUT_PARAM; |
| 1188 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1189 | static void timeout_doall_arg(SSL_SESSION *session, void *void_param) { |
Robert Sloan | a12bf46 | 2017-07-17 07:08:26 -0700 | [diff] [blame] | 1190 | TIMEOUT_PARAM *param = reinterpret_cast<TIMEOUT_PARAM *>(void_param); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1191 | |
| 1192 | if (param->time == 0 || |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 1193 | session->time + session->timeout < session->time || |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1194 | param->time > (session->time + session->timeout)) { |
Robert Sloan | a27a6a4 | 2017-09-05 08:39:28 -0700 | [diff] [blame] | 1195 | // The reason we don't call SSL_CTX_remove_session() is to |
| 1196 | // save on locking overhead |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1197 | (void) lh_SSL_SESSION_delete(param->cache, session); |
| 1198 | SSL_SESSION_list_remove(param->ctx, session); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1199 | if (param->ctx->remove_session_cb != NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1200 | param->ctx->remove_session_cb(param->ctx, session); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1201 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1202 | SSL_SESSION_free(session); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 1206 | void SSL_CTX_flush_sessions(SSL_CTX *ctx, uint64_t time) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1207 | TIMEOUT_PARAM tp; |
| 1208 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1209 | tp.ctx = ctx; |
| 1210 | tp.cache = ctx->sessions; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1211 | if (tp.cache == NULL) { |
| 1212 | return; |
| 1213 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1214 | tp.time = time; |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1215 | MutexWriteLock lock(&ctx->lock); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1216 | lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1219 | void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1220 | int (*cb)(SSL *ssl, SSL_SESSION *session)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1221 | ctx->new_session_cb = cb; |
| 1222 | } |
| 1223 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1224 | int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *session) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1225 | return ctx->new_session_cb; |
| 1226 | } |
| 1227 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1228 | void SSL_CTX_sess_set_remove_cb( |
| 1229 | SSL_CTX *ctx, void (*cb)(SSL_CTX *ctx, SSL_SESSION *session)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1230 | ctx->remove_session_cb = cb; |
| 1231 | } |
| 1232 | |
| 1233 | void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx, |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1234 | SSL_SESSION *session) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1235 | return ctx->remove_session_cb; |
| 1236 | } |
| 1237 | |
| 1238 | void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1239 | SSL_SESSION *(*cb)(SSL *ssl, const uint8_t *id, |
| 1240 | int id_len, int *out_copy)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1241 | ctx->get_session_cb = cb; |
| 1242 | } |
| 1243 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 1244 | SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, |
| 1245 | const uint8_t *id, |
| 1246 | int id_len, |
| 1247 | int *out_copy) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1248 | return ctx->get_session_cb; |
| 1249 | } |
| 1250 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1251 | void SSL_CTX_set_info_callback( |
| 1252 | SSL_CTX *ctx, void (*cb)(const SSL *ssl, int type, int value)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1253 | ctx->info_callback = cb; |
| 1254 | } |
| 1255 | |
| 1256 | void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1257 | int value) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1258 | return ctx->info_callback; |
| 1259 | } |
| 1260 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1261 | void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx, |
| 1262 | void (*cb)(SSL *ssl, EVP_PKEY **pkey)) { |
| 1263 | ctx->channel_id_cb = cb; |
| 1264 | } |
| 1265 | |
| 1266 | void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) { |
| 1267 | return ctx->channel_id_cb; |
| 1268 | } |