crl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* apps/crl.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include "apps.h"
  62. #include <openssl/bio.h>
  63. #include <openssl/err.h>
  64. #include <openssl/x509.h>
  65. #include <openssl/x509v3.h>
  66. #include <openssl/pem.h>
  67. #undef PROG
  68. #define PROG crl_main
  69. #undef POSTFIX
  70. #define POSTFIX ".rvk"
  71. static const char *crl_usage[] = {
  72. "usage: crl args\n",
  73. "\n",
  74. " -inform arg - input format - default PEM (DER or PEM)\n",
  75. " -outform arg - output format - default PEM\n",
  76. " -text - print out a text format version\n",
  77. " -in arg - input file - default stdin\n",
  78. " -out arg - output file - default stdout\n",
  79. " -hash - print hash value\n",
  80. #ifndef OPENSSL_NO_MD5
  81. " -hash_old - print old-style (MD5) hash value\n",
  82. #endif
  83. " -fingerprint - print the crl fingerprint\n",
  84. " -issuer - print issuer DN\n",
  85. " -lastupdate - lastUpdate field\n",
  86. " -nextupdate - nextUpdate field\n",
  87. " -crlnumber - print CRL number\n",
  88. " -noout - no CRL output\n",
  89. " -CAfile name - verify CRL using certificates in file \"name\"\n",
  90. " -CApath dir - verify CRL using certificates in \"dir\"\n",
  91. " -nameopt arg - various certificate name options\n",
  92. NULL
  93. };
  94. static BIO *bio_out = NULL;
  95. int MAIN(int, char **);
  96. int MAIN(int argc, char **argv)
  97. {
  98. unsigned long nmflag = 0;
  99. X509_CRL *x = NULL;
  100. char *CAfile = NULL, *CApath = NULL;
  101. int ret = 1, i, num, badops = 0, badsig = 0;
  102. BIO *out = NULL;
  103. int informat, outformat, keyformat;
  104. char *infile = NULL, *outfile = NULL, *crldiff = NULL, *keyfile = NULL;
  105. int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout =
  106. 0, text = 0;
  107. #ifndef OPENSSL_NO_MD5
  108. int hash_old = 0;
  109. #endif
  110. int fingerprint = 0, crlnumber = 0;
  111. const char **pp;
  112. X509_STORE *store = NULL;
  113. X509_STORE_CTX ctx;
  114. X509_LOOKUP *lookup = NULL;
  115. X509_OBJECT xobj;
  116. EVP_PKEY *pkey;
  117. int do_ver = 0;
  118. const EVP_MD *md_alg, *digest = EVP_sha1();
  119. apps_startup();
  120. if (bio_err == NULL)
  121. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  122. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  123. if (!load_config(bio_err, NULL))
  124. goto end;
  125. if (bio_out == NULL)
  126. if ((bio_out = BIO_new(BIO_s_file())) != NULL) {
  127. BIO_set_fp(bio_out, stdout, BIO_NOCLOSE);
  128. #ifdef OPENSSL_SYS_VMS
  129. {
  130. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  131. bio_out = BIO_push(tmpbio, bio_out);
  132. }
  133. #endif
  134. }
  135. informat = FORMAT_PEM;
  136. outformat = FORMAT_PEM;
  137. keyformat = FORMAT_PEM;
  138. argc--;
  139. argv++;
  140. num = 0;
  141. while (argc >= 1) {
  142. #ifdef undef
  143. if (strcmp(*argv, "-p") == 0) {
  144. if (--argc < 1)
  145. goto bad;
  146. if (!args_from_file(++argv, Nargc, Nargv)) {
  147. goto end;
  148. }
  149. */}
  150. #endif
  151. if (strcmp(*argv, "-inform") == 0) {
  152. if (--argc < 1)
  153. goto bad;
  154. informat = str2fmt(*(++argv));
  155. } else if (strcmp(*argv, "-outform") == 0) {
  156. if (--argc < 1)
  157. goto bad;
  158. outformat = str2fmt(*(++argv));
  159. } else if (strcmp(*argv, "-in") == 0) {
  160. if (--argc < 1)
  161. goto bad;
  162. infile = *(++argv);
  163. } else if (strcmp(*argv, "-gendelta") == 0) {
  164. if (--argc < 1)
  165. goto bad;
  166. crldiff = *(++argv);
  167. } else if (strcmp(*argv, "-key") == 0) {
  168. if (--argc < 1)
  169. goto bad;
  170. keyfile = *(++argv);
  171. } else if (strcmp(*argv, "-keyform") == 0) {
  172. if (--argc < 1)
  173. goto bad;
  174. keyformat = str2fmt(*(++argv));
  175. } else if (strcmp(*argv, "-out") == 0) {
  176. if (--argc < 1)
  177. goto bad;
  178. outfile = *(++argv);
  179. } else if (strcmp(*argv, "-CApath") == 0) {
  180. if (--argc < 1)
  181. goto bad;
  182. CApath = *(++argv);
  183. do_ver = 1;
  184. } else if (strcmp(*argv, "-CAfile") == 0) {
  185. if (--argc < 1)
  186. goto bad;
  187. CAfile = *(++argv);
  188. do_ver = 1;
  189. } else if (strcmp(*argv, "-verify") == 0)
  190. do_ver = 1;
  191. else if (strcmp(*argv, "-text") == 0)
  192. text = 1;
  193. else if (strcmp(*argv, "-hash") == 0)
  194. hash = ++num;
  195. #ifndef OPENSSL_NO_MD5
  196. else if (strcmp(*argv, "-hash_old") == 0)
  197. hash_old = ++num;
  198. #endif
  199. else if (strcmp(*argv, "-nameopt") == 0) {
  200. if (--argc < 1)
  201. goto bad;
  202. if (!set_name_ex(&nmflag, *(++argv)))
  203. goto bad;
  204. } else if (strcmp(*argv, "-issuer") == 0)
  205. issuer = ++num;
  206. else if (strcmp(*argv, "-lastupdate") == 0)
  207. lastupdate = ++num;
  208. else if (strcmp(*argv, "-nextupdate") == 0)
  209. nextupdate = ++num;
  210. else if (strcmp(*argv, "-noout") == 0)
  211. noout = ++num;
  212. else if (strcmp(*argv, "-fingerprint") == 0)
  213. fingerprint = ++num;
  214. else if (strcmp(*argv, "-crlnumber") == 0)
  215. crlnumber = ++num;
  216. else if (strcmp(*argv, "-badsig") == 0)
  217. badsig = 1;
  218. else if ((md_alg = EVP_get_digestbyname(*argv + 1))) {
  219. /* ok */
  220. digest = md_alg;
  221. } else {
  222. BIO_printf(bio_err, "unknown option %s\n", *argv);
  223. badops = 1;
  224. break;
  225. }
  226. argc--;
  227. argv++;
  228. }
  229. if (badops) {
  230. bad:
  231. for (pp = crl_usage; (*pp != NULL); pp++)
  232. BIO_printf(bio_err, "%s", *pp);
  233. goto end;
  234. }
  235. ERR_load_crypto_strings();
  236. x = load_crl(infile, informat);
  237. if (x == NULL) {
  238. goto end;
  239. }
  240. if (do_ver) {
  241. store = X509_STORE_new();
  242. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  243. if (lookup == NULL)
  244. goto end;
  245. if (!X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM))
  246. X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT);
  247. lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
  248. if (lookup == NULL)
  249. goto end;
  250. if (!X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM))
  251. X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
  252. ERR_clear_error();
  253. if (!X509_STORE_CTX_init(&ctx, store, NULL, NULL)) {
  254. BIO_printf(bio_err, "Error initialising X509 store\n");
  255. goto end;
  256. }
  257. i = X509_STORE_get_by_subject(&ctx, X509_LU_X509,
  258. X509_CRL_get_issuer(x), &xobj);
  259. if (i <= 0) {
  260. BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
  261. goto end;
  262. }
  263. pkey = X509_get_pubkey(xobj.data.x509);
  264. X509_OBJECT_free_contents(&xobj);
  265. if (!pkey) {
  266. BIO_printf(bio_err, "Error getting CRL issuer public key\n");
  267. goto end;
  268. }
  269. i = X509_CRL_verify(x, pkey);
  270. EVP_PKEY_free(pkey);
  271. if (i < 0)
  272. goto end;
  273. if (i == 0)
  274. BIO_printf(bio_err, "verify failure\n");
  275. else
  276. BIO_printf(bio_err, "verify OK\n");
  277. }
  278. if (crldiff) {
  279. X509_CRL *newcrl, *delta;
  280. if (!keyfile) {
  281. BIO_puts(bio_err, "Missing CRL signing key\n");
  282. goto end;
  283. }
  284. newcrl = load_crl(crldiff, informat);
  285. if (!newcrl)
  286. goto end;
  287. pkey = load_key(bio_err, keyfile, keyformat, 0, NULL, NULL,
  288. "CRL signing key");
  289. if (!pkey) {
  290. X509_CRL_free(newcrl);
  291. goto end;
  292. }
  293. delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
  294. X509_CRL_free(newcrl);
  295. EVP_PKEY_free(pkey);
  296. if (delta) {
  297. X509_CRL_free(x);
  298. x = delta;
  299. } else {
  300. BIO_puts(bio_err, "Error creating delta CRL\n");
  301. goto end;
  302. }
  303. }
  304. if (num) {
  305. for (i = 1; i <= num; i++) {
  306. if (issuer == i) {
  307. print_name(bio_out, "issuer=", X509_CRL_get_issuer(x),
  308. nmflag);
  309. }
  310. if (crlnumber == i) {
  311. ASN1_INTEGER *crlnum;
  312. crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
  313. BIO_printf(bio_out, "crlNumber=");
  314. if (crlnum) {
  315. i2a_ASN1_INTEGER(bio_out, crlnum);
  316. ASN1_INTEGER_free(crlnum);
  317. } else
  318. BIO_puts(bio_out, "<NONE>");
  319. BIO_printf(bio_out, "\n");
  320. }
  321. if (hash == i) {
  322. BIO_printf(bio_out, "%08lx\n",
  323. X509_NAME_hash(X509_CRL_get_issuer(x)));
  324. }
  325. #ifndef OPENSSL_NO_MD5
  326. if (hash_old == i) {
  327. BIO_printf(bio_out, "%08lx\n",
  328. X509_NAME_hash_old(X509_CRL_get_issuer(x)));
  329. }
  330. #endif
  331. if (lastupdate == i) {
  332. BIO_printf(bio_out, "lastUpdate=");
  333. ASN1_TIME_print(bio_out, X509_CRL_get_lastUpdate(x));
  334. BIO_printf(bio_out, "\n");
  335. }
  336. if (nextupdate == i) {
  337. BIO_printf(bio_out, "nextUpdate=");
  338. if (X509_CRL_get_nextUpdate(x))
  339. ASN1_TIME_print(bio_out, X509_CRL_get_nextUpdate(x));
  340. else
  341. BIO_printf(bio_out, "NONE");
  342. BIO_printf(bio_out, "\n");
  343. }
  344. if (fingerprint == i) {
  345. int j;
  346. unsigned int n;
  347. unsigned char md[EVP_MAX_MD_SIZE];
  348. if (!X509_CRL_digest(x, digest, md, &n)) {
  349. BIO_printf(bio_err, "out of memory\n");
  350. goto end;
  351. }
  352. BIO_printf(bio_out, "%s Fingerprint=",
  353. OBJ_nid2sn(EVP_MD_type(digest)));
  354. for (j = 0; j < (int)n; j++) {
  355. BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
  356. ? '\n' : ':');
  357. }
  358. }
  359. }
  360. }
  361. out = BIO_new(BIO_s_file());
  362. if (out == NULL) {
  363. ERR_print_errors(bio_err);
  364. goto end;
  365. }
  366. if (outfile == NULL) {
  367. BIO_set_fp(out, stdout, BIO_NOCLOSE);
  368. #ifdef OPENSSL_SYS_VMS
  369. {
  370. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  371. out = BIO_push(tmpbio, out);
  372. }
  373. #endif
  374. } else {
  375. if (BIO_write_filename(out, outfile) <= 0) {
  376. perror(outfile);
  377. goto end;
  378. }
  379. }
  380. if (text)
  381. X509_CRL_print(out, x);
  382. if (noout) {
  383. ret = 0;
  384. goto end;
  385. }
  386. if (badsig)
  387. x->signature->data[x->signature->length - 1] ^= 0x1;
  388. if (outformat == FORMAT_ASN1)
  389. i = (int)i2d_X509_CRL_bio(out, x);
  390. else if (outformat == FORMAT_PEM)
  391. i = PEM_write_bio_X509_CRL(out, x);
  392. else {
  393. BIO_printf(bio_err, "bad output format specified for outfile\n");
  394. goto end;
  395. }
  396. if (!i) {
  397. BIO_printf(bio_err, "unable to write CRL\n");
  398. goto end;
  399. }
  400. ret = 0;
  401. end:
  402. if (ret != 0)
  403. ERR_print_errors(bio_err);
  404. BIO_free_all(out);
  405. BIO_free_all(bio_out);
  406. bio_out = NULL;
  407. X509_CRL_free(x);
  408. if (store) {
  409. X509_STORE_CTX_cleanup(&ctx);
  410. X509_STORE_free(store);
  411. }
  412. apps_shutdown();
  413. OPENSSL_EXIT(ret);
  414. }