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