suppress listing of integrity algorithm if it is undefined
[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/crl.h>
24
25 /* warning intervals for list functions */
26 #define CERT_WARNING_INTERVAL 30 /* days */
27 #define CRL_WARNING_INTERVAL 7 /* days */
28
29 typedef struct private_stroke_list_t private_stroke_list_t;
30
31 /**
32 * private data of stroke_list
33 */
34 struct private_stroke_list_t {
35
36 /**
37 * public functions
38 */
39 stroke_list_t public;
40
41 /**
42 * timestamp of daemon start
43 */
44 time_t uptime;
45 };
46
47 /**
48 * log an IKE_SA to out
49 */
50 static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
51 {
52 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
53 u_int32_t rekey, reauth;
54
55 fprintf(out, "%12s[%d]: %N, %H[%D]...%H[%D]\n",
56 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
57 ike_sa_state_names, ike_sa->get_state(ike_sa),
58 ike_sa->get_my_host(ike_sa), ike_sa->get_my_id(ike_sa),
59 ike_sa->get_other_host(ike_sa), ike_sa->get_other_id(ike_sa));
60
61 if (all)
62 {
63 fprintf(out, "%12s[%d]: IKE SPIs: %.16llx_i%s %.16llx_r%s",
64 ike_sa->get_name(ike_sa), ike_sa->get_unique_id(ike_sa),
65 id->get_initiator_spi(id), id->is_initiator(id) ? "*" : "",
66 id->get_responder_spi(id), id->is_initiator(id) ? "" : "*");
67
68 rekey = ike_sa->get_statistic(ike_sa, STAT_REKEY_TIME);
69 reauth = ike_sa->get_statistic(ike_sa, STAT_REAUTH_TIME);
70 if (rekey)
71 {
72 fprintf(out, ", rekeying in %V", &rekey);
73 }
74 if (reauth)
75 {
76 fprintf(out, ", reauthentication in %V", &reauth);
77 }
78 if (!rekey && !reauth)
79 {
80 fprintf(out, ", rekeying disabled");
81 }
82 fprintf(out, "\n");
83 }
84 }
85
86 /**
87 * log an CHILD_SA to out
88 */
89 static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
90 {
91 u_int32_t rekey, now = time(NULL);
92 u_int32_t use_in, use_out, use_fwd;
93 encryption_algorithm_t encr_alg;
94 integrity_algorithm_t int_alg;
95 size_t encr_len, int_len;
96 mode_t mode;
97
98 child_sa->get_stats(child_sa, &mode, &encr_alg, &encr_len,
99 &int_alg, &int_len, &rekey, &use_in, &use_out,
100 &use_fwd);
101
102 fprintf(out, "%12s{%d}: %N, %N",
103 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
104 child_sa_state_names, child_sa->get_state(child_sa),
105 mode_names, mode);
106
107 if (child_sa->get_state(child_sa) == CHILD_INSTALLED)
108 {
109 fprintf(out, ", %N SPIs: %.8x_i %.8x_o",
110 protocol_id_names, child_sa->get_protocol(child_sa),
111 htonl(child_sa->get_spi(child_sa, TRUE)),
112 htonl(child_sa->get_spi(child_sa, FALSE)));
113
114 if (all)
115 {
116 fprintf(out, "\n%12s{%d}: ", child_sa->get_name(child_sa),
117 child_sa->get_reqid(child_sa));
118
119
120 if (child_sa->get_protocol(child_sa) == PROTO_ESP)
121 {
122 fprintf(out, "%N", encryption_algorithm_names, encr_alg);
123
124 if (encr_len)
125 {
126 fprintf(out, "-%d", encr_len);
127 }
128 if (int_alg != AUTH_UNDEFINED)
129 {
130 fprintf(out, "/");
131 }
132 }
133
134 if (int_alg != AUTH_UNDEFINED)
135 {
136 fprintf(out, "%N", integrity_algorithm_names, int_alg);
137 if (int_len)
138 {
139 fprintf(out, "-%d", int_len);
140 }
141 }
142 fprintf(out, ", rekeying ");
143
144 if (rekey)
145 {
146 fprintf(out, "in %#V", &now, &rekey);
147 }
148 else
149 {
150 fprintf(out, "disabled");
151 }
152
153 fprintf(out, ", last use: ");
154 use_in = max(use_in, use_fwd);
155 if (use_in)
156 {
157 fprintf(out, "%ds_i ", now - use_in);
158 }
159 else
160 {
161 fprintf(out, "no_i ");
162 }
163 if (use_out)
164 {
165 fprintf(out, "%ds_o ", now - use_out);
166 }
167 else
168 {
169 fprintf(out, "no_o ");
170 }
171 }
172 }
173
174 fprintf(out, "\n%12s{%d}: %#R=== %#R\n",
175 child_sa->get_name(child_sa), child_sa->get_reqid(child_sa),
176 child_sa->get_traffic_selectors(child_sa, TRUE),
177 child_sa->get_traffic_selectors(child_sa, FALSE));
178 }
179
180 /**
181 * Implementation of stroke_list_t.status.
182 */
183 static void status(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out, bool all)
184 {
185 enumerator_t *enumerator, *children;
186 iterator_t *iterator;
187 host_t *host;
188 peer_cfg_t *peer_cfg;
189 ike_cfg_t *ike_cfg;
190 child_cfg_t *child_cfg;
191 ike_sa_t *ike_sa;
192 char *name = NULL;
193 bool found = FALSE;
194 time_t uptime;
195
196 name = msg->status.name;
197
198 if (all)
199 {
200 uptime = time(NULL) - this->uptime;
201 fprintf(out, "Performance:\n");
202 fprintf(out, " uptime: %V, since %#T\n", &uptime, &this->uptime, FALSE);
203 fprintf(out, " worker threads: %d idle of %d,",
204 charon->processor->get_idle_threads(charon->processor),
205 charon->processor->get_total_threads(charon->processor));
206 fprintf(out, " job queue load: %d,",
207 charon->processor->get_job_load(charon->processor));
208 fprintf(out, " scheduled events: %d\n",
209 charon->scheduler->get_job_load(charon->scheduler));
210 iterator = charon->kernel_interface->create_address_iterator(
211 charon->kernel_interface);
212 fprintf(out, "Listening IP addresses:\n");
213 while (iterator->iterate(iterator, (void**)&host))
214 {
215 fprintf(out, " %H\n", host);
216 }
217 iterator->destroy(iterator);
218
219 fprintf(out, "Connections:\n");
220 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends);
221 while (enumerator->enumerate(enumerator, (void**)&peer_cfg))
222 {
223 if (peer_cfg->get_ike_version(peer_cfg) != 2 ||
224 (name && !streq(name, peer_cfg->get_name(peer_cfg))))
225 {
226 continue;
227 }
228
229 ike_cfg = peer_cfg->get_ike_cfg(peer_cfg);
230 fprintf(out, "%12s: %H[%D]...%H[%D]\n", peer_cfg->get_name(peer_cfg),
231 ike_cfg->get_my_host(ike_cfg), peer_cfg->get_my_id(peer_cfg),
232 ike_cfg->get_other_host(ike_cfg), peer_cfg->get_other_id(peer_cfg));
233 /* TODO: list CAs and groups */
234 children = peer_cfg->create_child_cfg_enumerator(peer_cfg);
235 while (children->enumerate(children, &child_cfg))
236 {
237 linked_list_t *my_ts, *other_ts;
238 my_ts = child_cfg->get_traffic_selectors(child_cfg, TRUE, NULL, NULL);
239 other_ts = child_cfg->get_traffic_selectors(child_cfg, FALSE, NULL, NULL);
240 fprintf(out, "%12s: %#R=== %#R\n", child_cfg->get_name(child_cfg),
241 my_ts, other_ts);
242 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
243 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
244 }
245 children->destroy(children);
246 }
247 enumerator->destroy(enumerator);
248 }
249
250 fprintf(out, "Security Associations:\n");
251 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
252 while (enumerator->enumerate(enumerator, &ike_sa))
253 {
254 bool ike_printed = FALSE;
255 child_sa_t *child_sa;
256 iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa);
257
258 if (name == NULL || streq(name, ike_sa->get_name(ike_sa)))
259 {
260 log_ike_sa(out, ike_sa, all);
261 found = TRUE;
262 ike_printed = TRUE;
263 }
264
265 while (children->iterate(children, (void**)&child_sa))
266 {
267 if (name == NULL || streq(name, child_sa->get_name(child_sa)))
268 {
269 if (!ike_printed)
270 {
271 log_ike_sa(out, ike_sa, all);
272 found = TRUE;
273 ike_printed = TRUE;
274 }
275 log_child_sa(out, child_sa, all);
276 }
277 }
278 children->destroy(children);
279 }
280 enumerator->destroy(enumerator);
281
282 if (!found)
283 {
284 if (name)
285 {
286 fprintf(out, " no match\n");
287 }
288 else
289 {
290 fprintf(out, " none\n");
291 }
292 }
293 }
294
295 /**
296 * create a unique certificate list without duplicates
297 * certicates having the same issuer are grouped together.
298 */
299 static linked_list_t* create_unique_cert_list(certificate_type_t type)
300 {
301 linked_list_t *list = linked_list_create();
302 enumerator_t *enumerator = charon->credentials->create_cert_enumerator(
303 charon->credentials, type, KEY_ANY,
304 NULL, FALSE);
305 certificate_t *cert;
306
307 while (enumerator->enumerate(enumerator, (void**)&cert))
308 {
309 iterator_t *iterator = list->create_iterator(list, TRUE);
310 identification_t *issuer = cert->get_issuer(cert);
311 bool previous_same, same = FALSE, last = TRUE;
312 certificate_t *list_cert;
313
314 while (iterator->iterate(iterator, (void**)&list_cert))
315 {
316 /* exit if we have a duplicate? */
317 if (list_cert->equals(list_cert, cert))
318 {
319 last = FALSE;
320 break;
321 }
322 /* group certificates with same issuer */
323 previous_same = same;
324 same = list_cert->has_issuer(list_cert, issuer);
325 if (previous_same && !same)
326 {
327 iterator->insert_before(iterator, (void *)cert->get_ref(cert));
328 last = FALSE;
329 break;
330 }
331 }
332 iterator->destroy(iterator);
333
334 if (last)
335 {
336 list->insert_last(list, (void *)cert->get_ref(cert));
337 }
338 }
339 enumerator->destroy(enumerator);
340 return list;
341 }
342
343 /**
344 * list all X.509 certificates matching the flags
345 */
346 static void stroke_list_certs(linked_list_t *list, char *label,
347 x509_flag_t flags, bool utc, FILE *out)
348 {
349 bool first = TRUE;
350 time_t now = time(NULL);
351 enumerator_t *enumerator = list->create_enumerator(list);
352 certificate_t *cert;
353
354 while (enumerator->enumerate(enumerator, (void**)&cert))
355 {
356 x509_t *x509 = (x509_t*)cert;
357 x509_flag_t x509_flags = x509->get_flags(x509);
358
359 /* list only if flag is set, or flags == 0 (ignoring self-signed) */
360 if ((x509_flags & flags) || (flags == (x509_flags & ~X509_SELF_SIGNED)))
361 {
362 enumerator_t *enumerator;
363 identification_t *altName;
364 bool first_altName = TRUE;
365 chunk_t serial = x509->get_serial(x509);
366 identification_t *authkey = x509->get_authKeyIdentifier(x509);
367 time_t notBefore, notAfter;
368 public_key_t *public = cert->get_public_key(cert);
369
370 if (first)
371 {
372 fprintf(out, "\n");
373 fprintf(out, "List of %s:\n", label);
374 first = FALSE;
375 }
376 fprintf(out, "\n");
377
378 /* list subjectAltNames */
379 enumerator = x509->create_subjectAltName_enumerator(x509);
380 while (enumerator->enumerate(enumerator, (void**)&altName))
381 {
382 if (first_altName)
383 {
384 fprintf(out, " altNames: ");
385 first_altName = FALSE;
386 }
387 else
388 {
389 fprintf(out, ", ");
390 }
391 fprintf(out, "%D", altName);
392 }
393 if (!first_altName)
394 {
395 fprintf(out, "\n");
396 }
397 enumerator->destroy(enumerator);
398
399 fprintf(out, " subject: \"%D\"\n", cert->get_subject(cert));
400 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
401 fprintf(out, " serial: %#B\n", &serial);
402
403 /* list validity */
404 cert->get_validity(cert, &now, &notBefore, &notAfter);
405 fprintf(out, " validity: not before %#T, ", &notBefore, utc);
406 if (now < notBefore)
407 {
408 fprintf(out, "not valid yet (valid in %#V)\n", &now, &notBefore);
409 }
410 else
411 {
412 fprintf(out, "ok\n");
413 }
414 fprintf(out, " not after %#T, ", &notAfter, utc);
415 if (now > notAfter)
416 {
417 fprintf(out, "expired (%#V ago)\n", &now, &notAfter);
418 }
419 else
420 {
421 fprintf(out, "ok");
422 if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
423 {
424 fprintf(out, " (expires in %#V)", &now, &notAfter);
425 }
426 fprintf(out, " \n");
427 }
428
429 /* list public key information */
430 if (public)
431 {
432 private_key_t *private = NULL;
433 identification_t *id, *keyid;
434
435 id = public->get_id(public, ID_PUBKEY_SHA1);
436 keyid = public->get_id(public, ID_PUBKEY_INFO_SHA1);
437
438 private = charon->credentials->get_private(
439 charon->credentials,
440 public->get_type(public), keyid, NULL);
441 fprintf(out, " pubkey: %N %d bits%s\n",
442 key_type_names, public->get_type(public),
443 public->get_keysize(public) * 8,
444 private ? ", has private key" : "");
445 fprintf(out, " keyid: %D\n", keyid);
446 fprintf(out, " subjkey: %D\n", id);
447 DESTROY_IF(private);
448 public->destroy(public);
449 }
450
451 /* list optional authorityKeyIdentifier */
452 if (authkey)
453 {
454 fprintf(out, " authkey: %D\n", authkey);
455 }
456 }
457 }
458 enumerator->destroy(enumerator);
459 }
460
461 /**
462 * list all X.509 attribute certificates
463 */
464 static void stroke_list_acerts(bool utc, FILE *out)
465 {
466
467 }
468
469 /**
470 * list all X.509 CRLs
471 */
472 static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
473 {
474 bool first = TRUE;
475 time_t thisUpdate, nextUpdate, now = time(NULL);
476 enumerator_t *enumerator = list->create_enumerator(list);
477 certificate_t *cert;
478
479 while (enumerator->enumerate(enumerator, (void**)&cert))
480 {
481 crl_t *crl = (crl_t*)cert;
482 chunk_t serial = crl->get_serial(crl);
483 identification_t *authkey = crl->get_authKeyIdentifier(crl);
484
485 if (first)
486 {
487 fprintf(out, "\n");
488 fprintf(out, "List of X.509 CRLs:\n");
489 first = FALSE;
490 }
491 fprintf(out, "\n");
492
493 fprintf(out, " issuer: \"%D\"\n", cert->get_issuer(cert));
494
495 /* list optional crlNumber */
496 if (serial.ptr)
497 {
498 fprintf(out, " serial: %#B\n", &serial);
499 }
500
501 /* count the number of revoked certificates */
502 {
503 int count = 0;
504 enumerator_t *enumerator = crl->create_enumerator(crl);
505
506 while (enumerator->enumerate(enumerator, NULL, NULL, NULL))
507 {
508 count++;
509 }
510 fprintf(out, " revoked: %d certificate%s\n", count,
511 (count == 1)? "" : "s");
512 enumerator->destroy(enumerator);
513 }
514
515 /* list validity */
516 cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
517 fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
518 fprintf(out, " next %#T, ", &nextUpdate, utc);
519 if (now > nextUpdate)
520 {
521 fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
522 }
523 else
524 {
525 fprintf(out, "ok");
526 if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
527 {
528 fprintf(out, " (expires in %#V)", &now, &nextUpdate);
529 }
530 fprintf(out, " \n");
531 }
532
533 /* list optional authorityKeyIdentifier */
534 if (authkey)
535 {
536 fprintf(out, " authkey: %D\n", authkey);
537 }
538 }
539 enumerator->destroy(enumerator);
540 }
541
542 /**
543 * list all OCSP responses
544 */
545 static void stroke_list_ocsp(linked_list_t* list, bool utc, FILE *out)
546 {
547 bool first = TRUE;
548 enumerator_t *enumerator = list->create_enumerator(list);
549 certificate_t *cert;
550
551 while (enumerator->enumerate(enumerator, (void**)&cert))
552 {
553 if (first)
554 {
555 fprintf(out, "\n");
556 fprintf(out, "List of OCSP responses:\n");
557 fprintf(out, "\n");
558 first = FALSE;
559 }
560
561 fprintf(out, " signer: \"%D\"\n", cert->get_issuer(cert));
562 }
563 enumerator->destroy(enumerator);
564 }
565
566 /**
567 * Implementation of stroke_list_t.list.
568 */
569 static void list(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out)
570 {
571 linked_list_t *cert_list = NULL;
572
573 if (msg->list.flags & (LIST_CERTS | LIST_CACERTS | LIST_OCSPCERTS | LIST_AACERTS))
574 {
575 cert_list = create_unique_cert_list(CERT_X509);
576 }
577 if (msg->list.flags & LIST_CERTS)
578 {
579 stroke_list_certs(cert_list, "X.509 End Entity Certificates",
580 0, msg->list.utc, out);
581 }
582 if (msg->list.flags & LIST_CACERTS)
583 {
584 stroke_list_certs(cert_list, "X.509 CA Certificates",
585 X509_CA, msg->list.utc, out);
586 }
587 if (msg->list.flags & LIST_OCSPCERTS)
588 {
589 stroke_list_certs(cert_list, "X.509 OCSP Signer Certificates",
590 X509_OCSP_SIGNER, msg->list.utc, out);
591 }
592 if (msg->list.flags & LIST_AACERTS)
593 {
594 stroke_list_certs(cert_list, "X.509 AA Certificates",
595 X509_AA, msg->list.utc, out);
596 }
597 if (msg->list.flags & LIST_ACERTS)
598 {
599 stroke_list_acerts(msg->list.utc, out);
600 }
601 if (msg->list.flags & LIST_CRLS)
602 {
603 linked_list_t *crl_list = create_unique_cert_list(CERT_X509_CRL);
604
605 stroke_list_crls(crl_list, msg->list.utc, out);
606 crl_list->destroy_offset(crl_list, offsetof(certificate_t, destroy));
607 }
608 if (msg->list.flags & LIST_OCSP)
609 {
610 linked_list_t *ocsp_list = create_unique_cert_list(CERT_X509_OCSP_RESPONSE);
611
612 stroke_list_ocsp(ocsp_list, msg->list.utc, out);
613 ocsp_list->destroy_offset(ocsp_list, offsetof(certificate_t, destroy));
614 }
615 DESTROY_OFFSET_IF(cert_list, offsetof(certificate_t, destroy));
616 }
617
618 /**
619 * Implementation of stroke_list_t.destroy
620 */
621 static void destroy(private_stroke_list_t *this)
622 {
623 free(this);
624 }
625
626 /*
627 * see header file
628 */
629 stroke_list_t *stroke_list_create()
630 {
631 private_stroke_list_t *this = malloc_thing(private_stroke_list_t);
632
633 this->public.list = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out))list;
634 this->public.status = (void(*)(stroke_list_t*, stroke_msg_t *msg, FILE *out,bool))status;
635 this->public.destroy = (void(*)(stroke_list_t*))destroy;
636
637 this->uptime = time(NULL);
638
639 return &this->public;
640 }
641