summaryrefslogtreecommitdiff
path: root/src/pluto/ac.c
blob: 3ee05d213dbc01e5acfc1113ee0c4ff4c7d95948 (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
/* Support of X.509 attribute certificates
 * Copyright (C) 2002 Ueli Galizzi, Ariane Seiler
 * Copyright (C) 2003 Martin Berner, Lukas Suter
 * Copyright (C) 2009 Andreas Steffen
 *
 * 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 <sys/stat.h>
#include <time.h>

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

#include "ac.h"
#include "ca.h"
#include "certs.h"
#include "fetch.h"
#include "log.h"

/**
 * Chained list of X.509 attribute certificates
 */
static linked_list_t *acerts   = NULL;

/**
 * Initialize the linked list of attribute certificates
 */
void ac_initialize(void)
{
	acerts = linked_list_create();
}

/**
 * Free the linked list of attribute certificates
 */
void ac_finalize(void)
{
	if (acerts)
	{
		acerts->destroy_offset(acerts, offsetof(certificate_t, destroy));
	}
}

/**
 *  Get a X.509 attribute certificate for a given holder
 */
certificate_t* ac_get_cert(identification_t *issuer, chunk_t serial)
{
	enumerator_t *enumerator;
	certificate_t *cert, *found = NULL;

	enumerator = acerts->create_enumerator(acerts);
	while (enumerator->enumerate(enumerator, &cert))
	{
		ac_t *ac = (ac_t*)cert;

		if (issuer->equals(issuer, ac->get_holderIssuer(ac)) &&
			  chunk_equals(serial, ac->get_holderSerial(ac)))
		{
			found = cert;
			break;
		}
	}
	enumerator->destroy(enumerator);
	return found;
}

/**
 * Verifies a X.509 attribute certificate
 */
bool ac_verify_cert(certificate_t *cert, bool strict)
{
	ac_t *ac = (ac_t*)cert;
	identification_t *subject = cert->get_subject(cert);
	identification_t *issuer  = cert->get_issuer(cert);
	chunk_t authKeyID = ac->get_authKeyIdentifier(ac);
	cert_t *aacert;
	time_t notBefore, valid_until;

	DBG1(DBG_LIB, "holder: '%Y'", subject);
	DBG1(DBG_LIB, "issuer: '%Y'", issuer);

	if (!cert->get_validity(cert, NULL, NULL, &valid_until))
	{
		DBG1(DBG_LIB, "attribute certificate is invalid (valid from %T to %T)",
			 &notBefore, FALSE, &valid_until, FALSE);
		return FALSE;
	}
	DBG1(DBG_LIB, "attribute certificate is valid until %T", &valid_until,
		 FALSE);

	lock_authcert_list("verify_x509acert");
	aacert = get_authcert(issuer, authKeyID, X509_AA);
	unlock_authcert_list("verify_x509acert");

	if (aacert == NULL)
	{
		DBG1(DBG_LIB, "issuer aacert not found");
		return FALSE;
	}
	DBG2(DBG_LIB, "issuer aacert found");

	if (!cert->issued_by(cert, aacert->cert))
	{
		DBG1(DBG_LIB, "attribute certificate signature is invalid");
		return FALSE;
	}
	DBG1(DBG_LIB, "attribute certificate signature is valid");

	return verify_x509cert(aacert, strict, &valid_until);
}

/**
 *  Add a X.509 attribute certificate to the chained list
 */
static void ac_add_cert(certificate_t *cert)
{
	ac_t *ac = (ac_t*)cert;
	identification_t *hIssuer = ac->get_holderIssuer(ac);
	chunk_t hSerial = ac->get_holderSerial(ac);

	enumerator_t *enumerator;
	certificate_t *cert_old;

	enumerator = acerts->create_enumerator(acerts);
	while (enumerator->enumerate(enumerator, &cert_old))
	{
		ac_t *ac_old = (ac_t*)cert_old;

		if (hIssuer->equals(hIssuer, ac_old->get_holderIssuer(ac_old)) &&
			   chunk_equals(hSerial, ac_old->get_holderSerial(ac_old)))
		{
			if (cert->is_newer(cert, cert_old))
			{
				acerts->remove_at(acerts, enumerator);
				cert_old->destroy(cert_old);
			}
			else
			{
				cert->destroy(cert);
				cert = NULL;
			}
			break;
		}
	}
	enumerator->destroy(enumerator);

	if (cert)
	{
		acerts->insert_last(acerts, cert);
	}
}

/**
 * Check if at least one peer attribute matches a connection attribute
 */
bool match_group_membership(ietf_attributes_t *peer_attributes, char *conn,
							ietf_attributes_t *conn_attributes)
{
	bool match;

	if (conn_attributes == NULL)
	{
		return TRUE;
	}

	match = conn_attributes->matches(conn_attributes, peer_attributes);
	DBG1(DBG_LIB, "%s: peer with attributes '%s' is %sa member of the "
		 "groups '%s'", conn, peer_attributes->get_string(peer_attributes),
		 match ? "" : "not ", conn_attributes->get_string(conn_attributes));

	return match;
}

/**
 * Loads X.509 attribute certificates
 */
void ac_load_certs(void)
{
	enumerator_t *enumerator;
	struct stat st;
	char *file;

	DBG1(DBG_LIB, "loading attribute certificates from '%s'", A_CERT_PATH);

	enumerator = enumerator_create_directory(A_CERT_PATH);
	if (!enumerator)
	{
		return;
	}

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

		if (!S_ISREG(st.st_mode))
		{
			/* skip special file */
			continue;
		}
		cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509_AC,
								  BUILD_FROM_FILE, file, BUILD_END);
		if (cert)
		{
			DBG1(DBG_LIB, "  loaded attribute certificate from '%s'", file);
			ac_add_cert(cert);
		}
	}
	enumerator->destroy(enumerator);
}

