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
;
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
, &use_in
, &bytes_in
);
277 fprintf(out
, ", %" PRIu64
" bytes_i", bytes_in
);
280 fprintf(out
, " (%" PRIu64
"s ago)", (u_int64_t
)(now
- use_in
));
283 child_sa
->get_usestats(child_sa
, FALSE
, &use_out
, &bytes_out
);
284 fprintf(out
, ", %" PRIu64
" bytes_o", bytes_out
);
287 fprintf(out
, " (%" PRIu64
"s ago)", (u_int64_t
)(now
- use_out
));
289 fprintf(out
, ", rekeying ");
291 rekey
= child_sa
->get_lifetime(child_sa
, FALSE
);
296 fprintf(out
, "active");
300 fprintf(out
, "in %V", &now
, &rekey
);
305 fprintf(out
, "disabled");
310 else if (child_sa
->get_state(child_sa
) == CHILD_REKEYING
)
312 rekey
= child_sa
->get_lifetime(child_sa
, TRUE
);
313 fprintf(out
, ", expires in %V", &now
, &rekey
);
316 fprintf(out
, "\n%12s{%d}: %#R=== %#R\n",
317 child_sa
->get_name(child_sa
), child_sa
->get_reqid(child_sa
),
318 child_sa
->get_traffic_selectors(child_sa
, TRUE
),
319 child_sa
->get_traffic_selectors(child_sa
, FALSE
));
323 * Log a configs local or remote authentication config to out
325 static void log_auth_cfgs(FILE *out
, peer_cfg_t
*peer_cfg
, bool local
)
327 enumerator_t
*enumerator
, *rules
;
330 auth_class_t auth_class
;
331 identification_t
*id
;
333 cert_validation_t valid
;
336 name
= peer_cfg
->get_name(peer_cfg
);
338 enumerator
= peer_cfg
->create_auth_cfg_enumerator(peer_cfg
, local
);
339 while (enumerator
->enumerate(enumerator
, &auth
))
341 fprintf(out
, "%12s: %s", name
, local ?
"local: " : "remote:");
342 id
= auth
->get(auth
, AUTH_RULE_IDENTITY
);
345 fprintf(out
, " [%Y]", id
);
347 fprintf(out
, " uses ");
349 auth_class
= (uintptr_t)auth
->get(auth
, AUTH_RULE_AUTH_CLASS
);
350 if (auth_class
== AUTH_CLASS_EAP
)
352 if ((uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_TYPE
) == EAP_NAK
)
354 fprintf(out
, "EAP authentication");
358 if ((uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_VENDOR
))
360 fprintf(out
, "EAP_%" PRIuPTR
"-%" PRIuPTR
" authentication",
361 (uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_TYPE
),
362 (uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_VENDOR
));
366 fprintf(out
, "%N authentication", eap_type_names
,
367 (uintptr_t)auth
->get(auth
, AUTH_RULE_EAP_TYPE
));
370 id
= auth
->get(auth
, AUTH_RULE_EAP_IDENTITY
);
373 fprintf(out
, " with EAP identity '%Y'", id
);
377 else if (auth_class
== AUTH_CLASS_XAUTH
)
379 fprintf(out
, "%N authentication: %s", auth_class_names
, auth_class
,
380 auth
->get(auth
, AUTH_RULE_XAUTH_BACKEND
) ?
: "any");
381 id
= auth
->get(auth
, AUTH_RULE_XAUTH_IDENTITY
);
384 fprintf(out
, " with XAuth identity '%Y'", id
);
390 fprintf(out
, "%N authentication\n", auth_class_names
, auth_class
);
393 cert
= auth
->get(auth
, AUTH_RULE_CA_CERT
);
396 fprintf(out
, "%12s: ca: \"%Y\"\n", name
, cert
->get_subject(cert
));
399 cert
= auth
->get(auth
, AUTH_RULE_IM_CERT
);
402 fprintf(out
, "%12s: im-ca: \"%Y\"\n", name
, cert
->get_subject(cert
));
405 cert
= auth
->get(auth
, AUTH_RULE_SUBJECT_CERT
);
408 fprintf(out
, "%12s: cert: \"%Y\"\n", name
,
409 cert
->get_subject(cert
));
412 valid
= (uintptr_t)auth
->get(auth
, AUTH_RULE_OCSP_VALIDATION
);
413 if (valid
!= VALIDATION_FAILED
)
415 fprintf(out
, "%12s: ocsp: status must be GOOD%s\n", name
,
416 (valid
== VALIDATION_SKIPPED
) ?
" or SKIPPED" : "");
419 valid
= (uintptr_t)auth
->get(auth
, AUTH_RULE_CRL_VALIDATION
);
420 if (valid
!= VALIDATION_FAILED
)
422 fprintf(out
, "%12s: crl: status must be GOOD%s\n", name
,
423 (valid
== VALIDATION_SKIPPED
) ?
" or SKIPPED" : "");
426 rules
= auth
->create_enumerator(auth
);
427 while (rules
->enumerate(rules
, &rule
, &id
))
429 if (rule
== AUTH_RULE_GROUP
)
431 fprintf(out
, "%12s: group: %Y\n", name
, id
);
434 rules
->destroy(rules
);
436 enumerator
->destroy(enumerator
);
439 METHOD(stroke_list_t
, status
, void,
440 private_stroke_list_t
*this, stroke_msg_t
*msg
, FILE *out
,
443 enumerator_t
*enumerator
, *children
;
445 child_cfg_t
*child_cfg
;
446 child_sa_t
*child_sa
;
448 linked_list_t
*my_ts
, *other_ts
;
449 bool first
, found
= FALSE
;
450 char *name
= msg
->status
.name
;
455 peer_cfg_t
*peer_cfg
;
456 ike_version_t ike_version
;
461 u_int size
, online
, offline
, i
;
462 struct utsname utsname
;
464 now
= time_monotonic(NULL
);
465 since
= time(NULL
) - (now
- this->uptime
);
467 fprintf(out
, "Status of IKE charon daemon (%sSwan "VERSION
, this->swan
);
468 if (uname(&utsname
) == 0)
470 fprintf(out
, ", %s %s, %s",
471 utsname
.sysname
, utsname
.release
, utsname
.machine
);
473 fprintf(out
, "):\n uptime: %V, since %T\n", &now
, &this->uptime
, &since
,
477 struct mallinfo mi
= mallinfo();
479 fprintf(out
, " malloc: sbrk %d, mmap %d, used %d, free %d\n",
480 mi
.arena
, mi
.hblkhd
, mi
.uordblks
, mi
.fordblks
);
482 #endif /* HAVE_MALLINFO */
483 fprintf(out
, " worker threads: %d of %d idle, ",
484 lib
->processor
->get_idle_threads(lib
->processor
),
485 lib
->processor
->get_total_threads(lib
->processor
));
486 for (i
= 0; i
< JOB_PRIO_MAX
; i
++)
488 fprintf(out
, "%s%d", i
== 0 ?
"" : "/",
489 lib
->processor
->get_working_threads(lib
->processor
, i
));
491 fprintf(out
, " working, job queue: ");
492 for (i
= 0; i
< JOB_PRIO_MAX
; i
++)
494 fprintf(out
, "%s%d", i
== 0 ?
"" : "/",
495 lib
->processor
->get_job_load(lib
->processor
, i
));
497 fprintf(out
, ", scheduled: %d\n",
498 lib
->scheduler
->get_job_load(lib
->scheduler
));
499 fprintf(out
, " loaded plugins: %s\n",
500 lib
->plugins
->loaded_plugins(lib
->plugins
));
503 enumerator
= this->attribute
->create_pool_enumerator(this->attribute
);
504 while (enumerator
->enumerate(enumerator
, &pool
, &size
, &online
, &offline
))
506 if (name
&& !streq(name
, pool
))
513 fprintf(out
, "Virtual IP pools (size/online/offline):\n");
515 fprintf(out
, " %s: %u/%u/%u\n", pool
, size
, online
, offline
);
517 enumerator
->destroy(enumerator
);
519 enumerator
= hydra
->kernel_interface
->create_address_enumerator(
520 hydra
->kernel_interface
, ADDR_TYPE_REGULAR
);
521 fprintf(out
, "Listening IP addresses:\n");
522 while (enumerator
->enumerate(enumerator
, (void**)&host
))
524 fprintf(out
, " %H\n", host
);
526 enumerator
->destroy(enumerator
);
528 fprintf(out
, "Connections:\n");
529 enumerator
= charon
->backends
->create_peer_cfg_enumerator(
530 charon
->backends
, NULL
, NULL
, NULL
, NULL
, IKE_ANY
);
531 while (enumerator
->enumerate(enumerator
, &peer_cfg
))
533 char *my_addr
, *other_addr
;
534 bool my_allow_any
, other_allow_any
;
536 if (name
&& !streq(name
, peer_cfg
->get_name(peer_cfg
)))
541 ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
542 ike_version
= peer_cfg
->get_ike_version(peer_cfg
);
543 my_addr
= ike_cfg
->get_my_addr(ike_cfg
, &my_allow_any
);
544 other_addr
= ike_cfg
->get_other_addr(ike_cfg
, &other_allow_any
);
545 fprintf(out
, "%12s: %s%s...%s%s %N", peer_cfg
->get_name(peer_cfg
),
546 my_allow_any ?
"%":"", my_addr
,
547 other_allow_any ?
"%":"", other_addr
,
548 ike_version_names
, ike_version
);
550 if (ike_version
== IKEV1
&& peer_cfg
->use_aggressive(peer_cfg
))
552 fprintf(out
, " Aggressive");
555 dpd
= peer_cfg
->get_dpd(peer_cfg
);
558 fprintf(out
, ", dpddelay=%us", dpd
);
562 log_auth_cfgs(out
, peer_cfg
, TRUE
);
563 log_auth_cfgs(out
, peer_cfg
, FALSE
);
565 children
= peer_cfg
->create_child_cfg_enumerator(peer_cfg
);
566 while (children
->enumerate(children
, &child_cfg
))
568 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
, NULL
);
569 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
, NULL
);
570 fprintf(out
, "%12s: child: %#R=== %#R%N",
571 child_cfg
->get_name(child_cfg
), my_ts
, other_ts
,
572 ipsec_mode_names
, child_cfg
->get_mode(child_cfg
));
573 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
574 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
578 fprintf(out
, ", dpdaction=%N", action_names
,
579 child_cfg
->get_dpd_action(child_cfg
));
583 children
->destroy(children
);
585 enumerator
->destroy(enumerator
);
588 /* Enumerate shunt policies */
590 enumerator
= charon
->shunts
->create_enumerator(charon
->shunts
);
591 while (enumerator
->enumerate(enumerator
, &child_cfg
))
593 if (name
&& !streq(name
, child_cfg
->get_name(child_cfg
)))
599 fprintf(out
, "Shunted Connections:\n");
602 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
, NULL
);
603 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
, NULL
);
604 fprintf(out
, "%12s: %#R=== %#R%N\n",
605 child_cfg
->get_name(child_cfg
), my_ts
, other_ts
,
606 ipsec_mode_names
, child_cfg
->get_mode(child_cfg
));
607 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
608 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
610 enumerator
->destroy(enumerator
);
612 /* Enumerate traps */
614 enumerator
= charon
->traps
->create_enumerator(charon
->traps
);
615 while (enumerator
->enumerate(enumerator
, NULL
, &child_sa
))
617 if (name
&& !streq(name
, child_sa
->get_name(child_sa
)))
623 fprintf(out
, "Routed Connections:\n");
626 log_child_sa(out
, child_sa
, all
);
628 enumerator
->destroy(enumerator
);
630 half_open
= charon
->ike_sa_manager
->get_half_open_count(
631 charon
->ike_sa_manager
, NULL
);
632 fprintf(out
, "Security Associations (%u up, %u connecting):\n",
633 charon
->ike_sa_manager
->get_count(charon
->ike_sa_manager
) - half_open
,
635 enumerator
= charon
->controller
->create_ike_sa_enumerator(
636 charon
->controller
, wait
);
637 while (enumerator
->enumerate(enumerator
, &ike_sa
))
639 bool ike_printed
= FALSE
;
640 enumerator_t
*children
= ike_sa
->create_child_sa_enumerator(ike_sa
);
642 if (name
== NULL
|| streq(name
, ike_sa
->get_name(ike_sa
)))
644 log_ike_sa(out
, ike_sa
, all
);
649 while (children
->enumerate(children
, (void**)&child_sa
))
651 if (name
== NULL
|| streq(name
, child_sa
->get_name(child_sa
)))
655 log_ike_sa(out
, ike_sa
, all
);
659 log_child_sa(out
, child_sa
, all
);
662 children
->destroy(children
);
664 enumerator
->destroy(enumerator
);
670 fprintf(out
, " no match\n");
674 fprintf(out
, " none\n");
680 * create a unique certificate list without duplicates
681 * certicates having the same issuer are grouped together.
683 static linked_list_t
* create_unique_cert_list(certificate_type_t type
)
685 linked_list_t
*list
= linked_list_create();
686 enumerator_t
*enumerator
= lib
->credmgr
->create_cert_enumerator(
687 lib
->credmgr
, type
, KEY_ANY
, NULL
, FALSE
);
690 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
692 enumerator_t
*added
= list
->create_enumerator(list
);
693 identification_t
*issuer
= cert
->get_issuer(cert
);
694 bool previous_same
, same
= FALSE
, found
= FALSE
;
695 certificate_t
*list_cert
;
697 while (added
->enumerate(added
, (void**)&list_cert
))
699 if (list_cert
->equals(list_cert
, cert
))
700 { /* stop if we found a duplicate*/
704 previous_same
= same
;
705 same
= list_cert
->has_issuer(list_cert
, issuer
);
706 if (previous_same
&& !same
)
707 { /* group certificates with same issuer */
713 list
->insert_before(list
, added
, cert
->get_ref(cert
));
715 added
->destroy(added
);
717 enumerator
->destroy(enumerator
);
722 * Print a single public key.
724 static void list_public_key(public_key_t
*public, FILE *out
)
726 private_key_t
*private = NULL
;
728 identification_t
*id
;
730 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1
, &keyid
))
732 id
= identification_create_from_encoding(ID_KEY_ID
, keyid
);
733 private = lib
->credmgr
->get_private(lib
->credmgr
,
734 public->get_type(public), id
, NULL
);
738 fprintf(out
, " pubkey: %N %d bits%s\n",
739 key_type_names
, public->get_type(public),
740 public->get_keysize(public),
741 private ?
", has private key" : "");
742 if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1
, &keyid
))
744 fprintf(out
, " keyid: %#B\n", &keyid
);
746 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1
, &keyid
))
748 fprintf(out
, " subjkey: %#B\n", &keyid
);
754 * list all raw public keys
756 static void stroke_list_pubkeys(linked_list_t
*list
, bool utc
, FILE *out
)
759 time_t now
= time(NULL
), notBefore
, notAfter
;
760 enumerator_t
*enumerator
;
763 enumerator
= list
->create_enumerator(list
);
764 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
766 identification_t
*subject
= cert
->get_subject(cert
);
767 public_key_t
*public = cert
->get_public_key(cert
);
774 fprintf(out
, "List of Raw Public Keys:\n");
779 /* list subject if available */
780 if (subject
->get_type(subject
) != ID_KEY_ID
)
782 fprintf(out
, " subject: %#Y\n", subject
);
785 /* list validity if available*/
786 cert
->get_validity(cert
, &now
, ¬Before
, ¬After
);
787 if (notBefore
!= UNDEFINED_TIME
&& notAfter
!= UNDEFINED_TIME
)
789 fprintf(out
, " validity: not before %T, ", ¬Before
, utc
);
792 fprintf(out
, "not valid yet (valid in %V)\n", &now
, ¬Before
);
796 fprintf(out
, "ok\n");
798 fprintf(out
, " not after %T, ", ¬After
, utc
);
801 fprintf(out
, "expired (%V ago)\n", &now
, ¬After
);
806 if (now
> notAfter
- CERT_WARNING_INTERVAL
* 60 * 60 * 24)
808 fprintf(out
, " (expires in %V)", &now
, ¬After
);
814 list_public_key(public, out
);
815 public->destroy(public);
818 enumerator
->destroy(enumerator
);
822 * list OpenPGP certificates
824 static void stroke_list_pgp(linked_list_t
*list
,bool utc
, FILE *out
)
827 time_t now
= time(NULL
);
828 enumerator_t
*enumerator
= list
->create_enumerator(list
);
831 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
833 time_t created
, until
;
834 public_key_t
*public;
835 pgp_certificate_t
*pgp_cert
= (pgp_certificate_t
*)cert
;
836 chunk_t fingerprint
= pgp_cert
->get_fingerprint(pgp_cert
);
842 fprintf(out
, "List of PGP End Entity Certificates:\n");
846 fprintf(out
, " userid: '%Y'\n", cert
->get_subject(cert
));
848 fprintf(out
, " digest: %#B\n", &fingerprint
);
851 cert
->get_validity(cert
, &now
, &created
, &until
);
852 fprintf(out
, " created: %T\n", &created
, utc
);
853 fprintf(out
, " until: %T%s\n", &until
, utc
,
854 (until
== TIME_32_BIT_SIGNED_MAX
) ?
" (expires never)":"");
856 public = cert
->get_public_key(cert
);
859 list_public_key(public, out
);
860 public->destroy(public);
863 enumerator
->destroy(enumerator
);
867 * list all X.509 certificates matching the flags
869 static void stroke_list_certs(linked_list_t
*list
, char *label
,
870 x509_flag_t flags
, bool utc
, FILE *out
)
873 time_t now
= time(NULL
);
874 enumerator_t
*enumerator
;
876 x509_flag_t flag_mask
;
878 /* mask all auxiliary flags */
879 flag_mask
= ~(X509_SERVER_AUTH
| X509_CLIENT_AUTH
| X509_IKE_INTERMEDIATE
|
880 X509_SELF_SIGNED
| X509_IP_ADDR_BLOCKS
);
882 enumerator
= list
->create_enumerator(list
);
883 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
885 x509_t
*x509
= (x509_t
*)cert
;
886 x509_flag_t x509_flags
= x509
->get_flags(x509
) & flag_mask
;
888 /* list only if flag is set or flag == 0 */
889 if ((x509_flags
& flags
) || (x509_flags
== flags
))
891 enumerator_t
*enumerator
;
892 identification_t
*altName
;
893 bool first_altName
= TRUE
;
895 chunk_t serial
, authkey
;
896 time_t notBefore
, notAfter
;
897 public_key_t
*public;
902 fprintf(out
, "List of %s:\n", label
);
907 /* list subjectAltNames */
908 enumerator
= x509
->create_subjectAltName_enumerator(x509
);
909 while (enumerator
->enumerate(enumerator
, (void**)&altName
))
913 fprintf(out
, " altNames: ");
914 first_altName
= FALSE
;
920 fprintf(out
, "%Y", altName
);
926 enumerator
->destroy(enumerator
);
928 fprintf(out
, " subject: \"%Y\"\n", cert
->get_subject(cert
));
929 fprintf(out
, " issuer: \"%Y\"\n", cert
->get_issuer(cert
));
930 serial
= chunk_skip_zero(x509
->get_serial(x509
));
931 fprintf(out
, " serial: %#B\n", &serial
);
934 cert
->get_validity(cert
, &now
, ¬Before
, ¬After
);
935 fprintf(out
, " validity: not before %T, ", ¬Before
, utc
);
938 fprintf(out
, "not valid yet (valid in %V)\n", &now
, ¬Before
);
942 fprintf(out
, "ok\n");
944 fprintf(out
, " not after %T, ", ¬After
, utc
);
947 fprintf(out
, "expired (%V ago)\n", &now
, ¬After
);
952 if (now
> notAfter
- CERT_WARNING_INTERVAL
* 60 * 60 * 24)
954 fprintf(out
, " (expires in %V)", &now
, ¬After
);
959 public = cert
->get_public_key(cert
);
962 list_public_key(public, out
);
963 public->destroy(public);
966 /* list optional authorityKeyIdentifier */
967 authkey
= x509
->get_authKeyIdentifier(x509
);
970 fprintf(out
, " authkey: %#B\n", &authkey
);
973 /* list optional pathLenConstraint */
974 pathlen
= x509
->get_constraint(x509
, X509_PATH_LEN
);
975 if (pathlen
!= X509_NO_CONSTRAINT
)
977 fprintf(out
, " pathlen: %u\n", pathlen
);
980 /* list optional ipAddrBlocks */
981 if (x509
->get_flags(x509
) & X509_IP_ADDR_BLOCKS
)
983 traffic_selector_t
*ipAddrBlock
;
984 bool first_ipAddrBlock
= TRUE
;
986 fprintf(out
, " addresses: ");
987 enumerator
= x509
->create_ipAddrBlock_enumerator(x509
);
988 while (enumerator
->enumerate(enumerator
, &ipAddrBlock
))
990 if (first_ipAddrBlock
)
992 first_ipAddrBlock
= FALSE
;
998 fprintf(out
, "%R", ipAddrBlock
);
1000 enumerator
->destroy(enumerator
);
1005 enumerator
->destroy(enumerator
);
1009 * list all X.509 attribute certificates
1011 static void stroke_list_acerts(linked_list_t
*list
, bool utc
, FILE *out
)
1014 time_t thisUpdate
, nextUpdate
, now
= time(NULL
);
1015 enumerator_t
*enumerator
= list
->create_enumerator(list
);
1016 certificate_t
*cert
;
1018 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
1020 ac_t
*ac
= (ac_t
*)cert
;
1021 identification_t
*id
;
1022 ietf_attributes_t
*groups
;
1028 fprintf(out
, "List of X.509 Attribute Certificates:\n");
1033 id
= cert
->get_subject(cert
);
1036 fprintf(out
, " holder: \"%Y\"\n", id
);
1038 id
= ac
->get_holderIssuer(ac
);
1041 fprintf(out
, " hissuer: \"%Y\"\n", id
);
1043 chunk
= chunk_skip_zero(ac
->get_holderSerial(ac
));
1046 fprintf(out
, " hserial: %#B\n", &chunk
);
1048 groups
= ac
->get_groups(ac
);
1051 fprintf(out
, " groups: %s\n", groups
->get_string(groups
));
1052 groups
->destroy(groups
);
1054 fprintf(out
, " issuer: \"%Y\"\n", cert
->get_issuer(cert
));
1055 chunk
= chunk_skip_zero(ac
->get_serial(ac
));
1056 fprintf(out
, " serial: %#B\n", &chunk
);
1059 cert
->get_validity(cert
, &now
, &thisUpdate
, &nextUpdate
);
1060 fprintf(out
, " updates: this %T\n", &thisUpdate
, utc
);
1061 fprintf(out
, " next %T, ", &nextUpdate
, utc
);
1062 if (now
> nextUpdate
)
1064 fprintf(out
, "expired (%V ago)\n", &now
, &nextUpdate
);
1069 if (now
> nextUpdate
- AC_WARNING_INTERVAL
* 60 * 60 * 24)
1071 fprintf(out
, " (expires in %V)", &now
, &nextUpdate
);
1073 fprintf(out
, " \n");
1076 /* list optional authorityKeyIdentifier */
1077 chunk
= ac
->get_authKeyIdentifier(ac
);
1080 fprintf(out
, " authkey: %#B\n", &chunk
);
1083 enumerator
->destroy(enumerator
);
1087 * list all X.509 CRLs
1089 static void stroke_list_crls(linked_list_t
*list
, bool utc
, FILE *out
)
1092 time_t thisUpdate
, nextUpdate
, now
= time(NULL
);
1093 enumerator_t
*enumerator
= list
->create_enumerator(list
);
1094 certificate_t
*cert
;
1096 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
1098 crl_t
*crl
= (crl_t
*)cert
;
1104 fprintf(out
, "List of X.509 CRLs:\n");
1109 fprintf(out
, " issuer: \"%Y\"\n", cert
->get_issuer(cert
));
1111 /* list optional crlNumber */
1112 chunk
= chunk_skip_zero(crl
->get_serial(crl
));
1115 fprintf(out
, " serial: %#B\n", &chunk
);
1117 if (crl
->is_delta_crl(crl
, &chunk
))
1119 chunk
= chunk_skip_zero(chunk
);
1120 fprintf(out
, " delta for: %#B\n", &chunk
);
1123 /* count the number of revoked certificates */
1126 enumerator_t
*enumerator
= crl
->create_enumerator(crl
);
1128 while (enumerator
->enumerate(enumerator
, NULL
, NULL
, NULL
))
1132 fprintf(out
, " revoked: %d certificate%s\n", count
,
1133 (count
== 1)?
"" : "s");
1134 enumerator
->destroy(enumerator
);
1138 cert
->get_validity(cert
, &now
, &thisUpdate
, &nextUpdate
);
1139 fprintf(out
, " updates: this %T\n", &thisUpdate
, utc
);
1140 fprintf(out
, " next %T, ", &nextUpdate
, utc
);
1141 if (now
> nextUpdate
)
1143 fprintf(out
, "expired (%V ago)\n", &now
, &nextUpdate
);
1148 if (now
> nextUpdate
- CRL_WARNING_INTERVAL
* 60 * 60 * 24)
1150 fprintf(out
, " (expires in %V)", &now
, &nextUpdate
);
1152 fprintf(out
, " \n");
1155 /* list optional authorityKeyIdentifier */
1156 chunk
= crl
->get_authKeyIdentifier(crl
);
1159 fprintf(out
, " authkey: %#B\n", &chunk
);
1162 enumerator
->destroy(enumerator
);
1166 * list all OCSP responses
1168 static void stroke_list_ocsp(linked_list_t
* list
, bool utc
, FILE *out
)
1170 bool first
= TRUE
, ok
;
1171 enumerator_t
*enumerator
= list
->create_enumerator(list
);
1172 certificate_t
*cert
;
1173 time_t produced
, usable
, now
= time(NULL
);
1175 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
1180 fprintf(out
, "List of OCSP responses:\n");
1184 fprintf(out
, " signer: \"%Y\"\n", cert
->get_issuer(cert
));
1186 /* check validity */
1187 ok
= cert
->get_validity(cert
, &now
, &produced
, &usable
);
1188 fprintf(out
, " validity: produced at %T\n", &produced
, utc
);
1189 fprintf(out
, " usable till %T, ", &usable
, utc
);
1192 fprintf(out
, "ok\n");
1196 fprintf(out
, "expired (%V ago)\n", &now
, &usable
);
1199 enumerator
->destroy(enumerator
);
1203 * Print the name of an algorithm plus the name of the plugin that registered it
1205 static void print_alg(FILE *out
, int *len
, enum_name_t
*alg_names
, int alg_type
,
1206 const char *plugin_name
)
1208 char alg_name
[BUF_LEN
];
1213 alg_name_len
= sprintf(alg_name
, " %N[%s]", alg_names
, alg_type
,
1218 alg_name_len
= sprintf(alg_name
, " [%s]", plugin_name
);
1220 if (*len
+ alg_name_len
> CRYPTO_MAX_ALG_LINE
)
1222 fprintf(out
, "\n ");
1225 fprintf(out
, "%s", alg_name
);
1226 *len
+= alg_name_len
;
1230 * List of registered cryptographical algorithms
1232 static void list_algs(FILE *out
)
1234 enumerator_t
*enumerator
;
1235 encryption_algorithm_t encryption
;
1236 integrity_algorithm_t integrity
;
1237 hash_algorithm_t hash
;
1238 pseudo_random_function_t prf
;
1239 diffie_hellman_group_t group
;
1240 rng_quality_t quality
;
1241 const char *plugin_name
;
1245 fprintf(out
, "List of registered IKEv2 Algorithms:\n");
1246 fprintf(out
, "\n encryption:");
1248 enumerator
= lib
->crypto
->create_crypter_enumerator(lib
->crypto
);
1249 while (enumerator
->enumerate(enumerator
, &encryption
, &plugin_name
))
1251 print_alg(out
, &len
, encryption_algorithm_names
, encryption
, plugin_name
);
1253 enumerator
->destroy(enumerator
);
1254 fprintf(out
, "\n integrity: ");
1256 enumerator
= lib
->crypto
->create_signer_enumerator(lib
->crypto
);
1257 while (enumerator
->enumerate(enumerator
, &integrity
, &plugin_name
))
1259 print_alg(out
, &len
, integrity_algorithm_names
, integrity
, plugin_name
);
1261 enumerator
->destroy(enumerator
);
1262 fprintf(out
, "\n aead: ");
1264 enumerator
= lib
->crypto
->create_aead_enumerator(lib
->crypto
);
1265 while (enumerator
->enumerate(enumerator
, &encryption
, &plugin_name
))
1267 print_alg(out
, &len
, encryption_algorithm_names
, encryption
, plugin_name
);
1269 enumerator
->destroy(enumerator
);
1270 fprintf(out
, "\n hasher: ");
1272 enumerator
= lib
->crypto
->create_hasher_enumerator(lib
->crypto
);
1273 while (enumerator
->enumerate(enumerator
, &hash
, &plugin_name
))
1275 print_alg(out
, &len
, hash_algorithm_names
, hash
, plugin_name
);
1277 enumerator
->destroy(enumerator
);
1278 fprintf(out
, "\n prf: ");
1280 enumerator
= lib
->crypto
->create_prf_enumerator(lib
->crypto
);
1281 while (enumerator
->enumerate(enumerator
, &prf
, &plugin_name
))
1283 print_alg(out
, &len
, pseudo_random_function_names
, prf
, plugin_name
);
1285 enumerator
->destroy(enumerator
);
1286 fprintf(out
, "\n dh-group: ");
1288 enumerator
= lib
->crypto
->create_dh_enumerator(lib
->crypto
);
1289 while (enumerator
->enumerate(enumerator
, &group
, &plugin_name
))
1291 print_alg(out
, &len
, diffie_hellman_group_names
, group
, plugin_name
);
1293 enumerator
->destroy(enumerator
);
1294 fprintf(out
, "\n random-gen:");
1296 enumerator
= lib
->crypto
->create_rng_enumerator(lib
->crypto
);
1297 while (enumerator
->enumerate(enumerator
, &quality
, &plugin_name
))
1299 print_alg(out
, &len
, rng_quality_names
, quality
, plugin_name
);
1301 enumerator
->destroy(enumerator
);
1302 fprintf(out
, "\n nonce-gen: ");
1304 enumerator
= lib
->crypto
->create_nonce_gen_enumerator(lib
->crypto
);
1305 while (enumerator
->enumerate(enumerator
, &plugin_name
))
1307 print_alg(out
, &len
, NULL
, 0, plugin_name
);
1309 enumerator
->destroy(enumerator
);
1314 * List loaded plugin information
1316 static void list_plugins(FILE *out
)
1318 plugin_feature_t
*features
, *fp
;
1319 enumerator_t
*enumerator
;
1320 linked_list_t
*list
;
1327 fprintf(out
, "List of loaded Plugins:\n");
1330 enumerator
= lib
->plugins
->create_plugin_enumerator(lib
->plugins
);
1331 while (enumerator
->enumerate(enumerator
, &plugin
, &list
))
1333 fprintf(out
, "%s:\n", plugin
->get_name(plugin
));
1334 if (plugin
->get_features
)
1336 count
= plugin
->get_features(plugin
, &features
);
1337 for (i
= 0; i
< count
; i
++)
1339 str
= plugin_feature_get_string(&features
[i
]);
1340 switch (features
[i
].kind
)
1342 case FEATURE_PROVIDE
:
1344 loaded
= list
->find_first(list
, NULL
,
1345 (void**)&fp
) == SUCCESS
;
1346 fprintf(out
, " %s%s\n",
1347 str
, loaded ?
"" : " (not loaded)");
1349 case FEATURE_DEPENDS
:
1350 fprintf(out
, " %s\n", str
);
1352 case FEATURE_SDEPEND
:
1353 fprintf(out
, " %s (soft)\n", str
);
1362 enumerator
->destroy(enumerator
);
1365 METHOD(stroke_list_t
, list
, void,
1366 private_stroke_list_t
*this, stroke_msg_t
*msg
, FILE *out
)
1368 linked_list_t
*cert_list
= NULL
;
1370 if (msg
->list
.flags
& LIST_PUBKEYS
)
1372 linked_list_t
*pubkey_list
= create_unique_cert_list(CERT_TRUSTED_PUBKEY
);
1374 stroke_list_pubkeys(pubkey_list
, msg
->list
.utc
, out
);
1375 pubkey_list
->destroy_offset(pubkey_list
, offsetof(certificate_t
, destroy
));
1377 if (msg
->list
.flags
& LIST_CERTS
)
1379 linked_list_t
*pgp_list
= create_unique_cert_list(CERT_GPG
);
1381 stroke_list_pgp(pgp_list
, msg
->list
.utc
, out
);
1382 pgp_list
->destroy_offset(pgp_list
, offsetof(certificate_t
, destroy
));
1384 if (msg
->list
.flags
& (LIST_CERTS
| LIST_CACERTS
| LIST_OCSPCERTS
| LIST_AACERTS
))
1386 cert_list
= create_unique_cert_list(CERT_X509
);
1388 if (msg
->list
.flags
& LIST_CERTS
)
1390 stroke_list_certs(cert_list
, "X.509 End Entity Certificates",
1391 X509_NONE
, msg
->list
.utc
, out
);
1393 if (msg
->list
.flags
& LIST_CACERTS
)
1395 stroke_list_certs(cert_list
, "X.509 CA Certificates",
1396 X509_CA
, msg
->list
.utc
, out
);
1398 if (msg
->list
.flags
& LIST_OCSPCERTS
)
1400 stroke_list_certs(cert_list
, "X.509 OCSP Signer Certificates",
1401 X509_OCSP_SIGNER
, msg
->list
.utc
, out
);
1403 if (msg
->list
.flags
& LIST_AACERTS
)
1405 stroke_list_certs(cert_list
, "X.509 AA Certificates",
1406 X509_AA
, msg
->list
.utc
, out
);
1408 DESTROY_OFFSET_IF(cert_list
, offsetof(certificate_t
, destroy
));
1410 if (msg
->list
.flags
& LIST_ACERTS
)
1412 linked_list_t
*ac_list
= create_unique_cert_list(CERT_X509_AC
);
1414 stroke_list_acerts(ac_list
, msg
->list
.utc
, out
);
1415 ac_list
->destroy_offset(ac_list
, offsetof(certificate_t
, destroy
));
1417 if (msg
->list
.flags
& LIST_CRLS
)
1419 linked_list_t
*crl_list
= create_unique_cert_list(CERT_X509_CRL
);
1421 stroke_list_crls(crl_list
, msg
->list
.utc
, out
);
1422 crl_list
->destroy_offset(crl_list
, offsetof(certificate_t
, destroy
));
1424 if (msg
->list
.flags
& LIST_OCSP
)
1426 linked_list_t
*ocsp_list
= create_unique_cert_list(CERT_X509_OCSP_RESPONSE
);
1428 stroke_list_ocsp(ocsp_list
, msg
->list
.utc
, out
);
1430 ocsp_list
->destroy_offset(ocsp_list
, offsetof(certificate_t
, destroy
));
1432 if (msg
->list
.flags
& LIST_ALGS
)
1436 if (msg
->list
.flags
& LIST_PLUGINS
)
1443 * Print leases of a single pool
1445 static void pool_leases(private_stroke_list_t
*this, FILE *out
, char *pool
,
1446 host_t
*address
, u_int size
, u_int online
, u_int offline
)
1448 enumerator_t
*enumerator
;
1449 identification_t
*id
;
1454 fprintf(out
, "Leases in pool '%s', usage: %u/%u, %u online\n",
1455 pool
, online
+ offline
, size
, online
);
1456 enumerator
= this->attribute
->create_lease_enumerator(this->attribute
, pool
);
1457 while (enumerator
&& enumerator
->enumerate(enumerator
, &id
, &lease
, &on
))
1459 if (!address
|| address
->ip_equals(address
, lease
))
1461 fprintf(out
, " %15H %s '%Y'\n",
1462 lease
, on ?
"online" : "offline", id
);
1466 enumerator
->destroy(enumerator
);
1469 fprintf(out
, " no matching leases found\n");
1473 METHOD(stroke_list_t
, leases
, void,
1474 private_stroke_list_t
*this, stroke_msg_t
*msg
, FILE *out
)
1476 enumerator_t
*enumerator
;
1477 u_int size
, offline
, online
;
1478 host_t
*address
= NULL
;
1482 if (msg
->leases
.address
)
1484 address
= host_create_from_string(msg
->leases
.address
, 0);
1487 enumerator
= this->attribute
->create_pool_enumerator(this->attribute
);
1488 while (enumerator
->enumerate(enumerator
, &pool
, &size
, &online
, &offline
))
1490 if (!msg
->leases
.pool
|| streq(msg
->leases
.pool
, pool
))
1492 pool_leases(this, out
, pool
, address
, size
, online
, offline
);
1496 enumerator
->destroy(enumerator
);
1499 if (msg
->leases
.pool
)
1501 fprintf(out
, "pool '%s' not found\n", msg
->leases
.pool
);
1505 fprintf(out
, "no pools found\n");
1508 DESTROY_IF(address
);
1511 METHOD(stroke_list_t
, destroy
, void,
1512 private_stroke_list_t
*this)
1520 stroke_list_t
*stroke_list_create(stroke_attribute_t
*attribute
)
1522 private_stroke_list_t
*this;
1529 .destroy
= _destroy
,
1531 .uptime
= time_monotonic(NULL
),
1533 .attribute
= attribute
,
1536 if (lib
->settings
->get_bool(lib
->settings
,
1537 "charon.i_dont_care_about_security_and_use_aggressive_mode_psk", FALSE
))
1539 this->swan
= "weak";
1542 return &this->public;