source: creddy/display.c @ 405bba3

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since 405bba3 was 405bba3, checked in by Mike Ryan <mikeryan@…>, 13 years ago

show cert validity period
see #17

  • Property mode set to 100644
File size: 2.4 KB
Line 
1#include <creddy.h>
2
3#include "creddy_common.h"
4
5// helper
6static void _print_validity(time_t not_before, time_t not_after);
7
8void display_main(options_t *opts) {
9    if (opts->cert == NULL)
10        usage(opts);
11
12    char *show = opts->show;
13    if (show == NULL)
14        usage(opts);
15
16    int show_issuer = 0;
17    int show_validity = 0;
18    char *opt;
19
20    while ((opt = strsep(&show, ",")) != NULL) {
21        if (strcmp(opt, "issuer") == 0)
22            show_issuer = 1;
23        else if (strcmp(opt, "validity") == 0)
24            show_validity = 1;
25        else if (strcmp(opt, "all") == 0) {
26            show_issuer = 1;
27            show_validity = 1;
28        }
29        else {
30            printf("Error: Unknown option to --show: %s\n", opt);
31            usage(opts);
32        }
33    }
34
35    // first try ID cert
36    creddy_id_t *id = creddy_id_from_file(opts->cert);
37    if (id != NULL) {
38        if (show_issuer) {
39            char *issuer = creddy_id_issuer(id);
40            printf("Issuer: %s\n", issuer);
41            free(issuer);
42        }
43
44        if (show_validity) {
45            time_t not_before, not_after;
46            creddy_id_validity(id, &not_before, &not_after);
47            _print_validity(not_before, not_after);
48        }
49
50        creddy_id_free(id);
51        return;
52    }
53
54    // then try attribute cert
55    certificate_t *ac = lib->creds->create(lib->creds,
56        CRED_CERTIFICATE, CERT_X509_AC,
57        BUILD_FROM_FILE, opts->cert,
58        BUILD_END
59    );
60    if (ac != NULL) {
61        if (show_issuer)
62            printf("Issuer: %Y\n", ac->get_issuer(ac));
63
64        if (show_validity) {
65            time_t not_before, not_after;
66            ac->get_validity(ac, NULL, &not_before, &not_after);
67            _print_validity(not_before, not_after);
68        }
69
70        DESTROY_IF(ac);
71        return;
72    }
73
74    // give up if neither works
75    errx(1, "Couldn't load %s as an ID or attribute cert", opts->cert);
76}
77
78// display the validity period of a cert
79static void _print_validity(time_t not_before, time_t not_after) {
80    char buf[256];
81    struct tm time_tm;
82
83    printf("Validity:\n");
84
85    strftime(buf, sizeof(buf), "%F %T %Z", localtime_r(&not_before, &time_tm));
86    printf("    Not before: %s [%lld]\n", buf, (long long)not_before);
87
88    strftime(buf, sizeof(buf), "%F %T %Z", localtime_r(&not_after, &time_tm));
89    printf("    Not after:  %s [%lld]\n", buf, (long long)not_after);
90}
Note: See TracBrowser for help on using the repository browser.