Centralise loading default apps config file
[openssl.git] / apps / crl.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include "apps.h"
62 #include <openssl/bio.h>
63 #include <openssl/err.h>
64 #include <openssl/x509.h>
65 #include <openssl/x509v3.h>
66 #include <openssl/pem.h>
67
68 typedef enum OPTION_choice {
69     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
70     OPT_INFORM, OPT_IN, OPT_OUTFORM, OPT_OUT, OPT_KEYFORM, OPT_KEY,
71     OPT_ISSUER, OPT_LASTUPDATE, OPT_NEXTUPDATE, OPT_FINGERPRINT,
72     OPT_CRLNUMBER, OPT_BADSIG, OPT_GENDELTA, OPT_CAPATH, OPT_CAFILE,
73     OPT_NOCAPATH, OPT_NOCAFILE, OPT_VERIFY, OPT_TEXT, OPT_HASH, OPT_HASH_OLD,
74     OPT_NOOUT, OPT_NAMEOPT, OPT_MD
75 } OPTION_CHOICE;
76
77 OPTIONS crl_options[] = {
78     {"help", OPT_HELP, '-', "Display this summary"},
79     {"inform", OPT_INFORM, 'F', "Input format; default PEM"},
80     {"in", OPT_IN, '<', "Input file - default stdin"},
81     {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
82     {"out", OPT_OUT, '>', "output file - default stdout"},
83     {"keyform", OPT_KEYFORM, 'F'},
84     {"key", OPT_KEY, '<'},
85     {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
86     {"lastupdate", OPT_LASTUPDATE, '-', "Set lastUpdate field"},
87     {"nextupdate", OPT_NEXTUPDATE, '-', "Set nextUpdate field"},
88     {"noout", OPT_NOOUT, '-', "No CRL output"},
89     {"fingerprint", OPT_FINGERPRINT, '-', "Print the crl fingerprint"},
90     {"crlnumber", OPT_CRLNUMBER, '-', "Print CRL number"},
91     {"badsig", OPT_BADSIG, '-'},
92     {"gendelta", OPT_GENDELTA, '<'},
93     {"CApath", OPT_CAPATH, '/', "Verify CRL using certificates in dir"},
94     {"CAfile", OPT_CAFILE, '<', "Verify CRL using certificates in file name"},
95     {"no-CAfile", OPT_NOCAFILE, '-',
96      "Do not load the default certificates file"},
97     {"no-CApath", OPT_NOCAPATH, '-',
98      "Do not load certificates from the default certificates directory"},
99     {"verify", OPT_VERIFY, '-'},
100     {"text", OPT_TEXT, '-', "Print out a text format version"},
101     {"hash", OPT_HASH, '-', "Print hash value"},
102     {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
103     {"", OPT_MD, '-', "Any supported digest"},
104 #ifndef OPENSSL_NO_MD5
105     {"hash_old", OPT_HASH_OLD, '-', "Print old-style (MD5) hash value"},
106 #endif
107     {NULL}
108 };
109
110 int crl_main(int argc, char **argv)
111 {
112     X509_CRL *x = NULL;
113     BIO *out = NULL;
114     X509_STORE *store = NULL;
115     X509_STORE_CTX ctx;
116     X509_LOOKUP *lookup = NULL;
117     X509_OBJECT xobj;
118     EVP_PKEY *pkey;
119     const EVP_MD *digest = EVP_sha1();
120     unsigned long nmflag = 0;
121     char nmflag_set = 0;
122     char *infile = NULL, *outfile = NULL, *crldiff = NULL, *keyfile = NULL;
123     char *CAfile = NULL, *CApath = NULL, *prog;
124     OPTION_CHOICE o;
125     int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout = 0;
126     int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;
127     int ret = 1, num = 0, badsig = 0, fingerprint = 0, crlnumber = 0;
128     int text = 0, do_ver = 0, noCAfile = 0, noCApath = 0;
129     int i;
130 #ifndef OPENSSL_NO_MD5
131     int hash_old = 0;
132 #endif
133
134     prog = opt_init(argc, argv, crl_options);
135     while ((o = opt_next()) != OPT_EOF) {
136         switch (o) {
137         case OPT_EOF:
138         case OPT_ERR:
139  opthelp:
140             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
141             goto end;
142         case OPT_HELP:
143             opt_help(crl_options);
144             ret = 0;
145             goto end;
146         case OPT_INFORM:
147             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
148                 goto opthelp;
149             break;
150         case OPT_IN:
151             infile = opt_arg();
152             break;
153         case OPT_OUTFORM:
154             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
155                 goto opthelp;
156             break;
157         case OPT_OUT:
158             outfile = opt_arg();
159             break;
160         case OPT_KEYFORM:
161             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
162                 goto opthelp;
163             break;
164         case OPT_KEY:
165             keyfile = opt_arg();
166             break;
167         case OPT_GENDELTA:
168             crldiff = opt_arg();
169             break;
170         case OPT_CAPATH:
171             CApath = opt_arg();
172             do_ver = 1;
173             break;
174         case OPT_CAFILE:
175             CAfile = opt_arg();
176             do_ver = 1;
177             break;
178         case OPT_NOCAPATH:
179             noCApath =  1;
180             break;
181         case OPT_NOCAFILE:
182             noCAfile =  1;
183             break;
184         case OPT_HASH_OLD:
185 #ifndef OPENSSL_NO_MD5
186             hash_old = ++num;
187 #endif
188             break;
189         case OPT_VERIFY:
190             do_ver = 1;
191             break;
192         case OPT_TEXT:
193             text = 1;
194             break;
195         case OPT_HASH:
196             hash = ++num;
197             break;
198         case OPT_ISSUER:
199             issuer = ++num;
200             break;
201         case OPT_LASTUPDATE:
202             lastupdate = ++num;
203             break;
204         case OPT_NEXTUPDATE:
205             nextupdate = ++num;
206             break;
207         case OPT_NOOUT:
208             noout = ++num;
209             break;
210         case OPT_FINGERPRINT:
211             fingerprint = ++num;
212             break;
213         case OPT_CRLNUMBER:
214             crlnumber = ++num;
215             break;
216         case OPT_BADSIG:
217             badsig = 1;
218             break;
219         case OPT_NAMEOPT:
220             nmflag_set = 1;
221             if (!set_name_ex(&nmflag, opt_arg()))
222                 goto opthelp;
223             break;
224         case OPT_MD:
225             if (!opt_md(opt_unknown(), &digest))
226                 goto opthelp;
227         }
228     }
229     argc = opt_num_rest();
230     argv = opt_rest();
231
232     if (!nmflag_set)
233         nmflag = XN_FLAG_ONELINE;
234
235     x = load_crl(infile, informat);
236     if (x == NULL)
237         goto end;
238
239     if (do_ver) {
240         if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
241             goto end;
242         lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
243         if (lookup == NULL)
244             goto end;
245         if (!X509_STORE_CTX_init(&ctx, store, NULL, NULL)) {
246             BIO_printf(bio_err, "Error initialising X509 store\n");
247             goto end;
248         }
249
250         i = X509_STORE_get_by_subject(&ctx, X509_LU_X509,
251                                       X509_CRL_get_issuer(x), &xobj);
252         if (i <= 0) {
253             BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
254             goto end;
255         }
256         pkey = X509_get_pubkey(xobj.data.x509);
257         X509_OBJECT_free_contents(&xobj);
258         if (!pkey) {
259             BIO_printf(bio_err, "Error getting CRL issuer public key\n");
260             goto end;
261         }
262         i = X509_CRL_verify(x, pkey);
263         EVP_PKEY_free(pkey);
264         if (i < 0)
265             goto end;
266         if (i == 0)
267             BIO_printf(bio_err, "verify failure\n");
268         else
269             BIO_printf(bio_err, "verify OK\n");
270     }
271
272     if (crldiff) {
273         X509_CRL *newcrl, *delta;
274         if (!keyfile) {
275             BIO_puts(bio_err, "Missing CRL signing key\n");
276             goto end;
277         }
278         newcrl = load_crl(crldiff, informat);
279         if (!newcrl)
280             goto end;
281         pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
282         if (!pkey) {
283             X509_CRL_free(newcrl);
284             goto end;
285         }
286         delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
287         X509_CRL_free(newcrl);
288         EVP_PKEY_free(pkey);
289         if (delta) {
290             X509_CRL_free(x);
291             x = delta;
292         } else {
293             BIO_puts(bio_err, "Error creating delta CRL\n");
294             goto end;
295         }
296     }
297
298     if (num) {
299         for (i = 1; i <= num; i++) {
300             if (issuer == i) {
301                 print_name(bio_out, "issuer=", X509_CRL_get_issuer(x),
302                            nmflag);
303             }
304             if (crlnumber == i) {
305                 ASN1_INTEGER *crlnum;
306                 crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
307                 BIO_printf(bio_out, "crlNumber=");
308                 if (crlnum) {
309                     i2a_ASN1_INTEGER(bio_out, crlnum);
310                     ASN1_INTEGER_free(crlnum);
311                 } else
312                     BIO_puts(bio_out, "<NONE>");
313                 BIO_printf(bio_out, "\n");
314             }
315             if (hash == i) {
316                 BIO_printf(bio_out, "%08lx\n",
317                            X509_NAME_hash(X509_CRL_get_issuer(x)));
318             }
319 #ifndef OPENSSL_NO_MD5
320             if (hash_old == i) {
321                 BIO_printf(bio_out, "%08lx\n",
322                            X509_NAME_hash_old(X509_CRL_get_issuer(x)));
323             }
324 #endif
325             if (lastupdate == i) {
326                 BIO_printf(bio_out, "lastUpdate=");
327                 ASN1_TIME_print(bio_out, X509_CRL_get_lastUpdate(x));
328                 BIO_printf(bio_out, "\n");
329             }
330             if (nextupdate == i) {
331                 BIO_printf(bio_out, "nextUpdate=");
332                 if (X509_CRL_get_nextUpdate(x))
333                     ASN1_TIME_print(bio_out, X509_CRL_get_nextUpdate(x));
334                 else
335                     BIO_printf(bio_out, "NONE");
336                 BIO_printf(bio_out, "\n");
337             }
338             if (fingerprint == i) {
339                 int j;
340                 unsigned int n;
341                 unsigned char md[EVP_MAX_MD_SIZE];
342
343                 if (!X509_CRL_digest(x, digest, md, &n)) {
344                     BIO_printf(bio_err, "out of memory\n");
345                     goto end;
346                 }
347                 BIO_printf(bio_out, "%s Fingerprint=",
348                            OBJ_nid2sn(EVP_MD_type(digest)));
349                 for (j = 0; j < (int)n; j++) {
350                     BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
351                                ? '\n' : ':');
352                 }
353             }
354         }
355     }
356     out = bio_open_default(outfile, 'w', outformat);
357     if (out == NULL)
358         goto end;
359
360     if (text)
361         X509_CRL_print(out, x);
362
363     if (noout) {
364         ret = 0;
365         goto end;
366     }
367
368     if (badsig) {
369         ASN1_BIT_STRING *sig;
370         unsigned char *psig;
371         X509_CRL_get0_signature(&sig, NULL, x);
372         psig = ASN1_STRING_data(sig);
373         psig[ASN1_STRING_length(sig) - 1] ^= 0x1;
374     }
375
376     if (outformat == FORMAT_ASN1)
377         i = (int)i2d_X509_CRL_bio(out, x);
378     else
379         i = PEM_write_bio_X509_CRL(out, x);
380     if (!i) {
381         BIO_printf(bio_err, "unable to write CRL\n");
382         goto end;
383     }
384     ret = 0;
385
386  end:
387     if (ret != 0)
388         ERR_print_errors(bio_err);
389     BIO_free_all(out);
390     X509_CRL_free(x);
391     if (store) {
392         X509_STORE_CTX_cleanup(&ctx);
393         X509_STORE_free(store);
394     }
395     return (ret);
396 }