aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/credentials/sets/cert_cache.c
blob: a300387bf25d7504bff3e0a32a3c7ce195c43838 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright (C) 2008 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * $Id$
 */

#include "cert_cache.h"

#include <daemon.h>
#include <utils/linked_list.h>

#define CACHE_LIMIT 30

typedef struct private_cert_cache_t private_cert_cache_t;
typedef struct relation_t relation_t;

/**
 * private data of cert_cache
 */
struct private_cert_cache_t {

	/**
	 * public functions
	 */
	cert_cache_t public;
	
	/**
	 * list of trusted subject-issuer relations, as relation_t
	 */
	linked_list_t *relations;
};

/**
 * A trusted relation between subject and issuer
 */
struct relation_t {
	/** subject of this relation */
	certificate_t *subject;
	/** issuer of this relation */
	certificate_t *issuer;
	/** time of last use */
	time_t last_use;
};

/**
 * destroy a relation_t structure
 */
static void relation_destroy(relation_t *this)
{
	this->subject->destroy(this->subject);
	this->issuer->destroy(this->issuer);
	free(this);
}

/**
 * Implementation of cert_cache_t.issued_by.
 */
static bool issued_by(private_cert_cache_t *this,
					  certificate_t *subject, certificate_t *issuer)
{
	relation_t *found = NULL, *current, *oldest = NULL;
	enumerator_t *enumerator;
	
	/* lookup cache */
	enumerator = this->relations->create_enumerator(this->relations);
	while (enumerator->enumerate(enumerator, &current))
	{
		if (current->subject == subject && current->issuer == issuer)
		{
			current->last_use = time(NULL);
			found = current;
			break;
		}
		if (oldest == NULL || oldest->last_use <= current->last_use)
		{
			oldest = current;
		}
	}
	enumerator->destroy(enumerator);
	if (found)
	{
		return TRUE;
	}
	/* no cache hit, check signature */
	if (!subject->issued_by(subject, issuer))
	{
		return FALSE;
	}
	/* cache if good, respect cache limit */
	found = malloc_thing(relation_t);
	found->subject = subject->get_ref(subject);
	found->issuer = issuer->get_ref(issuer);
	found->last_use = time(NULL);
	if (this->relations->get_count(this->relations) > CACHE_LIMIT && oldest)
	{
		this->relations->remove(this->relations, oldest, NULL);
		relation_destroy(oldest);
	}
	this->relations->insert_last(this->relations, found);
	return TRUE;
}

/**
 * data associated to a cert enumeration 
 */
typedef struct {
	/** type of requested certificate */
	certificate_type_t cert;
	/** type of requested key */
	key_type_t key;
	/** ID to get a cert from */
	identification_t *id;
} cert_data_t;

/**
 * filter function for certs enumerator
 */
static bool certs_filter(cert_data_t *data, relation_t **in, certificate_t **out)
{
	public_key_t *public;
	certificate_t *cert;
	
	cert = (*in)->subject;
	if (data->key == KEY_ANY && data->id && 
		(data->cert == CERT_ANY || data->cert == CERT_X509_CRL) &&
		cert->get_type(cert) == CERT_X509_CRL)
	{	/* CRL lookup is done using issuer/authkeyidentifier */
		if (cert->has_issuer(cert, data->id))
		{
			*out = cert;
			return TRUE;
		}
	}
	
	if ((data->cert == CERT_ANY || cert->get_type(cert) == data->cert) &&
		(!data->id || cert->has_subject(cert, data->id)))
	{
		if (data->key == KEY_ANY)
		{
			*out = cert;
			return TRUE;
		}
		public = cert->get_public_key(cert);
		if (public)
		{
			if (public->get_type(public) == data->key)
			{
				public->destroy(public);
				*out = cert;
				return TRUE;
			}
			public->destroy(public);
		}
	}
	return FALSE;
}

/**
 * implementation of credential_set_t.create_cert_enumerator
 */
static enumerator_t *create_enumerator(private_cert_cache_t *this,
									   certificate_type_t cert, key_type_t key, 
									   identification_t *id, bool trusted)
{
	cert_data_t *data;
	
	if (trusted)
	{
		return NULL;
	}
	data = malloc_thing(cert_data_t);
	data->cert = cert;
	data->key = key;
	data->id = id;
	
	return enumerator_create_filter(
							this->relations->create_enumerator(this->relations),
							(void*)certs_filter, data, (void*)free);
}

/**
 * Implementation of cert_cache_t.flush.
 */
static void flush(private_cert_cache_t *this, certificate_type_t type)
{
	enumerator_t *enumerator;
	relation_t *relation;
	
	enumerator = this->relations->create_enumerator(this->relations);
	while (enumerator->enumerate(enumerator, &relation))
	{
		if (type == CERT_ANY ||
			type == relation->subject->get_type(relation->subject))
		{
			this->relations->remove_at(this->relations, enumerator);
			relation_destroy(relation);
		}
	}
	enumerator->destroy(enumerator);
}

/**
 * Implementation of cert_cache_t.destroy
 */
static void destroy(private_cert_cache_t *this)
{
	this->relations->destroy_function(this->relations, (void*)relation_destroy);
	free(this);
}

/*
 * see header file
 */
cert_cache_t *cert_cache_create()
{
	private_cert_cache_t *this = malloc_thing(private_cert_cache_t);
	
	this->public.set.create_private_enumerator = (void*)return_null;
	this->public.set.create_cert_enumerator = (void*)create_enumerator;
	this->public.set.create_shared_enumerator = (void*)return_null;
	this->public.set.create_cdp_enumerator = (void*)return_null;
	this->public.issued_by = (bool(*)(cert_cache_t*, certificate_t *subject, certificate_t *issuer))issued_by;
	this->public.flush = (void(*)(cert_cache_t*, certificate_type_t type))flush;
	this->public.destroy = (void(*)(cert_cache_t*))destroy;
	
	this->relations = linked_list_create();
	
	return &this->public;
}