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