From f94443c63dfd63300e5bd29889935fd1f451175e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 21 Dec 2019 21:42:20 +0100 Subject: Identity storage and modification --- src/pubkey.h | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'src/pubkey.h') 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 class Signed; + class PublicKey { PublicKey(EVP_PKEY * key): key(key, EVP_PKEY_free) {} + friend class SecretKey; public: static optional load(const Ref &); + Ref store(const Storage &) const; + const shared_ptr key; }; class SecretKey { SecretKey(EVP_PKEY * key, const Stored & pub): - key(key, EVP_PKEY_free), pub(pub) {} + key(key, EVP_PKEY_free), pub_(pub) {} + SecretKey(shared_ptr && key, const Stored & pub): + key(key), pub_(pub) {} +public: + static SecretKey generate(const Storage & st); + static optional load(const Stored & st); + + Stored pub() const { return pub_; } + + template + Stored> sign(const Stored &) const; private: + vector sign(const Digest &) const; + const shared_ptr key; - Stored pub; + Stored pub_; }; class Signature { public: static optional load(const Ref &); + Ref store(const Storage &) const; bool verify(const Ref &) const; Stored key; vector sig; + +private: + friend class SecretKey; + Signature(const Stored & key, const vector & sig): + key(key), sig(sig) {} }; template @@ -45,13 +68,27 @@ class Signed { public: static optional> load(const Ref &); + Ref store(const Storage &) const; bool isSignedBy(const Stored &) const; const Stored data; const vector> sigs; + +private: + friend class SecretKey; + Signed(const Stored & data, const vector> & sigs): + data(data), sigs(sigs) {} }; +template +Stored> SecretKey::sign(const Stored & val) const +{ + auto st = val.ref.storage(); + auto sig = st.store(Signature(pub(), sign(val.ref.digest()))); + return st.store(Signed(val, { sig })); +} + template optional> Signed::load(const Ref & ref) { @@ -75,6 +112,18 @@ optional> Signed::load(const Ref & ref) }; } +template +Ref Signed::store(const Storage & st) const +{ + vector items; + + items.emplace_back("SDATA", data); + for (const auto & sig : sigs) + items.emplace_back("sig", sig); + + return st.storeObject(Record(std::move(items))); +} + template bool Signed::isSignedBy(const Stored & key) const { -- cgit v1.2.3