5b195e27a962ec87f86c4a073b88644b8f47ff48
[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, bool all)
393 {
394 enumerator_t *enumerator, *children;
395 ike_cfg_t *ike_cfg;
396 child_cfg_t *child_cfg;
397 child_sa_t *child_sa;
398 ike_sa_t *ike_sa;
399 bool first, found = FALSE;
400 char *name = msg->status.name;
401
402 if (all)
403 {
404 peer_cfg_t *peer_cfg;
405 char *plugin, *pool;
406 host_t *host;
407 u_int32_t dpd;
408 time_t since, now;
409 u_int size, online, offline;
410 now = time_monotonic(NULL);
411 since = time(NULL) - (now - this->uptime);
412
413 fprintf(out, "Status of IKEv2 charon daemon (strongSwan "VERSION"):\n");
414 fprintf(out, " uptime: %V, since %T\n", &now, &this->uptime, &since, FALSE);
415 #ifdef HAVE_MALLINFO
416 {
417 struct mallinfo mi = mallinfo();
418
419 fprintf(out, " malloc: sbrk %d, mmap %d, used %d, free %d\n",
420 mi.arena, mi.hblkhd, mi.uordblks, mi.fordblks);
421 }
422 #endif /* HAVE_MALLINFO */
423 fprintf(out, " worker threads: %d idle of %d,",
424 lib->processor->get_idle_threads(lib->processor),
425 lib->processor->get_total_threads(lib->processor));
426 fprintf(out, " job queue load: %d,",
427 lib->processor->get_job_load(lib->processor));
428 fprintf(out, " scheduled events: %d\n",
429 lib->scheduler->get_job_load(lib->scheduler));
430 fprintf(out, " loaded plugins: ");
431 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
432 while (enumerator->enumerate(enumerator, &plugin))
433 {
434 fprintf(out, "%s ", plugin);
435 }
436 enumerator->destroy(enumerator);
437 fprintf(out, "\n");
438
439 first = TRUE;
440 enumerator = this->attribute->create_pool_enumerator(this->attribute);
441 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
442 {
443 if (name && !streq(name, pool))
444 {
445 continue;
446 }
447 if (first)
448 {
449 first = FALSE;
450 fprintf(out, "Virtual IP pools (size/online/offline):\n");
451 }
452 fprintf(out, " %s: %u/%u/%u\n", pool, size, online, offline);
453 }
454 enumerator->destroy(enumerator);
455
456 enumerator = hydra->kernel_interface->create_address_enumerator(
457 hydra->kernel_interface, FALSE, FALSE);
458 fprintf(out, "Listening IP addresses:\n");
459 while (enumerator->enumerate(enumerator, (void**)&host))
460 {
461 fprintf(out, " %H\n", host);
462 }
463 enumerator->destroy(enumerator);
464
465 fprintf(out, "Connections:\n");
466 enumerator = charon->backends->create_peer_cfg_enumerator(
467 charon->backends, NULL, NULL, NULL, NULL);
468 while (enumerator->enumerate(enumerator, &peer_cfg))
469 {
470 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
471 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
472 {
473 continue;
474 }
475
476 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
477 fprintf(out, "%12s: %s...%s", peer_cfg->get_name(peer_cfg),
478 ike_cfg->get_my_addr(ike_cfg), ike_cfg->get_other_addr(ike_cfg));
479
480 dpd = peer_cfg->get_dpd(peer_cfg);
481 if (dpd)
482 {
483 fprintf(out, ", dpddelay=%us", dpd);
484 }
485 fprintf(out, "\n");
486
487 log_auth_cfgs(out, peer_cfg, TRUE);
488 log_auth_cfgs(out, peer_cfg, FALSE);
489
490 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
491 while (children->enumerate(children, &child_cfg))
492 {
493 linked_list_t *my_ts, *other_ts;
494
495 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
496 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
497 fprintf(out, "%12s: child: %#R=== %#R", child_cfg->get_name(child_cfg),
498 my_ts, other_ts);
499 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
500 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
501
502 if (dpd)
503 {
504 fprintf(out, ", dpdaction=%N", action_names,
505 child_cfg->get_dpd_action(child_cfg));
506 }
507 fprintf(out, "\n");
508 }
509 children->destroy(children);
510 }
511 enumerator->destroy(enumerator);
512 }
513
514 first = TRUE;
515 enumerator = charon->traps->create_enumerator(charon->traps);
516 while (enumerator->enumerate(enumerator, NULL, &child_sa))
517 {
518 if (first)
519 {
520 fprintf(out, "Routed Connections:\n");
521 first = FALSE;
522 }
523 log_child_sa(out, child_sa, all);
524 }
525 enumerator->destroy(enumerator);
526
527 fprintf(out, "Security Associations:\n");
528 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
529 while (enumerator->enumerate(enumerator, &ike_sa))
530 {
531 bool ike_printed = FALSE;
532 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
533
534 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
535 {
536 log_ike_sa(out, ike_sa, all);
537 found = TRUE;
538 ike_printed = TRUE;
539 }
540
541 while (children->iterate(children, (void**)&child_sa))
542 {
543 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
544 {
545 if (!ike_printed)
546 {
547 log_ike_sa(out, ike_sa, all);
548 found = TRUE;
549 ike_printed = TRUE;
550 }
551 log_child_sa(out, child_sa, all);
552 }
553 }
554 children->destroy(children);
555 }
556 enumerator->destroy(enumerator);
557
558 if (!found)
559 {
560 if (name)
561 {
562 fprintf(out, " no match\n");
563 }
564 else
565 {
566 fprintf(out, " none\n");
567 }
568 }
569 }
570
571 /**
572 * create a unique certificate list without duplicates
573 * certicates having the same issuer are grouped together.
574 */
575 static linked_list_t* create_unique_cert_list(certificate_type_t type)
576 {
577 linked_list_t *list = linked_list_create();
578 enumerator_t *enumerator = lib->credmgr->create_cert_enumerator(
579 lib->credmgr, type, KEY_ANY, NULL, FALSE);
580 certificate_t *cert;
581
582 while (enumerator->enumerate(enumerator, (void**)&cert))
583 {
584 iterator_t *iterator = list->create_iterator(list, TRUE);
585 identification_t *issuer = cert->get_issuer(cert);
586 bool previous_same, same = FALSE, last = TRUE;
587 certificate_t *list_cert;
588
589 while (iterator->iterate(iterator, (void**)&list_cert))
590 {
591 /* exit if we have a duplicate? */
592 if (list_cert->equals(list_cert, cert))
593 {
594 last = FALSE;
595 break;
596 }
597 /* group certificates with same issuer */
598 previous_same = same;
599 same = list_cert->has_issuer(list_cert, issuer);
600 if (previous_same && !same)
601 {
602 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
603 last = FALSE;
604 break;
605 }
606 }
607 iterator->destroy(iterator);
608
609 if (last)
610 {
611 list->insert_last(list, (void *)cert->get_ref(cert));
612 }
613 }
614 enumerator->destroy(enumerator);
615 return list;
616 }
617
618 /**
619 * Print a single public key.
620 */
621 static void list_public_key(public_key_t *public, FILE *out)
622 {
623 private_key_t *private = NULL;
624 chunk_t keyid;
625 identification_t *id;
626 auth_cfg_t *auth;
627
628 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &keyid))
629 {
630 id = identification_create_from_encoding(ID_KEY_ID, keyid);
631 auth = auth_cfg_create();
632 private = lib->credmgr->get_private(lib->credmgr,
633 public->get_type(public), id, auth);
634 auth->destroy(auth);
635 id->destroy(id);
636 }
637
638 fprintf(out, " pubkey: %N %d bits%s\n",
639 key_type_names, public->get_type(public),
640 public->get_keysize(public),
641 private ? ", has private key" : "");
642 if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1, &keyid))
643 {
644 fprintf(out, " keyid: %#B\n", &keyid);
645 }
646 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &keyid))
647 {
648 fprintf(out, " subjkey: %#B\n", &keyid);
649 }
650 DESTROY_IF(private);
651 }
652
653 /**
654 * list all raw public keys
655 */
656 static void stroke_list_pubkeys(linked_list_t *list, bool utc, FILE *out)
657 {
658 bool first = TRUE;
659
660 enumerator_t *enumerator = list->create_enumerator(list);
661 certificate_t *cert;
662
663 while (enumerator->enumerate(enumerator, (void**)&cert))
664 {
665 public_key_t *public = cert->get_public_key(cert);
666
667 if (public)
668 {
669 if (first)
670 {
671 fprintf(out, "\n");
672 fprintf(out, "List of Raw Public Keys:\n");
673 first = FALSE;
674 }
675 fprintf(out, "\n");
676
677 list_public_key(public, out);
678 public->destroy(public);
679 }
680 }
681 enumerator->destroy(enumerator);
682 }
683
684 /**
685 * list OpenPGP certificates
686 */
687 static void stroke_list_pgp(linked_list_t *list,bool utc, FILE *out)
688 {
689 bool first = TRUE;
690 time_t now = time(NULL);
691 enumerator_t *enumerator = list->create_enumerator(list);
692 certificate_t *cert;
693
694 while (enumerator->enumerate(enumerator, (void**)&cert))
695 {
696 time_t created, until;
697 public_key_t *public;
698 pgp_certificate_t *pgp_cert = (pgp_certificate_t*)cert;
699 chunk_t fingerprint = pgp_cert->get_fingerprint(pgp_cert);
700
701 if (first)
702 {
703
704 fprintf(out, "\n");
705 fprintf(out, "List of PGP End Entity Certificates:\n");
706 first = FALSE;
707 }
708 fprintf(out, "\n");
709 fprintf(out, " userid: '%Y'\n", cert->get_subject(cert));
710
711 fprintf(out, " digest: %#B\n", &fingerprint);
712
713 /* list validity */
714 cert->get_validity(cert, &now, &created, &until);
715 fprintf(out, " created: %T\n", &created, utc);
716 fprintf(out, " until: %T%s\n", &until, utc,
717 (until == TIME_32_BIT_SIGNED_MAX) ? " (expires never)":"");
718
719 public = cert->get_public_key(cert);
720 if (public)
721 {
722 list_public_key(public, out);
723 public->destroy(public);
724 }
725 }
726 enumerator->destroy(enumerator);
727 }
728
729 /**
730 * list all X.509 certificates matching the flags
731 */
732 static void stroke_list_certs(linked_list_t *list, char *label,
733 x509_flag_t flags, bool utc, FILE *out)
734 {
735 bool first = TRUE;
736 time_t now = time(NULL);
737 enumerator_t *enumerator;
738 certificate_t *cert;
739 x509_flag_t flag_mask;
740
741 /* mask all auxiliary flags */
742 flag_mask = ~(X509_SERVER_AUTH | X509_CLIENT_AUTH |
743 X509_SELF_SIGNED | X509_IP_ADDR_BLOCKS );
744
745 enumerator = list->create_enumerator(list);
746 while (enumerator->enumerate(enumerator, (void**)&cert))
747 {
748 x509_t *x509 = (x509_t*)cert;
749 x509_flag_t x509_flags = x509->get_flags(x509) & flag_mask;
750
751 /* list only if flag is set or flag == 0 */
752 if ((x509_flags & flags) || (x509_flags == flags))
753 {
754 enumerator_t *enumerator;
755 identification_t *altName;
756 bool first_altName = TRUE;
757 int pathlen;
758 chunk_t serial, authkey;
759 time_t notBefore, notAfter;
760 public_key_t *public;
761
762 if (first)
763 {
764 fprintf(out, "\n");
765 fprintf(out, "List of %s:\n", label);
766 first = FALSE;
767 }
768 fprintf(out, "\n");
769
770 /* list subjectAltNames */
771 enumerator = x509->create_subjectAltName_enumerator(x509);
772 while (enumerator->enumerate(enumerator, (void**)&altName))
773 {
774 if (first_altName)
775 {
776 fprintf(out, " altNames: ");
777 first_altName = FALSE;
778 }
779 else
780 {
781 fprintf(out, ", ");
782 }
783 fprintf(out, "%Y", altName);
784 }
785 if (!first_altName)
786 {
787 fprintf(out, "\n");
788 }
789 enumerator->destroy(enumerator);
790
791 fprintf(out, " subject: \"%Y\"\n", cert->get_subject(cert));
792 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
793 serial = x509->get_serial(x509);
794 fprintf(out, " serial: %#B\n", &serial);
795
796 /* list validity */
797 cert->get_validity(cert, &now, &notBefore, &notAfter);
798 fprintf(out, " validity: not before %T, ", &notBefore, utc);
799 if (now < notBefore)
800 {
801 fprintf(out, "not valid yet (valid in %V)\n", &now, &notBefore);
802 }
803 else
804 {
805 fprintf(out, "ok\n");
806 }
807 fprintf(out, " not after %T, ", &notAfter, utc);
808 if (now > notAfter)
809 {
810 fprintf(out, "expired (%V ago)\n", &now, &notAfter);
811 }
812 else
813 {
814 fprintf(out, "ok");
815 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
816 {
817 fprintf(out, " (expires in %V)", &now, &notAfter);
818 }
819 fprintf(out, " \n");
820 }
821
822 public = cert->get_public_key(cert);
823 if (public)
824 {
825 list_public_key(public, out);
826 public->destroy(public);
827 }
828
829 /* list optional authorityKeyIdentifier */
830 authkey = x509->get_authKeyIdentifier(x509);
831 if (authkey.ptr)
832 {
833 fprintf(out, " authkey: %#B\n", &authkey);
834 }
835
836 /* list optional pathLenConstraint */
837 pathlen = x509->get_constraint(x509, X509_PATH_LEN);
838 if (pathlen != X509_NO_CONSTRAINT)
839 {
840 fprintf(out, " pathlen: %d\n", pathlen);
841 }
842
843 /* list optional ipAddrBlocks */
844 if (x509->get_flags(x509) & X509_IP_ADDR_BLOCKS)
845 {
846 traffic_selector_t *ipAddrBlock;
847 bool first_ipAddrBlock = TRUE;
848
849 fprintf(out, " addresses: ");
850 enumerator = x509->create_ipAddrBlock_enumerator(x509);
851 while (enumerator->enumerate(enumerator, &ipAddrBlock))
852 {
853 if (first_ipAddrBlock)
854 {
855 first_ipAddrBlock = FALSE;
856 }
857 else
858 {
859 fprintf(out, ", ");
860 }
861 fprintf(out, "%R", ipAddrBlock);
862 }
863 enumerator->destroy(enumerator);
864 fprintf(out, "\n");
865 }
866 }
867 }
868 enumerator->destroy(enumerator);
869 }
870
871 /**
872 * list all X.509 attribute certificates
873 */
874 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
875 {
876 bool first = TRUE;
877 time_t thisUpdate, nextUpdate, now = time(NULL);
878 enumerator_t *enumerator = list->create_enumerator(list);
879 certificate_t *cert;
880
881 while (enumerator->enumerate(enumerator, (void**)&cert))
882 {
883 ac_t *ac = (ac_t*)cert;
884 identification_t *id;
885 ietf_attributes_t *groups;
886 chunk_t chunk;
887
888 if (first)
889 {
890 fprintf(out, "\n");
891 fprintf(out, "List of X.509 Attribute Certificates:\n");
892 first = FALSE;
893 }
894 fprintf(out, "\n");
895
896 id = cert->get_subject(cert);
897 if (id)
898 {
899 fprintf(out, " holder: \"%Y\"\n", id);
900 }
901 id = ac->get_holderIssuer(ac);
902 if (id)
903 {
904 fprintf(out, " hissuer: \"%Y\"\n", id);
905 }
906 chunk = ac->get_holderSerial(ac);
907 if (chunk.ptr)
908 {
909 fprintf(out, " hserial: %#B\n", &chunk);
910 }
911 groups = ac->get_groups(ac);
912 if (groups)
913 {
914 fprintf(out, " groups: %s\n", groups->get_string(groups));
915 groups->destroy(groups);
916 }
917 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
918 chunk = ac->get_serial(ac);
919 fprintf(out, " serial: %#B\n", &chunk);
920
921 /* list validity */
922 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
923 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
924 fprintf(out, " next %T, ", &nextUpdate, utc);
925 if (now > nextUpdate)
926 {
927 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
928 }
929 else
930 {
931 fprintf(out, "ok");
932 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
933 {
934 fprintf(out, " (expires in %V)", &now, &nextUpdate);
935 }
936 fprintf(out, " \n");
937 }
938
939 /* list optional authorityKeyIdentifier */
940 chunk = ac->get_authKeyIdentifier(ac);
941 if (chunk.ptr)
942 {
943 fprintf(out, " authkey: %#B\n", &chunk);
944 }
945 }
946 enumerator->destroy(enumerator);
947 }
948
949 /**
950 * list all X.509 CRLs
951 */
952 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
953 {
954 bool first = TRUE;
955 time_t thisUpdate, nextUpdate, now = time(NULL);
956 enumerator_t *enumerator = list->create_enumerator(list);
957 certificate_t *cert;
958
959 while (enumerator->enumerate(enumerator, (void**)&cert))
960 {
961 crl_t *crl = (crl_t*)cert;
962 chunk_t chunk;
963
964 if (first)
965 {
966 fprintf(out, "\n");
967 fprintf(out, "List of X.509 CRLs:\n");
968 first = FALSE;
969 }
970 fprintf(out, "\n");
971
972 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
973
974 /* list optional crlNumber */
975 chunk = crl->get_serial(crl);
976 if (chunk.ptr)
977 {
978 fprintf(out, " serial: %#B\n", &chunk);
979 }
980
981 /* count the number of revoked certificates */
982 {
983 int count = 0;
984 enumerator_t *enumerator = crl->create_enumerator(crl);
985
986 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
987 {
988 count++;
989 }
990 fprintf(out, " revoked: %d certificate%s\n", count,
991 (count == 1)? "" : "s");
992 enumerator->destroy(enumerator);
993 }
994
995 /* list validity */
996 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
997 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
998 fprintf(out, " next %T, ", &nextUpdate, utc);
999 if (now > nextUpdate)
1000 {
1001 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
1002 }
1003 else
1004 {
1005 fprintf(out, "ok");
1006 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
1007 {
1008 fprintf(out, " (expires in %V)", &now, &nextUpdate);
1009 }
1010 fprintf(out, " \n");
1011 }
1012
1013 /* list optional authorityKeyIdentifier */
1014 chunk = crl->get_authKeyIdentifier(crl);
1015 if (chunk.ptr)
1016 {
1017 fprintf(out, " authkey: %#B\n", &chunk);
1018 }
1019 }
1020 enumerator->destroy(enumerator);
1021 }
1022
1023 /**
1024 * list all OCSP responses
1025 */
1026 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
1027 {
1028 bool first = TRUE, ok;
1029 enumerator_t *enumerator = list->create_enumerator(list);
1030 certificate_t *cert;
1031 time_t produced, usable, now = time(NULL);
1032
1033 while (enumerator->enumerate(enumerator, (void**)&cert))
1034 {
1035 if (first)
1036 {
1037 fprintf(out, "\n");
1038 fprintf(out, "List of OCSP responses:\n");
1039 fprintf(out, "\n");
1040 first = FALSE;
1041 }
1042 fprintf(out, " signer: \"%Y\"\n", cert->get_issuer(cert));
1043
1044 /* check validity */
1045 ok = cert->get_validity(cert, &now, &produced, &usable);
1046 fprintf(out, " validity: produced at %T\n", &produced, utc);
1047 fprintf(out, " usable till %T, ", &usable, utc);
1048 if (ok)
1049 {
1050 fprintf(out, "ok\n");
1051 }
1052 else
1053 {
1054 fprintf(out, "expired (%V ago)\n", &now, &usable);
1055 }
1056 }
1057 enumerator->destroy(enumerator);
1058 }
1059
1060 /**
1061 * Print the name of an algorithm plus the name of the plugin that registered it
1062 */
1063 static void print_alg(FILE *out, int *len, enum_name_t *alg_names, int alg_type,
1064 const char *plugin_name)
1065 {
1066 char alg_name[BUF_LEN];
1067 int alg_name_len;
1068
1069 alg_name_len = sprintf(alg_name, " %N[%s]", alg_names, alg_type, plugin_name);
1070 if (*len + alg_name_len > CRYPTO_MAX_ALG_LINE)
1071 {
1072 fprintf(out, "\n ");
1073 *len = 13;
1074 }
1075 fprintf(out, "%s", alg_name);
1076 *len += alg_name_len;
1077 }
1078
1079 /**
1080 * List of registered cryptographical algorithms
1081 */
1082 static void list_algs(FILE *out)
1083 {
1084 enumerator_t *enumerator;
1085 encryption_algorithm_t encryption;
1086 integrity_algorithm_t integrity;
1087 hash_algorithm_t hash;
1088 pseudo_random_function_t prf;
1089 diffie_hellman_group_t group;
1090 rng_quality_t quality;
1091 const char *plugin_name;
1092 int len;
1093
1094 fprintf(out, "\n");
1095 fprintf(out, "List of registered IKEv2 Algorithms:\n");
1096 fprintf(out, "\n encryption:");
1097 len = 13;
1098 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
1099 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
1100 {
1101 print_alg(out, &len, encryption_algorithm_names, encryption, plugin_name);
1102 }
1103 enumerator->destroy(enumerator);
1104 fprintf(out, "\n integrity: ");
1105 len = 13;
1106 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
1107 while (enumerator->enumerate(enumerator, &integrity, &plugin_name))
1108 {
1109 print_alg(out, &len, integrity_algorithm_names, integrity, plugin_name);
1110 }
1111 enumerator->destroy(enumerator);
1112 fprintf(out, "\n aead: ");
1113 len = 13;
1114 enumerator = lib->crypto->create_aead_enumerator(lib->crypto);
1115 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
1116 {
1117 print_alg(out, &len, encryption_algorithm_names, encryption, plugin_name);
1118 }
1119 enumerator->destroy(enumerator);
1120 fprintf(out, "\n hasher: ");
1121 len = 13;
1122 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
1123 while (enumerator->enumerate(enumerator, &hash, &plugin_name))
1124 {
1125 print_alg(out, &len, hash_algorithm_names, hash, plugin_name);
1126 }
1127 enumerator->destroy(enumerator);
1128 fprintf(out, "\n prf: ");
1129 len = 13;
1130 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
1131 while (enumerator->enumerate(enumerator, &prf, &plugin_name))
1132 {
1133 print_alg(out, &len, pseudo_random_function_names, prf, plugin_name);
1134 }
1135 enumerator->destroy(enumerator);
1136 fprintf(out, "\n dh-group: ");
1137 len = 13;
1138 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
1139 while (enumerator->enumerate(enumerator, &group, &plugin_name))
1140 {
1141 print_alg(out, &len, diffie_hellman_group_names, group, plugin_name);
1142 }
1143 enumerator->destroy(enumerator);
1144 fprintf(out, "\n random-gen:");
1145 len = 13;
1146 enumerator = lib->crypto->create_rng_enumerator(lib->crypto);
1147 while (enumerator->enumerate(enumerator, &quality, &plugin_name))
1148 {
1149 print_alg(out, &len, rng_quality_names, quality, plugin_name);
1150 }
1151 enumerator->destroy(enumerator);
1152 fprintf(out, "\n");
1153 }
1154
1155 METHOD(stroke_list_t, list, void,
1156 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1157 {
1158 linked_list_t *cert_list = NULL;
1159
1160 if (msg->list.flags & LIST_PUBKEYS)
1161 {
1162 linked_list_t *pubkey_list = create_unique_cert_list(CERT_TRUSTED_PUBKEY);
1163
1164 stroke_list_pubkeys(pubkey_list, msg->list.utc, out);
1165 pubkey_list->destroy_offset(pubkey_list, offsetof(certificate_t, destroy));
1166 }
1167 if (msg->list.flags & LIST_CERTS)
1168 {
1169 linked_list_t *pgp_list = create_unique_cert_list(CERT_GPG);
1170
1171 stroke_list_pgp(pgp_list, msg->list.utc, out);
1172 pgp_list->destroy_offset(pgp_list, offsetof(certificate_t, destroy));
1173 }
1174 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
1175 {
1176 cert_list = create_unique_cert_list(CERT_X509);
1177 }
1178 if (msg->list.flags & LIST_CERTS)
1179 {
1180 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
1181 X509_NONE, msg->list.utc, out);
1182 }
1183 if (msg->list.flags & LIST_CACERTS)
1184 {
1185 stroke_list_certs(cert_list, "X.509 CA Certificates",
1186 X509_CA, msg->list.utc, out);
1187 }
1188 if (msg->list.flags & LIST_OCSPCERTS)
1189 {
1190 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
1191 X509_OCSP_SIGNER, msg->list.utc, out);
1192 }
1193 if (msg->list.flags & LIST_AACERTS)
1194 {
1195 stroke_list_certs(cert_list, "X.509 AA Certificates",
1196 X509_AA, msg->list.utc, out);
1197 }
1198 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
1199
1200 if (msg->list.flags & LIST_ACERTS)
1201 {
1202 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
1203
1204 stroke_list_acerts(ac_list, msg->list.utc, out);
1205 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
1206 }
1207 if (msg->list.flags & LIST_CRLS)
1208 {
1209 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
1210
1211 stroke_list_crls(crl_list, msg->list.utc, out);
1212 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
1213 }
1214 if (msg->list.flags & LIST_OCSP)
1215 {
1216 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
1217
1218 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
1219
1220 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
1221 }
1222 if (msg->list.flags & LIST_ALGS)
1223 {
1224 list_algs(out);
1225 }
1226 }
1227
1228 /**
1229 * Print leases of a single pool
1230 */
1231 static void pool_leases(private_stroke_list_t *this, FILE *out, char *pool,
1232 host_t *address, u_int size, u_int online, u_int offline)
1233 {
1234 enumerator_t *enumerator;
1235 identification_t *id;
1236 host_t *lease;
1237 bool on;
1238 int found = 0;
1239
1240 fprintf(out, "Leases in pool '%s', usage: %u/%u, %u online\n",
1241 pool, online + offline, size, online);
1242 enumerator = this->attribute->create_lease_enumerator(this->attribute, pool);
1243 while (enumerator && enumerator->enumerate(enumerator, &id, &lease, &on))
1244 {
1245 if (!address || address->ip_equals(address, lease))
1246 {
1247 fprintf(out, " %15H %s '%Y'\n",
1248 lease, on ? "online" : "offline", id);
1249 found++;
1250 }
1251 }
1252 enumerator->destroy(enumerator);
1253 if (!found)
1254 {
1255 fprintf(out, " no matching leases found\n");
1256 }
1257 }
1258
1259 METHOD(stroke_list_t, leases, void,
1260 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1261 {
1262 enumerator_t *enumerator;
1263 u_int size, offline, online;
1264 host_t *address = NULL;
1265 char *pool;
1266 int found = 0;
1267
1268 if (msg->leases.address)
1269 {
1270 address = host_create_from_string(msg->leases.address, 0);
1271 }
1272
1273 enumerator = this->attribute->create_pool_enumerator(this->attribute);
1274 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
1275 {
1276 if (!msg->leases.pool || streq(msg->leases.pool, pool))
1277 {
1278 pool_leases(this, out, pool, address, size, online, offline);
1279 found++;
1280 }
1281 }
1282 enumerator->destroy(enumerator);
1283 if (!found)
1284 {
1285 if (msg->leases.pool)
1286 {
1287 fprintf(out, "pool '%s' not found\n", msg->leases.pool);
1288 }
1289 else
1290 {
1291 fprintf(out, "no pools found\n");
1292 }
1293 }
1294 DESTROY_IF(address);
1295 }
1296
1297 METHOD(stroke_list_t, destroy, void,
1298 private_stroke_list_t *this)
1299 {
1300 free(this);
1301 }
1302
1303 /*
1304 * see header file
1305 */
1306 stroke_list_t *stroke_list_create(stroke_attribute_t *attribute)
1307 {
1308 private_stroke_list_t *this;
1309
1310 INIT(this,
1311 .public = {
1312
1313 .list = _list,
1314 .status = _status,
1315 .leases = _leases,
1316 .destroy = _destroy,
1317 },
1318 .uptime = time_monotonic(NULL),
1319 .attribute = attribute,
1320 );
1321
1322 return &this->public;
1323 }
1324