@@ -23,6 +23,7 @@ import (
23
23
"sync/atomic"
24
24
"time"
25
25
26
+ common_utils "github.com/hyperledger/fabric/common/util"
26
27
"github.com/hyperledger/fabric/gossip/api"
27
28
"github.com/hyperledger/fabric/gossip/comm"
28
29
"github.com/hyperledger/fabric/gossip/common"
@@ -115,6 +116,7 @@ type gossipChannel struct {
115
116
sync.RWMutex
116
117
shouldGossipStateInfo int32
117
118
mcs api.MessageCryptoService
119
+ pkiID common.PKIidType
118
120
stopChan chan struct {}
119
121
stateInfoMsg * proto.SignedGossipMessage
120
122
orgs []api.OrgIdentityType
@@ -147,8 +149,9 @@ func (mf *membershipFilter) GetMembership() []discovery.NetworkMember {
147
149
}
148
150
149
151
// NewGossipChannel creates a new GossipChannel
150
- func NewGossipChannel (mcs api.MessageCryptoService , chainID common.ChainID , adapter Adapter , joinMsg api.JoinChannelMessage ) GossipChannel {
152
+ func NewGossipChannel (pkiID common. PKIidType , mcs api.MessageCryptoService , chainID common.ChainID , adapter Adapter , joinMsg api.JoinChannelMessage ) GossipChannel {
151
153
gc := & gossipChannel {
154
+ pkiID : pkiID ,
152
155
mcs : mcs ,
153
156
Adapter : adapter ,
154
157
logger : util .GetLogger (util .LoggingChannelModule , adapter .GetConf ().ID ),
@@ -359,7 +362,7 @@ func (gc *gossipChannel) HandleMessage(msg proto.ReceivedMessage) {
359
362
return
360
363
}
361
364
if ! gc .IsOrgInChannel (orgID ) {
362
- gc .logger .Warning ("Point to point message came from" , msg .GetConnectionInfo ().ID , "but it's not eligible for the channel" , msg . GetGossipMessage (). Channel )
365
+ gc .logger .Warning ("Point to point message came from" , msg .GetConnectionInfo ().ID , "but it's not eligible for the channel" , string ( gc . chainID ) )
363
366
return
364
367
}
365
368
@@ -405,7 +408,7 @@ func (gc *gossipChannel) HandleMessage(msg proto.ReceivedMessage) {
405
408
}
406
409
if m .IsPullMsg () && m .GetPullMsgType () == proto .PullMsgType_BLOCK_MSG {
407
410
if ! gc .EligibleForChannel (discovery.NetworkMember {PKIid : msg .GetConnectionInfo ().ID }) {
408
- gc .logger .Warning (msg .GetConnectionInfo ().ID , "isn't eligible for channel" , gc .chainID )
411
+ gc .logger .Warning (msg .GetConnectionInfo ().ID , "isn't eligible for channel" , string ( gc .chainID ) )
409
412
return
410
413
}
411
414
if m .IsDataUpdate () {
@@ -437,35 +440,42 @@ func (gc *gossipChannel) HandleMessage(msg proto.ReceivedMessage) {
437
440
}
438
441
439
442
func (gc * gossipChannel ) handleStateInfSnapshot (m * proto.GossipMessage , sender common.PKIidType ) {
443
+ chanName := string (gc .chainID )
440
444
for _ , envelope := range m .GetStateSnapshot ().Elements {
441
445
stateInf , err := envelope .ToGossipMessage ()
442
446
if err != nil {
443
- gc .logger .Warning ("StateInfo snapshot contains an invalid message:" , err )
447
+ gc .logger .Warning ("Channel" , chanName , ": StateInfo snapshot contains an invalid message:" , err )
444
448
return
445
449
}
446
450
if ! stateInf .IsStateInfoMsg () {
447
- gc .logger .Warning ("Element of StateInfoSnapshot isn't a StateInfoMessage:" , stateInf , "message sent from" , sender )
451
+ gc .logger .Warning ("Channel" , chanName , ": Element of StateInfoSnapshot isn't a StateInfoMessage:" ,
452
+ stateInf , "message sent from" , sender )
448
453
return
449
454
}
450
-
451
- orgID := gc .GetOrgOfPeer (stateInf . GetStateInfo () .PkiId )
455
+ si := stateInf . GetStateInfo ()
456
+ orgID := gc .GetOrgOfPeer (si .PkiId )
452
457
if orgID == nil {
453
- gc .logger .Warning ("Couldn't find org identity of peer" , stateInf .GetStateInfo ().PkiId , "message sent from" , sender )
458
+ gc .logger .Warning ("Channel" , chanName , ": Couldn't find org identity of peer" ,
459
+ si .PkiId , "message sent from" , sender )
454
460
return
455
461
}
456
462
457
463
if ! gc .IsOrgInChannel (orgID ) {
458
- gc .logger .Warning ("Peer" , stateInf .GetStateInfo ().PkiId , "is not in an eligible org, can't process a stateInfo from it, sent from" , sender )
464
+ gc .logger .Warning ("Channel" , chanName , ": Peer" , stateInf .GetStateInfo ().PkiId ,
465
+ "is not in an eligible org, can't process a stateInfo from it, sent from" , sender )
459
466
return
460
467
}
461
468
462
- if ! bytes .Equal (stateInf .Channel , []byte (gc .chainID )) {
463
- gc .logger .Warning ("StateInfo message is of an invalid channel" , stateInf , "sent from" , sender )
469
+ expectedMAC := ChannelMAC (si .PkiId , gc .chainID )
470
+ if ! bytes .Equal (si .ChannelMAC , expectedMAC ) {
471
+ gc .logger .Warning ("Channel" , chanName , ": StateInfo message" , stateInf ,
472
+ ", has an invalid MAC. Expected" , expectedMAC , ", got" , si .ChannelMAC , ", sent from" , sender )
464
473
return
465
474
}
466
475
err = gc .ValidateStateInfoMessage (stateInf )
467
476
if err != nil {
468
- gc .logger .Warning ("Failed validating state info message:" , stateInf , ":" , err , "sent from" , sender )
477
+ gc .logger .Warning ("Channel" , chanName , ": Failed validating state info message:" ,
478
+ stateInf , ":" , err , "sent from" , sender )
469
479
return
470
480
}
471
481
gc .stateInfoMsgStore .Add (stateInf )
@@ -524,6 +534,26 @@ func (gc *gossipChannel) verifyMsg(msg proto.ReceivedMessage) bool {
524
534
return false
525
535
}
526
536
537
+ if m .IsStateInfoMsg () {
538
+ si := m .GetStateInfo ()
539
+ expectedMAC := ChannelMAC (si .PkiId , gc .chainID )
540
+ if ! bytes .Equal (expectedMAC , si .ChannelMAC ) {
541
+ gc .logger .Warning ("Message contains wrong channel MAC(" , si .ChannelMAC , "), expected" , expectedMAC )
542
+ return false
543
+ }
544
+ return true
545
+ }
546
+
547
+ if m .IsStateInfoPullRequestMsg () {
548
+ sipr := m .GetStateInfoPullReq ()
549
+ expectedMAC := ChannelMAC (msg .GetConnectionInfo ().ID , gc .chainID )
550
+ if ! bytes .Equal (expectedMAC , sipr .ChannelMAC ) {
551
+ gc .logger .Warning ("Message contains wrong channel MAC(" , sipr .ChannelMAC , "), expected" , expectedMAC )
552
+ return false
553
+ }
554
+ return true
555
+ }
556
+
527
557
if ! bytes .Equal (m .Channel , []byte (gc .chainID )) {
528
558
gc .logger .Warning ("Message contains wrong channel(" , m .Channel , "), expected" , gc .chainID )
529
559
return false
@@ -533,11 +563,12 @@ func (gc *gossipChannel) verifyMsg(msg proto.ReceivedMessage) bool {
533
563
534
564
func (gc * gossipChannel ) createStateInfoRequest () * proto.SignedGossipMessage {
535
565
return (& proto.GossipMessage {
536
- Channel : gc .chainID ,
537
- Tag : proto .GossipMessage_CHAN_OR_ORG ,
538
- Nonce : 0 ,
566
+ Tag : proto .GossipMessage_CHAN_OR_ORG ,
567
+ Nonce : 0 ,
539
568
Content : & proto.GossipMessage_StateInfoPullReq {
540
- StateInfoPullReq : & proto.StateInfoPullRequest {},
569
+ StateInfoPullReq : & proto.StateInfoPullRequest {
570
+ ChannelMAC : ChannelMAC (gc .pkiID , gc .chainID ),
571
+ },
541
572
},
542
573
}).NoopSign ()
543
574
}
@@ -586,3 +617,11 @@ func (cache *stateInfoCache) Add(msg *proto.SignedGossipMessage) bool {
586
617
}
587
618
return added
588
619
}
620
+
621
+ // ChannelMAC returns a byte slice that is derived from the peer's PKI-ID
622
+ // and a channel name
623
+ func ChannelMAC (pkiID common.PKIidType , channelID common.ChainID ) []byte {
624
+ // Hash is computed on (PKI-ID || channel ID)
625
+ preImage := append ([]byte (pkiID ), []byte (channelID )... )
626
+ return common_utils .ComputeSHA256 (preImage )
627
+ }
0 commit comments