summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/plugin_feature.c
blob: 0ea5eeaf83ec6be6d3ff35f28575726fb6bf8fa8 (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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
 * Copyright (C) 2012-2015 Tobias Brunner
 * Hochschule fuer Technik Rapperswil
 *
 * 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 <utils/debug.h>

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

/**
 * See header.
 */
u_int32_t plugin_feature_hash(plugin_feature_t *feature)
{
	chunk_t data = chunk_empty;

	switch (feature->type)
	{
		case FEATURE_NONE:
		case FEATURE_RNG:
		case FEATURE_NONCE_GEN:
		case FEATURE_DATABASE:
		case FEATURE_FETCHER:
		case FEATURE_RESOLVER:
			/* put these special cases in their (type-specific) buckets */
			data = chunk_empty;
			break;
		case FEATURE_CRYPTER:
			data = chunk_from_thing(feature->arg.crypter);
			break;
		case FEATURE_AEAD:
			data = chunk_from_thing(feature->arg.aead);
			break;
		case FEATURE_SIGNER:
			data = chunk_from_thing(feature->arg.signer);
			break;
		case FEATURE_HASHER:
			data = chunk_from_thing(feature->arg.hasher);
			break;
		case FEATURE_PRF:
			data = chunk_from_thing(feature->arg.prf);
			break;
		case FEATURE_DH:
			data = chunk_from_thing(feature->arg.dh_group);
			break;
		case FEATURE_PRIVKEY:
			data = chunk_from_thing(feature->arg.privkey);
			break;
		case FEATURE_PRIVKEY_GEN:
			data = chunk_from_thing(feature->arg.privkey_gen);
			break;
		case FEATURE_PUBKEY:
			data = chunk_from_thing(feature->arg.pubkey);
			break;
		case FEATURE_PRIVKEY_SIGN:
			data = chunk_from_thing(feature->arg.privkey_sign);
			break;
		case FEATURE_PUBKEY_VERIFY:
			data = chunk_from_thing(feature->arg.pubkey_verify);
			break;
		case FEATURE_PRIVKEY_DECRYPT:
			data = chunk_from_thing(feature->arg.privkey_decrypt);
			break;
		case FEATURE_PUBKEY_ENCRYPT:
			data = chunk_from_thing(feature->arg.pubkey_encrypt);
			break;
		case FEATURE_CERT_DECODE:
		case FEATURE_CERT_ENCODE:
			data = chunk_from_thing(feature->arg.cert);
			break;
		case FEATURE_CONTAINER_DECODE:
		case FEATURE_CONTAINER_ENCODE:
			data = chunk_from_thing(feature->arg.container);
			break;
		case FEATURE_EAP_SERVER:
		case FEATURE_EAP_PEER:
			data = chunk_from_thing(feature->arg.eap);
			break;
		case FEATURE_CUSTOM:
			data = chunk_create(feature->arg.custom,
								strlen(feature->arg.custom));
			break;
		case FEATURE_XAUTH_SERVER:
		case FEATURE_XAUTH_PEER:
			data = chunk_create(feature->arg.xauth,
								strlen(feature->arg.xauth));
			break;
	}
	return chunk_hash_inc(chunk_from_thing(feature->type),
						  chunk_hash(data));
}

/**
 * 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_NONCE_GEN:
			case FEATURE_RESOLVER:
				return TRUE;
			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_CONTAINER_DECODE:
			case FEATURE_CONTAINER_ENCODE:
				return a->arg.container == b->arg.container;
			case FEATURE_EAP_SERVER:
			case FEATURE_EAP_PEER:
				return a->arg.eap.vendor == b->arg.eap.vendor &&
					   a->arg.eap.type == b->arg.eap.type;
			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);
			case FEATURE_XAUTH_SERVER:
			case FEATURE_XAUTH_PEER:
				return streq(a->arg.xauth, b->arg.xauth);
		}
	}
	return FALSE;
}

/**
 * See header.
 */
bool plugin_feature_equals(plugin_feature_t *a, plugin_feature_t *b)
{
	if (a->type == b->type)
	{
		switch (a->type)
		{
			case FEATURE_NONE:
			case FEATURE_CRYPTER:
			case FEATURE_AEAD:
			case FEATURE_SIGNER:
			case FEATURE_HASHER:
			case FEATURE_PRF:
			case FEATURE_DH:
			case FEATURE_NONCE_GEN:
			case FEATURE_RESOLVER:
			case FEATURE_PRIVKEY:
			case FEATURE_PRIVKEY_GEN:
			case FEATURE_PUBKEY:
			case FEATURE_PRIVKEY_SIGN:
			case FEATURE_PUBKEY_VERIFY:
			case FEATURE_PRIVKEY_DECRYPT:
			case FEATURE_PUBKEY_ENCRYPT:
			case FEATURE_CERT_DECODE:
			case FEATURE_CERT_ENCODE:
			case FEATURE_CONTAINER_DECODE:
			case FEATURE_CONTAINER_ENCODE:
			case FEATURE_EAP_SERVER:
			case FEATURE_EAP_PEER:
			case FEATURE_CUSTOM:
			case FEATURE_XAUTH_SERVER:
			case FEATURE_XAUTH_PEER:
				return plugin_feature_matches(a, b);
			case FEATURE_RNG:
				return a->arg.rng_quality == b->arg.rng_quality;
			case FEATURE_DATABASE:
				return a->arg.database == b->arg.database;
			case FEATURE_FETCHER:
				if (a->arg.fetcher && b->arg.fetcher)
				{
					return streq(a->arg.fetcher, b->arg.fetcher);
				}
				return !a->arg.fetcher && !b->arg.fetcher;
		}
	}
	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_NONCE_GEN:
		case FEATURE_RESOLVER:
			if (asprintf(&str, "%N", plugin_feature_names, feature->type) > 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_CONTAINER_DECODE:
		case FEATURE_CONTAINER_ENCODE:
			if (asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					container_type_names, feature->arg.container) > 0)
			{
				return str;
			}
			break;
		case FEATURE_EAP_SERVER:
		case FEATURE_EAP_PEER:
			if (feature->arg.eap.vendor &&
				asprintf(&str, "%N:%d-%d", plugin_feature_names, feature->type,
					feature->arg.eap.type, feature->arg.eap.vendor) > 0)
			{
				return str;
			}
			else if (!feature->arg.eap.vendor &&
				asprintf(&str, "%N:%N", plugin_feature_names, feature->type,
					eap_type_short_names, feature->arg.eap.type) > 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;
		case FEATURE_XAUTH_SERVER:
		case FEATURE_XAUTH_PEER:
			if (asprintf(&str, "%N:%s", plugin_feature_names, feature->type,
					feature->arg.xauth) > 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 ||
			 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,
								feature->arg.crypter.key_size,
								name, reg->arg.reg.f);
			break;
		case FEATURE_AEAD:
			lib->crypto->add_aead(lib->crypto, feature->arg.aead.alg,
								feature->arg.aead.key_size,
								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_NONCE_GEN:
			lib->crypto->add_nonce_gen(lib->crypto,
								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_CONTAINER_DECODE:
		case FEATURE_CONTAINER_ENCODE:
			lib->creds->add_builder(lib->creds, CRED_CONTAINER,
								feature->arg.container, 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;
		case FEATURE_RESOLVER:
			lib->resolver->add_resolver(lib->resolver, reg->arg.reg.f);
			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 ||
			 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_NONCE_GEN:
			lib->crypto->remove_nonce_gen(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_CONTAINER_DECODE:
		case FEATURE_CONTAINER_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;
		case FEATURE_RESOLVER:
			lib->resolver->remove_resolver(lib->resolver, reg->arg.reg.f);
			break;
		default:
			break;
	}
	return TRUE;
}