xref: /freebsd/crypto/openssl/apps/verify.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery  * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
874664626SKris Kennaway  */
974664626SKris Kennaway 
1074664626SKris Kennaway #include <stdio.h>
1174664626SKris Kennaway #include <stdlib.h>
1274664626SKris Kennaway #include <string.h>
1374664626SKris Kennaway #include "apps.h"
14e71b7053SJung-uk Kim #include "progs.h"
1574664626SKris Kennaway #include <openssl/bio.h>
1674664626SKris Kennaway #include <openssl/err.h>
1774664626SKris Kennaway #include <openssl/x509.h>
18f579bf8eSKris Kennaway #include <openssl/x509v3.h>
1974664626SKris Kennaway #include <openssl/pem.h>
2074664626SKris Kennaway 
21e71b7053SJung-uk Kim static int cb(int ok, X509_STORE_CTX *ctx);
22e71b7053SJung-uk Kim static int check(X509_STORE *ctx, const char *file,
231f13597dSJung-uk Kim                  STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
24*b077aed3SPierre Pronchery                  STACK_OF(X509_CRL) *crls, int show_chain,
25*b077aed3SPierre Pronchery                  STACK_OF(OPENSSL_STRING) *opts);
265c87c606SMark Murray static int v_verbose = 0, vflags = 0;
2774664626SKris Kennaway 
28e71b7053SJung-uk Kim typedef enum OPTION_choice {
29*b077aed3SPierre Pronchery     OPT_COMMON,
30*b077aed3SPierre Pronchery     OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE,
31*b077aed3SPierre Pronchery     OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE,
32e71b7053SJung-uk Kim     OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN,
33*b077aed3SPierre Pronchery     OPT_V_ENUM, OPT_NAMEOPT, OPT_VFYOPT,
34*b077aed3SPierre Pronchery     OPT_VERBOSE,
35*b077aed3SPierre Pronchery     OPT_PROV_ENUM
36e71b7053SJung-uk Kim } OPTION_CHOICE;
37f579bf8eSKris Kennaway 
38e71b7053SJung-uk Kim const OPTIONS verify_options[] = {
39*b077aed3SPierre Pronchery     {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n"},
40*b077aed3SPierre Pronchery 
41*b077aed3SPierre Pronchery     OPT_SECTION("General"),
42e71b7053SJung-uk Kim     {"help", OPT_HELP, '-', "Display this summary"},
43e71b7053SJung-uk Kim #ifndef OPENSSL_NO_ENGINE
44e71b7053SJung-uk Kim     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
45e71b7053SJung-uk Kim #endif
46*b077aed3SPierre Pronchery     {"verbose", OPT_VERBOSE, '-',
47*b077aed3SPierre Pronchery         "Print extra information about the operations being performed."},
48*b077aed3SPierre Pronchery     {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
49*b077aed3SPierre Pronchery 
50*b077aed3SPierre Pronchery     OPT_SECTION("Certificate chain"),
51*b077aed3SPierre Pronchery     {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
52*b077aed3SPierre Pronchery     {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"},
53*b077aed3SPierre Pronchery     {"CApath", OPT_CAPATH, '/', "A directory of files with trusted certificates"},
54*b077aed3SPierre Pronchery     {"CAstore", OPT_CASTORE, ':', "URI to a store of trusted certificates"},
55*b077aed3SPierre Pronchery     {"no-CAfile", OPT_NOCAFILE, '-',
56*b077aed3SPierre Pronchery      "Do not load the default trusted certificates file"},
57*b077aed3SPierre Pronchery     {"no-CApath", OPT_NOCAPATH, '-',
58*b077aed3SPierre Pronchery      "Do not load trusted certificates from the default directory"},
59*b077aed3SPierre Pronchery     {"no-CAstore", OPT_NOCASTORE, '-',
60*b077aed3SPierre Pronchery      "Do not load trusted certificates from the default certificates store"},
61*b077aed3SPierre Pronchery     {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
62*b077aed3SPierre Pronchery     {"CRLfile", OPT_CRLFILE, '<',
63*b077aed3SPierre Pronchery         "File containing one or more CRL's (in PEM format) to load"},
64*b077aed3SPierre Pronchery     {"crl_download", OPT_CRL_DOWNLOAD, '-',
65*b077aed3SPierre Pronchery         "Try downloading CRL information for certificates via their CDP entries"},
66*b077aed3SPierre Pronchery     {"show_chain", OPT_SHOW_CHAIN, '-',
67*b077aed3SPierre Pronchery         "Display information about the certificate chain"},
68*b077aed3SPierre Pronchery 
69*b077aed3SPierre Pronchery     OPT_V_OPTIONS,
70*b077aed3SPierre Pronchery     {"vfyopt", OPT_VFYOPT, 's', "Verification parameter in n:v form"},
71*b077aed3SPierre Pronchery 
72*b077aed3SPierre Pronchery     OPT_PROV_OPTIONS,
73*b077aed3SPierre Pronchery 
74*b077aed3SPierre Pronchery     OPT_PARAMETERS(),
75*b077aed3SPierre Pronchery     {"cert", 0, 0, "Certificate(s) to verify (optional; stdin used otherwise)"},
76e71b7053SJung-uk Kim     {NULL}
77e71b7053SJung-uk Kim };
78e71b7053SJung-uk Kim 
verify_main(int argc,char ** argv)79e71b7053SJung-uk Kim int verify_main(int argc, char **argv)
8074664626SKris Kennaway {
815c87c606SMark Murray     ENGINE *e = NULL;
82ddd58736SKris Kennaway     STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
831f13597dSJung-uk Kim     STACK_OF(X509_CRL) *crls = NULL;
84*b077aed3SPierre Pronchery     STACK_OF(OPENSSL_STRING) *vfyopts = NULL;
85e71b7053SJung-uk Kim     X509_STORE *store = NULL;
863b4e3dcbSSimon L. B. Nielsen     X509_VERIFY_PARAM *vpm = NULL;
87*b077aed3SPierre Pronchery     const char *prog, *CApath = NULL, *CAfile = NULL, *CAstore = NULL;
88*b077aed3SPierre Pronchery     int noCApath = 0, noCAfile = 0, noCAstore = 0;
89e71b7053SJung-uk Kim     int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
90e71b7053SJung-uk Kim     OPTION_CHOICE o;
9174664626SKris Kennaway 
92e71b7053SJung-uk Kim     if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
935c87c606SMark Murray         goto end;
945c87c606SMark Murray 
95e71b7053SJung-uk Kim     prog = opt_init(argc, argv, verify_options);
96e71b7053SJung-uk Kim     while ((o = opt_next()) != OPT_EOF) {
97e71b7053SJung-uk Kim         switch (o) {
98e71b7053SJung-uk Kim         case OPT_EOF:
99e71b7053SJung-uk Kim         case OPT_ERR:
100*b077aed3SPierre Pronchery  opthelp:
101e71b7053SJung-uk Kim             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
102f579bf8eSKris Kennaway             goto end;
103e71b7053SJung-uk Kim         case OPT_HELP:
104e71b7053SJung-uk Kim             opt_help(verify_options);
105*b077aed3SPierre Pronchery             BIO_printf(bio_err, "\nRecognized certificate chain purposes:\n");
1066f9291ceSJung-uk Kim             for (i = 0; i < X509_PURPOSE_get_count(); i++) {
107*b077aed3SPierre Pronchery                 X509_PURPOSE *ptmp = X509_PURPOSE_get0(i);
108*b077aed3SPierre Pronchery 
109*b077aed3SPierre Pronchery                 BIO_printf(bio_err, "  %-15s  %s\n",
11009286989SJung-uk Kim                         X509_PURPOSE_get0_sname(ptmp),
111f579bf8eSKris Kennaway                         X509_PURPOSE_get0_name(ptmp));
112f579bf8eSKris Kennaway             }
113e71b7053SJung-uk Kim 
114*b077aed3SPierre Pronchery             BIO_printf(bio_err, "Recognized certificate policy names:\n");
115e71b7053SJung-uk Kim             for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
116*b077aed3SPierre Pronchery                 const X509_VERIFY_PARAM *vptmp = X509_VERIFY_PARAM_get0(i);
117*b077aed3SPierre Pronchery 
118*b077aed3SPierre Pronchery                 BIO_printf(bio_err, "  %s\n",
119e71b7053SJung-uk Kim                         X509_VERIFY_PARAM_get0_name(vptmp));
120f579bf8eSKris Kennaway             }
121e71b7053SJung-uk Kim             ret = 0;
122e71b7053SJung-uk Kim             goto end;
123e71b7053SJung-uk Kim         case OPT_V_CASES:
124e71b7053SJung-uk Kim             if (!opt_verify(o, vpm))
125e71b7053SJung-uk Kim                 goto end;
126e71b7053SJung-uk Kim             vpmtouched++;
127e71b7053SJung-uk Kim             break;
128e71b7053SJung-uk Kim         case OPT_CAPATH:
129e71b7053SJung-uk Kim             CApath = opt_arg();
130e71b7053SJung-uk Kim             break;
131e71b7053SJung-uk Kim         case OPT_CAFILE:
132e71b7053SJung-uk Kim             CAfile = opt_arg();
133e71b7053SJung-uk Kim             break;
134*b077aed3SPierre Pronchery         case OPT_CASTORE:
135*b077aed3SPierre Pronchery             CAstore = opt_arg();
136*b077aed3SPierre Pronchery             break;
137e71b7053SJung-uk Kim         case OPT_NOCAPATH:
138e71b7053SJung-uk Kim             noCApath = 1;
139e71b7053SJung-uk Kim             break;
140e71b7053SJung-uk Kim         case OPT_NOCAFILE:
141e71b7053SJung-uk Kim             noCAfile = 1;
142e71b7053SJung-uk Kim             break;
143*b077aed3SPierre Pronchery         case OPT_NOCASTORE:
144*b077aed3SPierre Pronchery             noCAstore = 1;
145*b077aed3SPierre Pronchery             break;
146e71b7053SJung-uk Kim         case OPT_UNTRUSTED:
147e71b7053SJung-uk Kim             /* Zero or more times */
148*b077aed3SPierre Pronchery             if (!load_certs(opt_arg(), 0, &untrusted, NULL,
149e71b7053SJung-uk Kim                             "untrusted certificates"))
150e71b7053SJung-uk Kim                 goto end;
151e71b7053SJung-uk Kim             break;
152e71b7053SJung-uk Kim         case OPT_TRUSTED:
153e71b7053SJung-uk Kim             /* Zero or more times */
154e71b7053SJung-uk Kim             noCAfile = 1;
155e71b7053SJung-uk Kim             noCApath = 1;
156*b077aed3SPierre Pronchery             noCAstore = 1;
157*b077aed3SPierre Pronchery             if (!load_certs(opt_arg(), 0, &trusted, NULL, "trusted certificates"))
158e71b7053SJung-uk Kim                 goto end;
159e71b7053SJung-uk Kim             break;
160e71b7053SJung-uk Kim         case OPT_CRLFILE:
161e71b7053SJung-uk Kim             /* Zero or more times */
162*b077aed3SPierre Pronchery             if (!load_crls(opt_arg(), &crls, NULL, "other CRLs"))
163e71b7053SJung-uk Kim                 goto end;
164e71b7053SJung-uk Kim             break;
165e71b7053SJung-uk Kim         case OPT_CRL_DOWNLOAD:
166e71b7053SJung-uk Kim             crl_download = 1;
167e71b7053SJung-uk Kim             break;
168e71b7053SJung-uk Kim         case OPT_ENGINE:
169e71b7053SJung-uk Kim             if ((e = setup_engine(opt_arg(), 0)) == NULL) {
170e71b7053SJung-uk Kim                 /* Failure message already displayed */
171e71b7053SJung-uk Kim                 goto end;
172e71b7053SJung-uk Kim             }
173e71b7053SJung-uk Kim             break;
174e71b7053SJung-uk Kim         case OPT_SHOW_CHAIN:
175e71b7053SJung-uk Kim             show_chain = 1;
176e71b7053SJung-uk Kim             break;
177e71b7053SJung-uk Kim         case OPT_NAMEOPT:
178e71b7053SJung-uk Kim             if (!set_nameopt(opt_arg()))
179e71b7053SJung-uk Kim                 goto end;
180e71b7053SJung-uk Kim             break;
181*b077aed3SPierre Pronchery         case OPT_VFYOPT:
182*b077aed3SPierre Pronchery             if (!vfyopts)
183*b077aed3SPierre Pronchery                 vfyopts = sk_OPENSSL_STRING_new_null();
184*b077aed3SPierre Pronchery             if (!vfyopts || !sk_OPENSSL_STRING_push(vfyopts, opt_arg()))
185*b077aed3SPierre Pronchery                 goto opthelp;
186*b077aed3SPierre Pronchery             break;
187e71b7053SJung-uk Kim         case OPT_VERBOSE:
188e71b7053SJung-uk Kim             v_verbose = 1;
189e71b7053SJung-uk Kim             break;
190*b077aed3SPierre Pronchery         case OPT_PROV_CASES:
191*b077aed3SPierre Pronchery             if (!opt_provider(o))
192*b077aed3SPierre Pronchery                 goto end;
193*b077aed3SPierre Pronchery             break;
194e71b7053SJung-uk Kim         }
195e71b7053SJung-uk Kim     }
196*b077aed3SPierre Pronchery 
197*b077aed3SPierre Pronchery     /* Extra arguments are certificates to verify. */
198e71b7053SJung-uk Kim     argc = opt_num_rest();
199e71b7053SJung-uk Kim     argv = opt_rest();
200*b077aed3SPierre Pronchery 
201*b077aed3SPierre Pronchery     if (trusted != NULL
202*b077aed3SPierre Pronchery         && (CAfile != NULL || CApath != NULL || CAstore != NULL)) {
203e71b7053SJung-uk Kim         BIO_printf(bio_err,
204*b077aed3SPierre Pronchery                    "%s: Cannot use -trusted with -CAfile, -CApath or -CAstore\n",
205e71b7053SJung-uk Kim                    prog);
206e71b7053SJung-uk Kim         goto end;
207e71b7053SJung-uk Kim     }
208e71b7053SJung-uk Kim 
209*b077aed3SPierre Pronchery     if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
210*b077aed3SPierre Pronchery                               CAstore, noCAstore)) == NULL)
211e71b7053SJung-uk Kim         goto end;
212e71b7053SJung-uk Kim     X509_STORE_set_verify_cb(store, cb);
213e71b7053SJung-uk Kim 
214e71b7053SJung-uk Kim     if (vpmtouched)
215e71b7053SJung-uk Kim         X509_STORE_set1_param(store, vpm);
216e71b7053SJung-uk Kim 
217e71b7053SJung-uk Kim     ERR_clear_error();
218e71b7053SJung-uk Kim 
219e71b7053SJung-uk Kim     if (crl_download)
220e71b7053SJung-uk Kim         store_setup_crl_download(store);
221e71b7053SJung-uk Kim 
222e71b7053SJung-uk Kim     ret = 0;
223e71b7053SJung-uk Kim     if (argc < 1) {
224*b077aed3SPierre Pronchery         if (check(store, NULL, untrusted, trusted, crls, show_chain,
225*b077aed3SPierre Pronchery                   vfyopts) != 1)
226e71b7053SJung-uk Kim             ret = -1;
227e71b7053SJung-uk Kim     } else {
228e71b7053SJung-uk Kim         for (i = 0; i < argc; i++)
229*b077aed3SPierre Pronchery             if (check(store, argv[i], untrusted, trusted, crls, show_chain,
230*b077aed3SPierre Pronchery                       vfyopts) != 1)
231e71b7053SJung-uk Kim                 ret = -1;
232e71b7053SJung-uk Kim     }
233e71b7053SJung-uk Kim 
234aeb5019cSJung-uk Kim  end:
2356f9291ceSJung-uk Kim     X509_VERIFY_PARAM_free(vpm);
236e71b7053SJung-uk Kim     X509_STORE_free(store);
237f579bf8eSKris Kennaway     sk_X509_pop_free(untrusted, X509_free);
238ddd58736SKris Kennaway     sk_X509_pop_free(trusted, X509_free);
2391f13597dSJung-uk Kim     sk_X509_CRL_pop_free(crls, X509_CRL_free);
240*b077aed3SPierre Pronchery     sk_OPENSSL_STRING_free(vfyopts);
2416cf8931aSJung-uk Kim     release_engine(e);
242e71b7053SJung-uk Kim     return (ret < 0 ? 2 : ret);
24374664626SKris Kennaway }
24474664626SKris Kennaway 
check(X509_STORE * ctx,const char * file,STACK_OF (X509)* uchain,STACK_OF (X509)* tchain,STACK_OF (X509_CRL)* crls,int show_chain,STACK_OF (OPENSSL_STRING)* opts)245e71b7053SJung-uk Kim static int check(X509_STORE *ctx, const char *file,
2461f13597dSJung-uk Kim                  STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
247*b077aed3SPierre Pronchery                  STACK_OF(X509_CRL) *crls, int show_chain,
248*b077aed3SPierre Pronchery                  STACK_OF(OPENSSL_STRING) *opts)
24974664626SKris Kennaway {
25074664626SKris Kennaway     X509 *x = NULL;
25174664626SKris Kennaway     int i = 0, ret = 0;
252f579bf8eSKris Kennaway     X509_STORE_CTX *csc;
253e71b7053SJung-uk Kim     STACK_OF(X509) *chain = NULL;
254e71b7053SJung-uk Kim     int num_untrusted;
25574664626SKris Kennaway 
256*b077aed3SPierre Pronchery     x = load_cert(file, FORMAT_UNDEF, "certificate file");
25774664626SKris Kennaway     if (x == NULL)
25874664626SKris Kennaway         goto end;
25974664626SKris Kennaway 
260*b077aed3SPierre Pronchery     if (opts != NULL) {
261*b077aed3SPierre Pronchery         for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) {
262*b077aed3SPierre Pronchery             char *opt = sk_OPENSSL_STRING_value(opts, i);
263*b077aed3SPierre Pronchery             if (x509_ctrl_string(x, opt) <= 0) {
264*b077aed3SPierre Pronchery                 BIO_printf(bio_err, "parameter error \"%s\"\n", opt);
265*b077aed3SPierre Pronchery                 ERR_print_errors(bio_err);
266*b077aed3SPierre Pronchery                 X509_free(x);
267*b077aed3SPierre Pronchery                 return 0;
268*b077aed3SPierre Pronchery             }
269*b077aed3SPierre Pronchery         }
270*b077aed3SPierre Pronchery     }
271*b077aed3SPierre Pronchery 
272f579bf8eSKris Kennaway     csc = X509_STORE_CTX_new();
2736f9291ceSJung-uk Kim     if (csc == NULL) {
274*b077aed3SPierre Pronchery         BIO_printf(bio_err, "error %s: X.509 store context allocation failed\n",
275e71b7053SJung-uk Kim                    (file == NULL) ? "stdin" : file);
276f579bf8eSKris Kennaway         goto end;
277f579bf8eSKris Kennaway     }
278e71b7053SJung-uk Kim 
2795c87c606SMark Murray     X509_STORE_set_flags(ctx, vflags);
2806f9291ceSJung-uk Kim     if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
281dea77ea6SJung-uk Kim         X509_STORE_CTX_free(csc);
282*b077aed3SPierre Pronchery         BIO_printf(bio_err,
283*b077aed3SPierre Pronchery                    "error %s: X.509 store context initialization failed\n",
284e71b7053SJung-uk Kim                    (file == NULL) ? "stdin" : file);
2855c87c606SMark Murray         goto end;
2865c87c606SMark Murray     }
287e71b7053SJung-uk Kim     if (tchain != NULL)
288e71b7053SJung-uk Kim         X509_STORE_CTX_set0_trusted_stack(csc, tchain);
289e71b7053SJung-uk Kim     if (crls != NULL)
2901f13597dSJung-uk Kim         X509_STORE_CTX_set0_crls(csc, crls);
291f579bf8eSKris Kennaway     i = X509_verify_cert(csc);
292e71b7053SJung-uk Kim     if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
293*b077aed3SPierre Pronchery         BIO_printf(bio_out, "%s: OK\n", (file == NULL) ? "stdin" : file);
294e71b7053SJung-uk Kim         ret = 1;
295e71b7053SJung-uk Kim         if (show_chain) {
296e71b7053SJung-uk Kim             int j;
297e71b7053SJung-uk Kim 
298e71b7053SJung-uk Kim             chain = X509_STORE_CTX_get1_chain(csc);
299e71b7053SJung-uk Kim             num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
300*b077aed3SPierre Pronchery             BIO_printf(bio_out, "Chain:\n");
301e71b7053SJung-uk Kim             for (j = 0; j < sk_X509_num(chain); j++) {
302e71b7053SJung-uk Kim                 X509 *cert = sk_X509_value(chain, j);
303*b077aed3SPierre Pronchery                 BIO_printf(bio_out, "depth=%d: ", j);
304e71b7053SJung-uk Kim                 X509_NAME_print_ex_fp(stdout,
305e71b7053SJung-uk Kim                                       X509_get_subject_name(cert),
306e71b7053SJung-uk Kim                                       0, get_nameopt());
307e71b7053SJung-uk Kim                 if (j < num_untrusted)
308*b077aed3SPierre Pronchery                     BIO_printf(bio_out, " (untrusted)");
309*b077aed3SPierre Pronchery                 BIO_printf(bio_out, "\n");
310e71b7053SJung-uk Kim             }
311e71b7053SJung-uk Kim             sk_X509_pop_free(chain, X509_free);
312e71b7053SJung-uk Kim         }
313e71b7053SJung-uk Kim     } else {
314*b077aed3SPierre Pronchery         BIO_printf(bio_err,
315*b077aed3SPierre Pronchery                    "error %s: verification failed\n",
316*b077aed3SPierre Pronchery                    (file == NULL) ? "stdin" : file);
317e71b7053SJung-uk Kim     }
318f579bf8eSKris Kennaway     X509_STORE_CTX_free(csc);
31974664626SKris Kennaway 
32074664626SKris Kennaway  end:
321e71b7053SJung-uk Kim     if (i <= 0)
32274664626SKris Kennaway         ERR_print_errors(bio_err);
3236f9291ceSJung-uk Kim     X509_free(x);
32474664626SKris Kennaway 
325e71b7053SJung-uk Kim     return ret;
32674664626SKris Kennaway }
32774664626SKris Kennaway 
cb(int ok,X509_STORE_CTX * ctx)328e71b7053SJung-uk Kim static int cb(int ok, X509_STORE_CTX *ctx)
32974664626SKris Kennaway {
3301f13597dSJung-uk Kim     int cert_error = X509_STORE_CTX_get_error(ctx);
3311f13597dSJung-uk Kim     X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
33274664626SKris Kennaway 
3336f9291ceSJung-uk Kim     if (!ok) {
334e71b7053SJung-uk Kim         if (current_cert != NULL) {
335e71b7053SJung-uk Kim             X509_NAME_print_ex(bio_err,
3361f13597dSJung-uk Kim                             X509_get_subject_name(current_cert),
337e71b7053SJung-uk Kim                             0, get_nameopt());
338e71b7053SJung-uk Kim             BIO_printf(bio_err, "\n");
3393b4e3dcbSSimon L. B. Nielsen         }
340e71b7053SJung-uk Kim         BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
3411f13597dSJung-uk Kim                X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
3421f13597dSJung-uk Kim                cert_error,
3431f13597dSJung-uk Kim                X509_STORE_CTX_get_error_depth(ctx),
3441f13597dSJung-uk Kim                X509_verify_cert_error_string(cert_error));
3456935a639SJung-uk Kim 
3466935a639SJung-uk Kim         /*
3476935a639SJung-uk Kim          * Pretend that some errors are ok, so they don't stop further
3486935a639SJung-uk Kim          * processing of the certificate chain.  Setting ok = 1 does this.
3496935a639SJung-uk Kim          * After X509_verify_cert() is done, we verify that there were
3506935a639SJung-uk Kim          * no actual errors, even if the returned value was positive.
3516935a639SJung-uk Kim          */
3526f9291ceSJung-uk Kim         switch (cert_error) {
3531f13597dSJung-uk Kim         case X509_V_ERR_NO_EXPLICIT_POLICY:
354e71b7053SJung-uk Kim             policies_print(ctx);
355e71b7053SJung-uk Kim             /* fall thru */
3561f13597dSJung-uk Kim         case X509_V_ERR_CERT_HAS_EXPIRED:
357*b077aed3SPierre Pronchery             /* Continue even if the leaf is a self-signed cert */
3581f13597dSJung-uk Kim         case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
3591f13597dSJung-uk Kim             /* Continue after extension errors too */
3601f13597dSJung-uk Kim         case X509_V_ERR_INVALID_CA:
3611f13597dSJung-uk Kim         case X509_V_ERR_INVALID_NON_CA:
3621f13597dSJung-uk Kim         case X509_V_ERR_PATH_LENGTH_EXCEEDED:
3631f13597dSJung-uk Kim         case X509_V_ERR_CRL_HAS_EXPIRED:
3641f13597dSJung-uk Kim         case X509_V_ERR_CRL_NOT_YET_VALID:
3651f13597dSJung-uk Kim         case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
366*b077aed3SPierre Pronchery             /* errors due to strict conformance checking (-x509_strict) */
367*b077aed3SPierre Pronchery         case X509_V_ERR_INVALID_PURPOSE:
368*b077aed3SPierre Pronchery         case X509_V_ERR_PATHLEN_INVALID_FOR_NON_CA:
369*b077aed3SPierre Pronchery         case X509_V_ERR_PATHLEN_WITHOUT_KU_KEY_CERT_SIGN:
370*b077aed3SPierre Pronchery         case X509_V_ERR_CA_BCONS_NOT_CRITICAL:
371*b077aed3SPierre Pronchery         case X509_V_ERR_CA_CERT_MISSING_KEY_USAGE:
372*b077aed3SPierre Pronchery         case X509_V_ERR_KU_KEY_CERT_SIGN_INVALID_FOR_NON_CA:
373*b077aed3SPierre Pronchery         case X509_V_ERR_ISSUER_NAME_EMPTY:
374*b077aed3SPierre Pronchery         case X509_V_ERR_SUBJECT_NAME_EMPTY:
375*b077aed3SPierre Pronchery         case X509_V_ERR_EMPTY_SUBJECT_SAN_NOT_CRITICAL:
376*b077aed3SPierre Pronchery         case X509_V_ERR_EMPTY_SUBJECT_ALT_NAME:
377*b077aed3SPierre Pronchery         case X509_V_ERR_SIGNATURE_ALGORITHM_INCONSISTENCY:
378*b077aed3SPierre Pronchery         case X509_V_ERR_AUTHORITY_KEY_IDENTIFIER_CRITICAL:
379*b077aed3SPierre Pronchery         case X509_V_ERR_SUBJECT_KEY_IDENTIFIER_CRITICAL:
380*b077aed3SPierre Pronchery         case X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER:
381*b077aed3SPierre Pronchery         case X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER:
382*b077aed3SPierre Pronchery         case X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3:
3831f13597dSJung-uk Kim             ok = 1;
3841f13597dSJung-uk Kim         }
3853b4e3dcbSSimon L. B. Nielsen         return ok;
3863b4e3dcbSSimon L. B. Nielsen 
38774664626SKris Kennaway     }
3881f13597dSJung-uk Kim     if (cert_error == X509_V_OK && ok == 2)
389e71b7053SJung-uk Kim         policies_print(ctx);
39074664626SKris Kennaway     if (!v_verbose)
39174664626SKris Kennaway         ERR_clear_error();
392e71b7053SJung-uk Kim     return ok;
39374664626SKris Kennaway }
394