pkcs7.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /* ====================================================================
  58. * Copyright (c) 1999-2015 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * licensing@OpenSSL.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. */
  105. #include <stdio.h>
  106. #include <stdlib.h>
  107. #include <string.h>
  108. #include <time.h>
  109. #include "apps.h"
  110. #include <openssl/err.h>
  111. #include <openssl/objects.h>
  112. #include <openssl/evp.h>
  113. #include <openssl/x509.h>
  114. #include <openssl/pkcs7.h>
  115. #include <openssl/pem.h>
  116. typedef enum OPTION_choice {
  117. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  118. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOOUT,
  119. OPT_TEXT, OPT_PRINT, OPT_PRINT_CERTS, OPT_ENGINE
  120. } OPTION_CHOICE;
  121. OPTIONS pkcs7_options[] = {
  122. {"help", OPT_HELP, '-', "Display this summary"},
  123. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  124. {"in", OPT_IN, '<', "Input file"},
  125. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  126. {"out", OPT_OUT, '>', "Output file"},
  127. {"noout", OPT_NOOUT, '-', "Don't output encoded data"},
  128. {"text", OPT_TEXT, '-', "Print full details of certificates"},
  129. {"print", OPT_PRINT, '-'},
  130. {"print_certs", OPT_PRINT_CERTS, '-',
  131. "Print_certs print any certs or crl in the input"},
  132. #ifndef OPENSSL_NO_ENGINE
  133. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  134. #endif
  135. {NULL}
  136. };
  137. int pkcs7_main(int argc, char **argv)
  138. {
  139. PKCS7 *p7 = NULL;
  140. BIO *in = NULL, *out = NULL;
  141. int informat = FORMAT_PEM, outformat = FORMAT_PEM;
  142. char *infile = NULL, *outfile = NULL, *prog;
  143. int i, print_certs = 0, text = 0, noout = 0, p7_print = 0, ret = 1;
  144. OPTION_CHOICE o;
  145. prog = opt_init(argc, argv, pkcs7_options);
  146. while ((o = opt_next()) != OPT_EOF) {
  147. switch (o) {
  148. case OPT_EOF:
  149. case OPT_ERR:
  150. opthelp:
  151. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  152. goto end;
  153. case OPT_HELP:
  154. opt_help(pkcs7_options);
  155. ret = 0;
  156. goto end;
  157. case OPT_INFORM:
  158. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  159. goto opthelp;
  160. break;
  161. case OPT_OUTFORM:
  162. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  163. goto opthelp;
  164. break;
  165. case OPT_IN:
  166. infile = opt_arg();
  167. break;
  168. case OPT_OUT:
  169. outfile = opt_arg();
  170. break;
  171. case OPT_NOOUT:
  172. noout = 1;
  173. break;
  174. case OPT_TEXT:
  175. text = 1;
  176. break;
  177. case OPT_PRINT:
  178. p7_print = 1;
  179. break;
  180. case OPT_PRINT_CERTS:
  181. print_certs = 1;
  182. break;
  183. case OPT_ENGINE:
  184. (void)setup_engine(opt_arg(), 0);
  185. break;
  186. }
  187. }
  188. argc = opt_num_rest();
  189. argv = opt_rest();
  190. in = bio_open_default(infile, 'r', informat);
  191. if (in == NULL)
  192. goto end;
  193. if (informat == FORMAT_ASN1)
  194. p7 = d2i_PKCS7_bio(in, NULL);
  195. else
  196. p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
  197. if (p7 == NULL) {
  198. BIO_printf(bio_err, "unable to load PKCS7 object\n");
  199. ERR_print_errors(bio_err);
  200. goto end;
  201. }
  202. out = bio_open_default(outfile, 'w', outformat);
  203. if (out == NULL)
  204. goto end;
  205. if (p7_print)
  206. PKCS7_print_ctx(out, p7, 0, NULL);
  207. if (print_certs) {
  208. STACK_OF(X509) *certs = NULL;
  209. STACK_OF(X509_CRL) *crls = NULL;
  210. i = OBJ_obj2nid(p7->type);
  211. switch (i) {
  212. case NID_pkcs7_signed:
  213. certs = p7->d.sign->cert;
  214. crls = p7->d.sign->crl;
  215. break;
  216. case NID_pkcs7_signedAndEnveloped:
  217. certs = p7->d.signed_and_enveloped->cert;
  218. crls = p7->d.signed_and_enveloped->crl;
  219. break;
  220. default:
  221. break;
  222. }
  223. if (certs != NULL) {
  224. X509 *x;
  225. for (i = 0; i < sk_X509_num(certs); i++) {
  226. x = sk_X509_value(certs, i);
  227. if (text)
  228. X509_print(out, x);
  229. else
  230. dump_cert_text(out, x);
  231. if (!noout)
  232. PEM_write_bio_X509(out, x);
  233. BIO_puts(out, "\n");
  234. }
  235. }
  236. if (crls != NULL) {
  237. X509_CRL *crl;
  238. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  239. crl = sk_X509_CRL_value(crls, i);
  240. X509_CRL_print(out, crl);
  241. if (!noout)
  242. PEM_write_bio_X509_CRL(out, crl);
  243. BIO_puts(out, "\n");
  244. }
  245. }
  246. ret = 0;
  247. goto end;
  248. }
  249. if (!noout) {
  250. if (outformat == FORMAT_ASN1)
  251. i = i2d_PKCS7_bio(out, p7);
  252. else
  253. i = PEM_write_bio_PKCS7(out, p7);
  254. if (!i) {
  255. BIO_printf(bio_err, "unable to write pkcs7 object\n");
  256. ERR_print_errors(bio_err);
  257. goto end;
  258. }
  259. }
  260. ret = 0;
  261. end:
  262. PKCS7_free(p7);
  263. BIO_free(in);
  264. BIO_free_all(out);
  265. return (ret);
  266. }