Change code for samples

This commit is contained in:
Dmitry Ilyin 2022-10-04 17:57:01 +03:00
parent 88317a4ef8
commit 10ed1f1c28
3 changed files with 16 additions and 30 deletions

View File

@ -274,7 +274,7 @@ main(int argc, char **argv)
int timeout = -1; int timeout = -1;
#ifdef USE_MBEDTLS #ifdef USE_MBEDTLS
mbedtls_ssl_context ssl; mbedtls_dyncontext* ssl = NULL;
mbedtls_ssl_config config; mbedtls_ssl_config config;
mbedtls_ctr_drbg_context ctr_drbg; mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_entropy_context entropy; mbedtls_entropy_context entropy;
@ -297,7 +297,6 @@ main(int argc, char **argv)
mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_entropy_init(&entropy); mbedtls_entropy_init(&entropy);
mbedtls_ssl_config_init(&config); mbedtls_ssl_config_init(&config);
mbedtls_ssl_init(&ssl);
#else #else
enum { HTTP, HTTPS } type = HTTP; enum { HTTP, HTTPS } type = HTTP;
#endif #endif
@ -428,7 +427,7 @@ main(int argc, char **argv)
mbedtls_ssl_conf_ca_chain(&config, &cacert, NULL); mbedtls_ssl_conf_ca_chain(&config, &cacert, NULL);
} }
mbedtls_ssl_setup(&ssl, &config); ssl = bufferevent_mbedtls_dyncontext_new(&config);
#else #else
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \ #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L) (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
@ -510,7 +509,7 @@ main(int argc, char **argv)
} }
#ifdef USE_MBEDTLS #ifdef USE_MBEDTLS
mbedtls_ssl_set_hostname(&ssl, host); mbedtls_ssl_set_hostname(ssl, host);
#else #else
// Create OpenSSL bufferevent and stack evhttp on top of it // Create OpenSSL bufferevent and stack evhttp on top of it
ssl = SSL_new(ssl_ctx); ssl = SSL_new(ssl_ctx);
@ -528,16 +527,15 @@ main(int argc, char **argv)
if (strcasecmp(scheme, "http") == 0) { if (strcasecmp(scheme, "http") == 0) {
bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE); bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
} else { } else {
#ifdef USE_MBEDTLS #ifndef USE_MBEDTLS
bev = bufferevent_mbedtls_socket_new(base, -1, &ssl,
BUFFEREVENT_SSL_CONNECTING,
BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
#else
type = HTTPS; type = HTTPS;
bev = bufferevent_openssl_socket_new(base, -1, ssl, bev = bufferevent_openssl_socket_new(
#else
bev = bufferevent_mbedtls_socket_new(
#endif
base, -1, ssl,
BUFFEREVENT_SSL_CONNECTING, BUFFEREVENT_SSL_CONNECTING,
BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS); BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);
#endif
} }
if (bev == NULL) { if (bev == NULL) {
@ -639,7 +637,6 @@ cleanup:
event_base_free(base); event_base_free(base);
#ifdef USE_MBEDTLS #ifdef USE_MBEDTLS
mbedtls_ssl_free(&ssl);
mbedtls_ssl_config_free(&config); mbedtls_ssl_config_free(&config);
mbedtls_ctr_drbg_free(&ctr_drbg); mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_x509_crt_free(&cacert); mbedtls_x509_crt_free(&cacert);

View File

@ -146,7 +146,7 @@ main(void)
mbedtls_entropy_context entropy; mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg; mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_ssl_context ssl; mbedtls_dyncontext* ssl;
mbedtls_ssl_config conf; mbedtls_ssl_config conf;
mbedtls_x509_crt cacert; mbedtls_x509_crt cacert;
@ -175,7 +175,6 @@ main(void)
* 0. Initialize the RNG and the session data * 0. Initialize the RNG and the session data
*/ */
mbedtls_net_init(&server_fd); mbedtls_net_init(&server_fd);
mbedtls_ssl_init(&ssl);
mbedtls_ssl_config_init(&conf); mbedtls_ssl_config_init(&conf);
mbedtls_x509_crt_init(&cacert); mbedtls_x509_crt_init(&cacert);
mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_ctr_drbg_init(&ctr_drbg);
@ -244,12 +243,9 @@ main(void)
mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg); mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &ctr_drbg);
mbedtls_ssl_conf_dbg(&conf, my_debug, stdout); mbedtls_ssl_conf_dbg(&conf, my_debug, stdout);
if ((ret = mbedtls_ssl_setup(&ssl, &conf)) != 0) { ssl = bufferevent_mbedtls_dyncontext_new(&conf);
mbedtls_printf(" failed\n ! mbedtls_ssl_setup returned %d\n\n", ret);
goto exit;
}
if ((ret = mbedtls_ssl_set_hostname(&ssl, SERVER_NAME)) != 0) { if ((ret = mbedtls_ssl_set_hostname(ssl, SERVER_NAME)) != 0) {
mbedtls_printf( mbedtls_printf(
" failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret); " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n", ret);
goto exit; goto exit;
@ -265,7 +261,7 @@ main(void)
bev = bufferevent_socket_new(evbase, server_fd.fd, BEV_OPT_CLOSE_ON_FREE); bev = bufferevent_socket_new(evbase, server_fd.fd, BEV_OPT_CLOSE_ON_FREE);
bevf = bufferevent_mbedtls_filter_new( bevf = bufferevent_mbedtls_filter_new(
evbase, bev, &ssl, BUFFEREVENT_SSL_CONNECTING, BEV_OPT_CLOSE_ON_FREE); evbase, bev, ssl, BUFFEREVENT_SSL_CONNECTING, BEV_OPT_CLOSE_ON_FREE);
bev = bevf; bev = bevf;
bufferevent_setcb(bev, readcb, writecb, eventcb, NULL); bufferevent_setcb(bev, readcb, writecb, eventcb, NULL);
@ -289,7 +285,6 @@ exit:
mbedtls_net_free(&server_fd); mbedtls_net_free(&server_fd);
mbedtls_x509_crt_free(&cacert); mbedtls_x509_crt_free(&cacert);
mbedtls_ssl_free(&ssl);
mbedtls_ssl_config_free(&conf); mbedtls_ssl_config_free(&conf);
mbedtls_ctr_drbg_free(&ctr_drbg); mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy); mbedtls_entropy_free(&entropy);

View File

@ -99,7 +99,7 @@ test()
{ {
struct event_base *base = NULL; struct event_base *base = NULL;
mbedtls_ssl_config *conf = NULL; mbedtls_ssl_config *conf = NULL;
mbedtls_ssl_context *ssl = NULL; mbedtls_dyncontext *ssl = NULL;
struct bufferevent *bev; struct bufferevent *bev;
int r = 1; int r = 1;
@ -114,12 +114,7 @@ test()
} }
mbedtls_ssl_config_init(conf); mbedtls_ssl_config_init(conf);
ssl = malloc(sizeof(*ssl)); ssl = bufferevent_mbedtls_dyncontext_new(conf);
if (!ssl) {
goto error;
}
mbedtls_ssl_init(ssl);
mbedtls_ssl_setup(ssl, conf);
bev = bufferevent_mbedtls_socket_new(base, -1, ssl, bev = bufferevent_mbedtls_socket_new(base, -1, ssl,
BUFFEREVENT_SSL_CONNECTING, BUFFEREVENT_SSL_CONNECTING,
@ -132,8 +127,7 @@ error:
if (base) if (base)
event_base_free(base); event_base_free(base);
if (ssl) { if (ssl) {
mbedtls_ssl_free(ssl); bufferevent_mbedtls_dyncontext_free(ssl);
free(ssl);
} }
if (conf) { if (conf) {
mbedtls_ssl_config_free(conf); mbedtls_ssl_config_free(conf);