| 1 |
/* |
| 2 |
* Copyright 1995-2025 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 |
|
| 283 |
if (crldiff != NULL) { |
| 284 |
X509_CRL *newcrl, *delta; |
| 285 |
if (!keyfile) { |
| 286 |
BIO_puts(bio_err, "Missing CRL signing key\n"); |
| 287 |
goto end; |
| 288 |
} |
| 289 |
newcrl = load_crl(crldiff, informat, 0, "other CRL"); |
| 290 |
if (!newcrl) |
| 291 |
goto end; |
| 292 |
pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key"); |
| 293 |
if (pkey == NULL) { |
| 294 |
X509_CRL_free(newcrl); |
| 295 |
goto end; |
| 296 |
} |
| 297 |
delta = X509_CRL_diff(x, newcrl, pkey, digest, 0); |
| 298 |
X509_CRL_free(newcrl); |
| 299 |
EVP_PKEY_free(pkey); |
| 300 |
if (delta) { |
| 301 |
X509_CRL_free(x); |
| 302 |
x = delta; |
| 303 |
} else { |
| 304 |
BIO_puts(bio_err, "Error creating delta CRL\n"); |
| 305 |
goto end; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
if (badsig) { |
| 310 |
const ASN1_BIT_STRING *sig; |
| 311 |
|
| 312 |
X509_CRL_get0_signature(x, &sig, NULL); |
| 313 |
corrupt_signature(sig); |
| 314 |
} |
| 315 |
|
| 316 |
if (num) { |
| 317 |
for (i = 1; i <= num; i++) { |
| 318 |
if (issuer == i) { |
| 319 |
print_name(bio_out, "issuer=", X509_CRL_get_issuer(x)); |
| 320 |
} |
| 321 |
if (crlnumber == i) { |
| 322 |
ASN1_INTEGER *crlnum; |
| 323 |
|
| 324 |
crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL); |
| 325 |
BIO_printf(bio_out, "crlNumber="); |
| 326 |
if (crlnum) { |
| 327 |
BIO_puts(bio_out, "0x"); |
| 328 |
i2a_ASN1_INTEGER(bio_out, crlnum); |
| 329 |
ASN1_INTEGER_free(crlnum); |
| 330 |
} else { |
| 331 |
BIO_puts(bio_out, "<NONE>"); |
| 332 |
} |
| 333 |
BIO_printf(bio_out, "\n"); |
| 334 |
} |
| 335 |
if (hash == i) { |
| 336 |
int ok; |
| 337 |
unsigned long hash_value = X509_NAME_hash_ex(X509_CRL_get_issuer(x), app_get0_libctx(), |
| 338 |
app_get0_propq(), &ok); |
| 339 |
|
| 340 |
if (num > 1) |
| 341 |
BIO_printf(bio_out, "issuer name hash="); |
| 342 |
if (ok) { |
| 343 |
BIO_printf(bio_out, "%08lx\n", hash_value); |
| 344 |
} else { |
| 345 |
BIO_puts(bio_out, "<ERROR>"); |
| 346 |
goto end; |
| 347 |
} |
| 348 |
} |
| 349 |
#ifndef OPENSSL_NO_MD5 |
| 350 |
if (hash_old == i) { |
| 351 |
if (num > 1) |
| 352 |
BIO_printf(bio_out, "issuer name old hash="); |
| 353 |
BIO_printf(bio_out, "%08lx\n", |
| 354 |
X509_NAME_hash_old(X509_CRL_get_issuer(x))); |
| 355 |
} |
| 356 |
#endif |
| 357 |
if (lastupdate == i) { |
| 358 |
BIO_printf(bio_out, "lastUpdate="); |
| 359 |
ASN1_TIME_print_ex(bio_out, X509_CRL_get0_lastUpdate(x), dateopt); |
| 360 |
BIO_printf(bio_out, "\n"); |
| 361 |
} |
| 362 |
if (nextupdate == i) { |
| 363 |
BIO_printf(bio_out, "nextUpdate="); |
| 364 |
if (X509_CRL_get0_nextUpdate(x)) |
| 365 |
ASN1_TIME_print_ex(bio_out, X509_CRL_get0_nextUpdate(x), dateopt); |
| 366 |
else |
| 367 |
BIO_printf(bio_out, "NONE"); |
| 368 |
BIO_printf(bio_out, "\n"); |
| 369 |
} |
| 370 |
if (fingerprint == i) { |
| 371 |
int j; |
| 372 |
unsigned int n; |
| 373 |
unsigned char md[EVP_MAX_MD_SIZE]; |
| 374 |
|
| 375 |
if (!X509_CRL_digest(x, digest, md, &n)) { |
| 376 |
BIO_printf(bio_err, "out of memory\n"); |
| 377 |
goto end; |
| 378 |
} |
| 379 |
BIO_printf(bio_out, "%s Fingerprint=", |
| 380 |
EVP_MD_get0_name(digest)); |
| 381 |
for (j = 0; j < (int)n; j++) { |
| 382 |
BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n) ? '\n' : ':'); |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
out = bio_open_default(outfile, 'w', outformat); |
| 388 |
if (out == NULL) |
| 389 |
goto end; |
| 390 |
|
| 391 |
if (text) |
| 392 |
X509_CRL_print_ex(out, x, get_nameopt()); |
| 393 |
|
| 394 |
if (noout) { |
| 395 |
ret = 0; |
| 396 |
goto end; |
| 397 |
} |
| 398 |
|
| 399 |
if (outformat == FORMAT_ASN1) |
| 400 |
i = (int)i2d_X509_CRL_bio(out, x); |
| 401 |
else |
| 402 |
i = PEM_write_bio_X509_CRL(out, x); |
| 403 |
if (!i) { |
| 404 |
BIO_printf(bio_err, "unable to write CRL\n"); |
| 405 |
goto end; |
| 406 |
} |
| 407 |
ret = 0; |
| 408 |
|
| 409 |
end: |
| 410 |
if (ret != 0) |
| 411 |
ERR_print_errors(bio_err); |
| 412 |
BIO_free_all(out); |
| 413 |
EVP_MD_free(digest); |
| 414 |
X509_CRL_free(x); |
| 415 |
X509_STORE_CTX_free(ctx); |
| 416 |
X509_STORE_free(store); |
| 417 |
return ret; |
| 418 |
} |