def __init__(self, key, algorithm): if algorithm not in ALGORITHMS.RSA: raise JWKError('hash_alg: %s is not a valid hash algorithm' % algorithm) if algorithm in ALGORITHMS.RSA_KW and algorithm != ALGORITHMS.RSA1_5: raise JWKError('alg: %s is not supported by the RSA backend' % algorithm) self.hash_alg = { ALGORITHMS.RS256: self.SHA256, ALGORITHMS.RS384: self.SHA384, ALGORITHMS.RS512: self.SHA512 }.get(algorithm) self._algorithm = algorithm if isinstance(key, dict): self._prepared_key = self._process_jwk(key) return if isinstance(key, (pyrsa.PublicKey, pyrsa.PrivateKey)): self._prepared_key = key return if isinstance(key, str): key = key.encode('utf-8') if isinstance(key, bytes): try: self._prepared_key = pyrsa.PublicKey.load_pkcs1(key) except ValueError: try: self._prepared_key = pyrsa.PublicKey.load_pkcs1_openssl_pem( key) except ValueError: try: self._prepared_key = pyrsa.PrivateKey.load_pkcs1(key) except ValueError: try: der = pyrsa_pem.load_pem(key, b'PRIVATE KEY') try: pkcs1_key = rsa_private_key_pkcs8_to_pkcs1(der) except PyAsn1Error: # If the key was encoded using the old, invalid, # encoding then pyasn1 will throw an error attempting # to parse the key. pkcs1_key = _legacy_private_key_pkcs8_to_pkcs1( der) self._prepared_key = pyrsa.PrivateKey.load_pkcs1( pkcs1_key, format="DER") except ValueError as e: raise JWKError(e) return raise JWKError('Unable to parse an RSA_JWK from key: %s' % key)
def __init__(self, key, algorithm): if algorithm not in ALGORITHMS.RSA: raise JWKError('hash_alg: %s is not a valid hash algorithm' % algorithm) self.hash_alg = { ALGORITHMS.RS256: self.SHA256, ALGORITHMS.RS384: self.SHA384, ALGORITHMS.RS512: self.SHA512 }.get(algorithm) self._algorithm = algorithm if isinstance(key, dict): self._prepared_key = self._process_jwk(key) return if isinstance(key, (pyrsa.PublicKey, pyrsa.PrivateKey)): self._prepared_key = key return if isinstance(key, six.string_types): key = key.encode('utf-8') if isinstance(key, six.binary_type): try: self._prepared_key = pyrsa.PublicKey.load_pkcs1(key) except ValueError: try: self._prepared_key = pyrsa.PublicKey.load_pkcs1_openssl_pem(key) except ValueError: try: self._prepared_key = pyrsa.PrivateKey.load_pkcs1(key) except ValueError: try: der = pyrsa_pem.load_pem(key, b'PRIVATE KEY') try: pkcs1_key = rsa_private_key_pkcs8_to_pkcs1(der) except PyAsn1Error: # If the key was encoded using the old, invalid, # encoding then pyasn1 will throw an error attempting # to parse the key. pkcs1_key = _legacy_private_key_pkcs8_to_pkcs1(der) self._prepared_key = pyrsa.PrivateKey.load_pkcs1(pkcs1_key, format="DER") except ValueError as e: raise JWKError(e) return raise JWKError('Unable to parse an RSA_JWK from key: %s' % key)
def __init__(self, key, algorithm): if algorithm not in ALGORITHMS.RSA: raise JWKError('hash_alg: %s is not a valid hash algorithm' % algorithm) self.hash_alg = { ALGORITHMS.RS256: self.SHA256, ALGORITHMS.RS384: self.SHA384, ALGORITHMS.RS512: self.SHA512 }.get(algorithm) self._algorithm = algorithm if isinstance(key, dict): self._prepared_key = self._process_jwk(key) return if isinstance(key, (pyrsa.PublicKey, pyrsa.PrivateKey)): self._prepared_key = key return if isinstance(key, six.string_types): key = key.encode('utf-8') if isinstance(key, six.binary_type): try: self._prepared_key = pyrsa.PublicKey.load_pkcs1(key) except ValueError: try: self._prepared_key = pyrsa.PublicKey.load_pkcs1_openssl_pem( key) except ValueError: try: self._prepared_key = pyrsa.PrivateKey.load_pkcs1(key) except ValueError: try: # python-rsa does not support PKCS8 yet so we have to manually remove OID der = pyrsa_pem.load_pem(key, b'PRIVATE KEY') header, der = der[:22], der[22:] if header != PKCS8_RSA_HEADER: raise ValueError("Invalid PKCS8 header") self._prepared_key = pyrsa.PrivateKey._load_pkcs1_der( der) except ValueError as e: raise JWKError(e) return raise JWKError('Unable to parse an RSA_JWK from key: %s' % key)
def __init__(self, key, algorithm): if algorithm not in ALGORITHMS.RSA: raise JWKError('hash_alg: %s is not a valid hash algorithm' % algorithm) self.hash_alg = { ALGORITHMS.RS256: self.SHA256, ALGORITHMS.RS384: self.SHA384, ALGORITHMS.RS512: self.SHA512 }.get(algorithm) self._algorithm = algorithm if isinstance(key, dict): self._prepared_key = self._process_jwk(key) return if isinstance(key, (pyrsa.PublicKey, pyrsa.PrivateKey)): self._prepared_key = key return if isinstance(key, six.string_types): key = key.encode('utf-8') if isinstance(key, six.binary_type): try: self._prepared_key = pyrsa.PublicKey.load_pkcs1(key) except ValueError: try: self._prepared_key = pyrsa.PublicKey.load_pkcs1_openssl_pem(key) except ValueError: try: self._prepared_key = pyrsa.PrivateKey.load_pkcs1(key) except ValueError: try: # python-rsa does not support PKCS8 yet so we have to manually remove OID der = pyrsa_pem.load_pem(key, b'PRIVATE KEY') header, der = der[:22], der[22:] if header != PKCS8_RSA_HEADER: raise ValueError("Invalid PKCS8 header") self._prepared_key = pyrsa.PrivateKey._load_pkcs1_der(der) except ValueError as e: raise JWKError(e) return raise JWKError('Unable to parse an RSA_JWK from key: %s' % key)