working dummy for NetworkManager DBUS interface
[strongswan.git] / src / charon / sa / tasks / ike_cert.c
1 /**
2 * @file ike_cert.c
3 *
4 * @brief Implementation of the ike_cert task.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006-2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "ike_cert.h"
24
25 #include <daemon.h>
26 #include <sa/ike_sa.h>
27 #include <crypto/hashers/hasher.h>
28 #include <encoding/payloads/cert_payload.h>
29 #include <encoding/payloads/certreq_payload.h>
30
31
32 typedef struct private_ike_cert_t private_ike_cert_t;
33
34 /**
35 * Private members of a ike_cert_t task.
36 */
37 struct private_ike_cert_t {
38
39 /**
40 * Public methods and task_t interface.
41 */
42 ike_cert_t public;
43
44 /**
45 * Assigned IKE_SA.
46 */
47 ike_sa_t *ike_sa;
48
49 /**
50 * Are we the initiator?
51 */
52 bool initiator;
53
54 /**
55 * list of CA cert hashes requested, items point to 20 byte chunk
56 */
57 linked_list_t *cas;
58
59 /**
60 * have we seen a certificate request?
61 */
62 bool certreq_seen;
63 };
64
65 /**
66 * read certificate requests
67 */
68 static void process_certreqs(private_ike_cert_t *this, message_t *message)
69 {
70 iterator_t *iterator;
71 payload_t *payload;
72
73 iterator = message->get_payload_iterator(message);
74 while (iterator->iterate(iterator, (void**)&payload))
75 {
76 if (payload->get_type(payload) == CERTIFICATE_REQUEST)
77 {
78 certreq_payload_t *certreq = (certreq_payload_t*)payload;
79 cert_encoding_t encoding;
80 chunk_t keyids, keyid;
81
82 this->certreq_seen = TRUE;
83
84 encoding = certreq->get_cert_encoding(certreq);
85 if (encoding != CERT_X509_SIGNATURE)
86 {
87 DBG1(DBG_IKE, "certreq payload %N not supported, ignored",
88 cert_encoding_names, encoding);
89 continue;
90 }
91
92 keyids = certreq->get_data(certreq);
93
94 while (keyids.len >= HASH_SIZE_SHA1)
95 {
96 keyid = chunk_create(keyids.ptr, HASH_SIZE_SHA1);
97 keyid = chunk_clone(keyid);
98 this->cas->insert_last(this->cas, keyid.ptr);
99 keyids = chunk_skip(keyids, HASH_SIZE_SHA1);
100 }
101 }
102 }
103 iterator->destroy(iterator);
104 }
105
106 /**
107 * import certificates
108 */
109 static void process_certs(private_ike_cert_t *this, message_t *message)
110 {
111 iterator_t *iterator;
112 payload_t *payload;
113
114 iterator = message->get_payload_iterator(message);
115 while (iterator->iterate(iterator, (void**)&payload))
116 {
117 if (payload->get_type(payload) == CERTIFICATE)
118 {
119 cert_encoding_t encoding;
120 x509_t *cert;
121 chunk_t cert_data;
122 bool found;
123 cert_payload_t *cert_payload = (cert_payload_t*)payload;
124
125 encoding = cert_payload->get_cert_encoding(cert_payload);
126 if (encoding != CERT_X509_SIGNATURE)
127 {
128 DBG1(DBG_IKE, "certificate payload %N not supported, ignored",
129 cert_encoding_names, encoding);
130 continue;
131 }
132
133 cert_data = cert_payload->get_data_clone(cert_payload);
134 cert = x509_create_from_chunk(cert_data, 0);
135 if (cert)
136 {
137 if (charon->credentials->verify(charon->credentials,
138 cert, &found))
139 {
140 DBG2(DBG_IKE, "received end entity certificate is trusted, "
141 "added to store");
142 if (!found)
143 {
144 charon->credentials->add_end_certificate(
145 charon->credentials, cert);
146 }
147 else
148 {
149 cert->destroy(cert);
150 }
151 }
152 else
153 {
154 DBG1(DBG_IKE, "received end entity certificate is not "
155 "trusted, discarded");
156 cert->destroy(cert);
157 }
158 }
159 else
160 {
161 DBG1(DBG_IKE, "parsing of received certificate failed, discarded");
162 chunk_free(&cert_data);
163 }
164 }
165 }
166 iterator->destroy(iterator);
167 }
168
169 /**
170 * build certificate requests
171 */
172 static void build_certreqs(private_ike_cert_t *this, message_t *message)
173 {
174 ike_cfg_t *ike_cfg;
175 peer_cfg_t *peer_cfg;
176 identification_t *ca;
177 certreq_payload_t *certreq;
178
179 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
180
181 if (ike_cfg->send_certreq(ike_cfg) != CERT_NEVER_SEND)
182 {
183 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
184
185 if (peer_cfg)
186 {
187 ca = peer_cfg->get_other_ca(peer_cfg);
188
189 if (ca && ca->get_type(ca) != ID_ANY)
190 {
191 certreq = certreq_payload_create_from_cacert(ca);
192 }
193 else
194 {
195 certreq = certreq_payload_create_from_cacerts();
196 }
197 }
198 else
199 {
200 certreq = certreq_payload_create_from_cacerts();
201 }
202
203 if (certreq)
204 {
205 message->add_payload(message, (payload_t*)certreq);
206 }
207 }
208 }
209
210 /**
211 * add certificates to message
212 */
213 static void build_certs(private_ike_cert_t *this, message_t *message)
214 {
215 peer_cfg_t *peer_cfg;
216 x509_t *cert;
217 cert_payload_t *payload;
218
219 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
220
221 if (peer_cfg && peer_cfg->get_auth_method(peer_cfg) == AUTH_RSA)
222 {
223 switch (peer_cfg->get_cert_policy(peer_cfg))
224 {
225 case CERT_NEVER_SEND:
226 break;
227 case CERT_SEND_IF_ASKED:
228 if (!this->certreq_seen)
229 {
230 break;
231 }
232 /* FALL */
233 case CERT_ALWAYS_SEND:
234 {
235 /* TODO: respect CA cert request */
236 cert = charon->credentials->get_certificate(charon->credentials,
237 peer_cfg->get_my_id(peer_cfg));
238 if (cert)
239 {
240 payload = cert_payload_create_from_x509(cert);
241 message->add_payload(message, (payload_t*)payload);
242 }
243 }
244 }
245 }
246 }
247
248 /**
249 * Implementation of task_t.process for initiator
250 */
251 static status_t build_i(private_ike_cert_t *this, message_t *message)
252 {
253 if (message->get_exchange_type(message) == IKE_SA_INIT)
254 {
255 return NEED_MORE;
256 }
257
258 build_certreqs(this, message);
259 build_certs(this, message);
260
261 return NEED_MORE;
262 }
263
264 /**
265 * Implementation of task_t.process for responder
266 */
267 static status_t process_r(private_ike_cert_t *this, message_t *message)
268 {
269 if (message->get_exchange_type(message) == IKE_SA_INIT)
270 {
271 return NEED_MORE;
272 }
273
274 process_certreqs(this, message);
275 process_certs(this, message);
276
277 return NEED_MORE;
278 }
279
280 /**
281 * Implementation of task_t.build for responder
282 */
283 static status_t build_r(private_ike_cert_t *this, message_t *message)
284 {
285 if (message->get_exchange_type(message) == IKE_SA_INIT)
286 {
287 build_certreqs(this, message);
288 return NEED_MORE;
289 }
290
291 build_certs(this, message);
292
293 return SUCCESS;
294 }
295
296 /**
297 * Implementation of task_t.process for initiator
298 */
299 static status_t process_i(private_ike_cert_t *this, message_t *message)
300 {
301 if (message->get_exchange_type(message) == IKE_SA_INIT)
302 {
303 process_certreqs(this, message);
304 return NEED_MORE;
305 }
306
307 process_certs(this, message);
308 return SUCCESS;
309 }
310
311 /**
312 * Implementation of task_t.get_type
313 */
314 static task_type_t get_type(private_ike_cert_t *this)
315 {
316 return IKE_CERT;
317 }
318
319 /**
320 * Implementation of task_t.migrate
321 */
322 static void migrate(private_ike_cert_t *this, ike_sa_t *ike_sa)
323 {
324 this->ike_sa = ike_sa;
325
326 this->cas->destroy_function(this->cas, free);
327 this->cas = linked_list_create();
328 this->certreq_seen = FALSE;
329 }
330
331 /**
332 * Implementation of task_t.destroy
333 */
334 static void destroy(private_ike_cert_t *this)
335 {
336 this->cas->destroy_function(this->cas, free);
337 free(this);
338 }
339
340 /*
341 * Described in header.
342 */
343 ike_cert_t *ike_cert_create(ike_sa_t *ike_sa, bool initiator)
344 {
345 private_ike_cert_t *this = malloc_thing(private_ike_cert_t);
346
347 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
348 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
349 this->public.task.destroy = (void(*)(task_t*))destroy;
350
351 if (initiator)
352 {
353 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
354 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
355 }
356 else
357 {
358 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
359 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
360 }
361
362 this->ike_sa = ike_sa;
363 this->initiator = initiator;
364 this->cas = linked_list_create();
365 this->certreq_seen = FALSE;
366
367 return &this->public;
368 }