GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libssl/ssl_sess.c Lines: 131 381 34.4 %
Date: 2017-11-07 Branches: 47 211 22.3 %

Line Branch Exec Source
1
/* $OpenBSD: ssl_sess.c,v 1.71 2017/04/10 17:27:33 jsing Exp $ */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@cryptsoft.com).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * This library is free for commercial and non-commercial use as long as
10
 * the following conditions are aheared to.  The following conditions
11
 * apply to all code found in this distribution, be it the RC4, RSA,
12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13
 * included with this distribution is covered by the same copyright terms
14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15
 *
16
 * Copyright remains Eric Young's, and as such any Copyright notices in
17
 * the code are not to be removed.
18
 * If this package is used in a product, Eric Young should be given attribution
19
 * as the author of the parts of the library used.
20
 * This can be in the form of a textual message at program startup or
21
 * in documentation (online or textual) provided with the package.
22
 *
23
 * Redistribution and use in source and binary forms, with or without
24
 * modification, are permitted provided that the following conditions
25
 * are met:
26
 * 1. Redistributions of source code must retain the copyright
27
 *    notice, this list of conditions and the following disclaimer.
28
 * 2. Redistributions in binary form must reproduce the above copyright
29
 *    notice, this list of conditions and the following disclaimer in the
30
 *    documentation and/or other materials provided with the distribution.
31
 * 3. All advertising materials mentioning features or use of this software
32
 *    must display the following acknowledgement:
33
 *    "This product includes cryptographic software written by
34
 *     Eric Young (eay@cryptsoft.com)"
35
 *    The word 'cryptographic' can be left out if the rouines from the library
36
 *    being used are not cryptographic related :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 * The licence and distribution terms for any publically available version or
54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55
 * copied and put under another distribution licence
56
 * [including the GNU Public Licence.]
57
 */
58
/* ====================================================================
59
 * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60
 *
61
 * Redistribution and use in source and binary forms, with or without
62
 * modification, are permitted provided that the following conditions
63
 * are met:
64
 *
65
 * 1. Redistributions of source code must retain the above copyright
66
 *    notice, this list of conditions and the following disclaimer.
67
 *
68
 * 2. Redistributions in binary form must reproduce the above copyright
69
 *    notice, this list of conditions and the following disclaimer in
70
 *    the documentation and/or other materials provided with the
71
 *    distribution.
72
 *
73
 * 3. All advertising materials mentioning features or use of this
74
 *    software must display the following acknowledgment:
75
 *    "This product includes software developed by the OpenSSL Project
76
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77
 *
78
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79
 *    endorse or promote products derived from this software without
80
 *    prior written permission. For written permission, please contact
81
 *    openssl-core@openssl.org.
82
 *
83
 * 5. Products derived from this software may not be called "OpenSSL"
84
 *    nor may "OpenSSL" appear in their names without prior written
85
 *    permission of the OpenSSL Project.
86
 *
87
 * 6. Redistributions of any form whatsoever must retain the following
88
 *    acknowledgment:
89
 *    "This product includes software developed by the OpenSSL Project
90
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91
 *
92
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103
 * OF THE POSSIBILITY OF SUCH DAMAGE.
104
 * ====================================================================
105
 *
106
 * This product includes cryptographic software written by Eric Young
107
 * (eay@cryptsoft.com).  This product includes software written by Tim
108
 * Hudson (tjh@cryptsoft.com).
109
 *
110
 */
111
/* ====================================================================
112
 * Copyright 2005 Nokia. All rights reserved.
113
 *
114
 * The portions of the attached software ("Contribution") is developed by
115
 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
116
 * license.
117
 *
118
 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
119
 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
120
 * support (see RFC 4279) to OpenSSL.
121
 *
122
 * No patent licenses or other rights except those expressly stated in
123
 * the OpenSSL open source license shall be deemed granted or received
124
 * expressly, by implication, estoppel, or otherwise.
125
 *
126
 * No assurances are provided by Nokia that the Contribution does not
127
 * infringe the patent or other intellectual property rights of any third
128
 * party or that the license provides you with all the necessary rights
129
 * to make use of the Contribution.
130
 *
131
 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
132
 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
133
 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
134
 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
135
 * OTHERWISE.
136
 */
