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