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 "apps.h"
00063 #include <openssl/bio.h>
00064 #include <openssl/err.h>
00065 #include <openssl/x509.h>
00066 #include <openssl/pem.h>
00067 #include <openssl/ssl.h>
00068
00069 #undef PROG
00070 #define PROG sess_id_main
00071
00072 static const char *sess_id_usage[]={
00073 "usage: sess_id args\n",
00074 "\n",
00075 " -inform arg - input format - default PEM (DER or PEM)\n",
00076 " -outform arg - output format - default PEM\n",
00077 " -in arg - input file - default stdin\n",
00078 " -out arg - output file - default stdout\n",
00079 " -text - print ssl session id details\n",
00080 " -cert - output certificate \n",
00081 " -noout - no CRL output\n",
00082 " -context arg - set the session ID context\n",
00083 NULL
00084 };
00085
00086 static SSL_SESSION *load_sess_id(char *file, int format);
00087
00088 int MAIN(int, char **);
00089
00090 int MAIN(int argc, char **argv)
00091 {
00092 SSL_SESSION *x=NULL;
00093 int ret=1,i,num,badops=0;
00094 BIO *out=NULL;
00095 int informat,outformat;
00096 char *infile=NULL,*outfile=NULL,*context=NULL;
00097 int cert=0,noout=0,text=0;
00098 const char **pp;
00099
00100 apps_startup();
00101
00102 if (bio_err == NULL)
00103 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
00104 BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
00105
00106 informat=FORMAT_PEM;
00107 outformat=FORMAT_PEM;
00108
00109 argc--;
00110 argv++;
00111 num=0;
00112 while (argc >= 1)
00113 {
00114 if (strcmp(*argv,"-inform") == 0)
00115 {
00116 if (--argc < 1) goto bad;
00117 informat=str2fmt(*(++argv));
00118 }
00119 else if (strcmp(*argv,"-outform") == 0)
00120 {
00121 if (--argc < 1) goto bad;
00122 outformat=str2fmt(*(++argv));
00123 }
00124 else if (strcmp(*argv,"-in") == 0)
00125 {
00126 if (--argc < 1) goto bad;
00127 infile= *(++argv);
00128 }
00129 else if (strcmp(*argv,"-out") == 0)
00130 {
00131 if (--argc < 1) goto bad;
00132 outfile= *(++argv);
00133 }
00134 else if (strcmp(*argv,"-text") == 0)
00135 text= ++num;
00136 else if (strcmp(*argv,"-cert") == 0)
00137 cert= ++num;
00138 else if (strcmp(*argv,"-noout") == 0)
00139 noout= ++num;
00140 else if (strcmp(*argv,"-context") == 0)
00141 {
00142 if(--argc < 1) goto bad;
00143 context=*++argv;
00144 }
00145 else
00146 {
00147 BIO_printf(bio_err,"unknown option %s\n",*argv);
00148 badops=1;
00149 break;
00150 }
00151 argc--;
00152 argv++;
00153 }
00154
00155 if (badops)
00156 {
00157 bad:
00158 for (pp=sess_id_usage; (*pp != NULL); pp++)
00159 BIO_printf(bio_err,"%s",*pp);
00160 goto end;
00161 }
00162
00163 ERR_load_crypto_strings();
00164 x=load_sess_id(infile,informat);
00165 if (x == NULL) { goto end; }
00166
00167 if(context)
00168 {
00169 x->sid_ctx_length=strlen(context);
00170 if(x->sid_ctx_length > SSL_MAX_SID_CTX_LENGTH)
00171 {
00172 BIO_printf(bio_err,"Context too long\n");
00173 goto end;
00174 }
00175 memcpy(x->sid_ctx,context,x->sid_ctx_length);
00176 }
00177
00178 #ifdef undef
00179
00180 {
00181 SSL_SESSION *s;
00182 char buf[1024*10],*p;
00183 int i;
00184
00185 s=SSL_SESSION_new();
00186
00187 p= &buf;
00188 i=i2d_SSL_SESSION(x,&p);
00189 p= &buf;
00190 d2i_SSL_SESSION(&s,&p,(long)i);
00191 p= &buf;
00192 d2i_SSL_SESSION(&s,&p,(long)i);
00193 p= &buf;
00194 d2i_SSL_SESSION(&s,&p,(long)i);
00195 SSL_SESSION_free(s);
00196 }
00197 #endif
00198
00199 if (!noout || text)
00200 {
00201 out=BIO_new(BIO_s_file());
00202 if (out == NULL)
00203 {
00204 ERR_print_errors(bio_err);
00205 goto end;
00206 }
00207
00208 if (outfile == NULL)
00209 {
00210 BIO_set_fp(out,stdout,BIO_NOCLOSE);
00211 #ifdef OPENSSL_SYS_VMS
00212 {
00213 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
00214 out = BIO_push(tmpbio, out);
00215 }
00216 #endif
00217 }
00218 else
00219 {
00220 if (BIO_write_filename(out,outfile) <= 0)
00221 {
00222 perror(outfile);
00223 goto end;
00224 }
00225 }
00226 }
00227
00228 if (text)
00229 {
00230 SSL_SESSION_print(out,x);
00231
00232 if (cert)
00233 {
00234 if (x->peer == NULL)
00235 BIO_puts(out,"No certificate present\n");
00236 else
00237 X509_print(out,x->peer);
00238 }
00239 }
00240
00241 if (!noout && !cert)
00242 {
00243 if (outformat == FORMAT_ASN1)
00244 i=i2d_SSL_SESSION_bio(out,x);
00245 else if (outformat == FORMAT_PEM)
00246 i=PEM_write_bio_SSL_SESSION(out,x);
00247 else {
00248 BIO_printf(bio_err,"bad output format specified for outfile\n");
00249 goto end;
00250 }
00251 if (!i) {
00252 BIO_printf(bio_err,"unable to write SSL_SESSION\n");
00253 goto end;
00254 }
00255 }
00256 else if (!noout && (x->peer != NULL))
00257 {
00258 if (outformat == FORMAT_ASN1)
00259 i=(int)i2d_X509_bio(out,x->peer);
00260 else if (outformat == FORMAT_PEM)
00261 i=PEM_write_bio_X509(out,x->peer);
00262 else {
00263 BIO_printf(bio_err,"bad output format specified for outfile\n");
00264 goto end;
00265 }
00266 if (!i) {
00267 BIO_printf(bio_err,"unable to write X509\n");
00268 goto end;
00269 }
00270 }
00271 ret=0;
00272 end:
00273 if (out != NULL) BIO_free_all(out);
00274 if (x != NULL) SSL_SESSION_free(x);
00275 apps_shutdown();
00276 OPENSSL_EXIT(ret);
00277 }
00278
00279 static SSL_SESSION *load_sess_id(char *infile, int format)
00280 {
00281 SSL_SESSION *x=NULL;
00282 BIO *in=NULL;
00283
00284 in=BIO_new(BIO_s_file());
00285 if (in == NULL)
00286 {
00287 ERR_print_errors(bio_err);
00288 goto end;
00289 }
00290
00291 if (infile == NULL)
00292 BIO_set_fp(in,stdin,BIO_NOCLOSE);
00293 else
00294 {
00295 if (BIO_read_filename(in,infile) <= 0)
00296 {
00297 perror(infile);
00298 goto end;
00299 }
00300 }
00301 if (format == FORMAT_ASN1)
00302 x=d2i_SSL_SESSION_bio(in,NULL);
00303 else if (format == FORMAT_PEM)
00304 x=PEM_read_bio_SSL_SESSION(in,NULL,NULL,NULL);
00305 else {
00306 BIO_printf(bio_err,"bad input format specified for input crl\n");
00307 goto end;
00308 }
00309 if (x == NULL)
00310 {
00311 BIO_printf(bio_err,"unable to load SSL_SESSION\n");
00312 ERR_print_errors(bio_err);
00313 goto end;
00314 }
00315
00316 end:
00317 if (in != NULL) BIO_free(in);
00318 return(x);
00319 }
00320