1 1.1 christos /* 2 1.1 christos * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * 4 1.1 christos * Licensed under the Apache License 2.0 (the "License"). You may not use 5 1.1 christos * this file except in compliance with the License. You can obtain a copy 6 1.1 christos * in the file LICENSE in the source distribution or at 7 1.1 christos * https://www.openssl.org/source/license.html 8 1.1 christos */ 9 1.1 christos 10 1.1 christos #include <stdio.h> 11 1.1 christos #include <stdlib.h> 12 1.1 christos #include <string.h> 13 1.1 christos #include "apps.h" 14 1.1 christos #include "progs.h" 15 1.1 christos #include <openssl/bio.h> 16 1.1 christos #include <openssl/err.h> 17 1.1 christos #include <openssl/x509.h> 18 1.1 christos #include <openssl/x509v3.h> 19 1.1 christos #include <openssl/pem.h> 20 1.1 christos 21 1.1 christos static int cb(int ok, X509_STORE_CTX *ctx); 22 1.1 christos static int check(X509_STORE *ctx, const char *file, 23 1.1.1.2 christos STACK_OF(X509) *uchain, STACK_OF(X509) *tchain, 24 1.1.1.2 christos STACK_OF(X509_CRL) *crls, int show_chain, 25 1.1.1.2 christos STACK_OF(OPENSSL_STRING) *opts); 26 1.1 christos static int v_verbose = 0, vflags = 0; 27 1.1 christos 28 1.1 christos typedef enum OPTION_choice { 29 1.1 christos OPT_COMMON, 30 1.1.1.2 christos OPT_ENGINE, 31 1.1.1.2 christos OPT_CAPATH, 32 1.1.1.2 christos OPT_CAFILE, 33 1.1.1.2 christos OPT_CASTORE, 34 1.1.1.2 christos OPT_NOCAPATH, 35 1.1.1.2 christos OPT_NOCAFILE, 36 1.1.1.2 christos OPT_NOCASTORE, 37 1.1.1.2 christos OPT_UNTRUSTED, 38 1.1.1.2 christos OPT_TRUSTED, 39 1.1.1.2 christos OPT_CRLFILE, 40 1.1.1.2 christos OPT_CRL_DOWNLOAD, 41 1.1.1.2 christos OPT_SHOW_CHAIN, 42 1.1.1.2 christos OPT_V_ENUM, 43 1.1.1.2 christos OPT_NAMEOPT, 44 1.1.1.2 christos OPT_VFYOPT, 45 1.1 christos OPT_VERBOSE, 46 1.1 christos OPT_PROV_ENUM 47 1.1 christos } OPTION_CHOICE; 48 1.1 christos 49 1.1 christos const OPTIONS verify_options[] = { 50 1.1.1.2 christos { OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n" }, 51 1.1 christos 52 1.1 christos OPT_SECTION("General"), 53 1.1.1.2 christos { "help", OPT_HELP, '-', "Display this summary" }, 54 1.1 christos #ifndef OPENSSL_NO_ENGINE 55 1.1.1.2 christos { "engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device" }, 56 1.1 christos #endif 57 1.1.1.2 christos { "verbose", OPT_VERBOSE, '-', 58 1.1.1.2 christos "Print extra information about the operations being performed." }, 59 1.1.1.2 christos { "nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options" }, 60 1.1 christos 61 1.1 christos OPT_SECTION("Certificate chain"), 62 1.1.1.2 christos { "trusted", OPT_TRUSTED, '<', "A file of trusted certificates" }, 63 1.1.1.2 christos { "CAfile", OPT_CAFILE, '<', "A file of trusted certificates" }, 64 1.1.1.2 christos { "CApath", OPT_CAPATH, '/', "A directory of files with trusted certificates" }, 65 1.1.1.2 christos { "CAstore", OPT_CASTORE, ':', "URI to a store of trusted certificates" }, 66 1.1.1.2 christos { "no-CAfile", OPT_NOCAFILE, '-', 67 1.1.1.2 christos "Do not load the default trusted certificates file" }, 68 1.1.1.2 christos { "no-CApath", OPT_NOCAPATH, '-', 69 1.1.1.2 christos "Do not load trusted certificates from the default directory" }, 70 1.1.1.2 christos { "no-CAstore", OPT_NOCASTORE, '-', 71 1.1.1.2 christos "Do not load trusted certificates from the default certificates store" }, 72 1.1.1.2 christos { "untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates" }, 73 1.1.1.2 christos { "CRLfile", OPT_CRLFILE, '<', 74 1.1.1.2 christos "File containing one or more CRL's (in PEM format) to load" }, 75 1.1.1.2 christos { "crl_download", OPT_CRL_DOWNLOAD, '-', 76 1.1.1.2 christos "Try downloading CRL information for certificates via their CDP entries" }, 77 1.1.1.2 christos { "show_chain", OPT_SHOW_CHAIN, '-', 78 1.1.1.2 christos "Display information about the certificate chain" }, 79 1.1 christos 80 1.1 christos OPT_V_OPTIONS, 81 1.1.1.2 christos { "vfyopt", OPT_VFYOPT, 's', "Verification parameter in n:v form" }, 82 1.1 christos 83 1.1 christos OPT_PROV_OPTIONS, 84 1.1 christos 85 1.1 christos OPT_PARAMETERS(), 86 1.1.1.2 christos { "cert", 0, 0, "Certificate(s) to verify (optional; stdin used otherwise)" }, 87 1.1.1.2 christos { NULL } 88 1.1 christos }; 89 1.1 christos 90 1.1 christos int verify_main(int argc, char **argv) 91 1.1 christos { 92 1.1 christos ENGINE *e = NULL; 93 1.1 christos STACK_OF(X509) *untrusted = NULL, *trusted = NULL; 94 1.1 christos STACK_OF(X509_CRL) *crls = NULL; 95 1.1 christos STACK_OF(OPENSSL_STRING) *vfyopts = NULL; 96 1.1 christos X509_STORE *store = NULL; 97 1.1 christos X509_VERIFY_PARAM *vpm = NULL; 98 1.1 christos const char *prog, *CApath = NULL, *CAfile = NULL, *CAstore = NULL; 99 1.1 christos int noCApath = 0, noCAfile = 0, noCAstore = 0; 100 1.1 christos int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1; 101 1.1 christos OPTION_CHOICE o; 102 1.1 christos 103 1.1 christos if ((vpm = X509_VERIFY_PARAM_new()) == NULL) 104 1.1 christos goto end; 105 1.1 christos 106 1.1 christos prog = opt_init(argc, argv, verify_options); 107 1.1 christos while ((o = opt_next()) != OPT_EOF) { 108 1.1 christos switch (o) { 109 1.1 christos case OPT_EOF: 110 1.1 christos case OPT_ERR: 111 1.1.1.2 christos opthelp: 112 1.1 christos BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); 113 1.1 christos goto end; 114 1.1 christos case OPT_HELP: 115 1.1 christos opt_help(verify_options); 116 1.1 christos BIO_printf(bio_err, "\nRecognized certificate chain purposes:\n"); 117 1.1 christos for (i = 0; i < X509_PURPOSE_get_count(); i++) { 118 1.1 christos X509_PURPOSE *ptmp = X509_PURPOSE_get0(i); 119 1.1 christos 120 1.1 christos BIO_printf(bio_err, " %-15s %s\n", 121 1.1.1.2 christos X509_PURPOSE_get0_sname(ptmp), 122 1.1.1.2 christos X509_PURPOSE_get0_name(ptmp)); 123 1.1 christos } 124 1.1 christos 125 1.1 christos BIO_printf(bio_err, "Recognized certificate policy names:\n"); 126 1.1 christos for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) { 127 1.1 christos const X509_VERIFY_PARAM *vptmp = X509_VERIFY_PARAM_get0(i); 128 1.1 christos 129 1.1 christos BIO_printf(bio_err, " %s\n", 130 1.1.1.2 christos X509_VERIFY_PARAM_get0_name(vptmp)); 131 1.1 christos } 132 1.1 christos ret = 0; 133 1.1 christos goto end; 134 1.1 christos case OPT_V_CASES: 135 1.1 christos if (!opt_verify(o, vpm)) 136 1.1 christos goto end; 137 1.1 christos vpmtouched++; 138 1.1 christos break; 139 1.1 christos case OPT_CAPATH: 140 1.1 christos CApath = opt_arg(); 141 1.1 christos break; 142 1.1 christos case OPT_CAFILE: 143 1.1 christos CAfile = opt_arg(); 144 1.1 christos break; 145 1.1 christos case OPT_CASTORE: 146 1.1 christos CAstore = opt_arg(); 147 1.1 christos break; 148 1.1 christos case OPT_NOCAPATH: 149 1.1 christos noCApath = 1; 150 1.1 christos break; 151 1.1 christos case OPT_NOCAFILE: 152 1.1 christos noCAfile = 1; 153 1.1 christos break; 154 1.1 christos case OPT_NOCASTORE: 155 1.1 christos noCAstore = 1; 156 1.1 christos break; 157 1.1 christos case OPT_UNTRUSTED: 158 1.1 christos /* Zero or more times */ 159 1.1 christos if (!load_certs(opt_arg(), 0, &untrusted, NULL, 160 1.1.1.2 christos "untrusted certificates")) 161 1.1 christos goto end; 162 1.1 christos break; 163 1.1 christos case OPT_TRUSTED: 164 1.1 christos /* Zero or more times */ 165 1.1 christos noCAfile = 1; 166 1.1 christos noCApath = 1; 167 1.1 christos noCAstore = 1; 168 1.1 christos if (!load_certs(opt_arg(), 0, &trusted, NULL, "trusted certificates")) 169 1.1 christos goto end; 170 1.1 christos break; 171 1.1 christos case OPT_CRLFILE: 172 1.1 christos /* Zero or more times */ 173 1.1 christos if (!load_crls(opt_arg(), &crls, NULL, "other CRLs")) 174 1.1 christos goto end; 175 1.1 christos break; 176 1.1 christos case OPT_CRL_DOWNLOAD: 177 1.1 christos crl_download = 1; 178 1.1 christos break; 179 1.1 christos case OPT_ENGINE: 180 1.1 christos if ((e = setup_engine(opt_arg(), 0)) == NULL) { 181 1.1 christos /* Failure message already displayed */ 182 1.1 christos goto end; 183 1.1 christos } 184 1.1 christos break; 185 1.1 christos case OPT_SHOW_CHAIN: 186 1.1 christos show_chain = 1; 187 1.1 christos break; 188 1.1 christos case OPT_NAMEOPT: 189 1.1 christos if (!set_nameopt(opt_arg())) 190 1.1 christos goto end; 191 1.1 christos break; 192 1.1 christos case OPT_VFYOPT: 193 1.1 christos if (!vfyopts) 194 1.1 christos vfyopts = sk_OPENSSL_STRING_new_null(); 195 1.1 christos if (!vfyopts || !sk_OPENSSL_STRING_push(vfyopts, opt_arg())) 196 1.1 christos goto opthelp; 197 1.1 christos break; 198 1.1 christos case OPT_VERBOSE: 199 1.1 christos v_verbose = 1; 200 1.1 christos break; 201 1.1 christos case OPT_PROV_CASES: 202 1.1 christos if (!opt_provider(o)) 203 1.1 christos goto end; 204 1.1 christos break; 205 1.1 christos } 206 1.1 christos } 207 1.1 christos 208 1.1 christos /* Extra arguments are certificates to verify. */ 209 1.1 christos argc = opt_num_rest(); 210 1.1 christos argv = opt_rest(); 211 1.1 christos 212 1.1 christos if (trusted != NULL 213 1.1 christos && (CAfile != NULL || CApath != NULL || CAstore != NULL)) { 214 1.1 christos BIO_printf(bio_err, 215 1.1.1.2 christos "%s: Cannot use -trusted with -CAfile, -CApath or -CAstore\n", 216 1.1.1.2 christos prog); 217 1.1 christos goto end; 218 1.1 christos } 219 1.1 christos 220 1.1 christos if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath, 221 1.1.1.2 christos CAstore, noCAstore)) 222 1.1.1.2 christos == NULL) 223 1.1 christos goto end; 224 1.1 christos X509_STORE_set_verify_cb(store, cb); 225 1.1 christos 226 1.1 christos if (vpmtouched) 227 1.1 christos X509_STORE_set1_param(store, vpm); 228 1.1 christos 229 1.1 christos ERR_clear_error(); 230 1.1 christos 231 1.1 christos if (crl_download) 232 1.1 christos store_setup_crl_download(store); 233 1.1 christos 234 1.1 christos ret = 0; 235 1.1 christos if (argc < 1) { 236 1.1 christos if (check(store, NULL, untrusted, trusted, crls, show_chain, 237 1.1.1.2 christos vfyopts) 238 1.1.1.2 christos != 1) 239 1.1 christos ret = -1; 240 1.1 christos } else { 241 1.1 christos for (i = 0; i < argc; i++) 242 1.1 christos if (check(store, argv[i], untrusted, trusted, crls, show_chain, 243 1.1.1.2 christos vfyopts) 244 1.1.1.2 christos != 1) 245 1.1 christos ret = -1; 246 1.1 christos } 247 1.1 christos 248 1.1.1.2 christos end: 249 1.1 christos X509_VERIFY_PARAM_free(vpm); 250 1.1 christos X509_STORE_free(store); 251 1.1 christos OSSL_STACK_OF_X509_free(untrusted); 252 1.1 christos OSSL_STACK_OF_X509_free(trusted); 253 1.1 christos sk_X509_CRL_pop_free(crls, X509_CRL_free); 254 1.1 christos sk_OPENSSL_STRING_free(vfyopts); 255 1.1 christos release_engine(e); 256 1.1 christos return (ret < 0 ? 2 : ret); 257 1.1 christos } 258 1.1 christos 259 1.1 christos static int check(X509_STORE *ctx, const char *file, 260 1.1.1.2 christos STACK_OF(X509) *uchain, STACK_OF(X509) *tchain, 261 1.1.1.2 christos STACK_OF(X509_CRL) *crls, int show_chain, 262 1.1.1.2 christos STACK_OF(OPENSSL_STRING) *opts) 263 1.1 christos { 264 1.1 christos X509 *x = NULL; 265 1.1 christos int i = 0, ret = 0; 266 1.1 christos X509_STORE_CTX *csc; 267 1.1 christos STACK_OF(X509) *chain = NULL; 268 1.1 christos int num_untrusted; 269 1.1 christos 270 1.1 christos x = load_cert(file, FORMAT_UNDEF, "certificate file"); 271 1.1 christos if (x == NULL) 272 1.1 christos goto end; 273 1.1 christos 274 1.1 christos if (opts != NULL) { 275 1.1 christos for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) { 276 1.1 christos char *opt = sk_OPENSSL_STRING_value(opts, i); 277 1.1 christos if (x509_ctrl_string(x, opt) <= 0) { 278 1.1 christos BIO_printf(bio_err, "parameter error \"%s\"\n", opt); 279 1.1 christos ERR_print_errors(bio_err); 280 1.1 christos X509_free(x); 281 1.1 christos return 0; 282 1.1 christos } 283 1.1 christos } 284 1.1 christos } 285 1.1 christos 286 1.1 christos csc = X509_STORE_CTX_new(); 287 1.1 christos if (csc == NULL) { 288 1.1 christos BIO_printf(bio_err, "error %s: X.509 store context allocation failed\n", 289 1.1.1.2 christos (file == NULL) ? "stdin" : file); 290 1.1 christos goto end; 291 1.1 christos } 292 1.1 christos 293 1.1 christos X509_STORE_set_flags(ctx, vflags); 294 1.1 christos if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) { 295 1.1 christos X509_STORE_CTX_free(csc); 296 1.1 christos BIO_printf(bio_err, 297 1.1.1.2 christos "error %s: X.509 store context initialization failed\n", 298 1.1.1.2 christos (file == NULL) ? "stdin" : file); 299 1.1 christos goto end; 300 1.1 christos } 301 1.1 christos if (tchain != NULL) 302 1.1 christos X509_STORE_CTX_set0_trusted_stack(csc, tchain); 303 1.1 christos if (crls != NULL) 304 1.1 christos X509_STORE_CTX_set0_crls(csc, crls); 305 1.1 christos i = X509_verify_cert(csc); 306 1.1 christos if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) { 307 1.1 christos BIO_printf(bio_out, "%s: OK\n", (file == NULL) ? "stdin" : file); 308 1.1 christos ret = 1; 309 1.1 christos if (show_chain) { 310 1.1 christos int j; 311 1.1 christos 312 1.1 christos chain = X509_STORE_CTX_get1_chain(csc); 313 1.1 christos num_untrusted = X509_STORE_CTX_get_num_untrusted(csc); 314 1.1 christos BIO_printf(bio_out, "Chain:\n"); 315 1.1 christos for (j = 0; j < sk_X509_num(chain); j++) { 316 1.1 christos X509 *cert = sk_X509_value(chain, j); 317 1.1 christos BIO_printf(bio_out, "depth=%d: ", j); 318 1.1 christos X509_NAME_print_ex_fp(stdout, 319 1.1.1.2 christos X509_get_subject_name(cert), 320 1.1.1.2 christos 0, get_nameopt()); 321 1.1 christos if (j < num_untrusted) 322 1.1 christos BIO_printf(bio_out, " (untrusted)"); 323 1.1 christos BIO_printf(bio_out, "\n"); 324 1.1 christos } 325 1.1 christos OSSL_STACK_OF_X509_free(chain); 326 1.1 christos } 327 1.1 christos } else { 328 1.1 christos BIO_printf(bio_err, 329 1.1.1.2 christos "error %s: verification failed\n", 330 1.1.1.2 christos (file == NULL) ? "stdin" : file); 331 1.1 christos } 332 1.1 christos X509_STORE_CTX_free(csc); 333 1.1 christos 334 1.1.1.2 christos end: 335 1.1 christos if (i <= 0) 336 1.1 christos ERR_print_errors(bio_err); 337 1.1 christos X509_free(x); 338 1.1 christos 339 1.1 christos return ret; 340 1.1 christos } 341 1.1 christos 342 1.1 christos static int cb(int ok, X509_STORE_CTX *ctx) 343 1.1 christos { 344 1.1 christos int cert_error = X509_STORE_CTX_get_error(ctx); 345 1.1 christos X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx); 346 1.1 christos 347 1.1 christos if (!ok) { 348 1.1 christos if (current_cert != NULL) { 349 1.1 christos X509_NAME_print_ex(bio_err, 350 1.1.1.2 christos X509_get_subject_name(current_cert), 351 1.1.1.2 christos 0, get_nameopt()); 352 1.1 christos BIO_printf(bio_err, "\n"); 353 1.1 christos } 354 1.1 christos BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n", 355 1.1.1.2 christos X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "", 356 1.1.1.2 christos cert_error, 357 1.1.1.2 christos X509_STORE_CTX_get_error_depth(ctx), 358 1.1.1.2 christos X509_verify_cert_error_string(cert_error)); 359 1.1 christos 360 1.1 christos /* 361 1.1 christos * Pretend that some errors are ok, so they don't stop further 362 1.1 christos * processing of the certificate chain. Setting ok = 1 does this. 363 1.1 christos * After X509_verify_cert() is done, we verify that there were 364 1.1 christos * no actual errors, even if the returned value was positive. 365 1.1 christos */ 366 1.1 christos switch (cert_error) { 367 1.1 christos case X509_V_ERR_NO_EXPLICIT_POLICY: 368 1.1 christos policies_print(ctx); 369 1.1 christos /* fall through */ 370 1.1 christos case X509_V_ERR_CERT_HAS_EXPIRED: 371 1.1 christos /* Continue even if the leaf is a self-signed cert */ 372 1.1 christos case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: 373 1.1 christos /* Continue after extension errors too */ 374 1.1 christos case X509_V_ERR_INVALID_CA: 375 1.1 christos case X509_V_ERR_INVALID_NON_CA: 376 1.1 christos case X509_V_ERR_PATH_LENGTH_EXCEEDED: 377 1.1 christos case X509_V_ERR_CRL_HAS_EXPIRED: 378 1.1 christos case X509_V_ERR_CRL_NOT_YET_VALID: 379 1.1 christos case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION: 380 1.1 christos /* errors due to strict conformance checking (-x509_strict) */ 381 1.1 christos case X509_V_ERR_INVALID_PURPOSE: 382 1.1 christos case X509_V_ERR_PATHLEN_INVALID_FOR_NON_CA: 383 1.1 christos case X509_V_ERR_PATHLEN_WITHOUT_KU_KEY_CERT_SIGN: 384 1.1 christos case X509_V_ERR_CA_BCONS_NOT_CRITICAL: 385 1.1 christos case X509_V_ERR_CA_CERT_MISSING_KEY_USAGE: 386 1.1 christos case X509_V_ERR_KU_KEY_CERT_SIGN_INVALID_FOR_NON_CA: 387 1.1 christos case X509_V_ERR_ISSUER_NAME_EMPTY: 388 1.1 christos case X509_V_ERR_SUBJECT_NAME_EMPTY: 389 1.1 christos case X509_V_ERR_EMPTY_SUBJECT_SAN_NOT_CRITICAL: 390 1.1 christos case X509_V_ERR_EMPTY_SUBJECT_ALT_NAME: 391 1.1 christos case X509_V_ERR_SIGNATURE_ALGORITHM_INCONSISTENCY: 392 1.1 christos case X509_V_ERR_AUTHORITY_KEY_IDENTIFIER_CRITICAL: 393 1.1 christos case X509_V_ERR_SUBJECT_KEY_IDENTIFIER_CRITICAL: 394 1.1 christos case X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER: 395 1.1 christos case X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER: 396 1.1 christos case X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3: 397 1.1 christos ok = 1; 398 1.1 christos } 399 1.1 christos return ok; 400 1.1 christos } 401 1.1 christos if (cert_error == X509_V_OK && ok == 2) 402 1.1 christos policies_print(ctx); 403 1.1 christos if (!v_verbose) 404 1.1 christos ERR_clear_error(); 405 1.1 christos return ok; 406 1.1 christos } 407