abrmd_policynvwritten.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. POLICYSESSION=1
  4. session_file=session.dat
  5. cleanup() {
  6. rm -f $session_file nvwrite.policy
  7. if [ "${1}" != "no-shutdown" ]; then
  8. shut_down
  9. fi
  10. }
  11. setup_nv_written_policy() {
  12. if [ $1 == $POLICYSESSION ];then
  13. tpm2 startauthsession -S $session_file --policy-session
  14. else
  15. tpm2 startauthsession -S $session_file
  16. fi
  17. tpm2 policycommandcode -S $session_file TPM2_CC_NV_Write
  18. tpm2 policynvwritten -S $session_file -L nvwrite.policy c
  19. }
  20. trap cleanup EXIT
  21. start_up
  22. cleanup "no-shutdown"
  23. tpm2 clear
  24. # Create a write once NV index. To do this the NV index is defined with a write
  25. # policy that is valid only if the NV index attribute "TPMA_NV_WRITTEN" was
  26. # never set.
  27. #Define the NV index write policy
  28. setup_nv_written_policy
  29. tpm2 flushcontext $session_file
  30. # Define the NV index with the policy
  31. tpm2 nvdefine -s 1 -a "authread|policywrite" -p nvrdpass -L nvwrite.policy
  32. # Write the NV index by satisfying the policy
  33. setup_nv_written_policy $POLICYSESSION
  34. echo 0xAA | xxd -r -p | tpm2 nvwrite 0x01000000 -i- -P session:$session_file
  35. tpm2 flushcontext $session_file
  36. # Attempt writing the NV index again
  37. setup_nv_written_policy $POLICYSESSION
  38. trap - ERR
  39. echo 0xAA | xxd -r -p | tpm2 nvwrite 0x01000000 -i- -P session:$session_file
  40. if [ $? != 1 ];then
  41. echo "FAIL: Expected tpm2 policynvwritten to fail!"
  42. exit 1
  43. fi
  44. trap onerror ERR
  45. tpm2 flushcontext session.dat
  46. exit 0