abrmd_policycphash.sh 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. cleanup() {
  4. rm -f helpers.sh signing_key_private.pem signing_key_public.pem \
  5. signing_key.ctx signing_key.name authorized.policy policy.dat \
  6. primary.ctx key.prv key.pub key.ctx new_parent.prv new_parent.pub \
  7. new_parent.ctx new_parent.name key.name name.hash policy.namehash \
  8. policynamehash.signature policy.namehash verification.tkt dupprv.bin \
  9. dupseed.dat
  10. tpm2 flushcontext session.ctx 2>/dev/null || true
  11. if [ "${1}" != "no-shutdown" ]; then
  12. shut_down
  13. fi
  14. }
  15. trap cleanup EXIT
  16. start_up
  17. cleanup "no-shutdown"
  18. generate_policycphash() {
  19. tpm2 startauthsession -S session.ctx -g sha256
  20. tpm2 policycphash -S session.ctx -L policy.cphash --cphash-input cp.hash
  21. tpm2 flushcontext session.ctx
  22. rm session.ctx
  23. }
  24. sign_and_verify_policycphash() {
  25. openssl dgst -sha256 -sign signing_key_private.pem \
  26. -out policycphash.signature policy.cphash
  27. tpm2 verifysignature -c signing_key.ctx -g sha256 -m policy.cphash \
  28. -s policycphash.signature -t verification.tkt -f rsassa
  29. }
  30. setup_authorized_policycphash() {
  31. tpm2 startauthsession -S session.ctx --policy-session -g sha256
  32. tpm2 policycphash -S session.ctx --cphash-input cp.hash
  33. tpm2 policyauthorize -S session.ctx -i policy.cphash -n signing_key.name \
  34. -t verification.tkt
  35. }
  36. setup_owner_policy() {
  37. tpm2 setprimarypolicy -C o -L policy.cphash -g sha256
  38. tpm2 startauthsession -S session.ctx --policy-session -g sha256
  39. tpm2 policycphash -S session.ctx --cphash-input cp.hash
  40. }
  41. start_policy_cphash() {
  42. tpm2 startauthsession -S session.ctx --policy-session -g sha256
  43. tpm2 policycphash -S session.ctx --cphash-input cp.hash
  44. }
  45. create_authorized_policy() {
  46. tpm2 clear
  47. # Define an authorized policy for an object
  48. openssl genrsa -out signing_key_private.pem 2048
  49. openssl rsa -in signing_key_private.pem -out signing_key_public.pem -pubout
  50. tpm2 loadexternal -G rsa -C o -u signing_key_public.pem -c signing_key.ctx \
  51. -n signing_key.name
  52. tpm2 startauthsession -S session.ctx -g sha256
  53. tpm2 policyauthorize -S session.ctx -L authorized.policy -n signing_key.name
  54. tpm2 flushcontext session.ctx
  55. }
  56. # Restrict the value that can be set through tpm2 nvsetbits.
  57. create_authorized_policy
  58. tpm2 nvdefine 1 -a "policywrite|authwrite|ownerread|nt=bits" -L authorized.policy
  59. ## Create policycphash
  60. tpm2 nvsetbits 1 -i 1 --cphash cp.hash
  61. generate_policycphash
  62. ## Sign and verify policycphash
  63. sign_and_verify_policycphash
  64. ## Satisfy policycphash and execute nvsetbits
  65. setup_authorized_policycphash
  66. tpm2 nvsetbits 1 -i 1 -P "session:session.ctx"
  67. tpm2 flushcontext session.ctx
  68. ## Attempt setting another bit which was not recorded in policycphash
  69. setup_authorized_policycphash
  70. trap - ERR
  71. tpm2 nvsetbits 1 -i 2 -P "session:session.ctx"
  72. if [ $? == 0 ];then
  73. echo "ERROR: nvsetbits must fail!"
  74. exit 1
  75. fi
  76. trap onerror ERR
  77. tpm2 flushcontext session.ctx
  78. tpm2 nvundefine 1
  79. # Test tpm2 nvextend
  80. create_authorized_policy
  81. tpm2 nvdefine 1 -a "nt=extend|ownerread|policywrite" -L authorized.policy
  82. echo "foo" | tpm2 nvextend -i- 1 --cphash cp.hash
  83. generate_policycphash
  84. sign_and_verify_policycphash
  85. setup_authorized_policycphash
  86. echo "foo" | tpm2 nvextend -i- 1 -P "session:session.ctx"
  87. tpm2 flushcontext session.ctx
  88. ## test the failing scenario
  89. setup_authorized_policycphash
  90. trap - ERR
  91. echo "food" | tpm2 nvextend -i- 1 -P "session:session.ctx"
  92. if [ $? == 0 ];then
  93. echo "ERROR: nvextend must fail!"
  94. exit 1
  95. fi
  96. trap onerror ERR
  97. tpm2 flushcontext session.ctx
  98. tpm2 nvundefine 1
  99. # Test tpm2 nvincrement
  100. create_authorized_policy
  101. tpm2 nvdefine 1 -s 8 -a "nt=counter|ownerread|policywrite" -L authorized.policy
  102. tpm2 nvincrement 1 --cphash cp.hash
  103. generate_policycphash
  104. sign_and_verify_policycphash
  105. setup_authorized_policycphash
  106. tpm2 nvincrement 1 -P "session:session.ctx"
  107. tpm2 flushcontext session.ctx
  108. tpm2 nvundefine 1
  109. # Test tpm2 nvread
  110. create_authorized_policy
  111. tpm2 nvdefine 1 -s 8 -a "ownerwrite|policyread" -L authorized.policy
  112. echo "foo" | tpm2 nvwrite 1 -i- -C o
  113. tpm2 nvread 1 -s 8 --cphash cp.hash
  114. generate_policycphash
  115. sign_and_verify_policycphash
  116. setup_authorized_policycphash
  117. tpm2 nvread 1 -s 8 -P "session:session.ctx" | xxd -p
  118. ## test the failing scenario
  119. setup_authorized_policycphash
  120. trap - ERR
  121. tpm2 nvread 1 -s 7 --offset 1 -P "session:session.ctx"
  122. if [ $? == 0 ];then
  123. echo "ERROR: nvread must fail!"
  124. exit 1
  125. fi
  126. trap onerror ERR
  127. tpm2 flushcontext session.ctx
  128. tpm2 nvundefine 1
  129. # Test tpm2 nvreadlock
  130. create_authorized_policy
  131. tpm2 nvdefine 1 -C o -s 32 -a "policyread|policywrite|read_stclear" \
  132. -L authorized.policy
  133. tpm2 nvreadlock 1 -C 0x01000001 --cphash cp.hash
  134. generate_policycphash
  135. sign_and_verify_policycphash
  136. setup_authorized_policycphash
  137. tpm2 nvreadlock 1 -C 0x01000001 -P "session:session.ctx"
  138. tpm2 flushcontext session.ctx
  139. tpm2 nvundefine 1
  140. # Test tpm2 nvwritelock
  141. create_authorized_policy
  142. tpm2 nvdefine 1 -C o -s 32 -a "policyread|policywrite|writedefine" \
  143. -L authorized.policy
  144. tpm2 nvwritelock 1 -C 0x01000001 --cphash cp.hash
  145. generate_policycphash
  146. sign_and_verify_policycphash
  147. setup_authorized_policycphash
  148. tpm2 nvwritelock 1 -C 0x01000001 -P "session:session.ctx"
  149. tpm2 flushcontext session.ctx
  150. tpm2 nvundefine 1
  151. ## attempt with globallock attribute set
  152. tpm2 nvdefine 1 -C o -s 32 -a "ownerread|ownerwrite|globallock"
  153. tpm2 nvwritelock --global -C o --cphash cp.hash
  154. generate_policycphash
  155. setup_owner_policy
  156. tpm2 nvwritelock --global -C o -P "session:session.ctx"
  157. tpm2 flushcontext session.ctx
  158. tpm2 nvundefine 1
  159. # Test tpm2 nvdefine
  160. tpm2 nvdefine 1 -C o -s 32 -a "ownerread|ownerwrite" --cphash cp.hash
  161. generate_policycphash
  162. setup_owner_policy
  163. tpm2 nvdefine 1 -C o -s 32 -a "ownerread|ownerwrite" -P "session:session.ctx"
  164. tpm2 flushcontext session.ctx
  165. ## attempt failing scenario
  166. start_policy_cphash
  167. trap - ERR
  168. tpm2 nvdefine 2 -C o -s 32 -a "ownerread|ownerwrite" -P "session:session.ctx"
  169. if [ $? == 0 ];then
  170. echo "ERROR: nvdefine must fail!"
  171. exit 1
  172. fi
  173. trap onerror ERR
  174. tpm2 flushcontext session.ctx
  175. tpm2 nvundefine 1
  176. # Test tpm2 nvundefine
  177. tpm2 nvdefine 1
  178. tpm2 nvundefine 1 --cphash cp.hash
  179. generate_policycphash
  180. setup_owner_policy
  181. tpm2 nvundefine 1 -P "session:session.ctx"
  182. tpm2 flushcontext session.ctx
  183. ## attempt failing scenario
  184. tpm2 nvdefine 2
  185. start_policy_cphash
  186. trap - ERR
  187. tpm2 nvundefine 2 -P "session:session.ctx"
  188. if [ $? == 0 ];then
  189. echo "ERROR: nvundefine must fail!"
  190. exit 1
  191. fi
  192. trap onerror ERR
  193. tpm2 flushcontext session.ctx
  194. tpm2 nvundefine -C p 2
  195. #Test tpm2 nvcertify
  196. tpm2 createprimary -C o -c primary.ctx -Q
  197. tpm2 create -G rsa -u signing_key.pub -r signing_key.priv -C primary.ctx \
  198. -c signing_key.ctx -Q
  199. tpm2 readpublic -c signing_key.ctx -f pem -o sslpub.pem -Q
  200. tpm2 nvdefine -s 32 -C o -a "ownerread|ownerwrite|authread|authwrite" 1
  201. dd if=/dev/urandom bs=1 count=32 status=none| tpm2 nvwrite 1 -i-
  202. tpm2 nvcertify -C signing_key.ctx -g sha256 -f plain -s rsassa \
  203. -o signature.bin --attestation attestation.bin --size 32 1 -c o --cphash cp.hash
  204. generate_policycphash
  205. setup_owner_policy
  206. tpm2 nvcertify -C signing_key.ctx -g sha256 -f plain -s rsassa \
  207. -o signature.bin --attestation attestation.bin --size 32 1 -c o -p "session:session.ctx"
  208. tpm2 flushcontext session.ctx
  209. tpm2 nvundefine 1
  210. #Test tpm2 policynv
  211. tpm2 nvdefine -C o -a "ownerwrite|ownerread" -s 2 1
  212. operandA=0x81
  213. operandB=0x80
  214. echo $operandA | xxd -r -p | tpm2 nvwrite -C o -i- 1
  215. tpm2 startauthsession -S policy_session.ctx --policy-session -g sha256
  216. echo $operandB | xxd -r -p | tpm2 policynv -i- -C o --cphash cp.hash 1 neq -S policy_session.ctx
  217. generate_policycphash
  218. setup_owner_policy
  219. echo $operandB | xxd -r -p | tpm2 policynv -S policy_session.ctx -i- -C o -P "session:session.ctx" 1 neq
  220. tpm2 flushcontext policy_session.ctx
  221. tpm2 flushcontext session.ctx
  222. tpm2 nvundefine 1
  223. # Test tpm2 policyauthorizenv
  224. tpm2 nvdefine -C o 1 -a "ownerread|ownerwrite" -s 34
  225. tpm2 startauthsession -S session.ctx
  226. tpm2 policypassword -S session.ctx -L policy.pass
  227. tpm2 flushcontext session.ctx
  228. POLICYDIGESTALGORITHM=000b
  229. echo $POLICYDIGESTALGORITHM | xxd -p -r | cat - policy.pass | \
  230. tpm2 nvwrite -C o 1 -i-
  231. tpm2 startauthsession -S policy_session.ctx --policy-session
  232. tpm2 policyauthorizenv -S policy_session.ctx -C o 1 --cphash cp.hash
  233. generate_policycphash
  234. setup_owner_policy
  235. tpm2 policypassword -S policy_session.ctx
  236. tpm2 policyauthorizenv -S policy_session.ctx -C o 1 -P "session:session.ctx"
  237. tpm2 flushcontext policy_session.ctx
  238. tpm2 flushcontext session.ctx
  239. tpm2 nvundefine 1
  240. # Test tpm2 policysecret
  241. tpm2 startauthsession -S policy_session.ctx --policy-session -g sha256
  242. tpm2 policysecret -S policy_session.ctx -c o --cphash cp.hash
  243. tpm2 startauthsession -S session.ctx -g sha256
  244. tpm2 policyauthvalue -S session.ctx -L policy.authval
  245. tpm2 policycphash -S session.ctx --cphash cp.hash -L policy.authval_cphash
  246. tpm2 flushcontext session.ctx
  247. tpm2 setprimarypolicy -C o -L policy.authval_cphash -g sha256
  248. tpm2 startauthsession -S session.ctx --policy-session -g sha256
  249. tpm2 policyauthvalue -S session.ctx
  250. tpm2 policycphash -S session.ctx --cphash cp.hash
  251. ## Changing the policysecret authhandle parameter fro "o" to "p" should fail
  252. tpm2 policysecret -S policy_session.ctx -c o session:session.ctx
  253. tpm2 flushcontext session.ctx
  254. tpm2 flushcontext policy_session.ctx
  255. # Test tpm2 create
  256. create_authorized_policy
  257. tpm2 createprimary -C o -c prim.ctx -G rsa -L authorized.policy
  258. tpm2 create -C prim.ctx -G rsa --cphash cp.hash
  259. generate_policycphash
  260. sign_and_verify_policycphash
  261. setup_authorized_policycphash
  262. tpm2 create -C prim.ctx -G rsa -P "session:session.ctx"
  263. tpm2 flushcontext session.ctx
  264. ## Attempt creating a key type that was not recorded in policycphash
  265. setup_authorized_policycphash
  266. trap - ERR
  267. tpm2 create -C prim.ctx -G aes -P "session:session.ctx"
  268. if [ $? == 0 ];then
  269. echo "ERROR: tpm2 create must fail!"
  270. exit 1
  271. fi
  272. trap onerror ERR
  273. tpm2 flushcontext session.ctx
  274. # Test tpm2 load
  275. create_authorized_policy
  276. tpm2 createprimary -C o -c prim.ctx -G rsa -L authorized.policy
  277. tpm2 create -C prim.ctx -G rsa -u key.pub -r key.priv
  278. tpm2 create -C prim.ctx -G rsa -u key_2.pub -r key_2.priv
  279. tpm2 load -C prim.ctx -u key.pub -r key.priv --cphash cp.hash
  280. generate_policycphash
  281. sign_and_verify_policycphash
  282. setup_authorized_policycphash
  283. tpm2 load -C prim.ctx -u key.pub -r key.priv -c key.ctx -P "session:session.ctx"
  284. tpm2 flushcontext session.ctx
  285. ## Attempt loading another key that was not recorded in policycphash
  286. setup_authorized_policycphash
  287. trap - ERR
  288. tpm2 load -C prim.ctx -u key_2.pub -r key_2.priv -c key_2.ctx \
  289. -P "session:session.ctx"
  290. if [ $? == 0 ];then
  291. echo "ERROR: tpm2 load must fail!"
  292. exit 1
  293. fi
  294. trap onerror ERR
  295. tpm2 flushcontext session.ctx
  296. # Test tpm2 activatecredential (using legacy option --cphash)
  297. create_authorized_policy
  298. tpm2 createprimary -C o -c prim.ctx -G rsa
  299. tpm2 readpublic -c prim.ctx -o prim.pub
  300. tpm2 create -C prim.ctx -u key.pub -r key.priv -c key.ctx -L authorized.policy
  301. tpm2 readpublic -c key.ctx -n key.name
  302. echo "plaintext" > plain.txt
  303. tpm2 makecredential -u prim.pub -s plain.txt -n `xxd -p -c 34 key.name` \
  304. -o cred.secret
  305. tpm2 activatecredential -c key.ctx -C prim.ctx -i cred.secret -o act_cred.secret \
  306. --cphash cp.hash
  307. tpm2 startauthsession -S session.ctx -g sha256
  308. tpm2 policycphash -S session.ctx -L policy.cphash --cphash cp.hash
  309. tpm2 policycommandcode -S session.ctx TPM2_CC_ActivateCredential -L policy.cphash
  310. tpm2 flushcontext session.ctx
  311. sign_and_verify_policycphash
  312. tpm2 startauthsession -S session.ctx --policy-session -g sha256
  313. tpm2 policycphash -S session.ctx --cphash cp.hash
  314. tpm2 policycommandcode -S session.ctx TPM2_CC_ActivateCredential
  315. tpm2 policyauthorize -S session.ctx -i policy.cphash -n signing_key.name \
  316. -t verification.tkt
  317. tpm2 activatecredential -c key.ctx -C prim.ctx -i cred.secret -o act_cred.secret \
  318. -p "session:session.ctx"
  319. tpm2 flushcontext session.ctx
  320. # Test tpm2 unseal
  321. create_authorized_policy
  322. tpm2 createprimary -C o -c prim.ctx
  323. echo "plaintext" | \
  324. tpm2 create -C prim.ctx -c key.ctx -u key.pub -r key.priv -L authorized.policy -i-
  325. tpm2 unseal -c key.ctx --cphash cp.hash
  326. generate_policycphash
  327. sign_and_verify_policycphash
  328. setup_authorized_policycphash
  329. tpm2 unseal -c key.ctx -p "session:session.ctx"
  330. tpm2 flushcontext session.ctx
  331. # Test tpm2 changeauth
  332. tpm2 clear
  333. tpm2 changeauth -c o ownerpassword --cphash cp.hash
  334. generate_policycphash
  335. setup_owner_policy
  336. tpm2 changeauth -c o ownerpassword -p session:session.ctx
  337. tpm2 flushcontext session.ctx
  338. ## Negative test
  339. tpm2 clear
  340. tpm2 changeauth -c o ownerpassword --cphash cp.hash
  341. generate_policycphash
  342. setup_owner_policy
  343. trap - ERR
  344. tpm2 changeauth -c o wrongownerpassword -p session:session.ctx
  345. if [ $? == 0 ];then
  346. echo "ERROR: tpm2 load must fail!"
  347. exit 1
  348. fi
  349. trap onerror ERR
  350. tpm2 flushcontext session.ctx
  351. #Test tpm2 duplicate
  352. create_authorized_policy
  353. tpm2 createprimary -C o -g sha256 -G rsa -c primary.ctx
  354. tpm2 create -C primary.ctx -g sha256 -G rsa -r duplicable_key.prv \
  355. -u duplicable_key.pub -a "sensitivedataorigin|sign|decrypt|encryptedduplication" \
  356. -L authorized.policy
  357. tpm2 load -C primary.ctx -r duplicable_key.prv -u duplicable_key.pub \
  358. -c duplicable_key.ctx
  359. tpm2 create -C primary.ctx -g sha256 -G rsa -r new_parent.prv \
  360. -u new_parent.pub -a "decrypt|fixedparent|fixedtpm|restricted|\
  361. sensitivedataorigin"
  362. tpm2 loadexternal -C o -u new_parent.pub -c new_parent.ctx
  363. dd if=/dev/urandom of=sym_key_in.bin bs=1 count=16 status=none
  364. tpm2 duplicate -C new_parent.ctx -c duplicable_key.ctx -G aes \
  365. -i sym_key_in.bin -r dupprv.bin -s dupseed.dat --cphash cp.hash
  366. tpm2 startauthsession -S session.ctx -g sha256
  367. tpm2 policycphash -S session.ctx --cphash-input cp.hash
  368. tpm2 policycommandcode -S session.ctx -L policy.cphash TPM2_CC_Duplicate
  369. tpm2 flushcontext session.ctx
  370. sign_and_verify_policycphash
  371. tpm2 startauthsession --policy-session -S session.ctx -g sha256
  372. tpm2 policycphash -S session.ctx --cphash-input cp.hash
  373. tpm2 policycommandcode -S session.ctx TPM2_CC_Duplicate
  374. tpm2 policyauthorize -S session.ctx -i policy.cphash -n signing_key.name \
  375. -t verification.tkt
  376. tpm2 duplicate -C new_parent.ctx -c duplicable_key.ctx -G aes \
  377. -i sym_key_in.bin -r dupprv.bin -s dupseed.dat -p "session:session.ctx"
  378. tpm2 flushcontext session.ctx
  379. ## attempt failing scenario
  380. dd if=/dev/urandom of=sym_key_in.bin bs=1 count=16 status=none
  381. tpm2 startauthsession --policy-session -S session.ctx -g sha256
  382. tpm2 policycphash -S session.ctx --cphash-input cp.hash
  383. tpm2 policycommandcode -S session.ctx TPM2_CC_Duplicate
  384. tpm2 policyauthorize -S session.ctx -i policy.cphash -n signing_key.name \
  385. -t verification.tkt
  386. trap - ERR
  387. tpm2 duplicate -C new_parent.ctx -c duplicable_key.ctx -G aes \
  388. -i sym_key_in.bin -r dupprv.bin -s dupseed.dat -p "session:session.ctx"
  389. if [ $? == 0 ];then
  390. echo "ERROR: tpm2 duplicate must fail!"
  391. exit 1
  392. fi
  393. trap onerror ERR
  394. tpm2 flushcontext session.ctx
  395. # Test tpm2 import
  396. create_authorized_policy
  397. tpm2 createprimary -C o -g sha256 -G rsa -c primary.ctx
  398. tpm2 create -C primary.ctx -g sha256 -G rsa -r new_parent.prv \
  399. -u new_parent.pub -c new_parent.ctx -L authorized.policy \
  400. -a "restricted|sensitivedataorigin|decrypt|userwithauth"
  401. tpm2 startauthsession -S session.ctx
  402. tpm2 policycommandcode -S session.ctx -L dpolicy.dat TPM2_CC_Duplicate
  403. tpm2 flushcontext session.ctx
  404. tpm2 create -C primary.ctx -g sha256 -G rsa -p foo -r dupkey.prv -u dupkey.pub \
  405. -L dpolicy.dat -a "sensitivedataorigin|decrypt|userwithauth" -c dupkey.ctx
  406. tpm2 startauthsession --policy-session -S session.ctx
  407. tpm2 policycommandcode -S session.ctx TPM2_CC_Duplicate
  408. tpm2 duplicate -C new_parent.ctx -c dupkey.ctx -G null -p "session:session.ctx" \
  409. -r duplicated.prv -s dup.seed
  410. tpm2 flushcontext session.ctx
  411. tpm2 import -C new_parent.ctx -u dupkey.pub -i duplicated.prv -r imported_dup.prv \
  412. -s dup.seed --cphash cp.hash
  413. generate_policycphash
  414. sign_and_verify_policycphash
  415. setup_authorized_policycphash
  416. tpm2 import -C new_parent.ctx -u dupkey.pub -i duplicated.prv -r imported_dup.prv \
  417. -s dup.seed -P "session:session.ctx"
  418. tpm2 flushcontext session.ctx
  419. # Test tpm2 rsadecrypt
  420. create_authorized_policy
  421. tpm2 createprimary -C o -c prim.ctx
  422. tpm2 create -C prim.ctx -c key.ctx -u key.pub -r key.priv -L authorized.policy \
  423. -G rsa
  424. echo "plaintext" > plain.txt
  425. tpm2 rsaencrypt -c key.ctx -o enc.out plain.txt
  426. tpm2 rsadecrypt -c key.ctx -s rsaes enc.out --cphash cp.hash
  427. generate_policycphash
  428. sign_and_verify_policycphash
  429. setup_authorized_policycphash
  430. tpm2 rsadecrypt -c key.ctx -s rsaes enc.out -o dec.out -p "session:session.ctx"
  431. tpm2 flushcontext session.ctx
  432. # Attempt failing case
  433. dd if=/dev/urandom of=rand.om bs=1 count=256 status=none
  434. setup_authorized_policycphash
  435. trap - ERR
  436. tpm2 rsadecrypt -c key.ctx -s rsaes rand.om -o dec.out -p "session:session.ctx"
  437. if [ $? == 0 ];then
  438. echo "ERROR: tpm2 rsadecrypt must fail!"
  439. exit 1
  440. fi
  441. trap onerror ERR
  442. tpm2 flushcontext session.ctx
  443. # Test tpm2 certify
  444. create_authorized_policy
  445. tpm2 createprimary -C o -G rsa -g sha256 -c prim.ctx -p primarypass
  446. tpm2 create -C prim.ctx -c key.ctx -G rsa -u key.pub -r key.priv \
  447. -L authorized.policy -P primarypass
  448. tpm2 certify -c prim.ctx -C key.ctx -g sha256 --cphash cp.hash
  449. tpm2 startauthsession -S session.ctx -g sha256
  450. tpm2 policycphash -S session.ctx --cphash-input cp.hash
  451. tpm2 policycommandcode -S session.ctx -L policy.cphash TPM2_CC_Certify
  452. tpm2 flushcontext session.ctx
  453. sign_and_verify_policycphash
  454. tpm2 startauthsession --policy-session -S session.ctx -g sha256
  455. tpm2 policycphash -S session.ctx --cphash-input cp.hash
  456. tpm2 policycommandcode -S session.ctx TPM2_CC_Certify
  457. tpm2 policyauthorize -S session.ctx -i policy.cphash -n signing_key.name \
  458. -t verification.tkt
  459. tpm2 certify -c prim.ctx -C key.ctx -g sha256 -o attest.out -s sig.out \
  460. -p "session:session.ctx" -P primarypass
  461. tpm2 flushcontext session.ctx
  462. # Test tpm2 certifycreation
  463. create_authorized_policy
  464. tpm2 createprimary -C o -c prim.ctx --creation-data create.dat \
  465. -d create.dig -t create.ticket
  466. tpm2 create -G rsa -u rsa.pub -r rsa.priv -C prim.ctx -c signingkey.ctx \
  467. -L authorized.policy
  468. tpm2 certifycreation -C signingkey.ctx -c prim.ctx -d create.dig \
  469. -t create.ticket -g sha256 -f plain -s rsassa --cphash cp.hash
  470. generate_policycphash
  471. sign_and_verify_policycphash
  472. setup_authorized_policycphash
  473. tpm2 certifycreation -C signingkey.ctx -c prim.ctx -d create.dig \
  474. -t create.ticket -g sha256 -o sig.nature --attestation attestat.ion -f plain \
  475. -s rsassa -P "session:session.ctx"
  476. tpm2 flushcontext session.ctx
  477. # Test tpm2 quote
  478. create_authorized_policy
  479. tpm2 createprimary -C e -c primary.ctx
  480. tpm2 create -C primary.ctx -u key.pub -r key.priv -c key.ctx \
  481. -L authorized.policy
  482. tpm2 quote -Q -c key.ctx -l 0x0004:16,17,18+0x000b:16,17,18 --cphash cp.hash
  483. generate_policycphash
  484. sign_and_verify_policycphash
  485. setup_authorized_policycphash
  486. tpm2 quote -Q -c key.ctx -l 0x0004:16,17,18+0x000b:16,17,18 \
  487. -p "session:session.ctx"
  488. tpm2 flushcontext session.ctx
  489. # Test tpm2 gettime
  490. create_authorized_policy
  491. tpm2 createprimary -C e -c primary.ctx
  492. tpm2 create -G rsa -u rsa.pub -r rsa.priv -C primary.ctx \
  493. -c rsa.ctx -L authorized.policy
  494. tpm2 gettime -c rsa.ctx -q "cafebabe" --cphash cp.hash
  495. generate_policycphash
  496. sign_and_verify_policycphash
  497. setup_authorized_policycphash
  498. tpm2 gettime -c rsa.ctx -q "cafebabe" -o attest.sig --attestation attest.data \
  499. -p "session:session.ctx"
  500. tpm2 flushcontext session.ctx
  501. # Test tpm2 sign
  502. create_authorized_policy
  503. tpm2 createprimary -C e -c primary.ctx
  504. tpm2 create -G rsa -u rsa.pub -r rsa.priv -C primary.ctx -c rsa.ctx \
  505. -L authorized.policy
  506. echo "my message" > message.dat
  507. tpm2 sign -c rsa.ctx -g sha256 message.dat --cphash cp.hash
  508. generate_policycphash
  509. sign_and_verify_policycphash
  510. setup_authorized_policycphash
  511. tpm2 sign -c rsa.ctx -g sha256 message.dat -o signature.dat \
  512. -p "session:session.ctx"
  513. tpm2 flushcontext session.ctx
  514. # Test tpm2 createprimary
  515. tpm2 clear
  516. tpm2 createprimary -C o -q "cafebabe" --cphash cp.hash
  517. generate_policycphash
  518. setup_owner_policy
  519. tpm2 createprimary -C o -q "cafebabe" -P "session:session.ctx" -c prim.ctx
  520. tpm2 flushcontext session.ctx
  521. # Test tpm2 hierarchycontrol
  522. tpm2 clear
  523. tpm2 hierarchycontrol -C p shEnable clear --cphash cp.hash
  524. generate_policycphash
  525. tpm2 setprimarypolicy -C p -L policy.cphash -g sha256
  526. tpm2 startauthsession -S session.ctx --policy-session -g sha256
  527. tpm2 policycphash -S session.ctx --cphash-input cp.hash
  528. tpm2 hierarchycontrol -C p shEnable clear -P "session:session.ctx"
  529. tpm2 flushcontext session.ctx
  530. # Test tpm2 setprimarypolicy
  531. tpm2 startauthsession -S session.ctx
  532. tpm2 policyauthvalue -S session.ctx -L policy.authvalue
  533. tpm2 flushcontext session.ctx
  534. create_authorized_policy
  535. tpm2 setprimarypolicy -C o -L authorized.policy -g sha256
  536. tpm2 setprimarypolicy -C o -L policy.authvalue -g sha256 --cphash cp.hash
  537. generate_policycphash
  538. sign_and_verify_policycphash
  539. setup_authorized_policycphash
  540. tpm2 setprimarypolicy -C o -L policy.authvalue -g sha256 -P "session:session.ctx"
  541. tpm2 flushcontext session.ctx
  542. # Test tpm2 clear
  543. tpm2 clear
  544. tpm2 clear -c l --cphash cp.hash
  545. generate_policycphash
  546. tpm2 setprimarypolicy -C l -L policy.cphash -g sha256
  547. tpm2 startauthsession -S session.ctx --policy-session -g sha256
  548. tpm2 policycphash -S session.ctx --cphash-input cp.hash
  549. tpm2 clear -c l "session:session.ctx"
  550. tpm2 flushcontext session.ctx
  551. # Test tpm2 clearcontrol
  552. tpm2 clear
  553. tpm2 clearcontrol -C l s --cphash cp.hash
  554. generate_policycphash
  555. tpm2 setprimarypolicy -C l -L policy.cphash -g sha256
  556. tpm2 startauthsession -S session.ctx --policy-session -g sha256
  557. tpm2 policycphash -S session.ctx --cphash-input cp.hash
  558. tpm2 clearcontrol -C l s -P "session:session.ctx"
  559. tpm2 flushcontext session.ctx
  560. # Test tpm2 dictionarylockout
  561. tpm2 clearcontrol -C p c
  562. tpm2 clear
  563. tpm2 dictionarylockout -s -n 5 -t 6 -l 7 --cphash cp.hash
  564. generate_policycphash
  565. tpm2 setprimarypolicy -C l -L policy.cphash -g sha256
  566. tpm2 startauthsession -S session.ctx --policy-session -g sha256
  567. tpm2 policycphash -S session.ctx --cphash-input cp.hash
  568. tpm2 dictionarylockout -s -n 5 -t 6 -l 7 --cphash cp.hash -p "session:session.ctx"
  569. tpm2 flushcontext session.ctx
  570. # Test evictcontrol
  571. tpm2 clear
  572. tpm2 createprimary -C o -c prim.ctx
  573. tpm2 evictcontrol -C o -c prim.ctx 0x81010001 --cphash cp.hash
  574. generate_policycphash
  575. setup_owner_policy
  576. tpm2 evictcontrol -C o -c prim.ctx 0x81010001 -P "session:session.ctx"
  577. tpm2 flushcontext session.ctx
  578. # Test clockset
  579. tpm2 clear
  580. let clockset=`tpm2 readclock | grep clock | grep -v info | awk '{print $2}'`+100000
  581. tpm2 setclock -c o $clockset --cphash cp.hash
  582. generate_policycphash
  583. setup_owner_policy
  584. tpm2 setclock -c o $clockset -p "session:session.ctx"
  585. tpm2 flushcontext session.ctx
  586. # Test clockrateadjust
  587. tpm2 clear
  588. tpm2 clockrateadjust s --cphash cp.hash
  589. generate_policycphash
  590. setup_owner_policy
  591. tpm2 clockrateadjust s -p "session:session.ctx"
  592. tpm2 flushcontext session.ctx
  593. # Test nvwrite
  594. create_authorized_policy
  595. tpm2 nvdefine 1 -s 8 -a "ownerread|authwrite|policywrite" -L authorized.policy
  596. echo "foo" | tpm2 nvwrite 1 -i- --cphash cp.hash
  597. xxd -p cp.hash
  598. generate_policycphash
  599. sign_and_verify_policycphash
  600. setup_authorized_policycphash
  601. echo "foo" | tpm2 nvwrite 1 -i- -P "session:session.ctx"
  602. tpm2 flushcontext session.ctx
  603. # Test encryptdecrypt
  604. create_authorized_policy
  605. tpm2 createprimary -Q -C e -g sha1 -G rsa -c primary.ctx
  606. tpm2 create -Q -g sha256 -G aes -u key.pub -r key.priv -C primary.ctx \
  607. -c decrypt.ctx -L authorized.policy
  608. dd if=/dev/urandom of=iv.dat bs=16 count=1
  609. echo "plaintext" > secret.dat
  610. cat secret.dat | tpm2 encryptdecrypt -c decrypt.ctx --iv iv.dat --cphash cp.hash
  611. generate_policycphash
  612. sign_and_verify_policycphash
  613. setup_authorized_policycphash
  614. echo "plaintext" | tpm2 encryptdecrypt -c decrypt.ctx --iv iv.dat:iv2.dat \
  615. -p "session:session.ctx" > secret2.dat
  616. # Test tpm2 hmac
  617. create_authorized_policy
  618. tpm2 createprimary -Q -C o -c prim.ctx
  619. tpm2 create -Q -C prim.ctx -c key.ctx -u key.pub -r key.priv -G hmac \
  620. -L authorized.policy
  621. echo "testdata" > plain.txt
  622. tpm2 hmac -c key.ctx plain.txt --cphash cp.hash
  623. generate_policycphash
  624. sign_and_verify_policycphash
  625. setup_authorized_policycphash
  626. tpm2 hmac -c key.ctx plain.txt -o hmac.bin -p "session:session.ctx"
  627. tpm2 flushcontext session.ctx
  628. # Test if specifying the <halg>: in the cphash path has effect on cphash alg
  629. TPM2_ALG_SHA256_SZ=34
  630. TPM2_ALG_SHA1_SZ=22
  631. ## Default must be sha256
  632. expected_hash_len=$TPM2_ALG_SHA256_SZ
  633. tpm2 getrandom 8 --cphash cp.hash
  634. file_size=`ls -l cp.hash | awk {'print $5'}`
  635. test $file_size -eq $expected_hash_len
  636. ## Check if a specific hash can be enforced with <halg>:
  637. expected_hash_len=$TPM2_ALG_SHA1_SZ
  638. tpm2 getrandom 8 --cphash sha1:cp.hash
  639. file_size=`ls -l cp.hash | awk {'print $5'}`
  640. test $file_size -eq $expected_hash_len
  641. exit 0