added a facility to hand out fingerprinting/key encoding to the pkcs1/pgp/... plugins
[strongswan.git] / src / libstrongswan / credentials / keys / key_encoding.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "key_encoding.h"
17
18 #include <stdint.h>
19
20 #include <utils/linked_list.h>
21 #include <utils/hashtable.h>
22 #include <utils/mutex.h>
23
24 typedef struct private_key_encoding_t private_key_encoding_t;
25
26 /**
27 * Private data of an key_encoding_t object.
28 */
29 struct private_key_encoding_t {
30
31 /**
32 * Public key_encoding_t interface.
33 */
34 key_encoding_t public;
35
36 /**
37 * cached encodings, a table for each encoding_type_t, containing chunk_t*
38 */
39 hashtable_t *cache[KEY_ENCODING_MAX];
40
41 /**
42 * Registered encoding fuctions, key_encoder_t
43 */
44 linked_list_t *encoders;
45
46 /**
47 * lock to access cache/encoders
48 */
49 rwlock_t *lock;
50 };
51
52 /**
53 * See header.
54 */
55 bool key_encoding_args(va_list args, ...)
56 {
57 va_list parts, copy;
58 bool failed = FALSE;
59
60 va_start(parts, args);
61
62 while (!failed)
63 {
64 key_encoding_part_t current, target;
65 chunk_t *out, data;
66
67 /* get the part we are looking for */
68 target = va_arg(parts, key_encoding_part_t);
69 if (target == KEY_PART_END)
70 {
71 break;
72 }
73 out = va_arg(parts, chunk_t*);
74
75 va_copy(copy, args);
76 while (!failed)
77 {
78 current = va_arg(copy, key_encoding_part_t);
79 if (current == KEY_PART_END)
80 {
81 failed = TRUE;
82 break;
83 }
84 data = va_arg(copy, chunk_t);
85 if (current == target)
86 {
87 *out = data;
88 break;
89 }
90 }
91 va_end(copy);
92 }
93 va_end(parts);
94 return !failed;
95 }
96
97 /**
98 * hashtable hash() function
99 */
100 static u_int hash(void *key)
101 {
102 return (uintptr_t)key;
103 }
104
105 /**
106 * hashtable equals() function
107 */
108 static bool equals(void *key1, void *key2)
109 {
110 return key1 == key2;
111 }
112
113 /**
114 * Implementation of key_encoding_t.encode
115 */
116 static bool encode(private_key_encoding_t *this, key_encoding_type_t type,
117 void *cache, chunk_t *encoding, ...)
118 {
119 enumerator_t *enumerator;
120 va_list args, copy;
121 key_encoder_t encode;
122 bool success = FALSE;
123 chunk_t *chunk;
124
125 if (type >= KEY_ENCODING_MAX || type < 0)
126 {
127 return FALSE;
128 }
129 this->lock->read_lock(this->lock);
130 if (cache)
131 {
132 chunk = this->cache[type]->get(this->cache[type], cache);
133 if (chunk)
134 {
135 *encoding = *chunk;
136 this->lock->unlock(this->lock);
137 return TRUE;
138 }
139 }
140 va_start(args, encoding);
141 enumerator = this->encoders->create_enumerator(this->encoders);
142 while (enumerator->enumerate(enumerator, &encode))
143 {
144 va_copy(copy, args);
145 success = encode(type, encoding, copy);
146 va_end(copy);
147 if (success)
148 {
149 if (cache)
150 {
151 chunk = malloc_thing(chunk_t);
152 *chunk = *encoding;
153 this->lock->unlock(this->lock);
154 this->lock->write_lock(this->lock);
155 this->cache[type]->put(this->cache[type], cache, chunk);
156 }
157 break;
158 }
159 }
160 enumerator->destroy(enumerator);
161 va_end(args);
162 this->lock->unlock(this->lock);
163 return success;
164 }
165
166 /**
167 * Implementation of key_encoding_t.clear_cache
168 */
169 static void clear_cache(private_key_encoding_t *this, void *cache)
170 {
171 key_encoding_type_t type;
172 chunk_t *chunk;
173
174 this->lock->write_lock(this->lock);
175 for (type = 0; type < KEY_ENCODING_MAX; type++)
176 {
177 chunk = this->cache[type]->remove(this->cache[type], cache);
178 if (chunk)
179 {
180 chunk_free(chunk);
181 free(chunk);
182 }
183 }
184 this->lock->unlock(this->lock);
185 }
186
187 /**
188 * Implementation of key_encoding_t.add_encoder
189 */
190 static void add_encoder(private_key_encoding_t *this, key_encoder_t encoder)
191 {
192 this->lock->write_lock(this->lock);
193 this->encoders->insert_last(this->encoders, encoder);
194 this->lock->unlock(this->lock);
195 }
196
197 /**
198 * Implementation of key_encoding_t.remove_encoder
199 */
200 static void remove_encoder(private_key_encoding_t *this, key_encoder_t encoder)
201 {
202 this->lock->write_lock(this->lock);
203 this->encoders->remove(this->encoders, encoder, NULL);
204 this->lock->unlock(this->lock);
205 }
206
207 /**
208 * Implementation of key_encoder_t.destroy.
209 */
210 static void destroy(private_key_encoding_t *this)
211 {
212 enumerator_t *enumerator;
213 key_encoding_type_t type;
214 chunk_t *chunk;
215 void *key;
216
217 for (type = 0; type < KEY_ENCODING_MAX; type++)
218 {
219 enumerator = this->cache[type]->create_enumerator(this->cache[type]);
220 while (enumerator->enumerate(enumerator, &key, &chunk))
221 {
222 chunk_free(chunk);
223 free(chunk);
224 }
225 enumerator->destroy(enumerator);
226 this->cache[type]->destroy(this->cache[type]);
227 }
228 this->encoders->destroy(this->encoders);
229 this->lock->destroy(this->lock);
230 free(this);
231 }
232
233 /**
234 * See header
235 */
236 key_encoding_t *key_encoding_create()
237 {
238 private_key_encoding_t *this = malloc_thing(private_key_encoding_t);
239 key_encoding_type_t type;
240
241 this->public.encode = (bool(*)(key_encoding_t*, key_encoding_type_t type, void *cache, chunk_t *encoding, ...))encode;
242 this->public.clear_cache = (void(*)(key_encoding_t*, void *cache))clear_cache;
243 this->public.add_encoder = (void(*)(key_encoding_t*, key_encoder_t encoder))add_encoder;
244 this->public.remove_encoder = (void(*)(key_encoding_t*, key_encoder_t encoder))remove_encoder;
245 this->public.destroy = (void(*)(key_encoding_t*))destroy;
246
247 for (type = 0; type < KEY_ENCODING_MAX; type++)
248 {
249 this->cache[type] = hashtable_create(hash, equals, 8);
250 }
251 this->encoders = linked_list_create();
252 this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);
253
254 return &this->public;
255 }
256