Skip to content
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

Check message address against signature #477

Merged
merged 6 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions consensus/istanbul/core/testbackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func (self *testSystemBackend) Broadcast(valSet istanbul.ValidatorSet, message [
return nil
}
func (self *testSystemBackend) Gossip(valSet istanbul.ValidatorSet, message []byte, msgCode uint64, ignoreCache bool) error {
testLogger.Warn("not sign any data")
return nil
}

Expand Down Expand Up @@ -130,16 +129,15 @@ func (self *testSystemBackend) Verify(proposal istanbul.Proposal, src istanbul.V
}

func (self *testSystemBackend) Sign(data []byte) ([]byte, error) {
testLogger.Warn("not sign any data")
return data, nil
return self.Address().Bytes(), nil
timmoreton marked this conversation as resolved.
Show resolved Hide resolved
}

func (self *testSystemBackend) CheckSignature([]byte, common.Address, []byte) error {
return nil
}

func (self *testSystemBackend) CheckValidatorSignature(data []byte, sig []byte) (common.Address, error) {
return common.Address{}, nil
return common.BytesToAddress(sig), nil
}

func (self *testSystemBackend) Hash(b interface{}) common.Hash {
Expand Down
16 changes: 14 additions & 2 deletions consensus/istanbul/core/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,15 @@ func testSubjectWithSignature(t *testing.T) {
}
expectedSig := []byte{0x01}

correctAddress := common.HexToAddress("0x1")
spooferAddress := common.HexToAddress("0x2")

subjectPayload, _ := Encode(s)
// 1. Encode test
m := &istanbul.Message{
Code: istanbul.MsgPreprepare,
Msg: subjectPayload,
Address: common.HexToAddress("0x1234567890"),
Address: correctAddress,
Signature: expectedSig,
CommittedSeal: []byte{},
}
Expand All @@ -141,7 +144,7 @@ func testSubjectWithSignature(t *testing.T) {
// 2.1 Test normal validate func
decodedMsg := new(istanbul.Message)
err = decodedMsg.FromPayload(msgPayload, func(data []byte, sig []byte) (common.Address, error) {
return common.Address{}, nil
return correctAddress, nil
})
if err != nil {
t.Errorf("error mismatch: have %v, want nil", err)
Expand Down Expand Up @@ -170,6 +173,15 @@ func testSubjectWithSignature(t *testing.T) {
if err != istanbul.ErrUnauthorizedAddress {
t.Errorf("error mismatch: have %v, want %v", err, istanbul.ErrUnauthorizedAddress)
}

// 2.4 Test spoofing signature by another validator validate func
decodedMsg = new(istanbul.Message)
err = decodedMsg.FromPayload(msgPayload, func(data []byte, sig []byte) (common.Address, error) {
return spooferAddress, nil
})
if err != istanbul.ErrInvalidSigner {
t.Errorf("error mismatch: have %v, want ErrInvalidSigner", err)
}
}

func TestMessageEncodeDecode(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions consensus/istanbul/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var (
// ErrUnauthorizedAddress is returned when given address cannot be found in
// current validator set.
ErrUnauthorizedAddress = errors.New("unauthorized address")
// ErrInvalidSigner is returned if a message's signature does not correspond to the address in msg.Address
ErrInvalidSigner = errors.New("signed by incorrect validator")
// ErrStoppedEngine is returned if the engine is stopped
ErrStoppedEngine = errors.New("stopped engine")
// ErrStartedEngine is returned if the engine is already started
Expand Down
11 changes: 8 additions & 3 deletions consensus/istanbul/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,15 @@ func (m *Message) FromPayload(b []byte, validateFn func([]byte, []byte) (common.
return err
}

_, err = validateFn(payload, m.Signature)
signed_val_addr, err := validateFn(payload, m.Signature)
if err != nil {
return err
}
if signed_val_addr != m.Address {
return ErrInvalidSigner
}
}
// Still return the message even the err is not nil
return err
return nil
}

func (m *Message) Payload() ([]byte, error) {
Expand Down