pin-tpm2 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. local _tpm2tools_info="$(tpm2_createprimary -v)"
  44. local _match='version="(.)\.'
  45. [[ ${_tpm2tools_info} =~ ${_match} ]] && TPM2TOOLS_VERSION="${BASH_REMATCH[1]}"
  46. if [[ $TPM2TOOLS_VERSION -lt 3 ]] || [[ $TPM2TOOLS_VERSION -gt 5 ]]; then
  47. echo "The tpm2 pin requires a tpm2-tools version between 3 and 5" >&2
  48. return 1
  49. fi
  50. export TPM2TOOLS_VERSION
  51. }
  52. validate_pcrs() {
  53. local _pcr_bank="${1}"
  54. local _pcrs="${2}"
  55. [ -z "${_pcr_bank}" ] && return 1
  56. [ -z "${_pcrs}" ] && return 0
  57. local _fail=
  58. local _pcrs_r=
  59. case "${TPM2TOOLS_VERSION}" in
  60. 3) _pcrs_r="$(tpm2_pcrlist -L "${_pcr_bank}":"${_pcrs}" | grep -v "^${_pcr_bank}")" || _fail=$?;;
  61. 4|5) _pcrs_r=$(tpm2_pcrread "${_pcr_bank}":"${_pcrs}" | grep -v " ${_pcr_bank}") || _fail=$?;;
  62. *) _fail=1
  63. esac
  64. if [ -n "${_fail}" ] || [ -z "${_pcrs_r}" ]; then
  65. return 1
  66. fi
  67. return 0
  68. }
  69. # Checking if we can run this test.
  70. if ! tpm2_available; then
  71. exit ${SKIP_RET_CODE}
  72. fi
  73. decode_jwe() {
  74. local jwe="${1}"
  75. local coded
  76. if ! coded=$(jose jwe fmt -i- <<< "${jwe}"); then
  77. return 1
  78. fi
  79. coded=$(jose fmt -j- -g protected -u- <<< "${coded}" | tr -d '"')
  80. jose b64 dec -i- <<< "${coded}"
  81. }
  82. test_pcr_ids() {
  83. local orig="${1}"
  84. local cfg="${2}"
  85. local expected_pcr_ids="${3}"
  86. local enc
  87. if ! enc=$(echo "${orig}" | clevis encrypt tpm2 "${cfg}"); then
  88. echo "${TEST}: encrypt failed for cfg: ${cfg}" >&1
  89. return 1
  90. fi
  91. local pcr_ids
  92. pcr_ids=$(decode_jwe "${enc}" \
  93. | jose fmt -j- -Og clevis -Og tpm2 -Og pcr_ids -u- 2>/dev/null)
  94. local dec
  95. dec=$(echo "${enc}" | clevis decrypt)
  96. if [ "${orig}" != "${dec}" ]; then
  97. echo "${TEST}: decoded text (${dec}) does not match original one (${orig})" >&2
  98. return 1
  99. fi
  100. if [ "${pcr_ids}" != "${expected_pcr_ids}" ]; then
  101. echo "${TEST}: pcr_ids (${pcr_ids}) do not match the expected (${expected_pcr_ids}) result." >&2
  102. return 1
  103. fi
  104. }
  105. test_enc_dec() {
  106. local cfg="${1}"
  107. output=$(echo Working | clevis encrypt tpm2 "${cfg}" | clevis decrypt)
  108. if [ "$output" != "Working" ]; then
  109. echo "Output after decrypting doesn't match: ${output} != 'Working'"
  110. return 1
  111. fi
  112. }
  113. test_enc_dec '{}' || exit 1
  114. test_pcr_ids "${orig}" '{}' "" || exit 1
  115. test_pcr_ids "${orig}" '{ }' "" || exit 1
  116. test_pcr_ids "${orig}" '{"key": "ecc"}' "" || exit 1
  117. # Issue #103: now let's try a few different configs with both strings and
  118. # arrays and check if we get the expected pcr_ids.
  119. # Let's first make sure this would be a valid configuration.
  120. _default_pcr_bank="sha1"
  121. if validate_pcrs "${_default_pcr_bank}" "4,16"; then
  122. test_pcr_ids "${orig}" '{"pcr_ids": "16"}' "16" || exit 1
  123. test_pcr_ids "${orig}" '{"pcr_ids": ["16"]}' "16" || exit 1
  124. test_pcr_ids "${orig}" '{"pcr_ids": "4, 16"}' "4,16" || exit 1
  125. test_pcr_ids "${orig}" '{"pcr_ids": "4,16"}' "4,16" || exit 1
  126. test_pcr_ids "${orig}" '{"pcr_ids": ["4,16"]}' "4,16" || exit 1
  127. test_pcr_ids "${orig}" '{"pcr_ids": [4,16]}' "4,16" || exit 1
  128. test_pcr_ids "${orig}" '{"pcr_ids": [4, 16]}' "4,16" || exit 1
  129. test_pcr_ids "${orig}" '{"pcr_ids": ["4","16"]}' "4,16" || exit 1
  130. ! test_pcr_ids "${orig}" '{"pcr_ids": ["4","16"]}' "foo bar" || exit 1
  131. else
  132. echo "Skipping tests related to issue#103 because the combination of pcr_bank and PCRs is invalid" >&2
  133. fi
  134. # Test with policies if we have the PIN rewrite available
  135. if ! $(which clevis-pin-tpm2 >/dev/null 2>&1);
  136. then
  137. echo "No PIN rewrite available"
  138. exit 0
  139. fi
  140. if ! $(which clevis-pin-tpm2-signtool >/dev/null 2>&1);
  141. then
  142. echo "No policy signtool available"
  143. exit 0
  144. fi
  145. function on_exit() {
  146. popd
  147. if [ ! -d "$TMP" ] || ! rm -rf "$TMP"; then
  148. echo "Delete temporary files failed!" >&2
  149. echo "You need to clean up: $TMP" >&2
  150. exit 1
  151. fi
  152. }
  153. if ! TMP="$(mktemp -d)"; then
  154. echo "Creating a temporary dir for TPM files failed!" >&2
  155. exit 1
  156. fi
  157. trap 'on_exit' EXIT
  158. pushd $TMP
  159. clevis-pin-tpm2-signtool >policy_working.json << EOP
  160. ---
  161. - policy_ref:
  162. steps:
  163. - PCRs:
  164. hash_algorithm: sha256
  165. selection:
  166. - pcr_id: 21
  167. value: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  168. - pcr_id: 22
  169. value: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  170. EOP
  171. clevis-pin-tpm2-signtool >policy_broken.json << EOP
  172. ---
  173. - policy_ref:
  174. steps:
  175. - PCRs:
  176. hash_algorithm: sha256
  177. selection:
  178. - pcr_id: 21
  179. value: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
  180. - pcr_id: 22
  181. value: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  182. EOP
  183. test_enc_dec '{"policy_pubkey_path":"./publickey.json", "policy_ref": "", "policy_path": "./policy_working.json"}' || exit 1
  184. ! test_enc_dec '{"policy_pubkey_path":"./publickey.json", "policy_ref": "", "policy_path": "./policy_broken.json"}' || exit 1