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

sess_id.c

Go to the documentation of this file.
00001 /* apps/sess_id.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 "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         /* just testing for memory leaks :-) */
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)) /* just print the certificate */
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 

© sourcejam.com 2005-2008