abrmd_policytemplate.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. cleanup() {
  4. rm -f prim.ctx template.data template.hash policy.template key.pub key.priv
  5. tpm2 flushcontext session.ctx 2>/dev/null || true
  6. if [ "${1}" != "no-shutdown" ]; then
  7. shut_down
  8. fi
  9. rm -f session.ctx
  10. }
  11. trap cleanup EXIT
  12. start_up
  13. cleanup "no-shutdown"
  14. #
  15. # Restrict the primary object type created under a hierarchy
  16. #
  17. ## Create a key template
  18. tpm2 createprimary -C o -c prim.ctx --template-data template.data
  19. cat template.data | openssl dgst -sha256 -binary -out template.hash
  20. ## Create the policytemplate
  21. tpm2 startauthsession -S session.ctx -g sha256
  22. tpm2 policytemplate -S session.ctx -L policy.template \
  23. --template-hash template.hash
  24. tpm2 flushcontext session.ctx
  25. ## Set the owner hierarchy policy to create primary keys of specific template
  26. tpm2 setprimarypolicy -C o -g sha256 -L policy.template
  27. ## Satisfy the policy and create a primary key
  28. tpm2 startauthsession -S session.ctx -g sha256 --policy-session
  29. tpm2 policytemplate -S session.ctx --template-hash template.hash
  30. tpm2 createprimary -C o -c prim2.ctx -P session:session.ctx
  31. tpm2 flushcontext session.ctx
  32. ## Attempt to create a primary key with a different template
  33. tpm2 startauthsession -S session.ctx -g sha256 --policy-session
  34. tpm2 policytemplate -S session.ctx --template-hash template.hash
  35. trap - ERR
  36. tpm2 createprimary -C o -G ecc -c prim2.ctx -P session:session.ctx
  37. if [ $? == 0 ];then
  38. echo "ERROR: Expected tpm2 createprimary should fail!"
  39. exit 1
  40. fi
  41. trap onerror ERR
  42. tpm2 flushcontext session.ctx
  43. #
  44. # Restrict the object type created under a primary key
  45. #
  46. tpm2 clear
  47. ## Create a key template
  48. tpm2 createprimary -C o -c prim.ctx -Q
  49. tpm2 create -C prim.ctx -u key.pub -r key.priv --template-data template.data -Q
  50. cat template.data | openssl dgst -sha256 -binary -out template.hash
  51. rm -f prim.ctx key.pub key.priv template.data
  52. ## Create the policytemplate
  53. tpm2 startauthsession -S session.ctx -g sha256
  54. tpm2 policytemplate -S session.ctx -L policy.template \
  55. --template-hash template.hash
  56. tpm2 flushcontext session.ctx
  57. ## Set the primary key auth policy to create keys of specific template
  58. tpm2 createprimary -C o -c prim.ctx -L policy.template -Q
  59. ## Satisfy the policy and create a key
  60. tpm2 startauthsession -S session.ctx -g sha256 --policy-session
  61. tpm2 policytemplate -S session.ctx --template-hash template.hash
  62. tpm2 create -C prim.ctx -u key.pub -r key.priv -P session:session.ctx -Q
  63. tpm2 flushcontext session.ctx
  64. ## Attempt to create a key with a different template
  65. tpm2 startauthsession -S session.ctx -g sha256 --policy-session
  66. tpm2 policytemplate -S session.ctx --template-hash template.hash
  67. trap - ERR
  68. tpm2 create -C prim.ctx -G ecc -u key.pub -r key.priv -P session:session.ctx
  69. if [ $? == 0 ];then
  70. echo "ERROR: Expected tpm2 createprimary should fail!"
  71. exit 1
  72. fi
  73. trap onerror ERR
  74. tpm2 flushcontext session.ctx
  75. exit 0