From 2fd8d75f43e29585fde7501b79180e57f960e001 Mon Sep 17 00:00:00 2001 From: pietfried Date: Fri, 29 Sep 2023 14:36:24 +0200 Subject: [PATCH] added implementation for enum to string conversions Signed-off-by: pietfried --- include/types.hpp | 13 ++++ lib/CMakeLists.txt | 1 + lib/evse_security.cpp | 11 ++-- lib/types.cpp | 147 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+), 5 deletions(-) create mode 100644 lib/types.cpp diff --git a/include/types.hpp b/include/types.hpp index 304ee17..4f62fae 100644 --- a/include/types.hpp +++ b/include/types.hpp @@ -120,6 +120,19 @@ struct GetKeyPairResult { std::optional pair; }; +namespace conversions { +std::string encoding_format_to_string(EncodingFormat e); +std::string ca_certificate_type_to_string(CaCertificateType e); +std::string leaf_certificate_type_to_string(LeafCertificateType e); +std::string certificate_type_to_string(CertificateType e); +std::string hash_algorithm_to_string(HashAlgorithm e); +std::string install_certificate_result_to_string(InstallCertificateResult e); +std::string delete_certificate_result_to_string(DeleteCertificateResult e); +std::string get_installed_certificates_status_to_string(GetInstalledCertificatesStatus e); +std::string get_key_pair_status_to_string(GetKeyPairStatus e); + +} // namespace conversions + } // namespace evse_security #endif // TYPES_HPP diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 514c5fc..68e3bf4 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -6,6 +6,7 @@ target_sources(evse_security PRIVATE evse_security.cpp x509_wrapper.cpp + types.cpp ) target_include_directories(evse_security diff --git a/lib/evse_security.cpp b/lib/evse_security.cpp index 1bfc219..e339a4e 100644 --- a/lib/evse_security.cpp +++ b/lib/evse_security.cpp @@ -181,7 +181,7 @@ static CertificateType get_certificate_type(const CaCertificateType ca_certifica static std::string get_random_file_name(const std::string& extension) { char path[] = "XXXXXX"; - mktemp(path); + mkstemp(path); return std::string(path) + extension; } @@ -269,11 +269,12 @@ EvseSecurity::EvseSecurity(const FilePaths& file_paths, const std::optionalca_bundle_path_map) { if (!std::filesystem::exists(pair.second)) { - throw std::runtime_error("Could not find configured " + std::to_string((int)pair.first) + - " bundle file at: " + file_paths.csms_ca_bundle.string()); + throw std::runtime_error("Could not find configured " + + conversions::ca_certificate_type_to_string(pair.first) + + " bundle file at: " + pair.second.string()); } else if (std::filesystem::is_directory(pair.second)) { - throw std::runtime_error("Provided bundle " + std::to_string((int)pair.first) + - " is directory: " + file_paths.csms_ca_bundle.string()); + throw std::runtime_error("Provided bundle " + conversions::ca_certificate_type_to_string(pair.first) + + " is directory: " + pair.second.string()); } } diff --git a/lib/types.cpp b/lib/types.cpp new file mode 100644 index 0000000..469965d --- /dev/null +++ b/lib/types.cpp @@ -0,0 +1,147 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright Pionix GmbH and Contributors to EVerest + +#include + +namespace evse_security { + +namespace conversions { + +std::string encoding_format_to_string(EncodingFormat e) { + switch (e) { + case EncodingFormat::DER: + return "DER"; + case EncodingFormat::PEM: + return "PEM"; + default: + throw std::out_of_range("Could not convert EncodingFormat to string"); + } +}; + +std::string ca_certificate_type_to_string(CaCertificateType e) { + switch (e) { + case CaCertificateType::V2G: + return "V2G"; + case CaCertificateType::MO: + return "MO"; + case CaCertificateType::CSMS: + return "CSMS"; + case CaCertificateType::MF: + return "MF"; + default: + throw std::out_of_range("Could not convert CaCertificateType to string"); + } +}; + +std::string leaf_certificate_type_to_string(LeafCertificateType e) { + switch (e) { + case LeafCertificateType::CSMS: + return "CSMS"; + case LeafCertificateType::V2G: + return "V2G"; + case LeafCertificateType::MF: + return "MF"; + default: + throw std::out_of_range("Could not convert LeafCertificateType to string"); + } +}; + +std::string certificate_type_to_string(CertificateType e) { + switch (e) { + case CertificateType::V2GRootCertificate: + return "V2GRootCertificate"; + case CertificateType::MORootCertificate: + return "MORootCertificate"; + case CertificateType::CSMSRootCertificate: + return "CSMSRootCertificate"; + case CertificateType::V2GCertificateChain: + return "V2GCertificateChain"; + case CertificateType::MFRootCertificate: + return "MFRootCertificate"; + default: + throw std::out_of_range("Could not convert CertificateType to string"); + } +}; + +std::string hash_algorithm_to_string(HashAlgorithm e) { + switch (e) { + case HashAlgorithm::SHA256: + return "SHA256"; + case HashAlgorithm::SHA384: + return "SHA384"; + case HashAlgorithm::SHA512: + return "SHA512"; + default: + throw std::out_of_range("Could not convert HashAlgorithm to string"); + } +}; + +std::string install_certificate_result_to_string(InstallCertificateResult e) { + switch (e) { + case InstallCertificateResult::InvalidSignature: + return "InvalidSignature"; + case InstallCertificateResult::InvalidCertificateChain: + return "InvalidCertificateChain"; + case InstallCertificateResult::InvalidFormat: + return "InvalidFormat"; + case InstallCertificateResult::InvalidCommonName: + return "InvalidCommonName"; + case InstallCertificateResult::NoRootCertificateInstalled: + return "NoRootCertificateInstalled"; + case InstallCertificateResult::Expired: + return "Expired"; + case InstallCertificateResult::CertificateStoreMaxLengthExceeded: + return "CertificateStoreMaxLengthExceeded"; + case InstallCertificateResult::WriteError: + return "WriteError"; + case InstallCertificateResult::Accepted: + return "Accepted"; + default: + throw std::out_of_range("Could not convert InstallCertificateResult to string"); + } +}; + +std::string delete_certificate_result_to_string(DeleteCertificateResult e) { + switch (e) { + case DeleteCertificateResult::Accepted: + return "Accepted"; + case DeleteCertificateResult::Failed: + return "Failed"; + case DeleteCertificateResult::NotFound: + return "NotFound"; + default: + throw std::out_of_range("Could not convert DeleteCertificateResult to string"); + } +}; + +std::string get_installed_certificates_status_to_string(GetInstalledCertificatesStatus e) { + switch (e) { + case GetInstalledCertificatesStatus::Accepted: + return "Accepted"; + case GetInstalledCertificatesStatus::NotFound: + return "NotFound"; + default: + throw std::out_of_range("Could not convert GetInstalledCertificatesStatus to string"); + } +}; + +std::string get_key_pair_status_to_string(GetKeyPairStatus e) { + switch (e) { + case GetKeyPairStatus::Accepted: + return "Accepted"; + case GetKeyPairStatus::Rejected: + return "Rejected"; + case GetKeyPairStatus::NotFound: + return "NotFound"; + case GetKeyPairStatus::NotFoundValid: + return "NotFoundValid"; + case GetKeyPairStatus::PrivateKeyNotFound: + return "PrivateKeyNotFound"; + default: + throw std::out_of_range("Could not convert GetKeyPairStatus to string"); + } +}; + +} // namespace conversions + +} // namespace evse_security