clevis-encrypt-tpm2 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. # Issue #103: We support passing pcr_ids using both a single string, as in
  92. # "1,3", as well as an actual JSON array, such as ["1,"3"]. Let's handle both
  93. # cases here.
  94. if [[ ${cfg// /} != '{}' ]] \
  95. && ! pcr_ids="$(jose fmt -j- -Og pcr_ids -u- 2>/dev/null <<< "$cfg")"; then
  96. # We failed to parse a string, so let's try to parse a JSON array instead.
  97. if jose fmt -j- -Og pcr_ids -A 2>/dev/null <<< "${cfg}"; then
  98. # OK, it is an array, so let's get the items and form a string.
  99. pcr_ids=
  100. for pcr in $(jose fmt -j- -Og pcr_ids -Af- <<< "${cfg}" \
  101. | tr -d '"'); do
  102. pcr_ids=$(printf '%s,%s' "${pcr_ids}" "${pcr}")
  103. done
  104. # Now let's remove the leading comma.
  105. pcr_ids=${pcr_ids/#,/}
  106. else
  107. # Not to add a policy that was not intended, in this case, no policy
  108. # at all, let's report the issue and exit.
  109. echo "Parsing the requested policy failed!" >&2
  110. exit 1
  111. fi
  112. fi
  113. pcr_digest="$(jose fmt -j- -Og pcr_digest -u- <<< "$cfg")" || true
  114. if ! jwk="$(jose jwk gen -i '{"alg":"A256GCM"}')"; then
  115. echo "Generating a jwk failed!" >&2
  116. exit 1
  117. fi
  118. if ! TMP="$(mktemp -d)"; then
  119. echo "Creating a temporary dir for TPM files failed!" >&2
  120. exit 1
  121. fi
  122. trap 'on_exit' EXIT
  123. case "$TPM2TOOLS_VERSION" in
  124. 3) tpm2_createprimary -Q -H "$auth" -g "$hash" -G "$key" -C "$TMP"/primary.context || fail=$?;;
  125. 4) tpm2_createprimary -Q -C "$auth" -g "$hash" -G "$key" -c "$TMP"/primary.context || fail=$?;;
  126. *) fail=1;;
  127. esac
  128. if [ -n "$fail" ]; then
  129. echo "Creating TPM2 primary key failed!" >&2
  130. exit 1
  131. fi
  132. policy_options=()
  133. if [ -n "$pcr_ids" ]; then
  134. if [ -z "$pcr_digest" ]; then
  135. case "$TPM2TOOLS_VERSION" in
  136. 3) tpm2_pcrlist -Q -L "$pcr_bank":"$pcr_ids" -o "$TMP"/pcr.digest || fail=$?;;
  137. 4) tpm2_pcrread -Q "$pcr_bank":"$pcr_ids" -o "$TMP"/pcr.digest || fail=$?;;
  138. *) fail=1;;
  139. esac
  140. if [ -n "$fail" ]; then
  141. echo "Creating PCR hashes file failed!" >&2
  142. exit 1
  143. fi
  144. else
  145. if ! jose b64 dec -i- -O "$TMP"/pcr.digest <<< "$pcr_digest"; then
  146. echo "Error decoding PCR digest!" >&2
  147. exit 1
  148. fi
  149. fi
  150. case "$TPM2TOOLS_VERSION" in
  151. 3) tpm2_createpolicy -Q -g "$hash" -P -L "$pcr_bank":"$pcr_ids" \
  152. -F "$TMP"/pcr.digest -f "$TMP"/pcr.policy || fail=$?;;
  153. 4) tpm2_createpolicy -Q -g "$hash" --policy-pcr -l "$pcr_bank":"$pcr_ids" \
  154. -f "$TMP"/pcr.digest -L "$TMP"/pcr.policy || fail=$?;;
  155. *) fail=1;;
  156. esac
  157. if [ -n "$fail" ]; then
  158. echo "create policy fail, please check the environment or parameters!"
  159. exit 1
  160. fi
  161. policy_options+=(-L "$TMP/pcr.policy")
  162. else
  163. obj_attr="$obj_attr|userwithauth"
  164. fi
  165. case "$TPM2TOOLS_VERSION" in
  166. 3) tpm2_create -Q -g "$hash" -G "$alg_create_key" -c "$TMP"/primary.context -u "$TMP"/jwk.pub \
  167. -r "$TMP"/jwk.priv -A "$obj_attr" "${policy_options[@]}" -I- <<< "$jwk" || fail=$?;;
  168. 4) tpm2_create -Q -g "$hash" -C "$TMP"/primary.context -u "$TMP"/jwk.pub \
  169. -r "$TMP"/jwk.priv -a "$obj_attr" "${policy_options[@]}" -i- <<< "$jwk" || fail=$?;;
  170. *) fail=1;;
  171. esac
  172. if [ -n "$fail" ]; then
  173. echo "Creating TPM2 object for jwk failed!" >&2
  174. exit 1
  175. fi
  176. if ! jwk_pub="$(jose b64 enc -I "$TMP"/jwk.pub)"; then
  177. echo "Encoding jwk.pub in Base64 failed!" >&2
  178. exit 1
  179. fi
  180. if ! jwk_priv="$(jose b64 enc -I "$TMP"/jwk.priv)"; then
  181. echo "Encoding jwk.priv in Base64 failed!" >&2
  182. exit 1
  183. fi
  184. jwe='{"protected":{"clevis":{"pin":"tpm2","tpm2":{}}}}'
  185. jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$hash" -s hash -UUUUo-)"
  186. jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$key" -s key -UUUUo-)"
  187. if [ -n "$pcr_ids" ]; then
  188. jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$pcr_bank" -s pcr_bank -UUUUo-)"
  189. jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$pcr_ids" -s pcr_ids -UUUUo-)"
  190. fi
  191. jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$jwk_pub" -s jwk_pub -UUUUo-)"
  192. jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$jwk_priv" -s jwk_priv -UUUUo-)"
  193. # The on_exit() trap will not be fired after exec, so let's clean up the temp
  194. # directory at this point.
  195. [ -d "${TMP}" ] && rm -rf "${TMP}"
  196. exec jose jwe enc -i- -k- -I- -c < <(echo -n "$jwe$jwk"; /bin/cat)