Skip to content

Commit

Permalink
Extend openssl wrappers for scitt (#6668)
Browse files Browse the repository at this point in the history
  • Loading branch information
achamayou authored Nov 20, 2024
1 parent ba6d143 commit 7d7abea
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/ds/x509_time_fmt.h → include/ccf/ds/x509_time_fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <time.h>
#include <vector>

namespace ds
namespace ccf::ds
{
static inline std::string to_x509_time_string(const std::tm& time)
{
Expand Down
6 changes: 3 additions & 3 deletions src/crypto/certs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "ccf/crypto/key_pair.h"
#include "ccf/crypto/pem.h"
#include "ds/x509_time_fmt.h"
#include "ccf/ds/x509_time_fmt.h"

#include <chrono>
#include <string>
Expand All @@ -17,9 +17,9 @@ namespace ccf::crypto
using namespace std::chrono_literals;
// Note: As per RFC 5280, the validity period runs until "notAfter"
// _inclusive_ so substract one second from the validity period.
auto valid_to = ::ds::time_point_from_string(valid_from) +
auto valid_to = ccf::ds::time_point_from_string(valid_from) +
std::chrono::days(validity_period_days) - 1s;
return ::ds::to_x509_time_string(valid_to);
return ccf::ds::to_x509_time_string(valid_to);
}

static Pem create_self_signed_cert(
Expand Down
14 changes: 11 additions & 3 deletions src/crypto/openssl/openssl_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

#define FMT_HEADER_ONLY

#include "ccf/ds/x509_time_fmt.h"

#include <chrono>
#include <ds/x509_time_fmt.h>
#include <fmt/format.h>
#include <memory>
#include <openssl/asn1.h>
Expand Down Expand Up @@ -360,15 +361,15 @@ namespace ccf::crypto
Unique_X509_TIME(const std::string& s) :
Unique_SSL_OBJECT(ASN1_TIME_new(), ASN1_TIME_free, /*check_null=*/false)
{
auto t = ::ds::to_x509_time_string(s);
auto t = ccf::ds::to_x509_time_string(s);
CHECK1(ASN1_TIME_set_string(*this, t.c_str()));
CHECK1(ASN1_TIME_normalize(*this));
}
Unique_X509_TIME(ASN1_TIME* t) :
Unique_SSL_OBJECT(t, ASN1_TIME_free, /*check_null=*/false)
{}
Unique_X509_TIME(const std::chrono::system_clock::time_point& t) :
Unique_X509_TIME(::ds::to_x509_time_string(t))
Unique_X509_TIME(ccf::ds::to_x509_time_string(t))
{}
};

Expand Down Expand Up @@ -424,5 +425,12 @@ namespace ccf::crypto
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
};

struct Unique_EVP_PKEY
: public Unique_SSL_OBJECT<EVP_PKEY, EVP_PKEY_new, EVP_PKEY_free>
{
Unique_EVP_PKEY() = default;
Unique_EVP_PKEY(EVP_PKEY* key) : Unique_SSL_OBJECT(key, EVP_PKEY_free) {}
};
}
}
6 changes: 3 additions & 3 deletions src/crypto/openssl/verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ namespace ccf::crypto
const std::chrono::system_clock::time_point& now) const
{
auto [from, to] = validity_period();
auto tp_to = ::ds::time_point_from_string(to);
auto tp_to = ccf::ds::time_point_from_string(to);
return std::chrono::duration_cast<std::chrono::seconds>(tp_to - now)
.count() +
1;
Expand All @@ -218,8 +218,8 @@ namespace ccf::crypto
const std::chrono::system_clock::time_point& now) const
{
auto [from, to] = validity_period();
auto tp_from = ::ds::time_point_from_string(from);
auto tp_to = ::ds::time_point_from_string(to);
auto tp_from = ccf::ds::time_point_from_string(from);
auto tp_to = ccf::ds::time_point_from_string(to);
auto total_sec =
std::chrono::duration_cast<std::chrono::seconds>(tp_to - tp_from)
.count() +
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/openssl/x509_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the Apache 2.0 License.
#pragma once

#include "ds/x509_time_fmt.h"
#include "ccf/ds/x509_time_fmt.h"
#include "openssl_wrappers.h"

#include <openssl/asn1.h>
Expand Down Expand Up @@ -30,6 +30,6 @@ namespace ccf::crypto::OpenSSL
{
std::tm t;
CHECK1(ASN1_TIME_to_tm(time, &t));
return ::ds::to_x509_time_string(t);
return ccf::ds::to_x509_time_string(t);
}
}
62 changes: 31 additions & 31 deletions src/crypto/test/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "ccf/crypto/rsa_key_pair.h"
#include "ccf/crypto/symmetric_key.h"
#include "ccf/crypto/verifier.h"
#include "ccf/ds/x509_time_fmt.h"
#include "crypto/certs.h"
#include "crypto/csr.h"
#include "crypto/openssl/cose_sign.h"
Expand All @@ -21,7 +22,6 @@
#include "crypto/openssl/symmetric_key.h"
#include "crypto/openssl/verifier.h"
#include "crypto/openssl/x509_time.h"
#include "ds/x509_time_fmt.h"

