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