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