createprimary.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. start_up
  4. cleanup() {
  5. rm -f policy.bin obj.pub pub.out primary.ctx
  6. if [ $(ina "$@" "keep-context") -ne 0 ]; then
  7. rm -f context.out
  8. fi
  9. if [ $(ina "$@" "no-shut-down") -ne 0 ]; then
  10. shut_down
  11. fi
  12. }
  13. trap cleanup EXIT
  14. cleanup "no-shut-down"
  15. # Keep the algorithm specifiers mixed to test friendly and raw
  16. # values.
  17. for gAlg in `populate_hash_algs 'and alg != "keyedhash"'`; do
  18. for GAlg in rsa xor ecc aes; do
  19. echo tpm2 createprimary -Q -g $gAlg -G $GAlg -c context.out
  20. tpm2 createprimary -Q -g $gAlg -G $GAlg -c context.out
  21. cleanup "no-shut-down" "keep-context"
  22. for Atype in o e n; do
  23. tpm2 createprimary -Q -C $Atype -g $gAlg -G $GAlg -c context.out
  24. cleanup "no-shut-down" "keep-context"
  25. done
  26. done
  27. done
  28. policy_orig=f28230c080bbe417141199e36d18978228d8948fc10a6a24921b9eba6bb1d988
  29. #test for createprimary objects with policy authorization structures
  30. echo -n "$policy_orig" | xxd -r -p > policy.bin
  31. tpm2 createprimary -Q -C o -G rsa -g sha256 -c context.out -L policy.bin \
  32. -a 'restricted|decrypt|fixedtpm|fixedparent|sensitivedataorigin'
  33. tpm2 readpublic -c context.out > pub.out
  34. policy_new=$(yaml_get_kv pub.out "authorization policy")
  35. test "$policy_orig" == "$policy_new"
  36. #
  37. # Test that -u can be specified to pass a TPMU_PUBLIC_ID union
  38. #
  39. # in this case TPM2B_PUBLIC_KEY_RSA (256 bytes of zero)
  40. printf '\x00\x01' > ud.1
  41. dd if=/dev/zero bs=256 count=1 of=ud.2
  42. cat ud.1 ud.2 > unique.dat
  43. tpm2 createprimary -C o -G rsa2048:aes128cfb -g sha256 -c prim.ctx \
  44. -a "restricted|decrypt|fixedtpm|fixedparent|sensitivedataorigin|userwithauth|\
  45. noda" -u unique.dat
  46. test -f prim.ctx
  47. rm -f prim.ctx
  48. # test the case with ECC key type with MAX_ECC_KEY_BITS=256
  49. printf '\x20\x00' > ecc_param_buf_size
  50. dd if=/dev/urandom bs=32 count=1 of=ecc_param_buf
  51. cat ecc_param_buf_size ecc_param_buf ecc_param_buf_size ecc_param_buf > unique.dat
  52. tpm2 createprimary -C o -G ecc -g sha256 -c prim.ctx -u unique.dat \
  53. -a "restricted|decrypt|fixedtpm|fixedparent|sensitivedataorigin|userwithauth|noda"
  54. test -f prim.ctx
  55. rm -f prim.ctx
  56. # test the case with RSA and unique data specified via stdin
  57. dd if=/dev/urandom bs=256 count=1 status=none | \
  58. tpm2 createprimary -C o -G rsa -g sha256 -c prim.ctx -u - \
  59. -a "restricted|decrypt|fixedtpm|fixedparent|sensitivedataorigin|userwithauth|noda"
  60. test -f prim.ctx
  61. rm -f prim.ctx
  62. # test the case with ECC and unique data specified via stdin
  63. dd if=/dev/urandom bs=96 count=1 status=none | \
  64. tpm2 createprimary -C o -G ecc -g sha256 -c prim.ctx -u - \
  65. -a "restricted|decrypt|fixedtpm|fixedparent|sensitivedataorigin|userwithauth|noda"
  66. test -f prim.ctx
  67. rm -f prim.ctx
  68. # Test that -g/-G do not need to be specified.
  69. tpm2 createprimary -Q -c context.out
  70. # Test that -o does not need to be specified.
  71. tpm2 createprimary -Q
  72. # Test that creation data has the specified outside info
  73. dd if=/dev/urandom of=outside.info bs=1 count=32
  74. tpm2 createprimary -C o -c context.out --creation-data creation.data \
  75. -q outside.info
  76. xxd -p creation.data | tr -d '\n' | grep `xxd -p outside.info | tr -d '\n'`
  77. # Test that selected pcrs digest is present in the creation data
  78. tpm2 pcrread sha256:0 -o pcr_data.bin
  79. tpm2 createprimary -C o -c context.out --creation-data creation.data \
  80. -l sha256:0
  81. xxd -p creation.data | tr -d '\n' | \
  82. grep `cat pcr_data.bin | openssl dgst -sha256 -binary | xxd -p | tr -d '\n'`
  83. # Test for session leaks
  84. BEFORE=$(tpm2 getcap handles-loaded-session; tpm2 getcap handles-saved-session)
  85. tpm2 createprimary -Q
  86. AFTER=$(tpm2 getcap handles-loaded-session; tpm2 getcap handles-saved-session)
  87. test "${BEFORE}" = "${AFTER}"
  88. exit 0