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