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

Added implementation for enum to string conversions #5

Merged
merged 1 commit into from
Sep 29, 2023
Merged
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
13 changes: 13 additions & 0 deletions include/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ struct GetKeyPairResult {
std::optional<KeyPair> 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
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ target_sources(evse_security
PRIVATE
evse_security.cpp
x509_wrapper.cpp
types.cpp
)

target_include_directories(evse_security
Expand Down
11 changes: 6 additions & 5 deletions lib/evse_security.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -269,11 +269,12 @@ EvseSecurity::EvseSecurity(const FilePaths& file_paths, const std::optional<std:

for (const auto& pair : this->ca_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());
}
}

Expand Down
147 changes: 147 additions & 0 deletions lib/types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Pionix GmbH and Contributors to EVerest

#include <types.hpp>

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