crl2p7.c 6.5 KB

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