rc_decode.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. # We don't need a TPM for this test, so unset the EXIT handler.
  4. trap - EXIT
  5. # Since this only tests rc_decode tool it on FreeBSD
  6. if [ "$OS" == "FreeBSD" ]; then
  7. exit 0
  8. fi
  9. #
  10. # codes was generated from the TPM2_RC constants in:
  11. # https://github.com/tpm2-software/tpm2-tss/blob/master/include/sapi/tss2_tpm2 types.h#L68
  12. # Some of these may not be used correctly, which is OK, as tpm2 rc_decode never
  13. # fails and should attempt to decode it or print some unknown status. This gives
  14. # us coverage for both known and unknown/malformed inputs.
  15. #
  16. # Details on error code encoding can be found at:
  17. # Section 6.6.2 of t "Trusted Platform Module Library Part 2: Structures Family “2.0” Level 00 Revision 01.38"
  18. # - https://trustedcomputinggroup.org/wp-content/uploads/TPM-Rev-2.0-Part-2-Structures-01.38.pdf
  19. #
  20. declare -A codes
  21. tss2_tpm2_types=''
  22. for dir in "$(pkg-config --variable includedir tss2-esys)" \
  23. /usr/local/include /usr/include; do
  24. if [ -f "$dir/tss2/tss2_tpm2_types.h" ]; then
  25. tss2_tpm2_types="$dir/tss2/tss2_tpm2_types.h"
  26. break
  27. fi
  28. done
  29. if [ -z "$tss2_tpm2_types" ]; then
  30. echo "Could not find TSS2 headers"
  31. exit 1
  32. fi
  33. # Populate the main TPM2_RC values
  34. eval $(grep -Po "^#define \K(TPM2_RC.*)" "$tss2_tpm2_types" \
  35. | grep -v '+' \
  36. | sed "s%/*[^/]*/$%%g" \
  37. | sed "s%[[:space:]]*((TPM2_RC)[[:space:]]*%=%g" \
  38. | sed "s%)%%g")
  39. # Generate the TPM2_RC array based on TSS2 header
  40. varlist="$(sed -rn "s%^#define (TPM2_RC_[^[:space:]]*)[[:space:]]*\(\(TPM2_RC\) \((TPM2_RC[^\)]*)[^/]*/\* ([^\*]*) \*/%\1=\$\(\(\2\)\):\3%p" "$tss2_tpm2_types")"
  41. while IFS='=' read key value; do
  42. codes[${key}]="${value}"
  43. done <<< "${varlist}"
  44. fail=0
  45. for key in "${!codes[@]}"; do
  46. value="$(printf '0x%03x' "$(eval echo ${codes[$key]%%:*})")"
  47. expected_msg="${codes[$key]##*:}"
  48. received_msg="$(tpm2 rc_decode ${value} | cut -d':' -f3)"
  49. if ! grep -iq "${received_msg# }" <<< "${expected_msg}"; then
  50. echo "$value raised an invalid error message"
  51. echo " - Expected : ${expected_msg}"
  52. echo " - Seen : ${received_msg# }"
  53. fail=1
  54. fi
  55. done
  56. #
  57. # Negative tests
  58. #
  59. if tpm2 rc_decode 0x6666329840938498293849238 &>/dev/null; then
  60. echo "Expected \"$cmd\" to fail."
  61. fail=1
  62. else
  63. true
  64. fi
  65. exit "$fail"