155714Skris/* apps/sess_id.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296465Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296465Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296465Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296465Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296465Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include <stdlib.h> 6155714Skris#include <string.h> 6255714Skris#include "apps.h" 6355714Skris#include <openssl/bio.h> 6455714Skris#include <openssl/err.h> 6555714Skris#include <openssl/x509.h> 6655714Skris#include <openssl/pem.h> 6755714Skris#include <openssl/ssl.h> 6855714Skris 6955714Skris#undef PROG 70296465Sdelphij#define PROG sess_id_main 7155714Skris 72296465Sdelphijstatic const char *sess_id_usage[] = { 73296465Sdelphij "usage: sess_id args\n", 74296465Sdelphij "\n", 75296465Sdelphij " -inform arg - input format - default PEM (DER or PEM)\n", 76296465Sdelphij " -outform arg - output format - default PEM\n", 77296465Sdelphij " -in arg - input file - default stdin\n", 78296465Sdelphij " -out arg - output file - default stdout\n", 79296465Sdelphij " -text - print ssl session id details\n", 80296465Sdelphij " -cert - output certificate \n", 81296465Sdelphij " -noout - no CRL output\n", 82296465Sdelphij " -context arg - set the session ID context\n", 83296465Sdelphij NULL 8455714Skris}; 8555714Skris 8655714Skrisstatic SSL_SESSION *load_sess_id(char *file, int format); 8759191Skris 8859191Skrisint MAIN(int, char **); 8959191Skris 9055714Skrisint MAIN(int argc, char **argv) 91296465Sdelphij{ 92296465Sdelphij SSL_SESSION *x = NULL; 93296465Sdelphij int ret = 1, i, num, badops = 0; 94296465Sdelphij BIO *out = NULL; 95296465Sdelphij int informat, outformat; 96296465Sdelphij char *infile = NULL, *outfile = NULL, *context = NULL; 97296465Sdelphij int cert = 0, noout = 0, text = 0; 98296465Sdelphij const char **pp; 9955714Skris 100296465Sdelphij apps_startup(); 10155714Skris 102296465Sdelphij if (bio_err == NULL) 103296465Sdelphij if ((bio_err = BIO_new(BIO_s_file())) != NULL) 104296465Sdelphij BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 10555714Skris 106296465Sdelphij informat = FORMAT_PEM; 107296465Sdelphij outformat = FORMAT_PEM; 10855714Skris 109296465Sdelphij argc--; 110296465Sdelphij argv++; 111296465Sdelphij num = 0; 112296465Sdelphij while (argc >= 1) { 113296465Sdelphij if (strcmp(*argv, "-inform") == 0) { 114296465Sdelphij if (--argc < 1) 115296465Sdelphij goto bad; 116296465Sdelphij informat = str2fmt(*(++argv)); 117296465Sdelphij } else if (strcmp(*argv, "-outform") == 0) { 118296465Sdelphij if (--argc < 1) 119296465Sdelphij goto bad; 120296465Sdelphij outformat = str2fmt(*(++argv)); 121296465Sdelphij } else if (strcmp(*argv, "-in") == 0) { 122296465Sdelphij if (--argc < 1) 123296465Sdelphij goto bad; 124296465Sdelphij infile = *(++argv); 125296465Sdelphij } else if (strcmp(*argv, "-out") == 0) { 126296465Sdelphij if (--argc < 1) 127296465Sdelphij goto bad; 128296465Sdelphij outfile = *(++argv); 129296465Sdelphij } else if (strcmp(*argv, "-text") == 0) 130296465Sdelphij text = ++num; 131296465Sdelphij else if (strcmp(*argv, "-cert") == 0) 132296465Sdelphij cert = ++num; 133296465Sdelphij else if (strcmp(*argv, "-noout") == 0) 134296465Sdelphij noout = ++num; 135296465Sdelphij else if (strcmp(*argv, "-context") == 0) { 136296465Sdelphij if (--argc < 1) 137296465Sdelphij goto bad; 138296465Sdelphij context = *++argv; 139296465Sdelphij } else { 140296465Sdelphij BIO_printf(bio_err, "unknown option %s\n", *argv); 141296465Sdelphij badops = 1; 142296465Sdelphij break; 143296465Sdelphij } 144296465Sdelphij argc--; 145296465Sdelphij argv++; 146296465Sdelphij } 14755714Skris 148296465Sdelphij if (badops) { 149296465Sdelphij bad: 150296465Sdelphij for (pp = sess_id_usage; (*pp != NULL); pp++) 151296465Sdelphij BIO_printf(bio_err, "%s", *pp); 152296465Sdelphij goto end; 153296465Sdelphij } 15455714Skris 155296465Sdelphij ERR_load_crypto_strings(); 156296465Sdelphij x = load_sess_id(infile, informat); 157296465Sdelphij if (x == NULL) { 158296465Sdelphij goto end; 159296465Sdelphij } 16055714Skris 161296465Sdelphij if (context) { 162296465Sdelphij x->sid_ctx_length = strlen(context); 163296465Sdelphij if (x->sid_ctx_length > SSL_MAX_SID_CTX_LENGTH) { 164296465Sdelphij BIO_printf(bio_err, "Context too long\n"); 165296465Sdelphij goto end; 166296465Sdelphij } 167296465Sdelphij memcpy(x->sid_ctx, context, x->sid_ctx_length); 168296465Sdelphij } 16955714Skris#ifdef undef 170296465Sdelphij /* just testing for memory leaks :-) */ 171296465Sdelphij { 172296465Sdelphij SSL_SESSION *s; 173296465Sdelphij char buf[1024 * 10], *p; 174296465Sdelphij int i; 17555714Skris 176296465Sdelphij s = SSL_SESSION_new(); 17755714Skris 178296465Sdelphij p = &buf; 179296465Sdelphij i = i2d_SSL_SESSION(x, &p); 180296465Sdelphij p = &buf; 181296465Sdelphij d2i_SSL_SESSION(&s, &p, (long)i); 182296465Sdelphij p = &buf; 183296465Sdelphij d2i_SSL_SESSION(&s, &p, (long)i); 184296465Sdelphij p = &buf; 185296465Sdelphij d2i_SSL_SESSION(&s, &p, (long)i); 186296465Sdelphij SSL_SESSION_free(s); 187296465Sdelphij } 18855714Skris#endif 18955714Skris 190296465Sdelphij if (!noout || text) { 191296465Sdelphij out = BIO_new(BIO_s_file()); 192296465Sdelphij if (out == NULL) { 193296465Sdelphij ERR_print_errors(bio_err); 194296465Sdelphij goto end; 195296465Sdelphij } 19655714Skris 197296465Sdelphij if (outfile == NULL) { 198296465Sdelphij BIO_set_fp(out, stdout, BIO_NOCLOSE); 199109998Smarkm#ifdef OPENSSL_SYS_VMS 200296465Sdelphij { 201296465Sdelphij BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 202296465Sdelphij out = BIO_push(tmpbio, out); 203296465Sdelphij } 20468651Skris#endif 205296465Sdelphij } else { 206296465Sdelphij if (BIO_write_filename(out, outfile) <= 0) { 207296465Sdelphij perror(outfile); 208296465Sdelphij goto end; 209296465Sdelphij } 210296465Sdelphij } 211296465Sdelphij } 21255714Skris 213296465Sdelphij if (text) { 214296465Sdelphij SSL_SESSION_print(out, x); 21555714Skris 216296465Sdelphij if (cert) { 217296465Sdelphij if (x->peer == NULL) 218296465Sdelphij BIO_puts(out, "No certificate present\n"); 219296465Sdelphij else 220296465Sdelphij X509_print(out, x->peer); 221296465Sdelphij } 222296465Sdelphij } 22355714Skris 224296465Sdelphij if (!noout && !cert) { 225296465Sdelphij if (outformat == FORMAT_ASN1) 226296465Sdelphij i = i2d_SSL_SESSION_bio(out, x); 227296465Sdelphij else if (outformat == FORMAT_PEM) 228296465Sdelphij i = PEM_write_bio_SSL_SESSION(out, x); 229296465Sdelphij else { 230296465Sdelphij BIO_printf(bio_err, "bad output format specified for outfile\n"); 231296465Sdelphij goto end; 232296465Sdelphij } 233296465Sdelphij if (!i) { 234296465Sdelphij BIO_printf(bio_err, "unable to write SSL_SESSION\n"); 235296465Sdelphij goto end; 236296465Sdelphij } 237296465Sdelphij } else if (!noout && (x->peer != NULL)) { /* just print the certificate */ 238296465Sdelphij if (outformat == FORMAT_ASN1) 239296465Sdelphij i = (int)i2d_X509_bio(out, x->peer); 240296465Sdelphij else if (outformat == FORMAT_PEM) 241296465Sdelphij i = PEM_write_bio_X509(out, x->peer); 242296465Sdelphij else { 243296465Sdelphij BIO_printf(bio_err, "bad output format specified for outfile\n"); 244296465Sdelphij goto end; 245296465Sdelphij } 246296465Sdelphij if (!i) { 247296465Sdelphij BIO_printf(bio_err, "unable to write X509\n"); 248296465Sdelphij goto end; 249296465Sdelphij } 250296465Sdelphij } 251296465Sdelphij ret = 0; 252296465Sdelphij end: 253296465Sdelphij if (out != NULL) 254296465Sdelphij BIO_free_all(out); 255296465Sdelphij if (x != NULL) 256296465Sdelphij SSL_SESSION_free(x); 257296465Sdelphij apps_shutdown(); 258296465Sdelphij OPENSSL_EXIT(ret); 259296465Sdelphij} 26055714Skris 26155714Skrisstatic SSL_SESSION *load_sess_id(char *infile, int format) 262296465Sdelphij{ 263296465Sdelphij SSL_SESSION *x = NULL; 264296465Sdelphij BIO *in = NULL; 26555714Skris 266296465Sdelphij in = BIO_new(BIO_s_file()); 267296465Sdelphij if (in == NULL) { 268296465Sdelphij ERR_print_errors(bio_err); 269296465Sdelphij goto end; 270296465Sdelphij } 27155714Skris 272296465Sdelphij if (infile == NULL) 273296465Sdelphij BIO_set_fp(in, stdin, BIO_NOCLOSE); 274296465Sdelphij else { 275296465Sdelphij if (BIO_read_filename(in, infile) <= 0) { 276296465Sdelphij perror(infile); 277296465Sdelphij goto end; 278296465Sdelphij } 279296465Sdelphij } 280296465Sdelphij if (format == FORMAT_ASN1) 281296465Sdelphij x = d2i_SSL_SESSION_bio(in, NULL); 282296465Sdelphij else if (format == FORMAT_PEM) 283296465Sdelphij x = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL); 284296465Sdelphij else { 285296465Sdelphij BIO_printf(bio_err, "bad input format specified for input crl\n"); 286296465Sdelphij goto end; 287296465Sdelphij } 288296465Sdelphij if (x == NULL) { 289296465Sdelphij BIO_printf(bio_err, "unable to load SSL_SESSION\n"); 290296465Sdelphij ERR_print_errors(bio_err); 291296465Sdelphij goto end; 292296465Sdelphij } 29355714Skris 294296465Sdelphij end: 295296465Sdelphij if (in != NULL) 296296465Sdelphij BIO_free(in); 297296465Sdelphij return (x); 298296465Sdelphij} 299