Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try to allow certificate-chains #8859

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/base/tlsstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ std::shared_ptr<X509> UnbufferedAsioTlsStream::GetPeerCertificate()
return std::shared_ptr<X509>(SSL_get_peer_certificate(native_handle()), X509_free);
}

STACK_OF(X509) *UnbufferedAsioTlsStream::GetPeerCertificateChain()
{
return SSL_get_peer_cert_chain(native_handle());
}

void UnbufferedAsioTlsStream::BeforeHandshake(handshake_type type)
{
namespace ssl = boost::asio::ssl;
Expand Down
1 change: 1 addition & 0 deletions lib/base/tlsstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class UnbufferedAsioTlsStream : public AsioTcpTlsStream
bool IsVerifyOK() const;
String GetVerifyError() const;
std::shared_ptr<X509> GetPeerCertificate();
STACK_OF(X509) *GetPeerCertificateChain();

template<class... Args>
inline
Expand Down
8 changes: 6 additions & 2 deletions lib/base/tlsutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,8 +948,10 @@ String BinaryToHex(const unsigned char* data, size_t length) {
return output;
}

bool VerifyCertificate(const std::shared_ptr<X509> &caCertificate, const std::shared_ptr<X509> &certificate, const String& crlFile)
bool VerifyCertificate(const String& caFile, const std::shared_ptr<X509> &certificate, const String& crlFile, STACK_OF(X509) *chain)
{
std::shared_ptr<X509> caCertificate = GetX509Certificate(caFile);

X509_STORE *store = X509_STORE_new();

if (!store)
Expand All @@ -961,8 +963,10 @@ bool VerifyCertificate(const std::shared_ptr<X509> &caCertificate, const std::sh
AddCRLToSSLContext(store, crlFile);
}

X509_STORE_load_locations(store, caFile.CStr(), NULL); /* ignore any errors for the moment, since this is just the convenient way to add full chain */

X509_STORE_CTX *csc = X509_STORE_CTX_new();
X509_STORE_CTX_init(csc, store, certificate.get(), nullptr);
X509_STORE_CTX_init(csc, store, certificate.get(), chain);

int rc = X509_verify_cert(csc);

Expand Down
2 changes: 1 addition & 1 deletion lib/base/tlsutility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ String SHA256(const String& s);
String RandomString(int length);
String BinaryToHex(const unsigned char* data, size_t length);

bool VerifyCertificate(const std::shared_ptr<X509>& caCertificate, const std::shared_ptr<X509>& certificate, const String& crlFile);
bool VerifyCertificate(const String& caFile, const std::shared_ptr<X509>& certificate, const String& crlFile, STACK_OF(X509) *chain = nullptr);
bool IsCa(const std::shared_ptr<X509>& cacert);
int GetCertificateVersion(const std::shared_ptr<X509>& cert);
String GetSignatureAlgorithm(const std::shared_ptr<X509>& cert);
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/pkiverifycommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ int PKIVerifyCommand::Run(const boost::program_options::variables_map& vm, const
bool signedByCA;

try {
signedByCA = VerifyCertificate(cacert, cert, crlFile);
signedByCA = VerifyCertificate(caCertFile, cert, crlFile);
} catch (const std::exception& ex) {
Log logmsg (LogCritical, "cli");
logmsg << "CRITICAL: Certificate with CN '" << certCN << "' is NOT signed by CA: ";
Expand Down
2 changes: 1 addition & 1 deletion lib/remote/apilistener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ std::shared_ptr<X509> ApiListener::RenewCert(const std::shared_ptr<X509>& cert)
{
std::shared_ptr<EVP_PKEY> pubkey (X509_get_pubkey(cert.get()), EVP_PKEY_free);
auto subject (X509_get_subject_name(cert.get()));
auto cacert (GetX509Certificate(GetDefaultCaPath()));
auto cacert (GetDefaultCaPath());
auto newcert (CreateCertIcingaCA(pubkey.get(), subject));

/* verify that the new cert matches the CA we're using for the ApiListener;
Expand Down
5 changes: 4 additions & 1 deletion lib/remote/jsonrpcconnection-pki.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ Value RequestCertificateHandler(const MessageOrigin::Ptr& origin, const Dictiona
String certText = params->Get("cert_request");

std::shared_ptr<X509> cert;
STACK_OF(X509) *chain;

Dictionary::Ptr result = new Dictionary();

/* Use the presented client certificate if not provided. */
if (certText.IsEmpty()) {
auto stream (origin->FromClient->GetStream());
cert = stream->next_layer().GetPeerCertificate();
chain = stream->next_layer().GetPeerCertificateChain();
} else {
cert = StringToCertificate(certText);
chain = nullptr;
}

if (!cert) {
Expand All @@ -61,7 +64,7 @@ Value RequestCertificateHandler(const MessageOrigin::Ptr& origin, const Dictiona
logmsg << "Received certificate request for CN '" << cn << "'";

try {
signedByCA = VerifyCertificate(cacert, cert, listener->GetCrlPath());
signedByCA = VerifyCertificate(listener->GetDefaultCaPath(), cert, listener->GetCrlPath(), chain);
if (!signedByCA) {
logmsg << " not";
}
Expand Down