Skip to content

Commit 56f4abf

Browse files
julianbrostyhabteab
authored andcommitted
VerifyCertificate: fix use after free
`X509_STORE_CTX_get_error(csc)` was called after `X509_STORE_CTX_free(csc)`. This is fixed by automatically freeing variables at the end of the function using `std::unique_ptr`.
1 parent d512c77 commit 56f4abf

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

lib/base/tlsutility.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -975,27 +975,24 @@ String BinaryToHex(const unsigned char* data, size_t length) {
975975

976976
bool VerifyCertificate(const std::shared_ptr<X509> &caCertificate, const std::shared_ptr<X509> &certificate, const String& crlFile)
977977
{
978-
X509_STORE *store = X509_STORE_new();
978+
std::unique_ptr<X509_STORE, decltype(&X509_STORE_free)> store{X509_STORE_new(), &X509_STORE_free};
979979

980980
if (!store)
981981
return false;
982982

983-
X509_STORE_add_cert(store, caCertificate.get());
983+
X509_STORE_add_cert(store.get(), caCertificate.get());
984984

985985
if (!crlFile.IsEmpty()) {
986-
AddCRLToSSLContext(store, crlFile);
986+
AddCRLToSSLContext(store.get(), crlFile);
987987
}
988988

989-
X509_STORE_CTX *csc = X509_STORE_CTX_new();
990-
X509_STORE_CTX_init(csc, store, certificate.get(), nullptr);
989+
std::unique_ptr<X509_STORE_CTX, decltype(&X509_STORE_CTX_free)> csc{X509_STORE_CTX_new(), &X509_STORE_CTX_free};
990+
X509_STORE_CTX_init(csc.get(), store.get(), certificate.get(), nullptr);
991991

992-
int rc = X509_verify_cert(csc);
993-
994-
X509_STORE_CTX_free(csc);
995-
X509_STORE_free(store);
992+
int rc = X509_verify_cert(csc.get());
996993

997994
if (rc == 0) {
998-
int err = X509_STORE_CTX_get_error(csc);
995+
int err = X509_STORE_CTX_get_error(csc.get());
999996

1000997
BOOST_THROW_EXCEPTION(openssl_error()
1001998
<< boost::errinfo_api_function("X509_verify_cert")

0 commit comments

Comments
 (0)