]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/verify.c
Don't use a default for --with-zlib-lib on Windows with option 'zlib'
[thirdparty/openssl.git] / apps / verify.c
CommitLineData
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 68static int cb(int ok, X509_STORE_CTX *ctx);
245d2ee3 69static int check(X509_STORE *ctx, char *file,
0f113f3e 70 STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
a773b52a 71 STACK_OF(X509_CRL) *crls, int show_chain);
0f113f3e 72static int v_verbose = 0, vflags = 0;
d02b48c6 73
7e1b7485
RS
74typedef enum OPTION_choice {
75 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
2b6bcb70
MC
76 OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE,
77 OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN,
7e1b7485
RS
78 OPT_V_ENUM,
79 OPT_VERBOSE
80} OPTION_CHOICE;
81
82OPTIONS 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"},
a64ba70d
MC
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"},
2b6bcb70
MC
90 {"no-CAfile", OPT_NOCAFILE, '-',
91 "Do not load the default certificates file"},
92 {"no-CApath", OPT_NOCAPATH, '-',
93 "Do not load certificates from the default certificates directory"},
a64ba70d 94 {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
5b89036c 95 {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
a64ba70d
MC
96 {"CRLfile", OPT_CRLFILE, '<',
97 "File containing one or more CRL's (in PEM format) to load"},
98 {"crl_download", OPT_CRL_DOWNLOAD, '-',
99 "Attempt to download CRL information for this certificate"},
100 {"show_chain", OPT_SHOW_CHAIN, '-',
101 "Display information about the certificate chain"},
9c3bcfa0 102 OPT_V_OPTIONS,
7e1b7485
RS
103#ifndef OPENSSL_NO_ENGINE
104 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
105#endif
7e1b7485
RS
106 {NULL}
107};
667ac4ec 108
7e1b7485 109int verify_main(int argc, char **argv)
0f113f3e 110{
0f113f3e
MC
111 STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
112 STACK_OF(X509_CRL) *crls = NULL;
7e1b7485 113 X509_STORE *store = NULL;
0f113f3e 114 X509_VERIFY_PARAM *vpm = NULL;
333b070e 115 char *prog, *CApath = NULL, *CAfile = NULL;
2b6bcb70 116 int noCApath = 0, noCAfile = 0;
7e1b7485
RS
117 int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
118 OPTION_CHOICE o;
d02b48c6 119
7e1b7485 120 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
0f113f3e 121 goto end;
0f113f3e 122
7e1b7485
RS
123 prog = opt_init(argc, argv, verify_options);
124 while ((o = opt_next()) != OPT_EOF) {
125 switch (o) {
126 case OPT_EOF:
127 case OPT_ERR:
128 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
129 goto end;
130 case OPT_HELP:
131 opt_help(verify_options);
132 BIO_printf(bio_err, "Recognized usages:\n");
133 for (i = 0; i < X509_PURPOSE_get_count(); i++) {
134 X509_PURPOSE *ptmp;
135 ptmp = X509_PURPOSE_get0(i);
136 BIO_printf(bio_err, "\t%-10s\t%s\n",
137 X509_PURPOSE_get0_sname(ptmp),
138 X509_PURPOSE_get0_name(ptmp));
139 }
0f113f3e 140
7e1b7485
RS
141 BIO_printf(bio_err, "Recognized verify names:\n");
142 for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
143 const X509_VERIFY_PARAM *vptmp;
144 vptmp = X509_VERIFY_PARAM_get0(i);
145 BIO_printf(bio_err, "\t%-10s\n",
146 X509_VERIFY_PARAM_get0_name(vptmp));
0f113f3e 147 }
7e1b7485
RS
148 ret = 0;
149 goto end;
150 case OPT_V_CASES:
151 if (!opt_verify(o, vpm))
0f113f3e 152 goto end;
7e1b7485
RS
153 vpmtouched++;
154 break;
155 case OPT_CAPATH:
156 CApath = opt_arg();
157 break;
158 case OPT_CAFILE:
159 CAfile = opt_arg();
160 break;
2b6bcb70
MC
161 case OPT_NOCAPATH:
162 noCApath = 1;
163 break;
164 case OPT_NOCAFILE:
165 noCAfile = 1;
166 break;
7e1b7485 167 case OPT_UNTRUSTED:
feb2f53e 168 /* Zero or more times */
a773b52a 169 if (!load_certs(opt_arg(), &untrusted, FORMAT_PEM, NULL,
feb2f53e
VD
170 "untrusted certificates"))
171 goto end;
7e1b7485
RS
172 break;
173 case OPT_TRUSTED:
feb2f53e
VD
174 /* Zero or more times */
175 noCAfile = 1;
176 noCApath = 1;
a773b52a 177 if (!load_certs(opt_arg(), &trusted, FORMAT_PEM, NULL,
feb2f53e
VD
178 "trusted certificates"))
179 goto end;
7e1b7485
RS
180 break;
181 case OPT_CRLFILE:
feb2f53e 182 /* Zero or more times */
a773b52a 183 if (!load_crls(opt_arg(), &crls, FORMAT_PEM, NULL,
feb2f53e
VD
184 "other CRLs"))
185 goto end;
7e1b7485
RS
186 break;
187 case OPT_CRL_DOWNLOAD:
188 crl_download = 1;
0f113f3e 189 break;
a773b52a
RS
190 case OPT_ENGINE:
191 if (setup_engine(opt_arg(), 0) == NULL) {
192 /* Failure message already displayed */
193 goto end;
194 }
195 break;
7e1b7485
RS
196 case OPT_SHOW_CHAIN:
197 show_chain = 1;
198 break;
7e1b7485
RS
199 case OPT_VERBOSE:
200 v_verbose = 1;
201 break;
202 }
0f113f3e 203 }
7e1b7485
RS
204 argc = opt_num_rest();
205 argv = opt_rest();
feb2f53e 206 if (trusted != NULL && (CAfile || CApath)) {
5b89036c
RS
207 BIO_printf(bio_err,
208 "%s: Cannot use -trusted with -CAfile or -CApath\n",
209 prog);
210 goto end;
211 }
d02b48c6 212
2b6bcb70 213 if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
7e1b7485
RS
214 goto end;
215 X509_STORE_set_verify_cb(store, cb);
5270e702 216
7e1b7485
RS
217 if (vpmtouched)
218 X509_STORE_set1_param(store, vpm);
0f113f3e
MC
219
220 ERR_clear_error();
221
0f113f3e 222 if (crl_download)
7e1b7485 223 store_setup_crl_download(store);
0f113f3e
MC
224
225 ret = 0;
226 if (argc < 1) {
a773b52a 227 if (check(store, NULL, untrusted, trusted, crls, show_chain) != 1)
0f113f3e
MC
228 ret = -1;
229 } else {
230 for (i = 0; i < argc; i++)
a773b52a 231 if (check(store, argv[i], untrusted, trusted, crls,
7e1b7485 232 show_chain) != 1)
0f113f3e
MC
233 ret = -1;
234 }
235
236 end:
222561fe
RS
237 X509_VERIFY_PARAM_free(vpm);
238 X509_STORE_free(store);
0f113f3e
MC
239 sk_X509_pop_free(untrusted, X509_free);
240 sk_X509_pop_free(trusted, X509_free);
241 sk_X509_CRL_pop_free(crls, X509_CRL_free);
7e1b7485 242 return (ret < 0 ? 2 : ret);
0f113f3e 243}
d02b48c6 244
245d2ee3 245static int check(X509_STORE *ctx, char *file,
0f113f3e 246 STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
a773b52a 247 STACK_OF(X509_CRL) *crls, int show_chain)
0f113f3e
MC
248{
249 X509 *x = NULL;
250 int i = 0, ret = 0;
251 X509_STORE_CTX *csc;
252 STACK_OF(X509) *chain = NULL;
7f3f41d8 253 int num_untrusted;
0f113f3e 254
a773b52a 255 x = load_cert(file, FORMAT_PEM, "certificate file");
0f113f3e
MC
256 if (x == NULL)
257 goto end;
0f113f3e
MC
258
259 csc = X509_STORE_CTX_new();
260 if (csc == NULL) {
63c6aa6b
VD
261 printf("error %s: X.509 store context allocation failed\n",
262 (file == NULL) ? "stdin" : file);
0f113f3e
MC
263 goto end;
264 }
265 X509_STORE_set_flags(ctx, vflags);
266 if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
63c6aa6b
VD
267 printf("error %s: X.509 store context initialization failed\n",
268 (file == NULL) ? "stdin" : file);
0f113f3e
MC
269 goto end;
270 }
271 if (tchain)
272 X509_STORE_CTX_trusted_stack(csc, tchain);
273 if (crls)
274 X509_STORE_CTX_set0_crls(csc, crls);
275 i = X509_verify_cert(csc);
d9e309a6 276 if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
63c6aa6b 277 printf("%s: OK\n", (file == NULL) ? "stdin" : file);
480405e4 278 ret = 1;
63c6aa6b
VD
279 if (show_chain) {
280 int j;
bb484020 281
63c6aa6b
VD
282 chain = X509_STORE_CTX_get1_chain(csc);
283 num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
284 printf("Chain:\n");
285 for (j = 0; j < sk_X509_num(chain); j++) {
286 X509 *cert = sk_X509_value(chain, j);
287 printf("depth=%d: ", j);
288 X509_NAME_print_ex_fp(stdout,
289 X509_get_subject_name(cert),
290 0, XN_FLAG_ONELINE);
291 if (j < num_untrusted)
292 printf(" (untrusted)");
293 printf("\n");
294 }
295 sk_X509_pop_free(chain, X509_free);
296 }
297 } else {
298 printf("error %s: verification failed\n", (file == NULL) ? "stdin" : file);
7f3f41d8 299 }
0f113f3e
MC
300 X509_STORE_CTX_free(csc);
301
0f113f3e 302 end:
480405e4 303 if (i <= 0)
63c6aa6b 304 ERR_print_errors(bio_err);
222561fe 305 X509_free(x);
0f113f3e 306
480405e4 307 return ret;
0f113f3e 308}
d02b48c6 309
6d23cf97 310static int cb(int ok, X509_STORE_CTX *ctx)
0f113f3e
MC
311{
312 int cert_error = X509_STORE_CTX_get_error(ctx);
313 X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
314
315 if (!ok) {
316 if (current_cert) {
ecf3a1fb
RS
317 X509_NAME_print_ex(bio_err,
318 X509_get_subject_name(current_cert),
319 0, XN_FLAG_ONELINE);
320 BIO_printf(bio_err, "\n");
0f113f3e 321 }
63c6aa6b
VD
322 BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
323 X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
0f113f3e
MC
324 cert_error,
325 X509_STORE_CTX_get_error_depth(ctx),
326 X509_verify_cert_error_string(cert_error));
327 switch (cert_error) {
328 case X509_V_ERR_NO_EXPLICIT_POLICY:
ecf3a1fb 329 policies_print(ctx);
0f113f3e
MC
330 case X509_V_ERR_CERT_HAS_EXPIRED:
331
332 /*
333 * since we are just checking the certificates, it is ok if they
334 * are self signed. But we should still warn the user.
335 */
0f113f3e
MC
336 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
337 /* Continue after extension errors too */
338 case X509_V_ERR_INVALID_CA:
339 case X509_V_ERR_INVALID_NON_CA:
340 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
341 case X509_V_ERR_INVALID_PURPOSE:
342 case X509_V_ERR_CRL_HAS_EXPIRED:
343 case X509_V_ERR_CRL_NOT_YET_VALID:
344 case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
345 ok = 1;
0f113f3e
MC
346 }
347
348 return ok;
349
350 }
351 if (cert_error == X509_V_OK && ok == 2)
ecf3a1fb 352 policies_print(ctx);
0f113f3e
MC
353 if (!v_verbose)
354 ERR_clear_error();
355 return (ok);
356}