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"
20 #include <sys/utsname.h>
24 #endif /* HAVE_MALLINFO */
28 #include <collections/linked_list.h>
29 #include <plugins/plugin.h>
30 #include <credentials/certificates/x509.h>
31 #include <credentials/certificates/ac.h>
32 #include <credentials/certificates/crl.h>
33 #include <credentials/certificates/pgp_certificate.h>
34 #include <credentials/ietf_attributes/ietf_attributes.h>
35 #include <config/peer_cfg.h>
37 /* warning intervals for list functions */
38 #define CERT_WARNING_INTERVAL 30 /* days */
39 #define CRL_WARNING_INTERVAL 7 /* days */
40 #define AC_WARNING_INTERVAL 1 /* day */
42 typedef struct private_stroke_list_t private_stroke_list_t
;
45 * private data of stroke_list
47 struct private_stroke_list_t
{
55 * Kind of *swan we run
60 * timestamp of daemon start
65 * strokes attribute provider
67 stroke_attribute_t
*attribute
;
71 * Log tasks of a specific queue to out
73 static void log_task_q(FILE *out
, ike_sa_t
*ike_sa
, task_queue_t q
, char *name
)
75 enumerator_t
*enumerator
;
79 enumerator
= ike_sa
->create_task_enumerator(ike_sa
, q
);
80 while (enumerator
->enumerate(enumerator
, &task
))
84 fprintf(out
, "%12s[%d]: Tasks %s: ", ike_sa
->get_name(ike_sa
),
85 ike_sa
->get_unique_id(ike_sa
), name
);
88 fprintf(out
, "%N ", task_type_names
, task
->get_type(task
));
90 enumerator
->destroy(enumerator
);
98 * log an IKE_SA to out
100 static void log_ike_sa(FILE *out
, ike_sa_t
*ike_sa
, bool all
)
102 ike_sa_id_t
*id
= ike_sa
->get_id(ike_sa
);
103 time_t now
= time_monotonic(NULL
);
105 fprintf(out
, "%12s[%d]: %N",
106 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
),
107 ike_sa_state_names
, ike_sa
->get_state(ike_sa
));
109 if (ike_sa
->get_state(ike_sa
) == IKE_ESTABLISHED
)
113 established
= ike_sa
->get_statistic(ike_sa
, STAT_ESTABLISHED
);
114 fprintf(out
, " %V ago", &now
, &established
);
117 fprintf(out
, ", %H[%Y]...%H[%Y]\n",
118 ike_sa
->get_my_host(ike_sa
), ike_sa
->get_my_id(ike_sa
),
119 ike_sa
->get_other_host(ike_sa
), ike_sa
->get_other_id(ike_sa
));
123 proposal_t
*ike_proposal
;
124 identification_t
*eap_id
;
126 eap_id
= ike_sa
->get_other_eap_id(ike_sa
);
128 if (!eap_id
->equals(eap_id
, ike_sa
->get_other_id(ike_sa
)))
130 fprintf(out
, "%12s[%d]: Remote %s identity: %Y\n",
131 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
),
132 ike_sa
->get_version(ike_sa
) == IKEV1 ?
"XAuth" : "EAP",
136 ike_proposal
= ike_sa
->get_proposal(ike_sa
);
138 fprintf(out
, "%12s[%d]: %N SPIs: %.16"PRIx64
"_i%s %.16"PRIx64
"_r%s",
139 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
),
140 ike_version_names
, ike_sa
->get_version(ike_sa
),
141 id
->get_initiator_spi(id
), id
->is_initiator(id
) ?
"*" : "",
142 id
->get_responder_spi(id
), id
->is_initiator(id
) ?
"" : "*");
145 if (ike_sa
->get_state(ike_sa
) == IKE_ESTABLISHED
)
147 time_t rekey
, reauth
;
148 peer_cfg_t
*peer_cfg
;
150 rekey
= ike_sa
->get_statistic(ike_sa
, STAT_REKEY
);
151 reauth
= ike_sa
->get_statistic(ike_sa
, STAT_REAUTH
);
152 peer_cfg
= ike_sa
->get_peer_cfg(ike_sa
);
156 fprintf(out
, ", rekeying in %V", &rekey
, &now
);
161 enumerator_t
*enumerator
;
165 enumerator
= peer_cfg
->create_auth_cfg_enumerator(peer_cfg
, TRUE
);
166 while (enumerator
->enumerate(enumerator
, &auth
))
173 fprintf(out
, "%N", auth_class_names
,
174 auth
->get(auth
, AUTH_RULE_AUTH_CLASS
));
176 enumerator
->destroy(enumerator
);
177 fprintf(out
, " reauthentication in %V", &reauth
, &now
);
179 if (!rekey
&& !reauth
)
181 fprintf(out
, ", rekeying disabled");
190 snprintf(buf
, BUF_LEN
, "%P", ike_proposal
);
191 fprintf(out
, "%12s[%d]: IKE proposal: %s\n",
192 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
),
196 log_task_q(out
, ike_sa
, TASK_QUEUE_QUEUED
, "queued");
197 log_task_q(out
, ike_sa
, TASK_QUEUE_ACTIVE
, "active");
198 log_task_q(out
, ike_sa
, TASK_QUEUE_PASSIVE
, "passive");
203 * log an CHILD_SA to out
205 static void log_child_sa(FILE *out
, child_sa_t
*child_sa
, bool all
)
207 time_t use_in
, use_out
, rekey
, now
;
208 u_int64_t bytes_in
, bytes_out
, packets_in
, packets_out
;
209 proposal_t
*proposal
;
210 child_cfg_t
*config
= child_sa
->get_config(child_sa
);
212 now
= time_monotonic(NULL
);
214 fprintf(out
, "%12s{%d}: %N, %N%s",
215 child_sa
->get_name(child_sa
), child_sa
->get_reqid(child_sa
),
216 child_sa_state_names
, child_sa
->get_state(child_sa
),
217 ipsec_mode_names
, child_sa
->get_mode(child_sa
),
218 config
->use_proxy_mode(config
) ?
"_PROXY" : "");
220 if (child_sa
->get_state(child_sa
) == CHILD_INSTALLED
)
222 fprintf(out
, ", %N%s SPIs: %.8x_i %.8x_o",
223 protocol_id_names
, child_sa
->get_protocol(child_sa
),
224 child_sa
->has_encap(child_sa
) ?
" in UDP" : "",
225 ntohl(child_sa
->get_spi(child_sa
, TRUE
)),
226 ntohl(child_sa
->get_spi(child_sa
, FALSE
)));
228 if (child_sa
->get_ipcomp(child_sa
) != IPCOMP_NONE
)
230 fprintf(out
, ", IPCOMP CPIs: %.4x_i %.4x_o",
231 ntohs(child_sa
->get_cpi(child_sa
, TRUE
)),
232 ntohs(child_sa
->get_cpi(child_sa
, FALSE
)));
237 fprintf(out
, "\n%12s{%d}: ", child_sa
->get_name(child_sa
),
238 child_sa
->get_reqid(child_sa
));
240 proposal
= child_sa
->get_proposal(child_sa
);
243 u_int16_t encr_alg
= ENCR_UNDEFINED
, int_alg
= AUTH_UNDEFINED
;
244 u_int16_t encr_size
= 0, int_size
= 0;
245 u_int16_t esn
= NO_EXT_SEQ_NUMBERS
;
247 proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
,
248 &encr_alg
, &encr_size
);
249 proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
,
250 &int_alg
, &int_size
);
251 proposal
->get_algorithm(proposal
, EXTENDED_SEQUENCE_NUMBERS
,
254 if (encr_alg
!= ENCR_UNDEFINED
)
256 fprintf(out
, "%N", encryption_algorithm_names
, encr_alg
);
259 fprintf(out
, "_%u", encr_size
);
262 if (int_alg
!= AUTH_UNDEFINED
)
264 fprintf(out
, "/%N", integrity_algorithm_names
, int_alg
);
267 fprintf(out
, "_%u", int_size
);
270 if (esn
== EXT_SEQ_NUMBERS
)
272 fprintf(out
, "/ESN");
276 child_sa
->get_usestats(child_sa
, TRUE
,
277 &use_in
, &bytes_in
, &packets_in
);
278 fprintf(out
, ", %" PRIu64
" bytes_i", bytes_in
);
281 fprintf(out
, " (%" PRIu64
" pkt%s, %" PRIu64
"s ago)",
282 packets_in
, (packets_in
== 1) ?
"": "s",
283 (u_int64_t
)(now
- use_in
));
286 child_sa
->get_usestats(child_sa
, FALSE
,
287 &use_out
, &bytes_out
, &packets_out
);
288 fprintf(out
, ", %" PRIu64
" bytes_o", bytes_out
);
291 fprintf(out
, " (%" PRIu64
" pkt%s, %" PRIu64
"s ago)",
292 packets_out
, (packets_out
== 1) ?
"": "s",
293 (u_int64_t
)(now
- use_out
));
295 fprintf(out
, ", rekeying ");
297 rekey
= child_sa
->get_lifetime(child_sa
, FALSE
);
302 fprintf(out
, "active");
306 fprintf(out
, "in %V", &now
, &rekey
);
311 fprintf(out
, "disabled");
316 else if (child_sa
->get_state(child_sa
) == CHILD_REKEYING
)
318 rekey
= child_sa
->get_lifetime(child_sa
, TRUE
);
319 fprintf(out
, ", expires in %V", &now
, &rekey
);
322 fprintf(out
, "\n%12s{%d}: %#R=== %#R\n",
323 child_sa
->get_name(child_sa
), child_sa
->get_reqid(child_sa
),
324 child_sa
->get_traffic_selectors(child_sa
, TRUE
),
325 child_sa
->get_traffic_selectors(child_sa
, FALSE
));
329 * Log a configs local or remote authentication config to out
331 static void log_auth_cfgs(FILE *out
, peer_cfg_t
*peer_cfg
, bool local
)
333 enumerator_t
*enumerator
, *rules
;
336 auth_class_t auth_class
;
337 identification_t
*id
;
339 cert_validation_t valid
;
342 name
= peer_cfg
->get_name(peer_cfg
);
344 enumerator
= peer_cfg
->create_auth_cfg_enumerator(peer_cfg
, local
);
345 while (enumerator
->enumerate(enumerator
, &auth
))
347 fprintf(out
, "%12s: %s", name
, local ?
"local: " : "remote:");
348 id
= auth
->get(auth
, AUTH_RULE_IDENTITY
);
351 fprintf(out
, " [%Y]", id
);
353 fprintf(out
, " uses ");
355 auth_class
= (uintptr_t)auth
->get(auth
, AUTH_RULE_AUTH_CLASS
);
356 if (auth_class
== AUTH_CLASS_EAP
)
358 if ((uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_TYPE
) == EAP_NAK
)
360 fprintf(out
, "EAP authentication");
364 if ((uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_VENDOR
))
366 fprintf(out
, "EAP_%" PRIuPTR
"-%" PRIuPTR
" authentication",
367 (uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_TYPE
),
368 (uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_VENDOR
));
372 fprintf(out
, "%N authentication", eap_type_names
,
373 (uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_TYPE
));
376 id
= auth
->get(auth
, AUTH_RULE_EAP_IDENTITY
);
379 fprintf(out
, " with EAP identity '%Y'", id
);
383 else if (auth_class
== AUTH_CLASS_XAUTH
)
385 fprintf(out
, "%N authentication: %s", auth_class_names
, auth_class
,
386 auth
->get(auth
, AUTH_RULE_XAUTH_BACKEND
) ?
: "any");
387 id
= auth
->get(auth
, AUTH_RULE_XAUTH_IDENTITY
);
390 fprintf(out
, " with XAuth identity '%Y'", id
);
396 fprintf(out
, "%N authentication\n", auth_class_names
, auth_class
);
399 cert
= auth
->get(auth
, AUTH_RULE_CA_CERT
);
402 fprintf(out
, "%12s: ca: \"%Y\"\n", name
, cert
->get_subject(cert
));
405 cert
= auth
->get(auth
, AUTH_RULE_IM_CERT
);
408 fprintf(out
, "%12s: im-ca: \"%Y\"\n", name
, cert
->get_subject(cert
));
411 cert
= auth
->get(auth
, AUTH_RULE_SUBJECT_CERT
);
414 fprintf(out
, "%12s: cert: \"%Y\"\n", name
,
415 cert
->get_subject(cert
));
418 valid
= (uintptr_t)auth
->get(auth
, AUTH_RULE_OCSP_VALIDATION
);
419 if (valid
!= VALIDATION_FAILED
)
421 fprintf(out
, "%12s: ocsp: status must be GOOD%s\n", name
,
422 (valid
== VALIDATION_SKIPPED
) ?
" or SKIPPED" : "");
425 valid
= (uintptr_t)auth
->get(auth
, AUTH_RULE_CRL_VALIDATION
);
426 if (valid
!= VALIDATION_FAILED
)
428 fprintf(out
, "%12s: crl: status must be GOOD%s\n", name
,
429 (valid
== VALIDATION_SKIPPED
) ?
" or SKIPPED" : "");
432 rules
= auth
->create_enumerator(auth
);
433 while (rules
->enumerate(rules
, &rule
, &id
))
435 if (rule
== AUTH_RULE_GROUP
)
437 fprintf(out
, "%12s: group: %Y\n", name
, id
);
440 rules
->destroy(rules
);
442 enumerator
->destroy(enumerator
);
445 METHOD(stroke_list_t
, status
, void,
446 private_stroke_list_t
*this, stroke_msg_t
*msg
, FILE *out
,
449 enumerator_t
*enumerator
, *children
;
451 child_cfg_t
*child_cfg
;
452 child_sa_t
*child_sa
;
454 linked_list_t
*my_ts
, *other_ts
;
455 bool first
, found
= FALSE
;
456 char *name
= msg
->status
.name
;
461 peer_cfg_t
*peer_cfg
;
462 ike_version_t ike_version
;
467 u_int size
, online
, offline
, i
;
468 struct utsname utsname
;
470 now
= time_monotonic(NULL
);
471 since
= time(NULL
) - (now
- this->uptime
);
473 fprintf(out
, "Status of IKE charon daemon (%sSwan "VERSION
, this->swan
);
474 if (uname(&utsname
) == 0)
476 fprintf(out
, ", %s %s, %s",
477 utsname
.sysname
, utsname
.release
, utsname
.machine
);
479 fprintf(out
, "):\n uptime: %V, since %T\n", &now
, &this->uptime
, &since
,
483 struct mallinfo mi
= mallinfo();
485 fprintf(out
, " malloc: sbrk %d, mmap %d, used %d, free %d\n",
486 mi
.arena
, mi
.hblkhd
, mi
.uordblks
, mi
.fordblks
);
488 #endif /* HAVE_MALLINFO */
489 fprintf(out
, " worker threads: %d of %d idle, ",
490 lib
->processor
->get_idle_threads(lib
->processor
),
491 lib
->processor
->get_total_threads(lib
->processor
));
492 for (i
= 0; i
< JOB_PRIO_MAX
; i
++)
494 fprintf(out
, "%s%d", i
== 0 ?
"" : "/",
495 lib
->processor
->get_working_threads(lib
->processor
, i
));
497 fprintf(out
, " working, job queue: ");
498 for (i
= 0; i
< JOB_PRIO_MAX
; i
++)
500 fprintf(out
, "%s%d", i
== 0 ?
"" : "/",
501 lib
->processor
->get_job_load(lib
->processor
, i
));
503 fprintf(out
, ", scheduled: %d\n",
504 lib
->scheduler
->get_job_load(lib
->scheduler
));
505 fprintf(out
, " loaded plugins: %s\n",
506 lib
->plugins
->loaded_plugins(lib
->plugins
));
509 enumerator
= this->attribute
->create_pool_enumerator(this->attribute
);
510 while (enumerator
->enumerate(enumerator
, &pool
, &size
, &online
, &offline
))
512 if (name
&& !streq(name
, pool
))
519 fprintf(out
, "Virtual IP pools (size/online/offline):\n");
521 fprintf(out
, " %s: %u/%u/%u\n", pool
, size
, online
, offline
);
523 enumerator
->destroy(enumerator
);
525 enumerator
= hydra
->kernel_interface
->create_address_enumerator(
526 hydra
->kernel_interface
, ADDR_TYPE_REGULAR
);
527 fprintf(out
, "Listening IP addresses:\n");
528 while (enumerator
->enumerate(enumerator
, (void**)&host
))
530 fprintf(out
, " %H\n", host
);
532 enumerator
->destroy(enumerator
);
534 fprintf(out
, "Connections:\n");
535 enumerator
= charon
->backends
->create_peer_cfg_enumerator(
536 charon
->backends
, NULL
, NULL
, NULL
, NULL
, IKE_ANY
);
537 while (enumerator
->enumerate(enumerator
, &peer_cfg
))
539 char *my_addr
, *other_addr
;
540 bool my_allow_any
, other_allow_any
;
542 if (name
&& !streq(name
, peer_cfg
->get_name(peer_cfg
)))
547 ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
548 ike_version
= peer_cfg
->get_ike_version(peer_cfg
);
549 my_addr
= ike_cfg
->get_my_addr(ike_cfg
, &my_allow_any
);
550 other_addr
= ike_cfg
->get_other_addr(ike_cfg
, &other_allow_any
);
551 fprintf(out
, "%12s: %s%s...%s%s %N", peer_cfg
->get_name(peer_cfg
),
552 my_allow_any ?
"%":"", my_addr
,
553 other_allow_any ?
"%":"", other_addr
,
554 ike_version_names
, ike_version
);
556 if (ike_version
== IKEV1
&& peer_cfg
->use_aggressive(peer_cfg
))
558 fprintf(out
, " Aggressive");
561 dpd
= peer_cfg
->get_dpd(peer_cfg
);
564 fprintf(out
, ", dpddelay=%us", dpd
);
568 log_auth_cfgs(out
, peer_cfg
, TRUE
);
569 log_auth_cfgs(out
, peer_cfg
, FALSE
);
571 children
= peer_cfg
->create_child_cfg_enumerator(peer_cfg
);
572 while (children
->enumerate(children
, &child_cfg
))
574 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
, NULL
);
575 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
, NULL
);
576 fprintf(out
, "%12s: child: %#R=== %#R%N",
577 child_cfg
->get_name(child_cfg
), my_ts
, other_ts
,
578 ipsec_mode_names
, child_cfg
->get_mode(child_cfg
));
579 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
580 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
584 fprintf(out
, ", dpdaction=%N", action_names
,
585 child_cfg
->get_dpd_action(child_cfg
));
589 children
->destroy(children
);
591 enumerator
->destroy(enumerator
);
594 /* Enumerate shunt policies */
596 enumerator
= charon
->shunts
->create_enumerator(charon
->shunts
);
597 while (enumerator
->enumerate(enumerator
, &child_cfg
))
599 if (name
&& !streq(name
, child_cfg
->get_name(child_cfg
)))
605 fprintf(out
, "Shunted Connections:\n");
608 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
, NULL
);
609 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
, NULL
);
610 fprintf(out
, "%12s: %#R=== %#R%N\n",
611 child_cfg
->get_name(child_cfg
), my_ts
, other_ts
,
612 ipsec_mode_names
, child_cfg
->get_mode(child_cfg
));
613 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
614 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
616 enumerator
->destroy(enumerator
);
618 /* Enumerate traps */
620 enumerator
= charon
->traps
->create_enumerator(charon
->traps
);
621 while (enumerator
->enumerate(enumerator
, NULL
, &child_sa
))
623 if (name
&& !streq(name
, child_sa
->get_name(child_sa
)))
629 fprintf(out
, "Routed Connections:\n");
632 log_child_sa(out
, child_sa
, all
);
634 enumerator
->destroy(enumerator
);
636 half_open
= charon
->ike_sa_manager
->get_half_open_count(
637 charon
->ike_sa_manager
, NULL
);
638 fprintf(out
, "Security Associations (%u up, %u connecting):\n",
639 charon
->ike_sa_manager
->get_count(charon
->ike_sa_manager
) - half_open
,
641 enumerator
= charon
->controller
->create_ike_sa_enumerator(
642 charon
->controller
, wait
);
643 while (enumerator
->enumerate(enumerator
, &ike_sa
))
645 bool ike_printed
= FALSE
;
646 enumerator_t
*children
= ike_sa
->create_child_sa_enumerator(ike_sa
);
648 if (name
== NULL
|| streq(name
, ike_sa
->get_name(ike_sa
)))
650 log_ike_sa(out
, ike_sa
, all
);
655 while (children
->enumerate(children
, (void**)&child_sa
))
657 if (name
== NULL
|| streq(name
, child_sa
->get_name(child_sa
)))
661 log_ike_sa(out
, ike_sa
, all
);
665 log_child_sa(out
, child_sa
, all
);
668 children
->destroy(children
);
670 enumerator
->destroy(enumerator
);
676 fprintf(out
, " no match\n");
680 fprintf(out
, " none\n");
686 * create a unique certificate list without duplicates
687 * certicates having the same issuer are grouped together.
689 static linked_list_t
* create_unique_cert_list(certificate_type_t type
)
691 linked_list_t
*list
= linked_list_create();
692 enumerator_t
*enumerator
= lib
->credmgr
->create_cert_enumerator(
693 lib
->credmgr
, type
, KEY_ANY
, NULL
, FALSE
);
696 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
698 enumerator_t
*added
= list
->create_enumerator(list
);
699 identification_t
*issuer
= cert
->get_issuer(cert
);
700 bool previous_same
, same
= FALSE
, found
= FALSE
;
701 certificate_t
*list_cert
;
703 while (added
->enumerate(added
, (void**)&list_cert
))
705 if (list_cert
->equals(list_cert
, cert
))
706 { /* stop if we found a duplicate*/
710 previous_same
= same
;
711 same
= list_cert
->has_issuer(list_cert
, issuer
);
712 if (previous_same
&& !same
)
713 { /* group certificates with same issuer */
719 list
->insert_before(list
, added
, cert
->get_ref(cert
));
721 added
->destroy(added
);
723 enumerator
->destroy(enumerator
);
728 * Print a single public key.
730 static void list_public_key(public_key_t
*public, FILE *out
)
732 private_key_t
*private = NULL
;
734 identification_t
*id
;
736 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1
, &keyid
))
738 id
= identification_create_from_encoding(ID_KEY_ID
, keyid
);
739 private = lib
->credmgr
->get_private(lib
->credmgr
,
740 public->get_type(public), id
, NULL
);
744 fprintf(out
, " pubkey: %N %d bits%s\n",
745 key_type_names
, public->get_type(public),
746 public->get_keysize(public),
747 private ?
", has private key" : "");
748 if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1
, &keyid
))
750 fprintf(out
, " keyid: %#B\n", &keyid
);
752 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1
, &keyid
))
754 fprintf(out
, " subjkey: %#B\n", &keyid
);
760 * list all raw public keys
762 static void stroke_list_pubkeys(linked_list_t
*list
, bool utc
, FILE *out
)
765 time_t now
= time(NULL
), notBefore
, notAfter
;
766 enumerator_t
*enumerator
;
769 enumerator
= list
->create_enumerator(list
);
770 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
772 identification_t
*subject
= cert
->get_subject(cert
);
773 public_key_t
*public = cert
->get_public_key(cert
);
780 fprintf(out
, "List of Raw Public Keys:\n");
785 /* list subject if available */
786 if (subject
->get_type(subject
) != ID_KEY_ID
)
788 fprintf(out
, " subject: %#Y\n", subject
);
791 /* list validity if available*/
792 cert
->get_validity(cert
, &now
, ¬Before
, ¬After
);
793 if (notBefore
!= UNDEFINED_TIME
&& notAfter
!= UNDEFINED_TIME
)
795 fprintf(out
, " validity: not before %T, ", ¬Before
, utc
);
798 fprintf(out
, "not valid yet (valid in %V)\n", &now
, ¬Before
);
802 fprintf(out
, "ok\n");
804 fprintf(out
, " not after %T, ", ¬After
, utc
);
807 fprintf(out
, "expired (%V ago)\n", &now
, ¬After
);
812 if (now
> notAfter
- CERT_WARNING_INTERVAL
* 60 * 60 * 24)
814 fprintf(out
, " (expires in %V)", &now
, ¬After
);
820 list_public_key(public, out
);
821 public->destroy(public);
824 enumerator
->destroy(enumerator
);
828 * list OpenPGP certificates
830 static void stroke_list_pgp(linked_list_t
*list
,bool utc
, FILE *out
)
833 time_t now
= time(NULL
);
834 enumerator_t
*enumerator
= list
->create_enumerator(list
);
837 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
839 time_t created
, until
;
840 public_key_t
*public;
841 pgp_certificate_t
*pgp_cert
= (pgp_certificate_t
*)cert
;
842 chunk_t fingerprint
= pgp_cert
->get_fingerprint(pgp_cert
);
848 fprintf(out
, "List of PGP End Entity Certificates:\n");
852 fprintf(out
, " userid: '%Y'\n", cert
->get_subject(cert
));
854 fprintf(out
, " digest: %#B\n", &fingerprint
);
857 cert
->get_validity(cert
, &now
, &created
, &until
);
858 fprintf(out
, " created: %T\n", &created
, utc
);
859 fprintf(out
, " until: %T%s\n", &until
, utc
,
860 (until
== TIME_32_BIT_SIGNED_MAX
) ?
" (expires never)":"");
862 public = cert
->get_public_key(cert
);
865 list_public_key(public, out
);
866 public->destroy(public);
869 enumerator
->destroy(enumerator
);
873 * list all X.509 certificates matching the flags
875 static void stroke_list_certs(linked_list_t
*list
, char *label
,
876 x509_flag_t flags
, bool utc
, FILE *out
)
879 time_t now
= time(NULL
);
880 enumerator_t
*enumerator
;
882 x509_flag_t flag_mask
;
884 /* mask all auxiliary flags */
885 flag_mask
= ~(X509_SERVER_AUTH
| X509_CLIENT_AUTH
| X509_IKE_INTERMEDIATE
|
886 X509_SELF_SIGNED
| X509_IP_ADDR_BLOCKS
);
888 enumerator
= list
->create_enumerator(list
);
889 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
891 x509_t
*x509
= (x509_t
*)cert
;
892 x509_flag_t x509_flags
= x509
->get_flags(x509
) & flag_mask
;
894 /* list only if flag is set or flag == 0 */
895 if ((x509_flags
& flags
) || (x509_flags
== flags
))
897 enumerator_t
*enumerator
;
898 identification_t
*altName
;
899 bool first_altName
= TRUE
;
901 chunk_t serial
, authkey
;
902 time_t notBefore
, notAfter
;
903 public_key_t
*public;
908 fprintf(out
, "List of %s:\n", label
);
913 /* list subjectAltNames */
914 enumerator
= x509
->create_subjectAltName_enumerator(x509
);
915 while (enumerator
->enumerate(enumerator
, (void**)&altName
))
919 fprintf(out
, " altNames: ");
920 first_altName
= FALSE
;
926 fprintf(out
, "%Y", altName
);
932 enumerator
->destroy(enumerator
);
934 fprintf(out
, " subject: \"%Y\"\n", cert
->get_subject(cert
));
935 fprintf(out
, " issuer: \"%Y\"\n", cert
->get_issuer(cert
));
936 serial
= chunk_skip_zero(x509
->get_serial(x509
));
937 fprintf(out
, " serial: %#B\n", &serial
);
940 cert
->get_validity(cert
, &now
, ¬Before
, ¬After
);
941 fprintf(out
, " validity: not before %T, ", ¬Before
, utc
);
944 fprintf(out
, "not valid yet (valid in %V)\n", &now
, ¬Before
);
948 fprintf(out
, "ok\n");
950 fprintf(out
, " not after %T, ", ¬After
, utc
);
953 fprintf(out
, "expired (%V ago)\n", &now
, ¬After
);
958 if (now
> notAfter
- CERT_WARNING_INTERVAL
* 60 * 60 * 24)
960 fprintf(out
, " (expires in %V)", &now
, ¬After
);
965 public = cert
->get_public_key(cert
);
968 list_public_key(public, out
);
969 public->destroy(public);
972 /* list optional authorityKeyIdentifier */
973 authkey
= x509
->get_authKeyIdentifier(x509
);
976 fprintf(out
, " authkey: %#B\n", &authkey
);
979 /* list optional pathLenConstraint */
980 pathlen
= x509
->get_constraint(x509
, X509_PATH_LEN
);
981 if (pathlen
!= X509_NO_CONSTRAINT
)
983 fprintf(out
, " pathlen: %u\n", pathlen
);
986 /* list optional ipAddrBlocks */
987 if (x509
->get_flags(x509
) & X509_IP_ADDR_BLOCKS
)
989 traffic_selector_t
*ipAddrBlock
;
990 bool first_ipAddrBlock
= TRUE
;
992 fprintf(out
, " addresses: ");
993 enumerator
= x509
->create_ipAddrBlock_enumerator(x509
);
994 while (enumerator
->enumerate(enumerator
, &ipAddrBlock
))
996 if (first_ipAddrBlock
)
998 first_ipAddrBlock
= FALSE
;
1004 fprintf(out
, "%R", ipAddrBlock
);
1006 enumerator
->destroy(enumerator
);
1011 enumerator
->destroy(enumerator
);
1015 * list all X.509 attribute certificates
1017 static void stroke_list_acerts(linked_list_t
*list
, bool utc
, FILE *out
)
1020 time_t thisUpdate
, nextUpdate
, now
= time(NULL
);
1021 enumerator_t
*enumerator
= list
->create_enumerator(list
);
1022 certificate_t
*cert
;
1024 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
1026 ac_t
*ac
= (ac_t
*)cert
;
1027 identification_t
*id
;
1028 ietf_attributes_t
*groups
;
1034 fprintf(out
, "List of X.509 Attribute Certificates:\n");
1039 id
= cert
->get_subject(cert
);
1042 fprintf(out
, " holder: \"%Y\"\n", id
);
1044 id
= ac
->get_holderIssuer(ac
);
1047 fprintf(out
, " hissuer: \"%Y\"\n", id
);
1049 chunk
= chunk_skip_zero(ac
->get_holderSerial(ac
));
1052 fprintf(out
, " hserial: %#B\n", &chunk
);
1054 groups
= ac
->get_groups(ac
);
1057 fprintf(out
, " groups: %s\n", groups
->get_string(groups
));
1058 groups
->destroy(groups
);
1060 fprintf(out
, " issuer: \"%Y\"\n", cert
->get_issuer(cert
));
1061 chunk
= chunk_skip_zero(ac
->get_serial(ac
));
1062 fprintf(out
, " serial: %#B\n", &chunk
);
1065 cert
->get_validity(cert
, &now
, &thisUpdate
, &nextUpdate
);
1066 fprintf(out
, " updates: this %T\n", &thisUpdate
, utc
);
1067 fprintf(out
, " next %T, ", &nextUpdate
, utc
);
1068 if (now
> nextUpdate
)
1070 fprintf(out
, "expired (%V ago)\n", &now
, &nextUpdate
);
1075 if (now
> nextUpdate
- AC_WARNING_INTERVAL
* 60 * 60 * 24)
1077 fprintf(out
, " (expires in %V)", &now
, &nextUpdate
);
1079 fprintf(out
, " \n");
1082 /* list optional authorityKeyIdentifier */
1083 chunk
= ac
->get_authKeyIdentifier(ac
);
1086 fprintf(out
, " authkey: %#B\n", &chunk
);
1089 enumerator
->destroy(enumerator
);
1093 * list all X.509 CRLs
1095 static void stroke_list_crls(linked_list_t
*list
, bool utc
, FILE *out
)
1098 time_t thisUpdate
, nextUpdate
, now
= time(NULL
);
1099 enumerator_t
*enumerator
= list
->create_enumerator(list
);
1100 certificate_t
*cert
;
1102 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
1104 crl_t
*crl
= (crl_t
*)cert
;
1110 fprintf(out
, "List of X.509 CRLs:\n");
1115 fprintf(out
, " issuer: \"%Y\"\n", cert
->get_issuer(cert
));
1117 /* list optional crlNumber */
1118 chunk
= chunk_skip_zero(crl
->get_serial(crl
));
1121 fprintf(out
, " serial: %#B\n", &chunk
);
1123 if (crl
->is_delta_crl(crl
, &chunk
))
1125 chunk
= chunk_skip_zero(chunk
);
1126 fprintf(out
, " delta for: %#B\n", &chunk
);
1129 /* count the number of revoked certificates */
1132 enumerator_t
*enumerator
= crl
->create_enumerator(crl
);
1134 while (enumerator
->enumerate(enumerator
, NULL
, NULL
, NULL
))
1138 fprintf(out
, " revoked: %d certificate%s\n", count
,
1139 (count
== 1)?
"" : "s");
1140 enumerator
->destroy(enumerator
);
1144 cert
->get_validity(cert
, &now
, &thisUpdate
, &nextUpdate
);
1145 fprintf(out
, " updates: this %T\n", &thisUpdate
, utc
);
1146 fprintf(out
, " next %T, ", &nextUpdate
, utc
);
1147 if (now
> nextUpdate
)
1149 fprintf(out
, "expired (%V ago)\n", &now
, &nextUpdate
);
1154 if (now
> nextUpdate
- CRL_WARNING_INTERVAL
* 60 * 60 * 24)
1156 fprintf(out
, " (expires in %V)", &now
, &nextUpdate
);
1158 fprintf(out
, " \n");
1161 /* list optional authorityKeyIdentifier */
1162 chunk
= crl
->get_authKeyIdentifier(crl
);
1165 fprintf(out
, " authkey: %#B\n", &chunk
);
1168 enumerator
->destroy(enumerator
);
1172 * list all OCSP responses
1174 static void stroke_list_ocsp(linked_list_t
* list
, bool utc
, FILE *out
)
1176 bool first
= TRUE
, ok
;
1177 enumerator_t
*enumerator
= list
->create_enumerator(list
);
1178 certificate_t
*cert
;
1179 time_t produced
, usable
, now
= time(NULL
);
1181 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
1186 fprintf(out
, "List of OCSP responses:\n");
1190 fprintf(out
, " signer: \"%Y\"\n", cert
->get_issuer(cert
));
1192 /* check validity */
1193 ok
= cert
->get_validity(cert
, &now
, &produced
, &usable
);
1194 fprintf(out
, " validity: produced at %T\n", &produced
, utc
);
1195 fprintf(out
, " usable till %T, ", &usable
, utc
);
1198 fprintf(out
, "ok\n");
1202 fprintf(out
, "expired (%V ago)\n", &now
, &usable
);
1205 enumerator
->destroy(enumerator
);
1209 * Print the name of an algorithm plus the name of the plugin that registered it
1211 static void print_alg(FILE *out
, int *len
, enum_name_t
*alg_names
, int alg_type
,
1212 const char *plugin_name
)
1214 char alg_name
[BUF_LEN
];
1219 alg_name_len
= sprintf(alg_name
, " %N[%s]", alg_names
, alg_type
,
1224 alg_name_len
= sprintf(alg_name
, " [%s]", plugin_name
);
1226 if (*len
+ alg_name_len
> CRYPTO_MAX_ALG_LINE
)
1228 fprintf(out
, "\n ");
1231 fprintf(out
, "%s", alg_name
);
1232 *len
+= alg_name_len
;
1236 * List of registered cryptographical algorithms
1238 static void list_algs(FILE *out
)
1240 enumerator_t
*enumerator
;
1241 encryption_algorithm_t encryption
;
1242 integrity_algorithm_t integrity
;
1243 hash_algorithm_t hash
;
1244 pseudo_random_function_t prf
;
1245 diffie_hellman_group_t group
;
1246 rng_quality_t quality
;
1247 const char *plugin_name
;
1251 fprintf(out
, "List of registered IKE algorithms:\n");
1252 fprintf(out
, "\n encryption:");
1254 enumerator
= lib
->crypto
->create_crypter_enumerator(lib
->crypto
);
1255 while (enumerator
->enumerate(enumerator
, &encryption
, &plugin_name
))
1257 print_alg(out
, &len
, encryption_algorithm_names
, encryption
, plugin_name
);
1259 enumerator
->destroy(enumerator
);
1260 fprintf(out
, "\n integrity: ");
1262 enumerator
= lib
->crypto
->create_signer_enumerator(lib
->crypto
);
1263 while (enumerator
->enumerate(enumerator
, &integrity
, &plugin_name
))
1265 print_alg(out
, &len
, integrity_algorithm_names
, integrity
, plugin_name
);
1267 enumerator
->destroy(enumerator
);
1268 fprintf(out
, "\n aead: ");
1270 enumerator
= lib
->crypto
->create_aead_enumerator(lib
->crypto
);
1271 while (enumerator
->enumerate(enumerator
, &encryption
, &plugin_name
))
1273 print_alg(out
, &len
, encryption_algorithm_names
, encryption
, plugin_name
);
1275 enumerator
->destroy(enumerator
);
1276 fprintf(out
, "\n hasher: ");
1278 enumerator
= lib
->crypto
->create_hasher_enumerator(lib
->crypto
);
1279 while (enumerator
->enumerate(enumerator
, &hash
, &plugin_name
))
1281 print_alg(out
, &len
, hash_algorithm_names
, hash
, plugin_name
);
1283 enumerator
->destroy(enumerator
);
1284 fprintf(out
, "\n prf: ");
1286 enumerator
= lib
->crypto
->create_prf_enumerator(lib
->crypto
);
1287 while (enumerator
->enumerate(enumerator
, &prf
, &plugin_name
))
1289 print_alg(out
, &len
, pseudo_random_function_names
, prf
, plugin_name
);
1291 enumerator
->destroy(enumerator
);
1292 fprintf(out
, "\n dh-group: ");
1294 enumerator
= lib
->crypto
->create_dh_enumerator(lib
->crypto
);
1295 while (enumerator
->enumerate(enumerator
, &group
, &plugin_name
))
1297 print_alg(out
, &len
, diffie_hellman_group_names
, group
, plugin_name
);
1299 enumerator
->destroy(enumerator
);
1300 fprintf(out
, "\n random-gen:");
1302 enumerator
= lib
->crypto
->create_rng_enumerator(lib
->crypto
);
1303 while (enumerator
->enumerate(enumerator
, &quality
, &plugin_name
))
1305 print_alg(out
, &len
, rng_quality_names
, quality
, plugin_name
);
1307 enumerator
->destroy(enumerator
);
1308 fprintf(out
, "\n nonce-gen: ");
1310 enumerator
= lib
->crypto
->create_nonce_gen_enumerator(lib
->crypto
);
1311 while (enumerator
->enumerate(enumerator
, &plugin_name
))
1313 print_alg(out
, &len
, NULL
, 0, plugin_name
);
1315 enumerator
->destroy(enumerator
);
1320 * List loaded plugin information
1322 static void list_plugins(FILE *out
)
1324 plugin_feature_t
*features
, *fp
;
1325 enumerator_t
*enumerator
;
1326 linked_list_t
*list
;
1333 fprintf(out
, "List of loaded Plugins:\n");
1336 enumerator
= lib
->plugins
->create_plugin_enumerator(lib
->plugins
);
1337 while (enumerator
->enumerate(enumerator
, &plugin
, &list
))
1339 fprintf(out
, "%s:\n", plugin
->get_name(plugin
));
1340 if (plugin
->get_features
)
1342 count
= plugin
->get_features(plugin
, &features
);
1343 for (i
= 0; i
< count
; i
++)
1345 str
= plugin_feature_get_string(&features
[i
]);
1346 switch (features
[i
].kind
)
1348 case FEATURE_PROVIDE
:
1350 loaded
= list
->find_first(list
, NULL
,
1351 (void**)&fp
) == SUCCESS
;
1352 fprintf(out
, " %s%s\n",
1353 str
, loaded ?
"" : " (not loaded)");
1355 case FEATURE_DEPENDS
:
1356 fprintf(out
, " %s\n", str
);
1358 case FEATURE_SDEPEND
:
1359 fprintf(out
, " %s (soft)\n", str
);
1367 list
->destroy(list
);
1369 enumerator
->destroy(enumerator
);
1372 METHOD(stroke_list_t
, list
, void,
1373 private_stroke_list_t
*this, stroke_msg_t
*msg
, FILE *out
)
1375 linked_list_t
*cert_list
= NULL
;
1377 if (msg
->list
.flags
& LIST_PUBKEYS
)
1379 linked_list_t
*pubkey_list
= create_unique_cert_list(CERT_TRUSTED_PUBKEY
);
1381 stroke_list_pubkeys(pubkey_list
, msg
->list
.utc
, out
);
1382 pubkey_list
->destroy_offset(pubkey_list
, offsetof(certificate_t
, destroy
));
1384 if (msg
->list
.flags
& LIST_CERTS
)
1386 linked_list_t
*pgp_list
= create_unique_cert_list(CERT_GPG
);
1388 stroke_list_pgp(pgp_list
, msg
->list
.utc
, out
);
1389 pgp_list
->destroy_offset(pgp_list
, offsetof(certificate_t
, destroy
));
1391 if (msg
->list
.flags
& (LIST_CERTS
| LIST_CACERTS
| LIST_OCSPCERTS
| LIST_AACERTS
))
1393 cert_list
= create_unique_cert_list(CERT_X509
);
1395 if (msg
->list
.flags
& LIST_CERTS
)
1397 stroke_list_certs(cert_list
, "X.509 End Entity Certificates",
1398 X509_NONE
, msg
->list
.utc
, out
);
1400 if (msg
->list
.flags
& LIST_CACERTS
)
1402 stroke_list_certs(cert_list
, "X.509 CA Certificates",
1403 X509_CA
, msg
->list
.utc
, out
);
1405 if (msg
->list
.flags
& LIST_OCSPCERTS
)
1407 stroke_list_certs(cert_list
, "X.509 OCSP Signer Certificates",
1408 X509_OCSP_SIGNER
, msg
->list
.utc
, out
);
1410 if (msg
->list
.flags
& LIST_AACERTS
)
1412 stroke_list_certs(cert_list
, "X.509 AA Certificates",
1413 X509_AA
, msg
->list
.utc
, out
);
1415 DESTROY_OFFSET_IF(cert_list
, offsetof(certificate_t
, destroy
));
1417 if (msg
->list
.flags
& LIST_ACERTS
)
1419 linked_list_t
*ac_list
= create_unique_cert_list(CERT_X509_AC
);
1421 stroke_list_acerts(ac_list
, msg
->list
.utc
, out
);
1422 ac_list
->destroy_offset(ac_list
, offsetof(certificate_t
, destroy
));
1424 if (msg
->list
.flags
& LIST_CRLS
)
1426 linked_list_t
*crl_list
= create_unique_cert_list(CERT_X509_CRL
);
1428 stroke_list_crls(crl_list
, msg
->list
.utc
, out
);
1429 crl_list
->destroy_offset(crl_list
, offsetof(certificate_t
, destroy
));
1431 if (msg
->list
.flags
& LIST_OCSP
)
1433 linked_list_t
*ocsp_list
= create_unique_cert_list(CERT_X509_OCSP_RESPONSE
);
1435 stroke_list_ocsp(ocsp_list
, msg
->list
.utc
, out
);
1437 ocsp_list
->destroy_offset(ocsp_list
, offsetof(certificate_t
, destroy
));
1439 if (msg
->list
.flags
& LIST_ALGS
)
1443 if (msg
->list
.flags
& LIST_PLUGINS
)
1450 * Print leases of a single pool
1452 static void pool_leases(private_stroke_list_t
*this, FILE *out
, char *pool
,
1453 host_t
*address
, u_int size
, u_int online
, u_int offline
)
1455 enumerator_t
*enumerator
;
1456 identification_t
*id
;
1461 fprintf(out
, "Leases in pool '%s', usage: %u/%u, %u online\n",
1462 pool
, online
+ offline
, size
, online
);
1463 enumerator
= this->attribute
->create_lease_enumerator(this->attribute
, pool
);
1464 while (enumerator
&& enumerator
->enumerate(enumerator
, &id
, &lease
, &on
))
1466 if (!address
|| address
->ip_equals(address
, lease
))
1468 fprintf(out
, " %15H %s '%Y'\n",
1469 lease
, on ?
"online" : "offline", id
);
1473 enumerator
->destroy(enumerator
);
1476 fprintf(out
, " no matching leases found\n");
1480 METHOD(stroke_list_t
, leases
, void,
1481 private_stroke_list_t
*this, stroke_msg_t
*msg
, FILE *out
)
1483 enumerator_t
*enumerator
;
1484 u_int size
, offline
, online
;
1485 host_t
*address
= NULL
;
1489 if (msg
->leases
.address
)
1491 address
= host_create_from_string(msg
->leases
.address
, 0);
1494 enumerator
= this->attribute
->create_pool_enumerator(this->attribute
);
1495 while (enumerator
->enumerate(enumerator
, &pool
, &size
, &online
, &offline
))
1497 if (!msg
->leases
.pool
|| streq(msg
->leases
.pool
, pool
))
1499 pool_leases(this, out
, pool
, address
, size
, online
, offline
);
1503 enumerator
->destroy(enumerator
);
1506 if (msg
->leases
.pool
)
1508 fprintf(out
, "pool '%s' not found\n", msg
->leases
.pool
);
1512 fprintf(out
, "no pools found\n");
1515 DESTROY_IF(address
);
1518 METHOD(stroke_list_t
, destroy
, void,
1519 private_stroke_list_t
*this)
1527 stroke_list_t
*stroke_list_create(stroke_attribute_t
*attribute
)
1529 private_stroke_list_t
*this;
1536 .destroy
= _destroy
,
1538 .uptime
= time_monotonic(NULL
),
1540 .attribute
= attribute
,
1543 if (lib
->settings
->get_bool(lib
->settings
,
1544 "charon.i_dont_care_about_security_and_use_aggressive_mode_psk", FALSE
))
1546 this->swan
= "weak";
1549 return &this->public;