Handle IOException in create_secure_session

This commit is contained in:
NIC619 2019-09-16 19:15:40 +08:00
parent c7593bff97
commit 09bfa0ab09
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17

View File

@ -16,6 +16,7 @@ from libp2p.crypto.ecc import ECCPublicKey
from libp2p.crypto.key_exchange import create_ephemeral_key_pair from libp2p.crypto.key_exchange import create_ephemeral_key_pair
from libp2p.crypto.keys import PrivateKey, PublicKey from libp2p.crypto.keys import PrivateKey, PublicKey
from libp2p.crypto.serialization import deserialize_public_key from libp2p.crypto.serialization import deserialize_public_key
from libp2p.io.exceptions import IOException
from libp2p.io.msgio import MsgIOReadWriter from libp2p.io.msgio import MsgIOReadWriter
from libp2p.network.connection.raw_connection_interface import IRawConnection from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.peer.id import ID as PeerID from libp2p.peer.id import ID as PeerID
@ -24,8 +25,8 @@ from libp2p.security.base_transport import BaseSecureTransport
from libp2p.security.secure_conn_interface import ISecureConn from libp2p.security.secure_conn_interface import ISecureConn
from .exceptions import ( from .exceptions import (
HandshakeFailed,
IncompatibleChoices, IncompatibleChoices,
InconsistentNonce,
InvalidSignatureOnExchange, InvalidSignatureOnExchange,
PeerMismatchException, PeerMismatchException,
SecioException, SecioException,
@ -408,14 +409,21 @@ async def create_secure_session(
except SecioException as e: except SecioException as e:
await conn.close() await conn.close()
raise e raise e
# `IOException` includes errors raised while read from/write to raw connection
except IOException:
raise SecioException("connection closed")
initiator = remote_peer is not None initiator = remote_peer is not None
session = _mk_session_from(local_private_key, session_parameters, msg_io, initiator) session = _mk_session_from(local_private_key, session_parameters, msg_io, initiator)
received_nonce = await _finish_handshake(session, remote_nonce) try:
received_nonce = await _finish_handshake(session, remote_nonce)
# `IOException` includes errors raised while read from/write to raw connection
except IOException:
raise SecioException("connection closed")
if received_nonce != local_nonce: if received_nonce != local_nonce:
await conn.close() await conn.close()
raise HandshakeFailed() raise InconsistentNonce()
return session return session