clevis-decrypt-tpm2 3.5 KB

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