clevis-encrypt-tpm2 7.9 KB

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