summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/vici/vici_cred.c
blob: 6c7c194c2d08273dfd26b8083f7415df5ba69398 (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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
 * Copyright (C) 2015-2016 Andreas Steffen
 * Copyright (C) 2016 Tobias Brunner
 * HSR Hochschule fuer Technik Rapperswil
 *
 * Copyright (C) 2014 Martin Willi
 * Copyright (C) 2014 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.
 */

#include "vici_cred.h"
#include "vici_builder.h"
#include "vici_cert_info.h"

#include <credentials/sets/mem_cred.h>
#include <credentials/certificates/ac.h>
#include <credentials/certificates/crl.h>
#include <credentials/certificates/x509.h>

#include <errno.h>

typedef struct private_vici_cred_t private_vici_cred_t;

/**
 * Directory for saved X.509 CRLs
 */
#define CRL_DIR SWANCTLDIR "/x509crl"

/**
 * Private data of an vici_cred_t object.
 */
struct private_vici_cred_t {

	/**
	 * Public vici_cred_t interface.
	 */
	vici_cred_t public;

	/**
	 * Dispatcher
	 */
	vici_dispatcher_t *dispatcher;

	/**
	 * credentials
	 */
	mem_cred_t *creds;

	/**
	 * separate credential set for token PINs
	 */
	mem_cred_t *pins;

	/**
	 * cache CRLs to disk?
	 */
	bool cachecrl;

};

METHOD(credential_set_t, cache_cert, void,
	private_vici_cred_t *this, certificate_t *cert)
{
	if (cert->get_type(cert) == CERT_X509_CRL && this->cachecrl)
	{
		/* CRLs get written to /etc/swanctl/x509crl/<authkeyId>.crl */
		crl_t *crl = (crl_t*)cert;

		cert->get_ref(cert);
		if (this->creds->add_crl(this->creds, crl))
		{
			char buf[BUF_LEN];
			chunk_t chunk, hex;
			bool is_delta_crl;

			is_delta_crl = crl->is_delta_crl(crl, NULL);
			chunk = crl->get_authKeyIdentifier(crl);
			hex = chunk_to_hex(chunk, NULL, FALSE);
			snprintf(buf, sizeof(buf), "%s/%s%s.crl", CRL_DIR, hex.ptr,
										is_delta_crl ? "_delta" : "");
			free(hex.ptr);

			if (cert->get_encoding(cert, CERT_ASN1_DER, &chunk))
			{
				if (chunk_write(chunk, buf, 022, TRUE))
				{
					DBG1(DBG_CFG, "  written crl file '%s' (%d bytes)",
						 buf, chunk.len);
				}
				else
				{
					DBG1(DBG_CFG, "  writing crl file '%s' failed: %s",
						 buf, strerror(errno));
				}
				free(chunk.ptr);
			}
		}
	}
}

/**
 * Create a (error) reply message
 */
static vici_message_t* create_reply(char *fmt, ...)
{
	vici_builder_t *builder;
	va_list args;

	builder = vici_builder_create();
	builder->add_kv(builder, "success", fmt ? "no" : "yes");
	if (fmt)
	{
		va_start(args, fmt);
		builder->vadd_kv(builder, "errmsg", fmt, args);
		va_end(args);
	}
	return builder->finalize(builder);
}

CALLBACK(load_cert, vici_message_t*,
	private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
{
	certificate_t *cert;
	certificate_type_t type;
	x509_flag_t ext_flag, flag = X509_NONE;
	x509_t *x509;
	chunk_t data;
	bool trusted = TRUE;
	char *str;

	str = message->get_str(message, NULL, "type");
	if (!str)
	{
		return create_reply("certificate type missing");
	}
	if (enum_from_name(certificate_type_names, str, &type))
	{
		if (type == CERT_X509)
		{
			str = message->get_str(message, "NONE", "flag");
			if (!enum_from_name(x509_flag_names, str, &flag))
			{
				return create_reply("invalid certificate flag '%s'", str);
			}
		}
	}
	else if	(!vici_cert_info_from_str(str, &type, &flag))
	{
		return create_reply("invalid certificate type '%s'", str);
	}

	data = message->get_value(message, chunk_empty, "data");
	if (!data.len)
	{
		return create_reply("certificate data missing");
	}

	/* do not set CA flag externally */
	ext_flag = (flag & X509_CA) ? X509_NONE : flag;

	cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, type,
							  BUILD_BLOB_PEM, data,
							  BUILD_X509_FLAG, ext_flag,
							  BUILD_END);
	if (!cert)
	{
		return create_reply("parsing %N certificate failed",
							certificate_type_names, type);
	}
	DBG1(DBG_CFG, "loaded certificate '%Y'", cert->get_subject(cert));

	/* check if CA certificate has CA basic constraint set */
	if (flag & X509_CA)
	{
		char err_msg[] = "ca certificate lacks CA basic constraint, rejected";
		x509 = (x509_t*)cert;

		if (!(x509->get_flags(x509) & X509_CA))
		{
			cert->destroy(cert);
			DBG1(DBG_CFG, "  %s", err_msg);
			return create_reply(err_msg);
		}
	}
	if (type == CERT_X509_CRL)
	{
		this->creds->add_crl(this->creds, (crl_t*)cert);
	}
	else
	{
		this->creds->add_cert(this->creds, trusted, cert);
	}
	return create_reply(NULL);
}

CALLBACK(load_key, vici_message_t*,
	private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
{
	key_type_t type;
	private_key_t *key;
	chunk_t data;
	char *str;

	str = message->get_str(message, NULL, "type");
	if (!str)
	{
		return create_reply("key type missing");
	}
	if (strcaseeq(str, "any"))
	{
		type = KEY_ANY;
	}
	else if (strcaseeq(str, "rsa"))
	{
		type = KEY_RSA;
	}
	else if (strcaseeq(str, "ecdsa"))
	{
		type = KEY_ECDSA;
	}
	else if (strcaseeq(str, "bliss"))
	{
		type = KEY_BLISS;
	}
	else
	{
		return create_reply("invalid key type: %s", str);
	}
	data = message->get_value(message, chunk_empty, "data");
	if (!data.len)
	{
		return create_reply("key data missing");
	}
	key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
							 BUILD_BLOB_PEM, data, BUILD_END);
	if (!key)
	{
		return create_reply("parsing %N private key failed",
							key_type_names, type);
	}

	DBG1(DBG_CFG, "loaded %N private key", key_type_names, type);

	this->creds->add_key(this->creds, key);

	return create_reply(NULL);
}

CALLBACK(unload_key, vici_message_t*,
	private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
{
	chunk_t keyid;
	char buf[BUF_LEN], *hex, *msg = NULL;

	hex = message->get_str(message, NULL, "id");
	if (!hex)
	{
		return create_reply("key id missing");
	}
	keyid = chunk_from_hex(chunk_from_str(hex), NULL);
	snprintf(buf, sizeof(buf), "%+B", &keyid);
	DBG1(DBG_CFG, "unloaded private key with id %s", buf);
	if (this->creds->remove_key(this->creds, keyid))
	{	/* also remove any potential PIN associated with this id */
		this->pins->remove_shared_unique(this->pins, buf);
	}
	else
	{
		msg = "key not found";
	}
	chunk_free(&keyid);
	return create_reply(msg);
}

CALLBACK(get_keys, vici_message_t*,
	private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
{
	vici_builder_t *builder;
	enumerator_t *enumerator;
	private_key_t *private;
	chunk_t keyid;

	builder = vici_builder_create();
	builder->begin_list(builder, "keys");

	enumerator = this->creds->set.create_private_enumerator(&this->creds->set,
															KEY_ANY, NULL);
	while (enumerator->enumerate(enumerator, &private))
	{
		if (private->get_fingerprint(private, KEYID_PUBKEY_SHA1, &keyid))
		{
			builder->add_li(builder, "%+B", &keyid);
		}
	}
	enumerator->destroy(enumerator);

	builder->end_list(builder);
	return builder->finalize(builder);
}

CALLBACK(load_token, vici_message_t*,
	private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
{
	vici_builder_t *builder;
	private_key_t *key;
	shared_key_t *shared = NULL;
	identification_t *owner;
	mem_cred_t *set = NULL;
	chunk_t handle, fp;
	char buf[BUF_LEN], *hex, *module, *pin, *unique = NULL;
	int slot;

	hex = message->get_str(message, NULL, "handle");
	if (!hex)
	{
		return create_reply("keyid missing");
	}
	handle = chunk_from_hex(chunk_from_str(hex), NULL);
	slot = message->get_int(message, -1, "slot");
	module = message->get_str(message, NULL, "module");
	pin = message->get_str(message, NULL, "pin");

	if (pin)
	{	/* provide the pin in a temporary credential set to access the key */
		shared = shared_key_create(SHARED_PIN, chunk_clone(chunk_from_str(pin)));
		owner = identification_create_from_encoding(ID_KEY_ID, handle);
		set = mem_cred_create();
		set->add_shared(set, shared->get_ref(shared), owner, NULL);
		lib->credmgr->add_local_set(lib->credmgr, &set->set, FALSE);
	}
	if (slot >= 0)
	{
		key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_ANY,
						BUILD_PKCS11_KEYID, handle,
						BUILD_PKCS11_SLOT, slot,
						module ? BUILD_PKCS11_MODULE : BUILD_END, module,
						BUILD_END);
	}
	else
	{
		key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_ANY,
						BUILD_PKCS11_KEYID, handle,
						module ? BUILD_PKCS11_MODULE : BUILD_END, module,
						BUILD_END);
	}
	if (set)
	{
		lib->credmgr->remove_local_set(lib->credmgr, &set->set);
		set->destroy(set);
	}
	if (!key)
	{
		chunk_free(&handle);
		DESTROY_IF(shared);
		return create_reply("loading private key from token failed");
	}
	builder = vici_builder_create();
	builder->add_kv(builder, "success", "yes");
	if (key->get_fingerprint(key, KEYID_PUBKEY_SHA1, &fp))
	{
		snprintf(buf, sizeof(buf), "%+B", &fp);
		builder->add_kv(builder, "id", "%s", buf);
		unique = buf;
	}
	if (shared && unique)
	{	/* use the handle as owner, but the key identifier as unique ID */
		owner = identification_create_from_encoding(ID_KEY_ID, handle);
		this->pins->add_shared_unique(this->pins, unique, shared,
									linked_list_create_with_items(owner, NULL));
	}
	else
	{
		DESTROY_IF(shared);
	}
	DBG1(DBG_CFG, "loaded %N private key from token", key_type_names,
		 key->get_type(key));
	this->creds->add_key(this->creds, key);
	chunk_free(&handle);
	return builder->finalize(builder);
}

CALLBACK(shared_owners, bool,
	linked_list_t *owners, vici_message_t *message, char *name, chunk_t value)
{
	if (streq(name, "owners"))
	{
		char buf[256];

		if (!vici_stringify(value, buf, sizeof(buf)))
		{
			return FALSE;
		}
		owners->insert_last(owners, identification_create_from_string(buf));
	}
	return TRUE;
}

CALLBACK(load_shared, vici_message_t*,
	private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
{
	shared_key_type_t type;
	linked_list_t *owners;
	chunk_t data;
	char *unique, *str, buf[512] = "";
	enumerator_t *enumerator;
	identification_t *owner;
	int len;

	unique = message->get_str(message, NULL, "id");
	str = message->get_str(message, NULL, "type");
	if (!str)
	{
		return create_reply("shared key type missing");
	}
	if (strcaseeq(str, "ike"))
	{
		type = SHARED_IKE;
	}
	else if (strcaseeq(str, "eap") || streq(str, "xauth"))
	{
		type = SHARED_EAP;
	}
	else if (strcaseeq(str, "ntlm"))
	{
		type = SHARED_NT_HASH;
	}
	else
	{
		return create_reply("invalid shared key type: %s", str);
	}
	data = message->get_value(message, chunk_empty, "data");
	if (!data.len)
	{
		return create_reply("shared key data missing");
	}

	owners = linked_list_create();
	if (!message->parse(message, NULL, NULL, NULL, shared_owners, owners))
	{
		owners->destroy_offset(owners, offsetof(identification_t, destroy));
		return create_reply("parsing shared key owners failed");
	}
	if (owners->get_count(owners) == 0)
	{
		owners->insert_last(owners, identification_create_from_string("%any"));
	}

	enumerator = owners->create_enumerator(owners);
	while (enumerator->enumerate(enumerator, &owner))
	{
		len = strlen(buf);
		if (len < sizeof(buf))
		{
			snprintf(buf + len, sizeof(buf) - len, "%s'%Y'",
					 len ? ", " : "", owner);
		}
	}
	enumerator->destroy(enumerator);

	if (unique)
	{
		DBG1(DBG_CFG, "loaded %N shared key with id '%s' for: %s",
			 shared_key_type_names, type, unique, buf);
	}
	else
	{
		DBG1(DBG_CFG, "loaded %N shared key for: %s",
			 shared_key_type_names, type, buf);
	}

	this->creds->add_shared_unique(this->creds, unique,
						shared_key_create(type, chunk_clone(data)), owners);

	return create_reply(NULL);
}

CALLBACK(unload_shared, vici_message_t*,
	private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
{
	char *unique;

	unique = message->get_str(message, NULL, "id");
	if (!unique)
	{
		return create_reply("unique identifier missing");
	}
	DBG1(DBG_CFG, "unloaded shared key with id '%s'", unique);
	this->creds->remove_shared_unique(this->creds, unique);
	return create_reply(NULL);
}

CALLBACK(get_shared, vici_message_t*,
	private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
{
	vici_builder_t *builder;
	enumerator_t *enumerator;
	char *unique;

	builder = vici_builder_create();
	builder->begin_list(builder, "keys");

	enumerator = this->creds->create_unique_shared_enumerator(this->creds);
	while (enumerator->enumerate(enumerator, &unique))
	{
		builder->add_li(builder, "%s", unique);
	}
	enumerator->destroy(enumerator);

	builder->end_list(builder);
	return builder->finalize(builder);
}

CALLBACK(clear_creds, vici_message_t*,
	private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
{
	this->creds->clear(this->creds);
	lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);

	return create_reply(NULL);
}

CALLBACK(flush_certs, vici_message_t*,
	private_vici_cred_t *this, char *name, u_int id, vici_message_t *message)
{
	certificate_type_t type = CERT_ANY;
	x509_flag_t flag = X509_NONE;
	char *str;

	str = message->get_str(message, NULL, "type");
	if (str && !enum_from_name(certificate_type_names, str, &type) &&
			   !vici_cert_info_from_str(str, &type, &flag))
	{
		return create_reply("invalid certificate type '%s'", str);
	}
	lib->credmgr->flush_cache(lib->credmgr, type);

	return create_reply(NULL);
}

static void manage_command(private_vici_cred_t *this,
						   char *name, vici_command_cb_t cb, bool reg)
{
	this->dispatcher->manage_command(this->dispatcher, name,
									 reg ? cb : NULL, this);
}

/**
 * (Un-)register dispatcher functions
 */
static void manage_commands(private_vici_cred_t *this, bool reg)
{
	manage_command(this, "clear-creds", clear_creds, reg);
	manage_command(this, "flush-certs", flush_certs, reg);
	manage_command(this, "load-cert", load_cert, reg);
	manage_command(this, "load-key", load_key, reg);
	manage_command(this, "unload-key", unload_key, reg);
	manage_command(this, "get-keys", get_keys, reg);
	manage_command(this, "load-token", load_token, reg);
	manage_command(this, "load-shared", load_shared, reg);
	manage_command(this, "unload-shared", unload_shared, reg);
	manage_command(this, "get-shared", get_shared, reg);
}

METHOD(vici_cred_t, add_cert, certificate_t*,
	private_vici_cred_t *this, certificate_t *cert)
{
	return this->creds->add_cert_ref(this->creds, TRUE, cert);
}

METHOD(vici_cred_t, destroy, void,
	private_vici_cred_t *this)
{
	manage_commands(this, FALSE);

	lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
	this->creds->destroy(this->creds);
	lib->credmgr->remove_set(lib->credmgr, &this->pins->set);
	this->pins->destroy(this->pins);
	free(this);
}

/**
 * See header
 */
vici_cred_t *vici_cred_create(vici_dispatcher_t *dispatcher)
{
	private_vici_cred_t *this;

	INIT(this,
		.public = {
			.set = {
				.create_private_enumerator = (void*)return_null,
				.create_cert_enumerator = (void*)return_null,
				.create_shared_enumerator = (void*)return_null,
				.create_cdp_enumerator = (void*)return_null,
				.cache_cert = (void*)_cache_cert,
			},
			.add_cert = _add_cert,
			.destroy = _destroy,
		},
		.dispatcher = dispatcher,
		.creds = mem_cred_create(),
		.pins = mem_cred_create(),
	);

	if (lib->settings->get_bool(lib->settings, "%s.cache_crls", FALSE, lib->ns))
	{
		this->cachecrl = TRUE;
		DBG1(DBG_CFG, "crl caching to %s enabled", CRL_DIR);
	}
	lib->credmgr->add_set(lib->credmgr, &this->creds->set);
	lib->credmgr->add_set(lib->credmgr, &this->pins->set);

	manage_commands(this, TRUE);

	return &this->public;
}