crl.c 13 KB

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