137
138
#include <openssl/lhash.h>
139
140
#ifndef OPENSSL_NO_ENGINE
141
#include <openssl/engine.h>
142
#endif
143
144
#include "ssl_locl.h"
145
146
static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
147
static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
148
static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
149
150
/* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
151
SSL_SESSION *
152
SSL_get_session(const SSL *ssl)
153
{
154
12
	return (ssl->session);
155
}
156
157
/* variant of SSL_get_session: caller really gets something */
158
SSL_SESSION *
159
SSL_get1_session(SSL *ssl)
160
{
161
	SSL_SESSION *sess;
162
163
	/*
164
	 * Need to lock this all up rather than just use CRYPTO_add so that
165
	 * somebody doesn't free ssl->session between when we check it's
166
	 * non-null and when we up the reference count.
167
	 */
168
	CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
169
	sess = ssl->session;
170
	if (sess)
171
		sess->references++;
172
	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
173
174
	return (sess);
175
}
176
177
int
178
SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
179
    CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
180
{
181
	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION,
182
	    argl, argp, new_func, dup_func, free_func);
183
}
184
185
int
186
SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
187
{
188
	return (CRYPTO_set_ex_data(&s->internal->ex_data, idx, arg));
189
}
190
191
void *
192
SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
193
{
194
	return (CRYPTO_get_ex_data(&s->internal->ex_data, idx));
195
}
196
197
SSL_SESSION *
198
SSL_SESSION_new(void)
199
{
200
	SSL_SESSION *ss;
201
202
3506
	if ((ss = calloc(1, sizeof(*ss))) == NULL) {
203
		SSLerrorx(ERR_R_MALLOC_FAILURE);
204
		return (NULL);
205
	}
206
1753
	if ((ss->internal = calloc(1, sizeof(*ss->internal))) == NULL) {
207
		free(ss);
208
		SSLerrorx(ERR_R_MALLOC_FAILURE);
209
		return (NULL);
210
	}
211
212
1753
	ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
213
1753
	ss->references = 1;
214
1753
	ss->timeout=60*5+4; /* 5 minute timeout by default */
215
1753
	ss->time = time(NULL);
216
1753
	ss->internal->prev = NULL;
217
1753
	ss->internal->next = NULL;
218
1753
	ss->tlsext_hostname = NULL;
219
220
1753
	ss->internal->tlsext_ecpointformatlist_length = 0;
221
1753
	ss->internal->tlsext_ecpointformatlist = NULL;
222
1753
	ss->internal->tlsext_supportedgroups_length = 0;
223
1753
	ss->internal->tlsext_supportedgroups = NULL;
224
225
1753
	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->internal->ex_data);
226
227
1753
	return (ss);
228
1753
}
229
230
const unsigned char *
231
SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
232
{
233
	if (len)
234
		*len = s->session_id_length;
235
	return s->session_id;
236
}
237
238
unsigned int
239
SSL_SESSION_get_compress_id(const SSL_SESSION *s)
240
{
241
	return 0;
242
}
243
244
/*
245
 * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
246
 * the ID with random gunk repeatedly until we have no conflict is going to
247
 * complete in one iteration pretty much "most" of the time (btw:
248
 * understatement). So, if it takes us 10 iterations and we still can't avoid
249
 * a conflict - well that's a reasonable point to call it quits. Either the
250
 * arc4random code is broken or someone is trying to open roughly very close to
251
 * 2^128 (or 2^256) SSL sessions to our server. How you might store that many
252
 * sessions is perhaps a more interesting question...
253
 */
