2
0

crl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright 1995-2022 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. else
  241. BIO_printf(bio_err, "verify OK\n");
  242. }
  243. if (crldiff != NULL) {
  244. X509_CRL *newcrl, *delta;
  245. if (!keyfile) {
  246. BIO_puts(bio_err, "Missing CRL signing key\n");
  247. goto end;
  248. }
  249. newcrl = load_crl(crldiff, informat, 0, "other CRL");
  250. if (!newcrl)
  251. goto end;
  252. pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
  253. if (pkey == NULL) {
  254. X509_CRL_free(newcrl);
  255. goto end;
  256. }
  257. delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
  258. X509_CRL_free(newcrl);
  259. EVP_PKEY_free(pkey);
  260. if (delta) {
  261. X509_CRL_free(x);
  262. x = delta;
  263. } else {
  264. BIO_puts(bio_err, "Error creating delta CRL\n");
  265. goto end;
  266. }
  267. }
  268. if (badsig) {
  269. const ASN1_BIT_STRING *sig;
  270. X509_CRL_get0_signature(x, &sig, NULL);
  271. corrupt_signature(sig);
  272. }
  273. if (num) {
  274. for (i = 1; i <= num; i++) {
  275. if (issuer == i) {
  276. print_name(bio_out, "issuer=", X509_CRL_get_issuer(x));
  277. }
  278. if (crlnumber == i) {
  279. ASN1_INTEGER *crlnum;
  280. crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
  281. BIO_printf(bio_out, "crlNumber=");
  282. if (crlnum) {
  283. BIO_puts(bio_out, "0x");
  284. i2a_ASN1_INTEGER(bio_out, crlnum);
  285. ASN1_INTEGER_free(crlnum);
  286. } else {
  287. BIO_puts(bio_out, "<NONE>");
  288. }
  289. BIO_printf(bio_out, "\n");
  290. }
  291. if (hash == i) {
  292. int ok;
  293. unsigned long hash_value =
  294. X509_NAME_hash_ex(X509_CRL_get_issuer(x), app_get0_libctx(),
  295. app_get0_propq(), &ok);
  296. if (num > 1)
  297. BIO_printf(bio_out, "issuer name hash=");
  298. if (ok) {
  299. BIO_printf(bio_out, "%08lx\n", hash_value);
  300. } else {
  301. BIO_puts(bio_out, "<ERROR>");
  302. goto end;
  303. }
  304. }
  305. #ifndef OPENSSL_NO_MD5
  306. if (hash_old == i) {
  307. if (num > 1)
  308. BIO_printf(bio_out, "issuer name old hash=");
  309. BIO_printf(bio_out, "%08lx\n",
  310. X509_NAME_hash_old(X509_CRL_get_issuer(x)));
  311. }
  312. #endif
  313. if (lastupdate == i) {
  314. BIO_printf(bio_out, "lastUpdate=");
  315. ASN1_TIME_print_ex(bio_out, X509_CRL_get0_lastUpdate(x), dateopt);
  316. BIO_printf(bio_out, "\n");
  317. }
  318. if (nextupdate == i) {
  319. BIO_printf(bio_out, "nextUpdate=");
  320. if (X509_CRL_get0_nextUpdate(x))
  321. ASN1_TIME_print_ex(bio_out, X509_CRL_get0_nextUpdate(x), dateopt);
  322. else
  323. BIO_printf(bio_out, "NONE");
  324. BIO_printf(bio_out, "\n");
  325. }
  326. if (fingerprint == i) {
  327. int j;
  328. unsigned int n;
  329. unsigned char md[EVP_MAX_MD_SIZE];
  330. if (!X509_CRL_digest(x, digest, md, &n)) {
  331. BIO_printf(bio_err, "out of memory\n");
  332. goto end;
  333. }
  334. BIO_printf(bio_out, "%s Fingerprint=",
  335. EVP_MD_get0_name(digest));
  336. for (j = 0; j < (int)n; j++) {
  337. BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
  338. ? '\n' : ':');
  339. }
  340. }
  341. }
  342. }
  343. out = bio_open_default(outfile, 'w', outformat);
  344. if (out == NULL)
  345. goto end;
  346. if (text)
  347. X509_CRL_print_ex(out, x, get_nameopt());
  348. if (noout) {
  349. ret = 0;
  350. goto end;
  351. }
  352. if (outformat == FORMAT_ASN1)
  353. i = (int)i2d_X509_CRL_bio(out, x);
  354. else
  355. i = PEM_write_bio_X509_CRL(out, x);
  356. if (!i) {
  357. BIO_printf(bio_err, "unable to write CRL\n");
  358. goto end;
  359. }
  360. ret = 0;
  361. end:
  362. if (ret != 0)
  363. ERR_print_errors(bio_err);
  364. BIO_free_all(out);
  365. EVP_MD_free(digest);
  366. X509_CRL_free(x);
  367. X509_STORE_CTX_free(ctx);
  368. X509_STORE_free(store);
  369. return ret;
  370. }