00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
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
00075
00076
00077
00078
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 }