def main(): global a class Chat_Server(threading.Thread): def __init__(self): print "Chat_Server init" threading.Thread.__init__(self) self.running = 1 self.conn = None self.addr = None self.host = '127.0.0.1' self.port = None def run(self): print "running chat server" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((self.host, self.port)) s.listen(1) print("waiting for connection from client") self.conn, addr = s.accept() while self.running == True: data = self.conn.recv(1024) if data: data = a.decrypt(data) if data == 'exit': self.running = 0 else: print "Client Says >> " + data else: break time.sleep(0) def kill(self): self.running = 0 class Chat_Client(threading.Thread): def __init__(self): print "Chat Client init" threading.Thread.__init__(self) self.host = None self.sock = None self.running = 1 self.port = None def run(self): print "Chat Client Run" self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect((self.host, self.port)) while self.running == True: rcv = self.sock.recv(1024) data = '' + rcv if data: data = a.decrypt(data) if data == 'exit': self.running = 0 else: print "Server Says >> " + data else: break time.sleep(0) def kill(self): self.running = 0 class Text_Input(threading.Thread): def __init__(self): print "text input init" threading.Thread.__init__(self) self.running = 1 def run(self): print "text input run " while self.running == True: text = raw_input('') try: text = text.replace('\n', '') + '\n' text = a.encrypt(text) chat_client.sock.sendall(text) except: Exception try: text = text.replace('\n', '') + '\n' text = a.encrypt(text) chat_server.conn.sendall(text) except: Exception time.sleep(0.1) def kill(self): self.running = 0 try: mode = sys.argv[1] except: exit(1) if mode == '-s': s = socket.socket() s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Socket object host = '127.0.0.1' # Get host name port = 8000 myName = raw_input("What is your name: ") otherName = raw_input("What is the other name: ") masterkey = raw_input("what is your previously decided on master key" ) # Reserve best port. a = Axolotl(myName, dbname=otherName + '.db', dbpassphrase=None, nonthreaded_sql=False) a.createState(other_name=otherName, mkey=hashlib.sha256(masterkey).digest(), mode=False) rkey = b2a(a.state['DHRs']).strip() print "Send this ratchet key to your client: ", rkey print 'Server started' print 'Waiting for cients to connect...' s.bind((host, port)) # Bind to the port s.listen(3) # Now wait for client connection. c, addr = s.accept() # Establish connection with client. print 'Got connection from', addr secret = raw_input("Enter shared secret: ") smpr = smp.SMP(secret) buffer = c.recv(4096)[4:] buffer = smpr.step2(buffer) tempBuffer = padBytes(longToBytes(len(buffer) + 4), 4) + buffer c.send(tempBuffer) buffer = c.recv(4096)[4:] buffer = smpr.step4(buffer) tempBuffer = padBytes(longToBytes(len(buffer) + 4), 4) + buffer c.send(tempBuffer) if smpr.match: print "match" else: print "no match" s.close() sys.exit() chat_server = Chat_Server() chat_server.port = int(raw_input("Enter port to listen on: ")) chat_server.start() text_input = Text_Input() text_input.start() elif mode == '-c': s = socket.socket() s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Socket object host = '127.0.0.1' # Get host name port = 8000 # Reserve best port. myName = raw_input("What is your name: ") otherName = raw_input("What is the other name: ") masterkey = raw_input("what is your previously decided on master key" ) # Reserve best port. rkey = raw_input( "what is the ratchet key you received from your partner:") print 'Connect to ', host, port s.connect((host, port)) secret = raw_input("Enter shared secret: ") smpr = smp.SMP(secret) buffer = smpr.step1() #print "buffer = {}\n".format( buffer ) tempBuffer = padBytes(longToBytes(len(buffer) + 4), 4) + buffer s.send(tempBuffer) buffer = s.recv(4096)[4:] buffer = smpr.step3(buffer) tempBuffer = padBytes(longToBytes(len(buffer) + 4), 4) + buffer s.send(tempBuffer) buffer = s.recv(4096)[4:] smpr.step5(buffer) if smpr.match: print "match" else: print "no match" s.close() sys.exit() a = Axolotl(myName, dbname=otherName + '.db', dbpassphrase=None, nonthreaded_sql=False) a.createState(other_name=otherName, mkey=hashlib.sha256(masterkey).digest(), mode=True, other_ratchetKey=a2b(rkey)) chat_client = Chat_Client() chat_client.host = raw_input("Enter host to connect to: ") chat_client.port = int(raw_input("Enter port to connect to: ")) chat_client.start() text_input = Text_Input() text_input.start()
OTHER_NICK = 'x' winlock = threading.Lock() transferlock = threading.Lock() cryptlock = threading.Lock() screen_needs_update = False HOST = '127.0.0.1' PORT = 50000 mkey = getpass('What is the masterkey (format: NNN-xxxx)? ') if mode == '-s': axolotl = Axolotl(NICK, dbname=OTHER_NICK + '.db', dbpassphrase=None, nonthreaded_sql=False) axolotl.createState(other_name=OTHER_NICK, mkey=hash_(mkey), mode=False) tor_process = tor(TOR_SERVER_PORT, TOR_SERVER_CONTROL_PORT, '/tmp/tor.server', '') hs, cookie, onion = ephemeralHiddenService() print 'Exchanging credentials via tor...' if credentialsSend(mkey, cookie, b2a(axolotl.state['DHRs']).strip(), onion): pass else: sys.exit(1) print 'Credentials sent, waiting for the other party to connect...' with socketcontext(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept()
OTHER_NICK = 'x' winlock = threading.Lock() transferlock = threading.Lock() cryptlock = threading.Lock() screen_needs_update = False HOST = '127.0.0.1' PORT=50000 mkey = getpass('What is the masterkey (format: NNN-xxxx)? ') if mode == '-s': axolotl = Axolotl(NICK, dbname=OTHER_NICK+'.db', dbpassphrase=None, nonthreaded_sql=False) axolotl.createState(other_name=OTHER_NICK, mkey=hash_(mkey), mode=False) tor_process = tor(TOR_SERVER_PORT, TOR_SERVER_CONTROL_PORT, '/tmp/tor.server', '') hs, cookie, onion = ephemeralHiddenService() print 'Exchanging credentials via tor...' if credentialsSend(mkey, cookie, b2a(axolotl.state['DHRs']).strip(), onion): pass else: sys.exit(1) print 'Credentials sent, waiting for the other party to connect...'
# need clean database for this example to work try: os.remove('./axolotl.db') except OSError: pass # create two instance classes with unencrypted database a = Axolotl('Angie', dbpassphrase=None) b = Axolotl('Barb', dbpassphrase=None) # request a master key mkey = raw_input('Provide a master key: ') # initialize their states a.createState(b.name, mkey, mode=True, other_ratchetKey=b.state['DHRs']) b.createState(a.name, mkey, mode=False) # tell who is who if a.mode: print 'Angie is Alice-like' print 'Barb is Bob-like' else: print 'Angie is Bob-like' print 'Barb is Alice-like' # send some messages back and forth msg0 = a.encrypt('message 0') print 'b decrypt: ', b.decrypt(msg0) msg1 = b.encrypt('message 1') print 'a decrypt: ', a.decrypt(msg1)
except ValueError: PORT = 50000 break if PORT >= 1025 and PORT <= 65535: pass elif PORT == 1: PORT = 1025 + randint(0, 64510) print 'PORT is ' + str(PORT) if mode == '-s': a = Axolotl(NICK, dbname=OTHER_NICK + '.db', dbpassphrase=None, nonthreaded_sql=False) a.createState(other_name=OTHER_NICK, mkey=hashlib.sha256(mkey).digest(), mode=False) print 'Your ratchet key is: %s' % b2a(a.state['DHRs']).strip() print 'Send this to %s...' % OTHER_NICK print 'Waiting for ' + OTHER_NICK + ' to connect...' with socketcontext(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() chatThread(conn) elif mode == '-c': rkey = raw_input('Enter %s\'s ratchet key: ' % OTHER_NICK) a = Axolotl(NICK, dbname=OTHER_NICK + '.db',
try: PORT = raw_input("TCP port (1 for random choice, 50000 is default): ") PORT = int(PORT) break except ValueError: PORT = 50000 break if PORT >= 1025 and PORT <= 65535: pass elif PORT == 1: PORT = 1025 + randint(0, 64510) print "PORT is " + str(PORT) if mode == "-s": a = Axolotl(NICK, dbname=OTHER_NICK + ".db", dbpassphrase=None, nonthreaded_sql=False) a.createState(other_name=OTHER_NICK, mkey=hashlib.sha256(mkey).digest(), mode=False) print "Your ratchet key is: %s" % b2a(a.state["DHRs"]).strip() print "Send this to %s..." % OTHER_NICK print "Waiting for " + OTHER_NICK + " to connect..." with socketcontext(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() chatThread(conn) elif mode == "-c": rkey = raw_input("Enter %s's ratchet key: " % OTHER_NICK) a = Axolotl(NICK, dbname=OTHER_NICK + ".db", dbpassphrase=None, nonthreaded_sql=False) a.createState(other_name=OTHER_NICK, mkey=hashlib.sha256(mkey).digest(), mode=True, other_ratchetKey=a2b(rkey))