sessionaudit.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. cleanup() {
  4. rm -f \
  5. prim.ctx signing_key.ctx signing_key.pub signing_key.priv \
  6. att.data att.sig cp.hash rp.hash cphash.bin rphash.bin zero.bin
  7. if [ "${1}" != "no-shutdown" ]; then
  8. shut_down
  9. fi
  10. }
  11. trap cleanup EXIT
  12. start_up
  13. cleanup "no-shutdown"
  14. #
  15. # Get audit digest for a TPM command TPM2_GetRandom using and audit session
  16. #
  17. tpm2 clear
  18. tpm2 createprimary -Q -C e -c prim.ctx
  19. tpm2 create -Q -C prim.ctx -c signing_key.ctx -u signing_key.pub \
  20. -r signing_key.priv
  21. tpm2 startauthsession -S session.ctx --audit-session
  22. tpm2 getrandom 8 -S session.ctx --cphash cp.hash --rphash rp.hash
  23. tpm2 getsessionauditdigest -c signing_key.ctx -m att.data -s att.sig \
  24. -S session.ctx
  25. tpm2 flushcontext session.ctx
  26. dd if=/dev/zero bs=1 count=32 status=none of=zero.bin
  27. dd if=cp.hash skip=2 bs=1 count=32 status=none of=cphash.bin
  28. dd if=rp.hash skip=2 bs=1 count=32 status=none of=rphash.bin
  29. diff \
  30. <( cat zero.bin cphash.bin rphash.bin | openssl dgst -sha256 -binary ) \
  31. <( tail -c 32 att.data )
  32. #
  33. # Get audit digest for a TPM command TPM2_CC_Create in an audit session
  34. #
  35. tpm2 clear
  36. tpm2 createprimary -Q -C e -c prim.ctx
  37. tpm2 create -Q -C prim.ctx -c signing_key.ctx -u signing_key.pub \
  38. -r signing_key.priv
  39. tpm2 startauthsession -S session.ctx --audit-session
  40. tpm2 create -Q -C prim.ctx -u key.pub -r key.priv --cphash cp.hash \
  41. --rphash rp.hash -S session.ctx
  42. tpm2 getsessionauditdigest -c signing_key.ctx -m att.data -s att.sig \
  43. -S session.ctx
  44. dd if=/dev/zero bs=1 count=32 status=none of=zero.bin
  45. dd if=cp.hash skip=2 bs=1 count=32 status=none of=cphash.bin
  46. dd if=rp.hash skip=2 bs=1 count=32 status=none of=rphash.bin
  47. diff \
  48. <( cat zero.bin cphash.bin rphash.bin | openssl dgst -sha256 -binary ) \
  49. <( tail -c 32 att.data )
  50. #
  51. # Get audit digest for a TPM command TPM2_CC_Create in an audit session
  52. #
  53. tpm2 clear
  54. tpm2 createprimary -Q -C e -c prim.ctx
  55. tpm2 create -Q -C prim.ctx -c signing_key.ctx -u signing_key.pub \
  56. -r signing_key.priv
  57. tpm2 createprimary -C o -c prim.ctx -G rsa
  58. tpm2 readpublic -c prim.ctx -o prim.pub
  59. tpm2 create -C prim.ctx -u key.pub -r key.priv -c key.ctx
  60. tpm2 readpublic -c key.ctx -n key.name
  61. echo "plaintext" > plain.txt
  62. tpm2 makecredential -u prim.pub -s plain.txt -n `xxd -p -c 34 key.name` \
  63. -o cred.secret
  64. tpm2 startauthsession -S session.ctx --audit-session
  65. tpm2 activatecredential -c key.ctx -C prim.ctx -i cred.secret \
  66. -o act_cred.secret -S session.ctx --cphash cp.hash --rphash rp.hash
  67. tpm2 getsessionauditdigest -c signing_key.ctx -m att.data -s att.sig \
  68. -S session.ctx
  69. dd if=/dev/zero bs=1 count=32 status=none of=zero.bin
  70. dd if=cp.hash skip=2 bs=1 count=32 status=none of=cphash.bin
  71. dd if=rp.hash skip=2 bs=1 count=32 status=none of=rphash.bin
  72. diff \
  73. <( cat zero.bin cphash.bin rphash.bin | openssl dgst -sha256 -binary ) \
  74. <( tail -c 32 att.data )
  75. #
  76. # Get audit digest for a TPM command TPM2_CC_Certify in an audit session
  77. #
  78. tpm2 clear -Q
  79. tpm2 createprimary -Q -C e -g sha256 -G rsa -c primary.ctx
  80. tpm2 create -Q -C primary.ctx -c signing_key.ctx -u signing_key.pub \
  81. -r signing_key.priv
  82. tpm2 create -Q -g sha256 -G rsa -u certify.pub -r certify.priv -C primary.ctx
  83. tpm2 load -Q -C primary.ctx -u certify.pub -r certify.priv -n certify.name \
  84. -c certify.ctx
  85. tpm2 startauthsession -S session.ctx --audit-session
  86. tpm2 certify -Q -c primary.ctx -C certify.ctx -g sha256 -o attest.out -s sig.out \
  87. --cphash cp.hash --rphash rp.hash -S session.ctx
  88. tpm2 getsessionauditdigest -c signing_key.ctx -m att.data -s att.sig \
  89. -S session.ctx
  90. dd if=/dev/zero bs=1 count=32 status=none of=zero.bin
  91. dd if=cp.hash skip=2 bs=1 count=32 status=none of=cphash.bin
  92. dd if=rp.hash skip=2 bs=1 count=32 status=none of=rphash.bin
  93. diff \
  94. <( cat zero.bin cphash.bin rphash.bin | openssl dgst -sha256 -binary ) \
  95. <( tail -c 32 att.data )
  96. #
  97. # Get audit digest for a TPM command TPM2_CC_CertifyCreation in an audit session
  98. #
  99. tpm2 clear -Q
  100. tpm2 createprimary -Q -C e -g sha256 -G rsa -c prim.ctx \
  101. -d create.dig -t create.ticket
  102. tpm2 create -Q -C prim.ctx -c signing_key.ctx -u signing_key.pub \
  103. -r signing_key.priv
  104. tpm2 create -G rsa -u rsa.pub -r rsa.priv -C prim.ctx -c certsigningkey.ctx
  105. tpm2 startauthsession -S session.ctx --audit-session
  106. tpm2 certifycreation -C certsigningkey.ctx -c prim.ctx -d create.dig \
  107. -t create.ticket -g sha256 -f plain -s rsassa \
  108. --cphash cp.hash --rphash rp.hash -S session.ctx
  109. tpm2 getsessionauditdigest -c signing_key.ctx -m att.data -s att.sig \
  110. -S session.ctx
  111. dd if=/dev/zero bs=1 count=32 status=none of=zero.bin
  112. dd if=cp.hash skip=2 bs=1 count=32 status=none of=cphash.bin
  113. dd if=rp.hash skip=2 bs=1 count=32 status=none of=rphash.bin
  114. diff \
  115. <( cat zero.bin cphash.bin rphash.bin | openssl dgst -sha256 -binary ) \
  116. <( tail -c 32 att.data )
  117. #
  118. # Get audit digest: TPM command TPM2_CC_HierarchyChangeauth in an audit session
  119. #
  120. tpm2 clear -Q
  121. tpm2 createprimary -Q -C e -g sha256 -G rsa -c prim.ctx \
  122. -d create.dig -t create.ticket
  123. tpm2 create -Q -C prim.ctx -c signing_key.ctx -u signing_key.pub \
  124. -r signing_key.priv
  125. tpm2 startauthsession -S session.ctx --audit-session
  126. tpm2 changeauth -c o ownerpassword --cphash cp.hash --rphash rp.hash \
  127. -S session.ctx
  128. tpm2 getsessionauditdigest -c signing_key.ctx -m att.data -s att.sig \
  129. -S session.ctx
  130. dd if=/dev/zero bs=1 count=32 status=none of=zero.bin
  131. dd if=cp.hash skip=2 bs=1 count=32 status=none of=cphash.bin
  132. dd if=rp.hash skip=2 bs=1 count=32 status=none of=rphash.bin
  133. diff \
  134. <( cat zero.bin cphash.bin rphash.bin | openssl dgst -sha256 -binary ) \
  135. <( tail -c 32 att.data )
  136. #
  137. # Get audit digest: TPM command TPM2_CC_ObjectChangeauth in an audit session
  138. #
  139. tpm2 clear -Q
  140. tpm2 createprimary -Q -C e -g sha256 -G rsa -c prim.ctx \
  141. -d create.dig -t create.ticket
  142. tpm2 create -Q -C prim.ctx -c signing_key.ctx -u signing_key.pub \
  143. -r signing_key.priv
  144. tpm2 create -Q -C prim.ctx -p foo -u key.pub -r key.priv -c key.ctx
  145. tpm2 startauthsession -S session.ctx --audit-session
  146. tpm2 changeauth -C prim.ctx -p foo -c key.ctx -r new.priv bar \
  147. --cphash cp.hash --rphash rp.hash -S session.ctx
  148. tpm2 getsessionauditdigest -c signing_key.ctx -m att.data -s att.sig \
  149. -S session.ctx
  150. dd if=/dev/zero bs=1 count=32 status=none of=zero.bin
  151. dd if=cp.hash skip=2 bs=1 count=32 status=none of=cphash.bin
  152. dd if=rp.hash skip=2 bs=1 count=32 status=none of=rphash.bin
  153. diff \
  154. <( cat zero.bin cphash.bin rphash.bin | openssl dgst -sha256 -binary ) \
  155. <( tail -c 32 att.data )
  156. #
  157. # Get audit digest: TPM command TPM2_CC_ChangeEPS in an audit session
  158. #
  159. tpm2 clear -Q
  160. tpm2 startauthsession -S session.ctx --audit-session
  161. tpm2 changeeps --cphash cp.hash --rphash rp.hash -S session.ctx
  162. tpm2 createprimary -Q -C e -g sha256 -G rsa -c prim.ctx
  163. tpm2 create -Q -C prim.ctx -c signing_key.ctx -u signing_key.pub \
  164. -r signing_key.priv
  165. tpm2 getsessionauditdigest -c signing_key.ctx -m att.data -s att.sig \
  166. -S session.ctx
  167. dd if=/dev/zero bs=1 count=32 status=none of=zero.bin
  168. dd if=cp.hash skip=2 bs=1 count=32 status=none of=cphash.bin
  169. dd if=rp.hash skip=2 bs=1 count=32 status=none of=rphash.bin
  170. diff \
  171. <( cat zero.bin cphash.bin rphash.bin | openssl dgst -sha256 -binary ) \
  172. <( tail -c 32 att.data )
  173. #
  174. # Get audit digest: TPM command TPM2_CC_ChangePPS in an audit session
  175. #
  176. tpm2 clear -Q
  177. tpm2 startauthsession -S session.ctx --audit-session
  178. tpm2 changepps --cphash cp.hash --rphash rp.hash -S session.ctx
  179. tpm2 createprimary -Q -C e -g sha256 -G rsa -c prim.ctx
  180. tpm2 create -Q -C prim.ctx -c signing_key.ctx -u signing_key.pub \
  181. -r signing_key.priv
  182. tpm2 getsessionauditdigest -c signing_key.ctx -m att.data -s att.sig \
  183. -S session.ctx
  184. dd if=/dev/zero bs=1 count=32 status=none of=zero.bin
  185. dd if=cp.hash skip=2 bs=1 count=32 status=none of=cphash.bin
  186. dd if=rp.hash skip=2 bs=1 count=32 status=none of=rphash.bin
  187. diff \
  188. <( cat zero.bin cphash.bin rphash.bin | openssl dgst -sha256 -binary ) \
  189. <( tail -c 32 att.data )
  190. #
  191. # Get audit digest: TPM command TPM2_CC_NV_Define in an audit session
  192. #
  193. tpm2 clear -Q
  194. tpm2 startauthsession -S session.ctx --audit-session
  195. tpm2 nvdefine 0x1500016 -C o -s 32 -a "ownerread|ownerwrite" \
  196. --cphash cp.hash --rphash rp.hash -S session.ctx
  197. tpm2 createprimary -Q -C e -g sha256 -G rsa -c prim.ctx
  198. tpm2 create -Q -C prim.ctx -c signing_key.ctx -u signing_key.pub \
  199. -r signing_key.priv
  200. tpm2 getsessionauditdigest -c signing_key.ctx -m att.data -s att.sig \
  201. -S session.ctx
  202. dd if=/dev/zero bs=1 count=32 status=none of=zero.bin
  203. dd if=cp.hash skip=2 bs=1 count=32 status=none of=cphash.bin
  204. dd if=rp.hash skip=2 bs=1 count=32 status=none of=rphash.bin
  205. diff \
  206. <( cat zero.bin cphash.bin rphash.bin | openssl dgst -sha256 -binary ) \
  207. <( tail -c 32 att.data )
  208. #
  209. # Get audit digest: TPM command TPM2_CC_NV_Extend in an audit session
  210. #
  211. tpm2 clear -Q
  212. tpm2 nvdefine -C o -a "nt=extend|ownerread|policywrite|ownerwrite|writedefine" 1
  213. tpm2 startauthsession -S session.ctx --audit-session
  214. echo 'my data' | tpm2 nvextend -C o -i- 1 -S session.ctx \
  215. --cphash cp.hash --rphash rp.hash
  216. tpm2 createprimary -Q -C e -g sha256 -G rsa -c prim.ctx
  217. tpm2 create -Q -C prim.ctx -c signing_key.ctx -u signing_key.pub \
  218. -r signing_key.priv
  219. tpm2 getsessionauditdigest -c signing_key.ctx -m att.data -s att.sig \
  220. -S session.ctx
  221. dd if=/dev/zero bs=1 count=32 status=none of=zero.bin
  222. dd if=cp.hash skip=2 bs=1 count=32 status=none of=cphash.bin
  223. dd if=rp.hash skip=2 bs=1 count=32 status=none of=rphash.bin
  224. diff \
  225. <( cat zero.bin cphash.bin rphash.bin | openssl dgst -sha256 -binary ) \
  226. <( tail -c 32 att.data )
  227. #
  228. # Get audit digest: TPM command TPM2_CC_Unseal in an audit session
  229. #
  230. tpm2 clear -Q
  231. tpm2 createprimary -Q -C e -g sha256 -G rsa -c prim.ctx
  232. tpm2 create -Q -C prim.ctx -c signing_key.ctx -u signing_key.pub \
  233. -r signing_key.priv
  234. echo "plaintext" | \
  235. tpm2 create -C prim.ctx -c key.ctx -u key.pub -r key.priv -i-
  236. tpm2 startauthsession -S session.ctx --audit-session
  237. tpm2 unseal -c key.ctx --cphash cp.hash --rphash rp.hash -S session.ctx
  238. tpm2 getsessionauditdigest -c signing_key.ctx -m att.data -s att.sig \
  239. -S session.ctx
  240. dd if=/dev/zero bs=1 count=32 status=none of=zero.bin
  241. dd if=cp.hash skip=2 bs=1 count=32 status=none of=cphash.bin
  242. dd if=rp.hash skip=2 bs=1 count=32 status=none of=rphash.bin
  243. diff \
  244. <( cat zero.bin cphash.bin rphash.bin | openssl dgst -sha256 -binary ) \
  245. <( tail -c 32 att.data )
  246. #
  247. # End
  248. #
  249. exit 0