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