clevis-encrypt-tpm2 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. SUMMARY="Encrypts using a TPM2.0 chip binding policy"
  21. # The owner hierarchy is the one that should be used by the Operating System.
  22. auth="o"
  23. # Algorithm type must be keyedhash for object with user provided sensitive data.
  24. alg_create_key="keyedhash"
  25. # Attributes for the created TPM2 object with the JWK as sensitive data.
  26. obj_attr="fixedtpm|fixedparent|sensitivedataorigin|noda|adminwithpolicy"
  27. function on_exit() {
  28. if ! rm -rf $TMP; then
  29. echo "Delete temporary files failed!" >&2
  30. exit 1
  31. fi
  32. }
  33. if [ "$1" == "--summary" ]; then
  34. echo "$SUMMARY"
  35. exit 0
  36. fi
  37. if [ -t 0 ]; then
  38. echo >&2
  39. echo "Usage: clevis encrypt tpm2 CONFIG < PLAINTEXT > JWE" >&2
  40. echo >&2
  41. echo $SUMMARY >&2
  42. echo >&2
  43. echo "This command uses the following configuration properties:" >&2
  44. echo >&2
  45. echo " hash: <string> Hash algorithm used in the computation of the object name (default: sha256)" >&2
  46. echo >&2
  47. echo " key: <string> Algorithm type for the generated key (default: ecc)" >&2
  48. echo >&2
  49. echo " pcr_bank: <string> PCR algorithm bank to use for policy (default: sha1)" >&2
  50. echo >&2
  51. echo " pcr_ids: <string> PCR list used for policy. If not present, no policy is used" >&2
  52. echo >&2
  53. echo " pcr_digest: <string> Binary PCR hashes encoded in base64. If not present, the hash values are looked up" >&2
  54. echo >&2
  55. exit 1
  56. fi
  57. TPM2TOOLS_INFO=`tpm2_pcrlist -v`
  58. if [[ $TPM2TOOLS_INFO != *version=\"3.* ]]; then
  59. echo "The tpm2 pin requires tpm2-tools version 3" >&2
  60. exit 1
  61. fi
  62. export TPM2TOOLS_TCTI_NAME=device
  63. export TPM2TOOLS_DEVICE_FILE=`ls /dev/tpmrm? 2>/dev/null`
  64. if [ -z "${TPM2TOOLS_DEVICE_FILE[0]}" ]; then
  65. echo "A TPM2 device with the in-kernel resource manager is needed!" >&2
  66. exit 1
  67. fi
  68. if ! [[ -r "${TPM2TOOLS_DEVICE_FILE[0]}" && -w "${TPM2TOOLS_DEVICE_FILE[0]}" ]]; then
  69. echo "The ${TPM2TOOLS_DEVICE_FILE[0]} device must be readable and writable!" >&2
  70. exit 1
  71. fi
  72. if ! cfg=`jose fmt -j "$1" -Oo- 2>/dev/null`; then
  73. echo "Configuration is malformed!" >&2
  74. exit 1
  75. fi
  76. hash=`jose fmt -j- -Og hash -u- <<< "$cfg"` || hash="sha256"
  77. key=`jose fmt -j- -Og key -u- <<< "$cfg"` || key="ecc"
  78. pcr_bank=`jose fmt -j- -Og pcr_hash -u- <<< "$cfg"` || pcr_bank="sha1"
  79. pcr_ids=`jose fmt -j- -Og pcr_ids -u- <<< "$cfg"` || true
  80. pcr_digest=`jose fmt -j- -Og pcr_digest -u- <<< "$cfg"` || true
  81. if ! jwk=`jose jwk gen -i '{"alg":"A256GCM"}'`; then
  82. echo "Generating a jwk failed!" >&2
  83. exit 1
  84. fi
  85. if ! TMP=`mktemp -d`; then
  86. echo "Creating a temporary dir for TPM files failed!" >&2
  87. exit 1
  88. fi
  89. trap 'on_exit' EXIT
  90. if ! tpm2_createprimary -Q -H "$auth" -g "$hash" -G "$key" -C $TMP/primary.context; then
  91. echo "Creating TPM2 primary key failed!" >&2
  92. exit 1
  93. fi
  94. if [ -n "$pcr_ids" ]; then
  95. if [ -z "$pcr_digest" ]; then
  96. if ! tpm2_pcrlist -Q -L "$pcr_bank":"$pcr_ids" -o $TMP/pcr.digest; then
  97. echo "Creating PCR hashes file failed!" >&2
  98. exit 1
  99. fi
  100. else
  101. if ! jose b64 dec -i- -O "$TMP"/pcr.digest <<< "$pcr_digest"; then
  102. echo "Error decoding PCR digest!" >&2
  103. exit 1
  104. fi
  105. fi
  106. if ! tpm2_createpolicy -Q -P -L "$pcr_bank":"$pcr_ids" -F $TMP/pcr.digest -f $TMP/pcr.policy; then
  107. echo "create policy fail, please check the environment or parameters!"
  108. exit 1
  109. fi
  110. policy_options="-L $TMP/pcr.policy"
  111. fi
  112. if ! tpm2_create -Q -g "$hash" -G "$alg_create_key" -c $TMP/primary.context -u $TMP/jwk.pub \
  113. -r $TMP/jwk.priv -A "$obj_attr" $policy_options -I- <<< "$jwk"; then
  114. echo "Creating TPM2 object for jwk failed!" >&2
  115. exit 1
  116. fi
  117. if ! jwk_pub=`jose b64 enc -I $TMP/jwk.pub`; then
  118. echo "Encoding jwk.pub in Base64 failed!" >&2
  119. exit 1
  120. fi
  121. if ! jwk_priv=`jose b64 enc -I $TMP/jwk.priv`; then
  122. echo "Encoding jwk.priv in Base64 failed!" >&2
  123. exit 1
  124. fi
  125. jwe='{"protected":{"clevis":{"pin":"tpm2","tpm2":{}}}}'
  126. jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$hash" -s hash -UUUUo-`
  127. jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$key" -s key -UUUUo-`
  128. if [ -n "$pcr_ids" ]; then
  129. jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$pcr_bank" -s pcr_bank -UUUUo-`
  130. jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$pcr_ids" -s pcr_ids -UUUUo-`
  131. fi
  132. jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$jwk_pub" -s jwk_pub -UUUUo-`
  133. jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tpm2 -q "$jwk_priv" -s jwk_priv -UUUUo-`
  134. jose jwe enc -i- -k- -I- -c < <(echo -n "$jwe$jwk"; /bin/cat)