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