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