2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
16 #include <openssl/err.h>
17 #include <openssl/objects.h>
18 #include <openssl/evp.h>
19 #include <openssl/x509.h>
20 #include <openssl/pkcs7.h>
21 #include <openssl/pem.h>
24 DEFINE_STACK_OF(X509_CRL)
26 typedef enum OPTION_choice {
27 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
28 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOOUT,
29 OPT_TEXT, OPT_PRINT, OPT_PRINT_CERTS, OPT_ENGINE,
33 const OPTIONS pkcs7_options[] = {
34 OPT_SECTION("General"),
35 {"help", OPT_HELP, '-', "Display this summary"},
36 #ifndef OPENSSL_NO_ENGINE
37 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
41 {"in", OPT_IN, '<', "Input file"},
42 {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
44 OPT_SECTION("Output"),
45 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
46 {"out", OPT_OUT, '>', "Output file"},
47 {"noout", OPT_NOOUT, '-', "Don't output encoded data"},
48 {"text", OPT_TEXT, '-', "Print full details of certificates"},
49 {"print", OPT_PRINT, '-', "Print out all fields of the PKCS7 structure"},
50 {"print_certs", OPT_PRINT_CERTS, '-',
51 "Print_certs print any certs or crl in the input"},
57 int pkcs7_main(int argc, char **argv)
60 PKCS7 *p7 = NULL, *p7i;
61 BIO *in = NULL, *out = NULL;
62 int informat = FORMAT_PEM, outformat = FORMAT_PEM;
63 char *infile = NULL, *outfile = NULL, *prog;
64 int i, print_certs = 0, text = 0, noout = 0, p7_print = 0, ret = 1;
66 OPENSSL_CTX *libctx = app_get0_libctx();
67 const char *propq = app_get0_propq();
69 prog = opt_init(argc, argv, pkcs7_options);
70 while ((o = opt_next()) != OPT_EOF) {
75 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
78 opt_help(pkcs7_options);
82 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
86 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
104 case OPT_PRINT_CERTS:
108 e = setup_engine(opt_arg(), 0);
111 if (!opt_provider(o))
116 argc = opt_num_rest();
120 in = bio_open_default(infile, 'r', informat);
124 p7 = PKCS7_new_with_libctx(libctx, propq);
126 BIO_printf(bio_err, "unable to allocate PKCS7 object\n");
127 ERR_print_errors(bio_err);
131 if (informat == FORMAT_ASN1)
132 p7i = d2i_PKCS7_bio(in, &p7);
134 p7i = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
136 BIO_printf(bio_err, "unable to load PKCS7 object\n");
137 ERR_print_errors(bio_err);
141 out = bio_open_default(outfile, 'w', outformat);
146 PKCS7_print_ctx(out, p7, 0, NULL);
149 STACK_OF(X509) *certs = NULL;
150 STACK_OF(X509_CRL) *crls = NULL;
152 i = OBJ_obj2nid(p7->type);
154 case NID_pkcs7_signed:
155 if (p7->d.sign != NULL) {
156 certs = p7->d.sign->cert;
157 crls = p7->d.sign->crl;
160 case NID_pkcs7_signedAndEnveloped:
161 if (p7->d.signed_and_enveloped != NULL) {
162 certs = p7->d.signed_and_enveloped->cert;
163 crls = p7->d.signed_and_enveloped->crl;
173 for (i = 0; i < sk_X509_num(certs); i++) {
174 x = sk_X509_value(certs, i);
178 dump_cert_text(out, x);
181 PEM_write_bio_X509(out, x);
188 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
189 crl = sk_X509_CRL_value(crls, i);
191 X509_CRL_print_ex(out, crl, get_nameopt());
194 PEM_write_bio_X509_CRL(out, crl);
204 if (outformat == FORMAT_ASN1)
205 i = i2d_PKCS7_bio(out, p7);
207 i = PEM_write_bio_PKCS7(out, p7);
210 BIO_printf(bio_err, "unable to write pkcs7 object\n");
211 ERR_print_errors(bio_err);