properly end CERT_PRE task after detecting the final authentication round
[strongswan.git] / src / charon / sa / tasks / ike_cert_pre.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2006-2009 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
17 #include "ike_cert_pre.h"
18
19 #include <daemon.h>
20 #include <sa/ike_sa.h>
21 #include <encoding/payloads/cert_payload.h>
22 #include <encoding/payloads/certreq_payload.h>
23 #include <credentials/certificates/x509.h>
24
25
26 typedef struct private_ike_cert_pre_t private_ike_cert_pre_t;
27
28 /**
29 * Private members of a ike_cert_pre_t task.
30 */
31 struct private_ike_cert_pre_t {
32
33 /**
34 * Public methods and task_t interface.
35 */
36 ike_cert_pre_t public;
37
38 /**
39 * Assigned IKE_SA.
40 */
41 ike_sa_t *ike_sa;
42
43 /**
44 * Are we the initiator?
45 */
46 bool initiator;
47
48 /**
49 * Do we accept HTTP certificate lookup requests
50 */
51 bool do_http_lookup;
52
53 /**
54 * wheter this is the final authentication round
55 */
56 bool final;
57 };
58
59 /**
60 * read certificate requests
61 */
62 static void process_certreqs(private_ike_cert_pre_t *this, message_t *message)
63 {
64 enumerator_t *enumerator;
65 payload_t *payload;
66 auth_cfg_t *auth;
67
68 auth = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
69
70 enumerator = message->create_payload_enumerator(message);
71 while (enumerator->enumerate(enumerator, &payload))
72 {
73 switch(payload->get_type(payload))
74 {
75 case CERTIFICATE_REQUEST:
76 {
77 certreq_payload_t *certreq = (certreq_payload_t*)payload;
78 enumerator_t *enumerator;
79 chunk_t keyid;
80
81 this->ike_sa->set_condition(this->ike_sa, COND_CERTREQ_SEEN, TRUE);
82
83 if (certreq->get_cert_type(certreq) != CERT_X509)
84 {
85 DBG1(DBG_IKE, "cert payload %N not supported - ignored",
86 certificate_type_names, certreq->get_cert_type(certreq));
87 break;
88 }
89 enumerator = certreq->create_keyid_enumerator(certreq);
90 while (enumerator->enumerate(enumerator, &keyid))
91 {
92 identification_t *id;
93 certificate_t *cert;
94
95 id = identification_create_from_encoding(
96 ID_PUBKEY_INFO_SHA1, keyid);
97 cert = charon->credentials->get_cert(charon->credentials,
98 CERT_X509, KEY_ANY, id, TRUE);
99 if (cert)
100 {
101 DBG1(DBG_IKE, "received cert request for \"%Y\"",
102 cert->get_subject(cert));
103 auth->add(auth, AUTH_RULE_CA_CERT, cert);
104 }
105 else
106 {
107 DBG1(DBG_IKE, "received cert request for unknown ca "
108 "with keyid %Y", id);
109 }
110 id->destroy(id);
111 }
112 enumerator->destroy(enumerator);
113 break;
114 }
115 case NOTIFY:
116 {
117 notify_payload_t *notify = (notify_payload_t*)payload;
118
119 /* we only handle one type of notify here */
120 if (notify->get_notify_type(notify) == HTTP_CERT_LOOKUP_SUPPORTED)
121 {
122 this->ike_sa->enable_extension(this->ike_sa, EXT_HASH_AND_URL);
123 }
124 break;
125 }
126 default:
127 /* ignore other payloads here, these are handled elsewhere */
128 break;
129 }
130 }
131 enumerator->destroy(enumerator);
132 }
133
134 /**
135 * tries to extract a certificate from the cert payload or the credential
136 * manager (based on the hash of a "Hash and URL" encoded cert).
137 * Note: the returned certificate (if any) has to be destroyed
138 */
139 static certificate_t *try_get_cert(cert_payload_t *cert_payload)
140 {
141 certificate_t *cert = NULL;
142
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 enumerator_t *enumerator;
179 payload_t *payload;
180 auth_cfg_t *auth;
181 bool first = TRUE;
182
183 auth = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);
184
185 enumerator = message->create_payload_enumerator(message);
186 while (enumerator->enumerate(enumerator, &payload))
187 {
188 if (payload->get_type(payload) == CERTIFICATE)
189 {
190 cert_payload_t *cert_payload;
191 cert_encoding_t encoding;
192 certificate_t *cert;
193 char *url;
194
195 cert_payload = (cert_payload_t*)payload;
196 encoding = cert_payload->get_cert_encoding(cert_payload);
197
198 switch (encoding)
199 {
200 case ENC_X509_HASH_AND_URL:
201 {
202 if (!this->do_http_lookup)
203 {
204 DBG1(DBG_IKE, "received hash-and-url encoded cert, but"
205 " we don't accept them, ignore");
206 break;
207 }
208 /* FALL */
209 }
210 case ENC_X509_SIGNATURE:
211 {
212 cert = try_get_cert(cert_payload);
213 if (cert)
214 {
215 if (first)
216 { /* the first is an end entity certificate */
217 DBG1(DBG_IKE, "received end entity cert \"%Y\"",
218 cert->get_subject(cert));
219 auth->add(auth, AUTH_HELPER_SUBJECT_CERT, cert);
220 first = FALSE;
221 }
222 else
223 {
224 DBG1(DBG_IKE, "received issuer cert \"%Y\"",
225 cert->get_subject(cert));
226 auth->add(auth, AUTH_HELPER_IM_CERT, cert);
227 }
228 }
229 else if (encoding == ENC_X509_HASH_AND_URL)
230 {
231 /* we fetch the certificate not yet, but only if
232 * it is really needed during authentication */
233 url = cert_payload->get_url(cert_payload);
234 if (!url)
235 {
236 DBG1(DBG_IKE, "received invalid hash-and-url "
237 "encoded cert, ignore");
238 break;
239 }
240 url = strdup(url);
241 if (first)
242 { /* first URL is for an end entity certificate */
243 DBG1(DBG_IKE, "received hash-and-url for end"
244 " entity cert \"%s\"", url);
245 auth->add(auth, AUTH_HELPER_SUBJECT_HASH_URL, url);
246 first = FALSE;
247 }
248 else
249 {
250 DBG1(DBG_IKE, "received hash-and-url for issuer"
251 " cert \"%s\"", url);
252 auth->add(auth, AUTH_HELPER_IM_HASH_URL, url);
253 }
254 }
255 break;
256 }
257 case ENC_PKCS7_WRAPPED_X509:
258 case ENC_PGP:
259 case ENC_DNS_SIGNED_KEY:
260 case ENC_KERBEROS_TOKEN:
261 case ENC_CRL:
262 case ENC_ARL:
263 case ENC_SPKI:
264 case ENC_X509_ATTRIBUTE:
265 case ENC_RAW_RSA_KEY:
266 case ENC_X509_HASH_AND_URL_BUNDLE:
267 case ENC_OCSP_CONTENT:
268 default:
269 DBG1(DBG_ENC, "certificate encoding %N not supported",
270 cert_encoding_names, encoding);
271 }
272 }
273 }
274 enumerator->destroy(enumerator);
275 }
276
277 /**
278 * add the keyid of a certificate to the certificate request payload
279 */
280 static void add_certreq(certreq_payload_t **req, certificate_t *cert)
281 {
282 switch (cert->get_type(cert))
283 {
284 case CERT_X509:
285 {
286 public_key_t *public;
287 identification_t *keyid;
288 x509_t *x509 = (x509_t*)cert;
289
290 if (!(x509->get_flags(x509) & X509_CA))
291 { /* no CA cert, skip */
292 break;
293 }
294 public = cert->get_public_key(cert);
295 if (!public)
296 {
297 break;
298 }
299 if (*req == NULL)
300 {
301 *req = certreq_payload_create_type(CERT_X509);
302 }
303 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
304 (*req)->add_keyid(*req, keyid->get_encoding(keyid));
305 public->destroy(public);
306 DBG1(DBG_IKE, "sending cert request for \"%Y\"",
307 cert->get_subject(cert));
308 break;
309 }
310 default:
311 break;
312 }
313 }
314
315 /**
316 * add a auth_cfg's CA certificates to the certificate request
317 */
318 static void add_certreqs(certreq_payload_t **req, auth_cfg_t *auth)
319 {
320 enumerator_t *enumerator;
321 auth_rule_t type;
322 void *value;
323
324 enumerator = auth->create_enumerator(auth);
325 while (enumerator->enumerate(enumerator, &type, &value))
326 {
327 switch (type)
328 {
329 case AUTH_RULE_CA_CERT:
330 add_certreq(req, (certificate_t*)value);
331 break;
332 default:
333 break;
334 }
335 }
336 enumerator->destroy(enumerator);
337 }
338
339 /**
340 * build certificate requests
341 */
342 static void build_certreqs(private_ike_cert_pre_t *this, message_t *message)
343 {
344 enumerator_t *enumerator;
345 ike_cfg_t *ike_cfg;
346 peer_cfg_t *peer_cfg;
347 certificate_t *cert;
348 auth_cfg_t *auth;
349 certreq_payload_t *req = NULL;
350
351 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
352 if (!ike_cfg->send_certreq(ike_cfg))
353 {
354 return;
355 }
356
357 /* check if we require a specific CA for that peer */
358 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
359 if (peer_cfg)
360 {
361 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, FALSE);
362 while (enumerator->enumerate(enumerator, &auth))
363 {
364 add_certreqs(&req, auth);
365 }
366 enumerator->destroy(enumerator);
367 }
368
369 if (!req)
370 {
371 /* otherwise add all trusted CA certificates */
372 enumerator = charon->credentials->create_cert_enumerator(
373 charon->credentials, CERT_ANY, KEY_ANY, NULL, TRUE);
374 while (enumerator->enumerate(enumerator, &cert))
375 {
376 add_certreq(&req, cert);
377 }
378 enumerator->destroy(enumerator);
379 }
380
381 if (req)
382 {
383 message->add_payload(message, (payload_t*)req);
384
385 if (lib->settings->get_bool(lib->settings, "charon.hash_and_url", FALSE))
386 {
387 message->add_notify(message, FALSE, HTTP_CERT_LOOKUP_SUPPORTED,
388 chunk_empty);
389 this->do_http_lookup = TRUE;
390 }
391 }
392 }
393
394 /**
395 * Check if this is the final authentication round
396 */
397 static bool final_auth(message_t *message)
398 {
399 enumerator_t *enumerator;
400 payload_t *payload;
401 notify_payload_t *notify;
402
403 /* we check for an AUTH payload without a ANOTHER_AUTH_FOLLOWS notify */
404 if (message->get_payload(message, AUTHENTICATION) == NULL)
405 {
406 return FALSE;
407 }
408 enumerator = message->create_payload_enumerator(message);
409 while (enumerator->enumerate(enumerator, &payload))
410 {
411 if (payload->get_type(payload) == NOTIFY)
412 {
413 notify = (notify_payload_t*)payload;
414 if (notify->get_notify_type(notify) == ANOTHER_AUTH_FOLLOWS)
415 {
416 enumerator->destroy(enumerator);
417 return FALSE;
418 }
419 }
420 }
421 enumerator->destroy(enumerator);
422 return TRUE;
423 }
424
425 /**
426 * Implementation of task_t.process for initiator
427 */
428 static status_t build_i(private_ike_cert_pre_t *this, message_t *message)
429 {
430 if (message->get_message_id(message) == 1)
431 { /* initiator sends CERTREQs in first IKE_AUTH */
432 build_certreqs(this, message);
433 }
434 return NEED_MORE;
435 }
436
437 /**
438 * Implementation of task_t.process for responder
439 */
440 static status_t process_r(private_ike_cert_pre_t *this, message_t *message)
441 {
442 if (message->get_exchange_type(message) != IKE_SA_INIT)
443 { /* handle certreqs/certs in any IKE_AUTH, just in case */
444 process_certreqs(this, message);
445 process_certs(this, message);
446 }
447 this->final = final_auth(message);
448 return NEED_MORE;
449 }
450
451 /**
452 * Implementation of task_t.build for responder
453 */
454 static status_t build_r(private_ike_cert_pre_t *this, message_t *message)
455 {
456 if (message->get_exchange_type(message) == IKE_SA_INIT)
457 {
458 build_certreqs(this, message);
459 }
460 if (this->final)
461 {
462 return SUCCESS;
463 }
464 return NEED_MORE;
465 }
466
467 /**
468 * Implementation of task_t.process for initiator
469 */
470 static status_t process_i(private_ike_cert_pre_t *this, message_t *message)
471 {
472 if (message->get_exchange_type(message) == IKE_SA_INIT)
473 {
474 process_certreqs(this, message);
475 }
476 process_certs(this, message);
477
478 if (final_auth(message))
479 {
480 return SUCCESS;
481 }
482 return NEED_MORE;
483 }
484
485 /**
486 * Implementation of task_t.get_type
487 */
488 static task_type_t get_type(private_ike_cert_pre_t *this)
489 {
490 return IKE_CERT_PRE;
491 }
492
493 /**
494 * Implementation of task_t.migrate
495 */
496 static void migrate(private_ike_cert_pre_t *this, ike_sa_t *ike_sa)
497 {
498 this->ike_sa = ike_sa;
499 }
500
501 /**
502 * Implementation of task_t.destroy
503 */
504 static void destroy(private_ike_cert_pre_t *this)
505 {
506 free(this);
507 }
508
509 /*
510 * Described in header.
511 */
512 ike_cert_pre_t *ike_cert_pre_create(ike_sa_t *ike_sa, bool initiator)
513 {
514 private_ike_cert_pre_t *this = malloc_thing(private_ike_cert_pre_t);
515
516 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
517 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
518 this->public.task.destroy = (void(*)(task_t*))destroy;
519
520 if (initiator)
521 {
522 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
523 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
524 }
525 else
526 {
527 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
528 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
529 }
530
531 this->ike_sa = ike_sa;
532 this->initiator = initiator;
533 this->do_http_lookup = FALSE;
534 this->final = FALSE;
535
536 return &this->public;
537 }