[0aaa651] | 1 | #include <creddy.h> |
---|
| 2 | |
---|
| 3 | #include "creddy_common.h" |
---|
| 4 | |
---|
[405bba3] | 5 | // helper |
---|
| 6 | static void _print_validity(time_t not_before, time_t not_after); |
---|
| 7 | |
---|
[0aaa651] | 8 | void 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; |
---|
[b19d1f0] | 17 | int show_subject = 0; |
---|
[405bba3] | 18 | int show_validity = 0; |
---|
[0aaa651] | 19 | char *opt; |
---|
| 20 | |
---|
| 21 | while ((opt = strsep(&show, ",")) != NULL) { |
---|
| 22 | if (strcmp(opt, "issuer") == 0) |
---|
| 23 | show_issuer = 1; |
---|
[b19d1f0] | 24 | else if (strcmp(opt, "subject") == 0) |
---|
| 25 | show_subject = 1; |
---|
[405bba3] | 26 | else if (strcmp(opt, "validity") == 0) |
---|
| 27 | show_validity = 1; |
---|
[0aaa651] | 28 | else if (strcmp(opt, "all") == 0) { |
---|
| 29 | show_issuer = 1; |
---|
[b19d1f0] | 30 | show_subject = 1; |
---|
[405bba3] | 31 | show_validity = 1; |
---|
[0aaa651] | 32 | } |
---|
| 33 | else { |
---|
| 34 | printf("Error: Unknown option to --show: %s\n", opt); |
---|
| 35 | usage(opts); |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | // first try ID cert |
---|
| 40 | creddy_id_t *id = creddy_id_from_file(opts->cert); |
---|
| 41 | if (id != NULL) { |
---|
| 42 | if (show_issuer) { |
---|
| 43 | char *issuer = creddy_id_issuer(id); |
---|
[405bba3] | 44 | printf("Issuer: %s\n", issuer); |
---|
| 45 | free(issuer); |
---|
| 46 | } |
---|
| 47 | |
---|
[b19d1f0] | 48 | if (show_subject) { |
---|
| 49 | char *subject = creddy_id_subject(id); |
---|
| 50 | printf("Subject: %s\n", subject); |
---|
| 51 | free(subject); |
---|
| 52 | } |
---|
| 53 | |
---|
[405bba3] | 54 | if (show_validity) { |
---|
| 55 | time_t not_before, not_after; |
---|
| 56 | creddy_id_validity(id, ¬_before, ¬_after); |
---|
| 57 | _print_validity(not_before, not_after); |
---|
[0aaa651] | 58 | } |
---|
| 59 | |
---|
| 60 | creddy_id_free(id); |
---|
| 61 | return; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | // then try attribute cert |
---|
| 65 | certificate_t *ac = lib->creds->create(lib->creds, |
---|
| 66 | CRED_CERTIFICATE, CERT_X509_AC, |
---|
| 67 | BUILD_FROM_FILE, opts->cert, |
---|
| 68 | BUILD_END |
---|
| 69 | ); |
---|
| 70 | if (ac != NULL) { |
---|
| 71 | if (show_issuer) |
---|
[405bba3] | 72 | printf("Issuer: %Y\n", ac->get_issuer(ac)); |
---|
| 73 | |
---|
[b19d1f0] | 74 | if (show_subject) |
---|
| 75 | printf("Subject: %Y\n", ac->get_subject(ac)); |
---|
| 76 | |
---|
[405bba3] | 77 | if (show_validity) { |
---|
| 78 | time_t not_before, not_after; |
---|
| 79 | ac->get_validity(ac, NULL, ¬_before, ¬_after); |
---|
| 80 | _print_validity(not_before, not_after); |
---|
| 81 | } |
---|
[0aaa651] | 82 | |
---|
| 83 | DESTROY_IF(ac); |
---|
| 84 | return; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | // give up if neither works |
---|
| 88 | errx(1, "Couldn't load %s as an ID or attribute cert", opts->cert); |
---|
| 89 | } |
---|
[405bba3] | 90 | |
---|
| 91 | // display the validity period of a cert |
---|
| 92 | static void _print_validity(time_t not_before, time_t not_after) { |
---|
| 93 | char buf[256]; |
---|
| 94 | struct tm time_tm; |
---|
| 95 | |
---|
| 96 | printf("Validity:\n"); |
---|
| 97 | |
---|
| 98 | strftime(buf, sizeof(buf), "%F %T %Z", localtime_r(¬_before, &time_tm)); |
---|
| 99 | printf(" Not before: %s [%lld]\n", buf, (long long)not_before); |
---|
| 100 | |
---|
| 101 | strftime(buf, sizeof(buf), "%F %T %Z", localtime_r(¬_after, &time_tm)); |
---|
| 102 | printf(" Not after: %s [%lld]\n", buf, (long long)not_after); |
---|
| 103 | } |
---|