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