crl.c 12 KB

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