Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add systemd support #6

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/cel_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ CEL_Parse_IMA_TEMPLATE_Event(
size_t len,
size_t *offset);

CEL_RC
CEL_Parse_SYSTEMD_Event(
CEL_PARSE_CONTEXT *ctx,
TPMS_CEL_EVENT *event,
const uint8_t *buffer,
size_t len,
size_t *offset);
#endif
21 changes: 21 additions & 0 deletions include/cel_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef BYTE CEL_TYPE;
#define CEL_TYPE_PCCLIENT_STD 5
#define CEL_TYPE_IMA_TEMPLATE 7
#define CEL_TYPE_IMA_TLV 8
#define CEL_TYPE_SYSTEMD 11

#define CEL_TYPE_MGMT_CEL_VERSION 1
#define CEL_TYPE_MGMT_FIRMWARE_END 2
Expand All @@ -48,7 +49,18 @@ typedef BYTE CEL_TYPE;
#define CEL_STATE_TRANS_HIBERNATE 1
#define CEL_STATE_TRANS_KEXEC 2

#define CEL_TYPE_SYSTEMD_EVENT_TYPE 0
#define CEL_TYPE_SYSTEMD_TIMESTAMP 1
#define CEL_TYPE_SYSTEMD_BOOT_ID 2
#define CEL_TYPE_SYSTEMD_STRING 3

#define CEL_TYPE_SYSTEMD_EVENT_PHASE 0
#define CEL_TYPE_SYSTEMD_EVENT_FILESYSTEM 1
#define CEL_TYPE_SYSTEMD_EVENT_VOLUME_KEY 2
#define CEL_TYPE_SYSTEMD_EVENT_MACHINE_ID 3

typedef CEL_TYPE TPMI_CEL_CONTENT_TYPE;
typedef CEL_TYPE TPMI_SYSTEMD_EVENTS;

typedef UINT32 TPMI_PC_CLIENT_EVENTS;
typedef UINT64 RECNUM;
Expand Down Expand Up @@ -99,12 +111,21 @@ struct TPML_EVENT_CELMGT {
TPMS_EVENT_CELMGT events[16];
};

typedef struct TPMS_EVENT_SYSTEMD TPMS_EVENT_SYSTEMD;
struct TPMS_EVENT_SYSTEMD {
TPMI_SYSTEMD_EVENTS event_type;
UINT64 timestamp;
BYTE boot_id[16];
BYTEBUFFER string;
};

typedef union TPMU_EVENT_CONTENT TPMU_EVENT_CONTENT;
union TPMU_EVENT_CONTENT {
TPML_EVENT_CELMGT celmgt;
TPMS_EVENT_PCCLIENT_STD pcclient_std;
TPMS_EVENT_IMA_TEMPLATE ima_template;
IMA_TLV ima_tlv;
TPMS_EVENT_SYSTEMD systemd;
};

typedef struct TPMS_CEL_EVENT TPMS_CEL_EVENT;
Expand Down
5 changes: 1 addition & 4 deletions include/meson.build
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Copyright (c) 2022 by Erik Larsson
# SPDX-License-Identifier: GPL-3.0-or-later

headers = ['cel_types.h', 'cel_parse.h', 'cel_tlv.h']
if json_dep.found()
headers += 'cel_json.h'
endif
headers = ['cel_types.h', 'cel_parse.h', 'cel_tlv.h', 'cel_json.h']
install_headers(headers, subdir: 'cel')
6 changes: 2 additions & 4 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ project(
license: 'GPL3',
default_options : ['warning_level=3']
)
dependencies = [dependency('tss2-mu')]
json_dep = dependency('json-c')
test_dep = [dependency('cmocka'), dependency('libcrypto')]
json_test_dep = [dependency('cmocka'), dependency('json-c')]
dependencies = [dependency('tss2-mu'), dependency('json-c')]
test_dep = [dependency('cmocka'), dependency('libcrypto'), dependency('json-c')]

inc = include_directories('include')

Expand Down
212 changes: 212 additions & 0 deletions src/cel_json_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/* Copyright (c) 2024 by Erik Larsson
SPDX-License-Identifier: GPL-3.0-or-later
*/

#include <json-c/json_object.h>
#include <string.h>
#include <stdlib.h>
#include "cel_types.h"
#include "cel_strings.h"
#include "cel_io.h"

CEL_RC
get_json_number(
const json_object *obj,
const char *key,
uint64_t *dest)
{
json_object *ji = NULL;

CHECK_NULL(obj);
CHECK_NULL(key);
CHECK_NULL(dest);

ji = json_object_object_get(obj, key);
if (!ji) {
return CEL_RC_INVALID_VALUE;
}

if (!json_object_is_type(ji, json_type_int)) {
return CEL_RC_INVALID_TYPE;
}

*dest = json_object_get_uint64(ji);
return CEL_RC_SUCCESS;
}

