xref: /minix3/crypto/external/bsd/openssl/dist/apps/crl.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* apps/crl.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc #include <stdio.h>
60ebfedea0SLionel Sambuc #include <stdlib.h>
61ebfedea0SLionel Sambuc #include <string.h>
62ebfedea0SLionel Sambuc #include "apps.h"
63ebfedea0SLionel Sambuc #include <openssl/bio.h>
64ebfedea0SLionel Sambuc #include <openssl/err.h>
65ebfedea0SLionel Sambuc #include <openssl/x509.h>
66ebfedea0SLionel Sambuc #include <openssl/x509v3.h>
67ebfedea0SLionel Sambuc #include <openssl/pem.h>
68ebfedea0SLionel Sambuc 
69ebfedea0SLionel Sambuc #undef PROG
70ebfedea0SLionel Sambuc #define PROG    crl_main
71ebfedea0SLionel Sambuc 
72ebfedea0SLionel Sambuc #undef POSTFIX
73ebfedea0SLionel Sambuc #define POSTFIX ".rvk"
74ebfedea0SLionel Sambuc 
75ebfedea0SLionel Sambuc static const char *crl_usage[] = {
76ebfedea0SLionel Sambuc     "usage: crl args\n",
77ebfedea0SLionel Sambuc     "\n",
78ebfedea0SLionel Sambuc     " -inform arg     - input format - default PEM (DER or PEM)\n",
79ebfedea0SLionel Sambuc     " -outform arg    - output format - default PEM\n",
80ebfedea0SLionel Sambuc     " -text           - print out a text format version\n",
81ebfedea0SLionel Sambuc     " -in arg         - input file - default stdin\n",
82ebfedea0SLionel Sambuc     " -out arg        - output file - default stdout\n",
83ebfedea0SLionel Sambuc     " -hash           - print hash value\n",
84*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_MD5
85*0a6a1f1dSLionel Sambuc     " -hash_old       - print old-style (MD5) hash value\n",
86*0a6a1f1dSLionel Sambuc #endif
87ebfedea0SLionel Sambuc     " -fingerprint    - print the crl fingerprint\n",
88ebfedea0SLionel Sambuc     " -issuer         - print issuer DN\n",
89ebfedea0SLionel Sambuc     " -lastupdate     - lastUpdate field\n",
90ebfedea0SLionel Sambuc     " -nextupdate     - nextUpdate field\n",
91ebfedea0SLionel Sambuc     " -crlnumber      - print CRL number\n",
92ebfedea0SLionel Sambuc     " -noout          - no CRL output\n",
93ebfedea0SLionel Sambuc     " -CAfile  name   - verify CRL using certificates in file \"name\"\n",
94ebfedea0SLionel Sambuc     " -CApath  dir    - verify CRL using certificates in \"dir\"\n",
95ebfedea0SLionel Sambuc     " -nameopt arg    - various certificate name options\n",
96ebfedea0SLionel Sambuc     NULL
97ebfedea0SLionel Sambuc };
98ebfedea0SLionel Sambuc 
99ebfedea0SLionel Sambuc static X509_CRL *load_crl(char *file, int format);
100ebfedea0SLionel Sambuc static BIO *bio_out = NULL;
101ebfedea0SLionel Sambuc 
102ebfedea0SLionel Sambuc int MAIN(int, char **);
103ebfedea0SLionel Sambuc 
MAIN(int argc,char ** argv)104ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
105ebfedea0SLionel Sambuc {
106ebfedea0SLionel Sambuc     unsigned long nmflag = 0;
107ebfedea0SLionel Sambuc     X509_CRL *x = NULL;
108ebfedea0SLionel Sambuc     char *CAfile = NULL, *CApath = NULL;
109ebfedea0SLionel Sambuc     int ret = 1, i, num, badops = 0;
110ebfedea0SLionel Sambuc     BIO *out = NULL;
111ebfedea0SLionel Sambuc     int informat, outformat;
112ebfedea0SLionel Sambuc     char *infile = NULL, *outfile = NULL;
113*0a6a1f1dSLionel Sambuc     int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout =
114*0a6a1f1dSLionel Sambuc         0, text = 0;
115*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_MD5
116*0a6a1f1dSLionel Sambuc     int hash_old = 0;
117*0a6a1f1dSLionel Sambuc #endif
118ebfedea0SLionel Sambuc     int fingerprint = 0, crlnumber = 0;
119ebfedea0SLionel Sambuc     const char **pp;
120ebfedea0SLionel Sambuc     X509_STORE *store = NULL;
121ebfedea0SLionel Sambuc     X509_STORE_CTX ctx;
122ebfedea0SLionel Sambuc     X509_LOOKUP *lookup = NULL;
123ebfedea0SLionel Sambuc     X509_OBJECT xobj;
124ebfedea0SLionel Sambuc     EVP_PKEY *pkey;
125ebfedea0SLionel Sambuc     int do_ver = 0;
126ebfedea0SLionel Sambuc     const EVP_MD *md_alg, *digest = EVP_sha1();
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc     apps_startup();
129ebfedea0SLionel Sambuc 
130ebfedea0SLionel Sambuc     if (bio_err == NULL)
131ebfedea0SLionel Sambuc         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
132ebfedea0SLionel Sambuc             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
133ebfedea0SLionel Sambuc 
134ebfedea0SLionel Sambuc     if (!load_config(bio_err, NULL))
135ebfedea0SLionel Sambuc         goto end;
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc     if (bio_out == NULL)
138*0a6a1f1dSLionel Sambuc         if ((bio_out = BIO_new(BIO_s_file())) != NULL) {
139ebfedea0SLionel Sambuc             BIO_set_fp(bio_out, stdout, BIO_NOCLOSE);
140ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
141ebfedea0SLionel Sambuc             {
142ebfedea0SLionel Sambuc                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
143ebfedea0SLionel Sambuc                 bio_out = BIO_push(tmpbio, bio_out);
144ebfedea0SLionel Sambuc             }
145ebfedea0SLionel Sambuc #endif
146ebfedea0SLionel Sambuc         }
147ebfedea0SLionel Sambuc 
148ebfedea0SLionel Sambuc     informat = FORMAT_PEM;
149ebfedea0SLionel Sambuc     outformat = FORMAT_PEM;
150ebfedea0SLionel Sambuc 
151ebfedea0SLionel Sambuc     argc--;
152ebfedea0SLionel Sambuc     argv++;
153ebfedea0SLionel Sambuc     num = 0;
154*0a6a1f1dSLionel Sambuc     while (argc >= 1) {
155ebfedea0SLionel Sambuc #ifdef undef
156*0a6a1f1dSLionel Sambuc         if (strcmp(*argv, "-p") == 0) {
157*0a6a1f1dSLionel Sambuc             if (--argc < 1)
158*0a6a1f1dSLionel Sambuc                 goto bad;
159*0a6a1f1dSLionel Sambuc             if (!args_from_file(++argv, Nargc, Nargv)) {
160*0a6a1f1dSLionel Sambuc                 goto end;
161ebfedea0SLionel Sambuc             }
162*0a6a1f1dSLionel Sambuc         */}
163ebfedea0SLionel Sambuc #endif
164*0a6a1f1dSLionel Sambuc         if (strcmp(*argv, "-inform") == 0) {
165*0a6a1f1dSLionel Sambuc             if (--argc < 1)
166*0a6a1f1dSLionel Sambuc                 goto bad;
167ebfedea0SLionel Sambuc             informat = str2fmt(*(++argv));
168*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-outform") == 0) {
169*0a6a1f1dSLionel Sambuc             if (--argc < 1)
170*0a6a1f1dSLionel Sambuc                 goto bad;
171ebfedea0SLionel Sambuc             outformat = str2fmt(*(++argv));
172*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-in") == 0) {
173*0a6a1f1dSLionel Sambuc             if (--argc < 1)
174*0a6a1f1dSLionel Sambuc                 goto bad;
175ebfedea0SLionel Sambuc             infile = *(++argv);
176*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-out") == 0) {
177*0a6a1f1dSLionel Sambuc             if (--argc < 1)
178*0a6a1f1dSLionel Sambuc                 goto bad;
179ebfedea0SLionel Sambuc             outfile = *(++argv);
180*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-CApath") == 0) {
181*0a6a1f1dSLionel Sambuc             if (--argc < 1)
182*0a6a1f1dSLionel Sambuc                 goto bad;
183ebfedea0SLionel Sambuc             CApath = *(++argv);
184ebfedea0SLionel Sambuc             do_ver = 1;
185*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-CAfile") == 0) {
186*0a6a1f1dSLionel Sambuc             if (--argc < 1)
187*0a6a1f1dSLionel Sambuc                 goto bad;
188ebfedea0SLionel Sambuc             CAfile = *(++argv);
189ebfedea0SLionel Sambuc             do_ver = 1;
190*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-verify") == 0)
191ebfedea0SLionel Sambuc             do_ver = 1;
192ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-text") == 0)
193ebfedea0SLionel Sambuc             text = 1;
194ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-hash") == 0)
195ebfedea0SLionel Sambuc             hash = ++num;
196*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_MD5
197*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-hash_old") == 0)
198*0a6a1f1dSLionel Sambuc             hash_old = ++num;
199*0a6a1f1dSLionel Sambuc #endif
200*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-nameopt") == 0) {
201*0a6a1f1dSLionel Sambuc             if (--argc < 1)
202*0a6a1f1dSLionel Sambuc                 goto bad;
203*0a6a1f1dSLionel Sambuc             if (!set_name_ex(&nmflag, *(++argv)))
204*0a6a1f1dSLionel Sambuc                 goto bad;
205*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-issuer") == 0)
206ebfedea0SLionel Sambuc             issuer = ++num;
207ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-lastupdate") == 0)
208ebfedea0SLionel Sambuc             lastupdate = ++num;
209ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-nextupdate") == 0)
210ebfedea0SLionel Sambuc             nextupdate = ++num;
211ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-noout") == 0)
212ebfedea0SLionel Sambuc             noout = ++num;
213ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-fingerprint") == 0)
214ebfedea0SLionel Sambuc             fingerprint = ++num;
215ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-crlnumber") == 0)
216ebfedea0SLionel Sambuc             crlnumber = ++num;
217*0a6a1f1dSLionel Sambuc         else if ((md_alg = EVP_get_digestbyname(*argv + 1))) {
218ebfedea0SLionel Sambuc             /* ok */
219ebfedea0SLionel Sambuc             digest = md_alg;
220*0a6a1f1dSLionel Sambuc         } else {
221ebfedea0SLionel Sambuc             BIO_printf(bio_err, "unknown option %s\n", *argv);
222ebfedea0SLionel Sambuc             badops = 1;
223ebfedea0SLionel Sambuc             break;
224ebfedea0SLionel Sambuc         }
225ebfedea0SLionel Sambuc         argc--;
226ebfedea0SLionel Sambuc         argv++;
227ebfedea0SLionel Sambuc     }
228ebfedea0SLionel Sambuc 
229*0a6a1f1dSLionel Sambuc     if (badops) {
230ebfedea0SLionel Sambuc  bad:
231ebfedea0SLionel Sambuc         for (pp = crl_usage; (*pp != NULL); pp++)
232ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%s", *pp);
233ebfedea0SLionel Sambuc         goto end;
234ebfedea0SLionel Sambuc     }
235ebfedea0SLionel Sambuc 
236ebfedea0SLionel Sambuc     ERR_load_crypto_strings();
237ebfedea0SLionel Sambuc     x = load_crl(infile, informat);
238*0a6a1f1dSLionel Sambuc     if (x == NULL) {
239*0a6a1f1dSLionel Sambuc         goto end;
240*0a6a1f1dSLionel Sambuc     }
241ebfedea0SLionel Sambuc 
242ebfedea0SLionel Sambuc     if (do_ver) {
243ebfedea0SLionel Sambuc         store = X509_STORE_new();
244ebfedea0SLionel Sambuc         lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
245*0a6a1f1dSLionel Sambuc         if (lookup == NULL)
246*0a6a1f1dSLionel Sambuc             goto end;
247ebfedea0SLionel Sambuc         if (!X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM))
248ebfedea0SLionel Sambuc             X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT);
249ebfedea0SLionel Sambuc 
250ebfedea0SLionel Sambuc         lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
251*0a6a1f1dSLionel Sambuc         if (lookup == NULL)
252*0a6a1f1dSLionel Sambuc             goto end;
253ebfedea0SLionel Sambuc         if (!X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM))
254ebfedea0SLionel Sambuc             X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
255ebfedea0SLionel Sambuc         ERR_clear_error();
256ebfedea0SLionel Sambuc 
257ebfedea0SLionel Sambuc         if (!X509_STORE_CTX_init(&ctx, store, NULL, NULL)) {
258*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "Error initialising X509 store\n");
259ebfedea0SLionel Sambuc             goto end;
260ebfedea0SLionel Sambuc         }
261ebfedea0SLionel Sambuc 
262ebfedea0SLionel Sambuc         i = X509_STORE_get_by_subject(&ctx, X509_LU_X509,
263ebfedea0SLionel Sambuc                                       X509_CRL_get_issuer(x), &xobj);
264ebfedea0SLionel Sambuc         if (i <= 0) {
265*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
266ebfedea0SLionel Sambuc             goto end;
267ebfedea0SLionel Sambuc         }
268ebfedea0SLionel Sambuc         pkey = X509_get_pubkey(xobj.data.x509);
269ebfedea0SLionel Sambuc         X509_OBJECT_free_contents(&xobj);
270ebfedea0SLionel Sambuc         if (!pkey) {
271*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "Error getting CRL issuer public key\n");
272ebfedea0SLionel Sambuc             goto end;
273ebfedea0SLionel Sambuc         }
274ebfedea0SLionel Sambuc         i = X509_CRL_verify(x, pkey);
275ebfedea0SLionel Sambuc         EVP_PKEY_free(pkey);
276*0a6a1f1dSLionel Sambuc         if (i < 0)
277*0a6a1f1dSLionel Sambuc             goto end;
278*0a6a1f1dSLionel Sambuc         if (i == 0)
279*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "verify failure\n");
280*0a6a1f1dSLionel Sambuc         else
281*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "verify OK\n");
282ebfedea0SLionel Sambuc     }
283ebfedea0SLionel Sambuc 
284*0a6a1f1dSLionel Sambuc     if (num) {
285*0a6a1f1dSLionel Sambuc         for (i = 1; i <= num; i++) {
286*0a6a1f1dSLionel Sambuc             if (issuer == i) {
287*0a6a1f1dSLionel Sambuc                 print_name(bio_out, "issuer=", X509_CRL_get_issuer(x),
288*0a6a1f1dSLionel Sambuc                            nmflag);
289ebfedea0SLionel Sambuc             }
290*0a6a1f1dSLionel Sambuc             if (crlnumber == i) {
291ebfedea0SLionel Sambuc                 ASN1_INTEGER *crlnum;
292*0a6a1f1dSLionel Sambuc                 crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
293ebfedea0SLionel Sambuc                 BIO_printf(bio_out, "crlNumber=");
294*0a6a1f1dSLionel Sambuc                 if (crlnum) {
295ebfedea0SLionel Sambuc                     i2a_ASN1_INTEGER(bio_out, crlnum);
296ebfedea0SLionel Sambuc                     ASN1_INTEGER_free(crlnum);
297*0a6a1f1dSLionel Sambuc                 } else
298ebfedea0SLionel Sambuc                     BIO_puts(bio_out, "<NONE>");
299ebfedea0SLionel Sambuc                 BIO_printf(bio_out, "\n");
300ebfedea0SLionel Sambuc             }
301*0a6a1f1dSLionel Sambuc             if (hash == i) {
302ebfedea0SLionel Sambuc                 BIO_printf(bio_out, "%08lx\n",
303ebfedea0SLionel Sambuc                            X509_NAME_hash(X509_CRL_get_issuer(x)));
304ebfedea0SLionel Sambuc             }
305*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_MD5
306*0a6a1f1dSLionel Sambuc             if (hash_old == i) {
307*0a6a1f1dSLionel Sambuc                 BIO_printf(bio_out, "%08lx\n",
308*0a6a1f1dSLionel Sambuc                            X509_NAME_hash_old(X509_CRL_get_issuer(x)));
309*0a6a1f1dSLionel Sambuc             }
310*0a6a1f1dSLionel Sambuc #endif
311*0a6a1f1dSLionel Sambuc             if (lastupdate == i) {
312ebfedea0SLionel Sambuc                 BIO_printf(bio_out, "lastUpdate=");
313*0a6a1f1dSLionel Sambuc                 ASN1_TIME_print(bio_out, X509_CRL_get_lastUpdate(x));
314ebfedea0SLionel Sambuc                 BIO_printf(bio_out, "\n");
315ebfedea0SLionel Sambuc             }
316*0a6a1f1dSLionel Sambuc             if (nextupdate == i) {
317ebfedea0SLionel Sambuc                 BIO_printf(bio_out, "nextUpdate=");
318ebfedea0SLionel Sambuc                 if (X509_CRL_get_nextUpdate(x))
319*0a6a1f1dSLionel Sambuc                     ASN1_TIME_print(bio_out, X509_CRL_get_nextUpdate(x));
320ebfedea0SLionel Sambuc                 else
321ebfedea0SLionel Sambuc                     BIO_printf(bio_out, "NONE");
322ebfedea0SLionel Sambuc                 BIO_printf(bio_out, "\n");
323ebfedea0SLionel Sambuc             }
324*0a6a1f1dSLionel Sambuc             if (fingerprint == i) {
325ebfedea0SLionel Sambuc                 int j;
326ebfedea0SLionel Sambuc                 unsigned int n;
327ebfedea0SLionel Sambuc                 unsigned char md[EVP_MAX_MD_SIZE];
328ebfedea0SLionel Sambuc 
329*0a6a1f1dSLionel Sambuc                 if (!X509_CRL_digest(x, digest, md, &n)) {
330ebfedea0SLionel Sambuc                     BIO_printf(bio_err, "out of memory\n");
331ebfedea0SLionel Sambuc                     goto end;
332ebfedea0SLionel Sambuc                 }
333ebfedea0SLionel Sambuc                 BIO_printf(bio_out, "%s Fingerprint=",
334ebfedea0SLionel Sambuc                            OBJ_nid2sn(EVP_MD_type(digest)));
335*0a6a1f1dSLionel Sambuc                 for (j = 0; j < (int)n; j++) {
336*0a6a1f1dSLionel Sambuc                     BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
337ebfedea0SLionel Sambuc                                ? '\n' : ':');
338ebfedea0SLionel Sambuc                 }
339ebfedea0SLionel Sambuc             }
340ebfedea0SLionel Sambuc         }
341ebfedea0SLionel Sambuc     }
342ebfedea0SLionel Sambuc 
343ebfedea0SLionel Sambuc     out = BIO_new(BIO_s_file());
344*0a6a1f1dSLionel Sambuc     if (out == NULL) {
345ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
346ebfedea0SLionel Sambuc         goto end;
347ebfedea0SLionel Sambuc     }
348ebfedea0SLionel Sambuc 
349*0a6a1f1dSLionel Sambuc     if (outfile == NULL) {
350ebfedea0SLionel Sambuc         BIO_set_fp(out, stdout, BIO_NOCLOSE);
351ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
352ebfedea0SLionel Sambuc         {
353ebfedea0SLionel Sambuc             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
354ebfedea0SLionel Sambuc             out = BIO_push(tmpbio, out);
355ebfedea0SLionel Sambuc         }
356ebfedea0SLionel Sambuc #endif
357*0a6a1f1dSLionel Sambuc     } else {
358*0a6a1f1dSLionel Sambuc         if (BIO_write_filename(out, outfile) <= 0) {
359ebfedea0SLionel Sambuc             perror(outfile);
360ebfedea0SLionel Sambuc             goto end;
361ebfedea0SLionel Sambuc         }
362ebfedea0SLionel Sambuc     }
363ebfedea0SLionel Sambuc 
364*0a6a1f1dSLionel Sambuc     if (text)
365*0a6a1f1dSLionel Sambuc         X509_CRL_print(out, x);
366ebfedea0SLionel Sambuc 
367*0a6a1f1dSLionel Sambuc     if (noout) {
368ebfedea0SLionel Sambuc         ret = 0;
369ebfedea0SLionel Sambuc         goto end;
370ebfedea0SLionel Sambuc     }
371ebfedea0SLionel Sambuc 
372ebfedea0SLionel Sambuc     if (outformat == FORMAT_ASN1)
373ebfedea0SLionel Sambuc         i = (int)i2d_X509_CRL_bio(out, x);
374ebfedea0SLionel Sambuc     else if (outformat == FORMAT_PEM)
375ebfedea0SLionel Sambuc         i = PEM_write_bio_X509_CRL(out, x);
376*0a6a1f1dSLionel Sambuc     else {
377ebfedea0SLionel Sambuc         BIO_printf(bio_err, "bad output format specified for outfile\n");
378ebfedea0SLionel Sambuc         goto end;
379ebfedea0SLionel Sambuc     }
380*0a6a1f1dSLionel Sambuc     if (!i) {
381*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err, "unable to write CRL\n");
382*0a6a1f1dSLionel Sambuc         goto end;
383*0a6a1f1dSLionel Sambuc     }
384ebfedea0SLionel Sambuc     ret = 0;
385ebfedea0SLionel Sambuc  end:
386ebfedea0SLionel Sambuc     BIO_free_all(out);
387ebfedea0SLionel Sambuc     BIO_free_all(bio_out);
388ebfedea0SLionel Sambuc     bio_out = NULL;
389ebfedea0SLionel Sambuc     X509_CRL_free(x);
390ebfedea0SLionel Sambuc     if (store) {
391ebfedea0SLionel Sambuc         X509_STORE_CTX_cleanup(&ctx);
392ebfedea0SLionel Sambuc         X509_STORE_free(store);
393ebfedea0SLionel Sambuc     }
394ebfedea0SLionel Sambuc     apps_shutdown();
395ebfedea0SLionel Sambuc     OPENSSL_EXIT(ret);
396ebfedea0SLionel Sambuc }
397ebfedea0SLionel Sambuc 
load_crl(char * infile,int format)398ebfedea0SLionel Sambuc static X509_CRL *load_crl(char *infile, int format)
399ebfedea0SLionel Sambuc {
400ebfedea0SLionel Sambuc     X509_CRL *x = NULL;
401ebfedea0SLionel Sambuc     BIO *in = NULL;
402ebfedea0SLionel Sambuc 
403ebfedea0SLionel Sambuc     in = BIO_new(BIO_s_file());
404*0a6a1f1dSLionel Sambuc     if (in == NULL) {
405ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
406ebfedea0SLionel Sambuc         goto end;
407ebfedea0SLionel Sambuc     }
408ebfedea0SLionel Sambuc 
409ebfedea0SLionel Sambuc     if (infile == NULL)
410ebfedea0SLionel Sambuc         BIO_set_fp(in, stdin, BIO_NOCLOSE);
411*0a6a1f1dSLionel Sambuc     else {
412*0a6a1f1dSLionel Sambuc         if (BIO_read_filename(in, infile) <= 0) {
413ebfedea0SLionel Sambuc             perror(infile);
414ebfedea0SLionel Sambuc             goto end;
415ebfedea0SLionel Sambuc         }
416ebfedea0SLionel Sambuc     }
417ebfedea0SLionel Sambuc     if (format == FORMAT_ASN1)
418ebfedea0SLionel Sambuc         x = d2i_X509_CRL_bio(in, NULL);
419ebfedea0SLionel Sambuc     else if (format == FORMAT_PEM)
420ebfedea0SLionel Sambuc         x = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
421ebfedea0SLionel Sambuc     else {
422ebfedea0SLionel Sambuc         BIO_printf(bio_err, "bad input format specified for input crl\n");
423ebfedea0SLionel Sambuc         goto end;
424ebfedea0SLionel Sambuc     }
425*0a6a1f1dSLionel Sambuc     if (x == NULL) {
426ebfedea0SLionel Sambuc         BIO_printf(bio_err, "unable to load CRL\n");
427ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
428ebfedea0SLionel Sambuc         goto end;
429ebfedea0SLionel Sambuc     }
430ebfedea0SLionel Sambuc 
431ebfedea0SLionel Sambuc  end:
432ebfedea0SLionel Sambuc     BIO_free(in);
433ebfedea0SLionel Sambuc     return (x);
434ebfedea0SLionel Sambuc }
435