int crypt_activate_by_keyring(struct crypt_device *cd,
			      const char *name,
			      const char *key_description,
			      int keyslot,
			      uint32_t flags)
{
	char *passphrase;
	size_t passphrase_size;
	int r;

	if (!cd || !key_description)
		return -EINVAL;

	log_dbg(cd, "%s volume %s [keyslot %d] using passphrase in keyring.",
		name ? "Activating" : "Checking", name ?: "passphrase", keyslot);

	if (!kernel_keyring_support()) {
		log_err(cd, _("Kernel keyring is not supported by the kernel."));
		return -EINVAL;
	}

	r = _activate_check_status(cd, name, flags & CRYPT_ACTIVATE_REFRESH);
	if (r < 0)
		return r;

	r = keyring_get_passphrase(key_description, &passphrase, &passphrase_size);
	if (r < 0) {
		log_err(cd, _("Failed to read passphrase from keyring (error %d)."), r);
		return -EINVAL;
	}

	r = _activate_by_passphrase(cd, name, keyslot, passphrase, passphrase_size, flags);

	crypt_safe_memzero(passphrase, passphrase_size);
	free(passphrase);

	return r;
}
int crypt_activate_by_keyring(struct crypt_device *cd,
			      const char *name,
			      const char *key_description,
			      int keyslot,
			      uint32_t flags)
{
	char *passphrase;
	size_t passphrase_size;
	int r;

	if (!cd || !key_description)
		return -EINVAL;

	log_dbg(cd, "%s volume %s [keyslot %d] using passphrase in keyring.",
		name ? "Activating" : "Checking", name ?: "passphrase", keyslot);

	if (!kernel_keyring_support()) {
		log_err(cd, _("Kernel keyring is not supported by the kernel."));
		return -EINVAL;
	}

	r = _activate_check_status(cd, name, flags & CRYPT_ACTIVATE_REFRESH);
	if (r < 0)
		return r;

	r = keyring_get_passphrase(key_description, &passphrase, &passphrase_size);
	if (r < 0) {
		log_err(cd, _("Failed to read passphrase from keyring (error %d)."), r);
		return -EINVAL;
	}

	r = _activate_by_passphrase(cd, name, keyslot, passphrase, passphrase_size, flags);

	crypt_safe_memzero(passphrase, passphrase_size);
	free(passphrase);

	return r;
}