crl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. " -fingerprint - print the crl fingerprint\n",
  81. " -issuer - print issuer DN\n",
  82. " -lastupdate - lastUpdate field\n",
  83. " -nextupdate - nextUpdate field\n",
  84. " -crlnumber - print CRL number\n",
  85. " -noout - no CRL output\n",
  86. " -CAfile name - verify CRL using certificates in file \"name\"\n",
  87. " -CApath dir - verify CRL using certificates in \"dir\"\n",
  88. " -nameopt arg - various certificate name options\n",
  89. NULL
  90. };
  91. static X509_CRL *load_crl(char *file, int format);
  92. static BIO *bio_out=NULL;
  93. int MAIN(int, char **);
  94. int MAIN(int argc, char **argv)
  95. {
  96. unsigned long nmflag = 0;
  97. X509_CRL *x=NULL;
  98. char *CAfile = NULL, *CApath = NULL;
  99. int ret=1,i,num,badops=0;
  100. BIO *out=NULL;
  101. int informat,outformat;
  102. char *infile=NULL,*outfile=NULL;
  103. int hash=0,issuer=0,lastupdate=0,nextupdate=0,noout=0,text=0;
  104. int fingerprint = 0, crlnumber = 0;
  105. const char **pp;
  106. X509_STORE *store = NULL;
  107. X509_STORE_CTX ctx;
  108. X509_LOOKUP *lookup = NULL;
  109. X509_OBJECT xobj;
  110. EVP_PKEY *pkey;
  111. int do_ver = 0;
  112. const EVP_MD *md_alg,*digest=EVP_sha1();
  113. apps_startup();
  114. if (bio_err == NULL)
  115. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  116. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  117. if (!load_config(bio_err, NULL))
  118. goto end;
  119. if (bio_out == NULL)
  120. if ((bio_out=BIO_new(BIO_s_file())) != NULL)
  121. {
  122. BIO_set_fp(bio_out,stdout,BIO_NOCLOSE);
  123. #ifdef OPENSSL_SYS_VMS
  124. {
  125. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  126. bio_out = BIO_push(tmpbio, bio_out);
  127. }
  128. #endif
  129. }
  130. informat=FORMAT_PEM;
  131. outformat=FORMAT_PEM;
  132. argc--;
  133. argv++;
  134. num=0;
  135. while (argc >= 1)
  136. {
  137. #ifdef undef
  138. if (strcmp(*argv,"-p") == 0)
  139. {
  140. if (--argc < 1) goto bad;
  141. if (!args_from_file(++argv,Nargc,Nargv)) { goto end; }*/
  142. }
  143. #endif
  144. if (strcmp(*argv,"-inform") == 0)
  145. {
  146. if (--argc < 1) goto bad;
  147. informat=str2fmt(*(++argv));
  148. }
  149. else if (strcmp(*argv,"-outform") == 0)
  150. {
  151. if (--argc < 1) goto bad;
  152. outformat=str2fmt(*(++argv));
  153. }
  154. else if (strcmp(*argv,"-in") == 0)
  155. {
  156. if (--argc < 1) goto bad;
  157. infile= *(++argv);
  158. }
  159. else if (strcmp(*argv,"-out") == 0)
  160. {
  161. if (--argc < 1) goto bad;
  162. outfile= *(++argv);
  163. }
  164. else if (strcmp(*argv,"-CApath") == 0)
  165. {
  166. if (--argc < 1) goto bad;
  167. CApath = *(++argv);
  168. do_ver = 1;
  169. }
  170. else if (strcmp(*argv,"-CAfile") == 0)
  171. {
  172. if (--argc < 1) goto bad;
  173. CAfile = *(++argv);
  174. do_ver = 1;
  175. }
  176. else if (strcmp(*argv,"-verify") == 0)
  177. do_ver = 1;
  178. else if (strcmp(*argv,"-text") == 0)
  179. text = 1;
  180. else if (strcmp(*argv,"-hash") == 0)
  181. hash= ++num;
  182. else if (strcmp(*argv,"-nameopt") == 0)
  183. {
  184. if (--argc < 1) goto bad;
  185. if (!set_name_ex(&nmflag, *(++argv))) goto bad;
  186. }
  187. else if (strcmp(*argv,"-issuer") == 0)
  188. issuer= ++num;
  189. else if (strcmp(*argv,"-lastupdate") == 0)
  190. lastupdate= ++num;
  191. else if (strcmp(*argv,"-nextupdate") == 0)
  192. nextupdate= ++num;
  193. else if (strcmp(*argv,"-noout") == 0)
  194. noout= ++num;
  195. else if (strcmp(*argv,"-fingerprint") == 0)
  196. fingerprint= ++num;
  197. else if (strcmp(*argv,"-crlnumber") == 0)
  198. crlnumber= ++num;
  199. else if ((md_alg=EVP_get_digestbyname(*argv + 1)))
  200. {
  201. /* ok */
  202. digest=md_alg;
  203. }
  204. else
  205. {
  206. BIO_printf(bio_err,"unknown option %s\n",*argv);
  207. badops=1;
  208. break;
  209. }
  210. argc--;
  211. argv++;
  212. }
  213. if (badops)
  214. {
  215. bad:
  216. for (pp=crl_usage; (*pp != NULL); pp++)
  217. BIO_printf(bio_err,"%s",*pp);
  218. goto end;
  219. }
  220. ERR_load_crypto_strings();
  221. x=load_crl(infile,informat);
  222. if (x == NULL) { goto end; }
  223. if(do_ver) {
  224. store = X509_STORE_new();
  225. lookup=X509_STORE_add_lookup(store,X509_LOOKUP_file());
  226. if (lookup == NULL) goto end;
  227. if (!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM))
  228. X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
  229. lookup=X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
  230. if (lookup == NULL) goto end;
  231. if (!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM))
  232. X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
  233. ERR_clear_error();
  234. if(!X509_STORE_CTX_init(&ctx, store, NULL, NULL)) {
  235. BIO_printf(bio_err,
  236. "Error initialising X509 store\n");
  237. goto end;
  238. }
  239. i = X509_STORE_get_by_subject(&ctx, X509_LU_X509,
  240. X509_CRL_get_issuer(x), &xobj);
  241. if(i <= 0) {
  242. BIO_printf(bio_err,
  243. "Error getting CRL issuer certificate\n");
  244. goto end;
  245. }
  246. pkey = X509_get_pubkey(xobj.data.x509);
  247. X509_OBJECT_free_contents(&xobj);
  248. if(!pkey) {
  249. BIO_printf(bio_err,
  250. "Error getting CRL issuer public key\n");
  251. goto end;
  252. }
  253. i = X509_CRL_verify(x, pkey);
  254. EVP_PKEY_free(pkey);
  255. if(i < 0) goto end;
  256. if(i == 0) BIO_printf(bio_err, "verify failure\n");
  257. else BIO_printf(bio_err, "verify OK\n");
  258. }
  259. if (num)
  260. {
  261. for (i=1; i<=num; i++)
  262. {
  263. if (issuer == i)
  264. {
  265. print_name(bio_out, "issuer=", X509_CRL_get_issuer(x), nmflag);
  266. }
  267. if (crlnumber == i)
  268. {
  269. ASN1_INTEGER *crlnum;
  270. crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number,
  271. NULL, NULL);
  272. BIO_printf(bio_out,"crlNumber=");
  273. if (crlnum)
  274. {
  275. i2a_ASN1_INTEGER(bio_out, crlnum);
  276. ASN1_INTEGER_free(crlnum);
  277. }
  278. else
  279. BIO_puts(bio_out, "<NONE>");
  280. BIO_printf(bio_out,"\n");
  281. }
  282. if (hash == i)
  283. {
  284. BIO_printf(bio_out,"%08lx\n",
  285. X509_NAME_hash(X509_CRL_get_issuer(x)));
  286. }
  287. if (lastupdate == i)
  288. {
  289. BIO_printf(bio_out,"lastUpdate=");
  290. ASN1_TIME_print(bio_out,
  291. X509_CRL_get_lastUpdate(x));
  292. BIO_printf(bio_out,"\n");
  293. }
  294. if (nextupdate == i)
  295. {
  296. BIO_printf(bio_out,"nextUpdate=");
  297. if (X509_CRL_get_nextUpdate(x))
  298. ASN1_TIME_print(bio_out,
  299. X509_CRL_get_nextUpdate(x));
  300. else
  301. BIO_printf(bio_out,"NONE");
  302. BIO_printf(bio_out,"\n");
  303. }
  304. if (fingerprint == i)
  305. {
  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. {
  311. BIO_printf(bio_err,"out of memory\n");
  312. goto end;
  313. }
  314. BIO_printf(bio_out,"%s Fingerprint=",
  315. OBJ_nid2sn(EVP_MD_type(digest)));
  316. for (j=0; j<(int)n; j++)
  317. {
  318. BIO_printf(bio_out,"%02X%c",md[j],
  319. (j+1 == (int)n)
  320. ?'\n':':');
  321. }
  322. }
  323. }
  324. }
  325. out=BIO_new(BIO_s_file());
  326. if (out == NULL)
  327. {
  328. ERR_print_errors(bio_err);
  329. goto end;
  330. }
  331. if (outfile == NULL)
  332. {
  333. BIO_set_fp(out,stdout,BIO_NOCLOSE);
  334. #ifdef OPENSSL_SYS_VMS
  335. {
  336. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  337. out = BIO_push(tmpbio, out);
  338. }
  339. #endif
  340. }
  341. else
  342. {
  343. if (BIO_write_filename(out,outfile) <= 0)
  344. {
  345. perror(outfile);
  346. goto end;
  347. }
  348. }
  349. if (text) X509_CRL_print(out, x);
  350. if (noout)
  351. {
  352. ret = 0;
  353. goto end;
  354. }
  355. if (outformat == FORMAT_ASN1)
  356. i=(int)i2d_X509_CRL_bio(out,x);
  357. else if (outformat == FORMAT_PEM)
  358. i=PEM_write_bio_X509_CRL(out,x);
  359. else
  360. {
  361. BIO_printf(bio_err,"bad output format specified for outfile\n");
  362. goto end;
  363. }
  364. if (!i) { BIO_printf(bio_err,"unable to write CRL\n"); goto end; }
  365. ret=0;
  366. end:
  367. BIO_free_all(out);
  368. BIO_free_all(bio_out);
  369. bio_out=NULL;
  370. X509_CRL_free(x);
  371. if(store) {
  372. X509_STORE_CTX_cleanup(&ctx);
  373. X509_STORE_free(store);
  374. }
  375. apps_shutdown();
  376. OPENSSL_EXIT(ret);
  377. }
  378. static X509_CRL *load_crl(char *infile, int format)
  379. {
  380. X509_CRL *x=NULL;
  381. BIO *in=NULL;
  382. in=BIO_new(BIO_s_file());
  383. if (in == NULL)
  384. {
  385. ERR_print_errors(bio_err);
  386. goto end;
  387. }
  388. if (infile == NULL)
  389. BIO_set_fp(in,stdin,BIO_NOCLOSE);
  390. else
  391. {
  392. if (BIO_read_filename(in,infile) <= 0)
  393. {
  394. perror(infile);
  395. goto end;
  396. }
  397. }
  398. if (format == FORMAT_ASN1)
  399. x=d2i_X509_CRL_bio(in,NULL);
  400. else if (format == FORMAT_PEM)
  401. x=PEM_read_bio_X509_CRL(in,NULL,NULL,NULL);
  402. else {
  403. BIO_printf(bio_err,"bad input format specified for input crl\n");
  404. goto end;
  405. }
  406. if (x == NULL)
  407. {
  408. BIO_printf(bio_err,"unable to load CRL\n");
  409. ERR_print_errors(bio_err);
  410. goto end;
  411. }
  412. end:
  413. BIO_free(in);
  414. return(x);
  415. }