@@ -22,10 +22,13 @@ import (
22
22
23
23
"github.com/hyperledger/fabric/bccsp"
24
24
"github.com/hyperledger/fabric/bccsp/factory"
25
+ mockpolicy "github.com/hyperledger/fabric/common/mocks/policies"
26
+ "github.com/hyperledger/fabric/common/policies"
25
27
"github.com/hyperledger/fabric/gossip/api"
26
28
"github.com/hyperledger/fabric/gossip/common"
27
29
"github.com/hyperledger/fabric/msp"
28
30
"github.com/hyperledger/fabric/msp/mgmt"
31
+ protoscommon "github.com/hyperledger/fabric/protos/common"
29
32
"github.com/op/go-logging"
30
33
)
31
34
@@ -56,7 +59,11 @@ func NewMessageCryptoService() api.MessageCryptoService {
56
59
// If the identity is invalid, revoked, expired it returns an error.
57
60
// Else, returns nil
58
61
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 )
60
67
return err
61
68
}
62
69
@@ -107,14 +114,25 @@ func (s *mspMessageCryptoService) Sign(msg []byte) ([]byte, error) {
107
114
// If the verification succeeded, Verify returns nil meaning no error occurred.
108
115
// If peerIdentity is nil, then the verification fails.
109
116
func (s * mspMessageCryptoService ) Verify (peerIdentity api.PeerIdentityType , signature , message []byte ) error {
110
- identity , err := s .getValidatedIdentity (peerIdentity )
117
+ identity , chainID , err := s .getValidatedIdentity (peerIdentity )
111
118
if err != nil {
112
119
logger .Errorf ("Failed getting validated identity from peer identity [%s]" , err )
113
120
114
121
return err
115
122
}
116
123
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 )
118
136
}
119
137
120
138
// VerifyByChannel checks that signature is a valid signature of message
@@ -127,36 +145,28 @@ func (s *mspMessageCryptoService) VerifyByChannel(chainID common.ChainID, peerId
127
145
return errors .New ("Invalid Peer Identity. It must be different from nil." )
128
146
}
129
147
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
+ )
154
164
}
155
165
156
- func (s * mspMessageCryptoService ) getValidatedIdentity (peerIdentity api.PeerIdentityType ) (msp.Identity , error ) {
166
+ func (s * mspMessageCryptoService ) getValidatedIdentity (peerIdentity api.PeerIdentityType ) (msp.Identity , common. ChainID , error ) {
157
167
// Validate arguments
158
168
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." )
160
170
}
161
171
162
172
// Notice that peerIdentity is assumed to be the serialization of an identity.
@@ -176,12 +186,16 @@ func (s *mspMessageCryptoService) getValidatedIdentity(peerIdentity api.PeerIden
176
186
// scoped messages.
177
187
// The following check is consistent with the SecurityAdvisor#OrgByPeerIdentity
178
188
// 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
180
190
// that DeserializeIdentity does not yet enforce MSP-IDs consistency.
181
191
// This check can be removed once DeserializeIdentity will be fixed.
182
192
if identity .GetMSPIdentifier () == mgmt .GetLocalSigningIdentityOrPanic ().GetMSPIdentifier () {
183
193
// 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 ()
185
199
}
186
200
}
187
201
@@ -195,17 +209,19 @@ func (s *mspMessageCryptoService) getValidatedIdentity(peerIdentity api.PeerIden
195
209
}
196
210
197
211
// 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
+
198
216
if err := identity .Validate (); err != nil {
199
217
logger .Debugf ("Failed validating identity [% x] on [%s]: [%s]" , peerIdentity , chainID , err )
200
218
continue
201
219
}
202
220
203
- // TODO: check that this identity is a reader of the channel
204
-
205
221
logger .Debugf ("Validation succesed [% x] on [%s]" , peerIdentity , chainID )
206
222
207
- return identity , nil
223
+ return identity , common . ChainID ( chainID ), nil
208
224
}
209
225
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 )
211
227
}
0 commit comments