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