crl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include "apps.h"
  61. #include <openssl/bio.h>
  62. #include <openssl/err.h>
  63. #include <openssl/x509.h>
  64. #include <openssl/x509v3.h>
  65. #include <openssl/pem.h>
  66. typedef enum OPTION_choice {
  67. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  68. OPT_INFORM, OPT_IN, OPT_OUTFORM, OPT_OUT, OPT_KEYFORM, OPT_KEY,
  69. OPT_ISSUER, OPT_LASTUPDATE, OPT_NEXTUPDATE, OPT_FINGERPRINT,
  70. OPT_CRLNUMBER, OPT_BADSIG, OPT_GENDELTA, OPT_CAPATH, OPT_CAFILE,
  71. OPT_NOCAPATH, OPT_NOCAFILE, OPT_VERIFY, OPT_TEXT, OPT_HASH, OPT_HASH_OLD,
  72. OPT_NOOUT, OPT_NAMEOPT, OPT_MD
  73. } OPTION_CHOICE;
  74. OPTIONS crl_options[] = {
  75. {"help", OPT_HELP, '-', "Display this summary"},
  76. {"inform", OPT_INFORM, 'F', "Input format; default PEM"},
  77. {"in", OPT_IN, '<', "Input file - default stdin"},
  78. {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
  79. {"out", OPT_OUT, '>', "output file - default stdout"},
  80. {"keyform", OPT_KEYFORM, 'F'},
  81. {"key", OPT_KEY, '<'},
  82. {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
  83. {"lastupdate", OPT_LASTUPDATE, '-', "Set lastUpdate field"},
  84. {"nextupdate", OPT_NEXTUPDATE, '-', "Set nextUpdate field"},
  85. {"noout", OPT_NOOUT, '-', "No CRL output"},
  86. {"fingerprint", OPT_FINGERPRINT, '-', "Print the crl fingerprint"},
  87. {"crlnumber", OPT_CRLNUMBER, '-', "Print CRL number"},
  88. {"badsig", OPT_BADSIG, '-'},
  89. {"gendelta", OPT_GENDELTA, '<'},
  90. {"CApath", OPT_CAPATH, '/', "Verify CRL using certificates in dir"},
  91. {"CAfile", OPT_CAFILE, '<', "Verify CRL using certificates in file name"},
  92. {"no-CAfile", OPT_NOCAFILE, '-',
  93. "Do not load the default certificates file"},
  94. {"no-CApath", OPT_NOCAPATH, '-',
  95. "Do not load certificates from the default certificates directory"},
  96. {"verify", OPT_VERIFY, '-'},
  97. {"text", OPT_TEXT, '-', "Print out a text format version"},
  98. {"hash", OPT_HASH, '-', "Print hash value"},
  99. {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
  100. {"", OPT_MD, '-', "Any supported digest"},
  101. #ifndef OPENSSL_NO_MD5
  102. {"hash_old", OPT_HASH_OLD, '-', "Print old-style (MD5) hash value"},
  103. #endif
  104. {NULL}
  105. };
  106. int crl_main(int argc, char **argv)
  107. {
  108. X509_CRL *x = NULL;
  109. BIO *out = NULL;
  110. X509_STORE *store = NULL;
  111. X509_STORE_CTX ctx;
  112. X509_LOOKUP *lookup = NULL;
  113. X509_OBJECT xobj;
  114. EVP_PKEY *pkey;
  115. const EVP_MD *digest = EVP_sha1();
  116. unsigned long nmflag = 0;
  117. char nmflag_set = 0;
  118. char *infile = NULL, *outfile = NULL, *crldiff = NULL, *keyfile = NULL;
  119. char *CAfile = NULL, *CApath = NULL, *prog;
  120. OPTION_CHOICE o;
  121. int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout = 0;
  122. int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;
  123. int ret = 1, num = 0, badsig = 0, fingerprint = 0, crlnumber = 0;
  124. int text = 0, do_ver = 0, noCAfile = 0, noCApath = 0;
  125. int i;
  126. #ifndef OPENSSL_NO_MD5
  127. int hash_old = 0;
  128. #endif
  129. prog = opt_init(argc, argv, crl_options);
  130. while ((o = opt_next()) != OPT_EOF) {
  131. switch (o) {
  132. case OPT_EOF:
  133. case OPT_ERR:
  134. opthelp:
  135. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  136. goto end;
  137. case OPT_HELP:
  138. opt_help(crl_options);
  139. ret = 0;
  140. goto end;
  141. case OPT_INFORM:
  142. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  143. goto opthelp;
  144. break;
  145. case OPT_IN:
  146. infile = opt_arg();
  147. break;
  148. case OPT_OUTFORM:
  149. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  150. goto opthelp;
  151. break;
  152. case OPT_OUT:
  153. outfile = opt_arg();
  154. break;
  155. case OPT_KEYFORM:
  156. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
  157. goto opthelp;
  158. break;
  159. case OPT_KEY:
  160. keyfile = opt_arg();
  161. break;
  162. case OPT_GENDELTA:
  163. crldiff = opt_arg();
  164. break;
  165. case OPT_CAPATH:
  166. CApath = opt_arg();
  167. do_ver = 1;
  168. break;
  169. case OPT_CAFILE:
  170. CAfile = opt_arg();
  171. do_ver = 1;
  172. break;
  173. case OPT_NOCAPATH:
  174. noCApath = 1;
  175. break;
  176. case OPT_NOCAFILE:
  177. noCAfile = 1;
  178. break;
  179. case OPT_HASH_OLD:
  180. #ifndef OPENSSL_NO_MD5
  181. hash_old = ++num;
  182. #endif
  183. break;
  184. case OPT_VERIFY:
  185. do_ver = 1;
  186. break;
  187. case OPT_TEXT:
  188. text = 1;
  189. break;
  190. case OPT_HASH:
  191. hash = ++num;
  192. break;
  193. case OPT_ISSUER:
  194. issuer = ++num;
  195. break;
  196. case OPT_LASTUPDATE:
  197. lastupdate = ++num;
  198. break;
  199. case OPT_NEXTUPDATE:
  200. nextupdate = ++num;
  201. break;
  202. case OPT_NOOUT:
  203. noout = ++num;
  204. break;
  205. case OPT_FINGERPRINT:
  206. fingerprint = ++num;
  207. break;
  208. case OPT_CRLNUMBER:
  209. crlnumber = ++num;
  210. break;
  211. case OPT_BADSIG:
  212. badsig = 1;
  213. break;
  214. case OPT_NAMEOPT:
  215. nmflag_set = 1;
  216. if (!set_name_ex(&nmflag, opt_arg()))
  217. goto opthelp;
  218. break;
  219. case OPT_MD:
  220. if (!opt_md(opt_unknown(), &digest))
  221. goto opthelp;
  222. }
  223. }
  224. argc = opt_num_rest();
  225. argv = opt_rest();
  226. if (!nmflag_set)
  227. nmflag = XN_FLAG_ONELINE;
  228. x = load_crl(infile, informat);
  229. if (x == NULL)
  230. goto end;
  231. if (do_ver) {
  232. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
  233. goto end;
  234. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  235. if (lookup == NULL)
  236. goto end;
  237. if (!X509_STORE_CTX_init(&ctx, store, NULL, NULL)) {
  238. BIO_printf(bio_err, "Error initialising X509 store\n");
  239. goto end;
  240. }
  241. i = X509_STORE_get_by_subject(&ctx, X509_LU_X509,
  242. X509_CRL_get_issuer(x), &xobj);
  243. if (i <= 0) {
  244. BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
  245. goto end;
  246. }
  247. pkey = X509_get0_pubkey(xobj.data.x509);
  248. X509_OBJECT_free_contents(&xobj);
  249. if (!pkey) {
  250. BIO_printf(bio_err, "Error getting CRL issuer public key\n");
  251. goto end;
  252. }
  253. i = X509_CRL_verify(x, pkey);
  254. if (i < 0)
  255. goto end;
  256. if (i == 0)
  257. BIO_printf(bio_err, "verify failure\n");
  258. else
  259. BIO_printf(bio_err, "verify OK\n");
  260. }
  261. if (crldiff) {
  262. X509_CRL *newcrl, *delta;
  263. if (!keyfile) {
  264. BIO_puts(bio_err, "Missing CRL signing key\n");
  265. goto end;
  266. }
  267. newcrl = load_crl(crldiff, informat);
  268. if (!newcrl)
  269. goto end;
  270. pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
  271. if (!pkey) {
  272. X509_CRL_free(newcrl);
  273. goto end;
  274. }
  275. delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
  276. X509_CRL_free(newcrl);
  277. EVP_PKEY_free(pkey);
  278. if (delta) {
  279. X509_CRL_free(x);
  280. x = delta;
  281. } else {
  282. BIO_puts(bio_err, "Error creating delta CRL\n");
  283. goto end;
  284. }
  285. }
  286. if (num) {
  287. for (i = 1; i <= num; i++) {
  288. if (issuer == i) {
  289. print_name(bio_out, "issuer=", X509_CRL_get_issuer(x),
  290. nmflag);
  291. }
  292. if (crlnumber == i) {
  293. ASN1_INTEGER *crlnum;
  294. crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
  295. BIO_printf(bio_out, "crlNumber=");
  296. if (crlnum) {
  297. i2a_ASN1_INTEGER(bio_out, crlnum);
  298. ASN1_INTEGER_free(crlnum);
  299. } else
  300. BIO_puts(bio_out, "<NONE>");
  301. BIO_printf(bio_out, "\n");
  302. }
  303. if (hash == i) {
  304. BIO_printf(bio_out, "%08lx\n",
  305. X509_NAME_hash(X509_CRL_get_issuer(x)));
  306. }
  307. #ifndef OPENSSL_NO_MD5
  308. if (hash_old == i) {
  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(bio_out, X509_CRL_get_lastUpdate(x));
  316. BIO_printf(bio_out, "\n");
  317. }
  318. if (nextupdate == i) {
  319. BIO_printf(bio_out, "nextUpdate=");
  320. if (X509_CRL_get_nextUpdate(x))
  321. ASN1_TIME_print(bio_out, X509_CRL_get_nextUpdate(x));
  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. OBJ_nid2sn(EVP_MD_type(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(out, x);
  348. if (noout) {
  349. ret = 0;
  350. goto end;
  351. }
  352. if (badsig) {
  353. ASN1_BIT_STRING *sig;
  354. unsigned char *psig;
  355. X509_CRL_get0_signature(&sig, NULL, x);
  356. psig = ASN1_STRING_data(sig);
  357. psig[ASN1_STRING_length(sig) - 1] ^= 0x1;
  358. }
  359. if (outformat == FORMAT_ASN1)
  360. i = (int)i2d_X509_CRL_bio(out, x);
  361. else
  362. i = PEM_write_bio_X509_CRL(out, x);
  363. if (!i) {
  364. BIO_printf(bio_err, "unable to write CRL\n");
  365. goto end;
  366. }
  367. ret = 0;
  368. end:
  369. if (ret != 0)
  370. ERR_print_errors(bio_err);
  371. BIO_free_all(out);
  372. X509_CRL_free(x);
  373. if (store) {
  374. X509_STORE_CTX_cleanup(&ctx);
  375. X509_STORE_free(store);
  376. }
  377. return (ret);
  378. }