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