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