pin-tpm2 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/bash -x
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2019 Red Hat, Inc.
  5. # Author: Sergio Correia <scorreia@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. TEST=$(basename "${0}")
  21. # Code to return to mark test as skipped.
  22. SKIP_RET_CODE=77
  23. tpm2_available() {
  24. # Old environment variables for tpm2-tools 3.0
  25. export TPM2TOOLS_TCTI_NAME=device
  26. export TPM2TOOLS_DEVICE_FILE=
  27. for dev in /dev/tpmrm?; do
  28. [ -e "${dev}" ] || continue
  29. TPM2TOOLS_DEVICE_FILE="${dev}"
  30. break
  31. done
  32. # New environment variable for tpm2-tools >= 3.1
  33. export TPM2TOOLS_TCTI="${TPM2TOOLS_TCTI_NAME}:${TPM2TOOLS_DEVICE_FILE}"
  34. if [ -z "${TPM2TOOLS_DEVICE_FILE}" ]; then
  35. echo "A TPM2 device with the in-kernel resource manager is needed!" >&2
  36. return 1
  37. fi
  38. if ! [[ -r "${TPM2TOOLS_DEVICE_FILE}" \
  39. && -w "${TPM2TOOLS_DEVICE_FILE}" ]]; then
  40. echo "The ${TPM2TOOLS_DEVICE_FILE} device must be readable and writable!" >&2
  41. return 1
  42. fi
  43. }
  44. # Checking if we can run this test.
  45. if ! tpm2_available; then
  46. exit ${SKIP_RET_CODE}
  47. fi
  48. decode_jwe() {
  49. local jwe="${1}"
  50. local coded
  51. if ! coded=$(jose jwe fmt -i- <<< "${jwe}"); then
  52. return 1
  53. fi
  54. coded=$(jose fmt -j- -g protected -u- <<< "${coded}" | tr -d '"')
  55. jose b64 dec -i- <<< "${coded}"
  56. }
  57. test_pcr_ids() {
  58. local orig="${1}"
  59. local cfg="${2}"
  60. local expected_pcr_ids="${3}"
  61. local enc
  62. if ! enc=$(echo "${orig}" | clevis encrypt tpm2 "${cfg}"); then
  63. echo "${TEST}: encrypt failed for cfg: ${cfg}" >&1
  64. return 1
  65. fi
  66. local pcr_ids
  67. pcr_ids=$(decode_jwe "${enc}" \
  68. | jose fmt -j- -Og clevis -Og tpm2 -Og pcr_ids -u- 2>/dev/null)
  69. local dec
  70. dec=$(echo "${enc}" | clevis decrypt)
  71. if [ "${orig}" != "${dec}" ]; then
  72. echo "${TEST}: decoded text (${dec}) does not match original one (${orig})" >&2
  73. return 1
  74. fi
  75. if [ "${pcr_ids}" != "${expected_pcr_ids}" ]; then
  76. echo "${TEST}: pcr_ids (${pcr_ids}) do not match the expected (${expected_pcr_ids}) result." >&2
  77. return 1
  78. fi
  79. }
  80. test_pcr_ids "${orig}" '{}' "" || exit 1
  81. test_pcr_ids "${orig}" '{ }' "" || exit 1
  82. test_pcr_ids "${orig}" '{"key": "ecc"}' "" || exit 1
  83. # Issue #103: now let's try a few different configs with both strings and
  84. # arrays and check if we get the expected pcr_ids.
  85. test_pcr_ids "${orig}" '{"pcr_ids": "16"}' "16" || exit 1
  86. test_pcr_ids "${orig}" '{"pcr_ids": ["16"]}' "16" || exit 1
  87. test_pcr_ids "${orig}" '{"pcr_ids": "4, 16"}' "4,16" || exit 1
  88. test_pcr_ids "${orig}" '{"pcr_ids": "4,16"}' "4,16" || exit 1
  89. test_pcr_ids "${orig}" '{"pcr_ids": ["4,16"]}' "4,16" || exit 1
  90. test_pcr_ids "${orig}" '{"pcr_ids": [4,16]}' "4,16" || exit 1
  91. test_pcr_ids "${orig}" '{"pcr_ids": [4, 16]}' "4,16" || exit 1
  92. test_pcr_ids "${orig}" '{"pcr_ids": ["4","16"]}' "4,16" || exit 1
  93. ! test_pcr_ids "${orig}" '{"pcr_ids": ["4","16"]}' "foo bar" || exit 1