⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ssltest.c

📁 一个用于点对点传输加密的工具包源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* ssl/ssltest.c *//* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. *  * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to.  The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code.  The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). *  * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *    "This product includes cryptographic software written by *     Eric Young (eay@cryptsoft.com)" *    The word 'cryptographic' can be left out if the rouines from the library *    being used are not cryptographic related :-). * 4. If you include any Windows specific code (or a derivative thereof) from  *    the apps directory (application code) you must include an acknowledgement: *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" *  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. *  * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed.  i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */#include <assert.h>#include <errno.h>#include <limits.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include "openssl/e_os.h"#include <openssl/bio.h>#include <openssl/crypto.h>#include <openssl/evp.h>#include <openssl/x509.h>#include <openssl/ssl.h>#include <openssl/err.h>#include <openssl/rand.h>#ifdef WINDOWS#include "../crypto/bio/bss_file.c"#endif#ifdef VMS#  define TEST_SERVER_CERT "SYS$DISK:[-.APPS]SERVER.PEM"#  define TEST_CLIENT_CERT "SYS$DISK:[-.APPS]CLIENT.PEM"#else#  define TEST_SERVER_CERT "../apps/server.pem"#  define TEST_CLIENT_CERT "../apps/client.pem"#endifstatic int MS_CALLBACK verify_callback(int ok, X509_STORE_CTX *ctx);#ifndef NO_RSAstatic RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export,int keylength);static void free_tmp_rsa(void);#endif#ifndef NO_DHstatic DH *get_dh512(void);static DH *get_dh1024(void);static DH *get_dh1024dsa(void);#endifstatic BIO *bio_err=NULL;static BIO *bio_stdout=NULL;static char *cipher=NULL;static int verbose=0;static int debug=0;#if 0/* Not used yet. */#ifdef FIONBIOstatic int s_nbio=0;#endif#endifstatic const char rnd_seed[] = "string to make the random number generator think it has entropy";int doit_biopair(SSL *s_ssl,SSL *c_ssl,long bytes,clock_t *s_time,clock_t *c_time);int doit(SSL *s_ssl,SSL *c_ssl,long bytes);static void sv_usage(void)	{	fprintf(stderr,"usage: ssltest [args ...]\n");	fprintf(stderr,"\n");	fprintf(stderr," -server_auth  - check server certificate\n");	fprintf(stderr," -client_auth  - do client authentication\n");	fprintf(stderr," -v            - more output\n");	fprintf(stderr," -d            - debug output\n");	fprintf(stderr," -reuse        - use session-id reuse\n");	fprintf(stderr," -num <val>    - number of connections to perform\n");	fprintf(stderr," -bytes <val>  - number of bytes to swap between client/server\n");#ifndef NO_DH	fprintf(stderr," -dhe1024      - use 1024 bit key (safe prime) for DHE\n");	fprintf(stderr," -dhe1024dsa   - use 1024 bit key (with 160-bit subprime) for DHE\n");	fprintf(stderr," -no_dhe       - disable DHE\n");#endif#ifndef NO_SSL2	fprintf(stderr," -ssl2         - use SSLv2\n");#endif#ifndef NO_SSL3	fprintf(stderr," -ssl3         - use SSLv3\n");#endif#ifndef NO_TLS1	fprintf(stderr," -tls1         - use TLSv1\n");#endif	fprintf(stderr," -CApath arg   - PEM format directory of CA's\n");	fprintf(stderr," -CAfile arg   - PEM format file of CA's\n");	fprintf(stderr," -cert arg     - Server certificate file\n");	fprintf(stderr," -key arg      - Server key file (default: same as -cert)\n");	fprintf(stderr," -c_cert arg   - Client certificate file\n");	fprintf(stderr," -c_key arg    - Client key file (default: same as -c_cert)\n");	fprintf(stderr," -cipher arg   - The cipher list\n");	fprintf(stderr," -bio_pair     - Use BIO pairs\n");	fprintf(stderr," -f            - Test even cases that can't work\n");	fprintf(stderr," -time         - measure processor time used by client and server\n");	}static void print_details(SSL *c_ssl, const char *prefix)	{	SSL_CIPHER *ciph;	X509 *cert;			ciph=SSL_get_current_cipher(c_ssl);	BIO_printf(bio_stdout,"%s%s, cipher %s %s",		prefix,		SSL_get_version(c_ssl),		SSL_CIPHER_get_version(ciph),		SSL_CIPHER_get_name(ciph));	cert=SSL_get_peer_certificate(c_ssl);	if (cert != NULL)		{		EVP_PKEY *pkey = X509_get_pubkey(cert);		if (pkey != NULL)			{			if (0) 				;#ifndef NO_RSA			else if (pkey->type == EVP_PKEY_RSA && pkey->pkey.rsa != NULL				&& pkey->pkey.rsa->n != NULL)				{				BIO_printf(bio_stdout, ", %d bit RSA",					BN_num_bits(pkey->pkey.rsa->n));				}#endif#ifndef NO_DSA			else if (pkey->type == EVP_PKEY_DSA && pkey->pkey.dsa != NULL				&& pkey->pkey.dsa->p != NULL)				{				BIO_printf(bio_stdout, ", %d bit DSA",					BN_num_bits(pkey->pkey.dsa->p));				}#endif			EVP_PKEY_free(pkey);			}		X509_free(cert);		}	/* The SSL API does not allow us to look at temporary RSA/DH keys,	 * otherwise we should print their lengths too */	BIO_printf(bio_stdout,"\n");	}int main(int argc, char *argv[])	{	char *CApath=NULL,*CAfile=NULL;	int badop=0;	int bio_pair=0;	int force=0;	int tls1=0,ssl2=0,ssl3=0,ret=1;	int client_auth=0;	int server_auth=0,i;	char *server_cert=TEST_SERVER_CERT;	char *server_key=NULL;	char *client_cert=TEST_CLIENT_CERT;	char *client_key=NULL;	SSL_CTX *s_ctx=NULL;	SSL_CTX *c_ctx=NULL;	SSL_METHOD *meth=NULL;	SSL *c_ssl,*s_ssl;	int number=1,reuse=0;	long bytes=1L;#ifndef NO_DH	DH *dh;	int dhe1024 = 0, dhe1024dsa = 0;#endif	int no_dhe = 0;	int print_time = 0;	clock_t s_time = 0, c_time = 0;	verbose = 0;	debug = 0;	cipher = 0;		CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);	RAND_seed(rnd_seed, sizeof rnd_seed);	bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);	bio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);	argc--;	argv++;	while (argc >= 1)		{		if	(strcmp(*argv,"-server_auth") == 0)			server_auth=1;		else if	(strcmp(*argv,"-client_auth") == 0)			client_auth=1;		else if	(strcmp(*argv,"-v") == 0)			verbose=1;		else if	(strcmp(*argv,"-d") == 0)			debug=1;		else if	(strcmp(*argv,"-reuse") == 0)			reuse=1;#ifndef NO_DH		else if	(strcmp(*argv,"-dhe1024") == 0)			dhe1024=1;		else if	(strcmp(*argv,"-dhe1024dsa") == 0)			dhe1024dsa=1;#endif		else if	(strcmp(*argv,"-no_dhe") == 0)			no_dhe=1;		else if	(strcmp(*argv,"-ssl2") == 0)			ssl2=1;		else if	(strcmp(*argv,"-tls1") == 0)			tls1=1;		else if	(strcmp(*argv,"-ssl3") == 0)			ssl3=1;		else if	(strncmp(*argv,"-num",4) == 0)			{			if (--argc < 1) goto bad;			number= atoi(*(++argv));			if (number == 0) number=1;			}		else if	(strcmp(*argv,"-bytes") == 0)			{			if (--argc < 1) goto bad;			bytes= atol(*(++argv));			if (bytes == 0L) bytes=1L;			i=strlen(argv[0]);			if (argv[0][i-1] == 'k') bytes*=1024L;			if (argv[0][i-1] == 'm') bytes*=1024L*1024L;			}		else if	(strcmp(*argv,"-cert") == 0)			{			if (--argc < 1) goto bad;			server_cert= *(++argv);			}		else if	(strcmp(*argv,"-s_cert") == 0)			{			if (--argc < 1) goto bad;			server_cert= *(++argv);			}		else if	(strcmp(*argv,"-key") == 0)			{			if (--argc < 1) goto bad;			server_key= *(++argv);			}		else if	(strcmp(*argv,"-s_key") == 0)			{			if (--argc < 1) goto bad;			server_key= *(++argv);			}		else if	(strcmp(*argv,"-c_cert") == 0)			{			if (--argc < 1) goto bad;			client_cert= *(++argv);			}		else if	(strcmp(*argv,"-c_key") == 0)			{			if (--argc < 1) goto bad;			client_key= *(++argv);			}		else if	(strcmp(*argv,"-cipher") == 0)			{			if (--argc < 1) goto bad;			cipher= *(++argv);			}		else if	(strcmp(*argv,"-CApath") == 0)			{			if (--argc < 1) goto bad;			CApath= *(++argv);			}		else if	(strcmp(*argv,"-CAfile") == 0)			{			if (--argc < 1) goto bad;			CAfile= *(++argv);			}		else if	(strcmp(*argv,"-bio_pair") == 0)			{			bio_pair = 1;			}		else if	(strcmp(*argv,"-f") == 0)			{			force = 1;			}		else if	(strcmp(*argv,"-time") == 0)			{			print_time = 1;			}		else			{			fprintf(stderr,"unknown option %s\n",*argv);			badop=1;			break;			}		argc--;		argv++;		}	if (badop)		{bad:		sv_usage();		goto end;		}	if (!ssl2 && !ssl3 && !tls1 && number > 1 && !reuse && !force)		{		fprintf(stderr, "This case cannot work.  Use -f to perform "			"the test anyway (and\n-d to see what happens), "			"or add one of -ssl2, -ssl3, -tls1, -reuse\n"			"to avoid protocol mismatch.\n");		exit(1);		}	if (print_time)		{		if (!bio_pair)			{			fprintf(stderr, "Using BIO pair (-bio_pair)\n");			bio_pair = 1;			}		if (number < 50 && !force)			fprintf(stderr, "Warning: For accurate timings, use more connections (e.g. -num 1000)\n");		}/*	if (cipher == NULL) cipher=getenv("SSL_CIPHER"); */	SSL_library_init();	SSL_load_error_strings();#if !defined(NO_SSL2) && !defined(NO_SSL3)	if (ssl2)		meth=SSLv2_method();	else 	if (tls1)		meth=TLSv1_method();	else	if (ssl3)		meth=SSLv3_method();	else		meth=SSLv23_method();#else#ifdef NO_SSL2	meth=SSLv3_method();#else	meth=SSLv2_method();#endif#endif	c_ctx=SSL_CTX_new(meth);	s_ctx=SSL_CTX_new(meth);	if ((c_ctx == NULL) || (s_ctx == NULL))		{		ERR_print_errors(bio_err);		goto end;		}	if (cipher != NULL)		{		SSL_CTX_set_cipher_list(c_ctx,cipher);		SSL_CTX_set_cipher_list(s_ctx,cipher);		}#ifndef NO_DH	if (!no_dhe)		{		if (dhe1024dsa)			{			/* use SSL_OP_SINGLE_DH_USE to avoid small subgroup attacks */			SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE);			dh=get_dh1024dsa();			}		else if (dhe1024)			dh=get_dh1024();		else			dh=get_dh512();		SSL_CTX_set_tmp_dh(s_ctx,dh);		DH_free(dh);		}#else	(void)no_dhe;#endif#ifndef NO_RSA	SSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);#endif	if (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))		{		ERR_print_errors(bio_err);		}	else if (!SSL_CTX_use_PrivateKey_file(s_ctx,		(server_key?server_key:server_cert), SSL_FILETYPE_PEM))		{		ERR_print_errors(bio_err);		goto end;		}	if (client_auth)		{		SSL_CTX_use_certificate_file(c_ctx,client_cert,			SSL_FILETYPE_PEM);		SSL_CTX_use_PrivateKey_file(c_ctx,			(client_key?client_key:client_cert),			SSL_FILETYPE_PEM);		}	if (	(!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||		(!SSL_CTX_set_default_verify_paths(s_ctx)) ||		(!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||		(!SSL_CTX_set_default_verify_paths(c_ctx)))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -