155714Skris/* apps/pkcs7.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280304Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280304Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280304Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280304Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280304Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include <stdlib.h>
6155714Skris#include <string.h>
6255714Skris#include <time.h>
6355714Skris#include "apps.h"
6455714Skris#include <openssl/err.h>
6555714Skris#include <openssl/objects.h>
6655714Skris#include <openssl/evp.h>
6755714Skris#include <openssl/x509.h>
6855714Skris#include <openssl/pkcs7.h>
6955714Skris#include <openssl/pem.h>
7055714Skris
7155714Skris#undef PROG
72280304Sjkim#define PROG    pkcs7_main
7355714Skris
74280304Sjkim/*-
75280304Sjkim * -inform arg  - input format - default PEM (DER or PEM)
7655714Skris * -outform arg - output format - default PEM
77280304Sjkim * -in arg      - input file - default stdin
78280304Sjkim * -out arg     - output file - default stdout
7955714Skris * -print_certs
8055714Skris */
8155714Skris
8259191Skrisint MAIN(int, char **);
8359191Skris
8455714Skrisint MAIN(int argc, char **argv)
85280304Sjkim{
86280304Sjkim    PKCS7 *p7 = NULL;
87280304Sjkim    int i, badops = 0;
88280304Sjkim    BIO *in = NULL, *out = NULL;
89280304Sjkim    int informat, outformat;
90280304Sjkim    char *infile, *outfile, *prog;
91280304Sjkim    int print_certs = 0, text = 0, noout = 0, p7_print = 0;
92280304Sjkim    int ret = 1;
93111147Snectar#ifndef OPENSSL_NO_ENGINE
94280304Sjkim    char *engine = NULL;
95111147Snectar#endif
9655714Skris
97280304Sjkim    apps_startup();
9855714Skris
99280304Sjkim    if (bio_err == NULL)
100280304Sjkim        if ((bio_err = BIO_new(BIO_s_file())) != NULL)
101280304Sjkim            BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
10255714Skris
103280304Sjkim    if (!load_config(bio_err, NULL))
104280304Sjkim        goto end;
105127128Snectar
106280304Sjkim    infile = NULL;
107280304Sjkim    outfile = NULL;
108280304Sjkim    informat = FORMAT_PEM;
109280304Sjkim    outformat = FORMAT_PEM;
11055714Skris
111280304Sjkim    prog = argv[0];
112280304Sjkim    argc--;
113280304Sjkim    argv++;
114280304Sjkim    while (argc >= 1) {
115280304Sjkim        if (strcmp(*argv, "-inform") == 0) {
116280304Sjkim            if (--argc < 1)
117280304Sjkim                goto bad;
118280304Sjkim            informat = str2fmt(*(++argv));
119280304Sjkim        } else if (strcmp(*argv, "-outform") == 0) {
120280304Sjkim            if (--argc < 1)
121280304Sjkim                goto bad;
122280304Sjkim            outformat = str2fmt(*(++argv));
123280304Sjkim        } else if (strcmp(*argv, "-in") == 0) {
124280304Sjkim            if (--argc < 1)
125280304Sjkim                goto bad;
126280304Sjkim            infile = *(++argv);
127280304Sjkim        } else if (strcmp(*argv, "-out") == 0) {
128280304Sjkim            if (--argc < 1)
129280304Sjkim                goto bad;
130280304Sjkim            outfile = *(++argv);
131280304Sjkim        } else if (strcmp(*argv, "-noout") == 0)
132280304Sjkim            noout = 1;
133280304Sjkim        else if (strcmp(*argv, "-text") == 0)
134280304Sjkim            text = 1;
135280304Sjkim        else if (strcmp(*argv, "-print") == 0)
136280304Sjkim            p7_print = 1;
137280304Sjkim        else if (strcmp(*argv, "-print_certs") == 0)
138280304Sjkim            print_certs = 1;
139111147Snectar#ifndef OPENSSL_NO_ENGINE
140280304Sjkim        else if (strcmp(*argv, "-engine") == 0) {
141280304Sjkim            if (--argc < 1)
142280304Sjkim                goto bad;
143280304Sjkim            engine = *(++argv);
144280304Sjkim        }
145111147Snectar#endif
146280304Sjkim        else {
147280304Sjkim            BIO_printf(bio_err, "unknown option %s\n", *argv);
148280304Sjkim            badops = 1;
149280304Sjkim            break;
150280304Sjkim        }
151280304Sjkim        argc--;
152280304Sjkim        argv++;
153280304Sjkim    }
15455714Skris
155280304Sjkim    if (badops) {
156280304Sjkim bad:
157280304Sjkim        BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
158280304Sjkim        BIO_printf(bio_err, "where options are\n");
159280304Sjkim        BIO_printf(bio_err, " -inform arg   input format - DER or PEM\n");
160280304Sjkim        BIO_printf(bio_err, " -outform arg  output format - DER or PEM\n");
161280304Sjkim        BIO_printf(bio_err, " -in arg       input file\n");
162280304Sjkim        BIO_printf(bio_err, " -out arg      output file\n");
163280304Sjkim        BIO_printf(bio_err,
164280304Sjkim                   " -print_certs  print any certs or crl in the input\n");
165280304Sjkim        BIO_printf(bio_err,
166280304Sjkim                   " -text         print full details of certificates\n");
167280304Sjkim        BIO_printf(bio_err, " -noout        don't output encoded data\n");
168111147Snectar#ifndef OPENSSL_NO_ENGINE
169280304Sjkim        BIO_printf(bio_err,
170280304Sjkim                   " -engine e     use engine e, possibly a hardware device.\n");
171111147Snectar#endif
172280304Sjkim        ret = 1;
173280304Sjkim        goto end;
174280304Sjkim    }
17555714Skris
176280304Sjkim    ERR_load_crypto_strings();
17755714Skris
178111147Snectar#ifndef OPENSSL_NO_ENGINE
179280304Sjkim    setup_engine(bio_err, engine, 0);
180111147Snectar#endif
181109998Smarkm
182280304Sjkim    in = BIO_new(BIO_s_file());
183280304Sjkim    out = BIO_new(BIO_s_file());
184280304Sjkim    if ((in == NULL) || (out == NULL)) {
185280304Sjkim        ERR_print_errors(bio_err);
186280304Sjkim        goto end;
187280304Sjkim    }
18855714Skris
189280304Sjkim    if (infile == NULL)
190280304Sjkim        BIO_set_fp(in, stdin, BIO_NOCLOSE);
191280304Sjkim    else {
192280304Sjkim        if (BIO_read_filename(in, infile) <= 0) {
193280304Sjkim            BIO_printf(bio_err, "unable to load input file\n");
194280304Sjkim            ERR_print_errors(bio_err);
195280304Sjkim            goto end;
196280304Sjkim        }
197280304Sjkim    }
19855714Skris
199280304Sjkim    if (informat == FORMAT_ASN1)
200280304Sjkim        p7 = d2i_PKCS7_bio(in, NULL);
201280304Sjkim    else if (informat == FORMAT_PEM)
202280304Sjkim        p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
203280304Sjkim    else {
204280304Sjkim        BIO_printf(bio_err, "bad input format specified for pkcs7 object\n");
205280304Sjkim        goto end;
206280304Sjkim    }
207280304Sjkim    if (p7 == NULL) {
208280304Sjkim        BIO_printf(bio_err, "unable to load PKCS7 object\n");
209280304Sjkim        ERR_print_errors(bio_err);
210280304Sjkim        goto end;
211280304Sjkim    }
21255714Skris
213280304Sjkim    if (outfile == NULL) {
214280304Sjkim        BIO_set_fp(out, stdout, BIO_NOCLOSE);
215109998Smarkm#ifdef OPENSSL_SYS_VMS
216280304Sjkim        {
217280304Sjkim            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
218280304Sjkim            out = BIO_push(tmpbio, out);
219280304Sjkim        }
22068651Skris#endif
221280304Sjkim    } else {
222280304Sjkim        if (BIO_write_filename(out, outfile) <= 0) {
223280304Sjkim            perror(outfile);
224280304Sjkim            goto end;
225280304Sjkim        }
226280304Sjkim    }
22755714Skris
228280304Sjkim    if (p7_print)
229280304Sjkim        PKCS7_print_ctx(out, p7, 0, NULL);
230238405Sjkim
231280304Sjkim    if (print_certs) {
232280304Sjkim        STACK_OF(X509) *certs = NULL;
233280304Sjkim        STACK_OF(X509_CRL) *crls = NULL;
23455714Skris
235280304Sjkim        i = OBJ_obj2nid(p7->type);
236280304Sjkim        switch (i) {
237280304Sjkim        case NID_pkcs7_signed:
238298999Sjkim            if (p7->d.sign != NULL) {
239298999Sjkim                certs = p7->d.sign->cert;
240298999Sjkim                crls = p7->d.sign->crl;
241298999Sjkim            }
242280304Sjkim            break;
243280304Sjkim        case NID_pkcs7_signedAndEnveloped:
244298999Sjkim            if (p7->d.signed_and_enveloped != NULL) {
245298999Sjkim                certs = p7->d.signed_and_enveloped->cert;
246298999Sjkim                crls = p7->d.signed_and_enveloped->crl;
247298999Sjkim            }
248280304Sjkim            break;
249280304Sjkim        default:
250280304Sjkim            break;
251280304Sjkim        }
25255714Skris
253280304Sjkim        if (certs != NULL) {
254280304Sjkim            X509 *x;
25555714Skris
256280304Sjkim            for (i = 0; i < sk_X509_num(certs); i++) {
257280304Sjkim                x = sk_X509_value(certs, i);
258280304Sjkim                if (text)
259280304Sjkim                    X509_print(out, x);
260280304Sjkim                else
261280304Sjkim                    dump_cert_text(out, x);
26255714Skris
263280304Sjkim                if (!noout)
264280304Sjkim                    PEM_write_bio_X509(out, x);
265280304Sjkim                BIO_puts(out, "\n");
266280304Sjkim            }
267280304Sjkim        }
268280304Sjkim        if (crls != NULL) {
269280304Sjkim            X509_CRL *crl;
27055714Skris
271280304Sjkim            for (i = 0; i < sk_X509_CRL_num(crls); i++) {
272280304Sjkim                crl = sk_X509_CRL_value(crls, i);
27355714Skris
274280304Sjkim                X509_CRL_print(out, crl);
27555714Skris
276280304Sjkim                if (!noout)
277280304Sjkim                    PEM_write_bio_X509_CRL(out, crl);
278280304Sjkim                BIO_puts(out, "\n");
279280304Sjkim            }
280280304Sjkim        }
28155714Skris
282280304Sjkim        ret = 0;
283280304Sjkim        goto end;
284280304Sjkim    }
28555714Skris
286280304Sjkim    if (!noout) {
287280304Sjkim        if (outformat == FORMAT_ASN1)
288280304Sjkim            i = i2d_PKCS7_bio(out, p7);
289280304Sjkim        else if (outformat == FORMAT_PEM)
290280304Sjkim            i = PEM_write_bio_PKCS7(out, p7);
291280304Sjkim        else {
292280304Sjkim            BIO_printf(bio_err, "bad output format specified for outfile\n");
293280304Sjkim            goto end;
294280304Sjkim        }
29555714Skris
296280304Sjkim        if (!i) {
297280304Sjkim            BIO_printf(bio_err, "unable to write pkcs7 object\n");
298280304Sjkim            ERR_print_errors(bio_err);
299280304Sjkim            goto end;
300280304Sjkim        }
301280304Sjkim    }
302280304Sjkim    ret = 0;
303280304Sjkim end:
304280304Sjkim    if (p7 != NULL)
305280304Sjkim        PKCS7_free(p7);
306280304Sjkim    if (in != NULL)
307280304Sjkim        BIO_free(in);
308280304Sjkim    if (out != NULL)
309280304Sjkim        BIO_free_all(out);
310280304Sjkim    apps_shutdown();
311280304Sjkim    OPENSSL_EXIT(ret);
312280304Sjkim}
313