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 9203296
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 638 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
13 changes: 9 additions & 4 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 @@ -1842,8 +1844,11 @@ def get_member(self, *argv, **kwargs):
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 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
18 changes: 11 additions & 7 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,6 +55,7 @@ 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
# 22 == hashlength + 2
assert len(self._prefix) == 22 # when this assumption changes, we need to ensure the
# dispersy_version and community_version properties are
# returned correctly
Expand Down Expand Up @@ -84,9 +86,10 @@ def can_decode_message(self, data):
"""
# at least a length of 23, as we need the prefix + 1 byte messagetype
assert isinstance(data, str), type(data)
assert len(data) >= 23
prefix_length = 22
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 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 @@ -1164,10 +1167,11 @@ def can_decode_message(self, data):
"""
Returns True when DATA can be decoded using this conversion.
"""
prefix_length = 22
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 Down
Loading

0 comments on commit 9203296

Please sign in to comment.