GOST2012 TLS ClientCertificateType Identifiers support
[openssl.git] / apps / verify.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 "progs.h"
15 #include <openssl/bio.h>
16 #include <openssl/err.h>
17 #include <openssl/x509.h>
18 #include <openssl/x509v3.h>
19 #include <openssl/pem.h>
20
21 static int cb(int ok, X509_STORE_CTX *ctx);
22 static int check(X509_STORE *ctx, const char *file,
23                  STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
24                  STACK_OF(X509_CRL) *crls, int show_chain,
25                  STACK_OF(OPENSSL_STRING) *opts);
26 static int v_verbose = 0, vflags = 0;
27
28 typedef enum OPTION_choice {
29     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
30     OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE,
31     OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE,
32     OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN,
33     OPT_V_ENUM, OPT_NAMEOPT, OPT_VFYOPT,
34     OPT_VERBOSE,
35     OPT_PROV_ENUM
36 } OPTION_CHOICE;
37
38 const OPTIONS verify_options[] = {
39     {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n"},
40
41     OPT_SECTION("General"),
42     {"help", OPT_HELP, '-', "Display this summary"},
43 #ifndef OPENSSL_NO_ENGINE
44     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
45 #endif
46     {"verbose", OPT_VERBOSE, '-',
47         "Print extra information about the operations being performed."},
48     {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
49
50     OPT_SECTION("Certificate chain"),
51     {"CApath", OPT_CAPATH, '/', "A directory of trusted certificates"},
52     {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"},
53     {"CAstore", OPT_CASTORE, ':', "URI to a store of trusted certificates"},
54     {"no-CAfile", OPT_NOCAFILE, '-',
55      "Do not load the default certificates file"},
56     {"no-CApath", OPT_NOCAPATH, '-',
57      "Do not load certificates from the default certificates directory"},
58     {"no-CAstore", OPT_NOCAPATH, '-',
59      "Do not load certificates from the default certificates store"},
60     {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
61     {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
62     {"CRLfile", OPT_CRLFILE, '<',
63         "File containing one or more CRL's (in PEM format) to load"},
64     {"crl_download", OPT_CRL_DOWNLOAD, '-',
65         "Attempt to download CRL information for this certificate"},
66     {"show_chain", OPT_SHOW_CHAIN, '-',
67         "Display information about the certificate chain"},
68
69     OPT_V_OPTIONS,
70     {"vfyopt", OPT_VFYOPT, 's', "Verification parameter in n:v form"},
71
72     OPT_PROV_OPTIONS,
73
74     OPT_PARAMETERS(),
75     {"cert", 0, 0, "Certificate(s) to verify (optional; stdin used otherwise)"},
76     {NULL}
77 };
78
79 int verify_main(int argc, char **argv)
80 {
81     ENGINE *e = NULL;
82     STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
83     STACK_OF(X509_CRL) *crls = NULL;
84     STACK_OF(OPENSSL_STRING) *vfyopts = NULL;
85     X509_STORE *store = NULL;
86     X509_VERIFY_PARAM *vpm = NULL;
87     const char *prog, *CApath = NULL, *CAfile = NULL, *CAstore = NULL;
88     int noCApath = 0, noCAfile = 0, noCAstore = 0;
89     int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
90     OPTION_CHOICE o;
91
92     if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
93         goto end;
94
95     prog = opt_init(argc, argv, verify_options);
96     while ((o = opt_next()) != OPT_EOF) {
97         switch (o) {
98         case OPT_EOF:
99         case OPT_ERR:
100  opthelp:
101             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
102             goto end;
103         case OPT_HELP:
104             opt_help(verify_options);
105             BIO_printf(bio_err, "\nRecognized certificate chain purposes:\n");
106             for (i = 0; i < X509_PURPOSE_get_count(); i++) {
107                 X509_PURPOSE *ptmp = X509_PURPOSE_get0(i);
108
109                 BIO_printf(bio_err, "  %-15s  %s\n",
110                         X509_PURPOSE_get0_sname(ptmp),
111                         X509_PURPOSE_get0_name(ptmp));
112             }
113
114             BIO_printf(bio_err, "Recognized certificate policy names:\n");
115             for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
116                 const X509_VERIFY_PARAM *vptmp = X509_VERIFY_PARAM_get0(i);
117
118                 BIO_printf(bio_err, "  %s\n",
119                         X509_VERIFY_PARAM_get0_name(vptmp));
120             }
121             ret = 0;
122             goto end;
123         case OPT_V_CASES:
124             if (!opt_verify(o, vpm))
125                 goto end;
126             vpmtouched++;
127             break;
128         case OPT_CAPATH:
129             CApath = opt_arg();
130             break;
131         case OPT_CAFILE:
132             CAfile = opt_arg();
133             break;
134         case OPT_CASTORE:
135             CAstore = opt_arg();
136             break;
137         case OPT_NOCAPATH:
138             noCApath = 1;
139             break;
140         case OPT_NOCAFILE:
141             noCAfile = 1;
142             break;
143         case OPT_NOCASTORE:
144             noCAstore = 1;
145             break;
146         case OPT_UNTRUSTED:
147             /* Zero or more times */
148             if (!load_certs(opt_arg(), &untrusted, FORMAT_PEM, NULL,
149                             "untrusted certificates"))
150                 goto end;
151             break;
152         case OPT_TRUSTED:
153             /* Zero or more times */
154             noCAfile = 1;
155             noCApath = 1;
156             noCAstore = 1;
157             if (!load_certs(opt_arg(), &trusted, FORMAT_PEM, NULL,
158                             "trusted certificates"))
159                 goto end;
160             break;
161         case OPT_CRLFILE:
162             /* Zero or more times */
163             if (!load_crls(opt_arg(), &crls, FORMAT_PEM, NULL,
164                            "other CRLs"))
165                 goto end;
166             break;
167         case OPT_CRL_DOWNLOAD:
168             crl_download = 1;
169             break;
170         case OPT_ENGINE:
171             if ((e = setup_engine(opt_arg(), 0)) == NULL) {
172                 /* Failure message already displayed */
173                 goto end;
174             }
175             break;
176         case OPT_SHOW_CHAIN:
177             show_chain = 1;
178             break;
179         case OPT_NAMEOPT:
180             if (!set_nameopt(opt_arg()))
181                 goto end;
182             break;
183         case OPT_VFYOPT:
184             if (!vfyopts)
185                 vfyopts = sk_OPENSSL_STRING_new_null();
186             if (!vfyopts || !sk_OPENSSL_STRING_push(vfyopts, opt_arg()))
187                 goto opthelp;
188             break;
189         case OPT_VERBOSE:
190             v_verbose = 1;
191             break;
192         case OPT_PROV_CASES:
193             if (!opt_provider(o))
194                 goto end;
195             break;
196         }
197     }
198     argc = opt_num_rest();
199     argv = opt_rest();
200     if (trusted != NULL
201         && (CAfile != NULL || CApath != NULL || CAstore != NULL)) {
202         BIO_printf(bio_err,
203                    "%s: Cannot use -trusted with -CAfile, -CApath or -CAstore\n",
204                    prog);
205         goto end;
206     }
207
208     if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
209                               CAstore, noCAstore)) == NULL)
210         goto end;
211     X509_STORE_set_verify_cb(store, cb);
212
213     if (vpmtouched)
214         X509_STORE_set1_param(store, vpm);
215
216     ERR_clear_error();
217
218     if (crl_download)
219         store_setup_crl_download(store);
220
221     ret = 0;
222     if (argc < 1) {
223         if (check(store, NULL, untrusted, trusted, crls, show_chain,
224                   vfyopts) != 1)
225             ret = -1;
226     } else {
227         for (i = 0; i < argc; i++)
228             if (check(store, argv[i], untrusted, trusted, crls, show_chain,
229                       vfyopts) != 1)
230                 ret = -1;
231     }
232
233  end:
234     X509_VERIFY_PARAM_free(vpm);
235     X509_STORE_free(store);
236     sk_X509_pop_free(untrusted, X509_free);
237     sk_X509_pop_free(trusted, X509_free);
238     sk_X509_CRL_pop_free(crls, X509_CRL_free);
239     sk_OPENSSL_STRING_free(vfyopts);
240     release_engine(e);
241     return (ret < 0 ? 2 : ret);
242 }
243
244 static int check(X509_STORE *ctx, const char *file,
245                  STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
246                  STACK_OF(X509_CRL) *crls, int show_chain,
247                  STACK_OF(OPENSSL_STRING) *opts)
248 {
249     X509 *x = NULL;
250     int i = 0, ret = 0;
251     X509_STORE_CTX *csc;
252     STACK_OF(X509) *chain = NULL;
253     int num_untrusted;
254
255     x = load_cert(file, FORMAT_PEM, "certificate file");
256     if (x == NULL)
257         goto end;
258
259     if (opts != NULL) {
260         for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) {
261             char *opt = sk_OPENSSL_STRING_value(opts, i);
262             if (x509_ctrl_string(x, opt) <= 0) {
263                 BIO_printf(bio_err, "parameter error \"%s\"\n", opt);
264                 ERR_print_errors(bio_err);
265                 return 0;
266             }
267         }
268     }
269
270     csc = X509_STORE_CTX_new();
271     if (csc == NULL) {
272         BIO_printf(bio_err, "error %s: X.509 store context allocation failed\n",
273                    (file == NULL) ? "stdin" : file);
274         goto end;
275     }
276
277     X509_STORE_set_flags(ctx, vflags);
278     if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
279         X509_STORE_CTX_free(csc);
280         BIO_printf(bio_err,
281                    "error %s: X.509 store context initialization failed\n",
282                    (file == NULL) ? "stdin" : file);
283         goto end;
284     }
285     if (tchain != NULL)
286         X509_STORE_CTX_set0_trusted_stack(csc, tchain);
287     if (crls != NULL)
288         X509_STORE_CTX_set0_crls(csc, crls);
289     i = X509_verify_cert(csc);
290     if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
291         BIO_printf(bio_out, "%s: OK\n", (file == NULL) ? "stdin" : file);
292         ret = 1;
293         if (show_chain) {
294             int j;
295
296             chain = X509_STORE_CTX_get1_chain(csc);
297             num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
298             BIO_printf(bio_out, "Chain:\n");
299             for (j = 0; j < sk_X509_num(chain); j++) {
300                 X509 *cert = sk_X509_value(chain, j);
301                 BIO_printf(bio_out, "depth=%d: ", j);
302                 X509_NAME_print_ex_fp(stdout,
303                                       X509_get_subject_name(cert),
304                                       0, get_nameopt());
305                 if (j < num_untrusted)
306                     BIO_printf(bio_out, " (untrusted)");
307                 BIO_printf(bio_out, "\n");
308             }
309             sk_X509_pop_free(chain, X509_free);
310         }
311     } else {
312         BIO_printf(bio_err,
313                    "error %s: verification failed\n",
314                    (file == NULL) ? "stdin" : file);
315     }
316     X509_STORE_CTX_free(csc);
317
318  end:
319     if (i <= 0)
320         ERR_print_errors(bio_err);
321     X509_free(x);
322
323     return ret;
324 }
325
326 static int cb(int ok, X509_STORE_CTX *ctx)
327 {
328     int cert_error = X509_STORE_CTX_get_error(ctx);
329     X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
330
331     if (!ok) {
332         if (current_cert != NULL) {
333             X509_NAME_print_ex(bio_err,
334                             X509_get_subject_name(current_cert),
335                             0, get_nameopt());
336             BIO_printf(bio_err, "\n");
337         }
338         BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
339                X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
340                cert_error,
341                X509_STORE_CTX_get_error_depth(ctx),
342                X509_verify_cert_error_string(cert_error));
343
344         /*
345          * Pretend that some errors are ok, so they don't stop further
346          * processing of the certificate chain.  Setting ok = 1 does this.
347          * After X509_verify_cert() is done, we verify that there were
348          * no actual errors, even if the returned value was positive.
349          */
350         switch (cert_error) {
351         case X509_V_ERR_NO_EXPLICIT_POLICY:
352             policies_print(ctx);
353             /* fall thru */
354         case X509_V_ERR_CERT_HAS_EXPIRED:
355             /* Continue even if the leaf is a self signed cert */
356         case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
357             /* Continue after extension errors too */
358         case X509_V_ERR_INVALID_CA:
359         case X509_V_ERR_INVALID_NON_CA:
360         case X509_V_ERR_PATH_LENGTH_EXCEEDED:
361         case X509_V_ERR_INVALID_PURPOSE:
362         case X509_V_ERR_CRL_HAS_EXPIRED:
363         case X509_V_ERR_CRL_NOT_YET_VALID:
364         case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
365             ok = 1;
366         }
367
368         return ok;
369
370     }
371     if (cert_error == X509_V_OK && ok == 2)
372         policies_print(ctx);
373     if (!v_verbose)
374         ERR_clear_error();
375     return ok;
376 }