attempt to achieve consistent debugging output
[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 id->destroy(id);
98 }
99 enumerator->destroy(enumerator);
100 }
101 }
102 iterator->destroy(iterator);
103
104 if (this->ike_sa->has_condition(this->ike_sa, COND_CERTREQ_SEEN) && !ca_found)
105 {
106 DBG1(DBG_IKE, "received cert request, but no such CA cert found");
107 }
108 }
109
110 /**
111 * import certificates
112 */
113 static void process_certs(private_ike_cert_pre_t *this, message_t *message)
114 {
115 iterator_t *iterator;
116 payload_t *payload;
117 auth_info_t *auth;
118 bool first = TRUE;
119
120 auth = this->ike_sa->get_other_auth(this->ike_sa);
121
122 iterator = message->get_payload_iterator(message);
123 while (iterator->iterate(iterator, (void**)&payload))
124 {
125 if (payload->get_type(payload) == CERTIFICATE)
126 {
127 certificate_t *cert;
128 cert_payload_t *cert_payload = (cert_payload_t*)payload;
129
130 cert = cert_payload->get_cert(cert_payload);
131 if (cert)
132 {
133 if (first)
134 { /* the first certificate MUST be an end entity one */
135
136 DBG1(DBG_IKE, "received end entity cert \"%D\"",
137 cert->get_subject(cert));
138 auth->add_item(auth, AUTHN_SUBJECT_CERT, cert);
139 first = FALSE;
140 }
141 else
142 {
143 DBG1(DBG_IKE, "received issuer cert \"%D\"",
144 cert->get_subject(cert));
145 auth->add_item(auth, AUTHN_IM_CERT, cert);
146 }
147 }
148 cert->destroy(cert);
149 }
150 }
151 iterator->destroy(iterator);
152 }
153
154 /**
155 * add a certificate request to the message, building request payload if required.
156 */
157 static void add_certreq_payload(message_t *message, certreq_payload_t **reqp,
158 certificate_t *cert)
159 {
160 public_key_t *public;
161 certreq_payload_t *req;
162
163 public = cert->get_public_key(cert);
164 if (!public)
165 {
166 return;
167 }
168 switch (cert->get_type(cert))
169 {
170 case CERT_X509:
171 {
172 identification_t *keyid;
173 x509_t *x509 = (x509_t*)cert;
174
175 if (!(x509->get_flags(x509) & X509_CA))
176 { /* no CA cert, skip */
177 break;
178 }
179 if (*reqp == NULL)
180 {
181 *reqp = certreq_payload_create_type(CERT_X509);
182 message->add_payload(message, (payload_t*)*reqp);
183 }
184 req = *reqp;
185 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
186 req->add_keyid(req, keyid->get_encoding(keyid));
187 DBG1(DBG_IKE, "sending cert request for \"%D\"",
188 cert->get_subject(cert));
189 break;
190 }
191 default:
192 break;
193 }
194 public->destroy(public);
195 }
196
197 /**
198 * build certificate requests
199 */
200 static void build_certreqs(private_ike_cert_pre_t *this, message_t *message)
201 {
202 ike_cfg_t *ike_cfg;
203 enumerator_t *enumerator;
204 certificate_t *cert;
205 auth_info_t *auth;
206 bool restricted = FALSE;
207 auth_item_t item;
208 certreq_payload_t *x509_req = NULL;
209
210 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
211 if (ike_cfg->send_certreq(ike_cfg) == CERT_NEVER_SEND)
212 {
213 return;
214 }
215 auth = this->ike_sa->get_other_auth(this->ike_sa);
216
217 /* check if we require a specific CA for that peer */
218 enumerator = auth->create_item_enumerator(auth);
219 while (enumerator->enumerate(enumerator, &item, &cert))
220 {
221 if (item == AUTHN_CA_CERT)
222 {
223 restricted = TRUE;
224 add_certreq_payload(message, &x509_req, cert);
225 }
226 }
227 enumerator->destroy(enumerator);
228
229 if (!restricted)
230 {
231 /* otherwise include all trusted CA certificates */
232 enumerator = charon->credentials->create_cert_enumerator(
233 charon->credentials, CERT_ANY, KEY_ANY, NULL, TRUE);
234 while (enumerator->enumerate(enumerator, &cert, TRUE))
235 {
236 add_certreq_payload(message, &x509_req, cert);
237 }
238 enumerator->destroy(enumerator);
239 }
240 }
241
242 /**
243 * Implementation of task_t.process for initiator
244 */
245 static status_t build_i(private_ike_cert_pre_t *this, message_t *message)
246 {
247 if (message->get_exchange_type(message) == IKE_SA_INIT)
248 {
249 return NEED_MORE;
250 }
251 build_certreqs(this, message);
252 return NEED_MORE;
253 }
254
255 /**
256 * Implementation of task_t.process for responder
257 */
258 static status_t process_r(private_ike_cert_pre_t *this, message_t *message)
259 {
260 if (message->get_exchange_type(message) == IKE_SA_INIT)
261 {
262 return NEED_MORE;
263 }
264 process_certreqs(this, message);
265 process_certs(this, message);
266 return NEED_MORE;
267 }
268
269 /**
270 * Implementation of task_t.build for responder
271 */
272 static status_t build_r(private_ike_cert_pre_t *this, message_t *message)
273 {
274 if (message->get_exchange_type(message) == IKE_SA_INIT)
275 {
276 build_certreqs(this, message);
277 return NEED_MORE;
278 }
279 return SUCCESS;
280 }
281
282 /**
283 * Implementation of task_t.process for initiator
284 */
285 static status_t process_i(private_ike_cert_pre_t *this, message_t *message)
286 {
287 if (message->get_exchange_type(message) == IKE_SA_INIT)
288 {
289 process_certreqs(this, message);
290 return NEED_MORE;
291 }
292 process_certs(this, message);
293 return SUCCESS;
294 }
295
296 /**
297 * Implementation of task_t.get_type
298 */
299 static task_type_t get_type(private_ike_cert_pre_t *this)
300 {
301 return IKE_CERT_PRE;
302 }
303
304 /**
305 * Implementation of task_t.migrate
306 */
307 static void migrate(private_ike_cert_pre_t *this, ike_sa_t *ike_sa)
308 {
309 this->ike_sa = ike_sa;
310 }
311
312 /**
313 * Implementation of task_t.destroy
314 */
315 static void destroy(private_ike_cert_pre_t *this)
316 {
317 free(this);
318 }
319
320 /*
321 * Described in header.
322 */
323 ike_cert_pre_t *ike_cert_pre_create(ike_sa_t *ike_sa, bool initiator)
324 {
325 private_ike_cert_pre_t *this = malloc_thing(private_ike_cert_pre_t);
326
327 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
328 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
329 this->public.task.destroy = (void(*)(task_t*))destroy;
330
331 if (initiator)
332 {
333 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
334 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
335 }
336 else
337 {
338 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
339 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
340 }
341
342 this->ike_sa = ike_sa;
343 this->initiator = initiator;
344
345 return &this->public;
346 }