2
0

crl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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_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. /* No remaining args. */
  196. argc = opt_num_rest();
  197. if (argc != 0)
  198. goto opthelp;
  199. x = load_crl(infile, "CRL");
  200. if (x == NULL)
  201. goto end;
  202. if (do_ver) {
  203. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  204. CAstore, noCAstore)) == NULL)
  205. goto end;
  206. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  207. if (lookup == NULL)
  208. goto end;
  209. ctx = X509_STORE_CTX_new();
  210. if (ctx == NULL || !X509_STORE_CTX_init(ctx, store, NULL, NULL)) {
  211. BIO_printf(bio_err, "Error initialising X509 store\n");
  212. goto end;
  213. }
  214. xobj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509,
  215. X509_CRL_get_issuer(x));
  216. if (xobj == NULL) {
  217. BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
  218. goto end;
  219. }
  220. pkey = X509_get_pubkey(X509_OBJECT_get0_X509(xobj));
  221. X509_OBJECT_free(xobj);
  222. if (pkey == NULL) {
  223. BIO_printf(bio_err, "Error getting CRL issuer public key\n");
  224. goto end;
  225. }
  226. i = X509_CRL_verify(x, pkey);
  227. EVP_PKEY_free(pkey);
  228. if (i < 0)
  229. goto end;
  230. if (i == 0)
  231. BIO_printf(bio_err, "verify failure\n");
  232. else
  233. BIO_printf(bio_err, "verify OK\n");
  234. }
  235. if (crldiff) {
  236. X509_CRL *newcrl, *delta;
  237. if (!keyfile) {
  238. BIO_puts(bio_err, "Missing CRL signing key\n");
  239. goto end;
  240. }
  241. newcrl = load_crl(crldiff, "other CRL");
  242. if (!newcrl)
  243. goto end;
  244. pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
  245. if (pkey == NULL) {
  246. X509_CRL_free(newcrl);
  247. goto end;
  248. }
  249. delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
  250. X509_CRL_free(newcrl);
  251. EVP_PKEY_free(pkey);
  252. if (delta) {
  253. X509_CRL_free(x);
  254. x = delta;
  255. } else {
  256. BIO_puts(bio_err, "Error creating delta CRL\n");
  257. goto end;
  258. }
  259. }
  260. if (badsig) {
  261. const ASN1_BIT_STRING *sig;
  262. X509_CRL_get0_signature(x, &sig, NULL);
  263. corrupt_signature(sig);
  264. }
  265. if (num) {
  266. for (i = 1; i <= num; i++) {
  267. if (issuer == i) {
  268. print_name(bio_out, "issuer=", X509_CRL_get_issuer(x),
  269. get_nameopt());
  270. }
  271. if (crlnumber == i) {
  272. ASN1_INTEGER *crlnum;
  273. crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
  274. BIO_printf(bio_out, "crlNumber=");
  275. if (crlnum) {
  276. BIO_puts(bio_out, "0x");
  277. i2a_ASN1_INTEGER(bio_out, crlnum);
  278. ASN1_INTEGER_free(crlnum);
  279. } else {
  280. BIO_puts(bio_out, "<NONE>");
  281. }
  282. BIO_printf(bio_out, "\n");
  283. }
  284. if (hash == i) {
  285. int ok;
  286. unsigned long hash_value =
  287. X509_NAME_hash_ex(X509_CRL_get_issuer(x), app_get0_libctx(),
  288. app_get0_propq(), &ok);
  289. BIO_printf(bio_out, "issuer name hash=");
  290. if (ok)
  291. BIO_printf(bio_out, "%08lx\n", hash_value);
  292. else
  293. BIO_puts(bio_out, "<ERROR>");
  294. }
  295. #ifndef OPENSSL_NO_MD5
  296. if (hash_old == i) {
  297. BIO_printf(bio_out, "issuer name old hash=");
  298. BIO_printf(bio_out, "%08lx\n",
  299. X509_NAME_hash_old(X509_CRL_get_issuer(x)));
  300. }
  301. #endif
  302. if (lastupdate == i) {
  303. BIO_printf(bio_out, "lastUpdate=");
  304. ASN1_TIME_print(bio_out, X509_CRL_get0_lastUpdate(x));
  305. BIO_printf(bio_out, "\n");
  306. }
  307. if (nextupdate == i) {
  308. BIO_printf(bio_out, "nextUpdate=");
  309. if (X509_CRL_get0_nextUpdate(x))
  310. ASN1_TIME_print(bio_out, X509_CRL_get0_nextUpdate(x));
  311. else
  312. BIO_printf(bio_out, "NONE");
  313. BIO_printf(bio_out, "\n");
  314. }
  315. if (fingerprint == i) {
  316. int j;
  317. unsigned int n;
  318. unsigned char md[EVP_MAX_MD_SIZE];
  319. if (!X509_CRL_digest(x, digest, md, &n)) {
  320. BIO_printf(bio_err, "out of memory\n");
  321. goto end;
  322. }
  323. BIO_printf(bio_out, "%s Fingerprint=",
  324. OBJ_nid2sn(EVP_MD_type(digest)));
  325. for (j = 0; j < (int)n; j++) {
  326. BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
  327. ? '\n' : ':');
  328. }
  329. }
  330. }
  331. }
  332. out = bio_open_default(outfile, 'w', outformat);
  333. if (out == NULL)
  334. goto end;
  335. if (text)
  336. X509_CRL_print_ex(out, x, get_nameopt());
  337. if (noout) {
  338. ret = 0;
  339. goto end;
  340. }
  341. if (outformat == FORMAT_ASN1)
  342. i = (int)i2d_X509_CRL_bio(out, x);
  343. else
  344. i = PEM_write_bio_X509_CRL(out, x);
  345. if (!i) {
  346. BIO_printf(bio_err, "unable to write CRL\n");
  347. goto end;
  348. }
  349. ret = 0;
  350. end:
  351. if (ret != 0)
  352. ERR_print_errors(bio_err);
  353. BIO_free_all(out);
  354. X509_CRL_free(x);
  355. X509_STORE_CTX_free(ctx);
  356. X509_STORE_free(store);
  357. return ret;
  358. }