do not report increasing rekey times if rekeying currently active
[strongswan.git] / src / charon / plugins / stroke / stroke_list.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "stroke_list.h"
17
18 #include <time.h>
19
20 #include <daemon.h>
21 #include <utils/linked_list.h>
22 #include <credentials/certificates/x509.h>
23 #include <credentials/certificates/ac.h>
24 #include <credentials/certificates/crl.h>
25 #include <config/peer_cfg.h>
26
27 /* warning intervals for list functions */
28 #define CERT_WARNING_INTERVAL 30 /* days */
29 #define CRL_WARNING_INTERVAL 7 /* days */
30 #define AC_WARNING_INTERVAL 1 /* day */
31
32 typedef struct private_stroke_list_t private_stroke_list_t;
33
34 /**
35 * private data of stroke_list
36 */
37 struct private_stroke_list_t {
38
39 /**
40 * public functions
41 */
42 stroke_list_t public;
43
44 /**
45 * timestamp of daemon start
46 */
47 time_t uptime;
48
49 /**
50 * strokes attribute provider
51 */
52 stroke_attribute_t *attribute;
53 };
54
55 /**
56 * log an IKE_SA to out
57 */
58 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
59 {
60 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
61 time_t now = time(NULL);
62
63 fprintf(out, "%12s[%d]: %N",
64 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
65 ike_sa_state_names, ike_sa->get_state(ike_sa));
66
67 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
68 {
69 time_t established;
70
71 established = ike_sa->get_statistic(ike_sa, STAT_ESTABLISHED);
72 fprintf(out, " %V ago", &now, &established);
73 }
74
75 fprintf(out, ", %H[%Y]...%H[%Y]\n",
76 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
77 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
78
79 if (all)
80 {
81 proposal_t *ike_proposal;
82
83 ike_proposal = ike_sa->get_proposal(ike_sa);
84
85 fprintf(out, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s",
86 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
87 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
88 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
89
90
91 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
92 {
93 time_t rekey, reauth;
94 peer_cfg_t *peer_cfg;
95
96 rekey = ike_sa->get_statistic(ike_sa, STAT_REKEY);
97 reauth = ike_sa->get_statistic(ike_sa, STAT_REAUTH);
98 peer_cfg = ike_sa->get_peer_cfg(ike_sa);
99
100 if (rekey)
101 {
102 fprintf(out, ", rekeying in %V", &rekey, &now);
103 }
104 if (reauth)
105 {
106 bool first = TRUE;
107 enumerator_t *enumerator;
108 auth_cfg_t *auth;
109
110 fprintf(out, ", ");
111 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, TRUE);
112 while (enumerator->enumerate(enumerator, &auth))
113 {
114 if (!first)
115 {
116 fprintf(out, "+");
117 }
118 first = FALSE;
119 fprintf(out, "%N", auth_class_names,
120 auth->get(auth, AUTH_RULE_AUTH_CLASS));
121 }
122 enumerator->destroy(enumerator);
123 fprintf(out, " reauthentication in %V", &reauth, &now);
124 }
125 if (!rekey && !reauth)
126 {
127 fprintf(out, ", rekeying disabled");
128 }
129 }
130 fprintf(out, "\n");
131
132 if (ike_proposal)
133 {
134 char buf[BUF_LEN];
135
136 snprintf(buf, BUF_LEN, "%P", ike_proposal);
137 fprintf(out, "%12s[%d]: IKE proposal: %s\n",
138 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
139 buf+4);
140 }
141 }
142 }
143
144 /**
145 * log an CHILD_SA to out
146 */
147 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
148 {
149 u_int32_t rekey, now = time(NULL);
150 u_int32_t use_in, use_out;
151 proposal_t *proposal;
152 child_cfg_t *config = child_sa->get_config(child_sa);
153
154 fprintf(out, "%12s{%d}: %N, %N%s",
155 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
156 child_sa_state_names, child_sa->get_state(child_sa),
157 ipsec_mode_names, child_sa->get_mode(child_sa),
158 config->use_proxy_mode(config) ? "_PROXY" : "");
159
160 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
161 {
162 fprintf(out, ", %N%s SPIs: %.8x_i %.8x_o",
163 protocol_id_names, child_sa->get_protocol(child_sa),
164 child_sa->has_encap(child_sa) ? " in UDP" : "",
165 ntohl(child_sa->get_spi(child_sa, TRUE)),
166 ntohl(child_sa->get_spi(child_sa, FALSE)));
167
168 if (child_sa->get_ipcomp(child_sa) != IPCOMP_NONE)
169 {
170 fprintf(out, ", IPCOMP CPIs: %.4x_i %.4x_o",
171 ntohs(child_sa->get_cpi(child_sa, TRUE)),
172 ntohs(child_sa->get_cpi(child_sa, FALSE)));
173 }
174
175 if (all)
176 {
177 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
178 child_sa->get_reqid(child_sa));
179
180 proposal = child_sa->get_proposal(child_sa);
181 if (proposal)
182 {
183 u_int16_t encr_alg = ENCR_UNDEFINED, int_alg = AUTH_UNDEFINED;
184 u_int16_t encr_size = 0, int_size = 0;
185
186 proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM,
187 &encr_alg, &encr_size);
188 proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
189 &int_alg, &int_size);
190
191 if (encr_alg != ENCR_UNDEFINED)
192 {
193 fprintf(out, "%N", encryption_algorithm_names, encr_alg);
194 if (encr_size)
195 {
196 fprintf(out, "-%d", encr_size);
197 }
198 }
199 if (int_alg != AUTH_UNDEFINED)
200 {
201 fprintf(out, "/%N", integrity_algorithm_names, int_alg);
202 if (int_size)
203 {
204 fprintf(out, "-%d", int_size);
205 }
206 }
207 }
208 fprintf(out, ", rekeying ");
209
210 rekey = child_sa->get_lifetime(child_sa, FALSE);
211 if (rekey)
212 {
213 if (now > rekey)
214 {
215 fprintf(out, "active");
216 }
217 else
218 {
219 fprintf(out, "in %V", &now, &rekey);
220 }
221 }
222 else
223 {
224 fprintf(out, "disabled");
225 }
226
227 fprintf(out, ", last use: ");
228 use_in = child_sa->get_usetime(child_sa, TRUE);
229 if (use_in)
230 {
231 fprintf(out, "%ds_i ", now - use_in);
232 }
233 else
234 {
235 fprintf(out, "no_i ");
236 }
237 use_out = child_sa->get_usetime(child_sa, FALSE);
238 if (use_out)
239 {
240 fprintf(out, "%ds_o ", now - use_out);
241 }
242 else
243 {
244 fprintf(out, "no_o ");
245 }
246 }
247 }
248
249 fprintf(out, "\n%12s{%d}: %#R=== %#R\n",
250 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
251 child_sa->get_traffic_selectors(child_sa, TRUE),
252 child_sa->get_traffic_selectors(child_sa, FALSE));
253 }
254
255 /**
256 * Log a configs local or remote authentication config to out
257 */
258 static void log_auth_cfgs(FILE *out, peer_cfg_t *peer_cfg, bool local)
259 {
260 enumerator_t *enumerator, *rules;
261 auth_rule_t rule;
262 auth_cfg_t *auth;
263 auth_class_t auth_class;
264 identification_t *id;
265 certificate_t *cert;
266 cert_validation_t valid;
267 char *name;
268
269 name = peer_cfg->get_name(peer_cfg);
270
271 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, local);
272 while (enumerator->enumerate(enumerator, &auth))
273 {
274 fprintf(out, "%12s: %s [%Y] uses ", name, local ? "local: " : "remote:",
275 auth->get(auth, AUTH_RULE_IDENTITY));
276
277 auth_class = (uintptr_t)auth->get(auth, AUTH_RULE_AUTH_CLASS);
278 if (auth_class != AUTH_CLASS_EAP)
279 {
280 fprintf(out, "%N authentication\n", auth_class_names, auth_class);
281 }
282 else
283 {
284 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE) == EAP_NAK)
285 {
286 fprintf(out, "EAP authentication");
287 }
288 else
289 {
290 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR))
291 {
292 fprintf(out, "EAP_%d-%d authentication",
293 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE),
294 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR));
295 }
296 else
297 {
298 fprintf(out, "%N authentication", eap_type_names,
299 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE));
300 }
301 }
302 id = auth->get(auth, AUTH_RULE_EAP_IDENTITY);
303 if (id)
304 {
305 fprintf(out, " with EAP identity '%Y'", id);
306 }
307 fprintf(out, "\n");
308 }
309
310 cert = auth->get(auth, AUTH_RULE_CA_CERT);
311 if (cert)
312 {
313 fprintf(out, "%12s: ca: \"%Y\"\n", name, cert->get_subject(cert));
314 }
315
316 cert = auth->get(auth, AUTH_RULE_IM_CERT);
317 if (cert)
318 {
319 fprintf(out, "%12s: im-ca: \"%Y\"\n", name, cert->get_subject(cert));
320 }
321
322 cert = auth->get(auth, AUTH_RULE_SUBJECT_CERT);
323 if (cert)
324 {
325 fprintf(out, "%12s: cert: \"%Y\"\n", name,
326 cert->get_subject(cert));
327 }
328
329 valid = (uintptr_t)auth->get(auth, AUTH_RULE_OCSP_VALIDATION);
330 if (valid != VALIDATION_FAILED)
331 {
332 fprintf(out, "%12s: ocsp: status must be GOOD%s\n", name,
333 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
334 }
335
336 valid = (uintptr_t)auth->get(auth, AUTH_RULE_CRL_VALIDATION);
337 if (valid != VALIDATION_FAILED)
338 {
339 fprintf(out, "%12s: crl: status must be GOOD%s\n", name,
340 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
341 }
342
343 rules = auth->create_enumerator(auth);
344 while (rules->enumerate(rules, &rule, &id))
345 {
346 if (rule == AUTH_RULE_AC_GROUP)
347 {
348 fprintf(out, "%12s: group: %Y\n", name, id);
349 }
350 }
351 rules->destroy(rules);
352 }
353 enumerator->destroy(enumerator);
354 }
355
356 /**
357 * Implementation of stroke_list_t.status.
358 */
359 static void status(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out, bool all)
360 {
361 enumerator_t *enumerator, *children;
362 ike_cfg_t *ike_cfg;
363 child_cfg_t *child_cfg;
364 child_sa_t *child_sa;
365 ike_sa_t *ike_sa;
366 bool found = FALSE;
367 char *name = msg->status.name;
368
369 if (all)
370 {
371 peer_cfg_t *peer_cfg;
372 char *plugin, *pool;
373 host_t *host;
374 u_int32_t dpd;
375 time_t now = time(NULL);
376 bool first = TRUE;
377 u_int size, online, offline;
378
379 fprintf(out, "Performance:\n");
380 fprintf(out, " uptime: %V, since %T\n", &now, &this->uptime, &this->uptime, FALSE);
381 fprintf(out, " worker threads: %d idle of %d,",
382 charon->processor->get_idle_threads(charon->processor),
383 charon->processor->get_total_threads(charon->processor));
384 fprintf(out, " job queue load: %d,",
385 charon->processor->get_job_load(charon->processor));
386 fprintf(out, " scheduled events: %d\n",
387 charon->scheduler->get_job_load(charon->scheduler));
388 fprintf(out, " loaded plugins: ");
389 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
390 while (enumerator->enumerate(enumerator, &plugin))
391 {
392 fprintf(out, "%s ", plugin);
393 }
394 enumerator->destroy(enumerator);
395 fprintf(out, "\n");
396
397 enumerator = this->attribute->create_pool_enumerator(this->attribute);
398 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
399 {
400 if (name && !streq(name, pool))
401 {
402 continue;
403 }
404 if (first)
405 {
406 first = FALSE;
407 fprintf(out, "Virtual IP pools (size/online/offline):\n");
408 }
409 fprintf(out, " %s: %u/%u/%u\n", pool, size, online, offline);
410 }
411 enumerator->destroy(enumerator);
412
413 enumerator = charon->kernel_interface->create_address_enumerator(
414 charon->kernel_interface, FALSE, FALSE);
415 fprintf(out, "Listening IP addresses:\n");
416 while (enumerator->enumerate(enumerator, (void**)&host))
417 {
418 fprintf(out, " %H\n", host);
419 }
420 enumerator->destroy(enumerator);
421
422 fprintf(out, "Connections:\n");
423 enumerator = charon->backends->create_peer_cfg_enumerator(
424 charon->backends, NULL, NULL, NULL, NULL);
425 while (enumerator->enumerate(enumerator, &peer_cfg))
426 {
427 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
428 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
429 {
430 continue;
431 }
432
433 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
434 fprintf(out, "%12s: %s...%s", peer_cfg->get_name(peer_cfg),
435 ike_cfg->get_my_addr(ike_cfg), ike_cfg->get_other_addr(ike_cfg));
436
437 dpd = peer_cfg->get_dpd(peer_cfg);
438 if (dpd)
439 {
440 fprintf(out, ", dpddelay=%us", dpd);
441 }
442 fprintf(out, "\n");
443
444 log_auth_cfgs(out, peer_cfg, TRUE);
445 log_auth_cfgs(out, peer_cfg, FALSE);
446
447 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
448 while (children->enumerate(children, &child_cfg))
449 {
450 linked_list_t *my_ts, *other_ts;
451
452 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
453 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
454 fprintf(out, "%12s: child: %#R=== %#R", child_cfg->get_name(child_cfg),
455 my_ts, other_ts);
456 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
457 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
458
459 if (dpd)
460 {
461 fprintf(out, ", dpdaction=%N", action_names,
462 child_cfg->get_dpd_action(child_cfg));
463 }
464 fprintf(out, "\n");
465 }
466 children->destroy(children);
467 }
468 enumerator->destroy(enumerator);
469 }
470
471 fprintf(out, "Routed Connections:\n");
472 enumerator = charon->traps->create_enumerator(charon->traps);
473 while (enumerator->enumerate(enumerator, NULL, &child_sa))
474 {
475 log_child_sa(out, child_sa, all);
476 }
477 enumerator->destroy(enumerator);
478
479 fprintf(out, "Security Associations:\n");
480 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
481 while (enumerator->enumerate(enumerator, &ike_sa))
482 {
483 bool ike_printed = FALSE;
484 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
485
486 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
487 {
488 log_ike_sa(out, ike_sa, all);
489 found = TRUE;
490 ike_printed = TRUE;
491 }
492
493 while (children->iterate(children, (void**)&child_sa))
494 {
495 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
496 {
497 if (!ike_printed)
498 {
499 log_ike_sa(out, ike_sa, all);
500 found = TRUE;
501 ike_printed = TRUE;
502 }
503 log_child_sa(out, child_sa, all);
504 }
505 }
506 children->destroy(children);
507 }
508 enumerator->destroy(enumerator);
509
510 if (!found)
511 {
512 if (name)
513 {
514 fprintf(out, " no match\n");
515 }
516 else
517 {
518 fprintf(out, " none\n");
519 }
520 }
521 }
522
523 /**
524 * create a unique certificate list without duplicates
525 * certicates having the same issuer are grouped together.
526 */
527 static linked_list_t* create_unique_cert_list(certificate_type_t type)
528 {
529 linked_list_t *list = linked_list_create();
530 enumerator_t *enumerator = charon->credentials->create_cert_enumerator(
531 charon->credentials, type, KEY_ANY,
532 NULL, FALSE);
533 certificate_t *cert;
534
535 while (enumerator->enumerate(enumerator, (void**)&cert))
536 {
537 iterator_t *iterator = list->create_iterator(list, TRUE);
538 identification_t *issuer = cert->get_issuer(cert);
539 bool previous_same, same = FALSE, last = TRUE;
540 certificate_t *list_cert;
541
542 while (iterator->iterate(iterator, (void**)&list_cert))
543 {
544 /* exit if we have a duplicate? */
545 if (list_cert->equals(list_cert, cert))
546 {
547 last = FALSE;
548 break;
549 }
550 /* group certificates with same issuer */
551 previous_same = same;
552 same = list_cert->has_issuer(list_cert, issuer);
553 if (previous_same && !same)
554 {
555 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
556 last = FALSE;
557 break;
558 }
559 }
560 iterator->destroy(iterator);
561
562 if (last)
563 {
564 list->insert_last(list, (void *)cert->get_ref(cert));
565 }
566 }
567 enumerator->destroy(enumerator);
568 return list;
569 }
570
571 /**
572 * list all raw public keys
573 */
574 static void stroke_list_pubkeys(linked_list_t *list, bool utc, FILE *out)
575 {
576 bool first = TRUE;
577
578 enumerator_t *enumerator = list->create_enumerator(list);
579 certificate_t *cert;
580
581 while (enumerator->enumerate(enumerator, (void**)&cert))
582 {
583 public_key_t *public = cert->get_public_key(cert);
584
585 if (public)
586 {
587 private_key_t *private = NULL;
588 identification_t *id, *keyid;
589
590 if (first)
591 {
592 fprintf(out, "\n");
593 fprintf(out, "List of Raw Public Keys:\n");
594 first = FALSE;
595 }
596 fprintf(out, "\n");
597
598 /* list public key information */
599 id = public->get_id(public, ID_PUBKEY_SHA1);
600 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
601
602 private = charon->credentials->get_private(
603 charon->credentials,
604 public->get_type(public), keyid, NULL);
605 fprintf(out, " pubkey: %N %d bits%s\n",
606 key_type_names, public->get_type(public),
607 public->get_keysize(public) * 8,
608 private ? ", has private key" : "");
609 fprintf(out, " keyid: %Y\n", keyid);
610 fprintf(out, " subjkey: %Y\n", id);
611 DESTROY_IF(private);
612 public->destroy(public);
613 }
614 }
615 enumerator->destroy(enumerator);
616 }
617
618 /**
619 * list all X.509 certificates matching the flags
620 */
621 static void stroke_list_certs(linked_list_t *list, char *label,
622 x509_flag_t flags, bool utc, FILE *out)
623 {
624 bool first = TRUE;
625 time_t now = time(NULL);
626 enumerator_t *enumerator = list->create_enumerator(list);
627 certificate_t *cert;
628
629 while (enumerator->enumerate(enumerator, (void**)&cert))
630 {
631 x509_t *x509 = (x509_t*)cert;
632 x509_flag_t x509_flags = x509->get_flags(x509);
633
634 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
635 if ((x509_flags & flags) || (flags == (x509_flags & ~X509_SELF_SIGNED)))
636 {
637 enumerator_t *enumerator;
638 identification_t *altName;
639 bool first_altName = TRUE;
640 chunk_t serial = x509->get_serial(x509);
641 identification_t *authkey = x509->get_authKeyIdentifier(x509);
642 time_t notBefore, notAfter;
643 public_key_t *public = cert->get_public_key(cert);
644
645 if (first)
646 {
647 fprintf(out, "\n");
648 fprintf(out, "List of %s:\n", label);
649 first = FALSE;
650 }
651 fprintf(out, "\n");
652
653 /* list subjectAltNames */
654 enumerator = x509->create_subjectAltName_enumerator(x509);
655 while (enumerator->enumerate(enumerator, (void**)&altName))
656 {
657 if (first_altName)
658 {
659 fprintf(out, " altNames: ");
660 first_altName = FALSE;
661 }
662 else
663 {
664 fprintf(out, ", ");
665 }
666 fprintf(out, "%Y", altName);
667 }
668 if (!first_altName)
669 {
670 fprintf(out, "\n");
671 }
672 enumerator->destroy(enumerator);
673
674 fprintf(out, " subject: \"%Y\"\n", cert->get_subject(cert));
675 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
676 fprintf(out, " serial: %#B\n", &serial);
677
678 /* list validity */
679 cert->get_validity(cert, &now, &notBefore, &notAfter);
680 fprintf(out, " validity: not before %T, ", &notBefore, utc);
681 if (now < notBefore)
682 {
683 fprintf(out, "not valid yet (valid in %V)\n", &now, &notBefore);
684 }
685 else
686 {
687 fprintf(out, "ok\n");
688 }
689 fprintf(out, " not after %T, ", &notAfter, utc);
690 if (now > notAfter)
691 {
692 fprintf(out, "expired (%V ago)\n", &now, &notAfter);
693 }
694 else
695 {
696 fprintf(out, "ok");
697 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
698 {
699 fprintf(out, " (expires in %V)", &now, &notAfter);
700 }
701 fprintf(out, " \n");
702 }
703
704 /* list public key information */
705 if (public)
706 {
707 private_key_t *private = NULL;
708 identification_t *id, *keyid;
709
710 id = public->get_id(public, ID_PUBKEY_SHA1);
711 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
712
713 private = charon->credentials->get_private(
714 charon->credentials,
715 public->get_type(public), keyid, NULL);
716 fprintf(out, " pubkey: %N %d bits%s\n",
717 key_type_names, public->get_type(public),
718 public->get_keysize(public) * 8,
719 private ? ", has private key" : "");
720 fprintf(out, " keyid: %Y\n", keyid);
721 fprintf(out, " subjkey: %Y\n", id);
722 DESTROY_IF(private);
723 public->destroy(public);
724 }
725
726 /* list optional authorityKeyIdentifier */
727 if (authkey)
728 {
729 fprintf(out, " authkey: %Y\n", authkey);
730 }
731 }
732 }
733 enumerator->destroy(enumerator);
734 }
735
736 /**
737 * list all X.509 attribute certificates
738 */
739 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
740 {
741 bool first = TRUE;
742 time_t thisUpdate, nextUpdate, now = time(NULL);
743 enumerator_t *enumerator = list->create_enumerator(list);
744 certificate_t *cert;
745
746 while (enumerator->enumerate(enumerator, (void**)&cert))
747 {
748 ac_t *ac = (ac_t*)cert;
749 chunk_t serial = ac->get_serial(ac);
750 chunk_t holderSerial = ac->get_holderSerial(ac);
751 identification_t *holderIssuer = ac->get_holderIssuer(ac);
752 identification_t *authkey = ac->get_authKeyIdentifier(ac);
753 identification_t *entityName = cert->get_subject(cert);
754
755 if (first)
756 {
757 fprintf(out, "\n");
758 fprintf(out, "List of X.509 Attribute Certificates:\n");
759 first = FALSE;
760 }
761 fprintf(out, "\n");
762
763 if (entityName)
764 {
765 fprintf(out, " holder: \"%Y\"\n", entityName);
766 }
767 if (holderIssuer)
768 {
769 fprintf(out, " hissuer: \"%Y\"\n", holderIssuer);
770 }
771 if (holderSerial.ptr)
772 {
773 fprintf(out, " hserial: %#B\n", &holderSerial);
774 }
775 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
776 fprintf(out, " serial: %#B\n", &serial);
777
778 /* list validity */
779 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
780 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
781 fprintf(out, " next %T, ", &nextUpdate, utc);
782 if (now > nextUpdate)
783 {
784 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
785 }
786 else
787 {
788 fprintf(out, "ok");
789 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
790 {
791 fprintf(out, " (expires in %V)", &now, &nextUpdate);
792 }
793 fprintf(out, " \n");
794 }
795
796 /* list optional authorityKeyIdentifier */
797 if (authkey)
798 {
799 fprintf(out, " authkey: %Y\n", authkey);
800 }
801 }
802 enumerator->destroy(enumerator);
803 }
804
805 /**
806 * list all X.509 CRLs
807 */
808 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
809 {
810 bool first = TRUE;
811 time_t thisUpdate, nextUpdate, now = time(NULL);
812 enumerator_t *enumerator = list->create_enumerator(list);
813 certificate_t *cert;
814
815 while (enumerator->enumerate(enumerator, (void**)&cert))
816 {
817 crl_t *crl = (crl_t*)cert;
818 chunk_t serial = crl->get_serial(crl);
819 identification_t *authkey = crl->get_authKeyIdentifier(crl);
820
821 if (first)
822 {
823 fprintf(out, "\n");
824 fprintf(out, "List of X.509 CRLs:\n");
825 first = FALSE;
826 }
827 fprintf(out, "\n");
828
829 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
830
831 /* list optional crlNumber */
832 if (serial.ptr)
833 {
834 fprintf(out, " serial: %#B\n", &serial);
835 }
836
837 /* count the number of revoked certificates */
838 {
839 int count = 0;
840 enumerator_t *enumerator = crl->create_enumerator(crl);
841
842 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
843 {
844 count++;
845 }
846 fprintf(out, " revoked: %d certificate%s\n", count,
847 (count == 1)? "" : "s");
848 enumerator->destroy(enumerator);
849 }
850
851 /* list validity */
852 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
853 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
854 fprintf(out, " next %T, ", &nextUpdate, utc);
855 if (now > nextUpdate)
856 {
857 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
858 }
859 else
860 {
861 fprintf(out, "ok");
862 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
863 {
864 fprintf(out, " (expires in %V)", &now, &nextUpdate);
865 }
866 fprintf(out, " \n");
867 }
868
869 /* list optional authorityKeyIdentifier */
870 if (authkey)
871 {
872 fprintf(out, " authkey: %Y\n", authkey);
873 }
874 }
875 enumerator->destroy(enumerator);
876 }
877
878 /**
879 * list all OCSP responses
880 */
881 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
882 {
883 bool first = TRUE;
884 enumerator_t *enumerator = list->create_enumerator(list);
885 certificate_t *cert;
886
887 while (enumerator->enumerate(enumerator, (void**)&cert))
888 {
889 if (first)
890 {
891 fprintf(out, "\n");
892 fprintf(out, "List of OCSP responses:\n");
893 fprintf(out, "\n");
894 first = FALSE;
895 }
896
897 fprintf(out, " signer: \"%Y\"\n", cert->get_issuer(cert));
898 }
899 enumerator->destroy(enumerator);
900 }
901
902 /**
903 * List of registered cryptographical algorithms
904 */
905 static void list_algs(FILE *out)
906 {
907 enumerator_t *enumerator;
908 encryption_algorithm_t encryption;
909 integrity_algorithm_t integrity;
910 hash_algorithm_t hash;
911 pseudo_random_function_t prf;
912 diffie_hellman_group_t group;
913
914 fprintf(out, "\n");
915 fprintf(out, "List of registered IKEv2 Algorithms:\n");
916 fprintf(out, "\n encryption: ");
917 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
918 while (enumerator->enumerate(enumerator, &encryption))
919 {
920 fprintf(out, "%N ", encryption_algorithm_names, encryption);
921 }
922 enumerator->destroy(enumerator);
923 fprintf(out, "\n integrity: ");
924 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
925 while (enumerator->enumerate(enumerator, &integrity))
926 {
927 fprintf(out, "%N ", integrity_algorithm_names, integrity);
928 }
929 enumerator->destroy(enumerator);
930 fprintf(out, "\n hasher: ");
931 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
932 while (enumerator->enumerate(enumerator, &hash))
933 {
934 fprintf(out, "%N ", hash_algorithm_names, hash);
935 }
936 enumerator->destroy(enumerator);
937 fprintf(out, "\n prf: ");
938 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
939 while (enumerator->enumerate(enumerator, &prf))
940 {
941 fprintf(out, "%N ", pseudo_random_function_names, prf);
942 }
943 enumerator->destroy(enumerator);
944 fprintf(out, "\n dh-group: ");
945 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
946 while (enumerator->enumerate(enumerator, &group))
947 {
948 fprintf(out, "%N ", diffie_hellman_group_names, group);
949 }
950 enumerator->destroy(enumerator);
951 fprintf(out, "\n");
952 }
953
954 /**
955 * Implementation of stroke_list_t.list.
956 */
957 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
958 {
959 linked_list_t *cert_list = NULL;
960
961 if (msg->list.flags & LIST_PUBKEYS)
962 {
963 linked_list_t *pubkey_list = create_unique_cert_list(CERT_TRUSTED_PUBKEY);
964
965 stroke_list_pubkeys(pubkey_list, msg->list.utc, out);
966 pubkey_list->destroy_offset(pubkey_list, offsetof(certificate_t, destroy));
967 }
968 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
969 {
970 cert_list = create_unique_cert_list(CERT_X509);
971 }
972 if (msg->list.flags & LIST_CERTS)
973 {
974 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
975 0, msg->list.utc, out);
976 }
977 if (msg->list.flags & LIST_CACERTS)
978 {
979 stroke_list_certs(cert_list, "X.509 CA Certificates",
980 X509_CA, msg->list.utc, out);
981 }
982 if (msg->list.flags & LIST_OCSPCERTS)
983 {
984 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
985 X509_OCSP_SIGNER, msg->list.utc, out);
986 }
987 if (msg->list.flags & LIST_AACERTS)
988 {
989 stroke_list_certs(cert_list, "X.509 AA Certificates",
990 X509_AA, msg->list.utc, out);
991 }
992 if (msg->list.flags & LIST_ACERTS)
993 {
994 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
995
996 stroke_list_acerts(ac_list, msg->list.utc, out);
997 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
998 }
999 if (msg->list.flags & LIST_CRLS)
1000 {
1001 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
1002
1003 stroke_list_crls(crl_list, msg->list.utc, out);
1004 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
1005 }
1006 if (msg->list.flags & LIST_OCSP)
1007 {
1008 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
1009
1010 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
1011
1012 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
1013 }
1014 if (msg->list.flags & LIST_ALGS)
1015 {
1016 list_algs(out);
1017 }
1018 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
1019 }
1020
1021 /**
1022 * Print leases of a single pool
1023 */
1024 static void pool_leases(private_stroke_list_t *this, FILE *out, char *pool,
1025 host_t *address, u_int size, u_int online, u_int offline)
1026 {
1027 enumerator_t *enumerator;
1028 identification_t *id;
1029 host_t *lease;
1030 bool on;
1031 int found = 0;
1032
1033 fprintf(out, "Leases in pool '%s', usage: %lu/%lu, %lu online\n",
1034 pool, online + offline, size, online);
1035 enumerator = this->attribute->create_lease_enumerator(this->attribute, pool);
1036 while (enumerator && enumerator->enumerate(enumerator, &id, &lease, &on))
1037 {
1038 if (!address || address->ip_equals(address, lease))
1039 {
1040 fprintf(out, " %15H %s '%Y'\n",
1041 lease, on ? "online" : "offline", id);
1042 found++;
1043 }
1044 }
1045 enumerator->destroy(enumerator);
1046 if (!found)
1047 {
1048 fprintf(out, " no matching leases found\n");
1049 }
1050 }
1051
1052 /**
1053 * Implementation of stroke_list_t.leases
1054 */
1055 static void leases(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1056 {
1057 enumerator_t *enumerator;
1058 u_int size, offline, online;
1059 host_t *address = NULL;
1060 char *pool;
1061 int found = 0;
1062
1063 if (msg->leases.address)
1064 {
1065 address = host_create_from_string(msg->leases.address, 0);
1066 }
1067
1068 enumerator = this->attribute->create_pool_enumerator(this->attribute);
1069 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
1070 {
1071 if (!msg->leases.pool || streq(msg->leases.pool, pool))
1072 {
1073 pool_leases(this, out, pool, address, size, online, offline);
1074 found++;
1075 }
1076 }
1077 enumerator->destroy(enumerator);
1078 if (!found)
1079 {
1080 if (msg->leases.pool)
1081 {
1082 fprintf(out, "pool '%s' not found\n", msg->leases.pool);
1083 }
1084 else
1085 {
1086 fprintf(out, "no pools found\n");
1087 }
1088 }
1089 DESTROY_IF(address);
1090 }
1091
1092 /**
1093 * Implementation of stroke_list_t.destroy
1094 */
1095 static void destroy(private_stroke_list_t *this)
1096 {
1097 free(this);
1098 }
1099
1100 /*
1101 * see header file
1102 */
1103 stroke_list_t *stroke_list_create(stroke_attribute_t *attribute)
1104 {
1105 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
1106
1107 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
1108 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
1109 this->public.leases = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))leases;
1110 this->public.destroy = (void(*)(stroke_list_t*))destroy;
1111
1112 this->uptime = time(NULL);
1113 this->attribute = attribute;
1114
1115 return &this->public;
1116 }
1117