254
255
#define MAX_SESS_ID_ATTEMPTS 10
256
257
static int
258
def_generate_session_id(const SSL *ssl, unsigned char *id, unsigned int *id_len)
259
{
260
	unsigned int retry = 0;
261
262
48
	do {
263
24
		arc4random_buf(id, *id_len);
264

24
	} while (SSL_has_matching_session_id(ssl, id, *id_len) &&
265
	    (++retry < MAX_SESS_ID_ATTEMPTS));
266
267
24
	if (retry < MAX_SESS_ID_ATTEMPTS)
268
24
		return 1;
269
270
	/* else - woops a session_id match */
271
	/* XXX We should also check the external cache --
272
	 * but the probability of a collision is negligible, and
273
	 * we could not prevent the concurrent creation of sessions
274
	 * with identical IDs since we currently don't have means
275
	 * to atomically check whether a session ID already exists
276
	 * and make a reservation for it if it does not
277
	 * (this problem applies to the internal cache as well).
278
	 */
279
	return 0;
280
24
}
281
282
int
283
ssl_get_new_session(SSL *s, int session)
284
{
285
2886
	unsigned int tmp;
286
	SSL_SESSION *ss = NULL;
287
	GEN_SESSION_CB cb = def_generate_session_id;
288
289
	/* This gets used by clients and servers. */
290
291
1443
	if ((ss = SSL_SESSION_new()) == NULL)
292
		return (0);
293
294
	/* If the context has a default timeout, use it */
295
1443
	if (s->session_ctx->session_timeout == 0)
296
		ss->timeout = SSL_get_default_timeout(s);
297
	else
298
		ss->timeout = s->session_ctx->session_timeout;
299
300
1443
	if (s->session != NULL) {
301
36
		SSL_SESSION_free(s->session);
302
36
		s->session = NULL;
303
36
	}
304
305
1443
	if (session) {
306

264
		switch (s->version) {
307
		case TLS1_VERSION:
308
		case TLS1_1_VERSION:
309
		case TLS1_2_VERSION:
310
		case DTLS1_VERSION:
311
264
			ss->ssl_version = s->version;
312
264
			ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
313
			break;
314
		default:
315
			SSLerror(s, SSL_R_UNSUPPORTED_SSL_VERSION);
316
			SSL_SESSION_free(ss);
317
			return (0);
318
		}
319
320
		/* If RFC4507 ticket use empty session ID. */
321
264
		if (s->internal->tlsext_ticket_expected) {
322
240
			ss->session_id_length = 0;
323
240
			goto sess_id_done;
324
		}
325
326
		/* Choose which callback will set the session ID. */
327
24
		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
328
24
		if (s->internal->generate_session_id)
329
			cb = s->internal->generate_session_id;
330
24
		else if (s->session_ctx->internal->generate_session_id)
331
			cb = s->session_ctx->internal->generate_session_id;
332
24
		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
333
334
		/* Choose a session ID. */
335
24
		tmp = ss->session_id_length;
336
24
		if (!cb(s, ss->session_id, &tmp)) {
337
			/* The callback failed */
338
			SSLerror(s, SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
339
			SSL_SESSION_free(ss);
340
			return (0);
341
		}
342
343
		/*
344
		 * Don't allow the callback to set the session length to zero.
345
		 * nor set it higher than it was.
346
		 */
347

48
		if (!tmp || (tmp > ss->session_id_length)) {
348
			/* The callback set an illegal length */
349
			SSLerror(s, SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
350
			SSL_SESSION_free(ss);
351
			return (0);
352
		}
353
24
		ss->session_id_length = tmp;
354
355
		/* Finally, check for a conflict. */
356
24
		if (SSL_has_matching_session_id(s, ss->session_id,
357
			ss->session_id_length)) {
358
			SSLerror(s, SSL_R_SSL_SESSION_ID_CONFLICT);
359
			SSL_SESSION_free(ss);
360
			return (0);
361
		}
362
363
sess_id_done:
364
264
		if (s->tlsext_hostname) {
365
			ss->tlsext_hostname = strdup(s->tlsext_hostname);
366
			if (ss->tlsext_hostname == NULL) {
367
				SSLerror(s, ERR_R_INTERNAL_ERROR);
368
				SSL_SESSION_free(ss);
369
				return 0;
370
			}
371
		}
372
	} else {
373
1179
		ss->session_id_length = 0;
374
	}
375
376
1443
	if (s->sid_ctx_length > sizeof ss->sid_ctx) {
377
		SSLerror(s, ERR_R_INTERNAL_ERROR);
378
		SSL_SESSION_free(ss);
379
		return 0;
380
	}
381
382
1443
	memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
383
1443
	ss->sid_ctx_length = s->sid_ctx_length;
384
1443
	s->session = ss;
385
1443
	ss->ssl_version = s->version;
386
1443
	ss->verify_result = X509_V_OK;
387
388
1443
	return (1);
389
1443
}
390
391
/*
392
 * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
393
 * connection. It is only called by servers.
394
 *
395
 *   session_id: points at the session ID in the ClientHello. This code will
396
 *       read past the end of this in order to parse out the session ticket
397
 *       extension, if any.
398
 *   len: the length of the session ID.
399
 *   limit: a pointer to the first byte after the ClientHello.
400
 *
401
 * Returns:
402
 *   -1: error
403
 *    0: a session may have been found.
404
 *
405
 * Side effects:
406
 *   - If a session is found then s->session is pointed at it (after freeing
407
 *     an existing session if need be) and s->verify_result is set from the
408
 *     session.
409
 *   - Both for new and resumed sessions, s->internal->tlsext_ticket_expected is set
410
 *     to 1 if the server should issue a new session ticket (to 0 otherwise).
411
 */
412
int
413
ssl_get_prev_session(SSL *s, unsigned char *session_id, int len,
414
    const unsigned char *limit)
415
{
416
528
	SSL_SESSION *ret = NULL;
417
	int fatal = 0;
418
	int try_session_cache = 1;
419
	int r;
420
421
	/* This is used only by servers. */
422
423
264
	if (len > SSL_MAX_SSL_SESSION_ID_LENGTH)
424
		goto err;
425
426
264
	if (len == 0)
427
264
		try_session_cache = 0;
428
429
	/* Sets s->internal->tlsext_ticket_expected. */
430
264
	r = tls1_process_ticket(s, session_id, len, limit, &ret);
431

264
	switch (r) {
432
	case -1: /* Error during processing */
433
		fatal = 1;
434
		goto err;
435
	case 0: /* No ticket found */
436
	case 1: /* Zero length ticket found */
437
		break; /* Ok to carry on processing session id. */
438
	case 2: /* Ticket found but not decrypted. */
439
	case 3: /* Ticket decrypted, *ret has been set. */
440
		try_session_cache = 0;
441
		break;
442
	default:
443
		abort();
444
	}
445
446

264
	if (try_session_cache && ret == NULL &&
447
	    !(s->session_ctx->internal->session_cache_mode &
448
	     SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
449
		SSL_SESSION data;
450
		data.ssl_version = s->version;
451
		data.session_id_length = len;
452
		memcpy(data.session_id, session_id, len);
453
454
		CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
455
		ret = lh_SSL_SESSION_retrieve(s->session_ctx->internal->sessions, &data);
456
		if (ret != NULL) {
457
			/* Don't allow other threads to steal it. */
458
			CRYPTO_add(&ret->references, 1,
459
			    CRYPTO_LOCK_SSL_SESSION);
460
		}
461
		CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
462
463
		if (ret == NULL)
464
			s->session_ctx->internal->stats.sess_miss++;
465
	}
466
467

264
	if (try_session_cache && ret == NULL &&
468
	    s->session_ctx->internal->get_session_cb != NULL) {
469
		int copy = 1;
470
471
		if ((ret = s->session_ctx->internal->get_session_cb(s,
472
		    session_id, len, &copy))) {
473
			s->session_ctx->internal->stats.sess_cb_hit++;
474
475
			/*
476
			 * Increment reference count now if the session
477
			 * callback asks us to do so (note that if the session
478
			 * structures returned by the callback are shared
479
			 * between threads, it must handle the reference count
480
			 * itself [i.e. copy == 0], or things won't be
481
			 * thread-safe).
482
			 */
483
			if (copy)
484
				CRYPTO_add(&ret->references, 1,
485
				    CRYPTO_LOCK_SSL_SESSION);
486
487
			/*
488
			 * Add the externally cached session to the internal
489
			 * cache as well if and only if we are supposed to.
490
			 */
491
			if (!(s->session_ctx->internal->session_cache_mode &
492
			    SSL_SESS_CACHE_NO_INTERNAL_STORE))
493
				/*
494
				 * The following should not return 1,
495
				 * otherwise, things are very strange.
496
				 */
497
				SSL_CTX_add_session(s->session_ctx, ret);
498
		}
499
	}
500
501
264
	if (ret == NULL)
502
		goto err;
503
504
	/* Now ret is non-NULL and we own one of its reference counts. */
505
506
	if (ret->sid_ctx_length != s->sid_ctx_length ||
507
	    timingsafe_memcmp(ret->sid_ctx,
508
		s->sid_ctx, ret->sid_ctx_length) != 0) {
509
		/* We have the session requested by the client, but we don't
510
		 * want to use it in this context. */
511
		goto err; /* treat like cache miss */
512
	}
513
514
	if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
515
		/*
516
		 * We can't be sure if this session is being used out of
517
		 * context, which is especially important for SSL_VERIFY_PEER.
518
		 * The application should have used
519
		 * SSL[_CTX]_set_session_id_context.
520
		 *
521
		 * For this error case, we generate an error instead of treating
522
		 * the event like a cache miss (otherwise it would be easy for
523
		 * applications to effectively disable the session cache by
524
		 * accident without anyone noticing).
525
		 */
526
		SSLerror(s, SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
527
		fatal = 1;
528
		goto err;
529
	}
530
531
	if (ret->cipher == NULL) {
532
		ret->cipher = ssl3_get_cipher_by_id(ret->cipher_id);
533
		if (ret->cipher == NULL)
534
			goto err;
535
	}
536
537
	if (ret->timeout < (time(NULL) - ret->time)) {
538
		/* timeout */
539
		s->session_ctx->internal->stats.sess_timeout++;
540
		if (try_session_cache) {
541
			/* session was from the cache, so remove it */
542
			SSL_CTX_remove_session(s->session_ctx, ret);
543
		}
544
		goto err;
545
	}
546
547
	s->session_ctx->internal->stats.sess_hit++;
548
549
	if (s->session != NULL)
550
		SSL_SESSION_free(s->session);
551
	s->session = ret;
552
	s->verify_result = s->session->verify_result;
553
	return 1;
554
555
err:
556
264
	if (ret != NULL) {
557
		SSL_SESSION_free(ret);
558
		if (!try_session_cache) {
559
			/*
560
			 * The session was from a ticket, so we should
561
			 * issue a ticket for the new session.
562
			 */
563
			s->internal->tlsext_ticket_expected = 1;
564
		}
565
	}
566
264
	if (fatal)
567
		return -1;
568
	else
569
264
		return 0;
570
264
}
571
572
int
573
SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
574
{
575
	int ret = 0;
576
	SSL_SESSION *s;
577
578
	/*
579
	 * Add just 1 reference count for the SSL_CTX's session cache
580
	 * even though it has two ways of access: each session is in a
581
	 * doubly linked list and an lhash.
582
	 */
583
	CRYPTO_add(&c->references, 1, CRYPTO_LOCK_SSL_SESSION);
584
585
	/*
586
	 * If session c is in already in cache, we take back the increment
587
	 * later.
588
	 */
589
	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
590
	s = lh_SSL_SESSION_insert(ctx->internal->sessions, c);
591
592
	/*
593
	 * s != NULL iff we already had a session with the given PID.
594
	 * In this case, s == c should hold (then we did not really modify
595
	 * ctx->internal->sessions), or we're in trouble.
596
	 */
597
	if (s != NULL && s != c) {
598
		/* We *are* in trouble ... */
599
		SSL_SESSION_list_remove(ctx, s);
600
		SSL_SESSION_free(s);
601
		/*
602
		 * ... so pretend the other session did not exist in cache
603
		 * (we cannot handle two SSL_SESSION structures with identical
604
		 * session ID in the same cache, which could happen e.g. when
605
		 * two threads concurrently obtain the same session from an
606
		 * external cache).
607
		 */
608
		s = NULL;
609
	}
610
611
	/* Put at the head of the queue unless it is already in the cache */
612
	if (s == NULL)
613
		SSL_SESSION_list_add(ctx, c);
614
615
	if (s != NULL) {
616
		/*
617
		 * existing cache entry -- decrement previously incremented
618
		 * reference count because it already takes into account the
619
		 * cache.
620
		 */
621
		SSL_SESSION_free(s); /* s == c */
622
		ret = 0;
623
	} else {
624
		/*
625
		 * New cache entry -- remove old ones if cache has become
626
		 * too large.
627
		 */
628
629
		ret = 1;
630
631
		if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
632
			while (SSL_CTX_sess_number(ctx) >
633
			    SSL_CTX_sess_get_cache_size(ctx)) {
634
				if (!remove_session_lock(ctx,
635
				    ctx->internal->session_cache_tail, 0))
636
					break;
637
				else
638
					ctx->internal->stats.sess_cache_full++;
639
			}
640
		}
641
	}
642
	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
643
	return (ret);
644
}
645
646
int
647
SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
648
{
649
	return remove_session_lock(ctx, c, 1);
650
}
651
652
static int
653
remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
654
{
655
	SSL_SESSION *r;
656
	int ret = 0;
657
658
	if ((c != NULL) && (c->session_id_length != 0)) {
659
		if (lck)
660
			CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
661
		if ((r = lh_SSL_SESSION_retrieve(ctx->internal->sessions, c)) == c) {
662
			ret = 1;
663
			r = lh_SSL_SESSION_delete(ctx->internal->sessions, c);
664
			SSL_SESSION_list_remove(ctx, c);
665
		}
666
		if (lck)
667
			CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
668
669
		if (ret) {
670
			r->internal->not_resumable = 1;
671
			if (ctx->internal->remove_session_cb != NULL)
672
				ctx->internal->remove_session_cb(ctx, r);
673
			SSL_SESSION_free(r);
674
		}
675
	} else
676
		ret = 0;
677
	return (ret);
678
}
679
680
void
681
SSL_SESSION_free(SSL_SESSION *ss)
682
{
683
	int i;
684
685
3506
	if (ss == NULL)
686
		return;
687
688
1753
	i = CRYPTO_add(&ss->references, -1, CRYPTO_LOCK_SSL_SESSION);
689
1753
	if (i > 0)
690
		return;
691
692
1753
	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->internal->ex_data);
693
694
1753
	explicit_bzero(ss->master_key, sizeof ss->master_key);
695
1753
	explicit_bzero(ss->session_id, sizeof ss->session_id);
696
697
1753
	ssl_sess_cert_free(ss->internal->sess_cert);
698
699
1753
	X509_free(ss->peer);
700
701
1753
	sk_SSL_CIPHER_free(ss->ciphers);
702
703
1753
	free(ss->tlsext_hostname);
704
1753
	free(ss->tlsext_tick);
705
1753
	free(ss->internal->tlsext_ecpointformatlist);
706
1753
	free(ss->internal->tlsext_supportedgroups);
707
708
1753
	freezero(ss->internal, sizeof(*ss->internal));
709
1753
	freezero(ss, sizeof(*ss));
710
3506
}
711
712
int
713
SSL_set_session(SSL *s, SSL_SESSION *session)
714
{
715
	int ret = 0;
716
	const SSL_METHOD *meth;
717
718
480
	if (session != NULL) {
719
		meth = s->ctx->method->internal->get_ssl_method(session->ssl_version);
720
		if (meth == NULL)
721
			meth = s->method->internal->get_ssl_method(session->ssl_version);
722
		if (meth == NULL) {
723
			SSLerror(s, SSL_R_UNABLE_TO_FIND_SSL_METHOD);
724
			return (0);
725
		}
726
727
		if (meth != s->method) {
728
			if (!SSL_set_ssl_method(s, meth))
729
				return (0);
730
		}
731
732
		/* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
733
		CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
734
		if (s->session != NULL)
735
			SSL_SESSION_free(s->session);
736
		s->session = session;
737
		s->verify_result = s->session->verify_result;
738
		/* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
739
		ret = 1;
740
	} else {
741
240
		if (s->session != NULL) {
742
36
			SSL_SESSION_free(s->session);
743
36
			s->session = NULL;
744
36
		}
745
746
240
		meth = s->ctx->method;
747
240
		if (meth != s->method) {
748
36
			if (!SSL_set_ssl_method(s, meth))
749
				return (0);
750
		}
751
		ret = 1;
752
	}
753
240
	return (ret);
754
240
}
755
756
long
757
SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
758
{
759
	if (s == NULL)
760
		return (0);
761
	s->timeout = t;
762
	return (1);
763
}
764
765
long
766
SSL_SESSION_get_timeout(const SSL_SESSION *s)
767
{
768
	if (s == NULL)
769
		return (0);
770
	return (s->timeout);
771
}
772
773
/* XXX 2038 */
774
long
775
SSL_SESSION_get_time(const SSL_SESSION *s)
776
{
777
	if (s == NULL)
778
		return (0);
779
	return (s->time);
780
}
781
782
/* XXX 2038 */
783
long
784
SSL_SESSION_set_time(SSL_SESSION *s, long t)
785
{
786
	if (s == NULL)
787
		return (0);
788
	s->time = t;
789
	return (t);
790
}
791
792
X509 *
793
SSL_SESSION_get0_peer(SSL_SESSION *s)
794
{
795
4
	return s->peer;
796
}
797
798
int
799
SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
800
    unsigned int sid_ctx_len)
801
{
802
	if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
803
		SSLerrorx(SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
804
		return 0;
805
	}
806
	s->sid_ctx_length = sid_ctx_len;
807
	memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
808
809
	return 1;
810
}
811
812
long
813
SSL_CTX_set_timeout(SSL_CTX *s, long t)
814
{
815
	long l;
816
817
	if (s == NULL)
818
		return (0);
819
	l = s->session_timeout;
820
	s->session_timeout = t;
821
822
	return (l);
823
}
824
825
long
826
SSL_CTX_get_timeout(const SSL_CTX *s)
827
{
828
	if (s == NULL)
829
		return (0);
830
	return (s->session_timeout);
831
}
832
833
int
834
SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s,
835
    void *secret, int *secret_len, STACK_OF(SSL_CIPHER) *peer_ciphers,
836
    SSL_CIPHER **cipher, void *arg), void *arg)
