merging modularized kernel interface back to trunk
[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 ike_cfg_t *ike_cfg;
231 child_cfg_t *child_cfg;
232 ike_sa_t *ike_sa;
233 bool found = FALSE;
234 char *name = msg->status.name;
235
236 if (all)
237 {
238 peer_cfg_t *peer_cfg;
239 char *plugin;
240 host_t *host;
241 u_int32_t dpd;
242 time_t uptime = time(NULL) - this->uptime;
243
244 fprintf(out, "Performance:\n");
245 fprintf(out, " uptime: %V, since %#T\n", &uptime, &this->uptime, FALSE);
246 fprintf(out, " worker threads: %d idle of %d,",
247 charon->processor->get_idle_threads(charon->processor),
248 charon->processor->get_total_threads(charon->processor));
249 fprintf(out, " job queue load: %d,",
250 charon->processor->get_job_load(charon->processor));
251 fprintf(out, " scheduled events: %d\n",
252 charon->scheduler->get_job_load(charon->scheduler));
253 fprintf(out, " loaded plugins: ");
254 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
255 while (enumerator->enumerate(enumerator, &plugin))
256 {
257 fprintf(out, "%s ", plugin);
258 }
259 enumerator->destroy(enumerator);
260 fprintf(out, "\n");
261
262 enumerator = charon->kernel_interface->create_address_enumerator(
263 charon->kernel_interface, FALSE, FALSE);
264 fprintf(out, "Listening IP addresses:\n");
265 while (enumerator->enumerate(enumerator, (void**)&host))
266 {
267 fprintf(out, " %H\n", host);
268 }
269 enumerator->destroy(enumerator);
270
271 fprintf(out, "Connections:\n");
272 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends);
273 while (enumerator->enumerate(enumerator, (void**)&peer_cfg))
274 {
275 void *ptr;
276 certificate_t *cert;
277 auth_item_t item;
278 auth_info_t *auth;
279 enumerator_t *auth_enumerator;
280 identification_t *my_ca = NULL, *other_ca = NULL;
281 identification_t *eap_identity = NULL;
282 u_int32_t *eap_type = NULL;
283 bool ac_groups = FALSE;
284
285 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
286 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
287 {
288 continue;
289 }
290
291 /* determine any required CAs, EAP type, EAP identity,
292 * and the presence of AC groups
293 */
294 auth = peer_cfg->get_auth(peer_cfg);
295 auth_enumerator = auth->create_item_enumerator(auth);
296 while (auth_enumerator->enumerate(auth_enumerator, &item, &ptr))
297 {
298 switch (item)
299 {
300 case AUTHN_EAP_TYPE:
301 eap_type = (u_int32_t *)ptr;
302 break;
303 case AUTHN_EAP_IDENTITY:
304 eap_identity = (identification_t *)ptr;
305 break;
306 case AUTHN_CA_CERT:
307 cert = (certificate_t *)ptr;
308 my_ca = cert->get_subject(cert);
309 break;
310 case AUTHN_CA_CERT_NAME:
311 my_ca = (identification_t *)ptr;
312 break;
313 case AUTHZ_CA_CERT:
314 cert = (certificate_t *)ptr;
315 other_ca = cert->get_subject(cert);
316 break;
317 case AUTHZ_CA_CERT_NAME:
318 other_ca = (identification_t *)ptr;
319 break;
320 case AUTHZ_AC_GROUP:
321 ac_groups = TRUE;
322 break;
323 default:
324 break;
325 }
326 }
327 auth_enumerator->destroy(auth_enumerator);
328
329 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
330 fprintf(out, "%12s: %s[%D]...%s[%D]\n", peer_cfg->get_name(peer_cfg),
331 ike_cfg->get_my_addr(ike_cfg), peer_cfg->get_my_id(peer_cfg),
332 ike_cfg->get_other_addr(ike_cfg), peer_cfg->get_other_id(peer_cfg));
333 if (my_ca || other_ca)
334 {
335 fprintf(out, "%12s: CAs: ", peer_cfg->get_name(peer_cfg));
336 if (my_ca)
337 {
338 fprintf(out, "\"%D\"...", my_ca);
339 }
340 else
341 {
342 fprintf(out, "%%any...");
343 }
344 if (other_ca)
345 {
346 fprintf(out, "\"%D\"\n", other_ca);
347 }
348 else
349 {
350 fprintf(out, "%%any\n");
351 }
352 }
353
354 if (ac_groups)
355 {
356 bool first = TRUE;
357
358 fprintf(out, "%12s: groups: ", peer_cfg->get_name(peer_cfg));
359 auth_enumerator = auth->create_item_enumerator(auth);
360 while (auth_enumerator->enumerate(auth_enumerator, &item, &ptr))
361 {
362 if (item == AUTHZ_AC_GROUP)
363 {
364 identification_t *group = (identification_t *)ptr;
365
366 fprintf(out, "%s%D", first? "":", ", group);
367 first = FALSE;
368 }
369 }
370 auth_enumerator->destroy(auth_enumerator);
371 fprintf(out, "\n");
372 }
373
374 fprintf(out, "%12s: %N ", peer_cfg->get_name(peer_cfg),
375 auth_class_names, get_auth_class(peer_cfg));
376 if (eap_type)
377 {
378 fprintf(out, "and %N ", eap_type_names, *eap_type);
379 }
380 fprintf(out, "authentication");
381 if (eap_identity)
382 {
383 fprintf(out, ", EAP identity: '%D'", eap_identity);
384 }
385 dpd = peer_cfg->get_dpd(peer_cfg);
386 if (dpd)
387 {
388 fprintf(out, ", dpddelay=%us", dpd);
389 }
390 fprintf(out, "\n");
391
392 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
393 while (children->enumerate(children, &child_cfg))
394 {
395 linked_list_t *my_ts, *other_ts;
396
397 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
398 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
399 fprintf(out, "%12s: %#R=== %#R", child_cfg->get_name(child_cfg),
400 my_ts, other_ts);
401 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
402 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
403
404 if (dpd)
405 {
406 fprintf(out, ", dpdaction=%N", action_names,
407 child_cfg->get_dpd_action(child_cfg));
408 }
409 fprintf(out, "\n");
410 }
411 children->destroy(children);
412 }
413 enumerator->destroy(enumerator);
414 }
415
416 fprintf(out, "Security Associations:\n");
417 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
418 while (enumerator->enumerate(enumerator, &ike_sa))
419 {
420 bool ike_printed = FALSE;
421 child_sa_t *child_sa;
422 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
423
424 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
425 {
426 log_ike_sa(out, ike_sa, all);
427 found = TRUE;
428 ike_printed = TRUE;
429 }
430
431 while (children->iterate(children, (void**)&child_sa))
432 {
433 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
434 {
435 if (!ike_printed)
436 {
437 log_ike_sa(out, ike_sa, all);
438 found = TRUE;
439 ike_printed = TRUE;
440 }
441 log_child_sa(out, child_sa, all);
442 }
443 }
444 children->destroy(children);
445 }
446 enumerator->destroy(enumerator);
447
448 if (!found)
449 {
450 if (name)
451 {
452 fprintf(out, " no match\n");
453 }
454 else
455 {
456 fprintf(out, " none\n");
457 }
458 }
459 }
460
461 /**
462 * create a unique certificate list without duplicates
463 * certicates having the same issuer are grouped together.
464 */
465 static linked_list_t* create_unique_cert_list(certificate_type_t type)
466 {
467 linked_list_t *list = linked_list_create();
468 enumerator_t *enumerator = charon->credentials->create_cert_enumerator(
469 charon->credentials, type, KEY_ANY,
470 NULL, FALSE);
471 certificate_t *cert;
472
473 while (enumerator->enumerate(enumerator, (void**)&cert))
474 {
475 iterator_t *iterator = list->create_iterator(list, TRUE);
476 identification_t *issuer = cert->get_issuer(cert);
477 bool previous_same, same = FALSE, last = TRUE;
478 certificate_t *list_cert;
479
480 while (iterator->iterate(iterator, (void**)&list_cert))
481 {
482 /* exit if we have a duplicate? */
483 if (list_cert->equals(list_cert, cert))
484 {
485 last = FALSE;
486 break;
487 }
488 /* group certificates with same issuer */
489 previous_same = same;
490 same = list_cert->has_issuer(list_cert, issuer);
491 if (previous_same && !same)
492 {
493 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
494 last = FALSE;
495 break;
496 }
497 }
498 iterator->destroy(iterator);
499
500 if (last)
501 {
502 list->insert_last(list, (void *)cert->get_ref(cert));
503 }
504 }
505 enumerator->destroy(enumerator);
506 return list;
507 }
508
509 /**
510 * list all X.509 certificates matching the flags
511 */
512 static void stroke_list_certs(linked_list_t *list, char *label,
513 x509_flag_t flags, bool utc, FILE *out)
514 {
515 bool first = TRUE;
516 time_t now = time(NULL);
517 enumerator_t *enumerator = list->create_enumerator(list);
518 certificate_t *cert;
519
520 while (enumerator->enumerate(enumerator, (void**)&cert))
521 {
522 x509_t *x509 = (x509_t*)cert;
523 x509_flag_t x509_flags = x509->get_flags(x509);
524
525 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
526 if ((x509_flags & flags) || (flags == (x509_flags & ~X509_SELF_SIGNED)))
527 {
528 enumerator_t *enumerator;
529 identification_t *altName;
530 bool first_altName = TRUE;
531 chunk_t serial = x509->get_serial(x509);
532 identification_t *authkey = x509->get_authKeyIdentifier(x509);
533 time_t notBefore, notAfter;
534 public_key_t *public = cert->get_public_key(cert);
535
536 if (first)
537 {
538 fprintf(out, "\n");
539 fprintf(out, "List of %s:\n", label);
540 first = FALSE;
541 }
542 fprintf(out, "\n");
543
544 /* list subjectAltNames */
545 enumerator = x509->create_subjectAltName_enumerator(x509);
546 while (enumerator->enumerate(enumerator, (void**)&altName))
547 {
548 if (first_altName)
549 {
550 fprintf(out, " altNames: ");
551 first_altName = FALSE;
552 }
553 else
554 {
555 fprintf(out, ", ");
556 }
557 fprintf(out, "%D", altName);
558 }
559 if (!first_altName)
560 {
561 fprintf(out, "\n");
562 }
563 enumerator->destroy(enumerator);
564
565 fprintf(out, " subject: \"%D\"\n", cert->get_subject(cert));
566 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
567 fprintf(out, " serial: %#B\n", &serial);
568
569 /* list validity */
570 cert->get_validity(cert, &now, &notBefore, &notAfter);
571 fprintf(out, " validity: not before %#T, ", &notBefore, utc);
572 if (now < notBefore)
573 {
574 fprintf(out, "not valid yet (valid in %#V)\n", &now, &notBefore);
575 }
576 else
577 {
578 fprintf(out, "ok\n");
579 }
580 fprintf(out, " not after %#T, ", &notAfter, utc);
581 if (now > notAfter)
582 {
583 fprintf(out, "expired (%#V ago)\n", &now, &notAfter);
584 }
585 else
586 {
587 fprintf(out, "ok");
588 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
589 {
590 fprintf(out, " (expires in %#V)", &now, &notAfter);
591 }
592 fprintf(out, " \n");
593 }
594
595 /* list public key information */
596 if (public)
597 {
598 private_key_t *private = NULL;
599 identification_t *id, *keyid;
600
601 id = public->get_id(public, ID_PUBKEY_SHA1);
602 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
603
604 private = charon->credentials->get_private(
605 charon->credentials,
606 public->get_type(public), keyid, NULL);
607 fprintf(out, " pubkey: %N %d bits%s\n",
608 key_type_names, public->get_type(public),
609 public->get_keysize(public) * 8,
610 private ? ", has private key" : "");
611 fprintf(out, " keyid: %D\n", keyid);
612 fprintf(out, " subjkey: %D\n", id);
613 DESTROY_IF(private);
614 public->destroy(public);
615 }
616
617 /* list optional authorityKeyIdentifier */
618 if (authkey)
619 {
620 fprintf(out, " authkey: %D\n", authkey);
621 }
622 }
623 }
624 enumerator->destroy(enumerator);
625 }
626
627 /**
628 * list all X.509 attribute certificates
629 */
630 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
631 {
632 bool first = TRUE;
633 time_t thisUpdate, nextUpdate, now = time(NULL);
634 enumerator_t *enumerator = list->create_enumerator(list);
635 certificate_t *cert;
636
637 while (enumerator->enumerate(enumerator, (void**)&cert))
638 {
639 ac_t *ac = (ac_t*)cert;
640 chunk_t serial = ac->get_serial(ac);
641 chunk_t holderSerial = ac->get_holderSerial(ac);
642 identification_t *holderIssuer = ac->get_holderIssuer(ac);
643 identification_t *authkey = ac->get_authKeyIdentifier(ac);
644 identification_t *entityName = cert->get_subject(cert);
645
646 if (first)
647 {
648 fprintf(out, "\n");
649 fprintf(out, "List of X.509 Attribute Certificates:\n");
650 first = FALSE;
651 }
652 fprintf(out, "\n");
653
654 if (entityName)
655 {
656 fprintf(out, " holder: \"%D\"\n", entityName);
657 }
658 if (holderIssuer)
659 {
660 fprintf(out, " hissuer: \"%D\"\n", holderIssuer);
661 }
662 if (holderSerial.ptr)
663 {
664 fprintf(out, " hserial: %#B\n", &holderSerial);
665 }
666 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
667 fprintf(out, " serial: %#B\n", &serial);
668
669 /* list validity */
670 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
671 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
672 fprintf(out, " next %#T, ", &nextUpdate, utc);
673 if (now > nextUpdate)
674 {
675 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
676 }
677 else
678 {
679 fprintf(out, "ok");
680 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
681 {
682 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
683 }
684 fprintf(out, " \n");
685 }
686
687 /* list optional authorityKeyIdentifier */
688 if (authkey)
689 {
690 fprintf(out, " authkey: %D\n", authkey);
691 }
692 }
693 enumerator->destroy(enumerator);
694 }
695
696 /**
697 * list all X.509 CRLs
698 */
699 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
700 {
701 bool first = TRUE;
702 time_t thisUpdate, nextUpdate, now = time(NULL);
703 enumerator_t *enumerator = list->create_enumerator(list);
704 certificate_t *cert;
705
706 while (enumerator->enumerate(enumerator, (void**)&cert))
707 {
708 crl_t *crl = (crl_t*)cert;
709 chunk_t serial = crl->get_serial(crl);
710 identification_t *authkey = crl->get_authKeyIdentifier(crl);
711
712 if (first)
713 {
714 fprintf(out, "\n");
715 fprintf(out, "List of X.509 CRLs:\n");
716 first = FALSE;
717 }
718 fprintf(out, "\n");
719
720 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
721
722 /* list optional crlNumber */
723 if (serial.ptr)
724 {
725 fprintf(out, " serial: %#B\n", &serial);
726 }
727
728 /* count the number of revoked certificates */
729 {
730 int count = 0;
731 enumerator_t *enumerator = crl->create_enumerator(crl);
732
733 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
734 {
735 count++;
736 }
737 fprintf(out, " revoked: %d certificate%s\n", count,
738 (count == 1)? "" : "s");
739 enumerator->destroy(enumerator);
740 }
741
742 /* list validity */
743 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
744 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
745 fprintf(out, " next %#T, ", &nextUpdate, utc);
746 if (now > nextUpdate)
747 {
748 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
749 }
750 else
751 {
752 fprintf(out, "ok");
753 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
754 {
755 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
756 }
757 fprintf(out, " \n");
758 }
759
760 /* list optional authorityKeyIdentifier */
761 if (authkey)
762 {
763 fprintf(out, " authkey: %D\n", authkey);
764 }
765 }
766 enumerator->destroy(enumerator);
767 }
768
769 /**
770 * list all OCSP responses
771 */
772 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
773 {
774 bool first = TRUE;
775 enumerator_t *enumerator = list->create_enumerator(list);
776 certificate_t *cert;
777
778 while (enumerator->enumerate(enumerator, (void**)&cert))
779 {
780 if (first)
781 {
782 fprintf(out, "\n");
783 fprintf(out, "List of OCSP responses:\n");
784 fprintf(out, "\n");
785 first = FALSE;
786 }
787
788 fprintf(out, " signer: \"%D\"\n", cert->get_issuer(cert));
789 }
790 enumerator->destroy(enumerator);
791 }
792
793 /**
794 * List of registered cryptographical algorithms
795 */
796 static void list_algs(FILE *out)
797 {
798 enumerator_t *enumerator;
799 encryption_algorithm_t encryption;
800 integrity_algorithm_t integrity;
801 hash_algorithm_t hash;
802 pseudo_random_function_t prf;
803 diffie_hellman_group_t group;
804
805 fprintf(out, "\n");
806 fprintf(out, "List of registered IKEv2 Algorithms:\n");
807 fprintf(out, "\n encryption: ");
808 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
809 while (enumerator->enumerate(enumerator, &encryption))
810 {
811 fprintf(out, "%N ", encryption_algorithm_names, encryption);
812 }
813 enumerator->destroy(enumerator);
814 fprintf(out, "\n integrity: ");
815 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
816 while (enumerator->enumerate(enumerator, &integrity))
817 {
818 fprintf(out, "%N ", integrity_algorithm_names, integrity);
819 }
820 enumerator->destroy(enumerator);
821 fprintf(out, "\n hasher: ");
822 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
823 while (enumerator->enumerate(enumerator, &hash))
824 {
825 fprintf(out, "%N ", hash_algorithm_names, hash);
826 }
827 enumerator->destroy(enumerator);
828 fprintf(out, "\n prf: ");
829 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
830 while (enumerator->enumerate(enumerator, &prf))
831 {
832 fprintf(out, "%N ", pseudo_random_function_names, prf);
833 }
834 enumerator->destroy(enumerator);
835 fprintf(out, "\n dh-group: ");
836 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
837 while (enumerator->enumerate(enumerator, &group))
838 {
839 fprintf(out, "%N ", diffie_hellman_group_names, group);
840 }
841 enumerator->destroy(enumerator);
842 fprintf(out, "\n");
843 }
844
845 /**
846 * Implementation of stroke_list_t.list.
847 */
848 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
849 {
850 linked_list_t *cert_list = NULL;
851
852 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
853 {
854 cert_list = create_unique_cert_list(CERT_X509);
855 }
856 if (msg->list.flags & LIST_CERTS)
857 {
858 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
859 0, msg->list.utc, out);
860 }
861 if (msg->list.flags & LIST_CACERTS)
862 {
863 stroke_list_certs(cert_list, "X.509 CA Certificates",
864 X509_CA, msg->list.utc, out);
865 }
866 if (msg->list.flags & LIST_OCSPCERTS)
867 {
868 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
869 X509_OCSP_SIGNER, msg->list.utc, out);
870 }
871 if (msg->list.flags & LIST_AACERTS)
872 {
873 stroke_list_certs(cert_list, "X.509 AA Certificates",
874 X509_AA, msg->list.utc, out);
875 }
876 if (msg->list.flags & LIST_ACERTS)
877 {
878 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
879
880 stroke_list_acerts(ac_list, msg->list.utc, out);
881 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
882 }
883 if (msg->list.flags & LIST_CRLS)
884 {
885 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
886
887 stroke_list_crls(crl_list, msg->list.utc, out);
888 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
889 }
890 if (msg->list.flags & LIST_OCSP)
891 {
892 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
893
894 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
895
896 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
897 }
898 if (msg->list.flags & LIST_ALGS)
899 {
900 list_algs(out);
901 }
902 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
903 }
904
905 /**
906 * Implementation of stroke_list_t.destroy
907 */
908 static void destroy(private_stroke_list_t *this)
909 {
910 free(this);
911 }
912
913 /*
914 * see header file
915 */
916 stroke_list_t *stroke_list_create()
917 {
918 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
919
920 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
921 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
922 this->public.destroy = (void(*)(stroke_list_t*))destroy;
923
924 this->uptime = time(NULL);
925
926 return &this->public;
927 }
928