1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/BC/libcrypto/topenssl/src/sess_id.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,320 @@
1.4 +/* apps/sess_id.c */
1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
1.6 + * All rights reserved.
1.7 + *
1.8 + * This package is an SSL implementation written
1.9 + * by Eric Young (eay@cryptsoft.com).
1.10 + * The implementation was written so as to conform with Netscapes SSL.
1.11 + *
1.12 + * This library is free for commercial and non-commercial use as long as
1.13 + * the following conditions are aheared to. The following conditions
1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
1.15 + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
1.16 + * included with this distribution is covered by the same copyright terms
1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1.18 + *
1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
1.20 + * the code are not to be removed.
1.21 + * If this package is used in a product, Eric Young should be given attribution
1.22 + * as the author of the parts of the library used.
1.23 + * This can be in the form of a textual message at program startup or
1.24 + * in documentation (online or textual) provided with the package.
1.25 + *
1.26 + * Redistribution and use in source and binary forms, with or without
1.27 + * modification, are permitted provided that the following conditions
1.28 + * are met:
1.29 + * 1. Redistributions of source code must retain the copyright
1.30 + * notice, this list of conditions and the following disclaimer.
1.31 + * 2. Redistributions in binary form must reproduce the above copyright
1.32 + * notice, this list of conditions and the following disclaimer in the
1.33 + * documentation and/or other materials provided with the distribution.
1.34 + * 3. All advertising materials mentioning features or use of this software
1.35 + * must display the following acknowledgement:
1.36 + * "This product includes cryptographic software written by
1.37 + * Eric Young (eay@cryptsoft.com)"
1.38 + * The word 'cryptographic' can be left out if the rouines from the library
1.39 + * being used are not cryptographic related :-).
1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from
1.41 + * the apps directory (application code) you must include an acknowledgement:
1.42 + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
1.43 + *
1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.47 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.54 + * SUCH DAMAGE.
1.55 + *
1.56 + * The licence and distribution terms for any publically available version or
1.57 + * derivative of this code cannot be changed. i.e. this code cannot simply be
1.58 + * copied and put under another distribution licence
1.59 + * [including the GNU Public Licence.]
1.60 + */
1.61 +
1.62 +#include <stdio.h>
1.63 +#include <stdlib.h>
1.64 +#include <string.h>
1.65 +#include "apps.h"
1.66 +#include <openssl/bio.h>
1.67 +#include <openssl/err.h>
1.68 +#include <openssl/x509.h>
1.69 +#include <openssl/pem.h>
1.70 +#include <openssl/ssl.h>
1.71 +
1.72 +#undef PROG
1.73 +#define PROG sess_id_main
1.74 +
1.75 +static const char *sess_id_usage[]={
1.76 +"usage: sess_id args\n",
1.77 +"\n",
1.78 +" -inform arg - input format - default PEM (DER or PEM)\n",
1.79 +" -outform arg - output format - default PEM\n",
1.80 +" -in arg - input file - default stdin\n",
1.81 +" -out arg - output file - default stdout\n",
1.82 +" -text - print ssl session id details\n",
1.83 +" -cert - output certificate \n",
1.84 +" -noout - no CRL output\n",
1.85 +" -context arg - set the session ID context\n",
1.86 +NULL
1.87 +};
1.88 +
1.89 +static SSL_SESSION *load_sess_id(char *file, int format);
1.90 +
1.91 +int MAIN(int, char **);
1.92 +
1.93 +int MAIN(int argc, char **argv)
1.94 + {
1.95 + SSL_SESSION *x=NULL;
1.96 + int ret=1,i,num,badops=0;
1.97 + BIO *out=NULL;
1.98 + int informat,outformat;
1.99 + char *infile=NULL,*outfile=NULL,*context=NULL;
1.100 + int cert=0,noout=0,text=0;
1.101 + const char **pp;
1.102 +
1.103 + apps_startup();
1.104 +
1.105 + if (bio_err == NULL)
1.106 + if ((bio_err=BIO_new(BIO_s_file())) != NULL)
1.107 + BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
1.108 + informat=FORMAT_PEM;
1.109 + outformat=FORMAT_PEM;
1.110 +
1.111 + argc--;
1.112 + argv++;
1.113 + num=0;
1.114 + while (argc >= 1)
1.115 + {
1.116 + if (strcmp(*argv,"-inform") == 0)
1.117 + {
1.118 + if (--argc < 1) goto bad;
1.119 + informat=str2fmt(*(++argv));
1.120 + }
1.121 + else if (strcmp(*argv,"-outform") == 0)
1.122 + {
1.123 + if (--argc < 1) goto bad;
1.124 + outformat=str2fmt(*(++argv));
1.125 + }
1.126 + else if (strcmp(*argv,"-in") == 0)
1.127 + {
1.128 + if (--argc < 1) goto bad;
1.129 + infile= *(++argv);
1.130 + }
1.131 + else if (strcmp(*argv,"-out") == 0)
1.132 + {
1.133 + if (--argc < 1) goto bad;
1.134 + outfile= *(++argv);
1.135 + }
1.136 + else if (strcmp(*argv,"-text") == 0)
1.137 + text= ++num;
1.138 + else if (strcmp(*argv,"-cert") == 0)
1.139 + cert= ++num;
1.140 + else if (strcmp(*argv,"-noout") == 0)
1.141 + noout= ++num;
1.142 + else if (strcmp(*argv,"-context") == 0)
1.143 + {
1.144 + if(--argc < 1) goto bad;
1.145 + context=*++argv;
1.146 + }
1.147 + else
1.148 + {
1.149 + BIO_printf(bio_err,"unknown option %s\n",*argv);
1.150 + badops=1;
1.151 + break;
1.152 + }
1.153 + argc--;
1.154 + argv++;
1.155 + }
1.156 +
1.157 + if (badops)
1.158 + {
1.159 +bad:
1.160 + for (pp=sess_id_usage; (*pp != NULL); pp++)
1.161 + BIO_printf(bio_err,"%s",*pp);
1.162 + goto end;
1.163 + }
1.164 +
1.165 + ERR_load_crypto_strings();
1.166 + x=load_sess_id(infile,informat);
1.167 + if (x == NULL) { goto end; }
1.168 +
1.169 + if(context)
1.170 + {
1.171 + x->sid_ctx_length=strlen(context);
1.172 + if(x->sid_ctx_length > SSL_MAX_SID_CTX_LENGTH)
1.173 + {
1.174 + BIO_printf(bio_err,"Context too long\n");
1.175 + goto end;
1.176 + }
1.177 + memcpy(x->sid_ctx,context,x->sid_ctx_length);
1.178 + }
1.179 +
1.180 +#ifdef undef
1.181 + /* just testing for memory leaks :-) */
1.182 + {
1.183 + SSL_SESSION *s;
1.184 + char buf[1024*10],*p;
1.185 + int i;
1.186 +
1.187 + s=SSL_SESSION_new();
1.188 +
1.189 + p= &buf;
1.190 + i=i2d_SSL_SESSION(x,&p);
1.191 + p= &buf;
1.192 + d2i_SSL_SESSION(&s,&p,(long)i);
1.193 + p= &buf;
1.194 + d2i_SSL_SESSION(&s,&p,(long)i);
1.195 + p= &buf;
1.196 + d2i_SSL_SESSION(&s,&p,(long)i);
1.197 + SSL_SESSION_free(s);
1.198 + }
1.199 +#endif
1.200 +
1.201 + if (!noout || text)
1.202 + {
1.203 + out=BIO_new(BIO_s_file());
1.204 + if (out == NULL)
1.205 + {
1.206 + ERR_print_errors(bio_err);
1.207 + goto end;
1.208 + }
1.209 +
1.210 + if (outfile == NULL)
1.211 + {
1.212 + BIO_set_fp(out,stdout,BIO_NOCLOSE);
1.213 +#ifdef OPENSSL_SYS_VMS
1.214 + {
1.215 + BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1.216 + out = BIO_push(tmpbio, out);
1.217 + }
1.218 +#endif
1.219 + }
1.220 + else
1.221 + {
1.222 + if (BIO_write_filename(out,outfile) <= 0)
1.223 + {
1.224 + perror(outfile);
1.225 + goto end;
1.226 + }
1.227 + }
1.228 + }
1.229 +
1.230 + if (text)
1.231 + {
1.232 + SSL_SESSION_print(out,x);
1.233 +
1.234 + if (cert)
1.235 + {
1.236 + if (x->peer == NULL)
1.237 + BIO_puts(out,"No certificate present\n");
1.238 + else
1.239 + X509_print(out,x->peer);
1.240 + }
1.241 + }
1.242 +
1.243 + if (!noout && !cert)
1.244 + {
1.245 + if (outformat == FORMAT_ASN1)
1.246 + i=i2d_SSL_SESSION_bio(out,x);
1.247 + else if (outformat == FORMAT_PEM)
1.248 + i=PEM_write_bio_SSL_SESSION(out,x);
1.249 + else {
1.250 + BIO_printf(bio_err,"bad output format specified for outfile\n");
1.251 + goto end;
1.252 + }
1.253 + if (!i) {
1.254 + BIO_printf(bio_err,"unable to write SSL_SESSION\n");
1.255 + goto end;
1.256 + }
1.257 + }
1.258 + else if (!noout && (x->peer != NULL)) /* just print the certificate */
1.259 + {
1.260 + if (outformat == FORMAT_ASN1)
1.261 + i=(int)i2d_X509_bio(out,x->peer);
1.262 + else if (outformat == FORMAT_PEM)
1.263 + i=PEM_write_bio_X509(out,x->peer);
1.264 + else {
1.265 + BIO_printf(bio_err,"bad output format specified for outfile\n");
1.266 + goto end;
1.267 + }
1.268 + if (!i) {
1.269 + BIO_printf(bio_err,"unable to write X509\n");
1.270 + goto end;
1.271 + }
1.272 + }
1.273 + ret=0;
1.274 +end:
1.275 + if (out != NULL) BIO_free_all(out);
1.276 + if (x != NULL) SSL_SESSION_free(x);
1.277 + apps_shutdown();
1.278 + OPENSSL_EXIT(ret);
1.279 + }
1.280 +
1.281 +static SSL_SESSION *load_sess_id(char *infile, int format)
1.282 + {
1.283 + SSL_SESSION *x=NULL;
1.284 + BIO *in=NULL;
1.285 +
1.286 + in=BIO_new(BIO_s_file());
1.287 + if (in == NULL)
1.288 + {
1.289 + ERR_print_errors(bio_err);
1.290 + goto end;
1.291 + }
1.292 +
1.293 + if (infile == NULL)
1.294 + BIO_set_fp(in,stdin,BIO_NOCLOSE);
1.295 +
1.296 + else
1.297 + {
1.298 + if (BIO_read_filename(in,infile) <= 0)
1.299 + {
1.300 + perror(infile);
1.301 + goto end;
1.302 + }
1.303 + }
1.304 + if (format == FORMAT_ASN1)
1.305 + x=d2i_SSL_SESSION_bio(in,NULL);
1.306 + else if (format == FORMAT_PEM)
1.307 + x=PEM_read_bio_SSL_SESSION(in,NULL,NULL,NULL);
1.308 + else {
1.309 + BIO_printf(bio_err,"bad input format specified for input crl\n");
1.310 + goto end;
1.311 + }
1.312 + if (x == NULL)
1.313 + {
1.314 + BIO_printf(bio_err,"unable to load SSL_SESSION\n");
1.315 + ERR_print_errors(bio_err);
1.316 + goto end;
1.317 + }
1.318 +
1.319 +end:
1.320 + if (in != NULL) BIO_free(in);
1.321 + return(x);
1.322 + }
1.323 +