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