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.
8296341Sdelphij *
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).
15296341Sdelphij *
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.
22296341Sdelphij *
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 :-).
37296341Sdelphij * 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)"
40296341Sdelphij *
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.
52296341Sdelphij *
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
59296341Sdelphij/*
60296341Sdelphij * This was written by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu> and
61296341Sdelphij * donated 'to the cause' along with lots and lots of other fixes to the
62296341Sdelphij * library.
63296341Sdelphij */
6455714Skris
6555714Skris#include <stdio.h>
6655714Skris#include <string.h>
6755714Skris#include <sys/types.h>
6855714Skris#include "apps.h"
6955714Skris#include <openssl/err.h>
7055714Skris#include <openssl/evp.h>
7155714Skris#include <openssl/x509.h>
7255714Skris#include <openssl/pkcs7.h>
7355714Skris#include <openssl/pem.h>
7455714Skris#include <openssl/objects.h>
7555714Skris
7655714Skrisstatic int add_certs_from_file(STACK_OF(X509) *stack, char *certfile);
7755714Skris#undef PROG
78296341Sdelphij#define PROG    crl2pkcs7_main
7955714Skris
80296341Sdelphij/*-
81296341Sdelphij * -inform arg  - input format - default PEM (DER or PEM)
8255714Skris * -outform arg - output format - default PEM
83296341Sdelphij * -in arg      - input file - default stdin
84296341Sdelphij * -out arg     - output file - default stdout
8555714Skris */
8655714Skris
8759191Skrisint MAIN(int, char **);
8859191Skris
8955714Skrisint MAIN(int argc, char **argv)
90296341Sdelphij{
91296341Sdelphij    int i, badops = 0;
92296341Sdelphij    BIO *in = NULL, *out = NULL;
93296341Sdelphij    int informat, outformat;
94296341Sdelphij    char *infile, *outfile, *prog, *certfile;
95296341Sdelphij    PKCS7 *p7 = NULL;
96296341Sdelphij    PKCS7_SIGNED *p7s = NULL;
97296341Sdelphij    X509_CRL *crl = NULL;
98296341Sdelphij    STACK_OF(OPENSSL_STRING) *certflst = NULL;
99296341Sdelphij    STACK_OF(X509_CRL) *crl_stack = NULL;
100296341Sdelphij    STACK_OF(X509) *cert_stack = NULL;
101296341Sdelphij    int ret = 1, nocrl = 0;
10255714Skris
103296341Sdelphij    apps_startup();
10455714Skris
105296341Sdelphij    if (bio_err == NULL)
106296341Sdelphij        if ((bio_err = BIO_new(BIO_s_file())) != NULL)
107296341Sdelphij            BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
10855714Skris
109296341Sdelphij    infile = NULL;
110296341Sdelphij    outfile = NULL;
111296341Sdelphij    informat = FORMAT_PEM;
112296341Sdelphij    outformat = FORMAT_PEM;
11355714Skris
114296341Sdelphij    prog = argv[0];
115296341Sdelphij    argc--;
116296341Sdelphij    argv++;
117296341Sdelphij    while (argc >= 1) {
118296341Sdelphij        if (strcmp(*argv, "-inform") == 0) {
119296341Sdelphij            if (--argc < 1)
120296341Sdelphij                goto bad;
121296341Sdelphij            informat = str2fmt(*(++argv));
122296341Sdelphij        } else if (strcmp(*argv, "-outform") == 0) {
123296341Sdelphij            if (--argc < 1)
124296341Sdelphij                goto bad;
125296341Sdelphij            outformat = str2fmt(*(++argv));
126296341Sdelphij        } else if (strcmp(*argv, "-in") == 0) {
127296341Sdelphij            if (--argc < 1)
128296341Sdelphij                goto bad;
129296341Sdelphij            infile = *(++argv);
130296341Sdelphij        } else if (strcmp(*argv, "-nocrl") == 0) {
131296341Sdelphij            nocrl = 1;
132296341Sdelphij        } else if (strcmp(*argv, "-out") == 0) {
133296341Sdelphij            if (--argc < 1)
134296341Sdelphij                goto bad;
135296341Sdelphij            outfile = *(++argv);
136296341Sdelphij        } else if (strcmp(*argv, "-certfile") == 0) {
137296341Sdelphij            if (--argc < 1)
138296341Sdelphij                goto bad;
139296341Sdelphij            if (!certflst)
140296341Sdelphij                certflst = sk_OPENSSL_STRING_new_null();
141296341Sdelphij            if (!certflst)
142296341Sdelphij                goto end;
143296341Sdelphij            if (!sk_OPENSSL_STRING_push(certflst, *(++argv))) {
144296341Sdelphij                sk_OPENSSL_STRING_free(certflst);
145296341Sdelphij                goto end;
146296341Sdelphij            }
147296341Sdelphij        } else {
148296341Sdelphij            BIO_printf(bio_err, "unknown option %s\n", *argv);
149296341Sdelphij            badops = 1;
150296341Sdelphij            break;
151296341Sdelphij        }
152296341Sdelphij        argc--;
153296341Sdelphij        argv++;
154296341Sdelphij    }
15555714Skris
156296341Sdelphij    if (badops) {
157296341Sdelphij bad:
158296341Sdelphij        BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
159296341Sdelphij        BIO_printf(bio_err, "where options are\n");
160296341Sdelphij        BIO_printf(bio_err, " -inform arg    input format - DER or PEM\n");
161296341Sdelphij        BIO_printf(bio_err, " -outform arg   output format - DER or PEM\n");
162296341Sdelphij        BIO_printf(bio_err, " -in arg        input file\n");
163296341Sdelphij        BIO_printf(bio_err, " -out arg       output file\n");
164296341Sdelphij        BIO_printf(bio_err,
165296341Sdelphij                   " -certfile arg  certificates file of chain to a trusted CA\n");
166296341Sdelphij        BIO_printf(bio_err, "                (can be used more than once)\n");
167296341Sdelphij        BIO_printf(bio_err,
168296341Sdelphij                   " -nocrl         no crl to load, just certs from '-certfile'\n");
169296341Sdelphij        ret = 1;
170296341Sdelphij        goto end;
171296341Sdelphij    }
17255714Skris
173296341Sdelphij    ERR_load_crypto_strings();
17455714Skris
175296341Sdelphij    in = BIO_new(BIO_s_file());
176296341Sdelphij    out = BIO_new(BIO_s_file());
177296341Sdelphij    if ((in == NULL) || (out == NULL)) {
178296341Sdelphij        ERR_print_errors(bio_err);
179296341Sdelphij        goto end;
180296341Sdelphij    }
18155714Skris
182296341Sdelphij    if (!nocrl) {
183296341Sdelphij        if (infile == NULL)
184296341Sdelphij            BIO_set_fp(in, stdin, BIO_NOCLOSE);
185296341Sdelphij        else {
186296341Sdelphij            if (BIO_read_filename(in, infile) <= 0) {
187296341Sdelphij                perror(infile);
188296341Sdelphij                goto end;
189296341Sdelphij            }
190296341Sdelphij        }
19155714Skris
192296341Sdelphij        if (informat == FORMAT_ASN1)
193296341Sdelphij            crl = d2i_X509_CRL_bio(in, NULL);
194296341Sdelphij        else if (informat == FORMAT_PEM)
195296341Sdelphij            crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
196296341Sdelphij        else {
197296341Sdelphij            BIO_printf(bio_err, "bad input format specified for input crl\n");
198296341Sdelphij            goto end;
199296341Sdelphij        }
200296341Sdelphij        if (crl == NULL) {
201296341Sdelphij            BIO_printf(bio_err, "unable to load CRL\n");
202296341Sdelphij            ERR_print_errors(bio_err);
203296341Sdelphij            goto end;
204296341Sdelphij        }
205296341Sdelphij    }
20655714Skris
207296341Sdelphij    if ((p7 = PKCS7_new()) == NULL)
208296341Sdelphij        goto end;
209296341Sdelphij    if ((p7s = PKCS7_SIGNED_new()) == NULL)
210296341Sdelphij        goto end;
211296341Sdelphij    p7->type = OBJ_nid2obj(NID_pkcs7_signed);
212296341Sdelphij    p7->d.sign = p7s;
213296341Sdelphij    p7s->contents->type = OBJ_nid2obj(NID_pkcs7_data);
21455714Skris
215296341Sdelphij    if (!ASN1_INTEGER_set(p7s->version, 1))
216296341Sdelphij        goto end;
217296341Sdelphij    if ((crl_stack = sk_X509_CRL_new_null()) == NULL)
218296341Sdelphij        goto end;
219296341Sdelphij    p7s->crl = crl_stack;
220296341Sdelphij    if (crl != NULL) {
221296341Sdelphij        sk_X509_CRL_push(crl_stack, crl);
222296341Sdelphij        crl = NULL;             /* now part of p7 for OPENSSL_freeing */
223296341Sdelphij    }
22455714Skris
225296341Sdelphij    if ((cert_stack = sk_X509_new_null()) == NULL)
226296341Sdelphij        goto end;
227296341Sdelphij    p7s->cert = cert_stack;
22855714Skris
229296341Sdelphij    if (certflst)
230296341Sdelphij        for (i = 0; i < sk_OPENSSL_STRING_num(certflst); i++) {
231296341Sdelphij            certfile = sk_OPENSSL_STRING_value(certflst, i);
232296341Sdelphij            if (add_certs_from_file(cert_stack, certfile) < 0) {
233296341Sdelphij                BIO_printf(bio_err, "error loading certificates\n");
234296341Sdelphij                ERR_print_errors(bio_err);
235296341Sdelphij                goto end;
236296341Sdelphij            }
237296341Sdelphij        }
23855714Skris
239296341Sdelphij    sk_OPENSSL_STRING_free(certflst);
240296341Sdelphij
241296341Sdelphij    if (outfile == NULL) {
242296341Sdelphij        BIO_set_fp(out, stdout, BIO_NOCLOSE);
243109998Smarkm#ifdef OPENSSL_SYS_VMS
244296341Sdelphij        {
245296341Sdelphij            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
246296341Sdelphij            out = BIO_push(tmpbio, out);
247296341Sdelphij        }
24868651Skris#endif
249296341Sdelphij    } else {
250296341Sdelphij        if (BIO_write_filename(out, outfile) <= 0) {
251296341Sdelphij            perror(outfile);
252296341Sdelphij            goto end;
253296341Sdelphij        }
254296341Sdelphij    }
25555714Skris
256296341Sdelphij    if (outformat == FORMAT_ASN1)
257296341Sdelphij        i = i2d_PKCS7_bio(out, p7);
258296341Sdelphij    else if (outformat == FORMAT_PEM)
259296341Sdelphij        i = PEM_write_bio_PKCS7(out, p7);
260296341Sdelphij    else {
261296341Sdelphij        BIO_printf(bio_err, "bad output format specified for outfile\n");
262296341Sdelphij        goto end;
263296341Sdelphij    }
264296341Sdelphij    if (!i) {
265296341Sdelphij        BIO_printf(bio_err, "unable to write pkcs7 object\n");
266296341Sdelphij        ERR_print_errors(bio_err);
267296341Sdelphij        goto end;
268296341Sdelphij    }
269296341Sdelphij    ret = 0;
270296341Sdelphij end:
271296341Sdelphij    if (in != NULL)
272296341Sdelphij        BIO_free(in);
273296341Sdelphij    if (out != NULL)
274296341Sdelphij        BIO_free_all(out);
275296341Sdelphij    if (p7 != NULL)
276296341Sdelphij        PKCS7_free(p7);
277296341Sdelphij    if (crl != NULL)
278296341Sdelphij        X509_CRL_free(crl);
27955714Skris
280296341Sdelphij    apps_shutdown();
281296341Sdelphij    OPENSSL_EXIT(ret);
282296341Sdelphij}
28355714Skris
284296341Sdelphij/*-
28555714Skris *----------------------------------------------------------------------
28655714Skris * int add_certs_from_file
28755714Skris *
288296341Sdelphij *      Read a list of certificates to be checked from a file.
28955714Skris *
29055714Skris * Results:
291296341Sdelphij *      number of certs added if successful, -1 if not.
29255714Skris *----------------------------------------------------------------------
29355714Skris */
29455714Skrisstatic int add_certs_from_file(STACK_OF(X509) *stack, char *certfile)
295296341Sdelphij{
296296341Sdelphij    BIO *in = NULL;
297296341Sdelphij    int count = 0;
298296341Sdelphij    int ret = -1;
299296341Sdelphij    STACK_OF(X509_INFO) *sk = NULL;
300296341Sdelphij    X509_INFO *xi;
30155714Skris
302296341Sdelphij    in = BIO_new(BIO_s_file());
303296341Sdelphij    if ((in == NULL) || (BIO_read_filename(in, certfile) <= 0)) {
304296341Sdelphij        BIO_printf(bio_err, "error opening the file, %s\n", certfile);
305296341Sdelphij        goto end;
306296341Sdelphij    }
30755714Skris
308296341Sdelphij    /* This loads from a file, a stack of x509/crl/pkey sets */
309296341Sdelphij    sk = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
310296341Sdelphij    if (sk == NULL) {
311296341Sdelphij        BIO_printf(bio_err, "error reading the file, %s\n", certfile);
312296341Sdelphij        goto end;
313296341Sdelphij    }
31455714Skris
315296341Sdelphij    /* scan over it and pull out the CRL's */
316296341Sdelphij    while (sk_X509_INFO_num(sk)) {
317296341Sdelphij        xi = sk_X509_INFO_shift(sk);
318296341Sdelphij        if (xi->x509 != NULL) {
319296341Sdelphij            sk_X509_push(stack, xi->x509);
320296341Sdelphij            xi->x509 = NULL;
321296341Sdelphij            count++;
322296341Sdelphij        }
323296341Sdelphij        X509_INFO_free(xi);
324296341Sdelphij    }
32555714Skris
326296341Sdelphij    ret = count;
327296341Sdelphij end:
328296341Sdelphij    /* never need to OPENSSL_free x */
329296341Sdelphij    if (in != NULL)
330296341Sdelphij        BIO_free(in);
331296341Sdelphij    if (sk != NULL)
332296341Sdelphij        sk_X509_INFO_free(sk);
333296341Sdelphij    return (ret);
334296341Sdelphij}
335