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