837
{
838
	if (s == NULL)
839
		return (0);
840
	s->internal->tls_session_secret_cb = tls_session_secret_cb;
841
	s->internal->tls_session_secret_cb_arg = arg;
842
	return (1);
843
}
844
845
int
846
SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
847
    void *arg)
848
{
849
	if (s == NULL)
850
		return (0);
851
	s->internal->tls_session_ticket_ext_cb = cb;
852
	s->internal->tls_session_ticket_ext_cb_arg = arg;
853
	return (1);
854
}
855
856
int
857
SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
858
{
859
16
	if (s->version >= TLS1_VERSION) {
860
8
		free(s->internal->tlsext_session_ticket);
861
8
		s->internal->tlsext_session_ticket =
862
8
		    malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
863
8
		if (!s->internal->tlsext_session_ticket) {
864
			SSLerror(s, ERR_R_MALLOC_FAILURE);
865
			return 0;
866
		}
867
868
8
		if (ext_data) {
869
4
			s->internal->tlsext_session_ticket->length = ext_len;
870
4
			s->internal->tlsext_session_ticket->data =
871
4
			    s->internal->tlsext_session_ticket + 1;
872
4
			memcpy(s->internal->tlsext_session_ticket->data,
873
			    ext_data, ext_len);
874
4
		} else {
875
4
			s->internal->tlsext_session_ticket->length = 0;
876
4
			s->internal->tlsext_session_ticket->data = NULL;
877
		}
878
879
8
		return 1;
880
	}
881
882
	return 0;
883
8
}
884
885
typedef struct timeout_param_st {
886
	SSL_CTX *ctx;
887
	long time;
888
	struct lhash_st_SSL_SESSION *cache;
889
} TIMEOUT_PARAM;
890
891
static void
892
timeout_doall_arg(SSL_SESSION *s, TIMEOUT_PARAM *p)
893
{
894
	if ((p->time == 0) || (p->time > (s->time + s->timeout))) {
895
		/* timeout */
896
		/* The reason we don't call SSL_CTX_remove_session() is to
897
		 * save on locking overhead */
898
		(void)lh_SSL_SESSION_delete(p->cache, s);
899
		SSL_SESSION_list_remove(p->ctx, s);
900
		s->internal->not_resumable = 1;
901
		if (p->ctx->internal->remove_session_cb != NULL)
902
			p->ctx->internal->remove_session_cb(p->ctx, s);
903
		SSL_SESSION_free(s);
904
	}
905
}
906
907
static void
908
timeout_LHASH_DOALL_ARG(void *arg1, void *arg2)
909
{
910
	SSL_SESSION *a = arg1;
911
	TIMEOUT_PARAM *b = arg2;
912
913
	timeout_doall_arg(a, b);
914
}
915
916
/* XXX 2038 */
917
void
918
SSL_CTX_flush_sessions(SSL_CTX *s, long t)
919
{
920
	unsigned long i;
921
1658
	TIMEOUT_PARAM tp;
922
923
829
	tp.ctx = s;
924
829
	tp.cache = s->internal->sessions;
925
829
	if (tp.cache == NULL)
926
		return;
927
829
	tp.time = t;
928
829
	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
929
829
	i = CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load;
930
829
	CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = 0;
931
829
	lh_SSL_SESSION_doall_arg(tp.cache, timeout_LHASH_DOALL_ARG,
932
	TIMEOUT_PARAM, &tp);
933
829
	CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = i;
934
829
	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
935
1658
}
936
937
int
938
ssl_clear_bad_session(SSL *s)
939
{
940

31365
	if ((s->session != NULL) && !(s->internal->shutdown & SSL_SENT_SHUTDOWN) &&
941
7158
	    !(SSL_in_init(s) || SSL_in_before(s))) {
942
		SSL_CTX_remove_session(s->ctx, s->session);
943
		return (1);
944
	} else
945
11444
		return (0);
946
11444
}
947
948
/* locked by SSL_CTX in the calling function */
949
static void
950
SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
951
{
952
	if ((s->internal->next == NULL) || (s->internal->prev == NULL))
953
		return;
954
955
	if (s->internal->next == (SSL_SESSION *)&(ctx->internal->session_cache_tail)) {
956
		/* last element in list */
957
		if (s->internal->prev == (SSL_SESSION *)&(ctx->internal->session_cache_head)) {
958
			/* only one element in list */
959
			ctx->internal->session_cache_head = NULL;
960
			ctx->internal->session_cache_tail = NULL;
961
		} else {
962
			ctx->internal->session_cache_tail = s->internal->prev;
963
			s->internal->prev->internal->next =
964
			    (SSL_SESSION *)&(ctx->internal->session_cache_tail);
965
		}
966
	} else {
967
		if (s->internal->prev == (SSL_SESSION *)&(ctx->internal->session_cache_head)) {
968
			/* first element in list */
969
			ctx->internal->session_cache_head = s->internal->next;
970
			s->internal->next->internal->prev =
971
			    (SSL_SESSION *)&(ctx->internal->session_cache_head);
972
		} else {
973
			/* middle of list */
974
			s->internal->next->internal->prev = s->internal->prev;
975
			s->internal->prev->internal->next = s->internal->next;
976
		}
977
	}
978
	s->internal->prev = s->internal->next = NULL;
979
}
980
981
static void
982
SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
983
{
984
	if ((s->internal->next != NULL) && (s->internal->prev != NULL))
985
		SSL_SESSION_list_remove(ctx, s);
986
987
	if (ctx->internal->session_cache_head == NULL) {
988
		ctx->internal->session_cache_head = s;
989
		ctx->internal->session_cache_tail = s;
990
		s->internal->prev = (SSL_SESSION *)&(ctx->internal->session_cache_head);
991
		s->internal->next = (SSL_SESSION *)&(ctx->internal->session_cache_tail);
992
	} else {
993
		s->internal->next = ctx->internal->session_cache_head;
994
		s->internal->next->internal->prev = s;
995
		s->internal->prev = (SSL_SESSION *)&(ctx->internal->session_cache_head);
996
		ctx->internal->session_cache_head = s;
997
	}
998
}
999
1000
void
1001
SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
1002
    int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) {
1003
	ctx->internal->new_session_cb = cb;
1004
}
1005
1006
int
1007
(*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
1008
{
1009
	return ctx->internal->new_session_cb;
1010
}
1011
1012
void
1013
SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
1014
    void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess))
