GH408 follow-on: update buflen
[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     if (!app_load_modules(NULL))
236         goto end;
237
238     x = load_crl(infile, informat);
239     if (x == NULL)
240         goto end;
241
242     if (do_ver) {
243         if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
244             goto end;
245         lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
246         if (lookup == NULL)
247             goto end;
248         if (!X509_STORE_CTX_init(&ctx, store, NULL, NULL)) {
249             BIO_printf(bio_err, "Error initialising X509 store\n");
250             goto end;
251         }
252
253         i = X509_STORE_get_by_subject(&ctx, X509_LU_X509,
254                                       X509_CRL_get_issuer(x), &xobj);
255         if (i <= 0) {
256             BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
257             goto end;
258         }
259         pkey = X509_get_pubkey(xobj.data.x509);
260         X509_OBJECT_free_contents(&xobj);
261         if (!pkey) {
262             BIO_printf(bio_err, "Error getting CRL issuer public key\n");
263             goto end;
264         }
265         i = X509_CRL_verify(x, pkey);
266         EVP_PKEY_free(pkey);
267         if (i < 0)
268             goto end;
269         if (i == 0)
270             BIO_printf(bio_err, "verify failure\n");
271         else
272             BIO_printf(bio_err, "verify OK\n");
273     }
274
275     if (crldiff) {
276         X509_CRL *newcrl, *delta;
277         if (!keyfile) {
278             BIO_puts(bio_err, "Missing CRL signing key\n");
279             goto end;
280         }
281         newcrl = load_crl(crldiff, informat);
282         if (!newcrl)
283             goto end;
284         pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
285         if (!pkey) {
286             X509_CRL_free(newcrl);
287             goto end;
288         }
289         delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
290         X509_CRL_free(newcrl);
291         EVP_PKEY_free(pkey);
292         if (delta) {
293             X509_CRL_free(x);
294             x = delta;
295         } else {
296             BIO_puts(bio_err, "Error creating delta CRL\n");
297             goto end;
298         }
299     }
300
301     if (num) {
302         for (i = 1; i <= num; i++) {
303             if (issuer == i) {
304                 print_name(bio_out, "issuer=", X509_CRL_get_issuer(x),
305                            nmflag);
306             }
307             if (crlnumber == i) {
308                 ASN1_INTEGER *crlnum;
309                 crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
310                 BIO_printf(bio_out, "crlNumber=");
311                 if (crlnum) {
312                     i2a_ASN1_INTEGER(bio_out, crlnum);
313                     ASN1_INTEGER_free(crlnum);
314                 } else
315                     BIO_puts(bio_out, "<NONE>");
316                 BIO_printf(bio_out, "\n");
317             }
318             if (hash == i) {
319                 BIO_printf(bio_out, "%08lx\n",
320                            X509_NAME_hash(X509_CRL_get_issuer(x)));
321             }
322 #ifndef OPENSSL_NO_MD5
323             if (hash_old == i) {
324                 BIO_printf(bio_out, "%08lx\n",
325                            X509_NAME_hash_old(X509_CRL_get_issuer(x)));
326             }
327 #endif
328             if (lastupdate == i) {
329                 BIO_printf(bio_out, "lastUpdate=");
330                 ASN1_TIME_print(bio_out, X509_CRL_get_lastUpdate(x));
331                 BIO_printf(bio_out, "\n");
332             }
333             if (nextupdate == i) {
334                 BIO_printf(bio_out, "nextUpdate=");
335                 if (X509_CRL_get_nextUpdate(x))
336                     ASN1_TIME_print(bio_out, X509_CRL_get_nextUpdate(x));
337                 else
338                     BIO_printf(bio_out, "NONE");
339                 BIO_printf(bio_out, "\n");
340             }
341             if (fingerprint == i) {
342                 int j;
343                 unsigned int n;
344                 unsigned char md[EVP_MAX_MD_SIZE];
345
346                 if (!X509_CRL_digest(x, digest, md, &n)) {
347                     BIO_printf(bio_err, "out of memory\n");
348                     goto end;
349                 }
350                 BIO_printf(bio_out, "%s Fingerprint=",
351                            OBJ_nid2sn(EVP_MD_type(digest)));
352                 for (j = 0; j < (int)n; j++) {
353                     BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
354                                ? '\n' : ':');
355                 }
356             }
357         }
358     }
359     out = bio_open_default(outfile, 'w', outformat);
360     if (out == NULL)
361         goto end;
362
363     if (text)
364         X509_CRL_print(out, x);
365
366     if (noout) {
367         ret = 0;
368         goto end;
369     }
370
371     if (badsig) {
372         ASN1_BIT_STRING *sig;
373         unsigned char *psig;
374         X509_CRL_get0_signature(&sig, NULL, x);
375         psig = ASN1_STRING_data(sig);
376         psig[ASN1_STRING_length(sig) - 1] ^= 0x1;
377     }
378
379     if (outformat == FORMAT_ASN1)
380         i = (int)i2d_X509_CRL_bio(out, x);
381     else
382         i = PEM_write_bio_X509_CRL(out, x);
383     if (!i) {
384         BIO_printf(bio_err, "unable to write CRL\n");
385         goto end;
386     }
387     ret = 0;
388
389  end:
390     if (ret != 0)
391         ERR_print_errors(bio_err);
392     BIO_free_all(out);
393     X509_CRL_free(x);
394     if (store) {
395         X509_STORE_CTX_cleanup(&ctx);
396         X509_STORE_free(store);
397     }
398     return (ret);
399 }