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