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