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