clevis-encrypt-tpm2 8.1 KB

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