1015
{
1016
	ctx->internal->remove_session_cb = cb;
1017
}
1018
1019
void
1020
(*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx, SSL_SESSION *sess)
1021
{
1022
	return ctx->internal->remove_session_cb;
1023
}
1024
1025
void
1026
SSL_CTX_sess_set_get_cb(SSL_CTX *ctx, SSL_SESSION *(*cb)(struct ssl_st *ssl,
1027
    unsigned char *data, int len, int *copy))
1028
{
1029
	ctx->internal->get_session_cb = cb;
1030
}
1031
1032
SSL_SESSION *
1033
(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, unsigned char *data,
1034
    int len, int *copy)
1035
{
1036
	return ctx->internal->get_session_cb;
1037
}
1038
1039
void
1040
SSL_CTX_set_info_callback(SSL_CTX *ctx,
1041
    void (*cb)(const SSL *ssl, int type, int val))
1042
{
1043
	ctx->internal->info_callback = cb;
1044
}
1045
1046
void
1047
(*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type, int val)
1048
{
1049
	return ctx->internal->info_callback;
1050
}
1051
1052
void
1053
SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1054
    int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
1055
{
1056
	ctx->internal->client_cert_cb = cb;
1057
}
1058
1059
int
1060
(*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509,
1061
    EVP_PKEY **pkey)
