summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/plugin_feature.c
blob: 2a97205bb1541b3edd0707aef6aa5ba20764d185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * Copyright (C) 2011 Martin Willi
 * Copyright (C) 2011 revosec AG
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#define _GNU_SOURCE
#include <stdio.h>

#include "plugin_feature.h"

#include <debug.h>

ENUM(plugin_feature_names, FEATURE_NONE, FEATURE_CUSTOM,
	"NONE",
	"CRYPTER",
	"AEAD",
	"SIGNER",
	"HASHER",
	"PRF",
	"DH",
	"RNG",
	"PRIVKEY",
	"PRIVKEY_GEN",
	"PRIVKEY_SIGN",
	"PRIVKEY_DECRYPT",
	"PUBKEY",
	"PUBKEY_VERIFY",
	"PUBKEY_ENCRYPT",
	"CERT_DECODE",
	"CERT_ENCODE",
	"EAP_SERVER",
	"EAP_CLIENT",
	"DATABASE",
	"FETCHER",
	"CUSTOM",
);

/**
 * See header.
 */
bool plugin_feature_matches(plugin_feature_t *a, plugin_feature_t *b)
{
	if (a->type == b->type)
	{
		switch (a->type)
		{
			case FEATURE_NONE:
				return FALSE;
			case FEATURE_CRYPTER:
				return a->arg.crypter.alg == b->arg.crypter.alg &&
					   a->arg.crypter.key_size == b->arg.crypter.key_size;
			case FEATURE_AEAD:
				return a->arg.aead.alg == b->arg.aead.alg &&
					   a->arg.aead.key_size == b->arg.aead.key_size;
			case FEATURE_SIGNER:
				return a->arg.signer == b->arg.signer;
			case FEATURE_HASHER:
				return a->arg.hasher == b->arg.hasher;
			case FEATURE_PRF:
				return a->arg.prf == b->arg.prf;
			case FEATURE_DH:
				return a->arg.dh_group == b->arg.dh_group;
			case FEATURE_RNG:
				return a->arg.rng_quality <= b->arg.rng_quality;
			case FEATURE_PRIVKEY:
			case FEATURE_PRIVKEY_GEN:
			case FEATURE_PUBKEY:
				return a->arg.privkey == b->arg.privkey;
			case FEATURE_PRIVKEY_SIGN:
			case FEATURE_PUBKEY_VERIFY:
				return a->arg.privkey_sign == b->arg.privkey_sign;
			case FEATURE_PRIVKEY_DECRYPT:
			case FEATURE_PUBKEY_ENCRYPT:
				return a->arg.privkey_decrypt == b->arg.privkey_decrypt;
			case FEATURE_CERT_DECODE:
			case FEATURE_CERT_ENCODE:
				return a->arg.cert == b->arg.cert;
			case FEATURE_EAP_SERVER:
			case FEATURE_EAP_PEER:
				return a->arg.eap == b->arg.eap;
			case FEATURE_DATABASE:
				return a->arg.database == DB_ANY ||
					   a->arg.database == b->arg.database;
			case FEATURE_FETCHER:
				return a->arg.fetcher == NULL ||
					   streq(a->arg.fetcher, b->arg.fetcher);
			case FEATURE_CUSTOM:
				return streq(a->arg.custom, b->arg.custom);
		}
	}
	return FALSE;
}

/**
 * See header.
 */
