Skip to content

Commit 9ad5683

Browse files
committed
VerifyCertificate: Work around issue in OpenSSL < 1.1.0 causing invalid certifcates being treated as valid
Old versions of OpenSSL stored a valid flag in the certificate (see inline code comment for details) that if already set, causes parts of the verification to be skipped and return that the certificate is valid, even if it's not actually signed by the CA in the trust store. This issue was assigned CVE-2025-48057.
1 parent d59755a commit 9ad5683

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

lib/base/tlsutility.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,19 +983,42 @@ String BinaryToHex(const unsigned char* data, size_t length) {
983983

984984
bool VerifyCertificate(const std::shared_ptr<X509> &caCertificate, const std::shared_ptr<X509> &certificate, const String& crlFile)
985985
{
986+
return VerifyCertificate(caCertificate.get(), certificate.get(), crlFile);
987+
}
988+
989+
bool VerifyCertificate(X509* caCertificate, X509* certificate, const String& crlFile)
990+
{
991+
#if OPENSSL_VERSION_NUMBER < 0x10100000L
992+
/*
993+
* OpenSSL older than version 1.1.0 stored a valid flag in the struct behind X509* which leads to certain validation
994+
* steps to be skipped on subsequent verification operations. If a certificate is verified multiple times with a
995+
* different configuration, for example with different trust anchors, this can result in the certificate
996+
* incorrectly being treated as valid.
997+
*
998+
* This issue is worked around by serializing and deserializing the certificate which creates a new struct instance
999+
* with the valid flag cleared, hence performing the full validation.
1000+
*
1001+
* The flag in question was removed in OpenSSL 1.1.0, so this extra step isn't necessary for more recent versions:
1002+
* https://github.com/openssl/openssl/commit/0e76014e584ba78ef1d6ecb4572391ef61c4fb51
1003+
*/
1004+
std::shared_ptr<X509> copy = StringToCertificate(CertificateToString(certificate));
1005+
VERIFY(copy.get() != certificate);
1006+
certificate = copy.get();
1007+
#endif
1008+
9861009
std::unique_ptr<X509_STORE, decltype(&X509_STORE_free)> store{X509_STORE_new(), &X509_STORE_free};
9871010

9881011
if (!store)
9891012
return false;
9901013

991-
X509_STORE_add_cert(store.get(), caCertificate.get());
1014+
X509_STORE_add_cert(store.get(), caCertificate);
9921015

9931016
if (!crlFile.IsEmpty()) {
9941017
AddCRLToSSLContext(store.get(), crlFile);
9951018
}
9961019

9971020
std::unique_ptr<X509_STORE_CTX, decltype(&X509_STORE_CTX_free)> csc{X509_STORE_CTX_new(), &X509_STORE_CTX_free};
998-
X509_STORE_CTX_init(csc.get(), store.get(), certificate.get(), nullptr);
1021+
X509_STORE_CTX_init(csc.get(), store.get(), certificate, nullptr);
9991022

10001023
int rc = X509_verify_cert(csc.get());
10011024

lib/base/tlsutility.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ String RandomString(int length);
7979
String BinaryToHex(const unsigned char* data, size_t length);
8080

8181
bool VerifyCertificate(const std::shared_ptr<X509>& caCertificate, const std::shared_ptr<X509>& certificate, const String& crlFile);
82+
bool VerifyCertificate(X509* caCertificate, X509* certificate, const String& crlFile);
8283
bool IsCa(const std::shared_ptr<X509>& cacert);
8384
int GetCertificateVersion(const std::shared_ptr<X509>& cert);
8485
String GetSignatureAlgorithm(const std::shared_ptr<X509>& cert);

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ add_boost_test(base
123123
base_tlsutility/iscertuptodate_ok
124124
base_tlsutility/iscertuptodate_expiring
125125
base_tlsutility/iscertuptodate_old
126+
base_tlsutility/VerifyCertificate_revalidate
126127
base_type/gettype
127128
base_type/assign
128129
base_type/byname

test/base-tlsutility.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,24 @@ BOOST_AUTO_TEST_CASE(iscertuptodate_old)
132132
})));
133133
}
134134

135+
BOOST_AUTO_TEST_CASE(VerifyCertificate_revalidate)
136+
{
137+
X509_NAME *caSubject = X509_NAME_new();
138+
X509_NAME_add_entry_by_txt(caSubject, "CN", MBSTRING_ASC, (const unsigned char*)"Icinga CA", -1, -1, 0);
139+
140+
auto signingCaKey = GenKeypair();
141+
auto signingCaCert = CreateCert(signingCaKey, caSubject, caSubject, signingCaKey, true);
142+
143+
X509_NAME *leafSubject = X509_NAME_new();
144+
X509_NAME_add_entry_by_txt(leafSubject, "CN", MBSTRING_ASC, (const unsigned char*)"Leaf Certificate", -1, -1, 0);
145+
auto leafKey = GenKeypair();
146+
auto leafCert = CreateCert(leafKey, leafSubject, caSubject, signingCaKey, false);
147+
BOOST_CHECK(VerifyCertificate(signingCaCert, leafCert, ""));
148+
149+
// Create a second CA with a different key, the leaf certificate is supposed to fail validation against that CA.
150+
auto otherCaKey = GenKeypair();
151+
auto otherCaCert = CreateCert(otherCaKey, caSubject, caSubject, otherCaKey, true);
152+
BOOST_CHECK_THROW(VerifyCertificate(otherCaCert, leafCert, ""), openssl_error);
153+
}
154+
135155
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)