crl2p7.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * This was written by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu> and
  59. * donated 'to the cause' along with lots and lots of other fixes to the
  60. * library.
  61. */
  62. #include <stdio.h>
  63. #include <string.h>
  64. #include <sys/types.h>
  65. #include "apps.h"
  66. #include <openssl/err.h>
  67. #include <openssl/evp.h>
  68. #include <openssl/x509.h>
  69. #include <openssl/pkcs7.h>
  70. #include <openssl/pem.h>
  71. #include <openssl/objects.h>
  72. static int add_certs_from_file(STACK_OF(X509) *stack, char *certfile);
  73. typedef enum OPTION_choice {
  74. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  75. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOCRL, OPT_CERTFILE
  76. } OPTION_CHOICE;
  77. OPTIONS crl2pkcs7_options[] = {
  78. {"help", OPT_HELP, '-', "Display this summary"},
  79. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  80. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  81. {"in", OPT_IN, '<', "Input file"},
  82. {"out", OPT_OUT, '>', "Output file"},
  83. {"nocrl", OPT_NOCRL, '-', "No crl to load, just certs from '-certfile'"},
  84. {"certfile", OPT_CERTFILE, '<',
  85. "File of chain of certs to a trusted CA; can be repeated"},
  86. {NULL}
  87. };
  88. int crl2pkcs7_main(int argc, char **argv)
  89. {
  90. BIO *in = NULL, *out = NULL;
  91. PKCS7 *p7 = NULL;
  92. PKCS7_SIGNED *p7s = NULL;
  93. STACK_OF(OPENSSL_STRING) *certflst = NULL;
  94. STACK_OF(X509) *cert_stack = NULL;
  95. STACK_OF(X509_CRL) *crl_stack = NULL;
  96. X509_CRL *crl = NULL;
  97. char *infile = NULL, *outfile = NULL, *prog, *certfile;
  98. int i = 0, informat = FORMAT_PEM, outformat = FORMAT_PEM, ret = 1, nocrl =
  99. 0;
  100. OPTION_CHOICE o;
  101. prog = opt_init(argc, argv, crl2pkcs7_options);
  102. while ((o = opt_next()) != OPT_EOF) {
  103. switch (o) {
  104. case OPT_EOF:
  105. case OPT_ERR:
  106. opthelp:
  107. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  108. goto end;
  109. case OPT_HELP:
  110. opt_help(crl2pkcs7_options);
  111. ret = 0;
  112. goto end;
  113. case OPT_INFORM:
  114. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  115. goto opthelp;
  116. break;
  117. case OPT_OUTFORM:
  118. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  119. goto opthelp;
  120. break;
  121. case OPT_IN:
  122. infile = opt_arg();
  123. break;
  124. case OPT_OUT:
  125. outfile = opt_arg();
  126. break;
  127. case OPT_NOCRL:
  128. nocrl = 1;
  129. break;
  130. case OPT_CERTFILE:
  131. if ((certflst == NULL)
  132. && (certflst = sk_OPENSSL_STRING_new_null()) == NULL)
  133. goto end;
  134. if (!sk_OPENSSL_STRING_push(certflst, opt_arg())) {
  135. sk_OPENSSL_STRING_free(certflst);
  136. goto end;
  137. }
  138. break;
  139. }
  140. }
  141. argc = opt_num_rest();
  142. argv = opt_rest();
  143. if (!nocrl) {
  144. in = bio_open_default(infile, 'r', informat);
  145. if (in == NULL)
  146. goto end;
  147. if (informat == FORMAT_ASN1)
  148. crl = d2i_X509_CRL_bio(in, NULL);
  149. else if (informat == FORMAT_PEM)
  150. crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
  151. if (crl == NULL) {
  152. BIO_printf(bio_err, "unable to load CRL\n");
  153. ERR_print_errors(bio_err);
  154. goto end;
  155. }
  156. }
  157. if ((p7 = PKCS7_new()) == NULL)
  158. goto end;
  159. if ((p7s = PKCS7_SIGNED_new()) == NULL)
  160. goto end;
  161. p7->type = OBJ_nid2obj(NID_pkcs7_signed);
  162. p7->d.sign = p7s;
  163. p7s->contents->type = OBJ_nid2obj(NID_pkcs7_data);
  164. if (!ASN1_INTEGER_set(p7s->version, 1))
  165. goto end;
  166. if ((crl_stack = sk_X509_CRL_new_null()) == NULL)
  167. goto end;
  168. p7s->crl = crl_stack;
  169. if (crl != NULL) {
  170. sk_X509_CRL_push(crl_stack, crl);
  171. crl = NULL; /* now part of p7 for OPENSSL_freeing */
  172. }
  173. if ((cert_stack = sk_X509_new_null()) == NULL)
  174. goto end;
  175. p7s->cert = cert_stack;
  176. if (certflst)
  177. for (i = 0; i < sk_OPENSSL_STRING_num(certflst); i++) {
  178. certfile = sk_OPENSSL_STRING_value(certflst, i);
  179. if (add_certs_from_file(cert_stack, certfile) < 0) {
  180. BIO_printf(bio_err, "error loading certificates\n");
  181. ERR_print_errors(bio_err);
  182. goto end;
  183. }
  184. }
  185. sk_OPENSSL_STRING_free(certflst);
  186. out = bio_open_default(outfile, 'w', outformat);
  187. if (out == NULL)
  188. goto end;
  189. if (outformat == FORMAT_ASN1)
  190. i = i2d_PKCS7_bio(out, p7);
  191. else if (outformat == FORMAT_PEM)
  192. i = PEM_write_bio_PKCS7(out, p7);
  193. if (!i) {
  194. BIO_printf(bio_err, "unable to write pkcs7 object\n");
  195. ERR_print_errors(bio_err);
  196. goto end;
  197. }
  198. ret = 0;
  199. end:
  200. BIO_free(in);
  201. BIO_free_all(out);
  202. PKCS7_free(p7);
  203. X509_CRL_free(crl);
  204. return (ret);
  205. }
  206. /*-
  207. *----------------------------------------------------------------------
  208. * int add_certs_from_file
  209. *
  210. * Read a list of certificates to be checked from a file.
  211. *
  212. * Results:
  213. * number of certs added if successful, -1 if not.
  214. *----------------------------------------------------------------------
  215. */
  216. static int add_certs_from_file(STACK_OF(X509) *stack, char *certfile)
  217. {
  218. BIO *in = NULL;
  219. int count = 0;
  220. int ret = -1;
  221. STACK_OF(X509_INFO) *sk = NULL;
  222. X509_INFO *xi;
  223. in = BIO_new_file(certfile, "r");
  224. if (in == NULL) {
  225. BIO_printf(bio_err, "error opening the file, %s\n", certfile);
  226. goto end;
  227. }
  228. /* This loads from a file, a stack of x509/crl/pkey sets */
  229. sk = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
  230. if (sk == NULL) {
  231. BIO_printf(bio_err, "error reading the file, %s\n", certfile);
  232. goto end;
  233. }
  234. /* scan over it and pull out the CRL's */
  235. while (sk_X509_INFO_num(sk)) {
  236. xi = sk_X509_INFO_shift(sk);
  237. if (xi->x509 != NULL) {
  238. sk_X509_push(stack, xi->x509);
  239. xi->x509 = NULL;
  240. count++;
  241. }
  242. X509_INFO_free(xi);
  243. }
  244. ret = count;
  245. end:
  246. /* never need to OPENSSL_free x */
  247. BIO_free(in);
  248. sk_X509_INFO_free(sk);
  249. return (ret);
  250. }