2 * Copyright (C) 2016-2019 Andreas Steffen
3 * Copyright (C) 2012-2015 Tobias Brunner
4 * Copyright (C) 2011 Martin Willi
6 * Copyright (C) secunet Security Networks AG
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>.
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
20 * @defgroup plugin_feature plugin_feature
24 #ifndef PLUGIN_FEATURE_H_
25 #define PLUGIN_FEATURE_H_
27 typedef struct plugin_feature_t plugin_feature_t
;
31 #include <plugins/plugin.h>
32 #include <credentials/containers/container.h>
35 * Callback function of a plugin to (un-)register a specified feature.
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
43 typedef bool (*plugin_feature_callback_t
)(plugin_t
*plugin
,
44 plugin_feature_t
*feature
,
45 bool reg
,void *cb_data
);
48 * Feature a plugin provides or depends on, including registration functions.
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
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:
70 // two features, one with two dependencies, both use a callback to register
76 // common constructor to register for a feature with one dependency
80 // feature that does not use a registration function
85 struct plugin_feature_t
{
88 /* plugin provides this feature */
90 /* a feature depends on this feature, hard dependency */
92 /* a feature can optionally use this feature, soft dependency */
94 /* register the specified function for all following features */
96 /* use a callback to register all following features */
119 /** diffie_hellman_t */
125 /** generic private key support */
127 /** generating new private keys */
129 /** private_key_t->sign() */
130 FEATURE_PRIVKEY_SIGN
,
131 /** private_key_t->decrypt() */
132 FEATURE_PRIVKEY_DECRYPT
,
133 /** generic public key support */
135 /** public_key_t->verify() */
136 FEATURE_PUBKEY_VERIFY
,
137 /** public_key_t->encrypt() */
138 FEATURE_PUBKEY_ENCRYPT
,
139 /** parsing certificates */
141 /** generating certificates */
143 /** parsing containers */
144 FEATURE_CONTAINER_DECODE
,
145 /** generating containers */
146 FEATURE_CONTAINER_ENCODE
,
147 /** EAP server implementation */
149 /** EAP peer implementation */
151 /** XAuth server implementation */
152 FEATURE_XAUTH_SERVER
,
153 /** XAuth peer implementation */
161 /** custom feature, described with a string */
164 /** More specific data for each type */
166 /** FEATURE_CRYPTER */
168 encryption_algorithm_t alg
;
173 encryption_algorithm_t alg
;
176 /** FEATURE_SIGNER */
177 integrity_algorithm_t signer
;
179 pseudo_random_function_t prf
;
181 ext_out_function_t xof
;
183 key_derivation_function_t kdf
;
186 /** FEATURE_HASHER */
187 hash_algorithm_t hasher
;
189 diffie_hellman_group_t dh_group
;
191 rng_quality_t rng_quality
;
192 /** FEATURE_PRIVKEY */
194 /** FEATURE_PRIVKEY_GEN */
195 key_type_t privkey_gen
;
196 /** FEATURE_PRIVKEY_SIGN */
197 signature_scheme_t privkey_sign
;
198 /** FEATURE_PRIVKEY_DECRYPT */
199 encryption_scheme_t privkey_decrypt
;
200 /** FEATURE_PUBKEY */
202 /** FEATURE_PUBKEY_VERIFY */
203 signature_scheme_t pubkey_verify
;
204 /** FEATURE_PUBKEY_ENCRYPT */
205 encryption_scheme_t pubkey_encrypt
;
206 /** FEATURE_CERT_DECODE/ENCODE */
207 certificate_type_t cert
;
208 /** FEATURE_CONTAINER_DECODE/ENCODE */
209 container_type_t container
;
210 /** FEATURE_EAP_SERVER/CLIENT */
211 eap_vendor_type_t eap
;
212 /** FEATURE_DATABASE */
213 db_driver_t database
;
214 /** FEATURE_FETCHER */
216 /** FEATURE_CUSTOM */
218 /** FEATURE_XAUTH_SERVER/CLIENT */
221 /** FEATURE_REGISTER */
223 /** final flag to pass for builder_function_t */
225 /** feature specific function to register for this type */
229 /** FEATURE_CALLBACK */
231 /** callback function to invoke for registration */
232 plugin_feature_callback_t f
;
233 /** data to pass to callback */
239 #define FEATURE(kind, type, ...) _PLUGIN_FEATURE_##type(kind, __VA_ARGS__)
242 * Define function to register directly for all upcoming features.
244 * @param type feature type to register
245 * @param f type specific function to register
246 * @param ... type specific additional arguments
248 #define PLUGIN_REGISTER(type, f, ...) _PLUGIN_FEATURE_REGISTER_##type(type, f, ##__VA_ARGS__)
251 * Define a callback to invoke for registering all upcoming features.
253 * @param cb type specific callback function to register
254 * @param data data pointer to pass to callback
256 #define PLUGIN_CALLBACK(cb, data) _PLUGIN_FEATURE_CALLBACK(cb, data)
259 * The upcoming features use neither a callback nor a register function.
261 #define PLUGIN_NOOP _PLUGIN_FEATURE_CALLBACK(NULL, NULL)
264 * Define a feature the plugin provides.
266 * @param type feature type to provide
267 * @param ... type specific arguments
269 #define PLUGIN_PROVIDE(type, ...) _PLUGIN_FEATURE_##type(PROVIDE, __VA_ARGS__)
272 * Define a hard dependency for the previously defined feature.
274 * @param type feature type to provide
275 * @param ... type specific arguments
277 #define PLUGIN_DEPENDS(type, ...) _PLUGIN_FEATURE_##type(DEPENDS, __VA_ARGS__)
280 * Define a soft dependency for the previously defined feature.
282 * @param type feature type to provide
283 * @param ... type specific arguments
285 #define PLUGIN_SDEPEND(type, ...) _PLUGIN_FEATURE_##type(SDEPEND, __VA_ARGS__)
287 #define __PLUGIN_FEATURE(kind, type, ...) (plugin_feature_t){ FEATURE_##kind, FEATURE_##type, { __VA_ARGS__ }}
288 #define _PLUGIN_FEATURE_CRYPTER(kind, alg, size) __PLUGIN_FEATURE(kind, CRYPTER, .crypter = { alg, size })
289 #define _PLUGIN_FEATURE_AEAD(kind, alg, size) __PLUGIN_FEATURE(kind, AEAD, .aead = { alg, size })
290 #define _PLUGIN_FEATURE_SIGNER(kind, alg) __PLUGIN_FEATURE(kind, SIGNER, .signer = alg)
291 #define _PLUGIN_FEATURE_HASHER(kind, alg) __PLUGIN_FEATURE(kind, HASHER, .hasher = alg)
292 #define _PLUGIN_FEATURE_PRF(kind, alg) __PLUGIN_FEATURE(kind, PRF, .prf = alg)
293 #define _PLUGIN_FEATURE_XOF(kind, alg) __PLUGIN_FEATURE(kind, XOF, .xof = alg)
294 #define _PLUGIN_FEATURE_KDF(kind, alg) __PLUGIN_FEATURE(kind, KDF, .kdf = alg)
295 #define _PLUGIN_FEATURE_DRBG(kind, type) __PLUGIN_FEATURE(kind, DRBG, .drbg = type)
296 #define _PLUGIN_FEATURE_DH(kind, group) __PLUGIN_FEATURE(kind, DH, .dh_group = group)
297 #define _PLUGIN_FEATURE_RNG(kind, quality) __PLUGIN_FEATURE(kind, RNG, .rng_quality = quality)
298 #define _PLUGIN_FEATURE_NONCE_GEN(kind, ...) __PLUGIN_FEATURE(kind, NONCE_GEN, .custom = NULL)
299 #define _PLUGIN_FEATURE_PRIVKEY(kind, type) __PLUGIN_FEATURE(kind, PRIVKEY, .privkey = type)
300 #define _PLUGIN_FEATURE_PRIVKEY_GEN(kind, type) __PLUGIN_FEATURE(kind, PRIVKEY_GEN, .privkey_gen = type)
301 #define _PLUGIN_FEATURE_PRIVKEY_SIGN(kind, scheme) __PLUGIN_FEATURE(kind, PRIVKEY_SIGN, .privkey_sign = scheme)
302 #define _PLUGIN_FEATURE_PRIVKEY_DECRYPT(kind, scheme) __PLUGIN_FEATURE(kind, PRIVKEY_DECRYPT, .privkey_decrypt = scheme)
303 #define _PLUGIN_FEATURE_PUBKEY(kind, type) __PLUGIN_FEATURE(kind, PUBKEY, .pubkey = type)
304 #define _PLUGIN_FEATURE_PUBKEY_VERIFY(kind, scheme) __PLUGIN_FEATURE(kind, PUBKEY_VERIFY, .pubkey_verify = scheme)
305 #define _PLUGIN_FEATURE_PUBKEY_ENCRYPT(kind, scheme) __PLUGIN_FEATURE(kind, PUBKEY_ENCRYPT, .pubkey_encrypt = scheme)
306 #define _PLUGIN_FEATURE_CERT_DECODE(kind, type) __PLUGIN_FEATURE(kind, CERT_DECODE, .cert = type)
307 #define _PLUGIN_FEATURE_CERT_ENCODE(kind, type) __PLUGIN_FEATURE(kind, CERT_ENCODE, .cert = type)
308 #define _PLUGIN_FEATURE_CONTAINER_DECODE(kind, type) __PLUGIN_FEATURE(kind, CONTAINER_DECODE, .container = type)
309 #define _PLUGIN_FEATURE_CONTAINER_ENCODE(kind, type) __PLUGIN_FEATURE(kind, CONTAINER_ENCODE, .container = type)
310 #define _PLUGIN_FEATURE_EAP_SERVER(kind, type) _PLUGIN_FEATURE_EAP_SERVER_VENDOR(kind, type, 0)
311 #define _PLUGIN_FEATURE_EAP_PEER(kind, type) _PLUGIN_FEATURE_EAP_PEER_VENDOR(kind, type, 0)
312 #define _PLUGIN_FEATURE_EAP_SERVER_VENDOR(kind, type, vendor)__PLUGIN_FEATURE(kind, EAP_SERVER, .eap = { type, vendor })
313 #define _PLUGIN_FEATURE_EAP_PEER_VENDOR(kind, type, vendor) __PLUGIN_FEATURE(kind, EAP_PEER, .eap = { type, vendor })
314 #define _PLUGIN_FEATURE_DATABASE(kind, type) __PLUGIN_FEATURE(kind, DATABASE, .database = type)
315 #define _PLUGIN_FEATURE_FETCHER(kind, type) __PLUGIN_FEATURE(kind, FETCHER, .fetcher = type)
316 #define _PLUGIN_FEATURE_RESOLVER(kind, ...) __PLUGIN_FEATURE(kind, RESOLVER, .custom = NULL)
317 #define _PLUGIN_FEATURE_CUSTOM(kind, name) __PLUGIN_FEATURE(kind, CUSTOM, .custom = name)
318 #define _PLUGIN_FEATURE_XAUTH_SERVER(kind, name) __PLUGIN_FEATURE(kind, XAUTH_SERVER, .xauth = name)
319 #define _PLUGIN_FEATURE_XAUTH_PEER(kind, name) __PLUGIN_FEATURE(kind, XAUTH_PEER, .xauth = name)
321 #define __PLUGIN_FEATURE_REGISTER(type, _f) (plugin_feature_t){ FEATURE_REGISTER, FEATURE_##type, .arg.reg.f = _f }
322 #define __PLUGIN_FEATURE_REGISTER_BUILDER(type, _f, _final) (plugin_feature_t){ FEATURE_REGISTER, FEATURE_##type, .arg.reg = {.f = _f, .final = _final, }}
323 #define _PLUGIN_FEATURE_REGISTER_CRYPTER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
324 #define _PLUGIN_FEATURE_REGISTER_AEAD(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
325 #define _PLUGIN_FEATURE_REGISTER_SIGNER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
326 #define _PLUGIN_FEATURE_REGISTER_HASHER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
327 #define _PLUGIN_FEATURE_REGISTER_PRF(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
328 #define _PLUGIN_FEATURE_REGISTER_XOF(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
329 #define _PLUGIN_FEATURE_REGISTER_KDF(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
330 #define _PLUGIN_FEATURE_REGISTER_DRBG(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
331 #define _PLUGIN_FEATURE_REGISTER_DH(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
332 #define _PLUGIN_FEATURE_REGISTER_RNG(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
333 #define _PLUGIN_FEATURE_REGISTER_NONCE_GEN(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
334 #define _PLUGIN_FEATURE_REGISTER_PRIVKEY(type, f, final) __PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
335 #define _PLUGIN_FEATURE_REGISTER_PRIVKEY_GEN(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
336 #define _PLUGIN_FEATURE_REGISTER_PUBKEY(type, f, final) __PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
337 #define _PLUGIN_FEATURE_REGISTER_CERT_DECODE(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
338 #define _PLUGIN_FEATURE_REGISTER_CERT_ENCODE(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
339 #define _PLUGIN_FEATURE_REGISTER_CONTAINER_DECODE(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
340 #define _PLUGIN_FEATURE_REGISTER_CONTAINER_ENCODE(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
341 #define _PLUGIN_FEATURE_REGISTER_DATABASE(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
342 #define _PLUGIN_FEATURE_REGISTER_FETCHER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
343 #define _PLUGIN_FEATURE_REGISTER_RESOLVER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
345 #define _PLUGIN_FEATURE_CALLBACK(_cb, _data) (plugin_feature_t){ FEATURE_CALLBACK, FEATURE_NONE, .arg.cb = { .f = _cb, .data = _data } }
348 * Names for plugin_feature_t types.
350 extern enum_name_t
*plugin_feature_names
;
353 * Add a set of plugin features to the given array, which must have enough space
354 * to store the added features.
356 * @param features the array of plugin features to extend
357 * @param to_add the features to add
358 * @param count number of features to add
359 * @param pos current position in the features array, gets advanced
361 static inline void plugin_features_add(plugin_feature_t
*features
,
362 plugin_feature_t
*to_add
,
367 for (i
= 0; i
< count
; i
++)
369 features
[(*pos
)++] = to_add
[i
];
374 * Calculates a hash value for the given feature.
376 * Since this is intended to be used with the plugin_features_matches function
377 * the hash is not really unique for all types of features (e.g. RNGs are all
378 * mapped to the same value because they are loosely matched by said function).
380 * @param feature feature to hash
381 * @return hash value of the feature
383 uint32_t plugin_feature_hash(plugin_feature_t
*feature
);
386 * Check if feature a matches to feature b.
388 * This is no check for equality. For instance, for FEATURE_RNG a matches b if
389 * a's strength is at least the strength of b. Or for FEATURE_SQL if a is
390 * DB_ANY it will match b if it is of the same type.
392 * @param a feature to check
393 * @param b feature to match against
394 * @return TRUE if a matches b
396 bool plugin_feature_matches(plugin_feature_t
*a
, plugin_feature_t
*b
);
399 * Check if feature a equals feature b.
402 * @param b feature to compare
403 * @return TRUE if a equals b
405 bool plugin_feature_equals(plugin_feature_t
*a
, plugin_feature_t
*b
);
408 * Get a string describing feature.
410 * @param feature feature to describe
411 * @return allocated string describing feature
413 char* plugin_feature_get_string(plugin_feature_t
*feature
);
416 * Load a plugin feature using a REGISTER/CALLBACK feature entry.
418 * @param plugin plugin providing feature
419 * @param feature feature to load
420 * @param reg REGISTER/CALLBACK feature entry to use for registration
422 bool plugin_feature_load(plugin_t
*plugin
, plugin_feature_t
*feature
,
423 plugin_feature_t
*reg
);
426 * Unload a plugin feature using a REGISTER/CALLBACK feature entry.
428 * @param plugin plugin providing feature
429 * @param feature feature to unload
430 * @param reg REGISTER/CALLBACK feature entry to use for deregistration
432 bool plugin_feature_unload(plugin_t
*plugin
, plugin_feature_t
*feature
,
433 plugin_feature_t
*reg
);
435 #endif /** PLUGIN_FEATURE_H_ @}*/