2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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>
22 typedef enum OPTION_choice
{
23 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
24 OPT_INFORM
, OPT_OUTFORM
, OPT_IN
, OPT_OUT
, OPT_NOOUT
,
25 OPT_TEXT
, OPT_PRINT
, OPT_PRINT_CERTS
, OPT_ENGINE
28 const OPTIONS pkcs7_options
[] = {
29 {"help", OPT_HELP
, '-', "Display this summary"},
30 {"inform", OPT_INFORM
, 'F', "Input format - DER or PEM"},
31 {"in", OPT_IN
, '<', "Input file"},
32 {"outform", OPT_OUTFORM
, 'F', "Output format - DER or PEM"},
33 {"out", OPT_OUT
, '>', "Output file"},
34 {"noout", OPT_NOOUT
, '-', "Don't output encoded data"},
35 {"text", OPT_TEXT
, '-', "Print full details of certificates"},
36 {"print", OPT_PRINT
, '-', "Print out all fields of the PKCS7 structure"},
37 {"print_certs", OPT_PRINT_CERTS
, '-',
38 "Print_certs print any certs or crl in the input"},
39 #ifndef OPENSSL_NO_ENGINE
40 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
45 int pkcs7_main(int argc
, char **argv
)
48 BIO
*in
= NULL
, *out
= NULL
;
49 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
;
50 char *infile
= NULL
, *outfile
= NULL
, *prog
;
51 int i
, print_certs
= 0, text
= 0, noout
= 0, p7_print
= 0, ret
= 1;
54 prog
= opt_init(argc
, argv
, pkcs7_options
);
55 while ((o
= opt_next()) != OPT_EOF
) {
60 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
63 opt_help(pkcs7_options
);
67 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &informat
))
71 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &outformat
))
93 (void)setup_engine(opt_arg(), 0);
97 argc
= opt_num_rest();
101 in
= bio_open_default(infile
, 'r', informat
);
105 if (informat
== FORMAT_ASN1
)
106 p7
= d2i_PKCS7_bio(in
, NULL
);
108 p7
= PEM_read_bio_PKCS7(in
, NULL
, NULL
, NULL
);
110 BIO_printf(bio_err
, "unable to load PKCS7 object\n");
111 ERR_print_errors(bio_err
);
115 out
= bio_open_default(outfile
, 'w', outformat
);
120 PKCS7_print_ctx(out
, p7
, 0, NULL
);
123 STACK_OF(X509
) *certs
= NULL
;
124 STACK_OF(X509_CRL
) *crls
= NULL
;
126 i
= OBJ_obj2nid(p7
->type
);
128 case NID_pkcs7_signed
:
129 if (p7
->d
.sign
!= NULL
) {
130 certs
= p7
->d
.sign
->cert
;
131 crls
= p7
->d
.sign
->crl
;
134 case NID_pkcs7_signedAndEnveloped
:
135 if (p7
->d
.signed_and_enveloped
!= NULL
) {
136 certs
= p7
->d
.signed_and_enveloped
->cert
;
137 crls
= p7
->d
.signed_and_enveloped
->crl
;
147 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
148 x
= sk_X509_value(certs
, i
);
152 dump_cert_text(out
, x
);
155 PEM_write_bio_X509(out
, x
);
162 for (i
= 0; i
< sk_X509_CRL_num(crls
); i
++) {
163 crl
= sk_X509_CRL_value(crls
, i
);
165 X509_CRL_print(out
, crl
);
168 PEM_write_bio_X509_CRL(out
, crl
);
178 if (outformat
== FORMAT_ASN1
)
179 i
= i2d_PKCS7_bio(out
, p7
);
181 i
= PEM_write_bio_PKCS7(out
, p7
);
184 BIO_printf(bio_err
, "unable to write pkcs7 object\n");
185 ERR_print_errors(bio_err
);