]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
f1fd4544 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
f1fd4544 | 8 | */ |
846e33c7 | 9 | |
ddac1974 NL |
10 | /* ==================================================================== |
11 | * Copyright 2005 Nokia. All rights reserved. | |
12 | * | |
13 | * The portions of the attached software ("Contribution") is developed by | |
14 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source | |
15 | * license. | |
16 | * | |
17 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of | |
18 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites | |
19 | * support (see RFC 4279) to OpenSSL. | |
20 | * | |
21 | * No patent licenses or other rights except those expressly stated in | |
22 | * the OpenSSL open source license shall be deemed granted or received | |
23 | * expressly, by implication, estoppel, or otherwise. | |
24 | * | |
25 | * No assurances are provided by Nokia that the Contribution does not | |
26 | * infringe the patent or other intellectual property rights of any third | |
27 | * party or that the license provides you with all the necessary rights | |
28 | * to make use of the Contribution. | |
29 | * | |
30 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN | |
31 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA | |
32 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY | |
33 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR | |
34 | * OTHERWISE. | |
35 | */ | |
d02b48c6 RE |
36 | |
37 | #include <stdio.h> | |
ec577822 BM |
38 | #include <openssl/lhash.h> |
39 | #include <openssl/rand.h> | |
3c27208f | 40 | #include <openssl/engine.h> |
d02b48c6 RE |
41 | #include "ssl_locl.h" |
42 | ||
58964a49 | 43 | static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s); |
0f113f3e | 44 | static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s); |
801294f8 | 45 | static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck); |
58964a49 | 46 | |
0821bcd4 | 47 | SSL_SESSION *SSL_get_session(const SSL *ssl) |
52732b38 | 48 | /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */ |
0f113f3e MC |
49 | { |
50 | return (ssl->session); | |
51 | } | |
52732b38 BM |
52 | |
53 | SSL_SESSION *SSL_get1_session(SSL *ssl) | |
54 | /* variant of SSL_get_session: caller really gets something */ | |
0f113f3e MC |
55 | { |
56 | SSL_SESSION *sess; | |
57 | /* | |
58 | * Need to lock this all up rather than just use CRYPTO_add so that | |
59 | * somebody doesn't free ssl->session between when we check it's non-null | |
60 | * and when we up the reference count. | |
61 | */ | |
16203f7b | 62 | CRYPTO_THREAD_read_lock(ssl->lock); |
0f113f3e MC |
63 | sess = ssl->session; |
64 | if (sess) | |
16203f7b AG |
65 | SSL_SESSION_up_ref(sess); |
66 | CRYPTO_THREAD_unlock(ssl->lock); | |
67 | return sess; | |
0f113f3e MC |
68 | } |
69 | ||
6b691a5c | 70 | int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) |
0f113f3e MC |
71 | { |
72 | return (CRYPTO_set_ex_data(&s->ex_data, idx, arg)); | |
73 | } | |
58964a49 | 74 | |
0821bcd4 | 75 | void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) |
0f113f3e MC |
76 | { |
77 | return (CRYPTO_get_ex_data(&s->ex_data, idx)); | |
78 | } | |
58964a49 | 79 | |
6b691a5c | 80 | SSL_SESSION *SSL_SESSION_new(void) |
0f113f3e MC |
81 | { |
82 | SSL_SESSION *ss; | |
83 | ||
b51bce94 | 84 | ss = OPENSSL_zalloc(sizeof(*ss)); |
0f113f3e MC |
85 | if (ss == NULL) { |
86 | SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE); | |
16203f7b | 87 | return NULL; |
0f113f3e | 88 | } |
0f113f3e MC |
89 | |
90 | ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */ | |
91 | ss->references = 1; | |
92 | ss->timeout = 60 * 5 + 4; /* 5 minute timeout by default */ | |
93 | ss->time = (unsigned long)time(NULL); | |
16203f7b AG |
94 | ss->lock = CRYPTO_THREAD_lock_new(); |
95 | if (ss->lock == NULL) { | |
96 | SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE); | |
97 | OPENSSL_free(ss); | |
98 | return NULL; | |
99 | } | |
100 | ||
25a807bc F |
101 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) { |
102 | CRYPTO_THREAD_lock_free(ss->lock); | |
103 | OPENSSL_free(ss); | |
104 | return NULL; | |
105 | } | |
16203f7b | 106 | return ss; |
0f113f3e | 107 | } |
d02b48c6 | 108 | |
98ece4ee MC |
109 | /* |
110 | * Create a new SSL_SESSION and duplicate the contents of |src| into it. If | |
111 | * ticket == 0 then no ticket information is duplicated, otherwise it is. | |
112 | */ | |
113 | SSL_SESSION *ssl_session_dup(SSL_SESSION *src, int ticket) | |
114 | { | |
115 | SSL_SESSION *dest; | |
116 | ||
117 | dest = OPENSSL_malloc(sizeof(*src)); | |
118 | if (dest == NULL) { | |
119 | goto err; | |
120 | } | |
121 | memcpy(dest, src, sizeof(*dest)); | |
122 | ||
708cf593 MC |
123 | /* |
124 | * Set the various pointers to NULL so that we can call SSL_SESSION_free in | |
125 | * the case of an error whilst halfway through constructing dest | |
126 | */ | |
127 | #ifndef OPENSSL_NO_PSK | |
128 | dest->psk_identity_hint = NULL; | |
129 | dest->psk_identity = NULL; | |
130 | #endif | |
131 | dest->ciphers = NULL; | |
132 | dest->tlsext_hostname = NULL; | |
133 | #ifndef OPENSSL_NO_EC | |
134 | dest->tlsext_ecpointformatlist = NULL; | |
135 | dest->tlsext_ellipticcurvelist = NULL; | |
136 | #endif | |
137 | dest->tlsext_tick = NULL; | |
138 | #ifndef OPENSSL_NO_SRP | |
139 | dest->srp_username = NULL; | |
140 | #endif | |
141 | memset(&dest->ex_data, 0, sizeof(dest->ex_data)); | |
142 | ||
143 | /* We deliberately don't copy the prev and next pointers */ | |
144 | dest->prev = NULL; | |
145 | dest->next = NULL; | |
146 | ||
147 | dest->references = 1; | |
148 | ||
16203f7b AG |
149 | dest->lock = CRYPTO_THREAD_lock_new(); |
150 | if (dest->lock == NULL) | |
151 | goto err; | |
152 | ||
708cf593 | 153 | if (src->peer != NULL) |
05f0fb9f | 154 | X509_up_ref(src->peer); |
708cf593 | 155 | |
36f038f1 DSH |
156 | if (src->peer_chain != NULL) { |
157 | dest->peer_chain = X509_chain_up_ref(src->peer_chain); | |
158 | if (dest->peer_chain == NULL) | |
159 | goto err; | |
160 | } | |
161 | ||
98ece4ee MC |
162 | #ifndef OPENSSL_NO_PSK |
163 | if (src->psk_identity_hint) { | |
7644a9ae | 164 | dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint); |
98ece4ee MC |
165 | if (dest->psk_identity_hint == NULL) { |
166 | goto err; | |
167 | } | |
98ece4ee MC |
168 | } |
169 | if (src->psk_identity) { | |
7644a9ae | 170 | dest->psk_identity = OPENSSL_strdup(src->psk_identity); |
98ece4ee MC |
171 | if (dest->psk_identity == NULL) { |
172 | goto err; | |
173 | } | |
98ece4ee MC |
174 | } |
175 | #endif | |
176 | ||
e8aa8b6c | 177 | if (src->ciphers != NULL) { |
98ece4ee MC |
178 | dest->ciphers = sk_SSL_CIPHER_dup(src->ciphers); |
179 | if (dest->ciphers == NULL) | |
180 | goto err; | |
98ece4ee MC |
181 | } |
182 | ||
183 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, | |
184 | &dest->ex_data, &src->ex_data)) { | |
185 | goto err; | |
186 | } | |
187 | ||
98ece4ee | 188 | if (src->tlsext_hostname) { |
7644a9ae | 189 | dest->tlsext_hostname = OPENSSL_strdup(src->tlsext_hostname); |
98ece4ee MC |
190 | if (dest->tlsext_hostname == NULL) { |
191 | goto err; | |
192 | } | |
98ece4ee | 193 | } |
708cf593 | 194 | #ifndef OPENSSL_NO_EC |
98ece4ee MC |
195 | if (src->tlsext_ecpointformatlist) { |
196 | dest->tlsext_ecpointformatlist = | |
7644a9ae | 197 | OPENSSL_memdup(src->tlsext_ecpointformatlist, |
98ece4ee MC |
198 | src->tlsext_ecpointformatlist_length); |
199 | if (dest->tlsext_ecpointformatlist == NULL) | |
200 | goto err; | |
98ece4ee MC |
201 | } |
202 | if (src->tlsext_ellipticcurvelist) { | |
203 | dest->tlsext_ellipticcurvelist = | |
7644a9ae | 204 | OPENSSL_memdup(src->tlsext_ellipticcurvelist, |
98ece4ee MC |
205 | src->tlsext_ellipticcurvelist_length); |
206 | if (dest->tlsext_ellipticcurvelist == NULL) | |
207 | goto err; | |
98ece4ee | 208 | } |
98ece4ee MC |
209 | #endif |
210 | ||
211 | if (ticket != 0) { | |
7644a9ae | 212 | dest->tlsext_tick = OPENSSL_memdup(src->tlsext_tick, src->tlsext_ticklen); |
e8aa8b6c | 213 | if (dest->tlsext_tick == NULL) |
98ece4ee | 214 | goto err; |
708cf593 MC |
215 | } else { |
216 | dest->tlsext_tick_lifetime_hint = 0; | |
217 | dest->tlsext_ticklen = 0; | |
98ece4ee MC |
218 | } |
219 | ||
220 | #ifndef OPENSSL_NO_SRP | |
98ece4ee | 221 | if (src->srp_username) { |
7644a9ae | 222 | dest->srp_username = OPENSSL_strdup(src->srp_username); |
98ece4ee MC |
223 | if (dest->srp_username == NULL) { |
224 | goto err; | |
225 | } | |
98ece4ee MC |
226 | } |
227 | #endif | |
228 | ||
229 | return dest; | |
230 | err: | |
231 | SSLerr(SSL_F_SSL_SESSION_DUP, ERR_R_MALLOC_FAILURE); | |
232 | SSL_SESSION_free(dest); | |
233 | return NULL; | |
234 | } | |
235 | ||
0f113f3e MC |
236 | const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, |
237 | unsigned int *len) | |
238 | { | |
239 | if (len) | |
240 | *len = s->session_id_length; | |
241 | return s->session_id; | |
242 | } | |
fddfc0af RG |
243 | const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s, |
244 | unsigned int *len) | |
245 | { | |
246 | if (len != NULL) | |
247 | *len = s->sid_ctx_length; | |
248 | return s->sid_ctx; | |
249 | } | |
4879ec7b | 250 | |
f9b0b452 | 251 | unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s) |
0f113f3e MC |
252 | { |
253 | return s->compress_meth; | |
254 | } | |
255 | ||
256 | /* | |
257 | * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling | |
258 | * the ID with random junk repeatedly until we have no conflict is going to | |
259 | * complete in one iteration pretty much "most" of the time (btw: | |
260 | * understatement). So, if it takes us 10 iterations and we still can't avoid | |
261 | * a conflict - well that's a reasonable point to call it quits. Either the | |
262 | * RAND code is broken or someone is trying to open roughly very close to | |
263 | * 2^256 SSL sessions to our server. How you might store that many sessions | |
264 | * is perhaps a more interesting question ... | |
265 | */ | |
dc644fe2 GT |
266 | |
267 | #define MAX_SESS_ID_ATTEMPTS 10 | |
268 | static int def_generate_session_id(const SSL *ssl, unsigned char *id, | |
0f113f3e | 269 | unsigned int *id_len) |
dc644fe2 | 270 | { |
0f113f3e MC |
271 | unsigned int retry = 0; |
272 | do | |
266483d2 | 273 | if (RAND_bytes(id, *id_len) <= 0) |
0f113f3e MC |
274 | return 0; |
275 | while (SSL_has_matching_session_id(ssl, id, *id_len) && | |
276 | (++retry < MAX_SESS_ID_ATTEMPTS)) ; | |
277 | if (retry < MAX_SESS_ID_ATTEMPTS) | |
278 | return 1; | |
279 | /* else - woops a session_id match */ | |
280 | /* | |
281 | * XXX We should also check the external cache -- but the probability of | |
282 | * a collision is negligible, and we could not prevent the concurrent | |
283 | * creation of sessions with identical IDs since we currently don't have | |
284 | * means to atomically check whether a session ID already exists and make | |
285 | * a reservation for it if it does not (this problem applies to the | |
286 | * internal cache as well). | |
287 | */ | |
288 | return 0; | |
dc644fe2 GT |
289 | } |
290 | ||
6b691a5c | 291 | int ssl_get_new_session(SSL *s, int session) |
0f113f3e MC |
292 | { |
293 | /* This gets used by clients and servers. */ | |
294 | ||
295 | unsigned int tmp; | |
296 | SSL_SESSION *ss = NULL; | |
297 | GEN_SESSION_CB cb = def_generate_session_id; | |
298 | ||
299 | if ((ss = SSL_SESSION_new()) == NULL) | |
300 | return (0); | |
301 | ||
302 | /* If the context has a default timeout, use it */ | |
303 | if (s->session_ctx->session_timeout == 0) | |
304 | ss->timeout = SSL_get_default_timeout(s); | |
305 | else | |
306 | ss->timeout = s->session_ctx->session_timeout; | |
307 | ||
62adbcee RS |
308 | SSL_SESSION_free(s->session); |
309 | s->session = NULL; | |
0f113f3e MC |
310 | |
311 | if (session) { | |
312 | if (s->version == SSL3_VERSION) { | |
313 | ss->ssl_version = SSL3_VERSION; | |
314 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
315 | } else if (s->version == TLS1_VERSION) { | |
316 | ss->ssl_version = TLS1_VERSION; | |
317 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
318 | } else if (s->version == TLS1_1_VERSION) { | |
319 | ss->ssl_version = TLS1_1_VERSION; | |
320 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
321 | } else if (s->version == TLS1_2_VERSION) { | |
322 | ss->ssl_version = TLS1_2_VERSION; | |
323 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
324 | } else if (s->version == DTLS1_BAD_VER) { | |
325 | ss->ssl_version = DTLS1_BAD_VER; | |
326 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
327 | } else if (s->version == DTLS1_VERSION) { | |
328 | ss->ssl_version = DTLS1_VERSION; | |
329 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
330 | } else if (s->version == DTLS1_2_VERSION) { | |
331 | ss->ssl_version = DTLS1_2_VERSION; | |
332 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
333 | } else { | |
334 | SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION); | |
335 | SSL_SESSION_free(ss); | |
336 | return (0); | |
337 | } | |
e481f9b9 | 338 | |
35a1cc90 MC |
339 | /*- |
340 | * If RFC5077 ticket, use empty session ID (as server). | |
341 | * Note that: | |
342 | * (a) ssl_get_prev_session() does lookahead into the | |
343 | * ClientHello extensions to find the session ticket. | |
d4d78943 MC |
344 | * When ssl_get_prev_session() fails, statem_srvr.c calls |
345 | * ssl_get_new_session() in tls_process_client_hello(). | |
35a1cc90 MC |
346 | * At that point, it has not yet parsed the extensions, |
347 | * however, because of the lookahead, it already knows | |
348 | * whether a ticket is expected or not. | |
349 | * | |
d4d78943 | 350 | * (b) statem_clnt.c calls ssl_get_new_session() before parsing |
35a1cc90 MC |
351 | * ServerHello extensions, and before recording the session |
352 | * ID received from the server, so this block is a noop. | |
353 | */ | |
0f113f3e MC |
354 | if (s->tlsext_ticket_expected) { |
355 | ss->session_id_length = 0; | |
356 | goto sess_id_done; | |
357 | } | |
e481f9b9 | 358 | |
0f113f3e | 359 | /* Choose which callback will set the session ID */ |
16203f7b AG |
360 | CRYPTO_THREAD_read_lock(s->lock); |
361 | CRYPTO_THREAD_read_lock(s->session_ctx->lock); | |
0f113f3e MC |
362 | if (s->generate_session_id) |
363 | cb = s->generate_session_id; | |
364 | else if (s->session_ctx->generate_session_id) | |
365 | cb = s->session_ctx->generate_session_id; | |
16203f7b AG |
366 | CRYPTO_THREAD_unlock(s->session_ctx->lock); |
367 | CRYPTO_THREAD_unlock(s->lock); | |
0f113f3e | 368 | /* Choose a session ID */ |
947f3156 | 369 | memset(ss->session_id, 0, ss->session_id_length); |
0f113f3e MC |
370 | tmp = ss->session_id_length; |
371 | if (!cb(s, ss->session_id, &tmp)) { | |
372 | /* The callback failed */ | |
373 | SSLerr(SSL_F_SSL_GET_NEW_SESSION, | |
374 | SSL_R_SSL_SESSION_ID_CALLBACK_FAILED); | |
375 | SSL_SESSION_free(ss); | |
376 | return (0); | |
377 | } | |
378 | /* | |
379 | * Don't allow the callback to set the session length to zero. nor | |
380 | * set it higher than it was. | |
381 | */ | |
cc99bfa7 | 382 | if (tmp == 0 || tmp > ss->session_id_length) { |
0f113f3e MC |
383 | /* The callback set an illegal length */ |
384 | SSLerr(SSL_F_SSL_GET_NEW_SESSION, | |
385 | SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH); | |
386 | SSL_SESSION_free(ss); | |
387 | return (0); | |
388 | } | |
389 | ss->session_id_length = tmp; | |
390 | /* Finally, check for a conflict */ | |
391 | if (SSL_has_matching_session_id(s, ss->session_id, | |
392 | ss->session_id_length)) { | |
393 | SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_SSL_SESSION_ID_CONFLICT); | |
394 | SSL_SESSION_free(ss); | |
395 | return (0); | |
396 | } | |
e481f9b9 | 397 | |
0f113f3e MC |
398 | sess_id_done: |
399 | if (s->tlsext_hostname) { | |
7644a9ae | 400 | ss->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname); |
0f113f3e MC |
401 | if (ss->tlsext_hostname == NULL) { |
402 | SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR); | |
403 | SSL_SESSION_free(ss); | |
404 | return 0; | |
405 | } | |
406 | } | |
0f113f3e MC |
407 | } else { |
408 | ss->session_id_length = 0; | |
409 | } | |
410 | ||
411 | if (s->sid_ctx_length > sizeof ss->sid_ctx) { | |
412 | SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR); | |
413 | SSL_SESSION_free(ss); | |
414 | return 0; | |
415 | } | |
416 | memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length); | |
417 | ss->sid_ctx_length = s->sid_ctx_length; | |
418 | s->session = ss; | |
419 | ss->ssl_version = s->version; | |
420 | ss->verify_result = X509_V_OK; | |
421 | ||
e7f0d921 DSH |
422 | /* If client supports extended master secret set it in session */ |
423 | if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) | |
424 | ss->flags |= SSL_SESS_FLAG_EXTMS; | |
425 | ||
0f113f3e MC |
426 | return (1); |
427 | } | |
d02b48c6 | 428 | |
3a83462d MC |
429 | /*- |
430 | * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this | |
c519e89f BM |
431 | * connection. It is only called by servers. |
432 | * | |
b3e2272c EK |
433 | * ext: ClientHello extensions (including length prefix) |
434 | * session_id: ClientHello session ID. | |
c519e89f BM |
435 | * |
436 | * Returns: | |
437 | * -1: error | |
438 | * 0: a session may have been found. | |
439 | * | |
440 | * Side effects: | |
441 | * - If a session is found then s->session is pointed at it (after freeing an | |
442 | * existing session if need be) and s->verify_result is set from the session. | |
443 | * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1 | |
444 | * if the server should issue a new session ticket (to 0 otherwise). | |
445 | */ | |
b3e2272c | 446 | int ssl_get_prev_session(SSL *s, const PACKET *ext, const PACKET *session_id) |
0f113f3e MC |
447 | { |
448 | /* This is used only by servers. */ | |
b56bce4f | 449 | |
0f113f3e MC |
450 | SSL_SESSION *ret = NULL; |
451 | int fatal = 0; | |
452 | int try_session_cache = 1; | |
0f113f3e | 453 | int r; |
d02b48c6 | 454 | |
293b5ca4 | 455 | if (PACKET_remaining(session_id) == 0) |
0f113f3e | 456 | try_session_cache = 0; |
c519e89f | 457 | |
e7f0d921 DSH |
458 | /* sets s->tlsext_ticket_expected and extended master secret flag */ |
459 | r = tls_check_serverhello_tlsext_early(s, ext, session_id, &ret); | |
0f113f3e MC |
460 | switch (r) { |
461 | case -1: /* Error during processing */ | |
462 | fatal = 1; | |
463 | goto err; | |
464 | case 0: /* No ticket found */ | |
465 | case 1: /* Zero length ticket found */ | |
466 | break; /* Ok to carry on processing session id. */ | |
467 | case 2: /* Ticket found but not decrypted. */ | |
468 | case 3: /* Ticket decrypted, *ret has been set. */ | |
469 | try_session_cache = 0; | |
470 | break; | |
471 | default: | |
472 | abort(); | |
473 | } | |
c519e89f | 474 | |
0f113f3e MC |
475 | if (try_session_cache && |
476 | ret == NULL && | |
739a5eee | 477 | !(s->session_ctx->session_cache_mode & |
0f113f3e MC |
478 | SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) { |
479 | SSL_SESSION data; | |
67202973 | 480 | size_t local_len; |
0f113f3e | 481 | data.ssl_version = s->version; |
947f3156 | 482 | memset(data.session_id, 0, sizeof(data.session_id)); |
67202973 EK |
483 | if (!PACKET_copy_all(session_id, data.session_id, |
484 | sizeof(data.session_id), | |
485 | &local_len)) { | |
486 | goto err; | |
487 | } | |
488 | data.session_id_length = local_len; | |
16203f7b | 489 | CRYPTO_THREAD_read_lock(s->session_ctx->lock); |
0f113f3e MC |
490 | ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data); |
491 | if (ret != NULL) { | |
492 | /* don't allow other threads to steal it: */ | |
16203f7b | 493 | SSL_SESSION_up_ref(ret); |
0f113f3e | 494 | } |
16203f7b | 495 | CRYPTO_THREAD_unlock(s->session_ctx->lock); |
0f113f3e MC |
496 | if (ret == NULL) |
497 | s->session_ctx->stats.sess_miss++; | |
498 | } | |
499 | ||
500 | if (try_session_cache && | |
501 | ret == NULL && s->session_ctx->get_session_cb != NULL) { | |
502 | int copy = 1; | |
b6981744 EK |
503 | ret = s->session_ctx->get_session_cb(s, PACKET_data(session_id), |
504 | PACKET_remaining(session_id), | |
505 | ©); | |
0f113f3e | 506 | |
bf0fc412 | 507 | if (ret != NULL) { |
0f113f3e MC |
508 | s->session_ctx->stats.sess_cb_hit++; |
509 | ||
510 | /* | |
511 | * Increment reference count now if the session callback asks us | |
512 | * to do so (note that if the session structures returned by the | |
513 | * callback are shared between threads, it must handle the | |
514 | * reference count itself [i.e. copy == 0], or things won't be | |
515 | * thread-safe). | |
516 | */ | |
517 | if (copy) | |
16203f7b | 518 | SSL_SESSION_up_ref(ret); |
0f113f3e MC |
519 | |
520 | /* | |
521 | * Add the externally cached session to the internal cache as | |
522 | * well if and only if we are supposed to. | |
523 | */ | |
524 | if (! | |
739a5eee | 525 | (s->session_ctx->session_cache_mode & |
69f68237 | 526 | SSL_SESS_CACHE_NO_INTERNAL_STORE)) { |
0f113f3e MC |
527 | /* |
528 | * The following should not return 1, otherwise, things are | |
529 | * very strange | |
530 | */ | |
61986d32 | 531 | if (SSL_CTX_add_session(s->session_ctx, ret)) |
69f68237 MC |
532 | goto err; |
533 | } | |
0f113f3e MC |
534 | } |
535 | } | |
536 | ||
537 | if (ret == NULL) | |
538 | goto err; | |
539 | ||
540 | /* Now ret is non-NULL and we own one of its reference counts. */ | |
541 | ||
542 | if (ret->sid_ctx_length != s->sid_ctx_length | |
543 | || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) { | |
544 | /* | |
545 | * We have the session requested by the client, but we don't want to | |
546 | * use it in this context. | |
547 | */ | |
548 | goto err; /* treat like cache miss */ | |
549 | } | |
550 | ||
551 | if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) { | |
552 | /* | |
553 | * We can't be sure if this session is being used out of context, | |
554 | * which is especially important for SSL_VERIFY_PEER. The application | |
555 | * should have used SSL[_CTX]_set_session_id_context. For this error | |
556 | * case, we generate an error instead of treating the event like a | |
557 | * cache miss (otherwise it would be easy for applications to | |
558 | * effectively disable the session cache by accident without anyone | |
559 | * noticing). | |
560 | */ | |
561 | ||
562 | SSLerr(SSL_F_SSL_GET_PREV_SESSION, | |
563 | SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); | |
564 | fatal = 1; | |
565 | goto err; | |
566 | } | |
567 | ||
568 | if (ret->cipher == NULL) { | |
569 | unsigned char buf[5], *p; | |
570 | unsigned long l; | |
571 | ||
572 | p = buf; | |
573 | l = ret->cipher_id; | |
574 | l2n(l, p); | |
575 | if ((ret->ssl_version >> 8) >= SSL3_VERSION_MAJOR) | |
576 | ret->cipher = ssl_get_cipher_by_char(s, &(buf[2])); | |
577 | else | |
578 | ret->cipher = ssl_get_cipher_by_char(s, &(buf[1])); | |
579 | if (ret->cipher == NULL) | |
580 | goto err; | |
581 | } | |
582 | ||
583 | if (ret->timeout < (long)(time(NULL) - ret->time)) { /* timeout */ | |
584 | s->session_ctx->stats.sess_timeout++; | |
585 | if (try_session_cache) { | |
586 | /* session was from the cache, so remove it */ | |
587 | SSL_CTX_remove_session(s->session_ctx, ret); | |
588 | } | |
589 | goto err; | |
590 | } | |
591 | ||
e7f0d921 DSH |
592 | /* Check extended master secret extension consistency */ |
593 | if (ret->flags & SSL_SESS_FLAG_EXTMS) { | |
594 | /* If old session includes extms, but new does not: abort handshake */ | |
595 | if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)) { | |
596 | SSLerr(SSL_F_SSL_GET_PREV_SESSION, SSL_R_INCONSISTENT_EXTMS); | |
597 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
598 | fatal = 1; | |
599 | goto err; | |
600 | } | |
601 | } else if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) { | |
602 | /* If new session includes extms, but old does not: do not resume */ | |
603 | goto err; | |
604 | } | |
605 | ||
0f113f3e MC |
606 | s->session_ctx->stats.sess_hit++; |
607 | ||
62adbcee | 608 | SSL_SESSION_free(s->session); |
0f113f3e MC |
609 | s->session = ret; |
610 | s->verify_result = s->session->verify_result; | |
611 | return 1; | |
8876bc05 BM |
612 | |
613 | err: | |
0f113f3e MC |
614 | if (ret != NULL) { |
615 | SSL_SESSION_free(ret); | |
e481f9b9 | 616 | |
0f113f3e MC |
617 | if (!try_session_cache) { |
618 | /* | |
619 | * The session was from a ticket, so we should issue a ticket for | |
620 | * the new session | |
621 | */ | |
622 | s->tlsext_ticket_expected = 1; | |
623 | } | |
0f113f3e MC |
624 | } |
625 | if (fatal) | |
626 | return -1; | |
627 | else | |
628 | return 0; | |
629 | } | |
d02b48c6 | 630 | |
6b691a5c | 631 | int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) |
0f113f3e MC |
632 | { |
633 | int ret = 0; | |
634 | SSL_SESSION *s; | |
635 | ||
636 | /* | |
637 | * add just 1 reference count for the SSL_CTX's session cache even though | |
638 | * it has two ways of access: each session is in a doubly linked list and | |
639 | * an lhash | |
640 | */ | |
16203f7b | 641 | SSL_SESSION_up_ref(c); |
0f113f3e MC |
642 | /* |
643 | * if session c is in already in cache, we take back the increment later | |
644 | */ | |
645 | ||
16203f7b | 646 | CRYPTO_THREAD_write_lock(ctx->lock); |
0f113f3e MC |
647 | s = lh_SSL_SESSION_insert(ctx->sessions, c); |
648 | ||
649 | /* | |
650 | * s != NULL iff we already had a session with the given PID. In this | |
651 | * case, s == c should hold (then we did not really modify | |
652 | * ctx->sessions), or we're in trouble. | |
653 | */ | |
654 | if (s != NULL && s != c) { | |
655 | /* We *are* in trouble ... */ | |
656 | SSL_SESSION_list_remove(ctx, s); | |
657 | SSL_SESSION_free(s); | |
658 | /* | |
659 | * ... so pretend the other session did not exist in cache (we cannot | |
660 | * handle two SSL_SESSION structures with identical session ID in the | |
661 | * same cache, which could happen e.g. when two threads concurrently | |
662 | * obtain the same session from an external cache) | |
663 | */ | |
664 | s = NULL; | |
665 | } | |
666 | ||
667 | /* Put at the head of the queue unless it is already in the cache */ | |
668 | if (s == NULL) | |
669 | SSL_SESSION_list_add(ctx, c); | |
670 | ||
671 | if (s != NULL) { | |
672 | /* | |
673 | * existing cache entry -- decrement previously incremented reference | |
674 | * count because it already takes into account the cache | |
675 | */ | |
676 | ||
677 | SSL_SESSION_free(s); /* s == c */ | |
678 | ret = 0; | |
679 | } else { | |
680 | /* | |
681 | * new cache entry -- remove old ones if cache has become too large | |
682 | */ | |
683 | ||
684 | ret = 1; | |
685 | ||
686 | if (SSL_CTX_sess_get_cache_size(ctx) > 0) { | |
687 | while (SSL_CTX_sess_number(ctx) > | |
688 | SSL_CTX_sess_get_cache_size(ctx)) { | |
689 | if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) | |
690 | break; | |
691 | else | |
692 | ctx->stats.sess_cache_full++; | |
693 | } | |
694 | } | |
695 | } | |
16203f7b AG |
696 | CRYPTO_THREAD_unlock(ctx->lock); |
697 | return ret; | |
0f113f3e | 698 | } |
d02b48c6 | 699 | |
6b691a5c | 700 | int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) |
801294f8 | 701 | { |
0f113f3e | 702 | return remove_session_lock(ctx, c, 1); |
801294f8 DSH |
703 | } |
704 | ||
0fda2e37 | 705 | static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck) |
0f113f3e MC |
706 | { |
707 | SSL_SESSION *r; | |
708 | int ret = 0; | |
709 | ||
710 | if ((c != NULL) && (c->session_id_length != 0)) { | |
711 | if (lck) | |
16203f7b | 712 | CRYPTO_THREAD_write_lock(ctx->lock); |
0f113f3e MC |
713 | if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) { |
714 | ret = 1; | |
715 | r = lh_SSL_SESSION_delete(ctx->sessions, c); | |
716 | SSL_SESSION_list_remove(ctx, c); | |
717 | } | |
e4612d02 | 718 | c->not_resumable = 1; |
0f113f3e MC |
719 | |
720 | if (lck) | |
16203f7b | 721 | CRYPTO_THREAD_unlock(ctx->lock); |
0f113f3e | 722 | |
e4612d02 | 723 | if (ret) |
0f113f3e | 724 | SSL_SESSION_free(r); |
e4612d02 MC |
725 | |
726 | if (ctx->remove_session_cb != NULL) | |
727 | ctx->remove_session_cb(ctx, c); | |
0f113f3e MC |
728 | } else |
729 | ret = 0; | |
730 | return (ret); | |
731 | } | |
d02b48c6 | 732 | |
6b691a5c | 733 | void SSL_SESSION_free(SSL_SESSION *ss) |
0f113f3e MC |
734 | { |
735 | int i; | |
d02b48c6 | 736 | |
0f113f3e MC |
737 | if (ss == NULL) |
738 | return; | |
e03ddfae | 739 | |
16203f7b | 740 | CRYPTO_atomic_add(&ss->references, -1, &i, ss->lock); |
f3f1cf84 | 741 | REF_PRINT_COUNT("SSL_SESSION", ss); |
0f113f3e MC |
742 | if (i > 0) |
743 | return; | |
f3f1cf84 | 744 | REF_ASSERT_ISNT(i < 0); |
d02b48c6 | 745 | |
0f113f3e | 746 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); |
58964a49 | 747 | |
0f113f3e MC |
748 | OPENSSL_cleanse(ss->master_key, sizeof ss->master_key); |
749 | OPENSSL_cleanse(ss->session_id, sizeof ss->session_id); | |
222561fe | 750 | X509_free(ss->peer); |
c34b0f99 | 751 | sk_X509_pop_free(ss->peer_chain, X509_free); |
25aaa98a | 752 | sk_SSL_CIPHER_free(ss->ciphers); |
25aaa98a RS |
753 | OPENSSL_free(ss->tlsext_hostname); |
754 | OPENSSL_free(ss->tlsext_tick); | |
e481f9b9 | 755 | #ifndef OPENSSL_NO_EC |
0f113f3e | 756 | ss->tlsext_ecpointformatlist_length = 0; |
25aaa98a | 757 | OPENSSL_free(ss->tlsext_ecpointformatlist); |
0f113f3e | 758 | ss->tlsext_ellipticcurvelist_length = 0; |
25aaa98a | 759 | OPENSSL_free(ss->tlsext_ellipticcurvelist); |
e481f9b9 | 760 | #endif /* OPENSSL_NO_EC */ |
ddac1974 | 761 | #ifndef OPENSSL_NO_PSK |
25aaa98a RS |
762 | OPENSSL_free(ss->psk_identity_hint); |
763 | OPENSSL_free(ss->psk_identity); | |
edc032b5 BL |
764 | #endif |
765 | #ifndef OPENSSL_NO_SRP | |
25aaa98a | 766 | OPENSSL_free(ss->srp_username); |
ed3883d2 | 767 | #endif |
16203f7b | 768 | CRYPTO_THREAD_lock_free(ss->lock); |
4b45c6e5 | 769 | OPENSSL_clear_free(ss, sizeof(*ss)); |
0f113f3e | 770 | } |
d02b48c6 | 771 | |
16203f7b AG |
772 | int SSL_SESSION_up_ref(SSL_SESSION *ss) |
773 | { | |
774 | int i; | |
775 | ||
776 | if (CRYPTO_atomic_add(&ss->references, 1, &i, ss->lock) <= 0) | |
777 | return 0; | |
778 | ||
779 | REF_PRINT_COUNT("SSL_SESSION", ss); | |
780 | REF_ASSERT_ISNT(i < 2); | |
781 | return ((i > 1) ? 1 : 0); | |
782 | } | |
783 | ||
6b691a5c | 784 | int SSL_set_session(SSL *s, SSL_SESSION *session) |
0f113f3e | 785 | { |
e70656cf MC |
786 | ssl_clear_bad_session(s); |
787 | if (s->ctx->method != s->method) { | |
788 | if (!SSL_set_ssl_method(s, s->ctx->method)) | |
789 | return 0; | |
790 | } | |
0f113f3e | 791 | |
e70656cf | 792 | if (session != NULL) { |
16203f7b | 793 | SSL_SESSION_up_ref(session); |
e70656cf | 794 | s->verify_result = session->verify_result; |
0f113f3e | 795 | } |
e70656cf MC |
796 | SSL_SESSION_free(s->session); |
797 | s->session = session; | |
798 | ||
799 | return 1; | |
0f113f3e | 800 | } |
d02b48c6 | 801 | |
fddfc0af RG |
802 | int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid, |
803 | unsigned int sid_len) | |
804 | { | |
805 | if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { | |
806 | SSLerr(SSL_F_SSL_SESSION_SET1_ID, | |
807 | SSL_R_SSL_SESSION_ID_TOO_LONG); | |
808 | return 0; | |
809 | } | |
810 | s->session_id_length = sid_len; | |
811 | memcpy(s->session_id, sid, sid_len); | |
812 | return 1; | |
813 | } | |
814 | ||
6b691a5c | 815 | long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) |
0f113f3e MC |
816 | { |
817 | if (s == NULL) | |
818 | return (0); | |
819 | s->timeout = t; | |
820 | return (1); | |
821 | } | |
d02b48c6 | 822 | |
0821bcd4 | 823 | long SSL_SESSION_get_timeout(const SSL_SESSION *s) |
0f113f3e MC |
824 | { |
825 | if (s == NULL) | |
826 | return (0); | |
827 | return (s->timeout); | |
828 | } | |
d02b48c6 | 829 | |
0821bcd4 | 830 | long SSL_SESSION_get_time(const SSL_SESSION *s) |
0f113f3e MC |
831 | { |
832 | if (s == NULL) | |
833 | return (0); | |
834 | return (s->time); | |
835 | } | |
d02b48c6 | 836 | |
6b691a5c | 837 | long SSL_SESSION_set_time(SSL_SESSION *s, long t) |
0f113f3e MC |
838 | { |
839 | if (s == NULL) | |
840 | return (0); | |
841 | s->time = t; | |
842 | return (t); | |
843 | } | |
d02b48c6 | 844 | |
bd01f649 TS |
845 | int SSL_SESSION_get_protocol_version(const SSL_SESSION *s) |
846 | { | |
bd01f649 TS |
847 | return s->ssl_version; |
848 | } | |
849 | ||
e9281323 RS |
850 | const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s) |
851 | { | |
852 | return s->cipher; | |
853 | } | |
854 | ||
4b6b8487 LC |
855 | const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s) |
856 | { | |
857 | return s->tlsext_hostname; | |
858 | } | |
859 | ||
f2baac27 MC |
860 | int SSL_SESSION_has_ticket(const SSL_SESSION *s) |
861 | { | |
862 | return (s->tlsext_ticklen > 0) ? 1 : 0; | |
863 | } | |
864 | ||
865 | unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s) | |
866 | { | |
867 | return s->tlsext_tick_lifetime_hint; | |
868 | } | |
869 | ||
48593cb1 | 870 | void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick, |
b7c9187b MC |
871 | size_t *len) |
872 | { | |
873 | *len = s->tlsext_ticklen; | |
61986d32 | 874 | if (tick != NULL) |
b7c9187b MC |
875 | *tick = s->tlsext_tick; |
876 | } | |
877 | ||
08557cf2 | 878 | X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) |
0f113f3e MC |
879 | { |
880 | return s->peer; | |
881 | } | |
882 | ||
883 | int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx, | |
884 | unsigned int sid_ctx_len) | |
885 | { | |
886 | if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { | |
887 | SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT, | |
888 | SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); | |
889 | return 0; | |
890 | } | |
891 | s->sid_ctx_length = sid_ctx_len; | |
892 | memcpy(s->sid_ctx, sid_ctx, sid_ctx_len); | |
893 | ||
894 | return 1; | |
895 | } | |
08557cf2 | 896 | |
6b691a5c | 897 | long SSL_CTX_set_timeout(SSL_CTX *s, long t) |
0f113f3e MC |
898 | { |
899 | long l; | |
900 | if (s == NULL) | |
901 | return (0); | |
902 | l = s->session_timeout; | |
903 | s->session_timeout = t; | |
904 | return (l); | |
905 | } | |
413c4f45 | 906 | |
0821bcd4 | 907 | long SSL_CTX_get_timeout(const SSL_CTX *s) |
0f113f3e MC |
908 | { |
909 | if (s == NULL) | |
910 | return (0); | |
911 | return (s->session_timeout); | |
912 | } | |
413c4f45 | 913 | |
0f113f3e MC |
914 | int SSL_set_session_secret_cb(SSL *s, |
915 | int (*tls_session_secret_cb) (SSL *s, | |
916 | void *secret, | |
917 | int *secret_len, | |
918 | STACK_OF(SSL_CIPHER) | |
919 | *peer_ciphers, | |
4a640fb6 | 920 | const SSL_CIPHER |
0f113f3e MC |
921 | **cipher, |
922 | void *arg), | |
923 | void *arg) | |
924 | { | |
925 | if (s == NULL) | |
926 | return (0); | |
927 | s->tls_session_secret_cb = tls_session_secret_cb; | |
928 | s->tls_session_secret_cb_arg = arg; | |
929 | return (1); | |
930 | } | |
12bf56c0 DSH |
931 | |
932 | int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb, | |
0f113f3e MC |
933 | void *arg) |
934 | { | |
935 | if (s == NULL) | |
936 | return (0); | |
937 | s->tls_session_ticket_ext_cb = cb; | |
938 | s->tls_session_ticket_ext_cb_arg = arg; | |
939 | return (1); | |
940 | } | |
12bf56c0 DSH |
941 | |
942 | int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len) | |
0f113f3e MC |
943 | { |
944 | if (s->version >= TLS1_VERSION) { | |
b548a1f1 RS |
945 | OPENSSL_free(s->tlsext_session_ticket); |
946 | s->tlsext_session_ticket = NULL; | |
0f113f3e MC |
947 | s->tlsext_session_ticket = |
948 | OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len); | |
a71edf3b | 949 | if (s->tlsext_session_ticket == NULL) { |
0f113f3e MC |
950 | SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE); |
951 | return 0; | |
952 | } | |
953 | ||
954 | if (ext_data) { | |
955 | s->tlsext_session_ticket->length = ext_len; | |
956 | s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1; | |
957 | memcpy(s->tlsext_session_ticket->data, ext_data, ext_len); | |
958 | } else { | |
959 | s->tlsext_session_ticket->length = 0; | |
960 | s->tlsext_session_ticket->data = NULL; | |
961 | } | |
962 | ||
963 | return 1; | |
964 | } | |
965 | ||
966 | return 0; | |
967 | } | |
0f113f3e MC |
968 | |
969 | typedef struct timeout_param_st { | |
970 | SSL_CTX *ctx; | |
971 | long time; | |
972 | LHASH_OF(SSL_SESSION) *cache; | |
973 | } TIMEOUT_PARAM; | |
d02b48c6 | 974 | |
2a056de8 | 975 | static void timeout_cb(SSL_SESSION *s, TIMEOUT_PARAM *p) |
0f113f3e MC |
976 | { |
977 | if ((p->time == 0) || (p->time > (s->time + s->timeout))) { /* timeout */ | |
978 | /* | |
979 | * The reason we don't call SSL_CTX_remove_session() is to save on | |
980 | * locking overhead | |
981 | */ | |
982 | (void)lh_SSL_SESSION_delete(p->cache, s); | |
983 | SSL_SESSION_list_remove(p->ctx, s); | |
984 | s->not_resumable = 1; | |
985 | if (p->ctx->remove_session_cb != NULL) | |
986 | p->ctx->remove_session_cb(p->ctx, s); | |
987 | SSL_SESSION_free(s); | |
988 | } | |
989 | } | |
d02b48c6 | 990 | |
2a056de8 | 991 | IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM); |
3c914840 | 992 | |
6b691a5c | 993 | void SSL_CTX_flush_sessions(SSL_CTX *s, long t) |
0f113f3e MC |
994 | { |
995 | unsigned long i; | |
996 | TIMEOUT_PARAM tp; | |
997 | ||
998 | tp.ctx = s; | |
999 | tp.cache = s->sessions; | |
1000 | if (tp.cache == NULL) | |
1001 | return; | |
1002 | tp.time = t; | |
16203f7b | 1003 | CRYPTO_THREAD_write_lock(s->lock); |
739a1eb1 RS |
1004 | i = lh_SSL_SESSION_get_down_load(s->sessions); |
1005 | lh_SSL_SESSION_set_down_load(s->sessions, 0); | |
2a056de8 | 1006 | lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp); |
739a1eb1 | 1007 | lh_SSL_SESSION_set_down_load(s->sessions, i); |
16203f7b | 1008 | CRYPTO_THREAD_unlock(s->lock); |
0f113f3e | 1009 | } |
d02b48c6 | 1010 | |
6b691a5c | 1011 | int ssl_clear_bad_session(SSL *s) |
0f113f3e MC |
1012 | { |
1013 | if ((s->session != NULL) && | |
1014 | !(s->shutdown & SSL_SENT_SHUTDOWN) && | |
1015 | !(SSL_in_init(s) || SSL_in_before(s))) { | |
e2bb9b9b | 1016 | SSL_CTX_remove_session(s->session_ctx, s->session); |
0f113f3e MC |
1017 | return (1); |
1018 | } else | |
1019 | return (0); | |
1020 | } | |
58964a49 RE |
1021 | |
1022 | /* locked by SSL_CTX in the calling function */ | |
6b691a5c | 1023 | static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) |
0f113f3e MC |
1024 | { |
1025 | if ((s->next == NULL) || (s->prev == NULL)) | |
1026 | return; | |
1027 | ||
1028 | if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) { | |
1029 | /* last element in list */ | |
1030 | if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) { | |
1031 | /* only one element in list */ | |
1032 | ctx->session_cache_head = NULL; | |
1033 | ctx->session_cache_tail = NULL; | |
1034 | } else { | |
1035 | ctx->session_cache_tail = s->prev; | |
1036 | s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail); | |
1037 | } | |
1038 | } else { | |
1039 | if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) { | |
1040 | /* first element in list */ | |
1041 | ctx->session_cache_head = s->next; | |
1042 | s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); | |
1043 | } else { | |
1044 | /* middle of list */ | |
1045 | s->next->prev = s->prev; | |
1046 | s->prev->next = s->next; | |
1047 | } | |
1048 | } | |
1049 | s->prev = s->next = NULL; | |
1050 | } | |
58964a49 | 1051 | |
6b691a5c | 1052 | static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) |
0f113f3e MC |
1053 | { |
1054 | if ((s->next != NULL) && (s->prev != NULL)) | |
1055 | SSL_SESSION_list_remove(ctx, s); | |
1056 | ||
1057 | if (ctx->session_cache_head == NULL) { | |
1058 | ctx->session_cache_head = s; | |
1059 | ctx->session_cache_tail = s; | |
1060 | s->prev = (SSL_SESSION *)&(ctx->session_cache_head); | |
1061 | s->next = (SSL_SESSION *)&(ctx->session_cache_tail); | |
1062 | } else { | |
1063 | s->next = ctx->session_cache_head; | |
1064 | s->next->prev = s; | |
1065 | s->prev = (SSL_SESSION *)&(ctx->session_cache_head); | |
1066 | ctx->session_cache_head = s; | |
1067 | } | |
1068 | } | |
58964a49 | 1069 | |
7806f3dd | 1070 | void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, |
0f113f3e MC |
1071 | int (*cb) (struct ssl_st *ssl, |
1072 | SSL_SESSION *sess)) | |
1073 | { | |
1074 | ctx->new_session_cb = cb; | |
1075 | } | |
7806f3dd | 1076 | |
0f113f3e MC |
1077 | int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) { |
1078 | return ctx->new_session_cb; | |
1079 | } | |
7806f3dd NL |
1080 | |
1081 | void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx, | |
0f113f3e MC |
1082 | void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess)) |
1083 | { | |
1084 | ctx->remove_session_cb = cb; | |
1085 | } | |
7806f3dd | 1086 | |
0f113f3e MC |
1087 | void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx, |
1088 | SSL_SESSION *sess) { | |
1089 | return ctx->remove_session_cb; | |
1090 | } | |
7806f3dd NL |
1091 | |
1092 | void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, | |
0f113f3e | 1093 | SSL_SESSION *(*cb) (struct ssl_st *ssl, |
b6981744 EK |
1094 | const unsigned char *data, |
1095 | int len, int *copy)) | |
0f113f3e MC |
1096 | { |
1097 | ctx->get_session_cb = cb; | |
1098 | } | |
1099 | ||
1100 | SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl, | |
b6981744 | 1101 | const unsigned char *data, |
0f113f3e MC |
1102 | int len, int *copy) { |
1103 | return ctx->get_session_cb; | |
1104 | } | |
1105 | ||
1106 | void SSL_CTX_set_info_callback(SSL_CTX *ctx, | |
1107 | void (*cb) (const SSL *ssl, int type, int val)) | |
1108 | { | |
1109 | ctx->info_callback = cb; | |
1110 | } | |
1111 | ||
1112 | void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type, | |
1113 | int val) { | |
1114 | return ctx->info_callback; | |
1115 | } | |
7806f3dd NL |
1116 | |
1117 | void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, | |
0f113f3e MC |
1118 | int (*cb) (SSL *ssl, X509 **x509, |
1119 | EVP_PKEY **pkey)) | |
1120 | { | |
1121 | ctx->client_cert_cb = cb; | |
1122 | } | |
7806f3dd | 1123 | |
0f113f3e MC |
1124 | int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509, |
1125 | EVP_PKEY **pkey) { | |
1126 | return ctx->client_cert_cb; | |
1127 | } | |
7806f3dd | 1128 | |
368888bc DSH |
1129 | #ifndef OPENSSL_NO_ENGINE |
1130 | int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e) | |
0f113f3e MC |
1131 | { |
1132 | if (!ENGINE_init(e)) { | |
1133 | SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB); | |
1134 | return 0; | |
1135 | } | |
1136 | if (!ENGINE_get_ssl_client_cert_function(e)) { | |
1137 | SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, | |
1138 | SSL_R_NO_CLIENT_CERT_METHOD); | |
1139 | ENGINE_finish(e); | |
1140 | return 0; | |
1141 | } | |
1142 | ctx->client_cert_engine = e; | |
1143 | return 1; | |
1144 | } | |
368888bc DSH |
1145 | #endif |
1146 | ||
7806f3dd | 1147 | void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx, |
0f113f3e MC |
1148 | int (*cb) (SSL *ssl, |
1149 | unsigned char *cookie, | |
1150 | unsigned int *cookie_len)) | |
1151 | { | |
1152 | ctx->app_gen_cookie_cb = cb; | |
1153 | } | |
7806f3dd NL |
1154 | |
1155 | void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx, | |
31011544 | 1156 | int (*cb) (SSL *ssl, const unsigned char *cookie, |
0f113f3e MC |
1157 | unsigned int cookie_len)) |
1158 | { | |
1159 | ctx->app_verify_cookie_cb = cb; | |
1160 | } | |
7806f3dd | 1161 | |
0f113f3e MC |
1162 | IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, |
1163 | SSL_SESSION) |