attestation.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. handle_ek=0x81010009
  4. context_ak=ak.ctx
  5. handle_nv=0x1500018
  6. ek_alg=rsa
  7. ak_alg=rsa
  8. digestAlg=sha256
  9. signAlg=rsassa
  10. ownerpw=ownerpass
  11. endorsepw=endorsepass
  12. akpw=akpass
  13. rand_pcr_value=6ea40aa7267bb71251c1de1c3605a3df759b86b22fa9f62aa298d4197cd88a38
  14. debug_pcr=16
  15. debug_pcr_list=15,16,22
  16. file_input_data=secret.data
  17. file_input_key=nv.data
  18. output_ek_pub_pem=ekpub.pem
  19. output_ek_pub=ek.pub
  20. output_ak_pub_pem=akpub.pem
  21. output_ak_pub=ak.pub
  22. output_ak_pub_name=ak.name
  23. output_mkcredential=mkcred.out
  24. output_actcredential=actcred.out
  25. output_quote=quote.out
  26. output_quotesig=quotesig.out
  27. output_quotepcr=quotepcr.out
  28. cleanup() {
  29. rm -f $file_input_data $file_input_key $output_ek_pub $output_ek_pub_pem \
  30. $output_ak_pub $output_ak_pub_pem $output_ak_pub_name $output_mkcredential \
  31. $output_actcredential $output_quote $output_quotesig $output_quotepcr \
  32. $context_ak rand.out session.ctx
  33. tpm2 pcrreset -Q $debug_pcr
  34. tpm2 evictcontrol -Q -C o -c $handle_ek -P "$ownerpw" 2>/dev/null || true
  35. tpm2 evictcontrol -Q -C o -c $context_ak -P "$ownerpw" 2>/dev/null || true
  36. tpm2 nvundefine -Q $handle_nv -C o \
  37. -P "$ownerpw" 2>/dev/null || true
  38. if [ $(ina "$@" "no-shut-down") -ne 0 ]; then
  39. shut_down
  40. fi
  41. }
  42. trap cleanup EXIT
  43. start_up
  44. cleanup "no-shut-down"
  45. echo 12345678 > $file_input_data
  46. echo 1234567890123456789012345678901 > $file_input_key
  47. getrandom() {
  48. loaded_randomness=`tpm2 getrandom --hex $1`
  49. }
  50. tpm2 changeauth -c o "$ownerpw"
  51. tpm2 changeauth -c e "$endorsepw"
  52. # Key generation
  53. tpm2 createek -Q -c $handle_ek -G $ek_alg -u $output_ek_pub_pem -f pem \
  54. -w "$ownerpw" -P "$endorsepw"
  55. tpm2 readpublic -Q -c $handle_ek -o $output_ek_pub
  56. tpm2 createak -Q -C $handle_ek -c $context_ak -G $ak_alg -g $digestAlg \
  57. -s $signAlg -u $output_ak_pub_pem -f pem -n $output_ak_pub_name -p "$akpw" \
  58. -P "$endorsepw"
  59. tpm2 readpublic -Q -c $context_ak -o $output_ak_pub
  60. # Validate keys (registrar)
  61. file_size=`ls -l $output_ak_pub_name | awk {'print $5'}`
  62. loaded_key_name=`cat $output_ak_pub_name | xxd -p -c $file_size`
  63. tpm2 makecredential -Q -T none -u $output_ek_pub -s $file_input_data \
  64. -n $loaded_key_name -o $output_mkcredential
  65. tpm2 startauthsession --policy-session -S session.ctx
  66. tpm2 policysecret -S session.ctx -c e $endorsepw
  67. tpm2 activatecredential -Q -c $context_ak -C $handle_ek \
  68. -i $output_mkcredential -o $output_actcredential -p "$akpw" \
  69. -P "session:session.ctx"
  70. tpm2 flushcontext session.ctx
  71. diff $file_input_data $output_actcredential
  72. # Quoting
  73. tpm2 pcrreset -Q $debug_pcr
  74. tpm2 pcrextend -Q $debug_pcr:sha256=$rand_pcr_value
  75. tpm2 pcrread -Q
  76. getrandom 20
  77. tpm2 quote -Q -c $context_ak -l $digestAlg:$debug_pcr_list \
  78. -q $loaded_randomness -m $output_quote -s $output_quotesig -o $output_quotepcr \
  79. -g $digestAlg -p "$akpw"
  80. # Verify quote
  81. tpm2 checkquote -Q -u $output_ak_pub_pem -m $output_quote -s $output_quotesig \
  82. -f $output_quotepcr -g $digestAlg -q $loaded_randomness
  83. # Save U key from verifier
  84. tpm2 nvdefine -Q $handle_nv -C o -s 32 -a "ownerread|ownerwrite" \
  85. -p "indexpass" -P "$ownerpw"
  86. tpm2 nvwrite -Q $handle_nv -C o -P "$ownerpw" -i $file_input_key
  87. tpm2 nvread -Q $handle_nv -C o -s 32 -P "$ownerpw"
  88. exit 0