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