char* plugin_feature_get_string(plugin_feature_t *feature)
{
	char *str = NULL;

	if (feature->kind == FEATURE_REGISTER)
	{
		return strdup("(register function)");
	}
	switch (feature->type)
	{
		case FEATURE_NONE:
			return strdup("NONE");
		case FEATURE_CRYPTER:
			if (asprintf(&str, "%N:%N-%d", plugin_feature_names, feature->type,
					encryption_algorithm_names, feature->arg.crypter.alg,
					feature->arg.crypter.key_size) > 0)
			{
				return str;
			}
			break;
		case FEATURE_AEAD:
			if (asprintf(&str, "%N:%N-%d", plugin_feature_names, feature->type,
					encryption_algorithm_names, feature->arg.aead.alg,
					feature->arg.aead.key_size) > 0)
			{
				return str;
			}
			break;
		case FEATURE_SIGNER:
			if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					integrity_algorithm_names, feature->arg.signer) > 0)
			{
				return str;
			}
			break;
		case FEATURE_HASHER:
			if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					hash_algorithm_names, feature->arg.hasher) > 0)
			{
				return str;
			}
			break;
		case FEATURE_PRF:
			if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					pseudo_random_function_names, feature->arg.prf) > 0)
			{
				return str;
			}
			break;
		case FEATURE_DH:
			if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					diffie_hellman_group_names, feature->arg.dh_group) > 0)
			{
				return str;
			}
			break;
		case FEATURE_RNG:
			if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					rng_quality_names, feature->arg.rng_quality) > 0)
			{
				return str;
			}
			break;
		case FEATURE_PRIVKEY:
		case FEATURE_PRIVKEY_GEN:
		case FEATURE_PUBKEY:
			if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					key_type_names, feature->arg.privkey) > 0)
			{
				return str;
			}
			break;
		case FEATURE_PRIVKEY_SIGN:
		case FEATURE_PUBKEY_VERIFY:
			if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					signature_scheme_names, feature->arg.privkey_sign) > 0)
			{
				return str;
			}
			break;
		case FEATURE_PRIVKEY_DECRYPT:
		case FEATURE_PUBKEY_ENCRYPT:
			if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					encryption_scheme_names, feature->arg.privkey_decrypt) > 0)
			{
				return str;
			}
			break;
		case FEATURE_CERT_DECODE:
		case FEATURE_CERT_ENCODE:
			if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					certificate_type_names, feature->arg.cert) > 0)
			{
				return str;
			}
			break;
		case FEATURE_EAP_SERVER:
		case FEATURE_EAP_PEER:
			if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					eap_type_short_names, feature->arg.eap) > 0)
			{
				return str;
			}
			break;
		case FEATURE_DATABASE:
			if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					db_driver_names, feature->arg.database) > 0)
			{
				return str;
			}
			break;
		case FEATURE_FETCHER:
			if (asprintf(&str, "%N:%s", plugin_feature_names, feature->type,
					feature->arg.fetcher) > 0)
			{
				return str;
			}
			break;
		case FEATURE_CUSTOM:
			if (asprintf(&str, "%N:%s", plugin_feature_names, feature->type,
					feature->arg.custom) > 0)
			{
				return str;
			}
			break;
	}
	if (!str)
	{
		str = strdup("(invalid)");
	}
	return str;
}

/**
 * See header.
 */
