| 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 encrypt "$@" |
| 25 |
fi |
| 26 |
|
| 27 |
SUMMARY="Encrypts using a TPM2.0 chip binding policy" |
| 28 |
# The owner hierarchy is the one that should be used by the Operating System. |
| 29 |
auth="o" |
| 30 |
# Algorithm type must be keyedhash for object with user provided sensitive data. |
| 31 |
alg_create_key="keyedhash" |
| 32 |
# Attributes for the created TPM2 object with the JWK as sensitive data. |
| 33 |
obj_attr="fixedtpm|fixedparent|noda|adminwithpolicy" |
| 34 |
|
| 35 |
function on_exit() { |
| 36 |
if [ ! -d "$TMP" ] || ! rm -rf "$TMP"; then |
| 37 |
echo "Delete temporary files failed!" >&2 |
| 38 |
echo "You need to clean up: $TMP" >&2 |
| 39 |
exit 1 |
| 40 |
fi |
| 41 |
} |
| 42 |
|
| 43 |
if [ "$1" == "--summary" ]; then |
| 44 |
echo "$SUMMARY" |
| 45 |
exit 0 |
| 46 |
fi |
| 47 |
|
| 48 |
if [ -t 0 ]; then |
| 49 |
exec >&2 |
| 50 |
echo |
| 51 |
echo "Usage: clevis encrypt tpm2 CONFIG < PLAINTEXT > JWE" |
| 52 |
echo |
| 53 |
echo "$SUMMARY" |
| 54 |
echo |
| 55 |
echo "This command uses the following configuration properties:" |
| 56 |
echo |
| 57 |
echo " hash: <string> Hash algorithm used in the computation of the object name (default: sha256)" |
| 58 |
echo |
| 59 |
echo " key: <string> Algorithm type for the generated key (default: ecc)" |
| 60 |
echo |
| 61 |
echo " pcr_bank: <string> PCR algorithm bank to use for policy (default: first supported by TPM)" |
| 62 |
echo |
| 63 |
echo " pcr_ids: <string> PCR list used for policy. If not present, no policy is used" |
| 64 |
echo |
| 65 |
echo " pcr_digest: <string> Binary PCR hashes encoded in base64. If not present, the hash values are looked up" |
| 66 |
echo |
| 67 |
exit 2 |
| 68 |
fi |
| 69 |
|
| 70 |
validate_pcrs() { |
| 71 |
local _tpm2_tools_v="${1}" |
| 72 |
local _pcr_bank="${2}" |
| 73 |
local _pcrs="${3}" |
| 74 |
[ -z "${_pcr_bank}" ] && return 1 |
| 75 |
[ -z "${_pcrs}" ] && return 0 |
| 76 |
|
| 77 |
local _fail= |
| 78 |
local _pcrs_r= |
| 79 |
case "${_tpm2_tools_v}" in |
| 80 |
3) _pcrs_r="$(tpm2_pcrlist -L "${_pcr_bank}":"${_pcrs}" | grep -v "^${_pcr_bank}")" || _fail=$?;; |
| 81 |
4|5) _pcrs_r=$(tpm2_pcrread "${_pcr_bank}":"${_pcrs}" | grep -v " ${_pcr_bank}") || _fail=$?;; |
| 82 |
*) _fail=1 |
| 83 |
esac |
| 84 |
|
| 85 |
if [ -n "${_fail}" ] || [ -z "${_pcrs_r}" ]; then |
| 86 |
return 1 |
| 87 |
fi |
| 88 |
return 0 |
| 89 |
} |
| 90 |
|
| 91 |
TPM2TOOLS_INFO="$(tpm2_createprimary -v)" |
| 92 |
|
| 93 |
match='version="(.)\.' |
| 94 |
[[ $TPM2TOOLS_INFO =~ $match ]] && TPM2TOOLS_VERSION="${BASH_REMATCH[1]}" |
| 95 |
if [[ $TPM2TOOLS_VERSION -lt 3 ]] || [[ $TPM2TOOLS_VERSION -gt 5 ]]; then |
| 96 |
echo "The tpm2 pin requires a tpm2-tools version between 3 and 5" >&2 |
| 97 |
exit 1 |
| 98 |
fi |
| 99 |
|
| 100 |
if [ -z "$TPM2TOOLS_TCTI" ]; then |
| 101 |
# Old environment variables for tpm2-tools 3.0 |
| 102 |
export TPM2TOOLS_TCTI_NAME=device |
| 103 |
export TPM2TOOLS_DEVICE_FILE= |
| 104 |
for dev in /dev/tpmrm?; do |
| 105 |
[ -e "$dev" ] || continue |
| 106 |
TPM2TOOLS_DEVICE_FILE="$dev" |
| 107 |
break |
| 108 |
done |
| 109 |
|
| 110 |
# New environment variable for tpm2-tools >= 3.1 |
| 111 |
export TPM2TOOLS_TCTI="$TPM2TOOLS_TCTI_NAME:$TPM2TOOLS_DEVICE_FILE" |
| 112 |
|
| 113 |
if [ -z "$TPM2TOOLS_DEVICE_FILE" ]; then |
| 114 |
echo "A TPM2 device with the in-kernel resource manager is needed!" >&2 |
| 115 |
exit 1 |
| 116 |
fi |
| 117 |
|
| 118 |
if ! [[ -r "$TPM2TOOLS_DEVICE_FILE" && -w "$TPM2TOOLS_DEVICE_FILE" ]]; then |
| 119 |
echo "The $TPM2TOOLS_DEVICE_FILE device must be readable and writable!" >&2 |
| 120 |
exit 1 |
| 121 |
fi |
| 122 |
fi |
| 123 |
|
| 124 |
if ! cfg="$(jose fmt -j "$1" -Oo- 2>/dev/null)"; then |
| 125 |
echo "Configuration is malformed!" >&2 |
| 126 |
exit 1 |
| 127 |
fi |
| 128 |
|
| 129 |
hash="$(jose fmt -j- -Og hash -u- <<< "$cfg")" || hash="sha256" |
| 130 |
|
| 131 |
key="$(jose fmt -j- -Og key -u- <<< "$cfg")" || key="ecc" |
| 132 |
|
| 133 |
pcr_bank="$(jose fmt -j- -Og pcr_bank -u- <<< "$cfg")" || { |
| 134 |
if ! pcr_bank=$(tpm2_getcap pcrs | |
| 135 |
awk '/^[[:space:]]*-[[:space:]]*([^:]+):[[:space:]]*\[[[:space:]]*[^][:space:]]/ \ |
| 136 |
{found=1; split($0, m, /[-:[:space:]]+/); print m[2]; exit} |
| 137 |
END {exit !found}'); then |
| 138 |
echo "Unable to find non-empty PCR algorithm bank, please check output of tpm2_getcap pcrs" >&2 |
| 139 |
exit 1 |
| 140 |
fi |
| 141 |
} |
| 142 |
|
| 143 |
# Trim the spaces from the config, so that we will not have issues parsing |
| 144 |
# the PCR IDs. |
| 145 |
pcr_cfg=${cfg//[[:space:]]/} |
| 146 |
# Issue #103: We support passing pcr_ids using both a single string, as in |
| 147 |
# "1,3", as well as an actual JSON array, such as ["1","3"]. Let's handle both |
| 148 |
# cases here. |
| 149 |
if jose fmt -j- -Og pcr_ids 2>/dev/null <<< "${pcr_cfg}" \ |
| 150 |
&& ! pcr_ids="$(jose fmt -j- -Og pcr_ids -u- 2>/dev/null \ |
| 151 |
<<< "${pcr_cfg}")"; then |
| 152 |
|
| 153 |
# We failed to parse a string, so let's try to parse a JSON array instead. |
| 154 |
if jose fmt -j- -Og pcr_ids -A 2>/dev/null <<< "${pcr_cfg}"; then |
| 155 |
# OK, it is an array, so let's get the items and form a string. |
| 156 |
pcr_ids= |
| 157 |
for pcr in $(jose fmt -j- -Og pcr_ids -Af- <<< "${pcr_cfg}" \ |
| 158 |
| tr -d '"'); do |
| 159 |
pcr_ids=$(printf '%s,%s' "${pcr_ids}" "${pcr}") |
| 160 |
done |
| 161 |
# Now let's remove the leading comma. |
| 162 |
pcr_ids=${pcr_ids/#,/} |
| 163 |
else |
| 164 |
# Not to add a policy that was not intended, in this case, no policy |
| 165 |
# at all, let's report the issue and exit. |
| 166 |
echo "Parsing the requested policy failed!" >&2 |
| 167 |
exit 1 |
| 168 |
fi |
| 169 |
fi |
| 170 |
|
| 171 |
if ! validate_pcrs "${TPM2TOOLS_VERSION}" "${pcr_bank}" "${pcr_ids}"; then |
| 172 |
echo "Unable to validate combination of PCR bank '${pcr_bank}' and PCR IDs '${pcr_ids}'." >&2 |
| 173 |
exit 1 |
| 174 |
fi |
| 175 |
|
| 176 |
pcr_digest="$(jose fmt -j- -Og pcr_digest -u- <<< "$cfg")" || true |
| 177 |
|
| 178 |
if ! jwk="$(jose jwk gen -i '{"alg":"A256GCM"}')"; then |
| 179 |
echo "Generating a jwk failed!" >&2 |
| 180 |
exit 1 |
| 181 |
fi |
| 182 |
|
| 183 |
mkdir -p "${TMPDIR:-/tmp}" |
| 184 |
if ! TMP="$(mktemp -d)"; then |
| 185 |
echo "Creating a temporary dir for TPM files failed!" >&2 |
| 186 |
exit 1 |
| 187 |
fi |
| 188 |
|
| 189 |
trap 'on_exit' EXIT |
| 190 |
|
| 191 |
case "$TPM2TOOLS_VERSION" in |
| 192 |
3) tpm2_createprimary -Q -H "$auth" -g "$hash" -G "$key" -C "$TMP"/primary.context || fail=$?;; |
| 193 |
4|5) tpm2_createprimary -Q -C "$auth" -g "$hash" -G "$key" -c "$TMP"/primary.context || fail=$?;; |
| 194 |
*) fail=1;; |
| 195 |
esac |
| 196 |
if [ -n "$fail" ]; then |
| 197 |
echo "Creating TPM2 primary key failed!" >&2 |
| 198 |
exit 1 |
| 199 |
fi |
| 200 |
tpm2_flushcontext -t |
| 201 |
|
| 202 |
policy_options=() |
| 203 |
if [ -n "$pcr_ids" ]; then |
| 204 |
if [ -z "$pcr_digest" ]; then |
| 205 |
case "$TPM2TOOLS_VERSION" in |
| 206 |
3) tpm2_pcrlist -Q -L "$pcr_bank":"$pcr_ids" -o "$TMP"/pcr.digest || fail=$?;; |
| 207 |
4|5) tpm2_pcrread -Q "$pcr_bank":"$pcr_ids" -o "$TMP"/pcr.digest || fail=$?;; |
| 208 |
*) fail=1;; |
| 209 |
esac |
| 210 |
if [ -n "$fail" ]; then |
| 211 |
echo "Creating PCR hashes file failed!" >&2 |
| 212 |
exit 1 |
| 213 |
fi |
| 214 |
tpm2_flushcontext -t |
| 215 |
else |
| 216 |
if ! jose b64 dec -i- -O "$TMP"/pcr.digest <<< "$pcr_digest"; then |
| 217 |
echo "Error decoding PCR digest!" >&2 |
| 218 |
exit 1 |
| 219 |
fi |
| 220 |
fi |
| 221 |
|
| 222 |
case "$TPM2TOOLS_VERSION" in |
| 223 |
3) tpm2_createpolicy -Q -g "$hash" -P -L "$pcr_bank":"$pcr_ids" \ |
| 224 |
-F "$TMP"/pcr.digest -f "$TMP"/pcr.policy || fail=$?;; |
| 225 |
4|5) tpm2_createpolicy -Q -g "$hash" --policy-pcr -l "$pcr_bank":"$pcr_ids" \ |
| 226 |
-f "$TMP"/pcr.digest -L "$TMP"/pcr.policy || fail=$?;; |
| 227 |
*) fail=1;; |
| 228 |
esac |
| 229 |
if [ -n "$fail" ]; then |
| 230 |
echo "create policy fail, please check the environment or parameters!" |
| 231 |
exit 1 |
| 232 |
fi |
| 233 |
tpm2_flushcontext -t |
| 234 |
tpm2_flushcontext -l |
| 235 |
|
| 236 |
policy_options+=(-L "$TMP/pcr.policy") |
| 237 |
else |
| 238 |
obj_attr="$obj_attr|userwithauth" |
| 239 |
fi |
| 240 |
|
| 241 |
case "$TPM2TOOLS_VERSION" in |
| 242 |
3) tpm2_create -Q -g "$hash" -G "$alg_create_key" -c "$TMP"/primary.context -u "$TMP"/jwk.pub \ |
| 243 |
-r "$TMP"/jwk.priv -A "$obj_attr" "${policy_options[@]}" -I- <<< "$jwk" || fail=$?;; |
| 244 |
4|5) tpm2_create -Q -g "$hash" -C "$TMP"/primary.context -u "$TMP"/jwk.pub \ |
| 245 |
-r "$TMP"/jwk.priv -a "$obj_attr" "${policy_options[@]}" -i- <<< "$jwk" || fail=$?;; |
| 246 |
*) fail=1;; |
| 247 |
esac |
| 248 |
if [ -n "$fail" ]; then |
| 249 |
echo "Creating TPM2 object for jwk failed!" >&2 |
| 250 |
exit 1 |
| 251 |
fi |
| 252 |
tpm2_flushcontext -t |
| 253 |
|
| 254 |
if ! jwk_pub="$(jose b64 enc -I "$TMP"/jwk.pub)"; then |
| 255 |
echo "Encoding jwk.pub in Base64 failed!" >&2 |
| 256 |
exit 1 |
| 257 |
fi |
| 258 |
|
| 259 |
if ! jwk_priv="$(jose b64 enc -I "$TMP"/jwk.priv)"; then |
| 260 |
echo "Encoding jwk.priv in Base64 failed!" >&2 |
| 261 |
exit 1 |
| 262 |
fi |
| 263 |
|
| 264 |
jwe='{"protected":{"clevis":{"pin":"tpm2","tpm2":{}}}}' |
| 265 |
jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$hash" -s hash -UUUUo-)" |
| 266 |
jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$key" -s key -UUUUo-)" |
| 267 |
|
| 268 |
if [ -n "$pcr_ids" ]; then |
| 269 |
jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$pcr_bank" -s pcr_bank -UUUUo-)" |
| 270 |
jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$pcr_ids" -s pcr_ids -UUUUo-)" |
| 271 |
fi |
| 272 |
|
| 273 |
jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$jwk_pub" -s jwk_pub -UUUUo-)" |
| 274 |
jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$jwk_priv" -s jwk_priv -UUUUo-)" |
| 275 |
|
| 276 |
# The on_exit() trap will not be fired after exec, so let's clean up the temp |
| 277 |
# directory at this point. |
| 278 |
[ -d "${TMP}" ] && rm -rf "${TMP}" |
| 279 |
|
| 280 |
exec jose jwe enc -i- -k- -I- -c < <(echo -n "$jwe$jwk"; /bin/cat) |