2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
16 #include "stroke_list.h"
22 #endif /* HAVE_MALLINFO */
26 #include <utils/linked_list.h>
27 #include <credentials/certificates/x509.h>
28 #include <credentials/certificates/ac.h>
29 #include <credentials/certificates/crl.h>
30 #include <credentials/certificates/pgp_certificate.h>
31 #include <credentials/ietf_attributes/ietf_attributes.h>
32 #include <config/peer_cfg.h>
34 /* warning intervals for list functions */
35 #define CERT_WARNING_INTERVAL 30 /* days */
36 #define CRL_WARNING_INTERVAL 7 /* days */
37 #define AC_WARNING_INTERVAL 1 /* day */
39 typedef struct private_stroke_list_t private_stroke_list_t
;
42 * private data of stroke_list
44 struct private_stroke_list_t
{
52 * timestamp of daemon start
57 * strokes attribute provider
59 stroke_attribute_t
*attribute
;
63 * Log tasks of a specific queue to out
65 static void log_task_q(FILE *out
, ike_sa_t
*ike_sa
, task_queue_t q
, char *name
)
67 enumerator_t
*enumerator
;
71 enumerator
= ike_sa
->create_task_enumerator(ike_sa
, q
);
72 while (enumerator
->enumerate(enumerator
, &task
))
76 fprintf(out
, "%12s[%d]: Tasks %s: ", ike_sa
->get_name(ike_sa
),
77 ike_sa
->get_unique_id(ike_sa
), name
);
80 fprintf(out
, "%N ", task_type_names
, task
->get_type(task
));
82 enumerator
->destroy(enumerator
);
90 * log an IKE_SA to out
92 static void log_ike_sa(FILE *out
, ike_sa_t
*ike_sa
, bool all
)
94 ike_sa_id_t
*id
= ike_sa
->get_id(ike_sa
);
95 time_t now
= time_monotonic(NULL
);
97 fprintf(out
, "%12s[%d]: %N",
98 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
),
99 ike_sa_state_names
, ike_sa
->get_state(ike_sa
));
101 if (ike_sa
->get_state(ike_sa
) == IKE_ESTABLISHED
)
105 established
= ike_sa
->get_statistic(ike_sa
, STAT_ESTABLISHED
);
106 fprintf(out
, " %V ago", &now
, &established
);
109 fprintf(out
, ", %H[%Y]...%H[%Y]\n",
110 ike_sa
->get_my_host(ike_sa
), ike_sa
->get_my_id(ike_sa
),
111 ike_sa
->get_other_host(ike_sa
), ike_sa
->get_other_id(ike_sa
));
115 proposal_t
*ike_proposal
;
117 ike_proposal
= ike_sa
->get_proposal(ike_sa
);
119 fprintf(out
, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s",
120 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
),
121 id
->get_initiator_spi(id
), id
->is_initiator(id
) ?
"*" : "",
122 id
->get_responder_spi(id
), id
->is_initiator(id
) ?
"" : "*");
125 if (ike_sa
->get_state(ike_sa
) == IKE_ESTABLISHED
)
127 time_t rekey
, reauth
;
128 peer_cfg_t
*peer_cfg
;
130 rekey
= ike_sa
->get_statistic(ike_sa
, STAT_REKEY
);
131 reauth
= ike_sa
->get_statistic(ike_sa
, STAT_REAUTH
);
132 peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
136 fprintf(out
, ", rekeying in %V", &rekey
, &now
);
141 enumerator_t
*enumerator
;
145 enumerator
= peer_cfg
->create_auth_cfg_enumerator(peer_cfg
, TRUE
);
146 while (enumerator
->enumerate(enumerator
, &auth
))
153 fprintf(out
, "%N", auth_class_names
,
154 auth
->get(auth
, AUTH_RULE_AUTH_CLASS
));
156 enumerator
->destroy(enumerator
);
157 fprintf(out
, " reauthentication in %V", &reauth
, &now
);
159 if (!rekey
&& !reauth
)
161 fprintf(out
, ", rekeying disabled");
170 snprintf(buf
, BUF_LEN
, "%P", ike_proposal
);
171 fprintf(out
, "%12s[%d]: IKE proposal: %s\n",
172 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
),
176 log_task_q(out
, ike_sa
, TASK_QUEUE_QUEUED
, "queued");
177 log_task_q(out
, ike_sa
, TASK_QUEUE_ACTIVE
, "active");
178 log_task_q(out
, ike_sa
, TASK_QUEUE_PASSIVE
, "passive");
183 * log an CHILD_SA to out
185 static void log_child_sa(FILE *out
, child_sa_t
*child_sa
, bool all
)
187 time_t use_in
, use_out
, rekey
, now
;
188 u_int64_t bytes_in
, bytes_out
;
189 proposal_t
*proposal
;
190 child_cfg_t
*config
= child_sa
->get_config(child_sa
);
193 fprintf(out
, "%12s{%d}: %N, %N%s",
194 child_sa
->get_name(child_sa
), child_sa
->get_reqid(child_sa
),
195 child_sa_state_names
, child_sa
->get_state(child_sa
),
196 ipsec_mode_names
, child_sa
->get_mode(child_sa
),
197 config
->use_proxy_mode(config
) ?
"_PROXY" : "");
199 if (child_sa
->get_state(child_sa
) == CHILD_INSTALLED
)
201 fprintf(out
, ", %N%s SPIs: %.8x_i %.8x_o",
202 protocol_id_names
, child_sa
->get_protocol(child_sa
),
203 child_sa
->has_encap(child_sa
) ?
" in UDP" : "",
204 ntohl(child_sa
->get_spi(child_sa
, TRUE
)),
205 ntohl(child_sa
->get_spi(child_sa
, FALSE
)));
207 if (child_sa
->get_ipcomp(child_sa
) != IPCOMP_NONE
)
209 fprintf(out
, ", IPCOMP CPIs: %.4x_i %.4x_o",
210 ntohs(child_sa
->get_cpi(child_sa
, TRUE
)),
211 ntohs(child_sa
->get_cpi(child_sa
, FALSE
)));
216 fprintf(out
, "\n%12s{%d}: ", child_sa
->get_name(child_sa
),
217 child_sa
->get_reqid(child_sa
));
219 proposal
= child_sa
->get_proposal(child_sa
);
222 u_int16_t encr_alg
= ENCR_UNDEFINED
, int_alg
= AUTH_UNDEFINED
;
223 u_int16_t encr_size
= 0, int_size
= 0;
225 proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
,
226 &encr_alg
, &encr_size
);
227 proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
,
228 &int_alg
, &int_size
);
230 if (encr_alg
!= ENCR_UNDEFINED
)
232 fprintf(out
, "%N", encryption_algorithm_names
, encr_alg
);
235 fprintf(out
, "_%u", encr_size
);
238 if (int_alg
!= AUTH_UNDEFINED
)
240 fprintf(out
, "/%N", integrity_algorithm_names
, int_alg
);
243 fprintf(out
, "_%u", int_size
);
248 now
= time_monotonic(NULL
);
249 child_sa
->get_usestats(child_sa
, TRUE
, &use_in
, &bytes_in
);
250 fprintf(out
, ", %llu bytes_i", bytes_in
);
253 fprintf(out
, " (%ds ago)", now
- use_in
);
256 child_sa
->get_usestats(child_sa
, FALSE
, &use_out
, &bytes_out
);
257 fprintf(out
, ", %llu bytes_o", bytes_out
);
260 fprintf(out
, " (%ds ago)", now
- use_out
);
262 fprintf(out
, ", rekeying ");
264 rekey
= child_sa
->get_lifetime(child_sa
, FALSE
);
269 fprintf(out
, "active");
273 fprintf(out
, "in %V", &now
, &rekey
);
278 fprintf(out
, "disabled");
284 fprintf(out
, "\n%12s{%d}: %#R=== %#R\n",
285 child_sa
->get_name(child_sa
), child_sa
->get_reqid(child_sa
),
286 child_sa
->get_traffic_selectors(child_sa
, TRUE
),
287 child_sa
->get_traffic_selectors(child_sa
, FALSE
));
291 * Log a configs local or remote authentication config to out
293 static void log_auth_cfgs(FILE *out
, peer_cfg_t
*peer_cfg
, bool local
)
295 enumerator_t
*enumerator
, *rules
;
298 auth_class_t auth_class
;
299 identification_t
*id
;
301 cert_validation_t valid
;
304 name
= peer_cfg
->get_name(peer_cfg
);
306 enumerator
= peer_cfg
->create_auth_cfg_enumerator(peer_cfg
, local
);
307 while (enumerator
->enumerate(enumerator
, &auth
))
309 fprintf(out
, "%12s: %s [%Y] uses ", name
, local ?
"local: " : "remote:",
310 auth
->get(auth
, AUTH_RULE_IDENTITY
));
312 auth_class
= (uintptr_t)auth
->get(auth
, AUTH_RULE_AUTH_CLASS
);
313 if (auth_class
!= AUTH_CLASS_EAP
)
315 fprintf(out
, "%N authentication\n", auth_class_names
, auth_class
);
319 if ((uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_TYPE
) == EAP_NAK
)
321 fprintf(out
, "EAP authentication");
325 if ((uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_VENDOR
))
327 fprintf(out
, "EAP_%d-%d authentication",
328 (uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_TYPE
),
329 (uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_VENDOR
));
333 fprintf(out
, "%N authentication", eap_type_names
,
334 (uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_TYPE
));
337 id
= auth
->get(auth
, AUTH_RULE_EAP_IDENTITY
);
340 fprintf(out
, " with EAP identity '%Y'", id
);
345 cert
= auth
->get(auth
, AUTH_RULE_CA_CERT
);
348 fprintf(out
, "%12s: ca: \"%Y\"\n", name
, cert
->get_subject(cert
));
351 cert
= auth
->get(auth
, AUTH_RULE_IM_CERT
);
354 fprintf(out
, "%12s: im-ca: \"%Y\"\n", name
, cert
->get_subject(cert
));
357 cert
= auth
->get(auth
, AUTH_RULE_SUBJECT_CERT
);
360 fprintf(out
, "%12s: cert: \"%Y\"\n", name
,
361 cert
->get_subject(cert
));
364 valid
= (uintptr_t)auth
->get(auth
, AUTH_RULE_OCSP_VALIDATION
);
365 if (valid
!= VALIDATION_FAILED
)
367 fprintf(out
, "%12s: ocsp: status must be GOOD%s\n", name
,
368 (valid
== VALIDATION_SKIPPED
) ?
" or SKIPPED" : "");
371 valid
= (uintptr_t)auth
->get(auth
, AUTH_RULE_CRL_VALIDATION
);
372 if (valid
!= VALIDATION_FAILED
)
374 fprintf(out
, "%12s: crl: status must be GOOD%s\n", name
,
375 (valid
== VALIDATION_SKIPPED
) ?
" or SKIPPED" : "");
378 rules
= auth
->create_enumerator(auth
);
379 while (rules
->enumerate(rules
, &rule
, &id
))
381 if (rule
== AUTH_RULE_GROUP
)
383 fprintf(out
, "%12s: group: %Y\n", name
, id
);
386 rules
->destroy(rules
);
388 enumerator
->destroy(enumerator
);
391 METHOD(stroke_list_t
, status
, void,
392 private_stroke_list_t
*this, stroke_msg_t
*msg
, FILE *out
, bool all
)
394 enumerator_t
*enumerator
, *children
;
396 child_cfg_t
*child_cfg
;
397 child_sa_t
*child_sa
;
399 bool first
, found
= FALSE
;
400 char *name
= msg
->status
.name
;
404 peer_cfg_t
*peer_cfg
;
410 u_int size
, online
, offline
, i
;
411 now
= time_monotonic(NULL
);
412 since
= time(NULL
) - (now
- this->uptime
);
414 fprintf(out
, "Status of IKEv2 charon daemon (strongSwan "VERSION
"):\n");
415 fprintf(out
, " uptime: %V, since %T\n", &now
, &this->uptime
, &since
, FALSE
);
418 struct mallinfo mi
= mallinfo();
420 fprintf(out
, " malloc: sbrk %d, mmap %d, used %d, free %d\n",
421 mi
.arena
, mi
.hblkhd
, mi
.uordblks
, mi
.fordblks
);
423 #endif /* HAVE_MALLINFO */
424 fprintf(out
, " worker threads: %d idle of %d,",
425 lib
->processor
->get_idle_threads(lib
->processor
),
426 lib
->processor
->get_total_threads(lib
->processor
));
427 fprintf(out
, " job queue load: ");
428 for (i
= 0; i
< JOB_PRIO_MAX
; i
++)
430 fprintf(out
, "%s%d", i
== 0 ?
"" : "/",
431 lib
->processor
->get_job_load(lib
->processor
, i
));
433 fprintf(out
, ", scheduled events: %d\n",
434 lib
->scheduler
->get_job_load(lib
->scheduler
));
435 fprintf(out
, " loaded plugins: ");
436 enumerator
= lib
->plugins
->create_plugin_enumerator(lib
->plugins
);
437 while (enumerator
->enumerate(enumerator
, &plugin
))
439 fprintf(out
, "%s ", plugin
->get_name(plugin
));
441 enumerator
->destroy(enumerator
);
445 enumerator
= this->attribute
->create_pool_enumerator(this->attribute
);
446 while (enumerator
->enumerate(enumerator
, &pool
, &size
, &online
, &offline
))
448 if (name
&& !streq(name
, pool
))
455 fprintf(out
, "Virtual IP pools (size/online/offline):\n");
457 fprintf(out
, " %s: %u/%u/%u\n", pool
, size
, online
, offline
);
459 enumerator
->destroy(enumerator
);
461 enumerator
= hydra
->kernel_interface
->create_address_enumerator(
462 hydra
->kernel_interface
, FALSE
, FALSE
);
463 fprintf(out
, "Listening IP addresses:\n");
464 while (enumerator
->enumerate(enumerator
, (void**)&host
))
466 fprintf(out
, " %H\n", host
);
468 enumerator
->destroy(enumerator
);
470 fprintf(out
, "Connections:\n");
471 enumerator
= charon
->backends
->create_peer_cfg_enumerator(
472 charon
->backends
, NULL
, NULL
, NULL
, NULL
);
473 while (enumerator
->enumerate(enumerator
, &peer_cfg
))
475 if (peer_cfg
->get_ike_version(peer_cfg
) != 2 ||
476 (name
&& !streq(name
, peer_cfg
->get_name(peer_cfg
))))
481 ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
482 fprintf(out
, "%12s: %s...%s", peer_cfg
->get_name(peer_cfg
),
483 ike_cfg
->get_my_addr(ike_cfg
), ike_cfg
->get_other_addr(ike_cfg
));
485 dpd
= peer_cfg
->get_dpd(peer_cfg
);
488 fprintf(out
, ", dpddelay=%us", dpd
);
492 log_auth_cfgs(out
, peer_cfg
, TRUE
);
493 log_auth_cfgs(out
, peer_cfg
, FALSE
);
495 children
= peer_cfg
->create_child_cfg_enumerator(peer_cfg
);
496 while (children
->enumerate(children
, &child_cfg
))
498 linked_list_t
*my_ts
, *other_ts
;
500 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
, NULL
);
501 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
, NULL
);
502 fprintf(out
, "%12s: child: %#R=== %#R", child_cfg
->get_name(child_cfg
),
504 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
505 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
509 fprintf(out
, ", dpdaction=%N", action_names
,
510 child_cfg
->get_dpd_action(child_cfg
));
514 children
->destroy(children
);
516 enumerator
->destroy(enumerator
);
520 enumerator
= charon
->traps
->create_enumerator(charon
->traps
);
521 while (enumerator
->enumerate(enumerator
, NULL
, &child_sa
))
525 fprintf(out
, "Routed Connections:\n");
528 log_child_sa(out
, child_sa
, all
);
530 enumerator
->destroy(enumerator
);
532 fprintf(out
, "Security Associations:\n");
533 enumerator
= charon
->controller
->create_ike_sa_enumerator(
534 charon
->controller
, TRUE
);
535 while (enumerator
->enumerate(enumerator
, &ike_sa
))
537 bool ike_printed
= FALSE
;
538 iterator_t
*children
= ike_sa
->create_child_sa_iterator(ike_sa
);
540 if (name
== NULL
|| streq(name
, ike_sa
->get_name(ike_sa
)))
542 log_ike_sa(out
, ike_sa
, all
);
547 while (children
->iterate(children
, (void**)&child_sa
))
549 if (name
== NULL
|| streq(name
, child_sa
->get_name(child_sa
)))
553 log_ike_sa(out
, ike_sa
, all
);
557 log_child_sa(out
, child_sa
, all
);
560 children
->destroy(children
);
562 enumerator
->destroy(enumerator
);
568 fprintf(out
, " no match\n");
572 fprintf(out
, " none\n");
578 * create a unique certificate list without duplicates
579 * certicates having the same issuer are grouped together.
581 static linked_list_t
* create_unique_cert_list(certificate_type_t type
)
583 linked_list_t
*list
= linked_list_create();
584 enumerator_t
*enumerator
= lib
->credmgr
->create_cert_enumerator(
585 lib
->credmgr
, type
, KEY_ANY
, NULL
, FALSE
);
588 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
590 iterator_t
*iterator
= list
->create_iterator(list
, TRUE
);
591 identification_t
*issuer
= cert
->get_issuer(cert
);
592 bool previous_same
, same
= FALSE
, last
= TRUE
;
593 certificate_t
*list_cert
;
595 while (iterator
->iterate(iterator
, (void**)&list_cert
))
597 /* exit if we have a duplicate? */
598 if (list_cert
->equals(list_cert
, cert
))
603 /* group certificates with same issuer */
604 previous_same
= same
;
605 same
= list_cert
->has_issuer(list_cert
, issuer
);
606 if (previous_same
&& !same
)
608 iterator
->insert_before(iterator
, (void *)cert
->get_ref(cert
));
613 iterator
->destroy(iterator
);
617 list
->insert_last(list
, (void *)cert
->get_ref(cert
));
620 enumerator
->destroy(enumerator
);
625 * Print a single public key.
627 static void list_public_key(public_key_t
*public, FILE *out
)
629 private_key_t
*private = NULL
;
631 identification_t
*id
;
634 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1
, &keyid
))
636 id
= identification_create_from_encoding(ID_KEY_ID
, keyid
);
637 auth
= auth_cfg_create();
638 private = lib
->credmgr
->get_private(lib
->credmgr
,
639 public->get_type(public), id
, auth
);
644 fprintf(out
, " pubkey: %N %d bits%s\n",
645 key_type_names
, public->get_type(public),
646 public->get_keysize(public),
647 private ?
", has private key" : "");
648 if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1
, &keyid
))
650 fprintf(out
, " keyid: %#B\n", &keyid
);
652 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1
, &keyid
))
654 fprintf(out
, " subjkey: %#B\n", &keyid
);
660 * list all raw public keys
662 static void stroke_list_pubkeys(linked_list_t
*list
, bool utc
, FILE *out
)
666 enumerator_t
*enumerator
= list
->create_enumerator(list
);
669 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
671 public_key_t
*public = cert
->get_public_key(cert
);
678 fprintf(out
, "List of Raw Public Keys:\n");
683 list_public_key(public, out
);
684 public->destroy(public);
687 enumerator
->destroy(enumerator
);
691 * list OpenPGP certificates
693 static void stroke_list_pgp(linked_list_t
*list
,bool utc
, FILE *out
)
696 time_t now
= time(NULL
);
697 enumerator_t
*enumerator
= list
->create_enumerator(list
);
700 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
702 time_t created
, until
;
703 public_key_t
*public;
704 pgp_certificate_t
*pgp_cert
= (pgp_certificate_t
*)cert
;
705 chunk_t fingerprint
= pgp_cert
->get_fingerprint(pgp_cert
);
711 fprintf(out
, "List of PGP End Entity Certificates:\n");
715 fprintf(out
, " userid: '%Y'\n", cert
->get_subject(cert
));
717 fprintf(out
, " digest: %#B\n", &fingerprint
);
720 cert
->get_validity(cert
, &now
, &created
, &until
);
721 fprintf(out
, " created: %T\n", &created
, utc
);
722 fprintf(out
, " until: %T%s\n", &until
, utc
,
723 (until
== TIME_32_BIT_SIGNED_MAX
) ?
" (expires never)":"");
725 public = cert
->get_public_key(cert
);
728 list_public_key(public, out
);
729 public->destroy(public);
732 enumerator
->destroy(enumerator
);
736 * list all X.509 certificates matching the flags
738 static void stroke_list_certs(linked_list_t
*list
, char *label
,
739 x509_flag_t flags
, bool utc
, FILE *out
)
742 time_t now
= time(NULL
);
743 enumerator_t
*enumerator
;
745 x509_flag_t flag_mask
;
747 /* mask all auxiliary flags */
748 flag_mask
= ~(X509_SERVER_AUTH
| X509_CLIENT_AUTH
|
749 X509_SELF_SIGNED
| X509_IP_ADDR_BLOCKS
);
751 enumerator
= list
->create_enumerator(list
);
752 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
754 x509_t
*x509
= (x509_t
*)cert
;
755 x509_flag_t x509_flags
= x509
->get_flags(x509
) & flag_mask
;
757 /* list only if flag is set or flag == 0 */
758 if ((x509_flags
& flags
) || (x509_flags
== flags
))
760 enumerator_t
*enumerator
;
761 identification_t
*altName
;
762 bool first_altName
= TRUE
;
764 chunk_t serial
, authkey
;
765 time_t notBefore
, notAfter
;
766 public_key_t
*public;
771 fprintf(out
, "List of %s:\n", label
);
776 /* list subjectAltNames */
777 enumerator
= x509
->create_subjectAltName_enumerator(x509
);
778 while (enumerator
->enumerate(enumerator
, (void**)&altName
))
782 fprintf(out
, " altNames: ");
783 first_altName
= FALSE
;
789 fprintf(out
, "%Y", altName
);
795 enumerator
->destroy(enumerator
);
797 fprintf(out
, " subject: \"%Y\"\n", cert
->get_subject(cert
));
798 fprintf(out
, " issuer: \"%Y\"\n", cert
->get_issuer(cert
));
799 serial
= x509
->get_serial(x509
);
800 fprintf(out
, " serial: %#B\n", &serial
);
803 cert
->get_validity(cert
, &now
, ¬Before
, ¬After
);
804 fprintf(out
, " validity: not before %T, ", ¬Before
, utc
);
807 fprintf(out
, "not valid yet (valid in %V)\n", &now
, ¬Before
);
811 fprintf(out
, "ok\n");
813 fprintf(out
, " not after %T, ", ¬After
, utc
);
816 fprintf(out
, "expired (%V ago)\n", &now
, ¬After
);
821 if (now
> notAfter
- CERT_WARNING_INTERVAL
* 60 * 60 * 24)
823 fprintf(out
, " (expires in %V)", &now
, ¬After
);
828 public = cert
->get_public_key(cert
);
831 list_public_key(public, out
);
832 public->destroy(public);
835 /* list optional authorityKeyIdentifier */
836 authkey
= x509
->get_authKeyIdentifier(x509
);
839 fprintf(out
, " authkey: %#B\n", &authkey
);
842 /* list optional pathLenConstraint */
843 pathlen
= x509
->get_constraint(x509
, X509_PATH_LEN
);
844 if (pathlen
!= X509_NO_CONSTRAINT
)
846 fprintf(out
, " pathlen: %u\n", pathlen
);
849 /* list optional ipAddrBlocks */
850 if (x509
->get_flags(x509
) & X509_IP_ADDR_BLOCKS
)
852 traffic_selector_t
*ipAddrBlock
;
853 bool first_ipAddrBlock
= TRUE
;
855 fprintf(out
, " addresses: ");
856 enumerator
= x509
->create_ipAddrBlock_enumerator(x509
);
857 while (enumerator
->enumerate(enumerator
, &ipAddrBlock
))
859 if (first_ipAddrBlock
)
861 first_ipAddrBlock
= FALSE
;
867 fprintf(out
, "%R", ipAddrBlock
);
869 enumerator
->destroy(enumerator
);
874 enumerator
->destroy(enumerator
);
878 * list all X.509 attribute certificates
880 static void stroke_list_acerts(linked_list_t
*list
, bool utc
, FILE *out
)
883 time_t thisUpdate
, nextUpdate
, now
= time(NULL
);
884 enumerator_t
*enumerator
= list
->create_enumerator(list
);
887 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
889 ac_t
*ac
= (ac_t
*)cert
;
890 identification_t
*id
;
891 ietf_attributes_t
*groups
;
897 fprintf(out
, "List of X.509 Attribute Certificates:\n");
902 id
= cert
->get_subject(cert
);
905 fprintf(out
, " holder: \"%Y\"\n", id
);
907 id
= ac
->get_holderIssuer(ac
);
910 fprintf(out
, " hissuer: \"%Y\"\n", id
);
912 chunk
= ac
->get_holderSerial(ac
);
915 fprintf(out
, " hserial: %#B\n", &chunk
);
917 groups
= ac
->get_groups(ac
);
920 fprintf(out
, " groups: %s\n", groups
->get_string(groups
));
921 groups
->destroy(groups
);
923 fprintf(out
, " issuer: \"%Y\"\n", cert
->get_issuer(cert
));
924 chunk
= ac
->get_serial(ac
);
925 fprintf(out
, " serial: %#B\n", &chunk
);
928 cert
->get_validity(cert
, &now
, &thisUpdate
, &nextUpdate
);
929 fprintf(out
, " updates: this %T\n", &thisUpdate
, utc
);
930 fprintf(out
, " next %T, ", &nextUpdate
, utc
);
931 if (now
> nextUpdate
)
933 fprintf(out
, "expired (%V ago)\n", &now
, &nextUpdate
);
938 if (now
> nextUpdate
- AC_WARNING_INTERVAL
* 60 * 60 * 24)
940 fprintf(out
, " (expires in %V)", &now
, &nextUpdate
);
945 /* list optional authorityKeyIdentifier */
946 chunk
= ac
->get_authKeyIdentifier(ac
);
949 fprintf(out
, " authkey: %#B\n", &chunk
);
952 enumerator
->destroy(enumerator
);
956 * list all X.509 CRLs
958 static void stroke_list_crls(linked_list_t
*list
, bool utc
, FILE *out
)
961 time_t thisUpdate
, nextUpdate
, now
= time(NULL
);
962 enumerator_t
*enumerator
= list
->create_enumerator(list
);
965 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
967 crl_t
*crl
= (crl_t
*)cert
;
973 fprintf(out
, "List of X.509 CRLs:\n");
978 fprintf(out
, " issuer: \"%Y\"\n", cert
->get_issuer(cert
));
980 /* list optional crlNumber */
981 chunk
= crl
->get_serial(crl
);
984 fprintf(out
, " serial: %#B\n", &chunk
);
986 if (crl
->is_delta_crl(crl
, &chunk
))
988 fprintf(out
, " delta for: %#B\n", &chunk
);
991 /* count the number of revoked certificates */
994 enumerator_t
*enumerator
= crl
->create_enumerator(crl
);
996 while (enumerator
->enumerate(enumerator
, NULL
, NULL
, NULL
))
1000 fprintf(out
, " revoked: %d certificate%s\n", count
,
1001 (count
== 1)?
"" : "s");
1002 enumerator
->destroy(enumerator
);
1006 cert
->get_validity(cert
, &now
, &thisUpdate
, &nextUpdate
);
1007 fprintf(out
, " updates: this %T\n", &thisUpdate
, utc
);
1008 fprintf(out
, " next %T, ", &nextUpdate
, utc
);
1009 if (now
> nextUpdate
)
1011 fprintf(out
, "expired (%V ago)\n", &now
, &nextUpdate
);
1016 if (now
> nextUpdate
- CRL_WARNING_INTERVAL
* 60 * 60 * 24)
1018 fprintf(out
, " (expires in %V)", &now
, &nextUpdate
);
1020 fprintf(out
, " \n");
1023 /* list optional authorityKeyIdentifier */
1024 chunk
= crl
->get_authKeyIdentifier(crl
);
1027 fprintf(out
, " authkey: %#B\n", &chunk
);
1030 enumerator
->destroy(enumerator
);
1034 * list all OCSP responses
1036 static void stroke_list_ocsp(linked_list_t
* list
, bool utc
, FILE *out
)
1038 bool first
= TRUE
, ok
;
1039 enumerator_t
*enumerator
= list
->create_enumerator(list
);
1040 certificate_t
*cert
;
1041 time_t produced
, usable
, now
= time(NULL
);
1043 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
1048 fprintf(out
, "List of OCSP responses:\n");
1052 fprintf(out
, " signer: \"%Y\"\n", cert
->get_issuer(cert
));
1054 /* check validity */
1055 ok
= cert
->get_validity(cert
, &now
, &produced
, &usable
);
1056 fprintf(out
, " validity: produced at %T\n", &produced
, utc
);
1057 fprintf(out
, " usable till %T, ", &usable
, utc
);
1060 fprintf(out
, "ok\n");
1064 fprintf(out
, "expired (%V ago)\n", &now
, &usable
);
1067 enumerator
->destroy(enumerator
);
1071 * Print the name of an algorithm plus the name of the plugin that registered it
1073 static void print_alg(FILE *out
, int *len
, enum_name_t
*alg_names
, int alg_type
,
1074 const char *plugin_name
)
1076 char alg_name
[BUF_LEN
];
1079 alg_name_len
= sprintf(alg_name
, " %N[%s]", alg_names
, alg_type
, plugin_name
);
1080 if (*len
+ alg_name_len
> CRYPTO_MAX_ALG_LINE
)
1082 fprintf(out
, "\n ");
1085 fprintf(out
, "%s", alg_name
);
1086 *len
+= alg_name_len
;
1090 * List of registered cryptographical algorithms
1092 static void list_algs(FILE *out
)
1094 enumerator_t
*enumerator
;
1095 encryption_algorithm_t encryption
;
1096 integrity_algorithm_t integrity
;
1097 hash_algorithm_t hash
;
1098 pseudo_random_function_t prf
;
1099 diffie_hellman_group_t group
;
1100 rng_quality_t quality
;
1101 const char *plugin_name
;
1105 fprintf(out
, "List of registered IKEv2 Algorithms:\n");
1106 fprintf(out
, "\n encryption:");
1108 enumerator
= lib
->crypto
->create_crypter_enumerator(lib
->crypto
);
1109 while (enumerator
->enumerate(enumerator
, &encryption
, &plugin_name
))
1111 print_alg(out
, &len
, encryption_algorithm_names
, encryption
, plugin_name
);
1113 enumerator
->destroy(enumerator
);
1114 fprintf(out
, "\n integrity: ");
1116 enumerator
= lib
->crypto
->create_signer_enumerator(lib
->crypto
);
1117 while (enumerator
->enumerate(enumerator
, &integrity
, &plugin_name
))
1119 print_alg(out
, &len
, integrity_algorithm_names
, integrity
, plugin_name
);
1121 enumerator
->destroy(enumerator
);
1122 fprintf(out
, "\n aead: ");
1124 enumerator
= lib
->crypto
->create_aead_enumerator(lib
->crypto
);
1125 while (enumerator
->enumerate(enumerator
, &encryption
, &plugin_name
))
1127 print_alg(out
, &len
, encryption_algorithm_names
, encryption
, plugin_name
);
1129 enumerator
->destroy(enumerator
);
1130 fprintf(out
, "\n hasher: ");
1132 enumerator
= lib
->crypto
->create_hasher_enumerator(lib
->crypto
);
1133 while (enumerator
->enumerate(enumerator
, &hash
, &plugin_name
))
1135 print_alg(out
, &len
, hash_algorithm_names
, hash
, plugin_name
);
1137 enumerator
->destroy(enumerator
);
1138 fprintf(out
, "\n prf: ");
1140 enumerator
= lib
->crypto
->create_prf_enumerator(lib
->crypto
);
1141 while (enumerator
->enumerate(enumerator
, &prf
, &plugin_name
))
1143 print_alg(out
, &len
, pseudo_random_function_names
, prf
, plugin_name
);
1145 enumerator
->destroy(enumerator
);
1146 fprintf(out
, "\n dh-group: ");
1148 enumerator
= lib
->crypto
->create_dh_enumerator(lib
->crypto
);
1149 while (enumerator
->enumerate(enumerator
, &group
, &plugin_name
))
1151 print_alg(out
, &len
, diffie_hellman_group_names
, group
, plugin_name
);
1153 enumerator
->destroy(enumerator
);
1154 fprintf(out
, "\n random-gen:");
1156 enumerator
= lib
->crypto
->create_rng_enumerator(lib
->crypto
);
1157 while (enumerator
->enumerate(enumerator
, &quality
, &plugin_name
))
1159 print_alg(out
, &len
, rng_quality_names
, quality
, plugin_name
);
1161 enumerator
->destroy(enumerator
);
1165 METHOD(stroke_list_t
, list
, void,
1166 private_stroke_list_t
*this, stroke_msg_t
*msg
, FILE *out
)
1168 linked_list_t
*cert_list
= NULL
;
1170 if (msg
->list
.flags
& LIST_PUBKEYS
)
1172 linked_list_t
*pubkey_list
= create_unique_cert_list(CERT_TRUSTED_PUBKEY
);
1174 stroke_list_pubkeys(pubkey_list
, msg
->list
.utc
, out
);
1175 pubkey_list
->destroy_offset(pubkey_list
, offsetof(certificate_t
, destroy
));
1177 if (msg
->list
.flags
& LIST_CERTS
)
1179 linked_list_t
*pgp_list
= create_unique_cert_list(CERT_GPG
);
1181 stroke_list_pgp(pgp_list
, msg
->list
.utc
, out
);
1182 pgp_list
->destroy_offset(pgp_list
, offsetof(certificate_t
, destroy
));
1184 if (msg
->list
.flags
& (LIST_CERTS
| LIST_CACERTS
| LIST_OCSPCERTS
| LIST_AACERTS
))
1186 cert_list
= create_unique_cert_list(CERT_X509
);
1188 if (msg
->list
.flags
& LIST_CERTS
)
1190 stroke_list_certs(cert_list
, "X.509 End Entity Certificates",
1191 X509_NONE
, msg
->list
.utc
, out
);
1193 if (msg
->list
.flags
& LIST_CACERTS
)
1195 stroke_list_certs(cert_list
, "X.509 CA Certificates",
1196 X509_CA
, msg
->list
.utc
, out
);
1198 if (msg
->list
.flags
& LIST_OCSPCERTS
)
1200 stroke_list_certs(cert_list
, "X.509 OCSP Signer Certificates",
1201 X509_OCSP_SIGNER
, msg
->list
.utc
, out
);
1203 if (msg
->list
.flags
& LIST_AACERTS
)
1205 stroke_list_certs(cert_list
, "X.509 AA Certificates",
1206 X509_AA
, msg
->list
.utc
, out
);
1208 DESTROY_OFFSET_IF(cert_list
, offsetof(certificate_t
, destroy
));
1210 if (msg
->list
.flags
& LIST_ACERTS
)
1212 linked_list_t
*ac_list
= create_unique_cert_list(CERT_X509_AC
);
1214 stroke_list_acerts(ac_list
, msg
->list
.utc
, out
);
1215 ac_list
->destroy_offset(ac_list
, offsetof(certificate_t
, destroy
));
1217 if (msg
->list
.flags
& LIST_CRLS
)
1219 linked_list_t
*crl_list
= create_unique_cert_list(CERT_X509_CRL
);
1221 stroke_list_crls(crl_list
, msg
->list
.utc
, out
);
1222 crl_list
->destroy_offset(crl_list
, offsetof(certificate_t
, destroy
));
1224 if (msg
->list
.flags
& LIST_OCSP
)
1226 linked_list_t
*ocsp_list
= create_unique_cert_list(CERT_X509_OCSP_RESPONSE
);
1228 stroke_list_ocsp(ocsp_list
, msg
->list
.utc
, out
);
1230 ocsp_list
->destroy_offset(ocsp_list
, offsetof(certificate_t
, destroy
));
1232 if (msg
->list
.flags
& LIST_ALGS
)
1239 * Print leases of a single pool
1241 static void pool_leases(private_stroke_list_t
*this, FILE *out
, char *pool
,
1242 host_t
*address
, u_int size
, u_int online
, u_int offline
)
1244 enumerator_t
*enumerator
;
1245 identification_t
*id
;
1250 fprintf(out
, "Leases in pool '%s', usage: %u/%u, %u online\n",
1251 pool
, online
+ offline
, size
, online
);
1252 enumerator
= this->attribute
->create_lease_enumerator(this->attribute
, pool
);
1253 while (enumerator
&& enumerator
->enumerate(enumerator
, &id
, &lease
, &on
))
1255 if (!address
|| address
->ip_equals(address
, lease
))
1257 fprintf(out
, " %15H %s '%Y'\n",
1258 lease
, on ?
"online" : "offline", id
);
1262 enumerator
->destroy(enumerator
);
1265 fprintf(out
, " no matching leases found\n");
1269 METHOD(stroke_list_t
, leases
, void,
1270 private_stroke_list_t
*this, stroke_msg_t
*msg
, FILE *out
)
1272 enumerator_t
*enumerator
;
1273 u_int size
, offline
, online
;
1274 host_t
*address
= NULL
;
1278 if (msg
->leases
.address
)
1280 address
= host_create_from_string(msg
->leases
.address
, 0);
1283 enumerator
= this->attribute
->create_pool_enumerator(this->attribute
);
1284 while (enumerator
->enumerate(enumerator
, &pool
, &size
, &online
, &offline
))
1286 if (!msg
->leases
.pool
|| streq(msg
->leases
.pool
, pool
))
1288 pool_leases(this, out
, pool
, address
, size
, online
, offline
);
1292 enumerator
->destroy(enumerator
);
1295 if (msg
->leases
.pool
)
1297 fprintf(out
, "pool '%s' not found\n", msg
->leases
.pool
);
1301 fprintf(out
, "no pools found\n");
1304 DESTROY_IF(address
);
1307 METHOD(stroke_list_t
, destroy
, void,
1308 private_stroke_list_t
*this)
1316 stroke_list_t
*stroke_list_create(stroke_attribute_t
*attribute
)
1318 private_stroke_list_t
*this;
1326 .destroy
= _destroy
,
1328 .uptime
= time_monotonic(NULL
),
1329 .attribute
= attribute
,
1332 return &this->public;