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