pin-tpm2 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_enc_dec() {
  81. local cfg="${1}"
  82. output=$(echo Working | clevis encrypt tpm2 "${cfg}" | clevis decrypt)
  83. if [ "$output" != "Working" ]; then
  84. echo "Output after decrypting doesn't match: ${output} != 'Working'"
  85. return 1
  86. fi
  87. }
  88. test_enc_dec '{}' || exit 1
  89. test_pcr_ids "${orig}" '{}' "" || exit 1
  90. test_pcr_ids "${orig}" '{ }' "" || exit 1
  91. test_pcr_ids "${orig}" '{"key": "ecc"}' "" || exit 1
  92. # Issue #103: now let's try a few different configs with both strings and
  93. # arrays and check if we get the expected pcr_ids.
  94. test_pcr_ids "${orig}" '{"pcr_ids": "16"}' "16" || exit 1
  95. test_pcr_ids "${orig}" '{"pcr_ids": ["16"]}' "16" || exit 1
  96. test_pcr_ids "${orig}" '{"pcr_ids": "4, 16"}' "4,16" || exit 1
  97. test_pcr_ids "${orig}" '{"pcr_ids": "4,16"}' "4,16" || exit 1
  98. test_pcr_ids "${orig}" '{"pcr_ids": ["4,16"]}' "4,16" || exit 1
  99. test_pcr_ids "${orig}" '{"pcr_ids": [4,16]}' "4,16" || exit 1
  100. test_pcr_ids "${orig}" '{"pcr_ids": [4, 16]}' "4,16" || exit 1
  101. test_pcr_ids "${orig}" '{"pcr_ids": ["4","16"]}' "4,16" || exit 1
  102. ! test_pcr_ids "${orig}" '{"pcr_ids": ["4","16"]}' "foo bar" || exit 1
  103. # Test with policies if we have the PIN rewrite available
  104. if ! $(which clevis-pin-tpm2 >/dev/null 2>&1);
  105. then
  106. echo "No PIN rewrite available"
  107. exit 0
  108. fi
  109. if ! $(which clevis-pin-tpm2-signtool >/dev/null 2>&1);
  110. then
  111. echo "No policy signtool available"
  112. exit 0
  113. fi
  114. function on_exit() {
  115. popd
  116. if [ ! -d "$TMP" ] || ! rm -rf "$TMP"; then
  117. echo "Delete temporary files failed!" >&2
  118. echo "You need to clean up: $TMP" >&2
  119. exit 1
  120. fi
  121. }
  122. if ! TMP="$(mktemp -d)"; then
  123. echo "Creating a temporary dir for TPM files failed!" >&2
  124. exit 1
  125. fi
  126. trap 'on_exit' EXIT
  127. pushd $TMP
  128. clevis-pin-tpm2-signtool >policy_working.json << EOP
  129. ---
  130. - policy_ref:
  131. steps:
  132. - PCRs:
  133. hash_algorithm: sha256
  134. selection:
  135. - pcr_id: 21
  136. value: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  137. - pcr_id: 22
  138. value: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  139. EOP
  140. clevis-pin-tpm2-signtool >policy_broken.json << EOP
  141. ---
  142. - policy_ref:
  143. steps:
  144. - PCRs:
  145. hash_algorithm: sha256
  146. selection:
  147. - pcr_id: 21
  148. value: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
  149. - pcr_id: 22
  150. value: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  151. EOP
  152. test_enc_dec '{"policy_pubkey_path":"./publickey.json", "policy_ref": "", "policy_path": "./policy_working.json"}' || exit 1
  153. ! test_enc_dec '{"policy_pubkey_path":"./publickey.json", "policy_ref": "", "policy_path": "./policy_broken.json"}' || exit 1