clevis-decrypt-tpm2 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 ! rm -r $TMP; then
  24. echo "Delete temporary files failed!" >&2
  25. exit 1
  26. fi
  27. }
  28. [ $# -eq 1 -a "$1" == "--summary" ] && exit 1
  29. if [ -t 0 ]; then
  30. echo >&2
  31. echo "Usage: clevis decrypt tpm2 < JWE > PLAINTEXT" >&2
  32. echo >&2
  33. exit 1
  34. fi
  35. TPM2TOOLS_INFO=`tpm2_pcrlist -v`
  36. if [[ $TPM2TOOLS_INFO != *version=\"3.* ]]; then
  37. echo "The tpm2 pin requires tpm2-tools version 3" >&2
  38. exit 1
  39. fi
  40. export TPM2TOOLS_TCTI_NAME=device
  41. export TPM2TOOLS_DEVICE_FILE=`ls /dev/tpmrm? 2>/dev/null`
  42. if [ -z "${TPM2TOOLS_DEVICE_FILE[0]}" ]; then
  43. echo "A TPM2 device with the in-kernel resource manager is needed!" >&2
  44. exit 1
  45. fi
  46. if ! [[ -r "${TPM2TOOLS_DEVICE_FILE[0]}" && -w "${TPM2TOOLS_DEVICE_FILE[0]}" ]]; then
  47. echo "The ${TPM2TOOLS_DEVICE_FILE[0]} device must be readable and writable!" >&2
  48. exit 1
  49. fi
  50. read -d . hdr
  51. if ! jhd=`jose b64 dec -i- <<< "$hdr"`; then
  52. echo "Error decoding JWE protected header!" >&2
  53. exit 1
  54. fi
  55. if [ `jose fmt -j- -Og clevis -g pin -u- <<< "$jhd"` != "tpm2" ]; then
  56. echo "JWE pin mismatch!" >&2
  57. exit 1
  58. fi
  59. if ! hash=`jose fmt -j- -Og clevis -g tpm2 -g hash -Su- <<< "$jhd"`; then
  60. echo "JWE missing required 'hash' header parameter!" >&2
  61. exit 1
  62. fi
  63. if ! key=`jose fmt -j- -Og clevis -g tpm2 -g key -Su- <<< "$jhd"`; then
  64. echo "JWE missing required 'key' header parameter!" >&2
  65. exit 1
  66. fi
  67. if ! jwk_pub=`jose fmt -j- -Og clevis -g tpm2 -g jwk_pub -Su- <<< "$jhd"`; then
  68. echo "JWE missing required 'key' header parameter!" >&2
  69. exit 1
  70. fi
  71. if ! jwk_priv=`jose fmt -j- -Og clevis -g tpm2 -g jwk_priv -Su- <<< "$jhd"`; then
  72. echo "JWE missing required 'key' header parameter!" >&2
  73. exit 1
  74. fi
  75. if ! TMP=`mktemp -d`; then
  76. echo "Creating a temporary dir for TPM files failed!" >&2
  77. exit 1
  78. fi
  79. trap 'on_exit' EXIT
  80. pcr_ids=`jose fmt -j- -Og clevis -g tpm2 -g pcr_ids -Su- <<< "$jhd"` || true
  81. if [ -n "$pcr_ids" ]; then
  82. pcr_bank=`jose fmt -j- -Og clevis -g tpm2 -g pcr_bank -Su- <<< "$jhd"`
  83. policy_options="-L $pcr_bank:$pcr_ids"
  84. fi
  85. if ! `jose b64 dec -i- -O $TMP/jwk.pub <<< "$jwk_pub"`; then
  86. echo "Decoding jwk.pub from Base64 failed!" >&2
  87. exit 1
  88. fi
  89. if ! `jose b64 dec -i- -O $TMP/jwk.priv <<< "$jwk_priv"`; then
  90. echo "Decoding jwk.priv from Base64 failed!" >&2
  91. exit 1
  92. fi
  93. if ! tpm2_createprimary -Q -H "$auth" -g "$hash" -G "$key" \
  94. -C $TMP/primary.context 2>/dev/null; then
  95. echo "Creating TPM2 primary key failed!" >&2
  96. exit 1
  97. fi
  98. if ! tpm2_load -Q -c $TMP/primary.context -u $TMP/jwk.pub -r $TMP/jwk.priv \
  99. -C $TMP/load.context 2>/dev/null; then
  100. echo "Loading jwk to TPM2 failed!" >&2
  101. exit 1
  102. fi
  103. if ! jwk=`tpm2_unseal -c $TMP/load.context $policy_options 2>/dev/null`; then
  104. echo "Unsealing jwk from TPM failed!" >&2
  105. exit 1
  106. fi
  107. jose jwe dec -k- -i- < <(echo -n "$jwk$hdr."; /bin/cat)