def METHOD_NAME(session, config):
    print("Trying to extract public key from the HSM.")
    try:
        if "label_pubkey" in config:
            public_key_label = config["label_pubkey"]
        else:
            print(
                "Config option 'label_pubkey' not found, "
                "using config option 'label' for public key."
            )
            public_key_label = config["label"]
        public_key = session.get_key(
            object_class=pkcs11.constants.ObjectClass.PUBLIC_KEY,
            label=public_key_label,
        )
        if public_key.key_type == pkcs11.mechanisms.KeyType.RSA:
            exponent = public_key[pkcs11.Attribute.PUBLIC_EXPONENT]
            modulus = public_key[pkcs11.Attribute.MODULUS]
            e = int.from_bytes(exponent, byteorder="big")
            n = int.from_bytes(modulus, byteorder="big")
            public_key = RSA.RSAPublicNumbers(e, n).public_key()
        elif public_key.key_type == pkcs11.mechanisms.KeyType.EC:
            ecpoints, _ = ecdsa.der.remove_octet_string(
                public_key[pkcs11.Attribute.EC_POINT]
            )
            public_key = EC.EllipticCurvePublicKey.from_encoded_point(
                EC.SECP256R1(), ecpoints
            )
        else:
            print("Incorrect public key algorithm")
            sys.exit(1)
        print(f"Got public key with label {public_key_label}.")
        return public_key
    except pkcs11.exceptions.PKCS11Error as e:
        handle_exceptions(e)
        print("Failed to extract the public key")
        sys.exit(1)