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