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