summaryrefslogtreecommitdiffstats
path: root/src/modules/tpm/tpm.c
blob: ecdd386094bd3016322f3fb41e81ba5a87375983 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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" },
    }))
};