]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
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" | |
ec577822 BM |
14 | #include <openssl/bio.h> |
15 | #include <openssl/err.h> | |
16 | #include <openssl/x509.h> | |
d4cec6a1 | 17 | #include <openssl/x509v3.h> |
ec577822 | 18 | #include <openssl/pem.h> |
d02b48c6 | 19 | |
6d23cf97 | 20 | static int cb(int ok, X509_STORE_CTX *ctx); |
cc696296 | 21 | static int check(X509_STORE *ctx, const char *file, |
0f113f3e | 22 | STACK_OF(X509) *uchain, STACK_OF(X509) *tchain, |
a773b52a | 23 | STACK_OF(X509_CRL) *crls, int show_chain); |
0f113f3e | 24 | static int v_verbose = 0, vflags = 0; |
d02b48c6 | 25 | |
7e1b7485 RS |
26 | typedef enum OPTION_choice { |
27 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
2b6bcb70 MC |
28 | OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE, |
29 | OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN, | |
7e1b7485 RS |
30 | OPT_V_ENUM, |
31 | OPT_VERBOSE | |
32 | } OPTION_CHOICE; | |
33 | ||
34 | OPTIONS verify_options[] = { | |
35 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"}, | |
36 | {OPT_HELP_STR, 1, '-', "Valid options are:\n"}, | |
37 | {"help", OPT_HELP, '-', "Display this summary"}, | |
a64ba70d MC |
38 | {"verbose", OPT_VERBOSE, '-', |
39 | "Print extra information about the operations being performed."}, | |
40 | {"CApath", OPT_CAPATH, '/', "A directory of trusted certificates"}, | |
41 | {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"}, | |
2b6bcb70 MC |
42 | {"no-CAfile", OPT_NOCAFILE, '-', |
43 | "Do not load the default certificates file"}, | |
44 | {"no-CApath", OPT_NOCAPATH, '-', | |
45 | "Do not load certificates from the default certificates directory"}, | |
a64ba70d | 46 | {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"}, |
5b89036c | 47 | {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"}, |
a64ba70d MC |
48 | {"CRLfile", OPT_CRLFILE, '<', |
49 | "File containing one or more CRL's (in PEM format) to load"}, | |
50 | {"crl_download", OPT_CRL_DOWNLOAD, '-', | |
51 | "Attempt to download CRL information for this certificate"}, | |
52 | {"show_chain", OPT_SHOW_CHAIN, '-', | |
53 | "Display information about the certificate chain"}, | |
9c3bcfa0 | 54 | OPT_V_OPTIONS, |
7e1b7485 RS |
55 | #ifndef OPENSSL_NO_ENGINE |
56 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
57 | #endif | |
7e1b7485 RS |
58 | {NULL} |
59 | }; | |
667ac4ec | 60 | |
7e1b7485 | 61 | int verify_main(int argc, char **argv) |
0f113f3e | 62 | { |
0f113f3e MC |
63 | STACK_OF(X509) *untrusted = NULL, *trusted = NULL; |
64 | STACK_OF(X509_CRL) *crls = NULL; | |
7e1b7485 | 65 | X509_STORE *store = NULL; |
0f113f3e | 66 | X509_VERIFY_PARAM *vpm = NULL; |
cc696296 | 67 | const char *prog, *CApath = NULL, *CAfile = NULL; |
2b6bcb70 | 68 | int noCApath = 0, noCAfile = 0; |
7e1b7485 RS |
69 | int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1; |
70 | OPTION_CHOICE o; | |
d02b48c6 | 71 | |
7e1b7485 | 72 | if ((vpm = X509_VERIFY_PARAM_new()) == NULL) |
0f113f3e | 73 | goto end; |
0f113f3e | 74 | |
7e1b7485 RS |
75 | prog = opt_init(argc, argv, verify_options); |
76 | while ((o = opt_next()) != OPT_EOF) { | |
77 | switch (o) { | |
78 | case OPT_EOF: | |
79 | case OPT_ERR: | |
80 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
81 | goto end; | |
82 | case OPT_HELP: | |
83 | opt_help(verify_options); | |
84 | BIO_printf(bio_err, "Recognized usages:\n"); | |
85 | for (i = 0; i < X509_PURPOSE_get_count(); i++) { | |
86 | X509_PURPOSE *ptmp; | |
87 | ptmp = X509_PURPOSE_get0(i); | |
88 | BIO_printf(bio_err, "\t%-10s\t%s\n", | |
89 | X509_PURPOSE_get0_sname(ptmp), | |
90 | X509_PURPOSE_get0_name(ptmp)); | |
91 | } | |
0f113f3e | 92 | |
7e1b7485 RS |
93 | BIO_printf(bio_err, "Recognized verify names:\n"); |
94 | for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) { | |
95 | const X509_VERIFY_PARAM *vptmp; | |
96 | vptmp = X509_VERIFY_PARAM_get0(i); | |
97 | BIO_printf(bio_err, "\t%-10s\n", | |
98 | X509_VERIFY_PARAM_get0_name(vptmp)); | |
0f113f3e | 99 | } |
7e1b7485 RS |
100 | ret = 0; |
101 | goto end; | |
102 | case OPT_V_CASES: | |
103 | if (!opt_verify(o, vpm)) | |
0f113f3e | 104 | goto end; |
7e1b7485 RS |
105 | vpmtouched++; |
106 | break; | |
107 | case OPT_CAPATH: | |
108 | CApath = opt_arg(); | |
109 | break; | |
110 | case OPT_CAFILE: | |
111 | CAfile = opt_arg(); | |
112 | break; | |
2b6bcb70 MC |
113 | case OPT_NOCAPATH: |
114 | noCApath = 1; | |
115 | break; | |
116 | case OPT_NOCAFILE: | |
117 | noCAfile = 1; | |
118 | break; | |
7e1b7485 | 119 | case OPT_UNTRUSTED: |
feb2f53e | 120 | /* Zero or more times */ |
a773b52a | 121 | if (!load_certs(opt_arg(), &untrusted, FORMAT_PEM, NULL, |
feb2f53e VD |
122 | "untrusted certificates")) |
123 | goto end; | |
7e1b7485 RS |
124 | break; |
125 | case OPT_TRUSTED: | |
feb2f53e VD |
126 | /* Zero or more times */ |
127 | noCAfile = 1; | |
128 | noCApath = 1; | |
a773b52a | 129 | if (!load_certs(opt_arg(), &trusted, FORMAT_PEM, NULL, |
feb2f53e VD |
130 | "trusted certificates")) |
131 | goto end; | |
7e1b7485 RS |
132 | break; |
133 | case OPT_CRLFILE: | |
feb2f53e | 134 | /* Zero or more times */ |
a773b52a | 135 | if (!load_crls(opt_arg(), &crls, FORMAT_PEM, NULL, |
feb2f53e VD |
136 | "other CRLs")) |
137 | goto end; | |
7e1b7485 RS |
138 | break; |
139 | case OPT_CRL_DOWNLOAD: | |
140 | crl_download = 1; | |
0f113f3e | 141 | break; |
a773b52a RS |
142 | case OPT_ENGINE: |
143 | if (setup_engine(opt_arg(), 0) == NULL) { | |
144 | /* Failure message already displayed */ | |
145 | goto end; | |
146 | } | |
147 | break; | |
7e1b7485 RS |
148 | case OPT_SHOW_CHAIN: |
149 | show_chain = 1; | |
150 | break; | |
7e1b7485 RS |
151 | case OPT_VERBOSE: |
152 | v_verbose = 1; | |
153 | break; | |
154 | } | |
0f113f3e | 155 | } |
7e1b7485 RS |
156 | argc = opt_num_rest(); |
157 | argv = opt_rest(); | |
feb2f53e | 158 | if (trusted != NULL && (CAfile || CApath)) { |
5b89036c RS |
159 | BIO_printf(bio_err, |
160 | "%s: Cannot use -trusted with -CAfile or -CApath\n", | |
161 | prog); | |
162 | goto end; | |
163 | } | |
d02b48c6 | 164 | |
2b6bcb70 | 165 | if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL) |
7e1b7485 RS |
166 | goto end; |
167 | X509_STORE_set_verify_cb(store, cb); | |
5270e702 | 168 | |
7e1b7485 RS |
169 | if (vpmtouched) |
170 | X509_STORE_set1_param(store, vpm); | |
0f113f3e MC |
171 | |
172 | ERR_clear_error(); | |
173 | ||
0f113f3e | 174 | if (crl_download) |
7e1b7485 | 175 | store_setup_crl_download(store); |
0f113f3e MC |
176 | |
177 | ret = 0; | |
178 | if (argc < 1) { | |
a773b52a | 179 | if (check(store, NULL, untrusted, trusted, crls, show_chain) != 1) |
0f113f3e MC |
180 | ret = -1; |
181 | } else { | |
182 | for (i = 0; i < argc; i++) | |
a773b52a | 183 | if (check(store, argv[i], untrusted, trusted, crls, |
7e1b7485 | 184 | show_chain) != 1) |
0f113f3e MC |
185 | ret = -1; |
186 | } | |
187 | ||
188 | end: | |
222561fe RS |
189 | X509_VERIFY_PARAM_free(vpm); |
190 | X509_STORE_free(store); | |
0f113f3e MC |
191 | sk_X509_pop_free(untrusted, X509_free); |
192 | sk_X509_pop_free(trusted, X509_free); | |
193 | sk_X509_CRL_pop_free(crls, X509_CRL_free); | |
7e1b7485 | 194 | return (ret < 0 ? 2 : ret); |
0f113f3e | 195 | } |
d02b48c6 | 196 | |
cc696296 | 197 | static int check(X509_STORE *ctx, const char *file, |
0f113f3e | 198 | STACK_OF(X509) *uchain, STACK_OF(X509) *tchain, |
a773b52a | 199 | STACK_OF(X509_CRL) *crls, int show_chain) |
0f113f3e MC |
200 | { |
201 | X509 *x = NULL; | |
202 | int i = 0, ret = 0; | |
203 | X509_STORE_CTX *csc; | |
204 | STACK_OF(X509) *chain = NULL; | |
7f3f41d8 | 205 | int num_untrusted; |
0f113f3e | 206 | |
a773b52a | 207 | x = load_cert(file, FORMAT_PEM, "certificate file"); |
0f113f3e MC |
208 | if (x == NULL) |
209 | goto end; | |
0f113f3e MC |
210 | |
211 | csc = X509_STORE_CTX_new(); | |
212 | if (csc == NULL) { | |
63c6aa6b VD |
213 | printf("error %s: X.509 store context allocation failed\n", |
214 | (file == NULL) ? "stdin" : file); | |
0f113f3e MC |
215 | goto end; |
216 | } | |
a392ef20 | 217 | |
0f113f3e MC |
218 | X509_STORE_set_flags(ctx, vflags); |
219 | if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) { | |
63c6aa6b VD |
220 | printf("error %s: X.509 store context initialization failed\n", |
221 | (file == NULL) ? "stdin" : file); | |
0f113f3e MC |
222 | goto end; |
223 | } | |
224 | if (tchain) | |
f0e0fd51 | 225 | X509_STORE_CTX_set0_trusted_stack(csc, tchain); |
0f113f3e MC |
226 | if (crls) |
227 | X509_STORE_CTX_set0_crls(csc, crls); | |
228 | i = X509_verify_cert(csc); | |
d9e309a6 | 229 | if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) { |
63c6aa6b | 230 | printf("%s: OK\n", (file == NULL) ? "stdin" : file); |
480405e4 | 231 | ret = 1; |
63c6aa6b VD |
232 | if (show_chain) { |
233 | int j; | |
bb484020 | 234 | |
63c6aa6b VD |
235 | chain = X509_STORE_CTX_get1_chain(csc); |
236 | num_untrusted = X509_STORE_CTX_get_num_untrusted(csc); | |
237 | printf("Chain:\n"); | |
238 | for (j = 0; j < sk_X509_num(chain); j++) { | |
239 | X509 *cert = sk_X509_value(chain, j); | |
240 | printf("depth=%d: ", j); | |
241 | X509_NAME_print_ex_fp(stdout, | |
242 | X509_get_subject_name(cert), | |
243 | 0, XN_FLAG_ONELINE); | |
244 | if (j < num_untrusted) | |
245 | printf(" (untrusted)"); | |
246 | printf("\n"); | |
247 | } | |
248 | sk_X509_pop_free(chain, X509_free); | |
249 | } | |
250 | } else { | |
251 | printf("error %s: verification failed\n", (file == NULL) ? "stdin" : file); | |
7f3f41d8 | 252 | } |
0f113f3e MC |
253 | X509_STORE_CTX_free(csc); |
254 | ||
0f113f3e | 255 | end: |
480405e4 | 256 | if (i <= 0) |
63c6aa6b | 257 | ERR_print_errors(bio_err); |
222561fe | 258 | X509_free(x); |
0f113f3e | 259 | |
480405e4 | 260 | return ret; |
0f113f3e | 261 | } |
d02b48c6 | 262 | |
6d23cf97 | 263 | static int cb(int ok, X509_STORE_CTX *ctx) |
0f113f3e MC |
264 | { |
265 | int cert_error = X509_STORE_CTX_get_error(ctx); | |
266 | X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx); | |
267 | ||
268 | if (!ok) { | |
269 | if (current_cert) { | |
ecf3a1fb RS |
270 | X509_NAME_print_ex(bio_err, |
271 | X509_get_subject_name(current_cert), | |
272 | 0, XN_FLAG_ONELINE); | |
273 | BIO_printf(bio_err, "\n"); | |
0f113f3e | 274 | } |
63c6aa6b VD |
275 | BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n", |
276 | X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "", | |
0f113f3e MC |
277 | cert_error, |
278 | X509_STORE_CTX_get_error_depth(ctx), | |
279 | X509_verify_cert_error_string(cert_error)); | |
280 | switch (cert_error) { | |
281 | case X509_V_ERR_NO_EXPLICIT_POLICY: | |
ecf3a1fb | 282 | policies_print(ctx); |
0f113f3e MC |
283 | case X509_V_ERR_CERT_HAS_EXPIRED: |
284 | ||
285 | /* | |
286 | * since we are just checking the certificates, it is ok if they | |
287 | * are self signed. But we should still warn the user. | |
288 | */ | |
0f113f3e MC |
289 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
290 | /* Continue after extension errors too */ | |
291 | case X509_V_ERR_INVALID_CA: | |
292 | case X509_V_ERR_INVALID_NON_CA: | |
293 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: | |
294 | case X509_V_ERR_INVALID_PURPOSE: | |
295 | case X509_V_ERR_CRL_HAS_EXPIRED: | |
296 | case X509_V_ERR_CRL_NOT_YET_VALID: | |
297 | case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION: | |
298 | ok = 1; | |
0f113f3e MC |
299 | } |
300 | ||
301 | return ok; | |
302 | ||
303 | } | |
304 | if (cert_error == X509_V_OK && ok == 2) | |
ecf3a1fb | 305 | policies_print(ctx); |
0f113f3e MC |
306 | if (!v_verbose) |
307 | ERR_clear_error(); | |
308 | return (ok); | |
309 | } |