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