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