diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2020-04-16 21:42:45 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2020-04-16 21:42:45 +0200 |
commit | 6cf5244bc514042fe419fffe1cd26a7f5e3c778f (patch) | |
tree | 4a72abf80980c3d10656bd82606a69329867d057 /src/pubkey.cpp | |
parent | a76a9ad65fa549d2c1650bb5a7d9a657186edc43 (diff) |
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.
Diffstat (limited to 'src/pubkey.cpp')
-rw-r--r-- | src/pubkey.cpp | 57 |
1 files changed, 28 insertions, 29 deletions
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> 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> 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<uint8_t> 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<uint8_t> 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<uint8_t> SecretKey::sign(const Digest & dgst) const return sigData; } -optional<Signature> 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<PublicKey>()) + if (auto sig = rec->item("sig").asBinary()) + return Signature(*key, *sig); - auto key = rec->item("key").as<PublicKey>(); - auto sig = rec->item("sig").asBinary(); - - if (!key || !sig) - return nullopt; - - return Signature(*key, *sig); + return Signature(Stored<PublicKey>::load(ref.storage().zref()), {}); } Ref Signature::store(const Storage & st) const @@ -151,11 +148,11 @@ bool Signature::verify(const Ref & ref) const } -optional<PublicKexKey> 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> 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<uint8_t> 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<uint8_t> 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))); } |