2
0

crl2p7.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include "apps.h"
  13. #include "progs.h"
  14. #include <openssl/err.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/pkcs7.h>
  18. #include <openssl/pem.h>
  19. #include <openssl/objects.h>
  20. DEFINE_STACK_OF(X509_CRL)
  21. DEFINE_STACK_OF(X509)
  22. DEFINE_STACK_OF(X509_INFO)
  23. DEFINE_STACK_OF_STRING()
  24. static int add_certs_from_file(STACK_OF(X509) *stack, char *certfile);
  25. typedef enum OPTION_choice {
  26. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  27. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOCRL, OPT_CERTFILE,
  28. OPT_PROV_ENUM
  29. } OPTION_CHOICE;
  30. const OPTIONS crl2pkcs7_options[] = {
  31. OPT_SECTION("General"),
  32. {"help", OPT_HELP, '-', "Display this summary"},
  33. OPT_SECTION("Input"),
  34. {"in", OPT_IN, '<', "Input file"},
  35. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  36. {"nocrl", OPT_NOCRL, '-', "No crl to load, just certs from '-certfile'"},
  37. {"certfile", OPT_CERTFILE, '<',
  38. "File of chain of certs to a trusted CA; can be repeated"},
  39. OPT_SECTION("Output"),
  40. {"out", OPT_OUT, '>', "Output file"},
  41. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  42. OPT_PROV_OPTIONS,
  43. {NULL}
  44. };
  45. int crl2pkcs7_main(int argc, char **argv)
  46. {
  47. BIO *in = NULL, *out = NULL;
  48. PKCS7 *p7 = NULL;
  49. PKCS7_SIGNED *p7s = NULL;
  50. STACK_OF(OPENSSL_STRING) *certflst = NULL;
  51. STACK_OF(X509) *cert_stack = NULL;
  52. STACK_OF(X509_CRL) *crl_stack = NULL;
  53. X509_CRL *crl = NULL;
  54. char *infile = NULL, *outfile = NULL, *prog, *certfile;
  55. int i = 0, informat = FORMAT_PEM, outformat = FORMAT_PEM, ret = 1, nocrl =
  56. 0;
  57. OPTION_CHOICE o;
  58. prog = opt_init(argc, argv, crl2pkcs7_options);
  59. while ((o = opt_next()) != OPT_EOF) {
  60. switch (o) {
  61. case OPT_EOF:
  62. case OPT_ERR:
  63. opthelp:
  64. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  65. goto end;
  66. case OPT_HELP:
  67. opt_help(crl2pkcs7_options);
  68. ret = 0;
  69. goto end;
  70. case OPT_INFORM:
  71. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  72. goto opthelp;
  73. break;
  74. case OPT_OUTFORM:
  75. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  76. goto opthelp;
  77. break;
  78. case OPT_IN:
  79. infile = opt_arg();
  80. break;
  81. case OPT_OUT:
  82. outfile = opt_arg();
  83. break;
  84. case OPT_NOCRL:
  85. nocrl = 1;
  86. break;
  87. case OPT_CERTFILE:
  88. if ((certflst == NULL)
  89. && (certflst = sk_OPENSSL_STRING_new_null()) == NULL)
  90. goto end;
  91. if (!sk_OPENSSL_STRING_push(certflst, opt_arg()))
  92. goto end;
  93. break;
  94. case OPT_PROV_CASES:
  95. if (!opt_provider(o))
  96. goto end;
  97. break;
  98. }
  99. }
  100. argc = opt_num_rest();
  101. if (argc != 0)
  102. goto opthelp;
  103. if (!nocrl) {
  104. in = bio_open_default(infile, 'r', informat);
  105. if (in == NULL)
  106. goto end;
  107. if (informat == FORMAT_ASN1)
  108. crl = d2i_X509_CRL_bio(in, NULL);
  109. else if (informat == FORMAT_PEM)
  110. crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
  111. if (crl == NULL) {
  112. BIO_printf(bio_err, "unable to load CRL\n");
  113. ERR_print_errors(bio_err);
  114. goto end;
  115. }
  116. }
  117. if ((p7 = PKCS7_new()) == NULL)
  118. goto end;
  119. if ((p7s = PKCS7_SIGNED_new()) == NULL)
  120. goto end;
  121. p7->type = OBJ_nid2obj(NID_pkcs7_signed);
  122. p7->d.sign = p7s;
  123. p7s->contents->type = OBJ_nid2obj(NID_pkcs7_data);
  124. if (!ASN1_INTEGER_set(p7s->version, 1))
  125. goto end;
  126. if ((crl_stack = sk_X509_CRL_new_null()) == NULL)
  127. goto end;
  128. p7s->crl = crl_stack;
  129. if (crl != NULL) {
  130. sk_X509_CRL_push(crl_stack, crl);
  131. crl = NULL; /* now part of p7 for OPENSSL_freeing */
  132. }
  133. if ((cert_stack = sk_X509_new_null()) == NULL)
  134. goto end;
  135. p7s->cert = cert_stack;
  136. if (certflst != NULL)
  137. for (i = 0; i < sk_OPENSSL_STRING_num(certflst); i++) {
  138. certfile = sk_OPENSSL_STRING_value(certflst, i);
  139. if (add_certs_from_file(cert_stack, certfile) < 0) {
  140. BIO_printf(bio_err, "error loading certificates\n");
  141. ERR_print_errors(bio_err);
  142. goto end;
  143. }
  144. }
  145. out = bio_open_default(outfile, 'w', outformat);
  146. if (out == NULL)
  147. goto end;
  148. if (outformat == FORMAT_ASN1)
  149. i = i2d_PKCS7_bio(out, p7);
  150. else if (outformat == FORMAT_PEM)
  151. i = PEM_write_bio_PKCS7(out, p7);
  152. if (!i) {
  153. BIO_printf(bio_err, "unable to write pkcs7 object\n");
  154. ERR_print_errors(bio_err);
  155. goto end;
  156. }
  157. ret = 0;
  158. end:
  159. sk_OPENSSL_STRING_free(certflst);
  160. BIO_free(in);
  161. BIO_free_all(out);
  162. PKCS7_free(p7);
  163. X509_CRL_free(crl);
  164. return ret;
  165. }
  166. /*-
  167. *----------------------------------------------------------------------
  168. * int add_certs_from_file
  169. *
  170. * Read a list of certificates to be checked from a file.
  171. *
  172. * Results:
  173. * number of certs added if successful, -1 if not.
  174. *----------------------------------------------------------------------
  175. */
  176. static int add_certs_from_file(STACK_OF(X509) *stack, char *certfile)
  177. {
  178. BIO *in = NULL;
  179. int count = 0;
  180. int ret = -1;
  181. STACK_OF(X509_INFO) *sk = NULL;
  182. X509_INFO *xi;
  183. in = BIO_new_file(certfile, "r");
  184. if (in == NULL) {
  185. BIO_printf(bio_err, "error opening the file, %s\n", certfile);
  186. goto end;
  187. }
  188. /* This loads from a file, a stack of x509/crl/pkey sets */
  189. sk = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
  190. if (sk == NULL) {
  191. BIO_printf(bio_err, "error reading the file, %s\n", certfile);
  192. goto end;
  193. }
  194. /* scan over it and pull out the CRL's */
  195. while (sk_X509_INFO_num(sk)) {
  196. xi = sk_X509_INFO_shift(sk);
  197. if (xi->x509 != NULL) {
  198. sk_X509_push(stack, xi->x509);
  199. xi->x509 = NULL;
  200. count++;
  201. }
  202. X509_INFO_free(xi);
  203. }
  204. ret = count;
  205. end:
  206. /* never need to OPENSSL_free x */
  207. BIO_free(in);
  208. sk_X509_INFO_free(sk);
  209. return ret;
  210. }