mirror of
https://github.com/LBRYFoundation/lbcd.git
synced 2025-08-23 17:47:24 +00:00
fix up some indexing in ParseSignature.
a test i was working on was crashing this otherwise. Make length checks more paranoid.
This commit is contained in:
parent
9cb1f47fb9
commit
5c8c454a18
1 changed files with 8 additions and 5 deletions
13
signature.go
13
signature.go
|
@ -58,15 +58,17 @@ func ParseSignature(sigStr []byte, curve elliptic.Curve) (*Signature, error) {
|
||||||
|
|
||||||
// Length of signature R.
|
// Length of signature R.
|
||||||
rLen := int(sigStr[index])
|
rLen := int(sigStr[index])
|
||||||
if rLen < 0 || rLen > len(sigStr)-index {
|
// must be positive, must be able to fit in another 0x2, <len> <s>
|
||||||
|
// hence the -3. We assume that the length must be at least one byte.
|
||||||
|
index++
|
||||||
|
if rLen <= 0 || rLen > len(sigStr)-index-3 {
|
||||||
return nil, errors.New("malformed signature: bogus R length")
|
return nil, errors.New("malformed signature: bogus R length")
|
||||||
}
|
}
|
||||||
index++
|
|
||||||
|
|
||||||
// Then R itself.
|
// Then R itself.
|
||||||
signature.R = new(big.Int).SetBytes(sigStr[index : index+rLen])
|
signature.R = new(big.Int).SetBytes(sigStr[index : index+rLen])
|
||||||
index += rLen
|
index += rLen
|
||||||
// 0x02
|
// 0x02. length already checked in previous if.
|
||||||
if sigStr[index] != 0x02 {
|
if sigStr[index] != 0x02 {
|
||||||
return nil, errors.New("malformed signature: no 2nd int marker")
|
return nil, errors.New("malformed signature: no 2nd int marker")
|
||||||
}
|
}
|
||||||
|
@ -74,10 +76,11 @@ func ParseSignature(sigStr []byte, curve elliptic.Curve) (*Signature, error) {
|
||||||
|
|
||||||
// Length of signature S.
|
// Length of signature S.
|
||||||
sLen := int(sigStr[index])
|
sLen := int(sigStr[index])
|
||||||
if sLen < 0 || sLen > len(sigStr)-index {
|
index++
|
||||||
|
// S should be the rest of the string.
|
||||||
|
if sLen <= 0 || sLen > len(sigStr)-index {
|
||||||
return nil, errors.New("malformed signature: bogus S length")
|
return nil, errors.New("malformed signature: bogus S length")
|
||||||
}
|
}
|
||||||
index++
|
|
||||||
|
|
||||||
// Then S itself.
|
// Then S itself.
|
||||||
signature.S = new(big.Int).SetBytes(sigStr[index : index+sLen])
|
signature.S = new(big.Int).SetBytes(sigStr[index : index+sLen])
|
||||||
|
|
Loading…
Add table
Reference in a new issue