Ejemplo n.º 1
0
    async def revoke(self, certificate):
        """
        Revoke certificate's public key into REMChain.
        Send transaction to chain.

        Args:
            certificate (object): certificate object

        Returns:
            Information about public key.

        To use:
            .. code-block:: python

                remme = Remme()
                revoke_response = remme.certificate.revoke(certificate)
        """
        if type(certificate) == str:
            certificate = certificate_from_pem(certificate=certificate)

        address = RSA.get_address_from_public_key(
            public_key_to_der(public_key=certificate.public_key()), )

        return await self._remme_public_key_storage.revoke(
            public_key_address=address)
Ejemplo n.º 2
0
    def __init__(self, private_key=None, public_key=None):
        """
        Constructor for RSA key pair. If only private key available then public key will be generate from private.

        Args:
            private_key (bytes): rsa private key
            public_key (bytes, optional): rsa public key
        """
        super(RSA, self).__init__()

        if private_key and public_key:
            self._private_key = private_key
            self._public_key = public_key
            self._private_key_obj = private_key_der_to_object(private_key=self._private_key)
            self._public_key_obj = public_key_der_to_object(public_key=self._public_key)

        elif private_key:
            self._private_key = private_key
            self._private_key_obj = private_key_der_to_object(private_key=self._private_key)
            self._public_key_obj = self._private_key_obj.public_key()
            self._public_key = public_key_to_der(public_key=self._public_key_obj)

        elif public_key:
            self._public_key = public_key
            self._public_key_obj = public_key_der_to_object(public_key=self._public_key)

        if self._private_key:
            self._private_key_hex = self._private_key.hex()

        self._public_key_hex = self._public_key.hex()

        self._address = generate_address(RemmeFamilyName.PUBLIC_KEY.value,  self._public_key)
        self._key_type = KeyType.RSA
Ejemplo n.º 3
0
    async def get_info(self, certificate):
        """
        Get info about certificate's public key.

        Args:
            certificate (object): certificate object

        Returns:
            Information about public key.

        To use:
            .. code-block:: python

                remme = Remme()
                info = remme.certificate.get_info(certificate)
        """
        if type(certificate) == str:
            certificate = certificate_from_pem(certificate=certificate)

        address = RSA.get_address_from_public_key(
            public_key_to_der(public_key=certificate.public_key()), )

        check_result = await self._remme_public_key_storage.get_info(
            public_key_address=address)

        if check_result is not None:
            return check_result
        else:
            raise Exception('This certificate was not found.')
Ejemplo n.º 4
0
    async def check(self, certificate):
        """
        Check certificate's public key on validity and revocation.

        Args:
            certificate (object): certificate object

        Returns:
            Boolean ``True``.

        To use:
            .. code-block:: python

                remme = Remme()
                is_valid = remme.certificate.check(certificate)
        """
        if type(certificate) == str:
            certificate = certificate_from_pem(certificate=certificate)

        address = RSA.get_address_from_public_key(
            public_key_to_der(public_key=certificate.public_key()), )

        check_result = await self._remme_public_key_storage.check(
            address=address)

        if check_result:
            return check_result
        else:
            raise Exception('This certificate was not found.')
Ejemplo n.º 5
0
    def generate_key_pair(options=None):
        """
        Generate public and private key pair.

        Args:
            options (integer): _rsa_key_size can be specified

        Returns:
            Generated key pair in bytes.
        """
        if options is not None:
            private_key = rsa.generate_private_key(public_exponent=65537, key_size=options, backend=default_backend())
            return private_key_to_der(private_key), public_key_to_der(private_key.public_key())

        private_key = rsa.generate_private_key(
            public_exponent=65537, key_size=RSA._rsa_key_size, backend=default_backend(),
        )
        return private_key_to_der(private_key), public_key_to_der(private_key.public_key())
Ejemplo n.º 6
0
    async def store(self, certificate):
        """
        Store your certificate public key and hash of certificate into REMChain.
        Your certificate should contains private and public keys.
        Send transaction to chain.

        Args:
            certificate (object): certificate object

        Returns:
            Information about storing public key to REMChain.

        To use:
            .. code-block:: python

                remme = Remme()
                certificate = remme.certificate.create(
                    common_name='user_name',
                    email='*****@*****.**',
                    name='John',
                    surname='Smith',
                    country_name='US',
                    validity=360,
                    serial=str(datetime.now())
                )
                store_response = remme.certificate.store(certificate)
        """
        if type(certificate) == str:
            certificate = certificate_from_pem(certificate=certificate)

        certificate_pem = certificate_to_pem(
            certificate=certificate).decode('utf-8')

        valid_from = math.floor(
            int(certificate.not_valid_before.strftime("%s")) / 1000)
        valid_to = math.floor(
            int(certificate.not_valid_after.strftime("%s")) / 1000)

        batch_response = await self._remme_public_key_storage.create_and_store(
            data=certificate_pem,
            keys=RSA(
                private_key=private_key_to_der(certificate.private_key),
                public_key=public_key_to_der(certificate.public_key()),
            ),
            rsa_signature_padding=RsaSignaturePadding.PSS,
            valid_from=valid_from,
            valid_to=valid_to,
            do_owner_pay=False,
        )

        return CertificateTransactionResponse(
            network_config=batch_response.network_config,
            batch_id=batch_response.batch_id,
            certificate=certificate,
        )
    def __init__(self, network_config, batch_id, certificate=None):
        """
        Args:
            network_config (dict): config of network (node address and ssl mode)
            batch_id (string): batch id
            certificate (optional): x509 certificate object
        """

        super(CertificateTransactionResponse, self).__init__(
            network_config=network_config,
            batch_id=batch_id,
        )
        self._certificate = certificate
        self._keys = RSA(
            private_key=private_key_to_der(self._certificate.private_key),
            public_key=public_key_to_der(self._certificate.public_key()),
        )
Ejemplo n.º 8
0
    def verify(self, certificate, data, signature, rsa_signature_padding=None):
        """
        Verify data with a public key (default for PSS).

        Args:
            certificate (object): certificate object
            data (string): data string which will be verified
            signature (string): hex string of signature
            rsa_signature_padding (RsaSignaturePadding): RSA padding

        Returns:
            Boolean ``True`` if signature is correct, or ``False`` if invalid.
        """
        if type(certificate) == str:
            certificate = certificate_from_pem(certificate=certificate)

        if certificate.private_key is None:
            raise Exception('Your certificate does not have private key.')

        keys = RSA(public_key=public_key_to_der(certificate.public_key()))

        return keys.verify(data=data,
                           signature=signature,
                           rsa_signature_padding=rsa_signature_padding)