mirror of
https://gitlab.com/cryptsetup/cryptsetup.git
synced 2025-12-19 06:40:08 +01:00
Move try_token_unlock to luks utils.
and rename it to luks_try_token_unlock.
This commit is contained in:
committed by
Milan Broz
parent
4493d9ad3e
commit
5f48657f4d
@@ -301,3 +301,73 @@ int luks_init_keyslot_context(struct crypt_device *cd,
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int luks_try_token_unlock(struct crypt_device *cd,
|
||||
int keyslot,
|
||||
int token_id,
|
||||
const char *activated_name,
|
||||
const char *token_type,
|
||||
uint32_t activate_flags,
|
||||
int tries,
|
||||
bool activation,
|
||||
bool token_only)
|
||||
{
|
||||
int r;
|
||||
struct crypt_keyslot_context *kc;
|
||||
size_t pin_len;
|
||||
char msg[64], *pin = NULL;
|
||||
|
||||
assert(tries >= 1);
|
||||
assert(token_id >= 0 || token_id == CRYPT_ANY_TOKEN);
|
||||
assert(keyslot >= 0 || keyslot == CRYPT_ANY_SLOT);
|
||||
|
||||
r = crypt_keyslot_context_init_by_token(cd, token_id, token_type, NULL, 0, NULL, &kc);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (activation)
|
||||
r = crypt_activate_by_keyslot_context(cd, activated_name, keyslot, kc, CRYPT_ANY_SLOT, NULL, activate_flags);
|
||||
else
|
||||
r = crypt_resume_by_keyslot_context(cd, activated_name, keyslot, kc);
|
||||
|
||||
tools_keyslot_msg(r, UNLOCKED);
|
||||
tools_token_error_msg(r, token_type, token_id, false);
|
||||
|
||||
/* Token requires PIN (-ENOANO). Ask for it if there is evident preference for tokens */
|
||||
if (r != -ENOANO || (!token_only && !token_type && token_id == CRYPT_ANY_TOKEN))
|
||||
goto out;
|
||||
|
||||
if (token_id == CRYPT_ANY_TOKEN)
|
||||
r = snprintf(msg, sizeof(msg), _("Enter token PIN: "));
|
||||
else
|
||||
r = snprintf(msg, sizeof(msg), _("Enter token %d PIN: "), token_id);
|
||||
if (r < 0 || (size_t)r >= sizeof(msg)) {
|
||||
r = -EINVAL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
do {
|
||||
r = tools_get_key(msg, &pin, &pin_len, 0, 0, NULL,
|
||||
ARG_UINT32(OPT_TIMEOUT_ID), verify_passphrase(0), 0, cd);
|
||||
if (r < 0)
|
||||
break;
|
||||
|
||||
r = crypt_keyslot_context_set_pin(cd, pin, pin_len, kc);
|
||||
crypt_safe_free(pin);
|
||||
if (r < 0)
|
||||
break;
|
||||
|
||||
if (activation)
|
||||
r = crypt_activate_by_keyslot_context(cd, activated_name, keyslot,
|
||||
kc, CRYPT_ANY_SLOT, NULL, activate_flags);
|
||||
else
|
||||
r = crypt_resume_by_keyslot_context(cd, activated_name, keyslot, kc);
|
||||
|
||||
tools_keyslot_msg(r, UNLOCKED);
|
||||
tools_token_error_msg(r, token_type, token_id, true);
|
||||
check_signal(&r);
|
||||
} while (r == -ENOANO && (--tries > 0));
|
||||
out:
|
||||
crypt_keyslot_context_free(kc);
|
||||
return r;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user