]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/crl.c
Process digest option after loading providers
[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_ERR = -1, OPT_EOF = 0, OPT_HELP,
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 const EVP_MD *digest = 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_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;
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 = ++num;
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 && !opt_md(digestname, &digest))
211 goto opthelp;
212 x = load_crl(infile, "CRL");
213 if (x == NULL)
214 goto end;
215
216 if (do_ver) {
217 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
218 CAstore, noCAstore)) == NULL)
219 goto end;
220 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
221 if (lookup == NULL)
222 goto end;
223 ctx = X509_STORE_CTX_new();
224 if (ctx == NULL || !X509_STORE_CTX_init(ctx, store, NULL, NULL)) {
225 BIO_printf(bio_err, "Error initialising X509 store\n");
226 goto end;
227 }
228
229 xobj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509,
230 X509_CRL_get_issuer(x));
231 if (xobj == NULL) {
232 BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
233 goto end;
234 }
235 pkey = X509_get_pubkey(X509_OBJECT_get0_X509(xobj));
236 X509_OBJECT_free(xobj);
237 if (pkey == NULL) {
238 BIO_printf(bio_err, "Error getting CRL issuer public key\n");
239 goto end;
240 }
241 i = X509_CRL_verify(x, pkey);
242 EVP_PKEY_free(pkey);
243 if (i < 0)
244 goto end;
245 if (i == 0)
246 BIO_printf(bio_err, "verify failure\n");
247 else
248 BIO_printf(bio_err, "verify OK\n");
249 }
250
251 if (crldiff) {
252 X509_CRL *newcrl, *delta;
253 if (!keyfile) {
254 BIO_puts(bio_err, "Missing CRL signing key\n");
255 goto end;
256 }
257 newcrl = load_crl(crldiff, "other CRL");
258 if (!newcrl)
259 goto end;
260 pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
261 if (pkey == NULL) {
262 X509_CRL_free(newcrl);
263 goto end;
264 }
265 delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
266 X509_CRL_free(newcrl);
267 EVP_PKEY_free(pkey);
268 if (delta) {
269 X509_CRL_free(x);
270 x = delta;
271 } else {
272 BIO_puts(bio_err, "Error creating delta CRL\n");
273 goto end;
274 }
275 }
276
277 if (badsig) {
278 const ASN1_BIT_STRING *sig;
279
280 X509_CRL_get0_signature(x, &sig, NULL);
281 corrupt_signature(sig);
282 }
283
284 if (num) {
285 for (i = 1; i <= num; i++) {
286 if (issuer == i) {
287 print_name(bio_out, "issuer=", X509_CRL_get_issuer(x),
288 get_nameopt());
289 }
290 if (crlnumber == i) {
291 ASN1_INTEGER *crlnum;
292
293 crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
294 BIO_printf(bio_out, "crlNumber=");
295 if (crlnum) {
296 BIO_puts(bio_out, "0x");
297 i2a_ASN1_INTEGER(bio_out, crlnum);
298 ASN1_INTEGER_free(crlnum);
299 } else {
300 BIO_puts(bio_out, "<NONE>");
301 }
302 BIO_printf(bio_out, "\n");
303 }
304 if (hash == i) {
305 int ok;
306 unsigned long hash_value =
307 X509_NAME_hash_ex(X509_CRL_get_issuer(x), app_get0_libctx(),
308 app_get0_propq(), &ok);
309
310 BIO_printf(bio_out, "issuer name hash=");
311 if (ok)
312 BIO_printf(bio_out, "%08lx\n", hash_value);
313 else
314 BIO_puts(bio_out, "<ERROR>");
315 }
316 #ifndef OPENSSL_NO_MD5
317 if (hash_old == i) {
318 BIO_printf(bio_out, "issuer name old hash=");
319 BIO_printf(bio_out, "%08lx\n",
320 X509_NAME_hash_old(X509_CRL_get_issuer(x)));
321 }
322 #endif
323 if (lastupdate == i) {
324 BIO_printf(bio_out, "lastUpdate=");
325 ASN1_TIME_print(bio_out, X509_CRL_get0_lastUpdate(x));
326 BIO_printf(bio_out, "\n");
327 }
328 if (nextupdate == i) {
329 BIO_printf(bio_out, "nextUpdate=");
330 if (X509_CRL_get0_nextUpdate(x))
331 ASN1_TIME_print(bio_out, X509_CRL_get0_nextUpdate(x));
332 else
333 BIO_printf(bio_out, "NONE");
334 BIO_printf(bio_out, "\n");
335 }
336 if (fingerprint == i) {
337 int j;
338 unsigned int n;
339 unsigned char md[EVP_MAX_MD_SIZE];
340
341 if (!X509_CRL_digest(x, digest, md, &n)) {
342 BIO_printf(bio_err, "out of memory\n");
343 goto end;
344 }
345 BIO_printf(bio_out, "%s Fingerprint=",
346 OBJ_nid2sn(EVP_MD_type(digest)));
347 for (j = 0; j < (int)n; j++) {
348 BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
349 ? '\n' : ':');
350 }
351 }
352 }
353 }
354 out = bio_open_default(outfile, 'w', outformat);
355 if (out == NULL)
356 goto end;
357
358 if (text)
359 X509_CRL_print_ex(out, x, get_nameopt());
360
361 if (noout) {
362 ret = 0;
363 goto end;
364 }
365
366 if (outformat == FORMAT_ASN1)
367 i = (int)i2d_X509_CRL_bio(out, x);
368 else
369 i = PEM_write_bio_X509_CRL(out, x);
370 if (!i) {
371 BIO_printf(bio_err, "unable to write CRL\n");
372 goto end;
373 }
374 ret = 0;
375
376 end:
377 if (ret != 0)
378 ERR_print_errors(bio_err);
379 BIO_free_all(out);
380 X509_CRL_free(x);
381 X509_STORE_CTX_free(ctx);
382 X509_STORE_free(store);
383 return ret;
384 }