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