implemented ipsec listalgs as a stroke command
[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 ipsec_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 ipsec_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 ike_cfg_t *ike_cfg;
231 child_cfg_t *child_cfg;
232 ike_sa_t *ike_sa;
233 bool found = FALSE;
234 char *name = msg->status.name;
235
236 if (all)
237 {
238 peer_cfg_t *peer_cfg;
239 char *plugin;
240 host_t *host;
241 u_int32_t dpd;
242 time_t uptime = time(NULL) - this->uptime;
243
244 fprintf(out, "Performance:\n");
245 fprintf(out, " uptime: %V, since %#T\n", &uptime, &this->uptime, FALSE);
246 fprintf(out, " worker threads: %d idle of %d,",
247 charon->processor->get_idle_threads(charon->processor),
248 charon->processor->get_total_threads(charon->processor));
249 fprintf(out, " job queue load: %d,",
250 charon->processor->get_job_load(charon->processor));
251 fprintf(out, " scheduled events: %d\n",
252 charon->scheduler->get_job_load(charon->scheduler));
253 fprintf(out, " loaded plugins: ");
254 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
255 while (enumerator->enumerate(enumerator, &plugin))
256 {
257 fprintf(out, "%s ", plugin);
258 }
259 enumerator->destroy(enumerator);
260 fprintf(out, "\n");
261
262 enumerator = charon->kernel_interface->create_address_enumerator(
263 charon->kernel_interface, FALSE, FALSE);
264 fprintf(out, "Listening IP addresses:\n");
265 while (enumerator->enumerate(enumerator, (void**)&host))
266 {
267 fprintf(out, " %H\n", host);
268 }
269 enumerator->destroy(enumerator);
270
271 fprintf(out, "Connections:\n");
272 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends);
273 while (enumerator->enumerate(enumerator, (void**)&peer_cfg))
274 {
275 void *ptr;
276 certificate_t *cert;
277 auth_item_t item;
278 auth_info_t *auth;
279 enumerator_t *auth_enumerator;
280 identification_t *my_ca = NULL, *other_ca = NULL;
281 identification_t *eap_identity = NULL;
282 u_int32_t *eap_type = NULL;
283 bool ac_groups = FALSE;
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, EAP type, EAP identity,
292 * and the presence of AC groups
293 */
294 auth = peer_cfg->get_auth(peer_cfg);
295 auth_enumerator = auth->create_item_enumerator(auth);
296 while (auth_enumerator->enumerate(auth_enumerator, &item, &ptr))
297 {
298 switch (item)
299 {
300 case AUTHN_EAP_TYPE:
301 eap_type = (u_int32_t *)ptr;
302 break;
303 case AUTHN_EAP_IDENTITY:
304 eap_identity = (identification_t *)ptr;
305 break;
306 case AUTHN_CA_CERT:
307 cert = (certificate_t *)ptr;
308 my_ca = cert->get_subject(cert);
309 break;
310 case AUTHN_CA_CERT_NAME:
311 my_ca = (identification_t *)ptr;
312 break;
313 case AUTHZ_CA_CERT:
314 cert = (certificate_t *)ptr;
315 other_ca = cert->get_subject(cert);
316 break;
317 case AUTHZ_CA_CERT_NAME:
318 other_ca = (identification_t *)ptr;
319 break;
320 case AUTHZ_AC_GROUP:
321 ac_groups = TRUE;
322 break;
323 default:
324 break;
325 }
326 }
327 auth_enumerator->destroy(auth_enumerator);
328
329 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
330 fprintf(out, "%12s: %s[%D]...%s[%D]\n", peer_cfg->get_name(peer_cfg),
331 ike_cfg->get_my_addr(ike_cfg), peer_cfg->get_my_id(peer_cfg),
332 ike_cfg->get_other_addr(ike_cfg), peer_cfg->get_other_id(peer_cfg));
333 if (my_ca || other_ca)
334 {
335 fprintf(out, "%12s: CAs: ", peer_cfg->get_name(peer_cfg));
336 if (my_ca)
337 {
338 fprintf(out, "\"%D\"...", my_ca);
339 }
340 else
341 {
342 fprintf(out, "%%any...");
343 }
344 if (other_ca)
345 {
346 fprintf(out, "\"%D\"\n", other_ca);
347 }
348 else
349 {
350 fprintf(out, "%%any\n");
351 }
352 }
353
354 if (ac_groups)
355 {
356 bool first = TRUE;
357
358 fprintf(out, "%12s: groups: ", peer_cfg->get_name(peer_cfg));
359 auth_enumerator = auth->create_item_enumerator(auth);
360 while (auth_enumerator->enumerate(auth_enumerator, &item, &ptr))
361 {
362 if (item == AUTHZ_AC_GROUP)
363 {
364 identification_t *group = (identification_t *)ptr;
365
366 fprintf(out, "%s%D", first? "":", ", group);
367 first = FALSE;
368 }
369 }
370 auth_enumerator->destroy(auth_enumerator);
371 fprintf(out, "\n");
372 }
373
374 fprintf(out, "%12s: %N ", peer_cfg->get_name(peer_cfg),
375 auth_class_names, get_auth_class(peer_cfg));
376 if (eap_type)
377 {
378 fprintf(out, "and %N ", eap_type_names, *eap_type);
379 }
380 fprintf(out, "authentication");
381 if (eap_identity)
382 {
383 fprintf(out, ", EAP identity: '%D'", eap_identity);
384 }
385 dpd = peer_cfg->get_dpd(peer_cfg);
386 if (dpd)
387 {
388 fprintf(out, ", dpddelay=%us", dpd);
389 }
390 fprintf(out, "\n");
391
392 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
393 while (children->enumerate(children, &child_cfg))
394 {
395 linked_list_t *my_ts, *other_ts;
396
397 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
398 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
399 fprintf(out, "%12s: %#R=== %#R", child_cfg->get_name(child_cfg),
400 my_ts, other_ts);
401 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
402 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
403
404 if (dpd)
405 {
406 fprintf(out, ", dpdaction=%N", action_names,
407 child_cfg->get_dpd_action(child_cfg));
408 }
409 fprintf(out, "\n");
410 }
411 children->destroy(children);
412 }
413 enumerator->destroy(enumerator);
414 }
415
416 fprintf(out, "Security Associations:\n");
417 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
418 while (enumerator->enumerate(enumerator, &ike_sa))
419 {
420 bool ike_printed = FALSE;
421 child_sa_t *child_sa;
422 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
423
424 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
425 {
426 log_ike_sa(out, ike_sa, all);
427 found = TRUE;
428 ike_printed = TRUE;
429 }
430
431 while (children->iterate(children, (void**)&child_sa))
432 {
433 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
434 {
435 if (!ike_printed)
436 {
437 log_ike_sa(out, ike_sa, all);
438 found = TRUE;
439 ike_printed = TRUE;
440 }
441 log_child_sa(out, child_sa, all);
442 }
443 }
444 children->destroy(children);
445 }
446 enumerator->destroy(enumerator);
447
448 if (!found)
449 {
450 if (name)
451 {
452 fprintf(out, " no match\n");
453 }
454 else
455 {
456 fprintf(out, " none\n");
457 }
458 }
459 }
460
461 /**
462 * create a unique certificate list without duplicates
463 * certicates having the same issuer are grouped together.
464 */
465 static linked_list_t* create_unique_cert_list(certificate_type_t type)
466 {
467 linked_list_t *list = linked_list_create();
468 enumerator_t *enumerator = charon->credentials->create_cert_enumerator(
469 charon->credentials, type, KEY_ANY,
470 NULL, FALSE);
471 certificate_t *cert;
472
473 while (enumerator->enumerate(enumerator, (void**)&cert))
474 {
475 iterator_t *iterator = list->create_iterator(list, TRUE);
476 identification_t *issuer = cert->get_issuer(cert);
477 bool previous_same, same = FALSE, last = TRUE;
478 certificate_t *list_cert;
479
480 while (iterator->iterate(iterator, (void**)&list_cert))
481 {
482 /* exit if we have a duplicate? */
483 if (list_cert->equals(list_cert, cert))
484 {
485 last = FALSE;
486 break;
487 }
488 /* group certificates with same issuer */
489 previous_same = same;
490 same = list_cert->has_issuer(list_cert, issuer);
491 if (previous_same && !same)
492 {
493 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
494 last = FALSE;
495 break;
496 }
497 }
498 iterator->destroy(iterator);
499
500 if (last)
501 {
502 list->insert_last(list, (void *)cert->get_ref(cert));
503 }
504 }
505 enumerator->destroy(enumerator);
506 return list;
507 }
508
509 /**
510 * list all raw public keys
511 */
512 static void stroke_list_pubkeys(linked_list_t *list, bool utc, FILE *out)
513 {
514 bool first = TRUE;
515
516 enumerator_t *enumerator = list->create_enumerator(list);
517 certificate_t *cert;
518
519 while (enumerator->enumerate(enumerator, (void**)&cert))
520 {
521 public_key_t *public = cert->get_public_key(cert);
522
523 if (public)
524 {
525 private_key_t *private = NULL;
526 identification_t *id, *keyid;
527
528 if (first)
529 {
530 fprintf(out, "\n");
531 fprintf(out, "List of Raw Public Keys:\n");
532 first = FALSE;
533 }
534 fprintf(out, "\n");
535
536 /* list public key information */
537 id = public->get_id(public, ID_PUBKEY_SHA1);
538 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
539
540 private = charon->credentials->get_private(
541 charon->credentials,
542 public->get_type(public), keyid, NULL);
543 fprintf(out, " pubkey: %N %d bits%s\n",
544 key_type_names, public->get_type(public),
545 public->get_keysize(public) * 8,
546 private ? ", has private key" : "");
547 fprintf(out, " keyid: %D\n", keyid);
548 fprintf(out, " subjkey: %D\n", id);
549 DESTROY_IF(private);
550 public->destroy(public);
551 }
552 }
553 enumerator->destroy(enumerator);
554 }
555
556 /**
557 * list all X.509 certificates matching the flags
558 */
559 static void stroke_list_certs(linked_list_t *list, char *label,
560 x509_flag_t flags, bool utc, FILE *out)
561 {
562 bool first = TRUE;
563 time_t now = time(NULL);
564 enumerator_t *enumerator = list->create_enumerator(list);
565 certificate_t *cert;
566
567 while (enumerator->enumerate(enumerator, (void**)&cert))
568 {
569 x509_t *x509 = (x509_t*)cert;
570 x509_flag_t x509_flags = x509->get_flags(x509);
571
572 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
573 if ((x509_flags & flags) || (flags == (x509_flags & ~X509_SELF_SIGNED)))
574 {
575 enumerator_t *enumerator;
576 identification_t *altName;
577 bool first_altName = TRUE;
578 chunk_t serial = x509->get_serial(x509);
579 identification_t *authkey = x509->get_authKeyIdentifier(x509);
580 time_t notBefore, notAfter;
581 public_key_t *public = cert->get_public_key(cert);
582
583 if (first)
584 {
585 fprintf(out, "\n");
586 fprintf(out, "List of %s:\n", label);
587 first = FALSE;
588 }
589 fprintf(out, "\n");
590
591 /* list subjectAltNames */
592 enumerator = x509->create_subjectAltName_enumerator(x509);
593 while (enumerator->enumerate(enumerator, (void**)&altName))
594 {
595 if (first_altName)
596 {
597 fprintf(out, " altNames: ");
598 first_altName = FALSE;
599 }
600 else
601 {
602 fprintf(out, ", ");
603 }
604 fprintf(out, "%D", altName);
605 }
606 if (!first_altName)
607 {
608 fprintf(out, "\n");
609 }
610 enumerator->destroy(enumerator);
611
612 fprintf(out, " subject: \"%D\"\n", cert->get_subject(cert));
613 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
614 fprintf(out, " serial: %#B\n", &serial);
615
616 /* list validity */
617 cert->get_validity(cert, &now, &notBefore, &notAfter);
618 fprintf(out, " validity: not before %#T, ", &notBefore, utc);
619 if (now < notBefore)
620 {
621 fprintf(out, "not valid yet (valid in %#V)\n", &now, &notBefore);
622 }
623 else
624 {
625 fprintf(out, "ok\n");
626 }
627 fprintf(out, " not after %#T, ", &notAfter, utc);
628 if (now > notAfter)
629 {
630 fprintf(out, "expired (%#V ago)\n", &now, &notAfter);
631 }
632 else
633 {
634 fprintf(out, "ok");
635 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
636 {
637 fprintf(out, " (expires in %#V)", &now, &notAfter);
638 }
639 fprintf(out, " \n");
640 }
641
642 /* list public key information */
643 if (public)
644 {
645 private_key_t *private = NULL;
646 identification_t *id, *keyid;
647
648 id = public->get_id(public, ID_PUBKEY_SHA1);
649 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
650
651 private = charon->credentials->get_private(
652 charon->credentials,
653 public->get_type(public), keyid, NULL);
654 fprintf(out, " pubkey: %N %d bits%s\n",
655 key_type_names, public->get_type(public),
656 public->get_keysize(public) * 8,
657 private ? ", has private key" : "");
658 fprintf(out, " keyid: %D\n", keyid);
659 fprintf(out, " subjkey: %D\n", id);
660 DESTROY_IF(private);
661 public->destroy(public);
662 }
663
664 /* list optional authorityKeyIdentifier */
665 if (authkey)
666 {
667 fprintf(out, " authkey: %D\n", authkey);
668 }
669 }
670 }
671 enumerator->destroy(enumerator);
672 }
673
674 /**
675 * list all X.509 attribute certificates
676 */
677 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
678 {
679 bool first = TRUE;
680 time_t thisUpdate, nextUpdate, now = time(NULL);
681 enumerator_t *enumerator = list->create_enumerator(list);
682 certificate_t *cert;
683
684 while (enumerator->enumerate(enumerator, (void**)&cert))
685 {
686 ac_t *ac = (ac_t*)cert;
687 chunk_t serial = ac->get_serial(ac);
688 chunk_t holderSerial = ac->get_holderSerial(ac);
689 identification_t *holderIssuer = ac->get_holderIssuer(ac);
690 identification_t *authkey = ac->get_authKeyIdentifier(ac);
691 identification_t *entityName = cert->get_subject(cert);
692
693 if (first)
694 {
695 fprintf(out, "\n");
696 fprintf(out, "List of X.509 Attribute Certificates:\n");
697 first = FALSE;
698 }
699 fprintf(out, "\n");
700
701 if (entityName)
702 {
703 fprintf(out, " holder: \"%D\"\n", entityName);
704 }
705 if (holderIssuer)
706 {
707 fprintf(out, " hissuer: \"%D\"\n", holderIssuer);
708 }
709 if (holderSerial.ptr)
710 {
711 fprintf(out, " hserial: %#B\n", &holderSerial);
712 }
713 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
714 fprintf(out, " serial: %#B\n", &serial);
715
716 /* list validity */
717 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
718 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
719 fprintf(out, " next %#T, ", &nextUpdate, utc);
720 if (now > nextUpdate)
721 {
722 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
723 }
724 else
725 {
726 fprintf(out, "ok");
727 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
728 {
729 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
730 }
731 fprintf(out, " \n");
732 }
733
734 /* list optional authorityKeyIdentifier */
735 if (authkey)
736 {
737 fprintf(out, " authkey: %D\n", authkey);
738 }
739 }
740 enumerator->destroy(enumerator);
741 }
742
743 /**
744 * list all X.509 CRLs
745 */
746 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
747 {
748 bool first = TRUE;
749 time_t thisUpdate, nextUpdate, now = time(NULL);
750 enumerator_t *enumerator = list->create_enumerator(list);
751 certificate_t *cert;
752
753 while (enumerator->enumerate(enumerator, (void**)&cert))
754 {
755 crl_t *crl = (crl_t*)cert;
756 chunk_t serial = crl->get_serial(crl);
757 identification_t *authkey = crl->get_authKeyIdentifier(crl);
758
759 if (first)
760 {
761 fprintf(out, "\n");
762 fprintf(out, "List of X.509 CRLs:\n");
763 first = FALSE;
764 }
765 fprintf(out, "\n");
766
767 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
768
769 /* list optional crlNumber */
770 if (serial.ptr)
771 {
772 fprintf(out, " serial: %#B\n", &serial);
773 }
774
775 /* count the number of revoked certificates */
776 {
777 int count = 0;
778 enumerator_t *enumerator = crl->create_enumerator(crl);
779
780 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
781 {
782 count++;
783 }
784 fprintf(out, " revoked: %d certificate%s\n", count,
785 (count == 1)? "" : "s");
786 enumerator->destroy(enumerator);
787 }
788
789 /* list validity */
790 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
791 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
792 fprintf(out, " next %#T, ", &nextUpdate, utc);
793 if (now > nextUpdate)
794 {
795 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
796 }
797 else
798 {
799 fprintf(out, "ok");
800 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
801 {
802 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
803 }
804 fprintf(out, " \n");
805 }
806
807 /* list optional authorityKeyIdentifier */
808 if (authkey)
809 {
810 fprintf(out, " authkey: %D\n", authkey);
811 }
812 }
813 enumerator->destroy(enumerator);
814 }
815
816 /**
817 * list all OCSP responses
818 */
819 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
820 {
821 bool first = TRUE;
822 enumerator_t *enumerator = list->create_enumerator(list);
823 certificate_t *cert;
824
825 while (enumerator->enumerate(enumerator, (void**)&cert))
826 {
827 if (first)
828 {
829 fprintf(out, "\n");
830 fprintf(out, "List of OCSP responses:\n");
831 fprintf(out, "\n");
832 first = FALSE;
833 }
834
835 fprintf(out, " signer: \"%D\"\n", cert->get_issuer(cert));
836 }
837 enumerator->destroy(enumerator);
838 }
839
840 /**
841 * List of registered cryptographical algorithms
842 */
843 static void list_algs(FILE *out)
844 {
845 enumerator_t *enumerator;
846 encryption_algorithm_t encryption;
847 integrity_algorithm_t integrity;
848 hash_algorithm_t hash;
849 pseudo_random_function_t prf;
850 diffie_hellman_group_t group;
851
852 fprintf(out, "\n");
853 fprintf(out, "List of registered IKEv2 Algorithms:\n");
854 fprintf(out, "\n encryption: ");
855 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
856 while (enumerator->enumerate(enumerator, &encryption))
857 {
858 fprintf(out, "%N ", encryption_algorithm_names, encryption);
859 }
860 enumerator->destroy(enumerator);
861 fprintf(out, "\n integrity: ");
862 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
863 while (enumerator->enumerate(enumerator, &integrity))
864 {
865 fprintf(out, "%N ", integrity_algorithm_names, integrity);
866 }
867 enumerator->destroy(enumerator);
868 fprintf(out, "\n hasher: ");
869 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
870 while (enumerator->enumerate(enumerator, &hash))
871 {
872 fprintf(out, "%N ", hash_algorithm_names, hash);
873 }
874 enumerator->destroy(enumerator);
875 fprintf(out, "\n prf: ");
876 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
877 while (enumerator->enumerate(enumerator, &prf))
878 {
879 fprintf(out, "%N ", pseudo_random_function_names, prf);
880 }
881 enumerator->destroy(enumerator);
882 fprintf(out, "\n dh-group: ");
883 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
884 while (enumerator->enumerate(enumerator, &group))
885 {
886 fprintf(out, "%N ", diffie_hellman_group_names, group);
887 }
888 enumerator->destroy(enumerator);
889 fprintf(out, "\n");
890 }
891
892 /**
893 * Implementation of stroke_list_t.list.
894 */
895 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
896 {
897 linked_list_t *cert_list = NULL;
898
899 if (msg->list.flags & LIST_PUBKEYS)
900 {
901 linked_list_t *pubkey_list = create_unique_cert_list(CERT_TRUSTED_PUBKEY);
902
903 stroke_list_pubkeys(pubkey_list, msg->list.utc, out);
904 pubkey_list->destroy_offset(pubkey_list, offsetof(certificate_t, destroy));
905 }
906 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
907 {
908 cert_list = create_unique_cert_list(CERT_X509);
909 }
910 if (msg->list.flags & LIST_CERTS)
911 {
912 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
913 0, msg->list.utc, out);
914 }
915 if (msg->list.flags & LIST_CACERTS)
916 {
917 stroke_list_certs(cert_list, "X.509 CA Certificates",
918 X509_CA, msg->list.utc, out);
919 }
920 if (msg->list.flags & LIST_OCSPCERTS)
921 {
922 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
923 X509_OCSP_SIGNER, msg->list.utc, out);
924 }
925 if (msg->list.flags & LIST_AACERTS)
926 {
927 stroke_list_certs(cert_list, "X.509 AA Certificates",
928 X509_AA, msg->list.utc, out);
929 }
930 if (msg->list.flags & LIST_ACERTS)
931 {
932 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
933
934 stroke_list_acerts(ac_list, msg->list.utc, out);
935 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
936 }
937 if (msg->list.flags & LIST_CRLS)
938 {
939 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
940
941 stroke_list_crls(crl_list, msg->list.utc, out);
942 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
943 }
944 if (msg->list.flags & LIST_OCSP)
945 {
946 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
947
948 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
949
950 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
951 }
952 if (msg->list.flags & LIST_ALGS)
953 {
954 list_algs(out);
955 }
956 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
957 }
958
959 /**
960 * Implementation of stroke_list_t.destroy
961 */
962 static void destroy(private_stroke_list_t *this)
963 {
964 free(this);
965 }
966
967 /*
968 * see header file
969 */
970 stroke_list_t *stroke_list_create()
971 {
972 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
973
974 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
975 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
976 this->public.destroy = (void(*)(stroke_list_t*))destroy;
977
978 this->uptime = time(NULL);
979
980 return &this->public;
981 }
982