Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/openssl/apps/crl.c
105541 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,
24
OPT_IN,
25
OPT_OUTFORM,
26
OPT_OUT,
27
OPT_KEYFORM,
28
OPT_KEY,
29
OPT_ISSUER,
30
OPT_LASTUPDATE,
31
OPT_NEXTUPDATE,
32
OPT_FINGERPRINT,
33
OPT_CRLNUMBER,
34
OPT_BADSIG,
35
OPT_GENDELTA,
36
OPT_CAPATH,
37
OPT_CAFILE,
38
OPT_CASTORE,
39
OPT_NOCAPATH,
40
OPT_NOCAFILE,
41
OPT_NOCASTORE,
42
OPT_VERIFY,
43
OPT_DATEOPT,
44
OPT_TEXT,
45
OPT_HASH,
46
OPT_HASH_OLD,
47
OPT_NOOUT,
48
OPT_NAMEOPT,
49
OPT_MD,
50
OPT_PROV_ENUM
51
} OPTION_CHOICE;
52
53
const OPTIONS crl_options[] = {
54
OPT_SECTION("General"),
55
{ "help", OPT_HELP, '-', "Display this summary" },
56
{ "verify", OPT_VERIFY, '-', "Verify CRL signature" },
57
58
OPT_SECTION("Input"),
59
{ "in", OPT_IN, '<', "Input file - default stdin" },
60
{ "inform", OPT_INFORM, 'F', "CRL input format (DER or PEM); has no effect" },
61
{ "key", OPT_KEY, '<', "CRL signing Private key to use" },
62
{ "keyform", OPT_KEYFORM, 'F', "Private key file format (DER/PEM/P12); has no effect" },
63
64
OPT_SECTION("Output"),
65
{ "out", OPT_OUT, '>', "output file - default stdout" },
66
{ "outform", OPT_OUTFORM, 'F', "Output format - default PEM" },
67
{ "dateopt", OPT_DATEOPT, 's', "Datetime format used for printing. (rfc_822/iso_8601). Default is rfc_822." },
68
{ "text", OPT_TEXT, '-', "Print out a text format version" },
69
{ "hash", OPT_HASH, '-', "Print hash value" },
70
#ifndef OPENSSL_NO_MD5
71
{ "hash_old", OPT_HASH_OLD, '-', "Print old-style (MD5) hash value" },
72
#endif
73
{ "nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options" },
74
{ "", OPT_MD, '-', "Any supported digest" },
75
76
OPT_SECTION("CRL"),
77
{ "issuer", OPT_ISSUER, '-', "Print issuer DN" },
78
{ "lastupdate", OPT_LASTUPDATE, '-', "Set lastUpdate field" },
79
{ "nextupdate", OPT_NEXTUPDATE, '-', "Set nextUpdate field" },
80
{ "noout", OPT_NOOUT, '-', "No CRL output" },
81
{ "fingerprint", OPT_FINGERPRINT, '-', "Print the crl fingerprint" },
82
{ "crlnumber", OPT_CRLNUMBER, '-', "Print CRL number" },
83
{ "badsig", OPT_BADSIG, '-', "Corrupt last byte of loaded CRL signature (for test)" },
84
{ "gendelta", OPT_GENDELTA, '<', "Other CRL to compare/diff to the Input one" },
85
86
OPT_SECTION("Certificate"),
87
{ "CApath", OPT_CAPATH, '/', "Verify CRL using certificates in dir" },
88
{ "CAfile", OPT_CAFILE, '<', "Verify CRL using certificates in file name" },
89
{ "CAstore", OPT_CASTORE, ':', "Verify CRL using certificates in store URI" },
90
{ "no-CAfile", OPT_NOCAFILE, '-',
91
"Do not load the default certificates file" },
92
{ "no-CApath", OPT_NOCAPATH, '-',
93
"Do not load certificates from the default certificates directory" },
94
{ "no-CAstore", OPT_NOCASTORE, '-',
95
"Do not load certificates from the default certificates store" },
96
OPT_PROV_OPTIONS,
97
{ NULL }
98
};
99
100
int crl_main(int argc, char **argv)
101
{
102
X509_CRL *x = NULL;
103
BIO *out = NULL;
104
X509_STORE *store = NULL;
105
X509_STORE_CTX *ctx = NULL;
106
X509_LOOKUP *lookup = NULL;
107
X509_OBJECT *xobj = NULL;
108
EVP_PKEY *pkey;
109
EVP_MD *digest = (EVP_MD *)EVP_sha1();
110
char *infile = NULL, *outfile = NULL, *crldiff = NULL, *keyfile = NULL;
111
char *digestname = NULL;
112
const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog;
113
OPTION_CHOICE o;
114
int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout = 0;
115
int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, keyformat = FORMAT_UNDEF;
116
int ret = 1, num = 0, badsig = 0, fingerprint = 0, crlnumber = 0;
117
int text = 0, do_ver = 0, noCAfile = 0, noCApath = 0, noCAstore = 0;
118
unsigned long dateopt = ASN1_DTFLGS_RFC822;
119
int i;
120
#ifndef OPENSSL_NO_MD5
121
int hash_old = 0;
122
#endif
123
124
opt_set_unknown_name("digest");
125
prog = opt_init(argc, argv, crl_options);
126
while ((o = opt_next()) != OPT_EOF) {
127
switch (o) {
128
case OPT_EOF:
129
case OPT_ERR:
130
opthelp:
131
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
132
goto end;
133
case OPT_HELP:
134
opt_help(crl_options);
135
ret = 0;
136
goto end;
137
case OPT_INFORM:
138
if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
139
goto opthelp;
140
break;
141
case OPT_IN:
142
infile = opt_arg();
143
break;
144
case OPT_OUTFORM:
145
if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
146
goto opthelp;
147
break;
148
case OPT_OUT:
149
outfile = opt_arg();
150
break;
151
case OPT_KEYFORM:
152
if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
153
goto opthelp;
154
break;
155
case OPT_KEY:
156
keyfile = opt_arg();
157
break;
158
case OPT_GENDELTA:
159
crldiff = opt_arg();
160
break;
161
case OPT_CAPATH:
162
CApath = opt_arg();
163
do_ver = 1;
164
break;
165
case OPT_CAFILE:
166
CAfile = opt_arg();
167
do_ver = 1;
168
break;
169
case OPT_CASTORE:
170
CAstore = opt_arg();
171
do_ver = 1;
172
break;
173
case OPT_NOCAPATH:
174
noCApath = 1;
175
break;
176
case OPT_NOCAFILE:
177
noCAfile = 1;
178
break;
179
case OPT_NOCASTORE:
180
noCAstore = 1;
181
break;
182
case OPT_HASH_OLD:
183
#ifndef OPENSSL_NO_MD5
184
hash_old = ++num;
185
#endif
186
break;
187
case OPT_VERIFY:
188
do_ver = 1;
189
break;
190
case OPT_DATEOPT:
191
if (!set_dateopt(&dateopt, opt_arg()))
192
goto opthelp;
193
break;
194
case OPT_TEXT:
195
text = 1;
196
break;
197
case OPT_HASH:
198
hash = ++num;
199
break;
200
case OPT_ISSUER:
201
issuer = ++num;
202
break;
203
case OPT_LASTUPDATE:
204
lastupdate = ++num;
205
break;
206
case OPT_NEXTUPDATE:
207
nextupdate = ++num;
208
break;
209
case OPT_NOOUT:
210
noout = 1;
211
break;
212
case OPT_FINGERPRINT:
213
fingerprint = ++num;
214
break;
215
case OPT_CRLNUMBER:
216
crlnumber = ++num;
217
break;
218
case OPT_BADSIG:
219
badsig = 1;
220
break;
221
case OPT_NAMEOPT:
222
if (!set_nameopt(opt_arg()))
223
goto opthelp;
224
break;
225
case OPT_MD:
226
digestname = opt_unknown();
227
break;
228
case OPT_PROV_CASES:
229
if (!opt_provider(o))
230
goto end;
231
break;
232
}
233
}
234
235
/* No remaining args. */
236
if (!opt_check_rest_arg(NULL))
237
goto opthelp;
238
239
if (!opt_md(digestname, &digest))
240
goto opthelp;
241
x = load_crl(infile, informat, 1, "CRL");
242
if (x == NULL)
243
goto end;
244
245
if (do_ver) {
246
if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
247
CAstore, noCAstore))
248
== NULL)
249
goto end;
250
lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
251
if (lookup == NULL)
252
goto end;
253
ctx = X509_STORE_CTX_new();
254
if (ctx == NULL || !X509_STORE_CTX_init(ctx, store, NULL, NULL)) {
255
BIO_printf(bio_err, "Error initialising X509 store\n");
256
goto end;
257
}
258
259
xobj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509,
260
X509_CRL_get_issuer(x));
261
if (xobj == NULL) {
262
BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
263
goto end;
264
}
265
pkey = X509_get_pubkey(X509_OBJECT_get0_X509(xobj));
266
X509_OBJECT_free(xobj);
267
if (pkey == NULL) {
268
BIO_printf(bio_err, "Error getting CRL issuer public key\n");
269
goto end;
270
}
271
i = X509_CRL_verify(x, pkey);
272
EVP_PKEY_free(pkey);
273
if (i < 0)
274
goto end;
275
if (i == 0) {
276
BIO_printf(bio_err, "verify failure\n");
277
goto end;
278
} else
279
BIO_printf(bio_err, "verify OK\n");
280
}
281
282
if (crldiff != NULL) {
283
X509_CRL *newcrl, *delta;
284
if (!keyfile) {
285
BIO_puts(bio_err, "Missing CRL signing key\n");
286
goto end;
287
}
288
newcrl = load_crl(crldiff, informat, 0, "other CRL");
289
if (!newcrl)
290
goto end;
291
pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
292
if (pkey == NULL) {
293
X509_CRL_free(newcrl);
294
goto end;
295
}
296
delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
297
X509_CRL_free(newcrl);
298
EVP_PKEY_free(pkey);
299
if (delta) {
300
X509_CRL_free(x);
301
x = delta;
302
} else {
303
BIO_puts(bio_err, "Error creating delta CRL\n");
304
goto end;
305
}
306
}
307
308
if (badsig) {
309
const ASN1_BIT_STRING *sig;
310
311
X509_CRL_get0_signature(x, &sig, NULL);
312
corrupt_signature(sig);
313
}
314
315
if (num) {
316
for (i = 1; i <= num; i++) {
317
if (issuer == i) {
318
print_name(bio_out, "issuer=", X509_CRL_get_issuer(x));
319
}
320
if (crlnumber == i) {
321
ASN1_INTEGER *crlnum;
322
323
crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
324
BIO_printf(bio_out, "crlNumber=");
325
if (crlnum) {
326
BIO_puts(bio_out, "0x");
327
i2a_ASN1_INTEGER(bio_out, crlnum);
328
ASN1_INTEGER_free(crlnum);
329
} else {
330
BIO_puts(bio_out, "<NONE>");
331
}
332
BIO_printf(bio_out, "\n");
333
}
334
if (hash == i) {
335
int ok;
336
unsigned long hash_value = X509_NAME_hash_ex(X509_CRL_get_issuer(x), app_get0_libctx(),
337
app_get0_propq(), &ok);
338
339
if (num > 1)
340
BIO_printf(bio_out, "issuer name hash=");
341
if (ok) {
342
BIO_printf(bio_out, "%08lx\n", hash_value);
343
} else {
344
BIO_puts(bio_out, "<ERROR>");
345
goto end;
346
}
347
}
348
#ifndef OPENSSL_NO_MD5
349
if (hash_old == i) {
350
if (num > 1)
351
BIO_printf(bio_out, "issuer name old hash=");
352
BIO_printf(bio_out, "%08lx\n",
353
X509_NAME_hash_old(X509_CRL_get_issuer(x)));
354
}
355
#endif
356
if (lastupdate == i) {
357
BIO_printf(bio_out, "lastUpdate=");
358
ASN1_TIME_print_ex(bio_out, X509_CRL_get0_lastUpdate(x), dateopt);
359
BIO_printf(bio_out, "\n");
360
}
361
if (nextupdate == i) {
362
BIO_printf(bio_out, "nextUpdate=");
363
if (X509_CRL_get0_nextUpdate(x))
364
ASN1_TIME_print_ex(bio_out, X509_CRL_get0_nextUpdate(x), dateopt);
365
else
366
BIO_printf(bio_out, "NONE");
367
BIO_printf(bio_out, "\n");
368
}
369
if (fingerprint == i) {
370
int j;
371
unsigned int n;
372
unsigned char md[EVP_MAX_MD_SIZE];
373
374
if (!X509_CRL_digest(x, digest, md, &n)) {
375
BIO_printf(bio_err, "out of memory\n");
376
goto end;
377
}
378
BIO_printf(bio_out, "%s Fingerprint=",
379
EVP_MD_get0_name(digest));
380
for (j = 0; j < (int)n; j++) {
381
BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n) ? '\n' : ':');
382
}
383
}
384
}
385
}
386
out = bio_open_default(outfile, 'w', outformat);
387
if (out == NULL)
388
goto end;
389
390
if (text)
391
X509_CRL_print_ex(out, x, get_nameopt());
392
393
if (noout) {
394
ret = 0;
395
goto end;
396
}
397
398
if (outformat == FORMAT_ASN1)
399
i = (int)i2d_X509_CRL_bio(out, x);
400
else
401
i = PEM_write_bio_X509_CRL(out, x);
402
if (!i) {
403
BIO_printf(bio_err, "unable to write CRL\n");
404
goto end;
405
}
406
ret = 0;
407
408
end:
409
if (ret != 0)
410
ERR_print_errors(bio_err);
411
BIO_free_all(out);
412
EVP_MD_free(digest);
413
X509_CRL_free(x);
414
X509_STORE_CTX_free(ctx);
415
X509_STORE_free(store);
416
return ret;
417
}
418
419