pkcs7.c revision 109998
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.
855714Skris *
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).
1555714Skris *
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.
2255714Skris *
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 :-).
3755714Skris * 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)"
4055714Skris *
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.
5255714Skris *
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
7255714Skris#define PROG	pkcs7_main
7355714Skris
7459191Skris/* -inform arg	- input format - default PEM (DER or PEM)
7555714Skris * -outform arg - output format - default PEM
7655714Skris * -in arg	- input file - default stdin
7755714Skris * -out arg	- output file - default stdout
7855714Skris * -print_certs
7955714Skris */
8055714Skris
8159191Skrisint MAIN(int, char **);
8259191Skris
8355714Skrisint MAIN(int argc, char **argv)
8455714Skris	{
85109998Smarkm	ENGINE *e = NULL;
8655714Skris	PKCS7 *p7=NULL;
8755714Skris	int i,badops=0;
8855714Skris	BIO *in=NULL,*out=NULL;
8955714Skris	int informat,outformat;
9059191Skris	char *infile,*outfile,*prog;
9159191Skris	int print_certs=0,text=0,noout=0;
92100936Snectar	int ret=1;
93109998Smarkm	char *engine=NULL;
9455714Skris
9555714Skris	apps_startup();
9655714Skris
9755714Skris	if (bio_err == NULL)
9855714Skris		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
9955714Skris			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
10055714Skris
10155714Skris	infile=NULL;
10255714Skris	outfile=NULL;
10355714Skris	informat=FORMAT_PEM;
10455714Skris	outformat=FORMAT_PEM;
10555714Skris
10655714Skris	prog=argv[0];
10755714Skris	argc--;
10855714Skris	argv++;
10955714Skris	while (argc >= 1)
11055714Skris		{
11155714Skris		if 	(strcmp(*argv,"-inform") == 0)
11255714Skris			{
11355714Skris			if (--argc < 1) goto bad;
11455714Skris			informat=str2fmt(*(++argv));
11555714Skris			}
11655714Skris		else if (strcmp(*argv,"-outform") == 0)
11755714Skris			{
11855714Skris			if (--argc < 1) goto bad;
11955714Skris			outformat=str2fmt(*(++argv));
12055714Skris			}
12155714Skris		else if (strcmp(*argv,"-in") == 0)
12255714Skris			{
12355714Skris			if (--argc < 1) goto bad;
12455714Skris			infile= *(++argv);
12555714Skris			}
12655714Skris		else if (strcmp(*argv,"-out") == 0)
12755714Skris			{
12855714Skris			if (--argc < 1) goto bad;
12955714Skris			outfile= *(++argv);
13055714Skris			}
13159191Skris		else if (strcmp(*argv,"-noout") == 0)
13259191Skris			noout=1;
13359191Skris		else if (strcmp(*argv,"-text") == 0)
13459191Skris			text=1;
13555714Skris		else if (strcmp(*argv,"-print_certs") == 0)
13655714Skris			print_certs=1;
137109998Smarkm		else if (strcmp(*argv,"-engine") == 0)
138109998Smarkm			{
139109998Smarkm			if (--argc < 1) goto bad;
140109998Smarkm			engine= *(++argv);
141109998Smarkm			}
14255714Skris		else
14355714Skris			{
14455714Skris			BIO_printf(bio_err,"unknown option %s\n",*argv);
14555714Skris			badops=1;
14655714Skris			break;
14755714Skris			}
14855714Skris		argc--;
14955714Skris		argv++;
15055714Skris		}
15155714Skris
15255714Skris	if (badops)
15355714Skris		{
15455714Skrisbad:
15555714Skris		BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
15655714Skris		BIO_printf(bio_err,"where options are\n");
15759191Skris		BIO_printf(bio_err," -inform arg   input format - DER or PEM\n");
15859191Skris		BIO_printf(bio_err," -outform arg  output format - DER or PEM\n");
15955714Skris		BIO_printf(bio_err," -in arg       input file\n");
16055714Skris		BIO_printf(bio_err," -out arg      output file\n");
16155714Skris		BIO_printf(bio_err," -print_certs  print any certs or crl in the input\n");
16259191Skris		BIO_printf(bio_err," -text         print full details of certificates\n");
16359191Skris		BIO_printf(bio_err," -noout        don't output encoded data\n");
164109998Smarkm		BIO_printf(bio_err," -engine e     use engine e, possibly a hardware device.\n");
165109998Smarkm		ret = 1;
166109998Smarkm		goto end;
16755714Skris		}
16855714Skris
16955714Skris	ERR_load_crypto_strings();
17055714Skris
171109998Smarkm        e = setup_engine(bio_err, engine, 0);
172109998Smarkm
17355714Skris	in=BIO_new(BIO_s_file());
17455714Skris	out=BIO_new(BIO_s_file());
17555714Skris	if ((in == NULL) || (out == NULL))
17655714Skris		{
17755714Skris		ERR_print_errors(bio_err);
17855714Skris                goto end;
17955714Skris                }
18055714Skris
18155714Skris	if (infile == NULL)
18255714Skris		BIO_set_fp(in,stdin,BIO_NOCLOSE);
18355714Skris	else
18455714Skris		{
18555714Skris		if (BIO_read_filename(in,infile) <= 0)
18655714Skris		if (in == NULL)
18755714Skris			{
18855714Skris			perror(infile);
18955714Skris			goto end;
19055714Skris			}
19155714Skris		}
19255714Skris
19355714Skris	if	(informat == FORMAT_ASN1)
19455714Skris		p7=d2i_PKCS7_bio(in,NULL);
19555714Skris	else if (informat == FORMAT_PEM)
19655714Skris		p7=PEM_read_bio_PKCS7(in,NULL,NULL,NULL);
19755714Skris	else
19855714Skris		{
19955714Skris		BIO_printf(bio_err,"bad input format specified for pkcs7 object\n");
20055714Skris		goto end;
20155714Skris		}
20255714Skris	if (p7 == NULL)
20355714Skris		{
20455714Skris		BIO_printf(bio_err,"unable to load PKCS7 object\n");
20555714Skris		ERR_print_errors(bio_err);
20655714Skris		goto end;
20755714Skris		}
20855714Skris
20955714Skris	if (outfile == NULL)
21068651Skris		{
21155714Skris		BIO_set_fp(out,stdout,BIO_NOCLOSE);
212109998Smarkm#ifdef OPENSSL_SYS_VMS
21368651Skris		{
21468651Skris		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
21568651Skris		out = BIO_push(tmpbio, out);
21668651Skris		}
21768651Skris#endif
21868651Skris		}
21955714Skris	else
22055714Skris		{
22155714Skris		if (BIO_write_filename(out,outfile) <= 0)
22255714Skris			{
22355714Skris			perror(outfile);
22455714Skris			goto end;
22555714Skris			}
22655714Skris		}
22755714Skris
22855714Skris	if (print_certs)
22955714Skris		{
23055714Skris		STACK_OF(X509) *certs=NULL;
23155714Skris		STACK_OF(X509_CRL) *crls=NULL;
23255714Skris
23355714Skris		i=OBJ_obj2nid(p7->type);
23455714Skris		switch (i)
23555714Skris			{
23655714Skris		case NID_pkcs7_signed:
23755714Skris			certs=p7->d.sign->cert;
23855714Skris			crls=p7->d.sign->crl;
23955714Skris			break;
24055714Skris		case NID_pkcs7_signedAndEnveloped:
24155714Skris			certs=p7->d.signed_and_enveloped->cert;
24255714Skris			crls=p7->d.signed_and_enveloped->crl;
24355714Skris			break;
24455714Skris		default:
24555714Skris			break;
24655714Skris			}
24755714Skris
24855714Skris		if (certs != NULL)
24955714Skris			{
25055714Skris			X509 *x;
25155714Skris
25255714Skris			for (i=0; i<sk_X509_num(certs); i++)
25355714Skris				{
25455714Skris				x=sk_X509_value(certs,i);
25559191Skris				if(text) X509_print(out, x);
25659191Skris				else dump_cert_text(out, x);
25755714Skris
25859191Skris				if(!noout) PEM_write_bio_X509(out,x);
25955714Skris				BIO_puts(out,"\n");
26055714Skris				}
26155714Skris			}
26255714Skris		if (crls != NULL)
26355714Skris			{
26455714Skris			X509_CRL *crl;
26555714Skris
26655714Skris			for (i=0; i<sk_X509_CRL_num(crls); i++)
26755714Skris				{
26855714Skris				crl=sk_X509_CRL_value(crls,i);
26955714Skris
27059191Skris				X509_CRL_print(out, crl);
27155714Skris
27259191Skris				if(!noout)PEM_write_bio_X509_CRL(out,crl);
27355714Skris				BIO_puts(out,"\n");
27455714Skris				}
27555714Skris			}
27655714Skris
27755714Skris		ret=0;
27855714Skris		goto end;
27955714Skris		}
28055714Skris
28159191Skris	if(!noout) {
28259191Skris		if 	(outformat == FORMAT_ASN1)
28359191Skris			i=i2d_PKCS7_bio(out,p7);
28459191Skris		else if (outformat == FORMAT_PEM)
28559191Skris			i=PEM_write_bio_PKCS7(out,p7);
28659191Skris		else	{
28759191Skris			BIO_printf(bio_err,"bad output format specified for outfile\n");
28859191Skris			goto end;
28959191Skris			}
29055714Skris
29159191Skris		if (!i)
29259191Skris			{
29359191Skris			BIO_printf(bio_err,"unable to write pkcs7 object\n");
29459191Skris			ERR_print_errors(bio_err);
29559191Skris			goto end;
29659191Skris			}
29759191Skris	}
29855714Skris	ret=0;
29955714Skrisend:
30055714Skris	if (p7 != NULL) PKCS7_free(p7);
30155714Skris	if (in != NULL) BIO_free(in);
30268651Skris	if (out != NULL) BIO_free_all(out);
303109998Smarkm	apps_shutdown();
304109998Smarkm	OPENSSL_EXIT(ret);
30555714Skris	}
306