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