2 * Copyright (C) 2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
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>.
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
17 * @defgroup plugin_feature plugin_feature
21 #ifndef PLUGIN_FEATURE_H_
22 #define PLUGIN_FEATURE_H_
24 typedef struct plugin_feature_t plugin_feature_t
;
28 #include <plugins/plugin.h>
31 * Callback function of a plugin to (un-)register a specified feature.
33 * @param plugin plugin instance
34 * @param feature feature to register
35 * @param reg TRUE to register, FALSE to unregister
36 * @param cb_data user data passed with callback function
37 * @return TRUE if registered successfully
39 typedef bool (*plugin_feature_callback_t
)(plugin_t
*plugin
,
40 plugin_feature_t
*feature
,
41 bool reg
,void *cb_data
);
44 * Feature a plugin provides or depends on, including registration functions.
46 * Each plugin returns a list of plugin features, allowing the plugin loader
47 * to resolve dependencies and register the feature. FEATURE_PROVIDE defines
48 * features provided by the plugin, hard (DEPENDS) or soft (SDEPEND) dependency
49 * specified is related to the previously defined PROVIDE feature.
50 * If a plugin feature requires to hook in functionality into the library
51 * or a daemon, it can use REGISTER or CALLBACK entries. Each PROVIDED feature
52 * uses the REGISTER/CALLBACK entry defined previously. The REGISTER entry
53 * defines a common feature registration function directly passed to the
54 * associated manager or factory (crypto/credential factory etc.). A callback
55 * function is more generic allows the loader to invoke a callback to do
58 * To conviently create feature lists, use the four macros PLUGIN_REGISTER,
59 * PLUGIN_CALLBACK, PLUGIN_PROVIDE, PLUGIN_DEPENDS and PLUGIN_SDEPEND. Use
60 * identation to show how the registration functions and dependencies are
61 * related to a provided feature, such as:
64 // two features, one with two dependencies, both use a callback to register
70 // common constructor to register for a feature with one dependency
74 // feature that does not use a registration function
78 struct plugin_feature_t
{
81 /* plugin provides this feature */
83 /* a feature depends on this feature, hard dependency */
85 /* a feature can optionally use this feature, soft dependency */
87 /* register the specified function for all following features */
89 /* use a callback to register all following features */
106 /** diffie_hellman_t */
110 /** generic private key support */
112 /** generating new private keys */
114 /** private_key_t->sign() */
115 FEATURE_PRIVKEY_SIGN
,
116 /** private_key_t->decrypt() */
117 FEATURE_PRIVKEY_DECRYPT
,
118 /** generic public key support */
120 /** public_key_t->verify() */
121 FEATURE_PUBKEY_VERIFY
,
122 /** public_key_t->encrypt() */
123 FEATURE_PUBKEY_ENCRYPT
,
124 /** parsing certificates */
126 /** generating certificates */
128 /** EAP server implementation */
130 /** EAP peer implementation */
136 /** custom feature, described with a string */
139 /** More specific data for each type */
141 /** FEATURE_CRYPTER */
143 encryption_algorithm_t alg
;
148 encryption_algorithm_t alg
;
151 /** FEATURE_SIGNER */
152 integrity_algorithm_t signer
;
154 pseudo_random_function_t prf
;
155 /** FEATURE_HASHER */
156 hash_algorithm_t hasher
;
158 diffie_hellman_group_t dh_group
;
160 rng_quality_t rng_quality
;
161 /** FEATURE_PRIVKEY */
163 /** FEATURE_PRIVKEY_GEN */
164 key_type_t privkey_gen
;
165 /** FEATURE_PRIVKEY_SIGN */
166 signature_scheme_t privkey_sign
;
167 /** FEATURE_PRIVKEY_DECRYPT */
168 encryption_scheme_t privkey_decrypt
;
169 /** FEATURE_PUBKEY */
171 /** FEATURE_PUBKEY_VERIFY */
172 signature_scheme_t pubkey_verify
;
173 /** FEATURE_PUBKEY_ENCRYPT */
174 encryption_scheme_t pubkey_encrypt
;
175 /** FEATURE_CERT_DECODE/ENCODE */
176 certificate_type_t cert
;
177 /** FEATURE_EAP_SERVER/CLIENT */
179 /** FEATURE_DATABASE */
180 db_driver_t database
;
181 /** FEATURE_FETCHER */
183 /** FEATURE_CUSTOM */
186 /** FEATURE_REGISTER */
188 /** feature specific function to register for this type */
190 /** final flag to pass for builder_function_t */
194 /** FEATURE_CALLBACK */
196 /** callback function to invoke for registration */
197 plugin_feature_callback_t f
;
198 /** data to pass to callback */
204 #define FEATURE(kind, type, ...) _PLUGIN_FEATURE_##type(kind, __VA_ARGS__)
207 * Define function to register directly for all upcoming features.
209 * @param type feature type to register
210 * @param f type specific function to register
211 * @param ... type specific additional arguments
213 #define PLUGIN_REGISTER(type, f, ...) _PLUGIN_FEATURE_REGISTER_##type(type, f, ##__VA_ARGS__)
216 * Define a callback to invoke for registering all upcoming features.
218 * @param type feature type to register
219 * @param cb type specific callback function to register
220 * @param data data pointer to pass to callback
222 #define PLUGIN_CALLBACK(cb, data) _PLUGIN_FEATURE_CALLBACK(cb, data)
225 * Define a feature the plugin provides.
227 * @param type feature type to provide
228 * @param ... type specific arguments
230 #define PLUGIN_PROVIDE(type, ...) _PLUGIN_FEATURE_##type(PROVIDE, __VA_ARGS__)
233 * Define a hard dependency for the previously defined feature.
235 * @param type feature type to provide
236 * @param ... type specific arguments
238 #define PLUGIN_DEPENDS(type, ...) _PLUGIN_FEATURE_##type(DEPENDS, __VA_ARGS__)
241 * Define a soft dependency for the previously defined feature.
243 * @param type feature type to provide
244 * @param ... type specific arguments
246 #define PLUGIN_SDEPEND(type, ...) _PLUGIN_FEATURE_##type(SDEPEND, __VA_ARGS__)
248 #define __PLUGIN_FEATURE(kind, type, ...) (plugin_feature_t){ FEATURE_##kind, FEATURE_##type, { __VA_ARGS__ }}
249 #define _PLUGIN_FEATURE_CRYPTER(kind, alg, size) __PLUGIN_FEATURE(kind, CRYPTER, .crypter = { alg, size })
250 #define _PLUGIN_FEATURE_AEAD(kind, alg, size) __PLUGIN_FEATURE(kind, AEAD, .aead = { alg, size })
251 #define _PLUGIN_FEATURE_SIGNER(kind, alg) __PLUGIN_FEATURE(kind, SIGNER, .signer = alg)
252 #define _PLUGIN_FEATURE_HASHER(kind, alg) __PLUGIN_FEATURE(kind, HASHER, .hasher = alg)
253 #define _PLUGIN_FEATURE_PRF(kind, alg) __PLUGIN_FEATURE(kind, PRF, .prf = alg)
254 #define _PLUGIN_FEATURE_DH(kind, group) __PLUGIN_FEATURE(kind, DH, .dh_group = group)
255 #define _PLUGIN_FEATURE_RNG(kind, quality) __PLUGIN_FEATURE(kind, RNG, .rng_quality = quality)
256 #define _PLUGIN_FEATURE_PRIVKEY(kind, type) __PLUGIN_FEATURE(kind, PRIVKEY, .privkey = type)
257 #define _PLUGIN_FEATURE_PRIVKEY_GEN(kind, type) __PLUGIN_FEATURE(kind, PRIVKEY_GEN, .privkey_gen = type)
258 #define _PLUGIN_FEATURE_PRIVKEY_SIGN(kind, scheme) __PLUGIN_FEATURE(kind, PRIVKEY_SIGN, .privkey_sign = scheme)
259 #define _PLUGIN_FEATURE_PRIVKEY_DECRYPT(kind, scheme) __PLUGIN_FEATURE(kind, PRIVKEY_DECRYPT, .privkey_decrypt = scheme)
260 #define _PLUGIN_FEATURE_PUBKEY(kind, type) __PLUGIN_FEATURE(kind, PUBKEY, .pubkey = type)
261 #define _PLUGIN_FEATURE_PUBKEY_VERIFY(kind, scheme) __PLUGIN_FEATURE(kind, PUBKEY_VERIFY, .pubkey_verify = scheme)
262 #define _PLUGIN_FEATURE_PUBKEY_ENCRYPT(kind, scheme) __PLUGIN_FEATURE(kind, PUBKEY_ENCRYPT, .pubkey_encrypt = scheme)
263 #define _PLUGIN_FEATURE_CERT_DECODE(kind, type) __PLUGIN_FEATURE(kind, CERT_DECODE, .cert = type)
264 #define _PLUGIN_FEATURE_CERT_ENCODE(kind, type) __PLUGIN_FEATURE(kind, CERT_ENCODE, .cert = type)
265 #define _PLUGIN_FEATURE_EAP_SERVER(kind, type) __PLUGIN_FEATURE(kind, EAP_SERVER, .eap = type)
266 #define _PLUGIN_FEATURE_EAP_PEER(kind, type) __PLUGIN_FEATURE(kind, EAP_PEER, .eap = type)
267 #define _PLUGIN_FEATURE_DATABASE(kind, type) __PLUGIN_FEATURE(kind, DATABASE, .database = type)
268 #define _PLUGIN_FEATURE_FETCHER(kind, type) __PLUGIN_FEATURE(kind, FETCHER, .fetcher = type)
270 #define __PLUGIN_FEATURE_REGISTER(type, _f) (plugin_feature_t){ FEATURE_REGISTER, FEATURE_##type, .arg.reg.f = _f }
271 #define __PLUGIN_FEATURE_REGISTER_BUILDER(type, _f, _final) (plugin_feature_t){ FEATURE_REGISTER, FEATURE_##type, .arg.reg = {.f = _f, .final = _final, }}
272 #define _PLUGIN_FEATURE_REGISTER_CRYPTER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
273 #define _PLUGIN_FEATURE_REGISTER_AEAD(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
274 #define _PLUGIN_FEATURE_REGISTER_SIGNER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
275 #define _PLUGIN_FEATURE_REGISTER_HASHER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
276 #define _PLUGIN_FEATURE_REGISTER_PRF(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
277 #define _PLUGIN_FEATURE_REGISTER_DH(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
278 #define _PLUGIN_FEATURE_REGISTER_RNG(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
279 #define _PLUGIN_FEATURE_REGISTER_PRIVKEY(type, f, final) __PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
280 #define _PLUGIN_FEATURE_REGISTER_PRIVKEY_GEN(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
281 #define _PLUGIN_FEATURE_REGISTER_PUBKEY(type, f, final) __PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
282 #define _PLUGIN_FEATURE_REGISTER_CERT_DECODE(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
283 #define _PLUGIN_FEATURE_REGISTER_CERT_ENCODE(type, f, final)__PLUGIN_FEATURE_REGISTER_BUILDER(type, f, final)
284 #define _PLUGIN_FEATURE_REGISTER_DATABASE(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
285 #define _PLUGIN_FEATURE_REGISTER_FETCHER(type, f) __PLUGIN_FEATURE_REGISTER(type, f)
287 #define _PLUGIN_FEATURE_CALLBACK(_cb, _data) (plugin_feature_t){ FEATURE_CALLBACK, FEATURE_NONE, .arg.cb = { .f = _cb, .data = _data } }
290 * Names for plugin_feature_t types.
292 extern enum_name_t
*plugin_feature_names
;
295 * Check if feature a matches to feature b.
297 * @param a feature to check
298 * @param b feature to match against
299 * @return TRUE if a matches b
301 bool plugin_feature_matches(plugin_feature_t
*a
, plugin_feature_t
*b
);
304 * Get a string describing feature.
306 * @param feature feature to describe
307 * @return allocated string describing feature
309 char* plugin_feature_get_string(plugin_feature_t
*feature
);
311 #endif /** PLUGIN_FEATURE_H_ @}*/