1062
{
1063
	return ctx->internal->client_cert_cb;
1064
}
1065
1066
#ifndef OPENSSL_NO_ENGINE
1067
int
1068
SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
1069
{
1070
	if (!ENGINE_init(e)) {
1071
		SSLerrorx(ERR_R_ENGINE_LIB);
1072
		return 0;
1073
	}
1074
	if (!ENGINE_get_ssl_client_cert_function(e)) {
1075
		SSLerrorx(SSL_R_NO_CLIENT_CERT_METHOD);
1076
		ENGINE_finish(e);
1077
		return 0;
1078
	}
1079
	ctx->internal->client_cert_engine = e;
1080
	return 1;
1081
}
1082
#endif
1083
1084
void
1085
SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
1086
    int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
1087
{
1088
	ctx->internal->app_gen_cookie_cb = cb;
1089
}
1090
1091
void
1092
SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
1093
    int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len))
1094
{
1095
	ctx->internal->app_verify_cookie_cb = cb;
1096
}
1097
1098
int
1099
PEM_write_SSL_SESSION(FILE *fp, SSL_SESSION *x)
1100
{
1101
	return PEM_ASN1_write((i2d_of_void *)i2d_SSL_SESSION,
1102
	    PEM_STRING_SSL_SESSION, fp, x, NULL, NULL, 0, NULL, NULL);
1103
}
1104
1105
SSL_SESSION *
1106
PEM_read_SSL_SESSION(FILE *fp, SSL_SESSION **x, pem_password_cb *cb, void *u)
1107
{
1108
	return PEM_ASN1_read((d2i_of_void *)d2i_SSL_SESSION,
1109
	    PEM_STRING_SSL_SESSION, fp, (void **)x, cb, u);
1110
}
1111
1112
SSL_SESSION *
1113
PEM_read_bio_SSL_SESSION(BIO *bp, SSL_SESSION **x, pem_password_cb *cb, void *u)
1114
{
1115
2
	return PEM_ASN1_read_bio((d2i_of_void *)d2i_SSL_SESSION,
1116
4
	    PEM_STRING_SSL_SESSION, bp, (void **)x, cb, u);
1117
}
1118
1119
int
1120
PEM_write_bio_SSL_SESSION(BIO *bp, SSL_SESSION *x)
1121
{
1122
4
	return PEM_ASN1_write_bio((i2d_of_void *)i2d_SSL_SESSION,
1123
8
	    PEM_STRING_SSL_SESSION, bp, x, NULL, NULL, 0, NULL, NULL);
1124
}