Replaced more complex iterator usages.
[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 enumerator_t *added = list->create_enumerator(list);
632 identification_t *issuer = cert->get_issuer(cert);
633 bool previous_same, same = FALSE, found = FALSE;
634 certificate_t *list_cert;
635
636 while (added->enumerate(added, (void**)&list_cert))
637 {
638 if (list_cert->equals(list_cert, cert))
639 { /* stop if we found a duplicate*/
640 found = TRUE;
641 break;
642 }
643 previous_same = same;
644 same = list_cert->has_issuer(list_cert, issuer);
645 if (previous_same && !same)
646 { /* group certificates with same issuer */
647 break;
648 }
649 }
650 if (!found)
651 {
652 list->insert_before(list, added, cert->get_ref(cert));
653 }
654 added->destroy(added);
655 }
656 enumerator->destroy(enumerator);
657 return list;
658 }
659
660 /**
661 * Print a single public key.
662 */
663 static void list_public_key(public_key_t *public, FILE *out)
664 {
665 private_key_t *private = NULL;
666 chunk_t keyid;
667 identification_t *id;
668 auth_cfg_t *auth;
669
670 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &keyid))
671 {
672 id = identification_create_from_encoding(ID_KEY_ID, keyid);
673 auth = auth_cfg_create();
674 private = lib->credmgr->get_private(lib->credmgr,
675 public->get_type(public), id, auth);
676 auth->destroy(auth);
677 id->destroy(id);
678 }
679
680 fprintf(out, " pubkey: %N %d bits%s\n",
681 key_type_names, public->get_type(public),
682 public->get_keysize(public),
683 private ? ", has private key" : "");
684 if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1, &keyid))
685 {
686 fprintf(out, " keyid: %#B\n", &keyid);
687 }
688 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &keyid))
689 {
690 fprintf(out, " subjkey: %#B\n", &keyid);
691 }
692 DESTROY_IF(private);
693 }
694
695 /**
696 * list all raw public keys
697 */
698 static void stroke_list_pubkeys(linked_list_t *list, bool utc, FILE *out)
699 {
700 bool first = TRUE;
701
702 enumerator_t *enumerator = list->create_enumerator(list);
703 certificate_t *cert;
704
705 while (enumerator->enumerate(enumerator, (void**)&cert))
706 {
707 public_key_t *public = cert->get_public_key(cert);
708
709 if (public)
710 {
711 if (first)
712 {
713 fprintf(out, "\n");
714 fprintf(out, "List of Raw Public Keys:\n");
715 first = FALSE;
716 }
717 fprintf(out, "\n");
718
719 list_public_key(public, out);
720 public->destroy(public);
721 }
722 }
723 enumerator->destroy(enumerator);
724 }
725
726 /**
727 * list OpenPGP certificates
728 */
729 static void stroke_list_pgp(linked_list_t *list,bool utc, FILE *out)
730 {
731 bool first = TRUE;
732 time_t now = time(NULL);
733 enumerator_t *enumerator = list->create_enumerator(list);
734 certificate_t *cert;
735
736 while (enumerator->enumerate(enumerator, (void**)&cert))
737 {
738 time_t created, until;
739 public_key_t *public;
740 pgp_certificate_t *pgp_cert = (pgp_certificate_t*)cert;
741 chunk_t fingerprint = pgp_cert->get_fingerprint(pgp_cert);
742
743 if (first)
744 {
745
746 fprintf(out, "\n");
747 fprintf(out, "List of PGP End Entity Certificates:\n");
748 first = FALSE;
749 }
750 fprintf(out, "\n");
751 fprintf(out, " userid: '%Y'\n", cert->get_subject(cert));
752
753 fprintf(out, " digest: %#B\n", &fingerprint);
754
755 /* list validity */
756 cert->get_validity(cert, &now, &created, &until);
757 fprintf(out, " created: %T\n", &created, utc);
758 fprintf(out, " until: %T%s\n", &until, utc,
759 (until == TIME_32_BIT_SIGNED_MAX) ? " (expires never)":"");
760
761 public = cert->get_public_key(cert);
762 if (public)
763 {
764 list_public_key(public, out);
765 public->destroy(public);
766 }
767 }
768 enumerator->destroy(enumerator);
769 }
770
771 /**
772 * list all X.509 certificates matching the flags
773 */
774 static void stroke_list_certs(linked_list_t *list, char *label,
775 x509_flag_t flags, bool utc, FILE *out)
776 {
777 bool first = TRUE;
778 time_t now = time(NULL);
779 enumerator_t *enumerator;
780 certificate_t *cert;
781 x509_flag_t flag_mask;
782
783 /* mask all auxiliary flags */
784 flag_mask = ~(X509_SERVER_AUTH | X509_CLIENT_AUTH |
785 X509_SELF_SIGNED | X509_IP_ADDR_BLOCKS );
786
787 enumerator = list->create_enumerator(list);
788 while (enumerator->enumerate(enumerator, (void**)&cert))
789 {
790 x509_t *x509 = (x509_t*)cert;
791 x509_flag_t x509_flags = x509->get_flags(x509) & flag_mask;
792
793 /* list only if flag is set or flag == 0 */
794 if ((x509_flags & flags) || (x509_flags == flags))
795 {
796 enumerator_t *enumerator;
797 identification_t *altName;
798 bool first_altName = TRUE;
799 u_int pathlen;
800 chunk_t serial, authkey;
801 time_t notBefore, notAfter;
802 public_key_t *public;
803
804 if (first)
805 {
806 fprintf(out, "\n");
807 fprintf(out, "List of %s:\n", label);
808 first = FALSE;
809 }
810 fprintf(out, "\n");
811
812 /* list subjectAltNames */
813 enumerator = x509->create_subjectAltName_enumerator(x509);
814 while (enumerator->enumerate(enumerator, (void**)&altName))
815 {
816 if (first_altName)
817 {
818 fprintf(out, " altNames: ");
819 first_altName = FALSE;
820 }
821 else
822 {
823 fprintf(out, ", ");
824 }
825 fprintf(out, "%Y", altName);
826 }
827 if (!first_altName)
828 {
829 fprintf(out, "\n");
830 }
831 enumerator->destroy(enumerator);
832
833 fprintf(out, " subject: \"%Y\"\n", cert->get_subject(cert));
834 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
835 serial = x509->get_serial(x509);
836 fprintf(out, " serial: %#B\n", &serial);
837
838 /* list validity */
839 cert->get_validity(cert, &now, &notBefore, &notAfter);
840 fprintf(out, " validity: not before %T, ", &notBefore, utc);
841 if (now < notBefore)
842 {
843 fprintf(out, "not valid yet (valid in %V)\n", &now, &notBefore);
844 }
845 else
846 {
847 fprintf(out, "ok\n");
848 }
849 fprintf(out, " not after %T, ", &notAfter, utc);
850 if (now > notAfter)
851 {
852 fprintf(out, "expired (%V ago)\n", &now, &notAfter);
853 }
854 else
855 {
856 fprintf(out, "ok");
857 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
858 {
859 fprintf(out, " (expires in %V)", &now, &notAfter);
860 }
861 fprintf(out, " \n");
862 }
863
864 public = cert->get_public_key(cert);
865 if (public)
866 {
867 list_public_key(public, out);
868 public->destroy(public);
869 }
870
871 /* list optional authorityKeyIdentifier */
872 authkey = x509->get_authKeyIdentifier(x509);
873 if (authkey.ptr)
874 {
875 fprintf(out, " authkey: %#B\n", &authkey);
876 }
877
878 /* list optional pathLenConstraint */
879 pathlen = x509->get_constraint(x509, X509_PATH_LEN);
880 if (pathlen != X509_NO_CONSTRAINT)
881 {
882 fprintf(out, " pathlen: %u\n", pathlen);
883 }
884
885 /* list optional ipAddrBlocks */
886 if (x509->get_flags(x509) & X509_IP_ADDR_BLOCKS)
887 {
888 traffic_selector_t *ipAddrBlock;
889 bool first_ipAddrBlock = TRUE;
890
891 fprintf(out, " addresses: ");
892 enumerator = x509->create_ipAddrBlock_enumerator(x509);
893 while (enumerator->enumerate(enumerator, &ipAddrBlock))
894 {
895 if (first_ipAddrBlock)
896 {
897 first_ipAddrBlock = FALSE;
898 }
899 else
900 {
901 fprintf(out, ", ");
902 }
903 fprintf(out, "%R", ipAddrBlock);
904 }
905 enumerator->destroy(enumerator);
906 fprintf(out, "\n");
907 }
908 }
909 }
910 enumerator->destroy(enumerator);
911 }
912
913 /**
914 * list all X.509 attribute certificates
915 */
916 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
917 {
918 bool first = TRUE;
919 time_t thisUpdate, nextUpdate, now = time(NULL);
920 enumerator_t *enumerator = list->create_enumerator(list);
921 certificate_t *cert;
922
923 while (enumerator->enumerate(enumerator, (void**)&cert))
924 {
925 ac_t *ac = (ac_t*)cert;
926 identification_t *id;
927 ietf_attributes_t *groups;
928 chunk_t chunk;
929
930 if (first)
931 {
932 fprintf(out, "\n");
933 fprintf(out, "List of X.509 Attribute Certificates:\n");
934 first = FALSE;
935 }
936 fprintf(out, "\n");
937
938 id = cert->get_subject(cert);
939 if (id)
940 {
941 fprintf(out, " holder: \"%Y\"\n", id);
942 }
943 id = ac->get_holderIssuer(ac);
944 if (id)
945 {
946 fprintf(out, " hissuer: \"%Y\"\n", id);
947 }
948 chunk = ac->get_holderSerial(ac);
949 if (chunk.ptr)
950 {
951 fprintf(out, " hserial: %#B\n", &chunk);
952 }
953 groups = ac->get_groups(ac);
954 if (groups)
955 {
956 fprintf(out, " groups: %s\n", groups->get_string(groups));
957 groups->destroy(groups);
958 }
959 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
960 chunk = ac->get_serial(ac);
961 fprintf(out, " serial: %#B\n", &chunk);
962
963 /* list validity */
964 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
965 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
966 fprintf(out, " next %T, ", &nextUpdate, utc);
967 if (now > nextUpdate)
968 {
969 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
970 }
971 else
972 {
973 fprintf(out, "ok");
974 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
975 {
976 fprintf(out, " (expires in %V)", &now, &nextUpdate);
977 }
978 fprintf(out, " \n");
979 }
980
981 /* list optional authorityKeyIdentifier */
982 chunk = ac->get_authKeyIdentifier(ac);
983 if (chunk.ptr)
984 {
985 fprintf(out, " authkey: %#B\n", &chunk);
986 }
987 }
988 enumerator->destroy(enumerator);
989 }
990
991 /**
992 * list all X.509 CRLs
993 */
994 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
995 {
996 bool first = TRUE;
997 time_t thisUpdate, nextUpdate, now = time(NULL);
998 enumerator_t *enumerator = list->create_enumerator(list);
999 certificate_t *cert;
1000
1001 while (enumerator->enumerate(enumerator, (void**)&cert))
1002 {
1003 crl_t *crl = (crl_t*)cert;
1004 chunk_t chunk;
1005
1006 if (first)
1007 {
1008 fprintf(out, "\n");
1009 fprintf(out, "List of X.509 CRLs:\n");
1010 first = FALSE;
1011 }
1012 fprintf(out, "\n");
1013
1014 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
1015
1016 /* list optional crlNumber */
1017 chunk = crl->get_serial(crl);
1018 if (chunk.ptr)
1019 {
1020 fprintf(out, " serial: %#B\n", &chunk);
1021 }
1022 if (crl->is_delta_crl(crl, &chunk))
1023 {
1024 fprintf(out, " delta for: %#B\n", &chunk);
1025 }
1026
1027 /* count the number of revoked certificates */
1028 {
1029 int count = 0;
1030 enumerator_t *enumerator = crl->create_enumerator(crl);
1031
1032 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
1033 {
1034 count++;
1035 }
1036 fprintf(out, " revoked: %d certificate%s\n", count,
1037 (count == 1)? "" : "s");
1038 enumerator->destroy(enumerator);
1039 }
1040
1041 /* list validity */
1042 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
1043 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
1044 fprintf(out, " next %T, ", &nextUpdate, utc);
1045 if (now > nextUpdate)
1046 {
1047 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
1048 }
1049 else
1050 {
1051 fprintf(out, "ok");
1052 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
1053 {
1054 fprintf(out, " (expires in %V)", &now, &nextUpdate);
1055 }
1056 fprintf(out, " \n");
1057 }
1058
1059 /* list optional authorityKeyIdentifier */
1060 chunk = crl->get_authKeyIdentifier(crl);
1061 if (chunk.ptr)
1062 {
1063 fprintf(out, " authkey: %#B\n", &chunk);
1064 }
1065 }
1066 enumerator->destroy(enumerator);
1067 }
1068
1069 /**
1070 * list all OCSP responses
1071 */
1072 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
1073 {
1074 bool first = TRUE, ok;
1075 enumerator_t *enumerator = list->create_enumerator(list);
1076 certificate_t *cert;
1077 time_t produced, usable, now = time(NULL);
1078
1079 while (enumerator->enumerate(enumerator, (void**)&cert))
1080 {
1081 if (first)
1082 {
1083 fprintf(out, "\n");
1084 fprintf(out, "List of OCSP responses:\n");
1085 fprintf(out, "\n");
1086 first = FALSE;
1087 }
1088 fprintf(out, " signer: \"%Y\"\n", cert->get_issuer(cert));
1089
1090 /* check validity */
1091 ok = cert->get_validity(cert, &now, &produced, &usable);
1092 fprintf(out, " validity: produced at %T\n", &produced, utc);
1093 fprintf(out, " usable till %T, ", &usable, utc);
1094 if (ok)
1095 {
1096 fprintf(out, "ok\n");
1097 }
1098 else
1099 {
1100 fprintf(out, "expired (%V ago)\n", &now, &usable);
1101 }
1102 }
1103 enumerator->destroy(enumerator);
1104 }
1105
1106 /**
1107 * Print the name of an algorithm plus the name of the plugin that registered it
1108 */
1109 static void print_alg(FILE *out, int *len, enum_name_t *alg_names, int alg_type,
1110 const char *plugin_name)
1111 {
1112 char alg_name[BUF_LEN];
1113 int alg_name_len;
1114
1115 alg_name_len = sprintf(alg_name, " %N[%s]", alg_names, alg_type, plugin_name);
1116 if (*len + alg_name_len > CRYPTO_MAX_ALG_LINE)
1117 {
1118 fprintf(out, "\n ");
1119 *len = 13;
1120 }
1121 fprintf(out, "%s", alg_name);
1122 *len += alg_name_len;
1123 }
1124
1125 /**
1126 * List of registered cryptographical algorithms
1127 */
1128 static void list_algs(FILE *out)
1129 {
1130 enumerator_t *enumerator;
1131 encryption_algorithm_t encryption;
1132 integrity_algorithm_t integrity;
1133 hash_algorithm_t hash;
1134 pseudo_random_function_t prf;
1135 diffie_hellman_group_t group;
1136 rng_quality_t quality;
1137 const char *plugin_name;
1138 int len;
1139
1140 fprintf(out, "\n");
1141 fprintf(out, "List of registered IKEv2 Algorithms:\n");
1142 fprintf(out, "\n encryption:");
1143 len = 13;
1144 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
1145 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
1146 {
1147 print_alg(out, &len, encryption_algorithm_names, encryption, plugin_name);
1148 }
1149 enumerator->destroy(enumerator);
1150 fprintf(out, "\n integrity: ");
1151 len = 13;
1152 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
1153 while (enumerator->enumerate(enumerator, &integrity, &plugin_name))
1154 {
1155 print_alg(out, &len, integrity_algorithm_names, integrity, plugin_name);
1156 }
1157 enumerator->destroy(enumerator);
1158 fprintf(out, "\n aead: ");
1159 len = 13;
1160 enumerator = lib->crypto->create_aead_enumerator(lib->crypto);
1161 while (enumerator->enumerate(enumerator, &encryption, &plugin_name))
1162 {
1163 print_alg(out, &len, encryption_algorithm_names, encryption, plugin_name);
1164 }
1165 enumerator->destroy(enumerator);
1166 fprintf(out, "\n hasher: ");
1167 len = 13;
1168 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
1169 while (enumerator->enumerate(enumerator, &hash, &plugin_name))
1170 {
1171 print_alg(out, &len, hash_algorithm_names, hash, plugin_name);
1172 }
1173 enumerator->destroy(enumerator);
1174 fprintf(out, "\n prf: ");
1175 len = 13;
1176 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
1177 while (enumerator->enumerate(enumerator, &prf, &plugin_name))
1178 {
1179 print_alg(out, &len, pseudo_random_function_names, prf, plugin_name);
1180 }
1181 enumerator->destroy(enumerator);
1182 fprintf(out, "\n dh-group: ");
1183 len = 13;
1184 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
1185 while (enumerator->enumerate(enumerator, &group, &plugin_name))
1186 {
1187 print_alg(out, &len, diffie_hellman_group_names, group, plugin_name);
1188 }
1189 enumerator->destroy(enumerator);
1190 fprintf(out, "\n random-gen:");
1191 len = 13;
1192 enumerator = lib->crypto->create_rng_enumerator(lib->crypto);
1193 while (enumerator->enumerate(enumerator, &quality, &plugin_name))
1194 {
1195 print_alg(out, &len, rng_quality_names, quality, plugin_name);
1196 }
1197 enumerator->destroy(enumerator);
1198 fprintf(out, "\n");
1199 }
1200
1201 METHOD(stroke_list_t, list, void,
1202 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1203 {
1204 linked_list_t *cert_list = NULL;
1205
1206 if (msg->list.flags & LIST_PUBKEYS)
1207 {
1208 linked_list_t *pubkey_list = create_unique_cert_list(CERT_TRUSTED_PUBKEY);
1209
1210 stroke_list_pubkeys(pubkey_list, msg->list.utc, out);
1211 pubkey_list->destroy_offset(pubkey_list, offsetof(certificate_t, destroy));
1212 }
1213 if (msg->list.flags & LIST_CERTS)
1214 {
1215 linked_list_t *pgp_list = create_unique_cert_list(CERT_GPG);
1216
1217 stroke_list_pgp(pgp_list, msg->list.utc, out);
1218 pgp_list->destroy_offset(pgp_list, offsetof(certificate_t, destroy));
1219 }
1220 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
1221 {
1222 cert_list = create_unique_cert_list(CERT_X509);
1223 }
1224 if (msg->list.flags & LIST_CERTS)
1225 {
1226 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
1227 X509_NONE, msg->list.utc, out);
1228 }
1229 if (msg->list.flags & LIST_CACERTS)
1230 {
1231 stroke_list_certs(cert_list, "X.509 CA Certificates",
1232 X509_CA, msg->list.utc, out);
1233 }
1234 if (msg->list.flags & LIST_OCSPCERTS)
1235 {
1236 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
1237 X509_OCSP_SIGNER, msg->list.utc, out);
1238 }
1239 if (msg->list.flags & LIST_AACERTS)
1240 {
1241 stroke_list_certs(cert_list, "X.509 AA Certificates",
1242 X509_AA, msg->list.utc, out);
1243 }
1244 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
1245
1246 if (msg->list.flags & LIST_ACERTS)
1247 {
1248 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
1249
1250 stroke_list_acerts(ac_list, msg->list.utc, out);
1251 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
1252 }
1253 if (msg->list.flags & LIST_CRLS)
1254 {
1255 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
1256
1257 stroke_list_crls(crl_list, msg->list.utc, out);
1258 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
1259 }
1260 if (msg->list.flags & LIST_OCSP)
1261 {
1262 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
1263
1264 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
1265
1266 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
1267 }
1268 if (msg->list.flags & LIST_ALGS)
1269 {
1270 list_algs(out);
1271 }
1272 }
1273
1274 /**
1275 * Print leases of a single pool
1276 */
1277 static void pool_leases(private_stroke_list_t *this, FILE *out, char *pool,
1278 host_t *address, u_int size, u_int online, u_int offline)
1279 {
1280 enumerator_t *enumerator;
1281 identification_t *id;
1282 host_t *lease;
1283 bool on;
1284 int found = 0;
1285
1286 fprintf(out, "Leases in pool '%s', usage: %u/%u, %u online\n",
1287 pool, online + offline, size, online);
1288 enumerator = this->attribute->create_lease_enumerator(this->attribute, pool);
1289 while (enumerator && enumerator->enumerate(enumerator, &id, &lease, &on))
1290 {
1291 if (!address || address->ip_equals(address, lease))
1292 {
1293 fprintf(out, " %15H %s '%Y'\n",
1294 lease, on ? "online" : "offline", id);
1295 found++;
1296 }
1297 }
1298 enumerator->destroy(enumerator);
1299 if (!found)
1300 {
1301 fprintf(out, " no matching leases found\n");
1302 }
1303 }
1304
1305 METHOD(stroke_list_t, leases, void,
1306 private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1307 {
1308 enumerator_t *enumerator;
1309 u_int size, offline, online;
1310 host_t *address = NULL;
1311 char *pool;
1312 int found = 0;
1313
1314 if (msg->leases.address)
1315 {
1316 address = host_create_from_string(msg->leases.address, 0);
1317 }
1318
1319 enumerator = this->attribute->create_pool_enumerator(this->attribute);
1320 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
1321 {
1322 if (!msg->leases.pool || streq(msg->leases.pool, pool))
1323 {
1324 pool_leases(this, out, pool, address, size, online, offline);
1325 found++;
1326 }
1327 }
1328 enumerator->destroy(enumerator);
1329 if (!found)
1330 {
1331 if (msg->leases.pool)
1332 {
1333 fprintf(out, "pool '%s' not found\n", msg->leases.pool);
1334 }
1335 else
1336 {
1337 fprintf(out, "no pools found\n");
1338 }
1339 }
1340 DESTROY_IF(address);
1341 }
1342
1343 METHOD(stroke_list_t, destroy, void,
1344 private_stroke_list_t *this)
1345 {
1346 free(this);
1347 }
1348
1349 /*
1350 * see header file
1351 */
1352 stroke_list_t *stroke_list_create(stroke_attribute_t *attribute)
1353 {
1354 private_stroke_list_t *this;
1355
1356 INIT(this,
1357 .public = {
1358
1359 .list = _list,
1360 .status = _status,
1361 .leases = _leases,
1362 .destroy = _destroy,
1363 },
1364 .uptime = time_monotonic(NULL),
1365 .attribute = attribute,
1366 );
1367
1368 return &this->public;
1369 }
1370