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