]>
Commit | Line | Data |
---|---|---|
58964a49 | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
2 | * All rights reserved. |
3 | * | |
4 | * This package is an SSL implementation written | |
5 | * by Eric Young (eay@cryptsoft.com). | |
6 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 7 | * |
d02b48c6 RE |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions | |
10 | * apply to all code found in this distribution, be it the RC4, RSA, | |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
12 | * included with this distribution is covered by the same copyright terms | |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 14 | * |
d02b48c6 RE |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. | |
17 | * If this package is used in a product, Eric Young should be given attribution | |
18 | * as the author of the parts of the library used. | |
19 | * This can be in the form of a textual message at program startup or | |
20 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 21 | * |
d02b48c6 RE |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions | |
24 | * are met: | |
25 | * 1. Redistributions of source code must retain the copyright | |
26 | * notice, this list of conditions and the following disclaimer. | |
27 | * 2. Redistributions in binary form must reproduce the above copyright | |
28 | * notice, this list of conditions and the following disclaimer in the | |
29 | * documentation and/or other materials provided with the distribution. | |
30 | * 3. All advertising materials mentioning features or use of this software | |
31 | * must display the following acknowledgement: | |
32 | * "This product includes cryptographic software written by | |
33 | * Eric Young (eay@cryptsoft.com)" | |
34 | * The word 'cryptographic' can be left out if the rouines from the library | |
35 | * being used are not cryptographic related :-). | |
0f113f3e | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 39 | * |
d02b48c6 RE |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
50 | * SUCH DAMAGE. | |
0f113f3e | 51 | * |
d02b48c6 RE |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
54 | * copied and put under another distribution licence | |
55 | * [including the GNU Public Licence.] | |
56 | */ | |
57 | ||
58 | #include <stdio.h> | |
59 | #include <stdlib.h> | |
60 | #include <string.h> | |
61 | #include "apps.h" | |
ec577822 BM |
62 | #include <openssl/bio.h> |
63 | #include <openssl/err.h> | |
64 | #include <openssl/x509.h> | |
d4cec6a1 | 65 | #include <openssl/x509v3.h> |
ec577822 | 66 | #include <openssl/pem.h> |
d02b48c6 | 67 | |
6d23cf97 | 68 | static int cb(int ok, X509_STORE_CTX *ctx); |
245d2ee3 | 69 | static int check(X509_STORE *ctx, char *file, |
0f113f3e MC |
70 | STACK_OF(X509) *uchain, STACK_OF(X509) *tchain, |
71 | STACK_OF(X509_CRL) *crls, ENGINE *e, int show_chain); | |
72 | static int v_verbose = 0, vflags = 0; | |
d02b48c6 | 73 | |
7e1b7485 RS |
74 | typedef enum OPTION_choice { |
75 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
76 | OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_UNTRUSTED, OPT_TRUSTED, | |
77 | OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN, | |
78 | OPT_V_ENUM, | |
79 | OPT_VERBOSE | |
80 | } OPTION_CHOICE; | |
81 | ||
82 | OPTIONS verify_options[] = { | |
83 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"}, | |
84 | {OPT_HELP_STR, 1, '-', "Valid options are:\n"}, | |
85 | {"help", OPT_HELP, '-', "Display this summary"}, | |
86 | {"verbose", OPT_VERBOSE, '-'}, | |
87 | {"CApath", OPT_CAPATH, '/'}, | |
88 | {"CAfile", OPT_CAFILE, '<'}, | |
89 | {"untrusted", OPT_UNTRUSTED, '<'}, | |
90 | {"trusted", OPT_TRUSTED, '<'}, | |
91 | {"CRLfile", OPT_CRLFILE, '<'}, | |
92 | {"crl_download", OPT_CRL_DOWNLOAD, '-'}, | |
93 | {"show_chain", OPT_SHOW_CHAIN, '-'}, | |
94 | #ifndef OPENSSL_NO_ENGINE | |
95 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
96 | #endif | |
97 | OPT_V_OPTIONS, | |
98 | {NULL} | |
99 | }; | |
667ac4ec | 100 | |
7e1b7485 | 101 | int verify_main(int argc, char **argv) |
0f113f3e MC |
102 | { |
103 | ENGINE *e = NULL; | |
0f113f3e MC |
104 | STACK_OF(X509) *untrusted = NULL, *trusted = NULL; |
105 | STACK_OF(X509_CRL) *crls = NULL; | |
7e1b7485 | 106 | X509_STORE *store = NULL; |
0f113f3e | 107 | X509_VERIFY_PARAM *vpm = NULL; |
333b070e | 108 | char *prog, *CApath = NULL, *CAfile = NULL; |
7e1b7485 RS |
109 | char *untfile = NULL, *trustfile = NULL, *crlfile = NULL; |
110 | int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1; | |
111 | OPTION_CHOICE o; | |
d02b48c6 | 112 | |
7e1b7485 | 113 | if ((vpm = X509_VERIFY_PARAM_new()) == NULL) |
0f113f3e | 114 | goto end; |
0f113f3e | 115 | |
7e1b7485 RS |
116 | prog = opt_init(argc, argv, verify_options); |
117 | while ((o = opt_next()) != OPT_EOF) { | |
118 | switch (o) { | |
119 | case OPT_EOF: | |
120 | case OPT_ERR: | |
121 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
122 | goto end; | |
123 | case OPT_HELP: | |
124 | opt_help(verify_options); | |
125 | BIO_printf(bio_err, "Recognized usages:\n"); | |
126 | for (i = 0; i < X509_PURPOSE_get_count(); i++) { | |
127 | X509_PURPOSE *ptmp; | |
128 | ptmp = X509_PURPOSE_get0(i); | |
129 | BIO_printf(bio_err, "\t%-10s\t%s\n", | |
130 | X509_PURPOSE_get0_sname(ptmp), | |
131 | X509_PURPOSE_get0_name(ptmp)); | |
132 | } | |
0f113f3e | 133 | |
7e1b7485 RS |
134 | BIO_printf(bio_err, "Recognized verify names:\n"); |
135 | for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) { | |
136 | const X509_VERIFY_PARAM *vptmp; | |
137 | vptmp = X509_VERIFY_PARAM_get0(i); | |
138 | BIO_printf(bio_err, "\t%-10s\n", | |
139 | X509_VERIFY_PARAM_get0_name(vptmp)); | |
0f113f3e | 140 | } |
7e1b7485 RS |
141 | ret = 0; |
142 | goto end; | |
143 | case OPT_V_CASES: | |
144 | if (!opt_verify(o, vpm)) | |
0f113f3e | 145 | goto end; |
7e1b7485 RS |
146 | vpmtouched++; |
147 | break; | |
148 | case OPT_CAPATH: | |
149 | CApath = opt_arg(); | |
150 | break; | |
151 | case OPT_CAFILE: | |
152 | CAfile = opt_arg(); | |
153 | break; | |
154 | case OPT_UNTRUSTED: | |
155 | untfile = opt_arg(); | |
156 | break; | |
157 | case OPT_TRUSTED: | |
158 | trustfile = opt_arg(); | |
159 | break; | |
160 | case OPT_CRLFILE: | |
161 | crlfile = opt_arg(); | |
162 | break; | |
163 | case OPT_CRL_DOWNLOAD: | |
164 | crl_download = 1; | |
0f113f3e | 165 | break; |
7e1b7485 RS |
166 | case OPT_SHOW_CHAIN: |
167 | show_chain = 1; | |
168 | break; | |
169 | case OPT_ENGINE: | |
333b070e | 170 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
171 | break; |
172 | case OPT_VERBOSE: | |
173 | v_verbose = 1; | |
174 | break; | |
175 | } | |
0f113f3e | 176 | } |
7e1b7485 RS |
177 | argc = opt_num_rest(); |
178 | argv = opt_rest(); | |
d02b48c6 | 179 | |
7e1b7485 RS |
180 | if (!(store = setup_verify(CAfile, CApath))) |
181 | goto end; | |
182 | X509_STORE_set_verify_cb(store, cb); | |
5270e702 | 183 | |
7e1b7485 RS |
184 | if (vpmtouched) |
185 | X509_STORE_set1_param(store, vpm); | |
0f113f3e MC |
186 | |
187 | ERR_clear_error(); | |
188 | ||
189 | if (untfile) { | |
7e1b7485 | 190 | untrusted = load_certs(untfile, FORMAT_PEM, |
0f113f3e MC |
191 | NULL, e, "untrusted certificates"); |
192 | if (!untrusted) | |
193 | goto end; | |
194 | } | |
195 | ||
196 | if (trustfile) { | |
7e1b7485 | 197 | trusted = load_certs(trustfile, FORMAT_PEM, |
0f113f3e MC |
198 | NULL, e, "trusted certificates"); |
199 | if (!trusted) | |
200 | goto end; | |
201 | } | |
202 | ||
203 | if (crlfile) { | |
7e1b7485 | 204 | crls = load_crls(crlfile, FORMAT_PEM, NULL, e, "other CRLs"); |
0f113f3e MC |
205 | if (!crls) |
206 | goto end; | |
207 | } | |
208 | ||
209 | if (crl_download) | |
7e1b7485 | 210 | store_setup_crl_download(store); |
0f113f3e MC |
211 | |
212 | ret = 0; | |
213 | if (argc < 1) { | |
7e1b7485 | 214 | if (check(store, NULL, untrusted, trusted, crls, e, show_chain) != 1) |
0f113f3e MC |
215 | ret = -1; |
216 | } else { | |
217 | for (i = 0; i < argc; i++) | |
7e1b7485 RS |
218 | if (check(store, argv[i], untrusted, trusted, crls, e, |
219 | show_chain) != 1) | |
0f113f3e MC |
220 | ret = -1; |
221 | } | |
222 | ||
223 | end: | |
222561fe RS |
224 | X509_VERIFY_PARAM_free(vpm); |
225 | X509_STORE_free(store); | |
0f113f3e MC |
226 | sk_X509_pop_free(untrusted, X509_free); |
227 | sk_X509_pop_free(trusted, X509_free); | |
228 | sk_X509_CRL_pop_free(crls, X509_CRL_free); | |
7e1b7485 | 229 | return (ret < 0 ? 2 : ret); |
0f113f3e | 230 | } |
d02b48c6 | 231 | |
245d2ee3 | 232 | static int check(X509_STORE *ctx, char *file, |
0f113f3e MC |
233 | STACK_OF(X509) *uchain, STACK_OF(X509) *tchain, |
234 | STACK_OF(X509_CRL) *crls, ENGINE *e, int show_chain) | |
235 | { | |
236 | X509 *x = NULL; | |
237 | int i = 0, ret = 0; | |
238 | X509_STORE_CTX *csc; | |
239 | STACK_OF(X509) *chain = NULL; | |
240 | ||
7e1b7485 | 241 | x = load_cert(file, FORMAT_PEM, NULL, e, "certificate file"); |
0f113f3e MC |
242 | if (x == NULL) |
243 | goto end; | |
7e1b7485 | 244 | printf("%s: ", (file == NULL) ? "stdin" : file); |
0f113f3e MC |
245 | |
246 | csc = X509_STORE_CTX_new(); | |
247 | if (csc == NULL) { | |
248 | ERR_print_errors(bio_err); | |
249 | goto end; | |
250 | } | |
251 | X509_STORE_set_flags(ctx, vflags); | |
252 | if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) { | |
253 | ERR_print_errors(bio_err); | |
254 | goto end; | |
255 | } | |
256 | if (tchain) | |
257 | X509_STORE_CTX_trusted_stack(csc, tchain); | |
258 | if (crls) | |
259 | X509_STORE_CTX_set0_crls(csc, crls); | |
260 | i = X509_verify_cert(csc); | |
261 | if (i > 0 && show_chain) | |
262 | chain = X509_STORE_CTX_get1_chain(csc); | |
263 | X509_STORE_CTX_free(csc); | |
264 | ||
265 | ret = 0; | |
266 | end: | |
267 | if (i > 0) { | |
7e1b7485 | 268 | printf("OK\n"); |
0f113f3e MC |
269 | ret = 1; |
270 | } else | |
271 | ERR_print_errors(bio_err); | |
272 | if (chain) { | |
273 | printf("Chain:\n"); | |
274 | for (i = 0; i < sk_X509_num(chain); i++) { | |
275 | X509 *cert = sk_X509_value(chain, i); | |
276 | printf("depth=%d: ", i); | |
277 | X509_NAME_print_ex_fp(stdout, | |
278 | X509_get_subject_name(cert), | |
279 | 0, XN_FLAG_ONELINE); | |
280 | printf("\n"); | |
281 | } | |
282 | sk_X509_pop_free(chain, X509_free); | |
283 | } | |
222561fe | 284 | X509_free(x); |
0f113f3e MC |
285 | |
286 | return (ret); | |
287 | } | |
d02b48c6 | 288 | |
6d23cf97 | 289 | static int cb(int ok, X509_STORE_CTX *ctx) |
0f113f3e MC |
290 | { |
291 | int cert_error = X509_STORE_CTX_get_error(ctx); | |
292 | X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx); | |
293 | ||
294 | if (!ok) { | |
295 | if (current_cert) { | |
ecf3a1fb RS |
296 | X509_NAME_print_ex(bio_err, |
297 | X509_get_subject_name(current_cert), | |
298 | 0, XN_FLAG_ONELINE); | |
299 | BIO_printf(bio_err, "\n"); | |
0f113f3e | 300 | } |
ecf3a1fb | 301 | BIO_printf(bio_err, "%serror %d at %d depth lookup:%s\n", |
0f113f3e MC |
302 | X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path]" : "", |
303 | cert_error, | |
304 | X509_STORE_CTX_get_error_depth(ctx), | |
305 | X509_verify_cert_error_string(cert_error)); | |
306 | switch (cert_error) { | |
307 | case X509_V_ERR_NO_EXPLICIT_POLICY: | |
ecf3a1fb | 308 | policies_print(ctx); |
0f113f3e MC |
309 | case X509_V_ERR_CERT_HAS_EXPIRED: |
310 | ||
311 | /* | |
312 | * since we are just checking the certificates, it is ok if they | |
313 | * are self signed. But we should still warn the user. | |
314 | */ | |
0f113f3e MC |
315 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
316 | /* Continue after extension errors too */ | |
317 | case X509_V_ERR_INVALID_CA: | |
318 | case X509_V_ERR_INVALID_NON_CA: | |
319 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: | |
320 | case X509_V_ERR_INVALID_PURPOSE: | |
321 | case X509_V_ERR_CRL_HAS_EXPIRED: | |
322 | case X509_V_ERR_CRL_NOT_YET_VALID: | |
323 | case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION: | |
324 | ok = 1; | |
0f113f3e MC |
325 | } |
326 | ||
327 | return ok; | |
328 | ||
329 | } | |
330 | if (cert_error == X509_V_OK && ok == 2) | |
ecf3a1fb | 331 | policies_print(ctx); |
0f113f3e MC |
332 | if (!v_verbose) |
333 | ERR_clear_error(); | |
334 | return (ok); | |
335 | } |