abrmd_policyauthorizenv.sh 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. nv_test_index=0x01500001
  4. cleanup() {
  5. tpm2 nvundefine -Q -C o $nv_test_index 2>/dev/null || true
  6. tpm2 flushcontext -t
  7. tpm2 flushcontext -l
  8. tpm2 flushcontext -s
  9. rm -f session.ctx policy.pass policyauthorizenv.1500001 prim.ctx key.pub \
  10. key.priv key.ctx policy.pcr0
  11. if [ "$1" != "no-shut-down" ]; then
  12. shut_down
  13. fi
  14. }
  15. trap cleanup EXIT
  16. start_up
  17. cleanup "no-shut-down"
  18. tpm2 clear
  19. # Define the test NV Index to store the auth policy
  20. tpm2 nvdefine -C o -p nvpass $nv_test_index -a "authread|authwrite" -s 34
  21. # Define the auth policy
  22. tpm2 startauthsession -S session.ctx
  23. tpm2 policypassword -S session.ctx -L policy.pass
  24. tpm2 flushcontext session.ctx
  25. # Write the auth policy to the NV Index
  26. POLICYDIGESTALGORITHM=000b
  27. echo $POLICYDIGESTALGORITHM | xxd -p -r | cat - policy.pass | \
  28. tpm2 nvwrite -C $nv_test_index -P nvpass $nv_test_index -i-
  29. # Define the policyauthorizenv
  30. tpm2 startauthsession -S session.ctx
  31. tpm2 policyauthorizenv -S session.ctx -C $nv_test_index -P nvpass \
  32. -L policyauthorizenv.1500001 $nv_test_index
  33. tpm2 flushcontext session.ctx
  34. # Create and load a sealing object with auth policy = policyauthorizenv
  35. tpm2 createprimary -C o -c prim.ctx
  36. echo "secretdata" | \
  37. tpm2 create -C prim.ctx -u key.pub -r key.priv \
  38. -a "fixedtpm|fixedparent|adminwithpolicy" -L policyauthorizenv.1500001 -i-
  39. tpm2 load -C prim.ctx -u key.pub -r key.priv -c key.ctx
  40. # Satisfy the auth policy stored in the NV Index and thus policyauthorizenv
  41. # And attempt user operation UNSEAL
  42. tpm2 startauthsession -S session.ctx --policy-session
  43. tpm2 policypassword -S session.ctx
  44. tpm2 policyauthorizenv -S session.ctx -C $nv_test_index -P nvpass $nv_test_index
  45. tpm2 unseal -c key.ctx -p session:session.ctx
  46. tpm2 flushcontext session.ctx
  47. # Define another auth policy and write to the NV Index
  48. tpm2 startauthsession -S session.ctx
  49. tpm2 policypcr -S session.ctx -l sha1:23 -L policy.pcr0
  50. tpm2 flushcontext session.ctx
  51. echo "000b" | xxd -p -r | cat - policy.pcr0 | \
  52. tpm2 nvwrite -C $nv_test_index -P nvpass $nv_test_index -i-
  53. # Satisfy the auth policy = policypassword not stored in the NV Index and
  54. # then the policyauthorizenv and
  55. # attempt user operation UNSEAL
  56. tpm2 startauthsession -S session.ctx --policy-session
  57. tpm2 policypassword -S session.ctx
  58. # should fail
  59. trap - ERR
  60. tpm2 policyauthorizenv -S session.ctx -C $nv_test_index -P nvpass $nv_test_index
  61. if [ $? != 1 ];then
  62. echo "FAIL:tpm2 policyauthorizenv didn't fail!"
  63. exit 1
  64. fi
  65. trap onerror ERR
  66. tpm2 flushcontext session.ctx
  67. # Satisfy the auth policy stored in the NV Index and thus policyauthorizenv
  68. # And attempt user operation UNSEAL
  69. tpm2 startauthsession -S session.ctx --policy-session
  70. tpm2 policypcr -S session.ctx -l sha1:23 -L policy.pcr0
  71. tpm2 policyauthorizenv -S session.ctx -C $nv_test_index -P nvpass $nv_test_index
  72. tpm2 unseal -c key.ctx -p session:session.ctx
  73. tpm2 flushcontext session.ctx
  74. exit 0