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