[Feature] Validate ECKey inputs and improve key handling
维护者通常 1 天内回复
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 40/100
- Issue 类型
- 功能
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- java
- 领域
- cryptography, security
调研方向
The ECKey class in the codebase handles secp256k1 key pairs. Start by locating the ECKey class and its constructors, focusing on private and public key validation methods. Review the keystore decryption path for temporary buffer cleanup. Write tests to verify input validation, equality symmetry, and that signing/recovery results remain unchanged for valid inputs. Check that modifications to returned arrays from getAddress() and getNodeId() do not affect cached values.
由索引模型根据 Issue 内容生成。
描述
Summary
Validate keys when they enter ECKey, protect its cached values from accidental modification, and clear temporary private-key bytes after keystore decryption.
What is the problem?
ECKey represents a secp256k1 key pair and is used for operations such as transaction and witness signing. Several entry points handle invalid input or key state inconsistently:
- Invalid keys can fail too late. Private-key input is not consistently checked for size and numeric range before conversion or curve calculations. Public-key construction does not consistently enforce the supported encoding, curve, and point-validity rules.
- A private key can be paired with the wrong public key. For example, an object can contain private key A and public key B. Its address is derived from B, while signature generation uses A.
sign()then tries to match the recovered public key to B; if no match is found, it throws. This can cause recoverable signing to fail after the object has already been constructed. - Callers can change cached values.
getAddress()andgetNodeId()return their internal arrays. Modifying a returned array also changes what the object returns later. - Equality can be asymmetric. A public-only key and a full key pair can represent the same public key, yet
publicOnly.equals(fullKey)can betruewhilefullKey.equals(publicOnly)isfalse. This violates the symmetry required by Java'sequals()contract. - Some APIs promise unsupported behavior or expose secrets. Constructors accept arbitrary cryptographic providers, although signing expects Bouncy Castle keys.
toStringWithPrivate()includes the private key in its output and can expose it if logged. - Keystore errors and cleanup need a consistent boundary. ECDSA private-key validation failures during keystore decryption can escape as
IllegalArgumentExceptioninstead ofCipherException. The temporary array containing the decrypted private key is not explicitly cleared.
Expected behavior
| Area | Required behavior |
|---|---|
| Private keys | Interpret byte arrays as unsigned big-endian integers and check their length before numeric conversion. Accept only values in 1 <= key < n, where n is the secp256k1 group order. Private-key import methods reject null and empty input. |
| Public keys | Accept 33-byte encodings starting with 0x02 or 0x03, or 65-byte encodings starting with 0x04. Decode bytes on secp256k1 and reject invalid points and the point at infinity. For ECPoint input, also verify that its curve is secp256k1. |
| Key pairs | When both components are supplied, verify that the public key is derived from the private key. Preserve public-only construction with a valid public key and no private key. |
| Cached values and equality | Return copies of address and node ID arrays. Define equality by public-key identity, consistently with hashCode(), regardless of private-key presence or the public key's input encoding. |
| Providers and helper APIs | Use Bouncy Castle for key generation and signing. Remove APIs that bypass key-pair checks or include private keys in string output. |
| Keystore decryption | Wrap ECDSA private-key validation failures in CipherException, preserving the cause. Clear the temporary decrypted private-key array on success, private-key validation failure, and declared-address mismatch. Shared cleanup applies to both ECDSA and SM2 decryption. |
Private-key byte arrays of 1–32 bytes remain supported when their value is valid. Also accept a valid 33-byte representation produced by BigInteger.toByteArray(): one leading 0x00 followed by a 32-byte magnitude whose high bit is set. That extra byte marks a positive number; it does not make the key invalid. Other encodings longer than 32 bytes are rejected.
The validation scope includes these helper entry points:
publicKeyFromPrivate()validates the private scalar before deriving the public key.fromNodeId()requires a 64-byte X/Y representation and applies public-key validation throughfromPublicOnly().signatureToKey()andrecoverFromSignature()apply the strengthened public-key invariants when constructing a returnedECKey.
These requirements do not imply identical exception types for every malformed argument to every helper. In particular, the existing fromNodeId(null) behavior is not being standardized by this change.
Clearing the temporary array reduces how long that copy remains in memory. The returned key object still retains the private key needed for signing.
Compatibility and migration
This changes the public Java API. Downstream callers using removed methods must update their code and recompile. Changes to equality and returned-array sharing can also affect callers that still compile successfully.
| Existing usage | Required change |
|---|---|
Provider-based ECKey constructors |
Use ECKey(SecureRandom) for generation, or the supported import constructors/factories. Arbitrary-provider support is removed. |
fromPrivateAndPrecalculatedPublic |
Use the validated ECKey(BigInteger, ECPoint) constructor when supplying both components. |
toStringWithPrivate() |
Removed without a private-key-printing replacement. Use toString() for diagnostic output. |
Expecting fromPrivate(byte[]) to return null for null or empty input |
Validate the input or handle IllegalArgumentException. |
| Handling invalid decrypted ECDSA keystore keys | Handle CipherException; the original validation exception is retained as its cause. |
Distinguishing public-only and full keys using equals(), sets, or map keys |
Both now have the same identity when their public keys match. Check private-key availability separately if needed. |
Relying on shared arrays from getAddress() or getNodeId() |
Returned arrays are independent copies. Modifying them no longer changes the object's cached value; use content comparison rather than array reference identity. |
Preserve the existing signing and recovery results for valid inputs. This work must not change transaction/block signature acceptance or TVM ecrecover behavior. Recovery helpers that return ECKey must enforce the strengthened public-key invariants, so malformed inputs may be rejected differently.
Configuration, consensus rules, and on-chain behavior remain unchanged. SM2 algorithm and key-validation changes are outside this issue; temporary-buffer cleanup applies to the shared keystore decryption path.
Validation
The acceptance criteria are:
- Input boundaries: Cover invalid scalar values, unsigned byte interpretation, oversized encodings, and valid 33-byte sign-padded keys. Reject malformed public-key encodings, invalid points, points on another curve supplied as
ECPoint, and mismatched key pairs. Accept valid pairs and public-only construction. ExercisepublicKeyFromPrivate()andfromNodeId()as well as constructors and import factories. - Equality and cached values: Check equality in both directions between public-only and full keys, and between keys constructed from compressed and uncompressed encodings. Equal keys must have equal hash codes. Modifying returned address or node ID arrays must not change cached values.
- Signing and recovery: Verify signing and recovery results for valid inputs, including existing signature vectors. Verify that
signatureToKey()andrecoverFromSignature()reject recovered public points that violate the new invariants. Cover transaction/block signature acceptance and TVMecrecovercompatibility. - Real invalid ECDSA keystores: Use a correct password, a valid MAC, and decryptable ciphertext whose plaintext is an invalid private scalar, such as zero or
n. Confirm that failure comes from private-key validation and is wrapped inCipherException, rather than from password, MAC, or file errors. - Temporary-buffer cleanup: Inspect the actual decrypted array after success and declared-address mismatch for both ECDSA and SM2, and after ECDSA private-key validation failure. Mocking or instrumentation may be used to observe that array, but must not replace all real invalid-key import coverage.
- Witness loading: Load a keystore containing a real invalid ECDSA private key through the witness initialization path. Confirm
WITNESS_KEYSTORE_LOADwithCipherExceptionand the original validation exception in the cause chain. Keep the existingWitnessInitializerproduction logic; an injected exception alone does not satisfy this end-to-end acceptance case.
- 主要语言
- Java
- 星标
- 4.2k
- 派生
- 1.8k
- 平均合并
- 3 天 21 小时
- 30 天内合并 PR
- 13
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
tronprotocol/java-tron 的其他 Issue
-
type:feature
难度 4/5 3-5 天 新手友好度 35/100
tronprotocol/java-tron#7013 ·
维护者通常 1 天内回复
-
type:feature
难度 5/5 一周以上 新手友好度 25/100
tronprotocol/java-tron#6989 · 2 条评论 ·
维护者通常 1 天内回复
-
Robustness fixes for multiple issues可能已有人在做 @xxo1shine 于 17 天前认领。 未关闭
难度 5/5 一周以上 新手友好度 35/100
tronprotocol/java-tron#6969 · 8 条评论 ·
维护者通常 1 天内回复
-
[Feature] decouple json-rpc filter processing from Manager可能已有人在做 @0xbigapple 于 16 天前认领。 未关闭type:feature
难度 5/5 一周以上 新手友好度 48/100
tronprotocol/java-tron#6963 · 8 条评论 ·
维护者通常 1 天内回复
-
[Feature] Remove unused SM2/SM3 crypto engine可能已有人在做 @Federico2014 于 15 天前认领。 未关闭type:feature
难度 5/5 一周以上 新手友好度 28/100
tronprotocol/java-tron#6959 · 3 条评论 ·
维护者通常 1 天内回复
查看 tronprotocol/java-tron 的全部 Issue
相似的 Issue
-
backend
难度 2/5 1-3 小时 新手友好度 76/100
bcgov/nr-forest-client#2524 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 85/100
Sinytra/ForgifiedFabricAPI#298 ·
-
难度 2/5 1-3 小时 新手友好度 67/100
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 74/100
维护者通常 1 天内回复
-
team:Lumberjack
难度 2/5 1-3 小时 新手友好度 76/100
OpenLiberty/open-liberty#35998 ·
维护者通常 1 天内回复