|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -xe |
| 4 | + |
| 5 | +SECRET_PATH=${SECRET_PATH:=default/machine/root} |
| 6 | +KEY=${KEY:=/opt/confidential-containers/kbs/user-keys/private.key} |
| 7 | + |
| 8 | + |
| 9 | +## set reference values for TPM |
| 10 | +for i in {7,14}; do |
| 11 | + value=$(sudo tpm2_pcrread sha256:${i} | awk -F: '/0x/ {sub(/.*0x/, "", $2); gsub(/[^0-9A-Fa-f]/, "", $2); print tolower($2)}') |
| 12 | + podman exec -ti kbs-client \ |
| 13 | + kbs-client config \ |
| 14 | + --auth-private-key ${KEY} \ |
| 15 | + set-sample-reference-value tpm_pcr${i} "${value}" |
| 16 | +done |
| 17 | + |
| 18 | +# Check reference values |
| 19 | +podman exec -ti kbs-client \ |
| 20 | + kbs-client config \ |
| 21 | + --auth-private-key ${KEY} \ |
| 22 | + get-reference-values |
| 23 | + |
| 24 | + |
| 25 | +# Create attestation policy |
| 26 | +## This policy allows access only if the system’s TPM or SNP |
| 27 | +## hardware measurements match trusted reference values |
| 28 | +cat << 'EOF' > A_policy.rego |
| 29 | +package policy |
| 30 | +import rego.v1 |
| 31 | +
|
| 32 | +default hardware := 97 |
| 33 | +default executables := 3 |
| 34 | +default configuration := 2 |
| 35 | +
|
| 36 | +##### TPM |
| 37 | +
|
| 38 | +hardware := 2 if { |
| 39 | + input.tpm.pcr07 in data.reference.tpm_pcr7 |
| 40 | + input.tpm.pcr14 in data.reference.tpm_pcr14 |
| 41 | +} |
| 42 | +
|
| 43 | +hardware := 2 if { |
| 44 | + input.snp.reported_tcb_snp == 27 |
| 45 | +} |
| 46 | +
|
| 47 | +
|
| 48 | +##### Final decision |
| 49 | +result := { |
| 50 | + "executables": executables, |
| 51 | + "hardware": hardware, |
| 52 | + "configuration": configuration |
| 53 | +} |
| 54 | +EOF |
| 55 | + |
| 56 | +podman cp A_policy.rego kbs-client:/A_policy.rego |
| 57 | +podman exec -ti kbs-client \ |
| 58 | + kbs-client config \ |
| 59 | + --auth-private-key ${KEY} \ |
| 60 | + set-attestation-policy \ |
| 61 | + --policy-file /A_policy.rego \ |
| 62 | + --type rego --id default_cpu |
| 63 | + |
| 64 | +# Upload resource |
| 65 | +cat > secret << EOF |
| 66 | +{ "key_type": "oct", "key": "2b442dd5db4478367729ef8bbf2e7480" } |
| 67 | +EOF |
| 68 | +podman cp secret kbs-client:/secret |
| 69 | +podman exec -ti kbs-client \ |
| 70 | + kbs-client config \ |
| 71 | + --auth-private-key ${KEY} \ |
| 72 | + set-resource --resource-file /secret \ |
| 73 | + --path ${SECRET_PATH} |
| 74 | + |
| 75 | + |
| 76 | +# Create resource policy |
| 77 | +## This policy allows access only if both CPUs report an "affirming" status |
| 78 | +## and provide TPM and SNP attestation evidence. |
| 79 | +cat << 'EOF' > R_policy.rego |
| 80 | +package policy |
| 81 | +import rego.v1 |
| 82 | +
|
| 83 | +default allow = false |
| 84 | +
|
| 85 | +allow if { |
| 86 | + input["submods"]["cpu0"]["ear.status"] == "affirming" |
| 87 | + input["submods"]["cpu1"]["ear.status"] == "affirming" |
| 88 | + input["submods"]["cpu1"]["ear.veraison.annotated-evidence"]["tpm"] |
| 89 | + input["submods"]["cpu0"]["ear.veraison.annotated-evidence"]["snp"] |
| 90 | +} |
| 91 | +EOF |
| 92 | + |
| 93 | +podman cp R_policy.rego kbs-client:/R_policy.rego |
| 94 | +podman exec -ti kbs-client \ |
| 95 | + kbs-client config \ |
| 96 | + --auth-private-key ${KEY} \ |
| 97 | + set-resource-policy \ |
| 98 | + --policy-file /R_policy.rego \ |
0 commit comments