bool plugin_feature_load(plugin_t *plugin, plugin_feature_t *feature,
						 plugin_feature_t *reg)
{
	char *name;

	if (!reg)
	{	/* noting to do for this feature */
		return TRUE;
	}
	if (reg->kind == FEATURE_CALLBACK)
	{
		if (reg->arg.cb.f(plugin, feature, TRUE, reg->arg.cb.data))
		{
			return TRUE;
		}
		return FALSE;
	}
	name = plugin->get_name(plugin);
	switch (feature->type)
	{
		case FEATURE_CRYPTER:
			lib->crypto->add_crypter(lib->crypto, feature->arg.crypter.alg,
								name, reg->arg.reg.f);
			break;
		case FEATURE_AEAD:
			lib->crypto->add_aead(lib->crypto, feature->arg.aead.alg,
								name, reg->arg.reg.f);
			break;
		case FEATURE_SIGNER:
			lib->crypto->add_signer(lib->crypto, feature->arg.signer,
								name, reg->arg.reg.f);
			break;
		case FEATURE_HASHER:
			lib->crypto->add_hasher(lib->crypto, feature->arg.hasher,
								name, reg->arg.reg.f);
			break;
		case FEATURE_PRF:
			lib->crypto->add_prf(lib->crypto, feature->arg.prf,
								name, reg->arg.reg.f);
			break;
		case FEATURE_DH:
			lib->crypto->add_dh(lib->crypto, feature->arg.dh_group,
								name, reg->arg.reg.f);
			break;
		case FEATURE_RNG:
			lib->crypto->add_rng(lib->crypto, feature->arg.rng_quality,
								name, reg->arg.reg.f);
			break;
		case FEATURE_PRIVKEY:
		case FEATURE_PRIVKEY_GEN:
			lib->creds->add_builder(lib->creds, CRED_PRIVATE_KEY,
								feature->arg.privkey, reg->arg.reg.final,
								reg->arg.reg.f);
			break;
		case FEATURE_PUBKEY:
			lib->creds->add_builder(lib->creds, CRED_PUBLIC_KEY,
								feature->arg.pubkey, reg->arg.reg.final,
								reg->arg.reg.f);
			break;
		case FEATURE_CERT_DECODE:
		case FEATURE_CERT_ENCODE:
			lib->creds->add_builder(lib->creds, CRED_CERTIFICATE,
								feature->arg.cert, reg->arg.reg.final,
								reg->arg.reg.f);
			break;
		case FEATURE_DATABASE:
			lib->db->add_database(lib->db, reg->arg.reg.f);
			break;
		case FEATURE_FETCHER:
			lib->fetcher->add_fetcher(lib->fetcher, reg->arg.reg.f,
									  feature->arg.fetcher);
			break;
		default:
			break;
	}
	return TRUE;
}

/**
 * See header.
 */
bool plugin_feature_unload(plugin_t *plugin, plugin_feature_t *feature,
						   plugin_feature_t *reg)
{
	if (!reg)
	{	/* noting to do for this feature */
		return TRUE;
	}
	if (reg->kind == FEATURE_CALLBACK)
	{
		if (reg->arg.cb.f(plugin, feature, FALSE, reg->arg.cb.data))
		{
			return TRUE;
		}
		return FALSE;
	}
	switch (feature->type)
	{
		case FEATURE_CRYPTER:
			lib->crypto->remove_crypter(lib->crypto, reg->arg.reg.f);
			break;
		case FEATURE_AEAD:
			lib->crypto->remove_aead(lib->crypto, reg->arg.reg.f);
			break;
		case FEATURE_SIGNER:
			lib->crypto->remove_signer(lib->crypto, reg->arg.reg.f);
			break;
		case FEATURE_HASHER:
			lib->crypto->remove_hasher(lib->crypto, reg->arg.reg.f);
			break;
		case FEATURE_PRF:
			lib->crypto->remove_prf(lib->crypto, reg->arg.reg.f);
			break;
		case FEATURE_DH:
			lib->crypto->remove_dh(lib->crypto, reg->arg.reg.f);
			break;
		case FEATURE_RNG:
			lib->crypto->remove_rng(lib->crypto, reg->arg.reg.f);
			break;
		case FEATURE_PRIVKEY:
		case FEATURE_PRIVKEY_GEN:
			lib->creds->remove_builder(lib->creds, reg->arg.reg.f);
			break;
		case FEATURE_PUBKEY:
			lib->creds->remove_builder(lib->creds, reg->arg.reg.f);
			break;
		case FEATURE_CERT_DECODE:
		case FEATURE_CERT_ENCODE:
			lib->creds->remove_builder(lib->creds, reg->arg.reg.f);
			break;
		case FEATURE_DATABASE:
			lib->db->remove_database(lib->db, reg->arg.reg.f);
			break;
		case FEATURE_FETCHER:
			lib->fetcher->remove_fetcher(lib->fetcher, reg->arg.reg.f);
			break;
		default:
			break;
	}
	return TRUE;
}