diff options
author | Sven Gothel <[email protected]> | 2021-06-20 21:19:30 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-06-20 21:19:30 +0200 |
commit | c980e3260244f20d0010f67f34eb50ee3473a247 (patch) | |
tree | 9cc15af5f1bff4acbb66876c7fb60c86fbbf3546 | |
parent | 8c35cb801d84c0bab700095a00e75b945a6c4231 (diff) |
Cipherpack::PackInfo: Production ready, used as return value for encryptThenSign_RSA1() and checkSignThenDecrypt_RSA1()
The `ts_creation_sec` timestamp shall be passed, created by encryptThenSign_RSA1()
and stored in DER-Header-1.
-rw-r--r-- | include/elevator/Cipherpack.hpp | 66 | ||||
-rw-r--r-- | include/elevator/IOUtil.hpp | 7 | ||||
-rw-r--r-- | src/elevator/Crypto.cpp | 28 | ||||
-rw-r--r-- | src/elevator/IOUtil.cpp | 13 |
4 files changed, 85 insertions, 29 deletions
diff --git a/include/elevator/Cipherpack.hpp b/include/elevator/Cipherpack.hpp index b7058c5..dfb3445 100644 --- a/include/elevator/Cipherpack.hpp +++ b/include/elevator/Cipherpack.hpp @@ -27,37 +27,77 @@ namespace elevator { */ class Cipherpack { public: + /** + * Simple package information POD, capturing an invalid or valid + * processed package creation (encryption) or un-packaging (decryption). + */ class PackInfo { private: - std::string filename; + uint64_t ts_creation_sec; + std::string source; + bool source_enc; + std::string stored_filename; + bool stored_enc; + std::string header_filename; uint32_t payload_version; uint32_t payload_version_parent; - uint64_t payload_size; - uint64_t ts_creation_sec; bool valid; public: + /** default ctor, denoting an invalid package information */ PackInfo() - : payload_version(0), payload_version_parent(0), - payload_size(0), - ts_creation_sec( jau::getWallClockSeconds() ), + : ts_creation_sec( jau::getWallClockSeconds() ), + source("none"), source_enc(false), + stored_filename("none"), stored_enc(false), + header_filename("none"), + payload_version(0), payload_version_parent(0), + valid(false) + { } + + /** Source ctor, denoting an invalid package information */ + PackInfo(const uint64_t ts_creation_sec_, const std::string& source_, const bool source_enc_) + : ts_creation_sec(ts_creation_sec_), + source(source_), source_enc(source_enc_), + stored_filename("none"), stored_enc(false), + header_filename("none"), + payload_version(0), payload_version_parent(0), valid(false) { } - PackInfo(const uint32_t pversion, const uint32_t pversion_parent) - : payload_version(pversion), payload_version_parent(pversion_parent), - payload_size(0), - ts_creation_sec( jau::getWallClockSeconds() ), - valid(false) // FIXME + /** Complete ctor, denoting a valid package information */ + PackInfo(const uint64_t ts_creation_sec_, + const std::string& source_, const bool source_enc_, + const std::string& stored_fname, bool stored_enc_, + const std::string& header_fname, + const uint32_t pversion, const uint32_t pversion_parent) + : ts_creation_sec(ts_creation_sec_), + source(source_), source_enc(source_enc_), + stored_filename(stored_fname), stored_enc(stored_enc_), + header_filename(header_fname), + payload_version(pversion), payload_version_parent(pversion_parent), + valid(true) { } + const std::string& getSource() const noexcept { return source; } + bool isSourceEncrypted() const noexcept { return source_enc; } + const std::string& getStoredFilename() const noexcept { return stored_filename; } + bool isStoredFileEncrypted() const noexcept { return stored_enc; } + + /** Returns the designated decrypted filename from DER-Header-1. */ + const std::string& getDesignatedFilename() const noexcept { return header_filename; } + constexpr uint32_t getPayloadVersion() const noexcept { return payload_version;} constexpr uint32_t getPayloadVersionParent() const noexcept { return payload_version_parent;} - constexpr uint64_t getPayloadSize() const noexcept { return payload_size;} - /** Returns the creation timestamp in seconds since Unix epoch */ + /** Returns the creation time in seconds since Unix epoch */ constexpr uint64_t getCreationTime() const noexcept { return ts_creation_sec; } + /** + * Return the creation time as a timestring `YYYY-MM-DD HH:MM:SS` + * @param local if true, returns the time in local time, otherwise UTC + */ + std::string getCreationTimeString(const bool local) const noexcept; + std::string toString() const noexcept; bool isValid() const noexcept { return valid; } diff --git a/include/elevator/IOUtil.hpp b/include/elevator/IOUtil.hpp index b81feaf..a3a69b8 100644 --- a/include/elevator/IOUtil.hpp +++ b/include/elevator/IOUtil.hpp @@ -19,6 +19,13 @@ namespace elevator { class IOUtil { public: + /** + * Return the given timestamp as a timestring in format `YYYY-MM-DD HH:MM:SS` + * @param timestamp_sec timestamp in seconds since Unix epoch + * @param local if true, returns the time in local time, otherwise UTC + */ + static std::string getTimestampString(const uint64_t timestamp_sec, const bool local) noexcept; + static bool file_exists(const std::string& name) noexcept; static bool remove(const std::string& fname) noexcept; diff --git a/src/elevator/Crypto.cpp b/src/elevator/Crypto.cpp index 4f14174..c99be58 100644 --- a/src/elevator/Crypto.cpp +++ b/src/elevator/Crypto.cpp @@ -55,23 +55,19 @@ std::unique_ptr<Botan::Private_Key> Cipherpack::load_private_key(const std::stri return key; } +std::string Cipherpack::PackInfo::getCreationTimeString(const bool local) const noexcept { + return IOUtil::getTimestampString(ts_creation_sec, local); +} + std::string Cipherpack::PackInfo::toString() const noexcept { + std::string source_enc_s = source_enc ? " (E)" : ""; + std::string stored_enc_s = stored_enc ? " (E)" : ""; std::string res = "PackInfo["; - res += "filename "+filename; - res += ", payload[version "+std::to_string(payload_version)+ - ", parent_version "+std::to_string(payload_version_parent)+ - ", size "+std::to_string(payload_size)+"], "; - { - std::time_t t0 = static_cast<std::time_t>(ts_creation_sec); - struct std::tm tm_0; - if( nullptr == ::gmtime_r( &t0, &tm_0 ) ) { - res += "1970-01-01 00:00:00"; // 19 + 1 - } else { - char b[20]; - strftime(b, sizeof(b), "%Y-%m-%d %H:%M:%S", &tm_0); - res += std::string(b); - } - } - res += ", valid "+std::to_string( isValid() )+"]"; + res += "source "+source+source_enc_s+ + ", filename[header "+header_filename+", stored "+stored_filename+stored_enc_s+ + "], creation "+getCreationTimeString(false)+ + " UTC, version["+std::to_string(payload_version)+ + ", parent "+std::to_string(payload_version_parent)+ + "], valid "+std::to_string( isValid() )+"]"; return res; } diff --git a/src/elevator/IOUtil.cpp b/src/elevator/IOUtil.cpp index 5bcf14c..1158942 100644 --- a/src/elevator/IOUtil.cpp +++ b/src/elevator/IOUtil.cpp @@ -26,6 +26,19 @@ using namespace elevator; +std::string IOUtil::getTimestampString(const uint64_t timestamp_sec, const bool local) noexcept { + std::time_t t0 = static_cast<std::time_t>(timestamp_sec); + struct std::tm tm_0; + struct std::tm * res = local ? ::localtime_r( &t0, &tm_0 ) : ::gmtime_r( &t0, &tm_0 ); + if( nullptr == res ) { + return "1970-01-01 00:00:00"; // 19 + 1 + } else { + char b[20]; + strftime(b, sizeof(b), "%Y-%m-%d %H:%M:%S", &tm_0); + return std::string(b); + } +} + bool IOUtil::file_exists(const std::string& name) noexcept { std::ifstream f(name); return f.good() && f.is_open(); |