implement basic listing of attribute certificates
[strongswan.git] / src / charon / plugins / stroke / stroke_list.c
1 /*
2 * Copyright (C) 2008 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 "stroke_list.h"
19
20 #include <daemon.h>
21 #include <utils/linked_list.h>
22 #include <credentials/certificates/x509.h>
23 #include <credentials/certificates/crl.h>
24
25 /* warning intervals for list functions */
26 #define CERT_WARNING_INTERVAL 30 /* days */
27 #define CRL_WARNING_INTERVAL 7 /* days */
28 #define AC_WARNING_INTERVAL 1 /* day */
29
30 typedef struct private_stroke_list_t private_stroke_list_t;
31
32 /**
33 * private data of stroke_list
34 */
35 struct private_stroke_list_t {
36
37 /**
38 * public functions
39 */
40 stroke_list_t public;
41
42 /**
43 * timestamp of daemon start
44 */
45 time_t uptime;
46 };
47
48 /**
49 * log an IKE_SA to out
50 */
51 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
52 {
53 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
54 u_int32_t rekey, reauth;
55
56 fprintf(out, "%12s[%d]: %N, %H[%D]...%H[%D]\n",
57 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
58 ike_sa_state_names, ike_sa->get_state(ike_sa),
59 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
60 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
61
62 if (all)
63 {
64 fprintf(out, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s",
65 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
66 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
67 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
68
69 rekey = ike_sa->get_statistic(ike_sa, STAT_REKEY_TIME);
70 reauth = ike_sa->get_statistic(ike_sa, STAT_REAUTH_TIME);
71 if (rekey)
72 {
73 fprintf(out, ", rekeying in %V", &rekey);
74 }
75 if (reauth)
76 {
77 fprintf(out, ", reauthentication in %V", &reauth);
78 }
79 if (!rekey && !reauth)
80 {
81 fprintf(out, ", rekeying disabled");
82 }
83 fprintf(out, "\n");
84 }
85 }
86
87 /**
88 * log an CHILD_SA to out
89 */
90 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
91 {
92 u_int32_t rekey, now = time(NULL);
93 u_int32_t use_in, use_out, use_fwd;
94 encryption_algorithm_t encr_alg;
95 integrity_algorithm_t int_alg;
96 size_t encr_len, int_len;
97 mode_t mode;
98
99 child_sa->get_stats(child_sa, &mode, &encr_alg, &encr_len,
100 &int_alg, &int_len, &rekey, &use_in, &use_out,
101 &use_fwd);
102
103 fprintf(out, "%12s{%d}: %N, %N",
104 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
105 child_sa_state_names, child_sa->get_state(child_sa),
106 mode_names, mode);
107
108 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
109 {
110 fprintf(out, ", %N SPIs: %.8x_i %.8x_o",
111 protocol_id_names, child_sa->get_protocol(child_sa),
112 htonl(child_sa->get_spi(child_sa, TRUE)),
113 htonl(child_sa->get_spi(child_sa, FALSE)));
114
115 if (all)
116 {
117 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
118 child_sa->get_reqid(child_sa));
119
120
121 if (child_sa->get_protocol(child_sa) == PROTO_ESP)
122 {
123 fprintf(out, "%N", encryption_algorithm_names, encr_alg);
124
125 if (encr_len)
126 {
127 fprintf(out, "-%d", encr_len);
128 }
129 if (int_alg != AUTH_UNDEFINED)
130 {
131 fprintf(out, "/");
132 }
133 }
134
135 if (int_alg != AUTH_UNDEFINED)
136 {
137 fprintf(out, "%N", integrity_algorithm_names, int_alg);
138 if (int_len)
139 {
140 fprintf(out, "-%d", int_len);
141 }
142 }
143 fprintf(out, ", rekeying ");
144
145 if (rekey)
146 {
147 fprintf(out, "in %#V", &now, &rekey);
148 }
149 else
150 {
151 fprintf(out, "disabled");
152 }
153
154 fprintf(out, ", last use: ");
155 use_in = max(use_in, use_fwd);
156 if (use_in)
157 {
158 fprintf(out, "%ds_i ", now - use_in);
159 }
160 else
161 {
162 fprintf(out, "no_i ");
163 }
164 if (use_out)
165 {
166 fprintf(out, "%ds_o ", now - use_out);
167 }
168 else
169 {
170 fprintf(out, "no_o ");
171 }
172 }
173 }
174
175 fprintf(out, "\n%12s{%d}: %#R=== %#R\n",
176 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
177 child_sa->get_traffic_selectors(child_sa, TRUE),
178 child_sa->get_traffic_selectors(child_sa, FALSE));
179 }
180
181 /**
182 * Implementation of stroke_list_t.status.
183 */
184 static void status(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out, bool all)
185 {
186 enumerator_t *enumerator, *children;
187 iterator_t *iterator;
188 host_t *host;
189 peer_cfg_t *peer_cfg;
190 ike_cfg_t *ike_cfg;
191 child_cfg_t *child_cfg;
192 ike_sa_t *ike_sa;
193 char *name = NULL;
194 bool found = FALSE;
195 time_t uptime;
196
197 name = msg->status.name;
198
199 if (all)
200 {
201 uptime = time(NULL) - this->uptime;
202 fprintf(out, "Performance:\n");
203 fprintf(out, " uptime: %V, since %#T\n", &uptime, &this->uptime, FALSE);
204 fprintf(out, " worker threads: %d idle of %d,",
205 charon->processor->get_idle_threads(charon->processor),
206 charon->processor->get_total_threads(charon->processor));
207 fprintf(out, " job queue load: %d,",
208 charon->processor->get_job_load(charon->processor));
209 fprintf(out, " scheduled events: %d\n",
210 charon->scheduler->get_job_load(charon->scheduler));
211 iterator = charon->kernel_interface->create_address_iterator(
212 charon->kernel_interface);
213 fprintf(out, "Listening IP addresses:\n");
214 while (iterator->iterate(iterator, (void**)&host))
215 {
216 fprintf(out, " %H\n", host);
217 }
218 iterator->destroy(iterator);
219
220 fprintf(out, "Connections:\n");
221 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends);
222 while (enumerator->enumerate(enumerator, (void**)&peer_cfg))
223 {
224 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
225 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
226 {
227 continue;
228 }
229
230 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
231 fprintf(out, "%12s: %H[%D]...%H[%D]\n", peer_cfg->get_name(peer_cfg),
232 ike_cfg->get_my_host(ike_cfg), peer_cfg->get_my_id(peer_cfg),
233 ike_cfg->get_other_host(ike_cfg), peer_cfg->get_other_id(peer_cfg));
234 /* TODO: list CAs and groups */
235 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
236 while (children->enumerate(children, &child_cfg))
237 {
238 linked_list_t *my_ts, *other_ts;
239 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
240 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
241 fprintf(out, "%12s: %#R=== %#R\n", child_cfg->get_name(child_cfg),
242 my_ts, other_ts);
243 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
244 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
245 }
246 children->destroy(children);
247 }
248 enumerator->destroy(enumerator);
249 }
250
251 fprintf(out, "Security Associations:\n");
252 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
253 while (enumerator->enumerate(enumerator, &ike_sa))
254 {
255 bool ike_printed = FALSE;
256 child_sa_t *child_sa;
257 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
258
259 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
260 {
261 log_ike_sa(out, ike_sa, all);
262 found = TRUE;
263 ike_printed = TRUE;
264 }
265
266 while (children->iterate(children, (void**)&child_sa))
267 {
268 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
269 {
270 if (!ike_printed)
271 {
272 log_ike_sa(out, ike_sa, all);
273 found = TRUE;
274 ike_printed = TRUE;
275 }
276 log_child_sa(out, child_sa, all);
277 }
278 }
279 children->destroy(children);
280 }
281 enumerator->destroy(enumerator);
282
283 if (!found)
284 {
285 if (name)
286 {
287 fprintf(out, " no match\n");
288 }
289 else
290 {
291 fprintf(out, " none\n");
292 }
293 }
294 }
295
296 /**
297 * create a unique certificate list without duplicates
298 * certicates having the same issuer are grouped together.
299 */
300 static linked_list_t* create_unique_cert_list(certificate_type_t type)
301 {
302 linked_list_t *list = linked_list_create();
303 enumerator_t *enumerator = charon->credentials->create_cert_enumerator(
304 charon->credentials, type, KEY_ANY,
305 NULL, FALSE);
306 certificate_t *cert;
307
308 while (enumerator->enumerate(enumerator, (void**)&cert))
309 {
310 iterator_t *iterator = list->create_iterator(list, TRUE);
311 identification_t *issuer = cert->get_issuer(cert);
312 bool previous_same, same = FALSE, last = TRUE;
313 certificate_t *list_cert;
314
315 while (iterator->iterate(iterator, (void**)&list_cert))
316 {
317 /* exit if we have a duplicate? */
318 if (list_cert->equals(list_cert, cert))
319 {
320 last = FALSE;
321 break;
322 }
323 /* group certificates with same issuer */
324 previous_same = same;
325 same = list_cert->has_issuer(list_cert, issuer);
326 if (previous_same && !same)
327 {
328 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
329 last = FALSE;
330 break;
331 }
332 }
333 iterator->destroy(iterator);
334
335 if (last)
336 {
337 list->insert_last(list, (void *)cert->get_ref(cert));
338 }
339 }
340 enumerator->destroy(enumerator);
341 return list;
342 }
343
344 /**
345 * list all X.509 certificates matching the flags
346 */
347 static void stroke_list_certs(linked_list_t *list, char *label,
348 x509_flag_t flags, bool utc, FILE *out)
349 {
350 bool first = TRUE;
351 time_t now = time(NULL);
352 enumerator_t *enumerator = list->create_enumerator(list);
353 certificate_t *cert;
354
355 while (enumerator->enumerate(enumerator, (void**)&cert))
356 {
357 x509_t *x509 = (x509_t*)cert;
358 x509_flag_t x509_flags = x509->get_flags(x509);
359
360 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
361 if ((x509_flags & flags) || (flags == (x509_flags & ~X509_SELF_SIGNED)))
362 {
363 enumerator_t *enumerator;
364 identification_t *altName;
365 bool first_altName = TRUE;
366 chunk_t serial = x509->get_serial(x509);
367 identification_t *authkey = x509->get_authKeyIdentifier(x509);
368 time_t notBefore, notAfter;
369 public_key_t *public = cert->get_public_key(cert);
370
371 if (first)
372 {
373 fprintf(out, "\n");
374 fprintf(out, "List of %s:\n", label);
375 first = FALSE;
376 }
377 fprintf(out, "\n");
378
379 /* list subjectAltNames */
380 enumerator = x509->create_subjectAltName_enumerator(x509);
381 while (enumerator->enumerate(enumerator, (void**)&altName))
382 {
383 if (first_altName)
384 {
385 fprintf(out, " altNames: ");
386 first_altName = FALSE;
387 }
388 else
389 {
390 fprintf(out, ", ");
391 }
392 fprintf(out, "%D", altName);
393 }
394 if (!first_altName)
395 {
396 fprintf(out, "\n");
397 }
398 enumerator->destroy(enumerator);
399
400 fprintf(out, " subject: \"%D\"\n", cert->get_subject(cert));
401 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
402 fprintf(out, " serial: %#B\n", &serial);
403
404 /* list validity */
405 cert->get_validity(cert, &now, &notBefore, &notAfter);
406 fprintf(out, " validity: not before %#T, ", &notBefore, utc);
407 if (now < notBefore)
408 {
409 fprintf(out, "not valid yet (valid in %#V)\n", &now, &notBefore);
410 }
411 else
412 {
413 fprintf(out, "ok\n");
414 }
415 fprintf(out, " not after %#T, ", &notAfter, utc);
416 if (now > notAfter)
417 {
418 fprintf(out, "expired (%#V ago)\n", &now, &notAfter);
419 }
420 else
421 {
422 fprintf(out, "ok");
423 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
424 {
425 fprintf(out, " (expires in %#V)", &now, &notAfter);
426 }
427 fprintf(out, " \n");
428 }
429
430 /* list public key information */
431 if (public)
432 {
433 private_key_t *private = NULL;
434 identification_t *id, *keyid;
435
436 id = public->get_id(public, ID_PUBKEY_SHA1);
437 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
438
439 private = charon->credentials->get_private(
440 charon->credentials,
441 public->get_type(public), keyid, NULL);
442 fprintf(out, " pubkey: %N %d bits%s\n",
443 key_type_names, public->get_type(public),
444 public->get_keysize(public) * 8,
445 private ? ", has private key" : "");
446 fprintf(out, " keyid: %D\n", keyid);
447 fprintf(out, " subjkey: %D\n", id);
448 DESTROY_IF(private);
449 public->destroy(public);
450 }
451
452 /* list optional authorityKeyIdentifier */
453 if (authkey)
454 {
455 fprintf(out, " authkey: %D\n", authkey);
456 }
457 }
458 }
459 enumerator->destroy(enumerator);
460 }
461
462 /**
463 * list all X.509 attribute certificates
464 */
465 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
466 {
467 bool first = TRUE;
468 time_t thisUpdate, nextUpdate, now = time(NULL);
469 enumerator_t *enumerator = list->create_enumerator(list);
470 certificate_t *cert;
471
472 while (enumerator->enumerate(enumerator, (void**)&cert))
473 {
474 if (first)
475 {
476 fprintf(out, "\n");
477 fprintf(out, "List of X.509 Attribute Certificates:\n");
478 first = FALSE;
479 }
480 fprintf(out, "\n");
481
482 fprintf(out, " holder: \"%D\"\n", cert->get_subject(cert));
483 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
484
485 /* list validity */
486 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
487 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
488 fprintf(out, " next %#T, ", &nextUpdate, utc);
489 if (now > nextUpdate)
490 {
491 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
492 }
493 else
494 {
495 fprintf(out, "ok");
496 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
497 {
498 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
499 }
500 fprintf(out, " \n");
501 }
502 }
503 enumerator->destroy(enumerator);
504 }
505
506 /**
507 * list all X.509 CRLs
508 */
509 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
510 {
511 bool first = TRUE;
512 time_t thisUpdate, nextUpdate, now = time(NULL);
513 enumerator_t *enumerator = list->create_enumerator(list);
514 certificate_t *cert;
515
516 while (enumerator->enumerate(enumerator, (void**)&cert))
517 {
518 crl_t *crl = (crl_t*)cert;
519 chunk_t serial = crl->get_serial(crl);
520 identification_t *authkey = crl->get_authKeyIdentifier(crl);
521
522 if (first)
523 {
524 fprintf(out, "\n");
525 fprintf(out, "List of X.509 CRLs:\n");
526 first = FALSE;
527 }
528 fprintf(out, "\n");
529
530 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
531
532 /* list optional crlNumber */
533 if (serial.ptr)
534 {
535 fprintf(out, " serial: %#B\n", &serial);
536 }
537
538 /* count the number of revoked certificates */
539 {
540 int count = 0;
541 enumerator_t *enumerator = crl->create_enumerator(crl);
542
543 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
544 {
545 count++;
546 }
547 fprintf(out, " revoked: %d certificate%s\n", count,
548 (count == 1)? "" : "s");
549 enumerator->destroy(enumerator);
550 }
551
552 /* list validity */
553 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
554 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
555 fprintf(out, " next %#T, ", &nextUpdate, utc);
556 if (now > nextUpdate)
557 {
558 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
559 }
560 else
561 {
562 fprintf(out, "ok");
563 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
564 {
565 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
566 }
567 fprintf(out, " \n");
568 }
569
570 /* list optional authorityKeyIdentifier */
571 if (authkey)
572 {
573 fprintf(out, " authkey: %D\n", authkey);
574 }
575 }
576 enumerator->destroy(enumerator);
577 }
578
579 /**
580 * list all OCSP responses
581 */
582 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
583 {
584 bool first = TRUE;
585 enumerator_t *enumerator = list->create_enumerator(list);
586 certificate_t *cert;
587
588 while (enumerator->enumerate(enumerator, (void**)&cert))
589 {
590 if (first)
591 {
592 fprintf(out, "\n");
593 fprintf(out, "List of OCSP responses:\n");
594 fprintf(out, "\n");
595 first = FALSE;
596 }
597
598 fprintf(out, " signer: \"%D\"\n", cert->get_issuer(cert));
599 }
600 enumerator->destroy(enumerator);
601 }
602
603 /**
604 * Implementation of stroke_list_t.list.
605 */
606 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
607 {
608 linked_list_t *cert_list = NULL;
609
610 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
611 {
612 cert_list = create_unique_cert_list(CERT_X509);
613 }
614 if (msg->list.flags & LIST_CERTS)
615 {
616 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
617 0, msg->list.utc, out);
618 }
619 if (msg->list.flags & LIST_CACERTS)
620 {
621 stroke_list_certs(cert_list, "X.509 CA Certificates",
622 X509_CA, msg->list.utc, out);
623 }
624 if (msg->list.flags & LIST_OCSPCERTS)
625 {
626 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
627 X509_OCSP_SIGNER, msg->list.utc, out);
628 }
629 if (msg->list.flags & LIST_AACERTS)
630 {
631 stroke_list_certs(cert_list, "X.509 AA Certificates",
632 X509_AA, msg->list.utc, out);
633 }
634 if (msg->list.flags & LIST_ACERTS)
635 {
636 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
637
638 stroke_list_acerts(ac_list, msg->list.utc, out);
639 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
640 }
641 if (msg->list.flags & LIST_CRLS)
642 {
643 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
644
645 stroke_list_crls(crl_list, msg->list.utc, out);
646 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
647 }
648 if (msg->list.flags & LIST_OCSP)
649 {
650 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
651
652 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
653 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
654 }
655 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
656 }
657
658 /**
659 * Implementation of stroke_list_t.destroy
660 */
661 static void destroy(private_stroke_list_t *this)
662 {
663 free(this);
664 }
665
666 /*
667 * see header file
668 */
669 stroke_list_t *stroke_list_create()
670 {
671 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
672
673 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
674 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
675 this->public.destroy = (void(*)(stroke_list_t*))destroy;
676
677 this->uptime = time(NULL);
678
679 return &this->public;
680 }
681