summaryrefslogtreecommitdiff
path: root/src/libstrongswan/credentials/keys/key_encoding.c
blob: 89b25226c30bd42c19f79184a49c0f4d21714e09 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright (C) 2009 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.
 */

#include "key_encoding.h"

#include <stdint.h>

#include <utils/linked_list.h>
#include <utils/hashtable.h>
#include <threading/rwlock.h>

typedef struct private_key_encoding_t private_key_encoding_t;

/**
 * Private data of an key_encoding_t object.
 */
struct private_key_encoding_t {

	/**
	 * Public key_encoding_t interface.
	 */
	key_encoding_t public;

	/**
	 * cached encodings, a table for each encoding_type_t, containing chunk_t*
	 */
	hashtable_t *cache[KEY_ENCODING_MAX];

	/**
	 * Registered encoding fuctions, key_encoder_t
	 */
	linked_list_t *encoders;

	/**
	 * lock to access cache/encoders
	 */
	rwlock_t *lock;
};

/**
 * See header.
 */
bool key_encoding_args(va_list args, ...)
{
	va_list parts, copy;
	bool failed = FALSE;

	va_start(parts, args);

	while (!failed)
	{
		key_encoding_part_t current, target;
		chunk_t *out, data;

		/* get the part we are looking for */
		target = va_arg(parts, key_encoding_part_t);
		if (target == KEY_PART_END)
		{
			break;
		}
		out = va_arg(parts, chunk_t*);

		va_copy(copy, args);
		while (!failed)
		{
			current = va_arg(copy, key_encoding_part_t);
			if (current == KEY_PART_END)
			{
				failed = TRUE;
				break;
			}
			data = va_arg(copy, chunk_t);
			if (current == target)
			{
				*out = data;
				break;
			}
		}
		va_end(copy);
	}
	va_end(parts);
	return !failed;
}

/**
 * hashtable hash() function
 */
static u_int hash(void *key)
{
	return (uintptr_t)key;
}

/**
 * hashtable equals() function
 */
static bool equals(void *key1, void *key2)
{
	return key1 == key2;
}

/**
 * Implementation of key_encoding_t.get_cache
 */
static bool get_cache(private_key_encoding_t *this, key_encoding_type_t type,
					  void *cache, chunk_t *encoding)
{
	chunk_t *chunk;

	if (type >= KEY_ENCODING_MAX || type < 0)
	{
		return FALSE;
	}
	this->lock->read_lock(this->lock);
	chunk = this->cache[type]->get(this->cache[type], cache);
	if (chunk)
	{
		*encoding = *chunk;
	}
	this->lock->unlock(this->lock);
	return !!chunk;
}

/**
 * Implementation of key_encoding_t.encode
 */
static bool encode(private_key_encoding_t *this, key_encoding_type_t type,
				   void *cache, chunk_t *encoding, ...)
{
	enumerator_t *enumerator;
	va_list args, copy;
	key_encoder_t encode;
	bool success = FALSE;
	chunk_t *chunk;

	if (type >= KEY_ENCODING_MAX || type < 0)
	{
		return FALSE;
	}
	this->lock->read_lock(this->lock);
	if (cache)
	{
		chunk = this->cache[type]->get(this->cache[type], cache);
		if (chunk)
		{
			*encoding = *chunk;
			this->lock->unlock(this->lock);
			return TRUE;
		}
	}
	va_start(args, encoding);
	enumerator = this->encoders->create_enumerator(this->encoders);
	while (enumerator->enumerate(enumerator, &encode))
	{
		va_copy(copy, args);
		success = encode(type, encoding, copy);
		va_end(copy);
		if (success)
		{
			if (cache)
			{
				chunk = malloc_thing(chunk_t);
				*chunk = *encoding;
				this->lock->unlock(this->lock);
				this->lock->write_lock(this->lock);
				this->cache[type]->put(this->cache[type], cache, chunk);
			}
			break;
		}
	}
	enumerator->destroy(enumerator);
	va_end(args);
	this->lock->unlock(this->lock);
	return success;
}

/**
 * Implementation of key_encoding_t.cache
 */
static void cache(private_key_encoding_t *this, key_encoding_type_t type,
				  void *cache, chunk_t encoding)
{
	chunk_t *chunk;

	if (type >= KEY_ENCODING_MAX || type < 0)
	{
		return free(encoding.ptr);
	}
	chunk = malloc_thing(chunk_t);
	*chunk = encoding;
	this->lock->write_lock(this->lock);
	chunk = this->cache[type]->put(this->cache[type], cache, chunk);
	this->lock->unlock(this->lock);
	/* free an encoding already associated to the cache */
	if (chunk)
	{
		free(chunk->ptr);
		free(chunk);
	}
}

/**
 * Implementation of key_encoding_t.clear_cache
 */
static void clear_cache(private_key_encoding_t *this, void *cache)
{
	key_encoding_type_t type;
	chunk_t *chunk;

	this->lock->write_lock(this->lock);
	for (type = 0; type < KEY_ENCODING_MAX; type++)
	{
		chunk = this->cache[type]->remove(this->cache[type], cache);
		if (chunk)
		{
			chunk_free(chunk);
			free(chunk);
		}
	}
	this->lock->unlock(this->lock);
}

/**
 * Implementation of key_encoding_t.add_encoder
 */
static void add_encoder(private_key_encoding_t *this, key_encoder_t encoder)
{
	this->lock->write_lock(this->lock);
	this->encoders->insert_last(this->encoders, encoder);
	this->lock->unlock(this->lock);
}

/**
 * Implementation of key_encoding_t.remove_encoder
 */
static void remove_encoder(private_key_encoding_t *this, key_encoder_t encoder)
{
	this->lock->write_lock(this->lock);
	this->encoders->remove(this->encoders, encoder, NULL);
	this->lock->unlock(this->lock);
}

/**
 * Implementation of key_encoder_t.destroy.
 */
static void destroy(private_key_encoding_t *this)
{
	key_encoding_type_t type;

	for (type = 0; type < KEY_ENCODING_MAX; type++)
	{
		/* We explicitly do not free remaining encodings. All keys should
		 * have gone now, and they are responsible for cleaning out their
		 * cache entries. Not flushing here allows the leak detective to
		 * complain if a key did not flush cached encodings. */
		this->cache[type]->destroy(this->cache[type]);
	}
	this->encoders->destroy(this->encoders);
	this->lock->destroy(this->lock);
	free(this);
}

/**
 * See header
 */
key_encoding_t *key_encoding_create()
{
	private_key_encoding_t *this = malloc_thing(private_key_encoding_t);
	key_encoding_type_t type;

	this->public.encode = (bool(*)(key_encoding_t*, key_encoding_type_t type, void *cache, chunk_t *encoding, ...))encode;
	this->public.get_cache = (bool(*)(key_encoding_t*, key_encoding_type_t type, void *cache, chunk_t *encoding))get_cache;
	this->public.cache = (void(*)(key_encoding_t*, key_encoding_type_t type, void *cache, chunk_t encoding))cache;
	this->public.clear_cache = (void(*)(key_encoding_t*, void *cache))clear_cache;
	this->public.add_encoder = (void(*)(key_encoding_t*, key_encoder_t encoder))add_encoder;
	this->public.remove_encoder = (void(*)(key_encoding_t*, key_encoder_t encoder))remove_encoder;
	this->public.destroy = (void(*)(key_encoding_t*))destroy;

	for (type = 0; type < KEY_ENCODING_MAX; type++)
	{
		this->cache[type] = hashtable_create(hash, equals, 8);
	}
	this->encoders = linked_list_create();
	this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);

	return &this->public;
}