initiator sends contents of rightca= if present as a certificate request without...
[strongswan.git] / src / charon / sa / tasks / ike_cert_pre.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2006-2007 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include "ike_cert_pre.h"
20
21 #include <daemon.h>
22 #include <sa/ike_sa.h>
23 #include <encoding/payloads/cert_payload.h>
24 #include <encoding/payloads/certreq_payload.h>
25 #include <credentials/certificates/x509.h>
26
27
28 typedef struct private_ike_cert_pre_t private_ike_cert_pre_t;
29
30 /**
31 * Private members of a ike_cert_pre_t task.
32 */
33 struct private_ike_cert_pre_t {
34
35 /**
36 * Public methods and task_t interface.
37 */
38 ike_cert_pre_t public;
39
40 /**
41 * Assigned IKE_SA.
42 */
43 ike_sa_t *ike_sa;
44
45 /**
46 * Are we the initiator?
47 */
48 bool initiator;
49
50 /**
51 * Did we send a HTTP_CERT_LOOKUP_SUPPORTED Notify?
52 */
53 bool http_cert_lookup_supported_sent;
54 };
55
56 /**
57 * read certificate requests
58 */
59 static void process_certreqs(private_ike_cert_pre_t *this, message_t *message)
60 {
61 iterator_t *iterator;
62 payload_t *payload;
63 auth_info_t *auth;
64 bool ca_found = FALSE;
65
66 auth = this->ike_sa->get_my_auth(this->ike_sa);
67
68 iterator = message->get_payload_iterator(message);
69 while (iterator->iterate(iterator, (void**)&payload))
70 {
71 switch(payload->get_type(payload))
72 {
73 case CERTIFICATE_REQUEST:
74 {
75 certreq_payload_t *certreq = (certreq_payload_t*)payload;
76 chunk_t keyid;
77 enumerator_t *enumerator;
78
79 this->ike_sa->set_condition(this->ike_sa, COND_CERTREQ_SEEN, TRUE);
80
81 if (certreq->get_cert_type(certreq) != CERT_X509)
82 {
83 DBG1(DBG_IKE, "cert payload %N not supported - ignored",
84 certificate_type_names, certreq->get_cert_type(certreq));
85 break;
86 }
87 enumerator = certreq->create_keyid_enumerator(certreq);
88 while (enumerator->enumerate(enumerator, &keyid))
89 {
90 identification_t *id;
91 certificate_t *cert;
92
93 id = identification_create_from_encoding(
94 ID_PUBKEY_INFO_SHA1, keyid);
95 cert = charon->credentials->get_cert(charon->credentials,
96 CERT_X509, KEY_ANY, id, TRUE);
97 if (cert)
98 {
99 DBG1(DBG_IKE, "received cert request for \"%D\"",
100 cert->get_subject(cert));
101 auth->add_item(auth, AUTHN_CA_CERT, cert);
102 cert->destroy(cert);
103 ca_found = TRUE;
104 }
105 else
106 {
107 DBG1(DBG_IKE, "received cert request for unknown ca "
108 "with keyid %D", id);
109 auth->add_item(auth, AUTHN_CA_CERT_KEYID, id);
110 }
111 id->destroy(id);
112 }
113 enumerator->destroy(enumerator);
114 break;
115 }
116 case NOTIFY:
117 {
118 notify_payload_t *notify = (notify_payload_t*)payload;
119
120 /* we only handle one type of notify here */
121 if (notify->get_notify_type(notify) == HTTP_CERT_LOOKUP_SUPPORTED)
122 {
123 this->ike_sa->enable_extension(this->ike_sa, EXT_HASH_AND_URL);
124 }
125 break;
126 }
127 default:
128 /* ignore other payloads here, these are handled elsewhere */
129 break;
130 }
131 }
132 iterator->destroy(iterator);
133 }
134
135 /**
136 * tries to extract a certificate from the cert payload or the credential
137 * manager (based on the hash of a "Hash and URL" encoded cert).
138 * Note: the returned certificate (if any) has to be destroyed
139 */
140 static certificate_t *try_get_cert(cert_payload_t *cert_payload)
141 {
142 certificate_t *cert = NULL;
143 switch (cert_payload->get_cert_encoding(cert_payload))
144 {
145 case ENC_X509_SIGNATURE:
146 {
147 cert = cert_payload->get_cert(cert_payload);
148 break;
149 }
150 case ENC_X509_HASH_AND_URL:
151 {
152 identification_t *id;
153 chunk_t hash = cert_payload->get_hash(cert_payload);
154 if (!hash.ptr)
155 {
156 /* invalid "Hash and URL" data (logged elsewhere) */
157 break;
158 }
159 id = identification_create_from_encoding(ID_CERT_DER_SHA1, hash);
160 cert = charon->credentials->get_cert(charon->credentials,
161 CERT_X509, KEY_ANY, id, FALSE);
162 id->destroy(id);
163 break;
164 }
165 default:
166 {
167 break;
168 }
169 }
170 return cert;
171 }
172
173 /**
174 * import certificates
175 */
176 static void process_certs(private_ike_cert_pre_t *this, message_t *message)
177 {
178 iterator_t *iterator;
179 payload_t *payload;
180 auth_info_t *auth;
181 bool first = TRUE;
182
183 auth = this->ike_sa->get_other_auth(this->ike_sa);
184
185 iterator = message->get_payload_iterator(message);
186 while (iterator->iterate(iterator, (void**)&payload))
187 {
188 if (payload->get_type(payload) == CERTIFICATE)
189 {
190 cert_payload_t *cert_payload = (cert_payload_t*)payload;
191 cert_encoding_t type = cert_payload->get_cert_encoding(cert_payload);
192 switch (type)
193 {
194 case ENC_X509_SIGNATURE:
195 case ENC_X509_HASH_AND_URL:
196 {
197 if (type == ENC_X509_HASH_AND_URL &&
198 !this->http_cert_lookup_supported_sent)
199 {
200 DBG1(DBG_IKE, "received hash-and-url encoded cert, but"
201 " we don't accept them, ignore");
202 break;
203 }
204
205 certificate_t *cert = try_get_cert(cert_payload);
206
207 if (cert)
208 {
209 /* we've got a certificate from the payload or the cache */
210 if (first)
211 { /* the first certificate MUST be an end entity one */
212 DBG1(DBG_IKE, "received end entity cert \"%D\"",
213 cert->get_subject(cert));
214 auth->add_item(auth, AUTHN_SUBJECT_CERT, cert);
215 first = FALSE;
216 }
217 else
218 {
219 DBG1(DBG_IKE, "received issuer cert \"%D\"",
220 cert->get_subject(cert));
221 auth->add_item(auth, AUTHN_IM_CERT, cert);
222 }
223 cert->destroy(cert);
224 }
225 else if (type == ENC_X509_HASH_AND_URL)
226 {
227 /* we received a "Hash and URL" encoded certificate that
228 * we haven't fetched yet, we store the URL and fetch
229 * it later */
230 char *url = cert_payload->get_url(cert_payload);
231 if (!url)
232 {
233 DBG1(DBG_IKE, "received invalid hash-and-url encoded"
234 " cert, ignore");
235 break;
236 }
237
238 if (first)
239 { /* the first certificate MUST be an end entity one */
240 DBG1(DBG_IKE, "received hash-and-url for end"
241 " entity cert \"%s\"", url);
242 auth->add_item(auth, AUTHN_SUBJECT_HASH_URL, url);
243 first = FALSE;
244 }
245 else
246 {
247 DBG1(DBG_IKE, "received hash-and-url for issuer"
248 " cert \"%s\"", url);
249 auth->add_item(auth, AUTHN_IM_HASH_URL, url);
250 }
251 }
252 break;
253 }
254 case ENC_PKCS7_WRAPPED_X509:
255 case ENC_PGP:
256 case ENC_DNS_SIGNED_KEY:
257 case ENC_KERBEROS_TOKEN:
258 case ENC_CRL:
259 case ENC_ARL:
260 case ENC_SPKI:
261 case ENC_X509_ATTRIBUTE:
262 case ENC_RAW_RSA_KEY:
263 case ENC_X509_HASH_AND_URL_BUNDLE:
264 case ENC_OCSP_CONTENT:
265 default:
266 DBG1(DBG_ENC, "certificate encoding %N not supported",
267 cert_encoding_names, cert_payload->get_cert_encoding(cert_payload));
268 }
269 }
270 }
271 iterator->destroy(iterator);
272 }
273
274 /**
275 * add a certificate request to the message, building request payload if required.
276 */
277 static void add_certreq_payload(message_t *message, certreq_payload_t **reqp,
278 certificate_t *cert)
279 {
280 public_key_t *public;
281 certreq_payload_t *req;
282
283 public = cert->get_public_key(cert);
284 if (!public)
285 {
286 return;
287 }
288 switch (cert->get_type(cert))
289 {
290 case CERT_X509:
291 {
292 identification_t *keyid;
293 x509_t *x509 = (x509_t*)cert;
294
295 if (!(x509->get_flags(x509) & X509_CA))
296 { /* no CA cert, skip */
297 break;
298 }
299 if (*reqp == NULL)
300 {
301 *reqp = certreq_payload_create_type(CERT_X509);
302 message->add_payload(message, (payload_t*)*reqp);
303 }
304 req = *reqp;
305 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
306 req->add_keyid(req, keyid->get_encoding(keyid));
307 DBG1(DBG_IKE, "sending cert request for \"%D\"",
308 cert->get_subject(cert));
309 break;
310 }
311 default:
312 break;
313 }
314 public->destroy(public);
315 }
316
317 /**
318 * build certificate requests
319 */
320 static void build_certreqs(private_ike_cert_pre_t *this, message_t *message)
321 {
322 ike_cfg_t *ike_cfg;
323 peer_cfg_t *peer_cfg;
324 enumerator_t *enumerator;
325 certificate_t *cert;
326 bool restricted = FALSE;
327 certreq_payload_t *x509_req = NULL;
328
329 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
330 if (!ike_cfg->send_certreq(ike_cfg))
331 {
332 return;
333 }
334
335 /* check if we require a specific CA for that peer */
336 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
337 if (peer_cfg)
338 {
339 auth_item_t item;
340 auth_info_t *auth = peer_cfg->get_auth(peer_cfg);
341
342 enumerator = auth->create_item_enumerator(auth);
343 while (enumerator->enumerate(enumerator, &item, &cert))
344 {
345 if (item == AUTHZ_CA_CERT)
346 {
347 restricted = TRUE;
348 add_certreq_payload(message, &x509_req, cert);
349 }
350 /* TODO: handle AUTHZ_CA_CERT_NAME case */
351 }
352 enumerator->destroy(enumerator);
353 }
354
355 if (!restricted)
356 {
357 /* otherwise include all trusted CA certificates */
358 enumerator = charon->credentials->create_cert_enumerator(
359 charon->credentials, CERT_ANY, KEY_ANY, NULL, TRUE);
360 while (enumerator->enumerate(enumerator, &cert, TRUE))
361 {
362 add_certreq_payload(message, &x509_req, cert);
363 }
364 enumerator->destroy(enumerator);
365 }
366
367 /* if we've added at least one certreq, we notify our peer that we support
368 * "Hash and URL" for the requested certificates */
369 if (lib->settings->get_bool(lib->settings, "charon.hash_and_url", FALSE) &&
370 message->get_payload(message, CERTIFICATE_REQUEST))
371 {
372 message->add_notify(message, FALSE, HTTP_CERT_LOOKUP_SUPPORTED, chunk_empty);
373 this->http_cert_lookup_supported_sent = TRUE;
374 }
375 }
376
377 /**
378 * Implementation of task_t.process for initiator
379 */
380 static status_t build_i(private_ike_cert_pre_t *this, message_t *message)
381 {
382 if (message->get_exchange_type(message) == IKE_SA_INIT)
383 {
384 return NEED_MORE;
385 }
386 build_certreqs(this, message);
387 return NEED_MORE;
388 }
389
390 /**
391 * Implementation of task_t.process for responder
392 */
393 static status_t process_r(private_ike_cert_pre_t *this, message_t *message)
394 {
395 if (message->get_exchange_type(message) == IKE_SA_INIT)
396 {
397 return NEED_MORE;
398 }
399 process_certreqs(this, message);
400 process_certs(this, message);
401 return NEED_MORE;
402 }
403
404 /**
405 * Implementation of task_t.build for responder
406 */
407 static status_t build_r(private_ike_cert_pre_t *this, message_t *message)
408 {
409 if (message->get_exchange_type(message) == IKE_SA_INIT)
410 {
411 build_certreqs(this, message);
412 return NEED_MORE;
413 }
414 return SUCCESS;
415 }
416
417 /**
418 * Implementation of task_t.process for initiator
419 */
420 static status_t process_i(private_ike_cert_pre_t *this, message_t *message)
421 {
422 if (message->get_exchange_type(message) == IKE_SA_INIT)
423 {
424 process_certreqs(this, message);
425 return NEED_MORE;
426 }
427 process_certs(this, message);
428 return SUCCESS;
429 }
430
431 /**
432 * Implementation of task_t.get_type
433 */
434 static task_type_t get_type(private_ike_cert_pre_t *this)
435 {
436 return IKE_CERT_PRE;
437 }
438
439 /**
440 * Implementation of task_t.migrate
441 */
442 static void migrate(private_ike_cert_pre_t *this, ike_sa_t *ike_sa)
443 {
444 this->ike_sa = ike_sa;
445 }
446
447 /**
448 * Implementation of task_t.destroy
449 */
450 static void destroy(private_ike_cert_pre_t *this)
451 {
452 free(this);
453 }
454
455 /*
456 * Described in header.
457 */
458 ike_cert_pre_t *ike_cert_pre_create(ike_sa_t *ike_sa, bool initiator)
459 {
460 private_ike_cert_pre_t *this = malloc_thing(private_ike_cert_pre_t);
461
462 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
463 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
464 this->public.task.destroy = (void(*)(task_t*))destroy;
465
466 if (initiator)
467 {
468 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
469 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
470 }
471 else
472 {
473 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
474 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
475 }
476
477 this->ike_sa = ike_sa;
478 this->initiator = initiator;
479 this->http_cert_lookup_supported_sent = FALSE;
480
481 return &this->public;
482 }