def test_get_current_event(): try: with LogCapture(): private_key = secrets.token_bytes(32) signing_key = SigningKey(private_key) public_key_feed_id = signing_key.verify_key.encode() content = Content('whateverapp/whateveraction', { 'oneKey': 'somevalue', 'someotherkey': 1 }) hash_of_content = hashlib.sha256(content.get_as_cbor()).hexdigest() hash_of_prev = None meta = Meta(public_key_feed_id, 0, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector = DatabaseConnector() connector.add_event(event) result = connector.get_current_event(public_key_feed_id) result = Event.from_cbor(result) assert result.meta.hash_of_content[1] == meta.hash_of_content[1] finally: try: if os.path.exists('cborDatabase.sqlite'): os.remove('cborDatabase.sqlite') if os.path.exists('eventDatabase.sqlite'): os.remove('eventDatabase.sqlite') else: assert False except PermissionError: print('Database is still in use')
def test_get_event(): try: with LogCapture() as log_cap: private_key = secrets.token_bytes(32) signing_key = SigningKey(private_key) public_key_feed_id = signing_key.verify_key.encode() content0 = Content('whateverapp/whateveraction', { 'firstkey': 'somevalue', 'someotherkey': 3 }) hash_of_content = hashlib.sha256( content0.get_as_cbor()).hexdigest() hash_of_prev = None meta = Meta(public_key_feed_id, 0, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content0).get_as_cbor() connector = DatabaseConnector() connector.add_event(event) meta = Meta(public_key_feed_id, 1, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) content1 = Content('whateverapp/whateveraction', { 'secondkey': 'somevalue', 'someotherkey': 4 }) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content1).get_as_cbor() connector.add_event(event) content2 = Content('whateverapp/whateveraction', { 'thirdkey': 'somevalue', 'someotherkey': 5 }) meta = Meta(public_key_feed_id, 2, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content2).get_as_cbor() connector.add_event(event) res0 = connector.get_event(public_key_feed_id, 0) res1 = connector.get_event(public_key_feed_id, 1) res2 = connector.get_event(public_key_feed_id, 2) result0 = Event.from_cbor(res0) result1 = Event.from_cbor(res1) result2 = Event.from_cbor(res2) assert result0.content.content == content0.content assert result1.content.content == content1.content assert result2.content.content == content2.content print(log_cap) finally: try: if os.path.exists('cborDatabase.sqlite'): os.remove('cborDatabase.sqlite') if os.path.exists('eventDatabase.sqlite'): os.remove('eventDatabase.sqlite') else: assert False except PermissionError: print('Database is still in use')
def start_client(): ## Alice #inputs = [sys.stdin] # Array of all input select has to look for # (standard input and socket, does not work on windows) print('Successfully connected to other user.') # message to the client that the connection worked print("I AM ALICE") alice = Alice(identifier_other='Bob') #print("X3DH status:", alice.x3dh_status) event_list = cf.get_all_saved_events(1) # print('len(event_list):', len(event_list)) content = None if len(event_list) > 0: last_event = event_list[-1] content, meta = last_event feed_id = Meta.from_cbor(meta).feed_id retrieve_new_messages_alice(alice=alice) while True: try: msg = input('Please enter your message: ') if msg == 'quit': break if msg == 'rsync': recv_rsync() retrieve_new_messages_alice(alice=alice) continue send_Bacnet_msg(encapsulate_message_tcp(alice, msg), feed_id, ecf.get_feed_id()) send_rsync() except KeyboardInterrupt: print('Interrupted') sys.exit(0) running = True sentKeys = False
def start_client(): ## Alice #inputs = [sys.stdin] # Array of all input select has to look for # (standard input and socket, does not work on windows) print('Successfully connected to other user.') # message to the client that the connection worked print("I AM ALICE") alice = Alice(identifier_other='Bob') #print("X3DH status:", alice.x3dh_status) event_list = cf.get_all_saved_events(1) #print('len(event_list):', len(event_list)) content = None if len(event_list) > 0: last_event = event_list[-1] content, meta = last_event feed_id = Meta.from_cbor(meta).feed_id if content[0] == 'ratchet/contactInfo': if alice.x3dh_status == 0: # received_keys = local_sock.recv(224) #received_keys = retrieve_msg_local() key_bundle_to_send = alice.x3dh_create_key_bundle_from_received_key_bundle(content[1]['key_bundle']) # local_sock.send(key_bundle_to_send) #send_msg_local(key_bundle_to_send) #print('befor send to send:', (key_bundle_to_send, ecf.get_feed_id(), feed_id)) send_second_Bacnet_prekey_bundle(key_bundle_to_send, ecf.get_feed_id(), feed_id) if ecf.get_feed_id() != feed_id: own_last_event_sequence_number = function.get_current_seq_no(ecf.get_feed_id()) own_last_event = Event.from_cbor(function.get_event(ecf.get_feed_id(), own_last_event_sequence_number)) last_own_message_not_reached = True for x in event_list: if x[0][0] != 'ratchet/message': continue if last_own_message_not_reached: if x[0][1]['ciphertext'] == own_last_event.content.content[1]['ciphertext']: last_own_message_not_reached = False continue continue #print(x[0][1]['ciphertext']) received_message_raw = expose_message_tcp(x[0][1]['ciphertext'], alice) print("Received:", received_message_raw) while True: try: msg = input('Please enter your message: ') if msg == 'quit': break send_Bacnet_msg(encapsulate_message_tcp(alice, msg), feed_id, ecf.get_feed_id()) except KeyboardInterrupt: print('Interrupted') sys.exit(0) running = True sentKeys = False
def test_get_event(): with session_scope(): with LogCapture() as log_cap: private_key = secrets.token_bytes(32) signing_key = SigningKey(private_key) public_key_feed_id = signing_key.verify_key.encode() content0 = Content('whateverapp/whateveraction', { 'firstkey': 'somevalue', 'someotherkey': 3 }) hash_of_content = hashlib.sha256( content0.get_as_cbor()).hexdigest() hash_of_prev = None meta = Meta(public_key_feed_id, 0, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content0).get_as_cbor() connector = DatabaseConnector() connector.add_event(event) meta = Meta(public_key_feed_id, 1, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) content1 = Content('whateverapp/whateveraction', { 'secondkey': 'somevalue', 'someotherkey': 4 }) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content1).get_as_cbor() connector.add_event(event) content2 = Content('whateverapp/whateveraction', { 'thirdkey': 'somevalue', 'someotherkey': 5 }) meta = Meta(public_key_feed_id, 2, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content2).get_as_cbor() connector.add_event(event) res0 = connector.get_event(public_key_feed_id, 0) res1 = connector.get_event(public_key_feed_id, 1) res2 = connector.get_event(public_key_feed_id, 2) result0 = Event.from_cbor(res0) result1 = Event.from_cbor(res1) result2 = Event.from_cbor(res2) assert result0.content.content == content0.content assert result1.content.content == content1.content assert result2.content.content == content2.content print(log_cap)
def save(self, message, chatID): to_save = self.username + "#split:#" + message signing_key = SigningKey(self.feed_id) public_key_feed_id = signing_key.verify_key.encode(encoder=HexEncoder) content = Content('chat/saveMessage', { 'messagekey': to_save, 'chat_id': chatID, 'timestampkey': time.time() }) hash_of_content = hashlib.sha256(content.get_as_cbor()).hexdigest() hash_of_prev = None meta = Meta(public_key_feed_id, 0, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() self.chat_function.insert_chat_msg(event) self.loadChat( self.partner ) #updating chat, so the send message is also added in the listbox self.text_field.delete(0, 'end')
def test_get_current_event(): with session_scope(): with LogCapture(): private_key = secrets.token_bytes(32) signing_key = SigningKey(private_key) public_key_feed_id = signing_key.verify_key.encode() content = Content('whateverapp/whateveraction', { 'oneKey': 'somevalue', 'someotherkey': 1 }) hash_of_content = hashlib.sha256(content.get_as_cbor()).hexdigest() hash_of_prev = None meta = Meta(public_key_feed_id, 0, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector = DatabaseConnector() connector.add_event(event) result = connector.get_current_event(public_key_feed_id) result = Event.from_cbor(result) assert result.meta.hash_of_content[1] == meta.hash_of_content[1]
def start_server(): ## Bob print("I AM BOB") bob = Bob(identifier_other='Alice') #print("Status:", bob.x3dh_status) event_list = cf.get_all_saved_events(1) content = None feed_id = None if len(event_list) > 0: last_event = event_list[-1] content, meta = last_event feed_id = Meta.from_cbor(meta).feed_id if bob.x3dh_status == 0: prekey_bundle = bob.x3dh_1_create_prekey_bundle() # TODO (identifier_other comes from bacnet): save_prekeys(prekey_bundle, identifier_other) send_first_Bacnet_prekey_bundle(ecf.get_feed_id(), prekey_bundle) send_rsync() exit() if event_list[1][0][0] == 'ratchet/connect' and bob.x3dh_status == 1: # print(event_list[1][0][1]['key_bundle']) bob.x3dh_2_complete_transaction_with_alice_keys(event_list[1][0][1]['key_bundle']) #print("Waiting for an initial message from alice...") bob.x3dh_status = 2 if content[0] == 'ratchet/message' or content[0] == 'ratchet/connect': if bob.x3dh_status == 2: retrieve_new_messages_bob(bob=bob) while True: try: msg = input('Please enter your message: ') if msg == 'quit': break if msg == 'rsync': recv_rsync() retrieve_new_messages_bob(bob=bob) continue send_Bacnet_msg(encapsulate_message_tcp(bob, msg), feed_id, ecf.get_feed_id()) send_rsync() except KeyboardInterrupt: print ('Interrupted') sys.exit(0)
def retrieve_new_messages_alice(alice) -> None: #print("X3DH status:", alice.x3dh_status) event_list = cf.get_all_saved_events(1) # print('len(event_list):', len(event_list)) content = None if len(event_list) > 0: last_event = event_list[-1] content, meta = last_event feed_id = Meta.from_cbor(meta).feed_id if content[0] == 'ratchet/contactInfo': if alice.x3dh_status == 0: # received_keys = local_sock.recv(224) # received_keys = retrieve_msg_local() key_bundle_to_send = alice.x3dh_create_key_bundle_from_received_key_bundle(content[1]['key_bundle']) # local_sock.send(key_bundle_to_send) # send_msg_local(key_bundle_to_send) # print('befor send to send:', (key_bundle_to_send, ecf.get_feed_id(), feed_id)) send_second_Bacnet_prekey_bundle(key_bundle_to_send, ecf.get_feed_id(), feed_id) send_Bacnet_msg( encapsulate_message_tcp(alice, "[This is a necessary first message, but you can ignore it.]"), feed_id, ecf.get_feed_id()) send_rsync() alice.x3dh_status = 2 if ecf.get_feed_id() != feed_id: own_last_event_sequence_number = function.get_current_seq_no(ecf.get_feed_id()) own_last_event = Event.from_cbor(function.get_event(ecf.get_feed_id(), own_last_event_sequence_number)) last_own_message_not_reached = True for x in event_list: if x[0][0] != 'ratchet/message': continue if last_own_message_not_reached: if x[0][1]['ciphertext'] == own_last_event.content.content[1]['ciphertext']: last_own_message_not_reached = False continue continue #print(x[0][1]['ciphertext']) received_message_raw = expose_message_tcp(x[0][1]['ciphertext'], alice) print("Received:", received_message_raw)
def retrieve_new_messages_bob(bob) -> None: #print("Status:", bob.x3dh_status) event_list = cf.get_all_saved_events(1) if len(event_list) > 0: last_event = event_list[-1] content, meta = last_event feed_id = Meta.from_cbor(meta).feed_id if ecf.get_feed_id() != feed_id: own_last_event_sequence_number = function.get_current_seq_no(ecf.get_feed_id()) own_last_event = Event.from_cbor(function.get_event(ecf.get_feed_id(), own_last_event_sequence_number)) last_own_message_not_reached = True for x in event_list: if own_last_event.content.content[0] == 'ratchet/contactInfo': if x[0][0] != 'ratchet/message': last_own_message_not_reached = False continue elif own_last_event.content.content[0] == 'ratchet/message': if x[0][0] != 'ratchet/message': continue if last_own_message_not_reached: if x[0][1]['ciphertext'] == own_last_event.content.content[1]['ciphertext']: last_own_message_not_reached = False continue continue # print('print msg') received_message_raw = expose_message_tcp(x[0][1]['ciphertext'], bob) print("Received:", received_message_raw) else: print('no new messages')
def test_get_current_seq_no(): try: private_key = secrets.token_bytes(32) signing_key = SigningKey(private_key) public_key_feed_id1 = signing_key.verify_key.encode() content = Content('whateverapp/whateveraction', { 'somekey': 'somevalue', 'someotherkey': 2 }) hash_of_content = hashlib.sha256(content.get_as_cbor()).hexdigest() hash_of_prev = None meta = Meta(public_key_feed_id1, 0, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector = DatabaseConnector() connector.add_event(event) meta = Meta(public_key_feed_id1, 1, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) meta = Meta(public_key_feed_id1, 2, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) content = Content('whateverapp/whateveraction', { 'test1': 'somevalue', 'someotherkey': 2 }) meta = Meta(public_key_feed_id1, 3, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) private_key = secrets.token_bytes(32) signing_key = SigningKey(private_key) public_key_feed_id2 = signing_key.verify_key.encode() meta = Meta(public_key_feed_id2, 0, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) meta = Meta(public_key_feed_id2, 1, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) meta = Meta(public_key_feed_id2, 2, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) meta = Meta(public_key_feed_id2, 3, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) content = Content('whateverapp/whateveraction', { 'test2': 'somevalue', 'someotherkey': 2 }) meta = Meta(public_key_feed_id2, 4, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) res1 = connector.get_current_seq_no(public_key_feed_id1) res2 = connector.get_current_seq_no(public_key_feed_id2) result1 = connector.get_current_event(public_key_feed_id1) result2 = connector.get_current_event(public_key_feed_id2) print(result1) print(result2) assert res1 == 3 assert res2 == 4 finally: try: if os.path.exists('cborDatabase.sqlite'): os.remove('cborDatabase.sqlite') if os.path.exists('eventDatabase.sqlite'): os.remove('eventDatabase.sqlite') else: assert False except PermissionError: print('Database is still in use')
def test_get_current_seq_no(): with session_scope(): private_key = secrets.token_bytes(32) signing_key = SigningKey(private_key) public_key_feed_id1 = signing_key.verify_key.encode() content = Content('whateverapp/whateveraction', { 'somekey': 'somevalue', 'someotherkey': 2 }) hash_of_content = hashlib.sha256(content.get_as_cbor()).hexdigest() hash_of_prev = None meta = Meta(public_key_feed_id1, 0, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector = DatabaseConnector() connector.add_event(event) meta = Meta(public_key_feed_id1, 1, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) meta = Meta(public_key_feed_id1, 2, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) content = Content('whateverapp/whateveraction', { 'test1': 'somevalue', 'someotherkey': 2 }) meta = Meta(public_key_feed_id1, 3, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) private_key = secrets.token_bytes(32) signing_key = SigningKey(private_key) public_key_feed_id2 = signing_key.verify_key.encode() meta = Meta(public_key_feed_id2, 0, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) meta = Meta(public_key_feed_id2, 1, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) meta = Meta(public_key_feed_id2, 2, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) meta = Meta(public_key_feed_id2, 3, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) content = Content('whateverapp/whateveraction', { 'test2': 'somevalue', 'someotherkey': 2 }) meta = Meta(public_key_feed_id2, 4, hash_of_prev, 'ed25519', ('sha256', hash_of_content)) signature = signing_key.sign(meta.get_as_cbor())._signature event = Event(meta, signature, content).get_as_cbor() connector.add_event(event) res1 = connector.get_current_seq_no(public_key_feed_id1) res2 = connector.get_current_seq_no(public_key_feed_id2) result1 = connector.get_current_event(public_key_feed_id1) result2 = connector.get_current_event(public_key_feed_id2) print(result1) print(result2) assert res1 == 3 assert res2 == 4
def start_server(): ## Bob print("I AM BOB") bob = Bob(identifier_other='Alice') #print("Status:", bob.x3dh_status) event_list = cf.get_all_saved_events(1) if len(event_list) > 0: last_event = event_list[-1] content, meta = last_event feed_id = Meta.from_cbor(meta).feed_id if bob.x3dh_status == 0: prekey_bundle = bob.x3dh_1_create_prekey_bundle() # TODO (identifier_other comes from bacnet): save_prekeys(prekey_bundle, identifier_other) send_first_Bacnet_prekey_bundle(ecf.get_feed_id(), prekey_bundle) exit() if event_list[1][0][0] == 'ratchet/connect' and bob.x3dh_status == 1: if bob.x3dh_status == 1: #print(event_list[1][0][1]['key_bundle']) bob.x3dh_2_complete_transaction_with_alice_keys(event_list[1][0][1]['key_bundle']) print("Waiting for an initial message from alice...") bob.x3dh_status = 2 if content[0] == 'ratchet/message' or content[0] == 'ratchet/connect': if bob.x3dh_status == 2: if ecf.get_feed_id() != feed_id: own_last_event_sequence_number = function.get_current_seq_no(ecf.get_feed_id()) own_last_event = Event.from_cbor(function.get_event(ecf.get_feed_id(), own_last_event_sequence_number)) last_own_message_not_reached = True for x in event_list: if own_last_event.content.content[0] == 'ratchet/contactInfo': if x[0][0] != 'ratchet/message': last_own_message_not_reached = False continue elif own_last_event.content.content[0] == 'ratchet/message': if x[0][0] != 'ratchet/message': continue if last_own_message_not_reached: if x[0][1]['ciphertext'] == own_last_event.content.content[1]['ciphertext']: last_own_message_not_reached = False continue continue #print('print msg') received_message_raw = expose_message_tcp(x[0][1]['ciphertext'], bob) print("Received:", received_message_raw) else: print('no new messages') while True: try: msg = input('Please enter your message: ') if msg == 'quit': break send_Bacnet_msg(encapsulate_message_tcp(bob, msg), feed_id, ecf.get_feed_id()) except KeyboardInterrupt: print('Interrupted') sys.exit(0)