pkcs7.c 5.4 KB

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