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