Skip to content

Commit e60dcfe

Browse files
committed
Adding Policy check place-holder
With this change-set, signatures are checked against the channel's reader policy when it makes sense. This change-set only introduces a fake check that will be replaced once the channel's polcies will be in place. Change-Id: I3311d7509dccf5a1d13d67d434fbba76ea7a8621 Signed-off-by: Angelo De Caro <[email protected]>
1 parent 372d853 commit e60dcfe

File tree

2 files changed

+56
-35
lines changed

2 files changed

+56
-35
lines changed

common/policies/policy.go

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ import (
2525
logging "github.com/op/go-logging"
2626
)
2727

28+
const (
29+
// ChannelReaders is the label for the channel's readers policy
30+
ChannelReaders = "ChannelReaders"
31+
)
32+
2833
var logger = logging.MustGetLogger("common/policies")
2934

3035
// Policy is used to determine if a signature is valid

peer/gossip/mcs/mcs.go

+51-35
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ import (
2222

2323
"github.com/hyperledger/fabric/bccsp"
2424
"github.com/hyperledger/fabric/bccsp/factory"
25+
mockpolicy "github.com/hyperledger/fabric/common/mocks/policies"
26+
"github.com/hyperledger/fabric/common/policies"
2527
"github.com/hyperledger/fabric/gossip/api"
2628
"github.com/hyperledger/fabric/gossip/common"
2729
"github.com/hyperledger/fabric/msp"
2830
"github.com/hyperledger/fabric/msp/mgmt"
31+
protoscommon "github.com/hyperledger/fabric/protos/common"
2932
"github.com/op/go-logging"
3033
)
3134

