From 76424950e373d3b04ac3dd13019151bfba3e8423 Mon Sep 17 00:00:00 2001 From: sumuel Date: Mon, 17 Aug 2026 20:44:55 +0000 Subject: Add the files --- src/modules/tpm/option.h | 9 +++++ src/modules/tpm/tpm.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ src/modules/tpm/tpm.h | 11 ++++++ 3 files changed, 118 insertions(+) create mode 100644 src/modules/tpm/option.h create mode 100644 src/modules/tpm/tpm.c create mode 100644 src/modules/tpm/tpm.h (limited to 'src/modules/tpm') diff --git a/src/modules/tpm/option.h b/src/modules/tpm/option.h new file mode 100644 index 0000000..d40806f --- /dev/null +++ b/src/modules/tpm/option.h @@ -0,0 +1,9 @@ +#pragma once + +#include "common/option.h" + +typedef struct FFTPMOptions { + FFModuleArgs moduleArgs; +} FFTPMOptions; + +static_assert(sizeof(FFTPMOptions) <= FF_OPTION_MAX_SIZE, "FFTPMOptions size exceeds maximum allowed size"); diff --git a/src/modules/tpm/tpm.c b/src/modules/tpm/tpm.c new file mode 100644 index 0000000..ecdd386 --- /dev/null +++ b/src/modules/tpm/tpm.c @@ -0,0 +1,98 @@ +#include "common/printing.h" +#include "common/jsonconfig.h" +#include "common/strutil.h" +#include "detection/tpm/tpm.h" +#include "modules/tpm/tpm.h" + +bool ffPrintTPM(FFTPMOptions* options) { + FFTPMResult result = { + .version = ffStrbufCreate(), + .description = ffStrbufCreate() + }; + const char* error = ffDetectTPM(&result); + + if (error) { + ffPrintError(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error); + return false; + } + + if (options->moduleArgs.outputFormat.length == 0) { + ffPrintLogoAndKey(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); + if (result.description.length > 0) { + ffStrbufPutTo(&result.description, stdout); + } else { + ffStrbufPutTo(&result.version, stdout); + } + } else { + FF_PRINT_FORMAT_CHECKED(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) { + FF_ARG(result.version, "version"), + FF_ARG(result.description, "description"), + })); + } + + ffStrbufDestroy(&result.version); + ffStrbufDestroy(&result.description); + + return true; +} + +void ffParseTPMJsonObject(FFTPMOptions* options, yyjson_val* module) { + yyjson_val *key, *val; + size_t idx, max; + yyjson_obj_foreach (module, idx, max, key, val) { + if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) { + continue; + } + + ffPrintError(FF_TPM_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key)); + } +} + +void ffGenerateTPMJsonConfig(FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs); +} + +bool ffGenerateTPMJsonResult(FF_A_UNUSED FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { + FFTPMResult result = { + .version = ffStrbufCreate(), + .description = ffStrbufCreate() + }; + const char* error = ffDetectTPM(&result); + + if (error) { + yyjson_mut_obj_add_str(doc, module, "error", error); + return false; + } + + yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); + yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version); + yyjson_mut_obj_add_strbuf(doc, obj, "description", &result.description); + + ffStrbufDestroy(&result.version); + ffStrbufDestroy(&result.description); + + return true; +} + +void ffInitTPMOptions(FFTPMOptions* options) { + ffOptionInitModuleArg(&options->moduleArgs, ""); +} + +void ffDestroyTPMOptions(FFTPMOptions* options) { + ffOptionDestroyModuleArg(&options->moduleArgs); +} + +FFModuleBaseInfo ffTPMModuleInfo = { + .name = FF_TPM_MODULE_NAME, + .description = "Print information about the Trusted Platform Module (TPM) security device", + .initOptions = (void*) ffInitTPMOptions, + .destroyOptions = (void*) ffDestroyTPMOptions, + .parseJsonObject = (void*) ffParseTPMJsonObject, + .printModule = (void*) ffPrintTPM, + .generateJsonResult = (void*) ffGenerateTPMJsonResult, + .generateJsonConfig = (void*) ffGenerateTPMJsonConfig, + .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) { + { "TPM device version", "version" }, + { "TPM general description", "description" }, + })) +}; diff --git a/src/modules/tpm/tpm.h b/src/modules/tpm/tpm.h new file mode 100644 index 0000000..215f8c9 --- /dev/null +++ b/src/modules/tpm/tpm.h @@ -0,0 +1,11 @@ +#pragma once + +#include "option.h" + +#define FF_TPM_MODULE_NAME "TPM" + +bool ffPrintTPM(FFTPMOptions* options); +void ffInitTPMOptions(FFTPMOptions* options); +void ffDestroyTPMOptions(FFTPMOptions* options); + +extern FFModuleBaseInfo ffTPMModuleInfo; -- cgit v1.2.3