Extract zfs-tpm*-clear-key to very common wrapper

This commit is contained in:
наб 2020-10-25 02:51:34 +02:00
parent 675a0c40b7
commit 8653f24924
No known key found for this signature in database
GPG Key ID: BCFD0B018D2658F1
3 changed files with 44 additions and 57 deletions

View File

@ -1,37 +1,15 @@
/* SPDX-License-Identifier: MIT */
#include <libzfs.h>
#include <stdio.h>
#include "../main.hpp"
#include "../main_clear.hpp"
#include "../tpm1x.hpp"
#include "../zfs.hpp"
#define THIS_BACKEND "TPM1.X"
int main(int argc, char ** argv) {
return do_main(
argc, argv, "", "", [&](auto) {},
[&](auto dataset) {
REQUIRE_KEY_LOADED(dataset);
char * handle_s{};
TRY_MAIN(parse_key_props(dataset, THIS_BACKEND, handle_s));
tpm1x_handle handle{}; // Not like we use this, but for symmetry with the other -clear-keys
TRY_MAIN(tpm1x_parse_handle(zfs_get_name(dataset), handle_s, handle));
if(zfs_crypto_rewrap(dataset, TRY_PTR("get clear rewrap args", clear_rewrap_args()), B_FALSE))
return __LINE__; // Error printed by libzfs
TRY_MAIN(clear_key_props(dataset));
return 0;
});
tpm1x_handle handle{}; // Not like we use this, but for symmetry with the other -clear-keys
return do_clear_main(
argc, argv, THIS_BACKEND, [&](auto dataset, auto handle_s) { return parse_key_props(dataset, THIS_BACKEND, handle_s); }, [&] { return 0; });
}

View File

@ -1,42 +1,17 @@
/* SPDX-License-Identifier: MIT */
#include <libzfs.h>
#include <stdio.h>
#include "../main.hpp"
#include "../main_clear.hpp"
#include "../tpm2.hpp"
#include "../zfs.hpp"
#define THIS_BACKEND "TPM2"
int main(int argc, char ** argv) {
return do_main(
argc, argv, "", "", [&](auto) {},
[&](auto dataset) {
REQUIRE_KEY_LOADED(dataset);
char * persistent_handle_s{};
TRY_MAIN(parse_key_props(dataset, THIS_BACKEND, persistent_handle_s));
TPMI_DH_PERSISTENT persistent_handle{};
TRY_MAIN(tpm2_parse_handle(zfs_get_name(dataset), persistent_handle_s, persistent_handle));
if(zfs_crypto_rewrap(dataset, TRY_PTR("get clear rewrap args", clear_rewrap_args()), B_FALSE))
return __LINE__; // Error printed by libzfs
TRY_MAIN(with_tpm2_session([&](auto tpm2_ctx, auto tpm2_session) {
TRY_MAIN(tpm2_free_persistent(tpm2_ctx, tpm2_session, persistent_handle));
return 0;
}));
TRY_MAIN(clear_key_props(dataset));
return 0;
});
TPMI_DH_PERSISTENT persistent_handle{};
return do_clear_main(
argc, argv, THIS_BACKEND,
[&](auto dataset, auto persistent_handle_s) { return tpm2_parse_handle(zfs_get_name(dataset), persistent_handle_s, persistent_handle); },
[&] { return with_tpm2_session([&](auto tpm2_ctx, auto tpm2_session) { return tpm2_free_persistent(tpm2_ctx, tpm2_session, persistent_handle); }); });
}

34
src/main_clear.hpp Normal file
View File

@ -0,0 +1,34 @@
/* SPDX-License-Identifier: MIT */
#pragma once
#include "main.hpp"
#include "zfs.hpp"
template <class H, class F>
int do_clear_main(int argc, char ** argv, const char * this_backend, H && handlefn, F && freefn) {
return do_main(
argc, argv, "", "", [&](auto) {},
[&](auto dataset) {
REQUIRE_KEY_LOADED(dataset);
char * handle_s{};
TRY_MAIN(parse_key_props(dataset, this_backend, handle_s));
TRY_MAIN(handlefn(dataset, handle_s));
if(zfs_crypto_rewrap(dataset, TRY_PTR("get clear rewrap args", clear_rewrap_args()), B_FALSE))
return __LINE__; // Error printed by libzfs
TRY_MAIN(freefn());
TRY_MAIN(clear_key_props(dataset));
return 0;
});
}