Skip to content

Commit

Permalink
Garbage collect routines with:
Browse files Browse the repository at this point in the history
- thread safety
- configurable gc time
- added dependency to libtimer

Signed-off-by: AssemblyJohn <[email protected]>
  • Loading branch information
AssemblyJohn committed Feb 13, 2024
1 parent f6fe092 commit b165517
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 83 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ if (NOT DISABLE_EDM)
set(EVSE_SECURITY_INSTALL OFF)
else()
find_package(everest-log REQUIRED)
find_package(everest-timer REQUIRED)
endif()

option(LIBEVSE_SECURITY_USE_BOOST_FILESYSTEM "Usage of boost/filesystem.hpp instead of std::filesystem" OFF)
Expand Down
5 changes: 4 additions & 1 deletion dependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ liblog:
git: https://github.com/EVerest/liblog.git
git_tag: v0.2.1
options: ["BUILD_EXAMPLES OFF"]

libtimer:
git: https://github.com/EVerest/libtimer.git
git_tag: v0.1.1
options: ["BUILD_EXAMPLES OFF"]
gtest:
# GoogleTest now follows the Abseil Live at Head philosophy. We recommend updating to the latest commit in the main branch as often as possible.
git: https://github.com/google/googletest.git
Expand Down
28 changes: 21 additions & 7 deletions include/evse_security/evse_security.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright Pionix GmbH and Contributors to EVerest
#pragma once

#include <everest/timer.hpp>

#include <evse_security/evse_types.hpp>
#include <evse_security/utils/evse_filesystem_types.hpp>

Expand Down Expand Up @@ -44,8 +46,11 @@ static constexpr std::uintmax_t DEFAULT_MAX_FILESYSTEM_SIZE = 1024 * 1024 * 50;
// Default maximum 2000 certificate entries
static constexpr std::uintmax_t DEFAULT_MAX_CERTIFICATE_ENTRIES = 2000;

// Expiry for CSRs that did not receive a response CSR, 10 minutes or reboot
static std::chrono::seconds DEFAULT_CSR_EXPIRY(10 * 60);
// Expiry for CSRs that did not receive a response CSR, 60 minutes
static std::chrono::seconds DEFAULT_CSR_EXPIRY(3600);

// Garbage collect default time, 20 minutes
static std::chrono::seconds DEFAULT_GARBAGE_COLLECT_TIME(20 * 60);

/// @brief This class holds filesystem paths to CA bundle file locations and directories for leaf certificates
class EvseSecurity {
Expand All @@ -59,7 +64,8 @@ class EvseSecurity {
EvseSecurity(const FilePaths& file_paths, const std::optional<std::string>& private_key_password = std::nullopt,
const std::optional<std::uintmax_t>& max_fs_usage_bytes = std::nullopt,
const std::optional<std::uintmax_t>& max_fs_certificate_store_entries = std::nullopt,
const std::optional<std::chrono::seconds>& csr_expiry = std::nullopt);
const std::optional<std::chrono::seconds>& csr_expiry = std::nullopt,
const std::optional<std::chrono::seconds>& garbage_collect_time = std::nullopt);

/// @brief Destructor
~EvseSecurity();
Expand Down Expand Up @@ -167,8 +173,7 @@ class EvseSecurity {

/// @brief Collects and deletes unfulfilled CSR private keys. If also deleting the expired
/// certificates, make sure the system clock is properly set for detecting expired certificates
/// @param delete_expired if the expired certificates should be deleted
void garbage_collect(bool delete_expired_certificates);
void garbage_collect();

/// @brief Verifies the file at the given \p path using the provided \p signing_certificate and \p signature
/// @param path
Expand All @@ -179,12 +184,16 @@ class EvseSecurity {
const std::string signature);

private:
// Internal versions of the functions do not lock the mutex
InstallCertificateResult verify_certificate_internal(const std::string& certificate_chain,
LeafCertificateType certificate_type);
GetKeyPairResult get_key_pair_internal(LeafCertificateType certificate_type, EncodingFormat encoding);

/// @brief Determines if the total filesize of certificates is > than the max_filesystem_usage bytes
bool is_filesystem_full();

private:
// TODO(ioan): implement library thread-safety
std::mutex security_mutex;
static std::mutex security_mutex;

// why not reusing the FilePaths here directly (storage duplication)
std::map<CaCertificateType, fs::path> ca_bundle_path_map;
Expand All @@ -200,6 +209,11 @@ class EvseSecurity {
std::uintmax_t max_fs_certificate_store_entries;
// Default csr expiry in seconds
std::chrono::seconds csr_expiry;
// Default time to garbage collect
std::chrono::seconds garbage_collect_time;

// GC timer
Everest::SteadyTimer garbage_collect_timer;

// FIXME(piet): map passwords to encrypted private key files
// is there only one password for all private keys?
Expand Down
2 changes: 2 additions & 0 deletions lib/evse_security/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ endif()
#############

target_link_libraries(evse_security
PUBLIC
everest::timer
PRIVATE
OpenSSL::SSL
OpenSSL::Crypto
Expand Down
Loading

0 comments on commit b165517

Please sign in to comment.