- 25A41366C0D57C2FA6EAABA61E0BDB07E77B9F48D09FB7FEA1D65296FCC68C8B8836F954B13DE8EE071061A6F49BF8DB89E958EC7B961AC974926F5163694C21
+ 2BEBEB6EE55F0941C567E114EE01352F18F96D02AC4E2F037B9CED7E71C219CC2ED51AC615D28E22244C5E07F136EC7CE1FB565C7217C62DE4708AEE4D8B05D5
blatta/lib/infosec.py
(37 . 7)(37 . 7)
5 def __init__(self, server=None):
6 self.server = server
7
8 def pack(self, peer, message):
9 def get_message_bytes(self, message, peer=None):
10 try:
11 timestamp = message.timestamp
12 except:
(52 . 14)(52 . 9)
14 else:
15 int_ts = timestamp
16
17 key_bytes = base64.b64decode(peer.get_key())
18 signing_key = key_bytes[:32]
19 cipher_key = key_bytes[32:]
20
21 # let's generate the self_chain value from the last message or set it to zero if
22 # there this is the first message
23
24
25 if message.original:
26 if command == DIRECT:
27 self_chain = self.server.state.get_last_message_hash(message.speaker, peer.peer_id)
(75 . 21)(70 . 21)
29 # pack message bytes
30
31 message_bytes = struct.pack(MESSAGE_PACKET_FORMAT, int_ts, self_chain, net_chain, speaker, message.body)
32 return message_bytes
33
34 # log messages
35 def pack(self, peer, message):
36 key_bytes = base64.b64decode(peer.get_key())
37 signing_key = key_bytes[:32]
38 cipher_key = key_bytes[32:]
39
40 if message.original:
41 if command == DIRECT:
42 self.server.state.log(message.speaker, message_bytes, peer.peer_id)
43 elif command == BROADCAST:
44 self.server.state.log(message.speaker, message_bytes)
45 message_bytes = self.get_message_bytes(message, peer)
46
47 # pack packet bytes
48
49 nonce = self._generate_nonce(16)
50 bounces = message.bounces
51 version = 0xfe
52 red_packet_bytes = struct.pack(RED_PACKET_FORMAT, nonce, bounces, version, command, self._pad(message_bytes, MAX_MESSAGE_LENGTH))
53 red_packet_bytes = struct.pack(RED_PACKET_FORMAT, nonce, bounces, version, message.command, self._pad(message_bytes, MAX_MESSAGE_LENGTH))
54
55 # encrypt packet
56
(104 . 9)(99 . 6)
58
59 signed_packet_bytes = struct.pack(BLACK_PACKET_FORMAT, black_packet_bytes, signature_bytes)
60
61 # we want to ignore this ts if it is sent back to us
62
63 self.server.recent.insert(int_ts)
64 return signed_packet_bytes
65
66 def unpack(self, peer, black_packet):
(148 . 16)(140 . 18)
68 if command == IGNORE:
69 return Message({"speaker": speaker, "error_code": IGNORED})
70
71
72 # check timestamp
73
74 if(int_ts not in self._ts_range()):
75 return Message({ "error_code": STALE_PACKET }, self.server)
76
77 if(self.server.recent.has(int_ts)):
78 # check for duplicates
79
80 message_hash = binascii.hexlify(hashlib.sha256(message_bytes).digest())
81 if(self.server.state.is_duplicate_message(message_hash)):
82 return Message({ "error_code": DUPLICATE_PACKET }, self.server)
83 else:
84 self.server.recent.insert(int_ts)
85 self.server.state.add_to_dedup_queue(message_hash)
86
87 # check self_chain
88