/**
 *  List all X.509 attribute certificates in the chained list
 */
void ac_list_certs(bool utc)
{
	enumerator_t *enumerator;
	certificate_t *cert;
	time_t now;

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

	if (acerts->get_count(acerts) > 0)
	{
		whack_log(RC_COMMENT, " ");
		whack_log(RC_COMMENT, "List of X.509 Attribute Certificates:");
	}

	enumerator = acerts->create_enumerator(acerts);
	while (enumerator->enumerate(enumerator, &cert))
	{
		ac_t *ac = (ac_t*)cert;
		identification_t *entityName, *holderIssuer, *issuer;
		chunk_t holderSerial, serial, authKeyID;
		time_t notBefore, notAfter;
		ietf_attributes_t *groups;

		whack_log(RC_COMMENT, " ");

		entityName = cert->get_subject(cert);
		if (entityName)
		{
			whack_log(RC_COMMENT, "  holder:   \"%Y\"", entityName);
		}

		holderIssuer = ac->get_holderIssuer(ac);
		if (holderIssuer)
		{
			whack_log(RC_COMMENT, "  hissuer:  \"%Y\"", holderIssuer);
		}

		holderSerial = ac->get_holderSerial(ac);
		if (holderSerial.ptr)
		{
			whack_log(RC_COMMENT, "  hserial:   %#B", &holderSerial);
		}

		groups = ac->get_groups(ac);
		if (groups)
		{
			whack_log(RC_COMMENT, "  groups:    %s", groups->get_string(groups));
			groups->destroy(groups);
		}

		issuer = cert->get_issuer(cert);
		whack_log(RC_COMMENT, "  issuer:   \"%Y\"", issuer);

		serial = ac->get_serial(ac);
		whack_log(RC_COMMENT, "  serial:    %#B", &serial);

		cert->get_validity(cert, &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, ACERT_WARNING_INTERVAL, TRUE));

		authKeyID = ac->get_authKeyIdentifier(ac);
		if (authKeyID.ptr)
		{
			whack_log(RC_COMMENT, "  authkey:   %#B", &authKeyID);
		}
	}
	enumerator->destroy(enumerator);
}