streamlined ipsec listalgs output
[strongswan.git] / src / charon / 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 * $Id$
16 */
17
18 #include "stroke_list.h"
19
20 #include <daemon.h>
21 #include <utils/linked_list.h>
22 #include <credentials/certificates/x509.h>
23 #include <credentials/certificates/ac.h>
24 #include <credentials/certificates/crl.h>
25 #include <config/peer_cfg.h>
26
27 /* warning intervals for list functions */
28 #define CERT_WARNING_INTERVAL 30 /* days */
29 #define CRL_WARNING_INTERVAL 7 /* days */
30 #define AC_WARNING_INTERVAL 1 /* day */
31
32 typedef struct private_stroke_list_t private_stroke_list_t;
33
34 /**
35 * private data of stroke_list
36 */
37 struct private_stroke_list_t {
38
39 /**
40 * public functions
41 */
42 stroke_list_t public;
43
44 /**
45 * timestamp of daemon start
46 */
47 time_t uptime;
48 };
49
50 /**
51 * get the authentication class of a config
52 */
53 auth_class_t get_auth_class(peer_cfg_t *config)
54 {
55 auth_class_t *class;
56 auth_info_t *auth_info;
57
58 auth_info = config->get_auth(config);
59 if (auth_info->get_item(auth_info, AUTHN_AUTH_CLASS, (void**)&class))
60 {
61 return *class;
62 }
63 /* fallback to pubkey authentication */
64 return AUTH_CLASS_PUBKEY;
65 }
66
67 /**
68 * log an IKE_SA to out
69 */
70 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
71 {
72 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
73
74 fprintf(out, "%12s[%d]: %N, %H[%D]...%H[%D]\n",
75 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
76 ike_sa_state_names, ike_sa->get_state(ike_sa),
77 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
78 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
79
80 if (all)
81 {
82 char *ike_proposal = ike_sa->get_proposal(ike_sa);
83
84 fprintf(out, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s",
85 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
86 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
87 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
88
89
90 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
91 {
92 u_int32_t rekey = ike_sa->get_statistic(ike_sa, STAT_REKEY_TIME);
93 u_int32_t reauth = ike_sa->get_statistic(ike_sa, STAT_REAUTH_TIME);
94
95 if (rekey)
96 {
97 fprintf(out, ", rekeying in %V", &rekey);
98 }
99 if (reauth)
100 {
101 fprintf(out, ", %N reauthentication in %V", auth_class_names,
102 get_auth_class(ike_sa->get_peer_cfg(ike_sa)), &reauth);
103 }
104 if (!rekey && !reauth)
105 {
106 fprintf(out, ", rekeying disabled");
107 }
108 }
109 fprintf(out, "\n");
110
111 if (ike_proposal)
112 {
113 fprintf(out, "%12s[%d]: IKE proposal: %s\n",
114 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
115 ike_proposal);
116 }
117 }
118 }
119
120 /**
121 * log an CHILD_SA to out
122 */
123 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
124 {
125 u_int32_t rekey, now = time(NULL);
126 u_int32_t use_in, use_out, use_fwd;
127 encryption_algorithm_t encr_alg;
128 integrity_algorithm_t int_alg;
129 size_t encr_len, int_len;
130 mode_t mode;
131
132 child_sa->get_stats(child_sa, &mode, &encr_alg, &encr_len,
133 &int_alg, &int_len, &rekey, &use_in, &use_out,
134 &use_fwd);
135
136 fprintf(out, "%12s{%d}: %N, %N",
137 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
138 child_sa_state_names, child_sa->get_state(child_sa),
139 mode_names, mode);
140
141 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
142 {
143 u_int16_t my_cpi = child_sa->get_cpi(child_sa, TRUE);
144 u_int16_t other_cpi = child_sa->get_cpi(child_sa, FALSE);
145
146 fprintf(out, ", %N SPIs: %.8x_i %.8x_o",
147 protocol_id_names, child_sa->get_protocol(child_sa),
148 ntohl(child_sa->get_spi(child_sa, TRUE)),
149 ntohl(child_sa->get_spi(child_sa, FALSE)));
150
151 /* Is IPCOMP activated ? */
152 if (my_cpi && other_cpi)
153 {
154 fprintf(out, ", IPCOMP CPIs: %.4x_i %.4x_o",
155 ntohs(my_cpi), ntohs(other_cpi));
156 }
157
158 if (all)
159 {
160 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
161 child_sa->get_reqid(child_sa));
162
163
164 if (child_sa->get_protocol(child_sa) == PROTO_ESP)
165 {
166 fprintf(out, "%N", encryption_algorithm_names, encr_alg);
167
168 if (encr_len)
169 {
170 fprintf(out, "-%d", encr_len);
171 }
172 if (int_alg != AUTH_UNDEFINED)
173 {
174 fprintf(out, "/");
175 }
176 }
177
178 if (int_alg != AUTH_UNDEFINED)
179 {
180 fprintf(out, "%N", integrity_algorithm_names, int_alg);
181 if (int_len)
182 {
183 fprintf(out, "-%d", int_len);
184 }
185 }
186 fprintf(out, ", rekeying ");
187
188 if (rekey)
189 {
190 fprintf(out, "in %#V", &now, &rekey);
191 }
192 else
193 {
194 fprintf(out, "disabled");
195 }
196
197 fprintf(out, ", last use: ");
198 use_in = max(use_in, use_fwd);
199 if (use_in)
200 {
201 fprintf(out, "%ds_i ", now - use_in);
202 }
203 else
204 {
205 fprintf(out, "no_i ");
206 }
207 if (use_out)
208 {
209 fprintf(out, "%ds_o ", now - use_out);
210 }
211 else
212 {
213 fprintf(out, "no_o ");
214 }
215 }
216 }
217
218 fprintf(out, "\n%12s{%d}: %#R=== %#R\n",
219 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
220 child_sa->get_traffic_selectors(child_sa, TRUE),
221 child_sa->get_traffic_selectors(child_sa, FALSE));
222 }
223
224 /**
225 * Implementation of stroke_list_t.status.
226 */
227 static void status(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out, bool all)
228 {
229 enumerator_t *enumerator, *children;
230 iterator_t *iterator;
231 ike_cfg_t *ike_cfg;
232 child_cfg_t *child_cfg;
233 ike_sa_t *ike_sa;
234 bool found = FALSE;
235 char *name = msg->status.name;
236
237 if (all)
238 {
239 peer_cfg_t *peer_cfg;
240 char *plugin;
241 host_t *host;
242 u_int32_t dpd;
243 time_t uptime = time(NULL) - this->uptime;
244
245 fprintf(out, "Performance:\n");
246 fprintf(out, " uptime: %V, since %#T\n", &uptime, &this->uptime, FALSE);
247 fprintf(out, " worker threads: %d idle of %d,",
248 charon->processor->get_idle_threads(charon->processor),
249 charon->processor->get_total_threads(charon->processor));
250 fprintf(out, " job queue load: %d,",
251 charon->processor->get_job_load(charon->processor));
252 fprintf(out, " scheduled events: %d\n",
253 charon->scheduler->get_job_load(charon->scheduler));
254 fprintf(out, " loaded plugins: ");
255 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
256 while (enumerator->enumerate(enumerator, &plugin))
257 {
258 fprintf(out, "%s ", plugin);
259 }
260 enumerator->destroy(enumerator);
261 fprintf(out, "\n");
262
263 iterator = charon->kernel_interface->create_address_iterator(
264 charon->kernel_interface);
265 fprintf(out, "Listening IP addresses:\n");
266 while (iterator->iterate(iterator, (void**)&host))
267 {
268 fprintf(out, " %H\n", host);
269 }
270 iterator->destroy(iterator);
271
272 fprintf(out, "Connections:\n");
273 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends);
274 while (enumerator->enumerate(enumerator, (void**)&peer_cfg))
275 {
276 void *ptr;
277 certificate_t *cert;
278 auth_item_t item;
279 auth_info_t *auth;
280 enumerator_t *auth_enumerator;
281 identification_t *my_ca = NULL, *other_ca = NULL;
282 identification_t *eap_identity = NULL;
283 u_int32_t *eap_type = NULL;
284
285 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
286 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
287 {
288 continue;
289 }
290
291 /* determine any required CAs */
292 auth = peer_cfg->get_auth(peer_cfg);
293 auth_enumerator = auth->create_item_enumerator(auth);
294 while (auth_enumerator->enumerate(auth_enumerator, &item, &ptr))
295 {
296 switch (item)
297 {
298 case AUTHN_EAP_TYPE:
299 eap_type = (u_int32_t *)ptr;
300 break;
301 case AUTHN_EAP_IDENTITY:
302 eap_identity = (identification_t *)ptr;
303 break;
304 case AUTHN_CA_CERT:
305 cert = (certificate_t *)ptr;
306 my_ca = cert->get_subject(cert);
307 break;
308 case AUTHN_CA_CERT_NAME:
309 my_ca = (identification_t *)ptr;
310 break;
311 case AUTHZ_CA_CERT:
312 cert = (certificate_t *)ptr;
313 other_ca = cert->get_subject(cert);
314 break;
315 case AUTHZ_CA_CERT_NAME:
316 other_ca = (identification_t *)ptr;
317 break;
318 default:
319 break;
320 }
321 }
322 auth_enumerator->destroy(auth_enumerator);
323
324 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
325 fprintf(out, "%12s: %s[%D]...%s[%D]\n", peer_cfg->get_name(peer_cfg),
326 ike_cfg->get_my_addr(ike_cfg), peer_cfg->get_my_id(peer_cfg),
327 ike_cfg->get_other_addr(ike_cfg), peer_cfg->get_other_id(peer_cfg));
328 if (my_ca || other_ca)
329 {
330 fprintf(out, "%12s: CAs: ", peer_cfg->get_name(peer_cfg));
331 if (my_ca)
332 {
333 fprintf(out, "\"%D\"...", my_ca);
334 }
335 else
336 {
337 fprintf(out, "%%any...");
338 }
339 if (other_ca)
340 {
341 fprintf(out, "\"%D\"\n", other_ca);
342 }
343 else
344 {
345 fprintf(out, "%%any\n");
346 }
347 }
348
349 fprintf(out, "%12s: %N ", peer_cfg->get_name(peer_cfg),
350 auth_class_names, get_auth_class(peer_cfg));
351 if (eap_type)
352 {
353 fprintf(out, "and %N ", eap_type_names, *eap_type);
354 }
355 fprintf(out, "authentication");
356 if (eap_identity)
357 {
358 fprintf(out, ", EAP identity: '%D'", eap_identity);
359 }
360 dpd = peer_cfg->get_dpd(peer_cfg);
361 if (dpd)
362 {
363 fprintf(out, ", dpddelay=%us", dpd);
364 }
365 fprintf(out, "\n");
366
367 /* TODO: list groups */
368
369 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
370 while (children->enumerate(children, &child_cfg))
371 {
372 linked_list_t *my_ts, *other_ts;
373
374 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
375 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
376 fprintf(out, "%12s: %#R=== %#R", child_cfg->get_name(child_cfg),
377 my_ts, other_ts);
378 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
379 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
380
381 if (dpd)
382 {
383 fprintf(out, ", dpdaction=%N", action_names,
384 child_cfg->get_dpd_action(child_cfg));
385 }
386 fprintf(out, "\n");
387 }
388 children->destroy(children);
389 }
390 enumerator->destroy(enumerator);
391 }
392
393 fprintf(out, "Security Associations:\n");
394 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
395 while (enumerator->enumerate(enumerator, &ike_sa))
396 {
397 bool ike_printed = FALSE;
398 child_sa_t *child_sa;
399 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
400
401 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
402 {
403 log_ike_sa(out, ike_sa, all);
404 found = TRUE;
405 ike_printed = TRUE;
406 }
407
408 while (children->iterate(children, (void**)&child_sa))
409 {
410 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
411 {
412 if (!ike_printed)
413 {
414 log_ike_sa(out, ike_sa, all);
415 found = TRUE;
416 ike_printed = TRUE;
417 }
418 log_child_sa(out, child_sa, all);
419 }
420 }
421 children->destroy(children);
422 }
423 enumerator->destroy(enumerator);
424
425 if (!found)
426 {
427 if (name)
428 {
429 fprintf(out, " no match\n");
430 }
431 else
432 {
433 fprintf(out, " none\n");
434 }
435 }
436 }
437
438 /**
439 * create a unique certificate list without duplicates
440 * certicates having the same issuer are grouped together.
441 */
442 static linked_list_t* create_unique_cert_list(certificate_type_t type)
443 {
444 linked_list_t *list = linked_list_create();
445 enumerator_t *enumerator = charon->credentials->create_cert_enumerator(
446 charon->credentials, type, KEY_ANY,
447 NULL, FALSE);
448 certificate_t *cert;
449
450 while (enumerator->enumerate(enumerator, (void**)&cert))
451 {
452 iterator_t *iterator = list->create_iterator(list, TRUE);
453 identification_t *issuer = cert->get_issuer(cert);
454 bool previous_same, same = FALSE, last = TRUE;
455 certificate_t *list_cert;
456
457 while (iterator->iterate(iterator, (void**)&list_cert))
458 {
459 /* exit if we have a duplicate? */
460 if (list_cert->equals(list_cert, cert))
461 {
462 last = FALSE;
463 break;
464 }
465 /* group certificates with same issuer */
466 previous_same = same;
467 same = list_cert->has_issuer(list_cert, issuer);
468 if (previous_same && !same)
469 {
470 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
471 last = FALSE;
472 break;
473 }
474 }
475 iterator->destroy(iterator);
476
477 if (last)
478 {
479 list->insert_last(list, (void *)cert->get_ref(cert));
480 }
481 }
482 enumerator->destroy(enumerator);
483 return list;
484 }
485
486 /**
487 * list all X.509 certificates matching the flags
488 */
489 static void stroke_list_certs(linked_list_t *list, char *label,
490 x509_flag_t flags, bool utc, FILE *out)
491 {
492 bool first = TRUE;
493 time_t now = time(NULL);
494 enumerator_t *enumerator = list->create_enumerator(list);
495 certificate_t *cert;
496
497 while (enumerator->enumerate(enumerator, (void**)&cert))
498 {
499 x509_t *x509 = (x509_t*)cert;
500 x509_flag_t x509_flags = x509->get_flags(x509);
501
502 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
503 if ((x509_flags & flags) || (flags == (x509_flags & ~X509_SELF_SIGNED)))
504 {
505 enumerator_t *enumerator;
506 identification_t *altName;
507 bool first_altName = TRUE;
508 chunk_t serial = x509->get_serial(x509);
509 identification_t *authkey = x509->get_authKeyIdentifier(x509);
510 time_t notBefore, notAfter;
511 public_key_t *public = cert->get_public_key(cert);
512
513 if (first)
514 {
515 fprintf(out, "\n");
516 fprintf(out, "List of %s:\n", label);
517 first = FALSE;
518 }
519 fprintf(out, "\n");
520
521 /* list subjectAltNames */
522 enumerator = x509->create_subjectAltName_enumerator(x509);
523 while (enumerator->enumerate(enumerator, (void**)&altName))
524 {
525 if (first_altName)
526 {
527 fprintf(out, " altNames: ");
528 first_altName = FALSE;
529 }
530 else
531 {
532 fprintf(out, ", ");
533 }
534 fprintf(out, "%D", altName);
535 }
536 if (!first_altName)
537 {
538 fprintf(out, "\n");
539 }
540 enumerator->destroy(enumerator);
541
542 fprintf(out, " subject: \"%D\"\n", cert->get_subject(cert));
543 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
544 fprintf(out, " serial: %#B\n", &serial);
545
546 /* list validity */
547 cert->get_validity(cert, &now, &notBefore, &notAfter);
548 fprintf(out, " validity: not before %#T, ", &notBefore, utc);
549 if (now < notBefore)
550 {
551 fprintf(out, "not valid yet (valid in %#V)\n", &now, &notBefore);
552 }
553 else
554 {
555 fprintf(out, "ok\n");
556 }
557 fprintf(out, " not after %#T, ", &notAfter, utc);
558 if (now > notAfter)
559 {
560 fprintf(out, "expired (%#V ago)\n", &now, &notAfter);
561 }
562 else
563 {
564 fprintf(out, "ok");
565 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
566 {
567 fprintf(out, " (expires in %#V)", &now, &notAfter);
568 }
569 fprintf(out, " \n");
570 }
571
572 /* list public key information */
573 if (public)
574 {
575 private_key_t *private = NULL;
576 identification_t *id, *keyid;
577
578 id = public->get_id(public, ID_PUBKEY_SHA1);
579 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
580
581 private = charon->credentials->get_private(
582 charon->credentials,
583 public->get_type(public), keyid, NULL);
584 fprintf(out, " pubkey: %N %d bits%s\n",
585 key_type_names, public->get_type(public),
586 public->get_keysize(public) * 8,
587 private ? ", has private key" : "");
588 fprintf(out, " keyid: %D\n", keyid);
589 fprintf(out, " subjkey: %D\n", id);
590 DESTROY_IF(private);
591 public->destroy(public);
592 }
593
594 /* list optional authorityKeyIdentifier */
595 if (authkey)
596 {
597 fprintf(out, " authkey: %D\n", authkey);
598 }
599 }
600 }
601 enumerator->destroy(enumerator);
602 }
603
604 /**
605 * list all X.509 attribute certificates
606 */
607 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
608 {
609 bool first = TRUE;
610 time_t thisUpdate, nextUpdate, now = time(NULL);
611 enumerator_t *enumerator = list->create_enumerator(list);
612 certificate_t *cert;
613
614 while (enumerator->enumerate(enumerator, (void**)&cert))
615 {
616 ac_t *ac = (ac_t*)cert;
617 chunk_t serial = ac->get_serial(ac);
618 chunk_t holderSerial = ac->get_holderSerial(ac);
619 identification_t *holderIssuer = ac->get_holderIssuer(ac);
620 identification_t *authkey = ac->get_authKeyIdentifier(ac);
621 identification_t *entityName = cert->get_subject(cert);
622
623 if (first)
624 {
625 fprintf(out, "\n");
626 fprintf(out, "List of X.509 Attribute Certificates:\n");
627 first = FALSE;
628 }
629 fprintf(out, "\n");
630
631 if (entityName)
632 {
633 fprintf(out, " holder: \"%D\"\n", entityName);
634 }
635 if (holderIssuer)
636 {
637 fprintf(out, " hissuer: \"%D\"\n", holderIssuer);
638 }
639 if (holderSerial.ptr)
640 {
641 fprintf(out, " hserial: %#B\n", &holderSerial);
642 }
643 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
644 fprintf(out, " serial: %#B\n", &serial);
645
646 /* list validity */
647 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
648 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
649 fprintf(out, " next %#T, ", &nextUpdate, utc);
650 if (now > nextUpdate)
651 {
652 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
653 }
654 else
655 {
656 fprintf(out, "ok");
657 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
658 {
659 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
660 }
661 fprintf(out, " \n");
662 }
663
664 /* list optional authorityKeyIdentifier */
665 if (authkey)
666 {
667 fprintf(out, " authkey: %D\n", authkey);
668 }
669 }
670 enumerator->destroy(enumerator);
671 }
672
673 /**
674 * list all X.509 CRLs
675 */
676 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
677 {
678 bool first = TRUE;
679 time_t thisUpdate, nextUpdate, now = time(NULL);
680 enumerator_t *enumerator = list->create_enumerator(list);
681 certificate_t *cert;
682
683 while (enumerator->enumerate(enumerator, (void**)&cert))
684 {
685 crl_t *crl = (crl_t*)cert;
686 chunk_t serial = crl->get_serial(crl);
687 identification_t *authkey = crl->get_authKeyIdentifier(crl);
688
689 if (first)
690 {
691 fprintf(out, "\n");
692 fprintf(out, "List of X.509 CRLs:\n");
693 first = FALSE;
694 }
695 fprintf(out, "\n");
696
697 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
698
699 /* list optional crlNumber */
700 if (serial.ptr)
701 {
702 fprintf(out, " serial: %#B\n", &serial);
703 }
704
705 /* count the number of revoked certificates */
706 {
707 int count = 0;
708 enumerator_t *enumerator = crl->create_enumerator(crl);
709
710 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
711 {
712 count++;
713 }
714 fprintf(out, " revoked: %d certificate%s\n", count,
715 (count == 1)? "" : "s");
716 enumerator->destroy(enumerator);
717 }
718
719 /* list validity */
720 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
721 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
722 fprintf(out, " next %#T, ", &nextUpdate, utc);
723 if (now > nextUpdate)
724 {
725 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
726 }
727 else
728 {
729 fprintf(out, "ok");
730 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
731 {
732 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
733 }
734 fprintf(out, " \n");
735 }
736
737 /* list optional authorityKeyIdentifier */
738 if (authkey)
739 {
740 fprintf(out, " authkey: %D\n", authkey);
741 }
742 }
743 enumerator->destroy(enumerator);
744 }
745
746 /**
747 * list all OCSP responses
748 */
749 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
750 {
751 bool first = TRUE;
752 enumerator_t *enumerator = list->create_enumerator(list);
753 certificate_t *cert;
754
755 while (enumerator->enumerate(enumerator, (void**)&cert))
756 {
757 if (first)
758 {
759 fprintf(out, "\n");
760 fprintf(out, "List of OCSP responses:\n");
761 fprintf(out, "\n");
762 first = FALSE;
763 }
764
765 fprintf(out, " signer: \"%D\"\n", cert->get_issuer(cert));
766 }
767 enumerator->destroy(enumerator);
768 }
769
770 /**
771 * List of registered cryptographical algorithms
772 */
773 static void list_algs(FILE *out)
774 {
775 enumerator_t *enumerator;
776 encryption_algorithm_t encryption;
777 integrity_algorithm_t integrity;
778 hash_algorithm_t hash;
779 pseudo_random_function_t prf;
780 diffie_hellman_group_t group;
781
782 fprintf(out, "\n");
783 fprintf(out, "List of registered IKEv2 Algorithms:\n");
784 fprintf(out, "\n encryption: ");
785 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
786 while (enumerator->enumerate(enumerator, &encryption))
787 {
788 fprintf(out, "%N ", encryption_algorithm_names, encryption);
789 }
790 enumerator->destroy(enumerator);
791 fprintf(out, "\n integrity: ");
792 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
793 while (enumerator->enumerate(enumerator, &integrity))
794 {
795 fprintf(out, "%N ", integrity_algorithm_names, integrity);
796 }
797 enumerator->destroy(enumerator);
798 fprintf(out, "\n hasher: ");
799 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
800 while (enumerator->enumerate(enumerator, &hash))
801 {
802 fprintf(out, "%N ", hash_algorithm_names, hash);
803 }
804 enumerator->destroy(enumerator);
805 fprintf(out, "\n prf: ");
806 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
807 while (enumerator->enumerate(enumerator, &prf))
808 {
809 fprintf(out, "%N ", pseudo_random_function_names, prf);
810 }
811 enumerator->destroy(enumerator);
812 fprintf(out, "\n dh-group: ");
813 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
814 while (enumerator->enumerate(enumerator, &group))
815 {
816 fprintf(out, "%N ", diffie_hellman_group_names, group);
817 }
818 enumerator->destroy(enumerator);
819 fprintf(out, "\n");
820 }
821
822 /**
823 * Implementation of stroke_list_t.list.
824 */
825 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
826 {
827 linked_list_t *cert_list = NULL;
828
829 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
830 {
831 cert_list = create_unique_cert_list(CERT_X509);
832 }
833 if (msg->list.flags & LIST_CERTS)
834 {
835 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
836 0, msg->list.utc, out);
837 }
838 if (msg->list.flags & LIST_CACERTS)
839 {
840 stroke_list_certs(cert_list, "X.509 CA Certificates",
841 X509_CA, msg->list.utc, out);
842 }
843 if (msg->list.flags & LIST_OCSPCERTS)
844 {
845 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
846 X509_OCSP_SIGNER, msg->list.utc, out);
847 }
848 if (msg->list.flags & LIST_AACERTS)
849 {
850 stroke_list_certs(cert_list, "X.509 AA Certificates",
851 X509_AA, msg->list.utc, out);
852 }
853 if (msg->list.flags & LIST_ACERTS)
854 {
855 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
856
857 stroke_list_acerts(ac_list, msg->list.utc, out);
858 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
859 }
860 if (msg->list.flags & LIST_CRLS)
861 {
862 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
863
864 stroke_list_crls(crl_list, msg->list.utc, out);
865 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
866 }
867 if (msg->list.flags & LIST_OCSP)
868 {
869 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
870
871 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
872
873 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
874 }
875 if (msg->list.flags & LIST_ALGS)
876 {
877 list_algs(out);
878 }
879 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
880 }
881
882 /**
883 * Implementation of stroke_list_t.destroy
884 */
885 static void destroy(private_stroke_list_t *this)
886 {
887 free(this);
888 }
889
890 /*
891 * see header file
892 */
893 stroke_list_t *stroke_list_create()
894 {
895 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
896
897 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
898 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
899 this->public.destroy = (void(*)(stroke_list_t*))destroy;
900
901 this->uptime = time(NULL);
902
903 return &this->public;
904 }
905