Refactor activation by volume key(s) in helper routine.

This commit is contained in:
Ondrej Kozina
2025-04-25 21:47:43 +02:00
committed by Milan Broz
parent 8fcd8a78d8
commit cf29d51589
3 changed files with 111 additions and 47 deletions

View File

@@ -376,3 +376,78 @@ out:
return r;
}
/* Initiates keyslot context(s) based on non-interactive input only */
int luks_init_keyslot_contexts_by_volume_keys(struct crypt_device *cd,
const char *vk_file1,
const char *vk_file2,
int keysize1_bytes,
int keysize2_bytes,
const char *vk_in_keyring1,
const char *vk_in_keyring2,
struct crypt_keyslot_context **r_kc1,
struct crypt_keyslot_context **r_kc2)
{
int r = -EINVAL;
char *vk_description = NULL, *key = NULL;
struct crypt_keyslot_context *kc1 = NULL, *kc2 = NULL;
assert(cd);
assert(r_kc1);
assert(r_kc2);
if (vk_file1 && keysize1_bytes > 0) {
r = tools_read_vk(vk_file1, &key, keysize1_bytes);
if (r < 0)
return r;
r = crypt_keyslot_context_init_by_volume_key(cd, key, keysize1_bytes, &kc1);
if (r < 0)
goto out;
}
/* volume key in file takes precedence */
if (vk_in_keyring1 && !kc1) {
r = tools_parse_vk_description(vk_in_keyring1, &vk_description);
if (r < 0)
goto out;
r = crypt_keyslot_context_init_by_vk_in_keyring(cd, vk_description, &kc1);
if (r < 0)
goto out;
}
if (vk_file2 && keysize2_bytes > 0) {
crypt_safe_free(key);
key = NULL;
r = tools_read_vk(vk_file2, &key, keysize2_bytes);
if (r < 0)
goto out;
r = crypt_keyslot_context_init_by_volume_key(cd, key, keysize2_bytes, &kc2);
if (r < 0)
goto out;
}
/* volume key in file takes precedence */
if (vk_in_keyring2 && !kc2) {
free(vk_description);
vk_description = NULL;
r = tools_parse_vk_description(vk_in_keyring2, &vk_description);
if (r < 0)
goto out;
r = crypt_keyslot_context_init_by_vk_in_keyring(cd, vk_description, &kc2);
}
out:
crypt_safe_free(key);
free(vk_description);
if (r) {
crypt_keyslot_context_free(kc1);
crypt_keyslot_context_free(kc2);
} else {
*r_kc1 = kc1;
*r_kc2 = kc2;
}
return r;
}