Skip to content

Commit bb3b20b

Browse files
committed
Goosip MessageCryptoService and SecurityAdvisor Impl
This change-set implements the gossip MessageCryptoService and SecurityAdvisor. This implementation will be used by a peer to initialiaze the gossip layer. What remains to do is to integrate the channel read policies once they are in place. This change-set comes in the context of JIRA item: https://jira.hyperledger.org/browse/FAB-1394 Notice that before starting using the implementation provided by this change-set, the implementation of identity.GetOrganizationUnits() and the enforcement that DeserializeIdentity fails when the mspIDs do not match must be in place. Change-Id: I8b9cbe90044de14a68ba78ce5f7391f9e5da2cd0 Signed-off-by: Angelo De Caro <[email protected]>
1 parent bb41bbc commit bb3b20b

18 files changed

+511
-26
lines changed

gossip/api/channel.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ import (
2424
// that provides security and identity related capabilities
2525
type SecurityAdvisor interface {
2626
// OrgByPeerIdentity returns the OrgIdentityType
27-
// of a given peer identity
27+
// of a given peer identity.
28+
// If any error occurs, nil is returned.
29+
// This method does not validate peerIdentity.
30+
// This validation is supposed to be done appropriately during the execution flow.
2831
OrgByPeerIdentity(PeerIdentityType) OrgIdentityType
29-
30-
// Verify verifies a JoinChannelMessage, returns nil on success,
31-
// and an error on failure
32-
Verify(JoinChannelMessage) error
3332
}
3433

3534
// ChannelNotifier is implemented by the gossip component and is used for the peer

gossip/api/crypto.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,28 @@ import "github.com/hyperledger/fabric/gossip/common"
2525
type MessageCryptoService interface {
2626

2727
// GetPKIidOfCert returns the PKI-ID of a peer's identity
28+
// If any error occurs, the method return nil
29+
// This method does not validate peerIdentity.
30+
// This validation is supposed to be done appropriately during the execution flow.
2831
GetPKIidOfCert(peerIdentity PeerIdentityType) common.PKIidType
2932

3033
// VerifyBlock returns nil if the block is properly signed,
3134
// else returns error
32-
VerifyBlock(signedBlock SignedBlock) error
35+
VerifyBlock(chainID common.ChainID, signedBlock SignedBlock) error
3336

3437
// Sign signs msg with this peer's signing key and outputs
3538
// the signature if no error occurred.
3639
Sign(msg []byte) ([]byte, error)
3740

3841
// Verify checks that signature is a valid signature of message under a peer's verification key.
3942
// If the verification succeeded, Verify returns nil meaning no error occurred.
40-
// If peerIdentity is nil, then the signature is verified against this peer's verification key.
43+
// If peerIdentity is nil, then the verification fails.
4144
Verify(peerIdentity PeerIdentityType, signature, message []byte) error
4245

4346
// VerifyByChannel checks that signature is a valid signature of message
4447
// under a peer's verification key, but also in the context of a specific channel.
4548
// If the verification succeeded, Verify returns nil meaning no error occurred.
46-
// If peerIdentity is nil, then the signature is verified against this peer's verification key.
49+
// If peerIdentity is nil, then the verification fails.
4750
VerifyByChannel(chainID common.ChainID, peerIdentity PeerIdentityType, signature, message []byte) error
4851

4952
// ValidateIdentity validates the identity of a remote peer.

gossip/comm/comm_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (*naiveSecProvider) GetPKIidOfCert(peerIdentity api.PeerIdentityType) commo
6161

6262
// VerifyBlock returns nil if the block is properly signed,
6363
// else returns error
64-
func (*naiveSecProvider) VerifyBlock(signedBlock api.SignedBlock) error {
64+
func (*naiveSecProvider) VerifyBlock(chainID common.ChainID, signedBlock api.SignedBlock) error {
6565
return nil
6666
}
6767

gossip/gossip/channel/channel.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ func (gc *gossipChannel) verifyBlock(msg *proto.GossipMessage, sender common.PKI
461461
gc.logger.Warning("Received empty payload from", sender)
462462
return false
463463
}
464-
err := gc.mcs.VerifyBlock(msg.GetDataMsg().Payload)
464+
err := gc.mcs.VerifyBlock(msg.Channel, msg.GetDataMsg().Payload)
465465
if err != nil {
466466
gc.logger.Warning("Received fabricated block from", sender, "in DataUpdate:", err)
467467
return false

gossip/gossip/channel/channel_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (cs *cryptoService) VerifyByChannel(_ common.ChainID, _ api.PeerIdentityTyp
9898
panic("Should not be called in this test")
9999
}
100100

101-
func (cs *cryptoService) VerifyBlock(signedBlock api.SignedBlock) error {
101+
func (cs *cryptoService) VerifyBlock(chainID common.ChainID, signedBlock api.SignedBlock) error {
102102
args := cs.Called(signedBlock)
103103
if args.Get(0) == nil {
104104
return nil

gossip/gossip/gossip_impl.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,7 @@ func (g *gossipServiceImpl) toDie() bool {
153153
}
154154

155155
func (g *gossipServiceImpl) JoinChan(joinMsg api.JoinChannelMessage, chainID common.ChainID) {
156-
if err := g.secAdvisor.Verify(joinMsg); err != nil {
157-
g.logger.Error("Failed verifying join channel message", joinMsg, "error:", err)
158-
return
159-
}
156+
// joinMsg is supposed to have been already verified
160157
g.chanState.joinChannel(joinMsg, chainID)
161158

162159
selfPkiID := g.mcs.GetPKIidOfCert(g.selfIdentity)
@@ -343,7 +340,7 @@ func (g *gossipServiceImpl) validateMsg(msg comm.ReceivedMessage) bool {
343340
return true
344341
}
345342

346-
if err := g.mcs.VerifyBlock(blockMsg); err != nil {
343+
if err := g.mcs.VerifyBlock(msg.GetGossipMessage().Channel, blockMsg); err != nil {
347344
g.logger.Warning("Could not verify block", blockMsg.Payload.SeqNum, ":", err)
348345
return false
349346
}

gossip/gossip/gossip_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (*naiveCryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) com
115115

116116
// VerifyBlock returns nil if the block is properly signed,
117117
// else returns error
118-
func (*naiveCryptoService) VerifyBlock(signedBlock api.SignedBlock) error {
118+
func (*naiveCryptoService) VerifyBlock(chainID common.ChainID, signedBlock api.SignedBlock) error {
119119
return nil
120120
}
121121

gossip/identity/identity.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ package identity
1818

1919
import (
2020
"bytes"
21-
"fmt"
2221
"sync"
2322

23+
"errors"
24+
2425
"github.com/hyperledger/fabric/gossip/api"
2526
"github.com/hyperledger/fabric/gossip/common"
2627
)
@@ -66,10 +67,10 @@ func NewIdentityMapper(mcs api.MessageCryptoService) Mapper {
6667
// in case the given pkiID doesn't match the identity
6768
func (is *identityMapperImpl) Put(pkiID common.PKIidType, identity api.PeerIdentityType) error {
6869
if pkiID == nil {
69-
return fmt.Errorf("pkiID is nil")
70+
return errors.New("PkiID is nil")
7071
}
7172
if identity == nil {
72-
return fmt.Errorf("identity is nil")
73+
return errors.New("Identity is nil")
7374
}
7475

7576
if err := is.mcs.ValidateIdentity(identity); err != nil {
@@ -78,7 +79,7 @@ func (is *identityMapperImpl) Put(pkiID common.PKIidType, identity api.PeerIdent
7879

7980
id := is.mcs.GetPKIidOfCert(identity)
8081
if !bytes.Equal(pkiID, id) {
81-
return fmt.Errorf("Identity doesn't match the computed pkiID")
82+
return errors.New("Identity doesn't match the computed pkiID")
8283
}
8384

8485
is.Lock()
@@ -94,7 +95,7 @@ func (is *identityMapperImpl) Get(pkiID common.PKIidType) (api.PeerIdentityType,
9495
defer is.RUnlock()
9596
identity, exists := is.pkiID2Cert[string(pkiID)]
9697
if !exists {
97-
return nil, fmt.Errorf("pkiID wasn't found")
98+
return nil, errors.New("PkiID wasn't found")
9899
}
99100
return identity, nil
100101
}

gossip/identity/identity_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (*naiveCryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) com
4242

4343
// VerifyBlock returns nil if the block is properly signed,
4444
// else returns error
45-
func (*naiveCryptoService) VerifyBlock(signedBlock api.SignedBlock) error {
45+
func (*naiveCryptoService) VerifyBlock(chainID common.ChainID, signedBlock api.SignedBlock) error {
4646
return nil
4747
}
4848

gossip/integration/integration.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (*naiveCryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) com
8282

8383
// VerifyBlock returns nil if the block is properly signed,
8484
// else returns error
85-
func (*naiveCryptoService) VerifyBlock(signedBlock api.SignedBlock) error {
85+
func (*naiveCryptoService) VerifyBlock(chainID common.ChainID, signedBlock api.SignedBlock) error {
8686
return nil
8787
}
8888

gossip/state/state_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (*naiveCryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) com
8888

8989
// VerifyBlock returns nil if the block is properly signed,
9090
// else returns error
91-
func (*naiveCryptoService) VerifyBlock(signedBlock api.SignedBlock) error {
91+
func (*naiveCryptoService) VerifyBlock(chainID common.ChainID, signedBlock api.SignedBlock) error {
9292
return nil
9393
}
9494

msp/mgmt/mgmt.go

+33
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ func GetManagerForChain(ChainID string) msp.MSPManager {
5959
return mspMgr
6060
}
6161

62+
// GetManagers returns all the managers registered
63+
func GetManagers() map[string]msp.MSPManager {
64+
m.Lock()
65+
defer m.Unlock()
66+
67+
clone := make(map[string]msp.MSPManager)
68+
69+
for key, mspManager := range mspMap {
70+
clone[key] = mspManager
71+
}
72+
73+
return clone
74+
}
75+
76+
// GetManagerForChainIfExists returns the MSPManager associated to ChainID
77+
// it it exists
78+
func GetManagerForChainIfExists(ChainID string) msp.MSPManager {
79+
m.Lock()
80+
defer m.Unlock()
81+
82+
return mspMap[ChainID]
83+
}
84+
6285
// GetLocalMSP returns the local msp (and creates it if it doesn't exist)
6386
func GetLocalMSP() msp.MSP {
6487
var lclMsp msp.MSP
@@ -96,3 +119,13 @@ func GetMSPCommon(chainID string) msp.Common {
96119

97120
return GetManagerForChain(chainID)
98121
}
122+
123+
// GetLocalSigningIdentityOrPanic returns the local signing identity or panic in case
124+
// or error
125+
func GetLocalSigningIdentityOrPanic() msp.SigningIdentity {
126+
id, err := GetLocalMSP().GetDefaultSigningIdentity()
127+
if err != nil {
128+
peerLogger.Panic("Failed getting local signing identity [%s]", err)
129+
}
130+
return id
131+
}

msp/msp.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import (
2727

2828
//Common is implemented by both MSPManger and MSP
2929
type Common interface {
30-
// DeserializeIdentity deserializes an identity
30+
// DeserializeIdentity deserializes an identity.
31+
// Deserialization will fail if the identity is associated to
32+
// an msp that is different from this one that is performing
33+
// the deserialization.
3134
DeserializeIdentity(serializedIdentity []byte) (Identity, error)
3235
}
3336

msp/mspimpl.go

+2
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ func (msp *bccspmsp) DeserializeIdentity(serializedID []byte) (Identity, error)
269269
return nil, fmt.Errorf("Could not deserialize a SerializedIdentity, err %s", err)
270270
}
271271

272+
// TODO: check that sId.Mspid is equal to this msp'id as per contract of the interface.
273+
272274
// This MSP will always deserialize certs this way
273275
bl, _ := pem.Decode(sId.IdBytes)
274276
if bl == nil {

0 commit comments

Comments
 (0)