// alterPublication will maliciously alter a publications information. // It returns true if the publication was altered. // It takes as input the publication. func (b *Broker) alterPublication(pub *pb.Publication) pb.Publication { tempPub := pb.Publication{ PubType: pub.PubType, PublisherID: pub.PublisherID, PublicationID: pub.PublicationID, TopicID: pub.TopicID, BrokerID: pub.BrokerID, } for i := range pub.Contents { tempPub.Contents = append(tempPub.Contents, pub.Contents[i]) } for i := range pub.ChainMACs { tempPub.ChainMACs = append(tempPub.ChainMACs, pub.ChainMACs[i]) } r := b.random.Intn(101) if r <= b.maliciousPercent { var alterType int if len(tempPub.ChainMACs) > 0 { alterType = r % 6 } else { alterType = r % 5 } switch alterType { case 0: tempPub.PublicationID = tempPub.PublicationID + 1 case 1: tempPub.PublisherID = tempPub.PublisherID + 1 case 2: tempPub.BrokerID = tempPub.BrokerID + 1 case 3: tempPub.TopicID = tempPub.TopicID + 1 case 4: if len(tempPub.Contents) > 0 { tempPub.Contents[0] = badMessage } case 5: tempPub.ChainMACs = nil } } return tempPub }
// addMACsRecursive adds MACs to the chain of MACs. Some older MACs may need to be kept depending // on the number of generations in the chain. // It takes as input the new publication, // the old publication, // the current node ID string in the tree (should start with the local node's children), // and the number of generations to add. func (b *Broker) addMACsRecursive(pub *pb.Publication, oldPub *pb.Publication, currentStr string, generations uint64) { // Add any old chain MACs that add going to the child node for _, oldChainMAC := range oldPub.ChainMACs { if oldChainMAC.To == currentStr { pub.ChainMACs = append(pub.ChainMACs, oldChainMAC) } } if generations > 1 { // Add MACs for all the broker children for _, childStr := range b.chainNodes[currentStr].children { addMAC := true // Check if the subscriber is subscribed to this topic if strings.HasPrefix(childStr, "S") { childID, _ := strconv.ParseUint(childStr[1:], 10, 64) b.subscribersMutex.RLock() if b.subscribers[childID].topics[pub.TopicID] == false { // Don't add the MAC is the subscriber isn't interested in this topic addMAC = false } b.subscribersMutex.RUnlock() } if addMAC { chainMAC := pb.ChainMAC{ From: b.localStr, To: childStr, MAC: common.CreatePublicationMAC(pub, b.chainNodes[childStr].key), } pub.ChainMACs = append(pub.ChainMACs, &chainMAC) // Recursively add child macs for next generation b.addMACsRecursive(pub, oldPub, childStr, generations-1) } } } }
// addMACsRecursive adds MACs to the chain of MACs. // It takes as input the publication, // the current node ID string in the tree (should start with the local node's children), // and the number of generations to add. func (p *Publisher) addMACs(pub *pb.Publication, currentStr string, generations uint64) { if generations > 1 { // Add MACs for all the children for _, childStr := range p.chainNodes[currentStr].children { chainMAC := pb.ChainMAC{ From: p.localStr, To: childStr, MAC: common.CreatePublicationMAC(pub, p.chainNodes[childStr].key), } pub.ChainMACs = append(pub.ChainMACs, &chainMAC) // Recursively add child macs for next generation p.addMACs(pub, childStr, generations-1) } } }