xref: /freebsd/crypto/openssl/apps/ciphers.c (revision 1f13597d10e771d5546d31839150812bde8e4a56)
174664626SKris Kennaway /* apps/ciphers.c */
274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
374664626SKris Kennaway  * All rights reserved.
474664626SKris Kennaway  *
574664626SKris Kennaway  * This package is an SSL implementation written
674664626SKris Kennaway  * by Eric Young (eay@cryptsoft.com).
774664626SKris Kennaway  * The implementation was written so as to conform with Netscapes SSL.
874664626SKris Kennaway  *
974664626SKris Kennaway  * This library is free for commercial and non-commercial use as long as
1074664626SKris Kennaway  * the following conditions are aheared to.  The following conditions
1174664626SKris Kennaway  * apply to all code found in this distribution, be it the RC4, RSA,
1274664626SKris Kennaway  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1374664626SKris Kennaway  * included with this distribution is covered by the same copyright terms
1474664626SKris Kennaway  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1574664626SKris Kennaway  *
1674664626SKris Kennaway  * Copyright remains Eric Young's, and as such any Copyright notices in
1774664626SKris Kennaway  * the code are not to be removed.
1874664626SKris Kennaway  * If this package is used in a product, Eric Young should be given attribution
1974664626SKris Kennaway  * as the author of the parts of the library used.
2074664626SKris Kennaway  * This can be in the form of a textual message at program startup or
2174664626SKris Kennaway  * in documentation (online or textual) provided with the package.
2274664626SKris Kennaway  *
2374664626SKris Kennaway  * Redistribution and use in source and binary forms, with or without
2474664626SKris Kennaway  * modification, are permitted provided that the following conditions
2574664626SKris Kennaway  * are met:
2674664626SKris Kennaway  * 1. Redistributions of source code must retain the copyright
2774664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
2874664626SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
2974664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
3074664626SKris Kennaway  *    documentation and/or other materials provided with the distribution.
3174664626SKris Kennaway  * 3. All advertising materials mentioning features or use of this software
3274664626SKris Kennaway  *    must display the following acknowledgement:
3374664626SKris Kennaway  *    "This product includes cryptographic software written by
3474664626SKris Kennaway  *     Eric Young (eay@cryptsoft.com)"
3574664626SKris Kennaway  *    The word 'cryptographic' can be left out if the rouines from the library
3674664626SKris Kennaway  *    being used are not cryptographic related :-).
3774664626SKris Kennaway  * 4. If you include any Windows specific code (or a derivative thereof) from
3874664626SKris Kennaway  *    the apps directory (application code) you must include an acknowledgement:
3974664626SKris Kennaway  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4074664626SKris Kennaway  *
4174664626SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4274664626SKris Kennaway  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4374664626SKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4474664626SKris Kennaway  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4574664626SKris Kennaway  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4674664626SKris Kennaway  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4774664626SKris Kennaway  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4874664626SKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4974664626SKris Kennaway  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5074664626SKris Kennaway  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5174664626SKris Kennaway  * SUCH DAMAGE.
5274664626SKris Kennaway  *
5374664626SKris Kennaway  * The licence and distribution terms for any publically available version or
5474664626SKris Kennaway  * derivative of this code cannot be changed.  i.e. this code cannot simply be
5574664626SKris Kennaway  * copied and put under another distribution licence
5674664626SKris Kennaway  * [including the GNU Public Licence.]
5774664626SKris Kennaway  */
5874664626SKris Kennaway 
5974664626SKris Kennaway #include <stdio.h>
6074664626SKris Kennaway #include <stdlib.h>
6174664626SKris Kennaway #include <string.h>
625c87c606SMark Murray #ifdef OPENSSL_NO_STDIO
6374664626SKris Kennaway #define APPS_WIN16
6474664626SKris Kennaway #endif
6574664626SKris Kennaway #include "apps.h"
6674664626SKris Kennaway #include <openssl/err.h>
6774664626SKris Kennaway #include <openssl/ssl.h>
6874664626SKris Kennaway 
6974664626SKris Kennaway #undef PROG
7074664626SKris Kennaway #define PROG	ciphers_main
7174664626SKris Kennaway 
723b4e3dcbSSimon L. B. Nielsen static const char *ciphers_usage[]={
7374664626SKris Kennaway "usage: ciphers args\n",
74*1f13597dSJung-uk Kim " -v          - verbose mode, a textual listing of the SSL/TLS ciphers in OpenSSL\n",
75*1f13597dSJung-uk Kim " -V          - even more verbose\n",
7674664626SKris Kennaway " -ssl2       - SSL2 mode\n",
7774664626SKris Kennaway " -ssl3       - SSL3 mode\n",
78ddd58736SKris Kennaway " -tls1       - TLS1 mode\n",
7974664626SKris Kennaway NULL
8074664626SKris Kennaway };
8174664626SKris Kennaway 
82f579bf8eSKris Kennaway int MAIN(int, char **);
83f579bf8eSKris Kennaway 
8474664626SKris Kennaway int MAIN(int argc, char **argv)
8574664626SKris Kennaway 	{
8674664626SKris Kennaway 	int ret=1,i;
87*1f13597dSJung-uk Kim 	int verbose=0,Verbose=0;
883b4e3dcbSSimon L. B. Nielsen 	const char **pp;
8974664626SKris Kennaway 	const char *p;
9074664626SKris Kennaway 	int badops=0;
9174664626SKris Kennaway 	SSL_CTX *ctx=NULL;
9274664626SKris Kennaway 	SSL *ssl=NULL;
9374664626SKris Kennaway 	char *ciphers=NULL;
94*1f13597dSJung-uk Kim 	const SSL_METHOD *meth=NULL;
9574664626SKris Kennaway 	STACK_OF(SSL_CIPHER) *sk;
9674664626SKris Kennaway 	char buf[512];
9774664626SKris Kennaway 	BIO *STDout=NULL;
9874664626SKris Kennaway 
995c87c606SMark Murray #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
10074664626SKris Kennaway 	meth=SSLv23_server_method();
1015c87c606SMark Murray #elif !defined(OPENSSL_NO_SSL3)
10274664626SKris Kennaway 	meth=SSLv3_server_method();
1035c87c606SMark Murray #elif !defined(OPENSSL_NO_SSL2)
10474664626SKris Kennaway 	meth=SSLv2_server_method();
10574664626SKris Kennaway #endif
10674664626SKris Kennaway 
10774664626SKris Kennaway 	apps_startup();
10874664626SKris Kennaway 
10974664626SKris Kennaway 	if (bio_err == NULL)
11074664626SKris Kennaway 		bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
11174664626SKris Kennaway 	STDout=BIO_new_fp(stdout,BIO_NOCLOSE);
1125c87c606SMark Murray #ifdef OPENSSL_SYS_VMS
113ddd58736SKris Kennaway 	{
114ddd58736SKris Kennaway 	BIO *tmpbio = BIO_new(BIO_f_linebuffer());
115ddd58736SKris Kennaway 	STDout = BIO_push(tmpbio, STDout);
116ddd58736SKris Kennaway 	}
117ddd58736SKris Kennaway #endif
118*1f13597dSJung-uk Kim 	if (!load_config(bio_err, NULL))
119*1f13597dSJung-uk Kim 		goto end;
12074664626SKris Kennaway 
12174664626SKris Kennaway 	argc--;
12274664626SKris Kennaway 	argv++;
12374664626SKris Kennaway 	while (argc >= 1)
12474664626SKris Kennaway 		{
12574664626SKris Kennaway 		if (strcmp(*argv,"-v") == 0)
12674664626SKris Kennaway 			verbose=1;
127*1f13597dSJung-uk Kim 		else if (strcmp(*argv,"-V") == 0)
128*1f13597dSJung-uk Kim 			verbose=Verbose=1;
1295c87c606SMark Murray #ifndef OPENSSL_NO_SSL2
13074664626SKris Kennaway 		else if (strcmp(*argv,"-ssl2") == 0)
13174664626SKris Kennaway 			meth=SSLv2_client_method();
13274664626SKris Kennaway #endif
1335c87c606SMark Murray #ifndef OPENSSL_NO_SSL3
13474664626SKris Kennaway 		else if (strcmp(*argv,"-ssl3") == 0)
13574664626SKris Kennaway 			meth=SSLv3_client_method();
13674664626SKris Kennaway #endif
1375c87c606SMark Murray #ifndef OPENSSL_NO_TLS1
138ddd58736SKris Kennaway 		else if (strcmp(*argv,"-tls1") == 0)
139ddd58736SKris Kennaway 			meth=TLSv1_client_method();
140ddd58736SKris Kennaway #endif
14174664626SKris Kennaway 		else if ((strncmp(*argv,"-h",2) == 0) ||
14274664626SKris Kennaway 			 (strcmp(*argv,"-?") == 0))
14374664626SKris Kennaway 			{
14474664626SKris Kennaway 			badops=1;
14574664626SKris Kennaway 			break;
14674664626SKris Kennaway 			}
14774664626SKris Kennaway 		else
14874664626SKris Kennaway 			{
14974664626SKris Kennaway 			ciphers= *argv;
15074664626SKris Kennaway 			}
15174664626SKris Kennaway 		argc--;
15274664626SKris Kennaway 		argv++;
15374664626SKris Kennaway 		}
15474664626SKris Kennaway 
15574664626SKris Kennaway 	if (badops)
15674664626SKris Kennaway 		{
15774664626SKris Kennaway 		for (pp=ciphers_usage; (*pp != NULL); pp++)
1585c87c606SMark Murray 			BIO_printf(bio_err,"%s",*pp);
15974664626SKris Kennaway 		goto end;
16074664626SKris Kennaway 		}
16174664626SKris Kennaway 
162f579bf8eSKris Kennaway 	OpenSSL_add_ssl_algorithms();
16374664626SKris Kennaway 
16474664626SKris Kennaway 	ctx=SSL_CTX_new(meth);
16574664626SKris Kennaway 	if (ctx == NULL) goto err;
166f579bf8eSKris Kennaway 	if (ciphers != NULL) {
167f579bf8eSKris Kennaway 		if(!SSL_CTX_set_cipher_list(ctx,ciphers)) {
168f579bf8eSKris Kennaway 			BIO_printf(bio_err, "Error in cipher list\n");
169f579bf8eSKris Kennaway 			goto err;
170f579bf8eSKris Kennaway 		}
171f579bf8eSKris Kennaway 	}
17274664626SKris Kennaway 	ssl=SSL_new(ctx);
17374664626SKris Kennaway 	if (ssl == NULL) goto err;
17474664626SKris Kennaway 
17574664626SKris Kennaway 
17674664626SKris Kennaway 	if (!verbose)
17774664626SKris Kennaway 		{
17874664626SKris Kennaway 		for (i=0; ; i++)
17974664626SKris Kennaway 			{
18074664626SKris Kennaway 			p=SSL_get_cipher_list(ssl,i);
18174664626SKris Kennaway 			if (p == NULL) break;
18274664626SKris Kennaway 			if (i != 0) BIO_printf(STDout,":");
18374664626SKris Kennaway 			BIO_printf(STDout,"%s",p);
18474664626SKris Kennaway 			}
18574664626SKris Kennaway 		BIO_printf(STDout,"\n");
18674664626SKris Kennaway 		}
187*1f13597dSJung-uk Kim 	else /* verbose */
18874664626SKris Kennaway 		{
18974664626SKris Kennaway 		sk=SSL_get_ciphers(ssl);
19074664626SKris Kennaway 
19174664626SKris Kennaway 		for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
19274664626SKris Kennaway 			{
193*1f13597dSJung-uk Kim 			SSL_CIPHER *c;
194*1f13597dSJung-uk Kim 
195*1f13597dSJung-uk Kim 			c = sk_SSL_CIPHER_value(sk,i);
196*1f13597dSJung-uk Kim 
197*1f13597dSJung-uk Kim 			if (Verbose)
198*1f13597dSJung-uk Kim 				{
199*1f13597dSJung-uk Kim 				unsigned long id = SSL_CIPHER_get_id(c);
200*1f13597dSJung-uk Kim 				int id0 = (int)(id >> 24);
201*1f13597dSJung-uk Kim 				int id1 = (int)((id >> 16) & 0xffL);
202*1f13597dSJung-uk Kim 				int id2 = (int)((id >> 8) & 0xffL);
203*1f13597dSJung-uk Kim 				int id3 = (int)(id & 0xffL);
204*1f13597dSJung-uk Kim 
205*1f13597dSJung-uk Kim 				if ((id & 0xff000000L) == 0x02000000L)
206*1f13597dSJung-uk Kim 					BIO_printf(STDout, "     0x%02X,0x%02X,0x%02X - ", id1, id2, id3); /* SSL2 cipher */
207*1f13597dSJung-uk Kim 				else if ((id & 0xff000000L) == 0x03000000L)
208*1f13597dSJung-uk Kim 					BIO_printf(STDout, "          0x%02X,0x%02X - ", id2, id3); /* SSL3 cipher */
209*1f13597dSJung-uk Kim 				else
210*1f13597dSJung-uk Kim 					BIO_printf(STDout, "0x%02X,0x%02X,0x%02X,0x%02X - ", id0, id1, id2, id3); /* whatever */
211*1f13597dSJung-uk Kim 				}
212*1f13597dSJung-uk Kim 
213*1f13597dSJung-uk Kim 			BIO_puts(STDout,SSL_CIPHER_description(c,buf,sizeof buf));
21474664626SKris Kennaway 			}
21574664626SKris Kennaway 		}
21674664626SKris Kennaway 
21774664626SKris Kennaway 	ret=0;
21874664626SKris Kennaway 	if (0)
21974664626SKris Kennaway 		{
22074664626SKris Kennaway err:
22174664626SKris Kennaway 		SSL_load_error_strings();
22274664626SKris Kennaway 		ERR_print_errors(bio_err);
22374664626SKris Kennaway 		}
22474664626SKris Kennaway end:
22574664626SKris Kennaway 	if (ctx != NULL) SSL_CTX_free(ctx);
22674664626SKris Kennaway 	if (ssl != NULL) SSL_free(ssl);
227ddd58736SKris Kennaway 	if (STDout != NULL) BIO_free_all(STDout);
2285c87c606SMark Murray 	apps_shutdown();
2295c87c606SMark Murray 	OPENSSL_EXIT(ret);
23074664626SKris Kennaway 	}
23174664626SKris Kennaway 
232