crl2p7.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright 1995-2018 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. } 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_stack = sk_X509_CRL_new_null()) == NULL)
  114. goto end;
  115. p7s->crl = crl_stack;
  116. if (crl != NULL) {
  117. sk_X509_CRL_push(crl_stack, crl);
  118. crl = NULL; /* now part of p7 for OPENSSL_freeing */
  119. }
  120. if ((cert_stack = sk_X509_new_null()) == NULL)
  121. goto end;
  122. p7s->cert = cert_stack;
  123. if (certflst != NULL)
  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. out = bio_open_default(outfile, 'w', outformat);
  133. if (out == NULL)
  134. goto end;
  135. if (outformat == FORMAT_ASN1)
  136. i = i2d_PKCS7_bio(out, p7);
  137. else if (outformat == FORMAT_PEM)
  138. i = PEM_write_bio_PKCS7(out, p7);
  139. if (!i) {
  140. BIO_printf(bio_err, "unable to write pkcs7 object\n");
  141. ERR_print_errors(bio_err);
  142. goto end;
  143. }
  144. ret = 0;
  145. end:
  146. sk_OPENSSL_STRING_free(certflst);
  147. BIO_free(in);
  148. BIO_free_all(out);
  149. PKCS7_free(p7);
  150. X509_CRL_free(crl);
  151. return ret;
  152. }
  153. /*-
  154. *----------------------------------------------------------------------
  155. * int add_certs_from_file
  156. *
  157. * Read a list of certificates to be checked from a file.
  158. *
  159. * Results:
  160. * number of certs added if successful, -1 if not.
  161. *----------------------------------------------------------------------
  162. */
  163. static int add_certs_from_file(STACK_OF(X509) *stack, char *certfile)
  164. {
  165. BIO *in = NULL;
  166. int count = 0;
  167. int ret = -1;
  168. STACK_OF(X509_INFO) *sk = NULL;
  169. X509_INFO *xi;
  170. in = BIO_new_file(certfile, "r");
  171. if (in == NULL) {
  172. BIO_printf(bio_err, "error opening the file, %s\n", certfile);
  173. goto end;
  174. }
  175. /* This loads from a file, a stack of x509/crl/pkey sets */
  176. sk = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
  177. if (sk == NULL) {
  178. BIO_printf(bio_err, "error reading the file, %s\n", certfile);
  179. goto end;
  180. }
  181. /* scan over it and pull out the CRL's */
  182. while (sk_X509_INFO_num(sk)) {
  183. xi = sk_X509_INFO_shift(sk);
  184. if (xi->x509 != NULL) {
  185. sk_X509_push(stack, xi->x509);
  186. xi->x509 = NULL;
  187. count++;
  188. }
  189. X509_INFO_free(xi);
  190. }
  191. ret = count;
  192. end:
  193. /* never need to OPENSSL_free x */
  194. BIO_free(in);
  195. sk_X509_INFO_free(sk);
  196. return ret;
  197. }