From 49bd460e3787f6dcf75ad6a54db67dd9485efe52 Mon Sep 17 00:00:00 2001 From: NIC619 Date: Thu, 17 Oct 2019 15:19:39 +0800 Subject: [PATCH] Catch `SedesException` in `deserialize_public_key` --- libp2p/security/insecure/transport.py | 3 +++ libp2p/security/secio/transport.py | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/libp2p/security/insecure/transport.py b/libp2p/security/insecure/transport.py index 81e70475..5f6ea919 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/transport.py b/libp2p/security/secio/transport.py index 79504463..f3e3fde9 100644 --- a/libp2p/security/secio/transport.py +++ b/libp2p/security/secio/transport.py @@ -14,6 +14,7 @@ from libp2p.crypto.authenticated_encryption import ( ) 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 @@ -31,6 +32,7 @@ from .exceptions import ( InvalidSignatureOnExchange, PeerMismatchException, SecioException, + SedesException, SelfEncryption, ) from .pb.spipe_pb2 import Exchange, Propose @@ -168,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