Added missing XAuth plugin feature enum names
[strongswan.git] / src / libstrongswan / plugins / plugin_feature.c
1 /*
2 * Copyright (C) 2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
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 #define _GNU_SOURCE
17 #include <stdio.h>
18
19 #include "plugin_feature.h"
20
21 #include <debug.h>
22
23 ENUM(plugin_feature_names, FEATURE_NONE, FEATURE_CUSTOM,
24 "NONE",
25 "CRYPTER",
26 "AEAD",
27 "SIGNER",
28 "HASHER",
29 "PRF",
30 "DH",
31 "RNG",
32 "PRIVKEY",
33 "PRIVKEY_GEN",
34 "PRIVKEY_SIGN",
35 "PRIVKEY_DECRYPT",
36 "PUBKEY",
37 "PUBKEY_VERIFY",
38 "PUBKEY_ENCRYPT",
39 "CERT_DECODE",
40 "CERT_ENCODE",
41 "EAP_SERVER",
42 "EAP_CLIENT",
43 "XAUTH_SERVER",
44 "XAUTH_CLIENT",
45 "DATABASE",
46 "FETCHER",
47 "CUSTOM",
48 );
49
50 /**
51 * See header.
52 */
53 bool plugin_feature_matches(plugin_feature_t *a, plugin_feature_t *b)
54 {
55 if (a->type == b->type)
56 {
57 switch (a->type)
58 {
59 case FEATURE_NONE:
60 return FALSE;
61 case FEATURE_CRYPTER:
62 return a->arg.crypter.alg == b->arg.crypter.alg &&
63 a->arg.crypter.key_size == b->arg.crypter.key_size;
64 case FEATURE_AEAD:
65 return a->arg.aead.alg == b->arg.aead.alg &&
66 a->arg.aead.key_size == b->arg.aead.key_size;
67 case FEATURE_SIGNER:
68 return a->arg.signer == b->arg.signer;
69 case FEATURE_HASHER:
70 return a->arg.hasher == b->arg.hasher;
71 case FEATURE_PRF:
72 return a->arg.prf == b->arg.prf;
73 case FEATURE_DH:
74 return a->arg.dh_group == b->arg.dh_group;
75 case FEATURE_RNG:
76 return a->arg.rng_quality <= b->arg.rng_quality;
77 case FEATURE_PRIVKEY:
78 case FEATURE_PRIVKEY_GEN:
79 case FEATURE_PUBKEY:
80 return a->arg.privkey == b->arg.privkey;
81 case FEATURE_PRIVKEY_SIGN:
82 case FEATURE_PUBKEY_VERIFY:
83 return a->arg.privkey_sign == b->arg.privkey_sign;
84 case FEATURE_PRIVKEY_DECRYPT:
85 case FEATURE_PUBKEY_ENCRYPT:
86 return a->arg.privkey_decrypt == b->arg.privkey_decrypt;
87 case FEATURE_CERT_DECODE:
88 case FEATURE_CERT_ENCODE:
89 return a->arg.cert == b->arg.cert;
90 case FEATURE_EAP_SERVER:
91 case FEATURE_EAP_PEER:
92 return a->arg.eap == b->arg.eap;
93 case FEATURE_DATABASE:
94 return a->arg.database == DB_ANY ||
95 a->arg.database == b->arg.database;
96 case FEATURE_FETCHER:
97 return a->arg.fetcher == NULL ||
98 streq(a->arg.fetcher, b->arg.fetcher);
99 case FEATURE_CUSTOM:
100 return streq(a->arg.custom, b->arg.custom);
101 case FEATURE_XAUTH_SERVER:
102 case FEATURE_XAUTH_PEER:
103 return streq(a->arg.xauth, b->arg.xauth);
104 }
105 }
106 return FALSE;
107 }
108
109 /**
110 * See header.
111 */
112 char* plugin_feature_get_string(plugin_feature_t *feature)
113 {
114 char *str = NULL;
115
116 if (feature->kind == FEATURE_REGISTER)
117 {
118 return strdup("(register function)");
119 }
120 switch (feature->type)
121 {
122 case FEATURE_NONE:
123 return strdup("NONE");
124 case FEATURE_CRYPTER:
125 if (asprintf(&str, "%N:%N-%d", plugin_feature_names, feature->type,
126 encryption_algorithm_names, feature->arg.crypter.alg,
127 feature->arg.crypter.key_size) > 0)
128 {
129 return str;
130 }
131 break;
132 case FEATURE_AEAD:
133 if (asprintf(&str, "%N:%N-%d", plugin_feature_names, feature->type,
134 encryption_algorithm_names, feature->arg.aead.alg,
135 feature->arg.aead.key_size) > 0)
136 {
137 return str;
138 }
139 break;
140 case FEATURE_SIGNER:
141 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
142 integrity_algorithm_names, feature->arg.signer) > 0)
143 {
144 return str;
145 }
146 break;
147 case FEATURE_HASHER:
148 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
149 hash_algorithm_names, feature->arg.hasher) > 0)
150 {
151 return str;
152 }
153 break;
154 case FEATURE_PRF:
155 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
156 pseudo_random_function_names, feature->arg.prf) > 0)
157 {
158 return str;
159 }
160 break;
161 case FEATURE_DH:
162 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
163 diffie_hellman_group_names, feature->arg.dh_group) > 0)
164 {
165 return str;
166 }
167 break;
168 case FEATURE_RNG:
169 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
170 rng_quality_names, feature->arg.rng_quality) > 0)
171 {
172 return str;
173 }
174 break;
175 case FEATURE_PRIVKEY:
176 case FEATURE_PRIVKEY_GEN:
177 case FEATURE_PUBKEY:
178 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
179 key_type_names, feature->arg.privkey) > 0)
180 {
181 return str;
182 }
183 break;
184 case FEATURE_PRIVKEY_SIGN:
185 case FEATURE_PUBKEY_VERIFY:
186 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
187 signature_scheme_names, feature->arg.privkey_sign) > 0)
188 {
189 return str;
190 }
191 break;
192 case FEATURE_PRIVKEY_DECRYPT:
193 case FEATURE_PUBKEY_ENCRYPT:
194 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
195 encryption_scheme_names, feature->arg.privkey_decrypt) > 0)
196 {
197 return str;
198 }
199 break;
200 case FEATURE_CERT_DECODE:
201 case FEATURE_CERT_ENCODE:
202 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
203 certificate_type_names, feature->arg.cert) > 0)
204 {
205 return str;
206 }
207 break;
208 case FEATURE_EAP_SERVER:
209 case FEATURE_EAP_PEER:
210 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
211 eap_type_short_names, feature->arg.eap) > 0)
212 {
213 return str;
214 }
215 break;
216 case FEATURE_DATABASE:
217 if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
218 db_driver_names, feature->arg.database) > 0)
219 {
220 return str;
221 }
222 break;
223 case FEATURE_FETCHER:
224 if (asprintf(&str, "%N:%s", plugin_feature_names, feature->type,
225 feature->arg.fetcher) > 0)
226 {
227 return str;
228 }
229 break;
230 case FEATURE_CUSTOM:
231 if (asprintf(&str, "%N:%s", plugin_feature_names, feature->type,
232 feature->arg.custom) > 0)
233 {
234 return str;
235 }
236 break;
237 case FEATURE_XAUTH_SERVER:
238 case FEATURE_XAUTH_PEER:
239 if (asprintf(&str, "%N:%s", plugin_feature_names, feature->type,
240 feature->arg.xauth) > 0)
241 {
242 return str;
243 }
244 break;
245 }
246 if (!str)
247 {
248 str = strdup("(invalid)");
249 }
250 return str;
251 }
252
253 /**
254 * See header.
255 */
256 bool plugin_feature_load(plugin_t *plugin, plugin_feature_t *feature,
257 plugin_feature_t *reg)
258 {
259 char *name;
260
261 if (!reg)
262 { /* noting to do for this feature */
263 return TRUE;
264 }
265 if (reg->kind == FEATURE_CALLBACK)
266 {
267 if (reg->arg.cb.f(plugin, feature, TRUE, reg->arg.cb.data))
268 {
269 return TRUE;
270 }
271 return FALSE;
272 }
273 name = plugin->get_name(plugin);
274 switch (feature->type)
275 {
276 case FEATURE_CRYPTER:
277 lib->crypto->add_crypter(lib->crypto, feature->arg.crypter.alg,
278 name, reg->arg.reg.f);
279 break;
280 case FEATURE_AEAD:
281 lib->crypto->add_aead(lib->crypto, feature->arg.aead.alg,
282 name, reg->arg.reg.f);
283 break;
284 case FEATURE_SIGNER:
285 lib->crypto->add_signer(lib->crypto, feature->arg.signer,
286 name, reg->arg.reg.f);
287 break;
288 case FEATURE_HASHER:
289 lib->crypto->add_hasher(lib->crypto, feature->arg.hasher,
290 name, reg->arg.reg.f);
291 break;
292 case FEATURE_PRF:
293 lib->crypto->add_prf(lib->crypto, feature->arg.prf,
294 name, reg->arg.reg.f);
295 break;
296 case FEATURE_DH:
297 lib->crypto->add_dh(lib->crypto, feature->arg.dh_group,
298 name, reg->arg.reg.f);
299 break;
300 case FEATURE_RNG:
301 lib->crypto->add_rng(lib->crypto, feature->arg.rng_quality,
302 name, reg->arg.reg.f);
303 break;
304 case FEATURE_PRIVKEY:
305 case FEATURE_PRIVKEY_GEN:
306 lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY,
307 feature->arg.privkey, reg->arg.reg.final,
308 reg->arg.reg.f);
309 break;
310 case FEATURE_PUBKEY:
311 lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY,
312 feature->arg.pubkey, reg->arg.reg.final,
313 reg->arg.reg.f);
314 break;
315 case FEATURE_CERT_DECODE:
316 case FEATURE_CERT_ENCODE:
317 lib->creds->add_builder(lib->creds, CRED_CERTIFICATE,
318 feature->arg.cert, reg->arg.reg.final,
319 reg->arg.reg.f);
320 break;
321 case FEATURE_DATABASE:
322 lib->db->add_database(lib->db, reg->arg.reg.f);
323 break;
324 case FEATURE_FETCHER:
325 lib->fetcher->add_fetcher(lib->fetcher, reg->arg.reg.f,
326 feature->arg.fetcher);
327 break;
328 default:
329 break;
330 }
331 return TRUE;
332 }
333
334 /**
335 * See header.
336 */
337 bool plugin_feature_unload(plugin_t *plugin, plugin_feature_t *feature,
338 plugin_feature_t *reg)
339 {
340 if (!reg)
341 { /* noting to do for this feature */
342 return TRUE;
343 }
344 if (reg->kind == FEATURE_CALLBACK)
345 {
346 if (reg->arg.cb.f(plugin, feature, FALSE, reg->arg.cb.data))
347 {
348 return TRUE;
349 }
350 return FALSE;
351 }
352 switch (feature->type)
353 {
354 case FEATURE_CRYPTER:
355 lib->crypto->remove_crypter(lib->crypto, reg->arg.reg.f);
356 break;
357 case FEATURE_AEAD:
358 lib->crypto->remove_aead(lib->crypto, reg->arg.reg.f);
359 break;
360 case FEATURE_SIGNER:
361 lib->crypto->remove_signer(lib->crypto, reg->arg.reg.f);
362 break;
363 case FEATURE_HASHER:
364 lib->crypto->remove_hasher(lib->crypto, reg->arg.reg.f);
365 break;
366 case FEATURE_PRF:
367 lib->crypto->remove_prf(lib->crypto, reg->arg.reg.f);
368 break;
369 case FEATURE_DH:
370 lib->crypto->remove_dh(lib->crypto, reg->arg.reg.f);
371 break;
372 case FEATURE_RNG:
373 lib->crypto->remove_rng(lib->crypto, reg->arg.reg.f);
374 break;
375 case FEATURE_PRIVKEY:
376 case FEATURE_PRIVKEY_GEN:
377 lib->creds->remove_builder(lib->creds, reg->arg.reg.f);
378 break;
379 case FEATURE_PUBKEY:
380 lib->creds->remove_builder(lib->creds, reg->arg.reg.f);
381 break;
382 case FEATURE_CERT_DECODE:
383 case FEATURE_CERT_ENCODE:
384 lib->creds->remove_builder(lib->creds, reg->arg.reg.f);
385 break;
386 case FEATURE_DATABASE:
387 lib->db->remove_database(lib->db, reg->arg.reg.f);
388 break;
389 case FEATURE_FETCHER:
390 lib->fetcher->remove_fetcher(lib->fetcher, reg->arg.reg.f);
391 break;
392 default:
393 break;
394 }
395 return TRUE;
396 }