def manage_request(self, data): # quello che mi arriva è del tipo [OPCODE|S|C|sito|...|nonce|hash]kcs | IV unpacked_data = utility.unpack_message(data, 4) if(unpacked_data is None or len(unpacked_data) != 2): utility.print_user_log(self.client_address,"Bad packet format") return (False, 109) encrypted_data , IV = unpacked_data decrypted_data = utility.decrypt_AES(encrypted_data, self.session_key, IV) if decrypted_data is None: return (False, 102) #print decrypted_data decrypted_fields = utility.unpack_message(decrypted_data, 4) if decrypted_data is None: utility.print_user_log(self.client_address,"Bad packet format") return (False, 109) data_to_hash = ''.join(decrypted_fields[:-1]) hashed_data_c = decrypted_fields[-1] hashed_data = utility.get_hash(data_to_hash) ret = utility.secure_cmp(hashed_data_c, hashed_data) utility.print_user_log(self.client_address,"Request: %s" % decrypted_fields[0]) if ret is False: print hexlify(hashed_data_c) print hexlify(hashed_data) utility.print_user_log(self.client_address,"Different hashes") return (False, 103) #print fields nonce = decrypted_fields[-2] if nonce in self.nonces: utility.print_user_log(self.client_address, "No fresh nonce!!") return (False, 104) self.nonces.append(nonce) #print fields #execute query response_fields = self.execute_query(decrypted_fields[:-2]) # è una lista [:-2] tutto tranne nonce hash response_fields.append(nonce) # aggiungo nonce alla lista response = utility.concatenate(*response_fields) hashed_response = utility.get_hash(response) #print "hash,", hashed_response #data_to_pack = utility.concatenate((response, hashed_response)) # concateno hash alla risposta response_fields.append(hashed_response) #print "RF: ",response_fields packed_message = utility.pack_message(*response_fields) #print packed_message IV = os.urandom(16) data_to_send = utility.encrypt_AES(packed_message, self.session_key, IV) if data_to_send is None: return (False, 106) data_to_send = utility.pack_message(data_to_send, IV) #print "DATA TO SEND \n", data_to_send #un = utility.unpack_message(data_to_send, 4) #print "ENC",un[0] #print "IV", un[1] utility.print_user_log(self.client_address, 'Response: '+ response_fields[0]) return (True, data_to_send)
def handle(self): print "New Connection from: " + self.client_address[0], ', on port: ' , self.client_address[1] # M1 i = 0 while(i < 4): utility.print_user_log(self.client_address, "KEY ESTABLISHMENT PROTOCOL") self.key_est_result = self.key_establishment_protocol() if self.key_est_result is None: i = i + 1 utility.print_user_log(self.client_address, "Errors during protocol number: %d" %i) fail = utility.pack_message('FAIL') ret = utility.send_data(self.request, fail) if ret is False: utility.print_user_log(self.client_address,"[ERROR] Error during sending data ") return None #self.request.close() elif self.key_est_result == 201: #utility.print_user_log(self.client_address, 'DISC') utility.print_user_log(self.client_address, "Client Disconnected") return None elif self.key_est_result == 203: i = i + 1 utility.print_user_log(self.client_address, "Errors during protocol number: %d" %i) fail = utility.pack_message('COPY') ret = utility.send_data(self.request, fail) else: utility.print_user_log(self.client_address,"Key establishment protocol has done correctly \n") i = 0 break if (i >= 2): utility.print_user_log(self.client_address, "To many errors! Disconnecting...") return None # after return, finish() will be called while(True): utility.print_line() utility.print_user_log(self.client_address, "Listening for requests...") data = utility.recv_data(self.request, 0) #print data if not data or data is None: utility.print_user_log(self.client_address, 'Client Disconnected' ) break # after return, finish() will be called data_to_send = self.manage_request(data) if data_to_send[0] is False: utility.print_user_log(self.client_address,"[ERROR] Unable to manage the request ") ret = utility.send_data(self.request, "FAIL") if ret is False: utility.print_user_log(self.client_address,"[ERROR] Error during sending data ") else: #print data_to_send[1] ret = utility.send_data(self.request, data_to_send[1]) if ret is False: utility.print_user_log(self.client_address,"[ERROR] Error during sending data ")
def message_M4(self, nonce_prot_c): utility.print_user_log(self.client_address,'Sending M4...') hash_nonce_c = utility.get_hash(nonce_prot_c) data_to_pack = utility.concatenate('100', self.server_ID, self.client_ID, hash_nonce_c) hash_data = utility.get_hash(data_to_pack) data = utility.pack_message('100', self.server_ID, self.client_ID, hash_nonce_c, hash_data) IV = os.urandom(16) enc_data = utility.encrypt_AES(data, self.session_key, IV) if enc_data is None: utility.print_user_log(self.client_address, '[ERROR] Error during encryption') return None data_to_send = utility.pack_message(enc_data, IV) return data_to_send
def message_M2(self): utility.print_user_log(self.client_address, "Sending M2..") # generate Nonce self.nonce_s = os.urandom(16) # print "nonce: ", nonce_s # read certificate cert = utility.read_cert('server_cert_1') # server_cert_1 if cert is None: utility.print_user_log(self.client_address,'[ERROR] Some errors occured reading the certificate') return None utility.print_user_log(self.client_address, 'Server certificate loaded') data_to_send = utility.pack_message(self.server_ID, cert, self.nonce_s) return data_to_send