Debian Codegraph @

src/pins/tpm2/clevis-decrypt-tpm2

raw
1 #!/bin/bash -e
2 # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
3 #
4 # Copyright (c) 2017 Red Hat, Inc.
5 # Author: Javier Martinez Canillas <[email protected]>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 # First try to use the new version of the PIN implementation
22 if command -v clevis-pin-tpm2 >/dev/null;
23 then
24 exec clevis-pin-tpm2 decrypt $@
25 fi
26
27 # The owner hierarchy is the one that should be used by the Operating System.
28 auth="o"
29
30 function on_exit() {
31 if [ ! -d "$TMP" ] || ! rm -rf "$TMP"; then
32 echo "Delete temporary files failed!" >&2
33 echo "You need to clean up: $TMP" >&2
34 exit 1
35 fi
36 }
37
38 [ $# -eq 1 ] && [ "$1" == "--summary" ] && exit 2
39
40 if [ -t 0 ]; then
41 exec >&2
42 echo
43 echo "Usage: clevis decrypt tpm2 < JWE > PLAINTEXT"
44 echo
45 exit 2
46 fi
47
48 TPM2TOOLS_INFO="$(tpm2_createprimary -v)"
49
50 match='version="(.)\.'
51 [[ $TPM2TOOLS_INFO =~ $match ]] && TPM2TOOLS_VERSION="${BASH_REMATCH[1]}"
52 if [[ $TPM2TOOLS_VERSION -lt 3 ]] || [[ $TPM2TOOLS_VERSION -gt 5 ]]; then
53 echo "The tpm2 pin requires a tpm2-tools version between 3 and 5" >&2
54 exit 1
55 fi
56
57 if [ -z "$TPM2TOOLS_TCTI" ]; then
58 # Old environment variables for tpm2-tools 3.0
59 export TPM2TOOLS_TCTI_NAME=device
60 export TPM2TOOLS_DEVICE_FILE=
61 for dev in /dev/tpmrm?; do
62 [ -e "$dev" ] || continue
63 TPM2TOOLS_DEVICE_FILE="$dev"
64 break
65 done
66
67 # New environment variable for tpm2-tools >= 3.1
68 export TPM2TOOLS_TCTI="$TPM2TOOLS_TCTI_NAME:$TPM2TOOLS_DEVICE_FILE"
69
70 if [ -z "$TPM2TOOLS_DEVICE_FILE" ]; then
71 echo "A TPM2 device with the in-kernel resource manager is needed!" >&2
72 exit 1
73 fi
74
75 if ! [[ -r "$TPM2TOOLS_DEVICE_FILE" && -w "$TPM2TOOLS_DEVICE_FILE" ]]; then
76 echo "The $TPM2TOOLS_DEVICE_FILE device must be readable and writable!" >&2
77 exit 1
78 fi
79 fi
80
81 read -r -d . hdr
82
83 if ! jhd="$(jose b64 dec -i- <<< "$hdr")"; then
84 echo "Error decoding JWE protected header!" >&2
85 exit 1
86 fi
87
88 if [ "$(jose fmt -j- -Og clevis -g pin -u- <<< "$jhd")" != "tpm2" ]; then
89 echo "JWE pin mismatch!" >&2
90 exit 1
91 fi
92
93 if ! hash="$(jose fmt -j- -Og clevis -g tpm2 -g hash -Su- <<< "$jhd")"; then
94 echo "JWE missing required 'hash' header parameter!" >&2
95 exit 1
96 fi
97
98 if ! key="$(jose fmt -j- -Og clevis -g tpm2 -g key -Su- <<< "$jhd")"; then
99 echo "JWE missing required 'key' header parameter!" >&2
100 exit 1
101 fi
102
103 if ! jwk_pub="$(jose fmt -j- -Og clevis -g tpm2 -g jwk_pub -Su- <<< "$jhd")"; then
104 echo "JWE missing required 'jwk_pub' header parameter!" >&2
105 exit 1
106 fi
107
108 if ! jwk_priv="$(jose fmt -j- -Og clevis -g tpm2 -g jwk_priv -Su- <<< "$jhd")"; then
109 echo "JWE missing required 'jwk_priv' header parameter!" >&2
110 exit 1
111 fi
112
113 mkdir -p "${TMPDIR:-/tmp}"
114 if ! TMP="$(mktemp -d)"; then
115 echo "Creating a temporary dir for TPM files failed!" >&2
116 exit 1
117 fi
118
119 trap 'on_exit' EXIT
120
121 pcr_ids="$(jose fmt -j- -Og clevis -g tpm2 -g pcr_ids -Su- <<< "$jhd")" || true
122
123 pcr_spec=''
124 if [ -n "$pcr_ids" ]; then
125 pcr_bank="$(jose fmt -j- -Og clevis -g tpm2 -g pcr_bank -Su- <<< "$jhd")"
126 pcr_spec="$pcr_bank:$pcr_ids"
127 fi
128
129 if ! jose b64 dec -i- -O "$TMP"/jwk.pub <<< "$jwk_pub"; then
130 echo "Decoding jwk.pub from Base64 failed!" >&2
131 exit 1
132 fi
133
134 if ! jose b64 dec -i- -O "$TMP"/jwk.priv <<< "$jwk_priv"; then
135 echo "Decoding jwk.priv from Base64 failed!" >&2
136 exit 1
137 fi
138
139 case "$TPM2TOOLS_VERSION" in
140 3) tpm2_createprimary -Q -H "$auth" -g "$hash" -G "$key" -C "$TMP"/primary.context || fail=$?;;
141 4|5) tpm2_createprimary -Q -C "$auth" -g "$hash" -G "$key" -c "$TMP"/primary.context || fail=$?;;
142 *) fail=1;;
143 esac
144 if [ -n "$fail" ]; then
145 echo "Creating TPM2 primary key failed!" >&2
146 exit 1
147 fi
148 tpm2_flushcontext -t
149
150 case "$TPM2TOOLS_VERSION" in
151 3) tpm2_load -Q -c "$TMP"/primary.context -u "$TMP"/jwk.pub -r "$TMP"/jwk.priv \
152 -C "$TMP"/load.context || fail=$?;;
153 4|5) tpm2_load -Q -C "$TMP"/primary.context -u "$TMP"/jwk.pub -r "$TMP"/jwk.priv \
154 -c "$TMP"/load.context || fail=$?;;
155 *) fail=1;;
156 esac
157 if [ -n "$fail" ]; then
158 echo "Loading jwk to TPM2 failed!" >&2
159 exit 1
160 fi
161 tpm2_flushcontext -t
162
163 case "$TPM2TOOLS_VERSION" in
164 3) jwk="$(tpm2_unseal -c "$TMP"/load.context ${pcr_spec:+-L $pcr_spec})" || fail=$?;;
165 4|5) jwk="$(tpm2_unseal -c "$TMP"/load.context ${pcr_spec:+-p pcr:$pcr_spec})" || fail=$?;;
166 *) fail=1;;
167 esac
168 if [ -n "$fail" ]; then
169 echo "Unsealing jwk from TPM failed!" >&2
170 exit 1
171 fi
172 tpm2_flushcontext -t
173
174 (echo -n "$jwk$hdr."; /bin/cat) | jose jwe dec -k- -i-
175 exit $?