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