def pem2ec(filename: str, kid: Optional[str] = None, private: bool = False, passphrase: Optional[str] = None) -> JWK: """Convert EC key from PEM to JWK""" if private: key = import_private_key_from_file(filename, passphrase) else: key = import_public_key_from_file(filename) jwk = ECKey(kid=kid) jwk.load_key(key) return jwk
def read_cosekey(filename: str, private: bool = True) -> CoseKey: """Read key and return CoseKey""" if filename.endswith(".json"): with open(filename, "rt") as jwk_file: jwk_dict = json.load(jwk_file) elif filename.endswith(".key"): key = import_private_key_from_pem_file(filename) jwk = ECKey() jwk.load_key(key) jwk_dict = jwk.serialize(private=private) elif filename.endswith(".crt"): if private: raise ValueError("No private keys in certificates") key = import_public_key_from_cert_file(filename) jwk = ECKey() jwk.load_key(key) jwk_dict = jwk.serialize(private=private) else: raise ValueError("Unknown key format") return cosekey_from_jwk_dict(jwk_dict, private)