Ejemplo n.º 1
0
def send_message_3(client_socket, merchant_SID, client_temporary_cert):
    print("Sent 3rd message.")
    NC = random.randint(100000, 10000000000)
    amount = b"500"
    PI = socket_functions.concat_messages(
        b"123456789101", b"11/22", b"123", merchant_SID, amount,
        crypto_lib.serialize_cert(client_temporary_cert),
        crypto_lib.int_to_bytes(NC), b"Merchant name")
    SIG_PI = crypto_lib.sign(PI, private_key_rsa)
    PM = socket_functions.concat_messages(PI, SIG_PI)
    encripted_PM = crypto_lib.encrypt_AES(PM, aes_key_PG, aes_iv_PG)
    PO_message = socket_functions.concat_messages(b"5 morcovi la 10 lei kg",
                                                  merchant_SID, amount,
                                                  crypto_lib.int_to_bytes(NC))
    SigC = crypto_lib.sign(PO_message, private_key_rsa)
    PO = socket_functions.concat_messages(PO_message, SigC)
    encrypted_aes_key_PG = crypto_lib.encrypt_RSA(aes_key_PG,
                                                  public_key_rsa_pg)
    encrypted_aes_iv_PG = crypto_lib.encrypt_RSA(aes_iv_PG, public_key_rsa_pg)
    message_to_send = socket_functions.concat_messages(encripted_PM, PO,
                                                       encrypted_aes_key_PG,
                                                       encrypted_aes_iv_PG)
    message_to_send = crypto_lib.encrypt_AES(message_to_send, aes_key_merchant,
                                             aes_iv_merchant)
    socket_functions.socket_send(client_socket, message_to_send)
    return NC, amount
Ejemplo n.º 2
0
def send_message_5(merchant_conn, resp, sid, amount, NC, AES_key_PG_M,
                   AES_IV_PG_M):
    print("Sent 5th message.")
    message_to_sign = socket_functions.concat_messages(resp, sid, amount, NC)
    signature = crypto_lib.sign(message_to_sign, private_key_rsa)
    message_to_send = socket_functions.concat_messages(resp, sid, signature)
    message_to_send = crypto_lib.encrypt_AES(message_to_send, AES_key_PG_M,
                                             AES_IV_PG_M)
    socket_functions.socket_send(merchant_conn, message_to_send)
Ejemplo n.º 3
0
def recv_message_5(pg_socket, amount, NC):
    print("Received 5th message")
    message = socket_functions.socket_recv(pg_socket)
    message = crypto_lib.decrypt_AES(message, aes_key_merchant_pg,
                                     aes_iv_merchant_pg)
    Resp, Sid, SigPG = socket_functions.split_message(message)
    checkSid = socket_functions.concat_messages(Resp, Sid, amount, NC)

    if crypto_lib.verify_signature_is_valid(SigPG, checkSid,
                                            public_key_rsa_pg):
        print("The signature is from the PG ")
    else:
        print("The signature is invalid")
    return_message = socket_functions.concat_messages(Resp, Sid, SigPG)
    return return_message
Ejemplo n.º 4
0
def send_message_2(client_conn, aes_key, aes_iv):
    print("Sent 2nd message.")
    SID = generate_SID()
    # print(crypto_lib.bytes_to_int(SID))
    SID_signature = crypto_lib.sign(SID, private_key_rsa)
    message_to_send = socket_functions.concat_messages(SID, SID_signature)
    encrypted_message_to_send = crypto_lib.encrypt_AES(message_to_send,
                                                       aes_key, aes_iv)
    socket_functions.socket_send(client_conn, encrypted_message_to_send)
    return SID
Ejemplo n.º 5
0
def send_message_4(pg_socket, PM, SID, amount, client_certificate,
                   aes_key_client_PG_encrypted, aes_iv_client_PG_encrypted):
    print("Sent 4th message.")
    sigM = crypto_lib.sign(
        socket_functions.concat_messages(
            SID, crypto_lib.serialize_cert(client_certificate), amount),
        private_key_rsa)
    message_to_send = socket_functions.concat_messages(
        PM, sigM, aes_key_client_PG_encrypted, aes_iv_client_PG_encrypted)
    message_to_send = crypto_lib.encrypt_AES(message_to_send,
                                             aes_key_merchant_pg,
                                             aes_iv_merchant_pg)
    encrypted_aes_key_PG = crypto_lib.encrypt_RSA(aes_key_merchant_pg,
                                                  public_key_rsa_pg)
    encrypted_aes_iv_PG = crypto_lib.encrypt_RSA(aes_iv_merchant_pg,
                                                 public_key_rsa_pg)
    message_to_send = socket_functions.concat_messages(message_to_send,
                                                       encrypted_aes_key_PG,
                                                       encrypted_aes_iv_PG)
    socket_functions.socket_send(pg_socket, message_to_send)
Ejemplo n.º 6
0
def recv_message_3(client_conn, aes_key, aes_iv):
    print("Received 3rd message")
    message = socket_functions.socket_recv(client_conn)
    message = crypto_lib.decrypt_AES(message, aes_key, aes_iv)
    PM, OrderDesc, SID, amount, NC, sig_PO, aes_key_client_PG_encrypted, aes_iv_client_PG_encrypted = socket_functions.split_message(
        message)
    checkSid = socket_functions.concat_messages(OrderDesc, SID, amount, NC)
    if crypto_lib.verify_signature_is_valid(sig_PO, checkSid,
                                            public_key_rsa_client):
        print("The signature is from the client ")
    else:
        print("The signature is invalid")
    return PM, amount, aes_key_client_PG_encrypted, aes_iv_client_PG_encrypted, NC
