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