Skip to content

Commit 17b001f

Browse files
committed
process certificate chains presented by the client
Similar to Icinga#8859 this patch works around Icinga#7719 by allowing the intermediate certificate presented by the icinga2-agent. To make this work the icinga2-master only holds to root-ca in its local ca.crt, while the icinga2-agent has the intermediate-cert in its local ca.crt (or the intermediate together with the root in the ca.crt / or the intermediate in the cert.pem - doesn't matter).
1 parent 528d165 commit 17b001f

File tree

6 files changed

+14
-10
lines changed

6 files changed

+14
-10
lines changed

lib/base/tlsutility.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -983,12 +983,12 @@ String BinaryToHex(const unsigned char* data, size_t length) {
983983
return output;
984984
}
985985

986-
bool VerifyCertificate(const std::shared_ptr<X509> &caCertificate, const std::shared_ptr<X509> &certificate, const String& crlFile)
986+
bool VerifyCertificate(const std::shared_ptr<X509> &caCertificate, const std::shared_ptr<X509> &certificate, const String& crlFile, const String& caBundleFile)
987987
{
988-
return VerifyCertificate(caCertificate.get(), certificate.get(), crlFile);
988+
return VerifyCertificate(caCertificate.get(), certificate.get(), crlFile, caBundleFile);
989989
}
990990

991-
bool VerifyCertificate(X509* caCertificate, X509* certificate, const String& crlFile)
991+
bool VerifyCertificate(X509* caCertificate, X509* certificate, const String& crlFile, const String& caBundleFile)
992992
{
993993
#if OPENSSL_VERSION_NUMBER < 0x10100000L
994994
/*
@@ -1019,6 +1019,10 @@ bool VerifyCertificate(X509* caCertificate, X509* certificate, const String& crl
10191019
AddCRLToSSLContext(store.get(), crlFile);
10201020
}
10211021

1022+
if (!caBundleFile.IsEmpty()) {
1023+
X509_STORE_load_locations(store.get(), caBundleFile.CStr(), NULL); /* ignore any errors for the moment, since this is just the convenient way to add full chain */
1024+
}
1025+
10221026
std::unique_ptr<X509_STORE_CTX, decltype(&X509_STORE_CTX_free)> csc{X509_STORE_CTX_new(), &X509_STORE_CTX_free};
10231027
X509_STORE_CTX_init(csc.get(), store.get(), certificate, nullptr);
10241028

lib/base/tlsutility.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ String SHA256(const String& s);
7878
String RandomString(int length);
7979
String BinaryToHex(const unsigned char* data, size_t length);
8080

81-
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);
81+
bool VerifyCertificate(const std::shared_ptr<X509> &caCertificate, const std::shared_ptr<X509> &certificate, const String& crlFile, const String& caBundleFile);
82+
bool VerifyCertificate(X509* caCertificate, X509* certificate, const String& crlFile, const String& caBundleFile);
8383
bool IsCa(const std::shared_ptr<X509>& cacert);
8484
int GetCertificateVersion(const std::shared_ptr<X509>& cert);
8585
String GetSignatureAlgorithm(const std::shared_ptr<X509>& cert);

lib/cli/pkiverifycommand.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ int PKIVerifyCommand::Run(const boost::program_options::variables_map& vm, const
130130
bool signedByCA;
131131

132132
try {
133-
signedByCA = VerifyCertificate(cacert, cert, crlFile);
133+
signedByCA = VerifyCertificate(cacert, cert, crlFile, caCertFile);
134134
} catch (const std::exception& ex) {
135135
Log logmsg (LogCritical, "cli");
136136
logmsg << "CRITICAL: Certificate with CN '" << certCN << "' is NOT signed by CA: ";

lib/remote/apilistener.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ std::shared_ptr<X509> ApiListener::RenewCert(const std::shared_ptr<X509>& cert,
193193
* we're using for cluster connections (there's no point in sending a client
194194
* a certificate it wouldn't be able to use to connect to us anyway) */
195195
try {
196-
if (!VerifyCertificate(cacert, newcert, GetCrlPath())) {
196+
if (!VerifyCertificate(cacert, newcert, GetCrlPath(), GetDefaultCaPath())) {
197197
Log(LogWarning, "ApiListener")
198198
<< "The CA in '" << GetDefaultCaPath() << "' does not match the CA which Icinga uses "
199199
<< "for its own cluster connections. This is most likely a configuration problem.";

lib/remote/jsonrpcconnection-pki.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Value RequestCertificateHandler(const MessageOrigin::Ptr& origin, const Dictiona
6262
logmsg << "Received certificate request for CN '" << cn << "'";
6363

6464
try {
65-
signedByCA = VerifyCertificate(cacert, cert, listener->GetCrlPath());
65+
signedByCA = VerifyCertificate(cacert, cert, listener->GetCrlPath(), listener->GetDefaultCaPath());
6666
if (!signedByCA) {
6767
logmsg << " not";
6868
}

test/base-tlsutility.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ BOOST_AUTO_TEST_CASE(VerifyCertificate_revalidate)
144144
X509_NAME_add_entry_by_txt(leafSubject, "CN", MBSTRING_ASC, (const unsigned char*)"Leaf Certificate", -1, -1, 0);
145145
auto leafKey = GenKeypair();
146146
auto leafCert = CreateCert(leafKey, leafSubject, caSubject, signingCaKey, false);
147-
BOOST_CHECK(VerifyCertificate(signingCaCert, leafCert, ""));
147+
BOOST_CHECK(VerifyCertificate(signingCaCert, leafCert, "", ""));
148148

149149
// Create a second CA with a different key, the leaf certificate is supposed to fail validation against that CA.
150150
auto otherCaKey = GenKeypair();
151151
auto otherCaCert = CreateCert(otherCaKey, caSubject, caSubject, otherCaKey, true);
152-
BOOST_CHECK_THROW(VerifyCertificate(otherCaCert, leafCert, ""), openssl_error);
152+
BOOST_CHECK_THROW(VerifyCertificate(otherCaCert, leafCert, "", ""), openssl_error);
153153
}
154154

155155
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)