Skip to content

Commit

Permalink
Merge branch 'main' into f/5241-historical-queries-customisable-error…
Browse files Browse the repository at this point in the history
…-handling
  • Loading branch information
achamayou authored Jul 3, 2024
2 parents aeecb0d + 5048783 commit ef11ec3
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 81 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,9 @@ if(BUILD_TESTS)
)
target_link_libraries(base64_test PRIVATE ${CMAKE_THREAD_LIBS_INIT})

add_unit_test(pem_test ${CMAKE_CURRENT_SOURCE_DIR}/src/crypto/test/pem.cpp)
target_link_libraries(pem_test PRIVATE ${CMAKE_THREAD_LIBS_INIT})

add_test_bin(
kp_cert_test ${CMAKE_CURRENT_SOURCE_DIR}/src/crypto/test/kp_cert.cpp
)
Expand Down
1 change: 1 addition & 0 deletions cmake/crypto.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(CCFCRYPTO_SRC
${CCF_DIR}/src/crypto/verifier.cpp
${CCF_DIR}/src/crypto/key_wrap.cpp
${CCF_DIR}/src/crypto/hmac.cpp
${CCF_DIR}/src/crypto/pem.cpp
${CCF_DIR}/src/crypto/ecdsa.cpp
${CCF_DIR}/src/crypto/openssl/symmetric_key.cpp
${CCF_DIR}/src/crypto/openssl/public_key.cpp
Expand Down
69 changes: 14 additions & 55 deletions include/ccf/crypto/pem.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,84 +18,57 @@ namespace ccf::crypto
{
private:
std::string s;

void check_pem_format()
{
if (s.find("-----BEGIN") == std::string::npos)
{
throw std::runtime_error(
fmt::format("PEM constructed with non-PEM data: {}", s));
}
}
void check_pem_format();

public:
Pem() = default;

Pem(const std::string& s_) : s(s_)
{
check_pem_format();
}

Pem(const uint8_t* data, size_t size)
{
if (size == 0)
throw std::logic_error("Got PEM of size 0.");

// If it's already null-terminated, don't suffix again
const auto null_terminated = *(data + size - 1) == 0;
if (null_terminated)
size -= 1;

s.assign(reinterpret_cast<const char*>(data), size);

check_pem_format();
}
Pem(const std::string& s_);
Pem(const uint8_t* data, size_t size);

explicit Pem(std::span<const uint8_t> s) : Pem(s.data(), s.size()) {}

explicit Pem(const std::vector<uint8_t>& v) : Pem(v.data(), v.size()) {}

bool operator==(const Pem& rhs) const
inline bool operator==(const Pem& rhs) const
{
return s == rhs.s;
}

bool operator!=(const Pem& rhs) const
inline bool operator!=(const Pem& rhs) const
{
return !(*this == rhs);
}

bool operator<(const Pem& rhs) const
inline bool operator<(const Pem& rhs) const
{
return s < rhs.s;
}

const std::string& str() const
inline const std::string& str() const
{
return s;
}

uint8_t* data()
inline uint8_t* data()
{
return reinterpret_cast<uint8_t*>(s.data());
}

const uint8_t* data() const
inline const uint8_t* data() const
{
return reinterpret_cast<const uint8_t*>(s.data());
}

size_t size() const
inline size_t size() const
{
return s.size();
}

bool empty() const
inline bool empty() const
{
return s.empty();
}

std::vector<uint8_t> raw() const
inline std::vector<uint8_t> raw() const
{
return {data(), data() + size()};
}
Expand Down Expand Up @@ -128,22 +101,8 @@ namespace ccf::crypto
return "Pem";
}

static std::vector<ccf::crypto::Pem> split_x509_cert_bundle(
const std::string_view& pem)
{
std::string separator("-----END CERTIFICATE-----");
std::vector<ccf::crypto::Pem> pems;
auto separator_end = 0;
auto next_separator_start = pem.find(separator);
while (next_separator_start != std::string_view::npos)
{
pems.emplace_back(std::string(
pem.substr(separator_end, next_separator_start + separator.size())));
separator_end = next_separator_start + separator.size();
next_separator_start = pem.find(separator, separator_end);
}
return pems;
}
std::vector<ccf::crypto::Pem> split_x509_cert_bundle(
const std::string_view& pem);

inline void fill_json_schema(nlohmann::json& schema, const Pem*)
{
Expand Down
7 changes: 0 additions & 7 deletions include/ccf/ds/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,6 @@ namespace ccf::logger
logger->write(line);
}

#ifndef INSIDE_ENCLAVE
if (line.log_level == LoggerLevel::FATAL)
{
throw std::logic_error("Fatal: " + format_to_text(line));
}
#endif

return true;
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/clients/perf/perf_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace client
if (core_id > threads || core_id < 0)
{
LOG_FATAL_FMT("Invalid core id: {}", core_id);
abort();
return false;
}

Expand All @@ -43,6 +44,7 @@ namespace client
if (sched_setaffinity(0, sizeof(cpu_set_t), &set) < 0)
{
LOG_FATAL_FMT("Unable to set affinity");
abort();
return false;
}

Expand Down
52 changes: 52 additions & 0 deletions src/crypto/pem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache 2.0 License.
#include "ccf/crypto/pem.h"

namespace ccf::crypto
{
void Pem::check_pem_format()
{
if (s.find("-----BEGIN") == std::string::npos)
{
throw std::runtime_error(
fmt::format("PEM constructed with non-PEM data: {}", s));
}
}

Pem::Pem(const std::string& s_) : s(s_)
{
check_pem_format();
}

Pem::Pem(const uint8_t* data, size_t size)
{
if (size == 0)
throw std::logic_error("Got PEM of size 0.");

// If it's already null-terminated, don't suffix again
const auto null_terminated = *(data + size - 1) == 0;
if (null_terminated)
size -= 1;

s.assign(reinterpret_cast<const char*>(data), size);

check_pem_format();
}

std::vector<ccf::crypto::Pem> split_x509_cert_bundle(
const std::string_view& pem)
{
std::string separator("-----END CERTIFICATE-----");
std::vector<ccf::crypto::Pem> pems;
auto separator_end = 0;
auto next_separator_start = pem.find(separator);
while (next_separator_start != std::string_view::npos)
{
pems.emplace_back(std::string(
pem.substr(separator_end, next_separator_start + separator.size())));
separator_end = next_separator_start + separator.size();
next_separator_start = pem.find(separator, separator_end);
}
return pems;
}
}
58 changes: 58 additions & 0 deletions src/crypto/test/pem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache 2.0 License.
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "ccf/crypto/pem.h"

#include <chrono>
#include <doctest/doctest.h>
#include <string>

using namespace std;
using namespace ccf::crypto;

TEST_CASE("Split x509 cert bundle")
{
REQUIRE(split_x509_cert_bundle("") == std::vector<Pem>{});

const std::string single_cert =
"-----BEGIN "
"CERTIFICATE-----"
"\nMIIByDCCAU6gAwIBAgIQOBe5SrcwReWmSzTjzj2HDjAKBggqhkjOPQQDAzATMREw\nDwYDVQ"
"QDDAhDQ0YgTm9kZTAeFw0yMzA1MTcxMzUwMzFaFw0yMzA1MTgxMzUwMzBa\nMBMxETAPBgNVBA"
"MMCENDRiBOb2RlMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE74qL\nAc/"
"45tiriN5MuquYhHVdMGQRvYSm08HBfYcODtET88qC0A39o6Y2TmfbIn6BdjMG\nkD58o377ZMT"
"aApQu/oJcwt7qZ9/LE8j8WU2qHn0cPTlpwH/"
"2tiud2w+U3voSo2cw\nZTASBgNVHRMBAf8ECDAGAQH/"
"AgEAMB0GA1UdDgQWBBS9FJNwWSXtUpHaBV57EwTW\noM8vHjAfBgNVHSMEGDAWgBS9FJNwWSXt"
"UpHaBV57EwTWoM8vHjAPBgNVHREECDAG\nhwR/"
"xF96MAoGCCqGSM49BAMDA2gAMGUCMQDKxpjPToJ7VSqKqQSeMuW9tr4iL+"
"9I\n7gTGdGwiIYV1qTSS35Sk9XQZ0VpSa58c/"
"5UCMEgmH71k7XlTGVUypm4jAgjpC46H\ns+hJpGMvyD9dKzEpZgmZYtghbyakUkwBiqmFQA=="
"\n-----END CERTIFICATE-----";
auto bundle = split_x509_cert_bundle(single_cert);
const auto cert_pem = Pem(single_cert);
REQUIRE(bundle.size() == 1);
REQUIRE(bundle[0] == cert_pem);

const std::string two_certs = single_cert + single_cert;
bundle = split_x509_cert_bundle(two_certs);
REQUIRE(bundle.size() == 2);
REQUIRE(bundle[0] == cert_pem);
REQUIRE(bundle[1] == cert_pem);

std::string bundle_with_invalid_suffix = single_cert + "ignored suffix";
bundle = split_x509_cert_bundle(bundle_with_invalid_suffix);
REQUIRE(bundle.size() == 1);
REQUIRE(bundle[0] == cert_pem);

bundle_with_invalid_suffix =
single_cert + "-----BEGIN CERTIFICATE-----\nignored suffix";
bundle = split_x509_cert_bundle(bundle_with_invalid_suffix);
REQUIRE(bundle.size() == 1);
REQUIRE(bundle[0] == cert_pem);

const std::string bundle_with_very_invalid_pem =
single_cert + "not a cert\n-----END CERTIFICATE-----";
REQUIRE_THROWS_AS(
split_x509_cert_bundle(bundle_with_very_invalid_pem), std::runtime_error);
}
4 changes: 2 additions & 2 deletions src/ds/test/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ TEST_CASE("Framework logging macros")

{
REQUIRE(logs.empty());
REQUIRE_THROWS(LOG_FATAL_FMT("Hello C"));
LOG_FATAL_FMT("Hello C");
REQUIRE(logs.size() == 1);

const auto& log = logs[0];
Expand Down Expand Up @@ -109,7 +109,7 @@ TEST_CASE("Application logging macros")

{
REQUIRE(logs.empty());
REQUIRE_THROWS(CCF_APP_FATAL("Hello C"));
CCF_APP_FATAL("Hello C");
REQUIRE(logs.size() == 1);

const auto& log = logs[0];
Expand Down
4 changes: 3 additions & 1 deletion src/endpoints/endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ namespace ccf::endpoints
{
if (installer == nullptr)
{
LOG_FATAL_FMT(
auto msg = fmt::format(
"Can't install this endpoint ({}) - it is not associated with an "
"installer",
full_uri_path);
LOG_FATAL_FMT("{}", msg);
throw std::logic_error(msg);
}
else
{
Expand Down
14 changes: 1 addition & 13 deletions src/host/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,6 @@ void print_version(size_t)
exit(0);
}

std::string read_required_environment_variable(
const std::string& envvar, const std::string& name)
{
auto ev = std::getenv(envvar.c_str());
if (ev == nullptr)
{
LOG_FATAL_FMT(
"Environment variable \"{}\" for {} is not set", envvar, name);
}
LOG_INFO_FMT("Reading {} from environment {}", name, envvar);
return ev;
}

int main(int argc, char** argv)
{
// ignore SIGPIPE
Expand Down Expand Up @@ -676,6 +663,7 @@ int main(int argc, char** argv)
else
{
LOG_FATAL_FMT("Start command should be start|join|recover. Exiting.");
return static_cast<int>(CLI::ExitCodes::ValidationError);
}

std::vector<uint8_t> startup_snapshot = {};
Expand Down
2 changes: 2 additions & 0 deletions src/host/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ namespace asynchost
virtual void on_resolve_failed()
{
LOG_FATAL_FMT("{} {} resolve failed", conn_name, name);
abort();
}
virtual void on_listen_failed()
{
LOG_FATAL_FMT("{} {} listen failed", conn_name, name);
abort();
}
};

Expand Down
5 changes: 2 additions & 3 deletions src/node/acme_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ namespace ACME
}
catch (const std::exception& ex)
{
LOG_FATAL_FMT("ACME: request callback failed: {}", ex.what());
LOG_FAIL_FMT("ACME: request callback failed: {}", ex.what());
return false;
}
});
Expand Down Expand Up @@ -807,8 +807,7 @@ namespace ACME
}
else
{
LOG_FATAL_FMT(
"ACME: unknown order status '{}', aborting", status);
LOG_FAIL_FMT("ACME: unknown order status '{}', aborting", status);
guard.unlock();
remove_order(*order_url_opt);
}
Expand Down
1 change: 1 addition & 0 deletions tests/perf-system/submitter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(CCFCRYPTO_SRC
${CCF_DIR}/src/crypto/verifier.cpp
${CCF_DIR}/src/crypto/key_wrap.cpp
${CCF_DIR}/src/crypto/hmac.cpp
${CCF_DIR}/src/crypto/pem.cpp
${CCF_DIR}/src/crypto/openssl/symmetric_key.cpp
${CCF_DIR}/src/crypto/openssl/public_key.cpp
${CCF_DIR}/src/crypto/openssl/key_pair.cpp
Expand Down

0 comments on commit ef11ec3

Please sign in to comment.