155714Skris/* apps/crl2p7.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/* This was written by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu>
6055714Skris * and donated 'to the cause' along with lots and lots of other fixes to
6155714Skris * the library. */
6255714Skris
6355714Skris#include <stdio.h>
6455714Skris#include <string.h>
6555714Skris#include <sys/types.h>
6655714Skris#include "apps.h"
6755714Skris#include <openssl/err.h>
6855714Skris#include <openssl/evp.h>
6955714Skris#include <openssl/x509.h>
7055714Skris#include <openssl/pkcs7.h>
7155714Skris#include <openssl/pem.h>
7255714Skris#include <openssl/objects.h>
7355714Skris
7455714Skrisstatic int add_certs_from_file(STACK_OF(X509) *stack, char *certfile);
7555714Skris#undef PROG
7655714Skris#define PROG	crl2pkcs7_main
7755714Skris
7859191Skris/* -inform arg	- input format - default PEM (DER or PEM)
7955714Skris * -outform arg - output format - default PEM
8055714Skris * -in arg	- input file - default stdin
8155714Skris * -out arg	- output file - default stdout
8255714Skris */
8355714Skris
8459191Skrisint MAIN(int, char **);
8559191Skris
8655714Skrisint MAIN(int argc, char **argv)
8755714Skris	{
8855714Skris	int i,badops=0;
8955714Skris	BIO *in=NULL,*out=NULL;
9055714Skris	int informat,outformat;
9155714Skris	char *infile,*outfile,*prog,*certfile;
9255714Skris	PKCS7 *p7 = NULL;
9355714Skris	PKCS7_SIGNED *p7s = NULL;
9455714Skris	X509_CRL *crl=NULL;
95238405Sjkim	STACK_OF(OPENSSL_STRING) *certflst=NULL;
9655714Skris	STACK_OF(X509_CRL) *crl_stack=NULL;
9755714Skris	STACK_OF(X509) *cert_stack=NULL;
9855714Skris	int ret=1,nocrl=0;
9955714Skris
10055714Skris	apps_startup();
10155714Skris
10255714Skris	if (bio_err == NULL)
10355714Skris		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
10455714Skris			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
10555714Skris
10655714Skris	infile=NULL;
10755714Skris	outfile=NULL;
10855714Skris	informat=FORMAT_PEM;
10955714Skris	outformat=FORMAT_PEM;
11055714Skris
11155714Skris	prog=argv[0];
11255714Skris	argc--;
11355714Skris	argv++;
11455714Skris	while (argc >= 1)
11555714Skris		{
11655714Skris		if 	(strcmp(*argv,"-inform") == 0)
11755714Skris			{
11855714Skris			if (--argc < 1) goto bad;
11955714Skris			informat=str2fmt(*(++argv));
12055714Skris			}
12155714Skris		else if (strcmp(*argv,"-outform") == 0)
12255714Skris			{
12355714Skris			if (--argc < 1) goto bad;
12455714Skris			outformat=str2fmt(*(++argv));
12555714Skris			}
12655714Skris		else if (strcmp(*argv,"-in") == 0)
12755714Skris			{
12855714Skris			if (--argc < 1) goto bad;
12955714Skris			infile= *(++argv);
13055714Skris			}
13155714Skris		else if (strcmp(*argv,"-nocrl") == 0)
13255714Skris			{
13355714Skris			nocrl=1;
13455714Skris			}
13555714Skris		else if (strcmp(*argv,"-out") == 0)
13655714Skris			{
13755714Skris			if (--argc < 1) goto bad;
13855714Skris			outfile= *(++argv);
13955714Skris			}
14055714Skris		else if (strcmp(*argv,"-certfile") == 0)
14155714Skris			{
14255714Skris			if (--argc < 1) goto bad;
143238405Sjkim			if(!certflst) certflst = sk_OPENSSL_STRING_new_null();
144279264Sdelphij			if (!certflst)
145279264Sdelphij				goto end;
146279264Sdelphij			if (!sk_OPENSSL_STRING_push(certflst,*(++argv)))
147279264Sdelphij				{
148279264Sdelphij				sk_OPENSSL_STRING_free(certflst);
149279264Sdelphij				goto end;
150279264Sdelphij				}
15155714Skris			}
15255714Skris		else
15355714Skris			{
15455714Skris			BIO_printf(bio_err,"unknown option %s\n",*argv);
15555714Skris			badops=1;
15655714Skris			break;
15755714Skris			}
15855714Skris		argc--;
15955714Skris		argv++;
16055714Skris		}
16155714Skris
16255714Skris	if (badops)
16355714Skris		{
16455714Skrisbad:
16555714Skris		BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
16655714Skris		BIO_printf(bio_err,"where options are\n");
16759191Skris		BIO_printf(bio_err," -inform arg    input format - DER or PEM\n");
16859191Skris		BIO_printf(bio_err," -outform arg   output format - DER or PEM\n");
16955714Skris		BIO_printf(bio_err," -in arg        input file\n");
17055714Skris		BIO_printf(bio_err," -out arg       output file\n");
17155714Skris		BIO_printf(bio_err," -certfile arg  certificates file of chain to a trusted CA\n");
17255714Skris		BIO_printf(bio_err,"                (can be used more than once)\n");
17355714Skris		BIO_printf(bio_err," -nocrl         no crl to load, just certs from '-certfile'\n");
174109998Smarkm		ret = 1;
175109998Smarkm		goto end;
17655714Skris		}
17755714Skris
17855714Skris	ERR_load_crypto_strings();
17955714Skris
18055714Skris	in=BIO_new(BIO_s_file());
18155714Skris	out=BIO_new(BIO_s_file());
18255714Skris	if ((in == NULL) || (out == NULL))
18355714Skris		{
18455714Skris		ERR_print_errors(bio_err);
18555714Skris		goto end;
18655714Skris		}
18755714Skris
18855714Skris	if (!nocrl)
18955714Skris		{
19055714Skris		if (infile == NULL)
19155714Skris			BIO_set_fp(in,stdin,BIO_NOCLOSE);
19255714Skris		else
19355714Skris			{
19455714Skris			if (BIO_read_filename(in,infile) <= 0)
19555714Skris				{
19655714Skris				perror(infile);
19755714Skris				goto end;
19855714Skris				}
19955714Skris			}
20055714Skris
20155714Skris		if 	(informat == FORMAT_ASN1)
20255714Skris			crl=d2i_X509_CRL_bio(in,NULL);
20355714Skris		else if (informat == FORMAT_PEM)
20455714Skris			crl=PEM_read_bio_X509_CRL(in,NULL,NULL,NULL);
20555714Skris		else	{
20655714Skris			BIO_printf(bio_err,"bad input format specified for input crl\n");
20755714Skris			goto end;
20855714Skris			}
20955714Skris		if (crl == NULL)
21055714Skris			{
21155714Skris			BIO_printf(bio_err,"unable to load CRL\n");
21255714Skris			ERR_print_errors(bio_err);
21355714Skris			goto end;
21455714Skris			}
21555714Skris		}
21655714Skris
21755714Skris	if ((p7=PKCS7_new()) == NULL) goto end;
21855714Skris	if ((p7s=PKCS7_SIGNED_new()) == NULL) goto end;
21955714Skris	p7->type=OBJ_nid2obj(NID_pkcs7_signed);
22055714Skris	p7->d.sign=p7s;
22155714Skris	p7s->contents->type=OBJ_nid2obj(NID_pkcs7_data);
22255714Skris
22355714Skris	if (!ASN1_INTEGER_set(p7s->version,1)) goto end;
22468651Skris	if ((crl_stack=sk_X509_CRL_new_null()) == NULL) goto end;
22555714Skris	p7s->crl=crl_stack;
22655714Skris	if (crl != NULL)
22755714Skris		{
22855714Skris		sk_X509_CRL_push(crl_stack,crl);
22968651Skris		crl=NULL; /* now part of p7 for OPENSSL_freeing */
23055714Skris		}
23155714Skris
23268651Skris	if ((cert_stack=sk_X509_new_null()) == NULL) goto end;
23355714Skris	p7s->cert=cert_stack;
23455714Skris
235238405Sjkim	if(certflst) for(i = 0; i < sk_OPENSSL_STRING_num(certflst); i++) {
236238405Sjkim		certfile = sk_OPENSSL_STRING_value(certflst, i);
23755714Skris		if (add_certs_from_file(cert_stack,certfile) < 0)
23855714Skris			{
23955714Skris			BIO_printf(bio_err, "error loading certificates\n");
24055714Skris			ERR_print_errors(bio_err);
24155714Skris			goto end;
24255714Skris			}
24355714Skris	}
24455714Skris
245238405Sjkim	sk_OPENSSL_STRING_free(certflst);
24655714Skris
24755714Skris	if (outfile == NULL)
24868651Skris		{
24955714Skris		BIO_set_fp(out,stdout,BIO_NOCLOSE);
250109998Smarkm#ifdef OPENSSL_SYS_VMS
25168651Skris		{
25268651Skris		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
25368651Skris		out = BIO_push(tmpbio, out);
25468651Skris		}
25568651Skris#endif
25668651Skris		}
25755714Skris	else
25855714Skris		{
25955714Skris		if (BIO_write_filename(out,outfile) <= 0)
26055714Skris			{
26155714Skris			perror(outfile);
26255714Skris			goto end;
26355714Skris			}
26455714Skris		}
26555714Skris
26655714Skris	if 	(outformat == FORMAT_ASN1)
26755714Skris		i=i2d_PKCS7_bio(out,p7);
26855714Skris	else if (outformat == FORMAT_PEM)
26955714Skris		i=PEM_write_bio_PKCS7(out,p7);
27055714Skris	else	{
27155714Skris		BIO_printf(bio_err,"bad output format specified for outfile\n");
27255714Skris		goto end;
27355714Skris		}
27455714Skris	if (!i)
27555714Skris		{
27655714Skris		BIO_printf(bio_err,"unable to write pkcs7 object\n");
27755714Skris		ERR_print_errors(bio_err);
27855714Skris		goto end;
27955714Skris		}
28055714Skris	ret=0;
28155714Skrisend:
28255714Skris	if (in != NULL) BIO_free(in);
28368651Skris	if (out != NULL) BIO_free_all(out);
28455714Skris	if (p7 != NULL) PKCS7_free(p7);
28555714Skris	if (crl != NULL) X509_CRL_free(crl);
28655714Skris
287109998Smarkm	apps_shutdown();
288109998Smarkm	OPENSSL_EXIT(ret);
28955714Skris	}
29055714Skris
29155714Skris/*
29255714Skris *----------------------------------------------------------------------
29355714Skris * int add_certs_from_file
29455714Skris *
29555714Skris *	Read a list of certificates to be checked from a file.
29655714Skris *
29755714Skris * Results:
29855714Skris *	number of certs added if successful, -1 if not.
29955714Skris *----------------------------------------------------------------------
30055714Skris */
30155714Skrisstatic int add_certs_from_file(STACK_OF(X509) *stack, char *certfile)
30255714Skris	{
30355714Skris	BIO *in=NULL;
30455714Skris	int count=0;
30555714Skris	int ret= -1;
30655714Skris	STACK_OF(X509_INFO) *sk=NULL;
30755714Skris	X509_INFO *xi;
30855714Skris
30955714Skris	in=BIO_new(BIO_s_file());
31055714Skris	if ((in == NULL) || (BIO_read_filename(in,certfile) <= 0))
31155714Skris		{
31255714Skris		BIO_printf(bio_err,"error opening the file, %s\n",certfile);
31355714Skris		goto end;
31455714Skris		}
31555714Skris
31655714Skris	/* This loads from a file, a stack of x509/crl/pkey sets */
31755714Skris	sk=PEM_X509_INFO_read_bio(in,NULL,NULL,NULL);
31855714Skris	if (sk == NULL) {
31955714Skris		BIO_printf(bio_err,"error reading the file, %s\n",certfile);
32055714Skris		goto end;
32155714Skris	}
32255714Skris
32355714Skris	/* scan over it and pull out the CRL's */
32455714Skris	while (sk_X509_INFO_num(sk))
32555714Skris		{
32655714Skris		xi=sk_X509_INFO_shift(sk);
32755714Skris		if (xi->x509 != NULL)
32855714Skris			{
32955714Skris			sk_X509_push(stack,xi->x509);
33055714Skris			xi->x509=NULL;
33155714Skris			count++;
33255714Skris			}
33355714Skris		X509_INFO_free(xi);
33455714Skris		}
33555714Skris
33655714Skris	ret=count;
33755714Skrisend:
33868651Skris 	/* never need to OPENSSL_free x */
33955714Skris	if (in != NULL) BIO_free(in);
34055714Skris	if (sk != NULL) sk_X509_INFO_free(sk);
34155714Skris	return(ret);
34255714Skris	}
34355714Skris
344