def init_self_connection(self): #Add myself to neighbors ownNeighborEntry = Neighbor() ownNeighborEntry.ipAddress = self.hostname ownNeighborEntry.encryptionKey = self.key ownNeighborEntry.encryptionIV = self.iv #Connect to own socket tempNeighborList = {} tempNeighborList[self.hostname] = ownNeighborEntry self.onAPJoined(tempNeighborList)
def sendFloodingMessage(self, message, max_ttl, msg_type='data', sub_type='std', key=str(), iv=str(), nodeID="255.255.255.255"): self.log.debug("sendFloodingMessage()") if len(key) != 16 or len(iv)!=16: key = self.key iv = self.iv lasthop = self.hostname else: self.log.debug("sendFloodingMessage, last hop key = old keys") lasthop = 'old_keys' #self.key_material[lasthop] = (key, iv) oldKeyNeighbor = Neighbor() oldKeyNeighbor.encryptionKey = key oldKeyNeighbor.encryptionIV = iv oldKeyNeighbor.pubKey = self.pubKey self.globalNeighborList[lasthop] = oldKeyNeighbor self.log.debug("SendFlooding to: nodeID: %s, msg_type=%s" %(nodeID, msg_type)) #Apply asymmetric encryption for unicast messages if nodeID != "255.255.255.255" and msg_type=="ctrl": #Apply enrcyption with public key of receiver if nodeID in self.globalNeighborList: self.log.debug('Additional public key encryption for unicast CTRL message to %s applied.' % (nodeID)) oaep = PKCS1_OAEP.new(self.globalNeighborList[nodeID].pubKey) asssymEncMessage = oaep.encrypt(message) message = asssymEncMessage else: self.log.error('Unicast Message to neighbor %s cannot be sent, neighbor is not available in neighbor list' % (nodeID)) self.sendingUserSpaceMessageEvent.set() return elif nodeID != "255.255.255.255" and msg_type=="data": if nodeID in self.globalNeighborList: self.log.debug('Additional symmetric key encryption for unicast STD message to %s applied.' % (nodeID)) aes = AES.new(self.globalNeighborList[nodeID].unicastSendKey, AES.MODE_CFB, self.globalNeighborList[nodeID].unicastSendIv) enc_message = aes.encrypt(message) # to base64 enc_message_b64 = base64.encodestring(enc_message) message = enc_message_b64 else: self.log.error('Unicast Message to neighbor %s cannot be sent, neighbor is not available in neighbor list' % (nodeID)) self.sendingUserSpaceMessageEvent.set() return #create signature: sig = self.security_helper.create_signature(message, self.rsaKey) # encrypt message aes = AES.new(key, AES.MODE_CFB, iv) enc_message = aes.encrypt(message) # to base64 enc_message_b64 = base64.encodestring(enc_message) if msg_type != "ctrl": self.log.debug('Source message: plain=%s, encrypted_B64=%s, rec:=%s' % (message, enc_message_b64, nodeID)) json_data = { 'host': self.hostname, 'lasthop': lasthop, 'id': self.local_id, 'rec': nodeID, 'ttl': max_ttl, 'msg_type' : msg_type, 'sub_type' : sub_type, 'msg': enc_message_b64, 'sig': sig } self.snd_socket.send_json(json_data) self.local_id = self.local_id + 1