Unverified Commit 55ec22f2 authored by Me No Dev's avatar Me No Dev Committed by GitHub

fix(ssl): Fix load stream memory leak (#9767)

* fix(ssl): Fix load stream memory leak

Loading Ca Cert/Certificate or Private Key from stream was leaking memory, due to buffers not being freed.

* fix(ssl): Cast the buffers to non-const
parent ce229a17
...@@ -40,6 +40,10 @@ NetworkClientSecure::NetworkClientSecure() { ...@@ -40,6 +40,10 @@ NetworkClientSecure::NetworkClientSecure() {
sslclient->socket = -1; sslclient->socket = -1;
sslclient->handshake_timeout = 120000; sslclient->handshake_timeout = 120000;
_use_insecure = false; _use_insecure = false;
_stillinPlainStart = false;
_ca_cert_free = false;
_cert_free = false;
_private_key_free = false;
_CA_cert = NULL; _CA_cert = NULL;
_cert = NULL; _cert = NULL;
_private_key = NULL; _private_key = NULL;
...@@ -68,6 +72,11 @@ NetworkClientSecure::NetworkClientSecure(int sock) { ...@@ -68,6 +72,11 @@ NetworkClientSecure::NetworkClientSecure(int sock) {
_connected = true; _connected = true;
} }
_use_insecure = false;
_stillinPlainStart = false;
_ca_cert_free = false;
_cert_free = false;
_private_key_free = false;
_CA_cert = NULL; _CA_cert = NULL;
_cert = NULL; _cert = NULL;
_private_key = NULL; _private_key = NULL;
...@@ -77,7 +86,17 @@ NetworkClientSecure::NetworkClientSecure(int sock) { ...@@ -77,7 +86,17 @@ NetworkClientSecure::NetworkClientSecure(int sock) {
_alpn_protos = NULL; _alpn_protos = NULL;
} }
NetworkClientSecure::~NetworkClientSecure() {} NetworkClientSecure::~NetworkClientSecure() {
if (_ca_cert_free && _CA_cert) {
free((void *)_CA_cert);
}
if (_cert_free && _cert) {
free((void *)_cert);
}
if (_private_key_free && _private_key) {
free((void *)_private_key);
}
}
void NetworkClientSecure::stop() { void NetworkClientSecure::stop() {
stop_ssl_socket(sslclient.get()); stop_ssl_socket(sslclient.get());
...@@ -310,6 +329,10 @@ void NetworkClientSecure::setInsecure() { ...@@ -310,6 +329,10 @@ void NetworkClientSecure::setInsecure() {
} }
void NetworkClientSecure::setCACert(const char *rootCA) { void NetworkClientSecure::setCACert(const char *rootCA) {
if (_ca_cert_free && _CA_cert) {
free((void *)_CA_cert);
_ca_cert_free = false;
}
_CA_cert = rootCA; _CA_cert = rootCA;
_use_insecure = false; _use_insecure = false;
} }
...@@ -327,10 +350,18 @@ void NetworkClientSecure::setCACertBundle(const uint8_t *bundle) { ...@@ -327,10 +350,18 @@ void NetworkClientSecure::setCACertBundle(const uint8_t *bundle) {
} }
void NetworkClientSecure::setCertificate(const char *client_ca) { void NetworkClientSecure::setCertificate(const char *client_ca) {
if (_cert_free && _cert) {
free((void *)_cert);
_cert_free = false;
}
_cert = client_ca; _cert = client_ca;
} }
void NetworkClientSecure::setPrivateKey(const char *private_key) { void NetworkClientSecure::setPrivateKey(const char *private_key) {
if (_private_key_free && _private_key) {
free((void *)_private_key);
_private_key_free = false;
}
_private_key = private_key; _private_key = private_key;
} }
...@@ -369,6 +400,7 @@ bool NetworkClientSecure::loadCACert(Stream &stream, size_t size) { ...@@ -369,6 +400,7 @@ bool NetworkClientSecure::loadCACert(Stream &stream, size_t size) {
bool ret = false; bool ret = false;
if (dest) { if (dest) {
setCACert(dest); setCACert(dest);
_ca_cert_free = true;
ret = true; ret = true;
} }
return ret; return ret;
...@@ -382,6 +414,7 @@ bool NetworkClientSecure::loadCertificate(Stream &stream, size_t size) { ...@@ -382,6 +414,7 @@ bool NetworkClientSecure::loadCertificate(Stream &stream, size_t size) {
bool ret = false; bool ret = false;
if (dest) { if (dest) {
setCertificate(dest); setCertificate(dest);
_cert_free = true;
ret = true; ret = true;
} }
return ret; return ret;
...@@ -395,6 +428,7 @@ bool NetworkClientSecure::loadPrivateKey(Stream &stream, size_t size) { ...@@ -395,6 +428,7 @@ bool NetworkClientSecure::loadPrivateKey(Stream &stream, size_t size) {
bool ret = false; bool ret = false;
if (dest) { if (dest) {
setPrivateKey(dest); setPrivateKey(dest);
_private_key_free = true;
ret = true; ret = true;
} }
return ret; return ret;
......
...@@ -31,7 +31,10 @@ protected: ...@@ -31,7 +31,10 @@ protected:
std::shared_ptr<sslclient_context> sslclient; std::shared_ptr<sslclient_context> sslclient;
bool _use_insecure; bool _use_insecure;
bool _stillinPlainStart = false; bool _stillinPlainStart;
bool _ca_cert_free;
bool _cert_free;
bool _private_key_free;
const char *_CA_cert; const char *_CA_cert;
const char *_cert; const char *_cert;
const char *_private_key; const char *_private_key;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment