pkcs7.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include "apps.h"
  14. #include "progs.h"
  15. #include <openssl/err.h>
  16. #include <openssl/objects.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/x509.h>
  19. #include <openssl/pkcs7.h>
  20. #include <openssl/pem.h>
  21. DEFINE_STACK_OF(X509)
  22. DEFINE_STACK_OF(X509_CRL)
  23. typedef enum OPTION_choice {
  24. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  25. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOOUT,
  26. OPT_TEXT, OPT_PRINT, OPT_PRINT_CERTS, OPT_ENGINE,
  27. OPT_PROV_ENUM
  28. } OPTION_CHOICE;
  29. const OPTIONS pkcs7_options[] = {
  30. OPT_SECTION("General"),
  31. {"help", OPT_HELP, '-', "Display this summary"},
  32. #ifndef OPENSSL_NO_ENGINE
  33. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  34. #endif
  35. OPT_SECTION("Input"),
  36. {"in", OPT_IN, '<', "Input file"},
  37. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  38. OPT_SECTION("Output"),
  39. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  40. {"out", OPT_OUT, '>', "Output file"},
  41. {"noout", OPT_NOOUT, '-', "Don't output encoded data"},
  42. {"text", OPT_TEXT, '-', "Print full details of certificates"},
  43. {"print", OPT_PRINT, '-', "Print out all fields of the PKCS7 structure"},
  44. {"print_certs", OPT_PRINT_CERTS, '-',
  45. "Print_certs print any certs or crl in the input"},
  46. OPT_PROV_OPTIONS,
  47. {NULL}
  48. };
  49. int pkcs7_main(int argc, char **argv)
  50. {
  51. ENGINE *e = NULL;
  52. PKCS7 *p7 = NULL;
  53. BIO *in = NULL, *out = NULL;
  54. int informat = FORMAT_PEM, outformat = FORMAT_PEM;
  55. char *infile = NULL, *outfile = NULL, *prog;
  56. int i, print_certs = 0, text = 0, noout = 0, p7_print = 0, ret = 1;
  57. OPTION_CHOICE o;
  58. prog = opt_init(argc, argv, pkcs7_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(pkcs7_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_NOOUT:
  85. noout = 1;
  86. break;
  87. case OPT_TEXT:
  88. text = 1;
  89. break;
  90. case OPT_PRINT:
  91. p7_print = 1;
  92. break;
  93. case OPT_PRINT_CERTS:
  94. print_certs = 1;
  95. break;
  96. case OPT_ENGINE:
  97. e = setup_engine(opt_arg(), 0);
  98. break;
  99. case OPT_PROV_CASES:
  100. if (!opt_provider(o))
  101. goto end;
  102. break;
  103. }
  104. }
  105. argc = opt_num_rest();
  106. if (argc != 0)
  107. goto opthelp;
  108. in = bio_open_default(infile, 'r', informat);
  109. if (in == NULL)
  110. goto end;
  111. if (informat == FORMAT_ASN1)
  112. p7 = d2i_PKCS7_bio(in, NULL);
  113. else
  114. p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
  115. if (p7 == NULL) {
  116. BIO_printf(bio_err, "unable to load PKCS7 object\n");
  117. ERR_print_errors(bio_err);
  118. goto end;
  119. }
  120. out = bio_open_default(outfile, 'w', outformat);
  121. if (out == NULL)
  122. goto end;
  123. if (p7_print)
  124. PKCS7_print_ctx(out, p7, 0, NULL);
  125. if (print_certs) {
  126. STACK_OF(X509) *certs = NULL;
  127. STACK_OF(X509_CRL) *crls = NULL;
  128. i = OBJ_obj2nid(p7->type);
  129. switch (i) {
  130. case NID_pkcs7_signed:
  131. if (p7->d.sign != NULL) {
  132. certs = p7->d.sign->cert;
  133. crls = p7->d.sign->crl;
  134. }
  135. break;
  136. case NID_pkcs7_signedAndEnveloped:
  137. if (p7->d.signed_and_enveloped != NULL) {
  138. certs = p7->d.signed_and_enveloped->cert;
  139. crls = p7->d.signed_and_enveloped->crl;
  140. }
  141. break;
  142. default:
  143. break;
  144. }
  145. if (certs != NULL) {
  146. X509 *x;
  147. for (i = 0; i < sk_X509_num(certs); i++) {
  148. x = sk_X509_value(certs, i);
  149. if (text)
  150. X509_print(out, x);
  151. else
  152. dump_cert_text(out, x);
  153. if (!noout)
  154. PEM_write_bio_X509(out, x);
  155. BIO_puts(out, "\n");
  156. }
  157. }
  158. if (crls != NULL) {
  159. X509_CRL *crl;
  160. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  161. crl = sk_X509_CRL_value(crls, i);
  162. X509_CRL_print_ex(out, crl, get_nameopt());
  163. if (!noout)
  164. PEM_write_bio_X509_CRL(out, crl);
  165. BIO_puts(out, "\n");
  166. }
  167. }
  168. ret = 0;
  169. goto end;
  170. }
  171. if (!noout) {
  172. if (outformat == FORMAT_ASN1)
  173. i = i2d_PKCS7_bio(out, p7);
  174. else
  175. i = PEM_write_bio_PKCS7(out, p7);
  176. if (!i) {
  177. BIO_printf(bio_err, "unable to write pkcs7 object\n");
  178. ERR_print_errors(bio_err);
  179. goto end;
  180. }
  181. }
  182. ret = 0;
  183. end:
  184. PKCS7_free(p7);
  185. release_engine(e);
  186. BIO_free(in);
  187. BIO_free_all(out);
  188. return ret;
  189. }