pkcs7.c 5.3 KB

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