print.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. ak_ctx=ak.ctx
  4. ek_handle=0x81010017
  5. ak_name_file=ak.name
  6. ak_pubkey_file=ak.pub
  7. quote_file=quote.bin
  8. print_file=quote.yaml
  9. cleanup() {
  10. rm -f $ak_name_file $ak_pubkey_file \
  11. $quote_file $print_file $ak_ctx \
  12. tpmt_public.ak
  13. if [ "$1" != "no-shut-down" ]; then
  14. shut_down
  15. fi
  16. }
  17. trap cleanup EXIT
  18. start_up
  19. cleanup "no-shut-down"
  20. tpm2 clear
  21. # Create signing key
  22. tpm2 createek -Q -G rsa -c $ek_handle
  23. tpm2 createak -Q -G rsa -g sha256 -s rsassa -C $ek_handle -c $ak_ctx\
  24. -u $ak_pubkey_file -n $ak_name_file
  25. tpm2 readpublic -c $ak_ctx -f tpmt -o tpmt_public.ak
  26. tpm2 print -t TPM2B_PUBLIC $ak_pubkey_file > $print_file
  27. yaml_verify $print_file
  28. tpm2 print -t TPMT_PUBLIC tpmt_public.ak > $print_file
  29. yaml_verify $print_file
  30. # Take PCR quote
  31. tpm2 quote -Q -c $ak_ctx -l "sha256:0,2,4,9,10,11,12,17" -q "0f8beb45ac" \
  32. -m $quote_file
  33. # Print TPM's quote file
  34. tpm2 print -t TPMS_ATTEST $quote_file > $print_file
  35. # Check printed yaml
  36. python << pyscript
  37. from __future__ import print_function
  38. import sys
  39. import re
  40. import yaml
  41. with open("$print_file") as fd:
  42. yaml = yaml.safe_load(fd)
  43. assert(yaml["magic"] == "ff544347")
  44. assert(yaml["type"] == 8018)
  45. assert(yaml["extraData"] == "0f8beb45ac")
  46. quote = yaml["attested"]["quote"]
  47. # there should be only one pcr selection
  48. assert(quote["pcrSelect"]["count"] == 1)
  49. pcr_select = quote["pcrSelect"]["pcrSelections"][0]
  50. # pcr selection should match above options
  51. assert(pcr_select["hash"] == "11 (sha256)")
  52. assert(pcr_select["sizeofSelect"] == 3)
  53. assert(pcr_select["pcrSelect"] == "151e02")
  54. # pcrDigest should be lowercase hex encoded sha256sum per above options
  55. assert(re.match('^[0-9a-f]{64}$', quote["pcrDigest"]))
  56. print("OK")
  57. pyscript
  58. # negative testing
  59. trap - ERR
  60. tpm2 print $quote_file
  61. if [ $? -eq 0 ]; then
  62. echo "Expected tpm2 print without -t to fail"
  63. exit 1
  64. fi
  65. exit 0