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 | |
| 136 | #include <stdio.h> |
| 137 | |
| 138 | #include <openssl/engine.h> |
| 139 | #include <openssl/err.h> |
| 140 | #include <openssl/lhash.h> |
| 141 | #include <openssl/mem.h> |
| 142 | #include <openssl/rand.h> |
| 143 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame^] | 144 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 145 | |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 146 | /* The address of this is a magic value, a pointer to which is returned by |
| 147 | * SSL_magic_pending_session_ptr(). It allows a session callback to indicate |
| 148 | * that it needs to asynchronously fetch session information. */ |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 149 | static const char g_pending_session_magic = 0; |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s); |
David Benjamin | b18f024 | 2015-03-10 18:30:08 -0400 | [diff] [blame] | 152 | static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck); |
| 154 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 155 | SSL_SESSION *SSL_magic_pending_session_ptr(void) { |
| 156 | return (SSL_SESSION *)&g_pending_session_magic; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 159 | SSL_SESSION *SSL_get_session(const SSL *ssl) |
| 160 | { |
| 161 | /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */ |
| 162 | return ssl->session; |
| 163 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 165 | SSL_SESSION *SSL_get1_session(SSL *ssl) { |
| 166 | /* variant of SSL_get_session: caller really gets something */ |
David Benjamin | 23a8ca1 | 2015-02-09 03:35:43 -0500 | [diff] [blame] | 167 | return SSL_SESSION_up_ref(ssl->session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 168 | } |
Adam Langley | 8eaaa86 | 2014-08-11 17:18:25 -0700 | [diff] [blame] | 169 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 170 | int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, |
| 171 | CRYPTO_EX_dup *dup_func, |
| 172 | CRYPTO_EX_free *free_func) { |
| 173 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp, |
| 174 | new_func, dup_func, free_func); |
| 175 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 176 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 177 | int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) { |
| 178 | return CRYPTO_set_ex_data(&s->ex_data, idx, arg); |
| 179 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 180 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 181 | void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) { |
| 182 | return CRYPTO_get_ex_data(&s->ex_data, idx); |
| 183 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 185 | SSL_SESSION *SSL_SESSION_new(void) { |
| 186 | SSL_SESSION *ss; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 187 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 188 | ss = (SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION)); |
| 189 | if (ss == NULL) { |
| 190 | OPENSSL_PUT_ERROR(SSL, SSL_SESSION_new, ERR_R_MALLOC_FAILURE); |
| 191 | return 0; |
| 192 | } |
| 193 | memset(ss, 0, sizeof(SSL_SESSION)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 194 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 195 | ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */ |
| 196 | ss->references = 1; |
| 197 | ss->timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
| 198 | ss->time = (unsigned long)time(NULL); |
| 199 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); |
| 200 | return ss; |
| 201 | } |
| 202 | |
| 203 | const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) { |
| 204 | if (len) { |
| 205 | *len = s->session_id_length; |
| 206 | } |
| 207 | return s->session_id; |
| 208 | } |
| 209 | |
| 210 | /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. |
| 211 | * SSLv3/TLSv1 has 32 bytes (256 bits). As such, filling the ID with random |
| 212 | * gunk repeatedly until we have no conflict is going to complete in one |
| 213 | * iteration pretty much "most" of the time (btw: understatement). So, if it |
| 214 | * takes us 10 iterations and we still can't avoid a conflict - well that's a |
| 215 | * reasonable point to call it quits. Either the RAND code is broken or someone |
| 216 | * is trying to open roughly very close to 2^128 (or 2^256) SSL sessions to our |
| 217 | * server. How you might store that many sessions is perhaps a more interesting |
| 218 | * question ... */ |
| 219 | static int def_generate_session_id(const SSL *ssl, uint8_t *id, |
| 220 | unsigned int *id_len) { |
| 221 | static const unsigned kMaxAttempts = 10; |
| 222 | unsigned int retry = 0; |
| 223 | do { |
| 224 | if (!RAND_bytes(id, *id_len)) { |
| 225 | return 0; |
| 226 | } |
| 227 | } while (SSL_has_matching_session_id(ssl, id, *id_len) && |
| 228 | (++retry < kMaxAttempts)); |
| 229 | |
| 230 | if (retry < kMaxAttempts) { |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | /* else - woops a session_id match */ |
| 235 | /* XXX We should also check the external cache -- but the probability of a |
| 236 | * collision is negligible, and we could not prevent the concurrent creation |
| 237 | * of sessions with identical IDs since we currently don't have means to |
| 238 | * atomically check whether a session ID already exists and make a |
| 239 | * reservation for it if it does not (this problem applies to the internal |
| 240 | * cache as well). */ |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | int ssl_get_new_session(SSL *s, int session) { |
| 245 | /* This gets used by clients and servers. */ |
| 246 | |
| 247 | unsigned int tmp; |
| 248 | SSL_SESSION *ss = NULL; |
| 249 | GEN_SESSION_CB cb = def_generate_session_id; |
| 250 | |
| 251 | if (s->mode & SSL_MODE_NO_SESSION_CREATION) { |
| 252 | OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, |
| 253 | SSL_R_SESSION_MAY_NOT_BE_CREATED); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | ss = SSL_SESSION_new(); |
| 258 | if (ss == NULL) { |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* If the context has a default timeout, use it over the default. */ |
| 263 | if (s->initial_ctx->session_timeout != 0) { |
| 264 | ss->timeout = s->initial_ctx->session_timeout; |
| 265 | } |
| 266 | |
| 267 | if (s->session != NULL) { |
| 268 | SSL_SESSION_free(s->session); |
| 269 | s->session = NULL; |
| 270 | } |
| 271 | |
| 272 | if (session) { |
| 273 | if (s->version == SSL3_VERSION || s->version == TLS1_VERSION || |
| 274 | s->version == TLS1_1_VERSION || s->version == TLS1_2_VERSION || |
| 275 | s->version == DTLS1_VERSION || s->version == DTLS1_2_VERSION) { |
| 276 | ss->ssl_version = s->version; |
| 277 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; |
| 278 | } else { |
| 279 | OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, |
| 280 | SSL_R_UNSUPPORTED_SSL_VERSION); |
| 281 | SSL_SESSION_free(ss); |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /* If RFC4507 ticket use empty session ID */ |
| 286 | if (s->tlsext_ticket_expected) { |
| 287 | ss->session_id_length = 0; |
| 288 | goto sess_id_done; |
| 289 | } |
| 290 | |
| 291 | /* Choose which callback will set the session ID */ |
| 292 | CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); |
| 293 | if (s->generate_session_id) { |
| 294 | cb = s->generate_session_id; |
| 295 | } else if (s->initial_ctx->generate_session_id) { |
| 296 | cb = s->initial_ctx->generate_session_id; |
| 297 | } |
| 298 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); |
| 299 | |
| 300 | /* Choose a session ID */ |
| 301 | tmp = ss->session_id_length; |
| 302 | if (!cb(s, ss->session_id, &tmp)) { |
| 303 | /* The callback failed */ |
| 304 | OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, |
| 305 | SSL_R_SSL_SESSION_ID_CALLBACK_FAILED); |
| 306 | SSL_SESSION_free(ss); |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | /* Don't allow the callback to set the session length to zero. nor set it |
| 311 | * higher than it was. */ |
| 312 | if (!tmp || tmp > ss->session_id_length) { |
| 313 | /* The callback set an illegal length */ |
| 314 | OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, |
| 315 | SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH); |
| 316 | SSL_SESSION_free(ss); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | ss->session_id_length = tmp; |
| 321 | /* Finally, check for a conflict */ |
| 322 | if (SSL_has_matching_session_id(s, ss->session_id, ss->session_id_length)) { |
| 323 | OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, |
| 324 | SSL_R_SSL_SESSION_ID_CONFLICT); |
| 325 | SSL_SESSION_free(ss); |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | sess_id_done: |
| 330 | if (s->tlsext_hostname) { |
| 331 | ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname); |
| 332 | if (ss->tlsext_hostname == NULL) { |
| 333 | OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR); |
| 334 | SSL_SESSION_free(ss); |
| 335 | return 0; |
| 336 | } |
| 337 | } |
| 338 | } else { |
| 339 | ss->session_id_length = 0; |
| 340 | } |
| 341 | |
| 342 | if (s->sid_ctx_length > sizeof(ss->sid_ctx)) { |
| 343 | OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR); |
| 344 | SSL_SESSION_free(ss); |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length); |
| 349 | ss->sid_ctx_length = s->sid_ctx_length; |
| 350 | s->session = ss; |
| 351 | ss->ssl_version = s->version; |
| 352 | ss->verify_result = X509_V_OK; |
| 353 | |
| 354 | return 1; |
| 355 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 356 | |
| 357 | /* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this |
| 358 | * connection. It is only called by servers. |
| 359 | * |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 360 | * ctx: contains the early callback context, which is the result of a |
| 361 | * shallow parse of the ClientHello. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 362 | * |
| 363 | * Returns: |
| 364 | * -1: error |
| 365 | * 0: a session may have been found. |
| 366 | * |
| 367 | * Side effects: |
| 368 | * - If a session is found then s->session is pointed at it (after freeing an |
| 369 | * existing session if need be) and s->verify_result is set from the session. |
| 370 | * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1 |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 371 | * if the server should issue a new session ticket (to 0 otherwise). */ |
| 372 | int ssl_get_prev_session(SSL *s, const struct ssl_early_callback_ctx *ctx) { |
| 373 | /* This is used only by servers. */ |
| 374 | SSL_SESSION *ret = NULL; |
| 375 | int fatal = 0; |
| 376 | int try_session_cache = 1; |
| 377 | int r; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 378 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 379 | if (ctx->session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 380 | goto err; |
| 381 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 382 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 383 | if (ctx->session_id_len == 0) { |
| 384 | try_session_cache = 0; |
| 385 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 386 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 387 | r = tls1_process_ticket(s, ctx, &ret); /* sets s->tlsext_ticket_expected */ |
| 388 | switch (r) { |
| 389 | case -1: /* Error during processing */ |
| 390 | fatal = 1; |
| 391 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 393 | case 0: /* No ticket found */ |
| 394 | case 1: /* Zero length ticket found */ |
| 395 | break; /* Ok to carry on processing session id. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 396 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 397 | case 2: /* Ticket found but not decrypted. */ |
| 398 | case 3: /* Ticket decrypted, *ret has been set. */ |
| 399 | try_session_cache = 0; |
| 400 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 401 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 402 | default: |
| 403 | abort(); |
| 404 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 406 | if (try_session_cache && ret == NULL && |
| 407 | !(s->initial_ctx->session_cache_mode & |
| 408 | SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) { |
| 409 | SSL_SESSION data; |
| 410 | data.ssl_version = s->version; |
| 411 | data.session_id_length = ctx->session_id_len; |
| 412 | if (ctx->session_id_len == 0) { |
| 413 | return 0; |
| 414 | } |
| 415 | memcpy(data.session_id, ctx->session_id, ctx->session_id_len); |
| 416 | CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); |
David Benjamin | 3363984 | 2015-02-09 03:34:47 -0500 | [diff] [blame] | 417 | ret = SSL_SESSION_up_ref(lh_SSL_SESSION_retrieve(s->initial_ctx->sessions, |
| 418 | &data)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 419 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 420 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 421 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 422 | if (try_session_cache && ret == NULL && |
| 423 | s->initial_ctx->get_session_cb != NULL) { |
| 424 | int copy = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 426 | ret = s->initial_ctx->get_session_cb(s, (uint8_t *)ctx->session_id, |
| 427 | ctx->session_id_len, ©); |
| 428 | if (ret != NULL) { |
| 429 | if (ret == SSL_magic_pending_session_ptr()) { |
| 430 | /* This is a magic value which indicates that the callback needs to |
| 431 | * unwind the stack and figure out the session asynchronously. */ |
| 432 | return PENDING_SESSION; |
| 433 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 434 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 435 | /* Increment reference count now if the session callback asks us to do so |
| 436 | * (note that if the session structures returned by the callback are |
| 437 | * shared between threads, it must handle the reference count itself |
| 438 | * [i.e. copy == 0], or things won't be thread-safe). */ |
| 439 | if (copy) { |
David Benjamin | 3363984 | 2015-02-09 03:34:47 -0500 | [diff] [blame] | 440 | SSL_SESSION_up_ref(ret); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 441 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 442 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 443 | /* Add the externally cached session to the internal cache as well if and |
| 444 | * only if we are supposed to. */ |
| 445 | if (!(s->initial_ctx->session_cache_mode & |
| 446 | SSL_SESS_CACHE_NO_INTERNAL_STORE)) { |
| 447 | /* The following should not return 1, otherwise, things are very |
| 448 | * strange */ |
| 449 | SSL_CTX_add_session(s->initial_ctx, ret); |
| 450 | } |
| 451 | } |
| 452 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 453 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 454 | if (ret == NULL) { |
| 455 | goto err; |
| 456 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 457 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 458 | /* Now ret is non-NULL and we own one of its reference counts. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 459 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 460 | if (ret->sid_ctx_length != s->sid_ctx_length || |
| 461 | memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) { |
| 462 | /* We have the session requested by the client, but we don't want to use it |
| 463 | * in this context. */ |
| 464 | goto err; /* treat like cache miss */ |
| 465 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 467 | if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) { |
| 468 | /* We can't be sure if this session is being used out of context, which is |
| 469 | * especially important for SSL_VERIFY_PEER. The application should have |
| 470 | * used SSL[_CTX]_set_session_id_context. |
| 471 | * |
| 472 | * For this error case, we generate an error instead of treating the event |
| 473 | * like a cache miss (otherwise it would be easy for applications to |
| 474 | * effectively disable the session cache by accident without anyone |
| 475 | * noticing). */ |
| 476 | OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session, |
| 477 | SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); |
| 478 | fatal = 1; |
| 479 | goto err; |
| 480 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 481 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 482 | if (ret->timeout < (long)(time(NULL) - ret->time)) { |
| 483 | /* timeout */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 484 | if (try_session_cache) { |
| 485 | /* session was from the cache, so remove it */ |
| 486 | SSL_CTX_remove_session(s->initial_ctx, ret); |
| 487 | } |
| 488 | goto err; |
| 489 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 490 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 491 | if (s->session != NULL) { |
| 492 | SSL_SESSION_free(s->session); |
| 493 | } |
| 494 | s->session = ret; |
| 495 | s->verify_result = s->session->verify_result; |
| 496 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 497 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 498 | err: |
| 499 | if (ret != NULL) { |
| 500 | SSL_SESSION_free(ret); |
| 501 | if (!try_session_cache) { |
| 502 | /* The session was from a ticket, so we should |
| 503 | * issue a ticket for the new session */ |
| 504 | s->tlsext_ticket_expected = 1; |
| 505 | } |
| 506 | } |
| 507 | if (fatal) { |
| 508 | return -1; |
| 509 | } |
| 510 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 513 | int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) { |
| 514 | int ret = 0; |
| 515 | SSL_SESSION *s; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 516 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 517 | /* add just 1 reference count for the SSL_CTX's session cache even though it |
| 518 | * has two ways of access: each session is in a doubly linked list and an |
| 519 | * lhash */ |
David Benjamin | 3363984 | 2015-02-09 03:34:47 -0500 | [diff] [blame] | 520 | SSL_SESSION_up_ref(c); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 521 | /* if session c is in already in cache, we take back the increment later */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 522 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 523 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); |
| 524 | if (!lh_SSL_SESSION_insert(ctx->sessions, &s, c)) { |
| 525 | return 0; |
| 526 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 527 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 528 | /* s != NULL iff we already had a session with the given PID. In this case, s |
| 529 | * == c should hold (then we did not really modify ctx->sessions), or we're |
| 530 | * in trouble. */ |
| 531 | if (s != NULL && s != c) { |
| 532 | /* We *are* in trouble ... */ |
| 533 | SSL_SESSION_list_remove(ctx, s); |
| 534 | SSL_SESSION_free(s); |
| 535 | /* ... so pretend the other session did not exist in cache (we cannot |
| 536 | * handle two SSL_SESSION structures with identical session ID in the same |
| 537 | * cache, which could happen e.g. when two threads concurrently obtain the |
| 538 | * same session from an external cache) */ |
| 539 | s = NULL; |
| 540 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 541 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 542 | /* Put at the head of the queue unless it is already in the cache */ |
| 543 | if (s == NULL) { |
| 544 | SSL_SESSION_list_add(ctx, c); |
| 545 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 546 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 547 | if (s != NULL) { |
| 548 | /* existing cache entry -- decrement previously incremented reference count |
| 549 | * because it already takes into account the cache */ |
| 550 | SSL_SESSION_free(s); /* s == c */ |
| 551 | ret = 0; |
| 552 | } else { |
| 553 | /* new cache entry -- remove old ones if cache has become too large */ |
| 554 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 555 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 556 | if (SSL_CTX_sess_get_cache_size(ctx) > 0) { |
| 557 | while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) { |
| 558 | if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) { |
| 559 | break; |
| 560 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 564 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 565 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); |
| 566 | return ret; |
| 567 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 568 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 569 | int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) { |
| 570 | return remove_session_lock(ctx, c, 1); |
| 571 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 572 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 573 | static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lock) { |
| 574 | SSL_SESSION *r; |
| 575 | int ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 576 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 577 | if (c != NULL && c->session_id_length != 0) { |
| 578 | if (lock) { |
| 579 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); |
| 580 | } |
| 581 | r = lh_SSL_SESSION_retrieve(ctx->sessions, c); |
| 582 | if (r == c) { |
| 583 | ret = 1; |
| 584 | r = lh_SSL_SESSION_delete(ctx->sessions, c); |
| 585 | SSL_SESSION_list_remove(ctx, c); |
| 586 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 587 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 588 | if (lock) { |
| 589 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); |
| 590 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 591 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 592 | if (ret) { |
| 593 | r->not_resumable = 1; |
| 594 | if (ctx->remove_session_cb != NULL) { |
| 595 | ctx->remove_session_cb(ctx, r); |
| 596 | } |
| 597 | SSL_SESSION_free(r); |
| 598 | } |
| 599 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 600 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 601 | return ret; |
| 602 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 603 | |
David Benjamin | 3363984 | 2015-02-09 03:34:47 -0500 | [diff] [blame] | 604 | SSL_SESSION *SSL_SESSION_up_ref(SSL_SESSION *session) { |
| 605 | if (session) { |
| 606 | CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION); |
| 607 | } |
| 608 | return session; |
| 609 | } |
| 610 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 611 | void SSL_SESSION_free(SSL_SESSION *ss) { |
| 612 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 613 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 614 | if (ss == NULL) { |
| 615 | return; |
| 616 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 617 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 618 | i = CRYPTO_add(&ss->references, -1, CRYPTO_LOCK_SSL_SESSION); |
| 619 | if (i > 0) { |
| 620 | return; |
| 621 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 622 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 623 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 624 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 625 | OPENSSL_cleanse(ss->master_key, sizeof ss->master_key); |
| 626 | OPENSSL_cleanse(ss->session_id, sizeof ss->session_id); |
| 627 | if (ss->sess_cert != NULL) { |
| 628 | ssl_sess_cert_free(ss->sess_cert); |
| 629 | } |
| 630 | if (ss->peer != NULL) { |
| 631 | X509_free(ss->peer); |
| 632 | } |
| 633 | if (ss->tlsext_hostname != NULL) { |
| 634 | OPENSSL_free(ss->tlsext_hostname); |
| 635 | } |
| 636 | if (ss->tlsext_tick != NULL) { |
| 637 | OPENSSL_free(ss->tlsext_tick); |
| 638 | } |
| 639 | if (ss->tlsext_signed_cert_timestamp_list != NULL) { |
| 640 | OPENSSL_free(ss->tlsext_signed_cert_timestamp_list); |
| 641 | } |
| 642 | if (ss->ocsp_response != NULL) { |
| 643 | OPENSSL_free(ss->ocsp_response); |
| 644 | } |
| 645 | if (ss->psk_identity != NULL) { |
| 646 | OPENSSL_free(ss->psk_identity); |
| 647 | } |
| 648 | OPENSSL_cleanse(ss, sizeof(*ss)); |
| 649 | OPENSSL_free(ss); |
| 650 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 651 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 652 | int SSL_set_session(SSL *s, SSL_SESSION *session) { |
| 653 | if (s->session == session) { |
| 654 | return 1; |
| 655 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 656 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 657 | if (s->session != NULL) { |
| 658 | SSL_SESSION_free(s->session); |
| 659 | } |
| 660 | s->session = session; |
| 661 | if (session != NULL) { |
David Benjamin | 3363984 | 2015-02-09 03:34:47 -0500 | [diff] [blame] | 662 | SSL_SESSION_up_ref(session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 663 | s->verify_result = session->verify_result; |
| 664 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 665 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 666 | return 1; |
| 667 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 668 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 669 | long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) { |
| 670 | if (s == NULL) { |
| 671 | return 0; |
| 672 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 673 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 674 | s->timeout = t; |
| 675 | return 1; |
| 676 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 677 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 678 | long SSL_SESSION_get_timeout(const SSL_SESSION *s) { |
| 679 | if (s == NULL) { |
| 680 | return 0; |
| 681 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 682 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 683 | return s->timeout; |
| 684 | } |
| 685 | |
| 686 | long SSL_SESSION_get_time(const SSL_SESSION *s) { |
| 687 | if (s == NULL) { |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | return s->time; |
| 692 | } |
| 693 | |
| 694 | long SSL_SESSION_set_time(SSL_SESSION *s, long t) { |
| 695 | if (s == NULL) { |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | s->time = t; |
| 700 | return t; |
| 701 | } |
| 702 | |
| 703 | X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) { return s->peer; } |
| 704 | |
| 705 | int SSL_SESSION_set1_id_context(SSL_SESSION *s, const uint8_t *sid_ctx, |
| 706 | unsigned int sid_ctx_len) { |
| 707 | if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { |
| 708 | OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context, |
| 709 | SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | s->sid_ctx_length = sid_ctx_len; |
| 714 | memcpy(s->sid_ctx, sid_ctx, sid_ctx_len); |
| 715 | |
| 716 | return 1; |
| 717 | } |
| 718 | |
| 719 | long SSL_CTX_set_timeout(SSL_CTX *s, long t) { |
| 720 | long l; |
| 721 | if (s == NULL) { |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | l = s->session_timeout; |
| 726 | s->session_timeout = t; |
| 727 | return l; |
| 728 | } |
| 729 | |
| 730 | long SSL_CTX_get_timeout(const SSL_CTX *s) { |
| 731 | if (s == NULL) { |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | return s->session_timeout; |
| 736 | } |
| 737 | |
| 738 | typedef struct timeout_param_st { |
| 739 | SSL_CTX *ctx; |
| 740 | long time; |
| 741 | LHASH_OF(SSL_SESSION) * cache; |
| 742 | } TIMEOUT_PARAM; |
| 743 | |
| 744 | static void timeout_doall_arg(SSL_SESSION *sess, void *void_param) { |
| 745 | TIMEOUT_PARAM *param = void_param; |
| 746 | |
| 747 | if (param->time == 0 || |
| 748 | param->time > (sess->time + sess->timeout)) { |
| 749 | /* timeout */ |
| 750 | /* The reason we don't call SSL_CTX_remove_session() is to |
| 751 | * save on locking overhead */ |
Adam Langley | a307dfd | 2015-01-09 15:42:58 -0800 | [diff] [blame] | 752 | (void) lh_SSL_SESSION_delete(param->cache, sess); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 753 | SSL_SESSION_list_remove(param->ctx, sess); |
| 754 | sess->not_resumable = 1; |
| 755 | if (param->ctx->remove_session_cb != NULL) { |
| 756 | param->ctx->remove_session_cb(param->ctx, sess); |
| 757 | } |
| 758 | SSL_SESSION_free(sess); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | void SSL_CTX_flush_sessions(SSL_CTX *s, long t) { |
| 763 | TIMEOUT_PARAM tp; |
| 764 | |
| 765 | tp.ctx = s; |
| 766 | tp.cache = s->sessions; |
| 767 | if (tp.cache == NULL) { |
| 768 | return; |
| 769 | } |
| 770 | tp.time = t; |
| 771 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); |
| 772 | lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp); |
| 773 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); |
| 774 | } |
| 775 | |
| 776 | int ssl_clear_bad_session(SSL *s) { |
| 777 | if (s->session != NULL && !(s->shutdown & SSL_SENT_SHUTDOWN) && |
| 778 | !SSL_in_init(s)) { |
| 779 | SSL_CTX_remove_session(s->ctx, s->session); |
| 780 | return 1; |
| 781 | } |
| 782 | |
| 783 | return 0; |
| 784 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 785 | |
| 786 | /* locked by SSL_CTX in the calling function */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 787 | static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) { |
| 788 | if (s->next == NULL || s->prev == NULL) { |
| 789 | return; |
| 790 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 791 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 792 | if (s->next == (SSL_SESSION *)&ctx->session_cache_tail) { |
| 793 | /* last element in list */ |
| 794 | if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) { |
| 795 | /* only one element in list */ |
| 796 | ctx->session_cache_head = NULL; |
| 797 | ctx->session_cache_tail = NULL; |
| 798 | } else { |
| 799 | ctx->session_cache_tail = s->prev; |
| 800 | s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail); |
| 801 | } |
| 802 | } else { |
| 803 | if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) { |
| 804 | /* first element in list */ |
| 805 | ctx->session_cache_head = s->next; |
| 806 | s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
| 807 | } else { /* middle of list */ |
| 808 | s->next->prev = s->prev; |
| 809 | s->prev->next = s->next; |
| 810 | } |
| 811 | } |
| 812 | s->prev = s->next = NULL; |
| 813 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 814 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 815 | static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) { |
| 816 | if (s->next != NULL && s->prev != NULL) { |
| 817 | SSL_SESSION_list_remove(ctx, s); |
| 818 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 819 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 820 | if (ctx->session_cache_head == NULL) { |
| 821 | ctx->session_cache_head = s; |
| 822 | ctx->session_cache_tail = s; |
| 823 | s->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
| 824 | s->next = (SSL_SESSION *)&(ctx->session_cache_tail); |
| 825 | } else { |
| 826 | s->next = ctx->session_cache_head; |
| 827 | s->next->prev = s; |
| 828 | s->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
| 829 | ctx->session_cache_head = s; |
| 830 | } |
| 831 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 832 | |
| 833 | void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 834 | int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) { |
| 835 | ctx->new_session_cb = cb; |
| 836 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 837 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 838 | int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess) { |
| 839 | return ctx->new_session_cb; |
| 840 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 841 | |
| 842 | void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 843 | void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess)) { |
| 844 | ctx->remove_session_cb = cb; |
| 845 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 846 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 847 | void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx, |
| 848 | SSL_SESSION *sess) { |
| 849 | return ctx->remove_session_cb; |
| 850 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 851 | |
| 852 | void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 853 | SSL_SESSION *(*cb)(struct ssl_st *ssl, |
| 854 | uint8_t *data, int len, |
| 855 | int *copy)) { |
| 856 | ctx->get_session_cb = cb; |
| 857 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 858 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 859 | SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, uint8_t *data, |
| 860 | int len, int *copy) { |
| 861 | return ctx->get_session_cb; |
| 862 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 863 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 864 | void SSL_CTX_set_info_callback(SSL_CTX *ctx, |
| 865 | void (*cb)(const SSL *ssl, int type, int val)) { |
| 866 | ctx->info_callback = cb; |
| 867 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 868 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 869 | void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type, |
| 870 | int val) { |
| 871 | return ctx->info_callback; |
| 872 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 873 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 874 | void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, X509 **x509, |
| 875 | EVP_PKEY **pkey)) { |
| 876 | ctx->client_cert_cb = cb; |
| 877 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 878 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 879 | int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509, |
| 880 | EVP_PKEY **pkey) { |
| 881 | return ctx->client_cert_cb; |
| 882 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 883 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 884 | void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 885 | void (*cb)(SSL *ssl, EVP_PKEY **pkey)) { |
| 886 | ctx->channel_id_cb = cb; |
| 887 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 888 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 889 | void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) { |
| 890 | return ctx->channel_id_cb; |
| 891 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 892 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 893 | IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION) |