Ejemplo n.º 1
0
 def loads_public_key(cls, obj):
     curve = cls.DSS_CURVES[obj['crv']]()
     public_numbers = EllipticCurvePublicNumbers(
         base64_to_int(obj['x']),
         base64_to_int(obj['y']),
         curve,
     )
     return public_numbers.public_key(default_backend())
Ejemplo n.º 2
0
 def load_public_key(self):
     curve = self.DSS_CURVES[self._dict_data['crv']]()
     public_numbers = EllipticCurvePublicNumbers(
         base64_to_int(self._dict_data['x']),
         base64_to_int(self._dict_data['y']),
         curve,
     )
     return public_numbers.public_key(default_backend())
Ejemplo n.º 3
0
    def load_private_key(self):
        obj = self._dict_data

        if 'oth' in obj:  # pragma: no cover
            # https://tools.ietf.org/html/rfc7518#section-6.3.2.7
            raise ValueError('"oth" is not supported yet')

        public_numbers = RSAPublicNumbers(base64_to_int(obj['e']),
                                          base64_to_int(obj['n']))

        if has_all_prime_factors(obj):
            numbers = RSAPrivateNumbers(d=base64_to_int(obj['d']),
                                        p=base64_to_int(obj['p']),
                                        q=base64_to_int(obj['q']),
                                        dmp1=base64_to_int(obj['dp']),
                                        dmq1=base64_to_int(obj['dq']),
                                        iqmp=base64_to_int(obj['qi']),
                                        public_numbers=public_numbers)
        else:
            d = base64_to_int(obj['d'])
            p, q = rsa_recover_prime_factors(public_numbers.n, d,
                                             public_numbers.e)
            numbers = RSAPrivateNumbers(d=d,
                                        p=p,
                                        q=q,
                                        dmp1=rsa_crt_dmp1(d, p),
                                        dmq1=rsa_crt_dmq1(d, q),
                                        iqmp=rsa_crt_iqmp(p, q),
                                        public_numbers=public_numbers)

        return numbers.private_key(default_backend())
Ejemplo n.º 4
0
    def loads(self, obj):
        for k in ['crv', 'x', 'y']:
            if k not in obj:
                raise ValueError('Not a elliptic curve key')

        curve = self.DSS_CURVES[obj['crv']]()
        public_numbers = EllipticCurvePublicNumbers(
            base64_to_int(obj['x']),
            base64_to_int(obj['y']),
            curve,
        )
        if 'd' in obj:
            private_numbers = EllipticCurvePrivateNumbers(
                base64_to_int(obj['d']), public_numbers)
            return private_numbers.private_key(default_backend())
        return public_numbers.public_key(default_backend())
Ejemplo n.º 5
0
    def loads_private_key(self, obj):
        if 'oth' in obj:
            # https://tools.ietf.org/html/rfc7518#section-6.3.2.7
            return self.loads_other_primes_info(obj)

        props = ['p', 'q', 'dp', 'dq', 'qi']
        props_found = [prop in obj for prop in props]
        any_props_found = any(props_found)

        if any_props_found and not all(props_found):
            raise ValueError('RSA key must include all parameters if any are present besides d')

        public_numbers = RSAPublicNumbers(
            base64_to_int(obj['e']), base64_to_int(obj['n'])
        )

        if any_props_found:
            numbers = RSAPrivateNumbers(
                d=base64_to_int(obj['d']),
                p=base64_to_int(obj['p']),
                q=base64_to_int(obj['q']),
                dmp1=base64_to_int(obj['dp']),
                dmq1=base64_to_int(obj['dq']),
                iqmp=base64_to_int(obj['qi']),
                public_numbers=public_numbers
            )
        else:
            d = base64_to_int(obj['d'])
            p, q = rsa_recover_prime_factors(
                public_numbers.n, d, public_numbers.e
            )
            numbers = RSAPrivateNumbers(
                d=d,
                p=p,
                q=q,
                dmp1=rsa_crt_dmp1(d, p),
                dmq1=rsa_crt_dmq1(d, q),
                iqmp=rsa_crt_iqmp(p, q),
                public_numbers=public_numbers
            )

        return numbers.private_key(default_backend())
Ejemplo n.º 6
0
 def loads_public_key(self, obj):
     numbers = RSAPublicNumbers(base64_to_int(obj['e']),
                                base64_to_int(obj['n']))
     return numbers.public_key(default_backend())
Ejemplo n.º 7
0
 def assertBase64IntEqual(self, x, y):
     self.assertEqual(base64_to_int(x), base64_to_int(y))
Ejemplo n.º 8
0
 def load_public_key(self):
     numbers = RSAPublicNumbers(base64_to_int(self._dict_data['e']),
                                base64_to_int(self._dict_data['n']))
     return numbers.public_key(default_backend())