Main Page | Class List | Directories | File List | Class Members | File Members

pkcs7.c

Go to the documentation of this file.
00001 /* apps/pkcs7.c */
00002 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
00003  * All rights reserved.
00004  *
00005  * This package is an SSL implementation written
00006  * by Eric Young (eay@cryptsoft.com).
00007  * The implementation was written so as to conform with Netscapes SSL.
00008  * 
00009  * This library is free for commercial and non-commercial use as long as
00010  * the following conditions are aheared to.  The following conditions
00011  * apply to all code found in this distribution, be it the RC4, RSA,
00012  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
00013  * included with this distribution is covered by the same copyright terms
00014  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
00015  * 
00016  * Copyright remains Eric Young's, and as such any Copyright notices in
00017  * the code are not to be removed.
00018  * If this package is used in a product, Eric Young should be given attribution
00019  * as the author of the parts of the library used.
00020  * This can be in the form of a textual message at program startup or
00021  * in documentation (online or textual) provided with the package.
00022  * 
00023  * Redistribution and use in source and binary forms, with or without
00024  * modification, are permitted provided that the following conditions
00025  * are met:
00026  * 1. Redistributions of source code must retain the copyright
00027  *    notice, this list of conditions and the following disclaimer.
00028  * 2. Redistributions in binary form must reproduce the above copyright
00029  *    notice, this list of conditions and the following disclaimer in the
00030  *    documentation and/or other materials provided with the distribution.
00031  * 3. All advertising materials mentioning features or use of this software
00032  *    must display the following acknowledgement:
00033  *    "This product includes cryptographic software written by
00034  *     Eric Young (eay@cryptsoft.com)"
00035  *    The word 'cryptographic' can be left out if the rouines from the library
00036  *    being used are not cryptographic related :-).
00037  * 4. If you include any Windows specific code (or a derivative thereof) from 
00038  *    the apps directory (application code) you must include an acknowledgement:
00039  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
00040  * 
00041  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
00042  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00043  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00044  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00045  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00046  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00047  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00048  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00049  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00050  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00051  * SUCH DAMAGE.
00052  * 
00053  * The licence and distribution terms for any publically available version or
00054  * derivative of this code cannot be changed.  i.e. this code cannot simply be
00055  * copied and put under another distribution licence
00056  * [including the GNU Public Licence.]
00057  */
00058 
00059 #include <stdio.h>
00060 #include <stdlib.h>
00061 #include <string.h>
00062 #include <time.h>
00063 #include "apps.h"
00064 #include <openssl/err.h>
00065 #include <openssl/objects.h>
00066 #include <openssl/evp.h>
00067 #include <openssl/x509.h>
00068 #include <openssl/pkcs7.h>
00069 #include <openssl/pem.h>
00070 
00071 #undef PROG
00072 #define PROG    pkcs7_main
00073 
00074 /* -inform arg  - input format - default PEM (DER or PEM)
00075  * -outform arg - output format - default PEM
00076  * -in arg      - input file - default stdin
00077  * -out arg     - output file - default stdout
00078  * -print_certs
00079  */
00080 
00081 int MAIN(int, char **);
00082 
00083 int MAIN(int argc, char **argv)
00084         {
00085 #ifndef OPENSSL_NO_ENGINE
00086         ENGINE *e = NULL;
00087 #endif
00088         PKCS7 *p7=NULL;
00089         int i,badops=0;
00090         BIO *in=NULL,*out=NULL;
00091         int informat,outformat;
00092         char *infile,*outfile,*prog;
00093         int print_certs=0,text=0,noout=0;
00094         int ret=1;
00095 #ifndef OPENSSL_NO_ENGINE
00096         char *engine=NULL;
00097 #endif
00098 
00099         apps_startup();
00100 
00101         if (bio_err == NULL)
00102                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
00103                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
00104 
00105         if (!load_config(bio_err, NULL))
00106                 goto end;
00107 
00108         infile=NULL;
00109         outfile=NULL;
00110         informat=FORMAT_PEM;
00111         outformat=FORMAT_PEM;
00112 
00113         prog=argv[0];
00114         argc--;
00115         argv++;
00116         while (argc >= 1)
00117                 {
00118                 if      (strcmp(*argv,"-inform") == 0)
00119                         {
00120                         if (--argc < 1) goto bad;
00121                         informat=str2fmt(*(++argv));
00122                         }
00123                 else if (strcmp(*argv,"-outform") == 0)
00124                         {
00125                         if (--argc < 1) goto bad;
00126                         outformat=str2fmt(*(++argv));
00127                         }
00128                 else if (strcmp(*argv,"-in") == 0)
00129                         {
00130                         if (--argc < 1) goto bad;
00131                         infile= *(++argv);
00132                         }
00133                 else if (strcmp(*argv,"-out") == 0)
00134                         {
00135                         if (--argc < 1) goto bad;
00136                         outfile= *(++argv);
00137                         }
00138                 else if (strcmp(*argv,"-noout") == 0)
00139                         noout=1;
00140                 else if (strcmp(*argv,"-text") == 0)
00141                         text=1;
00142                 else if (strcmp(*argv,"-print_certs") == 0)
00143                         print_certs=1;
00144 #ifndef OPENSSL_NO_ENGINE
00145                 else if (strcmp(*argv,"-engine") == 0)
00146                         {
00147                         if (--argc < 1) goto bad;
00148                         engine= *(++argv);
00149                         }
00150 #endif
00151                 else
00152                         {
00153                         BIO_printf(bio_err,"unknown option %s\n",*argv);
00154                         badops=1;
00155                         break;
00156                         }
00157                 argc--;
00158                 argv++;
00159                 }
00160 
00161         if (badops)
00162                 {
00163 bad:
00164                 BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
00165                 BIO_printf(bio_err,"where options are\n");
00166                 BIO_printf(bio_err," -inform arg   input format - DER or PEM\n");
00167                 BIO_printf(bio_err," -outform arg  output format - DER or PEM\n");
00168                 BIO_printf(bio_err," -in arg       input file\n");
00169                 BIO_printf(bio_err," -out arg      output file\n");
00170                 BIO_printf(bio_err," -print_certs  print any certs or crl in the input\n");
00171                 BIO_printf(bio_err," -text         print full details of certificates\n");
00172                 BIO_printf(bio_err," -noout        don't output encoded data\n");
00173 #ifndef OPENSSL_NO_ENGINE
00174                 BIO_printf(bio_err," -engine e     use engine e, possibly a hardware device.\n");
00175 #endif
00176                 ret = 1;
00177                 goto end;
00178                 }
00179 
00180         ERR_load_crypto_strings();
00181 
00182 #ifndef OPENSSL_NO_ENGINE
00183         e = setup_engine(bio_err, engine, 0);
00184 #endif
00185 
00186         in=BIO_new(BIO_s_file());
00187         out=BIO_new(BIO_s_file());
00188         if ((in == NULL) || (out == NULL))
00189                 {
00190                 ERR_print_errors(bio_err);
00191                 goto end;
00192                 }
00193 
00194         if (infile == NULL)
00195                 BIO_set_fp(in,stdin,BIO_NOCLOSE);
00196         else
00197                 {
00198                 if (BIO_read_filename(in,infile) <= 0)
00199                 if (in == NULL)
00200                         {
00201                         perror(infile);
00202                         goto end;
00203                         }
00204                 }
00205 
00206         if      (informat == FORMAT_ASN1)
00207                 p7=d2i_PKCS7_bio(in,NULL);
00208         else if (informat == FORMAT_PEM)
00209                 p7=PEM_read_bio_PKCS7(in,NULL,NULL,NULL);
00210         else
00211                 {
00212                 BIO_printf(bio_err,"bad input format specified for pkcs7 object\n");
00213                 goto end;
00214                 }
00215         if (p7 == NULL)
00216                 {
00217                 BIO_printf(bio_err,"unable to load PKCS7 object\n");
00218                 ERR_print_errors(bio_err);
00219                 goto end;
00220                 }
00221 
00222         if (outfile == NULL)
00223                 {
00224                 BIO_set_fp(out,stdout,BIO_NOCLOSE);
00225 #ifdef OPENSSL_SYS_VMS
00226                 {
00227                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
00228                 out = BIO_push(tmpbio, out);
00229                 }
00230 #endif
00231                 }
00232         else
00233                 {
00234                 if (BIO_write_filename(out,outfile) <= 0)
00235                         {
00236                         perror(outfile);
00237                         goto end;
00238                         }
00239                 }
00240 
00241         if (print_certs)
00242                 {
00243                 STACK_OF(X509) *certs=NULL;
00244                 STACK_OF(X509_CRL) *crls=NULL;
00245 
00246                 i=OBJ_obj2nid(p7->type);
00247                 switch (i)
00248                         {
00249                 case NID_pkcs7_signed:
00250                         certs=p7->d.sign->cert;
00251                         crls=p7->d.sign->crl;
00252                         break;
00253                 case NID_pkcs7_signedAndEnveloped:
00254                         certs=p7->d.signed_and_enveloped->cert;
00255                         crls=p7->d.signed_and_enveloped->crl;
00256                         break;
00257                 default:
00258                         break;
00259                         }
00260 
00261                 if (certs != NULL)
00262                         {
00263                         X509 *x;
00264 
00265                         for (i=0; i<sk_X509_num(certs); i++)
00266                                 {
00267                                 x=sk_X509_value(certs,i);
00268                                 if(text) X509_print(out, x);
00269                                 else dump_cert_text(out, x);
00270 
00271                                 if(!noout) PEM_write_bio_X509(out,x);
00272                                 BIO_puts(out,"\n");
00273                                 }
00274                         }
00275                 if (crls != NULL)
00276                         {
00277                         X509_CRL *crl;
00278 
00279                         for (i=0; i<sk_X509_CRL_num(crls); i++)
00280                                 {
00281                                 crl=sk_X509_CRL_value(crls,i);
00282 
00283                                 X509_CRL_print(out, crl);
00284 
00285                                 if(!noout)PEM_write_bio_X509_CRL(out,crl);
00286                                 BIO_puts(out,"\n");
00287                                 }
00288                         }
00289 
00290                 ret=0;
00291                 goto end;
00292                 }
00293 
00294         if(!noout) {
00295                 if      (outformat == FORMAT_ASN1)
00296                         i=i2d_PKCS7_bio(out,p7);
00297                 else if (outformat == FORMAT_PEM)
00298                         i=PEM_write_bio_PKCS7(out,p7);
00299                 else    {
00300                         BIO_printf(bio_err,"bad output format specified for outfile\n");
00301                         goto end;
00302                         }
00303 
00304                 if (!i)
00305                         {
00306                         BIO_printf(bio_err,"unable to write pkcs7 object\n");
00307                         ERR_print_errors(bio_err);
00308                         goto end;
00309                         }
00310         }
00311         ret=0;
00312 end:
00313         if (p7 != NULL) PKCS7_free(p7);
00314         if (in != NULL) BIO_free(in);
00315         if (out != NULL) BIO_free_all(out);
00316         apps_shutdown();
00317         OPENSSL_EXIT(ret);
00318         }

© sourcejam.com 2005-2008