Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/openssl/apps/crl.c
34889 views
1
/*
2
* Copyright 1995-2024 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_COMMON,
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_DATEOPT, 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
{"dateopt", OPT_DATEOPT, 's', "Datetime format used for printing. (rfc_822/iso_8601). Default is rfc_822."},
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
50
{"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
51
{"", OPT_MD, '-', "Any supported digest"},
52
53
OPT_SECTION("CRL"),
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"},
60
{"badsig", OPT_BADSIG, '-', "Corrupt last byte of loaded CRL signature (for test)" },
61
{"gendelta", OPT_GENDELTA, '<', "Other CRL to compare/diff to the Input one"},
62
63
OPT_SECTION("Certificate"),
64
{"CApath", OPT_CAPATH, '/', "Verify CRL using certificates in dir"},
65
{"CAfile", OPT_CAFILE, '<', "Verify CRL using certificates in file name"},
66
{"CAstore", OPT_CASTORE, ':', "Verify CRL using certificates in store URI"},
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"},
71
{"no-CAstore", OPT_NOCASTORE, '-',
72
"Do not load certificates from the default certificates store"},
73
OPT_PROV_OPTIONS,
74
{NULL}
75
};
76
77
int crl_main(int argc, char **argv)
78
{
79
X509_CRL *x = NULL;
80
BIO *out = NULL;
81
X509_STORE *store = NULL;
82
X509_STORE_CTX *ctx = NULL;
83
X509_LOOKUP *lookup = NULL;
84
X509_OBJECT *xobj = NULL;
85
EVP_PKEY *pkey;
86
EVP_MD *digest = (EVP_MD *)EVP_sha1();
87
char *infile = NULL, *outfile = NULL, *crldiff = NULL, *keyfile = NULL;
88
char *digestname = NULL;
89
const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog;
90
OPTION_CHOICE o;
91
int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout = 0;
92
int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, keyformat = FORMAT_UNDEF;
93
int ret = 1, num = 0, badsig = 0, fingerprint = 0, crlnumber = 0;
94
int text = 0, do_ver = 0, noCAfile = 0, noCApath = 0, noCAstore = 0;
95
unsigned long dateopt = ASN1_DTFLGS_RFC822;
96
int i;
97
#ifndef OPENSSL_NO_MD5
98
int hash_old = 0;
99
#endif
100
101
opt_set_unknown_name("digest");
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:
129
if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
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();
140
do_ver = 1;
141
break;
142
case OPT_CAFILE:
143
CAfile = opt_arg();
144
do_ver = 1;
145
break;
146
case OPT_CASTORE:
147
CAstore = opt_arg();
148
do_ver = 1;
149
break;
150
case OPT_NOCAPATH:
151
noCApath = 1;
152
break;
153
case OPT_NOCAFILE:
154
noCAfile = 1;
155
break;
156
case OPT_NOCASTORE:
157
noCAstore = 1;
158
break;
159
case OPT_HASH_OLD:
160
#ifndef OPENSSL_NO_MD5
161
hash_old = ++num;
162
#endif
163
break;
164
case OPT_VERIFY:
165
do_ver = 1;
166
break;
167
case OPT_DATEOPT:
168
if (!set_dateopt(&dateopt, opt_arg()))
169
goto opthelp;
170
break;
171
case OPT_TEXT:
172
text = 1;
173
break;
174
case OPT_HASH:
175
hash = ++num;
176
break;
177
case OPT_ISSUER:
178
issuer = ++num;
179
break;
180
case OPT_LASTUPDATE:
181
lastupdate = ++num;
182
break;
183
case OPT_NEXTUPDATE:
184
nextupdate = ++num;
185
break;
186
case OPT_NOOUT:
187
noout = 1;
188
break;
189
case OPT_FINGERPRINT:
190
fingerprint = ++num;
191
break;
192
case OPT_CRLNUMBER:
193
crlnumber = ++num;
194
break;
195
case OPT_BADSIG:
196
badsig = 1;
197
break;
198
case OPT_NAMEOPT:
199
if (!set_nameopt(opt_arg()))
200
goto opthelp;
201
break;
202
case OPT_MD:
203
digestname = opt_unknown();
204
break;
205
case OPT_PROV_CASES:
206
if (!opt_provider(o))
207
goto end;
208
break;
209
}
210
}
211
212
/* No remaining args. */
213
if (!opt_check_rest_arg(NULL))
214
goto opthelp;
215
216
if (!opt_md(digestname, &digest))
217
goto opthelp;
218
x = load_crl(infile, informat, 1, "CRL");
219
if (x == NULL)
220
goto end;
221
222
if (do_ver) {
223
if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
224
CAstore, noCAstore)) == NULL)
225
goto end;
226
lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
227
if (lookup == NULL)
228
goto end;
229
ctx = X509_STORE_CTX_new();
230
if (ctx == NULL || !X509_STORE_CTX_init(ctx, store, NULL, NULL)) {
231
BIO_printf(bio_err, "Error initialising X509 store\n");
232
goto end;
233
}
234
235
xobj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509,
236
X509_CRL_get_issuer(x));
237
if (xobj == NULL) {
238
BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
239
goto end;
240
}
241
pkey = X509_get_pubkey(X509_OBJECT_get0_X509(xobj));
242
X509_OBJECT_free(xobj);
243
if (pkey == NULL) {
244
BIO_printf(bio_err, "Error getting CRL issuer public key\n");
245
goto end;
246
}
247
i = X509_CRL_verify(x, pkey);
248
EVP_PKEY_free(pkey);
249
if (i < 0)
250
goto end;
251
if (i == 0) {
252
BIO_printf(bio_err, "verify failure\n");
253
goto end;
254
} else
255
BIO_printf(bio_err, "verify OK\n");
256
}
257
258
if (crldiff != NULL) {
259
X509_CRL *newcrl, *delta;
260
if (!keyfile) {
261
BIO_puts(bio_err, "Missing CRL signing key\n");
262
goto end;
263
}
264
newcrl = load_crl(crldiff, informat, 0, "other CRL");
265
if (!newcrl)
266
goto end;
267
pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
268
if (pkey == NULL) {
269
X509_CRL_free(newcrl);
270
goto end;
271
}
272
delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
273
X509_CRL_free(newcrl);
274
EVP_PKEY_free(pkey);
275
if (delta) {
276
X509_CRL_free(x);
277
x = delta;
278
} else {
279
BIO_puts(bio_err, "Error creating delta CRL\n");
280
goto end;
281
}
282
}
283
284
if (badsig) {
285
const ASN1_BIT_STRING *sig;
286
287
X509_CRL_get0_signature(x, &sig, NULL);
288
corrupt_signature(sig);
289
}
290
291
if (num) {
292
for (i = 1; i <= num; i++) {
293
if (issuer == i) {
294
print_name(bio_out, "issuer=", X509_CRL_get_issuer(x));
295
}
296
if (crlnumber == i) {
297
ASN1_INTEGER *crlnum;
298
299
crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
300
BIO_printf(bio_out, "crlNumber=");
301
if (crlnum) {
302
BIO_puts(bio_out, "0x");
303
i2a_ASN1_INTEGER(bio_out, crlnum);
304
ASN1_INTEGER_free(crlnum);
305
} else {
306
BIO_puts(bio_out, "<NONE>");
307
}
308
BIO_printf(bio_out, "\n");
309
}
310
if (hash == i) {
311
int ok;
312
unsigned long hash_value =
313
X509_NAME_hash_ex(X509_CRL_get_issuer(x), app_get0_libctx(),
314
app_get0_propq(), &ok);
315
316
if (num > 1)
317
BIO_printf(bio_out, "issuer name hash=");
318
if (ok) {
319
BIO_printf(bio_out, "%08lx\n", hash_value);
320
} else {
321
BIO_puts(bio_out, "<ERROR>");
322
goto end;
323
}
324
}
325
#ifndef OPENSSL_NO_MD5
326
if (hash_old == i) {
327
if (num > 1)
328
BIO_printf(bio_out, "issuer name old hash=");
329
BIO_printf(bio_out, "%08lx\n",
330
X509_NAME_hash_old(X509_CRL_get_issuer(x)));
331
}
332
#endif
333
if (lastupdate == i) {
334
BIO_printf(bio_out, "lastUpdate=");
335
ASN1_TIME_print_ex(bio_out, X509_CRL_get0_lastUpdate(x), dateopt);
336
BIO_printf(bio_out, "\n");
337
}
338
if (nextupdate == i) {
339
BIO_printf(bio_out, "nextUpdate=");
340
if (X509_CRL_get0_nextUpdate(x))
341
ASN1_TIME_print_ex(bio_out, X509_CRL_get0_nextUpdate(x), dateopt);
342
else
343
BIO_printf(bio_out, "NONE");
344
BIO_printf(bio_out, "\n");
345
}
346
if (fingerprint == i) {
347
int j;
348
unsigned int n;
349
unsigned char md[EVP_MAX_MD_SIZE];
350
351
if (!X509_CRL_digest(x, digest, md, &n)) {
352
BIO_printf(bio_err, "out of memory\n");
353
goto end;
354
}
355
BIO_printf(bio_out, "%s Fingerprint=",
356
EVP_MD_get0_name(digest));
357
for (j = 0; j < (int)n; j++) {
358
BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
359
? '\n' : ':');
360
}
361
}
362
}
363
}
364
out = bio_open_default(outfile, 'w', outformat);
365
if (out == NULL)
366
goto end;
367
368
if (text)
369
X509_CRL_print_ex(out, x, get_nameopt());
370
371
if (noout) {
372
ret = 0;
373
goto end;
374
}
375
376
if (outformat == FORMAT_ASN1)
377
i = (int)i2d_X509_CRL_bio(out, x);
378
else
379
i = PEM_write_bio_X509_CRL(out, x);
380
if (!i) {
381
BIO_printf(bio_err, "unable to write CRL\n");
382
goto end;
383
}
384
ret = 0;
385
386
end:
387
if (ret != 0)
388
ERR_print_errors(bio_err);
389
BIO_free_all(out);
390
EVP_MD_free(digest);
391
X509_CRL_free(x);
392
X509_STORE_CTX_free(ctx);
393
X509_STORE_free(store);
394
return ret;
395
}
396
397