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

📄 ssltest.c

📁 mediastreamer2是开源的网络传输媒体流的库
💻 C
📖 第 1 页 / 共 4 页
字号:
			{			print_time = 1;			}		else if	(strcmp(*argv,"-zlib") == 0)			{			comp = COMP_ZLIB;			}		else if	(strcmp(*argv,"-rle") == 0)			{			comp = COMP_RLE;			}		else if	(strcmp(*argv,"-named_curve") == 0)			{			if (--argc < 1) goto bad;#ifndef OPENSSL_NO_ECDH					named_curve = *(++argv);#else			fprintf(stderr,"ignoring -named_curve, since I'm compiled without ECDH\n");			++argv;#endif			}		else if	(strcmp(*argv,"-app_verify") == 0)			{			app_verify_arg.app_verify = 1;			}		else if	(strcmp(*argv,"-proxy") == 0)			{			app_verify_arg.allow_proxy_certs = 1;			}		else if (strcmp(*argv,"-test_cipherlist") == 0)			{			test_cipherlist = 1;			}		else			{			fprintf(stderr,"unknown option %s\n",*argv);			badop=1;			break;			}		argc--;		argv++;		}	if (badop)		{bad:		sv_usage();		goto end;		}	if (test_cipherlist == 1)		{		/* ensure that the cipher list are correctly sorted and exit */		if (do_test_cipherlist() == 0)			EXIT(1);		ret = 0;		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();#ifndef OPENSSL_NO_COMP	if (comp == COMP_ZLIB) cm = COMP_zlib();	if (comp == COMP_RLE) cm = COMP_rle();	if (cm != NULL)		{		if (cm->type != NID_undef)			{			if (SSL_COMP_add_compression_method(comp, cm) != 0)				{				fprintf(stderr,					"Failed to add compression method\n");				ERR_print_errors_fp(stderr);				}			}		else			{			fprintf(stderr,				"Warning: %s compression not supported\n",				(comp == COMP_RLE ? "rle" :					(comp == COMP_ZLIB ? "zlib" :						"unknown")));			ERR_print_errors_fp(stderr);			}		}	ssl_comp_methods = SSL_COMP_get_compression_methods();	fprintf(stderr, "Available compression methods:\n");	{	int j, n = sk_SSL_COMP_num(ssl_comp_methods);	if (n == 0)		fprintf(stderr, "  NONE\n");	else		for (j = 0; j < n; j++)			{			SSL_COMP *c = sk_SSL_COMP_value(ssl_comp_methods, j);			fprintf(stderr, "  %d: %s\n", c->id, c->name);			}	}#endif#if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_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 OPENSSL_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 OPENSSL_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 OPENSSL_NO_ECDH	if (!no_ecdhe)		{		int nid;		if (named_curve != NULL)			{			nid = OBJ_sn2nid(named_curve);			if (nid == 0)			{				BIO_printf(bio_err, "unknown curve name (%s)\n", named_curve);				goto end;				}			}		else			nid = NID_sect163r2;		ecdh = EC_KEY_new_by_curve_name(nid);		if (ecdh == NULL)			{			BIO_printf(bio_err, "unable to create curve\n");			goto end;			}		SSL_CTX_set_tmp_ecdh(s_ctx, ecdh);		SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_ECDH_USE);		EC_KEY_free(ecdh);		}#else	(void)no_ecdhe;#endif#ifndef OPENSSL_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)))		{		/* fprintf(stderr,"SSL_load_verify_locations\n"); */		ERR_print_errors(bio_err);		/* goto end; */		}	if (client_auth)		{		BIO_printf(bio_err,"client authentication\n");		SSL_CTX_set_verify(s_ctx,			SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,			verify_callback);		SSL_CTX_set_cert_verify_callback(s_ctx, app_verify_callback, &app_verify_arg);		}	if (server_auth)		{		BIO_printf(bio_err,"server authentication\n");		SSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,			verify_callback);		SSL_CTX_set_cert_verify_callback(c_ctx, app_verify_callback, &app_verify_arg);		}		{		int session_id_context = 0;		SSL_CTX_set_session_id_context(s_ctx, (void *)&session_id_context, sizeof session_id_context);	}	c_ssl=SSL_new(c_ctx);	s_ssl=SSL_new(s_ctx);#ifndef OPENSSL_NO_KRB5	if (c_ssl  &&  c_ssl->kssl_ctx)                {                char	localhost[MAXHOSTNAMELEN+2];		if (gethostname(localhost, sizeof localhost-1) == 0)                        {			localhost[sizeof localhost-1]='\0';			if(strlen(localhost) == sizeof localhost-1)				{				BIO_printf(bio_err,"localhost name too long\n");				goto end;				}			kssl_ctx_setstring(c_ssl->kssl_ctx, KSSL_SERVER,                                localhost);			}		}#endif    /* OPENSSL_NO_KRB5  */	for (i=0; i<number; i++)		{		if (!reuse) SSL_set_session(c_ssl,NULL);		if (bio_pair)			ret=doit_biopair(s_ssl,c_ssl,bytes,&s_time,&c_time);		else			ret=doit(s_ssl,c_ssl,bytes);		}	if (!verbose)		{		print_details(c_ssl, "");		}	if ((number > 1) || (bytes > 1L))		BIO_printf(bio_stdout, "%d handshakes of %ld bytes done\n",number,bytes);	if (print_time)		{#ifdef CLOCKS_PER_SEC		/* "To determine the time in seconds, the value returned		 * by the clock function should be divided by the value		 * of the macro CLOCKS_PER_SEC."		 *                                       -- ISO/IEC 9899 */		BIO_printf(bio_stdout, "Approximate total server time: %6.2f s\n"			"Approximate total client time: %6.2f s\n",			(double)s_time/CLOCKS_PER_SEC,			(double)c_time/CLOCKS_PER_SEC);#else		/* "`CLOCKS_PER_SEC' undeclared (first use this function)"		 *                            -- cc on NeXTstep/OpenStep */		BIO_printf(bio_stdout,			"Approximate total server time: %6.2f units\n"			"Approximate total client time: %6.2f units\n",			(double)s_time,			(double)c_time);#endif		}	SSL_free(s_ssl);	SSL_free(c_ssl);end:	if (s_ctx != NULL) SSL_CTX_free(s_ctx);	if (c_ctx != NULL) SSL_CTX_free(c_ctx);	if (bio_stdout != NULL) BIO_free(bio_stdout);#ifndef OPENSSL_NO_RSA	free_tmp_rsa();#endif#ifndef OPENSSL_NO_ENGINE	ENGINE_cleanup();#endif	CRYPTO_cleanup_all_ex_data();	ERR_free_strings();	ERR_remove_state(0);	EVP_cleanup();	CRYPTO_mem_leaks(bio_err);	if (bio_err != NULL) BIO_free(bio_err);	EXIT(ret);	return ret;	}int doit_biopair(SSL *s_ssl, SSL *c_ssl, long count,	clock_t *s_time, clock_t *c_time)	{	long cw_num = count, cr_num = count, sw_num = count, sr_num = count;	BIO *s_ssl_bio = NULL, *c_ssl_bio = NULL;	BIO *server = NULL, *server_io = NULL, *client = NULL, *client_io = NULL;	int ret = 1;		size_t bufsiz = 256; /* small buffer for testing */	if (!BIO_new_bio_pair(&server, bufsiz, &server_io, bufsiz))		goto err;	if (!BIO_new_bio_pair(&client, bufsiz, &client_io, bufsiz))		goto err;		s_ssl_bio = BIO_new(BIO_f_ssl());	if (!s_ssl_bio)		goto err;	c_ssl_bio = BIO_new(BIO_f_ssl());	if (!c_ssl_bio)		goto err;	SSL_set_connect_state(c_ssl);	SSL_set_bio(c_ssl, client, client);	(void)BIO_set_ssl(c_ssl_bio, c_ssl, BIO_NOCLOSE);	SSL_set_accept_state(s_ssl);	SSL_set_bio(s_ssl, server, server);	(void)BIO_set_ssl(s_ssl_bio, s_ssl, BIO_NOCLOSE);	do		{		/* c_ssl_bio:          SSL filter BIO		 *		 * client:             pseudo-I/O for SSL library		 *		 * client_io:          client's SSL communication; usually to be		 *                     relayed over some I/O facility, but in this		 *                     test program, we're the server, too:		 *		 * server_io:          server's SSL communication		 *		 * server:             pseudo-I/O for SSL library		 *		 * s_ssl_bio:          SSL filter BIO		 *		 * The client and the server each employ a "BIO pair":		 * client + client_io, server + server_io.		 * BIO pairs are symmetric.  A BIO pair behaves similar		 * to a non-blocking socketpair (but both endpoints must		 * be handled by the same thread).		 * [Here we could connect client and server to the ends		 * of a single BIO pair, but then this code would be less		 * suitable as an example for BIO pairs in general.]		 *		 * Useful functions for querying the state of BIO pair endpoints:		 *		 * BIO_ctrl_pending(bio)              number of bytes we can read now		 * BIO_ctrl_get_read_request(bio)     number of bytes needed to fulfil		 *                                      other side's read attempt		 * BIO_ctrl_get_write_guarantee(bio)   number of bytes we can write now		 *		 * ..._read_request is never more than ..._write_guarantee;		 * it depends on the application which one you should use.		 */		/* We have non-blocking behaviour throughout this test program, but		 * can be sure that there is *some* progress in each iteration; so		 * we don't have to worry about ..._SHOULD_READ or ..._SHOULD_WRITE		 * -- we just try everything in each iteration		 */			{			/* CLIENT */					MS_STATIC char cbuf[1024*8];			int i, r;			clock_t c_clock = clock();			memset(cbuf, 0, sizeof(cbuf));			if (debug)				if (SSL_in_init(c_ssl))					printf("client waiting in SSL_connect - %s\n",						SSL_state_string_long(c_ssl));			if (cw_num > 0)				{				/* Write to server. */								if (cw_num > (long)sizeof cbuf)					i = sizeof cbuf;				else					i = (int)cw_num;				r = BIO_write(c_ssl_bio, cbuf, i);				if (r < 0)					{					if (!BIO_should_retry(c_ssl_bio))						{						fprintf(stderr,"ERROR in CLIENT\n");						goto err;						}					/* BIO_should_retry(...) can just be ignored here.					 * The library expects us to call BIO_write with					 * the same arguments again, and that's what we will					 * do in the next iteration. */					}				else if (r == 0)					{					fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");					goto err;					}				else					{					if (debug)						printf("client wrote %d\n", r);					cw_num -= r;									}				}			if (cr_num > 0)				{				/* Read from server. */				r = BIO_read(c_ssl_bio, cbuf, sizeof(cbuf));				if (r < 0)					{					if (!BIO_should_retry(c_ssl_bio))						{						fprintf(stderr,"ERROR in CLIENT\n");						goto err;						}					/* Again, "BIO_should_retry" can be ignored. */					}				else if (r == 0)					{					fprintf(stderr,"SSL CLIENT STARTUP FAILED\n");					goto err;					}				else					{					if (debug)						printf("client read %d\n", r);					cr_num -= r;					}				}			/* c_time and s_time increments will typically be very small			 * (depending on machine speed and clock tick intervals),			 * but sampling over a large number of connections should			 * result in fairly accurate figures.  We cannot guarantee			 * a lot, however -- if each connection lasts for exactly			 * one clock tick, it will be counted only for the client			 * or only for the server or even not at all.			 */			*c_time += (clock() - c_clock);			}			{			/* SERVER */					MS_STATIC char sbuf[1024*8];			int i, r;			clock_t s_clock = clock();			memset(sbuf, 0, sizeof(sbuf));			if (debug)				if (SSL_in_init(s_ssl))					printf("server waiting in SSL_accept - %s\n",						SSL_state_string_long(s_ssl));			if (sw_num > 0)				{				/* Write to client. */								if (sw_num > (long)sizeof sbuf)					i = sizeof sbuf;				else					i = (int)sw_num;				r = BIO_write(s_ssl_bio, sbuf, i);				if (r < 0)					{					if (!BIO_should_retry(s_ssl_bio))						{						fprintf(stderr,"ERROR in SERVER\n");						goto err;						}					/* Ignore "BIO_should_retry". */					}				else if (r == 0)					{					fprintf(stderr,"SSL SERVER STARTUP FAILED\n");					goto err;					}				else					{					if (debug)						printf("server wrote %d\n", r);					sw_num -= r;									}				}			if (sr_num > 0)				{				/* Read from client. */				r = BIO_read(s_ssl_bio, sbuf, sizeof(sbuf));				if (r < 0)					{					if (!BIO_should_retry(s_ssl_bio))						{						fprintf(stderr,"ERROR in SERVER\n");						goto err;						}					/* blah, blah */					}				else if (r == 0)					{					fprintf(stderr,"SSL SERVER STARTUP FAILED\n");					goto err;					}				else					{					if (debug)						printf("server read %d\n", r);

⌨️ 快捷键说明

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