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