crl.c 12 KB

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