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