1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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 :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
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
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.]
62 #include <openssl/bio.h>
63 #include <openssl/err.h>
64 #include <openssl/x509.h>
65 #include <openssl/x509v3.h>
66 #include <openssl/pem.h>
68 static int cb(int ok
, X509_STORE_CTX
*ctx
);
69 static int check(X509_STORE
*ctx
, char *file
,
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;
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
,
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 "Print extra information about the operations being performed."},
88 {"CApath", OPT_CAPATH
, '/', "A directory of trusted certificates"},
89 {"CAfile", OPT_CAFILE
, '<', "A file of trusted certificates"},
90 {"untrusted", OPT_UNTRUSTED
, '<', "A file of untrusted certificates"},
91 {"trusted", OPT_TRUSTED
, '<', "A file of additional trusted certificates"},
92 {"CRLfile", OPT_CRLFILE
, '<',
93 "File containing one or more CRL's (in PEM format) to load"},
94 {"crl_download", OPT_CRL_DOWNLOAD
, '-',
95 "Attempt to download CRL information for this certificate"},
96 {"show_chain", OPT_SHOW_CHAIN
, '-',
97 "Display information about the certificate chain"},
99 #ifndef OPENSSL_NO_ENGINE
100 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
105 int verify_main(int argc
, char **argv
)
108 STACK_OF(X509
) *untrusted
= NULL
, *trusted
= NULL
;
109 STACK_OF(X509_CRL
) *crls
= NULL
;
110 X509_STORE
*store
= NULL
;
111 X509_VERIFY_PARAM
*vpm
= NULL
;
112 char *prog
, *CApath
= NULL
, *CAfile
= NULL
;
113 char *untfile
= NULL
, *trustfile
= NULL
, *crlfile
= NULL
;
114 int vpmtouched
= 0, crl_download
= 0, show_chain
= 0, i
= 0, ret
= 1;
117 if ((vpm
= X509_VERIFY_PARAM_new()) == NULL
)
120 prog
= opt_init(argc
, argv
, verify_options
);
121 while ((o
= opt_next()) != OPT_EOF
) {
125 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
128 opt_help(verify_options
);
129 BIO_printf(bio_err
, "Recognized usages:\n");
130 for (i
= 0; i
< X509_PURPOSE_get_count(); i
++) {
132 ptmp
= X509_PURPOSE_get0(i
);
133 BIO_printf(bio_err
, "\t%-10s\t%s\n",
134 X509_PURPOSE_get0_sname(ptmp
),
135 X509_PURPOSE_get0_name(ptmp
));
138 BIO_printf(bio_err
, "Recognized verify names:\n");
139 for (i
= 0; i
< X509_VERIFY_PARAM_get_count(); i
++) {
140 const X509_VERIFY_PARAM
*vptmp
;
141 vptmp
= X509_VERIFY_PARAM_get0(i
);
142 BIO_printf(bio_err
, "\t%-10s\n",
143 X509_VERIFY_PARAM_get0_name(vptmp
));
148 if (!opt_verify(o
, vpm
))
162 trustfile
= opt_arg();
167 case OPT_CRL_DOWNLOAD
:
174 e
= setup_engine(opt_arg(), 0);
181 argc
= opt_num_rest();
184 if (!app_load_modules(NULL
))
187 if ((store
= setup_verify(CAfile
, CApath
)) == NULL
)
189 X509_STORE_set_verify_cb(store
, cb
);
192 X509_STORE_set1_param(store
, vpm
);
197 untrusted
= load_certs(untfile
, FORMAT_PEM
,
198 NULL
, e
, "untrusted certificates");
204 trusted
= load_certs(trustfile
, FORMAT_PEM
,
205 NULL
, e
, "trusted certificates");
211 crls
= load_crls(crlfile
, FORMAT_PEM
, NULL
, e
, "other CRLs");
217 store_setup_crl_download(store
);
221 if (check(store
, NULL
, untrusted
, trusted
, crls
, e
, show_chain
) != 1)
224 for (i
= 0; i
< argc
; i
++)
225 if (check(store
, argv
[i
], untrusted
, trusted
, crls
, e
,
231 X509_VERIFY_PARAM_free(vpm
);
232 X509_STORE_free(store
);
233 sk_X509_pop_free(untrusted
, X509_free
);
234 sk_X509_pop_free(trusted
, X509_free
);
235 sk_X509_CRL_pop_free(crls
, X509_CRL_free
);
236 return (ret
< 0 ? 2 : ret
);
239 static int check(X509_STORE
*ctx
, char *file
,
240 STACK_OF(X509
) *uchain
, STACK_OF(X509
) *tchain
,
241 STACK_OF(X509_CRL
) *crls
, ENGINE
*e
, int show_chain
)
246 STACK_OF(X509
) *chain
= NULL
;
249 x
= load_cert(file
, FORMAT_PEM
, NULL
, e
, "certificate file");
252 printf("%s: ", (file
== NULL
) ? "stdin" : file
);
254 csc
= X509_STORE_CTX_new();
256 ERR_print_errors(bio_err
);
259 X509_STORE_set_flags(ctx
, vflags
);
260 if (!X509_STORE_CTX_init(csc
, ctx
, x
, uchain
)) {
261 ERR_print_errors(bio_err
);
265 X509_STORE_CTX_trusted_stack(csc
, tchain
);
267 X509_STORE_CTX_set0_crls(csc
, crls
);
268 i
= X509_verify_cert(csc
);
269 if (i
> 0 && show_chain
) {
270 chain
= X509_STORE_CTX_get1_chain(csc
);
271 num_untrusted
= X509_STORE_CTX_get_num_untrusted(csc
);
273 X509_STORE_CTX_free(csc
);
281 ERR_print_errors(bio_err
);
284 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
285 X509
*cert
= sk_X509_value(chain
, i
);
286 printf("depth=%d: ", i
);
287 X509_NAME_print_ex_fp(stdout
,
288 X509_get_subject_name(cert
),
290 if (i
< num_untrusted
) {
291 printf(" (untrusted)");
295 sk_X509_pop_free(chain
, X509_free
);
302 static int cb(int ok
, X509_STORE_CTX
*ctx
)
304 int cert_error
= X509_STORE_CTX_get_error(ctx
);
305 X509
*current_cert
= X509_STORE_CTX_get_current_cert(ctx
);
309 X509_NAME_print_ex(bio_err
,
310 X509_get_subject_name(current_cert
),
312 BIO_printf(bio_err
, "\n");
314 BIO_printf(bio_err
, "%serror %d at %d depth lookup:%s\n",
315 X509_STORE_CTX_get0_parent_ctx(ctx
) ? "[CRL path]" : "",
317 X509_STORE_CTX_get_error_depth(ctx
),
318 X509_verify_cert_error_string(cert_error
));
319 switch (cert_error
) {
320 case X509_V_ERR_NO_EXPLICIT_POLICY
:
322 case X509_V_ERR_CERT_HAS_EXPIRED
:
325 * since we are just checking the certificates, it is ok if they
326 * are self signed. But we should still warn the user.
328 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
329 /* Continue after extension errors too */
330 case X509_V_ERR_INVALID_CA
:
331 case X509_V_ERR_INVALID_NON_CA
:
332 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
333 case X509_V_ERR_INVALID_PURPOSE
:
334 case X509_V_ERR_CRL_HAS_EXPIRED
:
335 case X509_V_ERR_CRL_NOT_YET_VALID
:
336 case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION
:
343 if (cert_error
== X509_V_OK
&& ok
== 2)