Skip to content

Commit 76f02f0

Browse files
committed
Fixed bug in mbedtls_conf.cpp & openssl_conf.cpp in client mode.
1 parent dd8ba23 commit 76f02f0

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

lib_acl_cpp/src/stream/mbedtls_conf.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ mbedtls_ssl_config* mbedtls_conf::create_ssl_config(void)
841841
MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
842842
__ssl_conf_ciphersuites(conf, ciphers_);
843843

844+
// conf_ will be set to the first one.
844845
if (conf_ == NULL) {
845846
conf_ = conf;
846847
}
@@ -1020,11 +1021,14 @@ bool mbedtls_conf::load_ca(const char* ca_file, const char* ca_path)
10201021
ca_path, -ret);
10211022
free_ca();
10221023
return false;
1023-
} else {
1024-
// Setup ca cert
1025-
__ssl_conf_ca_chain(conf_, cacert_->next, NULL);
1026-
return true;
10271024
}
1025+
1026+
if (conf_ == NULL) {
1027+
conf_ = create_ssl_config();
1028+
}
1029+
// Setup ca cert
1030+
__ssl_conf_ca_chain(conf_, cacert_->next, NULL);
1031+
return true;
10281032
#else
10291033
(void) ca_file;
10301034
(void) ca_path;
@@ -1065,7 +1069,16 @@ bool mbedtls_conf::add_cert(const char* crt_file, const char* key_file,
10651069
} \
10661070
} while (0)
10671071

1068-
mbedtls_ssl_config* conf = create_ssl_config();
1072+
mbedtls_ssl_config* conf;
1073+
if (server_side_) {
1074+
conf = create_ssl_config();
1075+
}
1076+
// There's only one cert conf in client side mode.
1077+
else if (conf_) {
1078+
conf = conf_;
1079+
} else {
1080+
conf = conf_ = create_ssl_config();
1081+
}
10691082

10701083
X509_CRT *cert = NULL;
10711084
PKEY *pkey = NULL;
@@ -1181,12 +1194,17 @@ bool mbedtls_conf::setup_certs(void* ssl)
11811194
return false;
11821195
}
11831196

1197+
// If the default conf_ null, maybe the load_ca() and add_cert()
1198+
// didn't called before, so we just create a new one as the default.
1199+
if (conf_ == NULL) {
1200+
conf_ = create_ssl_config();
1201+
}
1202+
11841203
int ret = __ssl_setup((mbedtls_ssl_context*) ssl, conf_);
11851204
if (ret != 0) {
11861205
logger_error("ssl_setup error:-0x%04x", -ret);
11871206
return false;
11881207
}
1189-
11901208
return true;
11911209
#else
11921210
(void) ssl;

lib_acl_cpp/src/stream/openssl_conf.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,6 @@ openssl_conf::openssl_conf(bool server_side /* false */, int timeout /* 30 */)
553553
(void) timeout_;
554554
logger_error("HAS_OPENSSL not defined!");
555555
#endif // HAS_OPENSSL
556-
557556
}
558557

559558
openssl_conf::~openssl_conf(void)
@@ -874,6 +873,9 @@ bool openssl_conf::load_ca(const char* ca_file, const char* /* ca_path */)
874873
}
875874

876875
#ifdef HAS_OPENSSL
876+
if (ssl_ctx_ == NULL) {
877+
create_ssl_ctx(); // ssl_ctx_ will be set in it.
878+
}
877879
__ssl_ctx_set_verify_depth(ssl_ctx_, 5);
878880

879881
STACK_OF(X509_NAME)* list = __ssl_load_client_ca(ca_file);
@@ -913,8 +915,14 @@ bool openssl_conf::add_cert(const char* crt_file, const char* key_file,
913915
}
914916

915917
#ifdef HAS_OPENSSL
916-
SSL_CTX* ctx = create_ssl_ctx();
917-
918+
SSL_CTX* ctx;
919+
if (server_side_) {
920+
ctx = create_ssl_ctx();
921+
} else if (ssl_ctx_) {
922+
ctx = ssl_ctx_;
923+
} else {
924+
ctx = ssl_ctx_ = create_ssl_ctx();
925+
}
918926
#if 0
919927
if (__ssl_ctx_use_cert_chain(ctx, crt_file) != 1) {
920928
logger_error("use crt chain file(%s) error", crt_file);

0 commit comments

Comments
 (0)