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. 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 59280304Sjkim/* 60280304Sjkim * This was written by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu> and 61280304Sjkim * donated 'to the cause' along with lots and lots of other fixes to the 62280304Sjkim * library. 63280304Sjkim */ 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 78280304Sjkim#define PROG crl2pkcs7_main 7955714Skris 80280304Sjkim/*- 81280304Sjkim * -inform arg - input format - default PEM (DER or PEM) 8255714Skris * -outform arg - output format - default PEM 83280304Sjkim * -in arg - input file - default stdin 84280304Sjkim * -out arg - output file - default stdout 8555714Skris */ 8655714Skris 8759191Skrisint MAIN(int, char **); 8859191Skris 8955714Skrisint MAIN(int argc, char **argv) 90280304Sjkim{ 91280304Sjkim int i, badops = 0; 92280304Sjkim BIO *in = NULL, *out = NULL; 93280304Sjkim int informat, outformat; 94280304Sjkim char *infile, *outfile, *prog, *certfile; 95280304Sjkim PKCS7 *p7 = NULL; 96280304Sjkim PKCS7_SIGNED *p7s = NULL; 97280304Sjkim X509_CRL *crl = NULL; 98280304Sjkim STACK_OF(OPENSSL_STRING) *certflst = NULL; 99280304Sjkim STACK_OF(X509_CRL) *crl_stack = NULL; 100280304Sjkim STACK_OF(X509) *cert_stack = NULL; 101280304Sjkim int ret = 1, nocrl = 0; 10255714Skris 103280304Sjkim apps_startup(); 10455714Skris 105280304Sjkim if (bio_err == NULL) 106280304Sjkim if ((bio_err = BIO_new(BIO_s_file())) != NULL) 107280304Sjkim BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 10855714Skris 109280304Sjkim infile = NULL; 110280304Sjkim outfile = NULL; 111280304Sjkim informat = FORMAT_PEM; 112280304Sjkim outformat = FORMAT_PEM; 11355714Skris 114280304Sjkim prog = argv[0]; 115280304Sjkim argc--; 116280304Sjkim argv++; 117280304Sjkim while (argc >= 1) { 118280304Sjkim if (strcmp(*argv, "-inform") == 0) { 119280304Sjkim if (--argc < 1) 120280304Sjkim goto bad; 121280304Sjkim informat = str2fmt(*(++argv)); 122280304Sjkim } else if (strcmp(*argv, "-outform") == 0) { 123280304Sjkim if (--argc < 1) 124280304Sjkim goto bad; 125280304Sjkim outformat = str2fmt(*(++argv)); 126280304Sjkim } else if (strcmp(*argv, "-in") == 0) { 127280304Sjkim if (--argc < 1) 128280304Sjkim goto bad; 129280304Sjkim infile = *(++argv); 130280304Sjkim } else if (strcmp(*argv, "-nocrl") == 0) { 131280304Sjkim nocrl = 1; 132280304Sjkim } else if (strcmp(*argv, "-out") == 0) { 133280304Sjkim if (--argc < 1) 134280304Sjkim goto bad; 135280304Sjkim outfile = *(++argv); 136280304Sjkim } else if (strcmp(*argv, "-certfile") == 0) { 137280304Sjkim if (--argc < 1) 138280304Sjkim goto bad; 139280304Sjkim if (!certflst) 140280304Sjkim certflst = sk_OPENSSL_STRING_new_null(); 141280304Sjkim if (!certflst) 142280304Sjkim goto end; 143280304Sjkim if (!sk_OPENSSL_STRING_push(certflst, *(++argv))) { 144280304Sjkim sk_OPENSSL_STRING_free(certflst); 145280304Sjkim goto end; 146280304Sjkim } 147280304Sjkim } else { 148280304Sjkim BIO_printf(bio_err, "unknown option %s\n", *argv); 149280304Sjkim badops = 1; 150280304Sjkim break; 151280304Sjkim } 152280304Sjkim argc--; 153280304Sjkim argv++; 154280304Sjkim } 15555714Skris 156280304Sjkim if (badops) { 157280304Sjkim bad: 158280304Sjkim BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog); 159280304Sjkim BIO_printf(bio_err, "where options are\n"); 160280304Sjkim BIO_printf(bio_err, " -inform arg input format - DER or PEM\n"); 161280304Sjkim BIO_printf(bio_err, " -outform arg output format - DER or PEM\n"); 162280304Sjkim BIO_printf(bio_err, " -in arg input file\n"); 163280304Sjkim BIO_printf(bio_err, " -out arg output file\n"); 164280304Sjkim BIO_printf(bio_err, 165280304Sjkim " -certfile arg certificates file of chain to a trusted CA\n"); 166280304Sjkim BIO_printf(bio_err, " (can be used more than once)\n"); 167280304Sjkim BIO_printf(bio_err, 168280304Sjkim " -nocrl no crl to load, just certs from '-certfile'\n"); 169280304Sjkim ret = 1; 170280304Sjkim goto end; 171280304Sjkim } 17255714Skris 173280304Sjkim ERR_load_crypto_strings(); 17455714Skris 175280304Sjkim in = BIO_new(BIO_s_file()); 176280304Sjkim out = BIO_new(BIO_s_file()); 177280304Sjkim if ((in == NULL) || (out == NULL)) { 178280304Sjkim ERR_print_errors(bio_err); 179280304Sjkim goto end; 180280304Sjkim } 18155714Skris 182280304Sjkim if (!nocrl) { 183280304Sjkim if (infile == NULL) 184280304Sjkim BIO_set_fp(in, stdin, BIO_NOCLOSE); 185280304Sjkim else { 186280304Sjkim if (BIO_read_filename(in, infile) <= 0) { 187280304Sjkim perror(infile); 188280304Sjkim goto end; 189280304Sjkim } 190280304Sjkim } 19155714Skris 192280304Sjkim if (informat == FORMAT_ASN1) 193280304Sjkim crl = d2i_X509_CRL_bio(in, NULL); 194280304Sjkim else if (informat == FORMAT_PEM) 195280304Sjkim crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL); 196280304Sjkim else { 197280304Sjkim BIO_printf(bio_err, "bad input format specified for input crl\n"); 198280304Sjkim goto end; 199280304Sjkim } 200280304Sjkim if (crl == NULL) { 201280304Sjkim BIO_printf(bio_err, "unable to load CRL\n"); 202280304Sjkim ERR_print_errors(bio_err); 203280304Sjkim goto end; 204280304Sjkim } 205280304Sjkim } 20655714Skris 207280304Sjkim if ((p7 = PKCS7_new()) == NULL) 208280304Sjkim goto end; 209280304Sjkim if ((p7s = PKCS7_SIGNED_new()) == NULL) 210280304Sjkim goto end; 211280304Sjkim p7->type = OBJ_nid2obj(NID_pkcs7_signed); 212280304Sjkim p7->d.sign = p7s; 213280304Sjkim p7s->contents->type = OBJ_nid2obj(NID_pkcs7_data); 21455714Skris 215280304Sjkim if (!ASN1_INTEGER_set(p7s->version, 1)) 216280304Sjkim goto end; 217280304Sjkim if ((crl_stack = sk_X509_CRL_new_null()) == NULL) 218280304Sjkim goto end; 219280304Sjkim p7s->crl = crl_stack; 220280304Sjkim if (crl != NULL) { 221280304Sjkim sk_X509_CRL_push(crl_stack, crl); 222280304Sjkim crl = NULL; /* now part of p7 for OPENSSL_freeing */ 223280304Sjkim } 22455714Skris 225280304Sjkim if ((cert_stack = sk_X509_new_null()) == NULL) 226280304Sjkim goto end; 227280304Sjkim p7s->cert = cert_stack; 22855714Skris 229280304Sjkim if (certflst) 230280304Sjkim for (i = 0; i < sk_OPENSSL_STRING_num(certflst); i++) { 231280304Sjkim certfile = sk_OPENSSL_STRING_value(certflst, i); 232280304Sjkim if (add_certs_from_file(cert_stack, certfile) < 0) { 233280304Sjkim BIO_printf(bio_err, "error loading certificates\n"); 234280304Sjkim ERR_print_errors(bio_err); 235280304Sjkim goto end; 236280304Sjkim } 237280304Sjkim } 23855714Skris 239280304Sjkim sk_OPENSSL_STRING_free(certflst); 240280304Sjkim 241280304Sjkim if (outfile == NULL) { 242280304Sjkim BIO_set_fp(out, stdout, BIO_NOCLOSE); 243109998Smarkm#ifdef OPENSSL_SYS_VMS 244280304Sjkim { 245280304Sjkim BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 246280304Sjkim out = BIO_push(tmpbio, out); 247280304Sjkim } 24868651Skris#endif 249280304Sjkim } else { 250280304Sjkim if (BIO_write_filename(out, outfile) <= 0) { 251280304Sjkim perror(outfile); 252280304Sjkim goto end; 253280304Sjkim } 254280304Sjkim } 25555714Skris 256280304Sjkim if (outformat == FORMAT_ASN1) 257280304Sjkim i = i2d_PKCS7_bio(out, p7); 258280304Sjkim else if (outformat == FORMAT_PEM) 259280304Sjkim i = PEM_write_bio_PKCS7(out, p7); 260280304Sjkim else { 261280304Sjkim BIO_printf(bio_err, "bad output format specified for outfile\n"); 262280304Sjkim goto end; 263280304Sjkim } 264280304Sjkim if (!i) { 265280304Sjkim BIO_printf(bio_err, "unable to write pkcs7 object\n"); 266280304Sjkim ERR_print_errors(bio_err); 267280304Sjkim goto end; 268280304Sjkim } 269280304Sjkim ret = 0; 270280304Sjkim end: 271280304Sjkim if (in != NULL) 272280304Sjkim BIO_free(in); 273280304Sjkim if (out != NULL) 274280304Sjkim BIO_free_all(out); 275280304Sjkim if (p7 != NULL) 276280304Sjkim PKCS7_free(p7); 277280304Sjkim if (crl != NULL) 278280304Sjkim X509_CRL_free(crl); 27955714Skris 280280304Sjkim apps_shutdown(); 281280304Sjkim OPENSSL_EXIT(ret); 282280304Sjkim} 28355714Skris 284280304Sjkim/*- 28555714Skris *---------------------------------------------------------------------- 28655714Skris * int add_certs_from_file 28755714Skris * 288280304Sjkim * Read a list of certificates to be checked from a file. 28955714Skris * 29055714Skris * Results: 291280304Sjkim * number of certs added if successful, -1 if not. 29255714Skris *---------------------------------------------------------------------- 29355714Skris */ 29455714Skrisstatic int add_certs_from_file(STACK_OF(X509) *stack, char *certfile) 295280304Sjkim{ 296280304Sjkim BIO *in = NULL; 297280304Sjkim int count = 0; 298280304Sjkim int ret = -1; 299280304Sjkim STACK_OF(X509_INFO) *sk = NULL; 300280304Sjkim X509_INFO *xi; 30155714Skris 302280304Sjkim in = BIO_new(BIO_s_file()); 303280304Sjkim if ((in == NULL) || (BIO_read_filename(in, certfile) <= 0)) { 304280304Sjkim BIO_printf(bio_err, "error opening the file, %s\n", certfile); 305280304Sjkim goto end; 306280304Sjkim } 30755714Skris 308280304Sjkim /* This loads from a file, a stack of x509/crl/pkey sets */ 309280304Sjkim sk = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL); 310280304Sjkim if (sk == NULL) { 311280304Sjkim BIO_printf(bio_err, "error reading the file, %s\n", certfile); 312280304Sjkim goto end; 313280304Sjkim } 31455714Skris 315280304Sjkim /* scan over it and pull out the CRL's */ 316280304Sjkim while (sk_X509_INFO_num(sk)) { 317280304Sjkim xi = sk_X509_INFO_shift(sk); 318280304Sjkim if (xi->x509 != NULL) { 319280304Sjkim sk_X509_push(stack, xi->x509); 320280304Sjkim xi->x509 = NULL; 321280304Sjkim count++; 322280304Sjkim } 323280304Sjkim X509_INFO_free(xi); 324280304Sjkim } 32555714Skris 326280304Sjkim ret = count; 327280304Sjkim end: 328280304Sjkim /* never need to OPENSSL_free x */ 329280304Sjkim if (in != NULL) 330280304Sjkim BIO_free(in); 331280304Sjkim if (sk != NULL) 332280304Sjkim sk_X509_INFO_free(sk); 333280304Sjkim return (ret); 334280304Sjkim} 335