CEL_RC
get_json_handle(
const json_object *obj,
TPM2_HANDLE *dest)
{
uint64_t ti;
json_object *ji = NULL;

CHECK_NULL(obj);
CHECK_NULL(dest);

if (!json_object_object_get_ex(obj, "pcr", &ji) &&
!json_object_object_get_ex(obj, "nv_index", &ji)) {
return CEL_RC_INVALID_VALUE;
}

if (!json_object_is_type(ji, json_type_int)) {
return CEL_RC_INVALID_TYPE;
}

ti = json_object_get_uint64(ji);
if (ti > UINT32_MAX) {
return CEL_RC_INVALID_VALUE;
}

*dest = ti;
return CEL_RC_SUCCESS;
}

CEL_RC
get_json_bytebuffer(
const json_object *obj,
const char *key,
BYTEBUFFER *dest)
{
int hasit;
json_object *js = NULL;
const char *ts = NULL;
size_t sl = 0;

hasit = json_object_object_get_ex(obj, key, &js);
if (!hasit) {
return CEL_RC_INVALID_VALUE;
}

if (!json_object_is_type(js, json_type_string)) {
return CEL_RC_INVALID_TYPE;
}
ts = json_object_get_string(js);
if (!ts) {
return CEL_RC_INVALID_TYPE;
}

sl = strlen(ts);
if (sl > sizeof(dest->buffer)) {
return CEL_RC_SHORT_BUFFER;
}

memcpy(dest->buffer, ts, sl);
dest->size = sl;

return CEL_RC_SUCCESS;
}

CEL_RC
get_json_hex_string_full(
const json_object *obj,
const char *key,
uint8_t *dest,
size_t len)
{
CEL_RC r;
int hasit;
json_object *js = NULL;
const char *ts = NULL;
int expected_len = len * 2;

hasit = json_object_object_get_ex(obj, key, &js);
if (!hasit) {
return CEL_RC_INVALID_VALUE;
}

if (!json_object_is_type(js, json_type_string)) {
return CEL_RC_INVALID_TYPE;
}
if (json_object_get_string_len(js) != expected_len) {
return CEL_RC_INVALID_VALUE;
}
ts = json_object_get_string(js);
if (!ts) {
return CEL_RC_INVALID_TYPE;
}

r = unhexlify(ts, dest, len);
return r;
}

CEL_RC
get_json_content_type(
const json_object *obj,
CEL_TYPE *dest)
{
CEL_RC r;
int hasit;
json_object *jt = NULL;
const char *ts = NULL;
uint64_t ti = 0;

hasit = json_object_object_get_ex(obj, "content_type", &jt);
if (!hasit) {
return CEL_RC_INVALID_VALUE;
}

switch (json_object_get_type(jt)) {
case json_type_int:
ti = json_object_get_uint64(jt);
if (ti > UINT8_MAX) {
return CEL_RC_INVALID_VALUE;
}
*dest = ti;
break;
case json_type_string:
ts = json_object_get_string(jt);
r = str_to_content_type(ts, dest);
if (r) {
return r;
}
break;
default:
return CEL_RC_INVALID_TYPE;
}

return CEL_RC_SUCCESS;
}

CEL_RC
put_json_hex_string(
json_object *obj,
const char *key,
const uint8_t *src,
size_t len)
{
CEL_RC r;
int jr;
char *buf;
size_t buflen = (len * 2) + 1;
json_object *jf = NULL;

buf = malloc(buflen);
if (!buf) {
return CEL_RC_MEMORY;
}

r = hexlify(src, len, buf, buflen);
if (r) {
goto out;
}

jf = json_object_new_string(buf);
if (!jf) {
r = CEL_RC_MEMORY;
goto out;
}

jr = json_object_object_add(obj, key, jf);
if (jr) {
r = CEL_RC_MEMORY;
json_object_put(jf);
goto out;
}

r = CEL_RC_SUCCESS;
out:
free(buf);
return r;
}
47 changes: 47 additions & 0 deletions src/cel_json_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Copyright (c) 2024 by Erik Larsson
SPDX-License-Identifier: GPL-3.0-or-later
*/

#include <json-c/json_object.h>
#include "cel_types.h"

#ifndef _CEL_JSON_UTILS_H_
#define _CEL_JSON_UTILS_H_

CEL_RC
get_json_number(
const json_object *obj,
const char *key,
uint64_t *dest);

CEL_RC
get_json_handle(
const json_object *obj,
TPM2_HANDLE *dest);

CEL_RC
get_json_bytebuffer(
const json_object *obj,
const char *key,
BYTEBUFFER *dest);

CEL_RC
get_json_hex_string_full(
const json_object *obj,
const char *key,
uint8_t *dest,
size_t len);

CEL_RC
get_json_content_type(
const json_object *obj,
CEL_TYPE *dest);

CEL_RC
put_json_hex_string(
json_object *obj,
const char *key,
const uint8_t *src,
size_t len);

#endif
Loading
Loading