Skip to content

Commit

Permalink
Latest valid retrieval fix
Browse files Browse the repository at this point in the history
Signed-off-by: ioanbogdan <[email protected]>
  • Loading branch information
ioanbogdan committed Oct 23, 2023
1 parent 05fab52 commit c36d730
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 6 additions & 1 deletion include/x509_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ class X509Wrapper {
/// @return
std::string get_export_string() const;

/// @brief If the certificate is within the validity date
/// @brief If the certificate is within the validity date. Can return false in 2 cases,
/// if it is expired (current date > valid_to) or if (current data < valid_in), that is
/// we are not in force yet
bool is_valid() const;

/// @brief If the certificate has expired
bool is_expired() const;

public:
X509Wrapper& operator=(X509Wrapper&& other) = default;

Expand Down
9 changes: 7 additions & 2 deletions lib/x509_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void X509Wrapper::update_validity() {
ASN1_TIME* notAfter = X509_get_notAfter(get());

int day, sec;
ASN1_TIME_diff(&day, &sec, notBefore, nullptr);
ASN1_TIME_diff(&day, &sec, nullptr, notBefore);
valid_in = std::chrono::duration_cast<std::chrono::seconds>(ossl_days_to_seconds(day)).count() +
sec; // Convert days to seconds
ASN1_TIME_diff(&day, &sec, nullptr, notAfter);
Expand All @@ -125,7 +125,12 @@ int X509Wrapper::get_valid_to() const {
}

bool X509Wrapper::is_valid() const {
return (get_valid_in() >= 0);
// The valid_in must be in the past and the valid_to must be in the future
return (get_valid_in() <= 0) && (get_valid_to() >= 0);
}

bool X509Wrapper::is_expired() const {
return (get_valid_to() >= 0);
}

std::optional<std::filesystem::path> X509Wrapper::get_file() const {
Expand Down

0 comments on commit c36d730

Please sign in to comment.