diff --git a/libp2p/io/exceptions.py b/libp2p/io/exceptions.py index b8e4e01..d4e1dfa 100644 --- a/libp2p/io/exceptions.py +++ b/libp2p/io/exceptions.py @@ -21,3 +21,7 @@ class MissingLengthException(MsgioException): class MissingMessageException(MsgioException): pass + + +class DecryptionFailedException(MsgioException): + pass diff --git a/libp2p/peer/peerstore.py b/libp2p/peer/peerstore.py index c1eae37..a416c47 100644 --- a/libp2p/peer/peerstore.py +++ b/libp2p/peer/peerstore.py @@ -3,7 +3,7 @@ from typing import Any, Dict, List, Optional, Sequence from multiaddr import Multiaddr from .id import ID -from .peerdata import PeerData +from .peerdata import PeerData, PeerDataError from .peerinfo import PeerInfo from .peerstore_interface import IPeerStore @@ -54,7 +54,10 @@ class PeerStore(IPeerStore): def get(self, peer_id: ID, key: str) -> Any: if peer_id in self.peer_map: - val = self.peer_map[peer_id].get_metadata(key) + try: + val = self.peer_map[peer_id].get_metadata(key) + except PeerDataError as error: + raise PeerStoreError(error) return val raise PeerStoreError("peer ID not found") diff --git a/libp2p/security/insecure/transport.py b/libp2p/security/insecure/transport.py index 81e7047..5f6ea91 100644 --- a/libp2p/security/insecure/transport.py +++ b/libp2p/security/insecure/transport.py @@ -1,5 +1,6 @@ from typing import Optional +from libp2p.crypto.exceptions import MissingDeserializerError from libp2p.crypto.keys import PrivateKey, PublicKey from libp2p.crypto.pb import crypto_pb2 from libp2p.crypto.serialization import deserialize_public_key @@ -82,6 +83,8 @@ class InsecureSession(BaseSession): raise HandshakeFailure( f"unknown `key_type` of remote_msg.pubkey={remote_msg.pubkey}" ) + except MissingDeserializerError as error: + raise HandshakeFailure(error) peer_id_from_received_pubkey = ID.from_pubkey(received_pubkey) if peer_id_from_received_pubkey != received_peer_id: raise HandshakeFailure( diff --git a/libp2p/security/secio/exceptions.py b/libp2p/security/secio/exceptions.py index c03fda4..d86ce3b 100644 --- a/libp2p/security/secio/exceptions.py +++ b/libp2p/security/secio/exceptions.py @@ -28,3 +28,7 @@ class IncompatibleChoices(SecioException): class InconsistentNonce(SecioException): pass + + +class SedesException(SecioException): + pass diff --git a/libp2p/security/secio/transport.py b/libp2p/security/secio/transport.py index e1aa022..bd86251 100644 --- a/libp2p/security/secio/transport.py +++ b/libp2p/security/secio/transport.py @@ -11,12 +11,14 @@ from libp2p.crypto.authenticated_encryption import ( from libp2p.crypto.authenticated_encryption import ( initialize_pair as initialize_pair_for_encryption, ) +from libp2p.crypto.authenticated_encryption import InvalidMACException from libp2p.crypto.authenticated_encryption import MacAndCipher as Encrypter from libp2p.crypto.ecc import ECCPublicKey +from libp2p.crypto.exceptions import MissingDeserializerError 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 @@ -30,6 +32,7 @@ from .exceptions import ( InvalidSignatureOnExchange, PeerMismatchException, SecioException, + SedesException, SelfEncryption, ) from .pb.spipe_pb2 import Exchange, Propose @@ -122,7 +125,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) @@ -163,7 +170,10 @@ class Proposal: nonce = protobuf.rand public_key_protobuf_bytes = protobuf.public_key - public_key = deserialize_public_key(public_key_protobuf_bytes) + try: + public_key = deserialize_public_key(public_key_protobuf_bytes) + except MissingDeserializerError as error: + raise SedesException(error) exchanges = protobuf.exchanges ciphers = protobuf.ciphers hashes = protobuf.hashes diff --git a/libp2p/stream_muxer/mplex/mplex_stream.py b/libp2p/stream_muxer/mplex/mplex_stream.py index 7cc0564..8db4212 100644 --- a/libp2p/stream_muxer/mplex/mplex_stream.py +++ b/libp2p/stream_muxer/mplex/mplex_stream.py @@ -146,7 +146,7 @@ class MplexStream(IMuxedStream): :return: number of bytes written """ if self.event_local_closed.is_set(): - raise MplexStreamClosed(f"cannot write to closed stream: data={data}") + raise MplexStreamClosed(f"cannot write to closed stream: data={data!r}") flag = ( HeaderTags.MessageInitiator if self.is_initiator diff --git a/libp2p/utils.py b/libp2p/utils.py index 39c79e5..aeb7ae3 100644 --- a/libp2p/utils.py +++ b/libp2p/utils.py @@ -77,7 +77,7 @@ async def read_delim(reader: Reader) -> bytes: raise ParseError(f"`len(msg_bytes)` should not be 0") if msg_bytes[-1:] != b"\n": raise ParseError( - f'`msg_bytes` is not delimited by b"\\n": `msg_bytes`={msg_bytes}' + f'`msg_bytes` is not delimited by b"\\n": `msg_bytes`={msg_bytes!r}' ) return msg_bytes[:-1]