~nabijaczleweli/tzpfms

8653f2492461da8aee92fc3f115e33f975500a8a — наб a month ago 675a0c4
Extract zfs-tpm*-clear-key to very common wrapper
3 files changed, 44 insertions(+), 57 deletions(-)

M src/bin/zfs-tpm1x-clear-key.cpp
M src/bin/zfs-tpm2-clear-key.cpp
A src/main_clear.hpp
M src/bin/zfs-tpm1x-clear-key.cpp => src/bin/zfs-tpm1x-clear-key.cpp +4 -26
@@ 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; });
}

M src/bin/zfs-tpm2-clear-key.cpp => src/bin/zfs-tpm2-clear-key.cpp +6 -31
@@ 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); }); });
}

A src/main_clear.hpp => src/main_clear.hpp +34 -0
@@ 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;
	    });
}