1*440d1414Stb /* $OpenBSD: pkcs7.c,v 1.15 2023/07/23 11:39:29 tb Exp $ */
2dab3f910Sjsing /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3dab3f910Sjsing * All rights reserved.
4dab3f910Sjsing *
5dab3f910Sjsing * This package is an SSL implementation written
6dab3f910Sjsing * by Eric Young (eay@cryptsoft.com).
7dab3f910Sjsing * The implementation was written so as to conform with Netscapes SSL.
8dab3f910Sjsing *
9dab3f910Sjsing * This library is free for commercial and non-commercial use as long as
10dab3f910Sjsing * the following conditions are aheared to. The following conditions
11dab3f910Sjsing * apply to all code found in this distribution, be it the RC4, RSA,
12dab3f910Sjsing * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13dab3f910Sjsing * included with this distribution is covered by the same copyright terms
14dab3f910Sjsing * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15dab3f910Sjsing *
16dab3f910Sjsing * Copyright remains Eric Young's, and as such any Copyright notices in
17dab3f910Sjsing * the code are not to be removed.
18dab3f910Sjsing * If this package is used in a product, Eric Young should be given attribution
19dab3f910Sjsing * as the author of the parts of the library used.
20dab3f910Sjsing * This can be in the form of a textual message at program startup or
21dab3f910Sjsing * in documentation (online or textual) provided with the package.
22dab3f910Sjsing *
23dab3f910Sjsing * Redistribution and use in source and binary forms, with or without
24dab3f910Sjsing * modification, are permitted provided that the following conditions
25dab3f910Sjsing * are met:
26dab3f910Sjsing * 1. Redistributions of source code must retain the copyright
27dab3f910Sjsing * notice, this list of conditions and the following disclaimer.
28dab3f910Sjsing * 2. Redistributions in binary form must reproduce the above copyright
29dab3f910Sjsing * notice, this list of conditions and the following disclaimer in the
30dab3f910Sjsing * documentation and/or other materials provided with the distribution.
31dab3f910Sjsing * 3. All advertising materials mentioning features or use of this software
32dab3f910Sjsing * must display the following acknowledgement:
33dab3f910Sjsing * "This product includes cryptographic software written by
34dab3f910Sjsing * Eric Young (eay@cryptsoft.com)"
35dab3f910Sjsing * The word 'cryptographic' can be left out if the rouines from the library
36dab3f910Sjsing * being used are not cryptographic related :-).
37dab3f910Sjsing * 4. If you include any Windows specific code (or a derivative thereof) from
38dab3f910Sjsing * the apps directory (application code) you must include an acknowledgement:
39dab3f910Sjsing * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40dab3f910Sjsing *
41dab3f910Sjsing * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42dab3f910Sjsing * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43dab3f910Sjsing * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44dab3f910Sjsing * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45dab3f910Sjsing * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46dab3f910Sjsing * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47dab3f910Sjsing * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48dab3f910Sjsing * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49dab3f910Sjsing * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50dab3f910Sjsing * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51dab3f910Sjsing * SUCH DAMAGE.
52dab3f910Sjsing *
53dab3f910Sjsing * The licence and distribution terms for any publically available version or
54dab3f910Sjsing * derivative of this code cannot be changed. i.e. this code cannot simply be
55dab3f910Sjsing * copied and put under another distribution licence
56dab3f910Sjsing * [including the GNU Public Licence.]
57dab3f910Sjsing */
58dab3f910Sjsing
59dab3f910Sjsing #include <stdio.h>
60dab3f910Sjsing #include <stdlib.h>
61dab3f910Sjsing #include <string.h>
62dab3f910Sjsing #include <time.h>
63dab3f910Sjsing
64dab3f910Sjsing #include "apps.h"
65dab3f910Sjsing
66dab3f910Sjsing #include <openssl/err.h>
67dab3f910Sjsing #include <openssl/evp.h>
68dab3f910Sjsing #include <openssl/objects.h>
69dab3f910Sjsing #include <openssl/pem.h>
70dab3f910Sjsing #include <openssl/pkcs7.h>
71dab3f910Sjsing #include <openssl/x509.h>
72dab3f910Sjsing
7374906dc0Sjsing static struct {
7474906dc0Sjsing char *infile;
7574906dc0Sjsing int informat;
7674906dc0Sjsing int noout;
7774906dc0Sjsing char *outfile;
7874906dc0Sjsing int outformat;
7974906dc0Sjsing int p7_print;
8074906dc0Sjsing int print_certs;
8174906dc0Sjsing int text;
82e7718adaStb } cfg;
8374906dc0Sjsing
84ea149709Sguenther static const struct option pkcs7_options[] = {
8574906dc0Sjsing {
8674906dc0Sjsing .name = "in",
8774906dc0Sjsing .argname = "file",
8874906dc0Sjsing .desc = "Input file (default stdin)",
8974906dc0Sjsing .type = OPTION_ARG,
90e7718adaStb .opt.arg = &cfg.infile,
9174906dc0Sjsing },
9274906dc0Sjsing {
9374906dc0Sjsing .name = "inform",
9474906dc0Sjsing .argname = "format",
9574906dc0Sjsing .desc = "Input format (DER or PEM (default))",
9674906dc0Sjsing .type = OPTION_ARG_FORMAT,
97e7718adaStb .opt.value = &cfg.informat,
9874906dc0Sjsing },
9974906dc0Sjsing {
10074906dc0Sjsing .name = "noout",
10174906dc0Sjsing .desc = "Do not output encoded version of PKCS#7 structure",
10274906dc0Sjsing .type = OPTION_FLAG,
103e7718adaStb .opt.flag = &cfg.noout,
10474906dc0Sjsing },
10574906dc0Sjsing {
10674906dc0Sjsing .name = "out",
10774906dc0Sjsing .argname = "file",
10874906dc0Sjsing .desc = "Output file (default stdout)",
10974906dc0Sjsing .type = OPTION_ARG,
110e7718adaStb .opt.arg = &cfg.outfile,
11174906dc0Sjsing },
11274906dc0Sjsing {
11374906dc0Sjsing .name = "outform",
11474906dc0Sjsing .argname = "format",
11574906dc0Sjsing .desc = "Output format (DER or PEM (default))",
11674906dc0Sjsing .type = OPTION_ARG_FORMAT,
117e7718adaStb .opt.value = &cfg.outformat,
11874906dc0Sjsing },
11974906dc0Sjsing {
12074906dc0Sjsing .name = "print",
12174906dc0Sjsing .desc = "Output ASN.1 representation of PKCS#7 structure",
12274906dc0Sjsing .type = OPTION_FLAG,
123e7718adaStb .opt.flag = &cfg.p7_print,
12474906dc0Sjsing },
12574906dc0Sjsing {
12674906dc0Sjsing .name = "print_certs",
12774906dc0Sjsing .desc = "Print out any certificates or CRLs contained in file",
12874906dc0Sjsing .type = OPTION_FLAG,
129e7718adaStb .opt.flag = &cfg.print_certs,
13074906dc0Sjsing },
13174906dc0Sjsing {
13274906dc0Sjsing .name = "text",
13374906dc0Sjsing .desc = "Print out full certificate details",
13474906dc0Sjsing .type = OPTION_FLAG,
135e7718adaStb .opt.flag = &cfg.text,
13674906dc0Sjsing },
13774906dc0Sjsing { NULL },
13874906dc0Sjsing };
13974906dc0Sjsing
14074906dc0Sjsing static void
pkcs7_usage(void)141*440d1414Stb pkcs7_usage(void)
14274906dc0Sjsing {
1435284dfeaSbcook fprintf(stderr, "usage: pkcs7 [-in file] "
14474906dc0Sjsing "[-inform DER | PEM] [-noout]\n"
14574906dc0Sjsing " [-out file] [-outform DER | PEM] [-print_certs] [-text]\n\n");
14674906dc0Sjsing options_usage(pkcs7_options);
14774906dc0Sjsing }
148dab3f910Sjsing
149dab3f910Sjsing int
pkcs7_main(int argc,char ** argv)150dab3f910Sjsing pkcs7_main(int argc, char **argv)
151dab3f910Sjsing {
152dab3f910Sjsing PKCS7 *p7 = NULL;
153dab3f910Sjsing BIO *in = NULL, *out = NULL;
154dab3f910Sjsing int ret = 1;
15574906dc0Sjsing int i;
156dab3f910Sjsing
15751811eadSderaadt if (pledge("stdio cpath wpath rpath", NULL) == -1) {
1589bc487adSdoug perror("pledge");
159e370f0eeSdoug exit(1);
160e370f0eeSdoug }
1619bc487adSdoug
162e7718adaStb memset(&cfg, 0, sizeof(cfg));
163dab3f910Sjsing
164e7718adaStb cfg.informat = FORMAT_PEM;
165e7718adaStb cfg.outformat = FORMAT_PEM;
166dab3f910Sjsing
16774906dc0Sjsing if (options_parse(argc, argv, pkcs7_options, NULL, NULL) != 0) {
16874906dc0Sjsing pkcs7_usage();
169dab3f910Sjsing goto end;
170dab3f910Sjsing }
171dab3f910Sjsing
172dab3f910Sjsing in = BIO_new(BIO_s_file());
173dab3f910Sjsing out = BIO_new(BIO_s_file());
174dab3f910Sjsing if ((in == NULL) || (out == NULL)) {
175dab3f910Sjsing ERR_print_errors(bio_err);
176dab3f910Sjsing goto end;
177dab3f910Sjsing }
178e7718adaStb if (cfg.infile == NULL)
179dab3f910Sjsing BIO_set_fp(in, stdin, BIO_NOCLOSE);
180dab3f910Sjsing else {
181e7718adaStb if (BIO_read_filename(in, cfg.infile) <= 0) {
182e7718adaStb perror(cfg.infile);
183dab3f910Sjsing goto end;
184dab3f910Sjsing }
185dab3f910Sjsing }
186dab3f910Sjsing
187e7718adaStb if (cfg.informat == FORMAT_ASN1)
188dab3f910Sjsing p7 = d2i_PKCS7_bio(in, NULL);
189e7718adaStb else if (cfg.informat == FORMAT_PEM)
190dab3f910Sjsing p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
191dab3f910Sjsing else {
192dab3f910Sjsing BIO_printf(bio_err, "bad input format specified for pkcs7 object\n");
193dab3f910Sjsing goto end;
194dab3f910Sjsing }
195dab3f910Sjsing if (p7 == NULL) {
196dab3f910Sjsing BIO_printf(bio_err, "unable to load PKCS7 object\n");
197dab3f910Sjsing ERR_print_errors(bio_err);
198dab3f910Sjsing goto end;
199dab3f910Sjsing }
200e7718adaStb if (cfg.outfile == NULL) {
201dab3f910Sjsing BIO_set_fp(out, stdout, BIO_NOCLOSE);
202dab3f910Sjsing } else {
203e7718adaStb if (BIO_write_filename(out, cfg.outfile) <= 0) {
204e7718adaStb perror(cfg.outfile);
205dab3f910Sjsing goto end;
206dab3f910Sjsing }
207dab3f910Sjsing }
208dab3f910Sjsing
209e7718adaStb if (cfg.p7_print)
210dab3f910Sjsing PKCS7_print_ctx(out, p7, 0, NULL);
211dab3f910Sjsing
212e7718adaStb if (cfg.print_certs) {
213dab3f910Sjsing STACK_OF(X509) * certs = NULL;
214dab3f910Sjsing STACK_OF(X509_CRL) * crls = NULL;
215dab3f910Sjsing
216dab3f910Sjsing i = OBJ_obj2nid(p7->type);
217dab3f910Sjsing switch (i) {
218dab3f910Sjsing case NID_pkcs7_signed:
219ce9c3de3Stb if (p7->d.sign != NULL) {
220dab3f910Sjsing certs = p7->d.sign->cert;
221dab3f910Sjsing crls = p7->d.sign->crl;
222ce9c3de3Stb }
223dab3f910Sjsing break;
224dab3f910Sjsing case NID_pkcs7_signedAndEnveloped:
225ce9c3de3Stb if (p7->d.signed_and_enveloped != NULL) {
226dab3f910Sjsing certs = p7->d.signed_and_enveloped->cert;
227dab3f910Sjsing crls = p7->d.signed_and_enveloped->crl;
228ce9c3de3Stb }
229dab3f910Sjsing break;
230dab3f910Sjsing default:
231dab3f910Sjsing break;
232dab3f910Sjsing }
233dab3f910Sjsing
234dab3f910Sjsing if (certs != NULL) {
235dab3f910Sjsing X509 *x;
236dab3f910Sjsing
237dab3f910Sjsing for (i = 0; i < sk_X509_num(certs); i++) {
238dab3f910Sjsing x = sk_X509_value(certs, i);
239e7718adaStb if (cfg.text)
240dab3f910Sjsing X509_print(out, x);
241dab3f910Sjsing else
242dab3f910Sjsing dump_cert_text(out, x);
243dab3f910Sjsing
244e7718adaStb if (!cfg.noout)
245dab3f910Sjsing PEM_write_bio_X509(out, x);
246dab3f910Sjsing BIO_puts(out, "\n");
247dab3f910Sjsing }
248dab3f910Sjsing }
249dab3f910Sjsing if (crls != NULL) {
250dab3f910Sjsing X509_CRL *crl;
251dab3f910Sjsing
252dab3f910Sjsing for (i = 0; i < sk_X509_CRL_num(crls); i++) {
253dab3f910Sjsing crl = sk_X509_CRL_value(crls, i);
254dab3f910Sjsing
255dab3f910Sjsing X509_CRL_print(out, crl);
256dab3f910Sjsing
257e7718adaStb if (!cfg.noout)
258dab3f910Sjsing PEM_write_bio_X509_CRL(out, crl);
259dab3f910Sjsing BIO_puts(out, "\n");
260dab3f910Sjsing }
261dab3f910Sjsing }
262dab3f910Sjsing ret = 0;
263dab3f910Sjsing goto end;
264dab3f910Sjsing }
265e7718adaStb if (!cfg.noout) {
266e7718adaStb if (cfg.outformat == FORMAT_ASN1)
267dab3f910Sjsing i = i2d_PKCS7_bio(out, p7);
268e7718adaStb else if (cfg.outformat == FORMAT_PEM)
269dab3f910Sjsing i = PEM_write_bio_PKCS7(out, p7);
270dab3f910Sjsing else {
271dab3f910Sjsing BIO_printf(bio_err, "bad output format specified for outfile\n");
272dab3f910Sjsing goto end;
273dab3f910Sjsing }
274dab3f910Sjsing
275dab3f910Sjsing if (!i) {
276dab3f910Sjsing BIO_printf(bio_err, "unable to write pkcs7 object\n");
277dab3f910Sjsing ERR_print_errors(bio_err);
278dab3f910Sjsing goto end;
279dab3f910Sjsing }
280dab3f910Sjsing }
281dab3f910Sjsing ret = 0;
282dab3f910Sjsing end:
283dab3f910Sjsing if (p7 != NULL)
284dab3f910Sjsing PKCS7_free(p7);
285dab3f910Sjsing if (in != NULL)
286dab3f910Sjsing BIO_free(in);
287dab3f910Sjsing if (out != NULL)
288dab3f910Sjsing BIO_free_all(out);
289dab3f910Sjsing
290dab3f910Sjsing return (ret);
291dab3f910Sjsing }
292