diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2019-12-21 21:42:20 +0100 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2019-12-21 21:42:20 +0100 |
commit | f94443c63dfd63300e5bd29889935fd1f451175e (patch) | |
tree | 7722cfdac0ed76ff7f1fd525abb2ab15e0b0c998 /src/pubkey.h | |
parent | d084c069be38b6f3ad74912ca629403d9fdaec58 (diff) |
Identity storage and modification
Diffstat (limited to 'src/pubkey.h')
-rw-r--r-- | src/pubkey.h | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/src/pubkey.h b/src/pubkey.h index 7fe37ec..80da3fa 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -10,34 +10,57 @@ using std::shared_ptr; namespace erebos { +template<typename T> class Signed; + class PublicKey { PublicKey(EVP_PKEY * key): key(key, EVP_PKEY_free) {} + friend class SecretKey; public: static optional<PublicKey> load(const Ref &); + Ref store(const Storage &) const; + const shared_ptr<EVP_PKEY> key; }; class SecretKey { SecretKey(EVP_PKEY * key, const Stored<PublicKey> & pub): - key(key, EVP_PKEY_free), pub(pub) {} + key(key, EVP_PKEY_free), pub_(pub) {} + SecretKey(shared_ptr<EVP_PKEY> && key, const Stored<PublicKey> & pub): + key(key), pub_(pub) {} +public: + static SecretKey generate(const Storage & st); + static optional<SecretKey> load(const Stored<PublicKey> & st); + + Stored<PublicKey> pub() const { return pub_; } + + template<class T> + Stored<Signed<T>> sign(const Stored<T> &) const; private: + vector<uint8_t> sign(const Digest &) const; + const shared_ptr<EVP_PKEY> key; - Stored<PublicKey> pub; + Stored<PublicKey> pub_; }; class Signature { public: static optional<Signature> load(const Ref &); + Ref store(const Storage &) const; bool verify(const Ref &) const; Stored<PublicKey> key; vector<uint8_t> sig; + +private: + friend class SecretKey; + Signature(const Stored<PublicKey> & key, const vector<uint8_t> & sig): + key(key), sig(sig) {} }; template<typename T> @@ -45,13 +68,27 @@ class Signed { public: static optional<Signed<T>> load(const Ref &); + Ref store(const Storage &) const; bool isSignedBy(const Stored<PublicKey> &) const; const Stored<T> data; const vector<Stored<Signature>> sigs; + +private: + friend class SecretKey; + Signed(const Stored<T> & data, const vector<Stored<Signature>> & sigs): + data(data), sigs(sigs) {} }; +template<class T> +Stored<Signed<T>> SecretKey::sign(const Stored<T> & val) const +{ + auto st = val.ref.storage(); + auto sig = st.store(Signature(pub(), sign(val.ref.digest()))); + return st.store(Signed(val, { sig })); +} + template<typename T> optional<Signed<T>> Signed<T>::load(const Ref & ref) { @@ -76,6 +113,18 @@ optional<Signed<T>> Signed<T>::load(const Ref & ref) } template<typename T> +Ref Signed<T>::store(const Storage & st) const +{ + vector<Record::Item> items; + + items.emplace_back("SDATA", data); + for (const auto & sig : sigs) + items.emplace_back("sig", sig); + + return st.storeObject(Record(std::move(items))); +} + +template<typename T> bool Signed<T>::isSignedBy(const Stored<PublicKey> & key) const { for (const auto & sig : sigs) |