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