bd4ed05065082f40c36678d67f6e60915fb45acd
[openssl.git] / apps / verify.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
7  *
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).
14  *
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.
21  *
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 :-).
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)"
39  *
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.
51  *
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"
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>
67
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;
73
74 typedef enum OPTION_choice {
75     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
76     OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE,
77     OPT_UNTRUSTED, OPT_TRUSTED, 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         "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     {"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"},
94     {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
95     {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
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"},
102     OPT_V_OPTIONS,
103 #ifndef OPENSSL_NO_ENGINE
104     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
105 #endif
106     {NULL}
107 };
108
109 int verify_main(int argc, char **argv)
110 {
111     ENGINE *e = NULL;
112     STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
113     STACK_OF(X509_CRL) *crls = NULL;
114     X509_STORE *store = NULL;
115     X509_VERIFY_PARAM *vpm = NULL;
116     char *prog, *CApath = NULL, *CAfile = NULL;
117     int noCApath = 0, noCAfile = 0;
118     char *untfile = NULL, *trustfile = NULL, *crlfile = NULL;
119     int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
120     OPTION_CHOICE o;
121
122     if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
123         goto end;
124
125     prog = opt_init(argc, argv, verify_options);
126     while ((o = opt_next()) != OPT_EOF) {
127         switch (o) {
128         case OPT_EOF:
129         case OPT_ERR:
130             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
131             goto end;
132         case OPT_HELP:
133             opt_help(verify_options);
134             BIO_printf(bio_err, "Recognized usages:\n");
135             for (i = 0; i < X509_PURPOSE_get_count(); i++) {
136                 X509_PURPOSE *ptmp;
137                 ptmp = X509_PURPOSE_get0(i);
138                 BIO_printf(bio_err, "\t%-10s\t%s\n",
139                         X509_PURPOSE_get0_sname(ptmp),
140                         X509_PURPOSE_get0_name(ptmp));
141             }
142
143             BIO_printf(bio_err, "Recognized verify names:\n");
144             for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
145                 const X509_VERIFY_PARAM *vptmp;
146                 vptmp = X509_VERIFY_PARAM_get0(i);
147                 BIO_printf(bio_err, "\t%-10s\n",
148                         X509_VERIFY_PARAM_get0_name(vptmp));
149             }
150             ret = 0;
151             goto end;
152         case OPT_V_CASES:
153             if (!opt_verify(o, vpm))
154                 goto end;
155             vpmtouched++;
156             break;
157         case OPT_CAPATH:
158             CApath = opt_arg();
159             break;
160         case OPT_CAFILE:
161             CAfile = opt_arg();
162             break;
163         case OPT_NOCAPATH:
164             noCApath = 1;
165             break;
166         case OPT_NOCAFILE:
167             noCAfile = 1;
168             break;
169         case OPT_UNTRUSTED:
170             untfile = opt_arg();
171             break;
172         case OPT_TRUSTED:
173             trustfile = opt_arg();
174             break;
175         case OPT_CRLFILE:
176             crlfile = opt_arg();
177             break;
178         case OPT_CRL_DOWNLOAD:
179             crl_download = 1;
180             break;
181         case OPT_SHOW_CHAIN:
182             show_chain = 1;
183             break;
184         case OPT_ENGINE:
185             e = setup_engine(opt_arg(), 0);
186             break;
187         case OPT_VERBOSE:
188             v_verbose = 1;
189             break;
190         }
191     }
192     argc = opt_num_rest();
193     argv = opt_rest();
194     if (trustfile && (CAfile || CApath)) {
195         BIO_printf(bio_err,
196                    "%s: Cannot use -trusted with -CAfile or -CApath\n",
197                    prog);
198         goto end;
199     }
200
201     if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
202         goto end;
203     X509_STORE_set_verify_cb(store, cb);
204
205     if (vpmtouched)
206         X509_STORE_set1_param(store, vpm);
207
208     ERR_clear_error();
209
210     if (untfile) {
211         untrusted = load_certs(untfile, FORMAT_PEM,
212                                NULL, e, "untrusted certificates");
213         if (!untrusted)
214             goto end;
215     }
216
217     if (trustfile) {
218         trusted = load_certs(trustfile, FORMAT_PEM,
219                              NULL, e, "trusted certificates");
220         if (!trusted)
221             goto end;
222     }
223
224     if (crlfile) {
225         crls = load_crls(crlfile, FORMAT_PEM, NULL, e, "other CRLs");
226         if (!crls)
227             goto end;
228     }
229
230     if (crl_download)
231         store_setup_crl_download(store);
232
233     ret = 0;
234     if (argc < 1) {
235         if (check(store, NULL, untrusted, trusted, crls, e, show_chain) != 1)
236             ret = -1;
237     } else {
238         for (i = 0; i < argc; i++)
239             if (check(store, argv[i], untrusted, trusted, crls, e,
240                       show_chain) != 1)
241                 ret = -1;
242     }
243
244  end:
245     X509_VERIFY_PARAM_free(vpm);
246     X509_STORE_free(store);
247     sk_X509_pop_free(untrusted, X509_free);
248     sk_X509_pop_free(trusted, X509_free);
249     sk_X509_CRL_pop_free(crls, X509_CRL_free);
250     return (ret < 0 ? 2 : ret);
251 }
252
253 static int check(X509_STORE *ctx, char *file,
254                  STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
255                  STACK_OF(X509_CRL) *crls, ENGINE *e, int show_chain)
256 {
257     X509 *x = NULL;
258     int i = 0, ret = 0;
259     X509_STORE_CTX *csc;
260     STACK_OF(X509) *chain = NULL;
261     int num_untrusted;
262
263     x = load_cert(file, FORMAT_PEM, NULL, e, "certificate file");
264     if (x == NULL)
265         goto end;
266     printf("%s: ", (file == NULL) ? "stdin" : file);
267
268     csc = X509_STORE_CTX_new();
269     if (csc == NULL) {
270         ERR_print_errors(bio_err);
271         goto end;
272     }
273     X509_STORE_set_flags(ctx, vflags);
274     if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
275         ERR_print_errors(bio_err);
276         goto end;
277     }
278     if (tchain)
279         X509_STORE_CTX_trusted_stack(csc, tchain);
280     if (crls)
281         X509_STORE_CTX_set0_crls(csc, crls);
282     i = X509_verify_cert(csc);
283     if (i > 0) {
284         printf("OK\n");
285         ret = 1;
286         if (show_chain) {
287             int j;
288
289             chain = X509_STORE_CTX_get1_chain(csc);
290             num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
291             printf("Chain:\n");
292             for (j = 0; j < sk_X509_num(chain); j++) {
293                 X509 *cert = sk_X509_value(chain, j);
294                 printf("depth=%d: ", j);
295                 X509_NAME_print_ex_fp(stdout,
296                                       X509_get_subject_name(cert),
297                                       0, XN_FLAG_ONELINE);
298                 if (j < num_untrusted)
299                     printf(" (untrusted)");
300                 printf("\n");
301             }
302             sk_X509_pop_free(chain, X509_free);
303         }
304     }
305     X509_STORE_CTX_free(csc);
306
307  end:
308     if (i <= 0)
309         ERR_print_errors(bio_err);
310     X509_free(x);
311
312     return ret;
313 }
314
315 static int cb(int ok, X509_STORE_CTX *ctx)
316 {
317     int cert_error = X509_STORE_CTX_get_error(ctx);
318     X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
319
320     if (!ok) {
321         if (current_cert) {
322             X509_NAME_print_ex(bio_err,
323                             X509_get_subject_name(current_cert),
324                             0, XN_FLAG_ONELINE);
325             BIO_printf(bio_err, "\n");
326         }
327         BIO_printf(bio_err, "%serror %d at %d depth lookup:%s\n",
328                X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path]" : "",
329                cert_error,
330                X509_STORE_CTX_get_error_depth(ctx),
331                X509_verify_cert_error_string(cert_error));
332         switch (cert_error) {
333         case X509_V_ERR_NO_EXPLICIT_POLICY:
334             policies_print(ctx);
335         case X509_V_ERR_CERT_HAS_EXPIRED:
336
337             /*
338              * since we are just checking the certificates, it is ok if they
339              * are self signed. But we should still warn the user.
340              */
341         case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
342             /* Continue after extension errors too */
343         case X509_V_ERR_INVALID_CA:
344         case X509_V_ERR_INVALID_NON_CA:
345         case X509_V_ERR_PATH_LENGTH_EXCEEDED:
346         case X509_V_ERR_INVALID_PURPOSE:
347         case X509_V_ERR_CRL_HAS_EXPIRED:
348         case X509_V_ERR_CRL_NOT_YET_VALID:
349         case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
350             ok = 1;
351         }
352
353         return ok;
354
355     }
356     if (cert_error == X509_V_OK && ok == 2)
357         policies_print(ctx);
358     if (!v_verbose)
359         ERR_clear_error();
360     return (ok);
361 }