From 6cf5244bc514042fe419fffe1cd26a7f5e3c778f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Thu, 16 Apr 2020 21:42:45 +0200 Subject: Remove optional from load result Makes loading of data well-defined for arbitrary object contents. Introduce zero reference and object to represent missing or mismatched parts. --- src/pubkey.cpp | 57 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 29 deletions(-) (limited to 'src/pubkey.cpp') diff --git a/src/pubkey.cpp b/src/pubkey.cpp index 0e83136..6d85d33 100644 --- a/src/pubkey.cpp +++ b/src/pubkey.cpp @@ -8,11 +8,11 @@ using std::string; using namespace erebos; -optional PublicKey::load(const Ref & ref) +PublicKey PublicKey::load(const Ref & ref) { auto rec = ref->asRecord(); if (!rec) - return nullopt; + return PublicKey(nullptr); if (auto ktype = rec->item("type").asText()) if (ktype.value() != "ed25519") @@ -22,7 +22,7 @@ optional PublicKey::load(const Ref & ref) return PublicKey(EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, nullptr, pubkey.value().data(), pubkey.value().size())); - return nullopt; + return PublicKey(nullptr); } Ref PublicKey::store(const Storage & st) const @@ -31,12 +31,14 @@ Ref PublicKey::store(const Storage & st) const items.emplace_back("type", "ed25519"); - vector keyData; - size_t keyLen; - EVP_PKEY_get_raw_public_key(key.get(), nullptr, &keyLen); - keyData.resize(keyLen); - EVP_PKEY_get_raw_public_key(key.get(), keyData.data(), &keyLen); - items.emplace_back("pubkey", keyData); + if (key) { + vector keyData; + size_t keyLen; + EVP_PKEY_get_raw_public_key(key.get(), nullptr, &keyLen); + keyData.resize(keyLen); + EVP_PKEY_get_raw_public_key(key.get(), keyData.data(), &keyLen); + items.emplace_back("pubkey", keyData); + } return st.storeObject(Record(std::move(items))); } @@ -110,19 +112,14 @@ vector SecretKey::sign(const Digest & dgst) const return sigData; } -optional Signature::load(const Ref & ref) +Signature Signature::load(const Ref & ref) { - auto rec = ref->asRecord(); - if (!rec) - return nullopt; + if (auto rec = ref->asRecord()) + if (auto key = rec->item("key").as()) + if (auto sig = rec->item("sig").asBinary()) + return Signature(*key, *sig); - auto key = rec->item("key").as(); - auto sig = rec->item("sig").asBinary(); - - if (!key || !sig) - return nullopt; - - return Signature(*key, *sig); + return Signature(Stored::load(ref.storage().zref()), {}); } Ref Signature::store(const Storage & st) const @@ -151,11 +148,11 @@ bool Signature::verify(const Ref & ref) const } -optional PublicKexKey::load(const Ref & ref) +PublicKexKey PublicKexKey::load(const Ref & ref) { auto rec = ref->asRecord(); if (!rec) - return nullopt; + return PublicKexKey(nullptr); if (auto ktype = rec->item("type").asText()) if (ktype.value() != "x25519") @@ -165,7 +162,7 @@ optional PublicKexKey::load(const Ref & ref) return PublicKexKey(EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, nullptr, pubkey.value().data(), pubkey.value().size())); - return nullopt; + return PublicKexKey(nullptr); } Ref PublicKexKey::store(const Storage & st) const @@ -174,12 +171,14 @@ Ref PublicKexKey::store(const Storage & st) const items.emplace_back("type", "x25519"); - vector keyData; - size_t keyLen; - EVP_PKEY_get_raw_public_key(key.get(), nullptr, &keyLen); - keyData.resize(keyLen); - EVP_PKEY_get_raw_public_key(key.get(), keyData.data(), &keyLen); - items.emplace_back("pubkey", keyData); + if (key) { + vector keyData; + size_t keyLen; + EVP_PKEY_get_raw_public_key(key.get(), nullptr, &keyLen); + keyData.resize(keyLen); + EVP_PKEY_get_raw_public_key(key.get(), keyData.data(), &keyLen); + items.emplace_back("pubkey", keyData); + } return st.storeObject(Record(std::move(items))); } -- cgit v1.2.3