clevis-decrypt-tpm2 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 decrypt $@
  24. fi
  25. # The owner hierarchy is the one that should be used by the Operating System.
  26. auth="o"
  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. [ $# -eq 1 ] && [ "$1" == "--summary" ] && exit 2
  35. if [ -t 0 ]; then
  36. exec >&2
  37. echo
  38. echo "Usage: clevis decrypt tpm2 < JWE > PLAINTEXT"
  39. echo
  40. exit 2
  41. fi
  42. TPM2TOOLS_INFO="$(tpm2_createprimary -v)"
  43. match='version="(.)\.'
  44. [[ $TPM2TOOLS_INFO =~ $match ]] && TPM2TOOLS_VERSION="${BASH_REMATCH[1]}"
  45. if [[ $TPM2TOOLS_VERSION -lt 3 ]] || [[ $TPM2TOOLS_VERSION -gt 5 ]]; then
  46. echo "The tpm2 pin requires a tpm2-tools version between 3 and 5" >&2
  47. exit 1
  48. fi
  49. if [ -z "$TPM2TOOLS_TCTI" ]; then
  50. # Old environment variables for tpm2-tools 3.0
  51. export TPM2TOOLS_TCTI_NAME=device
  52. export TPM2TOOLS_DEVICE_FILE=
  53. for dev in /dev/tpmrm?; do
  54. [ -e "$dev" ] || continue
  55. TPM2TOOLS_DEVICE_FILE="$dev"
  56. break
  57. done
  58. # New environment variable for tpm2-tools >= 3.1
  59. export TPM2TOOLS_TCTI="$TPM2TOOLS_TCTI_NAME:$TPM2TOOLS_DEVICE_FILE"
  60. if [ -z "$TPM2TOOLS_DEVICE_FILE" ]; then
  61. echo "A TPM2 device with the in-kernel resource manager is needed!" >&2
  62. exit 1
  63. fi
  64. if ! [[ -r "$TPM2TOOLS_DEVICE_FILE" && -w "$TPM2TOOLS_DEVICE_FILE" ]]; then
  65. echo "The $TPM2TOOLS_DEVICE_FILE device must be readable and writable!" >&2
  66. exit 1
  67. fi
  68. fi
  69. read -r -d . hdr
  70. if ! jhd="$(jose b64 dec -i- <<< "$hdr")"; then
  71. echo "Error decoding JWE protected header!" >&2
  72. exit 1
  73. fi
  74. if [ "$(jose fmt -j- -Og clevis -g pin -u- <<< "$jhd")" != "tpm2" ]; then
  75. echo "JWE pin mismatch!" >&2
  76. exit 1
  77. fi
  78. if ! hash="$(jose fmt -j- -Og clevis -g tpm2 -g hash -Su- <<< "$jhd")"; then
  79. echo "JWE missing required 'hash' header parameter!" >&2
  80. exit 1
  81. fi
  82. if ! key="$(jose fmt -j- -Og clevis -g tpm2 -g key -Su- <<< "$jhd")"; then
  83. echo "JWE missing required 'key' header parameter!" >&2
  84. exit 1
  85. fi
  86. if ! jwk_pub="$(jose fmt -j- -Og clevis -g tpm2 -g jwk_pub -Su- <<< "$jhd")"; then
  87. echo "JWE missing required 'jwk_pub' header parameter!" >&2
  88. exit 1
  89. fi
  90. if ! jwk_priv="$(jose fmt -j- -Og clevis -g tpm2 -g jwk_priv -Su- <<< "$jhd")"; then
  91. echo "JWE missing required 'jwk_priv' header parameter!" >&2
  92. exit 1
  93. fi
  94. mkdir -p "${TMPDIR:-/tmp}"
  95. if ! TMP="$(mktemp -d)"; then
  96. echo "Creating a temporary dir for TPM files failed!" >&2
  97. exit 1
  98. fi
  99. trap 'on_exit' EXIT
  100. pcr_ids="$(jose fmt -j- -Og clevis -g tpm2 -g pcr_ids -Su- <<< "$jhd")" || true
  101. pcr_spec=''
  102. if [ -n "$pcr_ids" ]; then
  103. pcr_bank="$(jose fmt -j- -Og clevis -g tpm2 -g pcr_bank -Su- <<< "$jhd")"
  104. pcr_spec="$pcr_bank:$pcr_ids"
  105. fi
  106. if ! jose b64 dec -i- -O "$TMP"/jwk.pub <<< "$jwk_pub"; then
  107. echo "Decoding jwk.pub from Base64 failed!" >&2
  108. exit 1
  109. fi
  110. if ! jose b64 dec -i- -O "$TMP"/jwk.priv <<< "$jwk_priv"; then
  111. echo "Decoding jwk.priv from Base64 failed!" >&2
  112. exit 1
  113. fi
  114. case "$TPM2TOOLS_VERSION" in
  115. 3) tpm2_createprimary -Q -H "$auth" -g "$hash" -G "$key" -C "$TMP"/primary.context || fail=$?;;
  116. 4|5) tpm2_createprimary -Q -C "$auth" -g "$hash" -G "$key" -c "$TMP"/primary.context || fail=$?;;
  117. *) fail=1;;
  118. esac
  119. if [ -n "$fail" ]; then
  120. echo "Creating TPM2 primary key failed!" >&2
  121. exit 1
  122. fi
  123. tpm2_flushcontext -t
  124. case "$TPM2TOOLS_VERSION" in
  125. 3) tpm2_load -Q -c "$TMP"/primary.context -u "$TMP"/jwk.pub -r "$TMP"/jwk.priv \
  126. -C "$TMP"/load.context || fail=$?;;
  127. 4|5) tpm2_load -Q -C "$TMP"/primary.context -u "$TMP"/jwk.pub -r "$TMP"/jwk.priv \
  128. -c "$TMP"/load.context || fail=$?;;
  129. *) fail=1;;
  130. esac
  131. if [ -n "$fail" ]; then
  132. echo "Loading jwk to TPM2 failed!" >&2
  133. exit 1
  134. fi
  135. tpm2_flushcontext -t
  136. case "$TPM2TOOLS_VERSION" in
  137. 3) jwk="$(tpm2_unseal -c "$TMP"/load.context ${pcr_spec:+-L $pcr_spec})" || fail=$?;;
  138. 4|5) jwk="$(tpm2_unseal -c "$TMP"/load.context ${pcr_spec:+-p pcr:$pcr_spec})" || fail=$?;;
  139. *) fail=1;;
  140. esac
  141. if [ -n "$fail" ]; then
  142. echo "Unsealing jwk from TPM failed!" >&2
  143. exit 1
  144. fi
  145. tpm2_flushcontext -t
  146. (echo -n "$jwk$hdr."; /bin/cat) | jose jwe dec -k- -i-
  147. exit $?