a4eb465739d6e7173656dea7cb7dd589e5cf17e5
[openssl.git] / apps / verify.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "apps.h"
14 #include <openssl/bio.h>
15 #include <openssl/err.h>
16 #include <openssl/x509.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/pem.h>
19
20 static int cb(int ok, X509_STORE_CTX *ctx);
21 static int check(X509_STORE *ctx, const char *file,
22                  STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
23                  STACK_OF(X509_CRL) *crls, int show_chain);
24 static int v_verbose = 0, vflags = 0;
25 static unsigned long nmflag = 0;
26
27 typedef enum OPTION_choice {
28     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
29     OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE,
30     OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN,
31     OPT_V_ENUM, OPT_NAMEOPT, 
32     OPT_VERBOSE
33 } OPTION_CHOICE;
34
35 const OPTIONS verify_options[] = {
36     {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
37     {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
38     {"help", OPT_HELP, '-', "Display this summary"},
39     {"verbose", OPT_VERBOSE, '-',
40         "Print extra information about the operations being performed."},
41     {"CApath", OPT_CAPATH, '/', "A directory of trusted certificates"},
42     {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"},
43     {"no-CAfile", OPT_NOCAFILE, '-',
44      "Do not load the default certificates file"},
45     {"no-CApath", OPT_NOCAPATH, '-',
46      "Do not load certificates from the default certificates directory"},
47     {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
48     {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
49     {"CRLfile", OPT_CRLFILE, '<',
50         "File containing one or more CRL's (in PEM format) to load"},
51     {"crl_download", OPT_CRL_DOWNLOAD, '-',
52         "Attempt to download CRL information for this certificate"},
53     {"show_chain", OPT_SHOW_CHAIN, '-',
54         "Display information about the certificate chain"},
55     {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
56     OPT_V_OPTIONS,
57 #ifndef OPENSSL_NO_ENGINE
58     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
59 #endif
60     {NULL}
61 };
62
63 int verify_main(int argc, char **argv)
64 {
65     ENGINE *e = NULL;
66     STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
67     STACK_OF(X509_CRL) *crls = NULL;
68     X509_STORE *store = NULL;
69     X509_VERIFY_PARAM *vpm = NULL;
70     const char *prog, *CApath = NULL, *CAfile = NULL;
71     int noCApath = 0, noCAfile = 0;
72     int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
73     char nmflag_set = 0;
74     OPTION_CHOICE o;
75
76     if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
77         goto end;
78
79     prog = opt_init(argc, argv, verify_options);
80     while ((o = opt_next()) != OPT_EOF) {
81         switch (o) {
82         case OPT_EOF:
83         case OPT_ERR:
84             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
85             goto end;
86         case OPT_HELP:
87             opt_help(verify_options);
88             BIO_printf(bio_err, "Recognized usages:\n");
89             for (i = 0; i < X509_PURPOSE_get_count(); i++) {
90                 X509_PURPOSE *ptmp;
91                 ptmp = X509_PURPOSE_get0(i);
92                 BIO_printf(bio_err, "\t%-10s\t%s\n",
93                         X509_PURPOSE_get0_sname(ptmp),
94                         X509_PURPOSE_get0_name(ptmp));
95             }
96
97             BIO_printf(bio_err, "Recognized verify names:\n");
98             for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
99                 const X509_VERIFY_PARAM *vptmp;
100                 vptmp = X509_VERIFY_PARAM_get0(i);
101                 BIO_printf(bio_err, "\t%-10s\n",
102                         X509_VERIFY_PARAM_get0_name(vptmp));
103             }
104             ret = 0;
105             goto end;
106         case OPT_V_CASES:
107             if (!opt_verify(o, vpm))
108                 goto end;
109             vpmtouched++;
110             break;
111         case OPT_CAPATH:
112             CApath = opt_arg();
113             break;
114         case OPT_CAFILE:
115             CAfile = opt_arg();
116             break;
117         case OPT_NOCAPATH:
118             noCApath = 1;
119             break;
120         case OPT_NOCAFILE:
121             noCAfile = 1;
122             break;
123         case OPT_UNTRUSTED:
124             /* Zero or more times */
125             if (!load_certs(opt_arg(), &untrusted, FORMAT_PEM, NULL,
126                             "untrusted certificates"))
127                 goto end;
128             break;
129         case OPT_TRUSTED:
130             /* Zero or more times */
131             noCAfile = 1;
132             noCApath = 1;
133             if (!load_certs(opt_arg(), &trusted, FORMAT_PEM, NULL,
134                             "trusted certificates"))
135                 goto end;
136             break;
137         case OPT_CRLFILE:
138             /* Zero or more times */
139             if (!load_crls(opt_arg(), &crls, FORMAT_PEM, NULL,
140                            "other CRLs"))
141                 goto end;
142             break;
143         case OPT_CRL_DOWNLOAD:
144             crl_download = 1;
145             break;
146         case OPT_ENGINE:
147             if ((e = setup_engine(opt_arg(), 0)) == NULL) {
148                 /* Failure message already displayed */
149                 goto end;
150             }
151             break;
152         case OPT_SHOW_CHAIN:
153             show_chain = 1;
154             break;
155         case OPT_NAMEOPT:
156             nmflag_set = 1;
157             if (!set_name_ex(&nmflag, opt_arg()))
158                 goto end;
159             break;
160         case OPT_VERBOSE:
161             v_verbose = 1;
162             break;
163         }
164     }
165     argc = opt_num_rest();
166     argv = opt_rest();
167     if (trusted != NULL && (CAfile || CApath)) {
168         BIO_printf(bio_err,
169                    "%s: Cannot use -trusted with -CAfile or -CApath\n",
170                    prog);
171         goto end;
172     }
173
174     if (!nmflag_set)
175         nmflag = XN_FLAG_ONELINE;
176
177     if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
178         goto end;
179     X509_STORE_set_verify_cb(store, cb);
180
181     if (vpmtouched)
182         X509_STORE_set1_param(store, vpm);
183
184     ERR_clear_error();
185
186     if (crl_download)
187         store_setup_crl_download(store);
188
189     ret = 0;
190     if (argc < 1) {
191         if (check(store, NULL, untrusted, trusted, crls, show_chain) != 1)
192             ret = -1;
193     } else {
194         for (i = 0; i < argc; i++)
195             if (check(store, argv[i], untrusted, trusted, crls,
196                       show_chain) != 1)
197                 ret = -1;
198     }
199
200  end:
201     X509_VERIFY_PARAM_free(vpm);
202     X509_STORE_free(store);
203     sk_X509_pop_free(untrusted, X509_free);
204     sk_X509_pop_free(trusted, X509_free);
205     sk_X509_CRL_pop_free(crls, X509_CRL_free);
206     release_engine(e);
207     return (ret < 0 ? 2 : ret);
208 }
209
210 static int check(X509_STORE *ctx, const char *file,
211                  STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
212                  STACK_OF(X509_CRL) *crls, int show_chain)
213 {
214     X509 *x = NULL;
215     int i = 0, ret = 0;
216     X509_STORE_CTX *csc;
217     STACK_OF(X509) *chain = NULL;
218     int num_untrusted;
219
220     x = load_cert(file, FORMAT_PEM, "certificate file");
221     if (x == NULL)
222         goto end;
223
224     csc = X509_STORE_CTX_new();
225     if (csc == NULL) {
226         printf("error %s: X.509 store context allocation failed\n",
227                (file == NULL) ? "stdin" : file);
228         goto end;
229     }
230
231     X509_STORE_set_flags(ctx, vflags);
232     if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
233         printf("error %s: X.509 store context initialization failed\n",
234                (file == NULL) ? "stdin" : file);
235         goto end;
236     }
237     if (tchain)
238         X509_STORE_CTX_set0_trusted_stack(csc, tchain);
239     if (crls)
240         X509_STORE_CTX_set0_crls(csc, crls);
241     i = X509_verify_cert(csc);
242     if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
243         printf("%s: OK\n", (file == NULL) ? "stdin" : file);
244         ret = 1;
245         if (show_chain) {
246             int j;
247
248             chain = X509_STORE_CTX_get1_chain(csc);
249             num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
250             printf("Chain:\n");
251             for (j = 0; j < sk_X509_num(chain); j++) {
252                 X509 *cert = sk_X509_value(chain, j);
253                 printf("depth=%d: ", j);
254                 X509_NAME_print_ex_fp(stdout,
255                                       X509_get_subject_name(cert),
256                                       0, nmflag);
257                 if (j < num_untrusted)
258                     printf(" (untrusted)");
259                 printf("\n");
260             }
261             sk_X509_pop_free(chain, X509_free);
262         }
263     } else {
264         printf("error %s: verification failed\n", (file == NULL) ? "stdin" : file);
265     }
266     X509_STORE_CTX_free(csc);
267
268  end:
269     if (i <= 0)
270         ERR_print_errors(bio_err);
271     X509_free(x);
272
273     return ret;
274 }
275
276 static int cb(int ok, X509_STORE_CTX *ctx)
277 {
278     int cert_error = X509_STORE_CTX_get_error(ctx);
279     X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
280
281     if (!ok) {
282         if (current_cert) {
283             X509_NAME_print_ex(bio_err,
284                             X509_get_subject_name(current_cert),
285                             0, nmflag);
286             BIO_printf(bio_err, "\n");
287         }
288         BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
289                X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
290                cert_error,
291                X509_STORE_CTX_get_error_depth(ctx),
292                X509_verify_cert_error_string(cert_error));
293         switch (cert_error) {
294         case X509_V_ERR_NO_EXPLICIT_POLICY:
295             policies_print(ctx);
296         case X509_V_ERR_CERT_HAS_EXPIRED:
297
298             /*
299              * since we are just checking the certificates, it is ok if they
300              * are self signed. But we should still warn the user.
301              */
302         case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
303             /* Continue after extension errors too */
304         case X509_V_ERR_INVALID_CA:
305         case X509_V_ERR_INVALID_NON_CA:
306         case X509_V_ERR_PATH_LENGTH_EXCEEDED:
307         case X509_V_ERR_INVALID_PURPOSE:
308         case X509_V_ERR_CRL_HAS_EXPIRED:
309         case X509_V_ERR_CRL_NOT_YET_VALID:
310         case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
311             ok = 1;
312         }
313
314         return ok;
315
316     }
317     if (cert_error == X509_V_OK && ok == 2)
318         policies_print(ctx);
319     if (!v_verbose)
320         ERR_clear_error();
321     return (ok);
322 }