eBPF-based security platform
quote-check: add a standalone checker for keepalive quotes
John Fastabend6528a9b5a4ac
diff --git a/.agents/skills/pre-submit/SKILL.md b/.agents/skills/pre-submit/SKILL.mdindex 38674750..0984ee13 100644--- a/.agents/skills/pre-submit/SKILL.md+++ b/.agents/skills/pre-submit/SKILL.md@@ -54,8 +54,10 @@ signs off..github/scripts/check-sparse.sh.github/scripts/check-spdx.shmake -C Documentation check # DOCS_CONTAINER=0 without podman or docker-make -B && make -B shirudo-build && make -B -f Makefile.shirudo test-helpers+make -B && make -B shirudo-build && make -B quote-check &&+ make -B -f Makefile.shirudo test-helpersmake -B CC=clang && make -B CC=clang shirudo-build &&+ make -B CC=clang quote-check &&make -B -f Makefile.shirudo CC=clang test-helpersmake tests VM=0 SANITIZE=address,undefined # CC=gcc and CC=clang,# one worktree eachdiff --git a/.github/scripts/build-commits.sh b/.github/scripts/build-commits.shindex 3f421d3e..de42798c 100755--- a/.github/scripts/build-commits.sh+++ b/.github/scripts/build-commits.sh@@ -5,7 +5,8 @@# .github/scripts/build-commits.sh -- build every commit in a range on its# own, oldest first, so that the history bisects as# Documentation/contribute/code/contributing.rst asks: the binary, the-# shirudo-build helper and the test helpers. The first commit that does+# shirudo-build helper, the quote checker and the test helpers. The first+# commit that does# not build is named and the ones after it are not tried.## .github/scripts/build-commits.sh [<rev-list arguments>]@@ -46,7 +47,9 @@ for c in $commits; doprintf '==== [%d/%d] %s\n' "$i" "$n" "$(describe "$c")"figit checkout -q "$c"+ # A commit from before the quote checker has no target for it.if ! { make -j"$(nproc)" && make shirudo-build &&+ { ! grep -q '^quote-check:' Makefile || make quote-check; } &&make -C testing test-helpers; }; thenif [ -n "${GITHUB_ACTIONS:-}" ]; thenprintf '::endgroup::\n'diff --git a/.github/scripts/build-debian.sh b/.github/scripts/build-debian.shindex 01ac39bc..463b67dd 100755--- a/.github/scripts/build-debian.sh+++ b/.github/scripts/build-debian.sh@@ -52,6 +52,8 @@ cc --version | head -n 1export BPF_VMLINUX_BTF="$btf"make -j"$(nproc)"make shirudo-build+make quote-checkmake -C testing unitsh .github/scripts/check-operator-linkage.sh shirudo \- tools/shirudo-build/shirudo-build+ tools/shirudo-build/shirudo-build \+ tools/quote-check/shirudo-quote-checkdiff --git a/.github/workflows/build.yml b/.github/workflows/build.ymlindex 526d5fe7..67e9c349 100644--- a/.github/workflows/build.yml+++ b/.github/workflows/build.yml@@ -52,10 +52,12 @@ jobs:. build/vm-toolchain.envmake -j4 CC=${{ matrix.cc }}make CC=${{ matrix.cc }} shirudo-build+ make CC=${{ matrix.cc }} quote-checkmake -C testing CC=${{ matrix.cc }} test-helpersmake -C testing CC=${{ matrix.cc }} unitsh .github/scripts/check-operator-linkage.sh shirudo \- tools/shirudo-build/shirudo-build+ tools/shirudo-build/shirudo-build \+ tools/quote-check/shirudo-quote-checkbtf:name: bpf-next BTFdiff --git a/.github/workflows/packages.yml b/.github/workflows/packages.ymlindex 2273a934..84c653ed 100644--- a/.github/workflows/packages.yml+++ b/.github/workflows/packages.yml@@ -121,7 +121,8 @@ jobs:apt-get install -y /host/shirudo-operator_*.debsh .github/scripts/check-operator-linkage.sh /usr/bin/shirudo \- /usr/libexec/shirudo/shirudo-build+ /usr/libexec/shirudo/shirudo-build \+ /usr/bin/shirudo-quote-check/usr/bin/shirudo genkey@@ -383,7 +384,8 @@ jobs:rpm -q shirudo-operator clangrpm -qV shirudo-operatorsh .github/scripts/check-operator-linkage.sh /usr/bin/shirudo \- /usr/libexec/shirudo/shirudo-build+ /usr/libexec/shirudo/shirudo-build \+ /usr/bin/shirudo-quote-checkshirudo genkeyrm -rf /tmp/shirudo-smoke && mkdir -p /tmp/shirudo-smokecp testing/fixtures/basic.bpf.c /tmp/shirudo-smoke/diff --git a/.gitignore b/.gitignoreindex dba1f448..eb635f20 100644--- a/.gitignore+++ b/.gitignore@@ -18,6 +18,7 @@/shirudo-operator-*.rpm/*.spdx.json/tools/shirudo-build/shirudo-build+/tools/quote-check/shirudo-quote-check*.o*.d*.adiff --git a/AGENTS.md b/AGENTS.mdindex f6cf555d..f63940a2 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -317,8 +317,9 @@ one usually still compiles and passes tests.## Building, testing, packaging-- `make` builds the operator binary, `make shirudo-build` the helper and- `make target` the fully static PIE target profile (`make target-deps`+- `make` builds the operator binary, `make shirudo-build` the helper,+ `make quote-check` the backend's quote checker `shirudo-quote-check`+ and `make target` the fully static PIE target profile (`make target-deps`builds OpenSSL, libcurl and tpm2-tss from source first). A checkoutused in place of an installed operator package names the helper with`SHIRUDO_BUILD=<path>`.diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex 8dc3bc2b..fd7ea035 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -312,10 +312,14 @@ Other make targets* - ``make install-target``- Installs the static target binary as ``shirudo``, the units andtimers, the dracut module and ``shirudo-gen-initrd``.+ * - ``make quote-check``+ - Builds ``tools/quote-check/shirudo-quote-check``, the program a+ telemetry backend runs on keepalive quotes. See+ :doc:`/reference/cli/quote-check`.* - ``make install-operator``- Installs the operator binary, the helper under- ``$(libexecdir)/shirudo``, the image scripts, ``vmlinux.h`` and- the BPF headers.+ ``$(libexecdir)/shirudo``, ``shirudo-quote-check``, the image+ scripts, ``vmlinux.h`` and the BPF headers.* - ``DESTDIR=<dir>``, ``prefix=<dir>``, ``bindir=<dir>``,``libexecdir=<dir>``, ``systemdunitdir=<dir>``- Override the install paths. The unit and dracut directories are@@ -374,6 +378,10 @@ Project layout- The operator-only helper: compile, sign, unload directives,image signing, ``keys vouch``, ``bpm build``, ``push``,``tree append`` and ``config update``.+ * - :src:`tools/quote-check/`+ - ``shirudo-quote-check``, a program of its own that checks+ keepalive quotes with the TPM parsers of :src:`util/tpm_wire.c`.+ No other binary links it.* - :src:`tools/gen-image/`- ``shirudo-gen-image``, ``shirudo-gen-runtime`` and``shirudo-gen-verity``, which make the runtime and config images,@@ -454,7 +462,8 @@ the commit at ``HEAD`` alone without them.`Coccinelle rules`_.* - ``build-commits.sh <range>``- Builds every commit on its own: ``make``, ``make- shirudo-build`` and ``make -C testing test-helpers``. It+ shirudo-build``, ``make quote-check`` and ``make -C testing+ test-helpers``. Itrefuses to start with uncommitted changes and checks out eachcommit in the working tree.* - ``install-clang.sh [<package>...]``diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex f47be16e..bedafd7b 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -597,8 +597,11 @@ The unit suites in :src:`testing/unit/` cover 29 of the modules in``paths``, ``string``, ``sysinfo``, ``upatch``, ``upatch_decode``,``upatch_pins``, ``upatch_resolve``, ``upatch_verify`` and ``xdp``, and``overflow`` the arithmetic of-:src:`include/shirudo/overflow.h`. The other modules-are reached through the test helpers, the fuzz targets and the binaries+:src:`include/shirudo/overflow.h`. ``quote_check`` covers the rows of+``shirudo-quote-check`` in :src:`tools/quote-check/row.c`, on quotes a+software P-256 key signs: every reject reason, the signer's qualified+name, the empty PCR selection and the base64 of each field. The other+modules are reached through the test helpers, the fuzz targets and the binariesthe functional tests run... list-table::diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex 6aa45696..a50d7e6c 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -708,7 +708,10 @@ A case left out of the table is an unused static function, which failsthe build. Each case runs in a child process with a 60-second limit, soa crash, a sanitizer report or a hang fails that case alone.``unit_skip()`` skips a case with a reason. The suite links the util-objects from an archive and needs no build rule of its own.+objects from an archive and needs no build rule of its own. A suite for+code outside :src:`util/`, as ``quote_check`` is for+:src:`tools/quote-check/row.c`, names the object it links in+``UNIT_OBJS`` in :src:`Makefile.shirudo`.Write a shell test~~~~~~~~~~~~~~~~~~diff --git a/Documentation/reference/cli/index.rst b/Documentation/reference/cli/index.rstindex 5c33ee6a..ec7bb8f6 100644--- a/Documentation/reference/cli/index.rst+++ b/Documentation/reference/cli/index.rst@@ -14,7 +14,9 @@ categories.command names resolve, the runtime binary a target must use, the imagegate that runs before most commands, the ``shirudo-build`` helper andthe exit statuses. :doc:`/reference/cheat-sheet` collects command lines-for common tasks.+for common tasks. :doc:`/reference/cli/gen-tools` and+:doc:`/reference/cli/quote-check` describe the programs the packages+install beside ``shirudo``... toctree:::titlesonly:@@ -41,3 +43,4 @@ for common tasks.attest-challengeimagegen-tools+ quote-checkdiff --git a/Documentation/reference/cli/quote-check.rst b/Documentation/reference/cli/quote-check.rstnew file mode 100644index 00000000..b93010f8--- /dev/null+++ b/Documentation/reference/cli/quote-check.rst@@ -0,0 +1,220 @@+..+ SPDX-License-Identifier: BSD-2-Clause+ Copyright (c) 2026 Authors of shirudo++:page-type: Reference+:audience: Operators++shirudo-quote-check+===================++Check keepalive quotes without a TPM. A keepalive quote is a+``TPM2_Quote`` that selects no PCR and carries the SHA-256 of a keepalive+body as its qualifying data. ``shirudo-quote-check`` is a program of its+own, not a ``shirudo`` command. A telemetry backend runs it as a filter:+rows in, one verdict per row out.++Runs on: a backend host, installed by the operator package as+``$(bindir)/shirudo-quote-check``. Requires: nothing. It reads no file,+keeps no state, holds no key list and opens no connection. Changes+measured state: no.++Usage+-----++.. code-block:: none++ shirudo-quote-check++The program takes no option besides ``-h`` and ``--help``, which print+the usage on standard output. It reads rows from standard input until+the input ends and writes one line to standard output for each row, in+the order of the rows. It flushes standard output after every line, so+a caller that keeps the process running gets each answer before it sends+the next row.++Input+-----++One row per line, in the TabSeparated format of ClickHouse: four fields+with a tab between them.++.. list-table::+ :header-rows: 1+ :widths: 18 82++ * - Field+ - Content+ * - ``attest``+ - The ``TPMS_ATTEST`` the TPM signed, without the ``TPM2B`` size, in+ standard base64 with padding. At most 1024 bytes.+ * - ``sig``+ - The marshalled ``TPMT_SIGNATURE`` over it, in standard base64 with+ padding. At most 160 bytes.+ * - ``ak``+ - The marshalled ``TPMT_PUBLIC`` of the attestation key, without the+ ``TPM2B`` size, in standard base64 with padding. At most 512+ bytes.+ * - ``body_sha256``+ - The SHA-256 of the keepalive body the row belongs to, as 64 hex+ digits of either case.++A base64 field is accepted only when it is the encoding of its bytes:+no white space, no line break and no missing padding. The last row may+end without a newline.++Checks+------++The program runs these checks in order. The first one that fails decides+the reason.++.. list-table::+ :header-rows: 1+ :widths: 6 70 24++ * - Step+ - Check+ - Reason on failure+ * - 1+ - The row has four fields, the three base64 fields decode within+ their sizes and ``body_sha256`` is 64 hex digits.+ - ``malformed``+ * - 2+ - ``ak`` parses as an ECC key on NIST P-256 with the ECDSA scheme+ and SHA-256.+ - ``malformed``+ * - 3+ - ``attest`` parses as a quote (``TPM_ST_ATTEST_QUOTE``) with one+ SHA-256 selection in which no PCR is set.+ - ``malformed``+ * - 4+ - The signer in ``attest`` is the qualified name of ``ak`` under the+ endorsement hierarchy: ``0x000b`` and SHA-256 over+ ``TPM_RH_ENDORSEMENT`` and the name of ``ak``.+ - ``bad_signature``+ * - 5+ - ``sig`` parses as an ECDSA signature with SHA-256.+ - ``malformed``+ * - 6+ - The signature verifies over the SHA-256 of the ``attest`` bytes+ with the key in ``ak``. A key that is no point of the curve+ verifies nothing.+ - ``bad_signature``+ * - 7+ - The qualifying data of the quote equals ``body_sha256``.+ - ``body_mismatch``++``shirudo attest`` signs its quotes with the same kind of key over a+nonce the caller chooses, and refuses an empty PCR selection. Step 3+therefore keeps an attestation quote from passing as a keepalive quote.++A row that passes proves that the body is unchanged since the key in+``ak`` signed its hash. It does not prove that the key lives in a TPM,+and the program does not decide which keys a backend accepts.+:doc:`attest-challenge` and :doc:`attest-verify` tie an attestation key+to a TPM.++Output+------++One JSON object per row, on one line. A row that passes:++.. code-block:: none++ {"ok":1,"ak_name":"<hex>","ak_public":"<base64>","reset_count":<n>,"clock":<n>}++.. list-table::+ :header-rows: 1+ :widths: 18 82++ * - Key+ - Value+ * - ``ak_name``+ - The name of the attestation key, 68 hex digits: ``000b`` and the+ SHA-256 of the ``TPMT_PUBLIC`` bytes. This is the name ``shirudo+ attest verify --ak-name`` takes.+ * - ``ak_public``+ - The ``ak`` field as given.+ * - ``reset_count``+ - ``resetCount`` of the quote's clock information, the number of TPM+ resets.+ * - ``clock``+ - ``clock`` of the quote's clock information, the TPM's count of+ milliseconds, as an unsigned 64-bit number.++``reset_count`` and ``clock`` order the quotes of one key.++A row that fails:++.. code-block:: none++ {"ok":0,"reason":"<reason>"}++.. list-table::+ :header-rows: 1+ :widths: 22 78++ * - Reason+ - Meaning+ * - ``malformed``+ - The row, a field or a structure does not parse, the key is not a+ P-256 ECDSA key, or the quote is not a quote of no PCR. A line+ longer than the longest row and a line that holds a NUL are+ ``malformed`` too.+ * - ``bad_signature``+ - The signer is not the key as an endorsement primary, or the+ signature does not verify.+ * - ``body_mismatch``+ - The signature is valid and the quote covers another body.++Exit status+-----------++.. list-table::+ :header-rows: 1+ :widths: 12 88++ * - Status+ - Meaning+ * - 0+ - The input ended. Every row has its line, whatever the verdicts.+ * - 1+ - Reading, writing or a check could not run, for example on a+ closed output or without memory. The program prints+ ``shirudo-quote-check: <reason>`` on standard error. The row it+ stopped at has no line.+ * - 2+ - An argument other than ``-h`` or ``--help``.++Examples+--------++Check one row from a file. The row is one the unit suite makes with a+software key, hence the clock:++.. code-block:: console++ $ shirudo-quote-check < row.tsv+ {"ok":1,"ak_name":"000b5df9f114316811380fc4fcba38a6d03d306cb68efed75bc004f1bf8de9f75e78","ak_public":"ACMACwAFAHIAAAAQABgACwADABAAIKjxXZuRCOfQVIxeeEc3RYztqHejfb0I4q4eQxf8XcPfACDPkDqNmcCyzRlbPKHkxo/pC3z5bzCd2jbT7BpEAy7veg==","reset_count":2309737967,"clock":18364758544493064720}++A row with three fields:++.. code-block:: console++ $ printf 'a\tb\tc\n' | shirudo-quote-check+ {"ok":0,"reason":"malformed"}++Build it in a checkout:++.. code-block:: console++ $ make quote-check+ $ tools/quote-check/shirudo-quote-check --help++Related+-------++- :doc:`attest-verify`+- :doc:`/concepts/background/tpm`+- :doc:`/reference/files`diff --git a/Documentation/reference/files.rst b/Documentation/reference/files.rstindex 8ed65630..f8c10223 100644--- a/Documentation/reference/files.rst+++ b/Documentation/reference/files.rst@@ -61,6 +61,12 @@ Operator host- none- The compiler ``shirudo-build`` executes for ``.bpf.c`` input,clang 23.+ * - ``$(bindir)/shirudo-quote-check``+ - operator package, ``make install-operator``+ - ``0755``+ - Checks keepalive quotes for a telemetry backend. It reads standard+ input alone and writes no file. See+ :doc:`/reference/cli/quote-check`.* - ``/tmp/shirudo-build-XXXXXX.bpf.o``- ``build``- nonediff --git a/Documentation/start/install.rst b/Documentation/start/install.rstindex 6f5779c9..018d9fe6 100644--- a/Documentation/start/install.rst+++ b/Documentation/start/install.rst@@ -42,6 +42,8 @@ Packages- Operator host- - ``/usr/bin/shirudo``, linked against the host's shared libraries- the helper ``/usr/libexec/shirudo/shirudo-build``+ - ``/usr/bin/shirudo-quote-check``, which checks keepalive quotes+ for a telemetry backend- the image generators ``shirudo-gen-image``,``shirudo-gen-runtime`` and ``shirudo-gen-verity`` under``/usr/libexec/shirudo/gen-image/``diff --git a/Makefile b/Makefileindex d8e98d5f..e00e9ec6 100644--- a/Makefile+++ b/Makefile@@ -51,7 +51,7 @@ export prefix exec_prefix bindir libexecdir sharedir libdir mandir completionsdisystemdunitdir udevrulesdir dracutmodulesdir SHIRUDO_TARGET_PKG_CONFIG_PATH \SHIRUDO_TARGET_STATIC_LIBDIR-.PHONY: all help target target-deps target-deps-clean shirudo-target target-config shirudo-build docs bpf-deps install install-target install-operator clean distclean config tests proofs proofs-cbmc proofs-lean4 sparse tags cscope+.PHONY: all help target target-deps target-deps-clean shirudo-target target-config shirudo-build quote-check docs bpf-deps install install-target install-operator clean distclean config tests proofs proofs-cbmc proofs-lean4 sparse tags cscopeall: config+$(Q)$(MAKE) -f Makefile.shirudo PROFILE=operator CONFIG_MK=config.mk all@@ -88,6 +88,7 @@ help:@printf '%s\n' \'shirudo make targets:' \' Build: make, make target, make target-deps, make bpf-deps, make install' \+ ' Operator tools: make shirudo-build, make quote-check' \' Local stack: make stack-up, make oci, make telemetry, make vm, make stack-down' \' Checks: make sparse' \' Source index: make tags (ctags), make cscope' \@@ -101,6 +102,9 @@ help:shirudo-build:+$(Q)$(MAKE) -C tools/shirudo-build+quote-check:+ +$(Q)$(MAKE) -C tools/quote-check+sparse: config+$(Q)$(MAKE) -f Makefile.shirudo PROFILE=operator CONFIG_MK=config.mk sparsediff --git a/Makefile.shirudo b/Makefile.shirudoindex 57929928..7bb44928 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -334,12 +334,19 @@ BUILD_SRCS := \tools/shirudo-build/vouch.c \tools/shirudo-build/image.c+# shirudo-quote-check is a backend's program: it checks keepalive quotes+# with the TPM parsers of util/ and is never linked into shirudo.+QUOTE_CHECK_SRCS := \+ tools/quote-check/quote-check.c \+ tools/quote-check/row.c+# ----- derived names ----------------------------------------------UTIL_OBJS := $(foreach s,$(UTIL_SRCS),$(call obj,$(s:.c=.o)))BUILTIN_OBJS := $(foreach s,$(BUILTIN_SRCS),$(call obj,$(s:.c=.o)))BPF_OBJS := $(BPF_SRCS:.c=.bpf.o)BUILD_OBJS := $(foreach s,$(BUILD_SRCS),$(call obj,$(s:.c=.o)))+QUOTE_CHECK_OBJS := $(foreach s,$(QUOTE_CHECK_SRCS),$(call obj,$(s:.c=.o)))MAIN_OBJ := $(call obj,shirudo.o)VERSION_OBJ := $(call obj,util/version_id.o)@@ -349,13 +356,13 @@ VERSION_OBJ := $(call obj,util/version_id.o)# whose size grew in a header but whose .c was not re-touched, producing# an ABI mismatch and memory corruption at runtime.DEP_OBJS := $(MAIN_OBJ) $(BUILTIN_OBJS) $(UTIL_OBJS) $(VERSION_OBJ) \- $(BUILD_OBJS)+ $(BUILD_OBJS) $(QUOTE_CHECK_OBJS)DEPS := $(DEP_OBJS:.o=.d)# ----- top-level goals --------------------------------------------.PHONY: all install install-common install-target install-operator clean distclean \- test-helpers verity-test-helpers unit-tests \+ test-helpers verity-test-helpers unit-tests quote-check \bpf libbpfall: shirudo@@ -364,6 +371,8 @@ all: shirudo# operator install target.shirudo-build: tools/shirudo-build/shirudo-build+quote-check: tools/quote-check/shirudo-quote-check+# ----- phase 1: vendored libbpf and the vmlinux.h generator -------LIBBPF_SRCS := $(wildcard $(LIBBPF_DIR)/*.c $(LIBBPF_DIR)/*.h \@@ -503,6 +512,14 @@ tools/shirudo-build/shirudo-build: $(BUILD_OBJS) util/sign.o util/config_image.o$(Q)$(LD) $(CFLAGS) $(HARDEN_LDFLAGS) $(LDFLAGS) $(RPATH_LDFLAGS) -o $@ $^ \$(STATIC_LIBS) $(DYNAMIC_LIBS)+tools/quote-check/shirudo-quote-check: $(QUOTE_CHECK_OBJS) util/tpm_wire.o \+ util/crypto_openssl.o util/hex.o \+ util/json.o util/io.o util/seal.o \+ util/log.o util/malloc.o util/string.o+ @printf ' LD $@\n'+ $(Q)$(LD) $(CFLAGS) $(HARDEN_LDFLAGS) $(LDFLAGS) $(RPATH_LDFLAGS) -o $@ $^ \+ $(LIBCRYPTO_LIBS)+# ----- install ----------------------------------------------------# The reboot-persistence units, with @bindir@ resolved to the install path.@@ -597,11 +614,14 @@ install-target: shirudo-target install-common systemd/shirudo.service \$(Q)install -m 0644 systemd/90-shirudo-xdp-reconcile.rules \$(DESTDIR)$(udevrulesdir)/-install-operator: install shirudo-build $(VMLINUX_H) $(BPF_LIBBPF_HEADERS) $(BPF_LIB_HEADERS)+install-operator: install shirudo-build quote-check $(VMLINUX_H) $(BPF_LIBBPF_HEADERS) $(BPF_LIB_HEADERS)@printf ' INSTALL $(DESTDIR)$(libexecdir)/shirudo/shirudo-build\n'$(Q)install -d $(DESTDIR)$(libexecdir)/shirudo$(Q)install -m 0755 tools/shirudo-build/shirudo-build \$(DESTDIR)$(libexecdir)/shirudo/shirudo-build+ @printf ' INSTALL $(DESTDIR)$(bindir)/shirudo-quote-check\n'+ $(Q)install -m 0755 tools/quote-check/shirudo-quote-check \+ $(DESTDIR)$(bindir)/shirudo-quote-check$(Q)install -d $(DESTDIR)$(libexecdir)/shirudo/gen-image$(Q)install -m 0755 tools/gen-image/shirudo-gen-* \$(DESTDIR)$(libexecdir)/shirudo/gen-image/@@ -974,11 +994,16 @@ $(UNIT_LIB): $(UTIL_OBJS) $(VERSION_OBJ)$(Q)rm -f $@$(Q)$(AR) rcs $@ $^+# The quote checker's rows are no util module: its suite links the+# object the program is built from.+build/unit/quote_check: UNIT_OBJS := tools/quote-check/row.o+build/unit/quote_check: tools/quote-check/row.o+build/unit/%: testing/unit/%.c testing/unit/unit.c testing/unit/unit.h \$(UNIT_LIB) $(LIBBPF_A)@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) $(LDFLAGS) -Itesting/unit -o $@ $< testing/unit/unit.c \- $(UNIT_LIB) $(LIBBPF_A) $(DYNAMIC_LIBS)+ $(UNIT_OBJS) $(UNIT_LIB) $(LIBBPF_A) $(DYNAMIC_LIBS)build/unit/curl: LDFLAGS += -Wl,--wrap=curl_slist_free_all@@ -999,7 +1024,8 @@ unit-tests: $(UNIT_TESTS)# compiler would. sparse predefines no __FLT_EVAL_METHOD__, which# glibc's <math.h> tests, so -Wundef is the compiler's to check.SPARSE_TYPES_H ?= /usr/include/linux/types.h-SPARSE_SRCS := shirudo.c $(BUILTIN_SRCS) $(UTIL_SRCS) $(BUILD_SRCS)+SPARSE_SRCS := shirudo.c $(BUILTIN_SRCS) $(UTIL_SRCS) $(BUILD_SRCS) \+ $(QUOTE_CHECK_SRCS).PHONY: sparsesparse:@@ -1151,8 +1177,9 @@ fuzz-targets: $(FUZZ_TARGETS)clean:$(Q)rm -f shirudo shirudo-target tools/shirudo-build/shirudo-build $(TEST_HELPERS)+ $(Q)rm -f tools/quote-check/shirudo-quote-check$(Q)rm -f $(MAIN_OBJ) $(BUILTIN_OBJS) $(UTIL_OBJS) \- $(BUILD_OBJS) $(VERSION_OBJ) util/version_id.c+ $(BUILD_OBJS) $(QUOTE_CHECK_OBJS) $(VERSION_OBJ) util/version_id.c$(Q)rm -rf build/target build/unit$(Q)rm -f $(DEPS) testing/helper/*.d$(Q)rm -f $(BPF_OBJS) $(VMLINUX_H) systemd/shirudo.service \diff --git a/packaging/Makefile b/packaging/Makefileindex 4669bf61..55b3b028 100644--- a/packaging/Makefile+++ b/packaging/Makefile@@ -146,7 +146,8 @@ bintar-pkg: config $(ROOT)/packaging/tar/install.sh# shirudo + the systemd reboot, map-export, and keepalive units# (target node)# shirudo-operator_<version>_<arch>.deb-# shirudo + shirudo-build + BPF headers, no unit+# shirudo + shirudo-build + shirudo-quote-check + BPF headers,+# no unit# Rebuilds from clean with prefix=$(PKG_PREFIX) because the libexec path is# compiled into the binary and make cannot see the changed -D otherwise.bindeb-pkg:diff --git a/packaging/deb/builddeb.sh b/packaging/deb/builddeb.shindex d25a688d..cca05d5f 100755--- a/packaging/deb/builddeb.sh+++ b/packaging/deb/builddeb.sh@@ -48,7 +48,8 @@ target)operator)pkg=shirudo-operatorother=shirudo-target- bins="$stage/usr/bin/shirudo $stage/usr/libexec/shirudo/shirudo-build"+ bins="$stage/usr/bin/shirudo $stage/usr/libexec/shirudo/shirudo-build+ $stage/usr/bin/shirudo-quote-check"short="signed-BPF lockdown LSM platform (operator tools)";;*)@@ -97,7 +98,8 @@ mkdir -p "$stage/DEBIAN"" compiles and CMS-signs .upatch artifacts. It is for the machine that holds" \" the private signing key and produces artifacts for direct target loading;" \" it does not install the" \-" target systemd units."+" target systemd units. It also installs shirudo-quote-check, which checks" \+" keepalive quotes for a telemetry backend."fi} > "$stage/DEBIAN/control"diff --git a/packaging/rpm/buildrpm.sh b/packaging/rpm/buildrpm.shindex e37097d2..6538b72e 100755--- a/packaging/rpm/buildrpm.sh+++ b/packaging/rpm/buildrpm.sh@@ -105,7 +105,8 @@ dirs=$(cd "$stage" && find . -mindepth 1 -type d -path '*/shirudo*' | sed 's#^\."the operator-side tooling: the shirudo binary plus shirudo-build, which" \"compiles and CMS-signs .upatch artifacts. It is for the machine that holds" \"the private signing key and produces artifacts for direct target loading;" \-"it does not install the target systemd units."+"it does not install the target systemd units. It also installs" \+"shirudo-quote-check, which checks keepalive quotes for a telemetry backend."fiprintf '\n%%install\n'printf 'cp -a %s/. %%{buildroot}/\n' "$stage"diff --git a/testing/Makefile b/testing/Makefileindex cf204bb5..1c257bc5 100644--- a/testing/Makefile+++ b/testing/Makefile@@ -333,6 +333,7 @@ BUILD_LOCK := $(ROOT)/build/.testing-build.lockbuild-operator: build-unit+$(Q)flock $(BUILD_LOCK) $(MAKE) -C $(ROOT) shirudo-build+ +$(Q)flock $(BUILD_LOCK) $(MAKE) -C $(ROOT) quote-check+$(Q)flock $(BUILD_LOCK) $(MAKE) -C $(ROOT) -f Makefile.shirudo test-helpersbuild-unit:diff --git a/testing/unit/quote_check.c b/testing/unit/quote_check.cnew file mode 100644index 00000000..5c457423--- /dev/null+++ b/testing/unit/quote_check.c@@ -0,0 +1,538 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The rows of shirudo-quote-check, on quotes a software P-256 key signs+ * in place of a TPM's attestation key.+ */++#include <stdbool.h>+#include <string.h>++#include <openssl/bn.h>+#include <openssl/core_names.h>+#include <openssl/ecdsa.h>+#include <openssl/evp.h>++#include <shirudo/byteorder.h>+#include <shirudo/string.h>++#include "crypto.h"+#include "hex.h"+#include "tools/quote-check/row.h"+#include "unit.h"++#define TPM_ECC_NIST_P384 0x0004u+#define TPM_AK_ATTRS 0x00050072u+#define TPM_SIGNATURE_LEN (2 + 2 + 2 + 32 + 2 + 32)++#define ATTEST_MAGIC_OFF 0+#define ATTEST_CLOCK_OFF (4 + 2 + 2 + TPM_NAME_LEN + 2 + TPM_DIGEST_LEN)+#define SIG_ALG_OFF 0+#define SIG_R_OFF (2 + 2 + 2)+#define SIG_S_OFF (SIG_R_OFF + 32 + 2)++#define QUOTE_CLOCK 0xfedcba9876543210ull+#define QUOTE_RESET_COUNT 0x89abcdefu+#define QUOTE_RESTART_COUNT 3+#define QUOTE_FIRMWARE 0x2026111100000001ull++static const char keepalive_body[] = "{\"schema\":\"shirudo.keepalive/1\"}";++/* What a test sets before quote_sign() makes the bytes of a row from it. */+struct quote {+ struct tpm_public ak;+ bool signer_is_plain_name;+ __u8 extra[64];+ size_t extra_len;+ __u32 pcr_mask;+ bool certifies_nv;++ __u8 ak_public[TPM_PUBLIC_MAX];+ size_t ak_public_len;+ __u8 ak_name[TPM_NAME_LEN];+ __u8 attest[TPM_ATTEST_MAX];+ size_t attest_len;+ __u8 sig[TPM_SIGNATURE_LEN];+ char body_sha256[QUOTE_BODY_HEX_LEN + 1];+};++static EVP_PKEY *software_ak(void)+{+ static EVP_PKEY *key;++ if (!key)+ key = EVP_EC_gen("P-256");+ ASSERT_TRUE(key);+ return key;+}++static void software_ak_point(__u8 x[32], __u8 y[32])+{+ BIGNUM *bx = NULL, *by = NULL;++ ASSERT_EQ(EVP_PKEY_get_bn_param(software_ak(),+ OSSL_PKEY_PARAM_EC_PUB_X, &bx), 1);+ ASSERT_EQ(EVP_PKEY_get_bn_param(software_ak(),+ OSSL_PKEY_PARAM_EC_PUB_Y, &by), 1);+ ASSERT_EQ(BN_bn2binpad(bx, x, 32), 32);+ ASSERT_EQ(BN_bn2binpad(by, y, 32), 32);+ BN_free(bx);+ BN_free(by);+}++static void software_ak_sign(const __u8 digest[CRYPTO_SHA256_LEN],+ __u8 r[32], __u8 s[32])+{+ EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(software_ak(), NULL);+ size_t der_len = 128;+ const __u8 *p;+ ECDSA_SIG *sig;+ __u8 der[128];++ ASSERT_TRUE(ctx);+ ASSERT_TRUE(EVP_PKEY_sign_init(ctx) == 1);+ ASSERT_TRUE(EVP_PKEY_sign(ctx, der, &der_len, digest,+ CRYPTO_SHA256_LEN) == 1);+ p = der;+ sig = d2i_ECDSA_SIG(NULL, &p, (long)der_len);+ ASSERT_TRUE(sig);+ ASSERT_EQ(BN_bn2binpad(ECDSA_SIG_get0_r(sig), r, 32), 32);+ ASSERT_EQ(BN_bn2binpad(ECDSA_SIG_get0_s(sig), s, 32), 32);+ ECDSA_SIG_free(sig);+ EVP_PKEY_CTX_free(ctx);+}++/* A quote of no PCR over the body's SHA-256, by an endorsement primary. */+static void quote_init(struct quote *q)+{+ memset(q, 0, sizeof(*q));+ q->ak.type = TPM_ALG_ECC;+ q->ak.attrs = TPM_AK_ATTRS;+ q->ak.sym_alg = TPM_ALG_NULL;+ q->ak.scheme = TPM_ALG_ECDSA;+ q->ak.scheme_hash = TPM_ALG_SHA256;+ q->ak.curve = TPM_ECC_NIST_P256;+ software_ak_point(q->ak.x, q->ak.y);+ q->extra_len = CRYPTO_SHA256_LEN;+ ASSERT_EQ(sha256_buffer(keepalive_body, strlen(keepalive_body),+ q->extra), 0);+ hex_encode(q->extra, CRYPTO_SHA256_LEN, q->body_sha256);+}++static size_t put_be64_at(__u8 *p, __u64 v)+{+ put_be32(p, (__u32)(v >> 32));+ put_be32(p + 4, (__u32)v);+ return 8;+}++static void quote_marshal_attest(struct quote *q, const __u8 *signer)+{+ static const __u8 digest[TPM_DIGEST_LEN] = { 0xe3, 0xb0, 0xc4 };+ __u8 *p = q->attest;++ put_be32(p, TPM_GENERATED);+ p += 4;+ put_be16(p, q->certifies_nv ? TPM_ST_ATTEST_NV : TPM_ST_ATTEST_QUOTE);+ p += 2;+ p += tpm_put2b(p, signer, TPM_NAME_LEN);+ p += tpm_put2b(p, q->extra, q->extra_len);+ p += put_be64_at(p, QUOTE_CLOCK);+ put_be32(p, QUOTE_RESET_COUNT);+ put_be32(p + 4, QUOTE_RESTART_COUNT);+ p += 8;+ *p++ = 1;+ p += put_be64_at(p, QUOTE_FIRMWARE);+ if (q->certifies_nv) {+ p += tpm_put2b(p, signer, TPM_NAME_LEN);+ put_be16(p, 0);+ p += 2;+ p += tpm_put2b(p, digest, sizeof(digest));+ q->attest_len = (size_t)(p - q->attest);+ return;+ }+ put_be32(p, 1);+ put_be16(p + 4, TPM_ALG_SHA256);+ p += 6;+ *p++ = TPM_PCR_SELECT_LEN;+ *p++ = (__u8)q->pcr_mask;+ *p++ = (__u8)(q->pcr_mask >> 8);+ *p++ = (__u8)(q->pcr_mask >> 16);+ p += tpm_put2b(p, digest, sizeof(digest));+ q->attest_len = (size_t)(p - q->attest);+}++static void quote_sign(struct quote *q)+{+ __u8 digest[CRYPTO_SHA256_LEN], qualified[TPM_NAME_LEN];+ __u8 r[32], s[32], *p = q->sig;++ ASSERT_EQ(tpm_public_marshal(&q->ak, q->ak_public,+ sizeof(q->ak_public),+ &q->ak_public_len), 0);+ ASSERT_EQ(tpm_object_name(q->ak_public, q->ak_public_len,+ q->ak_name), 0);+ ASSERT_EQ(tpm_qualified_name(TPM_RH_ENDORSEMENT, q->ak_name,+ qualified), 0);+ quote_marshal_attest(q, q->signer_is_plain_name ? q->ak_name :+ qualified);+ ASSERT_EQ(sha256_buffer(q->attest, q->attest_len, digest), 0);+ software_ak_sign(digest, r, s);+ put_be16(p, TPM_ALG_ECDSA);+ put_be16(p + 2, TPM_ALG_SHA256);+ p += 4;+ p += tpm_put2b(p, r, 32);+ tpm_put2b(p, s, 32);+}++static void quote_row(const struct quote *q, char *row, size_t cap)+{+ char attest[BASE64_LEN(TPM_ATTEST_MAX) + 1];+ char sig[BASE64_LEN(TPM_SIGNATURE_LEN) + 1];+ char ak[BASE64_LEN(TPM_PUBLIC_MAX) + 1];++ ASSERT_EQ(base64_encode(q->attest, q->attest_len, attest,+ sizeof(attest)), 0);+ ASSERT_EQ(base64_encode(q->sig, sizeof(q->sig), sig, sizeof(sig)), 0);+ ASSERT_EQ(base64_encode(q->ak_public, q->ak_public_len, ak,+ sizeof(ak)), 0);+ ASSERT_TRUE(strsprintf(row, cap, "%s\t%s\t%s\t%s", attest, sig, ak,+ q->body_sha256) > 0);+}++static enum quote_verdict row_verdict(const char *row)+{+ struct quote_result res;++ ASSERT_EQ(quote_row_check(row, strlen(row), &res), 0);+ return res.verdict;+}++static enum quote_verdict quote_verdict(const struct quote *q)+{+ char row[QUOTE_ROW_MAX + 1];++ quote_row(q, row, sizeof(row));+ return row_verdict(row);+}++static void row_line(const char *row, char *line, size_t cap)+{+ struct quote_result res;++ ASSERT_EQ(quote_row_check(row, strlen(row), &res), 0);+ ASSERT_EQ(quote_row_emit(&res, line, cap), 0);+}++static void good_row_is_accepted(void)+{+ char row[QUOTE_ROW_MAX + 1], want[QUOTE_LINE_MAX], line[QUOTE_LINE_MAX];+ char ak_public[BASE64_LEN(TPM_PUBLIC_MAX) + 1];+ __u8 name[TPM_NAME_LEN] = { 0x00, 0x0b };+ char ak_name[2 * TPM_NAME_LEN + 1];+ struct quote_result res;+ struct quote q;++ quote_init(&q);+ quote_sign(&q);+ quote_row(&q, row, sizeof(row));+ ASSERT_EQ(quote_row_check(row, strlen(row), &res), 0);+ ASSERT_EQ(res.verdict, QUOTE_OK);+ ASSERT_EQ(sha256_buffer(q.ak_public, q.ak_public_len, name + 2), 0);+ EXPECT_MEMEQ(res.ak_name, name, sizeof(name));+ ASSERT_UEQ(res.ak_public_len, q.ak_public_len);+ EXPECT_MEMEQ(res.ak_public, q.ak_public, q.ak_public_len);+ EXPECT_UEQ(res.reset_count, QUOTE_RESET_COUNT);+ EXPECT_UEQ(res.clock, QUOTE_CLOCK);++ hex_encode(name, sizeof(name), ak_name);+ ASSERT_EQ(base64_encode(q.ak_public, q.ak_public_len, ak_public,+ sizeof(ak_public)), 0);+ ASSERT_TRUE(strsprintf(want, sizeof(want),+ "{\"ok\":1,\"ak_name\":\"%s\",\"ak_public\":\"%s\",\"reset_count\":%u,\"clock\":%llu}",+ ak_name, ak_public, QUOTE_RESET_COUNT,+ QUOTE_CLOCK) > 0);+ ASSERT_EQ(quote_row_emit(&res, line, sizeof(line)), 0);+ EXPECT_STREQ(line, want);+}++static void rejects_are_one_reason_each(void)+{+ char row[QUOTE_ROW_MAX + 1], line[QUOTE_LINE_MAX];+ struct quote q;++ row_line("", line, sizeof(line));+ EXPECT_STREQ(line, "{\"ok\":0,\"reason\":\"malformed\"}");++ quote_init(&q);+ q.signer_is_plain_name = true;+ quote_sign(&q);+ quote_row(&q, row, sizeof(row));+ row_line(row, line, sizeof(line));+ EXPECT_STREQ(line, "{\"ok\":0,\"reason\":\"bad_signature\"}");++ quote_init(&q);+ quote_sign(&q);+ q.body_sha256[0] = q.body_sha256[0] == '0' ? '1' : '0';+ quote_row(&q, row, sizeof(row));+ row_line(row, line, sizeof(line));+ EXPECT_STREQ(line, "{\"ok\":0,\"reason\":\"body_mismatch\"}");+}++static void changed_attest_or_signature_is_a_bad_signature(void)+{+ struct quote q;++ quote_init(&q);+ quote_sign(&q);+ q.attest[ATTEST_CLOCK_OFF + 7] ^= 0x01;+ EXPECT_EQ(quote_verdict(&q), QUOTE_BAD_SIGNATURE);++ quote_init(&q);+ quote_sign(&q);+ q.sig[SIG_R_OFF + 31] ^= 0x01;+ EXPECT_EQ(quote_verdict(&q), QUOTE_BAD_SIGNATURE);++ quote_init(&q);+ quote_sign(&q);+ q.sig[SIG_S_OFF + 31] ^= 0x01;+ EXPECT_EQ(quote_verdict(&q), QUOTE_BAD_SIGNATURE);+}++static void changed_magic_or_signature_header_is_malformed(void)+{+ struct quote q;++ quote_init(&q);+ quote_sign(&q);+ q.attest[ATTEST_MAGIC_OFF] ^= 0x01;+ EXPECT_EQ(quote_verdict(&q), QUOTE_MALFORMED);++ quote_init(&q);+ quote_sign(&q);+ q.sig[SIG_ALG_OFF + 1] ^= 0x01;+ EXPECT_EQ(quote_verdict(&q), QUOTE_MALFORMED);+}++static void other_body_sha256_is_a_body_mismatch(void)+{+ struct quote q;++ quote_init(&q);+ quote_sign(&q);+ q.body_sha256[63] = q.body_sha256[63] == 'f' ? 'e' : 'f';+ EXPECT_EQ(quote_verdict(&q), QUOTE_BODY_MISMATCH);+}++static void signed_extra_of_another_body_is_a_body_mismatch(void)+{+ struct quote q;++ quote_init(&q);+ q.extra[0] ^= 0x01;+ quote_sign(&q);+ EXPECT_EQ(quote_verdict(&q), QUOTE_BODY_MISMATCH);++ quote_init(&q);+ q.extra_len = 20;+ quote_sign(&q);+ EXPECT_EQ(quote_verdict(&q), QUOTE_BODY_MISMATCH);++ quote_init(&q);+ q.extra_len = 0;+ quote_sign(&q);+ EXPECT_EQ(quote_verdict(&q), QUOTE_BODY_MISMATCH);+}++static void plain_name_as_signer_is_a_bad_signature(void)+{+ struct quote q;++ quote_init(&q);+ q.signer_is_plain_name = true;+ quote_sign(&q);+ EXPECT_EQ(quote_verdict(&q), QUOTE_BAD_SIGNATURE);+}++static void quote_over_pcrs_is_malformed(void)+{+ struct quote q;++ quote_init(&q);+ q.pcr_mask = 1U << 9 | 1U << 11;+ quote_sign(&q);+ EXPECT_EQ(quote_verdict(&q), QUOTE_MALFORMED);++ quote_init(&q);+ q.pcr_mask = 1U << 23;+ quote_sign(&q);+ EXPECT_EQ(quote_verdict(&q), QUOTE_MALFORMED);+}++static void nv_certification_is_malformed(void)+{+ struct quote q;++ quote_init(&q);+ q.certifies_nv = true;+ quote_sign(&q);+ EXPECT_EQ(quote_verdict(&q), QUOTE_MALFORMED);+}++static void key_that_is_not_p256_ecdsa_is_malformed(void)+{+ struct quote q;++ quote_init(&q);+ q.ak.curve = TPM_ECC_NIST_P384;+ quote_sign(&q);+ EXPECT_EQ(quote_verdict(&q), QUOTE_MALFORMED);++ quote_init(&q);+ q.ak.scheme = TPM_ALG_NULL;+ quote_sign(&q);+ EXPECT_EQ(quote_verdict(&q), QUOTE_MALFORMED);++ quote_init(&q);+ q.ak.type = TPM_ALG_RSA;+ q.ak.scheme = TPM_ALG_NULL;+ q.ak.rsa_bits = 2048;+ q.ak.n_len = 256;+ q.ak.n[0] = 0x80;+ quote_sign(&q);+ EXPECT_EQ(quote_verdict(&q), QUOTE_MALFORMED);+}++static void key_off_the_curve_is_a_bad_signature(void)+{+ struct quote q;++ quote_init(&q);+ q.ak.x[31] ^= 0x01;+ quote_sign(&q);+ EXPECT_EQ(quote_verdict(&q), QUOTE_BAD_SIGNATURE);+}++/* Each of the three base64 fields, by the tabs before it. */+static char *row_field(char *row, int tabs)+{+ while (tabs--)+ row = strchr(row, '\t') + 1;+ return row;+}++static void base64_that_is_not_standard_is_malformed(void)+{+ char row[QUOTE_ROW_MAX + 2], *pad;+ struct quote q;+ int field;++ quote_init(&q);+ quote_sign(&q);+ quote_row(&q, row, sizeof(row));+ ASSERT_EQ(row_verdict(row), QUOTE_OK);++ for (field = 0; field < 3; field++) {+ char *text = row_field(row, field), was = *text;++ *text = '*';+ EXPECT_EQ(row_verdict(row), QUOTE_MALFORMED);+ *text = ' ';+ EXPECT_EQ(row_verdict(row), QUOTE_MALFORMED);+ *text = '-';+ EXPECT_EQ(row_verdict(row), QUOTE_MALFORMED);+ *text = was;+ }+ ASSERT_EQ(row_verdict(row), QUOTE_OK);++ /* The signature is 72 bytes, so nothing pads it: cut one digit. */+ pad = row_field(row, 2) - 2;+ memmove(pad, pad + 1, strlen(pad));+ EXPECT_EQ(row_verdict(row), QUOTE_MALFORMED);++ quote_row(&q, row, sizeof(row));+ pad = row_field(row, 2) - 1;+ memmove(pad + 1, pad, strlen(pad) + 1);+ *pad = '\n';+ EXPECT_EQ(row_verdict(row), QUOTE_MALFORMED);++ /*+ * The key is 88 bytes: its last digit holds two bits of it and+ * four that a decoder drops, so the next digit decodes the same.+ */+ quote_row(&q, row, sizeof(row));+ pad = strchr(row_field(row, 2), '=');+ ASSERT_TRUE(pad && pad[1] == '=' && pad[2] == '\t');+ pad[-1]++;+ EXPECT_EQ(row_verdict(row), QUOTE_MALFORMED);+}++static void row_that_does_not_split_is_malformed(void)+{+ char row[QUOTE_ROW_MAX + 8], *tab;+ struct quote q;+ size_t len;++ quote_init(&q);+ quote_sign(&q);+ quote_row(&q, row, sizeof(row));+ len = strlen(row);++ row[len] = '\t';+ row[len + 1] = '\0';+ EXPECT_EQ(row_verdict(row), QUOTE_MALFORMED);+ row[len] = '\n';+ EXPECT_EQ(row_verdict(row), QUOTE_MALFORMED);+ row[len - 1] = '\0';+ EXPECT_EQ(row_verdict(row), QUOTE_MALFORMED);++ quote_row(&q, row, sizeof(row));+ row[len - 1] = 'g';+ EXPECT_EQ(row_verdict(row), QUOTE_MALFORMED);++ quote_row(&q, row, sizeof(row));+ tab = strchr(row, '\t');+ *tab = ' ';+ EXPECT_EQ(row_verdict(row), QUOTE_MALFORMED);++ EXPECT_EQ(row_verdict(""), QUOTE_MALFORMED);+ EXPECT_EQ(row_verdict("\t\t\t"), QUOTE_MALFORMED);+ EXPECT_EQ(row_verdict("\t\t\t\t"), QUOTE_MALFORMED);+}++static void field_longer_than_its_structure_is_malformed(void)+{+ char row[BASE64_LEN(TPM_ATTEST_MAX) + 8 + QUOTE_ROW_MAX];+ char one[QUOTE_ROW_MAX + 1], *rest;+ struct quote q;++ quote_init(&q);+ quote_sign(&q);+ quote_row(&q, one, sizeof(one));+ rest = strchr(one, '\t');+ memset(row, 'A', BASE64_LEN(TPM_ATTEST_MAX) + 4);+ ASSERT_TRUE(strscpy(row + BASE64_LEN(TPM_ATTEST_MAX) + 4, rest,+ sizeof(row) - BASE64_LEN(TPM_ATTEST_MAX) - 4) > 0);+ EXPECT_EQ(row_verdict(row), QUOTE_MALFORMED);+}++static const struct unit_case cases[] = {+ UNIT_CASE(good_row_is_accepted),+ UNIT_CASE(rejects_are_one_reason_each),+ UNIT_CASE(changed_attest_or_signature_is_a_bad_signature),+ UNIT_CASE(changed_magic_or_signature_header_is_malformed),+ UNIT_CASE(other_body_sha256_is_a_body_mismatch),+ UNIT_CASE(signed_extra_of_another_body_is_a_body_mismatch),+ UNIT_CASE(plain_name_as_signer_is_a_bad_signature),+ UNIT_CASE(quote_over_pcrs_is_malformed),+ UNIT_CASE(nv_certification_is_malformed),+ UNIT_CASE(key_that_is_not_p256_ecdsa_is_malformed),+ UNIT_CASE(key_off_the_curve_is_a_bad_signature),+ UNIT_CASE(base64_that_is_not_standard_is_malformed),+ UNIT_CASE(row_that_does_not_split_is_malformed),+ UNIT_CASE(field_longer_than_its_structure_is_malformed),+ {}+};++UNIT_SUITE(quote_check, cases);diff --git a/tools/quote-check/Makefile b/tools/quote-check/Makefilenew file mode 100644index 00000000..cb1989c9--- /dev/null+++ b/tools/quote-check/Makefile@@ -0,0 +1,22 @@+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo++ROOT := $(abspath ../..)++ifeq ($(V),1)+ Q =+else+ Q = @+endif+export Q V++.PHONY: all clean quote-check++all: quote-check++quote-check:+ +$(Q)$(MAKE) -C $(ROOT) config+ +$(Q)$(MAKE) -C $(ROOT) -f Makefile.shirudo quote-check++clean:+ $(Q)rm -f shirudo-quote-check *.o *.ddiff --git a/tools/quote-check/quote-check.c b/tools/quote-check/quote-check.cnew file mode 100644index 00000000..fc949f41--- /dev/null+++ b/tools/quote-check/quote-check.c@@ -0,0 +1,96 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * shirudo-quote-check: rows of keepalive quotes on standard input, one+ * JSON verdict per row on standard output, for as long as rows come.+ */++#include <errno.h>+#include <stdio.h>+#include <string.h>++#include "io.h"+#include "malloc.h"+#include "row.h"++static void usage(FILE *fp)+{+ fprintf(fp,+ "usage: shirudo-quote-check\n"+ "\n"+ "Reads TabSeparated rows from standard input until it ends:\n"+ " <attest>\\t<sig>\\t<ak>\\t<body_sha256>\n"+ "attest, sig and ak are a keepalive quote's TPMS_ATTEST and\n"+ "TPMT_SIGNATURE and the signing key's TPMT_PUBLIC, in standard\n"+ "base64. body_sha256 is the SHA-256 of the record's body, 64\n"+ "hex digits. Writes one JSON line per row, in order:\n"+ " {\"ok\":1,\"ak_name\":\"<hex>\",\"ak_public\":\"<base64>\",\n"+ " \"reset_count\":<n>,\"clock\":<n>}\n"+ " {\"ok\":0,\"reason\":\"bad_signature\"|\"body_mismatch\"|\n"+ " \"malformed\"}\n");+}++/* got is what read_text_line() returned for line, never 0. */+static int line_check(const char *line, int got, struct quote_result *res)+{+ size_t len;++ /* A NUL in the line, or more bytes than the longest row has. */+ if (got == -EINVAL || got == -E2BIG) {+ *res = (struct quote_result){ .verdict = QUOTE_MALFORMED };+ return 0;+ }+ if (got < 0)+ return got;+ len = strlen(line);+ if (len && line[len - 1] == '\n')+ len--;+ return quote_row_check(line, len, res);+}++static int verdict_write(const struct quote_result *res)+{+ char json[QUOTE_LINE_MAX];+ int ret;++ ret = quote_row_emit(res, json, sizeof(json));+ if (ret)+ return ret;+ /* The caller waits for each row's answer before it sends the next. */+ if (puts(json) == EOF || fflush(stdout) == EOF)+ return -EIO;+ return 0;+}++int main(int argc, char **argv)+{+ struct quote_result res;+ char *line = NULL;+ size_t cap = 0;+ int got, ret = 0;++ if (argc == 2 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))) {+ usage(stdout);+ return 0;+ }+ if (argc != 1) {+ usage(stderr);+ return 2;+ }++ do {+ got = read_text_line(stdin, &line, &cap, QUOTE_ROW_MAX);+ if (!got)+ break;+ ret = line_check(line, got, &res);+ if (!ret)+ ret = verdict_write(&res);+ } while (!ret);+ libc_free(line);+ if (ret) {+ fprintf(stderr, "shirudo-quote-check: %s\n", strerror(-ret));+ return 1;+ }+ return 0;+}diff --git a/tools/quote-check/row.c b/tools/quote-check/row.cnew file mode 100644index 00000000..6f941fb8--- /dev/null+++ b/tools/quote-check/row.c@@ -0,0 +1,249 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * The checks a backend runs on a keepalive quote: the attestation key+ * signed it as an endorsement primary, and it covers the SHA-256 of the+ * body the record carries.+ */++#include <errno.h>+#include <stdbool.h>+#include <string.h>++#include <shirudo/string.h>++#include "crypto.h"+#include "hex.h"+#include "json.h"+#include "row.h"++enum {+ FIELD_ATTEST,+ FIELD_SIG,+ FIELD_AK,+ FIELD_BODY_SHA256,+ FIELD_COUNT,+};++struct field {+ const char *text;+ size_t len;+};++static bool row_split(const char *row, size_t len,+ struct field fields[FIELD_COUNT])+{+ size_t i;++ for (i = 0; i < FIELD_COUNT; i++) {+ const char *tab = memchr(row, '\t', len);+ bool last = i == FIELD_COUNT - 1;++ if (last != !tab)+ return false;+ fields[i].text = row;+ fields[i].len = tab ? (size_t)(tab - row) : len;+ if (tab) {+ len -= fields[i].len + 1;+ row = tab + 1;+ }+ }+ return true;+}++/*+ * OpenSSL's decoder skips white space and stops at a '-', so only a+ * field that encodes back to itself is standard base64 with padding.+ */+static int base64_field(const struct field *field,+ __u8 *out, size_t cap, size_t *len)+{+ __u8 bytes[BASE64_LEN(TPM_ATTEST_MAX) / 4 * 3];+ char again[BASE64_LEN(TPM_ATTEST_MAX) + 1];++ if (field->len > BASE64_LEN(cap) ||+ base64_decode(field->text, field->len, bytes, sizeof(bytes),+ len) ||+ *len > cap ||+ base64_encode(bytes, *len, again, sizeof(again)) ||+ strlen(again) != field->len ||+ memcmp(again, field->text, field->len))+ return -EINVAL;+ memcpy(out, bytes, *len);+ return 0;+}++static int body_sha256_field(const struct field *field,+ __u8 out[TPM_DIGEST_LEN])+{+ char hex[QUOTE_BODY_HEX_LEN + 1];++ if (field->len != QUOTE_BODY_HEX_LEN)+ return -EINVAL;+ memcpy(hex, field->text, QUOTE_BODY_HEX_LEN);+ hex[QUOTE_BODY_HEX_LEN] = '\0';+ return hex_decode(hex, out, TPM_DIGEST_LEN);+}++static bool ak_is_p256_ecdsa(const struct tpm_public *ak)+{+ return ak->type == TPM_ALG_ECC && ak->curve == TPM_ECC_NIST_P256 &&+ ak->scheme == TPM_ALG_ECDSA &&+ ak->scheme_hash == TPM_ALG_SHA256;+}++/*+ * shirudo attest signs its quotes with the same key over a nonce the+ * caller chooses and refuses an empty PCR selection, so the empty one+ * is what makes a quote a keepalive's.+ */+static bool attest_is_keepalive_quote(const struct tpm_attest *attest)+{+ return attest->type == TPM_ST_ATTEST_QUOTE && !attest->pcr_mask;+}++static int signer_is_endorsement_primary(const struct tpm_attest *attest,+ const __u8 ak_name[TPM_NAME_LEN],+ bool *is)+{+ __u8 qualified[TPM_NAME_LEN];+ int ret;++ ret = tpm_qualified_name(TPM_RH_ENDORSEMENT, ak_name, qualified);+ if (ret)+ return ret;+ *is = attest->signer_len == TPM_NAME_LEN &&+ !memcmp(attest->signer, qualified, TPM_NAME_LEN);+ return 0;+}++/*+ * 0 with *valid, or a negative errno. A key that is no point of the+ * curve, -EINVAL, verifies nothing, like a signature that does not+ * match.+ */+static int signature_verify(const struct tpm_public *ak,+ const __u8 *attest, size_t attest_len,+ const __u8 r[32], const __u8 s[32], bool *valid)+{+ __u8 digest[CRYPTO_SHA256_LEN];+ int ret;++ ret = sha256_buffer(attest, attest_len, digest);+ if (ret)+ return ret;+ ret = ecdsa_p256_verify(ak->x, ak->y, digest, r, s);+ if (ret && ret != -EKEYREJECTED && ret != -EINVAL)+ return ret;+ *valid = !ret;+ return 0;+}++int quote_row_check(const char *row, size_t len, struct quote_result *res)+{+ __u8 attest[TPM_ATTEST_MAX], sig[TPM_SIGNATURE_MAX];+ __u8 body_sha256[TPM_DIGEST_LEN], r[32], s[32];+ struct field fields[FIELD_COUNT];+ size_t attest_len, sig_len;+ struct tpm_attest quote;+ struct tpm_public ak;+ bool passed;+ int ret;++ memset(res, 0, sizeof(*res));+ res->verdict = QUOTE_MALFORMED;+ if (!row_split(row, len, fields) ||+ base64_field(&fields[FIELD_ATTEST], attest, sizeof(attest),+ &attest_len) ||+ base64_field(&fields[FIELD_SIG], sig, sizeof(sig), &sig_len) ||+ base64_field(&fields[FIELD_AK], res->ak_public,+ sizeof(res->ak_public), &res->ak_public_len) ||+ body_sha256_field(&fields[FIELD_BODY_SHA256], body_sha256))+ return 0;+ if (tpm_public_parse(res->ak_public, res->ak_public_len, &ak) ||+ !ak_is_p256_ecdsa(&ak))+ return 0;+ if (tpm_attest_parse(attest, attest_len, "e) ||+ !attest_is_keepalive_quote("e))+ return 0;++ ret = tpm_object_name(res->ak_public, res->ak_public_len,+ res->ak_name);+ if (!ret)+ ret = signer_is_endorsement_primary("e, res->ak_name,+ &passed);+ if (ret)+ return ret;+ if (!passed) {+ res->verdict = QUOTE_BAD_SIGNATURE;+ return 0;+ }++ if (tpm_ecdsa_signature_parse(sig, sig_len, r, s))+ return 0;+ ret = signature_verify(&ak, attest, attest_len, r, s, &passed);+ if (ret)+ return ret;+ if (!passed) {+ res->verdict = QUOTE_BAD_SIGNATURE;+ return 0;+ }++ if (quote.extra_len != sizeof(body_sha256) ||+ memcmp(quote.extra, body_sha256, sizeof(body_sha256))) {+ res->verdict = QUOTE_BODY_MISMATCH;+ return 0;+ }+ res->verdict = QUOTE_OK;+ res->reset_count = quote.reset_count;+ res->clock = quote.clock;+ return 0;+}++static const char *const reject_reason[] = {+ [QUOTE_BAD_SIGNATURE] = "bad_signature",+ [QUOTE_BODY_MISMATCH] = "body_mismatch",+ [QUOTE_MALFORMED] = "malformed",+};++static int emit_accept(struct json_emit *e, const struct quote_result *res)+{+ char ak_name[2 * TPM_NAME_LEN + 1], clock[24];+ char ak_public[BASE64_LEN(TPM_PUBLIC_MAX) + 1];+ int ret;++ ret = base64_encode(res->ak_public, res->ak_public_len, ak_public,+ sizeof(ak_public));+ if (ret)+ return ret;+ /* json_kv_int() takes a signed value and a clock has all 64 bits. */+ ret = strsprintf(clock, sizeof(clock), "%llu", res->clock);+ if (ret < 0)+ return ret;+ hex_encode(res->ak_name, sizeof(res->ak_name), ak_name);+ json_kv_int(e, "ok", 1);+ json_kv_string(e, "ak_name", ak_name);+ json_kv_string(e, "ak_public", ak_public);+ json_kv_int(e, "reset_count", res->reset_count);+ json_kv_raw(e, "clock", clock, (size_t)ret);+ return 0;+}++int quote_row_emit(const struct quote_result *res, char *line, size_t cap)+{+ struct json_emit e = { .buf = line, .cap = cap };+ int ret = 0;++ json_obj_open(&e);+ if (res->verdict == QUOTE_OK) {+ ret = emit_accept(&e, res);+ } else if ((size_t)res->verdict < ARRAY_SIZE(reject_reason)) {+ json_kv_int(&e, "ok", 0);+ json_kv_string(&e, "reason", reject_reason[res->verdict]);+ } else {+ ret = -EINVAL;+ }+ json_obj_close(&e);+ return ret ? ret : e.err;+}diff --git a/tools/quote-check/row.h b/tools/quote-check/row.hnew file mode 100644index 00000000..17f2f145--- /dev/null+++ b/tools/quote-check/row.h@@ -0,0 +1,58 @@+/* SPDX-License-Identifier: BSD-2-Clause */+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * One row of shirudo-quote-check: a keepalive quote and the SHA-256 of+ * the body it must cover, checked without a TPM, a key list or state.+ */++#ifndef SHIRUDO_QUOTE_CHECK_ROW_H+#define SHIRUDO_QUOTE_CHECK_ROW_H++#include <stddef.h>++#include <shirudo/misc.h>++#include "tpm.h"++#define BASE64_LEN(n) (4 * (((n) + 2) / 3))+#define QUOTE_BODY_HEX_LEN (2 * TPM_DIGEST_LEN)+/* attest, sig, ak and body_sha256 with the three tabs between them. */+#define QUOTE_ROW_MAX \+ (BASE64_LEN(TPM_ATTEST_MAX) + BASE64_LEN(TPM_SIGNATURE_MAX) + \+ BASE64_LEN(TPM_PUBLIC_MAX) + QUOTE_BODY_HEX_LEN + 3)+#define QUOTE_LINE_MAX 1024++enum quote_verdict {+ QUOTE_OK,+ QUOTE_BAD_SIGNATURE,+ QUOTE_BODY_MISMATCH,+ QUOTE_MALFORMED,+};++/* What follows the verdict describes a QUOTE_OK row alone. */+struct quote_result {+ enum quote_verdict verdict;+ __u8 ak_name[TPM_NAME_LEN];+ __u8 ak_public[TPM_PUBLIC_MAX];+ size_t ak_public_len;+ __u32 reset_count;+ __u64 clock;+};++/*+ * The len bytes at row are one TabSeparated row without its newline.+ * 0 with the verdict in *res, whatever the row holds, or a negative+ * errno when the check itself could not run and there is no verdict.+ */+int quote_row_check(const char *row, size_t len,+ struct quote_result *res) __must_check;++/*+ * The verdict as its JSON object, without a newline, in the cap bytes at+ * line; QUOTE_LINE_MAX holds any of them. 0 or a negative errno.+ */+int quote_row_emit(const struct quote_result *res,+ char *line, size_t cap) __must_check;++#endif /* SHIRUDO_QUOTE_CHECK_ROW_H */