Ejemplo n.º 7
0
def recv_message_6(client_socket, amount, NC):
    print("Received 6th message.")
    message = socket_functions.socket_recv(client_socket)
    message = crypto_lib.decrypt_AES(message, aes_key_merchant,
                                     aes_iv_merchant)
    Resp, Sid, SigPG = socket_functions.split_message(message)
    checkSid = socket_functions.concat_messages(Resp, Sid, amount,
                                                crypto_lib.int_to_bytes(NC))
    if crypto_lib.verify_signature_is_valid(SigPG, checkSid,
                                            public_key_rsa_pg):
        print("The signature is from the PG. ")
    else:
        print("The signature is invalid.")
    Resp = Resp.decode()
    print(Resp)
Ejemplo n.º 8
0
def send_message_1(client_socket):
    print("Sent 1st message.")
    client_temporary_cert = generate_cert_client()
    cert_to_send = crypto_lib.serialize_cert(client_temporary_cert)
    encrypted_cert_to_send = crypto_lib.encrypt_AES(cert_to_send,
                                                    aes_key_merchant,
                                                    aes_iv_merchant)
    encrypted_aes_key = crypto_lib.encrypt_RSA(aes_key_merchant,
                                               public_key_rsa_merchant)
    encrypted_aes_iv = crypto_lib.encrypt_RSA(aes_iv_merchant,
                                              public_key_rsa_merchant)
    message_to_send = socket_functions.concat_messages(encrypted_cert_to_send,
                                                       encrypted_aes_key,
                                                       encrypted_aes_iv)
    socket_functions.socket_send(client_socket, message_to_send)
    return client_temporary_cert
Ejemplo n.º 9
0
def recv_message_4(merchant_conn):
    print("Received 4th message")
    message = socket_functions.socket_recv(merchant_conn)
    message, AES_key_PG_M, AES_IV_PG_M = socket_functions.split_message(
        message)
    AES_key_PG_M = crypto_lib.decrypt_RSA(AES_key_PG_M, private_key_rsa)
    AES_IV_PG_M = crypto_lib.decrypt_RSA(AES_IV_PG_M, private_key_rsa)
    message = crypto_lib.decrypt_AES(message, AES_key_PG_M, AES_IV_PG_M)
    PM, sigM, AES_key_PG_C, AES_IV_PG_C = socket_functions.split_message(
        message)
    AES_key_PG_C = crypto_lib.decrypt_RSA(AES_key_PG_C, private_key_rsa)
    AES_IV_PG_C = crypto_lib.decrypt_RSA(AES_IV_PG_C, private_key_rsa)
    PM = crypto_lib.decrypt_AES(PM, AES_key_PG_C, AES_IV_PG_C)
    CardN, CardExp, CCode, sid, amount, PubKC, NC, M, sigC = socket_functions.split_message(
        PM)

    # Check merchant signature
    merchant_sig_msg = socket_functions.concat_messages(sid, PubKC, amount)
    if crypto_lib.verify_signature_is_valid(sigM, merchant_sig_msg,
                                            public_key_rsa_merchant):
        print("The signature is from the merchant")
    else:
        print("The signature is invalid")
        resp = b"Denied"
        return resp, sid, amount, NC, AES_key_PG_M, AES_IV_PG_M

    # Check client signature
    client_sig_msg = socket_functions.concat_messages(CardN, CardExp, CCode,
                                                      sid, amount, PubKC, NC,
                                                      M)
    if crypto_lib.verify_signature_is_valid(sigC, client_sig_msg,
                                            public_key_rsa_client):
        print("The signature is from the client ")
    else:
        print("The signature is invalid")
        resp = b"Denied"
        return resp, sid, amount, NC, AES_key_PG_M, AES_IV_PG_M

    if check_card_details(CardN, CardExp, CCode):
        print("The card details are correct")
    else:
        print("Card details invalid")
        resp = b"Denied, bad card details"
        return resp, sid, amount, NC, AES_key_PG_M, AES_IV_PG_M

    if not check_if_Nonce_sid_combo_fresh(NC, sid):
        resp = b"Denied, replay attack"
        return resp, sid, amount, NC, AES_key_PG_M, AES_IV_PG_M

    if not check_if_client_cert_fresh(PubKC):
        resp = b"Denied, replay attack"
        return resp, sid, amount, NC, AES_key_PG_M, AES_IV_PG_M

    transaction_mock = TransactionSim()
    if transaction_mock.client_has_enough_balance(int(amount.decode())):
        print("Before transaction:")
        transaction_mock.show_balance()
        transaction_mock.perform_transaction(int(amount.decode()))
        print("After transaction:")
        transaction_mock.show_balance()
        resp = b"Accepted, transaction performed"
        return resp, sid, amount, NC, AES_key_PG_M, AES_IV_PG_M
    else:
        resp = b"Denied, client doesn't have enough balance"
        return resp, sid, amount, NC, AES_key_PG_M, AES_IV_PG_M