|
| 1 | +/* LibTomCrypt, modular cryptographic library -- Tom St Denis */ |
| 2 | +/* SPDX-License-Identifier: Unlicense */ |
| 3 | +/* load a X.509 certificate chain and verify its validity */ |
| 4 | +#include <tomcrypt.h> |
| 5 | +#include <stdarg.h> |
| 6 | + |
| 7 | +#ifdef LTC_QUIET |
| 8 | +static void print_err(const char *fmt, ...) |
| 9 | +{ |
| 10 | + LTC_UNUSED_PARAM(fmt); |
| 11 | +} |
| 12 | + |
| 13 | +#define print_stderr(...) |
| 14 | +#else |
| 15 | +static void print_err(const char *fmt, ...) |
| 16 | +{ |
| 17 | + va_list args; |
| 18 | + |
| 19 | + va_start(args, fmt); |
| 20 | + vfprintf(stderr, fmt, args); |
| 21 | + va_end(args); |
| 22 | +} |
| 23 | + |
| 24 | +#define print_stderr(...) fprintf(stderr, ##__VA_ARGS__) |
| 25 | +#endif |
| 26 | + |
| 27 | + |
| 28 | +static unsigned long num_certs; |
| 29 | +static const ltc_x509_certificate *cert[32] = {0}; |
| 30 | +static FILE *f; |
| 31 | + |
| 32 | +static void die_(int err, int line) |
| 33 | +{ |
| 34 | + unsigned long n; |
| 35 | + print_err("%3d: LTC sez %s\n", line, error_to_string(err)); |
| 36 | + for (n = num_certs; n --> 0;) { |
| 37 | + x509_free(&cert[n]); |
| 38 | + } |
| 39 | + if (f) fclose(f); |
| 40 | + exit(EXIT_FAILURE); |
| 41 | +} |
| 42 | + |
| 43 | +#define die(i) do { die_(i, __LINE__); } while(0) |
| 44 | +#define DIE(s, ...) do { print_err("%3d: " s "\n", __LINE__, ##__VA_ARGS__); exit(EXIT_FAILURE); } while(0) |
| 45 | +#ifndef LTC_ARRAY_SIZE |
| 46 | +#define LTC_ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0])) |
| 47 | +#endif |
| 48 | + |
| 49 | +int main(int argc, char **argv) |
| 50 | +{ |
| 51 | + const unsigned char zero_cert_buf[sizeof(cert)] = {0}; |
| 52 | + int err, argn = 1; |
| 53 | + unsigned long len, processed, n; |
| 54 | + long tell, tot_data = 0; |
| 55 | + |
| 56 | + if ((err = register_all_hashes()) != CRYPT_OK) { |
| 57 | + die(err); |
| 58 | + } |
| 59 | + if ((err = crypt_mp_init("ltm")) != CRYPT_OK) { |
| 60 | + die(err); |
| 61 | + } |
| 62 | + |
| 63 | +next: |
| 64 | + tot_data = processed = num_certs = n = 0; |
| 65 | + if (argc > argn) f = fopen(argv[argn], "r"); |
| 66 | + else f = stdin; |
| 67 | + if (f == NULL) DIE("fopen sez no"); |
| 68 | + if (f != stdin) { |
| 69 | + fseek(f, 0, SEEK_END); |
| 70 | + tot_data = ftell(f); |
| 71 | + rewind(f); |
| 72 | + tell = 0; |
| 73 | + } else { |
| 74 | + tell = -1; |
| 75 | + } |
| 76 | + |
| 77 | + print_stderr("-=-=-=-=-=-=-\nDecode %s\n=-=-=-=-=-=-=\n", argv[argn]); |
| 78 | + |
| 79 | + while (tell != tot_data && (err = x509_import_pem_filehandle(f, &cert[n])) == CRYPT_OK) { |
| 80 | + if (f != stdin) { |
| 81 | + tell = ftell(f); |
| 82 | + print_stderr("len: %ld - tot: %ld - processed: %lu\n", tell, tot_data, processed); |
| 83 | + len = tell - processed; |
| 84 | + processed += len; |
| 85 | + } |
| 86 | + n++; |
| 87 | + if (n == LTC_ARRAY_SIZE(cert)) |
| 88 | + break; |
| 89 | + } |
| 90 | + num_certs = n; |
| 91 | + print_stderr("len: %ld - tot: %ld - processed: %lu\n", tell, tot_data, processed); |
| 92 | + if (err && argc > argn) goto check_next; |
| 93 | + if (err && err != CRYPT_NOP) die(err); |
| 94 | + for (n = 0; n < num_certs; ++n) { |
| 95 | + unsigned long m = n + 1 == num_certs ? n : n + 1; |
| 96 | + int stat; |
| 97 | + if ((err = x509_cert_is_signed_by(cert[n], &cert[m]->tbs_certificate.subject_public_key_info, &stat)) != CRYPT_OK) { |
| 98 | + print_err("%3d: LTC sez %s\n", __LINE__, error_to_string(err)); |
| 99 | + if (m == n) { |
| 100 | + print_stderr("Cert is last in chain, but not self-signed.\n"); |
| 101 | + } else { |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + { |
| 106 | + const ltc_x509_string *subjects[4]; |
| 107 | + int issuer_matches_subject = x509_cmp_name(&cert[n]->tbs_certificate.issuer, &cert[m]->tbs_certificate.subject); |
| 108 | + x509_name_detail_get(&cert[n]->tbs_certificate.subject, LTC_X509_CN, &subjects[0]); |
| 109 | + x509_name_detail_get(&cert[n]->tbs_certificate.subject, LTC_X509_O, &subjects[2]); |
| 110 | + if (n != m) { |
| 111 | + x509_name_detail_get(&cert[m]->tbs_certificate.subject, LTC_X509_CN, &subjects[1]); |
| 112 | + x509_name_detail_get(&cert[m]->tbs_certificate.subject, LTC_X509_O, &subjects[3]); |
| 113 | + } else { |
| 114 | + x509_name_detail_get(&cert[m]->tbs_certificate.issuer, LTC_X509_CN, &subjects[1]); |
| 115 | + x509_name_detail_get(&cert[m]->tbs_certificate.issuer, LTC_X509_O, &subjects[3]); |
| 116 | + } |
| 117 | +#define X509_STRING_STR(s) (s) ? (s)->str : "NULL" |
| 118 | + print_stderr("Cert: %s - %s\nCA: %s - %s\nIssuer matches subject: %s\nVerify: %s\n", |
| 119 | + X509_STRING_STR(subjects[0]), X509_STRING_STR(subjects[2]), |
| 120 | + X509_STRING_STR(subjects[1]), X509_STRING_STR(subjects[3]), |
| 121 | + issuer_matches_subject ? "True" : "False", stat ? "Success" : "Failed"); |
| 122 | + /* In case of LTC_QUIET this would show up as unused. */ |
| 123 | + LTC_UNUSED_PARAM(issuer_matches_subject); |
| 124 | + } |
| 125 | + } |
| 126 | +check_next: |
| 127 | + for (n = num_certs; n --> 0;) { |
| 128 | + x509_free(&cert[n]); |
| 129 | + } |
| 130 | + if (XMEMCMP(cert, zero_cert_buf, sizeof(zero_cert_buf))) { |
| 131 | + DIE("cert buf not completely cleaned"); |
| 132 | + } |
| 133 | + if (f != stdin) { |
| 134 | + fclose(f); |
| 135 | + argn++; |
| 136 | + if (argc > argn) { |
| 137 | + goto next; |
| 138 | + } |
| 139 | + } |
| 140 | + return 0; |
| 141 | +} |
0 commit comments