]>
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 | } | |
4879ec7b | 243 | |
f9b0b452 | 244 | unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s) |
0f113f3e MC |
245 | { |
246 | return s->compress_meth; | |
247 | } | |
248 | ||
249 | /* | |
250 | * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling | |
251 | * the ID with random junk repeatedly until we have no conflict is going to | |
252 | * complete in one iteration pretty much "most" of the time (btw: | |
253 | * understatement). So, if it takes us 10 iterations and we still can't avoid | |
254 | * a conflict - well that's a reasonable point to call it quits. Either the | |
255 | * RAND code is broken or someone is trying to open roughly very close to | |
256 | * 2^256 SSL sessions to our server. How you might store that many sessions | |
257 | * is perhaps a more interesting question ... | |
258 | */ | |
dc644fe2 GT |
259 | |
260 | #define MAX_SESS_ID_ATTEMPTS 10 | |
261 | static int def_generate_session_id(const SSL *ssl, unsigned char *id, | |
0f113f3e | 262 | unsigned int *id_len) |
dc644fe2 | 263 | { |
0f113f3e MC |
264 | unsigned int retry = 0; |
265 | do | |
266483d2 | 266 | if (RAND_bytes(id, *id_len) <= 0) |
0f113f3e MC |
267 | return 0; |
268 | while (SSL_has_matching_session_id(ssl, id, *id_len) && | |
269 | (++retry < MAX_SESS_ID_ATTEMPTS)) ; | |
270 | if (retry < MAX_SESS_ID_ATTEMPTS) | |
271 | return 1; | |
272 | /* else - woops a session_id match */ | |
273 | /* | |
274 | * XXX We should also check the external cache -- but the probability of | |
275 | * a collision is negligible, and we could not prevent the concurrent | |
276 | * creation of sessions with identical IDs since we currently don't have | |
277 | * means to atomically check whether a session ID already exists and make | |
278 | * a reservation for it if it does not (this problem applies to the | |
279 | * internal cache as well). | |
280 | */ | |
281 | return 0; | |
dc644fe2 GT |
282 | } |
283 | ||
6b691a5c | 284 | int ssl_get_new_session(SSL *s, int session) |
0f113f3e MC |
285 | { |
286 | /* This gets used by clients and servers. */ | |
287 | ||
288 | unsigned int tmp; | |
289 | SSL_SESSION *ss = NULL; | |
290 | GEN_SESSION_CB cb = def_generate_session_id; | |
291 | ||
292 | if ((ss = SSL_SESSION_new()) == NULL) | |
293 | return (0); | |
294 | ||
295 | /* If the context has a default timeout, use it */ | |
296 | if (s->session_ctx->session_timeout == 0) | |
297 | ss->timeout = SSL_get_default_timeout(s); | |
298 | else | |
299 | ss->timeout = s->session_ctx->session_timeout; | |
300 | ||
62adbcee RS |
301 | SSL_SESSION_free(s->session); |
302 | s->session = NULL; | |
0f113f3e MC |
303 | |
304 | if (session) { | |
305 | if (s->version == SSL3_VERSION) { | |
306 | ss->ssl_version = SSL3_VERSION; | |
307 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
308 | } else if (s->version == TLS1_VERSION) { | |
309 | ss->ssl_version = TLS1_VERSION; | |
310 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
311 | } else if (s->version == TLS1_1_VERSION) { | |
312 | ss->ssl_version = TLS1_1_VERSION; | |
313 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
314 | } else if (s->version == TLS1_2_VERSION) { | |
315 | ss->ssl_version = TLS1_2_VERSION; | |
316 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
317 | } else if (s->version == DTLS1_BAD_VER) { | |
318 | ss->ssl_version = DTLS1_BAD_VER; | |
319 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
320 | } else if (s->version == DTLS1_VERSION) { | |
321 | ss->ssl_version = DTLS1_VERSION; | |
322 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
323 | } else if (s->version == DTLS1_2_VERSION) { | |
324 | ss->ssl_version = DTLS1_2_VERSION; | |
325 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; | |
326 | } else { | |
327 | SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION); | |
328 | SSL_SESSION_free(ss); | |
329 | return (0); | |
330 | } | |
e481f9b9 | 331 | |
35a1cc90 MC |
332 | /*- |
333 | * If RFC5077 ticket, use empty session ID (as server). | |
334 | * Note that: | |
335 | * (a) ssl_get_prev_session() does lookahead into the | |
336 | * ClientHello extensions to find the session ticket. | |
d4d78943 MC |
337 | * When ssl_get_prev_session() fails, statem_srvr.c calls |
338 | * ssl_get_new_session() in tls_process_client_hello(). | |
35a1cc90 MC |
339 | * At that point, it has not yet parsed the extensions, |
340 | * however, because of the lookahead, it already knows | |
341 | * whether a ticket is expected or not. | |
342 | * | |
d4d78943 | 343 | * (b) statem_clnt.c calls ssl_get_new_session() before parsing |
35a1cc90 MC |
344 | * ServerHello extensions, and before recording the session |
345 | * ID received from the server, so this block is a noop. | |
346 | */ | |
0f113f3e MC |
347 | if (s->tlsext_ticket_expected) { |
348 | ss->session_id_length = 0; | |
349 | goto sess_id_done; | |
350 | } | |
e481f9b9 | 351 | |
0f113f3e | 352 | /* Choose which callback will set the session ID */ |
16203f7b AG |
353 | CRYPTO_THREAD_read_lock(s->lock); |
354 | CRYPTO_THREAD_read_lock(s->session_ctx->lock); | |
0f113f3e MC |
355 | if (s->generate_session_id) |
356 | cb = s->generate_session_id; | |
357 | else if (s->session_ctx->generate_session_id) | |
358 | cb = s->session_ctx->generate_session_id; | |
16203f7b AG |
359 | CRYPTO_THREAD_unlock(s->session_ctx->lock); |
360 | CRYPTO_THREAD_unlock(s->lock); | |
0f113f3e | 361 | /* Choose a session ID */ |
947f3156 | 362 | memset(ss->session_id, 0, ss->session_id_length); |
0f113f3e MC |
363 | tmp = ss->session_id_length; |
364 | if (!cb(s, ss->session_id, &tmp)) { | |
365 | /* The callback failed */ | |
366 | SSLerr(SSL_F_SSL_GET_NEW_SESSION, | |
367 | SSL_R_SSL_SESSION_ID_CALLBACK_FAILED); | |
368 | SSL_SESSION_free(ss); | |
369 | return (0); | |
370 | } | |
371 | /* | |
372 | * Don't allow the callback to set the session length to zero. nor | |
373 | * set it higher than it was. | |
374 | */ | |
cc99bfa7 | 375 | if (tmp == 0 || tmp > ss->session_id_length) { |
0f113f3e MC |
376 | /* The callback set an illegal length */ |
377 | SSLerr(SSL_F_SSL_GET_NEW_SESSION, | |
378 | SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH); | |
379 | SSL_SESSION_free(ss); | |
380 | return (0); | |
381 | } | |
382 | ss->session_id_length = tmp; | |
383 | /* Finally, check for a conflict */ | |
384 | if (SSL_has_matching_session_id(s, ss->session_id, | |
385 | ss->session_id_length)) { | |
386 | SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_SSL_SESSION_ID_CONFLICT); | |
387 | SSL_SESSION_free(ss); | |
388 | return (0); | |
389 | } | |
e481f9b9 | 390 | |
0f113f3e MC |
391 | sess_id_done: |
392 | if (s->tlsext_hostname) { | |
7644a9ae | 393 | ss->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname); |
0f113f3e MC |
394 | if (ss->tlsext_hostname == NULL) { |
395 | SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR); | |
396 | SSL_SESSION_free(ss); | |
397 | return 0; | |
398 | } | |
399 | } | |
0f113f3e MC |
400 | } else { |
401 | ss->session_id_length = 0; | |
402 | } | |
403 | ||
404 | if (s->sid_ctx_length > sizeof ss->sid_ctx) { | |
405 | SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR); | |
406 | SSL_SESSION_free(ss); | |
407 | return 0; | |
408 | } | |
409 | memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length); | |
410 | ss->sid_ctx_length = s->sid_ctx_length; | |
411 | s->session = ss; | |
412 | ss->ssl_version = s->version; | |
413 | ss->verify_result = X509_V_OK; | |
414 | ||
e7f0d921 DSH |
415 | /* If client supports extended master secret set it in session */ |
416 | if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) | |
417 | ss->flags |= SSL_SESS_FLAG_EXTMS; | |
418 | ||
0f113f3e MC |
419 | return (1); |
420 | } | |
d02b48c6 | 421 | |
3a83462d MC |
422 | /*- |
423 | * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this | |
c519e89f BM |
424 | * connection. It is only called by servers. |
425 | * | |
b3e2272c EK |
426 | * ext: ClientHello extensions (including length prefix) |
427 | * session_id: ClientHello session ID. | |
c519e89f BM |
428 | * |
429 | * Returns: | |
430 | * -1: error | |
431 | * 0: a session may have been found. | |
432 | * | |
433 | * Side effects: | |
434 | * - If a session is found then s->session is pointed at it (after freeing an | |
435 | * existing session if need be) and s->verify_result is set from the session. | |
436 | * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1 | |
437 | * if the server should issue a new session ticket (to 0 otherwise). | |
438 | */ | |
b3e2272c | 439 | int ssl_get_prev_session(SSL *s, const PACKET *ext, const PACKET *session_id) |
0f113f3e MC |
440 | { |
441 | /* This is used only by servers. */ | |
b56bce4f | 442 | |
0f113f3e MC |
443 | SSL_SESSION *ret = NULL; |
444 | int fatal = 0; | |
445 | int try_session_cache = 1; | |
0f113f3e | 446 | int r; |
d02b48c6 | 447 | |
293b5ca4 | 448 | if (PACKET_remaining(session_id) == 0) |
0f113f3e | 449 | try_session_cache = 0; |
c519e89f | 450 | |
e7f0d921 DSH |
451 | /* sets s->tlsext_ticket_expected and extended master secret flag */ |
452 | r = tls_check_serverhello_tlsext_early(s, ext, session_id, &ret); | |
0f113f3e MC |
453 | switch (r) { |
454 | case -1: /* Error during processing */ | |
455 | fatal = 1; | |
456 | goto err; | |
457 | case 0: /* No ticket found */ | |
458 | case 1: /* Zero length ticket found */ | |
459 | break; /* Ok to carry on processing session id. */ | |
460 | case 2: /* Ticket found but not decrypted. */ | |
461 | case 3: /* Ticket decrypted, *ret has been set. */ | |
462 | try_session_cache = 0; | |
463 | break; | |
464 | default: | |
465 | abort(); | |
466 | } | |
c519e89f | 467 | |
0f113f3e MC |
468 | if (try_session_cache && |
469 | ret == NULL && | |
739a5eee | 470 | !(s->session_ctx->session_cache_mode & |
0f113f3e MC |
471 | SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) { |
472 | SSL_SESSION data; | |
67202973 | 473 | size_t local_len; |
0f113f3e | 474 | data.ssl_version = s->version; |
947f3156 | 475 | memset(data.session_id, 0, sizeof(data.session_id)); |
67202973 EK |
476 | if (!PACKET_copy_all(session_id, data.session_id, |
477 | sizeof(data.session_id), | |
478 | &local_len)) { | |
479 | goto err; | |
480 | } | |
481 | data.session_id_length = local_len; | |
16203f7b | 482 | CRYPTO_THREAD_read_lock(s->session_ctx->lock); |
0f113f3e MC |
483 | ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data); |
484 | if (ret != NULL) { | |
485 | /* don't allow other threads to steal it: */ | |
16203f7b | 486 | SSL_SESSION_up_ref(ret); |
0f113f3e | 487 | } |
16203f7b | 488 | CRYPTO_THREAD_unlock(s->session_ctx->lock); |
0f113f3e MC |
489 | if (ret == NULL) |
490 | s->session_ctx->stats.sess_miss++; | |
491 | } | |
492 | ||
493 | if (try_session_cache && | |
494 | ret == NULL && s->session_ctx->get_session_cb != NULL) { | |
495 | int copy = 1; | |
b6981744 EK |
496 | ret = s->session_ctx->get_session_cb(s, PACKET_data(session_id), |
497 | PACKET_remaining(session_id), | |
498 | ©); | |
0f113f3e | 499 | |
bf0fc412 | 500 | if (ret != NULL) { |
0f113f3e MC |
501 | s->session_ctx->stats.sess_cb_hit++; |
502 | ||
503 | /* | |
504 | * Increment reference count now if the session callback asks us | |
505 | * to do so (note that if the session structures returned by the | |
506 | * callback are shared between threads, it must handle the | |
507 | * reference count itself [i.e. copy == 0], or things won't be | |
508 | * thread-safe). | |
509 | */ | |
510 | if (copy) | |
16203f7b | 511 | SSL_SESSION_up_ref(ret); |
0f113f3e MC |
512 | |
513 | /* | |
514 | * Add the externally cached session to the internal cache as | |
515 | * well if and only if we are supposed to. | |
516 | */ | |
517 | if (! | |
739a5eee | 518 | (s->session_ctx->session_cache_mode & |
69f68237 | 519 | SSL_SESS_CACHE_NO_INTERNAL_STORE)) { |
0f113f3e MC |
520 | /* |
521 | * The following should not return 1, otherwise, things are | |
522 | * very strange | |
523 | */ | |
61986d32 | 524 | if (SSL_CTX_add_session(s->session_ctx, ret)) |
69f68237 MC |
525 | goto err; |
526 | } | |
0f113f3e MC |
527 | } |
528 | } | |
529 | ||
530 | if (ret == NULL) | |
531 | goto err; | |
532 | ||
533 | /* Now ret is non-NULL and we own one of its reference counts. */ | |
534 | ||
535 | if (ret->sid_ctx_length != s->sid_ctx_length | |
536 | || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) { | |
537 | /* | |
538 | * We have the session requested by the client, but we don't want to | |
539 | * use it in this context. | |
540 | */ | |
541 | goto err; /* treat like cache miss */ | |
542 | } | |
543 | ||
544 | if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) { | |
545 | /* | |
546 | * We can't be sure if this session is being used out of context, | |
547 | * which is especially important for SSL_VERIFY_PEER. The application | |
548 | * should have used SSL[_CTX]_set_session_id_context. For this error | |
549 | * case, we generate an error instead of treating the event like a | |
550 | * cache miss (otherwise it would be easy for applications to | |
551 | * effectively disable the session cache by accident without anyone | |
552 | * noticing). | |
553 | */ | |
554 | ||
555 | SSLerr(SSL_F_SSL_GET_PREV_SESSION, | |
556 | SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); | |
557 | fatal = 1; | |
558 | goto err; | |
559 | } | |
560 | ||
561 | if (ret->cipher == NULL) { | |
562 | unsigned char buf[5], *p; | |
563 | unsigned long l; | |
564 | ||
565 | p = buf; | |
566 | l = ret->cipher_id; | |
567 | l2n(l, p); | |
568 | if ((ret->ssl_version >> 8) >= SSL3_VERSION_MAJOR) | |
569 | ret->cipher = ssl_get_cipher_by_char(s, &(buf[2])); | |
570 | else | |
571 | ret->cipher = ssl_get_cipher_by_char(s, &(buf[1])); | |
572 | if (ret->cipher == NULL) | |
573 | goto err; | |
574 | } | |
575 | ||
576 | if (ret->timeout < (long)(time(NULL) - ret->time)) { /* timeout */ | |
577 | s->session_ctx->stats.sess_timeout++; | |
578 | if (try_session_cache) { | |
579 | /* session was from the cache, so remove it */ | |
580 | SSL_CTX_remove_session(s->session_ctx, ret); | |
581 | } | |
582 | goto err; | |
583 | } | |
584 | ||
e7f0d921 DSH |
585 | /* Check extended master secret extension consistency */ |
586 | if (ret->flags & SSL_SESS_FLAG_EXTMS) { | |
587 | /* If old session includes extms, but new does not: abort handshake */ | |
588 | if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)) { | |
589 | SSLerr(SSL_F_SSL_GET_PREV_SESSION, SSL_R_INCONSISTENT_EXTMS); | |
590 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | |
591 | fatal = 1; | |
592 | goto err; | |
593 | } | |
594 | } else if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) { | |
595 | /* If new session includes extms, but old does not: do not resume */ | |
596 | goto err; | |
597 | } | |
598 | ||
0f113f3e MC |
599 | s->session_ctx->stats.sess_hit++; |
600 | ||
62adbcee | 601 | SSL_SESSION_free(s->session); |
0f113f3e MC |
602 | s->session = ret; |
603 | s->verify_result = s->session->verify_result; | |
604 | return 1; | |
8876bc05 BM |
605 | |
606 | err: | |
0f113f3e MC |
607 | if (ret != NULL) { |
608 | SSL_SESSION_free(ret); | |
e481f9b9 | 609 | |
0f113f3e MC |
610 | if (!try_session_cache) { |
611 | /* | |
612 | * The session was from a ticket, so we should issue a ticket for | |
613 | * the new session | |
614 | */ | |
615 | s->tlsext_ticket_expected = 1; | |
616 | } | |
0f113f3e MC |
617 | } |
618 | if (fatal) | |
619 | return -1; | |
620 | else | |
621 | return 0; | |
622 | } | |
d02b48c6 | 623 | |
6b691a5c | 624 | int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) |
0f113f3e MC |
625 | { |
626 | int ret = 0; | |
627 | SSL_SESSION *s; | |
628 | ||
629 | /* | |
630 | * add just 1 reference count for the SSL_CTX's session cache even though | |
631 | * it has two ways of access: each session is in a doubly linked list and | |
632 | * an lhash | |
633 | */ | |
16203f7b | 634 | SSL_SESSION_up_ref(c); |
0f113f3e MC |
635 | /* |
636 | * if session c is in already in cache, we take back the increment later | |
637 | */ | |
638 | ||
16203f7b | 639 | CRYPTO_THREAD_write_lock(ctx->lock); |
0f113f3e MC |
640 | s = lh_SSL_SESSION_insert(ctx->sessions, c); |
641 | ||
642 | /* | |
643 | * s != NULL iff we already had a session with the given PID. In this | |
644 | * case, s == c should hold (then we did not really modify | |
645 | * ctx->sessions), or we're in trouble. | |
646 | */ | |
647 | if (s != NULL && s != c) { | |
648 | /* We *are* in trouble ... */ | |
649 | SSL_SESSION_list_remove(ctx, s); | |
650 | SSL_SESSION_free(s); | |
651 | /* | |
652 | * ... so pretend the other session did not exist in cache (we cannot | |
653 | * handle two SSL_SESSION structures with identical session ID in the | |
654 | * same cache, which could happen e.g. when two threads concurrently | |
655 | * obtain the same session from an external cache) | |
656 | */ | |
657 | s = NULL; | |
658 | } | |
659 | ||
660 | /* Put at the head of the queue unless it is already in the cache */ | |
661 | if (s == NULL) | |
662 | SSL_SESSION_list_add(ctx, c); | |
663 | ||
664 | if (s != NULL) { | |
665 | /* | |
666 | * existing cache entry -- decrement previously incremented reference | |
667 | * count because it already takes into account the cache | |
668 | */ | |
669 | ||
670 | SSL_SESSION_free(s); /* s == c */ | |
671 | ret = 0; | |
672 | } else { | |
673 | /* | |
674 | * new cache entry -- remove old ones if cache has become too large | |
675 | */ | |
676 | ||
677 | ret = 1; | |
678 | ||
679 | if (SSL_CTX_sess_get_cache_size(ctx) > 0) { | |
680 | while (SSL_CTX_sess_number(ctx) > | |
681 | SSL_CTX_sess_get_cache_size(ctx)) { | |
682 | if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) | |
683 | break; | |
684 | else | |
685 | ctx->stats.sess_cache_full++; | |
686 | } | |
687 | } | |
688 | } | |
16203f7b AG |
689 | CRYPTO_THREAD_unlock(ctx->lock); |
690 | return ret; | |
0f113f3e | 691 | } |
d02b48c6 | 692 | |
6b691a5c | 693 | int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) |
801294f8 | 694 | { |
0f113f3e | 695 | return remove_session_lock(ctx, c, 1); |
801294f8 DSH |
696 | } |
697 | ||
0fda2e37 | 698 | static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck) |
0f113f3e MC |
699 | { |
700 | SSL_SESSION *r; | |
701 | int ret = 0; | |
702 | ||
703 | if ((c != NULL) && (c->session_id_length != 0)) { | |
704 | if (lck) | |
16203f7b | 705 | CRYPTO_THREAD_write_lock(ctx->lock); |
0f113f3e MC |
706 | if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) { |
707 | ret = 1; | |
708 | r = lh_SSL_SESSION_delete(ctx->sessions, c); | |
709 | SSL_SESSION_list_remove(ctx, c); | |
710 | } | |
e4612d02 | 711 | c->not_resumable = 1; |
0f113f3e MC |
712 | |
713 | if (lck) | |
16203f7b | 714 | CRYPTO_THREAD_unlock(ctx->lock); |
0f113f3e | 715 | |
e4612d02 | 716 | if (ret) |
0f113f3e | 717 | SSL_SESSION_free(r); |
e4612d02 MC |
718 | |
719 | if (ctx->remove_session_cb != NULL) | |
720 | ctx->remove_session_cb(ctx, c); | |
0f113f3e MC |
721 | } else |
722 | ret = 0; | |
723 | return (ret); | |
724 | } | |
d02b48c6 | 725 | |
6b691a5c | 726 | void SSL_SESSION_free(SSL_SESSION *ss) |
0f113f3e MC |
727 | { |
728 | int i; | |
d02b48c6 | 729 | |
0f113f3e MC |
730 | if (ss == NULL) |
731 | return; | |
e03ddfae | 732 | |
16203f7b | 733 | CRYPTO_atomic_add(&ss->references, -1, &i, ss->lock); |
f3f1cf84 | 734 | REF_PRINT_COUNT("SSL_SESSION", ss); |
0f113f3e MC |
735 | if (i > 0) |
736 | return; | |
f3f1cf84 | 737 | REF_ASSERT_ISNT(i < 0); |
d02b48c6 | 738 | |
0f113f3e | 739 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); |
58964a49 | 740 | |
0f113f3e MC |
741 | OPENSSL_cleanse(ss->master_key, sizeof ss->master_key); |
742 | OPENSSL_cleanse(ss->session_id, sizeof ss->session_id); | |
222561fe | 743 | X509_free(ss->peer); |
c34b0f99 | 744 | sk_X509_pop_free(ss->peer_chain, X509_free); |
25aaa98a | 745 | sk_SSL_CIPHER_free(ss->ciphers); |
25aaa98a RS |
746 | OPENSSL_free(ss->tlsext_hostname); |
747 | OPENSSL_free(ss->tlsext_tick); | |
e481f9b9 | 748 | #ifndef OPENSSL_NO_EC |
0f113f3e | 749 | ss->tlsext_ecpointformatlist_length = 0; |
25aaa98a | 750 | OPENSSL_free(ss->tlsext_ecpointformatlist); |
0f113f3e | 751 | ss->tlsext_ellipticcurvelist_length = 0; |
25aaa98a | 752 | OPENSSL_free(ss->tlsext_ellipticcurvelist); |
e481f9b9 | 753 | #endif /* OPENSSL_NO_EC */ |
ddac1974 | 754 | #ifndef OPENSSL_NO_PSK |
25aaa98a RS |
755 | OPENSSL_free(ss->psk_identity_hint); |
756 | OPENSSL_free(ss->psk_identity); | |
edc032b5 BL |
757 | #endif |
758 | #ifndef OPENSSL_NO_SRP | |
25aaa98a | 759 | OPENSSL_free(ss->srp_username); |
ed3883d2 | 760 | #endif |
16203f7b | 761 | CRYPTO_THREAD_lock_free(ss->lock); |
4b45c6e5 | 762 | OPENSSL_clear_free(ss, sizeof(*ss)); |
0f113f3e | 763 | } |
d02b48c6 | 764 | |
16203f7b AG |
765 | int SSL_SESSION_up_ref(SSL_SESSION *ss) |
766 | { | |
767 | int i; | |
768 | ||
769 | if (CRYPTO_atomic_add(&ss->references, 1, &i, ss->lock) <= 0) | |
770 | return 0; | |
771 | ||
772 | REF_PRINT_COUNT("SSL_SESSION", ss); | |
773 | REF_ASSERT_ISNT(i < 2); | |
774 | return ((i > 1) ? 1 : 0); | |
775 | } | |
776 | ||
6b691a5c | 777 | int SSL_set_session(SSL *s, SSL_SESSION *session) |
0f113f3e | 778 | { |
e70656cf MC |
779 | ssl_clear_bad_session(s); |
780 | if (s->ctx->method != s->method) { | |
781 | if (!SSL_set_ssl_method(s, s->ctx->method)) | |
782 | return 0; | |
783 | } | |
0f113f3e | 784 | |
e70656cf | 785 | if (session != NULL) { |
16203f7b | 786 | SSL_SESSION_up_ref(session); |
e70656cf | 787 | s->verify_result = session->verify_result; |
0f113f3e | 788 | } |
e70656cf MC |
789 | SSL_SESSION_free(s->session); |
790 | s->session = session; | |
791 | ||
792 | return 1; | |
0f113f3e | 793 | } |
d02b48c6 | 794 | |
6b691a5c | 795 | long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) |
0f113f3e MC |
796 | { |
797 | if (s == NULL) | |
798 | return (0); | |
799 | s->timeout = t; | |
800 | return (1); | |
801 | } | |
d02b48c6 | 802 | |
0821bcd4 | 803 | long SSL_SESSION_get_timeout(const SSL_SESSION *s) |
0f113f3e MC |
804 | { |
805 | if (s == NULL) | |
806 | return (0); | |
807 | return (s->timeout); | |
808 | } | |
d02b48c6 | 809 | |
0821bcd4 | 810 | long SSL_SESSION_get_time(const SSL_SESSION *s) |
0f113f3e MC |
811 | { |
812 | if (s == NULL) | |
813 | return (0); | |
814 | return (s->time); | |
815 | } | |
d02b48c6 | 816 | |
6b691a5c | 817 | long SSL_SESSION_set_time(SSL_SESSION *s, long t) |
0f113f3e MC |
818 | { |
819 | if (s == NULL) | |
820 | return (0); | |
821 | s->time = t; | |
822 | return (t); | |
823 | } | |
d02b48c6 | 824 | |
bd01f649 TS |
825 | int SSL_SESSION_get_protocol_version(const SSL_SESSION *s) |
826 | { | |
bd01f649 TS |
827 | return s->ssl_version; |
828 | } | |
829 | ||
e9281323 RS |
830 | const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s) |
831 | { | |
832 | return s->cipher; | |
833 | } | |
834 | ||
4b6b8487 LC |
835 | const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s) |
836 | { | |
837 | return s->tlsext_hostname; | |
838 | } | |
839 | ||
f2baac27 MC |
840 | int SSL_SESSION_has_ticket(const SSL_SESSION *s) |
841 | { | |
842 | return (s->tlsext_ticklen > 0) ? 1 : 0; | |
843 | } | |
844 | ||
845 | unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s) | |
846 | { | |
847 | return s->tlsext_tick_lifetime_hint; | |
848 | } | |
849 | ||
b7c9187b MC |
850 | void SSL_SESSION_get0_ticket(const SSL_SESSION *s, unsigned char **tick, |
851 | size_t *len) | |
852 | { | |
853 | *len = s->tlsext_ticklen; | |
61986d32 | 854 | if (tick != NULL) |
b7c9187b MC |
855 | *tick = s->tlsext_tick; |
856 | } | |
857 | ||
08557cf2 | 858 | X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) |
0f113f3e MC |
859 | { |
860 | return s->peer; | |
861 | } | |
862 | ||
863 | int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx, | |
864 | unsigned int sid_ctx_len) | |
865 | { | |
866 | if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { | |
867 | SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT, | |
868 | SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); | |
869 | return 0; | |
870 | } | |
871 | s->sid_ctx_length = sid_ctx_len; | |
872 | memcpy(s->sid_ctx, sid_ctx, sid_ctx_len); | |
873 | ||
874 | return 1; | |
875 | } | |
08557cf2 | 876 | |
6b691a5c | 877 | long SSL_CTX_set_timeout(SSL_CTX *s, long t) |
0f113f3e MC |
878 | { |
879 | long l; | |
880 | if (s == NULL) | |
881 | return (0); | |
882 | l = s->session_timeout; | |
883 | s->session_timeout = t; | |
884 | return (l); | |
885 | } | |
413c4f45 | 886 | |
0821bcd4 | 887 | long SSL_CTX_get_timeout(const SSL_CTX *s) |
0f113f3e MC |
888 | { |
889 | if (s == NULL) | |
890 | return (0); | |
891 | return (s->session_timeout); | |
892 | } | |
413c4f45 | 893 | |
0f113f3e MC |
894 | int SSL_set_session_secret_cb(SSL *s, |
895 | int (*tls_session_secret_cb) (SSL *s, | |
896 | void *secret, | |
897 | int *secret_len, | |
898 | STACK_OF(SSL_CIPHER) | |
899 | *peer_ciphers, | |
4a640fb6 | 900 | const SSL_CIPHER |
0f113f3e MC |
901 | **cipher, |
902 | void *arg), | |
903 | void *arg) | |
904 | { | |
905 | if (s == NULL) | |
906 | return (0); | |
907 | s->tls_session_secret_cb = tls_session_secret_cb; | |
908 | s->tls_session_secret_cb_arg = arg; | |
909 | return (1); | |
910 | } | |
12bf56c0 DSH |
911 | |
912 | int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb, | |
0f113f3e MC |
913 | void *arg) |
914 | { | |
915 | if (s == NULL) | |
916 | return (0); | |
917 | s->tls_session_ticket_ext_cb = cb; | |
918 | s->tls_session_ticket_ext_cb_arg = arg; | |
919 | return (1); | |
920 | } | |
12bf56c0 DSH |
921 | |
922 | int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len) | |
0f113f3e MC |
923 | { |
924 | if (s->version >= TLS1_VERSION) { | |
b548a1f1 RS |
925 | OPENSSL_free(s->tlsext_session_ticket); |
926 | s->tlsext_session_ticket = NULL; | |
0f113f3e MC |
927 | s->tlsext_session_ticket = |
928 | OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len); | |
a71edf3b | 929 | if (s->tlsext_session_ticket == NULL) { |
0f113f3e MC |
930 | SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE); |
931 | return 0; | |
932 | } | |
933 | ||
934 | if (ext_data) { | |
935 | s->tlsext_session_ticket->length = ext_len; | |
936 | s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1; | |
937 | memcpy(s->tlsext_session_ticket->data, ext_data, ext_len); | |
938 | } else { | |
939 | s->tlsext_session_ticket->length = 0; | |
940 | s->tlsext_session_ticket->data = NULL; | |
941 | } | |
942 | ||
943 | return 1; | |
944 | } | |
945 | ||
946 | return 0; | |
947 | } | |
0f113f3e MC |
948 | |
949 | typedef struct timeout_param_st { | |
950 | SSL_CTX *ctx; | |
951 | long time; | |
952 | LHASH_OF(SSL_SESSION) *cache; | |
953 | } TIMEOUT_PARAM; | |
d02b48c6 | 954 | |
2a056de8 | 955 | static void timeout_cb(SSL_SESSION *s, TIMEOUT_PARAM *p) |
0f113f3e MC |
956 | { |
957 | if ((p->time == 0) || (p->time > (s->time + s->timeout))) { /* timeout */ | |
958 | /* | |
959 | * The reason we don't call SSL_CTX_remove_session() is to save on | |
960 | * locking overhead | |
961 | */ | |
962 | (void)lh_SSL_SESSION_delete(p->cache, s); | |
963 | SSL_SESSION_list_remove(p->ctx, s); | |
964 | s->not_resumable = 1; | |
965 | if (p->ctx->remove_session_cb != NULL) | |
966 | p->ctx->remove_session_cb(p->ctx, s); | |
967 | SSL_SESSION_free(s); | |
968 | } | |
969 | } | |
d02b48c6 | 970 | |
2a056de8 | 971 | IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM); |
3c914840 | 972 | |
6b691a5c | 973 | void SSL_CTX_flush_sessions(SSL_CTX *s, long t) |
0f113f3e MC |
974 | { |
975 | unsigned long i; | |
976 | TIMEOUT_PARAM tp; | |
977 | ||
978 | tp.ctx = s; | |
979 | tp.cache = s->sessions; | |
980 | if (tp.cache == NULL) | |
981 | return; | |
982 | tp.time = t; | |
16203f7b | 983 | CRYPTO_THREAD_write_lock(s->lock); |
739a1eb1 RS |
984 | i = lh_SSL_SESSION_get_down_load(s->sessions); |
985 | lh_SSL_SESSION_set_down_load(s->sessions, 0); | |
2a056de8 | 986 | lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp); |
739a1eb1 | 987 | lh_SSL_SESSION_set_down_load(s->sessions, i); |
16203f7b | 988 | CRYPTO_THREAD_unlock(s->lock); |
0f113f3e | 989 | } |
d02b48c6 | 990 | |
6b691a5c | 991 | int ssl_clear_bad_session(SSL *s) |
0f113f3e MC |
992 | { |
993 | if ((s->session != NULL) && | |
994 | !(s->shutdown & SSL_SENT_SHUTDOWN) && | |
995 | !(SSL_in_init(s) || SSL_in_before(s))) { | |
e2bb9b9b | 996 | SSL_CTX_remove_session(s->session_ctx, s->session); |
0f113f3e MC |
997 | return (1); |
998 | } else | |
999 | return (0); | |
1000 | } | |
58964a49 RE |
1001 | |
1002 | /* locked by SSL_CTX in the calling function */ | |
6b691a5c | 1003 | static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) |
0f113f3e MC |
1004 | { |
1005 | if ((s->next == NULL) || (s->prev == NULL)) | |
1006 | return; | |
1007 | ||
1008 | if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) { | |
1009 | /* last element in list */ | |
1010 | if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) { | |
1011 | /* only one element in list */ | |
1012 | ctx->session_cache_head = NULL; | |
1013 | ctx->session_cache_tail = NULL; | |
1014 | } else { | |
1015 | ctx->session_cache_tail = s->prev; | |
1016 | s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail); | |
1017 | } | |
1018 | } else { | |
1019 | if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) { | |
1020 | /* first element in list */ | |
1021 | ctx->session_cache_head = s->next; | |
1022 | s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); | |
1023 | } else { | |
1024 | /* middle of list */ | |
1025 | s->next->prev = s->prev; | |
1026 | s->prev->next = s->next; | |
1027 | } | |
1028 | } | |
1029 | s->prev = s->next = NULL; | |
1030 | } | |
58964a49 | 1031 | |
6b691a5c | 1032 | static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) |
0f113f3e MC |
1033 | { |
1034 | if ((s->next != NULL) && (s->prev != NULL)) | |
1035 | SSL_SESSION_list_remove(ctx, s); | |
1036 | ||
1037 | if (ctx->session_cache_head == NULL) { | |
1038 | ctx->session_cache_head = s; | |
1039 | ctx->session_cache_tail = s; | |
1040 | s->prev = (SSL_SESSION *)&(ctx->session_cache_head); | |
1041 | s->next = (SSL_SESSION *)&(ctx->session_cache_tail); | |
1042 | } else { | |
1043 | s->next = ctx->session_cache_head; | |
1044 | s->next->prev = s; | |
1045 | s->prev = (SSL_SESSION *)&(ctx->session_cache_head); | |
1046 | ctx->session_cache_head = s; | |
1047 | } | |
1048 | } | |
58964a49 | 1049 | |
7806f3dd | 1050 | void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, |
0f113f3e MC |
1051 | int (*cb) (struct ssl_st *ssl, |
1052 | SSL_SESSION *sess)) | |
1053 | { | |
1054 | ctx->new_session_cb = cb; | |
1055 | } | |
7806f3dd | 1056 | |
0f113f3e MC |
1057 | int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) { |
1058 | return ctx->new_session_cb; | |
1059 | } | |
7806f3dd NL |
1060 | |
1061 | void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx, | |
0f113f3e MC |
1062 | void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess)) |
1063 | { | |
1064 | ctx->remove_session_cb = cb; | |
1065 | } | |
7806f3dd | 1066 | |
0f113f3e MC |
1067 | void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx, |
1068 | SSL_SESSION *sess) { | |
1069 | return ctx->remove_session_cb; | |
1070 | } | |
7806f3dd NL |
1071 | |
1072 | void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, | |
0f113f3e | 1073 | SSL_SESSION *(*cb) (struct ssl_st *ssl, |
b6981744 EK |
1074 | const unsigned char *data, |
1075 | int len, int *copy)) | |
0f113f3e MC |
1076 | { |
1077 | ctx->get_session_cb = cb; | |
1078 | } | |
1079 | ||
1080 | SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl, | |
b6981744 | 1081 | const unsigned char *data, |
0f113f3e MC |
1082 | int len, int *copy) { |
1083 | return ctx->get_session_cb; | |
1084 | } | |
1085 | ||
1086 | void SSL_CTX_set_info_callback(SSL_CTX *ctx, | |
1087 | void (*cb) (const SSL *ssl, int type, int val)) | |
1088 | { | |
1089 | ctx->info_callback = cb; | |
1090 | } | |
1091 | ||
1092 | void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type, | |
1093 | int val) { | |
1094 | return ctx->info_callback; | |
1095 | } | |
7806f3dd NL |
1096 | |
1097 | void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, | |
0f113f3e MC |
1098 | int (*cb) (SSL *ssl, X509 **x509, |
1099 | EVP_PKEY **pkey)) | |
1100 | { | |
1101 | ctx->client_cert_cb = cb; | |
1102 | } | |
7806f3dd | 1103 | |
0f113f3e MC |
1104 | int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509, |
1105 | EVP_PKEY **pkey) { | |
1106 | return ctx->client_cert_cb; | |
1107 | } | |
7806f3dd | 1108 | |
368888bc DSH |
1109 | #ifndef OPENSSL_NO_ENGINE |
1110 | int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e) | |
0f113f3e MC |
1111 | { |
1112 | if (!ENGINE_init(e)) { | |
1113 | SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB); | |
1114 | return 0; | |
1115 | } | |
1116 | if (!ENGINE_get_ssl_client_cert_function(e)) { | |
1117 | SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, | |
1118 | SSL_R_NO_CLIENT_CERT_METHOD); | |
1119 | ENGINE_finish(e); | |
1120 | return 0; | |
1121 | } | |
1122 | ctx->client_cert_engine = e; | |
1123 | return 1; | |
1124 | } | |
368888bc DSH |
1125 | #endif |
1126 | ||
7806f3dd | 1127 | void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx, |
0f113f3e MC |
1128 | int (*cb) (SSL *ssl, |
1129 | unsigned char *cookie, | |
1130 | unsigned int *cookie_len)) | |
1131 | { | |
1132 | ctx->app_gen_cookie_cb = cb; | |
1133 | } | |
7806f3dd NL |
1134 | |
1135 | void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx, | |
31011544 | 1136 | int (*cb) (SSL *ssl, const unsigned char *cookie, |
0f113f3e MC |
1137 | unsigned int cookie_len)) |
1138 | { | |
1139 | ctx->app_verify_cookie_cb = cb; | |
1140 | } | |
7806f3dd | 1141 | |
0f113f3e MC |
1142 | IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, |
1143 | SSL_SESSION) |