@@ -56,7 +59,11 @@ func NewMessageCryptoService() api.MessageCryptoService {
5659
// If the identity is invalid, revoked, expired it returns an error.
5760
// Else, returns nil
5861
func (s *mspMessageCryptoService) ValidateIdentity(peerIdentity api.PeerIdentityType) error {
59-
_, err := s.getValidatedIdentity(peerIdentity)
62+
// As prescibed by the contract of method,
63+
// here we check only that peerIdentity is not
64+
// invalid, revoked or expired.
65+
66+
_, _, err := s.getValidatedIdentity(peerIdentity)
6067
return err
6168
}
6269

@@ -107,14 +114,25 @@ func (s *mspMessageCryptoService) Sign(msg []byte) ([]byte, error) {
107114
// If the verification succeeded, Verify returns nil meaning no error occurred.
108115
// If peerIdentity is nil, then the verification fails.
109116
func (s *mspMessageCryptoService) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error {
110-
identity, err := s.getValidatedIdentity(peerIdentity)
117+
identity, chainID, err := s.getValidatedIdentity(peerIdentity)
111118
if err != nil {
112119
logger.Errorf("Failed getting validated identity from peer identity [%s]", err)
113120

114121
return err
115122
}
116123

117-
return identity.Verify(message, signature)
124+
if len(chainID) == 0 {
125+
// At this stage, this means that peerIdentity
126+
// belongs to this peer's LocalMSP.
127+
// The signature is validated directly
128+
return identity.Verify(message, signature)
129+
}
130+
131+
// At this stage, the signature must be validated
132+
// against the reader policy of the channel
133+
// identified by chainID
134+
135+
return s.VerifyByChannel(chainID, peerIdentity, signature, message)
118136
}
119137

120138
// VerifyByChannel checks that signature is a valid signature of message
@@ -127,36 +145,28 @@ func (s *mspMessageCryptoService) VerifyByChannel(chainID common.ChainID, peerId
127145
return errors.New("Invalid Peer Identity. It must be different from nil.")
128146
}
129147

130-
// Notice that peerIdentity is assumed to be the serialization of an identity.
131-
// So, first step is the identity deserialization, then identity verification and
132-
// finally signature verification.
133-
mspManager := mgmt.GetManagerForChainIfExists(string(chainID))
134-
if mspManager == nil {
135-
return fmt.Errorf("Failed getting manager for chain [%s]. It does not exists.", chainID)
136-
}
137-
138-
// Deserialize identity
139-
identity, err := mspManager.DeserializeIdentity([]byte(peerIdentity))
140-
if err != nil {
141-
return fmt.Errorf("Failed deserializing identity [%s]: [%s]", chainID, err)
142-
}
143-
144-
// Check identity validity
145-
if err := identity.Validate(); err != nil {
146-
return fmt.Errorf("Failed validating identity [%s][%s]: [%s]", chainID, identity, err)
147-
}
148-
149-
// TODO: check that this identity is a reader of the channel
150-
151-
// Verify signature
152-
logger.Debugf("Veryfining on [%s] signature [% x]", chainID, signature)
153-
return identity.Verify(message, signature)
148+
// Get the policy manager for channel chainID
149+
// TODO: replace this mock with the proper lookup once in place
150+
// For now, we accept all
151+
policyManager := mockpolicy.Manager{Policy: &mockpolicy.Policy{Err: nil}}
152+
153+
// Get channel reader policy
154+
policy, flag := policyManager.GetPolicy(policies.ChannelReaders)
155+
logger.Debugf("Got reader policy for channel [%s] with flag [%s]", string(chainID), flag)
156+
157+
return policy.Evaluate(
158+
[]*protoscommon.SignedData{{
159+
Data: message,
160+
Identity: []byte(peerIdentity),
161+
Signature: signature,
162+
}},
163+
)
154164
}
155165

156-
func (s *mspMessageCryptoService) getValidatedIdentity(peerIdentity api.PeerIdentityType) (msp.Identity, error) {
166+
func (s *mspMessageCryptoService) getValidatedIdentity(peerIdentity api.PeerIdentityType) (msp.Identity, common.ChainID, error) {
157167
// Validate arguments
158168
if len(peerIdentity) == 0 {
159-
return nil, errors.New("Invalid Peer Identity. It must be different from nil.")
169+
return nil, nil, errors.New("Invalid Peer Identity. It must be different from nil.")
160170
}
161171

162172
// Notice that peerIdentity is assumed to be the serialization of an identity.
@@ -176,12 +186,16 @@ func (s *mspMessageCryptoService) getValidatedIdentity(peerIdentity api.PeerIden
176186
// scoped messages.
177187
// The following check is consistent with the SecurityAdvisor#OrgByPeerIdentity
178188
// implementation.
179-
// TODO: Notice that the followin check saves us from the fact
189+
// TODO: Notice that the following check saves us from the fact
180190
// that DeserializeIdentity does not yet enforce MSP-IDs consistency.
181191
// This check can be removed once DeserializeIdentity will be fixed.
182192
if identity.GetMSPIdentifier() == mgmt.GetLocalSigningIdentityOrPanic().GetMSPIdentifier() {
183193
// Check identity validity
184-
return identity, identity.Validate()
194+
195+
// Notice that at this stage we don't have to check the identity
196+
// against any channel's policies.
197+
// This will be done by the caller function, if needed.
198+
return identity, nil, identity.Validate()
185199
}
186200
}
187201

@@ -195,17 +209,19 @@ func (s *mspMessageCryptoService) getValidatedIdentity(peerIdentity api.PeerIden
195209
}
196210

197211
// Check identity validity
212+
// Notice that at this stage we don't have to check the identity
213+
// against any channel's policies.
214+
// This will be done by the caller function, if needed.
215+
198216
if err := identity.Validate(); err != nil {
199217
logger.Debugf("Failed validating identity [% x] on [%s]: [%s]", peerIdentity, chainID, err)
200218
continue
201219
}
202220

203-
// TODO: check that this identity is a reader of the channel
204-
205221
logger.Debugf("Validation succesed [% x] on [%s]", peerIdentity, chainID)
206222

207-
return identity, nil
223+
return identity, common.ChainID(chainID), nil
208224
}
209225

210-
return nil, fmt.Errorf("Peer Identity [% x] cannot be validated. No MSP found able to do that.", peerIdentity)
226+
return nil, nil, fmt.Errorf("Peer Identity [% x] cannot be validated. No MSP found able to do that.", peerIdentity)
211227
}

0 commit comments

Comments
 (0)