crl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright 1995-2021 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. prog = opt_init(argc, argv, crl_options);
  93. while ((o = opt_next()) != OPT_EOF) {
  94. switch (o) {
  95. case OPT_EOF:
  96. case OPT_ERR:
  97. opthelp:
  98. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  99. goto end;
  100. case OPT_HELP:
  101. opt_help(crl_options);
  102. ret = 0;
  103. goto end;
  104. case OPT_INFORM:
  105. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  106. goto opthelp;
  107. break;
  108. case OPT_IN:
  109. infile = opt_arg();
  110. break;
  111. case OPT_OUTFORM:
  112. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  113. goto opthelp;
  114. break;
  115. case OPT_OUT:
  116. outfile = opt_arg();
  117. break;
  118. case OPT_KEYFORM:
  119. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
  120. goto opthelp;
  121. break;
  122. case OPT_KEY:
  123. keyfile = opt_arg();
  124. break;
  125. case OPT_GENDELTA:
  126. crldiff = opt_arg();
  127. break;
  128. case OPT_CAPATH:
  129. CApath = opt_arg();
  130. do_ver = 1;
  131. break;
  132. case OPT_CAFILE:
  133. CAfile = opt_arg();
  134. do_ver = 1;
  135. break;
  136. case OPT_CASTORE:
  137. CAstore = opt_arg();
  138. do_ver = 1;
  139. break;
  140. case OPT_NOCAPATH:
  141. noCApath = 1;
  142. break;
  143. case OPT_NOCAFILE:
  144. noCAfile = 1;
  145. break;
  146. case OPT_NOCASTORE:
  147. noCAstore = 1;
  148. break;
  149. case OPT_HASH_OLD:
  150. #ifndef OPENSSL_NO_MD5
  151. hash_old = ++num;
  152. #endif
  153. break;
  154. case OPT_VERIFY:
  155. do_ver = 1;
  156. break;
  157. case OPT_DATEOPT:
  158. if (!set_dateopt(&dateopt, opt_arg()))
  159. goto opthelp;
  160. break;
  161. case OPT_TEXT:
  162. text = 1;
  163. break;
  164. case OPT_HASH:
  165. hash = ++num;
  166. break;
  167. case OPT_ISSUER:
  168. issuer = ++num;
  169. break;
  170. case OPT_LASTUPDATE:
  171. lastupdate = ++num;
  172. break;
  173. case OPT_NEXTUPDATE:
  174. nextupdate = ++num;
  175. break;
  176. case OPT_NOOUT:
  177. noout = 1;
  178. break;
  179. case OPT_FINGERPRINT:
  180. fingerprint = ++num;
  181. break;
  182. case OPT_CRLNUMBER:
  183. crlnumber = ++num;
  184. break;
  185. case OPT_BADSIG:
  186. badsig = 1;
  187. break;
  188. case OPT_NAMEOPT:
  189. if (!set_nameopt(opt_arg()))
  190. goto opthelp;
  191. break;
  192. case OPT_MD:
  193. digestname = opt_unknown();
  194. break;
  195. case OPT_PROV_CASES:
  196. if (!opt_provider(o))
  197. goto end;
  198. break;
  199. }
  200. }
  201. /* No remaining args. */
  202. argc = opt_num_rest();
  203. if (argc != 0)
  204. goto opthelp;
  205. if (digestname != NULL) {
  206. if (!opt_md(digestname, &digest))
  207. goto opthelp;
  208. }
  209. x = load_crl(infile, informat, 1, "CRL");
  210. if (x == NULL)
  211. goto end;
  212. if (do_ver) {
  213. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  214. CAstore, noCAstore)) == NULL)
  215. goto end;
  216. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  217. if (lookup == NULL)
  218. goto end;
  219. ctx = X509_STORE_CTX_new();
  220. if (ctx == NULL || !X509_STORE_CTX_init(ctx, store, NULL, NULL)) {
  221. BIO_printf(bio_err, "Error initialising X509 store\n");
  222. goto end;
  223. }
  224. xobj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509,
  225. X509_CRL_get_issuer(x));
  226. if (xobj == NULL) {
  227. BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
  228. goto end;
  229. }
  230. pkey = X509_get_pubkey(X509_OBJECT_get0_X509(xobj));
  231. X509_OBJECT_free(xobj);
  232. if (pkey == NULL) {
  233. BIO_printf(bio_err, "Error getting CRL issuer public key\n");
  234. goto end;
  235. }
  236. i = X509_CRL_verify(x, pkey);
  237. EVP_PKEY_free(pkey);
  238. if (i < 0)
  239. goto end;
  240. if (i == 0)
  241. BIO_printf(bio_err, "verify failure\n");
  242. else
  243. BIO_printf(bio_err, "verify OK\n");
  244. }
  245. if (crldiff != NULL) {
  246. X509_CRL *newcrl, *delta;
  247. if (!keyfile) {
  248. BIO_puts(bio_err, "Missing CRL signing key\n");
  249. goto end;
  250. }
  251. newcrl = load_crl(crldiff, informat, 0, "other CRL");
  252. if (!newcrl)
  253. goto end;
  254. pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
  255. if (pkey == NULL) {
  256. X509_CRL_free(newcrl);
  257. goto end;
  258. }
  259. delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
  260. X509_CRL_free(newcrl);
  261. EVP_PKEY_free(pkey);
  262. if (delta) {
  263. X509_CRL_free(x);
  264. x = delta;
  265. } else {
  266. BIO_puts(bio_err, "Error creating delta CRL\n");
  267. goto end;
  268. }
  269. }
  270. if (badsig) {
  271. const ASN1_BIT_STRING *sig;
  272. X509_CRL_get0_signature(x, &sig, NULL);
  273. corrupt_signature(sig);
  274. }
  275. if (num) {
  276. for (i = 1; i <= num; i++) {
  277. if (issuer == i) {
  278. print_name(bio_out, "issuer=", X509_CRL_get_issuer(x));
  279. }
  280. if (crlnumber == i) {
  281. ASN1_INTEGER *crlnum;
  282. crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
  283. BIO_printf(bio_out, "crlNumber=");
  284. if (crlnum) {
  285. BIO_puts(bio_out, "0x");
  286. i2a_ASN1_INTEGER(bio_out, crlnum);
  287. ASN1_INTEGER_free(crlnum);
  288. } else {
  289. BIO_puts(bio_out, "<NONE>");
  290. }
  291. BIO_printf(bio_out, "\n");
  292. }
  293. if (hash == i) {
  294. int ok;
  295. unsigned long hash_value =
  296. X509_NAME_hash_ex(X509_CRL_get_issuer(x), app_get0_libctx(),
  297. app_get0_propq(), &ok);
  298. if (num > 1)
  299. BIO_printf(bio_out, "issuer name hash=");
  300. if (ok) {
  301. BIO_printf(bio_out, "%08lx\n", hash_value);
  302. } else {
  303. BIO_puts(bio_out, "<ERROR>");
  304. goto end;
  305. }
  306. }
  307. #ifndef OPENSSL_NO_MD5
  308. if (hash_old == i) {
  309. if (num > 1)
  310. BIO_printf(bio_out, "issuer name old hash=");
  311. BIO_printf(bio_out, "%08lx\n",
  312. X509_NAME_hash_old(X509_CRL_get_issuer(x)));
  313. }
  314. #endif
  315. if (lastupdate == i) {
  316. BIO_printf(bio_out, "lastUpdate=");
  317. ASN1_TIME_print_ex(bio_out, X509_CRL_get0_lastUpdate(x), dateopt);
  318. BIO_printf(bio_out, "\n");
  319. }
  320. if (nextupdate == i) {
  321. BIO_printf(bio_out, "nextUpdate=");
  322. if (X509_CRL_get0_nextUpdate(x))
  323. ASN1_TIME_print_ex(bio_out, X509_CRL_get0_nextUpdate(x), dateopt);
  324. else
  325. BIO_printf(bio_out, "NONE");
  326. BIO_printf(bio_out, "\n");
  327. }
  328. if (fingerprint == i) {
  329. int j;
  330. unsigned int n;
  331. unsigned char md[EVP_MAX_MD_SIZE];
  332. if (!X509_CRL_digest(x, digest, md, &n)) {
  333. BIO_printf(bio_err, "out of memory\n");
  334. goto end;
  335. }
  336. BIO_printf(bio_out, "%s Fingerprint=",
  337. EVP_MD_get0_name(digest));
  338. for (j = 0; j < (int)n; j++) {
  339. BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
  340. ? '\n' : ':');
  341. }
  342. }
  343. }
  344. }
  345. out = bio_open_default(outfile, 'w', outformat);
  346. if (out == NULL)
  347. goto end;
  348. if (text)
  349. X509_CRL_print_ex(out, x, get_nameopt());
  350. if (noout) {
  351. ret = 0;
  352. goto end;
  353. }
  354. if (outformat == FORMAT_ASN1)
  355. i = (int)i2d_X509_CRL_bio(out, x);
  356. else
  357. i = PEM_write_bio_X509_CRL(out, x);
  358. if (!i) {
  359. BIO_printf(bio_err, "unable to write CRL\n");
  360. goto end;
  361. }
  362. ret = 0;
  363. end:
  364. if (ret != 0)
  365. ERR_print_errors(bio_err);
  366. BIO_free_all(out);
  367. EVP_MD_free(digest);
  368. X509_CRL_free(x);
  369. X509_STORE_CTX_free(ctx);
  370. X509_STORE_free(store);
  371. return ret;
  372. }