abrmd_extended-sessions.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. alg_primary_obj=sha256
  4. alg_primary_key=ecc
  5. alg_create_obj=sha256
  6. alg_pcr_policy=sha1
  7. pcr_ids=0,1,2,3
  8. file_pcr_value=pcr.bin
  9. file_input_data=secret.data
  10. file_policy=policy.data
  11. file_primary_key_ctx=context.p_"$alg_primary_obj"_"$alg_primary_key"
  12. file_unseal_key_pub=opu_"$alg_create_obj"
  13. file_unseal_key_priv=opr_"$alg_create_obj"
  14. file_unseal_key_ctx=ctx_load_out_"$alg_primary_obj"_"$alg_primary_key"-\
  15. "$alg_create_obj"
  16. file_unseal_key_name=name.load_"$alg_primary_obj"_"$alg_primary_key"-\
  17. "$alg_create_obj"
  18. file_unseal_output_data=usl_"$file_unseal_key_ctx"
  19. file_session_file=session.dat
  20. secret=12345678
  21. cleanup() {
  22. rm -f $file_input_data $file_primary_key_ctx $file_unseal_key_pub \
  23. $file_unseal_key_priv $file_unseal_key_ctx $file_unseal_key_name \
  24. $file_unseal_output_data $file_pcr_value \
  25. $file_policy $file_session_file
  26. tpm2 flushcontext $file_session_file 2>/dev/null || true
  27. if [ "${1}" != "no-shutdown" ]; then
  28. shut_down
  29. fi
  30. }
  31. trap cleanup EXIT
  32. start_up
  33. cleanup "no-shutdown"
  34. echo $secret > $file_input_data
  35. tpm2 clear
  36. #
  37. # Test an extended policy session beyond client connections. This is ONLY
  38. # supported by abrmd since version: https://github.com/tpm2-software/tpm2-abrmd/
  39. # releases/tag/1.2.0 However, bug: https://github.com/tpm2-software/tpm2-abrmd/
  40. # issues/285 applies.
  41. #
  42. # The test works by:
  43. # Step 1: Creating a trial session and updating it with a policyPCR event to
  44. # generate a policy hash.
  45. #
  46. # Step 2: Creating an object and using that policy hash as the policy to satisfy
  47. # for usage.
  48. #
  49. # Step 3: Creating an actual policy session and using pcrpolicy event to update
  50. # the policy.
  51. #
  52. # Step 4: Using that actual policy session from step 3 in tpm2 unseal to unseal
  53. # the object.
  54. #
  55. tpm2 createprimary -Q -C e -g $alg_primary_obj -G $alg_primary_key \
  56. -c $file_primary_key_ctx
  57. tpm2 pcrread -Q -o $file_pcr_value ${alg_pcr_policy}:${pcr_ids}
  58. tpm2 startauthsession -Q -S $file_session_file
  59. tpm2 policypcr -Q -S $file_session_file -l ${alg_pcr_policy}:${pcr_ids} \
  60. -f $file_pcr_value -L $file_policy
  61. tpm2 flushcontext $file_session_file
  62. tpm2 create -Q -g $alg_create_obj -u $file_unseal_key_pub \
  63. -r $file_unseal_key_priv -i- -C $file_primary_key_ctx -L $file_policy \
  64. -a 'fixedtpm|fixedparent' <<< $secret
  65. tpm2 load -Q -C $file_primary_key_ctx -u $file_unseal_key_pub \
  66. -r $file_unseal_key_priv -n $file_unseal_key_name -c $file_unseal_key_ctx
  67. rm $file_session_file
  68. # Start a REAL encrypted and bound policy session (-a option) and perform a pcr
  69. # policy event
  70. tpm2 startauthsession --policy-session -c $file_primary_key_ctx \
  71. -S $file_session_file
  72. tpm2 policypcr -Q -S $file_session_file -l ${alg_pcr_policy}:${pcr_ids} \
  73. -f $file_pcr_value -L $file_policy
  74. unsealed=`tpm2 unseal -p"session:$file_session_file" -c $file_unseal_key_ctx`
  75. test "$unsealed" == "$secret"
  76. # Test resetting the policy session causes unseal to fail.
  77. tpm2 policyrestart -S $file_session_file
  78. # negative test, clear the error handler
  79. if tpm2 unseal -p"session:$file_session_file" \
  80. -c $file_unseal_key_ctx 2>/dev/null; then
  81. echo "Expected tpm2 unseal to fail after policy reset"
  82. exit 1
  83. else
  84. true
  85. fi
  86. # Test bounded sessions work with bind entities with auth
  87. tpm2 createprimary -Q -C o -c prim.ctx -p primepass
  88. ## Test with bounded and salted session
  89. tpm2 startauthsession -S session.ctx --hmac-session --tpmkey-context prim.ctx \
  90. --bind-context prim.ctx --bind-auth primepass
  91. tpm2 sessionconfig session.ctx --enable-encrypt --enable-decrypt
  92. tpm2 getrandom 8 -S session.ctx
  93. tpm2 flushcontext session.ctx
  94. ## Test with bounded only session
  95. tpm2 startauthsession -S session.ctx --hmac-session \
  96. --bind-context prim.ctx --bind-auth primepass
  97. tpm2 sessionconfig session.ctx --enable-encrypt --enable-decrypt
  98. tpm2 getrandom 8 -S session.ctx
  99. tpm2 flushcontext session.ctx
  100. ## Test with salted only session
  101. tpm2 startauthsession -S session.ctx --hmac-session \
  102. --tpmkey-context prim.ctx
  103. tpm2 sessionconfig session.ctx --enable-encrypt --enable-decrypt
  104. tpm2 getrandom 8 -S session.ctx
  105. tpm2 flushcontext session.ctx
  106. exit 0