including peers certificate when his certreq is empty
[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 connection_t *connection;
175 policy_t *policy;
176 identification_t *ca;
177 certreq_payload_t *certreq;
178
179 connection = this->ike_sa->get_connection(this->ike_sa);
180
181 if (connection->get_certreq_policy(connection) != CERT_NEVER_SEND)
182 {
183 policy = this->ike_sa->get_policy(this->ike_sa);
184
185 if (policy)
186 {
187 ca = policy->get_other_ca(policy);
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 policy_t *policy;
216 connection_t *connection;
217 x509_t *cert;
218 cert_payload_t *payload;
219
220 policy = this->ike_sa->get_policy(this->ike_sa);
221 connection = this->ike_sa->get_connection(this->ike_sa);
222
223 if (policy && policy->get_auth_method(policy) == AUTH_RSA)
224 {
225 switch (connection->get_cert_policy(connection))
226 {
227 case CERT_NEVER_SEND:
228 break;
229 case CERT_SEND_IF_ASKED:
230 if (!this->certreq_seen)
231 {
232 break;
233 }
234 /* FALL */
235 case CERT_ALWAYS_SEND:
236 {
237 /* TODO: respect CA cert request */
238 cert = charon->credentials->get_certificate(charon->credentials,
239 policy->get_my_id(policy));
240 if (cert)
241 {
242 payload = cert_payload_create_from_x509(cert);
243 message->add_payload(message, (payload_t*)payload);
244 }
245 }
246 }
247 }
248 }
249
250 /**
251 * Implementation of task_t.process for initiator
252 */
253 static status_t build_i(private_ike_cert_t *this, message_t *message)
254 {
255 if (message->get_exchange_type(message) == IKE_SA_INIT)
256 {
257 return NEED_MORE;
258 }
259
260 build_certreqs(this, message);
261 build_certs(this, message);
262
263 return NEED_MORE;
264 }
265
266 /**
267 * Implementation of task_t.process for responder
268 */
269 static status_t process_r(private_ike_cert_t *this, message_t *message)
270 {
271 if (message->get_exchange_type(message) == IKE_SA_INIT)
272 {
273 return NEED_MORE;
274 }
275
276 process_certreqs(this, message);
277 process_certs(this, message);
278
279 return NEED_MORE;
280 }
281
282 /**
283 * Implementation of task_t.build for responder
284 */
285 static status_t build_r(private_ike_cert_t *this, message_t *message)
286 {
287 if (message->get_exchange_type(message) == IKE_SA_INIT)
288 {
289 build_certreqs(this, message);
290 return NEED_MORE;
291 }
292
293 build_certs(this, message);
294
295 return SUCCESS;
296 }
297
298 /**
299 * Implementation of task_t.process for initiator
300 */
301 static status_t process_i(private_ike_cert_t *this, message_t *message)
302 {
303 if (message->get_exchange_type(message) == IKE_SA_INIT)
304 {
305 process_certreqs(this, message);
306 return NEED_MORE;
307 }
308
309 process_certs(this, message);
310 return SUCCESS;
311 }
312
313 /**
314 * Implementation of task_t.get_type
315 */
316 static task_type_t get_type(private_ike_cert_t *this)
317 {
318 return IKE_CERT;
319 }
320
321 /**
322 * Implementation of task_t.migrate
323 */
324 static void migrate(private_ike_cert_t *this, ike_sa_t *ike_sa)
325 {
326 this->ike_sa = ike_sa;
327
328 this->cas->destroy_function(this->cas, free);
329 this->cas = linked_list_create();
330 this->certreq_seen = FALSE;
331 }
332
333 /**
334 * Implementation of task_t.destroy
335 */
336 static void destroy(private_ike_cert_t *this)
337 {
338 this->cas->destroy_function(this->cas, free);
339 free(this);
340 }
341
342 /*
343 * Described in header.
344 */
345 ike_cert_t *ike_cert_create(ike_sa_t *ike_sa, bool initiator)
346 {
347 private_ike_cert_t *this = malloc_thing(private_ike_cert_t);
348
349 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
350 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
351 this->public.task.destroy = (void(*)(task_t*))destroy;
352
353 if (initiator)
354 {
355 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
356 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
357 }
358 else
359 {
360 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
361 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
362 }
363
364 this->ike_sa = ike_sa;
365 this->initiator = initiator;
366 this->cas = linked_list_create();
367 this->certreq_seen = FALSE;
368
369 return &this->public;
370 }