def peer_response(self, key, ticket, peer_username): # Step 1: Decrypt and split the ticket to get the information of the client ret = False ticket = CryptoLib.decryptUsingSymetricKey(key, self.sessionID, ticket) ticket = ticket.split(":") peer_address = ticket[0] peer_public_key = ticket[1] peer = Peer.Peer(peer_username, peer_address, peer_public_key) # Step 2: Generate and send contribution b = CryptoLib.generatePublicPrivateKeys(32) msg = pow(2, b, self.peerPrime) msg = CryptoLib.encryptUsingPublicKey(peer_public_key, msg) if not self.send_message(msg, peer_address[0], peer_address[1]): return ret # Step 3: Recieve peer contribution and iv and unpack it peer_contribution = self.receive_response() peer_contribution = CryptoLib.decryptUsingPrivateKey(self.clientPrivateKey, peer_contribution) peer_contribution = peer_contribution.split(":") peer_session_id = peer_contribution[1] peer_contribution = peer_contribution[0] # Construct shared key shared_key = pow(peer_contribution, b, self.peerPrime) # Step 4: Challenge Response (Two way authentication) - user sends challenge, client increments and sends # back response which is again incremented and sent back to user. All of this is done using the symmetric # key encryption with the shared key. challenge = random.randrange(1, 100000) challenge = CryptoLib.encyptUsingSymmetricKey(shared_key, peer_session_id, challenge) if not self.send_message(challenge, peer_address[0], peer_address[1]): return ret response = self.receive_response() response = CryptoLib.decryptUsingSymetricKey(shared_key, peer_session_id, response) # If authentication is successful, add the peer to list of connected peers and send back response if response == challenge + 1: response += 1 response = CryptoLib.encyptUsingSymmetricKey(shared_key, peer_session_id, response) if not self.send_message(response, peer_address[0], peer_address[1]): return ret peer.set_initialization_vector(peer_session_id) peer.set_shared_key(shared_key) self.peers[peer_address] = peer return True return False
def receiveClientMessages(self): msg, addr = self.sock.recvfrom(ChatServer.MSG_LEN) isClientKnown = self.clients.has_key(addr) if(isClientKnown): client = self.clients.get(addr) if(client == None): print "couldn't retrieve client" return if client.get_session_key() is None and client.get_initialization_vector() is None: decrypted_msg = CryptoLib.decryptUsingPrivateKey(self.private_key, msg) else: decrypted_msg = CryptoLib.decryptUsingSymetricKey(client.get_session_key(), client.get_initialization_vector(), msg) self.handleClientMessages(decrypted_msg, client, addr) else: if self.DBG: print "it's a potential client!!" self.handleClientMessages(msg, None, addr)