Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
encrypted_writer.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
44#include "../core/error_codes.h"
45
47
48#include <array>
49#include <memory>
50#include <mutex>
51#include <atomic>
52#include <chrono>
53#include <optional>
54#include <vector>
55#include <cstdint>
56#include <functional>
57
58// OpenSSL for AES-GCM encryption
59#if __has_include(<openssl/evp.h>)
60#include <openssl/evp.h>
61#include <openssl/rand.h>
62#include <openssl/err.h>
63#include <openssl/opensslv.h>
64#define LOGGER_HAS_OPENSSL_CRYPTO 1
65#endif
66
67namespace kcenon::logger {
68
81
95
101
107
112 std::optional<std::chrono::hours> key_rotation_interval;
113
118 std::filesystem::path key_rotation_path;
119
124 std::filesystem::path key_storage_base = "/var/log/keys";
125
133 security::secure_key encryption_key
134 ) : algorithm(algo), key(std::move(encryption_key)) {}
135
136 // Allow default construction with explicit key setting
138
139 // Move-only semantics (secure_key is move-only)
141 encryption_config& operator=(encryption_config&&) noexcept = default;
143 encryption_config& operator=(const encryption_config&) = delete;
144};
145
154 static constexpr uint32_t kMagic = 0x454E4352; // "ENCR"
155 static constexpr uint8_t kVersion = 1;
156 static constexpr size_t kIvSize = 16;
157 static constexpr size_t kTagSize = 16;
158
159 uint32_t magic = kMagic;
160 uint8_t version = kVersion;
161 uint8_t algorithm = 0;
162 uint16_t reserved = 0;
163 uint32_t original_length = 0;
164 uint32_t encrypted_length = 0;
165 std::array<uint8_t, kIvSize> iv{};
166 std::array<uint8_t, kTagSize> tag{};
167};
168
189public:
208 explicit encrypted_writer(
209 std::unique_ptr<log_writer_interface> wrapped,
210 encryption_config config
211 );
212
216 ~encrypted_writer() override;
217
218 // Non-copyable and non-movable (mutex cannot be moved)
223
230 common::VoidResult write(const log_entry& entry) override;
231
244 common::VoidResult rotate_key(security::secure_key new_key);
245
250 uint64_t get_entries_encrypted() const;
251
256 std::chrono::system_clock::time_point get_last_key_rotation() const;
257
266 static result<std::string> decrypt_entry(
267 const std::vector<uint8_t>& encrypted_data,
268 const security::secure_key& key
269 );
270
271private:
278 common::VoidResult encrypt_data(
279 const std::string& plaintext,
280 std::vector<uint8_t>& output
281 );
282
288 common::VoidResult generate_iv(uint8_t* iv);
289
294 bool should_rotate_key() const;
295
300 common::VoidResult auto_rotate_key_if_needed();
301
302#ifdef LOGGER_HAS_OPENSSL_CRYPTO
307 common::VoidResult init_cipher_context();
308
312 void cleanup_cipher_context();
313#endif
314
316 mutable std::mutex write_mutex_;
317
318 std::atomic<uint64_t> entries_encrypted_{0};
319 std::chrono::system_clock::time_point last_key_rotation_;
320 std::atomic<bool> is_initialized_{false};
321
322#ifdef LOGGER_HAS_OPENSSL_CRYPTO
323 EVP_CIPHER_CTX* cipher_ctx_ = nullptr;
324#endif
325};
326
345public:
350 explicit log_decryptor(const security::secure_key& key);
351
356
357 // Non-copyable and non-movable
358 log_decryptor(const log_decryptor&) = delete;
362
369 result<size_t> decrypt_file(
370 const std::filesystem::path& input_path,
371 const std::filesystem::path& output_path
372 );
373
380 result<size_t> decrypt_file_streaming(
381 const std::filesystem::path& input_path,
382 std::function<void(const std::string&)> callback
383 );
384
385private:
387
388#ifdef LOGGER_HAS_OPENSSL_CRYPTO
389 EVP_CIPHER_CTX* cipher_ctx_ = nullptr;
390#endif
391};
392
393} // namespace kcenon::logger
Abstract base class for decorator pattern log writers.
Decorator that encrypts log data before writing.
encrypted_writer & operator=(encrypted_writer &&)=delete
encrypted_writer & operator=(const encrypted_writer &)=delete
std::chrono::system_clock::time_point last_key_rotation_
encrypted_writer(const encrypted_writer &)=delete
encrypted_writer(encrypted_writer &&)=delete
Utility class for decrypting encrypted log files.
log_decryptor(log_decryptor &&)=delete
log_decryptor & operator=(log_decryptor &&)=delete
log_decryptor(const log_decryptor &)=delete
log_decryptor & operator=(const log_decryptor &)=delete
RAII wrapper for encryption keys with secure memory management.
Base class for decorator pattern writers.
Error codes specific to the logger system.
DLL export/import macros for logger_system shared library support.
#define LOGGER_SYSTEM_API
encryption_algorithm
Supported encryption algorithms for log encryption.
@ aes_256_gcm
AES-256 in GCM mode (recommended)
@ aes_256_cbc
AES-256 in CBC mode (legacy support)
@ chacha20_poly1305
ChaCha20-Poly1305 (modern alternative)
RAII wrapper for encryption keys with secure memory management.
Header prepended to each encrypted log entry.
Configuration for encrypted_writer.
bool rotate_iv_per_entry
Whether to generate unique IV for each log entry.
security::secure_key key
Encryption key (must be 32 bytes for AES-256)
encryption_config(encryption_config &&) noexcept=default
encryption_algorithm algorithm
Encryption algorithm to use.
std::filesystem::path key_storage_base
Allowed base directory for key storage.
std::optional< std::chrono::hours > key_rotation_interval
Optional key rotation interval.
encryption_config(encryption_algorithm algo, security::secure_key encryption_key)
Constructor for encryption_config.
std::filesystem::path key_rotation_path
Path for key rotation (new keys saved here)
Represents a single log entry with all associated metadata.
Definition log_entry.h:155