stroke parses and lists AC groups
[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/ac.h>
24 #include <credentials/certificates/crl.h>
25 #include <config/peer_cfg.h>
26
27 /* warning intervals for list functions */
28 #define CERT_WARNING_INTERVAL 30 /* days */
29 #define CRL_WARNING_INTERVAL 7 /* days */
30 #define AC_WARNING_INTERVAL 1 /* day */
31
32 typedef struct private_stroke_list_t private_stroke_list_t;
33
34 /**
35 * private data of stroke_list
36 */
37 struct private_stroke_list_t {
38
39 /**
40 * public functions
41 */
42 stroke_list_t public;
43
44 /**
45 * timestamp of daemon start
46 */
47 time_t uptime;
48 };
49
50 /**
51 * get the authentication class of a config
52 */
53 auth_class_t get_auth_class(peer_cfg_t *config)
54 {
55 auth_class_t *class;
56 auth_info_t *auth_info;
57
58 auth_info = config->get_auth(config);
59 if (auth_info->get_item(auth_info, AUTHN_AUTH_CLASS, (void**)&class))
60 {
61 return *class;
62 }
63 /* fallback to pubkey authentication */
64 return AUTH_CLASS_PUBKEY;
65 }
66
67 /**
68 * log an IKE_SA to out
69 */
70 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
71 {
72 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
73
74 fprintf(out, "%12s[%d]: %N, %H[%D]...%H[%D]\n",
75 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
76 ike_sa_state_names, ike_sa->get_state(ike_sa),
77 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
78 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
79
80 if (all)
81 {
82 char *ike_proposal = ike_sa->get_proposal(ike_sa);
83
84 fprintf(out, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s",
85 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
86 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
87 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
88
89
90 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
91 {
92 u_int32_t rekey = ike_sa->get_statistic(ike_sa, STAT_REKEY_TIME);
93 u_int32_t reauth = ike_sa->get_statistic(ike_sa, STAT_REAUTH_TIME);
94
95 if (rekey)
96 {
97 fprintf(out, ", rekeying in %V", &rekey);
98 }
99 if (reauth)
100 {
101 fprintf(out, ", %N reauthentication in %V", auth_class_names,
102 get_auth_class(ike_sa->get_peer_cfg(ike_sa)), &reauth);
103 }
104 if (!rekey && !reauth)
105 {
106 fprintf(out, ", rekeying disabled");
107 }
108 }
109 fprintf(out, "\n");
110
111 if (ike_proposal)
112 {
113 fprintf(out, "%12s[%d]: IKE proposal: %s\n",
114 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
115 ike_proposal);
116 }
117 }
118 }
119
120 /**
121 * log an CHILD_SA to out
122 */
123 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
124 {
125 u_int32_t rekey, now = time(NULL);
126 u_int32_t use_in, use_out, use_fwd;
127 encryption_algorithm_t encr_alg;
128 integrity_algorithm_t int_alg;
129 size_t encr_len, int_len;
130 mode_t mode;
131
132 child_sa->get_stats(child_sa, &mode, &encr_alg, &encr_len,
133 &int_alg, &int_len, &rekey, &use_in, &use_out,
134 &use_fwd);
135
136 fprintf(out, "%12s{%d}: %N, %N",
137 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
138 child_sa_state_names, child_sa->get_state(child_sa),
139 mode_names, mode);
140
141 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
142 {
143 u_int16_t my_cpi = child_sa->get_cpi(child_sa, TRUE);
144 u_int16_t other_cpi = child_sa->get_cpi(child_sa, FALSE);
145
146 fprintf(out, ", %N SPIs: %.8x_i %.8x_o",
147 protocol_id_names, child_sa->get_protocol(child_sa),
148 ntohl(child_sa->get_spi(child_sa, TRUE)),
149 ntohl(child_sa->get_spi(child_sa, FALSE)));
150
151 /* Is IPCOMP activated ? */
152 if (my_cpi && other_cpi)
153 {
154 fprintf(out, ", IPCOMP CPIs: %.4x_i %.4x_o",
155 ntohs(my_cpi), ntohs(other_cpi));
156 }
157
158 if (all)
159 {
160 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
161 child_sa->get_reqid(child_sa));
162
163
164 if (child_sa->get_protocol(child_sa) == PROTO_ESP)
165 {
166 fprintf(out, "%N", encryption_algorithm_names, encr_alg);
167
168 if (encr_len)
169 {
170 fprintf(out, "-%d", encr_len);
171 }
172 if (int_alg != AUTH_UNDEFINED)
173 {
174 fprintf(out, "/");
175 }
176 }
177
178 if (int_alg != AUTH_UNDEFINED)
179 {
180 fprintf(out, "%N", integrity_algorithm_names, int_alg);
181 if (int_len)
182 {
183 fprintf(out, "-%d", int_len);
184 }
185 }
186 fprintf(out, ", rekeying ");
187
188 if (rekey)
189 {
190 fprintf(out, "in %#V", &now, &rekey);
191 }
192 else
193 {
194 fprintf(out, "disabled");
195 }
196
197 fprintf(out, ", last use: ");
198 use_in = max(use_in, use_fwd);
199 if (use_in)
200 {
201 fprintf(out, "%ds_i ", now - use_in);
202 }
203 else
204 {
205 fprintf(out, "no_i ");
206 }
207 if (use_out)
208 {
209 fprintf(out, "%ds_o ", now - use_out);
210 }
211 else
212 {
213 fprintf(out, "no_o ");
214 }
215 }
216 }
217
218 fprintf(out, "\n%12s{%d}: %#R=== %#R\n",
219 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
220 child_sa->get_traffic_selectors(child_sa, TRUE),
221 child_sa->get_traffic_selectors(child_sa, FALSE));
222 }
223
224 /**
225 * Implementation of stroke_list_t.status.
226 */
227 static void status(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out, bool all)
228 {
229 enumerator_t *enumerator, *children;
230 iterator_t *iterator;
231 ike_cfg_t *ike_cfg;
232 child_cfg_t *child_cfg;
233 ike_sa_t *ike_sa;
234 bool found = FALSE;
235 char *name = msg->status.name;
236
237 if (all)
238 {
239 peer_cfg_t *peer_cfg;
240 char *plugin;
241 host_t *host;
242 u_int32_t dpd;
243 time_t uptime = time(NULL) - this->uptime;
244
245 fprintf(out, "Performance:\n");
246 fprintf(out, " uptime: %V, since %#T\n", &uptime, &this->uptime, FALSE);
247 fprintf(out, " worker threads: %d idle of %d,",
248 charon->processor->get_idle_threads(charon->processor),
249 charon->processor->get_total_threads(charon->processor));
250 fprintf(out, " job queue load: %d,",
251 charon->processor->get_job_load(charon->processor));
252 fprintf(out, " scheduled events: %d\n",
253 charon->scheduler->get_job_load(charon->scheduler));
254 fprintf(out, " loaded plugins: ");
255 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
256 while (enumerator->enumerate(enumerator, &plugin))
257 {
258 fprintf(out, "%s ", plugin);
259 }
260 enumerator->destroy(enumerator);
261 fprintf(out, "\n");
262
263 iterator = charon->kernel_interface->create_address_iterator(
264 charon->kernel_interface);
265 fprintf(out, "Listening IP addresses:\n");
266 while (iterator->iterate(iterator, (void**)&host))
267 {
268 fprintf(out, " %H\n", host);
269 }
270 iterator->destroy(iterator);
271
272 fprintf(out, "Connections:\n");
273 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends);
274 while (enumerator->enumerate(enumerator, (void**)&peer_cfg))
275 {
276 void *ptr;
277 certificate_t *cert;
278 auth_item_t item;
279 auth_info_t *auth;
280 enumerator_t *auth_enumerator;
281 identification_t *my_ca = NULL, *other_ca = NULL;
282 identification_t *eap_identity = NULL;
283 u_int32_t *eap_type = NULL;
284 bool ac_groups = FALSE;
285
286 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
287 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
288 {
289 continue;
290 }
291
292 /* determine any required CAs, EAP type, EAP identity,
293 * and the presence of AC groups
294 */
295 auth = peer_cfg->get_auth(peer_cfg);
296 auth_enumerator = auth->create_item_enumerator(auth);
297 while (auth_enumerator->enumerate(auth_enumerator, &item, &ptr))
298 {
299 switch (item)
300 {
301 case AUTHN_EAP_TYPE:
302 eap_type = (u_int32_t *)ptr;
303 break;
304 case AUTHN_EAP_IDENTITY:
305 eap_identity = (identification_t *)ptr;
306 break;
307 case AUTHN_CA_CERT:
308 cert = (certificate_t *)ptr;
309 my_ca = cert->get_subject(cert);
310 break;
311 case AUTHN_CA_CERT_NAME:
312 my_ca = (identification_t *)ptr;
313 break;
314 case AUTHZ_CA_CERT:
315 cert = (certificate_t *)ptr;
316 other_ca = cert->get_subject(cert);
317 break;
318 case AUTHZ_CA_CERT_NAME:
319 other_ca = (identification_t *)ptr;
320 break;
321 case AUTHZ_AC_GROUP:
322 ac_groups = TRUE;
323 break;
324 default:
325 break;
326 }
327 }
328 auth_enumerator->destroy(auth_enumerator);
329
330 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
331 fprintf(out, "%12s: %s[%D]...%s[%D]\n", peer_cfg->get_name(peer_cfg),
332 ike_cfg->get_my_addr(ike_cfg), peer_cfg->get_my_id(peer_cfg),
333 ike_cfg->get_other_addr(ike_cfg), peer_cfg->get_other_id(peer_cfg));
334 if (my_ca || other_ca)
335 {
336 fprintf(out, "%12s: CAs: ", peer_cfg->get_name(peer_cfg));
337 if (my_ca)
338 {
339 fprintf(out, "\"%D\"...", my_ca);
340 }
341 else
342 {
343 fprintf(out, "%%any...");
344 }
345 if (other_ca)
346 {
347 fprintf(out, "\"%D\"\n", other_ca);
348 }
349 else
350 {
351 fprintf(out, "%%any\n");
352 }
353 }
354
355 if (ac_groups)
356 {
357 bool first = TRUE;
358
359 fprintf(out, "%12s: groups: ", peer_cfg->get_name(peer_cfg));
360 auth_enumerator = auth->create_item_enumerator(auth);
361 while (auth_enumerator->enumerate(auth_enumerator, &item, &ptr))
362 {
363 if (item == AUTHZ_AC_GROUP)
364 {
365 identification_t *group = (identification_t *)ptr;
366
367 fprintf(out, "%s%D", first? "":", ", group);
368 first = FALSE;
369 }
370 }
371 auth_enumerator->destroy(auth_enumerator);
372 fprintf(out, "\n");
373 }
374
375 fprintf(out, "%12s: %N ", peer_cfg->get_name(peer_cfg),
376 auth_class_names, get_auth_class(peer_cfg));
377 if (eap_type)
378 {
379 fprintf(out, "and %N ", eap_type_names, *eap_type);
380 }
381 fprintf(out, "authentication");
382 if (eap_identity)
383 {
384 fprintf(out, ", EAP identity: '%D'", eap_identity);
385 }
386 dpd = peer_cfg->get_dpd(peer_cfg);
387 if (dpd)
388 {
389 fprintf(out, ", dpddelay=%us", dpd);
390 }
391 fprintf(out, "\n");
392
393 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
394 while (children->enumerate(children, &child_cfg))
395 {
396 linked_list_t *my_ts, *other_ts;
397
398 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
399 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
400 fprintf(out, "%12s: %#R=== %#R", child_cfg->get_name(child_cfg),
401 my_ts, other_ts);
402 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
403 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
404
405 if (dpd)
406 {
407 fprintf(out, ", dpdaction=%N", action_names,
408 child_cfg->get_dpd_action(child_cfg));
409 }
410 fprintf(out, "\n");
411 }
412 children->destroy(children);
413 }
414 enumerator->destroy(enumerator);
415 }
416
417 fprintf(out, "Security Associations:\n");
418 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
419 while (enumerator->enumerate(enumerator, &ike_sa))
420 {
421 bool ike_printed = FALSE;
422 child_sa_t *child_sa;
423 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
424
425 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
426 {
427 log_ike_sa(out, ike_sa, all);
428 found = TRUE;
429 ike_printed = TRUE;
430 }
431
432 while (children->iterate(children, (void**)&child_sa))
433 {
434 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
435 {
436 if (!ike_printed)
437 {
438 log_ike_sa(out, ike_sa, all);
439 found = TRUE;
440 ike_printed = TRUE;
441 }
442 log_child_sa(out, child_sa, all);
443 }
444 }
445 children->destroy(children);
446 }
447 enumerator->destroy(enumerator);
448
449 if (!found)
450 {
451 if (name)
452 {
453 fprintf(out, " no match\n");
454 }
455 else
456 {
457 fprintf(out, " none\n");
458 }
459 }
460 }
461
462 /**
463 * create a unique certificate list without duplicates
464 * certicates having the same issuer are grouped together.
465 */
466 static linked_list_t* create_unique_cert_list(certificate_type_t type)
467 {
468 linked_list_t *list = linked_list_create();
469 enumerator_t *enumerator = charon->credentials->create_cert_enumerator(
470 charon->credentials, type, KEY_ANY,
471 NULL, FALSE);
472 certificate_t *cert;
473
474 while (enumerator->enumerate(enumerator, (void**)&cert))
475 {
476 iterator_t *iterator = list->create_iterator(list, TRUE);
477 identification_t *issuer = cert->get_issuer(cert);
478 bool previous_same, same = FALSE, last = TRUE;
479 certificate_t *list_cert;
480
481 while (iterator->iterate(iterator, (void**)&list_cert))
482 {
483 /* exit if we have a duplicate? */
484 if (list_cert->equals(list_cert, cert))
485 {
486 last = FALSE;
487 break;
488 }
489 /* group certificates with same issuer */
490 previous_same = same;
491 same = list_cert->has_issuer(list_cert, issuer);
492 if (previous_same && !same)
493 {
494 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
495 last = FALSE;
496 break;
497 }
498 }
499 iterator->destroy(iterator);
500
501 if (last)
502 {
503 list->insert_last(list, (void *)cert->get_ref(cert));
504 }
505 }
506 enumerator->destroy(enumerator);
507 return list;
508 }
509
510 /**
511 * list all X.509 certificates matching the flags
512 */
513 static void stroke_list_certs(linked_list_t *list, char *label,
514 x509_flag_t flags, bool utc, FILE *out)
515 {
516 bool first = TRUE;
517 time_t now = time(NULL);
518 enumerator_t *enumerator = list->create_enumerator(list);
519 certificate_t *cert;
520
521 while (enumerator->enumerate(enumerator, (void**)&cert))
522 {
523 x509_t *x509 = (x509_t*)cert;
524 x509_flag_t x509_flags = x509->get_flags(x509);
525
526 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
527 if ((x509_flags & flags) || (flags == (x509_flags & ~X509_SELF_SIGNED)))
528 {
529 enumerator_t *enumerator;
530 identification_t *altName;
531 bool first_altName = TRUE;
532 chunk_t serial = x509->get_serial(x509);
533 identification_t *authkey = x509->get_authKeyIdentifier(x509);
534 time_t notBefore, notAfter;
535 public_key_t *public = cert->get_public_key(cert);
536
537 if (first)
538 {
539 fprintf(out, "\n");
540 fprintf(out, "List of %s:\n", label);
541 first = FALSE;
542 }
543 fprintf(out, "\n");
544
545 /* list subjectAltNames */
546 enumerator = x509->create_subjectAltName_enumerator(x509);
547 while (enumerator->enumerate(enumerator, (void**)&altName))
548 {
549 if (first_altName)
550 {
551 fprintf(out, " altNames: ");
552 first_altName = FALSE;
553 }
554 else
555 {
556 fprintf(out, ", ");
557 }
558 fprintf(out, "%D", altName);
559 }
560 if (!first_altName)
561 {
562 fprintf(out, "\n");
563 }
564 enumerator->destroy(enumerator);
565
566 fprintf(out, " subject: \"%D\"\n", cert->get_subject(cert));
567 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
568 fprintf(out, " serial: %#B\n", &serial);
569
570 /* list validity */
571 cert->get_validity(cert, &now, &notBefore, &notAfter);
572 fprintf(out, " validity: not before %#T, ", &notBefore, utc);
573 if (now < notBefore)
574 {
575 fprintf(out, "not valid yet (valid in %#V)\n", &now, &notBefore);
576 }
577 else
578 {
579 fprintf(out, "ok\n");
580 }
581 fprintf(out, " not after %#T, ", &notAfter, utc);
582 if (now > notAfter)
583 {
584 fprintf(out, "expired (%#V ago)\n", &now, &notAfter);
585 }
586 else
587 {
588 fprintf(out, "ok");
589 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
590 {
591 fprintf(out, " (expires in %#V)", &now, &notAfter);
592 }
593 fprintf(out, " \n");
594 }
595
596 /* list public key information */
597 if (public)
598 {
599 private_key_t *private = NULL;
600 identification_t *id, *keyid;
601
602 id = public->get_id(public, ID_PUBKEY_SHA1);
603 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
604
605 private = charon->credentials->get_private(
606 charon->credentials,
607 public->get_type(public), keyid, NULL);
608 fprintf(out, " pubkey: %N %d bits%s\n",
609 key_type_names, public->get_type(public),
610 public->get_keysize(public) * 8,
611 private ? ", has private key" : "");
612 fprintf(out, " keyid: %D\n", keyid);
613 fprintf(out, " subjkey: %D\n", id);
614 DESTROY_IF(private);
615 public->destroy(public);
616 }
617
618 /* list optional authorityKeyIdentifier */
619 if (authkey)
620 {
621 fprintf(out, " authkey: %D\n", authkey);
622 }
623 }
624 }
625 enumerator->destroy(enumerator);
626 }
627
628 /**
629 * list all X.509 attribute certificates
630 */
631 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
632 {
633 bool first = TRUE;
634 time_t thisUpdate, nextUpdate, now = time(NULL);
635 enumerator_t *enumerator = list->create_enumerator(list);
636 certificate_t *cert;
637
638 while (enumerator->enumerate(enumerator, (void**)&cert))
639 {
640 ac_t *ac = (ac_t*)cert;
641 chunk_t serial = ac->get_serial(ac);
642 chunk_t holderSerial = ac->get_holderSerial(ac);
643 identification_t *holderIssuer = ac->get_holderIssuer(ac);
644 identification_t *authkey = ac->get_authKeyIdentifier(ac);
645 identification_t *entityName = cert->get_subject(cert);
646
647 if (first)
648 {
649 fprintf(out, "\n");
650 fprintf(out, "List of X.509 Attribute Certificates:\n");
651 first = FALSE;
652 }
653 fprintf(out, "\n");
654
655 if (entityName)
656 {
657 fprintf(out, " holder: \"%D\"\n", entityName);
658 }
659 if (holderIssuer)
660 {
661 fprintf(out, " hissuer: \"%D\"\n", holderIssuer);
662 }
663 if (holderSerial.ptr)
664 {
665 fprintf(out, " hserial: %#B\n", &holderSerial);
666 }
667 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
668 fprintf(out, " serial: %#B\n", &serial);
669
670 /* list validity */
671 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
672 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
673 fprintf(out, " next %#T, ", &nextUpdate, utc);
674 if (now > nextUpdate)
675 {
676 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
677 }
678 else
679 {
680 fprintf(out, "ok");
681 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
682 {
683 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
684 }
685 fprintf(out, " \n");
686 }
687
688 /* list optional authorityKeyIdentifier */
689 if (authkey)
690 {
691 fprintf(out, " authkey: %D\n", authkey);
692 }
693 }
694 enumerator->destroy(enumerator);
695 }
696
697 /**
698 * list all X.509 CRLs
699 */
700 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
701 {
702 bool first = TRUE;
703 time_t thisUpdate, nextUpdate, now = time(NULL);
704 enumerator_t *enumerator = list->create_enumerator(list);
705 certificate_t *cert;
706
707 while (enumerator->enumerate(enumerator, (void**)&cert))
708 {
709 crl_t *crl = (crl_t*)cert;
710 chunk_t serial = crl->get_serial(crl);
711 identification_t *authkey = crl->get_authKeyIdentifier(crl);
712
713 if (first)
714 {
715 fprintf(out, "\n");
716 fprintf(out, "List of X.509 CRLs:\n");
717 first = FALSE;
718 }
719 fprintf(out, "\n");
720
721 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
722
723 /* list optional crlNumber */
724 if (serial.ptr)
725 {
726 fprintf(out, " serial: %#B\n", &serial);
727 }
728
729 /* count the number of revoked certificates */
730 {
731 int count = 0;
732 enumerator_t *enumerator = crl->create_enumerator(crl);
733
734 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
735 {
736 count++;
737 }
738 fprintf(out, " revoked: %d certificate%s\n", count,
739 (count == 1)? "" : "s");
740 enumerator->destroy(enumerator);
741 }
742
743 /* list validity */
744 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
745 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
746 fprintf(out, " next %#T, ", &nextUpdate, utc);
747 if (now > nextUpdate)
748 {
749 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
750 }
751 else
752 {
753 fprintf(out, "ok");
754 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
755 {
756 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
757 }
758 fprintf(out, " \n");
759 }
760
761 /* list optional authorityKeyIdentifier */
762 if (authkey)
763 {
764 fprintf(out, " authkey: %D\n", authkey);
765 }
766 }
767 enumerator->destroy(enumerator);
768 }
769
770 /**
771 * list all OCSP responses
772 */
773 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
774 {
775 bool first = TRUE;
776 enumerator_t *enumerator = list->create_enumerator(list);
777 certificate_t *cert;
778
779 while (enumerator->enumerate(enumerator, (void**)&cert))
780 {
781 if (first)
782 {
783 fprintf(out, "\n");
784 fprintf(out, "List of OCSP responses:\n");
785 fprintf(out, "\n");
786 first = FALSE;
787 }
788
789 fprintf(out, " signer: \"%D\"\n", cert->get_issuer(cert));
790 }
791 enumerator->destroy(enumerator);
792 }
793
794 /**
795 * List of registered cryptographical algorithms
796 */
797 static void list_algs(FILE *out)
798 {
799 enumerator_t *enumerator;
800 encryption_algorithm_t encryption;
801 integrity_algorithm_t integrity;
802 hash_algorithm_t hash;
803 pseudo_random_function_t prf;
804 diffie_hellman_group_t group;
805
806 fprintf(out, "\n");
807 fprintf(out, "List of registered IKEv2 Algorithms:\n");
808 fprintf(out, "\n encryption: ");
809 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
810 while (enumerator->enumerate(enumerator, &encryption))
811 {
812 fprintf(out, "%N ", encryption_algorithm_names, encryption);
813 }
814 enumerator->destroy(enumerator);
815 fprintf(out, "\n integrity: ");
816 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
817 while (enumerator->enumerate(enumerator, &integrity))
818 {
819 fprintf(out, "%N ", integrity_algorithm_names, integrity);
820 }
821 enumerator->destroy(enumerator);
822 fprintf(out, "\n hasher: ");
823 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
824 while (enumerator->enumerate(enumerator, &hash))
825 {
826 fprintf(out, "%N ", hash_algorithm_names, hash);
827 }
828 enumerator->destroy(enumerator);
829 fprintf(out, "\n prf: ");
830 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
831 while (enumerator->enumerate(enumerator, &prf))
832 {
833 fprintf(out, "%N ", pseudo_random_function_names, prf);
834 }
835 enumerator->destroy(enumerator);
836 fprintf(out, "\n dh-group: ");
837 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
838 while (enumerator->enumerate(enumerator, &group))
839 {
840 fprintf(out, "%N ", diffie_hellman_group_names, group);
841 }
842 enumerator->destroy(enumerator);
843 fprintf(out, "\n");
844 }
845
846 /**
847 * Implementation of stroke_list_t.list.
848 */
849 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
850 {
851 linked_list_t *cert_list = NULL;
852
853 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
854 {
855 cert_list = create_unique_cert_list(CERT_X509);
856 }
857 if (msg->list.flags & LIST_CERTS)
858 {
859 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
860 0, msg->list.utc, out);
861 }
862 if (msg->list.flags & LIST_CACERTS)
863 {
864 stroke_list_certs(cert_list, "X.509 CA Certificates",
865 X509_CA, msg->list.utc, out);
866 }
867 if (msg->list.flags & LIST_OCSPCERTS)
868 {
869 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
870 X509_OCSP_SIGNER, msg->list.utc, out);
871 }
872 if (msg->list.flags & LIST_AACERTS)
873 {
874 stroke_list_certs(cert_list, "X.509 AA Certificates",
875 X509_AA, msg->list.utc, out);
876 }
877 if (msg->list.flags & LIST_ACERTS)
878 {
879 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
880
881 stroke_list_acerts(ac_list, msg->list.utc, out);
882 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
883 }
884 if (msg->list.flags & LIST_CRLS)
885 {
886 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
887
888 stroke_list_crls(crl_list, msg->list.utc, out);
889 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
890 }
891 if (msg->list.flags & LIST_OCSP)
892 {
893 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
894
895 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
896
897 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
898 }
899 if (msg->list.flags & LIST_ALGS)
900 {
901 list_algs(out);
902 }
903 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
904 }
905
906 /**
907 * Implementation of stroke_list_t.destroy
908 */
909 static void destroy(private_stroke_list_t *this)
910 {
911 free(this);
912 }
913
914 /*
915 * see header file
916 */
917 stroke_list_t *stroke_list_create()
918 {
919 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
920
921 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
922 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
923 this->public.destroy = (void(*)(stroke_list_t*))destroy;
924
925 this->uptime = time(NULL);
926
927 return &this->public;
928 }
929