clevis-decrypt-tpm2 4.9 KB

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