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