def deposit(arguments): """ Verify that the check has been signed by an authorised person Verify that the content of the check has not been altered Verify that the check has not been already cashed Store the check in the db """ with open(arguments[0], "r") as file_: signed_check = unserialize(file_.readline()) client_key = Key.import_key_from_path(arguments[1]) bank_key = Key.import_key_from_path(arguments[2]) # the signature of the check check_signature = signed_check["signature"] # the check encoded in base64 base64_check = signed_check["base64_check"] # the check as a string dic_check = unserialize(base64_check) # the customer's signature (the one used to sign the check) customer_signature = dic_check["signature_customer_public_key"] data_signed_by_customer = create_data_to_sign(base64_check) # if the customer is part of the bank, the signature present in the check should be OK # check that the check has not already been cashed-in/altered in some way if verify_signature_check(client_key, check_signature, data_signed_by_customer): if verify_check_first(dic_check): print("This check has been cashed in") store_check(base64_check) exit(0) else: print("This check has already been cashed-in") exit(1) else: print("This check has been altered and connot be cashed in") exit(1)
def test_sign_small_string(self): """ Test that we only accept a client with a signature from the bank """ bankKey = Key.import_key_from_path("./cryptobank/test/functionalTests/keys/bank.key") bankKeyFalse = Key.import_key_from_path("./cryptobank/test/functionalTests/keys/bankFalse.key") signature = import_key("./cryptobank/test/functionalTests/keys/customer.signedkey") signature_false = import_key("./cryptobank/test/functionalTests/keys/customerFalse.signedkey") with open("./cryptobank/test/functionalTests/keys/customer.pubkey", "r") as file_: customer_key = file_.read() self.assertTrue(bankKey.verify(customer_key, signature)) self.assertFalse(bankKey.verify(customer_key, signature_false))
def sign_key(raw_data_path, bank_key="bank.key"): """ Sign the key if the customer To do so : - we import the bank private key - we open the customer's public key - we sign the public key - we print it """ bank_key = Key.import_key_from_path(bank_key) with open(raw_data_path, "r") as file_: data = file_.read() return bank_key.sign(data) # def check_customer_exists(pubkey, signature) """
def check_key(signed_key, bank_pubkey="bank.pubkey", customer_pubkey="customer.pubkey"): """ Will check that the key passed as a parameter is correct - loads the bank's public key - opens the signature - opens the client's public key - checks the key again the signature """ bank_key = Key.import_key_from_path(bank_pubkey) signature = import_key(signed_key) with open(customer_pubkey, "r") as file_: customer_key = file_.read() if bank_key.verify(customer_key, signature): return True else: return False
def test_merchant_changed_check(self): path = "./cryptobank/test/functionalTests/keys/" """ Check that a merchant cannot change the content of a check without the bank noticing """ client_key = Key.import_key_from_path(path + "customer.pubkey") with open(path + "check.json", "r") as file_: signed_check = unserialize(file_.readline()) check_signature = signed_check["signature"] base64_check = signed_check["base64_check"] #self.assertTrue(verify_signature_check(client_key, check_signature, base64_check)) # check that if someone has changed something to the check, the bank does not accept the check false_check = unserialize(base64_check) false_check["amount"] = 100 false_check_64 = serialize(false_check).decode() self.assertFalse(verify_signature_check(client_key, check_signature, false_check_64))
def verify_transaction(arguments, bank_pubkey="bank.pubkey", customer_pubkey="customer.pubkey"): """ Checks that the customer's key is valid Import le check et le transform en dic Import la transaction d'origine et la transforme en dic Verifie que les informations dans le cheque sont les même qu'il a envoyé Si OK Verifie que la signature est valide Si OK : renvoie 0 sur la sortie standard Sinon : renvoie 1 """ if check_key(arguments[3], bank_pubkey, customer_pubkey) is False: print("The client has not got an account with the bank") exit(1) with open(arguments[0]) as file_: original_transaction = unserialize(file_.readline()) with open(arguments[1]) as file_: signed_check = unserialize(file_.readline()) #this is the check that the customer has signed signed_transaction = unserialize(signed_check["base64_check"]) signature = signed_check["signature"] client_key = Key.import_key_from_path(arguments[2]) data_signed_by_customer = create_data_to_sign(signed_check["base64_check"]) # if the two checks match, we just have to check that the signature is ok. if signed_transaction == original_transaction: if client_key.verify(data_signed_by_customer, signature): exit(0) else: print("The signature does not appear to have been made by the client. Could there be Charly in the middle ? Better being safe than sorry... exiting") exit(1) else: print("the check the customer has signed is not the same as the one the merchant signed. Exiting") exit(1)
def test_file_import(self): k = Key.import_key_from_path("./cryptobank/test/example1.privatekey") self.assertEqual(k.d, 7260641110672835047827501997505943133111644574566000021223010655821235083273105396166649351538199689087470625243598930260920051965152495026558593677206110131225339468439963696250138457237146400566084663068588989184142313129218072345377097517321866479966875312693812985816283627389031218962071766399892103649986215460566584764250065985145026249061430407197849885219245804705911929101154059227132722588356478832245147574995036068957521408346894233847348879381167232592226850553762509438716835126943861635944585593943577474311423037504121629177079385831326372436253345586993761731264277329967225302363697165149604157685)
def test_import(self): k = Key.import_key(pubkey_1) self.assertEqual(k.e, 18897326884202641234381724217488958995190797122003233542684316952874488810869573730944622835354397244317859649915528385517373310402314740914570265474523272099131714896756052269490962230503225722132211115905742403996980711119850248355680219148536395687571429722875234218542660272413072090979567502042914122689850642334277524152955051075447016971996457587785108279649166801055888464505877529236280197432544496104519403557397458980707851341304380245408178980000883951963902366089873653696224677071369162307056676861208871622794020914091439567596698788739421579604610544106865335958324246485234101799018316906692573409315) k = Key.import_key(privatekey_1) self.assertEqual(k.d, 1410967230799177417042385412412002261427348694336208727250515911095177323623483151762166939809820990205237022066094942941400287898645783386591901529609566011801694246722792750695960118385852870318502773092576720230943886303000749384489126224075553163175214089595505046767412318159605636479520693358659923251626324913895694446856400218137968349272167522205637088311688055129655425751538433904067891821313582506976703769674480646979241672484357318411641630713839622220015025276426191378496311127345965320516733508678360347003805677062899095898339092982140221299400830116218888348410806444043671239604113952783660811471)
def setUpClass(self): """ Runned once """ self.keys = Key.import_key(privatekey_1)