unseal.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. pcr_specification=sha256:0,1,2,3+sha1:0,1,2,3
  7. file_pcr_value=pcr.bin
  8. file_input_data=secret.data
  9. file_policy=policy.data
  10. file_primary_key_ctx=context.p_"$alg_primary_obj"_"$alg_primary_key"
  11. file_unseal_key_pub=opu_"$alg_create_obj"
  12. file_unseal_key_priv=opr_"$alg_create_obj"
  13. file_unseal_key_ctx=ctx_load_out_"$alg_primary_obj"_"$alg_primary_key"-\
  14. "$alg_create_obj"
  15. file_unseal_key_name=name.load_"$alg_primary_obj"_"$alg_primary_key"-\
  16. "$alg_create_obj"
  17. file_unseal_output_data=usl_"$file_unseal_key_ctx"
  18. secret="12345678"
  19. cleanup() {
  20. rm -f $file_input_data $file_primary_key_ctx $file_unseal_key_pub \
  21. $file_unseal_key_priv $file_unseal_key_ctx $file_unseal_key_name \
  22. $file_unseal_output_data $file_pcr_value $file_policy
  23. if [ "$1" != "no-shut-down" ]; then
  24. shut_down
  25. fi
  26. }
  27. trap cleanup EXIT
  28. start_up
  29. cleanup "no-shut-down"
  30. echo $secret > $file_input_data
  31. tpm2 clear
  32. tpm2 createprimary -Q -C e -g $alg_primary_obj -G $alg_primary_key \
  33. -c $file_primary_key_ctx
  34. tpm2 create -Q -g $alg_create_obj -u $file_unseal_key_pub \
  35. -r $file_unseal_key_priv -i $file_input_data -C $file_primary_key_ctx
  36. tpm2 load -Q -C $file_primary_key_ctx -u $file_unseal_key_pub \
  37. -r $file_unseal_key_priv -n $file_unseal_key_name -c $file_unseal_key_ctx
  38. tpm2 unseal -Q -c $file_unseal_key_ctx -o $file_unseal_output_data
  39. cmp -s $file_unseal_output_data $file_input_data
  40. # Test -i using stdin via pipe
  41. rm $file_unseal_key_pub $file_unseal_key_priv $file_unseal_key_name \
  42. $file_unseal_key_ctx
  43. cat $file_input_data | tpm2 create -Q -g $alg_create_obj \
  44. -u $file_unseal_key_pub -r $file_unseal_key_priv -i- -C $file_primary_key_ctx
  45. tpm2 load -Q -C $file_primary_key_ctx -u $file_unseal_key_pub \
  46. -r $file_unseal_key_priv -n $file_unseal_key_name -c $file_unseal_key_ctx
  47. tpm2 unseal -Q -c $file_unseal_key_ctx -o $file_unseal_output_data
  48. cmp -s $file_unseal_output_data $file_input_data
  49. # Test using a PCR policy for auth and use file based stdin for -i
  50. rm $file_unseal_key_pub $file_unseal_key_priv $file_unseal_key_name \
  51. $file_unseal_key_ctx
  52. tpm2 pcrread -Q -o $file_pcr_value $pcr_specification
  53. tpm2 createpolicy -Q --policy-pcr -l $pcr_specification -f $file_pcr_value \
  54. -L $file_policy
  55. tpm2 create -Q -g $alg_create_obj -u $file_unseal_key_pub \
  56. -r $file_unseal_key_priv -i- -C $file_primary_key_ctx -L $file_policy \
  57. -a 'fixedtpm|fixedparent' <<< $secret
  58. tpm2 load -Q -C $file_primary_key_ctx -u $file_unseal_key_pub \
  59. -r $file_unseal_key_priv -n $file_unseal_key_name -c $file_unseal_key_ctx
  60. unsealed=`tpm2 unseal -V --object-context $file_unseal_key_ctx \
  61. -p pcr:$pcr_specification=$file_pcr_value`
  62. test "$unsealed" == "$secret"
  63. # Test that unseal fails if a PCR policy isn't provided
  64. trap - ERR
  65. tpm2 unseal -c $file_unseal_key_ctx 2> /dev/null
  66. if [ $? != 1 ]; then
  67. echo "tpm2 unseal didn't fail without a PCR policy!"
  68. exit 1
  69. fi
  70. # Test that unseal fails if PCR state isn't the same as the defined PCR policy
  71. tpm2 pcrextend 0:sha1=6c10289a8da7f774cf67bd2fc8502cd4b585346a
  72. tpm2 unseal -c $file_unseal_key_ctx -p pcr:$pcr_specification 2> /dev/null
  73. if [ $? != 1 ]; then
  74. echo "tpm2 unseal didn't fail with a PCR state different than the policy!"
  75. exit 1
  76. fi
  77. # Test that the object can be unsealed without a policy but a password
  78. trap onerror ERR
  79. rm $file_unseal_key_pub $file_unseal_key_priv $file_unseal_key_name \
  80. $file_unseal_key_ctx
  81. tpm2 pcrread -Q -o $file_pcr_value $pcr_specification
  82. tpm2 createpolicy -Q --policy-pcr -l $pcr_specification -f $file_pcr_value \
  83. -L $file_policy
  84. tpm2 create -Q -g $alg_create_obj -u $file_unseal_key_pub \
  85. -r $file_unseal_key_priv -i- -C $file_primary_key_ctx -L $file_policy \
  86. -p secretpass <<< $secret
  87. tpm2 load -Q -C $file_primary_key_ctx -u $file_unseal_key_pub \
  88. -r $file_unseal_key_priv -n $file_unseal_key_name -c $file_unseal_key_ctx
  89. unsealed=`tpm2 unseal -c $file_unseal_key_ctx -p secretpass`
  90. test "$unsealed" == "$secret"
  91. # Test that unseal fails when using a wrong password
  92. trap - ERR
  93. tpm2 unseal -c $file_unseal_key_ctx -p wrongpass 2> /dev/null
  94. if [ $? != 3 ]; then
  95. echo "tpm2 unseal didn't fail when using a wrong object password!"
  96. exit 1
  97. fi
  98. # Test unsealing with encrypted sessions
  99. trap onerror ERR
  100. tpm2 createprimary -Q -C o -c prim.ctx
  101. tpm2 startauthsession -S enc_session.ctx --hmac-session -c prim.ctx
  102. tpm2 sessionconfig enc_session.ctx --disable-encrypt
  103. tpm2 create -Q -C prim.ctx -u seal_key.pub -r seal_key.priv -c seal_key.ctx \
  104. -p sealkeypass -i- <<< $secret -S enc_session.ctx
  105. tpm2 sessionconfig enc_session.ctx --enable-encrypt
  106. unsealed=`tpm2 unseal -c seal_key.ctx -p sealkeypass -S enc_session.ctx`
  107. test "$unsealed" == "$secret"
  108. tpm2 flushcontext enc_session.ctx
  109. exit 0