From 722d54f6b74c2e97f319bc5a85f64fef83047d6b Mon Sep 17 00:00:00 2001 From: James Chapman Date: Fri, 7 Jun 2024 08:23:08 +0100 Subject: [PATCH] fix: addressing PR review comment change to provide utility function to create Handle Signed-off-by: James Chapman --- .../crypto/openssl/openssl_supplier.hpp | 4 +++ .../crypto/openssl/openssl_supplier.cpp | 33 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/include/evse_security/crypto/openssl/openssl_supplier.hpp b/include/evse_security/crypto/openssl/openssl_supplier.hpp index b0fe169..cd34813 100644 --- a/include/evse_security/crypto/openssl/openssl_supplier.hpp +++ b/include/evse_security/crypto/openssl/openssl_supplier.hpp @@ -3,6 +3,7 @@ #pragma once #include +#include namespace evse_security { @@ -51,6 +52,9 @@ class OpenSSLSupplier : public AbstractCryptoSupplier { static bool base64_encode_from_bytes(const std::vector& bytes, std::string& out_encoded); static bool base64_encode_from_string(const std::string& string, std::string& out_encoded); + + static X509Handle X509Handle_from_X509(X509* certificate); + static X509Handle_ptr X509Handle_ptr_from_X509(X509* certificate); }; } // namespace evse_security \ No newline at end of file diff --git a/lib/evse_security/crypto/openssl/openssl_supplier.cpp b/lib/evse_security/crypto/openssl/openssl_supplier.cpp index de2be01..6f48b2d 100644 --- a/lib/evse_security/crypto/openssl/openssl_supplier.cpp +++ b/lib/evse_security/crypto/openssl/openssl_supplier.cpp @@ -2,7 +2,6 @@ // Copyright Pionix GmbH and Contributors to EVerest #include -#include #include #include @@ -27,6 +26,30 @@ namespace evse_security { +struct X509HandleOpenSSL : public X509Handle { + X509HandleOpenSSL(X509* certificate) : x509(certificate) { + } + + X509* get() { + return x509.get(); + } + +private: + X509_ptr x509; +}; + +struct KeyHandleOpenSSL : public KeyHandle { + KeyHandleOpenSSL(EVP_PKEY* key) : key(key) { + } + + EVP_PKEY* get() { + return key.get(); + } + +private: + EVP_PKEY_ptr key; +}; + static X509* get(X509Handle* handle) { if (X509HandleOpenSSL* ssl_handle = dynamic_cast(handle)) { return ssl_handle->get(); @@ -901,4 +924,12 @@ bool OpenSSLSupplier::base64_encode_from_string(const std::string& string, std:: return base64_encode(reinterpret_cast(string.data()), string.size(), out_encoded); } +X509Handle OpenSSLSupplier::X509Handle_from_X509(X509* certificate) { + return X509HandleOpenSSL(certificate); +} + +X509Handle_ptr OpenSSLSupplier::X509Handle_ptr_from_X509(X509* certificate) { + return std::make_unique(certificate); +} + } // namespace evse_security