4f97d38022500e7395c590831e9107244e768624
[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, "_%u", 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, "_%u", 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 first, 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 u_int size, online, offline;
377
378 fprintf(out, "Daemon status (charon "VERSION"):\n");
379 fprintf(out, " uptime: %V, since %T\n", &now, &this->uptime, &this->uptime, FALSE);
380 fprintf(out, " worker threads: %d idle of %d,",
381 charon->processor->get_idle_threads(charon->processor),
382 charon->processor->get_total_threads(charon->processor));
383 fprintf(out, " job queue load: %d,",
384 charon->processor->get_job_load(charon->processor));
385 fprintf(out, " scheduled events: %d\n",
386 charon->scheduler->get_job_load(charon->scheduler));
387 fprintf(out, " loaded plugins: ");
388 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
389 while (enumerator->enumerate(enumerator, &plugin))
390 {
391 fprintf(out, "%s ", plugin);
392 }
393 enumerator->destroy(enumerator);
394 fprintf(out, "\n");
395
396 first = TRUE;
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 first = TRUE;
472 enumerator = charon->traps->create_enumerator(charon->traps);
473 while (enumerator->enumerate(enumerator, NULL, &child_sa))
474 {
475 if (first)
476 {
477 fprintf(out, "Routed Connections:\n");
478 first = FALSE;
479 }
480 log_child_sa(out, child_sa, all);
481 }
482 enumerator->destroy(enumerator);
483
484 fprintf(out, "Security Associations:\n");
485 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
486 while (enumerator->enumerate(enumerator, &ike_sa))
487 {
488 bool ike_printed = FALSE;
489 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
490
491 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
492 {
493 log_ike_sa(out, ike_sa, all);
494 found = TRUE;
495 ike_printed = TRUE;
496 }
497
498 while (children->iterate(children, (void**)&child_sa))
499 {
500 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
501 {
502 if (!ike_printed)
503 {
504 log_ike_sa(out, ike_sa, all);
505 found = TRUE;
506 ike_printed = TRUE;
507 }
508 log_child_sa(out, child_sa, all);
509 }
510 }
511 children->destroy(children);
512 }
513 enumerator->destroy(enumerator);
514
515 if (!found)
516 {
517 if (name)
518 {
519 fprintf(out, " no match\n");
520 }
521 else
522 {
523 fprintf(out, " none\n");
524 }
525 }
526 }
527
528 /**
529 * create a unique certificate list without duplicates
530 * certicates having the same issuer are grouped together.
531 */
532 static linked_list_t* create_unique_cert_list(certificate_type_t type)
533 {
534 linked_list_t *list = linked_list_create();
535 enumerator_t *enumerator = charon->credentials->create_cert_enumerator(
536 charon->credentials, type, KEY_ANY,
537 NULL, FALSE);
538 certificate_t *cert;
539
540 while (enumerator->enumerate(enumerator, (void**)&cert))
541 {
542 iterator_t *iterator = list->create_iterator(list, TRUE);
543 identification_t *issuer = cert->get_issuer(cert);
544 bool previous_same, same = FALSE, last = TRUE;
545 certificate_t *list_cert;
546
547 while (iterator->iterate(iterator, (void**)&list_cert))
548 {
549 /* exit if we have a duplicate? */
550 if (list_cert->equals(list_cert, cert))
551 {
552 last = FALSE;
553 break;
554 }
555 /* group certificates with same issuer */
556 previous_same = same;
557 same = list_cert->has_issuer(list_cert, issuer);
558 if (previous_same && !same)
559 {
560 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
561 last = FALSE;
562 break;
563 }
564 }
565 iterator->destroy(iterator);
566
567 if (last)
568 {
569 list->insert_last(list, (void *)cert->get_ref(cert));
570 }
571 }
572 enumerator->destroy(enumerator);
573 return list;
574 }
575
576 /**
577 * list all raw public keys
578 */
579 static void stroke_list_pubkeys(linked_list_t *list, bool utc, FILE *out)
580 {
581 bool first = TRUE;
582
583 enumerator_t *enumerator = list->create_enumerator(list);
584 certificate_t *cert;
585
586 while (enumerator->enumerate(enumerator, (void**)&cert))
587 {
588 public_key_t *public = cert->get_public_key(cert);
589
590 if (public)
591 {
592 private_key_t *private = NULL;
593 identification_t *id, *keyid;
594
595 if (first)
596 {
597 fprintf(out, "\n");
598 fprintf(out, "List of Raw Public Keys:\n");
599 first = FALSE;
600 }
601 fprintf(out, "\n");
602
603 /* list public key information */
604 id = public->get_id(public, ID_PUBKEY_SHA1);
605 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
606
607 private = charon->credentials->get_private(
608 charon->credentials,
609 public->get_type(public), keyid, NULL);
610 fprintf(out, " pubkey: %N %d bits%s\n",
611 key_type_names, public->get_type(public),
612 public->get_keysize(public) * 8,
613 private ? ", has private key" : "");
614 fprintf(out, " keyid: %Y\n", keyid);
615 fprintf(out, " subjkey: %Y\n", id);
616 DESTROY_IF(private);
617 public->destroy(public);
618 }
619 }
620 enumerator->destroy(enumerator);
621 }
622
623 /**
624 * list all X.509 certificates matching the flags
625 */
626 static void stroke_list_certs(linked_list_t *list, char *label,
627 x509_flag_t flags, bool utc, FILE *out)
628 {
629 bool first = TRUE;
630 time_t now = time(NULL);
631 enumerator_t *enumerator = list->create_enumerator(list);
632 certificate_t *cert;
633
634 while (enumerator->enumerate(enumerator, (void**)&cert))
635 {
636 x509_t *x509 = (x509_t*)cert;
637 x509_flag_t x509_flags = x509->get_flags(x509);
638
639 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
640 if ((x509_flags & flags) || (flags == (x509_flags & ~X509_SELF_SIGNED)))
641 {
642 enumerator_t *enumerator;
643 identification_t *altName;
644 bool first_altName = TRUE;
645 chunk_t serial = x509->get_serial(x509);
646 identification_t *authkey = x509->get_authKeyIdentifier(x509);
647 time_t notBefore, notAfter;
648 public_key_t *public = cert->get_public_key(cert);
649
650 if (first)
651 {
652 fprintf(out, "\n");
653 fprintf(out, "List of %s:\n", label);
654 first = FALSE;
655 }
656 fprintf(out, "\n");
657
658 /* list subjectAltNames */
659 enumerator = x509->create_subjectAltName_enumerator(x509);
660 while (enumerator->enumerate(enumerator, (void**)&altName))
661 {
662 if (first_altName)
663 {
664 fprintf(out, " altNames: ");
665 first_altName = FALSE;
666 }
667 else
668 {
669 fprintf(out, ", ");
670 }
671 fprintf(out, "%Y", altName);
672 }
673 if (!first_altName)
674 {
675 fprintf(out, "\n");
676 }
677 enumerator->destroy(enumerator);
678
679 fprintf(out, " subject: \"%Y\"\n", cert->get_subject(cert));
680 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
681 fprintf(out, " serial: %#B\n", &serial);
682
683 /* list validity */
684 cert->get_validity(cert, &now, &notBefore, &notAfter);
685 fprintf(out, " validity: not before %T, ", &notBefore, utc);
686 if (now < notBefore)
687 {
688 fprintf(out, "not valid yet (valid in %V)\n", &now, &notBefore);
689 }
690 else
691 {
692 fprintf(out, "ok\n");
693 }
694 fprintf(out, " not after %T, ", &notAfter, utc);
695 if (now > notAfter)
696 {
697 fprintf(out, "expired (%V ago)\n", &now, &notAfter);
698 }
699 else
700 {
701 fprintf(out, "ok");
702 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
703 {
704 fprintf(out, " (expires in %V)", &now, &notAfter);
705 }
706 fprintf(out, " \n");
707 }
708
709 /* list public key information */
710 if (public)
711 {
712 private_key_t *private = NULL;
713 identification_t *id, *keyid;
714
715 id = public->get_id(public, ID_PUBKEY_SHA1);
716 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
717
718 private = charon->credentials->get_private(
719 charon->credentials,
720 public->get_type(public), keyid, NULL);
721 fprintf(out, " pubkey: %N %d bits%s\n",
722 key_type_names, public->get_type(public),
723 public->get_keysize(public) * 8,
724 private ? ", has private key" : "");
725 fprintf(out, " keyid: %Y\n", keyid);
726 fprintf(out, " subjkey: %Y\n", id);
727 DESTROY_IF(private);
728 public->destroy(public);
729 }
730
731 /* list optional authorityKeyIdentifier */
732 if (authkey)
733 {
734 fprintf(out, " authkey: %Y\n", authkey);
735 }
736 }
737 }
738 enumerator->destroy(enumerator);
739 }
740
741 /**
742 * list all X.509 attribute certificates
743 */
744 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
745 {
746 bool first = TRUE;
747 time_t thisUpdate, nextUpdate, now = time(NULL);
748 enumerator_t *enumerator = list->create_enumerator(list);
749 certificate_t *cert;
750
751 while (enumerator->enumerate(enumerator, (void**)&cert))
752 {
753 ac_t *ac = (ac_t*)cert;
754 chunk_t serial = ac->get_serial(ac);
755 chunk_t holderSerial = ac->get_holderSerial(ac);
756 identification_t *holderIssuer = ac->get_holderIssuer(ac);
757 identification_t *authkey = ac->get_authKeyIdentifier(ac);
758 identification_t *entityName = cert->get_subject(cert);
759
760 if (first)
761 {
762 fprintf(out, "\n");
763 fprintf(out, "List of X.509 Attribute Certificates:\n");
764 first = FALSE;
765 }
766 fprintf(out, "\n");
767
768 if (entityName)
769 {
770 fprintf(out, " holder: \"%Y\"\n", entityName);
771 }
772 if (holderIssuer)
773 {
774 fprintf(out, " hissuer: \"%Y\"\n", holderIssuer);
775 }
776 if (holderSerial.ptr)
777 {
778 fprintf(out, " hserial: %#B\n", &holderSerial);
779 }
780 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
781 fprintf(out, " serial: %#B\n", &serial);
782
783 /* list validity */
784 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
785 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
786 fprintf(out, " next %T, ", &nextUpdate, utc);
787 if (now > nextUpdate)
788 {
789 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
790 }
791 else
792 {
793 fprintf(out, "ok");
794 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
795 {
796 fprintf(out, " (expires in %V)", &now, &nextUpdate);
797 }
798 fprintf(out, " \n");
799 }
800
801 /* list optional authorityKeyIdentifier */
802 if (authkey)
803 {
804 fprintf(out, " authkey: %Y\n", authkey);
805 }
806 }
807 enumerator->destroy(enumerator);
808 }
809
810 /**
811 * list all X.509 CRLs
812 */
813 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
814 {
815 bool first = TRUE;
816 time_t thisUpdate, nextUpdate, now = time(NULL);
817 enumerator_t *enumerator = list->create_enumerator(list);
818 certificate_t *cert;
819
820 while (enumerator->enumerate(enumerator, (void**)&cert))
821 {
822 crl_t *crl = (crl_t*)cert;
823 chunk_t serial = crl->get_serial(crl);
824 identification_t *authkey = crl->get_authKeyIdentifier(crl);
825
826 if (first)
827 {
828 fprintf(out, "\n");
829 fprintf(out, "List of X.509 CRLs:\n");
830 first = FALSE;
831 }
832 fprintf(out, "\n");
833
834 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
835
836 /* list optional crlNumber */
837 if (serial.ptr)
838 {
839 fprintf(out, " serial: %#B\n", &serial);
840 }
841
842 /* count the number of revoked certificates */
843 {
844 int count = 0;
845 enumerator_t *enumerator = crl->create_enumerator(crl);
846
847 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
848 {
849 count++;
850 }
851 fprintf(out, " revoked: %d certificate%s\n", count,
852 (count == 1)? "" : "s");
853 enumerator->destroy(enumerator);
854 }
855
856 /* list validity */
857 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
858 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
859 fprintf(out, " next %T, ", &nextUpdate, utc);
860 if (now > nextUpdate)
861 {
862 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
863 }
864 else
865 {
866 fprintf(out, "ok");
867 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
868 {
869 fprintf(out, " (expires in %V)", &now, &nextUpdate);
870 }
871 fprintf(out, " \n");
872 }
873
874 /* list optional authorityKeyIdentifier */
875 if (authkey)
876 {
877 fprintf(out, " authkey: %Y\n", authkey);
878 }
879 }
880 enumerator->destroy(enumerator);
881 }
882
883 /**
884 * list all OCSP responses
885 */
886 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
887 {
888 bool first = TRUE;
889 enumerator_t *enumerator = list->create_enumerator(list);
890 certificate_t *cert;
891
892 while (enumerator->enumerate(enumerator, (void**)&cert))
893 {
894 if (first)
895 {
896 fprintf(out, "\n");
897 fprintf(out, "List of OCSP responses:\n");
898 fprintf(out, "\n");
899 first = FALSE;
900 }
901
902 fprintf(out, " signer: \"%Y\"\n", cert->get_issuer(cert));
903 }
904 enumerator->destroy(enumerator);
905 }
906
907 /**
908 * List of registered cryptographical algorithms
909 */
910 static void list_algs(FILE *out)
911 {
912 enumerator_t *enumerator;
913 encryption_algorithm_t encryption;
914 integrity_algorithm_t integrity;
915 hash_algorithm_t hash;
916 pseudo_random_function_t prf;
917 diffie_hellman_group_t group;
918
919 fprintf(out, "\n");
920 fprintf(out, "List of registered IKEv2 Algorithms:\n");
921 fprintf(out, "\n encryption: ");
922 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
923 while (enumerator->enumerate(enumerator, &encryption))
924 {
925 fprintf(out, "%N ", encryption_algorithm_names, encryption);
926 }
927 enumerator->destroy(enumerator);
928 fprintf(out, "\n integrity: ");
929 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
930 while (enumerator->enumerate(enumerator, &integrity))
931 {
932 fprintf(out, "%N ", integrity_algorithm_names, integrity);
933 }
934 enumerator->destroy(enumerator);
935 fprintf(out, "\n hasher: ");
936 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
937 while (enumerator->enumerate(enumerator, &hash))
938 {
939 fprintf(out, "%N ", hash_algorithm_names, hash);
940 }
941 enumerator->destroy(enumerator);
942 fprintf(out, "\n prf: ");
943 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
944 while (enumerator->enumerate(enumerator, &prf))
945 {
946 fprintf(out, "%N ", pseudo_random_function_names, prf);
947 }
948 enumerator->destroy(enumerator);
949 fprintf(out, "\n dh-group: ");
950 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
951 while (enumerator->enumerate(enumerator, &group))
952 {
953 fprintf(out, "%N ", diffie_hellman_group_names, group);
954 }
955 enumerator->destroy(enumerator);
956 fprintf(out, "\n");
957 }
958
959 /**
960 * Implementation of stroke_list_t.list.
961 */
962 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
963 {
964 linked_list_t *cert_list = NULL;
965
966 if (msg->list.flags & LIST_PUBKEYS)
967 {
968 linked_list_t *pubkey_list = create_unique_cert_list(CERT_TRUSTED_PUBKEY);
969
970 stroke_list_pubkeys(pubkey_list, msg->list.utc, out);
971 pubkey_list->destroy_offset(pubkey_list, offsetof(certificate_t, destroy));
972 }
973 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
974 {
975 cert_list = create_unique_cert_list(CERT_X509);
976 }
977 if (msg->list.flags & LIST_CERTS)
978 {
979 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
980 0, msg->list.utc, out);
981 }
982 if (msg->list.flags & LIST_CACERTS)
983 {
984 stroke_list_certs(cert_list, "X.509 CA Certificates",
985 X509_CA, msg->list.utc, out);
986 }
987 if (msg->list.flags & LIST_OCSPCERTS)
988 {
989 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
990 X509_OCSP_SIGNER, msg->list.utc, out);
991 }
992 if (msg->list.flags & LIST_AACERTS)
993 {
994 stroke_list_certs(cert_list, "X.509 AA Certificates",
995 X509_AA, msg->list.utc, out);
996 }
997 if (msg->list.flags & LIST_ACERTS)
998 {
999 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
1000
1001 stroke_list_acerts(ac_list, msg->list.utc, out);
1002 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
1003 }
1004 if (msg->list.flags & LIST_CRLS)
1005 {
1006 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
1007
1008 stroke_list_crls(crl_list, msg->list.utc, out);
1009 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
1010 }
1011 if (msg->list.flags & LIST_OCSP)
1012 {
1013 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
1014
1015 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
1016
1017 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
1018 }
1019 if (msg->list.flags & LIST_ALGS)
1020 {
1021 list_algs(out);
1022 }
1023 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
1024 }
1025
1026 /**
1027 * Print leases of a single pool
1028 */
1029 static void pool_leases(private_stroke_list_t *this, FILE *out, char *pool,
1030 host_t *address, u_int size, u_int online, u_int offline)
1031 {
1032 enumerator_t *enumerator;
1033 identification_t *id;
1034 host_t *lease;
1035 bool on;
1036 int found = 0;
1037
1038 fprintf(out, "Leases in pool '%s', usage: %lu/%lu, %lu online\n",
1039 pool, online + offline, size, online);
1040 enumerator = this->attribute->create_lease_enumerator(this->attribute, pool);
1041 while (enumerator && enumerator->enumerate(enumerator, &id, &lease, &on))
1042 {
1043 if (!address || address->ip_equals(address, lease))
1044 {
1045 fprintf(out, " %15H %s '%Y'\n",
1046 lease, on ? "online" : "offline", id);
1047 found++;
1048 }
1049 }
1050 enumerator->destroy(enumerator);
1051 if (!found)
1052 {
1053 fprintf(out, " no matching leases found\n");
1054 }
1055 }
1056
1057 /**
1058 * Implementation of stroke_list_t.leases
1059 */
1060 static void leases(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1061 {
1062 enumerator_t *enumerator;
1063 u_int size, offline, online;
1064 host_t *address = NULL;
1065 char *pool;
1066 int found = 0;
1067
1068 if (msg->leases.address)
1069 {
1070 address = host_create_from_string(msg->leases.address, 0);
1071 }
1072
1073 enumerator = this->attribute->create_pool_enumerator(this->attribute);
1074 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
1075 {
1076 if (!msg->leases.pool || streq(msg->leases.pool, pool))
1077 {
1078 pool_leases(this, out, pool, address, size, online, offline);
1079 found++;
1080 }
1081 }
1082 enumerator->destroy(enumerator);
1083 if (!found)
1084 {
1085 if (msg->leases.pool)
1086 {
1087 fprintf(out, "pool '%s' not found\n", msg->leases.pool);
1088 }
1089 else
1090 {
1091 fprintf(out, "no pools found\n");
1092 }
1093 }
1094 DESTROY_IF(address);
1095 }
1096
1097 /**
1098 * Implementation of stroke_list_t.destroy
1099 */
1100 static void destroy(private_stroke_list_t *this)
1101 {
1102 free(this);
1103 }
1104
1105 /*
1106 * see header file
1107 */
1108 stroke_list_t *stroke_list_create(stroke_attribute_t *attribute)
1109 {
1110 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
1111
1112 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
1113 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
1114 this->public.leases = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))leases;
1115 this->public.destroy = (void(*)(stroke_list_t*))destroy;
1116
1117 this->uptime = time(NULL);
1118 this->attribute = attribute;
1119
1120 return &this->public;
1121 }
1122