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 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 142 | #include <openssl/err.h> |
| 143 | #include <openssl/lhash.h> |
| 144 | #include <openssl/mem.h> |
| 145 | #include <openssl/rand.h> |
| 146 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 147 | #include "internal.h" |
David Benjamin | 546f1a5 | 2015-04-15 16:46:09 -0400 | [diff] [blame] | 148 | #include "../crypto/internal.h" |
| 149 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | /* The address of this is a magic value, a pointer to which is returned by |
| 152 | * SSL_magic_pending_session_ptr(). It allows a session callback to indicate |
| 153 | * that it needs to asynchronously fetch session information. */ |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 154 | static const char g_pending_session_magic = 0; |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | |
David Benjamin | aa58513 | 2015-06-29 23:36:17 -0400 | [diff] [blame] | 156 | static CRYPTO_EX_DATA_CLASS g_ex_data_class = |
| 157 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 158 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 159 | static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session); |
| 160 | static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session); |
| 161 | static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | |
David Benjamin | abb6c1c | 2015-09-13 14:46:26 -0400 | [diff] [blame] | 163 | SSL_SESSION *SSL_SESSION_new(void) { |
Brian Smith | 5ba0689 | 2016-02-07 09:36:04 -1000 | [diff] [blame] | 164 | SSL_SESSION *session = OPENSSL_malloc(sizeof(SSL_SESSION)); |
David Benjamin | abb6c1c | 2015-09-13 14:46:26 -0400 | [diff] [blame] | 165 | if (session == NULL) { |
| 166 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 167 | return 0; |
| 168 | } |
| 169 | memset(session, 0, sizeof(SSL_SESSION)); |
| 170 | |
Adam Langley | 9498e74 | 2016-07-18 10:17:16 -0700 | [diff] [blame] | 171 | session->verify_result = X509_V_ERR_INVALID_CALL; |
David Benjamin | abb6c1c | 2015-09-13 14:46:26 -0400 | [diff] [blame] | 172 | session->references = 1; |
| 173 | session->timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame] | 174 | session->time = (long)time(NULL); |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 175 | CRYPTO_new_ex_data(&session->ex_data); |
David Benjamin | abb6c1c | 2015-09-13 14:46:26 -0400 | [diff] [blame] | 176 | return session; |
| 177 | } |
| 178 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 179 | SSL_SESSION *SSL_SESSION_dup(SSL_SESSION *session, int dup_flags) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 180 | SSL_SESSION *new_session = SSL_SESSION_new(); |
| 181 | if (new_session == NULL) { |
| 182 | goto err; |
| 183 | } |
| 184 | |
| 185 | new_session->ssl_version = session->ssl_version; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 186 | new_session->sid_ctx_length = session->sid_ctx_length; |
| 187 | memcpy(new_session->sid_ctx, session->sid_ctx, session->sid_ctx_length); |
| 188 | |
| 189 | /* Copy the key material. */ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 190 | new_session->master_key_length = session->master_key_length; |
| 191 | memcpy(new_session->master_key, session->master_key, |
| 192 | session->master_key_length); |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 193 | new_session->cipher = session->cipher; |
| 194 | |
| 195 | /* Copy authentication state. */ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 196 | if (session->psk_identity != NULL) { |
| 197 | new_session->psk_identity = BUF_strdup(session->psk_identity); |
| 198 | if (new_session->psk_identity == NULL) { |
| 199 | goto err; |
| 200 | } |
| 201 | } |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 202 | if (session->x509_peer != NULL) { |
| 203 | X509_up_ref(session->x509_peer); |
| 204 | new_session->x509_peer = session->x509_peer; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 205 | } |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 206 | if (session->x509_chain != NULL) { |
| 207 | new_session->x509_chain = X509_chain_up_ref(session->x509_chain); |
| 208 | if (new_session->x509_chain == NULL) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 209 | goto err; |
| 210 | } |
| 211 | } |
| 212 | new_session->verify_result = session->verify_result; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 213 | |
| 214 | new_session->ocsp_response_length = session->ocsp_response_length; |
| 215 | if (session->ocsp_response != NULL) { |
| 216 | new_session->ocsp_response = BUF_memdup(session->ocsp_response, |
| 217 | session->ocsp_response_length); |
| 218 | if (new_session->ocsp_response == NULL) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 219 | goto err; |
| 220 | } |
| 221 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 222 | |
| 223 | new_session->tlsext_signed_cert_timestamp_list_length = |
| 224 | session->tlsext_signed_cert_timestamp_list_length; |
| 225 | if (session->tlsext_signed_cert_timestamp_list != NULL) { |
| 226 | new_session->tlsext_signed_cert_timestamp_list = |
| 227 | BUF_memdup(session->tlsext_signed_cert_timestamp_list, |
| 228 | session->tlsext_signed_cert_timestamp_list_length); |
| 229 | if (new_session->tlsext_signed_cert_timestamp_list == NULL) { |
| 230 | goto err; |
| 231 | } |
| 232 | } |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 233 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 234 | memcpy(new_session->peer_sha256, session->peer_sha256, SHA256_DIGEST_LENGTH); |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 235 | new_session->peer_sha256_valid = session->peer_sha256_valid; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 236 | |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 237 | if (session->tlsext_hostname != NULL) { |
| 238 | new_session->tlsext_hostname = BUF_strdup(session->tlsext_hostname); |
| 239 | if (new_session->tlsext_hostname == NULL) { |
| 240 | goto err; |
| 241 | } |
| 242 | } |
| 243 | |
David Benjamin | 01a9057 | 2016-09-22 00:11:43 -0400 | [diff] [blame] | 244 | new_session->timeout = session->timeout; |
| 245 | new_session->time = session->time; |
| 246 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 247 | /* Copy non-authentication connection properties. */ |
| 248 | if (dup_flags & SSL_SESSION_INCLUDE_NONAUTH) { |
| 249 | new_session->session_id_length = session->session_id_length; |
| 250 | memcpy(new_session->session_id, session->session_id, |
| 251 | session->session_id_length); |
| 252 | |
| 253 | new_session->key_exchange_info = session->key_exchange_info; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 254 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 255 | memcpy(new_session->original_handshake_hash, |
| 256 | session->original_handshake_hash, |
| 257 | session->original_handshake_hash_len); |
| 258 | new_session->original_handshake_hash_len = |
| 259 | session->original_handshake_hash_len; |
| 260 | new_session->tlsext_tick_lifetime_hint = session->tlsext_tick_lifetime_hint; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 261 | new_session->ticket_age_add = session->ticket_age_add; |
| 262 | new_session->extended_master_secret = session->extended_master_secret; |
| 263 | } |
| 264 | |
| 265 | /* Copy the ticket. */ |
| 266 | if (dup_flags & SSL_SESSION_INCLUDE_TICKET) { |
| 267 | if (session->tlsext_tick != NULL) { |
| 268 | new_session->tlsext_tick = |
| 269 | BUF_memdup(session->tlsext_tick, session->tlsext_ticklen); |
| 270 | if (new_session->tlsext_tick == NULL) { |
| 271 | goto err; |
| 272 | } |
| 273 | } |
| 274 | new_session->tlsext_ticklen = session->tlsext_ticklen; |
| 275 | } |
| 276 | |
| 277 | /* The new_session does not get a copy of the ex_data. */ |
| 278 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 279 | new_session->not_resumable = 1; |
| 280 | return new_session; |
| 281 | |
| 282 | err: |
| 283 | SSL_SESSION_free(new_session); |
| 284 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 285 | return 0; |
| 286 | } |
| 287 | |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 288 | void ssl_session_refresh_time(SSL *ssl, SSL_SESSION *session) { |
| 289 | struct timeval now; |
| 290 | ssl_get_current_time(ssl, &now); |
| 291 | |
| 292 | /* To avoid overflows and underflows, if we've gone back in time or any value |
| 293 | * is negative, update the time, but mark the session expired. */ |
| 294 | if (session->time > now.tv_sec || |
| 295 | session->time < 0 || |
| 296 | now.tv_sec < 0) { |
| 297 | session->time = now.tv_sec; |
| 298 | session->timeout = 0; |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | /* Adjust the session time and timeout. If the session has already expired, |
| 303 | * clamp the timeout at zero. */ |
| 304 | long delta = now.tv_sec - session->time; |
| 305 | session->time = now.tv_sec; |
| 306 | if (session->timeout < delta) { |
| 307 | session->timeout = 0; |
| 308 | } else { |
| 309 | session->timeout -= delta; |
| 310 | } |
| 311 | } |
| 312 | |
David Benjamin | b919540 | 2016-08-05 10:51:43 -0400 | [diff] [blame] | 313 | int SSL_SESSION_up_ref(SSL_SESSION *session) { |
| 314 | CRYPTO_refcount_inc(&session->references); |
| 315 | return 1; |
David Benjamin | abb6c1c | 2015-09-13 14:46:26 -0400 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void SSL_SESSION_free(SSL_SESSION *session) { |
| 319 | if (session == NULL || |
| 320 | !CRYPTO_refcount_dec_and_test_zero(&session->references)) { |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | CRYPTO_free_ex_data(&g_ex_data_class, session, &session->ex_data); |
| 325 | |
| 326 | OPENSSL_cleanse(session->master_key, sizeof(session->master_key)); |
| 327 | OPENSSL_cleanse(session->session_id, sizeof(session->session_id)); |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 328 | X509_free(session->x509_peer); |
| 329 | sk_X509_pop_free(session->x509_chain, X509_free); |
David Benjamin | abb6c1c | 2015-09-13 14:46:26 -0400 | [diff] [blame] | 330 | OPENSSL_free(session->tlsext_hostname); |
| 331 | OPENSSL_free(session->tlsext_tick); |
| 332 | OPENSSL_free(session->tlsext_signed_cert_timestamp_list); |
| 333 | OPENSSL_free(session->ocsp_response); |
| 334 | OPENSSL_free(session->psk_identity); |
| 335 | OPENSSL_cleanse(session, sizeof(*session)); |
| 336 | OPENSSL_free(session); |
| 337 | } |
| 338 | |
| 339 | const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *session, |
| 340 | unsigned *out_len) { |
| 341 | if (out_len != NULL) { |
| 342 | *out_len = session->session_id_length; |
| 343 | } |
| 344 | return session->session_id; |
| 345 | } |
| 346 | |
| 347 | long SSL_SESSION_get_timeout(const SSL_SESSION *session) { |
| 348 | return session->timeout; |
| 349 | } |
| 350 | |
| 351 | long SSL_SESSION_get_time(const SSL_SESSION *session) { |
David Benjamin | 20c0e90 | 2015-10-01 16:13:22 -0400 | [diff] [blame] | 352 | if (session == NULL) { |
| 353 | /* NULL should crash, but silently accept it here for compatibility. */ |
| 354 | return 0; |
| 355 | } |
David Benjamin | abb6c1c | 2015-09-13 14:46:26 -0400 | [diff] [blame] | 356 | return session->time; |
| 357 | } |
| 358 | |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 359 | X509 *SSL_SESSION_get0_peer(const SSL_SESSION *session) { |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 360 | return session->x509_peer; |
David Benjamin | 14e2b50 | 2015-09-13 14:48:12 -0400 | [diff] [blame] | 361 | } |
David Benjamin | abb6c1c | 2015-09-13 14:46:26 -0400 | [diff] [blame] | 362 | |
David Benjamin | 818aff0 | 2016-04-21 16:58:49 -0400 | [diff] [blame] | 363 | size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, uint8_t *out, |
| 364 | size_t max_out) { |
| 365 | /* TODO(davidben): Fix master_key_length's type and remove these casts. */ |
| 366 | if (max_out == 0) { |
| 367 | return (size_t)session->master_key_length; |
| 368 | } |
| 369 | if (max_out > (size_t)session->master_key_length) { |
| 370 | max_out = (size_t)session->master_key_length; |
| 371 | } |
| 372 | memcpy(out, session->master_key, max_out); |
| 373 | return max_out; |
| 374 | } |
| 375 | |
David Benjamin | abb6c1c | 2015-09-13 14:46:26 -0400 | [diff] [blame] | 376 | long SSL_SESSION_set_time(SSL_SESSION *session, long time) { |
| 377 | if (session == NULL) { |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | session->time = time; |
| 382 | return time; |
| 383 | } |
| 384 | |
| 385 | long SSL_SESSION_set_timeout(SSL_SESSION *session, long timeout) { |
| 386 | if (session == NULL) { |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | session->timeout = timeout; |
| 391 | return 1; |
| 392 | } |
| 393 | |
| 394 | int SSL_SESSION_set1_id_context(SSL_SESSION *session, const uint8_t *sid_ctx, |
| 395 | unsigned sid_ctx_len) { |
| 396 | if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { |
| 397 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | session->sid_ctx_length = sid_ctx_len; |
| 402 | memcpy(session->sid_ctx, sid_ctx, sid_ctx_len); |
| 403 | |
| 404 | return 1; |
| 405 | } |
| 406 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 407 | SSL_SESSION *SSL_magic_pending_session_ptr(void) { |
| 408 | return (SSL_SESSION *)&g_pending_session_magic; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Adam Langley | d4aae0f | 2016-08-01 12:29:38 -0700 | [diff] [blame] | 411 | SSL_SESSION *SSL_get_session(const SSL *ssl) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 412 | /* Once the handshake completes we return the established session. Otherwise |
| 413 | * we return the intermediate session, either |session| (for resumption) or |
| 414 | * |new_session| if doing a full handshake. */ |
| 415 | if (!SSL_in_init(ssl)) { |
| 416 | return ssl->s3->established_session; |
| 417 | } |
| 418 | if (ssl->s3->new_session != NULL) { |
| 419 | return ssl->s3->new_session; |
| 420 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 421 | return ssl->session; |
| 422 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 423 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 424 | SSL_SESSION *SSL_get1_session(SSL *ssl) { |
David Benjamin | b919540 | 2016-08-05 10:51:43 -0400 | [diff] [blame] | 425 | SSL_SESSION *ret = SSL_get_session(ssl); |
| 426 | if (ret != NULL) { |
| 427 | SSL_SESSION_up_ref(ret); |
| 428 | } |
| 429 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 430 | } |
Adam Langley | 8eaaa86 | 2014-08-11 17:18:25 -0700 | [diff] [blame] | 431 | |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 432 | int SSL_SESSION_get_ex_new_index(long argl, void *argp, |
| 433 | CRYPTO_EX_unused *unused, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 434 | CRYPTO_EX_dup *dup_func, |
| 435 | CRYPTO_EX_free *free_func) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 436 | int index; |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 437 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func, |
| 438 | free_func)) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 439 | return -1; |
| 440 | } |
| 441 | return index; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 442 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 443 | |
David Benjamin | 190ab7f | 2015-09-13 14:20:58 -0400 | [diff] [blame] | 444 | int SSL_SESSION_set_ex_data(SSL_SESSION *session, int idx, void *arg) { |
| 445 | return CRYPTO_set_ex_data(&session->ex_data, idx, arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 446 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 447 | |
David Benjamin | 190ab7f | 2015-09-13 14:20:58 -0400 | [diff] [blame] | 448 | void *SSL_SESSION_get_ex_data(const SSL_SESSION *session, int idx) { |
| 449 | return CRYPTO_get_ex_data(&session->ex_data, idx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 450 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 451 | |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 452 | int ssl_get_new_session(SSL *ssl, int is_server) { |
| 453 | if (ssl->mode & SSL_MODE_NO_SESSION_CREATION) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 454 | OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_MAY_NOT_BE_CREATED); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 455 | return 0; |
| 456 | } |
| 457 | |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 458 | SSL_SESSION *session = SSL_SESSION_new(); |
| 459 | if (session == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 460 | return 0; |
| 461 | } |
| 462 | |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame] | 463 | /* Fill in the time from the |SSL_CTX|'s clock. */ |
| 464 | struct timeval now; |
| 465 | ssl_get_current_time(ssl, &now); |
| 466 | session->time = now.tv_sec; |
| 467 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 468 | /* If the context has a default timeout, use it over the default. */ |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 469 | if (ssl->initial_ctx->session_timeout != 0) { |
| 470 | session->timeout = ssl->initial_ctx->session_timeout; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 471 | } |
| 472 | |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 473 | session->ssl_version = ssl->version; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 474 | |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 475 | if (is_server) { |
David Benjamin | 78476f6 | 2016-11-12 11:20:55 +0900 | [diff] [blame] | 476 | if (ssl->s3->hs->ticket_expected) { |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 477 | /* Don't set session IDs for sessions resumed with tickets. This will keep |
| 478 | * them out of the session cache. */ |
| 479 | session->session_id_length = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 480 | } else { |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 481 | session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; |
| 482 | if (!RAND_bytes(session->session_id, session->session_id_length)) { |
| 483 | goto err; |
| 484 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 485 | } |
| 486 | |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 487 | if (ssl->tlsext_hostname != NULL) { |
| 488 | session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname); |
| 489 | if (session->tlsext_hostname == NULL) { |
| 490 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 491 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } else { |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 495 | session->session_id_length = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 496 | } |
| 497 | |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 498 | if (ssl->sid_ctx_length > sizeof(session->sid_ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 499 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 500 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 501 | } |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 502 | memcpy(session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length); |
| 503 | session->sid_ctx_length = ssl->sid_ctx_length; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 504 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 505 | /* The session is marked not resumable until it is completely filled in. */ |
| 506 | session->not_resumable = 1; |
Adam Langley | 9498e74 | 2016-07-18 10:17:16 -0700 | [diff] [blame] | 507 | session->verify_result = X509_V_ERR_INVALID_CALL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 508 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 509 | SSL_SESSION_free(ssl->s3->new_session); |
| 510 | ssl->s3->new_session = session; |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 511 | ssl_set_session(ssl, NULL); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 512 | return 1; |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 513 | |
| 514 | err: |
| 515 | SSL_SESSION_free(session); |
| 516 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 517 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 518 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 519 | int ssl_encrypt_ticket(SSL *ssl, CBB *out, const SSL_SESSION *session) { |
| 520 | int ret = 0; |
| 521 | |
| 522 | /* Serialize the SSL_SESSION to be encoded into the ticket. */ |
| 523 | uint8_t *session_buf = NULL; |
| 524 | size_t session_len; |
| 525 | if (!SSL_SESSION_to_bytes_for_ticket(session, &session_buf, &session_len)) { |
| 526 | return -1; |
| 527 | } |
| 528 | |
| 529 | EVP_CIPHER_CTX ctx; |
| 530 | EVP_CIPHER_CTX_init(&ctx); |
| 531 | HMAC_CTX hctx; |
| 532 | HMAC_CTX_init(&hctx); |
| 533 | |
| 534 | /* If the session is too long, emit a dummy value rather than abort the |
| 535 | * connection. */ |
| 536 | static const size_t kMaxTicketOverhead = |
| 537 | 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE; |
| 538 | if (session_len > 0xffff - kMaxTicketOverhead) { |
| 539 | static const char kTicketPlaceholder[] = "TICKET TOO LARGE"; |
| 540 | if (CBB_add_bytes(out, (const uint8_t *)kTicketPlaceholder, |
| 541 | strlen(kTicketPlaceholder))) { |
| 542 | ret = 1; |
| 543 | } |
| 544 | goto err; |
| 545 | } |
| 546 | |
| 547 | /* Initialize HMAC and cipher contexts. If callback present it does all the |
| 548 | * work otherwise use generated values from parent ctx. */ |
| 549 | SSL_CTX *tctx = ssl->initial_ctx; |
| 550 | uint8_t iv[EVP_MAX_IV_LENGTH]; |
| 551 | uint8_t key_name[16]; |
| 552 | if (tctx->tlsext_ticket_key_cb != NULL) { |
| 553 | if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, &ctx, &hctx, |
| 554 | 1 /* encrypt */) < 0) { |
| 555 | goto err; |
| 556 | } |
| 557 | } else { |
| 558 | if (!RAND_bytes(iv, 16) || |
| 559 | !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
| 560 | tctx->tlsext_tick_aes_key, iv) || |
| 561 | !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(), |
| 562 | NULL)) { |
| 563 | goto err; |
| 564 | } |
| 565 | memcpy(key_name, tctx->tlsext_tick_key_name, 16); |
| 566 | } |
| 567 | |
| 568 | uint8_t *ptr; |
| 569 | if (!CBB_add_bytes(out, key_name, 16) || |
| 570 | !CBB_add_bytes(out, iv, EVP_CIPHER_CTX_iv_length(&ctx)) || |
| 571 | !CBB_reserve(out, &ptr, session_len + EVP_MAX_BLOCK_LENGTH)) { |
| 572 | goto err; |
| 573 | } |
| 574 | |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 575 | size_t total = 0; |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 576 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 577 | memcpy(ptr, session_buf, session_len); |
| 578 | total = session_len; |
| 579 | #else |
| 580 | int len; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 581 | if (!EVP_EncryptUpdate(&ctx, ptr + total, &len, session_buf, session_len)) { |
| 582 | goto err; |
| 583 | } |
| 584 | total += len; |
| 585 | if (!EVP_EncryptFinal_ex(&ctx, ptr + total, &len)) { |
| 586 | goto err; |
| 587 | } |
| 588 | total += len; |
David Benjamin | fbc45d7 | 2016-09-22 01:21:24 -0400 | [diff] [blame] | 589 | #endif |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 590 | if (!CBB_did_write(out, total)) { |
| 591 | goto err; |
| 592 | } |
| 593 | |
| 594 | unsigned hlen; |
| 595 | if (!HMAC_Update(&hctx, CBB_data(out), CBB_len(out)) || |
| 596 | !CBB_reserve(out, &ptr, EVP_MAX_MD_SIZE) || |
| 597 | !HMAC_Final(&hctx, ptr, &hlen) || |
| 598 | !CBB_did_write(out, hlen)) { |
| 599 | goto err; |
| 600 | } |
| 601 | |
| 602 | ret = 1; |
| 603 | |
| 604 | err: |
| 605 | OPENSSL_free(session_buf); |
| 606 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 607 | HMAC_CTX_cleanup(&hctx); |
| 608 | return ret; |
| 609 | } |
| 610 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 611 | int ssl_session_is_context_valid(const SSL *ssl, const SSL_SESSION *session) { |
| 612 | if (session == NULL) { |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | return session->sid_ctx_length == ssl->sid_ctx_length && |
| 617 | memcmp(session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length) == 0; |
| 618 | } |
| 619 | |
| 620 | int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session) { |
| 621 | if (session == NULL) { |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | struct timeval now; |
| 626 | ssl_get_current_time(ssl, &now); |
David Benjamin | 1b22f85 | 2016-10-27 16:36:32 -0400 | [diff] [blame] | 627 | |
| 628 | /* Reject tickets from the future to avoid underflow. */ |
| 629 | if ((long)now.tv_sec < session->time) { |
| 630 | return 0; |
| 631 | } |
| 632 | |
David Benjamin | 8e816eb | 2016-11-02 23:24:29 -0400 | [diff] [blame] | 633 | return session->timeout > (long)now.tv_sec - session->time; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 634 | } |
| 635 | |
David Benjamin | 75f9914 | 2016-11-12 12:36:06 +0900 | [diff] [blame] | 636 | int ssl_session_is_resumable(const SSL *ssl, const SSL_SESSION *session) { |
| 637 | return ssl_session_is_context_valid(ssl, session) && |
| 638 | /* The session must not be expired. */ |
| 639 | ssl_session_is_time_valid(ssl, session) && |
| 640 | /* Only resume if the session's version matches the negotiated |
| 641 | * version. */ |
| 642 | ssl->version == session->ssl_version && |
David Benjamin | f01f42a | 2016-11-16 19:05:33 +0900 | [diff] [blame] | 643 | /* Only resume if the session's cipher matches the negotiated one. */ |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame^] | 644 | ssl->s3->tmp.new_cipher == session->cipher && |
| 645 | /* If the session contains a client certificate (either the full |
| 646 | * certificate or just the hash) then require that the form of the |
| 647 | * certificate matches the current configuration. */ |
| 648 | ((session->x509_peer == NULL && !session->peer_sha256_valid) || |
| 649 | session->peer_sha256_valid == |
| 650 | ssl->retain_only_sha256_of_client_certs); |
David Benjamin | 75f9914 | 2016-11-12 12:36:06 +0900 | [diff] [blame] | 651 | } |
| 652 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 653 | /* ssl_lookup_session looks up |session_id| in the session cache and sets |
| 654 | * |*out_session| to an |SSL_SESSION| object if found. The caller takes |
| 655 | * ownership of the result. */ |
| 656 | static enum ssl_session_result_t ssl_lookup_session( |
| 657 | SSL *ssl, SSL_SESSION **out_session, const uint8_t *session_id, |
| 658 | size_t session_id_len) { |
| 659 | *out_session = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 660 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 661 | if (session_id_len == 0 || session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 662 | return ssl_session_success; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 663 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 664 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 665 | SSL_SESSION *session = NULL; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 666 | /* Try the internal cache, if it exists. */ |
| 667 | if (!(ssl->initial_ctx->session_cache_mode & |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 668 | SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) { |
| 669 | SSL_SESSION data; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 670 | data.ssl_version = ssl->version; |
| 671 | data.session_id_length = session_id_len; |
| 672 | memcpy(data.session_id, session_id, session_id_len); |
| 673 | |
| 674 | CRYPTO_MUTEX_lock_read(&ssl->initial_ctx->lock); |
| 675 | session = lh_SSL_SESSION_retrieve(ssl->initial_ctx->sessions, &data); |
| 676 | if (session != NULL) { |
| 677 | SSL_SESSION_up_ref(session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 678 | } |
David Benjamin | 0f65395 | 2015-10-18 14:28:01 -0400 | [diff] [blame] | 679 | /* TODO(davidben): This should probably move it to the front of the list. */ |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 680 | CRYPTO_MUTEX_unlock_read(&ssl->initial_ctx->lock); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 681 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 682 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 683 | /* Fall back to the external cache, if it exists. */ |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 684 | if (session == NULL && |
| 685 | ssl->initial_ctx->get_session_cb != NULL) { |
| 686 | int copy = 1; |
| 687 | session = ssl->initial_ctx->get_session_cb(ssl, (uint8_t *)session_id, |
| 688 | session_id_len, ©); |
| 689 | |
Adam Langley | 81f4335 | 2016-08-26 09:22:56 -0700 | [diff] [blame] | 690 | if (session == NULL) { |
| 691 | return ssl_session_success; |
| 692 | } |
| 693 | |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 694 | if (session == SSL_magic_pending_session_ptr()) { |
| 695 | return ssl_session_retry; |
| 696 | } |
| 697 | |
| 698 | /* Increment reference count now if the session callback asks us to do so |
| 699 | * (note that if the session structures returned by the callback are shared |
| 700 | * between threads, it must handle the reference count itself [i.e. copy == |
| 701 | * 0], or things won't be thread-safe). */ |
| 702 | if (copy) { |
| 703 | SSL_SESSION_up_ref(session); |
| 704 | } |
| 705 | |
| 706 | /* Add the externally cached session to the internal cache if necessary. */ |
Adam Langley | 81f4335 | 2016-08-26 09:22:56 -0700 | [diff] [blame] | 707 | if (!(ssl->initial_ctx->session_cache_mode & |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 708 | SSL_SESS_CACHE_NO_INTERNAL_STORE)) { |
| 709 | SSL_CTX_add_session(ssl->initial_ctx, session); |
| 710 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 711 | } |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 712 | |
David Benjamin | 75f9914 | 2016-11-12 12:36:06 +0900 | [diff] [blame] | 713 | if (session != NULL && |
| 714 | !ssl_session_is_time_valid(ssl, session)) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 715 | /* The session was from the cache, so remove it. */ |
| 716 | SSL_CTX_remove_session(ssl->initial_ctx, session); |
| 717 | SSL_SESSION_free(session); |
| 718 | session = NULL; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | *out_session = session; |
| 722 | return ssl_session_success; |
| 723 | } |
| 724 | |
| 725 | enum ssl_session_result_t ssl_get_prev_session( |
David Benjamin | 75f9914 | 2016-11-12 12:36:06 +0900 | [diff] [blame] | 726 | SSL *ssl, SSL_SESSION **out_session, int *out_tickets_supported, |
| 727 | int *out_renew_ticket, const struct ssl_early_callback_ctx *ctx) { |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 728 | /* This is used only by servers. */ |
| 729 | assert(ssl->server); |
| 730 | SSL_SESSION *session = NULL; |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 731 | int renew_ticket = 0; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 732 | |
| 733 | /* If tickets are disabled, always behave as if no tickets are present. */ |
David Benjamin | 3e53633 | 2015-07-01 18:45:03 -0400 | [diff] [blame] | 734 | const uint8_t *ticket = NULL; |
| 735 | size_t ticket_len = 0; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 736 | const int tickets_supported = |
| 737 | !(SSL_get_options(ssl) & SSL_OP_NO_TICKET) && |
David Benjamin | c7ce977 | 2015-10-09 19:32:41 -0400 | [diff] [blame] | 738 | ssl->version > SSL3_VERSION && |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 739 | SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket, |
| 740 | &ticket, &ticket_len); |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 741 | if (tickets_supported && ticket_len > 0) { |
| 742 | if (!tls_process_ticket(ssl, &session, &renew_ticket, ticket, ticket_len, |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 743 | ctx->session_id, ctx->session_id_len)) { |
| 744 | return ssl_session_error; |
| 745 | } |
| 746 | } else { |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 747 | /* The client didn't send a ticket, so the session ID is a real ID. */ |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 748 | enum ssl_session_result_t lookup_ret = ssl_lookup_session( |
| 749 | ssl, &session, ctx->session_id, ctx->session_id_len); |
| 750 | if (lookup_ret != ssl_session_success) { |
| 751 | return lookup_ret; |
| 752 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 753 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 754 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 755 | *out_session = session; |
David Benjamin | 75f9914 | 2016-11-12 12:36:06 +0900 | [diff] [blame] | 756 | *out_tickets_supported = tickets_supported; |
| 757 | *out_renew_ticket = renew_ticket; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 758 | return ssl_session_success; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 759 | } |
| 760 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 761 | int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *session) { |
David Benjamin | 415660b | 2015-10-18 15:08:57 -0400 | [diff] [blame] | 762 | /* Although |session| is inserted into two structures (a doubly-linked list |
| 763 | * and the hash table), |ctx| only takes one reference. */ |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 764 | SSL_SESSION_up_ref(session); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 765 | |
David Benjamin | 415660b | 2015-10-18 15:08:57 -0400 | [diff] [blame] | 766 | SSL_SESSION *old_session; |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 767 | CRYPTO_MUTEX_lock_write(&ctx->lock); |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 768 | if (!lh_SSL_SESSION_insert(ctx->sessions, &old_session, session)) { |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 769 | CRYPTO_MUTEX_unlock_write(&ctx->lock); |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 770 | SSL_SESSION_free(session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 771 | return 0; |
| 772 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 773 | |
David Benjamin | 415660b | 2015-10-18 15:08:57 -0400 | [diff] [blame] | 774 | if (old_session != NULL) { |
| 775 | if (old_session == session) { |
| 776 | /* |session| was already in the cache. */ |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 777 | CRYPTO_MUTEX_unlock_write(&ctx->lock); |
David Benjamin | 415660b | 2015-10-18 15:08:57 -0400 | [diff] [blame] | 778 | SSL_SESSION_free(old_session); |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | /* There was a session ID collision. |old_session| must be removed from |
| 783 | * the linked list and released. */ |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 784 | SSL_SESSION_list_remove(ctx, old_session); |
| 785 | SSL_SESSION_free(old_session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 786 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 787 | |
David Benjamin | 415660b | 2015-10-18 15:08:57 -0400 | [diff] [blame] | 788 | SSL_SESSION_list_add(ctx, session); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 789 | |
David Benjamin | 415660b | 2015-10-18 15:08:57 -0400 | [diff] [blame] | 790 | /* Enforce any cache size limits. */ |
| 791 | if (SSL_CTX_sess_get_cache_size(ctx) > 0) { |
| 792 | while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) { |
| 793 | if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) { |
| 794 | break; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 798 | |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 799 | CRYPTO_MUTEX_unlock_write(&ctx->lock); |
David Benjamin | 415660b | 2015-10-18 15:08:57 -0400 | [diff] [blame] | 800 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 801 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 802 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 803 | int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *session) { |
| 804 | return remove_session_lock(ctx, session, 1); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 805 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 806 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 807 | static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 808 | int ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 809 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 810 | if (session != NULL && session->session_id_length != 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 811 | if (lock) { |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 812 | CRYPTO_MUTEX_lock_write(&ctx->lock); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 813 | } |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 814 | SSL_SESSION *found_session = lh_SSL_SESSION_retrieve(ctx->sessions, |
| 815 | session); |
| 816 | if (found_session == session) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 817 | ret = 1; |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 818 | found_session = lh_SSL_SESSION_delete(ctx->sessions, session); |
| 819 | SSL_SESSION_list_remove(ctx, session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 820 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 821 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 822 | if (lock) { |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 823 | CRYPTO_MUTEX_unlock_write(&ctx->lock); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 824 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 825 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 826 | if (ret) { |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 827 | found_session->not_resumable = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 828 | if (ctx->remove_session_cb != NULL) { |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 829 | ctx->remove_session_cb(ctx, found_session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 830 | } |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 831 | SSL_SESSION_free(found_session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 832 | } |
| 833 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 834 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 835 | return ret; |
| 836 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 837 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 838 | int SSL_set_session(SSL *ssl, SSL_SESSION *session) { |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 839 | /* SSL_set_session may only be called before the handshake has started. */ |
| 840 | if (ssl->state != SSL_ST_INIT || ssl->s3->initial_handshake_complete) { |
| 841 | abort(); |
| 842 | } |
| 843 | |
| 844 | ssl_set_session(ssl, session); |
| 845 | return 1; |
| 846 | } |
| 847 | |
| 848 | void ssl_set_session(SSL *ssl, SSL_SESSION *session) { |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 849 | if (ssl->session == session) { |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 850 | return; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 851 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 852 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 853 | SSL_SESSION_free(ssl->session); |
| 854 | ssl->session = session; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 855 | if (session != NULL) { |
David Benjamin | 3363984 | 2015-02-09 03:34:47 -0500 | [diff] [blame] | 856 | SSL_SESSION_up_ref(session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 857 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 858 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 859 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 860 | long SSL_CTX_set_timeout(SSL_CTX *ctx, long timeout) { |
| 861 | if (ctx == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 862 | return 0; |
| 863 | } |
| 864 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 865 | long old_timeout = ctx->session_timeout; |
| 866 | ctx->session_timeout = timeout; |
| 867 | return old_timeout; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 868 | } |
| 869 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 870 | long SSL_CTX_get_timeout(const SSL_CTX *ctx) { |
| 871 | if (ctx == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 872 | return 0; |
| 873 | } |
| 874 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 875 | return ctx->session_timeout; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | typedef struct timeout_param_st { |
| 879 | SSL_CTX *ctx; |
| 880 | long time; |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 881 | LHASH_OF(SSL_SESSION) *cache; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 882 | } TIMEOUT_PARAM; |
| 883 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 884 | static void timeout_doall_arg(SSL_SESSION *session, void *void_param) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 885 | TIMEOUT_PARAM *param = void_param; |
| 886 | |
| 887 | if (param->time == 0 || |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 888 | param->time > (session->time + session->timeout)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 889 | /* timeout */ |
| 890 | /* The reason we don't call SSL_CTX_remove_session() is to |
| 891 | * save on locking overhead */ |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 892 | (void) lh_SSL_SESSION_delete(param->cache, session); |
| 893 | SSL_SESSION_list_remove(param->ctx, session); |
| 894 | session->not_resumable = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 895 | if (param->ctx->remove_session_cb != NULL) { |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 896 | param->ctx->remove_session_cb(param->ctx, session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 897 | } |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 898 | SSL_SESSION_free(session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 902 | void SSL_CTX_flush_sessions(SSL_CTX *ctx, long time) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 903 | TIMEOUT_PARAM tp; |
| 904 | |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 905 | tp.ctx = ctx; |
| 906 | tp.cache = ctx->sessions; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 907 | if (tp.cache == NULL) { |
| 908 | return; |
| 909 | } |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 910 | tp.time = time; |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 911 | CRYPTO_MUTEX_lock_write(&ctx->lock); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 912 | lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp); |
David Benjamin | 29270de | 2016-05-24 15:28:36 +0000 | [diff] [blame] | 913 | CRYPTO_MUTEX_unlock_write(&ctx->lock); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 914 | } |
| 915 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 916 | /* locked by SSL_CTX in the calling function */ |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 917 | static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session) { |
| 918 | if (session->next == NULL || session->prev == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 919 | return; |
| 920 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 921 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 922 | if (session->next == (SSL_SESSION *)&ctx->session_cache_tail) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 923 | /* last element in list */ |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 924 | if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 925 | /* only one element in list */ |
| 926 | ctx->session_cache_head = NULL; |
| 927 | ctx->session_cache_tail = NULL; |
| 928 | } else { |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 929 | ctx->session_cache_tail = session->prev; |
| 930 | session->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 931 | } |
| 932 | } else { |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 933 | if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 934 | /* first element in list */ |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 935 | ctx->session_cache_head = session->next; |
| 936 | session->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 937 | } else { /* middle of list */ |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 938 | session->next->prev = session->prev; |
| 939 | session->prev->next = session->next; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 940 | } |
| 941 | } |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 942 | session->prev = session->next = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 943 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 944 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 945 | static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session) { |
| 946 | if (session->next != NULL && session->prev != NULL) { |
| 947 | SSL_SESSION_list_remove(ctx, session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 948 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 949 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 950 | if (ctx->session_cache_head == NULL) { |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 951 | ctx->session_cache_head = session; |
| 952 | ctx->session_cache_tail = session; |
| 953 | session->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
| 954 | session->next = (SSL_SESSION *)&(ctx->session_cache_tail); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 955 | } else { |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 956 | session->next = ctx->session_cache_head; |
| 957 | session->next->prev = session; |
| 958 | session->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
| 959 | ctx->session_cache_head = session; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 960 | } |
| 961 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 962 | |
| 963 | void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 964 | int (*cb)(SSL *ssl, SSL_SESSION *session)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 965 | ctx->new_session_cb = cb; |
| 966 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 967 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 968 | 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] | 969 | return ctx->new_session_cb; |
| 970 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 971 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 972 | void SSL_CTX_sess_set_remove_cb( |
| 973 | SSL_CTX *ctx, void (*cb)(SSL_CTX *ctx, SSL_SESSION *session)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 974 | ctx->remove_session_cb = cb; |
| 975 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 976 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 977 | 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] | 978 | SSL_SESSION *session) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 979 | return ctx->remove_session_cb; |
| 980 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 981 | |
| 982 | void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 983 | SSL_SESSION *(*cb)(SSL *ssl, |
| 984 | uint8_t *id, int id_len, |
| 985 | int *out_copy)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 986 | ctx->get_session_cb = cb; |
| 987 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 988 | |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 989 | SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))( |
| 990 | SSL *ssl, uint8_t *id, int id_len, int *out_copy) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 991 | return ctx->get_session_cb; |
| 992 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 993 | |
David Benjamin | 8217024 | 2015-10-17 22:51:17 -0400 | [diff] [blame] | 994 | void SSL_CTX_set_info_callback( |
| 995 | SSL_CTX *ctx, void (*cb)(const SSL *ssl, int type, int value)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 996 | ctx->info_callback = cb; |
| 997 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 998 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 999 | 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] | 1000 | int value) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1001 | return ctx->info_callback; |
| 1002 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1003 | |
David Benjamin | fd8e69f | 2015-10-03 10:49:20 -0400 | [diff] [blame] | 1004 | void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, |
| 1005 | X509 **out_x509, |
| 1006 | EVP_PKEY **out_pkey)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1007 | ctx->client_cert_cb = cb; |
| 1008 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1009 | |
David Benjamin | fd8e69f | 2015-10-03 10:49:20 -0400 | [diff] [blame] | 1010 | int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **out_x509, |
| 1011 | EVP_PKEY **out_pkey) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1012 | return ctx->client_cert_cb; |
| 1013 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1014 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1015 | void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1016 | void (*cb)(SSL *ssl, EVP_PKEY **pkey)) { |
| 1017 | ctx->channel_id_cb = cb; |
| 1018 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1019 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1020 | void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) { |
| 1021 | return ctx->channel_id_cb; |
| 1022 | } |