interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / trust1_credential_policy.sh

156 lines · 6192 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# FLAG(sovereignty): decodes systemd-creds credentials (systemd dependency), Linux host Gate 0 only; retire once credentials move off systemd-creds.
18# trust1_credential_policy.sh: show which TPM policy seals a systemd credential.
19#
20# TRUST-1 Gate 0 (G7) and Gate 6. Reads only the public header of an
21# encrypted systemd credential file (`systemd-creds encrypt` output, raw or
22# base64). It never decrypts, never contacts the TPM and never prints the
23# sealed blob or ciphertext. Output per file: sealing type, PCR mask and the
24# PCR indices it names, PCR bank, TPM primary key algorithm, and the policy
25# digest the TPM will demand (a public hash, not a secret).
26#
27# Header layout (systemd src/shared/creds-util.c, struct
28# encrypted_credential_header then tpm2_credential_header):
29# 0x00 id[16] 0x10 key_size u32 0x14 block_size u32 0x18 iv_size u32
30# 0x1c tag_size u32 0x20 iv[iv_size] padded to 8 bytes
31# then pcr_mask u64, pcr_bank u16, primary_alg u16, blob_size u32,
32# policy_hash_size u32, blob[blob_size] padded to 8, policy_hash[...]
33#
34# With --live, it also recomputes the TPM2 PolicyPCR digest from the PCR
35# values the TPM reports now (tpm2_pcrread, read-only) and prints
36# policy_matches_live_pcrs=yes|no: "yes" means this credential would unseal
37# with the current boot state, "no" means it would refuse.
38#
39# Usage: trust1_credential_policy.sh [--live] FILE... (root may be needed to
40# read /etc/credstore.encrypted; reading a header changes nothing.)
41
42set -euo pipefail
43
44live=0
45if [[ "${1:-}" == "--live" ]]; then live=1; shift; fi
46[[ $# -ge 1 ]] || { sed -n "2,24p" "$0"; exit 2; }
47
48hexdump_file() {
49 # Whole file as one lowercase hex string. Base64 credentials are decoded
50 # first; systemd writes them with line breaks.
51 local f="$1"
52 if LC_ALL=C grep -qv '^[A-Za-z0-9+/=]*$' "${f}"; then
53 od -An -v -tx1 "${f}"
54 else
55 base64 -d "${f}" | od -An -v -tx1
56 fi | tr -d ' \n'
57}
58
59# Little-endian unsigned integer of N bytes at byte offset O.
60le() {
61 local hex="$1" off="$2" n="$3" out="" i
62 for ((i = n - 1; i >= 0; i--)); do out+="${hex:$(((off + i) * 2)):2}"; done
63 echo $((16#${out}))
64}
65
66align8() { echo $((($1 + 7) / 8 * 8)); }
67
68type_name() {
69 case "$1" in
70 0c7cc07b-1176-4591-9c4b-0bea08bc20fe) echo "tpm2-only" ;;
71 93a89409-4874-4490-90ca-f2fc93cab553) echo "host+tpm2" ;;
72 *) echo "unknown" ;;
73 esac
74}
75
76bank_name() {
77 case "$1" in 4) echo sha1 ;; 11) echo sha256 ;; 12) echo sha384 ;; 13) echo sha512 ;; *) echo "alg-$1" ;; esac
78}
79
80alg_name() {
81 case "$1" in 1) echo rsa ;; 35) echo ecc ;; *) echo "alg-$1" ;; esac
82}
83
84hex_to_bin() { xxd -r -p; }
85
86# TPM2 PolicyPCR digest for a fresh session:
87# SHA256(00*32 || TPM_CC_PolicyPCR || TPML_PCR_SELECTION || SHA256(PCR values))
88# with TPML_PCR_SELECTION = count(1) || hashAlg || sizeofSelect(3) || bitmap.
89live_policy_digest() {
90 local mask="$1" bank="$2" bankname values="" i sel
91 bankname="$(bank_name "${bank}")"
92 for ((i = 0; i < 24; i++)); do
93 if (((mask >> i) & 1)); then
94 # FLAG(sovereignty): tpm2_pcrread (outside dep) on Linux host; replace with in-house C PCR reader.
95 values+="$(tpm2_pcrread "${bankname}:${i}" | awk -v i="${i}" '$1 == i && $2 == ":" { sub(/^0x/, "", $3); print tolower($3) }')"
96 fi
97 done
98 [[ -n "${values}" ]] || return 1
99 sel="$(printf '%02x%02x%02x' $((mask & 255)) $(((mask >> 8) & 255)) $(((mask >> 16) & 255)))"
100 local pcr_digest
101 pcr_digest="$(printf '%s' "${values}" | hex_to_bin | sha256sum | cut -d' ' -f1)"
102 printf '%064d%s%s%04x%s%s%s' 0 0000017f 00000001 "${bank}" 03 "${sel}" "${pcr_digest}" \
103 | hex_to_bin | sha256sum | cut -d' ' -f1
104}
105
106status=0
107for f in "$@"; do
108 hex="$(hexdump_file "${f}")"
109 if [[ ${#hex} -lt 128 ]]; then
110 echo "file=${f} error=too_short"; status=1; continue
111 fi
112 id="${hex:0:8}-${hex:8:4}-${hex:12:4}-${hex:16:4}-${hex:20:12}"
113 kind="$(type_name "${id}")"
114 iv_size="$(le "${hex}" 24 4)"
115 echo "file=${f}"
116 echo " file_sha256=$(sha256sum "${f}" | cut -d' ' -f1)"
117 echo " type_id=${id}"
118 echo " type=${kind}"
119 if [[ "${kind}" == unknown ]]; then
120 echo " error=unknown_type_id (not decoded; add the id only after checking systemd source)"
121 status=1
122 continue
123 fi
124 t=$((32 + $(align8 "${iv_size}")))
125 mask="$(le "${hex}" "${t}" 8)"
126 bank="$(le "${hex}" $((t + 8)) 2)"
127 palg="$(le "${hex}" $((t + 10)) 2)"
128 blob="$(le "${hex}" $((t + 12)) 4)"
129 phs="$(le "${hex}" $((t + 16)) 4)"
130 pcrs=""
131 for ((i = 0; i < 24; i++)); do
132 if (((mask >> i) & 1)); then pcrs+="${pcrs:+,}${i}"; fi
133 done
134 pol_off=$((t + 20 + $(align8 "${blob}")))
135 printf ' pcr_mask=0x%x\n' "${mask}"
136 echo " pcrs=${pcrs:-none}"
137 echo " pcr_bank=$(bank_name "${bank}")"
138 echo " primary_alg=$(alg_name "${palg}")"
139 echo " sealed_blob_bytes=${blob}"
140 echo " policy_hash_bytes=${phs}"
141 if [[ "${phs}" -gt 0 && "${phs}" -le 64 ]]; then
142 policy="${hex:$((pol_off * 2)):$((phs * 2))}"
143 echo " policy_hash=${policy}"
144 if [[ ${live} -eq 1 ]]; then
145 expect="$(live_policy_digest "${mask}" "${bank}")" || expect="unavailable"
146 echo " live_policy_digest=${expect}"
147 if [[ "${expect}" == "${policy}" ]]; then
148 echo " policy_matches_live_pcrs=yes"
149 else
150 echo " policy_matches_live_pcrs=no"
151 status=1
152 fi
153 fi
154 fi
155done
156exit "${status}"