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.
855714Skris *
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).
1555714Skris *
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.
2255714Skris *
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 :-).
3755714Skris * 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)"
4055714Skris *
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.
5255714Skris *
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
7055714Skris#define PROG	sess_id_main
7155714Skris
72160814Ssimonstatic const char *sess_id_usage[]={
7355714Skris"usage: sess_id args\n",
7455714Skris"\n",
7559191Skris" -inform arg     - input format - default PEM (DER or PEM)\n",
7655714Skris" -outform arg    - output format - default PEM\n",
7755714Skris" -in arg         - input file - default stdin\n",
7855714Skris" -out arg        - output file - default stdout\n",
7955714Skris" -text           - print ssl session id details\n",
8055714Skris" -cert           - output certificate \n",
8155714Skris" -noout          - no CRL output\n",
8255714Skris" -context arg    - set the session ID context\n",
8355714SkrisNULL
8455714Skris};
8555714Skris
8655714Skrisstatic SSL_SESSION *load_sess_id(char *file, int format);
8759191Skris
8859191Skrisint MAIN(int, char **);
8959191Skris
9055714Skrisint MAIN(int argc, char **argv)
9155714Skris	{
9255714Skris	SSL_SESSION *x=NULL;
93238405Sjkim	X509 *peer = NULL;
9455714Skris	int ret=1,i,num,badops=0;
9555714Skris	BIO *out=NULL;
9655714Skris	int informat,outformat;
9755714Skris	char *infile=NULL,*outfile=NULL,*context=NULL;
9855714Skris	int cert=0,noout=0,text=0;
99160814Ssimon	const char **pp;
10055714Skris
10155714Skris	apps_startup();
10255714Skris
10355714Skris	if (bio_err == NULL)
10455714Skris		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
10555714Skris			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
10655714Skris
10755714Skris	informat=FORMAT_PEM;
10855714Skris	outformat=FORMAT_PEM;
10955714Skris
11055714Skris	argc--;
11155714Skris	argv++;
11255714Skris	num=0;
11355714Skris	while (argc >= 1)
11455714Skris		{
11555714Skris		if 	(strcmp(*argv,"-inform") == 0)
11655714Skris			{
11755714Skris			if (--argc < 1) goto bad;
11855714Skris			informat=str2fmt(*(++argv));
11955714Skris			}
12055714Skris		else if (strcmp(*argv,"-outform") == 0)
12155714Skris			{
12255714Skris			if (--argc < 1) goto bad;
12355714Skris			outformat=str2fmt(*(++argv));
12455714Skris			}
12555714Skris		else if (strcmp(*argv,"-in") == 0)
12655714Skris			{
12755714Skris			if (--argc < 1) goto bad;
12855714Skris			infile= *(++argv);
12955714Skris			}
13055714Skris		else if (strcmp(*argv,"-out") == 0)
13155714Skris			{
13255714Skris			if (--argc < 1) goto bad;
13355714Skris			outfile= *(++argv);
13455714Skris			}
13555714Skris		else if (strcmp(*argv,"-text") == 0)
13655714Skris			text= ++num;
13755714Skris		else if (strcmp(*argv,"-cert") == 0)
13855714Skris			cert= ++num;
13955714Skris		else if (strcmp(*argv,"-noout") == 0)
14055714Skris			noout= ++num;
14155714Skris		else if (strcmp(*argv,"-context") == 0)
14255714Skris		    {
14355714Skris		    if(--argc < 1) goto bad;
14455714Skris		    context=*++argv;
14555714Skris		    }
14655714Skris		else
14755714Skris			{
14855714Skris			BIO_printf(bio_err,"unknown option %s\n",*argv);
14955714Skris			badops=1;
15055714Skris			break;
15155714Skris			}
15255714Skris		argc--;
15355714Skris		argv++;
15455714Skris		}
15555714Skris
15655714Skris	if (badops)
15755714Skris		{
15855714Skrisbad:
15955714Skris		for (pp=sess_id_usage; (*pp != NULL); pp++)
160109998Smarkm			BIO_printf(bio_err,"%s",*pp);
16155714Skris		goto end;
16255714Skris		}
16355714Skris
16455714Skris	ERR_load_crypto_strings();
16555714Skris	x=load_sess_id(infile,informat);
16655714Skris	if (x == NULL) { goto end; }
167238405Sjkim	peer = SSL_SESSION_get0_peer(x);
16855714Skris
16955714Skris	if(context)
17055714Skris	    {
171238405Sjkim	    size_t ctx_len = strlen(context);
172238405Sjkim	    if(ctx_len > SSL_MAX_SID_CTX_LENGTH)
17355714Skris		{
17455714Skris		BIO_printf(bio_err,"Context too long\n");
17555714Skris		goto end;
17655714Skris		}
177238405Sjkim	    SSL_SESSION_set1_id_context(x, (unsigned char *)context, ctx_len);
17855714Skris	    }
17955714Skris
18055714Skris#ifdef undef
18155714Skris	/* just testing for memory leaks :-) */
18255714Skris	{
18355714Skris	SSL_SESSION *s;
18455714Skris	char buf[1024*10],*p;
18555714Skris	int i;
18655714Skris
18755714Skris	s=SSL_SESSION_new();
18855714Skris
18955714Skris	p= &buf;
19055714Skris	i=i2d_SSL_SESSION(x,&p);
19155714Skris	p= &buf;
19255714Skris	d2i_SSL_SESSION(&s,&p,(long)i);
19355714Skris	p= &buf;
19455714Skris	d2i_SSL_SESSION(&s,&p,(long)i);
19555714Skris	p= &buf;
19655714Skris	d2i_SSL_SESSION(&s,&p,(long)i);
19755714Skris	SSL_SESSION_free(s);
19855714Skris	}
19955714Skris#endif
20055714Skris
20155714Skris	if (!noout || text)
20255714Skris		{
20355714Skris		out=BIO_new(BIO_s_file());
20455714Skris		if (out == NULL)
20555714Skris			{
20655714Skris			ERR_print_errors(bio_err);
20755714Skris			goto end;
20855714Skris			}
20955714Skris
21055714Skris		if (outfile == NULL)
21168651Skris			{
21255714Skris			BIO_set_fp(out,stdout,BIO_NOCLOSE);
213109998Smarkm#ifdef OPENSSL_SYS_VMS
21468651Skris			{
21568651Skris			BIO *tmpbio = BIO_new(BIO_f_linebuffer());
21668651Skris			out = BIO_push(tmpbio, out);
21768651Skris			}
21868651Skris#endif
21968651Skris			}
22055714Skris		else
22155714Skris			{
22255714Skris			if (BIO_write_filename(out,outfile) <= 0)
22355714Skris				{
22455714Skris				perror(outfile);
22555714Skris				goto end;
22655714Skris				}
22755714Skris			}
22855714Skris		}
22955714Skris
23055714Skris	if (text)
23155714Skris		{
23255714Skris		SSL_SESSION_print(out,x);
23355714Skris
23455714Skris		if (cert)
23555714Skris			{
236238405Sjkim			if (peer == NULL)
23755714Skris				BIO_puts(out,"No certificate present\n");
23855714Skris			else
239238405Sjkim				X509_print(out,peer);
24055714Skris			}
24155714Skris		}
24255714Skris
24355714Skris	if (!noout && !cert)
24455714Skris		{
24555714Skris		if 	(outformat == FORMAT_ASN1)
246160814Ssimon			i=i2d_SSL_SESSION_bio(out,x);
24755714Skris		else if (outformat == FORMAT_PEM)
24855714Skris			i=PEM_write_bio_SSL_SESSION(out,x);
24955714Skris		else	{
25055714Skris			BIO_printf(bio_err,"bad output format specified for outfile\n");
25155714Skris			goto end;
25255714Skris			}
25355714Skris		if (!i) {
25455714Skris			BIO_printf(bio_err,"unable to write SSL_SESSION\n");
25555714Skris			goto end;
25655714Skris			}
25755714Skris		}
258238405Sjkim	else if (!noout && (peer != NULL)) /* just print the certificate */
25955714Skris		{
26055714Skris		if 	(outformat == FORMAT_ASN1)
261238405Sjkim			i=(int)i2d_X509_bio(out,peer);
26255714Skris		else if (outformat == FORMAT_PEM)
263238405Sjkim			i=PEM_write_bio_X509(out,peer);
26455714Skris		else	{
26555714Skris			BIO_printf(bio_err,"bad output format specified for outfile\n");
26655714Skris			goto end;
26755714Skris			}
26855714Skris		if (!i) {
26955714Skris			BIO_printf(bio_err,"unable to write X509\n");
27055714Skris			goto end;
27155714Skris			}
27255714Skris		}
27355714Skris	ret=0;
27455714Skrisend:
27568651Skris	if (out != NULL) BIO_free_all(out);
27655714Skris	if (x != NULL) SSL_SESSION_free(x);
277109998Smarkm	apps_shutdown();
278109998Smarkm	OPENSSL_EXIT(ret);
27955714Skris	}
28055714Skris
28155714Skrisstatic SSL_SESSION *load_sess_id(char *infile, int format)
28255714Skris	{
28355714Skris	SSL_SESSION *x=NULL;
28455714Skris	BIO *in=NULL;
28555714Skris
28655714Skris	in=BIO_new(BIO_s_file());
28755714Skris	if (in == NULL)
28855714Skris		{
28955714Skris		ERR_print_errors(bio_err);
29055714Skris		goto end;
29155714Skris		}
29255714Skris
29355714Skris	if (infile == NULL)
29455714Skris		BIO_set_fp(in,stdin,BIO_NOCLOSE);
29555714Skris	else
29655714Skris		{
29755714Skris		if (BIO_read_filename(in,infile) <= 0)
29855714Skris			{
29955714Skris			perror(infile);
30055714Skris			goto end;
30155714Skris			}
30255714Skris		}
30355714Skris	if 	(format == FORMAT_ASN1)
30455714Skris		x=d2i_SSL_SESSION_bio(in,NULL);
30555714Skris	else if (format == FORMAT_PEM)
30655714Skris		x=PEM_read_bio_SSL_SESSION(in,NULL,NULL,NULL);
30755714Skris	else	{
30855714Skris		BIO_printf(bio_err,"bad input format specified for input crl\n");
30955714Skris		goto end;
31055714Skris		}
31155714Skris	if (x == NULL)
31255714Skris		{
31355714Skris		BIO_printf(bio_err,"unable to load SSL_SESSION\n");
31455714Skris		ERR_print_errors(bio_err);
31555714Skris		goto end;
31655714Skris		}
31755714Skris
31855714Skrisend:
31955714Skris	if (in != NULL) BIO_free(in);
32055714Skris	return(x);
32155714Skris	}
32255714Skris
323