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