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