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