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
18 #include "stroke_list.h"
21 #include <utils/linked_list.h>
22 #include <credentials/certificates/x509.h>
23 #include <credentials/certificates/crl.h>
25 /* warning intervals for list functions */
26 #define CERT_WARNING_INTERVAL 30 /* days */
27 #define CRL_WARNING_INTERVAL 7 /* days */
29 typedef struct private_stroke_list_t private_stroke_list_t
;
32 * private data of stroke_list
34 struct private_stroke_list_t
{
42 * timestamp of daemon start
48 * log an IKE_SA to out
50 static void log_ike_sa(FILE *out
, ike_sa_t
*ike_sa
, bool all
)
52 ike_sa_id_t
*id
= ike_sa
->get_id(ike_sa
);
53 u_int32_t rekey
, reauth
;
55 fprintf(out
, "%12s[%d]: %N, %H[%D]...%H[%D]\n",
56 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
),
57 ike_sa_state_names
, ike_sa
->get_state(ike_sa
),
58 ike_sa
->get_my_host(ike_sa
), ike_sa
->get_my_id(ike_sa
),
59 ike_sa
->get_other_host(ike_sa
), ike_sa
->get_other_id(ike_sa
));
63 fprintf(out
, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s",
64 ike_sa
->get_name(ike_sa
), ike_sa
->get_unique_id(ike_sa
),
65 id
->get_initiator_spi(id
), id
->is_initiator(id
) ?
"*" : "",
66 id
->get_responder_spi(id
), id
->is_initiator(id
) ?
"" : "*");
68 rekey
= ike_sa
->get_statistic(ike_sa
, STAT_REKEY_TIME
);
69 reauth
= ike_sa
->get_statistic(ike_sa
, STAT_REAUTH_TIME
);
72 fprintf(out
, ", rekeying in %V", &rekey
);
76 fprintf(out
, ", reauthentication in %V", &reauth
);
78 if (!rekey
&& !reauth
)
80 fprintf(out
, ", rekeying disabled");
87 * log an CHILD_SA to out
89 static void log_child_sa(FILE *out
, child_sa_t
*child_sa
, bool all
)
91 u_int32_t rekey
, now
= time(NULL
);
92 u_int32_t use_in
, use_out
, use_fwd
;
93 encryption_algorithm_t encr_alg
;
94 integrity_algorithm_t int_alg
;
95 size_t encr_len
, int_len
;
98 child_sa
->get_stats(child_sa
, &mode
, &encr_alg
, &encr_len
,
99 &int_alg
, &int_len
, &rekey
, &use_in
, &use_out
,
102 fprintf(out
, "%12s{%d}: %N, %N",
103 child_sa
->get_name(child_sa
), child_sa
->get_reqid(child_sa
),
104 child_sa_state_names
, child_sa
->get_state(child_sa
),
107 if (child_sa
->get_state(child_sa
) == CHILD_INSTALLED
)
109 fprintf(out
, ", %N SPIs: %.8x_i %.8x_o",
110 protocol_id_names
, child_sa
->get_protocol(child_sa
),
111 htonl(child_sa
->get_spi(child_sa
, TRUE
)),
112 htonl(child_sa
->get_spi(child_sa
, FALSE
)));
116 fprintf(out
, "\n%12s{%d}: ", child_sa
->get_name(child_sa
),
117 child_sa
->get_reqid(child_sa
));
120 if (child_sa
->get_protocol(child_sa
) == PROTO_ESP
)
122 fprintf(out
, "%N", encryption_algorithm_names
, encr_alg
);
126 fprintf(out
, "-%d", encr_len
);
131 fprintf(out
, "%N", integrity_algorithm_names
, int_alg
);
134 fprintf(out
, "-%d", int_len
);
136 fprintf(out
, ", rekeying ");
140 fprintf(out
, "in %#V", &now
, &rekey
);
144 fprintf(out
, "disabled");
147 fprintf(out
, ", last use: ");
148 use_in
= max(use_in
, use_fwd
);
151 fprintf(out
, "%ds_i ", now
- use_in
);
155 fprintf(out
, "no_i ");
159 fprintf(out
, "%ds_o ", now
- use_out
);
163 fprintf(out
, "no_o ");
168 fprintf(out
, "\n%12s{%d}: %#R=== %#R\n",
169 child_sa
->get_name(child_sa
), child_sa
->get_reqid(child_sa
),
170 child_sa
->get_traffic_selectors(child_sa
, TRUE
),
171 child_sa
->get_traffic_selectors(child_sa
, FALSE
));
175 * Implementation of stroke_list_t.status.
177 static void status(private_stroke_list_t
*this, stroke_msg_t
*msg
, FILE *out
, bool all
)
179 enumerator_t
*enumerator
, *children
;
180 iterator_t
*iterator
;
182 peer_cfg_t
*peer_cfg
;
184 child_cfg_t
*child_cfg
;
189 name
= msg
->status
.name
;
193 uptime
= time(NULL
) - this->uptime
;
194 fprintf(out
, "Performance:\n");
195 fprintf(out
, " uptime: %V, since %#T\n", &uptime
, &this->uptime
, FALSE
);
196 fprintf(out
, " worker threads: %d idle of %d,",
197 charon
->processor
->get_idle_threads(charon
->processor
),
198 charon
->processor
->get_total_threads(charon
->processor
));
199 fprintf(out
, " job queue load: %d,",
200 charon
->processor
->get_job_load(charon
->processor
));
201 fprintf(out
, " scheduled events: %d\n",
202 charon
->scheduler
->get_job_load(charon
->scheduler
));
203 iterator
= charon
->kernel_interface
->create_address_iterator(
204 charon
->kernel_interface
);
205 fprintf(out
, "Listening IP addresses:\n");
206 while (iterator
->iterate(iterator
, (void**)&host
))
208 fprintf(out
, " %H\n", host
);
210 iterator
->destroy(iterator
);
212 fprintf(out
, "Connections:\n");
213 enumerator
= charon
->backends
->create_peer_cfg_enumerator(charon
->backends
);
214 while (enumerator
->enumerate(enumerator
, (void**)&peer_cfg
))
216 if (peer_cfg
->get_ike_version(peer_cfg
) != 2 ||
217 (name
&& !streq(name
, peer_cfg
->get_name(peer_cfg
))))
222 ike_cfg
= peer_cfg
->get_ike_cfg(peer_cfg
);
223 fprintf(out
, "%12s: %H[%D]...%H[%D]\n", peer_cfg
->get_name(peer_cfg
),
224 ike_cfg
->get_my_host(ike_cfg
), peer_cfg
->get_my_id(peer_cfg
),
225 ike_cfg
->get_other_host(ike_cfg
), peer_cfg
->get_other_id(peer_cfg
));
226 /* TODO: list CAs and groups */
227 children
= peer_cfg
->create_child_cfg_enumerator(peer_cfg
);
228 while (children
->enumerate(children
, &child_cfg
))
230 linked_list_t
*my_ts
, *other_ts
;
231 my_ts
= child_cfg
->get_traffic_selectors(child_cfg
, TRUE
, NULL
, NULL
);
232 other_ts
= child_cfg
->get_traffic_selectors(child_cfg
, FALSE
, NULL
, NULL
);
233 fprintf(out
, "%12s: %#R=== %#R\n", child_cfg
->get_name(child_cfg
),
235 my_ts
->destroy_offset(my_ts
, offsetof(traffic_selector_t
, destroy
));
236 other_ts
->destroy_offset(other_ts
, offsetof(traffic_selector_t
, destroy
));
238 children
->destroy(children
);
240 enumerator
->destroy(enumerator
);
243 iterator
= charon
->ike_sa_manager
->create_iterator(charon
->ike_sa_manager
);
244 if (all
&& iterator
->get_count(iterator
) > 0)
246 fprintf(out
, "Security Associations:\n");
248 while (iterator
->iterate(iterator
, (void**)&ike_sa
))
250 bool ike_printed
= FALSE
;
251 child_sa_t
*child_sa
;
252 iterator_t
*children
= ike_sa
->create_child_sa_iterator(ike_sa
);
254 if (name
== NULL
|| streq(name
, ike_sa
->get_name(ike_sa
)))
256 log_ike_sa(out
, ike_sa
, all
);
260 while (children
->iterate(children
, (void**)&child_sa
))
262 if (name
== NULL
|| streq(name
, child_sa
->get_name(child_sa
)))
266 log_ike_sa(out
, ike_sa
, all
);
269 log_child_sa(out
, child_sa
, all
);
272 children
->destroy(children
);
274 iterator
->destroy(iterator
);
278 * create a unique certificate list without duplicates
280 static linked_list_t
* create_unique_cert_list(certificate_type_t type
)
282 linked_list_t
*list
= linked_list_create();
283 enumerator_t
*enumerator
= charon
->credentials
->create_cert_enumerator(
284 charon
->credentials
, type
, KEY_ANY
,
288 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
290 iterator_t
*iterator
= list
->create_iterator(list
, TRUE
);
291 identification_t
*issuer
= cert
->get_issuer(cert
);
292 bool previous_same
, same
= FALSE
, last
= TRUE
;
293 certificate_t
*list_cert
;
295 while (iterator
->iterate(iterator
, (void**)&list_cert
))
297 /* exit if we have a duplicate? */
298 if (list_cert
== cert
)
303 /* group certificates with same issuer */
304 previous_same
= same
;
305 same
= list_cert
->has_issuer(list_cert
, issuer
);
306 if (previous_same
&& !same
)
308 iterator
->insert_before(iterator
, (void *)cert
->get_ref(cert
));
313 iterator
->destroy(iterator
);
317 list
->insert_last(list
, (void *)cert
->get_ref(cert
));
320 enumerator
->destroy(enumerator
);
325 * list all X.509 certificates matching the flags
327 static void stroke_list_certs(linked_list_t
*list
, char *label
,
328 x509_flag_t flags
, bool utc
, FILE *out
)
331 time_t now
= time(NULL
);
332 enumerator_t
*enumerator
= list
->create_enumerator(list
);
335 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
337 x509_t
*x509
= (x509_t
*)cert
;
338 x509_flag_t x509_flags
= x509
->get_flags(x509
);
340 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
341 if ((x509_flags
& flags
) || (flags
== (x509_flags
& ~X509_SELF_SIGNED
)))
343 enumerator_t
*enumerator
;
344 identification_t
*altName
;
345 bool first_altName
= TRUE
;
346 chunk_t serial
= x509
->get_serial(x509
);
347 identification_t
*authkey
= x509
->get_authKeyIdentifier(x509
);
348 time_t notBefore
, notAfter
;
349 public_key_t
*public = cert
->get_public_key(cert
);
354 fprintf(out
, "List of %s:\n", label
);
359 /* list subjectAltNames */
360 enumerator
= x509
->create_subjectAltName_enumerator(x509
);
361 while (enumerator
->enumerate(enumerator
, (void**)&altName
))
365 fprintf(out
, " altNames: ");
366 first_altName
= FALSE
;
372 fprintf(out
, "%D", altName
);
378 enumerator
->destroy(enumerator
);
380 fprintf(out
, " subject: \"%D\"\n", cert
->get_subject(cert
));
381 fprintf(out
, " issuer: \"%D\"\n", cert
->get_issuer(cert
));
382 fprintf(out
, " serial: %#B\n", &serial
);
385 cert
->get_validity(cert
, &now
, ¬Before
, ¬After
);
386 fprintf(out
, " validity: not before %#T, ", ¬Before
, utc
);
389 fprintf(out
, "not valid yet (valid in %#V)\n", &now
, ¬Before
);
393 fprintf(out
, "ok\n");
395 fprintf(out
, " not after %#T, ", ¬After
, utc
);
398 fprintf(out
, "expired (%#V ago)\n", &now
, ¬After
);
403 if (now
> notAfter
- CERT_WARNING_INTERVAL
* 60 * 60 * 24)
405 fprintf(out
, " (expires in %#V)", &now
, ¬After
);
410 /* list public key information */
413 private_key_t
*private = NULL
;
414 identification_t
*id
, *keyid
;
416 id
= public->get_id(public, ID_PUBKEY_SHA1
);
417 keyid
= public->get_id(public, ID_PUBKEY_INFO_SHA1
);
419 private = charon
->credentials
->get_private(
421 public->get_type(public), keyid
, NULL
);
422 fprintf(out
, " pubkey: %N %d bits%s\n",
423 key_type_names
, public->get_type(public),
424 public->get_keysize(public) * 8,
425 private ?
", has private key" : "");
426 fprintf(out
, " keyid: %D\n", keyid
);
427 fprintf(out
, " subjkey: %D\n", id
);
429 public->destroy(public);
432 /* list optional authorityKeyIdentifier */
435 fprintf(out
, " authkey: %D\n", authkey
);
439 enumerator
->destroy(enumerator
);
443 * list all X.509 attribute certificates
445 static void stroke_list_acerts(bool utc
, FILE *out
)
451 * list all X.509 CRLs
453 static void stroke_list_crls(linked_list_t
*list
, bool utc
, FILE *out
)
456 time_t thisUpdate
, nextUpdate
, now
= time(NULL
);
457 enumerator_t
*enumerator
= list
->create_enumerator(list
);
460 while (enumerator
->enumerate(enumerator
, (void**)&cert
))
462 crl_t
*crl
= (crl_t
*)cert
;
463 chunk_t serial
= crl
->get_serial(crl
);
464 identification_t
*authkey
= crl
->get_authKeyIdentifier(crl
);
469 fprintf(out
, "List of X.509 CRLs:\n");
474 fprintf(out
, " issuer: \"%D\"\n", cert
->get_issuer(cert
));
476 /* list optional crlNumber */
479 fprintf(out
, " serial: %#B\n", &serial
);
482 /* count the number of revoked certificates */
485 enumerator_t
*enumerator
= crl
->create_enumerator(crl
);
487 while (enumerator
->enumerate(enumerator
, NULL
, NULL
, NULL
))
491 fprintf(out
, " revoked: %d certificate%s\n", count
,
492 (count
== 1)?
"" : "s");
493 enumerator
->destroy(enumerator
);
497 cert
->get_validity(cert
, &now
, &thisUpdate
, &nextUpdate
);
498 fprintf(out
, " updates: this %#T\n", &thisUpdate
, utc
);
499 fprintf(out
, " next %#T, ", &nextUpdate
, utc
);
500 if (now
> nextUpdate
)
502 fprintf(out
, "expired (%#V ago)\n", &now
, &nextUpdate
);
507 if (now
> nextUpdate
- CRL_WARNING_INTERVAL
* 60 * 60 * 24)
509 fprintf(out
, " (expires in %#V)", &now
, &nextUpdate
);
514 /* list optional authorityKeyIdentifier */
517 fprintf(out
, " authkey: %D\n", authkey
);
520 enumerator
->destroy(enumerator
);
524 * list all OCSP responses
526 static void stroke_list_ocsp(bool utc
, FILE *out
)
532 * Implementation of stroke_list_t.list.
534 static void list(private_stroke_list_t
*this, stroke_msg_t
*msg
, FILE *out
)
536 linked_list_t
*cert_list
= NULL
;
538 if (msg
->list
.flags
& (LIST_CERTS
| LIST_CACERTS
| LIST_OCSPCERTS
| LIST_AACERTS
))
540 cert_list
= create_unique_cert_list(CERT_X509
);
542 if (msg
->list
.flags
& LIST_CERTS
)
544 stroke_list_certs(cert_list
, "X.509 End Entity Certificates",
545 0, msg
->list
.utc
, out
);
547 if (msg
->list
.flags
& LIST_CACERTS
)
549 stroke_list_certs(cert_list
, "X.509 CA Certificates",
550 X509_CA
, msg
->list
.utc
, out
);
552 if (msg
->list
.flags
& LIST_OCSPCERTS
)
554 stroke_list_certs(cert_list
, "X.509 OCSP Signer Certificates",
555 X509_OCSP_SIGNER
, msg
->list
.utc
, out
);
557 if (msg
->list
.flags
& LIST_AACERTS
)
559 stroke_list_certs(cert_list
, "X.509 AA Certificates",
560 X509_AA
, msg
->list
.utc
, out
);
562 if (msg
->list
.flags
& LIST_ACERTS
)
564 stroke_list_acerts(msg
->list
.utc
, out
);
566 if (msg
->list
.flags
& LIST_CRLS
)
568 linked_list_t
*crl_list
= create_unique_cert_list(CERT_X509_CRL
);
570 stroke_list_crls(crl_list
, msg
->list
.utc
, out
);
571 crl_list
->destroy_offset(crl_list
, offsetof(certificate_t
, destroy
));
573 if (msg
->list
.flags
& LIST_OCSP
)
575 stroke_list_ocsp(msg
->list
.utc
, out
);
577 DESTROY_OFFSET_IF(cert_list
, offsetof(certificate_t
, destroy
));
581 * Implementation of stroke_list_t.destroy
583 static void destroy(private_stroke_list_t
*this)
591 stroke_list_t
*stroke_list_create()
593 private_stroke_list_t
*this = malloc_thing(private_stroke_list_t
);
595 this->public.list
= (void(*)(stroke_list_t
*, stroke_msg_t
*msg
, FILE *out
))list
;
596 this->public.status
= (void(*)(stroke_list_t
*, stroke_msg_t
*msg
, FILE *out
,bool))status
;
597 this->public.destroy
= (void(*)(stroke_list_t
*))destroy
;
599 this->uptime
= time(NULL
);
601 return &this->public;