nv.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. if [ "`uname`" == "FreeBSD" ]; then
  3. exit 77
  4. fi
  5. source helpers.sh
  6. nv_test_index=0x1500018
  7. large_file_name="nv.test_large_w"
  8. large_file_read_name="nv.test_large_r"
  9. pcr_specification=sha256:0,1,2,3+sha1:0,1,2,3
  10. file_pcr_value=pcr.bin
  11. file_policy=policy.data
  12. cleanup() {
  13. tpm2 nvundefine -Q $nv_test_index -C o 2>/dev/null || true
  14. tpm2 nvundefine -Q 0x1500016 -C o 2>/dev/null || true
  15. tpm2 nvundefine -Q 0x1500015 -C o -P owner 2>/dev/null || true
  16. rm -f policy.bin test.bin nv.test_w $large_file_name $large_file_read_name \
  17. nv.readlock foo.dat cmp.dat $file_pcr_value $file_policy nv.out cap.out yaml.out
  18. if [ "$1" != "no-shut-down" ]; then
  19. shut_down
  20. fi
  21. }
  22. trap cleanup EXIT
  23. start_up
  24. cleanup "no-shut-down"
  25. tpm2 clear
  26. #Test nvdefine with no options
  27. tpm2 nvdefine > yaml.out
  28. tpm2 nvundefine $(yaml_get_kv yaml.out "nv-index")
  29. #Test default values for the hierarchy "-a" parameter
  30. tpm2 nvdefine -Q $nv_test_index -s 32 -a "ownerread|policywrite|ownerwrite"
  31. tpm2 nvundefine -Q $nv_test_index
  32. #Test writing and reading
  33. tpm2 nvdefine -Q $nv_test_index -C o -s 32 \
  34. -a "ownerread|policywrite|ownerwrite"
  35. echo "please123abc" > nv.test_w
  36. tpm2 nvwrite -Q $nv_test_index -C o -i nv.test_w
  37. tpm2 nvread -Q $nv_test_index -C o -s 32 -o 0
  38. tpm2 nvreadpublic > nv.out
  39. yaml_get_kv nv.out "$nv_test_index" > /dev/null
  40. yaml_get_kv nv.out "$nv_test_index" "name" > /dev/null
  41. # Test writing to and reading from an offset by:
  42. # 1. writing "foo" into the nv file at an offset
  43. # 2. writing to the same offset in the nv index
  44. # 3. reading back the index
  45. # 4. comparing the result.
  46. echo -n "foo" > foo.dat
  47. dd if=foo.dat of=nv.test_w bs=1 seek=4 conv=notrunc 2>/dev/null
  48. # Test a pipe input
  49. cat foo.dat | tpm2 nvwrite -Q $nv_test_index -C o --offset 4 -i -
  50. tpm2 nvread $nv_test_index -C o -s 13 > cmp.dat
  51. cmp nv.test_w cmp.dat
  52. # Writing at an offset and data size too big shouldn't result in a change
  53. # to the index value.
  54. trap - ERR
  55. tpm2 nvwrite -Q $nv_test_index -C o -o 30 -i foo.dat 2>/dev/null
  56. if [ $? -eq 0 ]; then
  57. echo "Writing past the public size shouldn't work!"
  58. exit 1
  59. fi
  60. trap onerror ERR
  61. tpm2 nvread $nv_test_index -C o -s 13 > cmp.dat
  62. cmp nv.test_w cmp.dat
  63. tpm2 nvundefine $nv_test_index -C o
  64. tpm2 pcrread -Q -o $file_pcr_value $pcr_specification
  65. tpm2 createpolicy -Q --policy-pcr -l $pcr_specification -f $file_pcr_value \
  66. -L $file_policy
  67. tpm2 nvdefine -Q 0x1500016 -C o -s 32 -L $file_policy \
  68. -a "policyread|policywrite"
  69. # Write with index authorization for now, since tpm2 nvwrite does not support
  70. # pcr policy.
  71. echo -n "policy locked" | tpm2 nvwrite -Q 0x1500016 -C 0x1500016 \
  72. -P pcr:$pcr_specification=$file_pcr_value -i -
  73. str=`tpm2 nvread 0x1500016 -C 0x1500016 \
  74. -P pcr:$pcr_specification=$file_pcr_value -s 13`
  75. test "policy locked" == "$str"
  76. # this should fail because authread is not allowed
  77. trap - ERR
  78. tpm2 nvread 0x1500016 -C 0x1500016 -P "index" 2>/dev/null
  79. trap onerror ERR
  80. tpm2 nvundefine -Q 0x1500016 -C o
  81. #
  82. # Test large writes
  83. #
  84. tpm2 getcap properties-fixed > cap.out
  85. large_file_size=`yaml_get_kv cap.out "TPM2_PT_NV_INDEX_MAX" "raw"`
  86. nv_test_index=0x1000000
  87. # Create an nv space with attributes 1010 = TPMA_NV_PPWRITE and
  88. # TPMA_NV_AUTHWRITE
  89. tpm2 nvdefine -Q $nv_test_index -C o -s $large_file_size -a 0x2000A
  90. base64 /dev/urandom | head -c $(($large_file_size)) > $large_file_name
  91. # Test file input redirection
  92. tpm2 nvwrite -Q $nv_test_index -C o -i -< $large_file_name
  93. tpm2 nvread $nv_test_index -C o > $large_file_read_name
  94. cmp -s $large_file_read_name $large_file_name
  95. # test per-index readpublic
  96. tpm2 nvreadpublic "$nv_test_index" > nv.out
  97. yaml_get_kv nv.out "$nv_test_index" > /dev/null
  98. tpm2 nvundefine -Q $nv_test_index -C o
  99. #
  100. # Test NV access locked
  101. #
  102. tpm2 nvdefine -Q $nv_test_index -C o -s 32 \
  103. -a "ownerread|policywrite|ownerwrite|read_stclear|writedefine"
  104. echo "foobar" > nv.readlock
  105. tpm2 nvwrite -Q $nv_test_index -C o -i nv.readlock
  106. tpm2 nvread -Q $nv_test_index -C o -s 6 -o 0
  107. tpm2 nvreadlock -Q $nv_test_index -C o
  108. # Reset ERR signal handler to test for expected nvread error
  109. trap - ERR
  110. tpm2 nvread -Q $nv_test_index -C o -s 6 -o 0 2> /dev/null
  111. if [ $? != 1 ];then
  112. echo "nvread didn't fail!"
  113. exit 1
  114. fi
  115. trap onerror ERR
  116. # Test that write lock works
  117. tpm2 nvwritelock -C o $nv_test_index
  118. trap - ERR
  119. tpm2 nvwrite $nv_test_index -C o -i nv.readlock
  120. if [ $? != 1 ];then
  121. echo "nvwrite didn't fail!"
  122. exit 1
  123. fi
  124. tpm2 nvundefine -C o $nv_test_index
  125. trap onerror ERR
  126. #
  127. # Test that owner and index passwords work by
  128. # 1. Setting up the owner password
  129. # 2. Defining an nv index that can be satisfied by an:
  130. # a. Owner authorization
  131. # b. Index authorization
  132. # 3. Using index and owner based auth during write/read operations
  133. # 4. Testing that auth is needed or a failure occurs.
  134. #
  135. tpm2 changeauth -c o owner
  136. tpm2 nvdefine 0x1500015 -C o -s 32 \
  137. -a "policyread|policywrite|authread|authwrite|ownerwrite|ownerread" \
  138. -p "index" -P "owner"
  139. # Use index password write/read, implicit -a
  140. tpm2 nvwrite -Q 0x1500015 -P "index" -i nv.test_w
  141. tpm2 nvread -Q 0x1500015 -P "index"
  142. # Use index password write/read, explicit -a
  143. tpm2 nvwrite -Q 0x1500015 -C 0x1500015 -P "index" -i nv.test_w
  144. tpm2 nvread -Q 0x1500015 -C 0x1500015 -P "index"
  145. # use owner password
  146. tpm2 nvwrite -Q 0x1500015 -C o -P "owner" -i nv.test_w
  147. tpm2 nvread -Q 0x1500015 -C o -P "owner"
  148. # Check a bad password fails
  149. trap - ERR
  150. tpm2 nvwrite -Q 0x1500015 -C 0x1500015 -P "wrong" -i nv.test_w 2>/dev/null
  151. if [ $? -eq 0 ];then
  152. echo "nvwrite with bad password should fail!"
  153. exit 1
  154. fi
  155. # Check using authorisation with tpm2 nvundefine
  156. trap onerror ERR
  157. tpm2 nvundefine 0x1500015 -C o -P "owner"
  158. # Check nv index can be specified simply as an offset
  159. tpm2 nvdefine -Q -C o -s 32 -a "ownerread|ownerwrite" 1 -P "owner"
  160. tpm2 nvundefine 0x01000001 -C o -P "owner"
  161. # Test setbits
  162. tpm2 nvdefine -C o -P "owner" -a "nt=bits|ownerread|policywrite|ownerwrite|writedefine" $nv_test_index
  163. tpm2 nvsetbits -C o -P "owner" -i 0xbadc0de $nv_test_index
  164. check=$(tpm2 nvread -C o -P "owner" $nv_test_index | xxd -p | sed s/'^0*'/0x/)
  165. if [ "$check" != "0xbadc0de" ]; then
  166. echo "Expected setbits read value of 0xbadc0de, got \"$check\""
  167. exit 1
  168. fi
  169. # Test global writelock
  170. if is_cmd_supported "NV_GlobalWriteLock"; then
  171. tpm2 nvdefine -C o -P "owner" -s 32 -a "ownerread|ownerwrite|globallock" 42
  172. tpm2 nvdefine -C o -P "owner" -s 32 -a "ownerread|ownerwrite|globallock" 43
  173. tpm2 nvdefine -C o -P "owner" -s 32 -a "ownerread|ownerwrite|globallock" 44
  174. echo foo | tpm2 nvwrite -C o -P "owner" -i- 42
  175. echo foo | tpm2 nvwrite -C o -P "owner" -i- 43
  176. echo foo | tpm2 nvwrite -C o -P "owner" -i- 44
  177. tpm2 nvwritelock -Co -P owner --global
  178. # These writes should fail now that its in a writelocked state
  179. trap - ERR
  180. echo foo | tpm2 nvwrite -C o -P "owner" -i- 42
  181. if [ $? -eq 0 ]; then
  182. echo "Expected tpm2 nvwrite to fail after globalwritelock of index 42"
  183. exit 1
  184. fi
  185. echo foo | tpm2 nvwrite -C o -P "owner" -i- 43
  186. if [ $? -eq 0 ]; then
  187. echo "Expected tpm2 nvwrite to fail after globalwritelock of index 43"
  188. exit 1
  189. fi
  190. echo foo | tpm2 nvwrite -C o -P "owner" -i- 44
  191. if [ $? -eq 0 ]; then
  192. echo "Expected tpm2 nvwrite to fail after globalwritelock of index 44"
  193. exit 1
  194. fi
  195. fi
  196. trap onerror ERR
  197. tpm2 nvundefine -C o -P "owner" $nv_test_index
  198. # Test extend
  199. tpm2 nvdefine -C o -P "owner" -a "nt=extend|ownerread|policywrite|ownerwrite" $nv_test_index
  200. echo "foo" | tpm2 nvextend -C o -P "owner" -i- $nv_test_index
  201. check=$(tpm2 nvread -C o -P "owner" $nv_test_index | xxd -p -c 64 | sed s/'^0*'//)
  202. expected="1c8457de84bb43c18d5e1d75c43e393bdaa7bca8d25967eedd580c912db65e3e"
  203. if [ "$check" != "$expected" ]; then
  204. echo "Expected setbits read value of \"$expected\", got \"$check\""
  205. exit 1
  206. fi
  207. # Test nvextend and nvdefine with aux sessions
  208. tpm2 clear
  209. tpm2 createprimary -C o -c prim.ctx
  210. tpm2 startauthsession -S enc_session.ctx --hmac-session -c prim.ctx
  211. tpm2 changeauth -c o owner
  212. tpm2 nvdefine -C o -P owner -a "nt=extend|ownerread|policywrite|ownerwrite" \
  213. $nv_test_index -p nvindexauth -S enc_session.ctx
  214. echo "foo" | tpm2 nvextend -C o -P owner -i- $nv_test_index -S enc_session.ctx
  215. tpm2 flushcontext enc_session.ctx
  216. rm enc_session.ctx
  217. rm prim.ctx
  218. check=$(tpm2 nvread -C o -P owner $nv_test_index | xxd -p -c 64 | sed s/'^0*'//)
  219. expected="1c8457de84bb43c18d5e1d75c43e393bdaa7bca8d25967eedd580c912db65e3e"
  220. if [ "$check" != "$expected" ]; then
  221. echo "Expected setbits read value of \"$expected\", got \"$check\""
  222. exit 1
  223. fi
  224. exit 0