|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 | + |
| 4 | +#include <openssl/base.h> |
| 5 | +#include <openssl/x509.h> |
| 6 | +#include <openssl/pem.h> |
| 7 | +#include "internal.h" |
| 8 | + |
| 9 | +// TO-DO: We do not support using a default trust store, therefore -CAfile must |
| 10 | +// be a required argument. Once support for default trust stores is added, |
| 11 | +// make it an optional argument. |
| 12 | +static const argument_t kArguments[] = { |
| 13 | + { "-help", kBooleanArgument, "Display option summary" }, |
| 14 | + { "-CAfile", kRequiredArgument, "A file of trusted certificates. The " |
| 15 | + "file should contain one or more certificates in PEM format." }, |
| 16 | + { "", kOptionalArgument, "" } |
| 17 | +}; |
| 18 | + |
| 19 | +// setup_verification_store sets up an X509 certificate store for verification. |
| 20 | +// It configures the store with file and directory lookups. It loads the |
| 21 | +// specified CA file if provided and otherwise uses default locations. |
| 22 | +static X509_STORE *setup_verification_store(std::string CAfile) { |
| 23 | + bssl::UniquePtr<X509_STORE> store(X509_STORE_new()); |
| 24 | + X509_LOOKUP *lookup; |
| 25 | + |
| 26 | + if (!store) { |
| 27 | + return nullptr; |
| 28 | + } |
| 29 | + |
| 30 | + if (!CAfile.empty()) { |
| 31 | + lookup = X509_STORE_add_lookup(store.get(), X509_LOOKUP_file()); |
| 32 | + if (!lookup || !X509_LOOKUP_load_file(lookup, CAfile.c_str(), X509_FILETYPE_PEM)) { |
| 33 | + fprintf(stderr, "Error loading file %s\n", CAfile.c_str()); |
| 34 | + return nullptr; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Add default dir path |
| 39 | + lookup = X509_STORE_add_lookup(store.get(), X509_LOOKUP_hash_dir()); |
| 40 | + if (!lookup || !X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT)) { |
| 41 | + return nullptr; |
| 42 | + } |
| 43 | + |
| 44 | + return store.release(); |
| 45 | +} |
| 46 | + |
| 47 | +static int cb(int ok, X509_STORE_CTX *ctx) { |
| 48 | + if (!ok) { |
| 49 | + int cert_error = X509_STORE_CTX_get_error(ctx); |
| 50 | + X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx); |
| 51 | + |
| 52 | + if (current_cert != NULL) { |
| 53 | + X509_NAME_print_ex_fp(stderr, |
| 54 | + X509_get_subject_name(current_cert), |
| 55 | + 0, XN_FLAG_ONELINE); |
| 56 | + fprintf(stderr, "\n"); |
| 57 | + } |
| 58 | + fprintf(stderr, "%serror %d at %d depth lookup: %s\n", |
| 59 | + X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "", |
| 60 | + cert_error, |
| 61 | + X509_STORE_CTX_get_error_depth(ctx), |
| 62 | + X509_verify_cert_error_string(cert_error)); |
| 63 | + |
| 64 | + /* |
| 65 | + * Pretend that some errors are ok, so they don't stop further |
| 66 | + * processing of the certificate chain. Setting ok = 1 does this. |
| 67 | + * After X509_verify_cert() is done, we verify that there were |
| 68 | + * no actual errors, even if the returned value was positive. |
| 69 | + */ |
| 70 | + switch (cert_error) { |
| 71 | + case X509_V_ERR_NO_EXPLICIT_POLICY: |
| 72 | + /* fall thru */ |
| 73 | + case X509_V_ERR_CERT_HAS_EXPIRED: |
| 74 | + /* Continue even if the leaf is a self-signed cert */ |
| 75 | + case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
| 76 | + /* Continue after extension errors too */ |
| 77 | + case X509_V_ERR_INVALID_CA: |
| 78 | + case X509_V_ERR_INVALID_NON_CA: |
| 79 | + case X509_V_ERR_PATH_LENGTH_EXCEEDED: |
| 80 | + case X509_V_ERR_CRL_HAS_EXPIRED: |
| 81 | + case X509_V_ERR_CRL_NOT_YET_VALID: |
| 82 | + case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION: |
| 83 | + /* errors due to strict conformance checking (-x509_strict) */ |
| 84 | + case X509_V_ERR_INVALID_PURPOSE: |
| 85 | + ok = 1; |
| 86 | + } |
| 87 | + } |
| 88 | + return ok; |
| 89 | +} |
| 90 | + |
| 91 | +static int check(X509_STORE *ctx, const char *file) { |
| 92 | + bssl::UniquePtr<X509> cert; |
| 93 | + int i = 0, ret = 0; |
| 94 | + |
| 95 | + if (file) { |
| 96 | + ScopedFILE cert_file(fopen(file, "rb")); |
| 97 | + if (!cert_file) { |
| 98 | + fprintf(stderr, "error %s: reading certificate failed\n", file); |
| 99 | + return 0; |
| 100 | + } |
| 101 | + cert.reset(PEM_read_X509(cert_file.get(), nullptr, nullptr, nullptr)); |
| 102 | + |
| 103 | + } else { |
| 104 | + bssl::UniquePtr<BIO> input(BIO_new_fp(stdin, BIO_CLOSE)); |
| 105 | + cert.reset(PEM_read_bio_X509(input.get(), nullptr, nullptr, nullptr)); |
| 106 | + } |
| 107 | + |
| 108 | + if (cert.get() == nullptr) { |
| 109 | + return 0; |
| 110 | + } |
| 111 | + |
| 112 | + bssl::UniquePtr<X509_STORE_CTX> store_ctx(X509_STORE_CTX_new()); |
| 113 | + if (store_ctx == nullptr || store_ctx.get() == nullptr) { |
| 114 | + fprintf(stderr, "error %s: X.509 store context allocation failed\n", |
| 115 | + (file == nullptr) ? "stdin" : file); |
| 116 | + return 0; |
| 117 | + } |
| 118 | + |
| 119 | + if (!X509_STORE_CTX_init(store_ctx.get(), ctx, cert.get(), nullptr)) { |
| 120 | + fprintf(stderr, |
| 121 | + "error %s: X.509 store context initialization failed\n", |
| 122 | + (file == nullptr) ? "stdin" : file); |
| 123 | + return 0; |
| 124 | + } |
| 125 | + |
| 126 | + i = X509_verify_cert(store_ctx.get()); |
| 127 | + if (i > 0 && X509_STORE_CTX_get_error(store_ctx.get()) == X509_V_OK) { |
| 128 | + fprintf(stdout, "%s: OK\n", (file == nullptr) ? "stdin" : file); |
| 129 | + ret = 1; |
| 130 | + } else { |
| 131 | + fprintf(stderr, |
| 132 | + "error %s: verification failed\n", |
| 133 | + (file == nullptr) ? "stdin" : file); |
| 134 | + } |
| 135 | + |
| 136 | + return ret; |
| 137 | +} |
| 138 | + |
| 139 | +bool VerifyTool(const args_list_t &args) { |
| 140 | + std::string cafile; |
| 141 | + size_t i = 0; |
| 142 | + |
| 143 | + if (args.size() == 1 && args[0] == "-help") { |
| 144 | + fprintf(stderr, |
| 145 | + "Usage: verify [options] [cert.pem...]\n" |
| 146 | + "Certificates must be in PEM format. They can be specified in one or more files.\n" |
| 147 | + "If no files are specified, the tool will read from stdin.\n\n" |
| 148 | + "Valid options are:\n"); |
| 149 | + PrintUsage(kArguments); |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + // i helps track whether input will be provided via stdin or through a file |
| 154 | + if (args.size() >= 1 && args[0] == "-CAfile") { |
| 155 | + cafile = args[1]; |
| 156 | + i += 2; |
| 157 | + } else { |
| 158 | + fprintf(stderr, "-CAfile must be specified. This tool does not load" |
| 159 | + " the default trust store.\n"); |
| 160 | + return false; |
| 161 | + } |
| 162 | + |
| 163 | + bssl::UniquePtr<X509_STORE> store(setup_verification_store(cafile)); |
| 164 | + // Initialize certificate verification store |
| 165 | + if (!store.get()) { |
| 166 | + fprintf(stderr, "Error: Unable to setup certificate verification store."); |
| 167 | + return false; |
| 168 | + } |
| 169 | + X509_STORE_set_verify_cb(store.get(), cb); |
| 170 | + |
| 171 | + ERR_clear_error(); |
| 172 | + |
| 173 | + int ret = 1; |
| 174 | + |
| 175 | + // No additional file or certs provided, read from stdin |
| 176 | + if (args.size() == i) { |
| 177 | + ret &= check(store.get(), NULL); |
| 178 | + } else { |
| 179 | + // Certs provided as files |
| 180 | + for (; i < args.size(); i++) { |
| 181 | + ret &= check(store.get(), args[i].c_str()); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return ret == 1; |
| 186 | +} |
0 commit comments