summaryrefslogtreecommitdiff
path: root/src/pluto/ca.c
blob: e25e7f6f5b9e3034d3c80c03e65dee01de0da655 (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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/* Certification Authority (CA) support for IKE authentication
 * Copyright (C) 2002-2004 Andreas Steffen, Zuercher Hochschule Winterthur
 *
 * 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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <debug.h>
#include <utils/enumerator.h>
#include <credentials/certificates/x509.h>

#include <freeswan.h>

#include "constants.h"
#include "defs.h"
#include "log.h"
#include "x509.h"
#include "ca.h"
#include "certs.h"
#include "whack.h"
#include "fetch.h"
#include "smartcard.h"

/* chained list of X.509 authority certificates (ca, aa, and ocsp) */

static cert_t *x509authcerts = NULL;

/* chained list of X.509 certification authority information records */

static ca_info_t *ca_infos = NULL;

/*
 * Checks if CA a is trusted by CA b
 */
bool trusted_ca(identification_t *a, identification_t *b, int *pathlen)
{
	bool match = FALSE;

	/* no CA b specified -> any CA a is accepted */
	if (b == NULL)
	{
		*pathlen = (a == NULL) ? 0 : X509_MAX_PATH_LEN;
		return TRUE;
	}

	/* no CA a specified -> trust cannot be established */
	if (a == NULL)
	{
		*pathlen = X509_MAX_PATH_LEN;
		return FALSE;
	}

	*pathlen = 0;

	/* CA a equals CA b -> we have a match */
	if (a->equals(a, b))
	{
		return TRUE;
	}

	/* CA a might be a subordinate CA of b */
	lock_authcert_list("trusted_ca");

	while ((*pathlen)++ < X509_MAX_PATH_LEN)
	{
		certificate_t *certificate;
		identification_t *issuer;
		cert_t *cacert;

		cacert = get_authcert(a, chunk_empty, X509_CA);
		if (cacert == NULL)
		{
			break;
		}
		certificate = cacert->cert;
		
		/* is the certificate self-signed? */
		{
			x509_t *x509 = (x509_t*)certificate;

			if (x509->get_flags(x509) & X509_SELF_SIGNED)
			{
				break;
			}
		}

		/* does the issuer of CA a match CA b? */
		issuer = certificate->get_issuer(certificate);
		match = b->equals(b, issuer);

		/* we have a match and exit the loop */
		if (match)
		{
			break;
		}
		/* go one level up in the CA chain */
		a = issuer;
	}

	unlock_authcert_list("trusted_ca");
	return match;
}

/*
 * does our CA match one of the requested CAs?
 */
bool match_requested_ca(linked_list_t *requested_ca, identification_t *our_ca,
						int *our_pathlen)
{
	identification_t *ca;
	enumerator_t *enumerator;

	/* if no ca is requested than any ca will match */
	if (requested_ca == NULL || requested_ca->get_count(requested_ca) == 0)
	{
		*our_pathlen = 0;
		return TRUE;
	}

	*our_pathlen = X509_MAX_PATH_LEN + 1;

	enumerator = requested_ca->create_enumerator(requested_ca);
	while (enumerator->enumerate(enumerator, &ca))
	{
		int pathlen;

		if (trusted_ca(our_ca, ca, &pathlen) && pathlen < *our_pathlen)
		{
			*our_pathlen = pathlen;
		}
	}
	enumerator->destroy(enumerator);

	if (*our_pathlen > X509_MAX_PATH_LEN)
	{
		*our_pathlen = X509_MAX_PATH_LEN;
		return FALSE;
	}
	else
	{
		return TRUE;
	}
}

/*
 *  free the first authority certificate in the chain
 */
static void free_first_authcert(void)
{
	cert_t *first = x509authcerts;

	x509authcerts = first->next;
	cert_free(first);
}

/*
 *  free  all CA certificates
 */
void free_authcerts(void)
{
	lock_authcert_list("free_authcerts");

	while (x509authcerts != NULL)
	{
		free_first_authcert();
	}
	unlock_authcert_list("free_authcerts");
}

/*
 *  get a X.509 authority certificate with a given subject or keyid
 */
cert_t* get_authcert(identification_t *subject, chunk_t keyid,
						 x509_flag_t auth_flags)
{
	cert_t *cert, *prev_cert = NULL;

	/* the authority certificate list is empty */
	if (x509authcerts == NULL)
	{
		return NULL;
	}

	for (cert = x509authcerts; cert != NULL; prev_cert = cert, cert = cert->next)
	{
		certificate_t *certificate = cert->cert;
		x509_t *x509 = (x509_t*)certificate;

		/* skip non-matching types of authority certificates */
		if (!(x509->get_flags(x509) & auth_flags))
		{
			continue;
		}

		/* compare the keyid with the certificate's subjectKeyIdentifier */
		if (keyid.ptr)
		{
			chunk_t subjectKeyId;

			subjectKeyId = x509->get_subjectKeyIdentifier(x509);
			if (subjectKeyId.ptr && !chunk_equals(keyid, subjectKeyId))
			{
				continue;
			}
		}

		/* compare the subjectDistinguishedNames */
		if (!certificate->has_subject(certificate, subject))
		{
			continue;
		}

		/* found the authcert */
		if (cert != x509authcerts)
		{
			/* bring the certificate up front */
			prev_cert->next = cert->next;
			cert->next = x509authcerts;
			x509authcerts = cert;
		}
		return cert;
	}
	return NULL;
}

/*
 * add an authority certificate to the chained list
 */
cert_t* add_authcert(cert_t *cert, x509_flag_t auth_flags)
{
	certificate_t *certificate = cert->cert;
	x509_t *x509 = (x509_t*)certificate;
	cert_t *old_cert;

	lock_authcert_list("add_authcert");

	old_cert = get_authcert(certificate->get_subject(certificate), 
							x509->get_subjectKeyIdentifier(x509),
							auth_flags);
	if (old_cert)
	{
		if (certificate->equals(certificate, old_cert->cert))
		{
			DBG(DBG_CONTROL | DBG_PARSING ,
				DBG_log("  authcert is already present and identical")
			)
			unlock_authcert_list("add_authcert");

			cert_free(cert);
			return old_cert;
		}
		else
		{
			/* cert is already present but will be replaced by new cert */
			free_first_authcert();
			DBG(DBG_CONTROL | DBG_PARSING ,
				DBG_log("  existing authcert deleted")
			)
		}
	}

	/* add new authcert to chained list */
	cert->next = x509authcerts;
	x509authcerts = cert;
	cert_share(cert);  /* set count to one */
	DBG(DBG_CONTROL | DBG_PARSING,
		DBG_log("  authcert inserted")
	)
	unlock_authcert_list("add_authcert");
	return cert;
}

/*
 *  Loads authority certificates
 */
void load_authcerts(char *type, char *path, x509_flag_t auth_flags)
{
	enumerator_t *enumerator;
	struct stat st;
	char *file;

	DBG1("loading %s certificates from '%s'", type, path);

	enumerator = enumerator_create_directory(path);
	if (!enumerator)
	{
		DBG1("  reading directory '%s' failed");
		return;
	}

	while (enumerator->enumerate(enumerator, NULL, &file, &st))
	{
		cert_t *cert;

		if (!S_ISREG(st.st_mode))
		{
			/* skip special file */
			continue;
		}
		cert = load_cert(file, type, auth_flags);
		if (cert)
		{
			add_authcert(cert, auth_flags);
		}
	}
	enumerator->destroy(enumerator);
}

/*
 *  list all X.509 authcerts with given auth flags in a chained list
 */
void list_authcerts(const char *caption, x509_flag_t auth_flags, bool utc)
{
	lock_authcert_list("list_authcerts");
	list_x509cert_chain(caption, x509authcerts, auth_flags, utc);
	unlock_authcert_list("list_authcerts");
}

/*
 * get a cacert with a given subject or keyid from an alternative list
 */
static const cert_t* get_alt_cacert(identification_t *subject, chunk_t keyid,
										const cert_t *cert)
{
	if (cert == NULL)
	{
		return NULL;
	}
	for (; cert != NULL; cert = cert->next)
	{
		certificate_t *certificate = cert->cert;

		/* compare the keyid with the certificate's subjectKeyIdentifier */
		if (keyid.ptr)
		{
			x509_t *x509 = (x509_t*)certificate;
			chunk_t subjectKeyId;

			subjectKeyId = x509->get_subjectKeyIdentifier(x509);
			if (subjectKeyId.ptr && !chunk_equals(keyid, subjectKeyId))
			{
				continue;
			}
		}

		/* compare the subjectDistinguishedNames */
		if (!certificate->has_subject(certificate, subject))
		{
			continue;
		}

		/* we found the cacert */
		return cert;
	}
	return NULL;
}

/* establish trust into a candidate authcert by going up the trust chain.
 * validity and revocation status are not checked.
 */
bool trust_authcert_candidate(const cert_t *cert, const cert_t *alt_chain)
{
	int pathlen;

	lock_authcert_list("trust_authcert_candidate");

	for (pathlen = 0; pathlen < X509_MAX_PATH_LEN; pathlen++)
	{
		certificate_t *certificate = cert->cert;
		x509_t *x509 = (x509_t*)certificate;
		identification_t *subject = certificate->get_subject(certificate);
		identification_t *issuer = certificate->get_issuer(certificate);
		chunk_t authKeyID = x509->get_authKeyIdentifier(x509);
		const cert_t *authcert = NULL;

		DBG(DBG_CONTROL,
			DBG_log("subject: '%Y'", subject);
			DBG_log("issuer:  '%Y'", issuer);
			if (authKeyID.ptr != NULL)
			{
				DBG_log("authkey:  %#B", &authKeyID);
			}
		)

		/* search in alternative chain first */
		authcert = get_alt_cacert(issuer, authKeyID, alt_chain);

		if (authcert != NULL)
		{
			DBG(DBG_CONTROL,
				DBG_log("issuer cacert found in alternative chain")
			)
		}
		else
		{
			/* search in trusted chain */
			authcert = get_authcert(issuer, authKeyID, X509_CA);

			if (authcert != NULL)
			{
				DBG(DBG_CONTROL,
					DBG_log("issuer cacert found")
				)
			}
			else
			{
				plog("issuer cacert not found");
				unlock_authcert_list("trust_authcert_candidate");
				return FALSE;
			}
		}

		if (!certificate->issued_by(certificate, authcert->cert))
		{
			plog("certificate signature is invalid");
			unlock_authcert_list("trust_authcert_candidate");
			return FALSE;
		}
		DBG(DBG_CONTROL,
			DBG_log("certificate signature is valid")
		)

		/* check if cert is a self-signed root ca */
		if (pathlen > 0 && (x509->get_flags(x509) & X509_SELF_SIGNED))
		{
			DBG(DBG_CONTROL,
				DBG_log("reached self-signed root ca")
			)
			unlock_authcert_list("trust_authcert_candidate");
			return TRUE;
		}

		/* go up one step in the trust chain */
		cert = authcert;
	}
	plog("maximum ca path length of %d levels exceeded", X509_MAX_PATH_LEN);
	unlock_authcert_list("trust_authcert_candidate");
	return FALSE;
}

/*
 *  get a CA info record with a given authName or authKeyID
 */
ca_info_t* get_ca_info(identification_t *name, chunk_t keyid)
{
	ca_info_t *ca= ca_infos;

	while (ca != NULL)
	{
		if ((keyid.ptr) ? same_keyid(keyid, ca->authKeyID)
			: name->equals(name, ca->authName))
		{
			return ca;
		}
		ca = ca->next;
	}
	return NULL;
}


/*
 *  free the dynamic memory used by a ca_info record
 */
static void
free_ca_info(ca_info_t* ca_info)
{
	if (ca_info == NULL)
	{
		return;
	}
	ca_info->crluris->destroy_function(ca_info->crluris, free);
	DESTROY_IF(ca_info->authName);
	free(ca_info->name);
	free(ca_info->ldaphost);
	free(ca_info->ldapbase);
	free(ca_info->ocspuri);
	free(ca_info->authKeyID.ptr);
	free(ca_info);
}

/*
 *  free  all CA certificates
 */
void free_ca_infos(void)
{
	while (ca_infos != NULL)
	{
		ca_info_t *ca = ca_infos;

		ca_infos = ca_infos->next;
		free_ca_info(ca);
	}
}

/*
 * find a CA information record by name and optionally delete it
 */
bool find_ca_info_by_name(const char *name, bool delete)
{
	ca_info_t **ca_p = &ca_infos;
	ca_info_t *ca = *ca_p;

	while (ca != NULL)
	{
		/* is there already an entry? */
		if (streq(name, ca->name))
		{
			if (delete)
			{
				lock_ca_info_list("find_ca_info_by_name");
				*ca_p = ca->next;
				free_ca_info(ca);
				plog("deleting ca description \"%s\"", name);
				unlock_ca_info_list("find_ca_info_by_name");
			}
			return TRUE;
		}
		ca_p = &ca->next;
		ca = *ca_p;
	}
	return FALSE;
}

/*
 * Create an empty ca_info_t record
 */
ca_info_t* create_ca_info(void)
{
	ca_info_t *ca_info = malloc_thing(ca_info_t);

	memset(ca_info, 0, sizeof(ca_info_t));
	ca_info->crluris = linked_list_create();

	return ca_info;
}

/**
 * Adds a CA description to a chained list
 */
void add_ca_info(const whack_message_t *msg)
{
	smartcard_t *sc = NULL;
	cert_t *cert = NULL;
	bool cached_cert = FALSE;

	if (find_ca_info_by_name(msg->name, FALSE))
	{
		loglog(RC_DUPNAME, "attempt to redefine ca record \"%s\"", msg->name);
		return;
	}

	if (scx_on_smartcard(msg->cacert))
	{
		/* load CA cert from smartcard */
		cert = scx_load_cert(msg->cacert, &sc, &cached_cert);
	}
	else
	{
		/* load CA cert from file */
		cert = load_ca_cert(msg->cacert);
	}

	if (cert)
	{
		certificate_t *certificate = cert->cert;
		x509_t *x509 = (x509_t*)certificate;
		identification_t *subject = certificate->get_subject(certificate);
		chunk_t subjectKeyID = x509->get_subjectKeyIdentifier(x509);
		ca_info_t *ca = NULL;

		/* does the authname already exist? */
		ca = get_ca_info(subject, subjectKeyID);

		if (ca != NULL)
		{
			/* ca_info is already present */
			loglog(RC_DUPNAME, "  duplicate ca information in record \"%s\" found,"
							   "ignoring \"%s\"", ca->name, msg->name);
			cert_free(cert);
			return;
		}

		plog("added ca description \"%s\"", msg->name);

		/* create and initialize new ca_info record */
		ca = create_ca_info();

		/* name */
		ca->name = clone_str(msg->name);

		/* authName */
		ca->authName = subject->clone(subject);
		DBG(DBG_CONTROL,
			DBG_log("authname: '%Y'", subject)
		)

		/* authKeyID */
		if (subjectKeyID.ptr)
		{
			ca->authKeyID = chunk_clone(subjectKeyID);
			DBG(DBG_CONTROL | DBG_PARSING ,
				DBG_log("authkey:  %#B", &subjectKeyID)
			)
		}

		/* ldaphost */
		ca->ldaphost = clone_str(msg->ldaphost);

		/* ldapbase */
		ca->ldapbase = clone_str(msg->ldapbase);

		/* ocspuri */
		if (msg->ocspuri != NULL)
		{
			if (strncasecmp(msg->ocspuri, "http", 4) == 0)
				ca->ocspuri = clone_str(msg->ocspuri);
			else
				plog("  ignoring ocspuri with unkown protocol");
		}

		/* add crl uris */
		add_distribution_point(ca->crluris, msg->crluri);
		add_distribution_point(ca->crluris, msg->crluri2);

		/* strictrlpolicy */
		ca->strictcrlpolicy = msg->whack_strict;

		/* insert ca_info record into the chained list */
		lock_ca_info_list("add_ca_info");

		ca->next = ca_infos;
		ca_infos = ca;

		unlock_ca_info_list("add_ca_info");

		/* add cacert to list of authcerts */
		cert = add_authcert(cert, X509_CA);
		if (!cached_cert && sc != NULL)
		{
			if (sc->last_cert != NULL)
			{
				sc->last_cert->count--;
			}
			sc->last_cert = cert;
			cert_share(sc->last_cert);
		}
		if (sc != NULL)
			time(&sc->last_load);
	}
}

/*
 * list all ca_info records in the chained list
 */
void list_ca_infos(bool utc)
{
	ca_info_t *ca = ca_infos;

	if (ca != NULL)
	{
		whack_log(RC_COMMENT, " ");
		whack_log(RC_COMMENT, "List of X.509 CA Information Records:");
	}

	while (ca != NULL)
	{
		/* strictpolicy per CA not supported yet
		 *
		whack_log(RC_COMMENT, "%T, \"%s\", strictcrlpolicy: %s"
				, &ca->installed, utc, ca->name
				, ca->strictcrlpolicy? "yes":"no");
		*/
		whack_log(RC_COMMENT, " ");
		whack_log(RC_COMMENT, "  authname: \"%Y\"", ca->authName);
		if (ca->ldaphost)
		{
			whack_log(RC_COMMENT, "  ldaphost: '%s'", ca->ldaphost);
		}
		if (ca->ldapbase)
		{
			whack_log(RC_COMMENT, "  ldapbase: '%s'", ca->ldapbase);
		}
		if (ca->ocspuri)
		{
			whack_log(RC_COMMENT, "  ocspuri:  '%s'", ca->ocspuri);
		}

		list_distribution_points(ca->crluris);

		if (ca->authKeyID.ptr)
		{
			whack_log(RC_COMMENT, "  authkey:   %#B", &ca->authKeyID);
		}
		ca = ca->next;
	}
}