-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Crypto: move ops to Value #357
Open
aberaud
wants to merge
2
commits into
master
Choose a base branch
from
crypto_value
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,33 +68,19 @@ ValueType | |
SecureDht::secureType(ValueType&& type) | ||
{ | ||
type.storePolicy = [this,type](InfoHash id, Sp<Value>& v, const InfoHash& nid, const SockAddr& a) { | ||
if (v->isSigned()) { | ||
if (!v->signatureChecked) { | ||
v->signatureChecked = true; | ||
v->signatureValid = v->owner and v->owner->checkSignature(v->getToSign(), v->signature); | ||
} | ||
if (!v->signatureValid) { | ||
DHT_LOG.WARN("Signature verification failed"); | ||
return false; | ||
} | ||
} | ||
if (v->isSigned()) | ||
return v->checkSignature(); | ||
return type.storePolicy(id, v, nid, a); | ||
}; | ||
type.editPolicy = [this,type](InfoHash id, const Sp<Value>& o, Sp<Value>& n, const InfoHash& nid, const SockAddr& a) { | ||
if (!o->isSigned()) | ||
if (not o->isSigned()) | ||
return type.editPolicy(id, o, n, nid, a); | ||
if (o->owner != n->owner) { | ||
if (o->owner != n->owner or not n->isSigned()) { | ||
AmarOk1412 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
DHT_LOG.WARN("Edition forbidden: owner changed."); | ||
return false; | ||
} | ||
if (!n->signatureChecked) { | ||
n->signatureChecked = true; | ||
n->signatureValid = o->owner and o->owner->checkSignature(n->getToSign(), n->signature); | ||
} | ||
if (!n->signatureValid) { | ||
DHT_LOG.WARN("Edition forbidden: signature verification failed."); | ||
if (not n->checkSignature()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too. |
||
return false; | ||
} | ||
if (o->seq == n->seq) { | ||
// If the data is exactly the same, | ||
// it can be reannounced, possibly by someone else. | ||
|
@@ -241,35 +227,23 @@ SecureDht::checkValue(const Sp<Value>& v) | |
#endif | ||
return {}; | ||
} | ||
if (v->decrypted) { | ||
return v->decryptedValue; | ||
} | ||
v->decrypted = true; | ||
try { | ||
Value decrypted_val (decrypt(*v)); | ||
if (decrypted_val.recipient == getId()) { | ||
if (decrypted_val.owner) | ||
nodesPubKeys_[decrypted_val.owner->getId()] = decrypted_val.owner; | ||
v->decryptedValue = std::make_shared<Value>(std::move(decrypted_val)); | ||
return v->decryptedValue; | ||
if (auto decrypted_val = v->decrypt(*key_)) { | ||
if (decrypted_val->owner) | ||
nodesPubKeys_[decrypted_val->owner->getId()] = decrypted_val->owner; | ||
return decrypted_val; | ||
} | ||
// Ignore values belonging to other people | ||
} catch (const std::exception& e) { | ||
DHT_LOG.WARN("Could not decrypt value %s : %s", v->toString().c_str(), e.what()); | ||
} | ||
} | ||
// Check signed values | ||
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)) { | ||
v->signatureValid = true; | ||
nodesPubKeys_[v->owner->getId()] = v->owner; | ||
if (v->checkSignature()) { | ||
if (v->owner) | ||
nodesPubKeys_[v->owner->getId()] = v->owner; | ||
return v; | ||
} | ||
else | ||
} else | ||
DHT_LOG.WARN("Signature verification failed for %s", v->toString().c_str()); | ||
} | ||
// Forward normal values | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -233,6 +233,41 @@ Value::toJson() const | |
return val; | ||
} | ||
|
||
bool | ||
Value::checkSignature() | ||
{ | ||
if (!signatureChecked) { | ||
signatureChecked = true; | ||
if (isSigned()) { | ||
signatureValid = owner and owner->checkSignature(getToSign(), signature); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this block for homogeneization |
||
signatureValid = true; | ||
} | ||
} | ||
return signatureValid; | ||
} | ||
|
||
Sp<Value> | ||
Value::decrypt(const crypto::PrivateKey& key) | ||
{ | ||
if (not decrypted) { | ||
decrypted = true; | ||
if (isEncrypted()) { | ||
auto decryptedBlob = key.decrypt(cypher); | ||
std::unique_ptr<Value> v {new Value(id)}; | ||
auto msg = msgpack::unpack((const char*)decryptedBlob.data(), decryptedBlob.size()); | ||
v->msgpack_unpack_body(msg.get()); | ||
if (v->recipient != key.getPublicKey().getId()) | ||
throw crypto::DecryptError("Recipient mismatch"); | ||
// Ignore values belonging to other people | ||
if (not v->owner or not v->owner->checkSignature(v->getToSign(), v->signature)) | ||
throw crypto::DecryptError("Signature mismatch"); | ||
decryptedValue = std::move(v); | ||
} | ||
} | ||
return decryptedValue; | ||
} | ||
|
||
uint64_t | ||
unpackId(const Json::Value& json, const std::string& key) { | ||
uint64_t ret = 0; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No more log if checkSignature fails "Signature verification failed"