blob: 55534006d60e72288d13d71c81e2b991670187c2 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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 Benjamin9e4e01e2015-09-15 01:48:04 -0400136#include <openssl/ssl.h>
137
David Benjamine3aa1d92015-06-16 15:34:50 -0400138#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400139#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700140
Adam Langley95c29f32014-06-20 12:00:00 -0700141#include <openssl/err.h>
142#include <openssl/lhash.h>
143#include <openssl/mem.h>
144#include <openssl/rand.h>
145
David Benjamin2ee94aa2015-04-07 22:38:30 -0400146#include "internal.h"
David Benjamin546f1a52015-04-15 16:46:09 -0400147#include "../crypto/internal.h"
148
Adam Langley95c29f32014-06-20 12:00:00 -0700149
Adam Langleyb2ce0582014-06-20 12:00:00 -0700150/* The address of this is a magic value, a pointer to which is returned by
151 * SSL_magic_pending_session_ptr(). It allows a session callback to indicate
152 * that it needs to asynchronously fetch session information. */
Brian Smithefed2212015-01-28 16:20:02 -0800153static const char g_pending_session_magic = 0;
Adam Langleyb2ce0582014-06-20 12:00:00 -0700154
David Benjaminaa585132015-06-29 23:36:17 -0400155static CRYPTO_EX_DATA_CLASS g_ex_data_class =
156 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
David Benjamin9f33fc62015-04-15 17:29:53 -0400157
David Benjamindafbdd42015-09-14 01:40:10 -0400158static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session);
159static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session);
160static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700161
David Benjaminabb6c1c2015-09-13 14:46:26 -0400162SSL_SESSION *SSL_SESSION_new(void) {
Brian Smith5ba06892016-02-07 09:36:04 -1000163 SSL_SESSION *session = OPENSSL_malloc(sizeof(SSL_SESSION));
David Benjaminabb6c1c2015-09-13 14:46:26 -0400164 if (session == NULL) {
165 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
166 return 0;
167 }
168 memset(session, 0, sizeof(SSL_SESSION));
169
Adam Langley9498e742016-07-18 10:17:16 -0700170 session->verify_result = X509_V_ERR_INVALID_CALL;
David Benjaminabb6c1c2015-09-13 14:46:26 -0400171 session->references = 1;
172 session->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
173 session->time = (unsigned long)time(NULL);
David Benjamin8a589332015-12-04 23:14:35 -0500174 CRYPTO_new_ex_data(&session->ex_data);
David Benjaminabb6c1c2015-09-13 14:46:26 -0400175 return session;
176}
177
Steven Valdez87eab492016-06-27 16:34:59 -0400178SSL_SESSION *SSL_SESSION_dup(SSL_SESSION *session, int include_ticket) {
179 SSL_SESSION *new_session = SSL_SESSION_new();
180 if (new_session == NULL) {
181 goto err;
182 }
183
184 new_session->ssl_version = session->ssl_version;
185 new_session->key_exchange_info = session->key_exchange_info;
186 new_session->master_key_length = session->master_key_length;
187 memcpy(new_session->master_key, session->master_key,
188 session->master_key_length);
189 new_session->session_id_length = session->session_id_length;
190 memcpy(new_session->session_id, session->session_id,
191 session->session_id_length);
192 new_session->sid_ctx_length = session->sid_ctx_length;
193 memcpy(new_session->sid_ctx, session->sid_ctx, session->sid_ctx_length);
194 if (session->psk_identity != NULL) {
195 new_session->psk_identity = BUF_strdup(session->psk_identity);
196 if (new_session->psk_identity == NULL) {
197 goto err;
198 }
199 }
200 if (session->peer != NULL) {
201 new_session->peer = X509_up_ref(session->peer);
202 }
203 if (session->cert_chain != NULL) {
204 new_session->cert_chain = X509_chain_up_ref(session->cert_chain);
205 if (new_session->cert_chain == NULL) {
206 goto err;
207 }
208 }
209 new_session->verify_result = session->verify_result;
210 new_session->timeout = session->timeout;
211 new_session->time = session->time;
212 new_session->cipher = session->cipher;
213 /* The new_session does not get a copy of the ex_data. */
214 if (session->tlsext_hostname != NULL) {
215 new_session->tlsext_hostname = BUF_strdup(session->tlsext_hostname);
216 if (new_session->tlsext_hostname == NULL) {
217 goto err;
218 }
219 }
220 if (include_ticket) {
221 if (session->tlsext_tick != NULL) {
222 new_session->tlsext_tick = BUF_memdup(session->tlsext_tick,
223 session->tlsext_ticklen);
224 if (new_session->tlsext_tick == NULL) {
225 goto err;
226 }
227 }
228 new_session->tlsext_ticklen = session->tlsext_ticklen;
229 }
230
231 new_session->tlsext_signed_cert_timestamp_list_length =
232 session->tlsext_signed_cert_timestamp_list_length;
233 if (session->tlsext_signed_cert_timestamp_list != NULL) {
234 new_session->tlsext_signed_cert_timestamp_list =
235 BUF_memdup(session->tlsext_signed_cert_timestamp_list,
236 session->tlsext_signed_cert_timestamp_list_length);
237 if (new_session->tlsext_signed_cert_timestamp_list == NULL) {
238 goto err;
239 }
240 }
241 new_session->ocsp_response_length = session->ocsp_response_length;
242 if (session->ocsp_response != NULL) {
243 new_session->ocsp_response = BUF_memdup(session->ocsp_response,
244 session->ocsp_response_length);
245 if (new_session->ocsp_response == NULL) {
246 goto err;
247 }
248 }
249 memcpy(new_session->peer_sha256, session->peer_sha256, SHA256_DIGEST_LENGTH);
250 memcpy(new_session->original_handshake_hash,
251 session->original_handshake_hash,
252 session->original_handshake_hash_len);
253 new_session->original_handshake_hash_len =
254 session->original_handshake_hash_len;
255 new_session->tlsext_tick_lifetime_hint = session->tlsext_tick_lifetime_hint;
256 new_session->extended_master_secret = session->extended_master_secret;
257 new_session->peer_sha256_valid = session->peer_sha256_valid;
258 new_session->not_resumable = 1;
259 return new_session;
260
261err:
262 SSL_SESSION_free(new_session);
263 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
264 return 0;
265}
266
David Benjaminabb6c1c2015-09-13 14:46:26 -0400267SSL_SESSION *SSL_SESSION_up_ref(SSL_SESSION *session) {
268 if (session != NULL) {
269 CRYPTO_refcount_inc(&session->references);
270 }
271 return session;
272}
273
274void SSL_SESSION_free(SSL_SESSION *session) {
275 if (session == NULL ||
276 !CRYPTO_refcount_dec_and_test_zero(&session->references)) {
277 return;
278 }
279
280 CRYPTO_free_ex_data(&g_ex_data_class, session, &session->ex_data);
281
282 OPENSSL_cleanse(session->master_key, sizeof(session->master_key));
283 OPENSSL_cleanse(session->session_id, sizeof(session->session_id));
284 X509_free(session->peer);
285 sk_X509_pop_free(session->cert_chain, X509_free);
286 OPENSSL_free(session->tlsext_hostname);
287 OPENSSL_free(session->tlsext_tick);
288 OPENSSL_free(session->tlsext_signed_cert_timestamp_list);
289 OPENSSL_free(session->ocsp_response);
290 OPENSSL_free(session->psk_identity);
291 OPENSSL_cleanse(session, sizeof(*session));
292 OPENSSL_free(session);
293}
294
295const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *session,
296 unsigned *out_len) {
297 if (out_len != NULL) {
298 *out_len = session->session_id_length;
299 }
300 return session->session_id;
301}
302
303long SSL_SESSION_get_timeout(const SSL_SESSION *session) {
304 return session->timeout;
305}
306
307long SSL_SESSION_get_time(const SSL_SESSION *session) {
David Benjamin20c0e902015-10-01 16:13:22 -0400308 if (session == NULL) {
309 /* NULL should crash, but silently accept it here for compatibility. */
310 return 0;
311 }
David Benjaminabb6c1c2015-09-13 14:46:26 -0400312 return session->time;
313}
314
David Benjamin14e2b502015-09-13 14:48:12 -0400315uint32_t SSL_SESSION_get_key_exchange_info(const SSL_SESSION *session) {
David Benjaminabb6c1c2015-09-13 14:46:26 -0400316 return session->key_exchange_info;
317}
318
David Benjamin14e2b502015-09-13 14:48:12 -0400319X509 *SSL_SESSION_get0_peer(const SSL_SESSION *session) {
320 return session->peer;
321}
David Benjaminabb6c1c2015-09-13 14:46:26 -0400322
David Benjamin818aff02016-04-21 16:58:49 -0400323size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, uint8_t *out,
324 size_t max_out) {
325 /* TODO(davidben): Fix master_key_length's type and remove these casts. */
326 if (max_out == 0) {
327 return (size_t)session->master_key_length;
328 }
329 if (max_out > (size_t)session->master_key_length) {
330 max_out = (size_t)session->master_key_length;
331 }
332 memcpy(out, session->master_key, max_out);
333 return max_out;
334}
335
David Benjaminabb6c1c2015-09-13 14:46:26 -0400336long SSL_SESSION_set_time(SSL_SESSION *session, long time) {
337 if (session == NULL) {
338 return 0;
339 }
340
341 session->time = time;
342 return time;
343}
344
345long SSL_SESSION_set_timeout(SSL_SESSION *session, long timeout) {
346 if (session == NULL) {
347 return 0;
348 }
349
350 session->timeout = timeout;
351 return 1;
352}
353
354int SSL_SESSION_set1_id_context(SSL_SESSION *session, const uint8_t *sid_ctx,
355 unsigned sid_ctx_len) {
356 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
357 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
358 return 0;
359 }
360
361 session->sid_ctx_length = sid_ctx_len;
362 memcpy(session->sid_ctx, sid_ctx, sid_ctx_len);
363
364 return 1;
365}
366
Adam Langleyfcf25832014-12-18 17:42:32 -0800367SSL_SESSION *SSL_magic_pending_session_ptr(void) {
368 return (SSL_SESSION *)&g_pending_session_magic;
Adam Langley95c29f32014-06-20 12:00:00 -0700369}
370
Adam Langleyd4aae0f2016-08-01 12:29:38 -0700371SSL_SESSION *SSL_get_session(const SSL *ssl) {
Steven Valdez87eab492016-06-27 16:34:59 -0400372 /* Once the handshake completes we return the established session. Otherwise
373 * we return the intermediate session, either |session| (for resumption) or
374 * |new_session| if doing a full handshake. */
375 if (!SSL_in_init(ssl)) {
376 return ssl->s3->established_session;
377 }
378 if (ssl->s3->new_session != NULL) {
379 return ssl->s3->new_session;
380 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 return ssl->session;
382}
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langleyfcf25832014-12-18 17:42:32 -0800384SSL_SESSION *SSL_get1_session(SSL *ssl) {
385 /* variant of SSL_get_session: caller really gets something */
Steven Valdez87eab492016-06-27 16:34:59 -0400386 return SSL_SESSION_up_ref(SSL_get_session(ssl));
Adam Langleyfcf25832014-12-18 17:42:32 -0800387}
Adam Langley8eaaa862014-08-11 17:18:25 -0700388
David Benjamin8a589332015-12-04 23:14:35 -0500389int SSL_SESSION_get_ex_new_index(long argl, void *argp,
390 CRYPTO_EX_unused *unused,
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 CRYPTO_EX_dup *dup_func,
392 CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400393 int index;
David Benjamin8a589332015-12-04 23:14:35 -0500394 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
395 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400396 return -1;
397 }
398 return index;
Adam Langleyfcf25832014-12-18 17:42:32 -0800399}
Adam Langley95c29f32014-06-20 12:00:00 -0700400
David Benjamin190ab7f2015-09-13 14:20:58 -0400401int SSL_SESSION_set_ex_data(SSL_SESSION *session, int idx, void *arg) {
402 return CRYPTO_set_ex_data(&session->ex_data, idx, arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800403}
Adam Langley95c29f32014-06-20 12:00:00 -0700404
David Benjamin190ab7f2015-09-13 14:20:58 -0400405void *SSL_SESSION_get_ex_data(const SSL_SESSION *session, int idx) {
406 return CRYPTO_get_ex_data(&session->ex_data, idx);
Adam Langleyfcf25832014-12-18 17:42:32 -0800407}
Adam Langley95c29f32014-06-20 12:00:00 -0700408
David Benjamindc2aea22015-10-18 12:50:32 -0400409int ssl_get_new_session(SSL *ssl, int is_server) {
410 if (ssl->mode & SSL_MODE_NO_SESSION_CREATION) {
David Benjamin3570d732015-06-29 00:28:17 -0400411 OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_MAY_NOT_BE_CREATED);
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 return 0;
413 }
414
David Benjamindc2aea22015-10-18 12:50:32 -0400415 SSL_SESSION *session = SSL_SESSION_new();
416 if (session == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 return 0;
418 }
419
420 /* If the context has a default timeout, use it over the default. */
David Benjamindc2aea22015-10-18 12:50:32 -0400421 if (ssl->initial_ctx->session_timeout != 0) {
422 session->timeout = ssl->initial_ctx->session_timeout;
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 }
424
David Benjamindc2aea22015-10-18 12:50:32 -0400425 session->ssl_version = ssl->version;
Adam Langleyfcf25832014-12-18 17:42:32 -0800426
David Benjamindc2aea22015-10-18 12:50:32 -0400427 if (is_server) {
428 if (ssl->tlsext_ticket_expected) {
429 /* Don't set session IDs for sessions resumed with tickets. This will keep
430 * them out of the session cache. */
431 session->session_id_length = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800432 } else {
David Benjamindc2aea22015-10-18 12:50:32 -0400433 session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
434 if (!RAND_bytes(session->session_id, session->session_id_length)) {
435 goto err;
436 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800437 }
438
David Benjamindc2aea22015-10-18 12:50:32 -0400439 if (ssl->tlsext_hostname != NULL) {
440 session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
441 if (session->tlsext_hostname == NULL) {
442 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
443 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800444 }
445 }
446 } else {
David Benjamindc2aea22015-10-18 12:50:32 -0400447 session->session_id_length = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800448 }
449
David Benjamindc2aea22015-10-18 12:50:32 -0400450 if (ssl->sid_ctx_length > sizeof(session->sid_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400451 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamindc2aea22015-10-18 12:50:32 -0400452 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 }
David Benjamindc2aea22015-10-18 12:50:32 -0400454 memcpy(session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length);
455 session->sid_ctx_length = ssl->sid_ctx_length;
Adam Langleyfcf25832014-12-18 17:42:32 -0800456
Steven Valdez87eab492016-06-27 16:34:59 -0400457 /* The session is marked not resumable until it is completely filled in. */
458 session->not_resumable = 1;
Adam Langley9498e742016-07-18 10:17:16 -0700459 session->verify_result = X509_V_ERR_INVALID_CALL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800460
Steven Valdez87eab492016-06-27 16:34:59 -0400461 SSL_SESSION_free(ssl->s3->new_session);
462 ssl->s3->new_session = session;
463 SSL_set_session(ssl, NULL);
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 return 1;
David Benjamindc2aea22015-10-18 12:50:32 -0400465
466err:
467 SSL_SESSION_free(session);
468 return 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800469}
Adam Langley95c29f32014-06-20 12:00:00 -0700470
David Benjamine3aa1d92015-06-16 15:34:50 -0400471/* ssl_lookup_session looks up |session_id| in the session cache and sets
472 * |*out_session| to an |SSL_SESSION| object if found. The caller takes
473 * ownership of the result. */
474static enum ssl_session_result_t ssl_lookup_session(
475 SSL *ssl, SSL_SESSION **out_session, const uint8_t *session_id,
476 size_t session_id_len) {
477 *out_session = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700478
David Benjamine3aa1d92015-06-16 15:34:50 -0400479 if (session_id_len == 0 || session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
480 return ssl_session_success;
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 }
Adam Langley95c29f32014-06-20 12:00:00 -0700482
David Benjamine3aa1d92015-06-16 15:34:50 -0400483 SSL_SESSION *session;
484 /* Try the internal cache, if it exists. */
485 if (!(ssl->initial_ctx->session_cache_mode &
Adam Langleyfcf25832014-12-18 17:42:32 -0800486 SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
487 SSL_SESSION data;
David Benjamine3aa1d92015-06-16 15:34:50 -0400488 data.ssl_version = ssl->version;
489 data.session_id_length = session_id_len;
490 memcpy(data.session_id, session_id, session_id_len);
491
492 CRYPTO_MUTEX_lock_read(&ssl->initial_ctx->lock);
493 session = lh_SSL_SESSION_retrieve(ssl->initial_ctx->sessions, &data);
494 if (session != NULL) {
495 SSL_SESSION_up_ref(session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800496 }
David Benjamin0f653952015-10-18 14:28:01 -0400497 /* TODO(davidben): This should probably move it to the front of the list. */
David Benjamin29270de2016-05-24 15:28:36 +0000498 CRYPTO_MUTEX_unlock_read(&ssl->initial_ctx->lock);
Adam Langley4bdb6e42015-05-15 15:29:21 -0700499
David Benjamine3aa1d92015-06-16 15:34:50 -0400500 if (session != NULL) {
501 *out_session = session;
502 return ssl_session_success;
Adam Langleyfcf25832014-12-18 17:42:32 -0800503 }
504 }
Adam Langley95c29f32014-06-20 12:00:00 -0700505
David Benjamine3aa1d92015-06-16 15:34:50 -0400506 /* Fall back to the external cache, if it exists. */
507 if (ssl->initial_ctx->get_session_cb == NULL) {
508 return ssl_session_success;
509 }
510 int copy = 1;
511 session = ssl->initial_ctx->get_session_cb(ssl, (uint8_t *)session_id,
512 session_id_len, &copy);
513 if (session == NULL) {
514 return ssl_session_success;
515 }
516 if (session == SSL_magic_pending_session_ptr()) {
517 return ssl_session_retry;
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 }
Adam Langley95c29f32014-06-20 12:00:00 -0700519
David Benjamine3aa1d92015-06-16 15:34:50 -0400520 /* Increment reference count now if the session callback asks us to do so
521 * (note that if the session structures returned by the callback are shared
522 * between threads, it must handle the reference count itself [i.e. copy ==
523 * 0], or things won't be thread-safe). */
524 if (copy) {
525 SSL_SESSION_up_ref(session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800526 }
Adam Langley95c29f32014-06-20 12:00:00 -0700527
David Benjamine3aa1d92015-06-16 15:34:50 -0400528 /* Add the externally cached session to the internal cache if necessary. */
529 if (!(ssl->initial_ctx->session_cache_mode &
530 SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
531 SSL_CTX_add_session(ssl->initial_ctx, session);
532 }
533
534 *out_session = session;
535 return ssl_session_success;
536}
537
538enum ssl_session_result_t ssl_get_prev_session(
539 SSL *ssl, SSL_SESSION **out_session, int *out_send_ticket,
540 const struct ssl_early_callback_ctx *ctx) {
541 /* This is used only by servers. */
542 assert(ssl->server);
543 SSL_SESSION *session = NULL;
David Benjaminef1b0092015-11-21 14:05:44 -0500544 int renew_ticket = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -0400545
546 /* If tickets are disabled, always behave as if no tickets are present. */
David Benjamin3e536332015-07-01 18:45:03 -0400547 const uint8_t *ticket = NULL;
548 size_t ticket_len = 0;
David Benjamine3aa1d92015-06-16 15:34:50 -0400549 const int tickets_supported =
550 !(SSL_get_options(ssl) & SSL_OP_NO_TICKET) &&
David Benjaminc7ce9772015-10-09 19:32:41 -0400551 ssl->version > SSL3_VERSION &&
David Benjamine3aa1d92015-06-16 15:34:50 -0400552 SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
553 &ticket, &ticket_len);
David Benjaminef1b0092015-11-21 14:05:44 -0500554 int from_cache = 0;
555 if (tickets_supported && ticket_len > 0) {
556 if (!tls_process_ticket(ssl, &session, &renew_ticket, ticket, ticket_len,
David Benjamine3aa1d92015-06-16 15:34:50 -0400557 ctx->session_id, ctx->session_id_len)) {
558 return ssl_session_error;
559 }
560 } else {
David Benjaminef1b0092015-11-21 14:05:44 -0500561 /* The client didn't send a ticket, so the session ID is a real ID. */
David Benjamine3aa1d92015-06-16 15:34:50 -0400562 enum ssl_session_result_t lookup_ret = ssl_lookup_session(
563 ssl, &session, ctx->session_id, ctx->session_id_len);
564 if (lookup_ret != ssl_session_success) {
565 return lookup_ret;
566 }
David Benjaminef1b0092015-11-21 14:05:44 -0500567 from_cache = 1;
David Benjamine3aa1d92015-06-16 15:34:50 -0400568 }
569
570 if (session == NULL ||
571 session->sid_ctx_length != ssl->sid_ctx_length ||
572 memcmp(session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length) != 0) {
David Benjaminef1b0092015-11-21 14:05:44 -0500573 /* The client did not offer a suitable ticket or session ID. If supported,
574 * the new session should use a ticket. */
David Benjamine3aa1d92015-06-16 15:34:50 -0400575 goto no_session;
576 }
577
578 if ((ssl->verify_mode & SSL_VERIFY_PEER) && ssl->sid_ctx_length == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800579 /* We can't be sure if this session is being used out of context, which is
580 * especially important for SSL_VERIFY_PEER. The application should have
581 * used SSL[_CTX]_set_session_id_context.
582 *
583 * For this error case, we generate an error instead of treating the event
584 * like a cache miss (otherwise it would be easy for applications to
585 * effectively disable the session cache by accident without anyone
586 * noticing). */
David Benjamin3570d732015-06-29 00:28:17 -0400587 OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
David Benjaminef1b0092015-11-21 14:05:44 -0500588 SSL_SESSION_free(session);
589 return ssl_session_error;
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
David Benjamine3aa1d92015-06-16 15:34:50 -0400592 if (session->timeout < (long)(time(NULL) - session->time)) {
David Benjaminef1b0092015-11-21 14:05:44 -0500593 if (from_cache) {
David Benjamine3aa1d92015-06-16 15:34:50 -0400594 /* The session was from the cache, so remove it. */
595 SSL_CTX_remove_session(ssl->initial_ctx, session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 }
David Benjamine3aa1d92015-06-16 15:34:50 -0400597 goto no_session;
Adam Langleyfcf25832014-12-18 17:42:32 -0800598 }
Adam Langley95c29f32014-06-20 12:00:00 -0700599
David Benjamine3aa1d92015-06-16 15:34:50 -0400600 *out_session = session;
David Benjaminef1b0092015-11-21 14:05:44 -0500601 *out_send_ticket = renew_ticket;
David Benjamine3aa1d92015-06-16 15:34:50 -0400602 return ssl_session_success;
Adam Langley95c29f32014-06-20 12:00:00 -0700603
David Benjamine3aa1d92015-06-16 15:34:50 -0400604no_session:
605 *out_session = NULL;
606 *out_send_ticket = tickets_supported;
607 SSL_SESSION_free(session);
608 return ssl_session_success;
Adam Langley95c29f32014-06-20 12:00:00 -0700609}
610
David Benjamindafbdd42015-09-14 01:40:10 -0400611int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *session) {
David Benjamin415660b2015-10-18 15:08:57 -0400612 /* Although |session| is inserted into two structures (a doubly-linked list
613 * and the hash table), |ctx| only takes one reference. */
David Benjamindafbdd42015-09-14 01:40:10 -0400614 SSL_SESSION_up_ref(session);
Adam Langley95c29f32014-06-20 12:00:00 -0700615
David Benjamin415660b2015-10-18 15:08:57 -0400616 SSL_SESSION *old_session;
Adam Langley4bdb6e42015-05-15 15:29:21 -0700617 CRYPTO_MUTEX_lock_write(&ctx->lock);
David Benjamindafbdd42015-09-14 01:40:10 -0400618 if (!lh_SSL_SESSION_insert(ctx->sessions, &old_session, session)) {
David Benjamin29270de2016-05-24 15:28:36 +0000619 CRYPTO_MUTEX_unlock_write(&ctx->lock);
David Benjamindafbdd42015-09-14 01:40:10 -0400620 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 return 0;
622 }
Adam Langley95c29f32014-06-20 12:00:00 -0700623
David Benjamin415660b2015-10-18 15:08:57 -0400624 if (old_session != NULL) {
625 if (old_session == session) {
626 /* |session| was already in the cache. */
David Benjamin29270de2016-05-24 15:28:36 +0000627 CRYPTO_MUTEX_unlock_write(&ctx->lock);
David Benjamin415660b2015-10-18 15:08:57 -0400628 SSL_SESSION_free(old_session);
629 return 0;
630 }
631
632 /* There was a session ID collision. |old_session| must be removed from
633 * the linked list and released. */
David Benjamindafbdd42015-09-14 01:40:10 -0400634 SSL_SESSION_list_remove(ctx, old_session);
635 SSL_SESSION_free(old_session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 }
Adam Langley95c29f32014-06-20 12:00:00 -0700637
David Benjamin415660b2015-10-18 15:08:57 -0400638 SSL_SESSION_list_add(ctx, session);
Adam Langley95c29f32014-06-20 12:00:00 -0700639
David Benjamin415660b2015-10-18 15:08:57 -0400640 /* Enforce any cache size limits. */
641 if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
642 while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
643 if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
644 break;
Adam Langleyfcf25832014-12-18 17:42:32 -0800645 }
646 }
647 }
Adam Langley95c29f32014-06-20 12:00:00 -0700648
David Benjamin29270de2016-05-24 15:28:36 +0000649 CRYPTO_MUTEX_unlock_write(&ctx->lock);
David Benjamin415660b2015-10-18 15:08:57 -0400650 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800651}
Adam Langley95c29f32014-06-20 12:00:00 -0700652
David Benjamindafbdd42015-09-14 01:40:10 -0400653int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *session) {
654 return remove_session_lock(ctx, session, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800655}
Adam Langley95c29f32014-06-20 12:00:00 -0700656
David Benjamindafbdd42015-09-14 01:40:10 -0400657static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800658 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700659
David Benjamindafbdd42015-09-14 01:40:10 -0400660 if (session != NULL && session->session_id_length != 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800661 if (lock) {
Adam Langley4bdb6e42015-05-15 15:29:21 -0700662 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 }
David Benjamindafbdd42015-09-14 01:40:10 -0400664 SSL_SESSION *found_session = lh_SSL_SESSION_retrieve(ctx->sessions,
665 session);
666 if (found_session == session) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800667 ret = 1;
David Benjamindafbdd42015-09-14 01:40:10 -0400668 found_session = lh_SSL_SESSION_delete(ctx->sessions, session);
669 SSL_SESSION_list_remove(ctx, session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800670 }
Adam Langley95c29f32014-06-20 12:00:00 -0700671
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 if (lock) {
David Benjamin29270de2016-05-24 15:28:36 +0000673 CRYPTO_MUTEX_unlock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800674 }
Adam Langley95c29f32014-06-20 12:00:00 -0700675
Adam Langleyfcf25832014-12-18 17:42:32 -0800676 if (ret) {
David Benjamindafbdd42015-09-14 01:40:10 -0400677 found_session->not_resumable = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800678 if (ctx->remove_session_cb != NULL) {
David Benjamindafbdd42015-09-14 01:40:10 -0400679 ctx->remove_session_cb(ctx, found_session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800680 }
David Benjamindafbdd42015-09-14 01:40:10 -0400681 SSL_SESSION_free(found_session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800682 }
683 }
Adam Langley95c29f32014-06-20 12:00:00 -0700684
Adam Langleyfcf25832014-12-18 17:42:32 -0800685 return ret;
686}
Adam Langley95c29f32014-06-20 12:00:00 -0700687
David Benjamindafbdd42015-09-14 01:40:10 -0400688int SSL_set_session(SSL *ssl, SSL_SESSION *session) {
689 if (ssl->session == session) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800690 return 1;
691 }
Adam Langley95c29f32014-06-20 12:00:00 -0700692
David Benjamindafbdd42015-09-14 01:40:10 -0400693 SSL_SESSION_free(ssl->session);
694 ssl->session = session;
Adam Langleyfcf25832014-12-18 17:42:32 -0800695 if (session != NULL) {
David Benjamin33639842015-02-09 03:34:47 -0500696 SSL_SESSION_up_ref(session);
David Benjamindafbdd42015-09-14 01:40:10 -0400697 ssl->verify_result = session->verify_result;
Adam Langleyfcf25832014-12-18 17:42:32 -0800698 }
Adam Langley95c29f32014-06-20 12:00:00 -0700699
Adam Langleyfcf25832014-12-18 17:42:32 -0800700 return 1;
701}
Adam Langley95c29f32014-06-20 12:00:00 -0700702
David Benjamindafbdd42015-09-14 01:40:10 -0400703long SSL_CTX_set_timeout(SSL_CTX *ctx, long timeout) {
704 if (ctx == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800705 return 0;
706 }
707
David Benjamindafbdd42015-09-14 01:40:10 -0400708 long old_timeout = ctx->session_timeout;
709 ctx->session_timeout = timeout;
710 return old_timeout;
Adam Langleyfcf25832014-12-18 17:42:32 -0800711}
712
David Benjamindafbdd42015-09-14 01:40:10 -0400713long SSL_CTX_get_timeout(const SSL_CTX *ctx) {
714 if (ctx == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800715 return 0;
716 }
717
David Benjamindafbdd42015-09-14 01:40:10 -0400718 return ctx->session_timeout;
Adam Langleyfcf25832014-12-18 17:42:32 -0800719}
720
721typedef struct timeout_param_st {
722 SSL_CTX *ctx;
723 long time;
David Benjamin60da0cd2015-05-03 15:21:28 -0400724 LHASH_OF(SSL_SESSION) *cache;
Adam Langleyfcf25832014-12-18 17:42:32 -0800725} TIMEOUT_PARAM;
726
David Benjamindafbdd42015-09-14 01:40:10 -0400727static void timeout_doall_arg(SSL_SESSION *session, void *void_param) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800728 TIMEOUT_PARAM *param = void_param;
729
730 if (param->time == 0 ||
David Benjamindafbdd42015-09-14 01:40:10 -0400731 param->time > (session->time + session->timeout)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800732 /* timeout */
733 /* The reason we don't call SSL_CTX_remove_session() is to
734 * save on locking overhead */
David Benjamindafbdd42015-09-14 01:40:10 -0400735 (void) lh_SSL_SESSION_delete(param->cache, session);
736 SSL_SESSION_list_remove(param->ctx, session);
737 session->not_resumable = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800738 if (param->ctx->remove_session_cb != NULL) {
David Benjamindafbdd42015-09-14 01:40:10 -0400739 param->ctx->remove_session_cb(param->ctx, session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800740 }
David Benjamindafbdd42015-09-14 01:40:10 -0400741 SSL_SESSION_free(session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800742 }
743}
744
David Benjamindafbdd42015-09-14 01:40:10 -0400745void SSL_CTX_flush_sessions(SSL_CTX *ctx, long time) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800746 TIMEOUT_PARAM tp;
747
Adam Langley4bdb6e42015-05-15 15:29:21 -0700748 tp.ctx = ctx;
749 tp.cache = ctx->sessions;
Adam Langleyfcf25832014-12-18 17:42:32 -0800750 if (tp.cache == NULL) {
751 return;
752 }
David Benjamindafbdd42015-09-14 01:40:10 -0400753 tp.time = time;
Adam Langley4bdb6e42015-05-15 15:29:21 -0700754 CRYPTO_MUTEX_lock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800755 lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
David Benjamin29270de2016-05-24 15:28:36 +0000756 CRYPTO_MUTEX_unlock_write(&ctx->lock);
Adam Langleyfcf25832014-12-18 17:42:32 -0800757}
758
David Benjamin0d56f882015-12-19 17:05:56 -0500759int ssl_clear_bad_session(SSL *ssl) {
David Benjaminfa214e42016-05-10 17:03:10 -0400760 if (ssl->session != NULL &&
761 ssl->s3->send_shutdown != ssl_shutdown_close_notify &&
David Benjamin0d56f882015-12-19 17:05:56 -0500762 !SSL_in_init(ssl)) {
763 SSL_CTX_remove_session(ssl->ctx, ssl->session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800764 return 1;
765 }
766
767 return 0;
768}
Adam Langley95c29f32014-06-20 12:00:00 -0700769
770/* locked by SSL_CTX in the calling function */
David Benjamindafbdd42015-09-14 01:40:10 -0400771static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session) {
772 if (session->next == NULL || session->prev == NULL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 return;
774 }
Adam Langley95c29f32014-06-20 12:00:00 -0700775
David Benjamindafbdd42015-09-14 01:40:10 -0400776 if (session->next == (SSL_SESSION *)&ctx->session_cache_tail) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800777 /* last element in list */
David Benjamindafbdd42015-09-14 01:40:10 -0400778 if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 /* only one element in list */
780 ctx->session_cache_head = NULL;
781 ctx->session_cache_tail = NULL;
782 } else {
David Benjamindafbdd42015-09-14 01:40:10 -0400783 ctx->session_cache_tail = session->prev;
784 session->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800785 }
786 } else {
David Benjamindafbdd42015-09-14 01:40:10 -0400787 if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800788 /* first element in list */
David Benjamindafbdd42015-09-14 01:40:10 -0400789 ctx->session_cache_head = session->next;
790 session->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
Adam Langleyfcf25832014-12-18 17:42:32 -0800791 } else { /* middle of list */
David Benjamindafbdd42015-09-14 01:40:10 -0400792 session->next->prev = session->prev;
793 session->prev->next = session->next;
Adam Langleyfcf25832014-12-18 17:42:32 -0800794 }
795 }
David Benjamindafbdd42015-09-14 01:40:10 -0400796 session->prev = session->next = NULL;
Adam Langleyfcf25832014-12-18 17:42:32 -0800797}
Adam Langley95c29f32014-06-20 12:00:00 -0700798
David Benjamindafbdd42015-09-14 01:40:10 -0400799static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session) {
800 if (session->next != NULL && session->prev != NULL) {
801 SSL_SESSION_list_remove(ctx, session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800802 }
Adam Langley95c29f32014-06-20 12:00:00 -0700803
Adam Langleyfcf25832014-12-18 17:42:32 -0800804 if (ctx->session_cache_head == NULL) {
David Benjamindafbdd42015-09-14 01:40:10 -0400805 ctx->session_cache_head = session;
806 ctx->session_cache_tail = session;
807 session->prev = (SSL_SESSION *)&(ctx->session_cache_head);
808 session->next = (SSL_SESSION *)&(ctx->session_cache_tail);
Adam Langleyfcf25832014-12-18 17:42:32 -0800809 } else {
David Benjamindafbdd42015-09-14 01:40:10 -0400810 session->next = ctx->session_cache_head;
811 session->next->prev = session;
812 session->prev = (SSL_SESSION *)&(ctx->session_cache_head);
813 ctx->session_cache_head = session;
Adam Langleyfcf25832014-12-18 17:42:32 -0800814 }
815}
Adam Langley95c29f32014-06-20 12:00:00 -0700816
817void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400818 int (*cb)(SSL *ssl, SSL_SESSION *session)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800819 ctx->new_session_cb = cb;
820}
Adam Langley95c29f32014-06-20 12:00:00 -0700821
David Benjamindafbdd42015-09-14 01:40:10 -0400822int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *session) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800823 return ctx->new_session_cb;
824}
Adam Langley95c29f32014-06-20 12:00:00 -0700825
David Benjamindafbdd42015-09-14 01:40:10 -0400826void SSL_CTX_sess_set_remove_cb(
827 SSL_CTX *ctx, void (*cb)(SSL_CTX *ctx, SSL_SESSION *session)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800828 ctx->remove_session_cb = cb;
829}
Adam Langley95c29f32014-06-20 12:00:00 -0700830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400832 SSL_SESSION *session) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800833 return ctx->remove_session_cb;
834}
Adam Langley95c29f32014-06-20 12:00:00 -0700835
836void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
David Benjamindafbdd42015-09-14 01:40:10 -0400837 SSL_SESSION *(*cb)(SSL *ssl,
838 uint8_t *id, int id_len,
839 int *out_copy)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 ctx->get_session_cb = cb;
841}
Adam Langley95c29f32014-06-20 12:00:00 -0700842
David Benjamindafbdd42015-09-14 01:40:10 -0400843SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(
844 SSL *ssl, uint8_t *id, int id_len, int *out_copy) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800845 return ctx->get_session_cb;
846}
Adam Langley95c29f32014-06-20 12:00:00 -0700847
David Benjamin82170242015-10-17 22:51:17 -0400848void SSL_CTX_set_info_callback(
849 SSL_CTX *ctx, void (*cb)(const SSL *ssl, int type, int value)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800850 ctx->info_callback = cb;
851}
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
David Benjamin82170242015-10-17 22:51:17 -0400854 int value) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800855 return ctx->info_callback;
856}
Adam Langley95c29f32014-06-20 12:00:00 -0700857
David Benjaminfd8e69f2015-10-03 10:49:20 -0400858void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
859 X509 **out_x509,
860 EVP_PKEY **out_pkey)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800861 ctx->client_cert_cb = cb;
862}
Adam Langley95c29f32014-06-20 12:00:00 -0700863
David Benjaminfd8e69f2015-10-03 10:49:20 -0400864int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **out_x509,
865 EVP_PKEY **out_pkey) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800866 return ctx->client_cert_cb;
867}
Adam Langley95c29f32014-06-20 12:00:00 -0700868
Adam Langley1258b6a2014-06-20 12:00:00 -0700869void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
Adam Langleyfcf25832014-12-18 17:42:32 -0800870 void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
871 ctx->channel_id_cb = cb;
872}
Adam Langley1258b6a2014-06-20 12:00:00 -0700873
Adam Langleyfcf25832014-12-18 17:42:32 -0800874void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
875 return ctx->channel_id_cb;
876}