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