#include <chrono>
#include <cstring>
Expand Down Expand Up @@ -189,7 +189,7 @@ ccf::crypto::Pem generate_self_signed_cert(
constexpr size_t certificate_validity_period_days = 365;
using namespace std::literals;
auto valid_from =
::ds::to_x509_time_string(std::chrono::system_clock::now() - 24h);
ccf::ds::to_x509_time_string(std::chrono::system_clock::now() - 24h);

return ccf::crypto::create_self_signed_cert(
kp, name, {}, valid_from, certificate_validity_period_days);
Expand Down Expand Up @@ -755,71 +755,71 @@ void run_csr(bool corrupt_csr = false)
TEST_CASE("2-digit years")
{
auto time_str = "220405175422Z";
auto tp = ::ds::time_point_from_string(time_str);
auto conv = ::ds::to_x509_time_string(tp);
auto tp = ccf::ds::time_point_from_string(time_str);
auto conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == std::string("20") + time_str);
}

TEST_CASE("Non-ASN.1 timepoint formats")
{
auto time_str = "2022-04-05 18:53:27";
auto tp = ::ds::time_point_from_string(time_str);
auto conv = ::ds::to_x509_time_string(tp);
auto tp = ccf::ds::time_point_from_string(time_str);
auto conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == "20220405185327Z");

time_str = "2022-04-05 18:53:27.190380";
tp = ::ds::time_point_from_string(time_str);
conv = ::ds::to_x509_time_string(tp);
tp = ccf::ds::time_point_from_string(time_str);
conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == "20220405185327Z");

time_str = "2022-04-05 18:53:27 +03:00";
tp = ::ds::time_point_from_string(time_str);
conv = ::ds::to_x509_time_string(tp);
tp = ccf::ds::time_point_from_string(time_str);
conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == "20220405155327Z");

time_str = "2022-04-05 18:53:27 +0300";
tp = ::ds::time_point_from_string(time_str);
conv = ::ds::to_x509_time_string(tp);
tp = ccf::ds::time_point_from_string(time_str);
conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == "20220405155327Z");

time_str = "2022-04-05 18:53:27.190380+03:00";
tp = ::ds::time_point_from_string(time_str);
conv = ::ds::to_x509_time_string(tp);
tp = ccf::ds::time_point_from_string(time_str);
conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == "20220405155327Z");

time_str = "2022-04-05 18:53:27 -03:00";
tp = ::ds::time_point_from_string(time_str);
conv = ::ds::to_x509_time_string(tp);
tp = ccf::ds::time_point_from_string(time_str);
conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == "20220405215327Z");

time_str = "2022-04-07T10:37:49.567612";
tp = ::ds::time_point_from_string(time_str);
conv = ::ds::to_x509_time_string(tp);
tp = ccf::ds::time_point_from_string(time_str);
conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == "20220407103749Z");

