Skip to content
Snippets Groups Projects
Commit b2542a57 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

securedht: cache value signature check/decryption results

parent 00c1be9c
Branches feature/probepermanent
Tags
No related merge requests found
...@@ -122,6 +122,8 @@ private: ...@@ -122,6 +122,8 @@ private:
std::map<ValueType::Id, ValueType> types {}; std::map<ValueType::Id, ValueType> types {};
}; };
struct CryptoValueCache;
/** /**
* A "value" is data potentially stored on the Dht, with some metadata. * A "value" is data potentially stored on the Dht, with some metadata.
* *
...@@ -588,6 +590,14 @@ struct OPENDHT_PUBLIC Value ...@@ -588,6 +590,14 @@ struct OPENDHT_PUBLIC Value
* Hold encrypted version of the data. * Hold encrypted version of the data.
*/ */
Blob cypher {}; Blob cypher {};
private:
friend class SecureDht;
/* Cache for crypto ops */
bool signatureChecked {false};
bool signatureValid {false};
bool decrypted {false};
Sp<Value> decryptedValue {};
}; };
using ValuesExport = std::pair<InfoHash, Blob>; using ValuesExport = std::pair<InfoHash, Blob>;
......
...@@ -235,12 +235,17 @@ SecureDht::checkValue(const Sp<Value>& v) ...@@ -235,12 +235,17 @@ SecureDht::checkValue(const Sp<Value>& v)
#endif #endif
return {}; return {};
} }
if (v->decrypted) {
return v->decryptedValue;
}
v->decrypted = true;
try { try {
Value decrypted_val (decrypt(*v)); Value decrypted_val (decrypt(*v));
if (decrypted_val.recipient == getId()) { if (decrypted_val.recipient == getId()) {
if (decrypted_val.owner) if (decrypted_val.owner)
nodesPubKeys_[decrypted_val.owner->getId()] = decrypted_val.owner; nodesPubKeys_[decrypted_val.owner->getId()] = decrypted_val.owner;
return std::make_shared<Value>(std::move(decrypted_val)); v->decryptedValue = std::make_shared<Value>(std::move(decrypted_val));
return v->decryptedValue;
} }
// Ignore values belonging to other people // Ignore values belonging to other people
} catch (const std::exception& e) { } catch (const std::exception& e) {
...@@ -249,7 +254,12 @@ SecureDht::checkValue(const Sp<Value>& v) ...@@ -249,7 +254,12 @@ SecureDht::checkValue(const Sp<Value>& v)
} }
// Check signed values // Check signed values
else if (v->isSigned()) { else if (v->isSigned()) {
if (v->signatureChecked) {
return v->signatureValid ? v : Sp<Value>{};
}
v->signatureChecked = true;
if (v->owner and v->owner->checkSignature(v->getToSign(), v->signature)) { if (v->owner and v->owner->checkSignature(v->getToSign(), v->signature)) {
v->signatureValid = true;
nodesPubKeys_[v->owner->getId()] = v->owner; nodesPubKeys_[v->owner->getId()] = v->owner;
return v; return v;
} }
...@@ -355,7 +365,7 @@ SecureDht::putSigned(const InfoHash& hash, Sp<Value> val, DoneCallback callback, ...@@ -355,7 +365,7 @@ SecureDht::putSigned(const InfoHash& hash, Sp<Value> val, DoneCallback callback,
void void
SecureDht::putEncrypted(const InfoHash& hash, const InfoHash& to, Sp<Value> val, DoneCallback callback, bool permanent) SecureDht::putEncrypted(const InfoHash& hash, const InfoHash& to, Sp<Value> val, DoneCallback callback, bool permanent)
{ {
findPublicKey(to, [=](const Sp<const crypto::PublicKey> pk) { findPublicKey(to, [=](const Sp<const crypto::PublicKey>& pk) {
if(!pk || !*pk) { if(!pk || !*pk) {
if (callback) if (callback)
callback(false, {}); callback(false, {});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment