Add -CAstore and similar to all openssl commands that have -CApath
[openssl.git] / apps / crl.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 typedef enum OPTION_choice {
22     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
23     OPT_INFORM, OPT_IN, OPT_OUTFORM, OPT_OUT, OPT_KEYFORM, OPT_KEY,
24     OPT_ISSUER, OPT_LASTUPDATE, OPT_NEXTUPDATE, OPT_FINGERPRINT,
25     OPT_CRLNUMBER, OPT_BADSIG, OPT_GENDELTA, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE,
26     OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE, OPT_VERIFY, OPT_TEXT, OPT_HASH,
27     OPT_HASH_OLD, OPT_NOOUT, OPT_NAMEOPT, OPT_MD
28 } OPTION_CHOICE;
29
30 const OPTIONS crl_options[] = {
31     {"help", OPT_HELP, '-', "Display this summary"},
32     {"inform", OPT_INFORM, 'F', "Input format; default PEM"},
33     {"in", OPT_IN, '<', "Input file - default stdin"},
34     {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
35     {"out", OPT_OUT, '>', "output file - default stdout"},
36     {"keyform", OPT_KEYFORM, 'F', "Private key file format (PEM or ENGINE)"},
37     {"key", OPT_KEY, '<', "CRL signing Private key to use"},
38     {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
39     {"lastupdate", OPT_LASTUPDATE, '-', "Set lastUpdate field"},
40     {"nextupdate", OPT_NEXTUPDATE, '-', "Set nextUpdate field"},
41     {"noout", OPT_NOOUT, '-', "No CRL output"},
42     {"fingerprint", OPT_FINGERPRINT, '-', "Print the crl fingerprint"},
43     {"crlnumber", OPT_CRLNUMBER, '-', "Print CRL number"},
44     {"badsig", OPT_BADSIG, '-', "Corrupt last byte of loaded CRL signature (for test)" },
45     {"gendelta", OPT_GENDELTA, '<', "Other CRL to compare/diff to the Input one"},
46     {"CApath", OPT_CAPATH, '/', "Verify CRL using certificates in dir"},
47     {"CAfile", OPT_CAFILE, '<', "Verify CRL using certificates in file name"},
48     {"CAstore", OPT_CASTORE, ':', "Verify CRL using certificates in store URI"},
49     {"no-CAfile", OPT_NOCAFILE, '-',
50      "Do not load the default certificates file"},
51     {"no-CApath", OPT_NOCAPATH, '-',
52      "Do not load certificates from the default certificates directory"},
53     {"no-CAstore", OPT_NOCASTORE, '-',
54      "Do not load certificates from the default certificates store"},
55     {"verify", OPT_VERIFY, '-', "Verify CRL signature"},
56     {"text", OPT_TEXT, '-', "Print out a text format version"},
57     {"hash", OPT_HASH, '-', "Print hash value"},
58     {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
59     {"", OPT_MD, '-', "Any supported digest"},
60 #ifndef OPENSSL_NO_MD5
61     {"hash_old", OPT_HASH_OLD, '-', "Print old-style (MD5) hash value"},
62 #endif
63     {NULL}
64 };
65
66 int crl_main(int argc, char **argv)
67 {
68     X509_CRL *x = NULL;
69     BIO *out = NULL;
70     X509_STORE *store = NULL;
71     X509_STORE_CTX *ctx = NULL;
72     X509_LOOKUP *lookup = NULL;
73     X509_OBJECT *xobj = NULL;
74     EVP_PKEY *pkey;
75     const EVP_MD *digest = EVP_sha1();
76     char *infile = NULL, *outfile = NULL, *crldiff = NULL, *keyfile = NULL;
77     const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog;
78     OPTION_CHOICE o;
79     int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout = 0;
80     int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;
81     int ret = 1, num = 0, badsig = 0, fingerprint = 0, crlnumber = 0;
82     int text = 0, do_ver = 0, noCAfile = 0, noCApath = 0, noCAstore = 0;
83     int i;
84 #ifndef OPENSSL_NO_MD5
85     int hash_old = 0;
86 #endif
87
88     prog = opt_init(argc, argv, crl_options);
89     while ((o = opt_next()) != OPT_EOF) {
90         switch (o) {
91         case OPT_EOF:
92         case OPT_ERR:
93  opthelp:
94             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
95             goto end;
96         case OPT_HELP:
97             opt_help(crl_options);
98             ret = 0;
99             goto end;
100         case OPT_INFORM:
101             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
102                 goto opthelp;
103             break;
104         case OPT_IN:
105             infile = opt_arg();
106             break;
107         case OPT_OUTFORM:
108             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
109                 goto opthelp;
110             break;
111         case OPT_OUT:
112             outfile = opt_arg();
113             break;
114         case OPT_KEYFORM:
115             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
116                 goto opthelp;
117             break;
118         case OPT_KEY:
119             keyfile = opt_arg();
120             break;
121         case OPT_GENDELTA:
122             crldiff = opt_arg();
123             break;
124         case OPT_CAPATH:
125             CApath = opt_arg();
126             do_ver = 1;
127             break;
128         case OPT_CAFILE:
129             CAfile = opt_arg();
130             do_ver = 1;
131             break;
132         case OPT_CASTORE:
133             CAstore = opt_arg();
134             do_ver = 1;
135             break;
136         case OPT_NOCAPATH:
137             noCApath =  1;
138             break;
139         case OPT_NOCAFILE:
140             noCAfile =  1;
141             break;
142         case OPT_NOCASTORE:
143             noCAstore =  1;
144             break;
145         case OPT_HASH_OLD:
146 #ifndef OPENSSL_NO_MD5
147             hash_old = ++num;
148 #endif
149             break;
150         case OPT_VERIFY:
151             do_ver = 1;
152             break;
153         case OPT_TEXT:
154             text = 1;
155             break;
156         case OPT_HASH:
157             hash = ++num;
158             break;
159         case OPT_ISSUER:
160             issuer = ++num;
161             break;
162         case OPT_LASTUPDATE:
163             lastupdate = ++num;
164             break;
165         case OPT_NEXTUPDATE:
166             nextupdate = ++num;
167             break;
168         case OPT_NOOUT:
169             noout = ++num;
170             break;
171         case OPT_FINGERPRINT:
172             fingerprint = ++num;
173             break;
174         case OPT_CRLNUMBER:
175             crlnumber = ++num;
176             break;
177         case OPT_BADSIG:
178             badsig = 1;
179             break;
180         case OPT_NAMEOPT:
181             if (!set_nameopt(opt_arg()))
182                 goto opthelp;
183             break;
184         case OPT_MD:
185             if (!opt_md(opt_unknown(), &digest))
186                 goto opthelp;
187         }
188     }
189     argc = opt_num_rest();
190     if (argc != 0)
191         goto opthelp;
192
193     x = load_crl(infile, informat);
194     if (x == NULL)
195         goto end;
196
197     if (do_ver) {
198         if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
199                                   CAstore, noCAstore)) == NULL)
200             goto end;
201         lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
202         if (lookup == NULL)
203             goto end;
204         ctx = X509_STORE_CTX_new();
205         if (ctx == NULL || !X509_STORE_CTX_init(ctx, store, NULL, NULL)) {
206             BIO_printf(bio_err, "Error initialising X509 store\n");
207             goto end;
208         }
209
210         xobj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509,
211                                                  X509_CRL_get_issuer(x));
212         if (xobj == NULL) {
213             BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
214             goto end;
215         }
216         pkey = X509_get_pubkey(X509_OBJECT_get0_X509(xobj));
217         X509_OBJECT_free(xobj);
218         if (pkey == NULL) {
219             BIO_printf(bio_err, "Error getting CRL issuer public key\n");
220             goto end;
221         }
222         i = X509_CRL_verify(x, pkey);
223         EVP_PKEY_free(pkey);
224         if (i < 0)
225             goto end;
226         if (i == 0)
227             BIO_printf(bio_err, "verify failure\n");
228         else
229             BIO_printf(bio_err, "verify OK\n");
230     }
231
232     if (crldiff) {
233         X509_CRL *newcrl, *delta;
234         if (!keyfile) {
235             BIO_puts(bio_err, "Missing CRL signing key\n");
236             goto end;
237         }
238         newcrl = load_crl(crldiff, informat);
239         if (!newcrl)
240             goto end;
241         pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
242         if (pkey == NULL) {
243             X509_CRL_free(newcrl);
244             goto end;
245         }
246         delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
247         X509_CRL_free(newcrl);
248         EVP_PKEY_free(pkey);
249         if (delta) {
250             X509_CRL_free(x);
251             x = delta;
252         } else {
253             BIO_puts(bio_err, "Error creating delta CRL\n");
254             goto end;
255         }
256     }
257
258     if (badsig) {
259         const ASN1_BIT_STRING *sig;
260
261         X509_CRL_get0_signature(x, &sig, NULL);
262         corrupt_signature(sig);
263     }
264
265     if (num) {
266         for (i = 1; i <= num; i++) {
267             if (issuer == i) {
268                 print_name(bio_out, "issuer=", X509_CRL_get_issuer(x),
269                            get_nameopt());
270             }
271             if (crlnumber == i) {
272                 ASN1_INTEGER *crlnum;
273                 crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
274                 BIO_printf(bio_out, "crlNumber=");
275                 if (crlnum) {
276                     i2a_ASN1_INTEGER(bio_out, crlnum);
277                     ASN1_INTEGER_free(crlnum);
278                 } else
279                     BIO_puts(bio_out, "<NONE>");
280                 BIO_printf(bio_out, "\n");
281             }
282             if (hash == i) {
283                 BIO_printf(bio_out, "%08lx\n",
284                            X509_NAME_hash(X509_CRL_get_issuer(x)));
285             }
286 #ifndef OPENSSL_NO_MD5
287             if (hash_old == i) {
288                 BIO_printf(bio_out, "%08lx\n",
289                            X509_NAME_hash_old(X509_CRL_get_issuer(x)));
290             }
291 #endif
292             if (lastupdate == i) {
293                 BIO_printf(bio_out, "lastUpdate=");
294                 ASN1_TIME_print(bio_out, X509_CRL_get0_lastUpdate(x));
295                 BIO_printf(bio_out, "\n");
296             }
297             if (nextupdate == i) {
298                 BIO_printf(bio_out, "nextUpdate=");
299                 if (X509_CRL_get0_nextUpdate(x))
300                     ASN1_TIME_print(bio_out, X509_CRL_get0_nextUpdate(x));
301                 else
302                     BIO_printf(bio_out, "NONE");
303                 BIO_printf(bio_out, "\n");
304             }
305             if (fingerprint == i) {
306                 int j;
307                 unsigned int n;
308                 unsigned char md[EVP_MAX_MD_SIZE];
309
310                 if (!X509_CRL_digest(x, digest, md, &n)) {
311                     BIO_printf(bio_err, "out of memory\n");
312                     goto end;
313                 }
314                 BIO_printf(bio_out, "%s Fingerprint=",
315                            OBJ_nid2sn(EVP_MD_type(digest)));
316                 for (j = 0; j < (int)n; j++) {
317                     BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
318                                ? '\n' : ':');
319                 }
320             }
321         }
322     }
323     out = bio_open_default(outfile, 'w', outformat);
324     if (out == NULL)
325         goto end;
326
327     if (text)
328         X509_CRL_print_ex(out, x, get_nameopt());
329
330     if (noout) {
331         ret = 0;
332         goto end;
333     }
334
335     if (outformat == FORMAT_ASN1)
336         i = (int)i2d_X509_CRL_bio(out, x);
337     else
338         i = PEM_write_bio_X509_CRL(out, x);
339     if (!i) {
340         BIO_printf(bio_err, "unable to write CRL\n");
341         goto end;
342     }
343     ret = 0;
344
345  end:
346     if (ret != 0)
347         ERR_print_errors(bio_err);
348     BIO_free_all(out);
349     X509_CRL_free(x);
350     X509_STORE_CTX_free(ctx);
351     X509_STORE_free(store);
352     return ret;
353 }