time_str = "2022-04-07T10:37:49.567612+03:00";
tp = ::ds::time_point_from_string(time_str);
conv = ::ds::to_x509_time_string(tp);
tp = ccf::ds::time_point_from_string(time_str);
conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == "20220407073749Z");

time_str = "2022-04-07T10:37:49.567612Z";
tp = ::ds::time_point_from_string(time_str);
conv = ::ds::to_x509_time_string(tp);
tp = ccf::ds::time_point_from_string(time_str);
conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == "20220407103749Z");

time_str = "220425165619+0000";
tp = ::ds::time_point_from_string(time_str);
conv = ::ds::to_x509_time_string(tp);
tp = ccf::ds::time_point_from_string(time_str);
conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == "20220425165619Z");

time_str = "220425165619+0200";
tp = ::ds::time_point_from_string(time_str);
conv = ::ds::to_x509_time_string(tp);
tp = ccf::ds::time_point_from_string(time_str);
conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == "20220425145619Z");

time_str = "20220425165619-0300";
tp = ::ds::time_point_from_string(time_str);
conv = ::ds::to_x509_time_string(tp);
tp = ccf::ds::time_point_from_string(time_str);
conv = ccf::ds::to_x509_time_string(tp);
REQUIRE(conv == "20220425195619Z");
}

Expand Down Expand Up @@ -987,9 +987,9 @@ TEST_CASE("x509 time")
auto to = ccf::crypto::OpenSSL::Unique_X509_TIME(adjusted_time);

