updated TODO
[strongswan.git] / src / charon / sa / tasks / ike_cert_pre.c
1 /*
2 * Copyright (C) 2006-2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include "ike_cert_pre.h"
19
20 #include <daemon.h>
21 #include <sa/ike_sa.h>
22 #include <encoding/payloads/cert_payload.h>
23 #include <encoding/payloads/certreq_payload.h>
24 #include <credentials/certificates/x509.h>
25
26
27 typedef struct private_ike_cert_pre_t private_ike_cert_pre_t;
28
29 /**
30 * Private members of a ike_cert_pre_t task.
31 */
32 struct private_ike_cert_pre_t {
33
34 /**
35 * Public methods and task_t interface.
36 */
37 ike_cert_pre_t public;
38
39 /**
40 * Assigned IKE_SA.
41 */
42 ike_sa_t *ike_sa;
43
44 /**
45 * Are we the initiator?
46 */
47 bool initiator;
48 };
49
50 /**
51 * read certificate requests
52 */
53 static void process_certreqs(private_ike_cert_pre_t *this, message_t *message)
54 {
55 iterator_t *iterator;
56 payload_t *payload;
57 auth_info_t *auth;
58 bool ca_found = FALSE;
59
60 auth = this->ike_sa->get_my_auth(this->ike_sa);
61
62 iterator = message->get_payload_iterator(message);
63 while (iterator->iterate(iterator, (void**)&payload))
64 {
65 if (payload->get_type(payload) == CERTIFICATE_REQUEST)
66 {
67 certreq_payload_t *certreq = (certreq_payload_t*)payload;
68 chunk_t keyid;
69 enumerator_t *enumerator;
70
71 this->ike_sa->set_condition(this->ike_sa, COND_CERTREQ_SEEN, TRUE);
72
73 if (certreq->get_cert_type(certreq) != CERT_X509)
74 {
75 DBG1(DBG_IKE, "cert payload %N not supported - ignored",
76 certificate_type_names, certreq->get_cert_type(certreq));
77 continue;
78 }
79 enumerator = certreq->create_keyid_enumerator(certreq);
80 while (enumerator->enumerate(enumerator, &keyid))
81 {
82 identification_t *id;
83 certificate_t *cert;
84
85 id = identification_create_from_encoding(
86 ID_PUBKEY_INFO_SHA1, keyid);
87 cert = charon->credentials->get_cert(charon->credentials,
88 CERT_X509, KEY_ANY, id, TRUE);
89 if (cert)
90 {
91 DBG1(DBG_IKE, "received cert request for \"%D\"",
92 cert->get_subject(cert));
93 auth->add_item(auth, AUTHN_CA_CERT, cert);
94 cert->destroy(cert);
95 ca_found = TRUE;
96 }
97 else
98 {
99 DBG1(DBG_IKE, "received cert request for unknown ca "
100 "\"%D\"", id);
101 auth->add_item(auth, AUTHN_CA_CERT_KEYID, id);
102 }
103 id->destroy(id);
104 }
105 enumerator->destroy(enumerator);
106 }
107 }
108 iterator->destroy(iterator);
109 }
110
111 /**
112 * import certificates
113 */
114 static void process_certs(private_ike_cert_pre_t *this, message_t *message)
115 {
116 iterator_t *iterator;
117 payload_t *payload;
118 auth_info_t *auth;
119 bool first = TRUE;
120
121 auth = this->ike_sa->get_other_auth(this->ike_sa);
122
123 iterator = message->get_payload_iterator(message);
124 while (iterator->iterate(iterator, (void**)&payload))
125 {
126 if (payload->get_type(payload) == CERTIFICATE)
127 {
128 certificate_t *cert;
129 cert_payload_t *cert_payload = (cert_payload_t*)payload;
130
131 cert = cert_payload->get_cert(cert_payload);
132 if (cert)
133 {
134 if (first)
135 { /* the first certificate MUST be an end entity one */
136
137 DBG1(DBG_IKE, "received end entity cert \"%D\"",
138 cert->get_subject(cert));
139 auth->add_item(auth, AUTHN_SUBJECT_CERT, cert);
140 first = FALSE;
141 }
142 else
143 {
144 DBG1(DBG_IKE, "received issuer cert \"%D\"",
145 cert->get_subject(cert));
146 auth->add_item(auth, AUTHN_IM_CERT, cert);
147 }
148 }
149 cert->destroy(cert);
150 }
151 }
152 iterator->destroy(iterator);
153 }
154
155 /**
156 * add a certificate request to the message, building request payload if required.
157 */
158 static void add_certreq_payload(message_t *message, certreq_payload_t **reqp,
159 certificate_t *cert)
160 {
161 public_key_t *public;
162 certreq_payload_t *req;
163
164 public = cert->get_public_key(cert);
165 if (!public)
166 {
167 return;
168 }
169 switch (cert->get_type(cert))
170 {
171 case CERT_X509:
172 {
173 identification_t *keyid;
174 x509_t *x509 = (x509_t*)cert;
175
176 if (!(x509->get_flags(x509) & X509_CA))
177 { /* no CA cert, skip */
178 break;
179 }
180 if (*reqp == NULL)
181 {
182 *reqp = certreq_payload_create_type(CERT_X509);
183 message->add_payload(message, (payload_t*)*reqp);
184 }
185 req = *reqp;
186 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
187 req->add_keyid(req, keyid->get_encoding(keyid));
188 DBG1(DBG_IKE, "sending cert request for \"%D\"",
189 cert->get_subject(cert));
190 break;
191 }
192 default:
193 break;
194 }
195 public->destroy(public);
196 }
197
198 /**
199 * build certificate requests
200 */
201 static void build_certreqs(private_ike_cert_pre_t *this, message_t *message)
202 {
203 ike_cfg_t *ike_cfg;
204 enumerator_t *enumerator;
205 certificate_t *cert;
206 auth_info_t *auth;
207 bool restricted = FALSE;
208 auth_item_t item;
209 certreq_payload_t *x509_req = NULL;
210
211 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
212 if (ike_cfg->send_certreq(ike_cfg) == CERT_NEVER_SEND)
213 {
214 return;
215 }
216 auth = this->ike_sa->get_other_auth(this->ike_sa);
217
218 /* check if we require a specific CA for that peer */
219 enumerator = auth->create_item_enumerator(auth);
220 while (enumerator->enumerate(enumerator, &item, &cert))
221 {
222 if (item == AUTHN_CA_CERT)
223 {
224 restricted = TRUE;
225 add_certreq_payload(message, &x509_req, cert);
226 }
227 }
228 enumerator->destroy(enumerator);
229
230 if (!restricted)
231 {
232 /* otherwise include all trusted CA certificates */
233 enumerator = charon->credentials->create_cert_enumerator(
234 charon->credentials, CERT_ANY, KEY_ANY, NULL, TRUE);
235 while (enumerator->enumerate(enumerator, &cert, TRUE))
236 {
237 add_certreq_payload(message, &x509_req, cert);
238 }
239 enumerator->destroy(enumerator);
240 }
241 }
242
243 /**
244 * Implementation of task_t.process for initiator
245 */
246 static status_t build_i(private_ike_cert_pre_t *this, message_t *message)
247 {
248 if (message->get_exchange_type(message) == IKE_SA_INIT)
249 {
250 return NEED_MORE;
251 }
252 build_certreqs(this, message);
253 return NEED_MORE;
254 }
255
256 /**
257 * Implementation of task_t.process for responder
258 */
259 static status_t process_r(private_ike_cert_pre_t *this, message_t *message)
260 {
261 if (message->get_exchange_type(message) == IKE_SA_INIT)
262 {
263 return NEED_MORE;
264 }
265 process_certreqs(this, message);
266 process_certs(this, message);
267 return NEED_MORE;
268 }
269
270 /**
271 * Implementation of task_t.build for responder
272 */
273 static status_t build_r(private_ike_cert_pre_t *this, message_t *message)
274 {
275 if (message->get_exchange_type(message) == IKE_SA_INIT)
276 {
277 build_certreqs(this, message);
278 return NEED_MORE;
279 }
280 return SUCCESS;
281 }
282
283 /**
284 * Implementation of task_t.process for initiator
285 */
286 static status_t process_i(private_ike_cert_pre_t *this, message_t *message)
287 {
288 if (message->get_exchange_type(message) == IKE_SA_INIT)
289 {
290 process_certreqs(this, message);
291 return NEED_MORE;
292 }
293 process_certs(this, message);
294 return SUCCESS;
295 }
296
297 /**
298 * Implementation of task_t.get_type
299 */
300 static task_type_t get_type(private_ike_cert_pre_t *this)
301 {
302 return IKE_CERT_PRE;
303 }
304
305 /**
306 * Implementation of task_t.migrate
307 */
308 static void migrate(private_ike_cert_pre_t *this, ike_sa_t *ike_sa)
309 {
310 this->ike_sa = ike_sa;
311 }
312
313 /**
314 * Implementation of task_t.destroy
315 */
316 static void destroy(private_ike_cert_pre_t *this)
317 {
318 free(this);
319 }
320
321 /*
322 * Described in header.
323 */
324 ike_cert_pre_t *ike_cert_pre_create(ike_sa_t *ike_sa, bool initiator)
325 {
326 private_ike_cert_pre_t *this = malloc_thing(private_ike_cert_pre_t);
327
328 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
329 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
330 this->public.task.destroy = (void(*)(task_t*))destroy;
331
332 if (initiator)
333 {
334 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
335 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
336 }
337 else
338 {
339 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
340 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
341 }
342
343 this->ike_sa = ike_sa;
344 this->initiator = initiator;
345
346 return &this->public;
347 }