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