class CryptoPeerListener(PeerListener): def __init__(self, ip, port, pubkey, secret, ctx, guid, data_cb): super(CryptoPeerListener, self).__init__(ip, port, ctx, guid, data_cb) self.pubkey = pubkey self.secret = secret # FIXME: refactor this mess # this was copied as is from CryptoTransportLayer # soon all crypto code will be refactored and this will be removed self.cryptor = Cryptor(pubkey_hex=self.pubkey, privkey_hex=self.secret) @staticmethod def is_handshake(message): """ Return whether message is a plaintext handshake :param message: serialized JSON :return: True if proper handshake message """ try: message = json.loads(message) except (ValueError, TypeError): return False return 'type' in message def _on_raw_message(self, serialized): """ Handles receipt of encrypted/plaintext message and passes to appropriate callback. :param serialized: :return: """ if not self.is_handshake(serialized): try: message = self.cryptor.decrypt(serialized) message = json.loads(message) signature = message['sig'].decode('hex') signed_data = message['data'] if CryptoPeerListener.validate_signature( signature, signed_data): message = signed_data.decode('hex') message = json.loads(message) if message.get('guid') != self.guid: return else: return except RuntimeError as e: self.log.error('Could not decrypt message properly %s', e) return except Exception as e: self.log.error('Cannot unpack data: %s', e) return else: message = json.loads(serialized) self.log.debugv('Received message of type "%s"', message.get('type', 'unknown')) self._data_cb(message) @staticmethod def validate_signature(signature, data): data_json = json.loads(data.decode('hex')) sig_cryptor = Cryptor(pubkey_hex=data_json['pubkey']) if sig_cryptor.verify(signature, data): return True else: return False
class CryptoPeerListener(PeerListener): def __init__(self, ip, port, pubkey, secret, ctx, guid, data_cb): super(CryptoPeerListener, self).__init__(ip, port, ctx, guid, data_cb) self.pubkey = pubkey self.secret = secret # FIXME: refactor this mess # this was copied as is from CryptoTransportLayer # soon all crypto code will be refactored and this will be removed self.cryptor = Cryptor(pubkey_hex=self.pubkey, privkey_hex=self.secret) @staticmethod def is_handshake(message): """ Return whether message is a plaintext handshake :param message: serialized JSON :return: True if proper handshake message """ try: message = json.loads(message) except (ValueError, TypeError): return False return 'type' in message def _on_raw_message(self, serialized): """ Handles receipt of encrypted/plaintext message and passes to appropriate callback. :param serialized: :return: """ if not self.is_handshake(serialized): try: message = self.cryptor.decrypt(serialized) message = json.loads(message) signature = message['sig'].decode('hex') signed_data = message['data'] if CryptoPeerListener.validate_signature(signature, signed_data): message = signed_data.decode('hex') message = json.loads(message) if message.get('guid') != self.guid: return else: return except RuntimeError as e: self.log.error('Could not decrypt message properly %s', e) return except Exception as e: self.log.error('Cannot unpack data: %s', e) return else: message = json.loads(serialized) self.log.info('Message [%s]', message.get('type')) self._data_cb(message) @staticmethod def validate_signature(signature, data): data_json = json.loads(data.decode('hex')) sig_cryptor = Cryptor(pubkey_hex=data_json['pubkey']) if sig_cryptor.verify(signature, data): return True else: return False