From 29bf623d2cb030ca19b7408fc345a4f9d336a68d Mon Sep 17 00:00:00 2001 From: NIC619 Date: Thu, 17 Oct 2019 14:30:30 +0800 Subject: [PATCH] Raise `DecryptionFailedException` when failed to decrypt read msg --- libp2p/security/secio/transport.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libp2p/security/secio/transport.py b/libp2p/security/secio/transport.py index e1aa022..7950446 100644 --- a/libp2p/security/secio/transport.py +++ b/libp2p/security/secio/transport.py @@ -4,6 +4,7 @@ import itertools from typing import Optional, Tuple import multihash +from crypto.authenticated_encryption import InvalidMACException from libp2p.crypto.authenticated_encryption import ( EncryptionParameters as AuthenticatedEncryptionParameters, @@ -16,7 +17,7 @@ from libp2p.crypto.ecc import ECCPublicKey from libp2p.crypto.key_exchange import create_ephemeral_key_pair from libp2p.crypto.keys import PrivateKey, PublicKey from libp2p.crypto.serialization import deserialize_public_key -from libp2p.io.exceptions import IOException +from libp2p.io.exceptions import DecryptionFailedException, IOException from libp2p.io.msgio import MsgIOReadWriter from libp2p.network.connection.raw_connection_interface import IRawConnection from libp2p.peer.id import ID as PeerID @@ -122,7 +123,11 @@ class SecureSession(BaseSession): async def read_msg(self) -> bytes: msg = await self.conn.read_msg() - return self.remote_encrypter.decrypt_if_valid(msg) + try: + decrypted_msg = self.remote_encrypter.decrypt_if_valid(msg) + except InvalidMACException: + raise DecryptionFailedException + return decrypted_msg async def write(self, data: bytes) -> int: await self.write_msg(data)