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