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