pkcs7.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright 1995-2022 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. typedef enum OPTION_choice {
  22. OPT_COMMON,
  23. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOOUT,
  24. OPT_TEXT, OPT_PRINT, OPT_PRINT_CERTS, OPT_QUIET,
  25. OPT_ENGINE, OPT_PROV_ENUM
  26. } OPTION_CHOICE;
  27. const OPTIONS pkcs7_options[] = {
  28. OPT_SECTION("General"),
  29. {"help", OPT_HELP, '-', "Display this summary"},
  30. #ifndef OPENSSL_NO_ENGINE
  31. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  32. #endif
  33. OPT_SECTION("Input"),
  34. {"in", OPT_IN, '<', "Input file"},
  35. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  36. OPT_SECTION("Output"),
  37. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  38. {"out", OPT_OUT, '>', "Output file"},
  39. {"noout", OPT_NOOUT, '-', "Don't output encoded data"},
  40. {"text", OPT_TEXT, '-', "Print full details of certificates"},
  41. {"print", OPT_PRINT, '-', "Print out all fields of the PKCS7 structure"},
  42. {"print_certs", OPT_PRINT_CERTS, '-',
  43. "Print_certs print any certs or crl in the input"},
  44. {"quiet", OPT_QUIET, '-',
  45. "When used with -print_certs, it produces a cleaner output"},
  46. OPT_PROV_OPTIONS,
  47. {NULL}
  48. };
  49. int pkcs7_main(int argc, char **argv)
  50. {
  51. ENGINE *e = NULL;
  52. PKCS7 *p7 = NULL, *p7i;
  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, quiet = 0, ret = 1;
  57. OPTION_CHOICE o;
  58. OSSL_LIB_CTX *libctx = app_get0_libctx();
  59. prog = opt_init(argc, argv, pkcs7_options);
  60. while ((o = opt_next()) != OPT_EOF) {
  61. switch (o) {
  62. case OPT_EOF:
  63. case OPT_ERR:
  64. opthelp:
  65. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  66. goto end;
  67. case OPT_HELP:
  68. opt_help(pkcs7_options);
  69. ret = 0;
  70. goto end;
  71. case OPT_INFORM:
  72. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  73. goto opthelp;
  74. break;
  75. case OPT_OUTFORM:
  76. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  77. goto opthelp;
  78. break;
  79. case OPT_IN:
  80. infile = opt_arg();
  81. break;
  82. case OPT_OUT:
  83. outfile = opt_arg();
  84. break;
  85. case OPT_NOOUT:
  86. noout = 1;
  87. break;
  88. case OPT_TEXT:
  89. text = 1;
  90. break;
  91. case OPT_PRINT:
  92. p7_print = 1;
  93. break;
  94. case OPT_PRINT_CERTS:
  95. print_certs = 1;
  96. break;
  97. case OPT_QUIET:
  98. quiet = 1;
  99. break;
  100. case OPT_ENGINE:
  101. e = setup_engine(opt_arg(), 0);
  102. break;
  103. case OPT_PROV_CASES:
  104. if (!opt_provider(o))
  105. goto end;
  106. break;
  107. }
  108. }
  109. /* No extra arguments. */
  110. if (!opt_check_rest_arg(NULL))
  111. goto opthelp;
  112. in = bio_open_default(infile, 'r', informat);
  113. if (in == NULL)
  114. goto end;
  115. p7 = PKCS7_new_ex(libctx, app_get0_propq());
  116. if (p7 == NULL) {
  117. BIO_printf(bio_err, "unable to allocate PKCS7 object\n");
  118. ERR_print_errors(bio_err);
  119. goto end;
  120. }
  121. if (informat == FORMAT_ASN1)
  122. p7i = d2i_PKCS7_bio(in, &p7);
  123. else
  124. p7i = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
  125. if (p7i == NULL) {
  126. BIO_printf(bio_err, "unable to load PKCS7 object\n");
  127. ERR_print_errors(bio_err);
  128. goto end;
  129. }
  130. out = bio_open_default(outfile, 'w', outformat);
  131. if (out == NULL)
  132. goto end;
  133. if (p7_print)
  134. PKCS7_print_ctx(out, p7, 0, NULL);
  135. if (print_certs) {
  136. STACK_OF(X509) *certs = NULL;
  137. STACK_OF(X509_CRL) *crls = NULL;
  138. i = OBJ_obj2nid(p7->type);
  139. switch (i) {
  140. case NID_pkcs7_signed:
  141. if (p7->d.sign != NULL) {
  142. certs = p7->d.sign->cert;
  143. crls = p7->d.sign->crl;
  144. }
  145. break;
  146. case NID_pkcs7_signedAndEnveloped:
  147. if (p7->d.signed_and_enveloped != NULL) {
  148. certs = p7->d.signed_and_enveloped->cert;
  149. crls = p7->d.signed_and_enveloped->crl;
  150. }
  151. break;
  152. default:
  153. break;
  154. }
  155. if (certs != NULL) {
  156. X509 *x;
  157. for (i = 0; i < sk_X509_num(certs); i++) {
  158. x = sk_X509_value(certs, i);
  159. if (text)
  160. X509_print(out, x);
  161. else if (!quiet)
  162. dump_cert_text(out, x);
  163. if (!noout)
  164. PEM_write_bio_X509(out, x);
  165. BIO_puts(out, "\n");
  166. }
  167. }
  168. if (crls != NULL) {
  169. X509_CRL *crl;
  170. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  171. crl = sk_X509_CRL_value(crls, i);
  172. X509_CRL_print_ex(out, crl, get_nameopt());
  173. if (!noout)
  174. PEM_write_bio_X509_CRL(out, crl);
  175. BIO_puts(out, "\n");
  176. }
  177. }
  178. ret = 0;
  179. goto end;
  180. }
  181. if (!noout) {
  182. if (outformat == FORMAT_ASN1)
  183. i = i2d_PKCS7_bio(out, p7);
  184. else
  185. i = PEM_write_bio_PKCS7(out, p7);
  186. if (!i) {
  187. BIO_printf(bio_err, "unable to write pkcs7 object\n");
  188. ERR_print_errors(bio_err);
  189. goto end;
  190. }
  191. }
  192. ret = 0;
  193. end:
  194. PKCS7_free(p7);
  195. release_engine(e);
  196. BIO_free(in);
  197. BIO_free_all(out);
  198. return ret;
  199. }