Home | History | Annotate | Line # | Download | only in apps
sess_id.c revision 1.1
      1  1.1  christos /* apps/sess_id.c */
      2  1.1  christos /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
      3  1.1  christos  * All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * This package is an SSL implementation written
      6  1.1  christos  * by Eric Young (eay (at) cryptsoft.com).
      7  1.1  christos  * The implementation was written so as to conform with Netscapes SSL.
      8  1.1  christos  *
      9  1.1  christos  * This library is free for commercial and non-commercial use as long as
     10  1.1  christos  * the following conditions are aheared to.  The following conditions
     11  1.1  christos  * apply to all code found in this distribution, be it the RC4, RSA,
     12  1.1  christos  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
     13  1.1  christos  * included with this distribution is covered by the same copyright terms
     14  1.1  christos  * except that the holder is Tim Hudson (tjh (at) cryptsoft.com).
     15  1.1  christos  *
     16  1.1  christos  * Copyright remains Eric Young's, and as such any Copyright notices in
     17  1.1  christos  * the code are not to be removed.
     18  1.1  christos  * If this package is used in a product, Eric Young should be given attribution
     19  1.1  christos  * as the author of the parts of the library used.
     20  1.1  christos  * This can be in the form of a textual message at program startup or
     21  1.1  christos  * in documentation (online or textual) provided with the package.
     22  1.1  christos  *
     23  1.1  christos  * Redistribution and use in source and binary forms, with or without
     24  1.1  christos  * modification, are permitted provided that the following conditions
     25  1.1  christos  * are met:
     26  1.1  christos  * 1. Redistributions of source code must retain the copyright
     27  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     28  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     29  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     30  1.1  christos  *    documentation and/or other materials provided with the distribution.
     31  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     32  1.1  christos  *    must display the following acknowledgement:
     33  1.1  christos  *    "This product includes cryptographic software written by
     34  1.1  christos  *     Eric Young (eay (at) cryptsoft.com)"
     35  1.1  christos  *    The word 'cryptographic' can be left out if the rouines from the library
     36  1.1  christos  *    being used are not cryptographic related :-).
     37  1.1  christos  * 4. If you include any Windows specific code (or a derivative thereof) from
     38  1.1  christos  *    the apps directory (application code) you must include an acknowledgement:
     39  1.1  christos  *    "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)"
     40  1.1  christos  *
     41  1.1  christos  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     42  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     43  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     44  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     45  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     46  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     47  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     48  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     49  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     50  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     51  1.1  christos  * SUCH DAMAGE.
     52  1.1  christos  *
     53  1.1  christos  * The licence and distribution terms for any publically available version or
     54  1.1  christos  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     55  1.1  christos  * copied and put under another distribution licence
     56  1.1  christos  * [including the GNU Public Licence.]
     57  1.1  christos  */
     58  1.1  christos 
     59  1.1  christos #include <stdio.h>
     60  1.1  christos #include <stdlib.h>
     61  1.1  christos #include <string.h>
     62  1.1  christos #include "apps.h"
     63  1.1  christos #include <openssl/bio.h>
     64  1.1  christos #include <openssl/err.h>
     65  1.1  christos #include <openssl/x509.h>
     66  1.1  christos #include <openssl/pem.h>
     67  1.1  christos #include <openssl/ssl.h>
     68  1.1  christos 
     69  1.1  christos #undef PROG
     70  1.1  christos #define PROG    sess_id_main
     71  1.1  christos 
     72  1.1  christos static const char *sess_id_usage[] = {
     73  1.1  christos     "usage: sess_id args\n",
     74  1.1  christos     "\n",
     75  1.1  christos     " -inform arg     - input format - default PEM (DER or PEM)\n",
     76  1.1  christos     " -outform arg    - output format - default PEM\n",
     77  1.1  christos     " -in arg         - input file - default stdin\n",
     78  1.1  christos     " -out arg        - output file - default stdout\n",
     79  1.1  christos     " -text           - print ssl session id details\n",
     80  1.1  christos     " -cert           - output certificate \n",
     81  1.1  christos     " -noout          - no CRL output\n",
     82  1.1  christos     " -context arg    - set the session ID context\n",
     83  1.1  christos     NULL
     84  1.1  christos };
     85  1.1  christos 
     86  1.1  christos static SSL_SESSION *load_sess_id(char *file, int format);
     87  1.1  christos 
     88  1.1  christos int MAIN(int, char **);
     89  1.1  christos 
     90  1.1  christos int MAIN(int argc, char **argv)
     91  1.1  christos {
     92  1.1  christos     SSL_SESSION *x = NULL;
     93  1.1  christos     X509 *peer = NULL;
     94  1.1  christos     int ret = 1, i, num, badops = 0;
     95  1.1  christos     BIO *out = NULL;
     96  1.1  christos     int informat, outformat;
     97  1.1  christos     char *infile = NULL, *outfile = NULL, *context = NULL;
     98  1.1  christos     int cert = 0, noout = 0, text = 0;
     99  1.1  christos     const char **pp;
    100  1.1  christos 
    101  1.1  christos     apps_startup();
    102  1.1  christos 
    103  1.1  christos     if (bio_err == NULL)
    104  1.1  christos         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
    105  1.1  christos             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
    106  1.1  christos 
    107  1.1  christos     informat = FORMAT_PEM;
    108  1.1  christos     outformat = FORMAT_PEM;
    109  1.1  christos 
    110  1.1  christos     argc--;
    111  1.1  christos     argv++;
    112  1.1  christos     num = 0;
    113  1.1  christos     while (argc >= 1) {
    114  1.1  christos         if (strcmp(*argv, "-inform") == 0) {
    115  1.1  christos             if (--argc < 1)
    116  1.1  christos                 goto bad;
    117  1.1  christos             informat = str2fmt(*(++argv));
    118  1.1  christos         } else if (strcmp(*argv, "-outform") == 0) {
    119  1.1  christos             if (--argc < 1)
    120  1.1  christos                 goto bad;
    121  1.1  christos             outformat = str2fmt(*(++argv));
    122  1.1  christos         } else if (strcmp(*argv, "-in") == 0) {
    123  1.1  christos             if (--argc < 1)
    124  1.1  christos                 goto bad;
    125  1.1  christos             infile = *(++argv);
    126  1.1  christos         } else if (strcmp(*argv, "-out") == 0) {
    127  1.1  christos             if (--argc < 1)
    128  1.1  christos                 goto bad;
    129  1.1  christos             outfile = *(++argv);
    130  1.1  christos         } else if (strcmp(*argv, "-text") == 0)
    131  1.1  christos             text = ++num;
    132  1.1  christos         else if (strcmp(*argv, "-cert") == 0)
    133  1.1  christos             cert = ++num;
    134  1.1  christos         else if (strcmp(*argv, "-noout") == 0)
    135  1.1  christos             noout = ++num;
    136  1.1  christos         else if (strcmp(*argv, "-context") == 0) {
    137  1.1  christos             if (--argc < 1)
    138  1.1  christos                 goto bad;
    139  1.1  christos             context = *++argv;
    140  1.1  christos         } else {
    141  1.1  christos             BIO_printf(bio_err, "unknown option %s\n", *argv);
    142  1.1  christos             badops = 1;
    143  1.1  christos             break;
    144  1.1  christos         }
    145  1.1  christos         argc--;
    146  1.1  christos         argv++;
    147  1.1  christos     }
    148  1.1  christos 
    149  1.1  christos     if (badops) {
    150  1.1  christos  bad:
    151  1.1  christos         for (pp = sess_id_usage; (*pp != NULL); pp++)
    152  1.1  christos             BIO_printf(bio_err, "%s", *pp);
    153  1.1  christos         goto end;
    154  1.1  christos     }
    155  1.1  christos 
    156  1.1  christos     ERR_load_crypto_strings();
    157  1.1  christos     x = load_sess_id(infile, informat);
    158  1.1  christos     if (x == NULL) {
    159  1.1  christos         goto end;
    160  1.1  christos     }
    161  1.1  christos     peer = SSL_SESSION_get0_peer(x);
    162  1.1  christos 
    163  1.1  christos     if (context) {
    164  1.1  christos         size_t ctx_len = strlen(context);
    165  1.1  christos         if (ctx_len > SSL_MAX_SID_CTX_LENGTH) {
    166  1.1  christos             BIO_printf(bio_err, "Context too long\n");
    167  1.1  christos             goto end;
    168  1.1  christos         }
    169  1.1  christos         SSL_SESSION_set1_id_context(x, (unsigned char *)context, ctx_len);
    170  1.1  christos     }
    171  1.1  christos #ifdef undef
    172  1.1  christos     /* just testing for memory leaks :-) */
    173  1.1  christos     {
    174  1.1  christos         SSL_SESSION *s;
    175  1.1  christos         char buf[1024 * 10], *p;
    176  1.1  christos         int i;
    177  1.1  christos 
    178  1.1  christos         s = SSL_SESSION_new();
    179  1.1  christos 
    180  1.1  christos         p = &buf;
    181  1.1  christos         i = i2d_SSL_SESSION(x, &p);
    182  1.1  christos         p = &buf;
    183  1.1  christos         d2i_SSL_SESSION(&s, &p, (long)i);
    184  1.1  christos         p = &buf;
    185  1.1  christos         d2i_SSL_SESSION(&s, &p, (long)i);
    186  1.1  christos         p = &buf;
    187  1.1  christos         d2i_SSL_SESSION(&s, &p, (long)i);
    188  1.1  christos         SSL_SESSION_free(s);
    189  1.1  christos     }
    190  1.1  christos #endif
    191  1.1  christos 
    192  1.1  christos     if (!noout || text) {
    193  1.1  christos         out = BIO_new(BIO_s_file());
    194  1.1  christos         if (out == NULL) {
    195  1.1  christos             ERR_print_errors(bio_err);
    196  1.1  christos             goto end;
    197  1.1  christos         }
    198  1.1  christos 
    199  1.1  christos         if (outfile == NULL) {
    200  1.1  christos             BIO_set_fp(out, stdout, BIO_NOCLOSE);
    201  1.1  christos #ifdef OPENSSL_SYS_VMS
    202  1.1  christos             {
    203  1.1  christos                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
    204  1.1  christos                 out = BIO_push(tmpbio, out);
    205  1.1  christos             }
    206  1.1  christos #endif
    207  1.1  christos         } else {
    208  1.1  christos             if (BIO_write_filename(out, outfile) <= 0) {
    209  1.1  christos                 perror(outfile);
    210  1.1  christos                 goto end;
    211  1.1  christos             }
    212  1.1  christos         }
    213  1.1  christos     }
    214  1.1  christos 
    215  1.1  christos     if (text) {
    216  1.1  christos         SSL_SESSION_print(out, x);
    217  1.1  christos 
    218  1.1  christos         if (cert) {
    219  1.1  christos             if (peer == NULL)
    220  1.1  christos                 BIO_puts(out, "No certificate present\n");
    221  1.1  christos             else
    222  1.1  christos                 X509_print(out, peer);
    223  1.1  christos         }
    224  1.1  christos     }
    225  1.1  christos 
    226  1.1  christos     if (!noout && !cert) {
    227  1.1  christos         if (outformat == FORMAT_ASN1)
    228  1.1  christos             i = i2d_SSL_SESSION_bio(out, x);
    229  1.1  christos         else if (outformat == FORMAT_PEM)
    230  1.1  christos             i = PEM_write_bio_SSL_SESSION(out, x);
    231  1.1  christos         else {
    232  1.1  christos             BIO_printf(bio_err, "bad output format specified for outfile\n");
    233  1.1  christos             goto end;
    234  1.1  christos         }
    235  1.1  christos         if (!i) {
    236  1.1  christos             BIO_printf(bio_err, "unable to write SSL_SESSION\n");
    237  1.1  christos             goto end;
    238  1.1  christos         }
    239  1.1  christos     } else if (!noout && (peer != NULL)) { /* just print the certificate */
    240  1.1  christos         if (outformat == FORMAT_ASN1)
    241  1.1  christos             i = (int)i2d_X509_bio(out, peer);
    242  1.1  christos         else if (outformat == FORMAT_PEM)
    243  1.1  christos             i = PEM_write_bio_X509(out, peer);
    244  1.1  christos         else {
    245  1.1  christos             BIO_printf(bio_err, "bad output format specified for outfile\n");
    246  1.1  christos             goto end;
    247  1.1  christos         }
    248  1.1  christos         if (!i) {
    249  1.1  christos             BIO_printf(bio_err, "unable to write X509\n");
    250  1.1  christos             goto end;
    251  1.1  christos         }
    252  1.1  christos     }
    253  1.1  christos     ret = 0;
    254  1.1  christos  end:
    255  1.1  christos     if (out != NULL)
    256  1.1  christos         BIO_free_all(out);
    257  1.1  christos     if (x != NULL)
    258  1.1  christos         SSL_SESSION_free(x);
    259  1.1  christos     apps_shutdown();
    260  1.1  christos     OPENSSL_EXIT(ret);
    261  1.1  christos }
    262  1.1  christos 
    263  1.1  christos static SSL_SESSION *load_sess_id(char *infile, int format)
    264  1.1  christos {
    265  1.1  christos     SSL_SESSION *x = NULL;
    266  1.1  christos     BIO *in = NULL;
    267  1.1  christos 
    268  1.1  christos     in = BIO_new(BIO_s_file());
    269  1.1  christos     if (in == NULL) {
    270  1.1  christos         ERR_print_errors(bio_err);
    271  1.1  christos         goto end;
    272  1.1  christos     }
    273  1.1  christos 
    274  1.1  christos     if (infile == NULL)
    275  1.1  christos         BIO_set_fp(in, stdin, BIO_NOCLOSE);
    276  1.1  christos     else {
    277  1.1  christos         if (BIO_read_filename(in, infile) <= 0) {
    278  1.1  christos             perror(infile);
    279  1.1  christos             goto end;
    280  1.1  christos         }
    281  1.1  christos     }
    282  1.1  christos     if (format == FORMAT_ASN1)
    283  1.1  christos         x = d2i_SSL_SESSION_bio(in, NULL);
    284  1.1  christos     else if (format == FORMAT_PEM)
    285  1.1  christos         x = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL);
    286  1.1  christos     else {
    287  1.1  christos         BIO_printf(bio_err, "bad input format specified for input crl\n");
    288  1.1  christos         goto end;
    289  1.1  christos     }
    290  1.1  christos     if (x == NULL) {
    291  1.1  christos         BIO_printf(bio_err, "unable to load SSL_SESSION\n");
    292  1.1  christos         ERR_print_errors(bio_err);
    293  1.1  christos         goto end;
    294  1.1  christos     }
    295  1.1  christos 
    296  1.1  christos  end:
    297  1.1  christos     if (in != NULL)
    298  1.1  christos         BIO_free(in);
    299  1.1  christos     return (x);
    300  1.1  christos }
    301