2
0

pkcs7.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* apps/pkcs7.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 <time.h>
  62. #include "apps.h"
  63. #include "err.h"
  64. #include "objects.h"
  65. #include "evp.h"
  66. #include "x509.h"
  67. #include "pkcs7.h"
  68. #include "pem.h"
  69. #undef PROG
  70. #define PROG pkcs7_main
  71. /* -inform arg - input format - default PEM (one of DER, TXT or PEM)
  72. * -outform arg - output format - default PEM
  73. * -in arg - input file - default stdin
  74. * -out arg - output file - default stdout
  75. * -des - encrypt output if PEM format with DES in cbc mode
  76. * -des3 - encrypt output if PEM format
  77. * -idea - encrypt output if PEM format
  78. * -print_certs
  79. */
  80. int MAIN(argc, argv)
  81. int argc;
  82. char **argv;
  83. {
  84. PKCS7 *p7=NULL;
  85. int i,badops=0;
  86. #if !defined(NO_DES) || !defined(NO_IDEA)
  87. EVP_CIPHER *enc=NULL;
  88. #endif
  89. BIO *in=NULL,*out=NULL;
  90. int informat,outformat;
  91. char *infile,*outfile,*prog,buf[256];
  92. int print_certs=0;
  93. int ret=0;
  94. apps_startup();
  95. if (bio_err == NULL)
  96. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  97. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  98. infile=NULL;
  99. outfile=NULL;
  100. informat=FORMAT_PEM;
  101. outformat=FORMAT_PEM;
  102. prog=argv[0];
  103. argc--;
  104. argv++;
  105. while (argc >= 1)
  106. {
  107. if (strcmp(*argv,"-inform") == 0)
  108. {
  109. if (--argc < 1) goto bad;
  110. informat=str2fmt(*(++argv));
  111. }
  112. else if (strcmp(*argv,"-outform") == 0)
  113. {
  114. if (--argc < 1) goto bad;
  115. outformat=str2fmt(*(++argv));
  116. }
  117. else if (strcmp(*argv,"-in") == 0)
  118. {
  119. if (--argc < 1) goto bad;
  120. infile= *(++argv);
  121. }
  122. else if (strcmp(*argv,"-out") == 0)
  123. {
  124. if (--argc < 1) goto bad;
  125. outfile= *(++argv);
  126. }
  127. else if (strcmp(*argv,"-print_certs") == 0)
  128. print_certs=1;
  129. #ifndef NO_DES
  130. else if (strcmp(*argv,"-des") == 0)
  131. enc=EVP_des_cbc();
  132. else if (strcmp(*argv,"-des3") == 0)
  133. enc=EVP_des_ede3_cbc();
  134. #endif
  135. #ifndef NO_IDEA
  136. else if (strcmp(*argv,"-idea") == 0)
  137. enc=EVP_idea_cbc();
  138. #endif
  139. else
  140. {
  141. BIO_printf(bio_err,"unknown option %s\n",*argv);
  142. badops=1;
  143. break;
  144. }
  145. argc--;
  146. argv++;
  147. }
  148. if (badops)
  149. {
  150. bad:
  151. BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
  152. BIO_printf(bio_err,"where options are\n");
  153. BIO_printf(bio_err," -inform arg input format - one of DER TXT PEM\n");
  154. BIO_printf(bio_err," -outform arg output format - one of DER TXT PEM\n");
  155. BIO_printf(bio_err," -in arg inout file\n");
  156. BIO_printf(bio_err," -out arg output file\n");
  157. BIO_printf(bio_err," -print_certs print any certs or crl in the input\n");
  158. BIO_printf(bio_err," -des encrypt PEM output with cbc des\n");
  159. BIO_printf(bio_err," -des3 encrypt PEM output with ede cbc des using 168 bit key\n");
  160. #ifndef NO_IDEA
  161. BIO_printf(bio_err," -idea encrypt PEM output with cbc idea\n");
  162. #endif
  163. EXIT(1);
  164. }
  165. ERR_load_crypto_strings();
  166. in=BIO_new(BIO_s_file());
  167. out=BIO_new(BIO_s_file());
  168. if ((in == NULL) || (out == NULL))
  169. {
  170. ERR_print_errors(bio_err);
  171. goto end;
  172. }
  173. if (infile == NULL)
  174. BIO_set_fp(in,stdin,BIO_NOCLOSE);
  175. else
  176. {
  177. if (BIO_read_filename(in,infile) <= 0)
  178. if (in == NULL)
  179. {
  180. perror(infile);
  181. goto end;
  182. }
  183. }
  184. if (informat == FORMAT_ASN1)
  185. p7=d2i_PKCS7_bio(in,NULL);
  186. else if (informat == FORMAT_PEM)
  187. p7=PEM_read_bio_PKCS7(in,NULL,NULL);
  188. else
  189. {
  190. BIO_printf(bio_err,"bad input format specified for pkcs7 object\n");
  191. goto end;
  192. }
  193. if (p7 == NULL)
  194. {
  195. BIO_printf(bio_err,"unable to load PKCS7 object\n");
  196. ERR_print_errors(bio_err);
  197. goto end;
  198. }
  199. if (outfile == NULL)
  200. BIO_set_fp(out,stdout,BIO_NOCLOSE);
  201. else
  202. {
  203. if (BIO_write_filename(out,outfile) <= 0)
  204. {
  205. perror(outfile);
  206. goto end;
  207. }
  208. }
  209. if (print_certs)
  210. {
  211. STACK *certs=NULL;
  212. STACK *crls=NULL;
  213. i=OBJ_obj2nid(p7->type);
  214. switch (i)
  215. {
  216. case NID_pkcs7_signed:
  217. certs=p7->d.sign->cert;
  218. crls=p7->d.sign->crl;
  219. break;
  220. case NID_pkcs7_signedAndEnveloped:
  221. certs=p7->d.signed_and_enveloped->cert;
  222. crls=p7->d.signed_and_enveloped->crl;
  223. break;
  224. default:
  225. break;
  226. }
  227. if (certs != NULL)
  228. {
  229. X509 *x;
  230. for (i=0; i<sk_num(certs); i++)
  231. {
  232. x=(X509 *)sk_value(certs,i);
  233. X509_NAME_oneline(X509_get_subject_name(x),
  234. buf,256);
  235. BIO_puts(out,"subject=");
  236. BIO_puts(out,buf);
  237. X509_NAME_oneline(X509_get_issuer_name(x),
  238. buf,256);
  239. BIO_puts(out,"\nissuer= ");
  240. BIO_puts(out,buf);
  241. BIO_puts(out,"\n");
  242. PEM_write_bio_X509(out,x);
  243. BIO_puts(out,"\n");
  244. }
  245. }
  246. if (crls != NULL)
  247. {
  248. X509_CRL *crl;
  249. for (i=0; i<sk_num(crls); i++)
  250. {
  251. crl=(X509_CRL *)sk_value(crls,i);
  252. X509_NAME_oneline(crl->crl->issuer,buf,256);
  253. BIO_puts(out,"issuer= ");
  254. BIO_puts(out,buf);
  255. BIO_puts(out,"\nlast update=");
  256. ASN1_UTCTIME_print(out,crl->crl->lastUpdate);
  257. BIO_puts(out,"\nnext update=");
  258. ASN1_UTCTIME_print(out,crl->crl->nextUpdate);
  259. BIO_puts(out,"\n");
  260. PEM_write_bio_X509_CRL(out,crl);
  261. BIO_puts(out,"\n");
  262. }
  263. }
  264. ret=0;
  265. goto end;
  266. }
  267. if (outformat == FORMAT_ASN1)
  268. i=i2d_PKCS7_bio(out,p7);
  269. else if (outformat == FORMAT_PEM)
  270. i=PEM_write_bio_PKCS7(out,p7);
  271. else {
  272. BIO_printf(bio_err,"bad output format specified for outfile\n");
  273. goto end;
  274. }
  275. if (!i)
  276. {
  277. BIO_printf(bio_err,"unable to write pkcs7 object\n");
  278. ERR_print_errors(bio_err);
  279. goto end;
  280. }
  281. ret=0;
  282. end:
  283. if (p7 != NULL) PKCS7_free(p7);
  284. if (in != NULL) BIO_free(in);
  285. if (out != NULL) BIO_free(out);
  286. EXIT(ret);
  287. }