/src/openssl/ssl/ssl_sess.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright 2005 Nokia. All rights reserved. |
4 | | * |
5 | | * Licensed under the Apache License 2.0 (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 |
9 | | */ |
10 | | |
11 | | #if defined(__TANDEM) && defined(_SPT_MODEL_) |
12 | | # include <spthread.h> |
13 | | # include <spt_extensions.h> /* timeval */ |
14 | | #endif |
15 | | #include <stdio.h> |
16 | | #include <openssl/rand.h> |
17 | | #include <openssl/engine.h> |
18 | | #include "internal/refcount.h" |
19 | | #include "internal/cryptlib.h" |
20 | | #include "ssl_local.h" |
21 | | #include "statem/statem_local.h" |
22 | | |
23 | | static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s); |
24 | | static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s); |
25 | | static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck); |
26 | | |
27 | | DEFINE_STACK_OF(SSL_SESSION) |
28 | | |
29 | | __owur static ossl_inline int sess_timedout(OSSL_TIME t, SSL_SESSION *ss) |
30 | 0 | { |
31 | 0 | return ossl_time_compare(t, ss->calc_timeout) > 0; |
32 | 0 | } |
33 | | |
34 | | /* |
35 | | * Returns -1/0/+1 as other XXXcmp-type functions |
36 | | * Takes calculated timeout into consideration |
37 | | */ |
38 | | __owur static ossl_inline int timeoutcmp(SSL_SESSION *a, SSL_SESSION *b) |
39 | 0 | { |
40 | 0 | return ossl_time_compare(a->calc_timeout, b->calc_timeout); |
41 | 0 | } |
42 | | |
43 | | /* |
44 | | * Calculates effective timeout |
45 | | * Locking must be done by the caller of this function |
46 | | */ |
47 | | void ssl_session_calculate_timeout(SSL_SESSION *ss) |
48 | 647 | { |
49 | 647 | ss->calc_timeout = ossl_time_add(ss->time, ss->timeout); |
50 | 647 | } |
51 | | |
52 | | /* |
53 | | * SSL_get_session() and SSL_get1_session() are problematic in TLS1.3 because, |
54 | | * unlike in earlier protocol versions, the session ticket may not have been |
55 | | * sent yet even though a handshake has finished. The session ticket data could |
56 | | * come in sometime later...or even change if multiple session ticket messages |
57 | | * are sent from the server. The preferred way for applications to obtain |
58 | | * a resumable session is to use SSL_CTX_sess_set_new_cb(). |
59 | | */ |
60 | | |
61 | | SSL_SESSION *SSL_get_session(const SSL *ssl) |
62 | | /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */ |
63 | 0 | { |
64 | 0 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
65 | |
|
66 | 0 | if (sc == NULL) |
67 | 0 | return NULL; |
68 | | |
69 | 0 | return sc->session; |
70 | 0 | } |
71 | | |
72 | | SSL_SESSION *SSL_get1_session(SSL *ssl) |
73 | | /* variant of SSL_get_session: caller really gets something */ |
74 | 0 | { |
75 | 0 | SSL_SESSION *sess; |
76 | | |
77 | | /* |
78 | | * Need to lock this all up rather than just use CRYPTO_add so that |
79 | | * somebody doesn't free ssl->session between when we check it's non-null |
80 | | * and when we up the reference count. |
81 | | */ |
82 | 0 | if (!CRYPTO_THREAD_read_lock(ssl->lock)) |
83 | 0 | return NULL; |
84 | 0 | sess = SSL_get_session(ssl); |
85 | 0 | if (sess != NULL) |
86 | 0 | SSL_SESSION_up_ref(sess); |
87 | 0 | CRYPTO_THREAD_unlock(ssl->lock); |
88 | 0 | return sess; |
89 | 0 | } |
90 | | |
91 | | int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) |
92 | 0 | { |
93 | 0 | return CRYPTO_set_ex_data(&s->ex_data, idx, arg); |
94 | 0 | } |
95 | | |
96 | | void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) |
97 | 0 | { |
98 | 0 | return CRYPTO_get_ex_data(&s->ex_data, idx); |
99 | 0 | } |
100 | | |
101 | | SSL_SESSION *SSL_SESSION_new(void) |
102 | 359 | { |
103 | 359 | SSL_SESSION *ss; |
104 | | |
105 | 359 | if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL)) |
106 | 0 | return NULL; |
107 | | |
108 | 359 | ss = OPENSSL_zalloc(sizeof(*ss)); |
109 | 359 | if (ss == NULL) |
110 | 0 | return NULL; |
111 | | |
112 | 359 | ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */ |
113 | 359 | ss->references = 1; |
114 | | /* 5 minute timeout by default */ |
115 | 359 | ss->timeout = ossl_seconds2time(60 * 5 + 4); |
116 | 359 | ss->time = ossl_time_now(); |
117 | 359 | ssl_session_calculate_timeout(ss); |
118 | 359 | ss->lock = CRYPTO_THREAD_lock_new(); |
119 | 359 | if (ss->lock == NULL) { |
120 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
121 | 0 | OPENSSL_free(ss); |
122 | 0 | return NULL; |
123 | 0 | } |
124 | | |
125 | 359 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) { |
126 | 0 | CRYPTO_THREAD_lock_free(ss->lock); |
127 | 0 | OPENSSL_free(ss); |
128 | 0 | return NULL; |
129 | 0 | } |
130 | 359 | return ss; |
131 | 359 | } |
132 | | |
133 | | SSL_SESSION *SSL_SESSION_dup(const SSL_SESSION *src) |
134 | 0 | { |
135 | 0 | return ssl_session_dup(src, 1); |
136 | 0 | } |
137 | | |
138 | | /* |
139 | | * Create a new SSL_SESSION and duplicate the contents of |src| into it. If |
140 | | * ticket == 0 then no ticket information is duplicated, otherwise it is. |
141 | | */ |
142 | | SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket) |
143 | 0 | { |
144 | 0 | SSL_SESSION *dest; |
145 | |
|
146 | 0 | dest = OPENSSL_malloc(sizeof(*dest)); |
147 | 0 | if (dest == NULL) { |
148 | 0 | goto err; |
149 | 0 | } |
150 | 0 | memcpy(dest, src, sizeof(*dest)); |
151 | | |
152 | | /* |
153 | | * Set the various pointers to NULL so that we can call SSL_SESSION_free in |
154 | | * the case of an error whilst halfway through constructing dest |
155 | | */ |
156 | 0 | #ifndef OPENSSL_NO_PSK |
157 | 0 | dest->psk_identity_hint = NULL; |
158 | 0 | dest->psk_identity = NULL; |
159 | 0 | #endif |
160 | 0 | dest->ext.hostname = NULL; |
161 | 0 | dest->ext.tick = NULL; |
162 | 0 | dest->ext.alpn_selected = NULL; |
163 | 0 | #ifndef OPENSSL_NO_SRP |
164 | 0 | dest->srp_username = NULL; |
165 | 0 | #endif |
166 | 0 | dest->peer_chain = NULL; |
167 | 0 | dest->peer = NULL; |
168 | 0 | dest->peer_rpk = NULL; |
169 | 0 | dest->ticket_appdata = NULL; |
170 | 0 | memset(&dest->ex_data, 0, sizeof(dest->ex_data)); |
171 | | |
172 | | /* As the copy is not in the cache, we remove the associated pointers */ |
173 | 0 | dest->prev = NULL; |
174 | 0 | dest->next = NULL; |
175 | 0 | dest->owner = NULL; |
176 | |
|
177 | 0 | dest->references = 1; |
178 | |
|
179 | 0 | dest->lock = CRYPTO_THREAD_lock_new(); |
180 | 0 | if (dest->lock == NULL) { |
181 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
182 | 0 | goto err; |
183 | 0 | } |
184 | | |
185 | 0 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, dest, &dest->ex_data)) { |
186 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
187 | 0 | goto err; |
188 | 0 | } |
189 | | |
190 | 0 | if (src->peer != NULL) { |
191 | 0 | if (!X509_up_ref(src->peer)) { |
192 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB); |
193 | 0 | goto err; |
194 | 0 | } |
195 | 0 | dest->peer = src->peer; |
196 | 0 | } |
197 | | |
198 | 0 | if (src->peer_chain != NULL) { |
199 | 0 | dest->peer_chain = X509_chain_up_ref(src->peer_chain); |
200 | 0 | if (dest->peer_chain == NULL) { |
201 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB); |
202 | 0 | goto err; |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | 0 | if (src->peer_rpk != NULL) { |
207 | 0 | if (!EVP_PKEY_up_ref(src->peer_rpk)) |
208 | 0 | goto err; |
209 | 0 | dest->peer_rpk = src->peer_rpk; |
210 | 0 | } |
211 | | |
212 | 0 | #ifndef OPENSSL_NO_PSK |
213 | 0 | if (src->psk_identity_hint) { |
214 | 0 | dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint); |
215 | 0 | if (dest->psk_identity_hint == NULL) |
216 | 0 | goto err; |
217 | 0 | } |
218 | 0 | if (src->psk_identity) { |
219 | 0 | dest->psk_identity = OPENSSL_strdup(src->psk_identity); |
220 | 0 | if (dest->psk_identity == NULL) |
221 | 0 | goto err; |
222 | 0 | } |
223 | 0 | #endif |
224 | | |
225 | 0 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, |
226 | 0 | &dest->ex_data, &src->ex_data)) { |
227 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB); |
228 | 0 | goto err; |
229 | 0 | } |
230 | | |
231 | 0 | if (src->ext.hostname) { |
232 | 0 | dest->ext.hostname = OPENSSL_strdup(src->ext.hostname); |
233 | 0 | if (dest->ext.hostname == NULL) |
234 | 0 | goto err; |
235 | 0 | } |
236 | | |
237 | 0 | if (ticket != 0 && src->ext.tick != NULL) { |
238 | 0 | dest->ext.tick = |
239 | 0 | OPENSSL_memdup(src->ext.tick, src->ext.ticklen); |
240 | 0 | if (dest->ext.tick == NULL) |
241 | 0 | goto err; |
242 | 0 | } else { |
243 | 0 | dest->ext.tick_lifetime_hint = 0; |
244 | 0 | dest->ext.ticklen = 0; |
245 | 0 | } |
246 | | |
247 | 0 | if (src->ext.alpn_selected != NULL) { |
248 | 0 | dest->ext.alpn_selected = OPENSSL_memdup(src->ext.alpn_selected, |
249 | 0 | src->ext.alpn_selected_len); |
250 | 0 | if (dest->ext.alpn_selected == NULL) |
251 | 0 | goto err; |
252 | 0 | } |
253 | | |
254 | 0 | #ifndef OPENSSL_NO_SRP |
255 | 0 | if (src->srp_username) { |
256 | 0 | dest->srp_username = OPENSSL_strdup(src->srp_username); |
257 | 0 | if (dest->srp_username == NULL) |
258 | 0 | goto err; |
259 | 0 | } |
260 | 0 | #endif |
261 | | |
262 | 0 | if (src->ticket_appdata != NULL) { |
263 | 0 | dest->ticket_appdata = |
264 | 0 | OPENSSL_memdup(src->ticket_appdata, src->ticket_appdata_len); |
265 | 0 | if (dest->ticket_appdata == NULL) |
266 | 0 | goto err; |
267 | 0 | } |
268 | | |
269 | 0 | return dest; |
270 | 0 | err: |
271 | 0 | SSL_SESSION_free(dest); |
272 | 0 | return NULL; |
273 | 0 | } |
274 | | |
275 | | const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) |
276 | 0 | { |
277 | 0 | if (len) |
278 | 0 | *len = (unsigned int)s->session_id_length; |
279 | 0 | return s->session_id; |
280 | 0 | } |
281 | | const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s, |
282 | | unsigned int *len) |
283 | 0 | { |
284 | 0 | if (len != NULL) |
285 | 0 | *len = (unsigned int)s->sid_ctx_length; |
286 | 0 | return s->sid_ctx; |
287 | 0 | } |
288 | | |
289 | | unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s) |
290 | 0 | { |
291 | 0 | return s->compress_meth; |
292 | 0 | } |
293 | | |
294 | | /* |
295 | | * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling |
296 | | * the ID with random junk repeatedly until we have no conflict is going to |
297 | | * complete in one iteration pretty much "most" of the time (btw: |
298 | | * understatement). So, if it takes us 10 iterations and we still can't avoid |
299 | | * a conflict - well that's a reasonable point to call it quits. Either the |
300 | | * RAND code is broken or someone is trying to open roughly very close to |
301 | | * 2^256 SSL sessions to our server. How you might store that many sessions |
302 | | * is perhaps a more interesting question ... |
303 | | */ |
304 | | |
305 | 0 | #define MAX_SESS_ID_ATTEMPTS 10 |
306 | | static int def_generate_session_id(SSL *ssl, unsigned char *id, |
307 | | unsigned int *id_len) |
308 | 0 | { |
309 | 0 | unsigned int retry = 0; |
310 | 0 | do { |
311 | 0 | if (RAND_bytes_ex(ssl->ctx->libctx, id, *id_len, 0) <= 0) |
312 | 0 | return 0; |
313 | 0 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
314 | 0 | if (retry > 0) { |
315 | 0 | id[0]++; |
316 | 0 | } |
317 | 0 | #endif |
318 | 0 | } while (SSL_has_matching_session_id(ssl, id, *id_len) && |
319 | 0 | (++retry < MAX_SESS_ID_ATTEMPTS)) ; |
320 | 0 | if (retry < MAX_SESS_ID_ATTEMPTS) |
321 | 0 | return 1; |
322 | | /* else - woops a session_id match */ |
323 | | /* |
324 | | * XXX We should also check the external cache -- but the probability of |
325 | | * a collision is negligible, and we could not prevent the concurrent |
326 | | * creation of sessions with identical IDs since we currently don't have |
327 | | * means to atomically check whether a session ID already exists and make |
328 | | * a reservation for it if it does not (this problem applies to the |
329 | | * internal cache as well). |
330 | | */ |
331 | 0 | return 0; |
332 | 0 | } |
333 | | |
334 | | int ssl_generate_session_id(SSL_CONNECTION *s, SSL_SESSION *ss) |
335 | 0 | { |
336 | 0 | unsigned int tmp; |
337 | 0 | GEN_SESSION_CB cb = def_generate_session_id; |
338 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
339 | |
|
340 | 0 | switch (s->version) { |
341 | 0 | case SSL3_VERSION: |
342 | 0 | case TLS1_VERSION: |
343 | 0 | case TLS1_1_VERSION: |
344 | 0 | case TLS1_2_VERSION: |
345 | 0 | case TLS1_3_VERSION: |
346 | 0 | case DTLS1_BAD_VER: |
347 | 0 | case DTLS1_VERSION: |
348 | 0 | case DTLS1_2_VERSION: |
349 | 0 | ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; |
350 | 0 | break; |
351 | 0 | default: |
352 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNSUPPORTED_SSL_VERSION); |
353 | 0 | return 0; |
354 | 0 | } |
355 | | |
356 | | /*- |
357 | | * If RFC5077 ticket, use empty session ID (as server). |
358 | | * Note that: |
359 | | * (a) ssl_get_prev_session() does lookahead into the |
360 | | * ClientHello extensions to find the session ticket. |
361 | | * When ssl_get_prev_session() fails, statem_srvr.c calls |
362 | | * ssl_get_new_session() in tls_process_client_hello(). |
363 | | * At that point, it has not yet parsed the extensions, |
364 | | * however, because of the lookahead, it already knows |
365 | | * whether a ticket is expected or not. |
366 | | * |
367 | | * (b) statem_clnt.c calls ssl_get_new_session() before parsing |
368 | | * ServerHello extensions, and before recording the session |
369 | | * ID received from the server, so this block is a noop. |
370 | | */ |
371 | 0 | if (s->ext.ticket_expected) { |
372 | 0 | ss->session_id_length = 0; |
373 | 0 | return 1; |
374 | 0 | } |
375 | | |
376 | | /* Choose which callback will set the session ID */ |
377 | 0 | if (!CRYPTO_THREAD_read_lock(SSL_CONNECTION_GET_SSL(s)->lock)) |
378 | 0 | return 0; |
379 | 0 | if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock)) { |
380 | 0 | CRYPTO_THREAD_unlock(ssl->lock); |
381 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
382 | 0 | SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); |
383 | 0 | return 0; |
384 | 0 | } |
385 | 0 | if (s->generate_session_id) |
386 | 0 | cb = s->generate_session_id; |
387 | 0 | else if (s->session_ctx->generate_session_id) |
388 | 0 | cb = s->session_ctx->generate_session_id; |
389 | 0 | CRYPTO_THREAD_unlock(s->session_ctx->lock); |
390 | 0 | CRYPTO_THREAD_unlock(ssl->lock); |
391 | | /* Choose a session ID */ |
392 | 0 | memset(ss->session_id, 0, ss->session_id_length); |
393 | 0 | tmp = (int)ss->session_id_length; |
394 | 0 | if (!cb(ssl, ss->session_id, &tmp)) { |
395 | | /* The callback failed */ |
396 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
397 | 0 | SSL_R_SSL_SESSION_ID_CALLBACK_FAILED); |
398 | 0 | return 0; |
399 | 0 | } |
400 | | /* |
401 | | * Don't allow the callback to set the session length to zero. nor |
402 | | * set it higher than it was. |
403 | | */ |
404 | 0 | if (tmp == 0 || tmp > ss->session_id_length) { |
405 | | /* The callback set an illegal length */ |
406 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
407 | 0 | SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH); |
408 | 0 | return 0; |
409 | 0 | } |
410 | 0 | ss->session_id_length = tmp; |
411 | | /* Finally, check for a conflict */ |
412 | 0 | if (SSL_has_matching_session_id(ssl, ss->session_id, |
413 | 0 | (unsigned int)ss->session_id_length)) { |
414 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SSL_SESSION_ID_CONFLICT); |
415 | 0 | return 0; |
416 | 0 | } |
417 | | |
418 | 0 | return 1; |
419 | 0 | } |
420 | | |
421 | | int ssl_get_new_session(SSL_CONNECTION *s, int session) |
422 | 0 | { |
423 | | /* This gets used by clients and servers. */ |
424 | |
|
425 | 0 | SSL_SESSION *ss = NULL; |
426 | |
|
427 | 0 | if ((ss = SSL_SESSION_new()) == NULL) { |
428 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
429 | 0 | return 0; |
430 | 0 | } |
431 | | |
432 | | /* If the context has a default timeout, use it */ |
433 | 0 | if (ossl_time_is_zero(s->session_ctx->session_timeout)) |
434 | 0 | ss->timeout = SSL_CONNECTION_GET_SSL(s)->method->get_timeout(); |
435 | 0 | else |
436 | 0 | ss->timeout = s->session_ctx->session_timeout; |
437 | 0 | ssl_session_calculate_timeout(ss); |
438 | |
|
439 | 0 | SSL_SESSION_free(s->session); |
440 | 0 | s->session = NULL; |
441 | |
|
442 | 0 | if (session) { |
443 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
444 | | /* |
445 | | * We generate the session id while constructing the |
446 | | * NewSessionTicket in TLSv1.3. |
447 | | */ |
448 | 0 | ss->session_id_length = 0; |
449 | 0 | } else if (!ssl_generate_session_id(s, ss)) { |
450 | | /* SSLfatal() already called */ |
451 | 0 | SSL_SESSION_free(ss); |
452 | 0 | return 0; |
453 | 0 | } |
454 | |
|
455 | 0 | } else { |
456 | 0 | ss->session_id_length = 0; |
457 | 0 | } |
458 | | |
459 | 0 | if (s->sid_ctx_length > sizeof(ss->sid_ctx)) { |
460 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
461 | 0 | SSL_SESSION_free(ss); |
462 | 0 | return 0; |
463 | 0 | } |
464 | 0 | memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length); |
465 | 0 | ss->sid_ctx_length = s->sid_ctx_length; |
466 | 0 | s->session = ss; |
467 | 0 | ss->ssl_version = s->version; |
468 | 0 | ss->verify_result = X509_V_OK; |
469 | | |
470 | | /* If client supports extended master secret set it in session */ |
471 | 0 | if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) |
472 | 0 | ss->flags |= SSL_SESS_FLAG_EXTMS; |
473 | |
|
474 | 0 | return 1; |
475 | 0 | } |
476 | | |
477 | | SSL_SESSION *lookup_sess_in_cache(SSL_CONNECTION *s, |
478 | | const unsigned char *sess_id, |
479 | | size_t sess_id_len) |
480 | 0 | { |
481 | 0 | SSL_SESSION *ret = NULL; |
482 | |
|
483 | 0 | if ((s->session_ctx->session_cache_mode |
484 | 0 | & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP) == 0) { |
485 | 0 | SSL_SESSION data; |
486 | |
|
487 | 0 | data.ssl_version = s->version; |
488 | 0 | if (!ossl_assert(sess_id_len <= SSL_MAX_SSL_SESSION_ID_LENGTH)) |
489 | 0 | return NULL; |
490 | | |
491 | 0 | memcpy(data.session_id, sess_id, sess_id_len); |
492 | 0 | data.session_id_length = sess_id_len; |
493 | |
|
494 | 0 | if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock)) |
495 | 0 | return NULL; |
496 | 0 | ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data); |
497 | 0 | if (ret != NULL) { |
498 | | /* don't allow other threads to steal it: */ |
499 | 0 | SSL_SESSION_up_ref(ret); |
500 | 0 | } |
501 | 0 | CRYPTO_THREAD_unlock(s->session_ctx->lock); |
502 | 0 | if (ret == NULL) |
503 | 0 | ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss); |
504 | 0 | } |
505 | | |
506 | 0 | if (ret == NULL && s->session_ctx->get_session_cb != NULL) { |
507 | 0 | int copy = 1; |
508 | |
|
509 | 0 | ret = s->session_ctx->get_session_cb(SSL_CONNECTION_GET_SSL(s), |
510 | 0 | sess_id, sess_id_len, ©); |
511 | |
|
512 | 0 | if (ret != NULL) { |
513 | 0 | ssl_tsan_counter(s->session_ctx, |
514 | 0 | &s->session_ctx->stats.sess_cb_hit); |
515 | | |
516 | | /* |
517 | | * Increment reference count now if the session callback asks us |
518 | | * to do so (note that if the session structures returned by the |
519 | | * callback are shared between threads, it must handle the |
520 | | * reference count itself [i.e. copy == 0], or things won't be |
521 | | * thread-safe). |
522 | | */ |
523 | 0 | if (copy) |
524 | 0 | SSL_SESSION_up_ref(ret); |
525 | | |
526 | | /* |
527 | | * Add the externally cached session to the internal cache as |
528 | | * well if and only if we are supposed to. |
529 | | */ |
530 | 0 | if ((s->session_ctx->session_cache_mode & |
531 | 0 | SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0) { |
532 | | /* |
533 | | * Either return value of SSL_CTX_add_session should not |
534 | | * interrupt the session resumption process. The return |
535 | | * value is intentionally ignored. |
536 | | */ |
537 | 0 | (void)SSL_CTX_add_session(s->session_ctx, ret); |
538 | 0 | } |
539 | 0 | } |
540 | 0 | } |
541 | |
|
542 | 0 | return ret; |
543 | 0 | } |
544 | | |
545 | | /*- |
546 | | * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this |
547 | | * connection. It is only called by servers. |
548 | | * |
549 | | * hello: The parsed ClientHello data |
550 | | * |
551 | | * Returns: |
552 | | * -1: fatal error |
553 | | * 0: no session found |
554 | | * 1: a session may have been found. |
555 | | * |
556 | | * Side effects: |
557 | | * - If a session is found then s->session is pointed at it (after freeing an |
558 | | * existing session if need be) and s->verify_result is set from the session. |
559 | | * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1 |
560 | | * if the server should issue a new session ticket (to 0 otherwise). |
561 | | */ |
562 | | int ssl_get_prev_session(SSL_CONNECTION *s, CLIENTHELLO_MSG *hello) |
563 | 0 | { |
564 | | /* This is used only by servers. */ |
565 | |
|
566 | 0 | SSL_SESSION *ret = NULL; |
567 | 0 | int fatal = 0; |
568 | 0 | int try_session_cache = 0; |
569 | 0 | SSL_TICKET_STATUS r; |
570 | |
|
571 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
572 | | /* |
573 | | * By default we will send a new ticket. This can be overridden in the |
574 | | * ticket processing. |
575 | | */ |
576 | 0 | s->ext.ticket_expected = 1; |
577 | 0 | if (!tls_parse_extension(s, TLSEXT_IDX_psk_kex_modes, |
578 | 0 | SSL_EXT_CLIENT_HELLO, hello->pre_proc_exts, |
579 | 0 | NULL, 0) |
580 | 0 | || !tls_parse_extension(s, TLSEXT_IDX_psk, SSL_EXT_CLIENT_HELLO, |
581 | 0 | hello->pre_proc_exts, NULL, 0)) |
582 | 0 | return -1; |
583 | | |
584 | 0 | ret = s->session; |
585 | 0 | } else { |
586 | | /* sets s->ext.ticket_expected */ |
587 | 0 | r = tls_get_ticket_from_client(s, hello, &ret); |
588 | 0 | switch (r) { |
589 | 0 | case SSL_TICKET_FATAL_ERR_MALLOC: |
590 | 0 | case SSL_TICKET_FATAL_ERR_OTHER: |
591 | 0 | fatal = 1; |
592 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
593 | 0 | goto err; |
594 | 0 | case SSL_TICKET_NONE: |
595 | 0 | case SSL_TICKET_EMPTY: |
596 | 0 | if (hello->session_id_len > 0) { |
597 | 0 | try_session_cache = 1; |
598 | 0 | ret = lookup_sess_in_cache(s, hello->session_id, |
599 | 0 | hello->session_id_len); |
600 | 0 | } |
601 | 0 | break; |
602 | 0 | case SSL_TICKET_NO_DECRYPT: |
603 | 0 | case SSL_TICKET_SUCCESS: |
604 | 0 | case SSL_TICKET_SUCCESS_RENEW: |
605 | 0 | break; |
606 | 0 | } |
607 | 0 | } |
608 | | |
609 | 0 | if (ret == NULL) |
610 | 0 | goto err; |
611 | | |
612 | | /* Now ret is non-NULL and we own one of its reference counts. */ |
613 | | |
614 | | /* Check TLS version consistency */ |
615 | 0 | if (ret->ssl_version != s->version) |
616 | 0 | goto err; |
617 | | |
618 | 0 | if (ret->sid_ctx_length != s->sid_ctx_length |
619 | 0 | || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) { |
620 | | /* |
621 | | * We have the session requested by the client, but we don't want to |
622 | | * use it in this context. |
623 | | */ |
624 | 0 | goto err; /* treat like cache miss */ |
625 | 0 | } |
626 | | |
627 | 0 | if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) { |
628 | | /* |
629 | | * We can't be sure if this session is being used out of context, |
630 | | * which is especially important for SSL_VERIFY_PEER. The application |
631 | | * should have used SSL[_CTX]_set_session_id_context. For this error |
632 | | * case, we generate an error instead of treating the event like a |
633 | | * cache miss (otherwise it would be easy for applications to |
634 | | * effectively disable the session cache by accident without anyone |
635 | | * noticing). |
636 | | */ |
637 | |
|
638 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
639 | 0 | SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED); |
640 | 0 | fatal = 1; |
641 | 0 | goto err; |
642 | 0 | } |
643 | | |
644 | 0 | if (sess_timedout(ossl_time_now(), ret)) { |
645 | 0 | ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_timeout); |
646 | 0 | if (try_session_cache) { |
647 | | /* session was from the cache, so remove it */ |
648 | 0 | SSL_CTX_remove_session(s->session_ctx, ret); |
649 | 0 | } |
650 | 0 | goto err; |
651 | 0 | } |
652 | | |
653 | | /* Check extended master secret extension consistency */ |
654 | 0 | if (ret->flags & SSL_SESS_FLAG_EXTMS) { |
655 | | /* If old session includes extms, but new does not: abort handshake */ |
656 | 0 | if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)) { |
657 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INCONSISTENT_EXTMS); |
658 | 0 | fatal = 1; |
659 | 0 | goto err; |
660 | 0 | } |
661 | 0 | } else if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) { |
662 | | /* If new session includes extms, but old does not: do not resume */ |
663 | 0 | goto err; |
664 | 0 | } |
665 | | |
666 | 0 | if (!SSL_CONNECTION_IS_TLS13(s)) { |
667 | | /* We already did this for TLS1.3 */ |
668 | 0 | SSL_SESSION_free(s->session); |
669 | 0 | s->session = ret; |
670 | 0 | } |
671 | |
|
672 | 0 | ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_hit); |
673 | 0 | s->verify_result = s->session->verify_result; |
674 | 0 | return 1; |
675 | | |
676 | 0 | err: |
677 | 0 | if (ret != NULL) { |
678 | 0 | SSL_SESSION_free(ret); |
679 | | /* In TLSv1.3 s->session was already set to ret, so we NULL it out */ |
680 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) |
681 | 0 | s->session = NULL; |
682 | |
|
683 | 0 | if (!try_session_cache) { |
684 | | /* |
685 | | * The session was from a ticket, so we should issue a ticket for |
686 | | * the new session |
687 | | */ |
688 | 0 | s->ext.ticket_expected = 1; |
689 | 0 | } |
690 | 0 | } |
691 | 0 | if (fatal) |
692 | 0 | return -1; |
693 | | |
694 | 0 | return 0; |
695 | 0 | } |
696 | | |
697 | | int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) |
698 | 0 | { |
699 | 0 | int ret = 0; |
700 | 0 | SSL_SESSION *s; |
701 | | |
702 | | /* |
703 | | * add just 1 reference count for the SSL_CTX's session cache even though |
704 | | * it has two ways of access: each session is in a doubly linked list and |
705 | | * an lhash |
706 | | */ |
707 | 0 | SSL_SESSION_up_ref(c); |
708 | | /* |
709 | | * if session c is in already in cache, we take back the increment later |
710 | | */ |
711 | |
|
712 | 0 | if (!CRYPTO_THREAD_write_lock(ctx->lock)) { |
713 | 0 | SSL_SESSION_free(c); |
714 | 0 | return 0; |
715 | 0 | } |
716 | 0 | s = lh_SSL_SESSION_insert(ctx->sessions, c); |
717 | | |
718 | | /* |
719 | | * s != NULL iff we already had a session with the given PID. In this |
720 | | * case, s == c should hold (then we did not really modify |
721 | | * ctx->sessions), or we're in trouble. |
722 | | */ |
723 | 0 | if (s != NULL && s != c) { |
724 | | /* We *are* in trouble ... */ |
725 | 0 | SSL_SESSION_list_remove(ctx, s); |
726 | 0 | SSL_SESSION_free(s); |
727 | | /* |
728 | | * ... so pretend the other session did not exist in cache (we cannot |
729 | | * handle two SSL_SESSION structures with identical session ID in the |
730 | | * same cache, which could happen e.g. when two threads concurrently |
731 | | * obtain the same session from an external cache) |
732 | | */ |
733 | 0 | s = NULL; |
734 | 0 | } else if (s == NULL && |
735 | 0 | lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) { |
736 | | /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */ |
737 | | |
738 | | /* |
739 | | * ... so take back the extra reference and also don't add |
740 | | * the session to the SSL_SESSION_list at this time |
741 | | */ |
742 | 0 | s = c; |
743 | 0 | } |
744 | | |
745 | | /* Adjust last used time, and add back into the cache at the appropriate spot */ |
746 | 0 | if (ctx->session_cache_mode & SSL_SESS_CACHE_UPDATE_TIME) { |
747 | 0 | c->time = ossl_time_now(); |
748 | 0 | ssl_session_calculate_timeout(c); |
749 | 0 | } |
750 | |
|
751 | 0 | if (s == NULL) { |
752 | | /* |
753 | | * new cache entry -- remove old ones if cache has become too large |
754 | | * delete cache entry *before* add, so we don't remove the one we're adding! |
755 | | */ |
756 | |
|
757 | 0 | ret = 1; |
758 | |
|
759 | 0 | if (SSL_CTX_sess_get_cache_size(ctx) > 0) { |
760 | 0 | while (SSL_CTX_sess_number(ctx) >= SSL_CTX_sess_get_cache_size(ctx)) { |
761 | 0 | if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) |
762 | 0 | break; |
763 | 0 | else |
764 | 0 | ssl_tsan_counter(ctx, &ctx->stats.sess_cache_full); |
765 | 0 | } |
766 | 0 | } |
767 | 0 | } |
768 | |
|
769 | 0 | SSL_SESSION_list_add(ctx, c); |
770 | |
|
771 | 0 | if (s != NULL) { |
772 | | /* |
773 | | * existing cache entry -- decrement previously incremented reference |
774 | | * count because it already takes into account the cache |
775 | | */ |
776 | |
|
777 | 0 | SSL_SESSION_free(s); /* s == c */ |
778 | 0 | ret = 0; |
779 | 0 | } |
780 | 0 | CRYPTO_THREAD_unlock(ctx->lock); |
781 | 0 | return ret; |
782 | 0 | } |
783 | | |
784 | | int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) |
785 | 0 | { |
786 | 0 | return remove_session_lock(ctx, c, 1); |
787 | 0 | } |
788 | | |
789 | | static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck) |
790 | 0 | { |
791 | 0 | SSL_SESSION *r; |
792 | 0 | int ret = 0; |
793 | |
|
794 | 0 | if ((c != NULL) && (c->session_id_length != 0)) { |
795 | 0 | if (lck) { |
796 | 0 | if (!CRYPTO_THREAD_write_lock(ctx->lock)) |
797 | 0 | return 0; |
798 | 0 | } |
799 | 0 | if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) != NULL) { |
800 | 0 | ret = 1; |
801 | 0 | r = lh_SSL_SESSION_delete(ctx->sessions, r); |
802 | 0 | SSL_SESSION_list_remove(ctx, r); |
803 | 0 | } |
804 | 0 | c->not_resumable = 1; |
805 | |
|
806 | 0 | if (lck) |
807 | 0 | CRYPTO_THREAD_unlock(ctx->lock); |
808 | |
|
809 | 0 | if (ctx->remove_session_cb != NULL) |
810 | 0 | ctx->remove_session_cb(ctx, c); |
811 | |
|
812 | 0 | if (ret) |
813 | 0 | SSL_SESSION_free(r); |
814 | 0 | } |
815 | 0 | return ret; |
816 | 0 | } |
817 | | |
818 | | void SSL_SESSION_free(SSL_SESSION *ss) |
819 | 11.0k | { |
820 | 11.0k | int i; |
821 | | |
822 | 11.0k | if (ss == NULL) |
823 | 10.6k | return; |
824 | 359 | CRYPTO_DOWN_REF(&ss->references, &i, ss->lock); |
825 | 359 | REF_PRINT_COUNT("SSL_SESSION", ss); |
826 | 359 | if (i > 0) |
827 | 0 | return; |
828 | 359 | REF_ASSERT_ISNT(i < 0); |
829 | | |
830 | 359 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data); |
831 | | |
832 | 359 | OPENSSL_cleanse(ss->master_key, sizeof(ss->master_key)); |
833 | 359 | OPENSSL_cleanse(ss->session_id, sizeof(ss->session_id)); |
834 | 359 | X509_free(ss->peer); |
835 | 359 | EVP_PKEY_free(ss->peer_rpk); |
836 | 359 | OSSL_STACK_OF_X509_free(ss->peer_chain); |
837 | 359 | OPENSSL_free(ss->ext.hostname); |
838 | 359 | OPENSSL_free(ss->ext.tick); |
839 | 359 | #ifndef OPENSSL_NO_PSK |
840 | 359 | OPENSSL_free(ss->psk_identity_hint); |
841 | 359 | OPENSSL_free(ss->psk_identity); |
842 | 359 | #endif |
843 | 359 | #ifndef OPENSSL_NO_SRP |
844 | 359 | OPENSSL_free(ss->srp_username); |
845 | 359 | #endif |
846 | 359 | OPENSSL_free(ss->ext.alpn_selected); |
847 | 359 | OPENSSL_free(ss->ticket_appdata); |
848 | 359 | CRYPTO_THREAD_lock_free(ss->lock); |
849 | 359 | OPENSSL_clear_free(ss, sizeof(*ss)); |
850 | 359 | } |
851 | | |
852 | | int SSL_SESSION_up_ref(SSL_SESSION *ss) |
853 | 0 | { |
854 | 0 | int i; |
855 | |
|
856 | 0 | if (CRYPTO_UP_REF(&ss->references, &i, ss->lock) <= 0) |
857 | 0 | return 0; |
858 | | |
859 | 0 | REF_PRINT_COUNT("SSL_SESSION", ss); |
860 | 0 | REF_ASSERT_ISNT(i < 2); |
861 | 0 | return ((i > 1) ? 1 : 0); |
862 | 0 | } |
863 | | |
864 | | int SSL_set_session(SSL *s, SSL_SESSION *session) |
865 | 0 | { |
866 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
867 | |
|
868 | 0 | if (sc == NULL) |
869 | 0 | return 0; |
870 | | |
871 | 0 | ssl_clear_bad_session(sc); |
872 | 0 | if (s->defltmeth != s->method) { |
873 | 0 | if (!SSL_set_ssl_method(s, s->defltmeth)) |
874 | 0 | return 0; |
875 | 0 | } |
876 | | |
877 | 0 | if (session != NULL) { |
878 | 0 | SSL_SESSION_up_ref(session); |
879 | 0 | sc->verify_result = session->verify_result; |
880 | 0 | } |
881 | 0 | SSL_SESSION_free(sc->session); |
882 | 0 | sc->session = session; |
883 | |
|
884 | 0 | return 1; |
885 | 0 | } |
886 | | |
887 | | int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid, |
888 | | unsigned int sid_len) |
889 | 0 | { |
890 | 0 | if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
891 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_TOO_LONG); |
892 | 0 | return 0; |
893 | 0 | } |
894 | 0 | s->session_id_length = sid_len; |
895 | 0 | if (sid != s->session_id) |
896 | 0 | memcpy(s->session_id, sid, sid_len); |
897 | 0 | return 1; |
898 | 0 | } |
899 | | |
900 | | long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) |
901 | 0 | { |
902 | 0 | OSSL_TIME new_timeout = ossl_seconds2time(t); |
903 | |
|
904 | 0 | if (s == NULL || t < 0) |
905 | 0 | return 0; |
906 | 0 | if (s->owner != NULL) { |
907 | 0 | if (!CRYPTO_THREAD_write_lock(s->owner->lock)) |
908 | 0 | return 0; |
909 | 0 | s->timeout = new_timeout; |
910 | 0 | ssl_session_calculate_timeout(s); |
911 | 0 | SSL_SESSION_list_add(s->owner, s); |
912 | 0 | CRYPTO_THREAD_unlock(s->owner->lock); |
913 | 0 | } else { |
914 | 0 | s->timeout = new_timeout; |
915 | 0 | ssl_session_calculate_timeout(s); |
916 | 0 | } |
917 | 0 | return 1; |
918 | 0 | } |
919 | | |
920 | | long SSL_SESSION_get_timeout(const SSL_SESSION *s) |
921 | 0 | { |
922 | 0 | if (s == NULL) |
923 | 0 | return 0; |
924 | 0 | return (long)ossl_time_to_time_t(s->timeout); |
925 | 0 | } |
926 | | |
927 | | long SSL_SESSION_get_time(const SSL_SESSION *s) |
928 | 0 | { |
929 | 0 | if (s == NULL) |
930 | 0 | return 0; |
931 | 0 | return (long)ossl_time_to_time_t(s->time); |
932 | 0 | } |
933 | | |
934 | | long SSL_SESSION_set_time(SSL_SESSION *s, long t) |
935 | 0 | { |
936 | 0 | OSSL_TIME new_time = ossl_time_from_time_t((time_t)t); |
937 | |
|
938 | 0 | if (s == NULL) |
939 | 0 | return 0; |
940 | 0 | if (s->owner != NULL) { |
941 | 0 | if (!CRYPTO_THREAD_write_lock(s->owner->lock)) |
942 | 0 | return 0; |
943 | 0 | s->time = new_time; |
944 | 0 | ssl_session_calculate_timeout(s); |
945 | 0 | SSL_SESSION_list_add(s->owner, s); |
946 | 0 | CRYPTO_THREAD_unlock(s->owner->lock); |
947 | 0 | } else { |
948 | 0 | s->time = new_time; |
949 | 0 | ssl_session_calculate_timeout(s); |
950 | 0 | } |
951 | 0 | return t; |
952 | 0 | } |
953 | | |
954 | | int SSL_SESSION_get_protocol_version(const SSL_SESSION *s) |
955 | 0 | { |
956 | 0 | return s->ssl_version; |
957 | 0 | } |
958 | | |
959 | | int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version) |
960 | 0 | { |
961 | 0 | s->ssl_version = version; |
962 | 0 | return 1; |
963 | 0 | } |
964 | | |
965 | | const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s) |
966 | 0 | { |
967 | 0 | return s->cipher; |
968 | 0 | } |
969 | | |
970 | | int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher) |
971 | 0 | { |
972 | 0 | s->cipher = cipher; |
973 | 0 | return 1; |
974 | 0 | } |
975 | | |
976 | | const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s) |
977 | 0 | { |
978 | 0 | return s->ext.hostname; |
979 | 0 | } |
980 | | |
981 | | int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname) |
982 | 0 | { |
983 | 0 | OPENSSL_free(s->ext.hostname); |
984 | 0 | if (hostname == NULL) { |
985 | 0 | s->ext.hostname = NULL; |
986 | 0 | return 1; |
987 | 0 | } |
988 | 0 | s->ext.hostname = OPENSSL_strdup(hostname); |
989 | |
|
990 | 0 | return s->ext.hostname != NULL; |
991 | 0 | } |
992 | | |
993 | | int SSL_SESSION_has_ticket(const SSL_SESSION *s) |
994 | 0 | { |
995 | 0 | return (s->ext.ticklen > 0) ? 1 : 0; |
996 | 0 | } |
997 | | |
998 | | unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s) |
999 | 0 | { |
1000 | 0 | return s->ext.tick_lifetime_hint; |
1001 | 0 | } |
1002 | | |
1003 | | void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick, |
1004 | | size_t *len) |
1005 | 0 | { |
1006 | 0 | *len = s->ext.ticklen; |
1007 | 0 | if (tick != NULL) |
1008 | 0 | *tick = s->ext.tick; |
1009 | 0 | } |
1010 | | |
1011 | | uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s) |
1012 | 0 | { |
1013 | 0 | return s->ext.max_early_data; |
1014 | 0 | } |
1015 | | |
1016 | | int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data) |
1017 | 0 | { |
1018 | 0 | s->ext.max_early_data = max_early_data; |
1019 | |
|
1020 | 0 | return 1; |
1021 | 0 | } |
1022 | | |
1023 | | void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s, |
1024 | | const unsigned char **alpn, |
1025 | | size_t *len) |
1026 | 0 | { |
1027 | 0 | *alpn = s->ext.alpn_selected; |
1028 | 0 | *len = s->ext.alpn_selected_len; |
1029 | 0 | } |
1030 | | |
1031 | | int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn, |
1032 | | size_t len) |
1033 | 0 | { |
1034 | 0 | OPENSSL_free(s->ext.alpn_selected); |
1035 | 0 | if (alpn == NULL || len == 0) { |
1036 | 0 | s->ext.alpn_selected = NULL; |
1037 | 0 | s->ext.alpn_selected_len = 0; |
1038 | 0 | return 1; |
1039 | 0 | } |
1040 | 0 | s->ext.alpn_selected = OPENSSL_memdup(alpn, len); |
1041 | 0 | if (s->ext.alpn_selected == NULL) { |
1042 | 0 | s->ext.alpn_selected_len = 0; |
1043 | 0 | return 0; |
1044 | 0 | } |
1045 | 0 | s->ext.alpn_selected_len = len; |
1046 | |
|
1047 | 0 | return 1; |
1048 | 0 | } |
1049 | | |
1050 | | X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) |
1051 | 0 | { |
1052 | 0 | return s->peer; |
1053 | 0 | } |
1054 | | |
1055 | | EVP_PKEY *SSL_SESSION_get0_peer_rpk(SSL_SESSION *s) |
1056 | 0 | { |
1057 | 0 | return s->peer_rpk; |
1058 | 0 | } |
1059 | | |
1060 | | int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx, |
1061 | | unsigned int sid_ctx_len) |
1062 | 0 | { |
1063 | 0 | if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { |
1064 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
1065 | 0 | return 0; |
1066 | 0 | } |
1067 | 0 | s->sid_ctx_length = sid_ctx_len; |
1068 | 0 | if (sid_ctx != s->sid_ctx) |
1069 | 0 | memcpy(s->sid_ctx, sid_ctx, sid_ctx_len); |
1070 | |
|
1071 | 0 | return 1; |
1072 | 0 | } |
1073 | | |
1074 | | int SSL_SESSION_is_resumable(const SSL_SESSION *s) |
1075 | 0 | { |
1076 | | /* |
1077 | | * In the case of EAP-FAST, we can have a pre-shared "ticket" without a |
1078 | | * session ID. |
1079 | | */ |
1080 | 0 | return !s->not_resumable |
1081 | 0 | && (s->session_id_length > 0 || s->ext.ticklen > 0); |
1082 | 0 | } |
1083 | | |
1084 | | long SSL_CTX_set_timeout(SSL_CTX *s, long t) |
1085 | 0 | { |
1086 | 0 | long l; |
1087 | |
|
1088 | 0 | if (s == NULL) |
1089 | 0 | return 0; |
1090 | 0 | l = (long)ossl_time2seconds(s->session_timeout); |
1091 | 0 | s->session_timeout = ossl_seconds2time(t); |
1092 | 0 | return l; |
1093 | 0 | } |
1094 | | |
1095 | | long SSL_CTX_get_timeout(const SSL_CTX *s) |
1096 | 0 | { |
1097 | 0 | if (s == NULL) |
1098 | 0 | return 0; |
1099 | 0 | return (long)ossl_time2seconds(s->session_timeout); |
1100 | 0 | } |
1101 | | |
1102 | | int SSL_set_session_secret_cb(SSL *s, |
1103 | | tls_session_secret_cb_fn tls_session_secret_cb, |
1104 | | void *arg) |
1105 | 0 | { |
1106 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1107 | |
|
1108 | 0 | if (sc == NULL) |
1109 | 0 | return 0; |
1110 | | |
1111 | 0 | sc->ext.session_secret_cb = tls_session_secret_cb; |
1112 | 0 | sc->ext.session_secret_cb_arg = arg; |
1113 | 0 | return 1; |
1114 | 0 | } |
1115 | | |
1116 | | int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb, |
1117 | | void *arg) |
1118 | 0 | { |
1119 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1120 | |
|
1121 | 0 | if (sc == NULL) |
1122 | 0 | return 0; |
1123 | | |
1124 | 0 | sc->ext.session_ticket_cb = cb; |
1125 | 0 | sc->ext.session_ticket_cb_arg = arg; |
1126 | 0 | return 1; |
1127 | 0 | } |
1128 | | |
1129 | | int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len) |
1130 | 0 | { |
1131 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
1132 | |
|
1133 | 0 | if (sc == NULL) |
1134 | 0 | return 0; |
1135 | | |
1136 | 0 | if (sc->version >= TLS1_VERSION) { |
1137 | 0 | OPENSSL_free(sc->ext.session_ticket); |
1138 | 0 | sc->ext.session_ticket = NULL; |
1139 | 0 | sc->ext.session_ticket = |
1140 | 0 | OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len); |
1141 | 0 | if (sc->ext.session_ticket == NULL) |
1142 | 0 | return 0; |
1143 | | |
1144 | 0 | if (ext_data != NULL) { |
1145 | 0 | sc->ext.session_ticket->length = ext_len; |
1146 | 0 | sc->ext.session_ticket->data = sc->ext.session_ticket + 1; |
1147 | 0 | memcpy(sc->ext.session_ticket->data, ext_data, ext_len); |
1148 | 0 | } else { |
1149 | 0 | sc->ext.session_ticket->length = 0; |
1150 | 0 | sc->ext.session_ticket->data = NULL; |
1151 | 0 | } |
1152 | |
|
1153 | 0 | return 1; |
1154 | 0 | } |
1155 | | |
1156 | 0 | return 0; |
1157 | 0 | } |
1158 | | |
1159 | | void SSL_CTX_flush_sessions(SSL_CTX *s, long t) |
1160 | 0 | { |
1161 | 0 | STACK_OF(SSL_SESSION) *sk; |
1162 | 0 | SSL_SESSION *current; |
1163 | 0 | unsigned long i; |
1164 | 0 | const OSSL_TIME timeout = ossl_time_from_time_t(t); |
1165 | |
|
1166 | 0 | if (!CRYPTO_THREAD_write_lock(s->lock)) |
1167 | 0 | return; |
1168 | | |
1169 | 0 | sk = sk_SSL_SESSION_new_null(); |
1170 | 0 | i = lh_SSL_SESSION_get_down_load(s->sessions); |
1171 | 0 | lh_SSL_SESSION_set_down_load(s->sessions, 0); |
1172 | | |
1173 | | /* |
1174 | | * Iterate over the list from the back (oldest), and stop |
1175 | | * when a session can no longer be removed. |
1176 | | * Add the session to a temporary list to be freed outside |
1177 | | * the SSL_CTX lock. |
1178 | | * But still do the remove_session_cb() within the lock. |
1179 | | */ |
1180 | 0 | while (s->session_cache_tail != NULL) { |
1181 | 0 | current = s->session_cache_tail; |
1182 | 0 | if (t == 0 || sess_timedout(timeout, current)) { |
1183 | 0 | lh_SSL_SESSION_delete(s->sessions, current); |
1184 | 0 | SSL_SESSION_list_remove(s, current); |
1185 | 0 | current->not_resumable = 1; |
1186 | 0 | if (s->remove_session_cb != NULL) |
1187 | 0 | s->remove_session_cb(s, current); |
1188 | | /* |
1189 | | * Throw the session on a stack, it's entirely plausible |
1190 | | * that while freeing outside the critical section, the |
1191 | | * session could be re-added, so avoid using the next/prev |
1192 | | * pointers. If the stack failed to create, or the session |
1193 | | * couldn't be put on the stack, just free it here |
1194 | | */ |
1195 | 0 | if (sk == NULL || !sk_SSL_SESSION_push(sk, current)) |
1196 | 0 | SSL_SESSION_free(current); |
1197 | 0 | } else { |
1198 | 0 | break; |
1199 | 0 | } |
1200 | 0 | } |
1201 | |
|
1202 | 0 | lh_SSL_SESSION_set_down_load(s->sessions, i); |
1203 | 0 | CRYPTO_THREAD_unlock(s->lock); |
1204 | |
|
1205 | 0 | sk_SSL_SESSION_pop_free(sk, SSL_SESSION_free); |
1206 | 0 | } |
1207 | | |
1208 | | int ssl_clear_bad_session(SSL_CONNECTION *s) |
1209 | 0 | { |
1210 | 0 | if ((s->session != NULL) && |
1211 | 0 | !(s->shutdown & SSL_SENT_SHUTDOWN) && |
1212 | 0 | !(SSL_in_init(SSL_CONNECTION_GET_SSL(s)) |
1213 | 0 | || SSL_in_before(SSL_CONNECTION_GET_SSL(s)))) { |
1214 | 0 | SSL_CTX_remove_session(s->session_ctx, s->session); |
1215 | 0 | return 1; |
1216 | 0 | } else |
1217 | 0 | return 0; |
1218 | 0 | } |
1219 | | |
1220 | | /* locked by SSL_CTX in the calling function */ |
1221 | | static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) |
1222 | 0 | { |
1223 | 0 | if ((s->next == NULL) || (s->prev == NULL)) |
1224 | 0 | return; |
1225 | | |
1226 | 0 | if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) { |
1227 | | /* last element in list */ |
1228 | 0 | if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) { |
1229 | | /* only one element in list */ |
1230 | 0 | ctx->session_cache_head = NULL; |
1231 | 0 | ctx->session_cache_tail = NULL; |
1232 | 0 | } else { |
1233 | 0 | ctx->session_cache_tail = s->prev; |
1234 | 0 | s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail); |
1235 | 0 | } |
1236 | 0 | } else { |
1237 | 0 | if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) { |
1238 | | /* first element in list */ |
1239 | 0 | ctx->session_cache_head = s->next; |
1240 | 0 | s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
1241 | 0 | } else { |
1242 | | /* middle of list */ |
1243 | 0 | s->next->prev = s->prev; |
1244 | 0 | s->prev->next = s->next; |
1245 | 0 | } |
1246 | 0 | } |
1247 | 0 | s->prev = s->next = NULL; |
1248 | 0 | s->owner = NULL; |
1249 | 0 | } |
1250 | | |
1251 | | static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) |
1252 | 0 | { |
1253 | 0 | SSL_SESSION *next; |
1254 | |
|
1255 | 0 | if ((s->next != NULL) && (s->prev != NULL)) |
1256 | 0 | SSL_SESSION_list_remove(ctx, s); |
1257 | |
|
1258 | 0 | if (ctx->session_cache_head == NULL) { |
1259 | 0 | ctx->session_cache_head = s; |
1260 | 0 | ctx->session_cache_tail = s; |
1261 | 0 | s->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
1262 | 0 | s->next = (SSL_SESSION *)&(ctx->session_cache_tail); |
1263 | 0 | } else { |
1264 | 0 | if (timeoutcmp(s, ctx->session_cache_head) >= 0) { |
1265 | | /* |
1266 | | * if we timeout after (or the same time as) the first |
1267 | | * session, put us first - usual case |
1268 | | */ |
1269 | 0 | s->next = ctx->session_cache_head; |
1270 | 0 | s->next->prev = s; |
1271 | 0 | s->prev = (SSL_SESSION *)&(ctx->session_cache_head); |
1272 | 0 | ctx->session_cache_head = s; |
1273 | 0 | } else if (timeoutcmp(s, ctx->session_cache_tail) < 0) { |
1274 | | /* if we timeout before the last session, put us last */ |
1275 | 0 | s->prev = ctx->session_cache_tail; |
1276 | 0 | s->prev->next = s; |
1277 | 0 | s->next = (SSL_SESSION *)&(ctx->session_cache_tail); |
1278 | 0 | ctx->session_cache_tail = s; |
1279 | 0 | } else { |
1280 | | /* |
1281 | | * we timeout somewhere in-between - if there is only |
1282 | | * one session in the cache it will be caught above |
1283 | | */ |
1284 | 0 | next = ctx->session_cache_head->next; |
1285 | 0 | while (next != (SSL_SESSION*)&(ctx->session_cache_tail)) { |
1286 | 0 | if (timeoutcmp(s, next) >= 0) { |
1287 | 0 | s->next = next; |
1288 | 0 | s->prev = next->prev; |
1289 | 0 | next->prev->next = s; |
1290 | 0 | next->prev = s; |
1291 | 0 | break; |
1292 | 0 | } |
1293 | 0 | next = next->next; |
1294 | 0 | } |
1295 | 0 | } |
1296 | 0 | } |
1297 | 0 | s->owner = ctx; |
1298 | 0 | } |
1299 | | |
1300 | | void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx, |
1301 | | int (*cb) (struct ssl_st *ssl, SSL_SESSION *sess)) |
1302 | 0 | { |
1303 | 0 | ctx->new_session_cb = cb; |
1304 | 0 | } |
1305 | | |
1306 | 0 | int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) { |
1307 | 0 | return ctx->new_session_cb; |
1308 | 0 | } |
1309 | | |
1310 | | void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx, |
1311 | | void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess)) |
1312 | 0 | { |
1313 | 0 | ctx->remove_session_cb = cb; |
1314 | 0 | } |
1315 | | |
1316 | | void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx, |
1317 | 0 | SSL_SESSION *sess) { |
1318 | 0 | return ctx->remove_session_cb; |
1319 | 0 | } |
1320 | | |
1321 | | void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, |
1322 | | SSL_SESSION *(*cb) (SSL *ssl, |
1323 | | const unsigned char *data, |
1324 | | int len, int *copy)) |
1325 | 0 | { |
1326 | 0 | ctx->get_session_cb = cb; |
1327 | 0 | } |
1328 | | |
1329 | | SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl, |
1330 | | const unsigned char |
1331 | | *data, int len, |
1332 | 0 | int *copy) { |
1333 | 0 | return ctx->get_session_cb; |
1334 | 0 | } |
1335 | | |
1336 | | void SSL_CTX_set_info_callback(SSL_CTX *ctx, |
1337 | | void (*cb) (const SSL *ssl, int type, int val)) |
1338 | 0 | { |
1339 | 0 | ctx->info_callback = cb; |
1340 | 0 | } |
1341 | | |
1342 | | void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type, |
1343 | 0 | int val) { |
1344 | 0 | return ctx->info_callback; |
1345 | 0 | } |
1346 | | |
1347 | | void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, |
1348 | | int (*cb) (SSL *ssl, X509 **x509, |
1349 | | EVP_PKEY **pkey)) |
1350 | 0 | { |
1351 | 0 | ctx->client_cert_cb = cb; |
1352 | 0 | } |
1353 | | |
1354 | | int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509, |
1355 | 0 | EVP_PKEY **pkey) { |
1356 | 0 | return ctx->client_cert_cb; |
1357 | 0 | } |
1358 | | |
1359 | | void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx, |
1360 | | int (*cb) (SSL *ssl, |
1361 | | unsigned char *cookie, |
1362 | | unsigned int *cookie_len)) |
1363 | 0 | { |
1364 | 0 | ctx->app_gen_cookie_cb = cb; |
1365 | 0 | } |
1366 | | |
1367 | | void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx, |
1368 | | int (*cb) (SSL *ssl, |
1369 | | const unsigned char *cookie, |
1370 | | unsigned int cookie_len)) |
1371 | 0 | { |
1372 | 0 | ctx->app_verify_cookie_cb = cb; |
1373 | 0 | } |
1374 | | |
1375 | | int SSL_SESSION_set1_ticket_appdata(SSL_SESSION *ss, const void *data, size_t len) |
1376 | 0 | { |
1377 | 0 | OPENSSL_free(ss->ticket_appdata); |
1378 | 0 | ss->ticket_appdata_len = 0; |
1379 | 0 | if (data == NULL || len == 0) { |
1380 | 0 | ss->ticket_appdata = NULL; |
1381 | 0 | return 1; |
1382 | 0 | } |
1383 | 0 | ss->ticket_appdata = OPENSSL_memdup(data, len); |
1384 | 0 | if (ss->ticket_appdata != NULL) { |
1385 | 0 | ss->ticket_appdata_len = len; |
1386 | 0 | return 1; |
1387 | 0 | } |
1388 | 0 | return 0; |
1389 | 0 | } |
1390 | | |
1391 | | int SSL_SESSION_get0_ticket_appdata(SSL_SESSION *ss, void **data, size_t *len) |
1392 | 0 | { |
1393 | 0 | *data = ss->ticket_appdata; |
1394 | 0 | *len = ss->ticket_appdata_len; |
1395 | 0 | return 1; |
1396 | 0 | } |
1397 | | |
1398 | | void SSL_CTX_set_stateless_cookie_generate_cb( |
1399 | | SSL_CTX *ctx, |
1400 | | int (*cb) (SSL *ssl, |
1401 | | unsigned char *cookie, |
1402 | | size_t *cookie_len)) |
1403 | 0 | { |
1404 | 0 | ctx->gen_stateless_cookie_cb = cb; |
1405 | 0 | } |
1406 | | |
1407 | | void SSL_CTX_set_stateless_cookie_verify_cb( |
1408 | | SSL_CTX *ctx, |
1409 | | int (*cb) (SSL *ssl, |
1410 | | const unsigned char *cookie, |
1411 | | size_t cookie_len)) |
1412 | 0 | { |
1413 | 0 | ctx->verify_stateless_cookie_cb = cb; |
1414 | 0 | } |
1415 | | |
1416 | | IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION) |