2019-08-17 09:54:06 +08:00
|
|
|
from dataclasses import dataclass
|
2019-09-04 04:28:31 +08:00
|
|
|
import itertools
|
2019-08-25 04:52:09 +08:00
|
|
|
from typing import Optional, Tuple
|
2019-08-17 09:54:06 +08:00
|
|
|
|
2019-10-17 15:34:11 +08:00
|
|
|
import multihash
|
2019-08-23 22:55:49 +08:00
|
|
|
|
|
|
|
from libp2p.crypto.authenticated_encryption import (
|
|
|
|
EncryptionParameters as AuthenticatedEncryptionParameters,
|
|
|
|
)
|
|
|
|
from libp2p.crypto.authenticated_encryption import (
|
|
|
|
initialize_pair as initialize_pair_for_encryption,
|
|
|
|
)
|
2019-10-18 15:44:07 +08:00
|
|
|
from libp2p.crypto.authenticated_encryption import InvalidMACException
|
2019-08-23 22:55:49 +08:00
|
|
|
from libp2p.crypto.authenticated_encryption import MacAndCipher as Encrypter
|
|
|
|
from libp2p.crypto.ecc import ECCPublicKey
|
2019-10-17 15:19:39 +08:00
|
|
|
from libp2p.crypto.exceptions import MissingDeserializerError
|
2019-08-23 22:55:49 +08:00
|
|
|
from libp2p.crypto.key_exchange import create_ephemeral_key_pair
|
2019-08-22 23:55:36 +08:00
|
|
|
from libp2p.crypto.keys import PrivateKey, PublicKey
|
2019-08-24 04:12:13 +08:00
|
|
|
from libp2p.crypto.serialization import deserialize_public_key
|
2020-02-17 23:33:45 +08:00
|
|
|
from libp2p.io.abc import EncryptedMsgReadWriter
|
2019-10-17 14:30:30 +08:00
|
|
|
from libp2p.io.exceptions import DecryptionFailedException, IOException
|
2020-02-20 21:48:03 +08:00
|
|
|
from libp2p.io.msgio import FixedSizeLenMsgReadWriter
|
2019-08-17 09:54:06 +08:00
|
|
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
|
|
|
from libp2p.peer.id import ID as PeerID
|
|
|
|
from libp2p.security.base_transport import BaseSecureTransport
|
|
|
|
from libp2p.security.secure_conn_interface import ISecureConn
|
2020-02-17 23:33:45 +08:00
|
|
|
from libp2p.security.secure_session import SecureSession
|
2019-11-21 11:47:54 +08:00
|
|
|
from libp2p.typing import TProtocol
|
2019-08-17 09:54:06 +08:00
|
|
|
|
2019-08-23 22:55:49 +08:00
|
|
|
from .exceptions import (
|
2019-08-24 05:43:36 +08:00
|
|
|
IncompatibleChoices,
|
2019-09-16 19:15:40 +08:00
|
|
|
InconsistentNonce,
|
2019-08-23 22:55:49 +08:00
|
|
|
InvalidSignatureOnExchange,
|
|
|
|
PeerMismatchException,
|
|
|
|
SecioException,
|
2019-10-17 15:19:39 +08:00
|
|
|
SedesException,
|
2019-08-24 05:02:53 +08:00
|
|
|
SelfEncryption,
|
2019-08-23 22:55:49 +08:00
|
|
|
)
|
2019-08-22 23:55:36 +08:00
|
|
|
from .pb.spipe_pb2 import Exchange, Propose
|
|
|
|
|
2019-11-21 11:47:54 +08:00
|
|
|
ID = TProtocol("/secio/1.0.0")
|
2019-08-17 09:54:06 +08:00
|
|
|
|
2019-08-22 23:55:36 +08:00
|
|
|
NONCE_SIZE = 16 # bytes
|
2020-02-17 19:02:18 +08:00
|
|
|
SIZE_SECIO_LEN_BYTES = 4
|
2019-08-17 09:54:06 +08:00
|
|
|
|
2019-08-22 23:55:36 +08:00
|
|
|
# NOTE: the following is only a subset of allowable parameters according to the
|
|
|
|
# `secio` specification.
|
|
|
|
DEFAULT_SUPPORTED_EXCHANGES = "P-256"
|
|
|
|
DEFAULT_SUPPORTED_CIPHERS = "AES-128"
|
|
|
|
DEFAULT_SUPPORTED_HASHES = "SHA256"
|
2019-08-17 09:54:06 +08:00
|
|
|
|
|
|
|
|
2020-02-20 21:48:03 +08:00
|
|
|
class SecioPacketReadWriter(FixedSizeLenMsgReadWriter):
|
2020-02-17 19:02:18 +08:00
|
|
|
size_len_bytes = SIZE_SECIO_LEN_BYTES
|
|
|
|
|
|
|
|
|
2020-02-17 23:33:45 +08:00
|
|
|
class SecioMsgReadWriter(EncryptedMsgReadWriter):
|
2020-02-20 21:48:03 +08:00
|
|
|
read_writer: SecioPacketReadWriter
|
2019-09-04 13:08:09 +08:00
|
|
|
|
2019-08-24 05:43:36 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
local_encryption_parameters: AuthenticatedEncryptionParameters,
|
|
|
|
remote_encryption_parameters: AuthenticatedEncryptionParameters,
|
2020-02-20 21:48:03 +08:00
|
|
|
read_writer: SecioPacketReadWriter,
|
2019-08-24 05:43:36 +08:00
|
|
|
) -> None:
|
|
|
|
self.local_encryption_parameters = local_encryption_parameters
|
|
|
|
self.remote_encryption_parameters = remote_encryption_parameters
|
2019-08-23 22:55:49 +08:00
|
|
|
self._initialize_authenticated_encryption_for_local_peer()
|
|
|
|
self._initialize_authenticated_encryption_for_remote_peer()
|
2020-02-17 23:33:45 +08:00
|
|
|
self.read_writer = read_writer
|
2019-09-04 13:08:09 +08:00
|
|
|
|
2019-08-23 22:55:49 +08:00
|
|
|
def _initialize_authenticated_encryption_for_local_peer(self) -> None:
|
|
|
|
self.local_encrypter = Encrypter(self.local_encryption_parameters)
|
|
|
|
|
|
|
|
def _initialize_authenticated_encryption_for_remote_peer(self) -> None:
|
|
|
|
self.remote_encrypter = Encrypter(self.remote_encryption_parameters)
|
|
|
|
|
2020-02-17 23:33:45 +08:00
|
|
|
def encrypt(self, data: bytes) -> bytes:
|
|
|
|
encrypted_data = self.local_encrypter.encrypt(data)
|
|
|
|
tag = self.local_encrypter.authenticate(encrypted_data)
|
|
|
|
return encrypted_data + tag
|
2019-09-04 13:08:09 +08:00
|
|
|
|
2020-02-17 23:33:45 +08:00
|
|
|
def decrypt(self, data: bytes) -> bytes:
|
2019-10-17 14:30:30 +08:00
|
|
|
try:
|
2020-02-17 23:33:45 +08:00
|
|
|
decrypted_data = self.remote_encrypter.decrypt_if_valid(data)
|
2019-12-20 00:31:18 +08:00
|
|
|
except InvalidMACException as e:
|
|
|
|
raise DecryptionFailedException() from e
|
2020-02-17 23:33:45 +08:00
|
|
|
return decrypted_data
|
2019-08-23 22:55:49 +08:00
|
|
|
|
2019-09-04 13:08:09 +08:00
|
|
|
async def write_msg(self, msg: bytes) -> None:
|
2020-02-17 23:33:45 +08:00
|
|
|
data_encrypted = self.encrypt(msg)
|
|
|
|
await self.read_writer.write_msg(data_encrypted)
|
|
|
|
|
|
|
|
async def read_msg(self) -> bytes:
|
|
|
|
msg_encrypted = await self.read_writer.read_msg()
|
|
|
|
return self.decrypt(msg_encrypted)
|
|
|
|
|
|
|
|
async def close(self) -> None:
|
|
|
|
await self.read_writer.close()
|
2019-08-17 09:54:06 +08:00
|
|
|
|
|
|
|
|
2019-08-22 23:55:36 +08:00
|
|
|
@dataclass(frozen=True)
|
|
|
|
class Proposal:
|
2019-10-24 14:41:10 +08:00
|
|
|
"""A ``Proposal`` represents the set of session parameters one peer in a
|
|
|
|
pair of peers attempting to negotiate a `secio` channel prefers."""
|
2019-08-22 23:55:36 +08:00
|
|
|
|
|
|
|
nonce: bytes
|
|
|
|
public_key: PublicKey
|
|
|
|
exchanges: str = DEFAULT_SUPPORTED_EXCHANGES # comma separated list
|
|
|
|
ciphers: str = DEFAULT_SUPPORTED_CIPHERS # comma separated list
|
|
|
|
hashes: str = DEFAULT_SUPPORTED_HASHES # comma separated list
|
|
|
|
|
|
|
|
def serialize(self) -> bytes:
|
|
|
|
protobuf = Propose(
|
2019-08-25 01:57:56 +08:00
|
|
|
rand=self.nonce,
|
|
|
|
public_key=self.public_key.serialize(),
|
|
|
|
exchanges=self.exchanges,
|
|
|
|
ciphers=self.ciphers,
|
|
|
|
hashes=self.hashes,
|
2019-08-22 23:55:36 +08:00
|
|
|
)
|
|
|
|
return protobuf.SerializeToString()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def deserialize(cls, protobuf_bytes: bytes) -> "Proposal":
|
2019-08-25 01:57:56 +08:00
|
|
|
protobuf = Propose.FromString(protobuf_bytes)
|
2019-08-22 23:55:36 +08:00
|
|
|
|
|
|
|
nonce = protobuf.rand
|
|
|
|
public_key_protobuf_bytes = protobuf.public_key
|
2019-10-17 15:19:39 +08:00
|
|
|
try:
|
|
|
|
public_key = deserialize_public_key(public_key_protobuf_bytes)
|
|
|
|
except MissingDeserializerError as error:
|
2019-12-20 00:31:18 +08:00
|
|
|
raise SedesException() from error
|
2019-08-22 23:55:36 +08:00
|
|
|
exchanges = protobuf.exchanges
|
|
|
|
ciphers = protobuf.ciphers
|
|
|
|
hashes = protobuf.hashes
|
|
|
|
|
|
|
|
return cls(nonce, public_key, exchanges, ciphers, hashes)
|
|
|
|
|
|
|
|
def calculate_peer_id(self) -> PeerID:
|
|
|
|
return PeerID.from_pubkey(self.public_key)
|
2019-08-17 09:54:06 +08:00
|
|
|
|
|
|
|
|
2019-08-22 23:55:36 +08:00
|
|
|
@dataclass
|
|
|
|
class EncryptionParameters:
|
|
|
|
permanent_public_key: PublicKey
|
|
|
|
|
|
|
|
curve_type: str
|
|
|
|
cipher_type: str
|
|
|
|
hash_type: str
|
|
|
|
|
|
|
|
ephemeral_public_key: PublicKey
|
2019-08-23 22:55:49 +08:00
|
|
|
|
2019-08-24 05:43:36 +08:00
|
|
|
def __init__(self) -> None:
|
|
|
|
pass
|
|
|
|
|
2019-08-23 22:55:49 +08:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class SessionParameters:
|
|
|
|
local_peer: PeerID
|
|
|
|
local_encryption_parameters: EncryptionParameters
|
|
|
|
|
|
|
|
remote_peer: PeerID
|
|
|
|
remote_encryption_parameters: EncryptionParameters
|
|
|
|
|
|
|
|
# order is a comparator used to break the symmetry b/t each pair of peers
|
|
|
|
order: int
|
|
|
|
shared_key: bytes
|
2019-08-17 09:54:06 +08:00
|
|
|
|
2019-08-24 05:43:36 +08:00
|
|
|
def __init__(self) -> None:
|
|
|
|
pass
|
|
|
|
|
2019-08-17 09:54:06 +08:00
|
|
|
|
2020-02-20 21:48:03 +08:00
|
|
|
async def _response_to_msg(read_writer: SecioPacketReadWriter, msg: bytes) -> bytes:
|
2019-09-04 13:08:09 +08:00
|
|
|
await read_writer.write_msg(msg)
|
|
|
|
return await read_writer.read_msg()
|
2019-08-17 09:54:06 +08:00
|
|
|
|
|
|
|
|
2019-08-22 23:55:36 +08:00
|
|
|
def _mk_multihash_sha256(data: bytes) -> bytes:
|
2019-08-25 01:57:56 +08:00
|
|
|
return multihash.digest(data, "sha2-256")
|
2019-08-22 23:55:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _mk_score(public_key: PublicKey, nonce: bytes) -> bytes:
|
|
|
|
return _mk_multihash_sha256(public_key.serialize() + nonce)
|
|
|
|
|
|
|
|
|
|
|
|
def _select_parameter_from_order(
|
|
|
|
order: int, supported_parameters: str, available_parameters: str
|
|
|
|
) -> str:
|
|
|
|
if order < 0:
|
|
|
|
first_choices = available_parameters.split(",")
|
|
|
|
second_choices = supported_parameters.split(",")
|
|
|
|
elif order > 0:
|
|
|
|
first_choices = supported_parameters.split(",")
|
|
|
|
second_choices = available_parameters.split(",")
|
|
|
|
else:
|
|
|
|
return supported_parameters.split(",")[0]
|
|
|
|
|
2019-09-04 04:28:31 +08:00
|
|
|
for first, second in itertools.product(first_choices, second_choices):
|
2019-08-22 23:55:36 +08:00
|
|
|
if first == second:
|
|
|
|
return first
|
2019-08-24 05:43:36 +08:00
|
|
|
raise IncompatibleChoices()
|
2019-08-22 23:55:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _select_encryption_parameters(
|
|
|
|
local_proposal: Proposal, remote_proposal: Proposal
|
2019-08-23 22:55:49 +08:00
|
|
|
) -> Tuple[str, str, str, int]:
|
2019-08-22 23:55:36 +08:00
|
|
|
first_score = _mk_score(remote_proposal.public_key, local_proposal.nonce)
|
|
|
|
second_score = _mk_score(local_proposal.public_key, remote_proposal.nonce)
|
|
|
|
|
|
|
|
order = 0
|
|
|
|
if first_score < second_score:
|
|
|
|
order = -1
|
|
|
|
elif second_score < first_score:
|
|
|
|
order = 1
|
|
|
|
|
2019-08-24 05:02:53 +08:00
|
|
|
if order == 0:
|
|
|
|
raise SelfEncryption()
|
2019-08-22 23:55:36 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
_select_parameter_from_order(
|
|
|
|
order, DEFAULT_SUPPORTED_EXCHANGES, remote_proposal.exchanges
|
|
|
|
),
|
2019-08-23 22:55:49 +08:00
|
|
|
_select_parameter_from_order(
|
2019-08-22 23:55:36 +08:00
|
|
|
order, DEFAULT_SUPPORTED_CIPHERS, remote_proposal.ciphers
|
|
|
|
),
|
2019-08-23 22:55:49 +08:00
|
|
|
_select_parameter_from_order(
|
2019-08-22 23:55:36 +08:00
|
|
|
order, DEFAULT_SUPPORTED_HASHES, remote_proposal.hashes
|
|
|
|
),
|
2019-08-23 22:55:49 +08:00
|
|
|
order,
|
2019-08-22 23:55:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def _establish_session_parameters(
|
|
|
|
local_peer: PeerID,
|
|
|
|
local_private_key: PrivateKey,
|
|
|
|
remote_peer: Optional[PeerID],
|
2020-02-20 21:48:03 +08:00
|
|
|
conn: SecioPacketReadWriter,
|
2019-08-22 23:55:36 +08:00
|
|
|
nonce: bytes,
|
2019-08-23 22:55:49 +08:00
|
|
|
) -> Tuple[SessionParameters, bytes]:
|
|
|
|
# establish shared encryption parameters
|
2019-08-22 23:55:36 +08:00
|
|
|
session_parameters = SessionParameters()
|
|
|
|
session_parameters.local_peer = local_peer
|
|
|
|
|
|
|
|
local_encryption_parameters = EncryptionParameters()
|
|
|
|
session_parameters.local_encryption_parameters = local_encryption_parameters
|
|
|
|
|
|
|
|
local_public_key = local_private_key.get_public_key()
|
|
|
|
local_encryption_parameters.permanent_public_key = local_public_key
|
|
|
|
|
|
|
|
local_proposal = Proposal(nonce, local_public_key)
|
|
|
|
serialized_local_proposal = local_proposal.serialize()
|
|
|
|
serialized_remote_proposal = await _response_to_msg(conn, serialized_local_proposal)
|
|
|
|
|
|
|
|
remote_encryption_parameters = EncryptionParameters()
|
|
|
|
session_parameters.remote_encryption_parameters = remote_encryption_parameters
|
|
|
|
remote_proposal = Proposal.deserialize(serialized_remote_proposal)
|
|
|
|
remote_encryption_parameters.permanent_public_key = remote_proposal.public_key
|
|
|
|
|
|
|
|
remote_peer_from_proposal = remote_proposal.calculate_peer_id()
|
|
|
|
if not remote_peer:
|
|
|
|
remote_peer = remote_peer_from_proposal
|
|
|
|
elif remote_peer != remote_peer_from_proposal:
|
2019-09-04 04:28:18 +08:00
|
|
|
raise PeerMismatchException(
|
|
|
|
{
|
|
|
|
"expected_remote_peer": remote_peer,
|
|
|
|
"received_remote_peer": remote_peer_from_proposal,
|
|
|
|
}
|
|
|
|
)
|
2019-08-22 23:55:36 +08:00
|
|
|
session_parameters.remote_peer = remote_peer
|
|
|
|
|
2019-08-23 22:55:49 +08:00
|
|
|
curve_param, cipher_param, hash_param, order = _select_encryption_parameters(
|
2019-08-22 23:55:36 +08:00
|
|
|
local_proposal, remote_proposal
|
|
|
|
)
|
|
|
|
local_encryption_parameters.curve_type = curve_param
|
|
|
|
local_encryption_parameters.cipher_type = cipher_param
|
|
|
|
local_encryption_parameters.hash_type = hash_param
|
|
|
|
remote_encryption_parameters.curve_type = curve_param
|
|
|
|
remote_encryption_parameters.cipher_type = cipher_param
|
|
|
|
remote_encryption_parameters.hash_type = hash_param
|
2019-08-23 22:55:49 +08:00
|
|
|
session_parameters.order = order
|
2019-08-17 09:54:06 +08:00
|
|
|
|
|
|
|
# exchange ephemeral pub keys
|
2019-08-23 22:55:49 +08:00
|
|
|
local_ephemeral_public_key, shared_key_generator = create_ephemeral_key_pair(
|
|
|
|
curve_param
|
2019-08-17 09:54:06 +08:00
|
|
|
)
|
2019-08-23 22:55:49 +08:00
|
|
|
local_encryption_parameters.ephemeral_public_key = local_ephemeral_public_key
|
|
|
|
local_selection = (
|
|
|
|
serialized_local_proposal
|
|
|
|
+ serialized_remote_proposal
|
|
|
|
+ local_ephemeral_public_key.to_bytes()
|
2019-08-17 09:54:06 +08:00
|
|
|
)
|
2019-08-23 22:55:49 +08:00
|
|
|
exchange_signature = local_private_key.sign(local_selection)
|
|
|
|
local_exchange = Exchange(
|
|
|
|
ephemeral_public_key=local_ephemeral_public_key.to_bytes(),
|
|
|
|
signature=exchange_signature,
|
2019-08-17 09:54:06 +08:00
|
|
|
)
|
|
|
|
|
2019-08-23 22:55:49 +08:00
|
|
|
serialized_local_exchange = local_exchange.SerializeToString()
|
|
|
|
serialized_remote_exchange = await _response_to_msg(conn, serialized_local_exchange)
|
2019-08-17 09:54:06 +08:00
|
|
|
|
2019-08-23 22:55:49 +08:00
|
|
|
remote_exchange = Exchange()
|
|
|
|
remote_exchange.ParseFromString(serialized_remote_exchange)
|
|
|
|
|
|
|
|
remote_ephemeral_public_key_bytes = remote_exchange.ephemeral_public_key
|
|
|
|
remote_ephemeral_public_key = ECCPublicKey.from_bytes(
|
2019-09-04 04:21:07 +08:00
|
|
|
remote_ephemeral_public_key_bytes, curve_param
|
2019-08-23 22:55:49 +08:00
|
|
|
)
|
|
|
|
remote_encryption_parameters.ephemeral_public_key = remote_ephemeral_public_key
|
|
|
|
remote_selection = (
|
|
|
|
serialized_remote_proposal
|
|
|
|
+ serialized_local_proposal
|
|
|
|
+ remote_ephemeral_public_key_bytes
|
|
|
|
)
|
|
|
|
valid_signature = remote_encryption_parameters.permanent_public_key.verify(
|
|
|
|
remote_selection, remote_exchange.signature
|
2019-08-17 09:54:06 +08:00
|
|
|
)
|
2019-08-23 22:55:49 +08:00
|
|
|
if not valid_signature:
|
|
|
|
raise InvalidSignatureOnExchange()
|
|
|
|
|
|
|
|
shared_key = shared_key_generator(remote_ephemeral_public_key_bytes)
|
|
|
|
session_parameters.shared_key = shared_key
|
|
|
|
|
|
|
|
return session_parameters, remote_proposal.nonce
|
2019-08-17 09:54:06 +08:00
|
|
|
|
|
|
|
|
2019-08-23 22:55:49 +08:00
|
|
|
def _mk_session_from(
|
2019-08-24 05:43:36 +08:00
|
|
|
local_private_key: PrivateKey,
|
|
|
|
session_parameters: SessionParameters,
|
2020-02-20 21:48:03 +08:00
|
|
|
conn: SecioPacketReadWriter,
|
2019-10-25 01:22:24 +08:00
|
|
|
is_initiator: bool,
|
2019-08-23 22:55:49 +08:00
|
|
|
) -> SecureSession:
|
|
|
|
key_set1, key_set2 = initialize_pair_for_encryption(
|
|
|
|
session_parameters.local_encryption_parameters.cipher_type,
|
|
|
|
session_parameters.local_encryption_parameters.hash_type,
|
|
|
|
session_parameters.shared_key,
|
|
|
|
)
|
2019-08-17 09:54:06 +08:00
|
|
|
|
2019-08-23 22:55:49 +08:00
|
|
|
if session_parameters.order < 0:
|
|
|
|
key_set1, key_set2 = key_set2, key_set1
|
2020-02-17 23:33:45 +08:00
|
|
|
secio_read_writer = SecioMsgReadWriter(key_set1, key_set2, conn)
|
2020-02-18 22:43:29 +08:00
|
|
|
remote_permanent_pubkey = (
|
|
|
|
session_parameters.remote_encryption_parameters.permanent_public_key
|
|
|
|
)
|
2019-08-23 22:55:49 +08:00
|
|
|
session = SecureSession(
|
2020-02-18 22:43:29 +08:00
|
|
|
local_peer=session_parameters.local_peer,
|
|
|
|
local_private_key=local_private_key,
|
|
|
|
remote_peer=session_parameters.remote_peer,
|
|
|
|
remote_permanent_pubkey=remote_permanent_pubkey,
|
|
|
|
is_initiator=is_initiator,
|
|
|
|
conn=secio_read_writer,
|
2019-08-23 22:55:49 +08:00
|
|
|
)
|
|
|
|
return session
|
2019-08-17 09:54:06 +08:00
|
|
|
|
2019-08-23 22:55:49 +08:00
|
|
|
|
2019-09-04 13:08:09 +08:00
|
|
|
async def _finish_handshake(session: SecureSession, remote_nonce: bytes) -> bytes:
|
2020-02-17 23:33:45 +08:00
|
|
|
await session.conn.write_msg(remote_nonce)
|
|
|
|
return await session.conn.read_msg()
|
2019-08-17 09:54:06 +08:00
|
|
|
|
|
|
|
|
2019-08-22 23:55:36 +08:00
|
|
|
async def create_secure_session(
|
2019-08-24 17:43:27 +08:00
|
|
|
local_nonce: bytes,
|
|
|
|
local_peer: PeerID,
|
|
|
|
local_private_key: PrivateKey,
|
|
|
|
conn: IRawConnection,
|
|
|
|
remote_peer: PeerID = None,
|
2019-08-22 23:55:36 +08:00
|
|
|
) -> ISecureConn:
|
2019-08-17 09:54:06 +08:00
|
|
|
"""
|
2019-08-22 23:55:36 +08:00
|
|
|
Attempt the initial `secio` handshake with the remote peer.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
|
|
|
If successful, return an object that provides secure communication
|
|
|
|
to the ``remote_peer``. Raise `SecioException` when `conn` closed.
|
2019-09-19 22:19:36 +08:00
|
|
|
Raise `InconsistentNonce` when handshake failed
|
2019-08-17 09:54:06 +08:00
|
|
|
"""
|
2020-02-20 21:48:03 +08:00
|
|
|
msg_io = SecioPacketReadWriter(conn)
|
2019-08-22 23:55:36 +08:00
|
|
|
try:
|
2019-08-23 22:55:49 +08:00
|
|
|
session_parameters, remote_nonce = await _establish_session_parameters(
|
2019-09-04 13:08:09 +08:00
|
|
|
local_peer, local_private_key, remote_peer, msg_io, local_nonce
|
2019-08-22 23:55:36 +08:00
|
|
|
)
|
2019-08-23 22:55:49 +08:00
|
|
|
except SecioException as e:
|
2019-08-27 01:03:12 +08:00
|
|
|
await conn.close()
|
2019-08-22 23:55:36 +08:00
|
|
|
raise e
|
2019-09-16 19:15:40 +08:00
|
|
|
# `IOException` includes errors raised while read from/write to raw connection
|
2019-12-20 00:31:18 +08:00
|
|
|
except IOException as e:
|
|
|
|
raise SecioException("connection closed") from e
|
2019-08-17 09:54:06 +08:00
|
|
|
|
2019-10-25 01:22:24 +08:00
|
|
|
is_initiator = remote_peer is not None
|
2019-10-25 01:52:03 +08:00
|
|
|
session = _mk_session_from(
|
|
|
|
local_private_key, session_parameters, msg_io, is_initiator
|
|
|
|
)
|
2019-08-17 09:54:06 +08:00
|
|
|
|
2019-09-16 19:15:40 +08:00
|
|
|
try:
|
|
|
|
received_nonce = await _finish_handshake(session, remote_nonce)
|
|
|
|
# `IOException` includes errors raised while read from/write to raw connection
|
2019-12-20 00:31:18 +08:00
|
|
|
except IOException as e:
|
|
|
|
raise SecioException("connection closed") from e
|
2019-08-23 22:55:49 +08:00
|
|
|
if received_nonce != local_nonce:
|
2019-08-27 01:03:12 +08:00
|
|
|
await conn.close()
|
2019-09-16 19:15:40 +08:00
|
|
|
raise InconsistentNonce()
|
2019-08-17 09:54:06 +08:00
|
|
|
|
|
|
|
return session
|
|
|
|
|
|
|
|
|
2019-08-24 17:43:27 +08:00
|
|
|
class Transport(BaseSecureTransport):
|
2019-10-24 14:41:10 +08:00
|
|
|
"""``Transport`` provides a security upgrader for a ``IRawConnection``,
|
|
|
|
following the `secio` protocol defined in the libp2p specs."""
|
2019-08-17 09:54:06 +08:00
|
|
|
|
2019-08-22 23:55:36 +08:00
|
|
|
def get_nonce(self) -> bytes:
|
|
|
|
return self.secure_bytes_provider(NONCE_SIZE)
|
|
|
|
|
2019-08-17 09:54:06 +08:00
|
|
|
async def secure_inbound(self, conn: IRawConnection) -> ISecureConn:
|
|
|
|
"""
|
2019-10-25 02:10:45 +08:00
|
|
|
Secure the connection, either locally or by communicating with opposing
|
|
|
|
node via conn, for an inbound connection (i.e. we are not the
|
2019-10-24 14:41:10 +08:00
|
|
|
initiator)
|
|
|
|
|
2019-08-17 09:54:06 +08:00
|
|
|
:return: secure connection object (that implements secure_conn_interface)
|
|
|
|
"""
|
2019-08-24 17:43:27 +08:00
|
|
|
local_nonce = self.get_nonce()
|
|
|
|
local_peer = self.local_peer
|
|
|
|
local_private_key = self.local_private_key
|
|
|
|
|
|
|
|
return await create_secure_session(
|
|
|
|
local_nonce, local_peer, local_private_key, conn
|
|
|
|
)
|
2019-08-17 09:54:06 +08:00
|
|
|
|
|
|
|
async def secure_outbound(
|
|
|
|
self, conn: IRawConnection, peer_id: PeerID
|
|
|
|
) -> ISecureConn:
|
|
|
|
"""
|
2019-10-25 02:10:45 +08:00
|
|
|
Secure the connection, either locally or by communicating with opposing
|
|
|
|
node via conn, for an inbound connection (i.e. we are the initiator)
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-08-17 09:54:06 +08:00
|
|
|
:return: secure connection object (that implements secure_conn_interface)
|
|
|
|
"""
|
2019-08-24 17:43:27 +08:00
|
|
|
local_nonce = self.get_nonce()
|
|
|
|
local_peer = self.local_peer
|
|
|
|
local_private_key = self.local_private_key
|
|
|
|
|
|
|
|
return await create_secure_session(
|
|
|
|
local_nonce, local_peer, local_private_key, conn, peer_id
|
|
|
|
)
|