2
0

crl2p7.c 6.2 KB

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