Skip to content

Commit

Permalink
Removing M2Crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram van den Heuvel committed Jun 4, 2017
1 parent 5b0b9b4 commit 6cef7d0
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 659 deletions.
2 changes: 1 addition & 1 deletion authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def has_valid_signature_for(self, placeholder, payload):
return self._member.verify(payload, self._signature)

def _is_sig_empty(self):
return self._signature == "" or self._signature == "\x00" * self._member.signature_length
return not self._signature or self._signature == "\x00" * self._member.signature_length


def __init__(self, encoding="default"):
Expand Down
15 changes: 10 additions & 5 deletions community.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from twisted.internet.task import LoopingCall, deferLater
from twisted.python.threadable import isInIOThread

from crypto import DispersyPublicKey, DispersyCrypto
from .authentication import NoAuthentication, MemberAuthentication, DoubleMemberAuthentication
from .bloomfilter import BloomFilter
from .candidate import Candidate, WalkCandidate
Expand Down Expand Up @@ -481,13 +482,14 @@ def _download_master_member_identity(self):
self._logger.debug("using dummy master member")

try:
public_key, = self._dispersy.database.execute(u"SELECT public_key FROM member WHERE id = ?", (self._master_member.database_id,)).next()
public_key_binary, = self._dispersy.database.execute(u"SELECT public_key FROM member WHERE id = ?", (self._master_member.database_id,)).next()
public_key = DispersyPublicKey.from_bytes(public_key_binary)
except StopIteration:
pass
else:
if public_key:
self._logger.debug("%s found master member", self._cid.encode("HEX"))
self._master_member = self._dispersy.get_member(public_key=str(public_key))
self._master_member = self._dispersy.get_member(public_key=public_key)
assert self._master_member.public_key
self.cancel_pending_task("download master member identity")
else:
Expand Down Expand Up @@ -1841,9 +1843,12 @@ def get_member(self, *argv, **kwargs):
assert isinstance(mid, str)
assert isinstance(public_key, str)
assert isinstance(private_key, str)
assert not mid or len(mid) == 20
assert not public_key or self._dispersy.crypto.is_valid_public_bin(public_key)
assert not private_key or self._dispersy.crypto.is_valid_private_bin(private_key)
assert not mid or len(mid) == 32
assert not public_key or DispersyCrypto.is_valid_public_key(public_key)
assert not private_key or DispersyCrypto.is_valid_private_key(private_key)

public_key = DispersyPublicKey.from_bytes(public_key) if public_key else None
private_key = DispersyPublicKey.from_bytes(private_key) if private_key else None

member = self._dispersy.get_member(mid=mid, public_key=public_key, private_key=private_key)
# We only need to check if this member has an identity message in this community if we still don't have the full
Expand Down
48 changes: 26 additions & 22 deletions conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from M2Crypto.EC import ECError

from crypto import DispersyCrypto
from .authentication import Authentication, NoAuthentication, MemberAuthentication, DoubleMemberAuthentication
from .bloomfilter import BloomFilter
from .candidate import Candidate
Expand Down Expand Up @@ -54,7 +55,8 @@ def __init__(self, community, dispersy_version, community_version):
# the messages that this instance can handle, and that this instance produces, is identified
# by _prefix.
self._prefix = dispersy_version + community_version + community.cid
assert len(self._prefix) == 22 # when this assumption changes, we need to ensure the
# 34 is the hashlength, 32, plus 2.
assert len(self._prefix) == 34 # when this assumption changes, we need to ensure the
# dispersy_version and community_version properties are
# returned correctly

Expand Down Expand Up @@ -82,11 +84,12 @@ def can_decode_message(self, data):
"""
Returns True when DATA can be decoded using this conversion.
"""
# at least a length of 23, as we need the prefix + 1 byte messagetype
# at least a length of 35, as we need the prefix + 1 byte messagetype
assert isinstance(data, str), type(data)
assert len(data) >= 23
prefix_length = 34
assert len(data) > prefix_length

return (len(data) >= 23 and data[:22] == self._prefix)
return len(data) > prefix_length and data[:prefix_length] == self._prefix

@abstractmethod
def decode_meta_message(self, data):
Expand All @@ -100,7 +103,7 @@ def decode_meta_message(self, data):
def decode_message(self, address, data, verify=True, source=u"unknown"):
"""
DATA is a string, where the first byte is the on-the-wire Dispersy version, the second byte
is the on-the-wire Community version and the following 20 bytes is the Community Identifier.
is the on-the-wire Community version and the following 32 bytes is the Community Identifier.
The rest is the message payload.
Returns a Message instance.
Expand All @@ -118,7 +121,7 @@ def can_encode_message(self, message):
def encode_message(self, message, sign=True):
"""
Encode a Message instance into a binary string where the first byte is the on-the-wire
Dispersy version, the second byte is the on-the-wire Community version and the following 20
Dispersy version, the second byte is the on-the-wire Community version and the following 32
bytes is the Community Identifier. The rest is the message payload.
Returns a binary string.
Expand Down Expand Up @@ -281,8 +284,8 @@ def _decode_missing_sequence(self, placeholder, offset, data):
if len(data) < offset + 29:
raise DropPacket("Insufficient packet size")

