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