Ejemplo n.º 1
0
    def load_rsa_private_numbers(self, numbers):
        rsa._check_private_key_components(
            numbers.p,
            numbers.q,
            numbers.d,
            numbers.dmp1,
            numbers.dmq1,
            numbers.iqmp,
            numbers.public_numbers.e,
            numbers.public_numbers.n
        )
        rsa_cdata = self._lib.RSA_new()
        assert rsa_cdata != self._ffi.NULL
        rsa_cdata = self._ffi.gc(rsa_cdata, self._lib.RSA_free)
        rsa_cdata.p = self._int_to_bn(numbers.p)
        rsa_cdata.q = self._int_to_bn(numbers.q)
        rsa_cdata.d = self._int_to_bn(numbers.d)
        rsa_cdata.dmp1 = self._int_to_bn(numbers.dmp1)
        rsa_cdata.dmq1 = self._int_to_bn(numbers.dmq1)
        rsa_cdata.iqmp = self._int_to_bn(numbers.iqmp)
        rsa_cdata.e = self._int_to_bn(numbers.public_numbers.e)
        rsa_cdata.n = self._int_to_bn(numbers.public_numbers.n)
        res = self._lib.RSA_blinding_on(rsa_cdata, self._ffi.NULL)
        assert res == 1

        return _RSAPrivateKey(self, rsa_cdata)
Ejemplo n.º 2
0
    def load_rsa_private_numbers(self, numbers):
        rsa._check_private_key_components(
            numbers.p,
            numbers.q,
            numbers.d,
            numbers.dmp1,
            numbers.dmq1,
            numbers.iqmp,
            numbers.public_numbers.e,
            numbers.public_numbers.n
        )
        rsa_cdata = self._lib.RSA_new()
        assert rsa_cdata != self._ffi.NULL
        rsa_cdata = self._ffi.gc(rsa_cdata, self._lib.RSA_free)
        rsa_cdata.p = self._int_to_bn(numbers.p)
        rsa_cdata.q = self._int_to_bn(numbers.q)
        rsa_cdata.d = self._int_to_bn(numbers.d)
        rsa_cdata.dmp1 = self._int_to_bn(numbers.dmp1)
        rsa_cdata.dmq1 = self._int_to_bn(numbers.dmq1)
        rsa_cdata.iqmp = self._int_to_bn(numbers.iqmp)
        rsa_cdata.e = self._int_to_bn(numbers.public_numbers.e)
        rsa_cdata.n = self._int_to_bn(numbers.public_numbers.n)
        res = self._lib.RSA_blinding_on(rsa_cdata, self._ffi.NULL)
        assert res == 1

        return _RSAPrivateKey(self, rsa_cdata)
Ejemplo n.º 3
0
    def load_rsa_private_numbers(self, numbers):
        rsa._check_private_key_components(
            numbers.p,
            numbers.q,
            numbers.d,
            numbers.dmp1,
            numbers.dmq1,
            numbers.iqmp,
            numbers.public_numbers.e,
            numbers.public_numbers.n
        )

        attrs = build_attributes([
            (self._binding.CKA_TOKEN, False),  # don't persist it
            (self._binding.CKA_CLASS, self._binding.CKO_PRIVATE_KEY),
            (self._binding.CKA_KEY_TYPE, self._binding.CKK_RSA),
            (
                self._binding.CKA_MODULUS,
                utils.int_to_bytes(numbers.public_numbers.n)
            ),
            (
                self._binding.CKA_PUBLIC_EXPONENT,
                utils.int_to_bytes(numbers.public_numbers.e)
            ),
            (
                self._binding.CKA_PRIVATE_EXPONENT,
                utils.int_to_bytes(numbers.d)
            ),
            (self._binding.CKA_PRIME_1, utils.int_to_bytes(numbers.p)),
            (self._binding.CKA_PRIME_2, utils.int_to_bytes(numbers.q)),
            (self._binding.CKA_EXPONENT_1, utils.int_to_bytes(numbers.dmp1)),
            (self._binding.CKA_EXPONENT_2, utils.int_to_bytes(numbers.dmq1)),
            (self._binding.CKA_COEFFICIENT, utils.int_to_bytes(numbers.iqmp)),
        ], self)

        session = self._session_pool.acquire()
        # TODO: do we want to delete the object from the session when it
        # is no longer in scope?
        object_handle = self._ffi.new("CK_OBJECT_HANDLE *")
        res = self._lib.C_CreateObject(
            session[0], attrs.template, len(attrs.template), object_handle
        )
        self._check_error(res)

        return _RSAPrivateKey(self, object_handle[0])