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