]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
6738bf14 | 2 | * Copyright 1995-2018 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 RS |
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, | |
fd3397fc RL |
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, | |
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"}, |
5388f986 | 37 | {"inform", OPT_INFORM, 'F', "Input format; default PEM"}, |
16e1b281 | 38 | {"key", OPT_KEY, '<', "CRL signing Private key to use"}, |
5388f986 RS |
39 | {"keyform", OPT_KEYFORM, 'F', "Private key file format (PEM or ENGINE)"}, |
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', "Various certificate name options"}, | |
50 | {"", OPT_MD, '-', "Any supported digest"}, | |
51 | ||
52 | OPT_SECTION("CRL"), | |
7e1b7485 RS |
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"}, | |
16e1b281 | 59 | {"badsig", OPT_BADSIG, '-', "Corrupt last byte of loaded CRL signature (for test)" }, |
12d56b29 | 60 | {"gendelta", OPT_GENDELTA, '<', "Other CRL to compare/diff to the Input one"}, |
5388f986 RS |
61 | |
62 | OPT_SECTION("Certificate"), | |
7e1b7485 RS |
63 | {"CApath", OPT_CAPATH, '/', "Verify CRL using certificates in dir"}, |
64 | {"CAfile", OPT_CAFILE, '<', "Verify CRL using certificates in file name"}, | |
fd3397fc | 65 | {"CAstore", OPT_CASTORE, ':', "Verify CRL using certificates in store URI"}, |
2b6bcb70 MC |
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"}, | |
fd3397fc RL |
70 | {"no-CAstore", OPT_NOCASTORE, '-', |
71 | "Do not load certificates from the default certificates store"}, | |
6bd4e3f2 | 72 | OPT_PROV_OPTIONS, |
7e1b7485 | 73 | {NULL} |
d02b48c6 RE |
74 | }; |
75 | ||
7e1b7485 | 76 | int crl_main(int argc, char **argv) |
0f113f3e | 77 | { |
0f113f3e | 78 | X509_CRL *x = NULL; |
0f113f3e | 79 | BIO *out = NULL; |
0f113f3e | 80 | X509_STORE *store = NULL; |
f0e0fd51 | 81 | X509_STORE_CTX *ctx = NULL; |
0f113f3e | 82 | X509_LOOKUP *lookup = NULL; |
f0e0fd51 | 83 | X509_OBJECT *xobj = NULL; |
0f113f3e | 84 | EVP_PKEY *pkey; |
7e1b7485 | 85 | const EVP_MD *digest = EVP_sha1(); |
7e1b7485 | 86 | char *infile = NULL, *outfile = NULL, *crldiff = NULL, *keyfile = NULL; |
fd3397fc | 87 | const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog; |
7e1b7485 | 88 | OPTION_CHOICE o; |
9c3bcfa0 | 89 | int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout = 0; |
7e1b7485 | 90 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM; |
9c3bcfa0 | 91 | int ret = 1, num = 0, badsig = 0, fingerprint = 0, crlnumber = 0; |
fd3397fc | 92 | int text = 0, do_ver = 0, noCAfile = 0, noCApath = 0, noCAstore = 0; |
9c3bcfa0 | 93 | int i; |
7e1b7485 RS |
94 | #ifndef OPENSSL_NO_MD5 |
95 | int hash_old = 0; | |
645749ef | 96 | #endif |
d02b48c6 | 97 | |
7e1b7485 RS |
98 | prog = opt_init(argc, argv, crl_options); |
99 | while ((o = opt_next()) != OPT_EOF) { | |
100 | switch (o) { | |
101 | case OPT_EOF: | |
102 | case OPT_ERR: | |
103 | opthelp: | |
104 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
105 | goto end; | |
106 | case OPT_HELP: | |
107 | opt_help(crl_options); | |
108 | ret = 0; | |
109 | goto end; | |
110 | case OPT_INFORM: | |
111 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat)) | |
112 | goto opthelp; | |
113 | break; | |
114 | case OPT_IN: | |
115 | infile = opt_arg(); | |
116 | break; | |
117 | case OPT_OUTFORM: | |
118 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
119 | goto opthelp; | |
120 | break; | |
121 | case OPT_OUT: | |
122 | outfile = opt_arg(); | |
123 | break; | |
124 | case OPT_KEYFORM: | |
125 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat)) | |
126 | goto opthelp; | |
127 | break; | |
128 | case OPT_KEY: | |
129 | keyfile = opt_arg(); | |
130 | break; | |
131 | case OPT_GENDELTA: | |
132 | crldiff = opt_arg(); | |
133 | break; | |
134 | case OPT_CAPATH: | |
135 | CApath = opt_arg(); | |
0f113f3e | 136 | do_ver = 1; |
7e1b7485 RS |
137 | break; |
138 | case OPT_CAFILE: | |
139 | CAfile = opt_arg(); | |
0f113f3e | 140 | do_ver = 1; |
7e1b7485 | 141 | break; |
fd3397fc RL |
142 | case OPT_CASTORE: |
143 | CAstore = opt_arg(); | |
144 | do_ver = 1; | |
145 | break; | |
2b6bcb70 MC |
146 | case OPT_NOCAPATH: |
147 | noCApath = 1; | |
148 | break; | |
149 | case OPT_NOCAFILE: | |
150 | noCAfile = 1; | |
151 | break; | |
fd3397fc RL |
152 | case OPT_NOCASTORE: |
153 | noCAstore = 1; | |
154 | break; | |
7e1b7485 | 155 | case OPT_HASH_OLD: |
9c3bcfa0 | 156 | #ifndef OPENSSL_NO_MD5 |
0f113f3e | 157 | hash_old = ++num; |
de2d97cd | 158 | #endif |
9c3bcfa0 | 159 | break; |
7e1b7485 RS |
160 | case OPT_VERIFY: |
161 | do_ver = 1; | |
162 | break; | |
163 | case OPT_TEXT: | |
164 | text = 1; | |
165 | break; | |
166 | case OPT_HASH: | |
167 | hash = ++num; | |
168 | break; | |
169 | case OPT_ISSUER: | |
0f113f3e | 170 | issuer = ++num; |
7e1b7485 RS |
171 | break; |
172 | case OPT_LASTUPDATE: | |
0f113f3e | 173 | lastupdate = ++num; |
7e1b7485 RS |
174 | break; |
175 | case OPT_NEXTUPDATE: | |
0f113f3e | 176 | nextupdate = ++num; |
7e1b7485 RS |
177 | break; |
178 | case OPT_NOOUT: | |
0f113f3e | 179 | noout = ++num; |
7e1b7485 RS |
180 | break; |
181 | case OPT_FINGERPRINT: | |
0f113f3e | 182 | fingerprint = ++num; |
7e1b7485 RS |
183 | break; |
184 | case OPT_CRLNUMBER: | |
0f113f3e | 185 | crlnumber = ++num; |
7e1b7485 RS |
186 | break; |
187 | case OPT_BADSIG: | |
0f113f3e | 188 | badsig = 1; |
0f113f3e | 189 | break; |
7e1b7485 | 190 | case OPT_NAMEOPT: |
b5c4209b | 191 | if (!set_nameopt(opt_arg())) |
7e1b7485 RS |
192 | goto opthelp; |
193 | break; | |
194 | case OPT_MD: | |
195 | if (!opt_md(opt_unknown(), &digest)) | |
196 | goto opthelp; | |
6bd4e3f2 P |
197 | break; |
198 | case OPT_PROV_CASES: | |
199 | if (!opt_provider(o)) | |
200 | goto end; | |
201 | break; | |
0f113f3e | 202 | } |
0f113f3e | 203 | } |
7e1b7485 | 204 | argc = opt_num_rest(); |
03358517 KR |
205 | if (argc != 0) |
206 | goto opthelp; | |
d02b48c6 | 207 | |
0f113f3e | 208 | x = load_crl(infile, informat); |
7e1b7485 | 209 | if (x == NULL) |
0f113f3e | 210 | goto end; |
d02b48c6 | 211 | |
0f113f3e | 212 | if (do_ver) { |
fd3397fc RL |
213 | if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath, |
214 | CAstore, noCAstore)) == NULL) | |
0f113f3e | 215 | goto end; |
7e1b7485 | 216 | lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()); |
0f113f3e MC |
217 | if (lookup == NULL) |
218 | goto end; | |
f0e0fd51 | 219 | ctx = X509_STORE_CTX_new(); |
70015713 | 220 | if (ctx == NULL || !X509_STORE_CTX_init(ctx, store, NULL, NULL)) { |
0f113f3e MC |
221 | BIO_printf(bio_err, "Error initialising X509 store\n"); |
222 | goto end; | |
223 | } | |
090d848e | 224 | |
6ddbb4cd RS |
225 | xobj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509, |
226 | X509_CRL_get_issuer(x)); | |
f0e0fd51 | 227 | if (xobj == NULL) { |
0f113f3e MC |
228 | BIO_printf(bio_err, "Error getting CRL issuer certificate\n"); |
229 | goto end; | |
230 | } | |
f0e0fd51 RS |
231 | pkey = X509_get_pubkey(X509_OBJECT_get0_X509(xobj)); |
232 | X509_OBJECT_free(xobj); | |
12a765a5 | 233 | if (pkey == NULL) { |
0f113f3e MC |
234 | BIO_printf(bio_err, "Error getting CRL issuer public key\n"); |
235 | goto end; | |
236 | } | |
237 | i = X509_CRL_verify(x, pkey); | |
f0e0fd51 | 238 | EVP_PKEY_free(pkey); |
0f113f3e MC |
239 | if (i < 0) |
240 | goto end; | |
241 | if (i == 0) | |
242 | BIO_printf(bio_err, "verify failure\n"); | |
243 | else | |
244 | BIO_printf(bio_err, "verify OK\n"); | |
245 | } | |
090d848e | 246 | |
0f113f3e MC |
247 | if (crldiff) { |
248 | X509_CRL *newcrl, *delta; | |
249 | if (!keyfile) { | |
250 | BIO_puts(bio_err, "Missing CRL signing key\n"); | |
251 | goto end; | |
252 | } | |
253 | newcrl = load_crl(crldiff, informat); | |
254 | if (!newcrl) | |
255 | goto end; | |
7e1b7485 | 256 | pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key"); |
12a765a5 | 257 | if (pkey == NULL) { |
0f113f3e MC |
258 | X509_CRL_free(newcrl); |
259 | goto end; | |
260 | } | |
261 | delta = X509_CRL_diff(x, newcrl, pkey, digest, 0); | |
262 | X509_CRL_free(newcrl); | |
263 | EVP_PKEY_free(pkey); | |
264 | if (delta) { | |
265 | X509_CRL_free(x); | |
266 | x = delta; | |
267 | } else { | |
268 | BIO_puts(bio_err, "Error creating delta CRL\n"); | |
269 | goto end; | |
270 | } | |
271 | } | |
2e8cb108 | 272 | |
0f022f5a | 273 | if (badsig) { |
5e6089f0 | 274 | const ASN1_BIT_STRING *sig; |
0f022f5a | 275 | |
5e6089f0 | 276 | X509_CRL_get0_signature(x, &sig, NULL); |
a0754084 | 277 | corrupt_signature(sig); |
0f022f5a DSH |
278 | } |
279 | ||
0f113f3e MC |
280 | if (num) { |
281 | for (i = 1; i <= num; i++) { | |
282 | if (issuer == i) { | |
283 | print_name(bio_out, "issuer=", X509_CRL_get_issuer(x), | |
b5c4209b | 284 | get_nameopt()); |
0f113f3e MC |
285 | } |
286 | if (crlnumber == i) { | |
287 | ASN1_INTEGER *crlnum; | |
288 | crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL); | |
289 | BIO_printf(bio_out, "crlNumber="); | |
290 | if (crlnum) { | |
291 | i2a_ASN1_INTEGER(bio_out, crlnum); | |
292 | ASN1_INTEGER_free(crlnum); | |
293 | } else | |
294 | BIO_puts(bio_out, "<NONE>"); | |
295 | BIO_printf(bio_out, "\n"); | |
296 | } | |
297 | if (hash == i) { | |
298 | BIO_printf(bio_out, "%08lx\n", | |
299 | X509_NAME_hash(X509_CRL_get_issuer(x))); | |
300 | } | |
de2d97cd | 301 | #ifndef OPENSSL_NO_MD5 |
0f113f3e MC |
302 | if (hash_old == i) { |
303 | BIO_printf(bio_out, "%08lx\n", | |
304 | X509_NAME_hash_old(X509_CRL_get_issuer(x))); | |
305 | } | |
de2d97cd | 306 | #endif |
0f113f3e MC |
307 | if (lastupdate == i) { |
308 | BIO_printf(bio_out, "lastUpdate="); | |
568ce3a5 | 309 | ASN1_TIME_print(bio_out, X509_CRL_get0_lastUpdate(x)); |
0f113f3e MC |
310 | BIO_printf(bio_out, "\n"); |
311 | } | |
312 | if (nextupdate == i) { | |
313 | BIO_printf(bio_out, "nextUpdate="); | |
568ce3a5 DSH |
314 | if (X509_CRL_get0_nextUpdate(x)) |
315 | ASN1_TIME_print(bio_out, X509_CRL_get0_nextUpdate(x)); | |
0f113f3e MC |
316 | else |
317 | BIO_printf(bio_out, "NONE"); | |
318 | BIO_printf(bio_out, "\n"); | |
319 | } | |
320 | if (fingerprint == i) { | |
321 | int j; | |
322 | unsigned int n; | |
323 | unsigned char md[EVP_MAX_MD_SIZE]; | |
439df508 | 324 | |
0f113f3e MC |
325 | if (!X509_CRL_digest(x, digest, md, &n)) { |
326 | BIO_printf(bio_err, "out of memory\n"); | |
327 | goto end; | |
328 | } | |
329 | BIO_printf(bio_out, "%s Fingerprint=", | |
330 | OBJ_nid2sn(EVP_MD_type(digest))); | |
331 | for (j = 0; j < (int)n; j++) { | |
332 | BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n) | |
333 | ? '\n' : ':'); | |
334 | } | |
335 | } | |
336 | } | |
337 | } | |
bdd58d98 | 338 | out = bio_open_default(outfile, 'w', outformat); |
7e1b7485 | 339 | if (out == NULL) |
0f113f3e | 340 | goto end; |
d02b48c6 | 341 | |
0f113f3e | 342 | if (text) |
b5c4209b | 343 | X509_CRL_print_ex(out, x, get_nameopt()); |
2f0cd195 | 344 | |
0f113f3e MC |
345 | if (noout) { |
346 | ret = 0; | |
347 | goto end; | |
348 | } | |
2f0cd195 | 349 | |
0f113f3e MC |
350 | if (outformat == FORMAT_ASN1) |
351 | i = (int)i2d_X509_CRL_bio(out, x); | |
7e1b7485 | 352 | else |
0f113f3e | 353 | i = PEM_write_bio_X509_CRL(out, x); |
0f113f3e MC |
354 | if (!i) { |
355 | BIO_printf(bio_err, "unable to write CRL\n"); | |
356 | goto end; | |
357 | } | |
358 | ret = 0; | |
7e1b7485 | 359 | |
0f113f3e MC |
360 | end: |
361 | if (ret != 0) | |
362 | ERR_print_errors(bio_err); | |
363 | BIO_free_all(out); | |
0f113f3e | 364 | X509_CRL_free(x); |
f0e0fd51 RS |
365 | X509_STORE_CTX_free(ctx); |
366 | X509_STORE_free(store); | |
26a7d938 | 367 | return ret; |
0f113f3e | 368 | } |