]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
4333b89f | 2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include "apps.h" | |
dab2cd68 | 14 | #include "progs.h" |
ec577822 BM |
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> | |
d02b48c6 | 20 | |
7e1b7485 | 21 | typedef enum OPTION_choice { |
b0f96018 | 22 | OPT_COMMON, |
7e1b7485 RS |
23 | OPT_INFORM, OPT_IN, OPT_OUTFORM, OPT_OUT, OPT_KEYFORM, OPT_KEY, |
24 | OPT_ISSUER, OPT_LASTUPDATE, OPT_NEXTUPDATE, OPT_FINGERPRINT, | |
fd3397fc | 25 | OPT_CRLNUMBER, OPT_BADSIG, OPT_GENDELTA, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE, |
8c5bff22 | 26 | OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE, OPT_VERIFY, OPT_DATEOPT, OPT_TEXT, OPT_HASH, |
6bd4e3f2 | 27 | OPT_HASH_OLD, OPT_NOOUT, OPT_NAMEOPT, OPT_MD, OPT_PROV_ENUM |
7e1b7485 | 28 | } OPTION_CHOICE; |
d02b48c6 | 29 | |
44c83ebd | 30 | const OPTIONS crl_options[] = { |
5388f986 | 31 | OPT_SECTION("General"), |
7e1b7485 | 32 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
33 | {"verify", OPT_VERIFY, '-', "Verify CRL signature"}, |
34 | ||
35 | OPT_SECTION("Input"), | |
7e1b7485 | 36 | {"in", OPT_IN, '<', "Input file - default stdin"}, |
6d382c74 | 37 | {"inform", OPT_INFORM, 'F', "CRL input format (DER or PEM); has no effect"}, |
16e1b281 | 38 | {"key", OPT_KEY, '<', "CRL signing Private key to use"}, |
6d382c74 | 39 | {"keyform", OPT_KEYFORM, 'F', "Private key file format (DER/PEM/P12); has no effect"}, |
5388f986 RS |
40 | |
41 | OPT_SECTION("Output"), | |
42 | {"out", OPT_OUT, '>', "output file - default stdout"}, | |
43 | {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"}, | |
8c5bff22 | 44 | {"dateopt", OPT_DATEOPT, 's', "Datetime format used for printing. (rfc_822/iso_8601). Default is rfc_822."}, |
5388f986 RS |
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 | |
2b264aee | 50 | {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"}, |
5388f986 RS |
51 | {"", OPT_MD, '-', "Any supported digest"}, |
52 | ||
53 | OPT_SECTION("CRL"), | |
7e1b7485 RS |
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"}, | |
16e1b281 | 60 | {"badsig", OPT_BADSIG, '-', "Corrupt last byte of loaded CRL signature (for test)" }, |
12d56b29 | 61 | {"gendelta", OPT_GENDELTA, '<', "Other CRL to compare/diff to the Input one"}, |
5388f986 RS |
62 | |
63 | OPT_SECTION("Certificate"), | |
7e1b7485 RS |
64 | {"CApath", OPT_CAPATH, '/', "Verify CRL using certificates in dir"}, |
65 | {"CAfile", OPT_CAFILE, '<', "Verify CRL using certificates in file name"}, | |
fd3397fc | 66 | {"CAstore", OPT_CASTORE, ':', "Verify CRL using certificates in store URI"}, |
2b6bcb70 MC |
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"}, | |
fd3397fc RL |
71 | {"no-CAstore", OPT_NOCASTORE, '-', |
72 | "Do not load certificates from the default certificates store"}, | |
6bd4e3f2 | 73 | OPT_PROV_OPTIONS, |
7e1b7485 | 74 | {NULL} |
d02b48c6 RE |
75 | }; |
76 | ||
7e1b7485 | 77 | int crl_main(int argc, char **argv) |
0f113f3e | 78 | { |
0f113f3e | 79 | X509_CRL *x = NULL; |
0f113f3e | 80 | BIO *out = NULL; |
0f113f3e | 81 | X509_STORE *store = NULL; |
f0e0fd51 | 82 | X509_STORE_CTX *ctx = NULL; |
0f113f3e | 83 | X509_LOOKUP *lookup = NULL; |
f0e0fd51 | 84 | X509_OBJECT *xobj = NULL; |
0f113f3e | 85 | EVP_PKEY *pkey; |
606a417f | 86 | EVP_MD *digest = (EVP_MD *)EVP_sha1(); |
7e1b7485 | 87 | char *infile = NULL, *outfile = NULL, *crldiff = NULL, *keyfile = NULL; |
d0190e11 | 88 | char *digestname = NULL; |
fd3397fc | 89 | const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog; |
7e1b7485 | 90 | OPTION_CHOICE o; |
9c3bcfa0 | 91 | int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout = 0; |
d382e796 | 92 | int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, keyformat = FORMAT_UNDEF; |
9c3bcfa0 | 93 | int ret = 1, num = 0, badsig = 0, fingerprint = 0, crlnumber = 0; |
fd3397fc | 94 | int text = 0, do_ver = 0, noCAfile = 0, noCApath = 0, noCAstore = 0; |
8c5bff22 | 95 | unsigned long dateopt = ASN1_DTFLGS_RFC822; |
9c3bcfa0 | 96 | int i; |
7e1b7485 RS |
97 | #ifndef OPENSSL_NO_MD5 |
98 | int hash_old = 0; | |
645749ef | 99 | #endif |
d02b48c6 | 100 | |
2c272447 | 101 | opt_set_unknown_name("digest"); |
7e1b7485 RS |
102 | prog = opt_init(argc, argv, crl_options); |
103 | while ((o = opt_next()) != OPT_EOF) { | |
104 | switch (o) { | |
105 | case OPT_EOF: | |
106 | case OPT_ERR: | |
107 | opthelp: | |
108 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
109 | goto end; | |
110 | case OPT_HELP: | |
111 | opt_help(crl_options); | |
112 | ret = 0; | |
113 | goto end; | |
114 | case OPT_INFORM: | |
115 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
116 | goto opthelp; | |
117 | break; | |
118 | case OPT_IN: | |
119 | infile = opt_arg(); | |
120 | break; | |
121 | case OPT_OUTFORM: | |
122 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
123 | goto opthelp; | |
124 | break; | |
125 | case OPT_OUT: | |
126 | outfile = opt_arg(); | |
127 | break; | |
128 | case OPT_KEYFORM: | |
6d382c74 | 129 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat)) |
7e1b7485 RS |
130 | goto opthelp; |
131 | break; | |
132 | case OPT_KEY: | |
133 | keyfile = opt_arg(); | |
134 | break; | |
135 | case OPT_GENDELTA: | |
136 | crldiff = opt_arg(); | |
137 | break; | |
138 | case OPT_CAPATH: | |
139 | CApath = opt_arg(); | |
0f113f3e | 140 | do_ver = 1; |
7e1b7485 RS |
141 | break; |
142 | case OPT_CAFILE: | |
143 | CAfile = opt_arg(); | |
0f113f3e | 144 | do_ver = 1; |
7e1b7485 | 145 | break; |
fd3397fc RL |
146 | case OPT_CASTORE: |
147 | CAstore = opt_arg(); | |
148 | do_ver = 1; | |
149 | break; | |
2b6bcb70 MC |
150 | case OPT_NOCAPATH: |
151 | noCApath = 1; | |
152 | break; | |
153 | case OPT_NOCAFILE: | |
154 | noCAfile = 1; | |
155 | break; | |
fd3397fc RL |
156 | case OPT_NOCASTORE: |
157 | noCAstore = 1; | |
158 | break; | |
7e1b7485 | 159 | case OPT_HASH_OLD: |
9c3bcfa0 | 160 | #ifndef OPENSSL_NO_MD5 |
0f113f3e | 161 | hash_old = ++num; |
de2d97cd | 162 | #endif |
9c3bcfa0 | 163 | break; |
7e1b7485 RS |
164 | case OPT_VERIFY: |
165 | do_ver = 1; | |
166 | break; | |
8c5bff22 WE |
167 | case OPT_DATEOPT: |
168 | if (!set_dateopt(&dateopt, opt_arg())) | |
169 | goto opthelp; | |
170 | break; | |
7e1b7485 RS |
171 | case OPT_TEXT: |
172 | text = 1; | |
173 | break; | |
174 | case OPT_HASH: | |
175 | hash = ++num; | |
176 | break; | |
177 | case OPT_ISSUER: | |
0f113f3e | 178 | issuer = ++num; |
7e1b7485 RS |
179 | break; |
180 | case OPT_LASTUPDATE: | |
0f113f3e | 181 | lastupdate = ++num; |
7e1b7485 RS |
182 | break; |
183 | case OPT_NEXTUPDATE: | |
0f113f3e | 184 | nextupdate = ++num; |
7e1b7485 RS |
185 | break; |
186 | case OPT_NOOUT: | |
872b7979 | 187 | noout = 1; |
7e1b7485 RS |
188 | break; |
189 | case OPT_FINGERPRINT: | |
0f113f3e | 190 | fingerprint = ++num; |
7e1b7485 RS |
191 | break; |
192 | case OPT_CRLNUMBER: | |
0f113f3e | 193 | crlnumber = ++num; |
7e1b7485 RS |
194 | break; |
195 | case OPT_BADSIG: | |
0f113f3e | 196 | badsig = 1; |
0f113f3e | 197 | break; |
7e1b7485 | 198 | case OPT_NAMEOPT: |
b5c4209b | 199 | if (!set_nameopt(opt_arg())) |
7e1b7485 RS |
200 | goto opthelp; |
201 | break; | |
202 | case OPT_MD: | |
d0190e11 | 203 | digestname = opt_unknown(); |
6bd4e3f2 P |
204 | break; |
205 | case OPT_PROV_CASES: | |
206 | if (!opt_provider(o)) | |
207 | goto end; | |
208 | break; | |
0f113f3e | 209 | } |
0f113f3e | 210 | } |
021410ea RS |
211 | |
212 | /* No remaining args. */ | |
d9f07357 | 213 | if (!opt_check_rest_arg(NULL)) |
03358517 | 214 | goto opthelp; |
d02b48c6 | 215 | |
d9f07357 DDO |
216 | if (!opt_md(digestname, &digest)) |
217 | goto opthelp; | |
d382e796 | 218 | x = load_crl(infile, informat, 1, "CRL"); |
7e1b7485 | 219 | if (x == NULL) |
0f113f3e | 220 | goto end; |
d02b48c6 | 221 | |
0f113f3e | 222 | if (do_ver) { |
fd3397fc RL |
223 | if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath, |
224 | CAstore, noCAstore)) == NULL) | |
0f113f3e | 225 | goto end; |
7e1b7485 | 226 | lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()); |
0f113f3e MC |
227 | if (lookup == NULL) |
228 | goto end; | |
f0e0fd51 | 229 | ctx = X509_STORE_CTX_new(); |
70015713 | 230 | if (ctx == NULL || !X509_STORE_CTX_init(ctx, store, NULL, NULL)) { |
0f113f3e MC |
231 | BIO_printf(bio_err, "Error initialising X509 store\n"); |
232 | goto end; | |
233 | } | |
090d848e | 234 | |
6ddbb4cd RS |
235 | xobj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509, |
236 | X509_CRL_get_issuer(x)); | |
f0e0fd51 | 237 | if (xobj == NULL) { |
0f113f3e MC |
238 | BIO_printf(bio_err, "Error getting CRL issuer certificate\n"); |
239 | goto end; | |
240 | } | |
f0e0fd51 RS |
241 | pkey = X509_get_pubkey(X509_OBJECT_get0_X509(xobj)); |
242 | X509_OBJECT_free(xobj); | |
12a765a5 | 243 | if (pkey == NULL) { |
0f113f3e MC |
244 | BIO_printf(bio_err, "Error getting CRL issuer public key\n"); |
245 | goto end; | |
246 | } | |
247 | i = X509_CRL_verify(x, pkey); | |
f0e0fd51 | 248 | EVP_PKEY_free(pkey); |
0f113f3e MC |
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 | } | |
090d848e | 256 | |
e9d62da6 | 257 | if (crldiff != NULL) { |
0f113f3e MC |
258 | X509_CRL *newcrl, *delta; |
259 | if (!keyfile) { | |
260 | BIO_puts(bio_err, "Missing CRL signing key\n"); | |
261 | goto end; | |
262 | } | |
d382e796 | 263 | newcrl = load_crl(crldiff, informat, 0, "other CRL"); |
0f113f3e MC |
264 | if (!newcrl) |
265 | goto end; | |
7e1b7485 | 266 | pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key"); |
12a765a5 | 267 | if (pkey == NULL) { |
0f113f3e MC |
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 | } | |
2e8cb108 | 282 | |
0f022f5a | 283 | if (badsig) { |
5e6089f0 | 284 | const ASN1_BIT_STRING *sig; |
0f022f5a | 285 | |
5e6089f0 | 286 | X509_CRL_get0_signature(x, &sig, NULL); |
a0754084 | 287 | corrupt_signature(sig); |
0f022f5a DSH |
288 | } |
289 | ||
0f113f3e MC |
290 | if (num) { |
291 | for (i = 1; i <= num; i++) { | |
292 | if (issuer == i) { | |
46a11faf | 293 | print_name(bio_out, "issuer=", X509_CRL_get_issuer(x)); |
0f113f3e MC |
294 | } |
295 | if (crlnumber == i) { | |
296 | ASN1_INTEGER *crlnum; | |
bf973d06 | 297 | |
0f113f3e MC |
298 | crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL); |
299 | BIO_printf(bio_out, "crlNumber="); | |
300 | if (crlnum) { | |
6be235a0 | 301 | BIO_puts(bio_out, "0x"); |
0f113f3e MC |
302 | i2a_ASN1_INTEGER(bio_out, crlnum); |
303 | ASN1_INTEGER_free(crlnum); | |
bf973d06 | 304 | } else { |
0f113f3e | 305 | BIO_puts(bio_out, "<NONE>"); |
bf973d06 | 306 | } |
0f113f3e MC |
307 | BIO_printf(bio_out, "\n"); |
308 | } | |
309 | if (hash == i) { | |
bf973d06 DDO |
310 | int ok; |
311 | unsigned long hash_value = | |
312 | X509_NAME_hash_ex(X509_CRL_get_issuer(x), app_get0_libctx(), | |
313 | app_get0_propq(), &ok); | |
314 | ||
48fad58f TM |
315 | if (num > 1) |
316 | BIO_printf(bio_out, "issuer name hash="); | |
317 | if (ok) { | |
bf973d06 | 318 | BIO_printf(bio_out, "%08lx\n", hash_value); |
48fad58f | 319 | } else { |
bf973d06 | 320 | BIO_puts(bio_out, "<ERROR>"); |
48fad58f TM |
321 | goto end; |
322 | } | |
0f113f3e | 323 | } |
de2d97cd | 324 | #ifndef OPENSSL_NO_MD5 |
0f113f3e | 325 | if (hash_old == i) { |
48fad58f TM |
326 | if (num > 1) |
327 | BIO_printf(bio_out, "issuer name old hash="); | |
0f113f3e MC |
328 | BIO_printf(bio_out, "%08lx\n", |
329 | X509_NAME_hash_old(X509_CRL_get_issuer(x))); | |
330 | } | |
de2d97cd | 331 | #endif |
0f113f3e MC |
332 | if (lastupdate == i) { |
333 | BIO_printf(bio_out, "lastUpdate="); | |
8c5bff22 | 334 | ASN1_TIME_print_ex(bio_out, X509_CRL_get0_lastUpdate(x), dateopt); |
0f113f3e MC |
335 | BIO_printf(bio_out, "\n"); |
336 | } | |
337 | if (nextupdate == i) { | |
338 | BIO_printf(bio_out, "nextUpdate="); | |
568ce3a5 | 339 | if (X509_CRL_get0_nextUpdate(x)) |
8c5bff22 | 340 | ASN1_TIME_print_ex(bio_out, X509_CRL_get0_nextUpdate(x), dateopt); |
0f113f3e MC |
341 | else |
342 | BIO_printf(bio_out, "NONE"); | |
343 | BIO_printf(bio_out, "\n"); | |
344 | } | |
345 | if (fingerprint == i) { | |
346 | int j; | |
347 | unsigned int n; | |
348 | unsigned char md[EVP_MAX_MD_SIZE]; | |
439df508 | 349 | |
0f113f3e MC |
350 | if (!X509_CRL_digest(x, digest, md, &n)) { |
351 | BIO_printf(bio_err, "out of memory\n"); | |
352 | goto end; | |
353 | } | |
ed576acd TM |
354 | BIO_printf(bio_out, "%s Fingerprint=", |
355 | EVP_MD_get0_name(digest)); | |
0f113f3e MC |
356 | for (j = 0; j < (int)n; j++) { |
357 | BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n) | |
358 | ? '\n' : ':'); | |
359 | } | |
360 | } | |
361 | } | |
362 | } | |
bdd58d98 | 363 | out = bio_open_default(outfile, 'w', outformat); |
7e1b7485 | 364 | if (out == NULL) |
0f113f3e | 365 | goto end; |
d02b48c6 | 366 | |
0f113f3e | 367 | if (text) |
b5c4209b | 368 | X509_CRL_print_ex(out, x, get_nameopt()); |
2f0cd195 | 369 | |
0f113f3e MC |
370 | if (noout) { |
371 | ret = 0; | |
372 | goto end; | |
373 | } | |
2f0cd195 | 374 | |
0f113f3e MC |
375 | if (outformat == FORMAT_ASN1) |
376 | i = (int)i2d_X509_CRL_bio(out, x); | |
7e1b7485 | 377 | else |
0f113f3e | 378 | i = PEM_write_bio_X509_CRL(out, x); |
0f113f3e MC |
379 | if (!i) { |
380 | BIO_printf(bio_err, "unable to write CRL\n"); | |
381 | goto end; | |
382 | } | |
383 | ret = 0; | |
7e1b7485 | 384 | |
0f113f3e MC |
385 | end: |
386 | if (ret != 0) | |
387 | ERR_print_errors(bio_err); | |
388 | BIO_free_all(out); | |
606a417f | 389 | EVP_MD_free(digest); |
0f113f3e | 390 | X509_CRL_free(x); |
f0e0fd51 RS |
391 | X509_STORE_CTX_free(ctx); |
392 | X509_STORE_free(store); | |
26a7d938 | 393 | return ret; |
0f113f3e | 394 | } |