member_id = data[offset:offset + 20]
offset += 20
member_id = data[offset:offset + 32]
offset += 32
member = self._community.get_member(mid=member_id)
if member is None:
raise DropPacket("Unknown member")
Expand Down Expand Up @@ -387,10 +390,10 @@ def _encode_missing_identity(self, message):
return (message.payload.mid,)

def _decode_missing_identity(self, placeholder, offset, data):
if len(data) < offset + 20:
if len(data) < offset + 32:
raise DropPacket("Insufficient packet size")

return offset + 20, placeholder.meta.payload.Implementation(placeholder.meta.payload, data[offset:offset + 20])
return offset + 32, placeholder.meta.payload.Implementation(placeholder.meta.payload, data[offset:offset + 32])

def _encode_destroy_community(self, message):
if message.payload.is_soft_kill:
Expand Down Expand Up @@ -906,7 +909,7 @@ def _encode_member_authentication(self, container, message):
container.append(message.authentication.member.mid)
elif encoding == "bin":
assert message.authentication.member.public_key
assert self._community.dispersy.crypto.is_valid_public_bin(message.authentication.member.public_key), message.authentication.member.public_key.encode("HEX")
assert DispersyCrypto.is_valid_public_key(message.authentication.member.public_key), message.authentication.member.public_key.encode("HEX")
container.extend((self._struct_H.pack(len(message.authentication.member.public_key)), message.authentication.member.public_key))
else:
raise NotImplementedError(encoding)
Expand Down Expand Up @@ -1070,10 +1073,10 @@ def _decode_member_authentication(self, placeholder):

encoding = self.__get_authentication_encoding(authentication)
if encoding == "sha1":
if len(data) < offset + 20:
if len(data) < offset + 32:
raise DropPacket("Insufficient packet size (_decode_member_authentication sha1)")
member_id = data[offset:offset + 20]
offset += 20
member_id = data[offset:offset + 32]
offset += 32

try:
member = self._community.get_member(mid=member_id)
Expand Down Expand Up @@ -1121,11 +1124,11 @@ def _decode_double_member_authentication(self, placeholder):
encoding = self.__get_authentication_encoding(authentication)
if encoding == "sha1":
for _ in range(2):
member_id = data[offset:offset + 20]
member_id = data[offset:offset + 32]
member = self._community.get_member(mid=member_id)
if not member:
raise DelayPacketByMissingMember(self._community, member_id)
offset += 20
offset += 32
members.append(member)

elif encoding == "bin":
Expand Down Expand Up @@ -1164,10 +1167,11 @@ def can_decode_message(self, data):
"""
Returns True when DATA can be decoded using this conversion.
"""
prefix_length = 34
assert isinstance(data, str), type(data)
return (len(data) >= 23 and
data[:22] == self._prefix and
data[22] in self._decode_message_map)
return (len(data) > prefix_length and
data[:prefix_length] == self._prefix and
data[prefix_length] in self._decode_message_map)

def decode_meta_message(self, data):
"""
Expand All @@ -1177,7 +1181,7 @@ def decode_meta_message(self, data):
if not self.can_decode_message(data):
raise DropPacket("Cannot decode message")

return self._decode_message_map[data[22]].meta
return self._decode_message_map[data[34]].meta

@attach_runtime_statistics(u"{0.__class__.__name__}.{function_name} {return_value}")
def decode_message(self, candidate, data, verify=True, allow_empty_signature=False, source="unknown"):
Expand All @@ -1199,10 +1203,10 @@ def decode_message(self, candidate, data, verify=True, allow_empty_signature=Fal
if not self.can_decode_message(data):
raise DropPacket("Cannot decode message")

decode_functions = self._decode_message_map[data[22]]
decode_functions = self._decode_message_map[data[34]]

# placeholder
placeholder = self.Placeholder(candidate, decode_functions.meta, 23, data, verify, allow_empty_signature)
placeholder = self.Placeholder(candidate, decode_functions.meta, 35, data, verify, allow_empty_signature)

# authentication
decode_functions.authentication(placeholder)
Expand Down
Loading

0 comments on commit 6cef7d0

Please sign in to comment.