aead: Support custom AEAD salt sizes
[strongswan.git] / src / libstrongswan / plugins / plugin_feature.h
1 /*
2 * Copyright (C) 2012-2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2011 Martin Willi
6 * Copyright (C) 2011 revosec AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 /**
20 * @defgroup plugin_feature plugin_feature
21 * @{ @ingroup plugins
22 */
23
24 #ifndef PLUGIN_FEATURE_H_
25 #define PLUGIN_FEATURE_H_
26
27 typedef struct plugin_feature_t plugin_feature_t;
28
29 #include <library.h>
30 #include <eap/eap.h>
31 #include <plugins/plugin.h>
32 #include <credentials/containers/container.h>
33
34 /**
35 * Callback function of a plugin to (un-)register a specified feature.
36 *
37 * @param plugin plugin instance
38 * @param feature feature to register
39 * @param reg TRUE to register, FALSE to unregister
40 * @param cb_data user data passed with callback function
41 * @return TRUE if registered successfully
42 */
43 typedef bool (*plugin_feature_callback_t)(plugin_t *plugin,
44 plugin_feature_t *feature,
45 bool reg,void *cb_data);
46
47 /**
48 * Feature a plugin provides or depends on, including registration functions.
49 *
50 * Each plugin returns a list of plugin features, allowing the plugin loader
51 * to resolve dependencies and register the feature. FEATURE_PROVIDE defines
52 * features provided by the plugin, hard (DEPENDS) or soft (SDEPEND) dependency
53 * specified is related to the previously defined PROVIDE feature.
54 * If a plugin feature requires to hook in functionality into the library
55 * or a daemon, it can use REGISTER or CALLBACK entries. Each PROVIDE feature
56 * uses the REGISTER/CALLBACK entry defined previously. The REGISTER entry
57 * defines a common feature registration function directly passed to the
58 * associated manager or factory (crypto/credential factory etc.). A callback
59 * function is more generic allows the loader to invoke a callback to do
60 * the registration. PROVIDE features that do not use a registration or callback
61 * function must be listed before any REGISTER/CALLBACK entry, or use the NOOP
62 * helper macro.
63 *
64 * To conveniently create feature lists, use the macros PLUGIN_REGISTER,
65 * PLUGIN_CALLBACK, PLUGIN_NOOP, PLUGIN_PROVIDE, PLUGIN_DEPENDS and
66 * PLUGIN_SDEPEND. Use indentation to show how the registration functions
67 * and dependencies are related to a provided feature, such as:
68 *
69 * @verbatim
70 // two features, one with two dependencies, both use a callback to register
71 PLUGIN_CALLBACK(...),
72 PLUGIN_PROVIDE(...),
73 PLUGIN_DEPENDS(...),
74 PLUGIN_SDEPEND(...),
75 PLUGIN_PROVIDE(...),
76 // common constructor to register for a feature with one dependency
77 PLUGIN_REGISTER(...),
78 PLUGIN_PROVIDE(...),
79 PLUGIN_DEPENDS(...),
80 // feature that does not use a registration function
81 PLUGIN_NOOP,
82 PLUGIN_PROVIDE(...),
83 @endverbatim
84 */
85 struct plugin_feature_t {
86 /** kind of entry */
87 enum {
88 /* plugin provides this feature */
89 FEATURE_PROVIDE,
90 /* a feature depends on this feature, hard dependency */
91 FEATURE_DEPENDS,
92 /* a feature can optionally use this feature, soft dependency */
93 FEATURE_SDEPEND,
94 /* register the specified function for all following features */
95 FEATURE_REGISTER,
96 /* use a callback to register all following features */
97 FEATURE_CALLBACK,
98 } kind;
99 /* type of feature */
100 enum {
101 /** not a feature */
102 FEATURE_NONE,
103 /** crypter_t */
104 FEATURE_CRYPTER,
105 /** aead_t */
106 FEATURE_AEAD,
107 /** signer_t */
108 FEATURE_SIGNER,
109 /** hasher_t */
110 FEATURE_HASHER,
111 /** prf_t */
112 FEATURE_PRF,
113 /** diffie_hellman_t */
114 FEATURE_DH,
115 /** rng_t */
116 FEATURE_RNG,
117 /** nonce_gen_t */
118 FEATURE_NONCE_GEN,
119 /** generic private key support */
120 FEATURE_PRIVKEY,
121 /** generating new private keys */
122 FEATURE_PRIVKEY_GEN,
123 /** private_key_t->sign() */
124 FEATURE_PRIVKEY_SIGN,
125 /** private_key_t->decrypt() */
126 FEATURE_PRIVKEY_DECRYPT,
127 /** generic public key support */
128 FEATURE_PUBKEY,
129 /** public_key_t->verify() */
130 FEATURE_PUBKEY_VERIFY,
131 /** public_key_t->encrypt() */
132 FEATURE_PUBKEY_ENCRYPT,
133 /** parsing certificates */
134 FEATURE_CERT_DECODE,
135 /** generating certificates */
136 FEATURE_CERT_ENCODE,
137 /** parsing containers */
138 FEATURE_CONTAINER_DECODE,
139 /** generating containers */
140 FEATURE_CONTAINER_ENCODE,
141 /** EAP server implementation */
142 FEATURE_EAP_SERVER,
143 /** EAP peer implementation */
144 FEATURE_EAP_PEER,
145 /** XAuth server implementation */
146 FEATURE_XAUTH_SERVER,
147 /** XAuth peer implementation */
148 FEATURE_XAUTH_PEER,
149 /** database_t */
150 FEATURE_DATABASE,
151 /** fetcher_t */
152 FEATURE_FETCHER,
153 /** resolver_t */
154 FEATURE_RESOLVER,
155 /** custom feature, described with a string */
156 FEATURE_CUSTOM,
157 } type;
158 /** More specific data for each type */
159 union {
160 /** FEATURE_CRYPTER */
161 struct {
162 encryption_algorithm_t alg;
163 size_t key_size;
164 } crypter;
165 /** FEATURE_AEAD */
166 struct {
167 encryption_algorithm_t alg;
168 size_t key_size;
169 } aead;
170 /** FEATURE_SIGNER */
171 integrity_algorithm_t signer;
172 /** FEATURE_PRF */
173 pseudo_random_function_t prf;
174 /** FEATURE_HASHER */
175 hash_algorithm_t hasher;
176 /** FEATURE_DH */
177 diffie_hellman_group_t dh_group;
178 /** FEATURE_RNG */
179 rng_quality_t rng_quality;
180 /** FEATURE_PRIVKEY */
181 key_type_t privkey;
182 /** FEATURE_PRIVKEY_GEN */
183 key_type_t privkey_gen;
184 /** FEATURE_PRIVKEY_SIGN */
185 signature_scheme_t privkey_sign;
186 /** FEATURE_PRIVKEY_DECRYPT */
187 encryption_scheme_t privkey_decrypt;
188 /** FEATURE_PUBKEY */
189 key_type_t pubkey;
190 /** FEATURE_PUBKEY_VERIFY */
191 signature_scheme_t pubkey_verify;
192 /** FEATURE_PUBKEY_ENCRYPT */
193 encryption_scheme_t pubkey_encrypt;
194 /** FEATURE_CERT_DECODE/ENCODE */
195 certificate_type_t cert;
196 /** FEATURE_CONTAINER_DECODE/ENCODE */
197 container_type_t container;
198 /** FEATURE_EAP_SERVER/CLIENT */
199 eap_type_t eap;
200 /** FEATURE_DATABASE */
201 db_driver_t database;
202 /** FEATURE_FETCHER */
203 char *fetcher;
204 /** FEATURE_CUSTOM */
205 char *custom;
206 /** FEATURE_XAUTH_SERVER/CLIENT */
207 char *xauth;
208
209 /** FEATURE_REGISTER */
210 struct {
211 /** final flag to pass for builder_function_t */
212 bool final;
213 /** feature specific function to register for this type */
214 void *f;
215 } reg;
216
217 /** FEATURE_CALLBACK */
218 struct {
219 /** callback function to invoke for registration */
220 plugin_feature_callback_t f;
221 /** data to pass to callback */
222 void *data;
223 } cb;
224 } arg;
225 };
226
227 #define FEATURE(kind, type, ...) _PLUGIN_FEATURE_##type(kind, __VA_ARGS__)
228
229 /**
230 * Define function to register directly for all upcoming features.
231 *
232 * @param type feature type to register
233 * @param f type specific function to register
234 * @param ... type specific additional arguments
235 */
236 #define PLUGIN_REGISTER(type, f, ...) _PLUGIN_FEATURE_REGISTER_##type(type, f, ##__VA_ARGS__)
237
238 /**
239 * Define a callback to invoke for registering all upcoming features.
240 *
241 * @param cb type specific callback function to register
242 * @param data data pointer to pass to callback
243 */
244 #define PLUGIN_CALLBACK(cb, data) _PLUGIN_FEATURE_CALLBACK(cb, data)
245
246 /**
247 * The upcoming features use neither a callback nor a register function.
248 */
249 #define PLUGIN_NOOP _PLUGIN_FEATURE_CALLBACK(NULL, NULL)
250
251 /**
252 * Define a feature the plugin provides.
253 *
254 * @param type feature type to provide
255 * @param ... type specific arguments
256 */
257 #define PLUGIN_PROVIDE(type, ...) _PLUGIN_FEATURE_##type(PROVIDE, __VA_ARGS__)
258
259 /**
260 * Define a hard dependency for the previously defined feature.
261 *
262 * @param type feature type to provide
263 * @param ... type specific arguments
264 */
265 #define PLUGIN_DEPENDS(type, ...) _PLUGIN_FEATURE_##type(DEPENDS, __VA_ARGS__)
266
267 /**
268 * Define a soft dependency for the previously defined feature.
269 *
270 * @param type feature type to provide
271 * @param ... type specific arguments
272 */
273 #define PLUGIN_SDEPEND(type, ...) _PLUGIN_FEATURE_##type(SDEPEND, __VA_ARGS__)
274
275 #define __PLUGIN_FEATURE(kind, type, ...) (plugin_feature_t){ FEATURE_##kind, FEATURE_##type, { __VA_ARGS__ }}
276 #define _PLUGIN_FEATURE_CRYPTER(kind, alg, size) __PLUGIN_FEATURE(kind, CRYPTER, .crypter = { alg, size })
277 #define _PLUGIN_FEATURE_AEAD(kind, alg, size) __PLUGIN_FEATURE(kind, AEAD, .aead = { alg, size })
278 #define _PLUGIN_FEATURE_SIGNER(kind, alg) __PLUGIN_FEATURE(kind, SIGNER, .signer = alg)
279 #define _PLUGIN_FEATURE_HASHER(kind, alg) __PLUGIN_FEATURE(kind, HASHER, .hasher = alg)
280 #define _PLUGIN_FEATURE_PRF(kind, alg) __PLUGIN_FEATURE(kind, PRF, .prf = alg)
281 #define _PLUGIN_FEATURE_DH(kind, group) __PLUGIN_FEATURE(kind, DH, .dh_group = group)
282 #define _PLUGIN_FEATURE_RNG(kind, quality) __PLUGIN_FEATURE(kind, RNG, .rng_quality = quality)
283 #define _PLUGIN_FEATURE_NONCE_GEN(kind, ...) __PLUGIN_FEATURE(kind, NONCE_GEN, .custom = NULL)
284 #define _PLUGIN_FEATURE_PRIVKEY(kind, type) __PLUGIN_FEATURE(kind, PRIVKEY, .privkey = type)
285 #define _PLUGIN_FEATURE_PRIVKEY_GEN(kind, type) __PLUGIN_FEATURE(kind, PRIVKEY_GEN, .privkey_gen = type)
286 #define _PLUGIN_FEATURE_PRIVKEY_SIGN(kind, scheme) __PLUGIN_FEATURE(kind, PRIVKEY_SIGN, .privkey_sign = scheme)
287 #define _PLUGIN_FEATURE_PRIVKEY_DECRYPT(kind, scheme) __PLUGIN_FEATURE(kind, PRIVKEY_DECRYPT, .privkey_decrypt = scheme)
288 #define _PLUGIN_FEATURE_PUBKEY(kind, type) __PLUGIN_FEATURE(kind, PUBKEY, .pubkey = type)
289 #define _PLUGIN_FEATURE_PUBKEY_VERIFY(kind, scheme) __PLUGIN_FEATURE(kind, PUBKEY_VERIFY, .pubkey_verify = scheme)
290 #define _PLUGIN_FEATURE_PUBKEY_ENCRYPT(kind, scheme) __PLUGIN_FEATURE(kind, PUBKEY_ENCRYPT, .pubkey_encrypt = scheme)
291 #define _PLUGIN_FEATURE_CERT_DECODE(kind, type) __PLUGIN_FEATURE(kind, CERT_DECODE, .cert = type)
292 #define _PLUGIN_FEATURE_CERT_ENCODE(kind, type) __PLUGIN_FEATURE(kind, CERT_ENCODE, .cert = type)
293 #define _PLUGIN_FEATURE_CONTAINER_DECODE(kind, type) __PLUGIN_FEATURE(kind, CONTAINER_DECODE, .container = type)
294 #define _PLUGIN_FEATURE_CONTAINER_ENCODE(kind, type) __PLUGIN_FEATURE(kind, CONTAINER_ENCODE, .container = type)
295 #define _PLUGIN_FEATURE_EAP_SERVER(kind, type) __PLUGIN_FEATURE(kind, EAP_SERVER, .eap = type)
296 #define _PLUGIN_FEATURE_EAP_PEER(kind, type) __PLUGIN_FEATURE(kind, EAP_PEER, .eap = type)
297 #define _PLUGIN_FEATURE_DATABASE(kind, type) __PLUGIN_FEATURE(kind, DATABASE, .database = type)
298 #define _PLUGIN_FEATURE_FETCHER(kind, type) __PLUGIN_FEATURE(kind, FETCHER, .fetcher = type)
299 #define _PLUGIN_FEATURE_RESOLVER(kind, ...) __PLUGIN_FEATURE(kind, RESOLVER, .custom = NULL)
300 #define _PLUGIN_FEATURE_CUSTOM(kind, name) __PLUGIN_FEATURE(kind, CUSTOM, .custom = name)
301 #define _PLUGIN_FEATURE_XAUTH_SERVER(kind, name) __PLUGIN_FEATURE(kind, XAUTH_SERVER, .xauth = name)
302 #define _PLUGIN_FEATURE_XAUTH_PEER(kind, name) __PLUGIN_FEATURE(kind, XAUTH_PEER, .xauth = name)
303
304 #define __PLUGIN_FEATURE_REGISTER(type, _f) (plugin_feature_t){ FEATURE_REGISTER, FEATURE_##type, .arg.reg.f = _f }
305 #define __PLUGIN_FEATURE_REGISTER_BUILDER(type, _f, _final) (plugin_feature_t){ FEATURE_REGISTER, FEATURE_##type, .arg.reg = {.f = _f, .final = _final, }}
306 #define _PLUGIN_FEATURE_REGISTER_CRYPTER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
307 #define _PLUGIN_FEATURE_REGISTER_AEAD(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
308 #define _PLUGIN_FEATURE_REGISTER_SIGNER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
309 #define _PLUGIN_FEATURE_REGISTER_HASHER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
310 #define _PLUGIN_FEATURE_REGISTER_PRF(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
311 #define _PLUGIN_FEATURE_REGISTER_DH(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
312 #define _PLUGIN_FEATURE_REGISTER_RNG(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
313 #define _PLUGIN_FEATURE_REGISTER_NONCE_GEN(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
314 #define _PLUGIN_FEATURE_REGISTER_PRIVKEY(type, f, final) __PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
315 #define _PLUGIN_FEATURE_REGISTER_PRIVKEY_GEN(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
316 #define _PLUGIN_FEATURE_REGISTER_PUBKEY(type, f, final) __PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
317 #define _PLUGIN_FEATURE_REGISTER_CERT_DECODE(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
318 #define _PLUGIN_FEATURE_REGISTER_CERT_ENCODE(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
319 #define _PLUGIN_FEATURE_REGISTER_CONTAINER_DECODE(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
320 #define _PLUGIN_FEATURE_REGISTER_CONTAINER_ENCODE(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
321 #define _PLUGIN_FEATURE_REGISTER_DATABASE(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
322 #define _PLUGIN_FEATURE_REGISTER_FETCHER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
323 #define _PLUGIN_FEATURE_REGISTER_RESOLVER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
324
325 #define _PLUGIN_FEATURE_CALLBACK(_cb, _data) (plugin_feature_t){ FEATURE_CALLBACK, FEATURE_NONE, .arg.cb = { .f = _cb, .data = _data } }
326
327 /**
328 * Names for plugin_feature_t types.
329 */
330 extern enum_name_t *plugin_feature_names;
331
332 /**
333 * Add a set of plugin features to the given array, which must have enough space
334 * to store the added features.
335 *
336 * @param features the array of plugin features to extend
337 * @param to_add the features to add
338 * @param count number of features to add
339 * @param pos current position in the features array, gets advanced
340 */
341 static inline void plugin_features_add(plugin_feature_t *features,
342 plugin_feature_t *to_add,
343 int count, int *pos)
344 {
345 int i;
346
347 for (i = 0; i < count; i++)
348 {
349 features[(*pos)++] = to_add[i];
350 }
351 }
352
353 /**
354 * Calculates a hash value for the given feature.
355 *
356 * Since this is intended to be used with the plugin_features_matches function
357 * the hash is not really unique for all types of features (e.g. RNGs are all
358 * mapped to the same value because they are loosely matched by said function).
359 *
360 * @param feature feature to hash
361 * @return hash value of the feature
362 */
363 u_int32_t plugin_feature_hash(plugin_feature_t *feature);
364
365 /**
366 * Check if feature a matches to feature b.
367 *
368 * This is no check for equality. For instance, for FEATURE_RNG a matches b if
369 * a's strength is at least the strength of b. Or for FEATURE_SQL if a is
370 * DB_ANY it will match b if it is of the same type.
371 *
372 * @param a feature to check
373 * @param b feature to match against
374 * @return TRUE if a matches b
375 */
376 bool plugin_feature_matches(plugin_feature_t *a, plugin_feature_t *b);
377
378 /**
379 * Check if feature a equals feature b.
380 *
381 * @param a feature
382 * @param b feature to compare
383 * @return TRUE if a equals b
384 */
385 bool plugin_feature_equals(plugin_feature_t *a, plugin_feature_t *b);
386
387 /**
388 * Get a string describing feature.
389 *
390 * @param feature feature to describe
391 * @return allocated string describing feature
392 */
393 char* plugin_feature_get_string(plugin_feature_t *feature);
394
395 /**
396 * Load a plugin feature using a REGISTER/CALLBACK feature entry.
397 *
398 * @param plugin plugin providing feature
399 * @param feature feature to load
400 * @param reg REGISTER/CALLBACK feature entry to use for registration
401 */
402 bool plugin_feature_load(plugin_t *plugin, plugin_feature_t *feature,
403 plugin_feature_t *reg);
404
405 /**
406 * Unload a plugin feature using a REGISTER/CALLBACK feature entry.
407 *
408 * @param plugin plugin providing feature
409 * @param feature feature to unload
410 * @param reg REGISTER/CALLBACK feature entry to use for deregistration
411 */
412 bool plugin_feature_unload(plugin_t *plugin, plugin_feature_t *feature,
413 plugin_feature_t *reg);
414
415 #endif /** PLUGIN_FEATURE_H_ @}*/