Debian Codegraph @

src/pins/tpm1/clevis-encrypt-tpm1

raw symbols SCIP
1 #!/bin/bash -e
2 # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
3 #
4 # Copyright (c) 2024 Oldřich Jedlička
5 #
6 # Author: Oldřich Jedlička <[email protected]>
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #
21
22 SUMMARY="Encrypts using a TPM1.2 chip binding policy"
23
24 if [ "$1" == "--summary" ]; then
25 echo "$SUMMARY"
26 exit 0
27 fi
28
29 if [ -t 0 ]; then
30 exec >&2
31 echo
32 echo "Usage: clevis encrypt tpm1 CONFIG < PLAINTEXT > JWE"
33 echo
34 echo "$SUMMARY"
35 echo
36 echo "This command uses the following configuration properties:"
37 echo
38 echo " pcr_ids: <string> PCR list used for policy. If not present, no policy is used"
39 echo
40 exit 2
41 fi
42
43 validate_pcrs() {
44 local _pcr_bank="${1}"
45 local _pcrs="${2}"
46 local _pcr
47 [ -z "${_pcr_bank}" ] && return 1
48 [ -z "${_pcrs}" ] && return 0
49
50 if [ -z "$TSS_TCSD_PORT" ]; then
51 for _pcr in ${_pcrs//,/ }; do
52 [ -f "/sys/class/tpm/tpm0/pcr-${_pcr_bank}/${_pcr}" ] || return 1
53 done
54 else
55 local pcr_args="${_pcrs:+-p}${_pcrs//,/ -p}"
56 [ "${_pcr_bank}" = "sha1" ] || return 1
57 tpm_sealdata ${pcr_args} -z </dev/null >/dev/null 2>&1 || return 1
58 fi
59
60 return 0
61 }
62
63 tpm_version_bin="$(command -v tpm_version || echo /usr/sbin/tpm_version)"
64 if ! "$tpm_version_bin" >/dev/null 2>&1; then
65 # The tpm_version outputs garbage to stdout on success, so let the
66 # tpm_version output the error again cleanly now
67 echo "The tpm1 pin requires tcsd daemon (trousers) running:" >&2
68 if [ -x "$tpm_version_bin" ]; then
69 ( "$tpm_version_bin" 2>&1 | tr '\0' ' ' ) >&2
70 else
71 echo Cannot check, tpm_version from tpm-tools not found >&2
72 fi
73 exit 1
74 fi
75
76 if ! cfg="$(jose fmt -j "$1" -Oo- 2>/dev/null)"; then
77 echo "Configuration is malformed!" >&2
78 exit 1
79 fi
80
81 # TPM1.1 and TPM1.2 has only sha1
82 pcr_bank="sha1"
83
84 # Trim the spaces from the config, so that we will not have issues parsing
85 # the PCR IDs.
86 pcr_cfg=${cfg//[[:space:]]/}
87 # Issue #103: We support passing pcr_ids using both a single string, as in
88 # "1,3", as well as an actual JSON array, such as ["1","3"]. Let's handle both
89 # cases here.
90 if jose fmt -j- -Og pcr_ids 2>/dev/null <<< "${pcr_cfg}" \
91 && ! pcr_ids="$(jose fmt -j- -Og pcr_ids -u- 2>/dev/null \
92 <<< "${pcr_cfg}")"; then
93
94 # We failed to parse a string, so let's try to parse a JSON array instead.
95 if jose fmt -j- -Og pcr_ids -A 2>/dev/null <<< "${pcr_cfg}"; then
96 # OK, it is an array, so let's get the items and form a string.
97 pcr_ids=
98 for pcr in $(jose fmt -j- -Og pcr_ids -Af- <<< "${pcr_cfg}" \
99 | tr -d '"'); do
100 pcr_ids=$(printf '%s,%s' "${pcr_ids}" "${pcr}")
101 done
102 # Now let's remove the leading comma.
103 pcr_ids=${pcr_ids/#,/}
104 else
105 # Not to add a policy that was not intended, in this case, no policy
106 # at all, let's report the issue and exit.
107 echo "Parsing the requested PCRs failed!" >&2
108 exit 1
109 fi
110 fi
111
112 if ! validate_pcrs "${pcr_bank}" "${pcr_ids}"; then
113 echo "Unable to validate combination of PCR bank '${pcr_bank}' and PCR IDs '${pcr_ids}'." >&2
114 exit 1
115 fi
116
117 if ! jwk="$(jose jwk gen -i '{"alg":"A256GCM"}')"; then
118 echo "Generating a jwk failed!" >&2
119 exit 1
120 fi
121
122 pcr_args="${pcr_ids:+-p}${pcr_ids//,/ -p}"
123 if ! jwk_sealed=$(tpm_sealdata -i /dev/stdin -o /dev/stdout ${pcr_args} -z <<< "$jwk"); then
124 echo "Unable to seal jwk" >&2
125 exit 1
126 fi
127
128 if ! jwk_b64="$(jose b64 enc -I- <<< "$jwk_sealed")"; then
129 echo "Encoding sealed jwk in Base64 failed!" >&2
130 exit 1
131 fi
132
133 jwe='{"protected":{"clevis":{"pin":"tpm1","tpm1":{}}}}'
134 if [ -n "$pcr_ids" ]; then
135 jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm1 -q "$pcr_ids" -s pcr_ids -UUUUo-)"
136 fi
137 jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm1 -q "$jwk_b64" -s jwk -UUUUo-)"
138
139 exec jose jwe enc -i- -k- -I- -c < <(echo -n "$jwe$jwk"; /bin/cat)