Beispiel #1
0
    def test_cert_signature(self):
        """GIVEN conf.TX_CERT_AUTH = True, PeerAuthorization create using cert
        WHEN create new tx and create signature
        THEN tx.public_key must be x.509 der cert
        """
        channel_name = "cert_channel"
        conf.CHANNEL_OPTION = {
            channel_name: {
                "load_cert": True,
                "consensus_cert_use": True,
                "tx_cert_use": True,
                "key_load_type": conf.KeyLoadType.FILE_LOAD,
                "public_path": os.path.join(conf.LOOPCHAIN_ROOT_PATH, 'resources/default_certs/cert.pem'),
                "private_path": os.path.join(conf.LOOPCHAIN_ROOT_PATH, 'resources/default_certs/key.pem'),
                "private_password": None
            }
        }
        peer_auth = PeerAuthorization(channel_name)

        # WHEN
        tx = Transaction()
        tx.put_data('{"a":"b"}')
        tx.sign_hash(peer_auth)

        # THEN
        logging.debug(f"tx public key : {tx.public_key}")
        self.assertEqual(tx.public_key, peer_auth.tx_cert)
        # tx.publickey using for load certificate and not raise any error
        x509.load_der_x509_certificate(tx.public_key, default_backend())
Beispiel #2
0
    def test_signature_validate(self):
        """GIVEN success tx, invalid public key tx, invalid signature tx,
        WHEN validate 3 tx
        THEN only success tx validate return true
        """
        # GIVEN
        # init peer_auth for signautre

        channel =  list(conf.CHANNEL_OPTION)[0]
        peer_auth = PeerAuthorization(channel=channel)

        # create txs
        success_tx = test_util.create_basic_tx("aaa", peer_auth)

        # public key and signature property must don't have setter
        invalid_public_tx = test_util.create_basic_tx("aaa", peer_auth)
        invalid_public_tx._Transaction__public_key = b'invalid_public'

        invalid_sign_tx = test_util.create_basic_tx("aaa", peer_auth)
        invalid_sign_tx._Transaction__signature = b'invalid_sign'

        hash_generator = get_tx_hash_generator(channel)
        tx_validator = TxValidator(channel, conf.CHANNEL_OPTION[channel]["send_tx_type"], hash_generator)
        # WHEN THEN
        self.assertTrue(tx_validator.validate(success_tx))
        logging.debug("start validate invalid public key")
        self.assertFalse(tx_validator.validate(invalid_public_tx))
        logging.debug("start validate invalid signature")
        self.assertFalse(tx_validator.validate(invalid_sign_tx))
    def test_signature_validate(self):
        """ GIVEN success tx, invalid public key tx, invalid signature tx,
        WHEN validate 3 tx
        THEN only success tx validate return true
        """
        # GIVEN
        # init peer_auth for signautre
        peer_auth = PeerAuthorization(public_file=conf.PUBLIC_PATH,
                                      pri_file=conf.PRIVATE_PATH,
                                      cert_pass=conf.DEFAULT_PW)

        # create txs
        success_tx = test_util.create_basic_tx("aaa", peer_auth)

        # public key and signature property must don't have setter
        invalid_public_tx = test_util.create_basic_tx("aaa", peer_auth)
        invalid_public_tx._Transaction__public_key = b'invalid_public'

        invalid_sign_tx = test_util.create_basic_tx("aaa", peer_auth)
        invalid_sign_tx._Transaction__signature = b'invalid_sign'

        # WHEN THEN
        self.assertTrue(Transaction.validate(success_tx))
        logging.debug("start validate invalid public key")
        self.assertFalse(
            Transaction.validate(invalid_public_tx, is_exception_log=False))
        logging.debug("start validate invalid signature")
        self.assertFalse(
            Transaction.validate(invalid_sign_tx, is_exception_log=False))
Beispiel #4
0
def create_default_peer_auth() -> PeerAuthorization:
    channel = list(conf.CHANNEL_OPTION)[0]
    peer_auth = PeerAuthorization(channel)
    return peer_auth
Beispiel #5
0
def create_peer_auth() -> PeerAuthorization:
    peer_auth = PeerAuthorization(public_file=conf.PUBLIC_PATH,
                                  pri_file=conf.PRIVATE_PATH,
                                  cert_pass=conf.DEFAULT_PW)
    return peer_auth
    def test_load_pki_by_seed(self):
        """ GIVEN random table conf.KMS = TRUE
        WHEN create two PeerAuthorization
        THEN create PeerAuthorization success
        THEN both PeerAuthorization sign and verify can both signature
        """

        # GIVEN
        conf.ENABLE_KMS = True
        seed = 1234
        rs_service = RadioStationService(rand_seed=seed)
        rand_table = rs_service._RadioStationService__random_table

        # WHEN THEN
        peer_auth = PeerAuthorization(rand_table=rand_table)
        peer_auth2 = PeerAuthorization(rand_table=rand_table)

        # THEN
        sign = peer_auth.sign_data(b'a')
        sign2 = peer_auth2.sign_data(b'a')
        # both public key must same
        self.assertEqual(peer_auth.get_public_der(),
                         peer_auth2.get_public_der())
        # both peer_auth can verify signature
        self.assertTrue(peer_auth.verify_data(b'a', sign2))
        self.assertTrue(peer_auth2.verify_data(b'a', sign))

        # if have another seed can't verify
        conf.MY_SEED = 100
        peer_auth3 = PeerAuthorization(rand_table=rand_table)
        self.assertFalse(peer_auth3.verify_data(b'a', sign))

        conf.ENABLE_KMS = False