helpers.sh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. set -E
  3. shopt -s expand_aliases
  4. # We get what python interpreter to run from configure, so alias python here
  5. # to make subordiante scripts work.
  6. alias python=${PYTHON:-python}
  7. # Return 0 if run by a TPM simulator, return 1 otherwise
  8. is_simulator() {
  9. # both simulators mssim and swtpm have their vendor sting set to "SW":
  10. # TPM2_PT_VENDOR_STRING_1:
  11. # raw: 0x53572020
  12. # value: "SW"
  13. tpm2 getcap properties-fixed \
  14. | grep -zP "TPM2_PT_VENDOR_STRING_1:\s*raw: 0x53572020" &>/dev/null
  15. }
  16. # Return 0 if algorithm is supported, return 1 otherwise
  17. # Error if TPM is simulator and algorithm is unsupported
  18. is_alg_supported() {
  19. if tpm2 testparms $1 2> /dev/null; then
  20. return 0
  21. else
  22. if is_simulator; then
  23. echo "ERROR: $1 is not supported by the TPM simulator."
  24. exit 1
  25. else
  26. echo "SKIP: Testing on a non-simulator TPM. Skipping unsupported algorithm $1"
  27. return 1
  28. fi
  29. fi
  30. }
  31. # Return 0 if command is supported, return 1 otherwise
  32. # Error if TPM is simulator and command is unsupported
  33. is_cmd_supported() {
  34. if tpm2 getcap commands | grep -i "$1:" &> /dev/null; then
  35. return 0
  36. else
  37. if is_simulator; then
  38. echo "ERROR: $1 is not supported by the TPM simulator."
  39. exit 1
  40. else
  41. echo "SKIP: Testing on a non-simulator TPM. Skipping unsupported command $1"
  42. return 1
  43. fi
  44. fi
  45. }
  46. function filter_algs_by() {
  47. python << pyscript
  48. from __future__ import print_function
  49. import sys
  50. import yaml
  51. with open("$1") as f:
  52. try:
  53. y = yaml.safe_load(f)
  54. for alg, details in y.items():
  55. if $2:
  56. print(alg)
  57. except yaml.YAMLError as exc:
  58. sys.exit(exc)
  59. pyscript
  60. }
  61. populate_algs() {
  62. algs="$(mktemp)"
  63. tpm2 getcap algorithms > "${algs}"
  64. filter_algs_by "${algs}" "${1}"
  65. rm "${algs}"
  66. }
  67. populate_hash_algs() {
  68. populate_algs "details['hash'] and not details['method'] and not details['symmetric'] and not details['signing'] $1"
  69. }
  70. # Return alg argument if supported by TPM.
  71. hash_alg_supported() {
  72. local orig_alg="$1"
  73. local alg="$orig_alg"
  74. local algs_supported
  75. algs_supported="$(populate_hash_algs name)"
  76. local hex2name=(
  77. [0x04]="sha1"
  78. [0x0B]="sha256"
  79. [0x0C]="sha384"
  80. [0x0D]="sha512"
  81. [0x12]="sm3_256"
  82. )
  83. if [ -z "$alg" ]; then
  84. echo "$algs_supported"
  85. return
  86. fi
  87. if [ "$alg" = "${alg//[^0-9a-fA-FxX]/}" ]; then
  88. alg=${hex2name["$alg"]}
  89. [ -z "$alg" ] && return
  90. fi
  91. local t_alg
  92. for t_alg in $algs_supported; do
  93. if [ "$t_alg" = "$alg" ]; then
  94. echo "$orig_alg"
  95. return
  96. fi
  97. done
  98. }
  99. # Get nice names of supported algorithms lengths
  100. # Does not work with hashes!
  101. # e.g. calling "populate_alg_lengths rsa" will print:
  102. # rsa1024
  103. # rsa2048
  104. populate_alg_lengths() {
  105. #set -x
  106. alg="$1"
  107. local lengths="1 128 192 224 256 384 512 1024 2048 4096"
  108. local populated=""
  109. for len in $lengths; do
  110. if tpm2 testparms "$alg$len" 2> /dev/null; then
  111. if [ -z "$populated" ]; then
  112. populated="$alg$len"
  113. else
  114. populated="$populated\n$alg$len"
  115. fi
  116. fi
  117. done;
  118. printf "$populated"
  119. }
  120. # Get nice name of the algorithm with its weakest supported key size
  121. # Does not work with hashes!
  122. # e.g. calling "weakest_alg aes" will print "aes128"
  123. weakest_alg() {
  124. populate_alg_lengths "$1" | head -n1
  125. }
  126. # Get nice name of the algorithm with its strongest supported key size
  127. # Does not work with hashes!
  128. # e.g. calling "strongest_alg aes" will print "aes256"
  129. strongest_alg() {
  130. populate_alg_lengths "$1" | tail -n1
  131. }
  132. # Get nice names of supported algorithm modes
  133. # Does not work with hashes!
  134. # e.g. calling "populate_alg_modes aes128" will print:
  135. # aes128cfb
  136. # aes128cbc
  137. populate_alg_modes() {
  138. #set -x
  139. alg="$1"
  140. local modes="ctr ofb cbc cfb ecb"
  141. local populated=""
  142. for mode in $modes; do
  143. if tpm2 testparms "$alg$mode" 2> /dev/null; then
  144. if [ -z "$populated" ]; then
  145. populated="$alg$mode"
  146. else
  147. populated="$populated\n$alg$mode"
  148. fi
  149. fi
  150. done;
  151. printf "$populated"
  152. }
  153. #
  154. # Verifies that the contexts of a file path provided
  155. # as the first argument loads as a YAML file.
  156. #
  157. function yaml_verify() {
  158. python << pyscript
  159. from __future__ import print_function
  160. import sys
  161. import yaml
  162. with open("$1") as f:
  163. try:
  164. y = yaml.safe_load(f)
  165. except yaml.YAMLError as exc:
  166. sys.exit(exc)
  167. pyscript
  168. }
  169. #
  170. # Given a file as argument 1, prints the value of the key
  171. # provided as argument 2 and optionally argument 3 (for nested maps).
  172. # Note that all names and values are parsed as strings.
  173. #
  174. function yaml_get_kv() {
  175. third_arg=""
  176. if [ $# -eq 3 ]; then
  177. third_arg=$3
  178. fi
  179. python << pyscript
  180. from __future__ import print_function
  181. import sys
  182. import yaml
  183. with open("$1") as f:
  184. try:
  185. y = yaml.load(f, Loader=yaml.BaseLoader)
  186. if $# == 3:
  187. print(y["$2"]["$third_arg"])
  188. else:
  189. print(y["$2"])
  190. except yaml.YAMLError as exc:
  191. sys.exit(exc)
  192. pyscript
  193. }
  194. function recreate_info() {
  195. echo
  196. echo "--- To recreate this test run the following: ---"
  197. local a="export TPM2_ABRMD=\"$TPM2_ABRMD\"\n"
  198. a="$a""export TPM2_SIM=\"$TPM2_SIM\"\n"
  199. a="$a""export TPM2ABRMD_TCTI=\"$TPM2ABRMD_TCTI\"\n"
  200. a="$a""export TPM2_SIMPORT=\"$TPM2_SIMPORT\"\n"
  201. a="$a""export TPM2TOOLS_TEST_TCTI=\"$TPM2TOOLS_TEST_TCTI\"\n"
  202. a="$a""export TPM2TOOLS_TEST_PERSISTENT=\"$TPM2TOOLS_TEST_PERSISTENT\"\n"
  203. a="$a""export PATH=\"$PATH\"\n"
  204. a="$a""export srcdir=\"$srcdir\"\n"
  205. a="$a""export abs_srcdir=\"$abs_srcdir\"\n"
  206. a="$a""export abs_builddir=\"$abs_builddir\"\n"
  207. echo "#!/usr/bin/env bash"
  208. echo -e "$a"
  209. local script="$tpm2_test_original_cwd""/""$0"
  210. echo $(realpath "$script")
  211. echo "--- EOF ---"
  212. echo
  213. }
  214. tpm2_test_original_cwd=""
  215. tpm2_test_cwd=""
  216. function switch_to_test_dir() {
  217. tpm2_test_original_cwd=`pwd`;
  218. tpm2_test_cwd=$(mktemp -d ${TMPDIR:-/tmp}/tpm2_test_XXXXXX)
  219. echo "creating simulator working dir: $tpm2_test_cwd"
  220. pushd "$tpm2_test_cwd"
  221. echo "Switched to CWD: $(pwd)"
  222. }
  223. function switch_back_from_test_dir() {
  224. popd
  225. }
  226. tpm2_sim_pid=""
  227. tpm2_abrmd_pid=""
  228. tpm2_tcti_opts=""
  229. tpm2tools_tcti=""
  230. sock_tool="unknown"
  231. OS=$(uname)
  232. if [ "$OS" == "Linux" ]; then
  233. sock_tool="ss -lntp4"
  234. elif [ "$OS" == "FreeBSD" ]; then
  235. sock_tool="sockstat -l4"
  236. fi
  237. function start_sim() {
  238. local max_cnt=10
  239. # if a user is specifying the sim port, then only attempt it once
  240. if [ -n "$TPM2_SIMPORT" ]; then
  241. max_cnt=1
  242. fi
  243. while [ $max_cnt -gt 0 ]; do
  244. # If either the requested simulator port or the port that will be used
  245. # by mssim TCTI which is tpm2_sim_port + 1 is occupied (ESTABLISHED, TIME_WAIT, etc...),
  246. # just continue up to 10 retries
  247. # (See : https://github.com/tpm2-software/tpm2-tss/blob/master/src/tss2-tcti/tcti-mssim.c:559)
  248. if [ -z "$TPM2_SIMPORT" ]; then
  249. tpm2_sim_port="$(od -A n -N 2 -t u2 /dev/urandom | awk -v min=2321 -v max=65534 '{print ($1 % (max - min)) + min}')"
  250. else
  251. tpm2_sim_port=$TPM2_SIMPORT
  252. fi
  253. tpm2_sim_cmd_port=$((tpm2_sim_port + 1))
  254. echo "Attempting to start simulator on port: $tpm2_sim_port"
  255. case "$TPM2_SIM" in
  256. *swtpm) "$TPM2_SIM" socket --tpm2 --server port="$tpm2_sim_port" \
  257. --ctrl type=tcp,port="$tpm2_sim_cmd_port" \
  258. --flags not-need-init --tpmstate dir="$PWD" &;;
  259. *tpm_server) "$TPM2_SIM" -port "$tpm2_sim_port" &;;
  260. *) echo "Unknown TPM simulator $TPM2_SIM"; return 1;;
  261. esac
  262. tpm2_sim_pid=$!
  263. sleep 1
  264. ${sock_tool} 2>/dev/null | grep ${TPM2_SIM} | grep ${tpm2_sim_pid} | grep ${tpm2_sim_port}
  265. tpm2_sim_port_rc=$?
  266. ${sock_tool} 2>/dev/null | grep ${TPM2_SIM} | grep ${tpm2_sim_pid} | grep ${tpm2_sim_cmd_port}
  267. tpm2_sim_cmd_port_rc=$?
  268. if [[ $tpm2_sim_port_rc -eq 0 ]] && [[ $tpm2_sim_cmd_port_rc -eq 0 ]]; then
  269. echo "Started simulator on port $tpm2_sim_port in dir \"$PWD\""
  270. TPM2_SIMPORT=$tpm2_sim_port
  271. case "$TPM2_SIM" in
  272. *swtpm) tpm2tools_tcti="swtpm:host=localhost,port=$TPM2_SIMPORT";;
  273. *tpm_server) tpm2tools_tcti="mssim:host=localhost,port=$TPM2_SIMPORT";;
  274. *) echo "Unknown TPM simulator $TPM2_SIM"; return 1;;
  275. esac
  276. echo "tpm2tools_tcti=\"$tpm2tools_tcti\""
  277. return 0
  278. else
  279. echo "Could not start simulator at port: $tpm2_sim_port"
  280. kill "$tpm2_sim_pid"
  281. let "max_cnt=max_cnt-1"
  282. echo "Tries left: $max_cnt"
  283. fi
  284. done
  285. echo "Maximum attempts reached. Aborting"
  286. return 1
  287. }
  288. function start_abrmd() {
  289. local tpm2_tabrmd_opts
  290. # if we don't have an explicit TCTI to connect to, generate it
  291. if [ -z "$TPM2ABRMD_TCTI" ]; then
  292. echo "TPM2ABRMD_TCTI is empty, configuring"
  293. if [ -z "$TPM2_SIMPORT" ]; then
  294. echo "No simulator port found, can not determine ABRMD TCTI conf"
  295. return 1
  296. fi
  297. # TCTI information for use with ABRMD
  298. local name="com.intel.tss2.Tabrmd${TPM2_SIMPORT}"
  299. tpm2_tabrmd_opts="--session --dbus-name=$name"
  300. case "$TPM2_SIM" in
  301. *swtpm) tpm2_tabrmd_opts="$tpm2_tabrmd_opts --tcti=swtpm:port=$TPM2_SIMPORT";;
  302. *tpm_server) tpm2_tabrmd_opts="$tpm2_tabrmd_opts --tcti=mssim:port=$TPM2_SIMPORT";;
  303. *) echo "Unknown TPM simulator $TPM2_SIM"; return 1;;
  304. esac
  305. echo "TPM2ABRMD_TCTI=\"$tpm2_tabrmd_opts\""
  306. TPM2ABRMD_TCTI="$tpm2_tabrmd_opts"
  307. fi
  308. if [ $UID -eq 0 ]; then
  309. tpm2_tabrmd_opts="--allow-root $tpm2_tabrmd_opts"
  310. fi
  311. echo "tpm2-abrmd command: $TPM2_ABRMD $tpm2_tabrmd_opts $TPM2ABRMD_TCTI"
  312. $TPM2_ABRMD $tpm2_tabrmd_opts $TPM2ABRMD_TCTI &
  313. tpm2_abrmd_pid=$!
  314. sleep 2
  315. if ! kill -0 "$tpm2_abrmd_pid"; then
  316. (>&2 echo "Could not start tpm2-abrmd \"$TPM2_ABRMD\", exit code: $?")
  317. kill -9 $tpm2_abrmd_pid
  318. return 1
  319. fi
  320. # set a possible tools tcti to use abrmd
  321. tpm2tools_tcti="tabrmd:bus_type=session,bus_name=$name"
  322. echo "tpm2tools_tcti=\"$tpm2tools_tcti\""
  323. return 0
  324. }
  325. #
  326. # This start up routine performs the following actions and should
  327. # be called by testing scripts if they need a TCTI. It also outputs
  328. # information for how to recreate the test outside of the test harness.
  329. #
  330. # 1. Start the simulator if specified via env var TPM2_SIM. if TPM2_SIMPORT
  331. # is set, it attempts to start the simulator AT that port, else it tries
  332. # a random port, and sets TPM2_SIMPORT to the random port if successful.
  333. #
  334. # 2. Start abrmd if specified via env var TPM2_ABRMD. if TPM2ABRMD_TCTI is
  335. # set it starts abrmd using that TCTI, else it uses the value of TPM2_SIMPORT.
  336. #
  337. # 3. Pick a TCTI for the tools based on:
  338. # a) TPM2TOOLS_TEST_TCTI user specified, just use it.
  339. # b) TPM2TOOLS_TEST_TCTI not specified, the start_sim and start_anrmd routines
  340. # set tpm2tools_tcti variable, so use that.
  341. #
  342. function start_up() {
  343. switch_to_test_dir
  344. run_startup=true
  345. if [ -n "$TPM2_SIM" ]; then
  346. # Start the simulator
  347. echo "Starting the simulator"
  348. start_sim || exit 1
  349. echo "Started the simulator"
  350. else
  351. echo "not starting simulator"
  352. fi
  353. if [ -n "$TPM2_ABRMD" ]; then
  354. echo "Starting tpm2-abrmd"
  355. # Start tpm2-abrmd
  356. start_abrmd || exit 1
  357. run_startup=false
  358. else
  359. echo "not starting abrmd"
  360. fi
  361. echo "TPM2TOOLS_TEST_TCTI=$TPM2TOOLS_TEST_TCTI"
  362. if [ -z "$TPM2TOOLS_TEST_TCTI" ]; then
  363. echo "TPM2TOOLS_TEST_TCTI not set, attempting to figure out default"
  364. if [ -z "$tpm2tools_tcti" ]; then
  365. echo "The simulator not abrmd was started, cannot determine a TCTI for tools."
  366. exit 1;
  367. fi
  368. TPM2TOOLS_TEST_TCTI="$tpm2tools_tcti"
  369. fi
  370. echo "export TPM2TOOLS_TCTI=\"$TPM2TOOLS_TEST_TCTI\""
  371. export TPM2TOOLS_TCTI="$TPM2TOOLS_TEST_TCTI"
  372. recreate_info
  373. echo "run_startup: $run_startup"
  374. if [ $run_startup = true ]; then
  375. tpm2 startup -c
  376. fi
  377. if ! tpm2 clear; then
  378. exit 1
  379. fi
  380. }
  381. function shut_down() {
  382. echo "Shutting down"
  383. switch_back_from_test_dir
  384. fail=0
  385. if [ -n "$tpm2_abrmd_pid" ]; then
  386. if kill -0 "$tpm2_abrmd_pid"; then
  387. if ! kill -9 "$tpm2_abrmd_pid"; then
  388. (>&2 echo "ERROR: could not kill tpm2_abrmd on pid: $tpm2_abrmd_pid")
  389. fail=1
  390. fi
  391. else
  392. (>&2 echo "WARNING: tpm2_abrmd already stopped ($tpm2_abrmd_pid)")
  393. fi
  394. fi
  395. tpm2_abrmd_pid=""
  396. if [ -n "$tpm2_sim_pid" ]; then
  397. if kill -0 "$tpm2_sim_pid"; then
  398. if ! kill -9 "$tpm2_sim_pid"; then
  399. (>&2 echo "ERROR: could not kill tpm2 simulator on pid: $tpm2_sim_pid")
  400. fail=1
  401. fi
  402. else
  403. (>&2 echo "WARNING: TPM simulator already stopped ($tpm2_sim_pid)")
  404. fi
  405. fi
  406. tpm2_sim_pid=""
  407. echo "Removing sim dir: $tpm2_test_cwd"
  408. rm -rf "$tpm2_test_cwd" 2>/dev/null
  409. if [ $fail -ne 0 ]; then
  410. exit 1
  411. fi
  412. }
  413. #
  414. # Set the default EXIT handler to always shut down, tests
  415. # can override this.
  416. #
  417. trap shut_down EXIT
  418. #
  419. # Set the default on ERR handler to print the line number
  420. # and failed command.
  421. #
  422. onerror() {
  423. echo "$BASH_COMMAND on line ${BASH_LINENO[0]} failed: $?"
  424. exit 1
  425. }
  426. trap onerror ERR
  427. #
  428. # print 0 if the list of arguments 1 to n-1 contains the last argument n
  429. # print 1 otherwise
  430. #
  431. function ina() {
  432. local n=$#
  433. local value=${!n}
  434. for ((i=1;i < $#;i++)) {
  435. if [ "${!i}" == "${value}" ]; then
  436. echo 0
  437. return
  438. fi
  439. }
  440. echo 1
  441. }
  442. # Causes a test to skip by exiting with error code 77
  443. # See the automake manual for the exit codes:
  444. # https://www.gnu.org/software/automake/manual/html_node/Scripts_002dbased-Testsuites.html
  445. function skip_test() {
  446. exit 77
  447. }
  448. function setup_fapi() {
  449. tempdir=`pwd`/$(mktemp -d tss2_fapi.XXXXXX)
  450. KEYSTORE_USER=keystore_user
  451. KEYSTORE_SYSTEM=keystore_system
  452. LOG_DIR=log
  453. PROFILE_NAME_ECC=P_ECCP256SHA256
  454. PROFILE_NAME_RSA=P_RSA2048SHA256
  455. if [ "$1" = "ECC" ]; then
  456. PROFILE_NAME=$PROFILE_NAME_ECC
  457. else
  458. PROFILE_NAME=$PROFILE_NAME_RSA
  459. fi
  460. mkdir -p $tempdir/$KEYSTORE_USER/policy $tempdir/$KEYSTORE_SYSTEM/policy \
  461. $tempdir/$LOG_DIR
  462. cat > $tempdir/fapi_config.json <<EOF
  463. {
  464. "profile_name": "${PROFILE_NAME}",
  465. "profile_dir": "$tempdir/",
  466. "user_dir": "$tempdir/${KEYSTORE_USER}",
  467. "system_dir": "$tempdir/${KEYSTORE_SYSTEM}",
  468. "tcti": "${TPM2TOOLS_TCTI}",
  469. "system_pcrs" : [],
  470. "ek_cert_less": "yes",
  471. "log_dir" : "$tempdir/${LOG_DIR}",
  472. }
  473. EOF
  474. export TSS2_FAPICONF=$tempdir/fapi_config.json
  475. export TEMP_DIR=$tempdir
  476. dd if=/dev/zero of=$tempdir/big_file.file bs=1M count=10
  477. touch $tempdir/empty.file
  478. SANITIZER_FILTER="*"AddressSanitizer"*"
  479. setup_profiles $tempdir
  480. setup_policies $tempdir
  481. resetPCR16
  482. }
  483. # Reset PCR 16. Important when using physical TPM
  484. function resetPCR16(){
  485. tpm2 pcrreset 16
  486. }
  487. function setup_profiles() {
  488. # Setup Profiles
  489. cat > $tempdir/${PROFILE_NAME_RSA}.json <<EOF
  490. {
  491. "type": "TPM2_ALG_RSA",
  492. "nameAlg":"TPM2_ALG_SHA256",
  493. "srk_template": "system,restricted,decrypt,0x81000001",
  494. "srk_persistent": 1,
  495. "ek_template": "system,restricted,decrypt",
  496. "rsa_signing_scheme": {
  497. "scheme":"TPM2_ALG_RSAPSS",
  498. "details":{
  499. "hashAlg":"TPM2_ALG_SHA256"
  500. }
  501. },
  502. "rsa_decrypt_scheme": {
  503. "scheme":"TPM2_ALG_OAEP",
  504. "details":{
  505. "hashAlg":"TPM2_ALG_SHA256"
  506. }
  507. },
  508. "sym_mode":"TPM2_ALG_CFB",
  509. "sym_parameters": {
  510. "algorithm":"TPM2_ALG_AES",
  511. "keyBits":"128",
  512. "mode":"TPM2_ALG_CFB"
  513. },
  514. "sym_block_size": 16,
  515. "pcr_selection": [
  516. { "hash": "TPM2_ALG_SHA1",
  517. "pcrSelect": [ ]
  518. },
  519. { "hash": "TPM2_ALG_SHA256",
  520. "pcrSelect": [ 8, 9 , 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 ]
  521. }
  522. ],
  523. "exponent": 0,
  524. "keyBits": 2048,
  525. "session_hash_alg": "TPM2_ALG_SHA256",
  526. "session_symmetric":{
  527. "algorithm":"TPM2_ALG_AES",
  528. "keyBits":"128",
  529. "mode":"TPM2_ALG_CFB"
  530. },
  531. "ek_policy": {
  532. "description": "Endorsement hierarchy used for policy secret.",
  533. "policy":[
  534. {
  535. "type":"POLICYSECRET",
  536. "objectName": "4000000b",
  537. }
  538. ]
  539. }
  540. }
  541. EOF
  542. cat > $tempdir/${PROFILE_NAME_ECC}.json <<EOF
  543. {
  544. "type": "TPM2_ALG_ECC",
  545. "nameAlg":"TPM2_ALG_SHA256",
  546. "srk_template": "system,restricted,decrypt,0x81000001",
  547. "srk_persistent": 0,
  548. "ek_template": "system,restricted,decrypt",
  549. "ecc_signing_scheme": {
  550. "scheme":"TPM2_ALG_ECDSA",
  551. "details":{
  552. "hashAlg":"TPM2_ALG_SHA256"
  553. },
  554. },
  555. "sym_mode":"TPM2_ALG_CFB",
  556. "sym_parameters": {
  557. "algorithm":"TPM2_ALG_AES",
  558. "keyBits":"128",
  559. "mode":"TPM2_ALG_CFB"
  560. },
  561. "sym_block_size": 16,
  562. "pcr_selection": [
  563. { "hash": "TPM2_ALG_SHA1",
  564. "pcrSelect": [ ],
  565. },
  566. { "hash": "TPM2_ALG_SHA256",
  567. "pcrSelect": [ 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 ]
  568. }
  569. ],
  570. "curveID": "TPM2_ECC_NIST_P256",
  571. "ek_policy": {
  572. "description": "Endorsement hierarchy used for policy secret.",
  573. "policy":[
  574. {
  575. "type":"POLICYSECRET",
  576. "objectName": "4000000b",
  577. }
  578. ]
  579. }
  580. }
  581. EOF
  582. }
  583. function setup_policies() {
  584. tempdir=$1
  585. # Setup Policy Authorize
  586. cat > $tempdir/pol_authorize.json <<EOF
  587. {
  588. "description":"Description pol_authorize",
  589. "policy":[
  590. {
  591. "type": "POLICYAUTHORIZE",
  592. "keyPath": "/HS/SRK/myPolicySignKey"
  593. }
  594. ]
  595. }
  596. EOF
  597. # Setup Policy Authorize with reference value
  598. cat > $tempdir/pol_authorize_ref.json <<EOF
  599. {
  600. "description":"Description pol_authorize",
  601. "policy":[
  602. {
  603. "type": "POLICYAUTHORIZE",
  604. "keyPath": "/HS/SRK/myPolicySignKey",
  605. "policyRef": "f0f1f2f3f4f5f6f7f8f9"
  606. }
  607. ]
  608. }
  609. EOF
  610. # Setup Policy Authorize NV
  611. cat > $tempdir/pol_authorize_nv.json <<EOF
  612. {
  613. "description":"Description pol_authorize_nv",
  614. "policy":[
  615. {
  616. "type": "POLICYAUTHORIZENV",
  617. "nvPath": "/nv/Owner/myNV",
  618. }
  619. ]
  620. }
  621. EOF
  622. # Setup Policy Duplicate
  623. cat > $tempdir/pol_duplicate.json <<EOF
  624. {
  625. "description":"Description pol_duplicate",
  626. "policy":[
  627. {
  628. "type": "POLICYDUPLICATIONSELECT",
  629. "newParentPath": "ext/myNewParent",
  630. }
  631. ]
  632. }
  633. EOF
  634. # Setup Policy PCR
  635. cat > $tempdir/pol_pcr16_0.json <<EOF
  636. {
  637. "description":"Description pol_16_0",
  638. "policy":[
  639. {
  640. "type":"POLICYPCR",
  641. "pcrs":[
  642. {
  643. "pcr":16,
  644. "hashAlg":"TPM2_ALG_SHA",
  645. "digest":"0000000000000000000000000000000000000000"
  646. }
  647. ]
  648. }
  649. ]
  650. }
  651. EOF
  652. # Setup Policy with branch for write and branch for read.
  653. cat > $tempdir/pol_nv_read_write.json <<EOF
  654. {
  655. "description": "Different policy for NV read and NV write",
  656. "policy": [
  657. {
  658. "type": "or",
  659. "branches": [
  660. {
  661. "name": "NVWrite",
  662. "description": "For NV Write we want to have PCR16 at 0",
  663. "policy": [
  664. {
  665. "type": "commandCode",
  666. "code": "NV_WRITE"
  667. },
  668. {
  669. "type": "pcr",
  670. "pcrs": [
  671. {
  672. "hashAlg": "sha256",
  673. "pcr": 16,
  674. "digest": "0000000000000000000000000000000000000000000000000000000000000000"
  675. }
  676. ]
  677. }
  678. ]
  679. },
  680. {
  681. "name": "NVRead",
  682. "description": "For NV Read we don't need any auth",
  683. "policy": [
  684. {
  685. "type": "commandCode",
  686. "code": "NV_READ"
  687. }
  688. ]
  689. }
  690. ]
  691. }
  692. ]
  693. }
  694. EOF
  695. # Setup Policy with policy password or branch for write and branch for read.
  696. cat > $tempdir/pol_pwd_nv_read_write.json <<EOF
  697. {
  698. "description": "Policy password or different policy for NV read and NV write",
  699. "policy": [
  700. {
  701. "type": "or",
  702. "branches": [
  703. {
  704. "name": "Password",
  705. "description": "We can always supply the auth value",
  706. "policy": [
  707. {
  708. "type": "password"
  709. }
  710. ]
  711. },
  712. {
  713. "name": "NVReadWrite",
  714. "description": "For NV Read Write we have a special handling",
  715. "policy": [
  716. {
  717. "type": "or",
  718. "branches": [
  719. {
  720. "name": "NVWrite",
  721. "description": "For NV Write we want to have PCR16 at 0",
  722. "policy": [
  723. {
  724. "type": "commandCode",
  725. "code": "NV_WRITE"
  726. },
  727. {
  728. "type": "pcr",
  729. "pcrs": [
  730. {
  731. "hashAlg": "sha256",
  732. "pcr": 16,
  733. "digest": "0000000000000000000000000000000000000000000000000000000000000000"
  734. }
  735. ]
  736. }
  737. ]
  738. },
  739. {
  740. "name": "NVRead",
  741. "description": "For NV Read we don't need any auth",
  742. "policy": [
  743. {
  744. "type": "commandCode",
  745. "code": "NV_READ"
  746. }
  747. ]
  748. }
  749. ]
  750. }
  751. ]
  752. }
  753. ]
  754. }
  755. ]
  756. }
  757. EOF
  758. # Setup Policy Signed
  759. cat > $tempdir/pol_signed.json <<EOF
  760. {
  761. "description":"Description pol_signed",
  762. "policy":[
  763. {
  764. "type": "POLICYSIGNED",
  765. "keyPEM": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoGL6IrCSAznmIIzBessI\nmW7tPOUy78uWTIaub32KnYHn78KXprrZ3ykp6WDrOQeMjv4AA+14mJbg77apVYXy\nEnkFdOMa1hszSJnp6cJvx7ILngLvFUxzbVki\/ehvgS3nRk67Njal+nMTe8hpe3UK\nQeV\/Ij+F0r6Yz91W+4LPmncAiUesRZLetI2BZsKwHYRMznmpIYpoua1NtS8QpEXR\nMmsUue19eS\/XRAPmmCfnb5BX2Tn06iCpk6wO+RfMo9etcX5cLSAuIYEQYCvV2\/0X\nTfEw607vttBN0Y54LrVOKno1vRXd5sxyRlfB0WL42F4VG5TfcJo5u1Xq7k9m9K57\n8wIDAQAB\n-----END PUBLIC KEY-----\n"
  766. }
  767. ]
  768. }
  769. EOF
  770. }