clevis-encrypt-tpm2 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. validate_pcrs() {
  65. local _tpm2_tools_v="${1}"
  66. local _pcr_bank="${2}"
  67. local _pcrs="${3}"
  68. [ -z "${_pcr_bank}" ] && return 1
  69. [ -z "${_pcrs}" ] && return 0
  70. local _fail=
  71. local _pcrs_r=
  72. case "${_tpm2_tools_v}" in
  73. 3) _pcrs_r="$(tpm2_pcrlist -L "${_pcr_bank}":"${_pcrs}" | grep -v "^${_pcr_bank}")" || _fail=$?;;
  74. 4|5) _pcrs_r=$(tpm2_pcrread "${_pcr_bank}":"${_pcrs}" | grep -v " ${_pcr_bank}") || _fail=$?;;
  75. *) _fail=1
  76. esac
  77. if [ -n "${_fail}" ] || [ -z "${_pcrs_r}" ]; then
  78. return 1
  79. fi
  80. return 0
  81. }
  82. TPM2TOOLS_INFO="$(tpm2_createprimary -v)"
  83. match='version="(.)\.'
  84. [[ $TPM2TOOLS_INFO =~ $match ]] && TPM2TOOLS_VERSION="${BASH_REMATCH[1]}"
  85. if [[ $TPM2TOOLS_VERSION -lt 3 ]] || [[ $TPM2TOOLS_VERSION -gt 5 ]]; then
  86. echo "The tpm2 pin requires a tpm2-tools version between 3 and 5" >&2
  87. exit 1
  88. fi
  89. if [ -z "$TPM2TOOLS_TCTI" ]; then
  90. # Old environment variables for tpm2-tools 3.0
  91. export TPM2TOOLS_TCTI_NAME=device
  92. export TPM2TOOLS_DEVICE_FILE=
  93. for dev in /dev/tpmrm?; do
  94. [ -e "$dev" ] || continue
  95. TPM2TOOLS_DEVICE_FILE="$dev"
  96. break
  97. done
  98. # New environment variable for tpm2-tools >= 3.1
  99. export TPM2TOOLS_TCTI="$TPM2TOOLS_TCTI_NAME:$TPM2TOOLS_DEVICE_FILE"
  100. if [ -z "$TPM2TOOLS_DEVICE_FILE" ]; then
  101. echo "A TPM2 device with the in-kernel resource manager is needed!" >&2
  102. exit 1
  103. fi
  104. if ! [[ -r "$TPM2TOOLS_DEVICE_FILE" && -w "$TPM2TOOLS_DEVICE_FILE" ]]; then
  105. echo "The $TPM2TOOLS_DEVICE_FILE device must be readable and writable!" >&2
  106. exit 1
  107. fi
  108. fi
  109. if ! cfg="$(jose fmt -j "$1" -Oo- 2>/dev/null)"; then
  110. echo "Configuration is malformed!" >&2
  111. exit 1
  112. fi
  113. hash="$(jose fmt -j- -Og hash -u- <<< "$cfg")" || hash="sha256"
  114. key="$(jose fmt -j- -Og key -u- <<< "$cfg")" || key="ecc"
  115. pcr_bank="$(jose fmt -j- -Og pcr_bank -u- <<< "$cfg")" || pcr_bank="sha1"
  116. # Trim the spaces from the config, so that we will not have issues parsing
  117. # the PCR IDs.
  118. pcr_cfg=${cfg//[[:space:]]/}
  119. # Issue #103: We support passing pcr_ids using both a single string, as in
  120. # "1,3", as well as an actual JSON array, such as ["1","3"]. Let's handle both
  121. # cases here.
  122. if jose fmt -j- -Og pcr_ids 2>/dev/null <<< "${pcr_cfg}" \
  123. && ! pcr_ids="$(jose fmt -j- -Og pcr_ids -u- 2>/dev/null \
  124. <<< "${pcr_cfg}")"; then
  125. # We failed to parse a string, so let's try to parse a JSON array instead.
  126. if jose fmt -j- -Og pcr_ids -A 2>/dev/null <<< "${pcr_cfg}"; then
  127. # OK, it is an array, so let's get the items and form a string.
  128. pcr_ids=
  129. for pcr in $(jose fmt -j- -Og pcr_ids -Af- <<< "${pcr_cfg}" \
  130. | tr -d '"'); do
  131. pcr_ids=$(printf '%s,%s' "${pcr_ids}" "${pcr}")
  132. done
  133. # Now let's remove the leading comma.
  134. pcr_ids=${pcr_ids/#,/}
  135. else
  136. # Not to add a policy that was not intended, in this case, no policy
  137. # at all, let's report the issue and exit.
  138. echo "Parsing the requested policy failed!" >&2
  139. exit 1
  140. fi
  141. fi
  142. if ! validate_pcrs "${TPM2TOOLS_VERSION}" "${pcr_bank}" "${pcr_ids}"; then
  143. echo "Unable to validate combination of PCR bank '${pcr_bank}' and PCR IDs '${pcr_ids}'." >&2
  144. exit 1
  145. fi
  146. pcr_digest="$(jose fmt -j- -Og pcr_digest -u- <<< "$cfg")" || true
  147. if ! jwk="$(jose jwk gen -i '{"alg":"A256GCM"}')"; then
  148. echo "Generating a jwk failed!" >&2
  149. exit 1
  150. fi
  151. mkdir -p "${TMPDIR:-/tmp}"
  152. if ! TMP="$(mktemp -d)"; then
  153. echo "Creating a temporary dir for TPM files failed!" >&2
  154. exit 1
  155. fi
  156. trap 'on_exit' EXIT
  157. case "$TPM2TOOLS_VERSION" in
  158. 3) tpm2_createprimary -Q -H "$auth" -g "$hash" -G "$key" -C "$TMP"/primary.context || fail=$?;;
  159. 4|5) tpm2_createprimary -Q -C "$auth" -g "$hash" -G "$key" -c "$TMP"/primary.context || fail=$?;;
  160. *) fail=1;;
  161. esac
  162. if [ -n "$fail" ]; then
  163. echo "Creating TPM2 primary key failed!" >&2
  164. exit 1
  165. fi
  166. tpm2_flushcontext -t
  167. policy_options=()
  168. if [ -n "$pcr_ids" ]; then
  169. if [ -z "$pcr_digest" ]; then
  170. case "$TPM2TOOLS_VERSION" in
  171. 3) tpm2_pcrlist -Q -L "$pcr_bank":"$pcr_ids" -o "$TMP"/pcr.digest || fail=$?;;
  172. 4|5) tpm2_pcrread -Q "$pcr_bank":"$pcr_ids" -o "$TMP"/pcr.digest || fail=$?;;
  173. *) fail=1;;
  174. esac
  175. if [ -n "$fail" ]; then
  176. echo "Creating PCR hashes file failed!" >&2
  177. exit 1
  178. fi
  179. tpm2_flushcontext -t
  180. else
  181. if ! jose b64 dec -i- -O "$TMP"/pcr.digest <<< "$pcr_digest"; then
  182. echo "Error decoding PCR digest!" >&2
  183. exit 1
  184. fi
  185. fi
  186. case "$TPM2TOOLS_VERSION" in
  187. 3) tpm2_createpolicy -Q -g "$hash" -P -L "$pcr_bank":"$pcr_ids" \
  188. -F "$TMP"/pcr.digest -f "$TMP"/pcr.policy || fail=$?;;
  189. 4|5) tpm2_createpolicy -Q -g "$hash" --policy-pcr -l "$pcr_bank":"$pcr_ids" \
  190. -f "$TMP"/pcr.digest -L "$TMP"/pcr.policy || fail=$?;;
  191. *) fail=1;;
  192. esac
  193. if [ -n "$fail" ]; then
  194. echo "create policy fail, please check the environment or parameters!"
  195. exit 1
  196. fi
  197. tpm2_flushcontext -t
  198. policy_options+=(-L "$TMP/pcr.policy")
  199. else
  200. obj_attr="$obj_attr|userwithauth"
  201. fi
  202. case "$TPM2TOOLS_VERSION" in
  203. 3) tpm2_create -Q -g "$hash" -G "$alg_create_key" -c "$TMP"/primary.context -u "$TMP"/jwk.pub \
  204. -r "$TMP"/jwk.priv -A "$obj_attr" "${policy_options[@]}" -I- <<< "$jwk" || fail=$?;;
  205. 4|5) tpm2_create -Q -g "$hash" -C "$TMP"/primary.context -u "$TMP"/jwk.pub \
  206. -r "$TMP"/jwk.priv -a "$obj_attr" "${policy_options[@]}" -i- <<< "$jwk" || fail=$?;;
  207. *) fail=1;;
  208. esac
  209. if [ -n "$fail" ]; then
  210. echo "Creating TPM2 object for jwk failed!" >&2
  211. exit 1
  212. fi
  213. tpm2_flushcontext -t
  214. if ! jwk_pub="$(jose b64 enc -I "$TMP"/jwk.pub)"; then
  215. echo "Encoding jwk.pub in Base64 failed!" >&2
  216. exit 1
  217. fi
  218. if ! jwk_priv="$(jose b64 enc -I "$TMP"/jwk.priv)"; then
  219. echo "Encoding jwk.priv in Base64 failed!" >&2
  220. exit 1
  221. fi
  222. jwe='{"protected":{"clevis":{"pin":"tpm2","tpm2":{}}}}'
  223. jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$hash" -s hash -UUUUo-)"
  224. jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$key" -s key -UUUUo-)"
  225. if [ -n "$pcr_ids" ]; then
  226. jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$pcr_bank" -s pcr_bank -UUUUo-)"
  227. jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$pcr_ids" -s pcr_ids -UUUUo-)"
  228. fi
  229. jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$jwk_pub" -s jwk_pub -UUUUo-)"
  230. jwe="$(jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$jwk_priv" -s jwk_priv -UUUUo-)"
  231. # The on_exit() trap will not be fired after exec, so let's clean up the temp
  232. # directory at this point.
  233. [ -d "${TMP}" ] && rm -rf "${TMP}"
  234. exec jose jwe enc -i- -k- -I- -c < <(echo -n "$jwe$jwk"; /bin/cat)