summaryrefslogtreecommitdiff
path: root/src/charon/sa/tasks/ike_cert.c
blob: 1606007423ef23f83602a701b5c4baf065460801 (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
/**
 * @file ike_cert.c
 *
 * @brief Implementation of the ike_cert task.
 *
 */

/*
 * Copyright (C) 2006-2007 Martin Willi
 * 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 "ike_cert.h"

#include <daemon.h>
#include <sa/ike_sa.h>
#include <crypto/hashers/hasher.h>
#include <encoding/payloads/cert_payload.h>
#include <encoding/payloads/certreq_payload.h>


typedef struct private_ike_cert_t private_ike_cert_t;

/**
 * Private members of a ike_cert_t task.
 */
struct private_ike_cert_t {
	
	/**
	 * Public methods and task_t interface.
	 */
	ike_cert_t public;
	
	/**
	 * Assigned IKE_SA.
	 */
	ike_sa_t *ike_sa;
	
	/**
	 * Are we the initiator?
	 */
	bool initiator;
	
	/**
	 * list of CA cert hashes requested, items point to 20 byte chunk
	 */
	linked_list_t *cas;
	
	/** 
	 * have we seen a certificate request?
	 */
	bool certreq_seen;
};

/**
 * read certificate requests 
 */
static void process_certreqs(private_ike_cert_t *this, message_t *message)
{
	iterator_t *iterator;
	payload_t *payload;
	
	iterator = message->get_payload_iterator(message);
	while (iterator->iterate(iterator, (void**)&payload))
	{
		if (payload->get_type(payload) == CERTIFICATE_REQUEST)
		{
			certreq_payload_t *certreq = (certreq_payload_t*)payload;
			cert_encoding_t encoding;
			chunk_t keyids, keyid;
			
			this->certreq_seen = TRUE;
			
			encoding = certreq->get_cert_encoding(certreq);
			if (encoding != CERT_X509_SIGNATURE)
			{
				DBG1(DBG_IKE, "certreq payload %N not supported, ignored",
					 cert_encoding_names, encoding);
				continue;
			}

			keyids = certreq->get_data(certreq);
					
			while (keyids.len >= HASH_SIZE_SHA1)
			{
				keyid = chunk_create(keyids.ptr, HASH_SIZE_SHA1);
				keyid = chunk_clone(keyid);
				this->cas->insert_last(this->cas, keyid.ptr);
				keyids = chunk_skip(keyids, HASH_SIZE_SHA1);
			}
		}
	}
	iterator->destroy(iterator);
}

/**
 * import certificates
 */
static void process_certs(private_ike_cert_t *this, message_t *message)
{
	iterator_t *iterator;
	payload_t *payload;
	
	iterator = message->get_payload_iterator(message);
	while (iterator->iterate(iterator, (void**)&payload))
	{
		if (payload->get_type(payload) == CERTIFICATE)
		{
			cert_encoding_t encoding;
			x509_t *cert;
			chunk_t cert_data;
			bool found;
			cert_payload_t *cert_payload = (cert_payload_t*)payload;
			
			encoding = cert_payload->get_cert_encoding(cert_payload);
			if (encoding != CERT_X509_SIGNATURE)
			{
				DBG1(DBG_IKE, "certificate payload %N not supported, ignored",
					 cert_encoding_names, encoding);
				continue;
			}
			
			cert_data = cert_payload->get_data_clone(cert_payload);
			cert = x509_create_from_chunk(cert_data, 0);
			if (cert)
			{
				if (charon->credentials->verify(charon->credentials,
												cert, &found))
				{
					DBG2(DBG_IKE, "received end entity certificate is trusted, "
						 "added to store");
					if (!found)
					{
						charon->credentials->add_end_certificate(
													charon->credentials, cert);
					}
					else
					{
						cert->destroy(cert);
					}
				}
				else
				{
					DBG1(DBG_IKE, "received end entity certificate is not "
						 "trusted, discarded");
					cert->destroy(cert);
				}
			}
			else
			{
				DBG1(DBG_IKE, "parsing of received certificate failed, discarded");
				chunk_free(&cert_data);
			}
		}
	}
	iterator->destroy(iterator);
}

/**
 * build certificate requests
 */
static void build_certreqs(private_ike_cert_t *this, message_t *message)
{
	connection_t *connection;
	policy_t *policy;
	identification_t *ca;
	certreq_payload_t *certreq;
	
	connection = this->ike_sa->get_connection(this->ike_sa);
	
	if (connection->get_certreq_policy(connection) != CERT_NEVER_SEND)
	{	
		policy = this->ike_sa->get_policy(this->ike_sa);
		
		if (policy)
		{
			ca = policy->get_other_ca(policy);
			
			if (ca && ca->get_type(ca) != ID_ANY)
			{
				certreq = certreq_payload_create_from_cacert(ca);
			}
			else
			{
				certreq = certreq_payload_create_from_cacerts();
			}
		}
		else
		{
			certreq = certreq_payload_create_from_cacerts();
		}
		
		if (certreq)
		{
			message->add_payload(message, (payload_t*)certreq);
		}
	}
}

/**
 * add certificates to message
 */
static void build_certs(private_ike_cert_t *this, message_t *message)
{
	policy_t *policy;
	connection_t *connection;
	x509_t *cert;
	cert_payload_t *payload;
	
	policy = this->ike_sa->get_policy(this->ike_sa);
	connection = this->ike_sa->get_connection(this->ike_sa);

	if (policy && policy->get_auth_method(policy) == AUTH_RSA)
	{
		switch (connection->get_cert_policy(connection))
		{
			case CERT_NEVER_SEND:
				break;
			case CERT_SEND_IF_ASKED:
				if (!this->certreq_seen)
				{
					break;
				}
				/* FALL */
			case CERT_ALWAYS_SEND:
			{
				/* TODO: respect CA cert request */
				cert = charon->credentials->get_certificate(charon->credentials,
													policy->get_my_id(policy));
				if (cert)
				{
					payload = cert_payload_create_from_x509(cert);
					message->add_payload(message, (payload_t*)payload);
				}
			}	
		}
	}
}

/**
 * Implementation of task_t.process for initiator
 */
static status_t build_i(private_ike_cert_t *this, message_t *message)
{
	if (message->get_exchange_type(message) == IKE_SA_INIT)
	{
		return NEED_MORE;
	}
		
	build_certreqs(this, message);
	build_certs(this, message);
	
	return NEED_MORE;
}

/**
 * Implementation of task_t.process for responder
 */
static status_t process_r(private_ike_cert_t *this, message_t *message)
{	
	if (message->get_exchange_type(message) == IKE_SA_INIT)
	{
		return NEED_MORE;
	}
	
	process_certreqs(this, message);
	process_certs(this, message);
	
	return NEED_MORE;
}

/**
 * Implementation of task_t.build for responder
 */
static status_t build_r(private_ike_cert_t *this, message_t *message)
{
	if (message->get_exchange_type(message) == IKE_SA_INIT)
	{
		build_certreqs(this, message);
		return NEED_MORE;
	}
	
	build_certs(this, message);
	
	return SUCCESS;
}

/**
 * Implementation of task_t.process for initiator
 */
static status_t process_i(private_ike_cert_t *this, message_t *message)
{
	if (message->get_exchange_type(message) == IKE_SA_INIT)
	{
		process_certreqs(this, message);
		return NEED_MORE;
	}
	
	process_certs(this, message);
	return SUCCESS;
}

/**
 * Implementation of task_t.get_type
 */
static task_type_t get_type(private_ike_cert_t *this)
{
	return IKE_CERT;
}

/**
 * Implementation of task_t.migrate
 */
static void migrate(private_ike_cert_t *this, ike_sa_t *ike_sa)
{
	this->ike_sa = ike_sa;
	
	this->cas->destroy_function(this->cas, free);
	this->cas = linked_list_create();
	this->certreq_seen = FALSE;
}

/**
 * Implementation of task_t.destroy
 */
static void destroy(private_ike_cert_t *this)
{
	this->cas->destroy_function(this->cas, free);
	free(this);
}

/*
 * Described in header.
 */
ike_cert_t *ike_cert_create(ike_sa_t *ike_sa, bool initiator)
{
	private_ike_cert_t *this = malloc_thing(private_ike_cert_t);

	this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
	this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
	this->public.task.destroy = (void(*)(task_t*))destroy;
	
	if (initiator)
	{
		this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
		this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
	}
	else
	{
		this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
		this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
	}
	
	this->ike_sa = ike_sa;
	this->initiator = initiator;
	this->cas = linked_list_create();
	this->certreq_seen = FALSE;
	
	return &this->public;
}