Use bits instead of bytes for a private/public key
[strongswan.git] / src / libcharon / 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
16 #include "stroke_list.h"
17
18 #include <time.h>
19
20 #ifdef HAVE_MALLINFO
21 #include <malloc.h>
22 #endif /* HAVE_MALLINFO */
23
24 #include <daemon.h>
25 #include <utils/linked_list.h>
26 #include <credentials/certificates/x509.h>
27 #include <credentials/certificates/ac.h>
28 #include <credentials/certificates/crl.h>
29 #include <credentials/certificates/pgp_certificate.h>
30 #include <credentials/ietf_attributes/ietf_attributes.h>
31 #include <config/peer_cfg.h>
32
33 /* warning intervals for list functions */
34 #define CERT_WARNING_INTERVAL 30 /* days */
35 #define CRL_WARNING_INTERVAL 7 /* days */
36 #define AC_WARNING_INTERVAL 1 /* day */
37
38 typedef struct private_stroke_list_t private_stroke_list_t;
39
40 /**
41 * private data of stroke_list
42 */
43 struct private_stroke_list_t {
44
45 /**
46 * public functions
47 */
48 stroke_list_t public;
49
50 /**
51 * timestamp of daemon start
52 */
53 time_t uptime;
54
55 /**
56 * strokes attribute provider
57 */
58 stroke_attribute_t *attribute;
59 };
60
61 /**
62 * Log tasks of a specific queue to out
63 */
64 static void log_task_q(FILE *out, ike_sa_t *ike_sa, task_queue_t q, char *name)
65 {
66 enumerator_t *enumerator;
67 bool has = FALSE;
68 task_t *task;
69
70 enumerator = ike_sa->create_task_enumerator(ike_sa, q);
71 while (enumerator->enumerate(enumerator, &task))
72 {
73 if (!has)
74 {
75 fprintf(out, "%12s[%d]: Tasks %s: ", ike_sa->get_name(ike_sa),
76 ike_sa->get_unique_id(ike_sa), name);
77 has = TRUE;
78 }
79 fprintf(out, "%N ", task_type_names, task->get_type(task));
80 }
81 enumerator->destroy(enumerator);
82 if (has)
83 {
84 fprintf(out, "\n");
85 }
86 }
87
88 /**
89 * log an IKE_SA to out
90 */
91 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
92 {
93 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
94 time_t now = time_monotonic(NULL);
95
96 fprintf(out, "%12s[%d]: %N",
97 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
98 ike_sa_state_names, ike_sa->get_state(ike_sa));
99
100 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
101 {
102 time_t established;
103
104 established = ike_sa->get_statistic(ike_sa, STAT_ESTABLISHED);
105 fprintf(out, " %V ago", &now, &established);
106 }
107
108 fprintf(out, ", %H[%Y]...%H[%Y]\n",
109 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
110 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
111
112 if (all)
113 {
114 proposal_t *ike_proposal;
115
116 ike_proposal = ike_sa->get_proposal(ike_sa);
117
118 fprintf(out, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s",
119 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
120 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
121 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
122
123
124 if (ike_sa->get_state(ike_sa) == IKE_ESTABLISHED)
125 {
126 time_t rekey, reauth;
127 peer_cfg_t *peer_cfg;
128
129 rekey = ike_sa->get_statistic(ike_sa, STAT_REKEY);
130 reauth = ike_sa->get_statistic(ike_sa, STAT_REAUTH);
131 peer_cfg = ike_sa->get_peer_cfg(ike_sa);
132
133 if (rekey)
134 {
135 fprintf(out, ", rekeying in %V", &rekey, &now);
136 }
137 if (reauth)
138 {
139 bool first = TRUE;
140 enumerator_t *enumerator;
141 auth_cfg_t *auth;
142
143 fprintf(out, ", ");
144 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, TRUE);
145 while (enumerator->enumerate(enumerator, &auth))
146 {
147 if (!first)
148 {
149 fprintf(out, "+");
150 }
151 first = FALSE;
152 fprintf(out, "%N", auth_class_names,
153 auth->get(auth, AUTH_RULE_AUTH_CLASS));
154 }
155 enumerator->destroy(enumerator);
156 fprintf(out, " reauthentication in %V", &reauth, &now);
157 }
158 if (!rekey && !reauth)
159 {
160 fprintf(out, ", rekeying disabled");
161 }
162 }
163 fprintf(out, "\n");
164
165 if (ike_proposal)
166 {
167 char buf[BUF_LEN];
168
169 snprintf(buf, BUF_LEN, "%P", ike_proposal);
170 fprintf(out, "%12s[%d]: IKE proposal: %s\n",
171 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
172 buf+4);
173 }
174
175 log_task_q(out, ike_sa, TASK_QUEUE_QUEUED, "queued");
176 log_task_q(out, ike_sa, TASK_QUEUE_ACTIVE, "active");
177 log_task_q(out, ike_sa, TASK_QUEUE_PASSIVE, "passive");
178 }
179 }
180
181 /**
182 * log an CHILD_SA to out
183 */
184 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
185 {
186 time_t use_in, use_out, rekey, now;
187 u_int64_t bytes_in, bytes_out;
188 proposal_t *proposal;
189 child_cfg_t *config = child_sa->get_config(child_sa);
190
191
192 fprintf(out, "%12s{%d}: %N, %N%s",
193 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
194 child_sa_state_names, child_sa->get_state(child_sa),
195 ipsec_mode_names, child_sa->get_mode(child_sa),
196 config->use_proxy_mode(config) ? "_PROXY" : "");
197
198 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
199 {
200 fprintf(out, ", %N%s SPIs: %.8x_i %.8x_o",
201 protocol_id_names, child_sa->get_protocol(child_sa),
202 child_sa->has_encap(child_sa) ? " in UDP" : "",
203 ntohl(child_sa->get_spi(child_sa, TRUE)),
204 ntohl(child_sa->get_spi(child_sa, FALSE)));
205
206 if (child_sa->get_ipcomp(child_sa) != IPCOMP_NONE)
207 {
208 fprintf(out, ", IPCOMP CPIs: %.4x_i %.4x_o",
209 ntohs(child_sa->get_cpi(child_sa, TRUE)),
210 ntohs(child_sa->get_cpi(child_sa, FALSE)));
211 }
212
213 if (all)
214 {
215 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
216 child_sa->get_reqid(child_sa));
217
218 proposal = child_sa->get_proposal(child_sa);
219 if (proposal)
220 {
221 u_int16_t encr_alg = ENCR_UNDEFINED, int_alg = AUTH_UNDEFINED;
222 u_int16_t encr_size = 0, int_size = 0;
223
224 proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM,
225 &encr_alg, &encr_size);
226 proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
227 &int_alg, &int_size);
228
229 if (encr_alg != ENCR_UNDEFINED)
230 {
231 fprintf(out, "%N", encryption_algorithm_names, encr_alg);
232 if (encr_size)
233 {
234 fprintf(out, "_%u", encr_size);
235 }
236 }
237 if (int_alg != AUTH_UNDEFINED)
238 {
239 fprintf(out, "/%N", integrity_algorithm_names, int_alg);
240 if (int_size)
241 {
242 fprintf(out, "_%u", int_size);
243 }
244 }
245 }
246
247 now = time_monotonic(NULL);
248 child_sa->get_usestats(child_sa, TRUE, &use_in, &bytes_in);
249 fprintf(out, ", %llu bytes_i", bytes_in);
250 if (use_in)
251 {
252 fprintf(out, " (%ds ago)", now - use_in);
253 }
254
255 child_sa->get_usestats(child_sa, FALSE, &use_out, &bytes_out);
256 fprintf(out, ", %llu bytes_o", bytes_out);
257 if (use_out)
258 {
259 fprintf(out, " (%ds ago)", now - use_out);
260 }
261 fprintf(out, ", rekeying ");
262
263 rekey = child_sa->get_lifetime(child_sa, FALSE);
264 if (rekey)
265 {
266 if (now > rekey)
267 {
268 fprintf(out, "active");
269 }
270 else
271 {
272 fprintf(out, "in %V", &now, &rekey);
273 }
274 }
275 else
276 {
277 fprintf(out, "disabled");
278 }
279
280 }
281 }
282
283 fprintf(out, "\n%12s{%d}: %#R=== %#R\n",
284 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
285 child_sa->get_traffic_selectors(child_sa, TRUE),
286 child_sa->get_traffic_selectors(child_sa, FALSE));
287 }
288
289 /**
290 * Log a configs local or remote authentication config to out
291 */
292 static void log_auth_cfgs(FILE *out, peer_cfg_t *peer_cfg, bool local)
293 {
294 enumerator_t *enumerator, *rules;
295 auth_rule_t rule;
296 auth_cfg_t *auth;
297 auth_class_t auth_class;
298 identification_t *id;
299 certificate_t *cert;
300 cert_validation_t valid;
301 char *name;
302
303 name = peer_cfg->get_name(peer_cfg);
304
305 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, local);
306 while (enumerator->enumerate(enumerator, &auth))
307 {
308 fprintf(out, "%12s: %s [%Y] uses ", name, local ? "local: " : "remote:",
309 auth->get(auth, AUTH_RULE_IDENTITY));
310
311 auth_class = (uintptr_t)auth->get(auth, AUTH_RULE_AUTH_CLASS);
312 if (auth_class != AUTH_CLASS_EAP)
313 {
314 fprintf(out, "%N authentication\n", auth_class_names, auth_class);
315 }
316 else
317 {
318 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE) == EAP_NAK)
319 {
320 fprintf(out, "EAP authentication");
321 }
322 else
323 {
324 if ((uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR))
325 {
326 fprintf(out, "EAP_%d-%d authentication",
327 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE),
328 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_VENDOR));
329 }
330 else
331 {
332 fprintf(out, "%N authentication", eap_type_names,
333 (uintptr_t)auth->get(auth, AUTH_RULE_EAP_TYPE));
334 }
335 }
336 id = auth->get(auth, AUTH_RULE_EAP_IDENTITY);
337 if (id)
338 {
339 fprintf(out, " with EAP identity '%Y'", id);
340 }
341 fprintf(out, "\n");
342 }
343
344 cert = auth->get(auth, AUTH_RULE_CA_CERT);
345 if (cert)
346 {
347 fprintf(out, "%12s: ca: \"%Y\"\n", name, cert->get_subject(cert));
348 }
349
350 cert = auth->get(auth, AUTH_RULE_IM_CERT);
351 if (cert)
352 {
353 fprintf(out, "%12s: im-ca: \"%Y\"\n", name, cert->get_subject(cert));
354 }
355
356 cert = auth->get(auth, AUTH_RULE_SUBJECT_CERT);
357 if (cert)
358 {
359 fprintf(out, "%12s: cert: \"%Y\"\n", name,
360 cert->get_subject(cert));
361 }
362
363 valid = (uintptr_t)auth->get(auth, AUTH_RULE_OCSP_VALIDATION);
364 if (valid != VALIDATION_FAILED)
365 {
366 fprintf(out, "%12s: ocsp: status must be GOOD%s\n", name,
367 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
368 }
369
370 valid = (uintptr_t)auth->get(auth, AUTH_RULE_CRL_VALIDATION);
371 if (valid != VALIDATION_FAILED)
372 {
373 fprintf(out, "%12s: crl: status must be GOOD%s\n", name,
374 (valid == VALIDATION_SKIPPED) ? " or SKIPPED" : "");
375 }
376
377 rules = auth->create_enumerator(auth);
378 while (rules->enumerate(rules, &rule, &id))
379 {
380 if (rule == AUTH_RULE_GROUP)
381 {
382 fprintf(out, "%12s: group: %Y\n", name, id);
383 }
384 }
385 rules->destroy(rules);
386 }
387 enumerator->destroy(enumerator);
388 }
389
390 /**
391 * Implementation of stroke_list_t.status.
392 */
393 static void status(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out, bool all)
394 {
395 enumerator_t *enumerator, *children;
396 ike_cfg_t *ike_cfg;
397 child_cfg_t *child_cfg;
398 child_sa_t *child_sa;
399 ike_sa_t *ike_sa;
400 bool first, found = FALSE;
401 char *name = msg->status.name;
402
403 if (all)
404 {
405 peer_cfg_t *peer_cfg;
406 char *plugin, *pool;
407 host_t *host;
408 u_int32_t dpd;
409 time_t since, now;
410 u_int size, online, offline;
411 now = time_monotonic(NULL);
412 since = time(NULL) - (now - this->uptime);
413
414 fprintf(out, "Status of IKEv2 charon daemon (strongSwan "VERSION"):\n");
415 fprintf(out, " uptime: %V, since %T\n", &now, &this->uptime, &since, FALSE);
416 #ifdef HAVE_MALLINFO
417 {
418 struct mallinfo mi = mallinfo();
419
420 fprintf(out, " malloc: sbrk %d, mmap %d, used %d, free %d\n",
421 mi.arena, mi.hblkhd, mi.uordblks, mi.fordblks);
422 }
423 #endif /* HAVE_MALLINFO */
424 fprintf(out, " worker threads: %d idle of %d,",
425 charon->processor->get_idle_threads(charon->processor),
426 charon->processor->get_total_threads(charon->processor));
427 fprintf(out, " job queue load: %d,",
428 charon->processor->get_job_load(charon->processor));
429 fprintf(out, " scheduled events: %d\n",
430 charon->scheduler->get_job_load(charon->scheduler));
431 fprintf(out, " loaded plugins: ");
432 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
433 while (enumerator->enumerate(enumerator, &plugin))
434 {
435 fprintf(out, "%s ", plugin);
436 }
437 enumerator->destroy(enumerator);
438 fprintf(out, "\n");
439
440 first = TRUE;
441 enumerator = this->attribute->create_pool_enumerator(this->attribute);
442 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
443 {
444 if (name && !streq(name, pool))
445 {
446 continue;
447 }
448 if (first)
449 {
450 first = FALSE;
451 fprintf(out, "Virtual IP pools (size/online/offline):\n");
452 }
453 fprintf(out, " %s: %u/%u/%u\n", pool, size, online, offline);
454 }
455 enumerator->destroy(enumerator);
456
457 enumerator = charon->kernel_interface->create_address_enumerator(
458 charon->kernel_interface, FALSE, FALSE);
459 fprintf(out, "Listening IP addresses:\n");
460 while (enumerator->enumerate(enumerator, (void**)&host))
461 {
462 fprintf(out, " %H\n", host);
463 }
464 enumerator->destroy(enumerator);
465
466 fprintf(out, "Connections:\n");
467 enumerator = charon->backends->create_peer_cfg_enumerator(
468 charon->backends, NULL, NULL, NULL, NULL);
469 while (enumerator->enumerate(enumerator, &peer_cfg))
470 {
471 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
472 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
473 {
474 continue;
475 }
476
477 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
478 fprintf(out, "%12s: %s...%s", peer_cfg->get_name(peer_cfg),
479 ike_cfg->get_my_addr(ike_cfg), ike_cfg->get_other_addr(ike_cfg));
480
481 dpd = peer_cfg->get_dpd(peer_cfg);
482 if (dpd)
483 {
484 fprintf(out, ", dpddelay=%us", dpd);
485 }
486 fprintf(out, "\n");
487
488 log_auth_cfgs(out, peer_cfg, TRUE);
489 log_auth_cfgs(out, peer_cfg, FALSE);
490
491 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
492 while (children->enumerate(children, &child_cfg))
493 {
494 linked_list_t *my_ts, *other_ts;
495
496 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
497 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
498 fprintf(out, "%12s: child: %#R=== %#R", child_cfg->get_name(child_cfg),
499 my_ts, other_ts);
500 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
501 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
502
503 if (dpd)
504 {
505 fprintf(out, ", dpdaction=%N", action_names,
506 child_cfg->get_dpd_action(child_cfg));
507 }
508 fprintf(out, "\n");
509 }
510 children->destroy(children);
511 }
512 enumerator->destroy(enumerator);
513 }
514
515 first = TRUE;
516 enumerator = charon->traps->create_enumerator(charon->traps);
517 while (enumerator->enumerate(enumerator, NULL, &child_sa))
518 {
519 if (first)
520 {
521 fprintf(out, "Routed Connections:\n");
522 first = FALSE;
523 }
524 log_child_sa(out, child_sa, all);
525 }
526 enumerator->destroy(enumerator);
527
528 fprintf(out, "Security Associations:\n");
529 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
530 while (enumerator->enumerate(enumerator, &ike_sa))
531 {
532 bool ike_printed = FALSE;
533 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
534
535 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
536 {
537 log_ike_sa(out, ike_sa, all);
538 found = TRUE;
539 ike_printed = TRUE;
540 }
541
542 while (children->iterate(children, (void**)&child_sa))
543 {
544 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
545 {
546 if (!ike_printed)
547 {
548 log_ike_sa(out, ike_sa, all);
549 found = TRUE;
550 ike_printed = TRUE;
551 }
552 log_child_sa(out, child_sa, all);
553 }
554 }
555 children->destroy(children);
556 }
557 enumerator->destroy(enumerator);
558
559 if (!found)
560 {
561 if (name)
562 {
563 fprintf(out, " no match\n");
564 }
565 else
566 {
567 fprintf(out, " none\n");
568 }
569 }
570 }
571
572 /**
573 * create a unique certificate list without duplicates
574 * certicates having the same issuer are grouped together.
575 */
576 static linked_list_t* create_unique_cert_list(certificate_type_t type)
577 {
578 linked_list_t *list = linked_list_create();
579 enumerator_t *enumerator = lib->credmgr->create_cert_enumerator(
580 lib->credmgr, type, KEY_ANY, NULL, FALSE);
581 certificate_t *cert;
582
583 while (enumerator->enumerate(enumerator, (void**)&cert))
584 {
585 iterator_t *iterator = list->create_iterator(list, TRUE);
586 identification_t *issuer = cert->get_issuer(cert);
587 bool previous_same, same = FALSE, last = TRUE;
588 certificate_t *list_cert;
589
590 while (iterator->iterate(iterator, (void**)&list_cert))
591 {
592 /* exit if we have a duplicate? */
593 if (list_cert->equals(list_cert, cert))
594 {
595 last = FALSE;
596 break;
597 }
598 /* group certificates with same issuer */
599 previous_same = same;
600 same = list_cert->has_issuer(list_cert, issuer);
601 if (previous_same && !same)
602 {
603 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
604 last = FALSE;
605 break;
606 }
607 }
608 iterator->destroy(iterator);
609
610 if (last)
611 {
612 list->insert_last(list, (void *)cert->get_ref(cert));
613 }
614 }
615 enumerator->destroy(enumerator);
616 return list;
617 }
618
619 /**
620 * Print a single public key.
621 */
622 static void list_public_key(public_key_t *public, FILE *out)
623 {
624 private_key_t *private = NULL;
625 chunk_t keyid;
626 identification_t *id;
627 auth_cfg_t *auth;
628
629 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &keyid))
630 {
631 id = identification_create_from_encoding(ID_KEY_ID, keyid);
632 auth = auth_cfg_create();
633 private = lib->credmgr->get_private(lib->credmgr,
634 public->get_type(public), id, auth);
635 auth->destroy(auth);
636 id->destroy(id);
637 }
638
639 fprintf(out, " pubkey: %N %d bits%s\n",
640 key_type_names, public->get_type(public),
641 public->get_keysize(public),
642 private ? ", has private key" : "");
643 if (public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1, &keyid))
644 {
645 fprintf(out, " keyid: %#B\n", &keyid);
646 }
647 if (public->get_fingerprint(public, KEYID_PUBKEY_SHA1, &keyid))
648 {
649 fprintf(out, " subjkey: %#B\n", &keyid);
650 }
651 DESTROY_IF(private);
652 }
653
654 /**
655 * list all raw public keys
656 */
657 static void stroke_list_pubkeys(linked_list_t *list, bool utc, FILE *out)
658 {
659 bool first = TRUE;
660
661 enumerator_t *enumerator = list->create_enumerator(list);
662 certificate_t *cert;
663
664 while (enumerator->enumerate(enumerator, (void**)&cert))
665 {
666 public_key_t *public = cert->get_public_key(cert);
667
668 if (public)
669 {
670 if (first)
671 {
672 fprintf(out, "\n");
673 fprintf(out, "List of Raw Public Keys:\n");
674 first = FALSE;
675 }
676 fprintf(out, "\n");
677
678 list_public_key(public, out);
679 public->destroy(public);
680 }
681 }
682 enumerator->destroy(enumerator);
683 }
684
685 /**
686 * list OpenPGP certificates
687 */
688 static void stroke_list_pgp(linked_list_t *list,bool utc, FILE *out)
689 {
690 bool first = TRUE;
691 time_t now = time(NULL);
692 enumerator_t *enumerator = list->create_enumerator(list);
693 certificate_t *cert;
694
695 while (enumerator->enumerate(enumerator, (void**)&cert))
696 {
697 time_t created, until;
698 public_key_t *public;
699 pgp_certificate_t *pgp_cert = (pgp_certificate_t*)cert;
700 chunk_t fingerprint = pgp_cert->get_fingerprint(pgp_cert);
701
702 if (first)
703 {
704
705 fprintf(out, "\n");
706 fprintf(out, "List of PGP End Entity Certificates:\n");
707 first = FALSE;
708 }
709 fprintf(out, "\n");
710 fprintf(out, " userid: '%Y'\n", cert->get_subject(cert));
711
712 fprintf(out, " digest: %#B\n", &fingerprint);
713
714 /* list validity */
715 cert->get_validity(cert, &now, &created, &until);
716 fprintf(out, " created: %T\n", &created, utc);
717 fprintf(out, " until: %T%s\n", &until, utc,
718 (until == TIME_32_BIT_SIGNED_MAX) ? " (expires never)":"");
719
720 public = cert->get_public_key(cert);
721 if (public)
722 {
723 list_public_key(public, out);
724 public->destroy(public);
725 }
726 }
727 enumerator->destroy(enumerator);
728 }
729
730 /**
731 * list all X.509 certificates matching the flags
732 */
733 static void stroke_list_certs(linked_list_t *list, char *label,
734 x509_flag_t flags, bool utc, FILE *out)
735 {
736 bool first = TRUE;
737 time_t now = time(NULL);
738 enumerator_t *enumerator;
739 certificate_t *cert;
740 x509_flag_t flag_mask;
741
742 /* mask all auxiliary flags */
743 flag_mask = ~(X509_SERVER_AUTH | X509_CLIENT_AUTH |
744 X509_SELF_SIGNED | X509_IP_ADDR_BLOCKS );
745
746 enumerator = list->create_enumerator(list);
747 while (enumerator->enumerate(enumerator, (void**)&cert))
748 {
749 x509_t *x509 = (x509_t*)cert;
750 x509_flag_t x509_flags = x509->get_flags(x509) & flag_mask;
751
752 /* list only if flag is set or flag == 0 */
753 if ((x509_flags & flags) || (x509_flags == flags))
754 {
755 enumerator_t *enumerator;
756 identification_t *altName;
757 bool first_altName = TRUE;
758 int pathlen;
759 chunk_t serial, authkey;
760 time_t notBefore, notAfter;
761 public_key_t *public;
762
763 if (first)
764 {
765 fprintf(out, "\n");
766 fprintf(out, "List of %s:\n", label);
767 first = FALSE;
768 }
769 fprintf(out, "\n");
770
771 /* list subjectAltNames */
772 enumerator = x509->create_subjectAltName_enumerator(x509);
773 while (enumerator->enumerate(enumerator, (void**)&altName))
774 {
775 if (first_altName)
776 {
777 fprintf(out, " altNames: ");
778 first_altName = FALSE;
779 }
780 else
781 {
782 fprintf(out, ", ");
783 }
784 fprintf(out, "%Y", altName);
785 }
786 if (!first_altName)
787 {
788 fprintf(out, "\n");
789 }
790 enumerator->destroy(enumerator);
791
792 fprintf(out, " subject: \"%Y\"\n", cert->get_subject(cert));
793 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
794 serial = x509->get_serial(x509);
795 fprintf(out, " serial: %#B\n", &serial);
796
797 /* list validity */
798 cert->get_validity(cert, &now, &notBefore, &notAfter);
799 fprintf(out, " validity: not before %T, ", &notBefore, utc);
800 if (now < notBefore)
801 {
802 fprintf(out, "not valid yet (valid in %V)\n", &now, &notBefore);
803 }
804 else
805 {
806 fprintf(out, "ok\n");
807 }
808 fprintf(out, " not after %T, ", &notAfter, utc);
809 if (now > notAfter)
810 {
811 fprintf(out, "expired (%V ago)\n", &now, &notAfter);
812 }
813 else
814 {
815 fprintf(out, "ok");
816 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
817 {
818 fprintf(out, " (expires in %V)", &now, &notAfter);
819 }
820 fprintf(out, " \n");
821 }
822
823 public = cert->get_public_key(cert);
824 if (public)
825 {
826 list_public_key(public, out);
827 public->destroy(public);
828 }
829
830 /* list optional authorityKeyIdentifier */
831 authkey = x509->get_authKeyIdentifier(x509);
832 if (authkey.ptr)
833 {
834 fprintf(out, " authkey: %#B\n", &authkey);
835 }
836
837 /* list optional pathLenConstraint */
838 pathlen = x509->get_pathLenConstraint(x509);
839 if (pathlen != X509_NO_PATH_LEN_CONSTRAINT)
840 {
841 fprintf(out, " pathlen: %d\n", pathlen);
842 }
843
844 /* list optional ipAddrBlocks */
845 if (x509->get_flags(x509) & X509_IP_ADDR_BLOCKS)
846 {
847 traffic_selector_t *ipAddrBlock;
848 bool first_ipAddrBlock = TRUE;
849
850 fprintf(out, " addresses: ");
851 enumerator = x509->create_ipAddrBlock_enumerator(x509);
852 while (enumerator->enumerate(enumerator, &ipAddrBlock))
853 {
854 if (first_ipAddrBlock)
855 {
856 first_ipAddrBlock = FALSE;
857 }
858 else
859 {
860 fprintf(out, ", ");
861 }
862 fprintf(out, "%R", ipAddrBlock);
863 }
864 enumerator->destroy(enumerator);
865 fprintf(out, "\n");
866 }
867 }
868 }
869 enumerator->destroy(enumerator);
870 }
871
872 /**
873 * list all X.509 attribute certificates
874 */
875 static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
876 {
877 bool first = TRUE;
878 time_t thisUpdate, nextUpdate, now = time(NULL);
879 enumerator_t *enumerator = list->create_enumerator(list);
880 certificate_t *cert;
881
882 while (enumerator->enumerate(enumerator, (void**)&cert))
883 {
884 ac_t *ac = (ac_t*)cert;
885 identification_t *id;
886 ietf_attributes_t *groups;
887 chunk_t chunk;
888
889 if (first)
890 {
891 fprintf(out, "\n");
892 fprintf(out, "List of X.509 Attribute Certificates:\n");
893 first = FALSE;
894 }
895 fprintf(out, "\n");
896
897 id = cert->get_subject(cert);
898 if (id)
899 {
900 fprintf(out, " holder: \"%Y\"\n", id);
901 }
902 id = ac->get_holderIssuer(ac);
903 if (id)
904 {
905 fprintf(out, " hissuer: \"%Y\"\n", id);
906 }
907 chunk = ac->get_holderSerial(ac);
908 if (chunk.ptr)
909 {
910 fprintf(out, " hserial: %#B\n", &chunk);
911 }
912 groups = ac->get_groups(ac);
913 if (groups)
914 {
915 fprintf(out, " groups: %s\n", groups->get_string(groups));
916 groups->destroy(groups);
917 }
918 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
919 chunk = ac->get_serial(ac);
920 fprintf(out, " serial: %#B\n", &chunk);
921
922 /* list validity */
923 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
924 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
925 fprintf(out, " next %T, ", &nextUpdate, utc);
926 if (now > nextUpdate)
927 {
928 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
929 }
930 else
931 {
932 fprintf(out, "ok");
933 if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
934 {
935 fprintf(out, " (expires in %V)", &now, &nextUpdate);
936 }
937 fprintf(out, " \n");
938 }
939
940 /* list optional authorityKeyIdentifier */
941 chunk = ac->get_authKeyIdentifier(ac);
942 if (chunk.ptr)
943 {
944 fprintf(out, " authkey: %#B\n", &chunk);
945 }
946 }
947 enumerator->destroy(enumerator);
948 }
949
950 /**
951 * list all X.509 CRLs
952 */
953 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
954 {
955 bool first = TRUE;
956 time_t thisUpdate, nextUpdate, now = time(NULL);
957 enumerator_t *enumerator = list->create_enumerator(list);
958 certificate_t *cert;
959
960 while (enumerator->enumerate(enumerator, (void**)&cert))
961 {
962 crl_t *crl = (crl_t*)cert;
963 chunk_t chunk;
964
965 if (first)
966 {
967 fprintf(out, "\n");
968 fprintf(out, "List of X.509 CRLs:\n");
969 first = FALSE;
970 }
971 fprintf(out, "\n");
972
973 fprintf(out, " issuer: \"%Y\"\n", cert->get_issuer(cert));
974
975 /* list optional crlNumber */
976 chunk = crl->get_serial(crl);
977 if (chunk.ptr)
978 {
979 fprintf(out, " serial: %#B\n", &chunk);
980 }
981
982 /* count the number of revoked certificates */
983 {
984 int count = 0;
985 enumerator_t *enumerator = crl->create_enumerator(crl);
986
987 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
988 {
989 count++;
990 }
991 fprintf(out, " revoked: %d certificate%s\n", count,
992 (count == 1)? "" : "s");
993 enumerator->destroy(enumerator);
994 }
995
996 /* list validity */
997 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
998 fprintf(out, " updates: this %T\n", &thisUpdate, utc);
999 fprintf(out, " next %T, ", &nextUpdate, utc);
1000 if (now > nextUpdate)
1001 {
1002 fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
1003 }
1004 else
1005 {
1006 fprintf(out, "ok");
1007 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
1008 {
1009 fprintf(out, " (expires in %V)", &now, &nextUpdate);
1010 }
1011 fprintf(out, " \n");
1012 }
1013
1014 /* list optional authorityKeyIdentifier */
1015 chunk = crl->get_authKeyIdentifier(crl);
1016 if (chunk.ptr)
1017 {
1018 fprintf(out, " authkey: %#B\n", &chunk);
1019 }
1020 }
1021 enumerator->destroy(enumerator);
1022 }
1023
1024 /**
1025 * list all OCSP responses
1026 */
1027 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
1028 {
1029 bool first = TRUE;
1030 enumerator_t *enumerator = list->create_enumerator(list);
1031 certificate_t *cert;
1032
1033 while (enumerator->enumerate(enumerator, (void**)&cert))
1034 {
1035 if (first)
1036 {
1037 fprintf(out, "\n");
1038 fprintf(out, "List of OCSP responses:\n");
1039 fprintf(out, "\n");
1040 first = FALSE;
1041 }
1042
1043 fprintf(out, " signer: \"%Y\"\n", cert->get_issuer(cert));
1044 }
1045 enumerator->destroy(enumerator);
1046 }
1047
1048 /**
1049 * List of registered cryptographical algorithms
1050 */
1051 static void list_algs(FILE *out)
1052 {
1053 enumerator_t *enumerator;
1054 encryption_algorithm_t encryption;
1055 integrity_algorithm_t integrity;
1056 hash_algorithm_t hash;
1057 pseudo_random_function_t prf;
1058 diffie_hellman_group_t group;
1059
1060 fprintf(out, "\n");
1061 fprintf(out, "List of registered IKEv2 Algorithms:\n");
1062 fprintf(out, "\n encryption: ");
1063 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
1064 while (enumerator->enumerate(enumerator, &encryption))
1065 {
1066 fprintf(out, "%N ", encryption_algorithm_names, encryption);
1067 }
1068 enumerator->destroy(enumerator);
1069 fprintf(out, "\n integrity: ");
1070 enumerator = lib->crypto->create_signer_enumerator(lib->crypto);
1071 while (enumerator->enumerate(enumerator, &integrity))
1072 {
1073 fprintf(out, "%N ", integrity_algorithm_names, integrity);
1074 }
1075 enumerator->destroy(enumerator);
1076 fprintf(out, "\n hasher: ");
1077 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
1078 while (enumerator->enumerate(enumerator, &hash))
1079 {
1080 fprintf(out, "%N ", hash_algorithm_names, hash);
1081 }
1082 enumerator->destroy(enumerator);
1083 fprintf(out, "\n prf: ");
1084 enumerator = lib->crypto->create_prf_enumerator(lib->crypto);
1085 while (enumerator->enumerate(enumerator, &prf))
1086 {
1087 fprintf(out, "%N ", pseudo_random_function_names, prf);
1088 }
1089 enumerator->destroy(enumerator);
1090 fprintf(out, "\n dh-group: ");
1091 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
1092 while (enumerator->enumerate(enumerator, &group))
1093 {
1094 fprintf(out, "%N ", diffie_hellman_group_names, group);
1095 }
1096 enumerator->destroy(enumerator);
1097 fprintf(out, "\n");
1098 }
1099
1100 /**
1101 * Implementation of stroke_list_t.list.
1102 */
1103 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1104 {
1105 linked_list_t *cert_list = NULL;
1106
1107 if (msg->list.flags & LIST_PUBKEYS)
1108 {
1109 linked_list_t *pubkey_list = create_unique_cert_list(CERT_TRUSTED_PUBKEY);
1110
1111 stroke_list_pubkeys(pubkey_list, msg->list.utc, out);
1112 pubkey_list->destroy_offset(pubkey_list, offsetof(certificate_t, destroy));
1113 }
1114 if (msg->list.flags & LIST_CERTS)
1115 {
1116 linked_list_t *pgp_list = create_unique_cert_list(CERT_GPG);
1117
1118 stroke_list_pgp(pgp_list, msg->list.utc, out);
1119 pgp_list->destroy_offset(pgp_list, offsetof(certificate_t, destroy));
1120 }
1121 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
1122 {
1123 cert_list = create_unique_cert_list(CERT_X509);
1124 }
1125 if (msg->list.flags & LIST_CERTS)
1126 {
1127 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
1128 X509_NONE, msg->list.utc, out);
1129 }
1130 if (msg->list.flags & LIST_CACERTS)
1131 {
1132 stroke_list_certs(cert_list, "X.509 CA Certificates",
1133 X509_CA, msg->list.utc, out);
1134 }
1135 if (msg->list.flags & LIST_OCSPCERTS)
1136 {
1137 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
1138 X509_OCSP_SIGNER, msg->list.utc, out);
1139 }
1140 if (msg->list.flags & LIST_AACERTS)
1141 {
1142 stroke_list_certs(cert_list, "X.509 AA Certificates",
1143 X509_AA, msg->list.utc, out);
1144 }
1145 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
1146
1147 if (msg->list.flags & LIST_ACERTS)
1148 {
1149 linked_list_t *ac_list = create_unique_cert_list(CERT_X509_AC);
1150
1151 stroke_list_acerts(ac_list, msg->list.utc, out);
1152 ac_list->destroy_offset(ac_list, offsetof(certificate_t, destroy));
1153 }
1154 if (msg->list.flags & LIST_CRLS)
1155 {
1156 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
1157
1158 stroke_list_crls(crl_list, msg->list.utc, out);
1159 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
1160 }
1161 if (msg->list.flags & LIST_OCSP)
1162 {
1163 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
1164
1165 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
1166
1167 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
1168 }
1169 if (msg->list.flags & LIST_ALGS)
1170 {
1171 list_algs(out);
1172 }
1173 }
1174
1175 /**
1176 * Print leases of a single pool
1177 */
1178 static void pool_leases(private_stroke_list_t *this, FILE *out, char *pool,
1179 host_t *address, u_int size, u_int online, u_int offline)
1180 {
1181 enumerator_t *enumerator;
1182 identification_t *id;
1183 host_t *lease;
1184 bool on;
1185 int found = 0;
1186
1187 fprintf(out, "Leases in pool '%s', usage: %lu/%lu, %lu online\n",
1188 pool, online + offline, size, online);
1189 enumerator = this->attribute->create_lease_enumerator(this->attribute, pool);
1190 while (enumerator && enumerator->enumerate(enumerator, &id, &lease, &on))
1191 {
1192 if (!address || address->ip_equals(address, lease))
1193 {
1194 fprintf(out, " %15H %s '%Y'\n",
1195 lease, on ? "online" : "offline", id);
1196 found++;
1197 }
1198 }
1199 enumerator->destroy(enumerator);
1200 if (!found)
1201 {
1202 fprintf(out, " no matching leases found\n");
1203 }
1204 }
1205
1206 /**
1207 * Implementation of stroke_list_t.leases
1208 */
1209 static void leases(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
1210 {
1211 enumerator_t *enumerator;
1212 u_int size, offline, online;
1213 host_t *address = NULL;
1214 char *pool;
1215 int found = 0;
1216
1217 if (msg->leases.address)
1218 {
1219 address = host_create_from_string(msg->leases.address, 0);
1220 }
1221
1222 enumerator = this->attribute->create_pool_enumerator(this->attribute);
1223 while (enumerator->enumerate(enumerator, &pool, &size, &online, &offline))
1224 {
1225 if (!msg->leases.pool || streq(msg->leases.pool, pool))
1226 {
1227 pool_leases(this, out, pool, address, size, online, offline);
1228 found++;
1229 }
1230 }
1231 enumerator->destroy(enumerator);
1232 if (!found)
1233 {
1234 if (msg->leases.pool)
1235 {
1236 fprintf(out, "pool '%s' not found\n", msg->leases.pool);
1237 }
1238 else
1239 {
1240 fprintf(out, "no pools found\n");
1241 }
1242 }
1243 DESTROY_IF(address);
1244 }
1245
1246 /**
1247 * Implementation of stroke_list_t.destroy
1248 */
1249 static void destroy(private_stroke_list_t *this)
1250 {
1251 free(this);
1252 }
1253
1254 /*
1255 * see header file
1256 */
1257 stroke_list_t *stroke_list_create(stroke_attribute_t *attribute)
1258 {
1259 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
1260
1261 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
1262 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
1263 this->public.leases = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))leases;
1264 this->public.destroy = (void(*)(stroke_list_t*))destroy;
1265
1266 this->uptime = time_monotonic(NULL);
1267 this->attribute = attribute;
1268
1269 return &this->public;
1270 }
1271