22#if __has_include(<openssl/rand.h>)
23#include <openssl/rand.h>
24#include <openssl/crypto.h>
25#include <openssl/opensslv.h>
71 :
data_(std::move(other.data_)) {
79 data_ = std::move(other.data_);
88 const std::vector<uint8_t>&
data()
const {
120 volatile uint8_t* ptr =
data_.data();
121 for (
size_t i = 0; i <
data_.size(); ++i) {
156 "Failed to generate secure random key"};
160 std::random_device rd;
161 for (
size_t i = 0; i <
size; ++i) {
162 key.
mutable_data()[i] =
static_cast<uint8_t
>(rd() & 0xFF);
178 const std::filesystem::path& path,
179 const std::filesystem::path& allowed_base =
"/var/log/keys"
183 if (validation.is_err()) {
189 auto parent = path.parent_path();
190 if (!parent.empty() && !std::filesystem::exists(parent)) {
191 std::filesystem::create_directories(parent);
193 }
catch (
const std::filesystem::filesystem_error& e) {
196 std::string(
"Failed to create key directory: ") + e.what()
201 std::ofstream file(path, std::ios::binary | std::ios::trunc);
205 "Failed to open key file for writing"
210 reinterpret_cast<const char*
>(key.
data().data()),
217 "Failed to write key data"
225 std::filesystem::permissions(
227 std::filesystem::perms::owner_read |
228 std::filesystem::perms::owner_write,
229 std::filesystem::perm_options::replace
231 }
catch (
const std::filesystem::filesystem_error& e) {
233 std::filesystem::remove(path);
236 std::string(
"Failed to set secure permissions: ") + e.what()
251 const std::filesystem::path& path,
252 size_t expected_size = 32,
253 const std::filesystem::path& allowed_base =
"/var/log/keys"
257 if (validation.is_err()) {
264 if (!std::filesystem::exists(path)) {
267 "Key file does not exist"};
272 auto status = std::filesystem::status(path);
273 auto perms = status.permissions();
275 if ((perms & std::filesystem::perms::group_read) != std::filesystem::perms::none ||
276 (perms & std::filesystem::perms::others_read) != std::filesystem::perms::none) {
279 "Key file permissions are too permissive (must be 0600 or stricter)"};
281 }
catch (
const std::filesystem::filesystem_error& e) {
284 std::string(
"Failed to check file permissions: ") + e.what()};
288 size_t file_size = 0;
290 file_size = std::filesystem::file_size(path);
291 }
catch (
const std::filesystem::filesystem_error& e) {
294 std::string(
"Failed to get file size: ") + e.what()};
297 if (file_size != expected_size) {
300 "Invalid key file size (expected " + std::to_string(expected_size) +
301 " bytes, got " + std::to_string(file_size) +
")"};
305 std::ifstream file(path, std::ios::binary);
309 "Failed to open key file for reading"};
321 "Failed to read key data"};
335 const std::filesystem::path& path,
336 const std::filesystem::path& allowed_base
340 auto canonical_path = std::filesystem::weakly_canonical(path);
341 auto canonical_base = std::filesystem::weakly_canonical(allowed_base);
344 auto [mismatch_iter, _] = std::mismatch(
345 canonical_base.begin(), canonical_base.end(),
346 canonical_path.begin(), canonical_path.end()
349 if (mismatch_iter != canonical_base.end()) {
352 "Key path must be within allowed directory: " +
353 canonical_base.string()
358 }
catch (
const std::filesystem::filesystem_error& e) {
361 std::string(
"Path validation failed: ") + e.what()
Secure storage and retrieval of encryption keys.
static result< secure_key > load_key(const std::filesystem::path &path, size_t expected_size=32, const std::filesystem::path &allowed_base="/var/log/keys")
Load key from file with permission verification.
static common::VoidResult save_key(const secure_key &key, const std::filesystem::path &path, const std::filesystem::path &allowed_base="/var/log/keys")
Save key to file with secure permissions.
static common::VoidResult validate_key_path(const std::filesystem::path &path, const std::filesystem::path &allowed_base)
Validate key file path (prevent path traversal)
static result< secure_key > generate_key(size_t size=32)
Generate a secure random key.
RAII wrapper for encryption keys with secure memory management.
secure_key(std::vector< uint8_t > data)
Construct with data.
size_t size() const
Get key size in bytes.
void secure_clear()
Securely clear key data from memory.
secure_key(size_t size)
Construct with specified size.
secure_key(const secure_key &)=delete
const std::vector< uint8_t > & data() const
Get const reference to key data.
std::vector< uint8_t > data_
secure_key & operator=(secure_key &&other) noexcept
~secure_key()
Destructor - securely clears key from memory.
std::vector< uint8_t > & mutable_data()
Get mutable reference to key data (use with caution)
secure_key & operator=(const secure_key &)=delete
secure_key(secure_key &&other) noexcept
Error codes specific to the logger system.
@ path_traversal_detected
common::VoidResult make_logger_void_result(logger_error_code code, const std::string &message="")
std::string get_logger_error_message(const common::VoidResult &result)
Get error message from a VoidResult.
@ size
Rotate based on file size only.
logger_error_code get_logger_error_code(const common::VoidResult &result)