abrmd_policysigned.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. cleanup() {
  4. rm -f session.ctx secret.dat private.pem public.pem signature.dat \
  5. signing_key.ctx policy.signed prim.ctx sealing_key.priv sealing_key.pub \
  6. unsealed.dat qual.dat to_sign.bin
  7. tpm2 flushcontext $session_ctx 2>/dev/null || true
  8. if [ "${1}" != "no-shutdown" ]; then
  9. shut_down
  10. fi
  11. }
  12. trap cleanup EXIT
  13. start_up
  14. cleanup "no-shutdown"
  15. echo "plaintext" > secret.dat
  16. # Create the signing authority
  17. openssl genrsa -out private.pem 2048
  18. openssl rsa -in private.pem -outform PEM -pubout -out public.pem
  19. # Load the verification key and Create the policysigned policy
  20. tpm2 loadexternal -C o -G rsa -u public.pem -c signing_key.ctx
  21. #
  22. # Test with policy expiration set to zero and no other dependencies
  23. #
  24. tpm2 startauthsession -S session.ctx
  25. tpm2 policysigned -S session.ctx -c signing_key.ctx -L policy.signed
  26. tpm2 flushcontext session.ctx
  27. ## Create a sealing object to use the policysigned
  28. tpm2 createprimary -C o -c prim.ctx -Q
  29. tpm2 create -u sealing_key.pub -r sealing_key.priv -c sealing_key.ctx \
  30. -C prim.ctx -i secret.dat -L policy.signed -Q
  31. ## Unseal secret
  32. tpm2 startauthsession -S session.ctx --policy-session
  33. ### Generate signature with nonceTPM, cpHashA, policyRef and expiration set to 0
  34. tpm2 policysigned -S session.ctx -c signing_key.ctx --raw-data to_sign.bin
  35. openssl dgst -sha256 -sign private.pem -out signature.dat to_sign.bin
  36. ### Satisfy policy
  37. tpm2 policysigned -S session.ctx -g sha256 -s signature.dat -f rsassa \
  38. -c signing_key.ctx
  39. ### Unseal
  40. tpm2 unseal -p session:session.ctx -c sealing_key.ctx -o unsealed.dat
  41. tpm2 flushcontext session.ctx
  42. diff secret.dat unsealed.dat
  43. rm -f unsealed.dat
  44. #
  45. # Test with policy expiration set to zero and policyref/qualifier data
  46. #
  47. dd if=/dev/urandom of=qual.dat bs=1 count=32 status=none
  48. tpm2 startauthsession -S session.ctx
  49. tpm2 policysigned -S session.ctx -c signing_key.ctx -L policy.signed -q qual.dat
  50. tpm2 flushcontext session.ctx
  51. ## Create a sealing object to use the policysigned
  52. tpm2 createprimary -C o -c prim.ctx -Q
  53. tpm2 create -u sealing_key.pub -r sealing_key.priv -c sealing_key.ctx \
  54. -C prim.ctx -i secret.dat -L policy.signed -Q
  55. ## Unseal secret
  56. tpm2 startauthsession -S session.ctx --policy-session
  57. ### Generate signature with nonceTPM, cpHashA, and expiration set to 0
  58. tpm2 policysigned -S session.ctx -c signing_key.ctx -q qual.dat \
  59. --raw-data to_sign.bin
  60. openssl dgst -sha256 -sign private.pem -out signature.dat to_sign.bin
  61. ### Satisfy policy
  62. tpm2 policysigned -S session.ctx -g sha256 -s signature.dat -f rsassa \
  63. -c signing_key.ctx -q qual.dat
  64. tpm2 unseal -p session:session.ctx -c sealing_key.ctx -o unsealed.dat
  65. tpm2 flushcontext session.ctx
  66. diff secret.dat unsealed.dat
  67. rm -f unsealed.dat
  68. #
  69. # Test with nonceTPM
  70. #
  71. tpm2 startauthsession -S session.ctx
  72. tpm2 policysigned -S session.ctx -c signing_key.ctx -L policy.signed
  73. tpm2 flushcontext session.ctx
  74. tpm2 createprimary -C o -c prim.ctx -Q
  75. tpm2 create -u sealing_key.pub -r sealing_key.priv -c sealing_key.ctx \
  76. -C prim.ctx -i secret.dat -L policy.signed -Q
  77. ## Unseal secret
  78. tpm2 startauthsession -S session.ctx --policy-session
  79. ### Generate signature
  80. tpm2 policysigned -S session.ctx -c signing_key.ctx -x --raw-data to_sign.bin
  81. openssl dgst -sha256 -sign private.pem -out signature.dat to_sign.bin
  82. ###Satisfy the policy
  83. tpm2 policysigned -S session.ctx -g sha256 -s signature.dat -f rsassa \
  84. -c signing_key.ctx -x
  85. tpm2 unseal -p session:session.ctx -c sealing_key.ctx -o unsealed.dat
  86. tpm2 flushcontext session.ctx
  87. diff secret.dat unsealed.dat
  88. rm -f unsealed.dat
  89. #
  90. # Test with cpHashA with ECDSA signature
  91. #
  92. openssl ecparam -name prime256v1 -genkey -noout -out signing_key.priv
  93. openssl ec -in signing_key.priv -outform PEM -pubout -out signing_key.pub
  94. tpm2 loadexternal -C o -G ecc -u signing_key.pub -c signing_key_pub.ctx
  95. ## Create cpHash and policy digest
  96. tpm2 dictionarylockout -c --cphash cphash.bin
  97. tpm2 startauthsession -S session.ctx
  98. tpm2 policysigned -S session.ctx -c signing_key_pub.ctx -L policy.signed \
  99. --cphash-input cphash.bin
  100. tpm2 flushcontext session.ctx
  101. ## Set lockout hierarchy authValue and policyAuth
  102. tpm2 changeauth -c l "password"
  103. tpm2 setprimarypolicy -C l -P "password" -L policy.signed -g sha256
  104. tpm2 startauthsession -S session.ctx --policy-session
  105. ### Generate signature with cpHashA
  106. tpm2 policysigned -S session.ctx -c signing_key_pub.ctx --raw-data to_sign.bin \
  107. --cphash-input cphash.bin
  108. openssl dgst -sha256 -sign signing_key.priv -out signature.dat to_sign.bin
  109. ### Satisfy policy
  110. tpm2 policysigned -S session.ctx -g sha256 -s signature.dat -f ecdsa \
  111. -c signing_key_pub.ctx --cphash cphash.bin
  112. ### Authorize
  113. tpm2 dictionarylockout -c session:session.ctx
  114. tpm2 flushcontext session.ctx
  115. rm -f signing_key.priv signing_key.pub signing_key_pub.ctx cphash.bin \
  116. session.ctx policy.signed to_sign.bin signature.dat
  117. exit 0