summaryrefslogtreecommitdiff
path: root/src/pluto/x509.c
blob: f017e57752fcf5ee5e049f79e09d426253cf7a13 (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
/* Support of X.509 certificates
 * Copyright (C) 2000 Andreas Hess, Patric Lichtsteiner, Roger Wegmann
 * Copyright (C) 2001 Marco Bertossa, Andreas Schleiss
 * Copyright (C) 2002 Mario Strasser
 * Copyright (C) 2000-2009 Andreas Steffen - Hochschule fuer Technik Rapperswil
 *
 * 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 <unistd.h>
#include <dirent.h>
#include <time.h>
#include <sys/types.h>

#include <freeswan.h>

#include <asn1/asn1.h>
#include <crypto/hashers/hasher.h>
#include <utils/enumerator.h>
#include <utils/identification.h>

#include "constants.h"
#include "defs.h"
#include "log.h"
#include "x509.h"
#include "crl.h"
#include "ca.h"
#include "certs.h"
#include "keys.h"
#include "whack.h"
#include "fetch.h"
#include "ocsp.h"

/**
 * Check for equality between two key identifiers
 */
bool same_keyid(chunk_t a, chunk_t b)
{
	if (a.ptr == NULL || b.ptr == NULL)
	{
		return FALSE;
	}
	return chunk_equals(a, b);
}

/**
 * Stores a chained list of end certs and CA certs
 */
void store_x509certs(linked_list_t *certs, bool strict)
{
	cert_t *x509cert, *cacerts = NULL;
	certificate_t *cert;
	enumerator_t *enumerator;

	/* first extract CA certs, ignoring self-signed root CA certs */

	enumerator = certs->create_enumerator(certs);
	while (enumerator->enumerate(enumerator, &cert))
	{
		x509_t *x509 = (x509_t*)cert;
		x509_flag_t flags;

		flags = x509->get_flags(x509);
		if (flags & X509_CA)
		{
			/* we don't accept self-signed CA certs */
			if (flags & X509_SELF_SIGNED)
			{
				plog("self-signed cacert rejected");
			}
			else
			{
				/* insertion into temporary chain of candidate CA certs */
				x509cert = malloc_thing(cert_t);
				*x509cert = cert_empty;
				x509cert->cert = cert->get_ref(cert);
				x509cert->next = cacerts;
				cacerts = x509cert;
			}
		}
	}
	enumerator->destroy(enumerator);

	/* now verify the candidate CA certs */

	while (cacerts)
	{
		cert_t *cert = cacerts;

		cacerts = cacerts->next;

		if (trust_authcert_candidate(cert, cacerts))
		{
			add_authcert(cert, X509_CA);
		}
		else
		{
			plog("intermediate cacert rejected");
			cert_free(cert);
		}
	}

	/* now verify the end certificates */

	enumerator = certs->create_enumerator(certs);
	while (enumerator->enumerate(enumerator, &cert))
	{
		time_t valid_until;
		x509_t *x509 = (x509_t*)cert;

		if (!(x509->get_flags(x509) & X509_CA))
		{
			x509cert = malloc_thing(cert_t);
			*x509cert = cert_empty;
			x509cert->cert = cert->get_ref(cert);

			if (verify_x509cert(x509cert, strict, &valid_until))
			{
				DBG(DBG_CONTROL | DBG_PARSING,
					DBG_log("public key validated")
				)
				add_public_key_from_cert(x509cert, valid_until, DAL_SIGNED);
			}
			else
			{
				plog("X.509 certificate rejected");
				cert_free(x509cert);
			}
		}
	}
	enumerator->destroy(enumerator);
}

/**
 * Check if a signature over binary blob is genuine
 */
bool x509_check_signature(chunk_t tbs, chunk_t sig, int algorithm,
						  certificate_t *issuer_cert)
{
	bool success;
	public_key_t *key;
	signature_scheme_t scheme;

	scheme = signature_scheme_from_oid(algorithm);
	if (scheme == SIGN_UNKNOWN)
	{
		return FALSE;
	}

	key = issuer_cert->get_public_key(issuer_cert);
	if (key == NULL)
	{
		return FALSE;
	}
	success = key->verify(key, scheme, tbs, sig);
	key->destroy(key);

	return success;
}

/**
 * Build an ASN.1 encoded PKCS#1 signature over a binary blob
 */
chunk_t x509_build_signature(chunk_t tbs, int algorithm, private_key_t *key,
							 bool bit_string)
{
	chunk_t signature;
	signature_scheme_t scheme = signature_scheme_from_oid(algorithm);

	if (scheme == SIGN_UNKNOWN || !key->sign(key, scheme, tbs, &signature))
	{
		return chunk_empty;
	}
	return (bit_string) ? asn1_bitstring("m", signature)
						: asn1_wrap(ASN1_OCTET_STRING, "m", signature);
}

/**
 * Verifies a X.509 certificate
 */
bool verify_x509cert(cert_t *cert, bool strict, time_t *until)
{
	int pathlen, pathlen_constraint;

	*until = 0;

	for (pathlen = -1; pathlen <= X509_MAX_PATH_LEN; pathlen++)
	{
		certificate_t *certificate = cert->cert;
		identification_t *subject = certificate->get_subject(certificate);
		identification_t *issuer  = certificate->get_issuer(certificate);
		x509_t *x509 = (x509_t*)certificate;
		chunk_t authKeyID = x509->get_authKeyIdentifier(x509);
		cert_t *issuer_cert;
		time_t notBefore, notAfter;
		bool valid;

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

		valid = certificate->get_validity(certificate, NULL,
										  &notBefore, &notAfter);
		if (*until == UNDEFINED_TIME || notAfter < *until)
		{
			*until = notAfter;
		}
		if (!valid)
		{
			plog("certificate is invalid (valid from %T to %T)",
				 &notBefore, FALSE, &notAfter, FALSE);
			return FALSE;
		}
		DBG(DBG_CONTROL,
			DBG_log("certificate is valid")
		)

		lock_authcert_list("verify_x509cert");
		issuer_cert = get_authcert(issuer, authKeyID, X509_CA);
		if (issuer_cert == NULL)
		{
			plog("issuer cacert not found");
			unlock_authcert_list("verify_x509cert");
			return FALSE;
		}
		DBG(DBG_CONTROL,
			DBG_log("issuer cacert found")
		)

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

		/* check path length constraint */
		pathlen_constraint = x509->get_constraint(x509, X509_PATH_LEN);
		if (pathlen_constraint != X509_NO_CONSTRAINT &&
			pathlen > pathlen_constraint)
		{
			plog("path length of %d violates constraint of %d",
				 pathlen, pathlen_constraint);
			return FALSE;
		}

		/* 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 with a path length of %d",
						 pathlen)
			)
			return TRUE;
		}
		else
		{
			time_t nextUpdate = *until;
			time_t revocationDate = UNDEFINED_TIME;
			crl_reason_t revocationReason = CRL_REASON_UNSPECIFIED;

			/* first check certificate revocation using ocsp */
			cert_status_t status = verify_by_ocsp(cert, &nextUpdate
				, &revocationDate, &revocationReason);

			/* if ocsp service is not available then fall back to crl */
			if ((status == CERT_UNDEFINED)
			||  (status == CERT_UNKNOWN && strict))
			{
				status = verify_by_crl(cert, &nextUpdate, &revocationDate
					, &revocationReason);
			}

			switch (status)
			{
			case CERT_GOOD:
				/* if status information is stale */
				if (strict && nextUpdate < time(NULL))
				{
					DBG(DBG_CONTROL,
						DBG_log("certificate is good but status is stale")
					)
					remove_x509_public_key(cert);
					return FALSE;
				}
				DBG(DBG_CONTROL,
					DBG_log("certificate is good")
				)

				/* with strict crl policy the public key must have the same
				 * lifetime as the validity of the ocsp status or crl lifetime
				 */
				if (strict && nextUpdate < *until)
				{
					*until = nextUpdate;
				}
				break;
			case CERT_REVOKED:
				plog("certificate was revoked on %T, reason: %N"
					, &revocationDate, TRUE
					, crl_reason_names, revocationReason);
				remove_x509_public_key(cert);
				return FALSE;
			case CERT_UNKNOWN:
			case CERT_UNDEFINED:
			default:
				plog("certificate status unknown");
				if (strict)
				{
					remove_x509_public_key(cert);
					return FALSE;
				}
				break;
			}
		}

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

/**
 * List all X.509 certs in a chained list
 */
void list_x509cert_chain(const char *caption, cert_t* cert,
						 x509_flag_t flags, bool utc)
{
	bool first = TRUE;
	time_t now;

	/* determine the current time */
	time(&now);

	while (cert)
	{
		certificate_t *certificate = cert->cert;
		x509_t *x509 = (x509_t*)certificate;

		if (certificate->get_type(certificate) == CERT_X509 &&
		   (flags == X509_NONE || (flags & x509->get_flags(x509))))
		{
			enumerator_t *enumerator;
			char buf[BUF_LEN];
			char *pos = buf;
			int len = BUF_LEN, pathlen;
			bool first_altName = TRUE;
			identification_t *id;
			time_t notBefore, notAfter;
			public_key_t *key;
			chunk_t serial, keyid, subjkey, authkey;

			if (first)
			{
				whack_log(RC_COMMENT, " ");
				whack_log(RC_COMMENT, "List of X.509 %s Certificates:", caption);
				first = FALSE;
			}
			whack_log(RC_COMMENT, " ");

			enumerator = x509->create_subjectAltName_enumerator(x509);
			while (enumerator->enumerate(enumerator, &id))
			{
				int written;

				if (first_altName)
				{
					written = snprintf(pos, len, "%Y", id);
					first_altName = FALSE;
				}
				else
				{
					written = snprintf(pos, len, ", %Y", id);
				}
				if (written < 0 || written >= len)
				{
					break;
				}
				pos += written;
				len -= written;
			}
			enumerator->destroy(enumerator);
			if (!first_altName)
			{
				whack_log(RC_COMMENT, "  altNames:  %s", buf);
			}

			whack_log(RC_COMMENT, "  subject:  \"%Y\"",
				certificate->get_subject(certificate));
			whack_log(RC_COMMENT, "  issuer:   \"%Y\"",
				certificate->get_issuer(certificate));
				serial = chunk_skip_zero(x509->get_serial(x509));
			whack_log(RC_COMMENT, "  serial:    %#B", &serial);

			/* list validity */
			certificate->get_validity(certificate, &now, &notBefore, &notAfter);
			whack_log(RC_COMMENT, "  validity:  not before %T %s",
				&notBefore, utc,
				(notBefore < now)?"ok":"fatal (not valid yet)");
			whack_log(RC_COMMENT, "             not after  %T %s",
				&notAfter, utc,
				check_expiry(notAfter, CA_CERT_WARNING_INTERVAL, TRUE));

			key = certificate->get_public_key(certificate);
			if (key)
			{
				whack_log(RC_COMMENT, "  pubkey:    %N %4d bits%s",
					key_type_names, key->get_type(key),
					key->get_keysize(key),
					cert->smartcard ? ", on smartcard" :
					(has_private_key(cert)? ", has private key" : ""));

				if (key->get_fingerprint(key, KEYID_PUBKEY_INFO_SHA1, &keyid))
				{
					whack_log(RC_COMMENT, "  keyid:     %#B", &keyid);
				}
				if (key->get_fingerprint(key, KEYID_PUBKEY_SHA1, &subjkey))
				{
					whack_log(RC_COMMENT, "  subjkey:   %#B", &subjkey);
				}
				key->destroy(key);
			}

			/* list optional authorityKeyIdentifier */
			authkey = x509->get_authKeyIdentifier(x509);
			if (authkey.ptr)
			{
				whack_log(RC_COMMENT, "  authkey:   %#B", &authkey);
			}

			/* list optional pathLenConstraint */
			pathlen = x509->get_constraint(x509, X509_PATH_LEN);
			if (pathlen != X509_NO_CONSTRAINT)
			{
				whack_log(RC_COMMENT, "  pathlen:   %d", pathlen);
			}

		}
		cert = cert->next;
	}
}