pkcs7.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <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_ENGINE,
  25. 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. OPT_PROV_OPTIONS,
  45. {NULL}
  46. };
  47. int pkcs7_main(int argc, char **argv)
  48. {
  49. ENGINE *e = NULL;
  50. PKCS7 *p7 = NULL, *p7i;
  51. BIO *in = NULL, *out = NULL;
  52. int informat = FORMAT_PEM, outformat = FORMAT_PEM;
  53. char *infile = NULL, *outfile = NULL, *prog;
  54. int i, print_certs = 0, text = 0, noout = 0, p7_print = 0, ret = 1;
  55. OPTION_CHOICE o;
  56. OSSL_LIB_CTX *libctx = app_get0_libctx();
  57. prog = opt_init(argc, argv, pkcs7_options);
  58. while ((o = opt_next()) != OPT_EOF) {
  59. switch (o) {
  60. case OPT_EOF:
  61. case OPT_ERR:
  62. opthelp:
  63. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  64. goto end;
  65. case OPT_HELP:
  66. opt_help(pkcs7_options);
  67. ret = 0;
  68. goto end;
  69. case OPT_INFORM:
  70. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  71. goto opthelp;
  72. break;
  73. case OPT_OUTFORM:
  74. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  75. goto opthelp;
  76. break;
  77. case OPT_IN:
  78. infile = opt_arg();
  79. break;
  80. case OPT_OUT:
  81. outfile = opt_arg();
  82. break;
  83. case OPT_NOOUT:
  84. noout = 1;
  85. break;
  86. case OPT_TEXT:
  87. text = 1;
  88. break;
  89. case OPT_PRINT:
  90. p7_print = 1;
  91. break;
  92. case OPT_PRINT_CERTS:
  93. print_certs = 1;
  94. break;
  95. case OPT_ENGINE:
  96. e = setup_engine(opt_arg(), 0);
  97. break;
  98. case OPT_PROV_CASES:
  99. if (!opt_provider(o))
  100. goto end;
  101. break;
  102. }
  103. }
  104. /* No extra arguments. */
  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. p7 = PKCS7_new_ex(libctx, app_get0_propq());
  112. if (p7 == NULL) {
  113. BIO_printf(bio_err, "unable to allocate PKCS7 object\n");
  114. ERR_print_errors(bio_err);
  115. goto end;
  116. }
  117. if (informat == FORMAT_ASN1)
  118. p7i = d2i_PKCS7_bio(in, &p7);
  119. else
  120. p7i = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
  121. if (p7i == NULL) {
  122. BIO_printf(bio_err, "unable to load PKCS7 object\n");
  123. ERR_print_errors(bio_err);
  124. goto end;
  125. }
  126. out = bio_open_default(outfile, 'w', outformat);
  127. if (out == NULL)
  128. goto end;
  129. if (p7_print)
  130. PKCS7_print_ctx(out, p7, 0, NULL);
  131. if (print_certs) {
  132. STACK_OF(X509) *certs = NULL;
  133. STACK_OF(X509_CRL) *crls = NULL;
  134. i = OBJ_obj2nid(p7->type);
  135. switch (i) {
  136. case NID_pkcs7_signed:
  137. if (p7->d.sign != NULL) {
  138. certs = p7->d.sign->cert;
  139. crls = p7->d.sign->crl;
  140. }
  141. break;
  142. case NID_pkcs7_signedAndEnveloped:
  143. if (p7->d.signed_and_enveloped != NULL) {
  144. certs = p7->d.signed_and_enveloped->cert;
  145. crls = p7->d.signed_and_enveloped->crl;
  146. }
  147. break;
  148. default:
  149. break;
  150. }
  151. if (certs != NULL) {
  152. X509 *x;
  153. for (i = 0; i < sk_X509_num(certs); i++) {
  154. x = sk_X509_value(certs, i);
  155. if (text)
  156. X509_print(out, x);
  157. else
  158. dump_cert_text(out, x);
  159. if (!noout)
  160. PEM_write_bio_X509(out, x);
  161. BIO_puts(out, "\n");
  162. }
  163. }
  164. if (crls != NULL) {
  165. X509_CRL *crl;
  166. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  167. crl = sk_X509_CRL_value(crls, i);
  168. X509_CRL_print_ex(out, crl, get_nameopt());
  169. if (!noout)
  170. PEM_write_bio_X509_CRL(out, crl);
  171. BIO_puts(out, "\n");
  172. }
  173. }
  174. ret = 0;
  175. goto end;
  176. }
  177. if (!noout) {
  178. if (outformat == FORMAT_ASN1)
  179. i = i2d_PKCS7_bio(out, p7);
  180. else
  181. i = PEM_write_bio_PKCS7(out, p7);
  182. if (!i) {
  183. BIO_printf(bio_err, "unable to write pkcs7 object\n");
  184. ERR_print_errors(bio_err);
  185. goto end;
  186. }
  187. }
  188. ret = 0;
  189. end:
  190. PKCS7_free(p7);
  191. release_engine(e);
  192. BIO_free(in);
  193. BIO_free_all(out);
  194. return ret;
  195. }