// Convert to string and back to time_points
auto from_conv = ::ds::time_point_from_string(
auto from_conv = ccf::ds::time_point_from_string(
ccf::crypto::OpenSSL::to_x509_time_string(from));
auto to_conv = ::ds::time_point_from_string(
auto to_conv = ccf::ds::time_point_from_string(
ccf::crypto::OpenSSL::to_x509_time_string(to));

// Diff is still the same amount of days
Expand All @@ -1007,7 +1007,7 @@ TEST_CASE("x509 time")
for (auto const& days_offset : days_offsets)
{
auto adjusted_time = time + std::chrono::days(days_offset);
auto adjusted_str = ::ds::to_x509_time_string(adjusted_time);
auto adjusted_str = ccf::ds::to_x509_time_string(adjusted_time);
auto asn1_time = ccf::crypto::OpenSSL::Unique_X509_TIME(adjusted_str);
auto converted_str = ccf::crypto::OpenSSL::to_x509_time_string(asn1_time);
REQUIRE(converted_str == adjusted_str);
Expand Down
3 changes: 2 additions & 1 deletion src/crypto/test/kp_cert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include <CLI11/CLI11.hpp>

constexpr size_t certificate_validity_period_days = 365;
auto valid_from = ::ds::to_x509_time_string(std::chrono::system_clock::now());
auto valid_from =
ccf::ds::to_x509_time_string(std::chrono::system_clock::now());
auto valid_to = ccf::crypto::compute_cert_valid_to_string(
valid_from, certificate_validity_period_days);

Expand Down
6 changes: 3 additions & 3 deletions src/endpoints/authentication/cert_auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

#include "ccf/endpoints/authentication/cert_auth.h"

#include "ccf/ds/x509_time_fmt.h"
#include "ccf/pal/locking.h"
#include "ccf/rpc_context.h"
#include "ccf/service/tables/members.h"
#include "ccf/service/tables/nodes.h"
#include "ccf/service/tables/users.h"
#include "ds/lru.h"
#include "ds/x509_time_fmt.h"
#include "enclave/enclave_time.h"

namespace ccf
Expand Down Expand Up @@ -49,12 +49,12 @@ namespace ccf

const auto valid_from_unix_time =
duration_cast<seconds>(
::ds::time_point_from_string(valid_from_timestring)
ccf::ds::time_point_from_string(valid_from_timestring)
.time_since_epoch())
.count();
const auto valid_to_unix_time =
duration_cast<seconds>(
::ds::time_point_from_string(valid_to_timestring)
ccf::ds::time_point_from_string(valid_to_timestring)
.time_since_epoch())
.count();

Expand Down
4 changes: 2 additions & 2 deletions src/host/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "ccf/ds/logger.h"
#include "ccf/ds/unit_strings.h"
#include "ccf/ds/x509_time_fmt.h"
#include "ccf/pal/attestation.h"
#include "ccf/pal/platform.h"
#include "ccf/version.h"
Expand All @@ -13,7 +14,6 @@
#include "ds/non_blocking.h"
#include "ds/nonstd.h"
#include "ds/oversized.h"
#include "ds/x509_time_fmt.h"
#include "enclave.h"
#include "handle_ring_buffer.h"
#include "host/env.h"
Expand Down Expand Up @@ -593,7 +593,7 @@ int main(int argc, char** argv)
LOG_INFO_FMT("Startup host time: {}", startup_host_time);

startup_config.startup_host_time =
::ds::to_x509_time_string(startup_host_time);
ccf::ds::to_x509_time_string(startup_host_time);

if (config.command.type == StartType::Start)
{
Expand Down
4 changes: 2 additions & 2 deletions src/node/rpc/test/frontend_test_infra.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using TResponse = ::http::SimpleResponseProcessor::Response;
constexpr size_t certificate_validity_period_days = 365;
using namespace std::literals;
auto valid_from =
::ds::to_x509_time_string(std::chrono::system_clock::now() - 24h);
ccf::ds::to_x509_time_string(std::chrono::system_clock::now() - 24h);
auto valid_to = ccf::crypto::compute_cert_valid_to_string(
valid_from, certificate_validity_period_days);

Expand Down Expand Up @@ -118,7 +118,7 @@ std::unique_ptr<ccf::NetworkIdentity> make_test_network_ident()
{
using namespace std::literals;
const auto valid_from =
::ds::to_x509_time_string(std::chrono::system_clock::now() - 24h);
ccf::ds::to_x509_time_string(std::chrono::system_clock::now() - 24h);
return std::make_unique<ReplicatedNetworkIdentity>(
"CN=CCF test network",
ccf::crypto::service_identity_curve_choice,
Expand Down
5 changes: 3 additions & 2 deletions src/node/test/channels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ static std::pair<std::string, size_t> make_validity_pair(bool expired)
if (expired)
{
return std::make_pair(
::ds::to_x509_time_string(now - std::chrono::days(2 * validity_days)),
ccf::ds::to_x509_time_string(now - std::chrono::days(2 * validity_days)),
validity_days);
}
else
{
return std::make_pair(::ds::to_x509_time_string(now - 24h), validity_days);
return std::make_pair(
ccf::ds::to_x509_time_string(now - 24h), validity_days);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/node/test/historical_queries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using NumToString = ccf::kv::Map<size_t, std::string>;
constexpr size_t certificate_validity_period_days = 365;
using namespace std::literals;
auto valid_from =
::ds::to_x509_time_string(std::chrono::system_clock::now() - 24h);
ccf::ds::to_x509_time_string(std::chrono::system_clock::now() - 24h);

auto valid_to = ccf::crypto::compute_cert_valid_to_string(
valid_from, certificate_validity_period_days);
Expand Down
4 changes: 2 additions & 2 deletions src/node/test/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#include "ccf/app_interface.h"
#include "ccf/ds/logger.h"
#include "ccf/ds/x509_time_fmt.h"
#include "ccf/service/tables/nodes.h"
#include "crypto/certs.h"
#include "crypto/openssl/hash.h"
#include "ds/x509_time_fmt.h"
#include "kv/kv_types.h"
#include "kv/store.h"
#include "kv/test/null_encryptor.h"
Expand All @@ -26,7 +26,7 @@ using MapT = ccf::kv::Map<size_t, size_t>;
constexpr size_t certificate_validity_period_days = 365;
using namespace std::literals;
auto valid_from =
::ds::to_x509_time_string(std::chrono::system_clock::now() - 24h);
ccf::ds::to_x509_time_string(std::chrono::system_clock::now() - 24h);

auto valid_to = ccf::crypto::compute_cert_valid_to_string(
valid_from, certificate_validity_period_days);
Expand Down
Loading

0 comments on commit 7d7abea

Please sign in to comment.