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