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