diff --git a/pkg/crypto/keys/publickey.go b/pkg/crypto/keys/publickey.go index bf579ec06..311a7a207 100644 --- a/pkg/crypto/keys/publickey.go +++ b/pkg/crypto/keys/publickey.go @@ -8,6 +8,7 @@ import ( "encoding/json" "errors" "fmt" + gio "io" "math/big" "github.com/btcsuite/btcd/btcec" @@ -234,7 +235,15 @@ func decodeCompressedY(x *big.Int, ylsb uint, curve elliptic.Curve) (*big.Int, e func (p *PublicKey) DecodeBytes(data []byte) error { b := io.NewBinReaderFromBuf(data) p.DecodeBinary(b) - return b.Err + if b.Err != nil { + return b.Err + } + + b.ReadB() + if b.Err != gio.EOF { + return errors.New("extra data") + } + return nil } // DecodeBinary decodes a PublicKey from the given BinReader using information @@ -375,7 +384,7 @@ func (p *PublicKey) UnmarshalJSON(data []byte) error { return errors.New("wrong format") } - bytes := make([]byte, l-2) + bytes := make([]byte, hex.DecodedLen(l-2)) _, err := hex.Decode(bytes, data[1:l-1]) if err != nil { return err diff --git a/pkg/crypto/keys/publickey_test.go b/pkg/crypto/keys/publickey_test.go index 310fb6d68..30d992c14 100644 --- a/pkg/crypto/keys/publickey_test.go +++ b/pkg/crypto/keys/publickey_test.go @@ -67,6 +67,9 @@ func TestNewPublicKeyFromBytes(t *testing.T) { pub2, err := NewPublicKeyFromBytes(b, elliptic.P256()) require.NoError(t, err) require.Same(t, pub, pub2) + + _, err = NewPublicKeyFromBytes([]byte{0x00, 0x01}, elliptic.P256()) + require.Error(t, err) } func TestDecodeFromString(t *testing.T) {