Repository navigation
feat(crypto): add strict ECDSA validation #7001
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
base: release_v4.8.3
Are you sure you want to change the base?
Changes from all commits
b125b72
63c14c8
7a8ade7
a746a56
8f787f0
1e0b6a0
dfc2f60
a72b84e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
|
|
||
| package org.tron.core.capsule; | ||
|
|
||
| import static org.tron.core.Constant.PER_SIGN_LENGTH; | ||
| import static org.tron.core.exception.BadBlockException.TypeEnum.CALC_MERKLE_ROOT_FAILED; | ||
|
|
||
| import com.google.common.primitives.Longs; | ||
|
|
@@ -186,10 +187,17 @@ private Sha256Hash getRawHash() { | |
| public boolean validateSignature(DynamicPropertiesStore dynamicPropertiesStore, | ||
| AccountStore accountStore) throws ValidateSignatureException { | ||
| try { | ||
| ByteString witnessSignature = block.getBlockHeader().getWitnessSignature(); | ||
| boolean strictEcdsaValidation = CommonParameter.getInstance().isECKeyCryptoEngine() | ||
| && dynamicPropertiesStore.allowStrictEcdsaValidation(); | ||
|
Comment on lines
+191
to
+192
Collaborator
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. [SHOULD] Revalidate the witness signature after the parent-block state is established
For example, block B may pass verification under the old rules while its parent block A is still being processed. After A activates strict signature validation, B can be applied without revalidation in Suggested fix: Revalidate the witness signature at the beginning of if (!block.generatedByMyself
&& !block.validateSignature(getDynamicPropertiesStore(), getAccountStore())) {
throw new ValidateSignatureException(
"block " + block.getNum() + " signature invalid");
}Please also add a regression test covering B passing preliminary verification under the old rules but being rejected after A activates strict validation.
Collaborator
Author
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. @3for Revalidation in
I suggest handling this in a separate PR: move authoritative state-dependent validation into Manager after the correct parent state is established, with corresponding changes to synchronization, broadcasting, caching, and peer attribution. This would address both stale acceptance and premature rejection while avoiding duplicate witness verification. |
||
| if (strictEcdsaValidation | ||
| && !SignUtils.isValidLength(witnessSignature.size())) { | ||
|
Federico2014 marked this conversation as resolved.
|
||
| throw new ValidateSignatureException("Invalid ECDSA signature format"); | ||
| } | ||
| byte[] sigAddress = SignUtils.signatureToAddress(getRawHash().getBytes(), | ||
| TransactionCapsule.getBase64FromByteString( | ||
| block.getBlockHeader().getWitnessSignature()), | ||
| CommonParameter.getInstance().isECKeyCryptoEngine()); | ||
| witnessSignature), | ||
| CommonParameter.getInstance().isECKeyCryptoEngine(), strictEcdsaValidation); | ||
| byte[] witnessAccountAddress = block.getBlockHeader().getRawData().getWitnessAddress() | ||
| .toByteArray(); | ||
|
|
||
|
|
@@ -332,18 +340,27 @@ public boolean hasWitnessSignature() { | |
| public boolean sanitize() { | ||
| boolean blockHasUnknown = !this.block.getUnknownFields().asMap().isEmpty(); | ||
| boolean headerHasUnknown = !this.block.getBlockHeader().getUnknownFields().asMap().isEmpty(); | ||
| if (!blockHasUnknown && !headerHasUnknown) { | ||
| ByteString witnessSignature = this.block.getBlockHeader().getWitnessSignature(); | ||
| boolean hasOverlongWitnessSignature = witnessSignature.size() > PER_SIGN_LENGTH; | ||
| if (!blockHasUnknown && !headerHasUnknown && !hasOverlongWitnessSignature) { | ||
| return false; | ||
| } | ||
| UnknownFieldSet empty = UnknownFieldSet.getDefaultInstance(); | ||
| Block.Builder builder = this.block.toBuilder(); | ||
| BlockHeader.Builder headerBuilder = this.block.getBlockHeader().toBuilder(); | ||
| if (blockHasUnknown) { | ||
| builder.setUnknownFields(empty); | ||
| } | ||
| if (headerHasUnknown) { | ||
| builder.setBlockHeader(this.block.getBlockHeader().toBuilder() | ||
| .setUnknownFields(empty) | ||
| .build()); | ||
| headerBuilder.setUnknownFields(empty); | ||
| } | ||
| if (hasOverlongWitnessSignature) { | ||
| // Copy the canonical prefix so it does not retain the oversized signature's backing array. | ||
| headerBuilder.setWitnessSignature(ByteString.copyFrom( | ||
| witnessSignature.substring(0, PER_SIGN_LENGTH).toByteArray())); | ||
| } | ||
|
3for marked this conversation as resolved.
|
||
| if (headerHasUnknown || hasOverlongWitnessSignature) { | ||
| builder.setBlockHeader(headerBuilder.build()); | ||
| } | ||
| this.block = builder.build(); | ||
| return true; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.