]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/verify.c
Add "sections" to -help output
[thirdparty/openssl.git] / apps / verify.c
CommitLineData
846e33c7 1/*
6738bf14 2 * Copyright 1995-2018 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 21static int cb(int ok, X509_STORE_CTX *ctx);
cc696296 22static 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,
25 unsigned char *sm2id, size_t sm2idlen);
0f113f3e 26static int v_verbose = 0, vflags = 0;
d02b48c6 27
7e1b7485
RS
28typedef 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,
bd91e3c8 33 OPT_V_ENUM, OPT_NAMEOPT,
8267becb 34 OPT_VERBOSE, OPT_SM2ID, OPT_SM2HEXID
7e1b7485
RS
35} OPTION_CHOICE;
36
44c83ebd 37const OPTIONS verify_options[] = {
7e1b7485 38 {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
5388f986
RS
39
40 OPT_SECTION("General"),
7e1b7485 41 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
42#ifndef OPENSSL_NO_ENGINE
43 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
44#endif
a64ba70d
MC
45 {"verbose", OPT_VERBOSE, '-',
46 "Print extra information about the operations being performed."},
5388f986
RS
47 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
48
49 OPT_SECTION("Certificate chain"),
a64ba70d
MC
50 {"CApath", OPT_CAPATH, '/', "A directory of trusted certificates"},
51 {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"},
fd3397fc 52 {"CAstore", OPT_CASTORE, ':', "URI to a store of trusted certificates"},
2b6bcb70
MC
53 {"no-CAfile", OPT_NOCAFILE, '-',
54 "Do not load the default certificates file"},
55 {"no-CApath", OPT_NOCAPATH, '-',
56 "Do not load certificates from the default certificates directory"},
fd3397fc
RL
57 {"no-CAstore", OPT_NOCAPATH, '-',
58 "Do not load certificates from the default certificates store"},
a64ba70d 59 {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
5b89036c 60 {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
a64ba70d
MC
61 {"CRLfile", OPT_CRLFILE, '<',
62 "File containing one or more CRL's (in PEM format) to load"},
63 {"crl_download", OPT_CRL_DOWNLOAD, '-',
64 "Attempt to download CRL information for this certificate"},
65 {"show_chain", OPT_SHOW_CHAIN, '-',
66 "Display information about the certificate chain"},
5388f986 67
9c3bcfa0 68 OPT_V_OPTIONS,
8267becb 69#ifndef OPENSSL_NO_SM2
70 {"sm2-id", OPT_SM2ID, 's',
71 "Specify an ID string to verify an SM2 certificate"},
72 {"sm2-hex-id", OPT_SM2HEXID, 's',
73 "Specify a hex ID string to verify an SM2 certificate"},
7e1b7485 74#endif
7e1b7485
RS
75 {NULL}
76};
667ac4ec 77
7e1b7485 78int verify_main(int argc, char **argv)
0f113f3e 79{
dd1abd44 80 ENGINE *e = NULL;
0f113f3e
MC
81 STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
82 STACK_OF(X509_CRL) *crls = NULL;
7e1b7485 83 X509_STORE *store = NULL;
0f113f3e 84 X509_VERIFY_PARAM *vpm = NULL;
fd3397fc
RL
85 const char *prog, *CApath = NULL, *CAfile = NULL, *CAstore = NULL;
86 int noCApath = 0, noCAfile = 0, noCAstore = 0;
7e1b7485
RS
87 int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
88 OPTION_CHOICE o;
8267becb 89 unsigned char *sm2_id = NULL;
90 size_t sm2_idlen = 0;
875c9a9a 91 int sm2_free = 0;
d02b48c6 92
7e1b7485 93 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
0f113f3e 94 goto end;
0f113f3e 95
7e1b7485
RS
96 prog = opt_init(argc, argv, verify_options);
97 while ((o = opt_next()) != OPT_EOF) {
98 switch (o) {
99 case OPT_EOF:
100 case OPT_ERR:
101 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
102 goto end;
103 case OPT_HELP:
104 opt_help(verify_options);
105 BIO_printf(bio_err, "Recognized usages:\n");
106 for (i = 0; i < X509_PURPOSE_get_count(); i++) {
107 X509_PURPOSE *ptmp;
108 ptmp = X509_PURPOSE_get0(i);
109 BIO_printf(bio_err, "\t%-10s\t%s\n",
110 X509_PURPOSE_get0_sname(ptmp),
111 X509_PURPOSE_get0_name(ptmp));
112 }
0f113f3e 113
7e1b7485
RS
114 BIO_printf(bio_err, "Recognized verify names:\n");
115 for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
116 const X509_VERIFY_PARAM *vptmp;
117 vptmp = X509_VERIFY_PARAM_get0(i);
118 BIO_printf(bio_err, "\t%-10s\n",
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 */
a773b52a 148 if (!load_certs(opt_arg(), &untrusted, FORMAT_PEM, 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;
a773b52a 157 if (!load_certs(opt_arg(), &trusted, FORMAT_PEM, NULL,
feb2f53e
VD
158 "trusted certificates"))
159 goto end;
7e1b7485
RS
160 break;
161 case OPT_CRLFILE:
feb2f53e 162 /* Zero or more times */
a773b52a 163 if (!load_crls(opt_arg(), &crls, FORMAT_PEM, NULL,
feb2f53e
VD
164 "other CRLs"))
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;
7e1b7485
RS
183 case OPT_VERBOSE:
184 v_verbose = 1;
185 break;
8267becb 186 case OPT_SM2ID:
ad14e8e5
SL
187 if (sm2_id != NULL) {
188 BIO_printf(bio_err,
189 "Use one of the options 'sm2-hex-id' or 'sm2-id' \n");
190 goto end;
191 }
8267becb 192 sm2_id = (unsigned char *)opt_arg();
193 sm2_idlen = strlen((const char *)sm2_id);
194 break;
195 case OPT_SM2HEXID:
ad14e8e5
SL
196 if (sm2_id != NULL) {
197 BIO_printf(bio_err,
198 "Use one of the options 'sm2-hex-id' or 'sm2-id' \n");
199 goto end;
200 }
8267becb 201 /* try to parse the input as hex string first */
875c9a9a 202 sm2_free = 1;
8267becb 203 sm2_id = OPENSSL_hexstr2buf(opt_arg(), (long *)&sm2_idlen);
204 if (sm2_id == NULL) {
205 BIO_printf(bio_err, "Invalid hex string input\n");
206 goto end;
207 }
208 break;
7e1b7485 209 }
0f113f3e 210 }
7e1b7485
RS
211 argc = opt_num_rest();
212 argv = opt_rest();
fd3397fc
RL
213 if (trusted != NULL
214 && (CAfile != NULL || CApath != NULL || CAstore != NULL)) {
5b89036c 215 BIO_printf(bio_err,
fd3397fc 216 "%s: Cannot use -trusted with -CAfile, -CApath or -CAstore\n",
5b89036c
RS
217 prog);
218 goto end;
219 }
d02b48c6 220
fd3397fc
RL
221 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
222 CAstore, noCAstore)) == NULL)
7e1b7485
RS
223 goto end;
224 X509_STORE_set_verify_cb(store, cb);
5270e702 225
7e1b7485
RS
226 if (vpmtouched)
227 X509_STORE_set1_param(store, vpm);
0f113f3e
MC
228
229 ERR_clear_error();
230
0f113f3e 231 if (crl_download)
7e1b7485 232 store_setup_crl_download(store);
0f113f3e
MC
233
234 ret = 0;
235 if (argc < 1) {
8267becb 236 if (check(store, NULL, untrusted, trusted, crls, show_chain,
237 sm2_id, sm2_idlen) != 1)
0f113f3e
MC
238 ret = -1;
239 } else {
240 for (i = 0; i < argc; i++)
a773b52a 241 if (check(store, argv[i], untrusted, trusted, crls,
8267becb 242 show_chain, sm2_id, sm2_idlen) != 1)
0f113f3e
MC
243 ret = -1;
244 }
245
246 end:
875c9a9a
PY
247 if (sm2_free)
248 OPENSSL_free(sm2_id);
222561fe
RS
249 X509_VERIFY_PARAM_free(vpm);
250 X509_STORE_free(store);
0f113f3e
MC
251 sk_X509_pop_free(untrusted, X509_free);
252 sk_X509_pop_free(trusted, X509_free);
253 sk_X509_CRL_pop_free(crls, X509_CRL_free);
dd1abd44 254 release_engine(e);
7e1b7485 255 return (ret < 0 ? 2 : ret);
0f113f3e 256}
d02b48c6 257
cc696296 258static int check(X509_STORE *ctx, const char *file,
0f113f3e 259 STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
8267becb 260 STACK_OF(X509_CRL) *crls, int show_chain,
261 unsigned char *sm2id, size_t sm2idlen)
0f113f3e
MC
262{
263 X509 *x = NULL;
264 int i = 0, ret = 0;
265 X509_STORE_CTX *csc;
266 STACK_OF(X509) *chain = NULL;
7f3f41d8 267 int num_untrusted;
0f113f3e 268
a773b52a 269 x = load_cert(file, FORMAT_PEM, "certificate file");
0f113f3e
MC
270 if (x == NULL)
271 goto end;
0f113f3e 272
8267becb 273 if (sm2id != NULL) {
274#ifndef OPENSSL_NO_SM2
ccf45361 275 ASN1_OCTET_STRING *v;
8267becb 276
ccf45361
PY
277 v = ASN1_OCTET_STRING_new();
278 if (v == NULL) {
279 BIO_printf(bio_err, "error: SM2 ID allocation failed\n");
280 goto end;
281 }
8267becb 282
ccf45361
PY
283 if (!ASN1_OCTET_STRING_set(v, sm2id, sm2idlen)) {
284 BIO_printf(bio_err, "error: setting SM2 ID failed\n");
285 ASN1_OCTET_STRING_free(v);
286 goto end;
287 }
288
289 X509_set0_sm2_id(x, v);
8267becb 290#endif
291 }
292
0f113f3e
MC
293 csc = X509_STORE_CTX_new();
294 if (csc == NULL) {
ccf45361
PY
295 BIO_printf(bio_err, "error %s: X.509 store context allocation failed\n",
296 (file == NULL) ? "stdin" : file);
0f113f3e
MC
297 goto end;
298 }
a392ef20 299
0f113f3e
MC
300 X509_STORE_set_flags(ctx, vflags);
301 if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
aebd0e5c 302 X509_STORE_CTX_free(csc);
ccf45361
PY
303 BIO_printf(bio_err,
304 "error %s: X.509 store context initialization failed\n",
305 (file == NULL) ? "stdin" : file);
0f113f3e
MC
306 goto end;
307 }
2234212c 308 if (tchain != NULL)
f0e0fd51 309 X509_STORE_CTX_set0_trusted_stack(csc, tchain);
2234212c 310 if (crls != NULL)
0f113f3e
MC
311 X509_STORE_CTX_set0_crls(csc, crls);
312 i = X509_verify_cert(csc);
d9e309a6 313 if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
ccf45361 314 BIO_printf(bio_out, "%s: OK\n", (file == NULL) ? "stdin" : file);
480405e4 315 ret = 1;
63c6aa6b
VD
316 if (show_chain) {
317 int j;
bb484020 318
63c6aa6b
VD
319 chain = X509_STORE_CTX_get1_chain(csc);
320 num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
ccf45361 321 BIO_printf(bio_out, "Chain:\n");
63c6aa6b
VD
322 for (j = 0; j < sk_X509_num(chain); j++) {
323 X509 *cert = sk_X509_value(chain, j);
ccf45361 324 BIO_printf(bio_out, "depth=%d: ", j);
63c6aa6b
VD
325 X509_NAME_print_ex_fp(stdout,
326 X509_get_subject_name(cert),
b5c4209b 327 0, get_nameopt());
63c6aa6b 328 if (j < num_untrusted)
ccf45361
PY
329 BIO_printf(bio_out, " (untrusted)");
330 BIO_printf(bio_out, "\n");
63c6aa6b
VD
331 }
332 sk_X509_pop_free(chain, X509_free);
333 }
334 } else {
ccf45361
PY
335 BIO_printf(bio_err,
336 "error %s: verification failed\n",
337 (file == NULL) ? "stdin" : file);
7f3f41d8 338 }
0f113f3e
MC
339 X509_STORE_CTX_free(csc);
340
0f113f3e 341 end:
480405e4 342 if (i <= 0)
63c6aa6b 343 ERR_print_errors(bio_err);
222561fe 344 X509_free(x);
0f113f3e 345
480405e4 346 return ret;
0f113f3e 347}
d02b48c6 348
6d23cf97 349static int cb(int ok, X509_STORE_CTX *ctx)
0f113f3e
MC
350{
351 int cert_error = X509_STORE_CTX_get_error(ctx);
352 X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
353
354 if (!ok) {
2234212c 355 if (current_cert != NULL) {
ecf3a1fb
RS
356 X509_NAME_print_ex(bio_err,
357 X509_get_subject_name(current_cert),
b5c4209b 358 0, get_nameopt());
ecf3a1fb 359 BIO_printf(bio_err, "\n");
0f113f3e 360 }
63c6aa6b
VD
361 BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
362 X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
0f113f3e
MC
363 cert_error,
364 X509_STORE_CTX_get_error_depth(ctx),
365 X509_verify_cert_error_string(cert_error));
9b10986d
RL
366
367 /*
368 * Pretend that some errors are ok, so they don't stop further
369 * processing of the certificate chain. Setting ok = 1 does this.
370 * After X509_verify_cert() is done, we verify that there were
371 * no actual errors, even if the returned value was positive.
372 */
0f113f3e
MC
373 switch (cert_error) {
374 case X509_V_ERR_NO_EXPLICIT_POLICY:
ecf3a1fb 375 policies_print(ctx);
018fcbec 376 /* fall thru */
0f113f3e 377 case X509_V_ERR_CERT_HAS_EXPIRED:
9b10986d 378 /* Continue even if the leaf is a self signed cert */
0f113f3e
MC
379 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
380 /* Continue after extension errors too */
381 case X509_V_ERR_INVALID_CA:
382 case X509_V_ERR_INVALID_NON_CA:
383 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
384 case X509_V_ERR_INVALID_PURPOSE:
385 case X509_V_ERR_CRL_HAS_EXPIRED:
386 case X509_V_ERR_CRL_NOT_YET_VALID:
387 case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
388 ok = 1;
0f113f3e
MC
389 }
390
391 return ok;
392
393 }
394 if (cert_error == X509_V_OK && ok == 2)
ecf3a1fb 395 policies_print(ctx);
0f113f3e
MC
396 if (!v_verbose)
397 ERR_clear_error();
26a7d938 398 return ok;
0f113f3e 399}