clevis-decrypt-tpm2 4.9 KB

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