Esempio n. 1
0
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)
Esempio n. 2
0
    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))
Esempio n. 3
0
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)
    """
Esempio n. 4
0
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
Esempio n. 5
0
 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))
Esempio n. 6
0
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)
Esempio n. 7
0
 def test_file_import(self):
     k = Key.import_key_from_path("./cryptobank/test/example1.privatekey")
     self.assertEqual(k.d, 7260641110672835047827501997505943133111644574566000021223010655821235083273105396166649351538199689087470625243598930260920051965152495026558593677206110131225339468439963696250138457237146400566084663068588989184142313129218072345377097517321866479966875312693812985816283627389031218962071766399892103649986215460566584764250065985145026249061430407197849885219245804705911929101154059227132722588356478832245147574995036068957521408346894233847348879381167232592226850553762509438716835126943861635944585593943577474311423037504121629177079385831326372436253345586993761731264277329967225302363697165149604157685)