xref: /onnv-gate/usr/src/common/openssl/apps/sess_id.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* apps/sess_id.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <stdlib.h>
610Sstevel@tonic-gate #include <string.h>
620Sstevel@tonic-gate #include "apps.h"
630Sstevel@tonic-gate #include <openssl/bio.h>
640Sstevel@tonic-gate #include <openssl/err.h>
650Sstevel@tonic-gate #include <openssl/x509.h>
660Sstevel@tonic-gate #include <openssl/pem.h>
670Sstevel@tonic-gate #include <openssl/ssl.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #undef PROG
700Sstevel@tonic-gate #define PROG	sess_id_main
710Sstevel@tonic-gate 
72*2139Sjp161948 static const char *sess_id_usage[]={
730Sstevel@tonic-gate "usage: sess_id args\n",
740Sstevel@tonic-gate "\n",
750Sstevel@tonic-gate " -inform arg     - input format - default PEM (DER or PEM)\n",
760Sstevel@tonic-gate " -outform arg    - output format - default PEM\n",
770Sstevel@tonic-gate " -in arg         - input file - default stdin\n",
780Sstevel@tonic-gate " -out arg        - output file - default stdout\n",
790Sstevel@tonic-gate " -text           - print ssl session id details\n",
800Sstevel@tonic-gate " -cert           - output certificate \n",
810Sstevel@tonic-gate " -noout          - no CRL output\n",
820Sstevel@tonic-gate " -context arg    - set the session ID context\n",
830Sstevel@tonic-gate NULL
840Sstevel@tonic-gate };
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static SSL_SESSION *load_sess_id(char *file, int format);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate int MAIN(int, char **);
890Sstevel@tonic-gate 
MAIN(int argc,char ** argv)900Sstevel@tonic-gate int MAIN(int argc, char **argv)
910Sstevel@tonic-gate 	{
920Sstevel@tonic-gate 	SSL_SESSION *x=NULL;
930Sstevel@tonic-gate 	int ret=1,i,num,badops=0;
940Sstevel@tonic-gate 	BIO *out=NULL;
950Sstevel@tonic-gate 	int informat,outformat;
960Sstevel@tonic-gate 	char *infile=NULL,*outfile=NULL,*context=NULL;
970Sstevel@tonic-gate 	int cert=0,noout=0,text=0;
98*2139Sjp161948 	const char **pp;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	apps_startup();
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	if (bio_err == NULL)
1030Sstevel@tonic-gate 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
1040Sstevel@tonic-gate 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	informat=FORMAT_PEM;
1070Sstevel@tonic-gate 	outformat=FORMAT_PEM;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	argc--;
1100Sstevel@tonic-gate 	argv++;
1110Sstevel@tonic-gate 	num=0;
1120Sstevel@tonic-gate 	while (argc >= 1)
1130Sstevel@tonic-gate 		{
1140Sstevel@tonic-gate 		if 	(strcmp(*argv,"-inform") == 0)
1150Sstevel@tonic-gate 			{
1160Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1170Sstevel@tonic-gate 			informat=str2fmt(*(++argv));
1180Sstevel@tonic-gate 			}
1190Sstevel@tonic-gate 		else if (strcmp(*argv,"-outform") == 0)
1200Sstevel@tonic-gate 			{
1210Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1220Sstevel@tonic-gate 			outformat=str2fmt(*(++argv));
1230Sstevel@tonic-gate 			}
1240Sstevel@tonic-gate 		else if (strcmp(*argv,"-in") == 0)
1250Sstevel@tonic-gate 			{
1260Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1270Sstevel@tonic-gate 			infile= *(++argv);
1280Sstevel@tonic-gate 			}
1290Sstevel@tonic-gate 		else if (strcmp(*argv,"-out") == 0)
1300Sstevel@tonic-gate 			{
1310Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1320Sstevel@tonic-gate 			outfile= *(++argv);
1330Sstevel@tonic-gate 			}
1340Sstevel@tonic-gate 		else if (strcmp(*argv,"-text") == 0)
1350Sstevel@tonic-gate 			text= ++num;
1360Sstevel@tonic-gate 		else if (strcmp(*argv,"-cert") == 0)
1370Sstevel@tonic-gate 			cert= ++num;
1380Sstevel@tonic-gate 		else if (strcmp(*argv,"-noout") == 0)
1390Sstevel@tonic-gate 			noout= ++num;
1400Sstevel@tonic-gate 		else if (strcmp(*argv,"-context") == 0)
1410Sstevel@tonic-gate 		    {
1420Sstevel@tonic-gate 		    if(--argc < 1) goto bad;
1430Sstevel@tonic-gate 		    context=*++argv;
1440Sstevel@tonic-gate 		    }
1450Sstevel@tonic-gate 		else
1460Sstevel@tonic-gate 			{
1470Sstevel@tonic-gate 			BIO_printf(bio_err,"unknown option %s\n",*argv);
1480Sstevel@tonic-gate 			badops=1;
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 			}
1510Sstevel@tonic-gate 		argc--;
1520Sstevel@tonic-gate 		argv++;
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (badops)
1560Sstevel@tonic-gate 		{
1570Sstevel@tonic-gate bad:
1580Sstevel@tonic-gate 		for (pp=sess_id_usage; (*pp != NULL); pp++)
1590Sstevel@tonic-gate 			BIO_printf(bio_err,"%s",*pp);
1600Sstevel@tonic-gate 		goto end;
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	ERR_load_crypto_strings();
1640Sstevel@tonic-gate 	x=load_sess_id(infile,informat);
1650Sstevel@tonic-gate 	if (x == NULL) { goto end; }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if(context)
1680Sstevel@tonic-gate 	    {
1690Sstevel@tonic-gate 	    x->sid_ctx_length=strlen(context);
1700Sstevel@tonic-gate 	    if(x->sid_ctx_length > SSL_MAX_SID_CTX_LENGTH)
1710Sstevel@tonic-gate 		{
1720Sstevel@tonic-gate 		BIO_printf(bio_err,"Context too long\n");
1730Sstevel@tonic-gate 		goto end;
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 	    memcpy(x->sid_ctx,context,x->sid_ctx_length);
1760Sstevel@tonic-gate 	    }
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate #ifdef undef
1790Sstevel@tonic-gate 	/* just testing for memory leaks :-) */
1800Sstevel@tonic-gate 	{
1810Sstevel@tonic-gate 	SSL_SESSION *s;
1820Sstevel@tonic-gate 	char buf[1024*10],*p;
1830Sstevel@tonic-gate 	int i;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	s=SSL_SESSION_new();
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	p= &buf;
1880Sstevel@tonic-gate 	i=i2d_SSL_SESSION(x,&p);
1890Sstevel@tonic-gate 	p= &buf;
1900Sstevel@tonic-gate 	d2i_SSL_SESSION(&s,&p,(long)i);
1910Sstevel@tonic-gate 	p= &buf;
1920Sstevel@tonic-gate 	d2i_SSL_SESSION(&s,&p,(long)i);
1930Sstevel@tonic-gate 	p= &buf;
1940Sstevel@tonic-gate 	d2i_SSL_SESSION(&s,&p,(long)i);
1950Sstevel@tonic-gate 	SSL_SESSION_free(s);
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate #endif
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if (!noout || text)
2000Sstevel@tonic-gate 		{
2010Sstevel@tonic-gate 		out=BIO_new(BIO_s_file());
2020Sstevel@tonic-gate 		if (out == NULL)
2030Sstevel@tonic-gate 			{
2040Sstevel@tonic-gate 			ERR_print_errors(bio_err);
2050Sstevel@tonic-gate 			goto end;
2060Sstevel@tonic-gate 			}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 		if (outfile == NULL)
2090Sstevel@tonic-gate 			{
2100Sstevel@tonic-gate 			BIO_set_fp(out,stdout,BIO_NOCLOSE);
2110Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
2120Sstevel@tonic-gate 			{
2130Sstevel@tonic-gate 			BIO *tmpbio = BIO_new(BIO_f_linebuffer());
2140Sstevel@tonic-gate 			out = BIO_push(tmpbio, out);
2150Sstevel@tonic-gate 			}
2160Sstevel@tonic-gate #endif
2170Sstevel@tonic-gate 			}
2180Sstevel@tonic-gate 		else
2190Sstevel@tonic-gate 			{
2200Sstevel@tonic-gate 			if (BIO_write_filename(out,outfile) <= 0)
2210Sstevel@tonic-gate 				{
2220Sstevel@tonic-gate 				perror(outfile);
2230Sstevel@tonic-gate 				goto end;
2240Sstevel@tonic-gate 				}
2250Sstevel@tonic-gate 			}
2260Sstevel@tonic-gate 		}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (text)
2290Sstevel@tonic-gate 		{
2300Sstevel@tonic-gate 		SSL_SESSION_print(out,x);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		if (cert)
2330Sstevel@tonic-gate 			{
2340Sstevel@tonic-gate 			if (x->peer == NULL)
2350Sstevel@tonic-gate 				BIO_puts(out,"No certificate present\n");
2360Sstevel@tonic-gate 			else
2370Sstevel@tonic-gate 				X509_print(out,x->peer);
2380Sstevel@tonic-gate 			}
2390Sstevel@tonic-gate 		}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (!noout && !cert)
2420Sstevel@tonic-gate 		{
2430Sstevel@tonic-gate 		if 	(outformat == FORMAT_ASN1)
244*2139Sjp161948 			i=i2d_SSL_SESSION_bio(out,x);
2450Sstevel@tonic-gate 		else if (outformat == FORMAT_PEM)
2460Sstevel@tonic-gate 			i=PEM_write_bio_SSL_SESSION(out,x);
2470Sstevel@tonic-gate 		else	{
2480Sstevel@tonic-gate 			BIO_printf(bio_err,"bad output format specified for outfile\n");
2490Sstevel@tonic-gate 			goto end;
2500Sstevel@tonic-gate 			}
2510Sstevel@tonic-gate 		if (!i) {
2520Sstevel@tonic-gate 			BIO_printf(bio_err,"unable to write SSL_SESSION\n");
2530Sstevel@tonic-gate 			goto end;
2540Sstevel@tonic-gate 			}
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 	else if (!noout && (x->peer != NULL)) /* just print the certificate */
2570Sstevel@tonic-gate 		{
2580Sstevel@tonic-gate 		if 	(outformat == FORMAT_ASN1)
2590Sstevel@tonic-gate 			i=(int)i2d_X509_bio(out,x->peer);
2600Sstevel@tonic-gate 		else if (outformat == FORMAT_PEM)
2610Sstevel@tonic-gate 			i=PEM_write_bio_X509(out,x->peer);
2620Sstevel@tonic-gate 		else	{
2630Sstevel@tonic-gate 			BIO_printf(bio_err,"bad output format specified for outfile\n");
2640Sstevel@tonic-gate 			goto end;
2650Sstevel@tonic-gate 			}
2660Sstevel@tonic-gate 		if (!i) {
2670Sstevel@tonic-gate 			BIO_printf(bio_err,"unable to write X509\n");
2680Sstevel@tonic-gate 			goto end;
2690Sstevel@tonic-gate 			}
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 	ret=0;
2720Sstevel@tonic-gate end:
2730Sstevel@tonic-gate 	if (out != NULL) BIO_free_all(out);
2740Sstevel@tonic-gate 	if (x != NULL) SSL_SESSION_free(x);
2750Sstevel@tonic-gate 	apps_shutdown();
2760Sstevel@tonic-gate 	OPENSSL_EXIT(ret);
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
load_sess_id(char * infile,int format)2790Sstevel@tonic-gate static SSL_SESSION *load_sess_id(char *infile, int format)
2800Sstevel@tonic-gate 	{
2810Sstevel@tonic-gate 	SSL_SESSION *x=NULL;
2820Sstevel@tonic-gate 	BIO *in=NULL;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	in=BIO_new(BIO_s_file());
2850Sstevel@tonic-gate 	if (in == NULL)
2860Sstevel@tonic-gate 		{
2870Sstevel@tonic-gate 		ERR_print_errors(bio_err);
2880Sstevel@tonic-gate 		goto end;
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if (infile == NULL)
2920Sstevel@tonic-gate 		BIO_set_fp(in,stdin,BIO_NOCLOSE);
2930Sstevel@tonic-gate 	else
2940Sstevel@tonic-gate 		{
2950Sstevel@tonic-gate 		if (BIO_read_filename(in,infile) <= 0)
2960Sstevel@tonic-gate 			{
2970Sstevel@tonic-gate 			perror(infile);
2980Sstevel@tonic-gate 			goto end;
2990Sstevel@tonic-gate 			}
3000Sstevel@tonic-gate 		}
3010Sstevel@tonic-gate 	if 	(format == FORMAT_ASN1)
3020Sstevel@tonic-gate 		x=d2i_SSL_SESSION_bio(in,NULL);
3030Sstevel@tonic-gate 	else if (format == FORMAT_PEM)
3040Sstevel@tonic-gate 		x=PEM_read_bio_SSL_SESSION(in,NULL,NULL,NULL);
3050Sstevel@tonic-gate 	else	{
3060Sstevel@tonic-gate 		BIO_printf(bio_err,"bad input format specified for input crl\n");
3070Sstevel@tonic-gate 		goto end;
3080Sstevel@tonic-gate 		}
3090Sstevel@tonic-gate 	if (x == NULL)
3100Sstevel@tonic-gate 		{
3110Sstevel@tonic-gate 		BIO_printf(bio_err,"unable to load SSL_SESSION\n");
3120Sstevel@tonic-gate 		ERR_print_errors(bio_err);
3130Sstevel@tonic-gate 		goto end;
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate end:
3170Sstevel@tonic-gate 	if (in != NULL) BIO_free(in);
3180Sstevel@tonic-gate 	return(x);
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
321