activecredential.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. cleanup() {
  4. rm -f secret.data ek.pub ak.pub ak.name mkcred.out actcred.out ak.out \
  5. ak.ctx session.ctx
  6. # Evict persistent handles, we want them to always succeed and never trip
  7. # the onerror trap.
  8. tpm2 evictcontrol -Q -C o -c 0x81010009 2>/dev/null || true
  9. if [ "$1" != "no-shut-down" ]; then
  10. shut_down
  11. fi
  12. }
  13. trap cleanup EXIT
  14. start_up
  15. cleanup "no-shut-down"
  16. echo 12345678 > secret.data
  17. tpm2 createek -Q -c 0x81010009 -G rsa -u ek.pub
  18. tpm2 createak -C 0x81010009 -c ak.ctx -G rsa -g sha256 -s rsassa -u ak.pub \
  19. -n ak.name -p akpass> ak.out
  20. file_size=`ls -l ak.name | awk {'print $5'}`
  21. loaded_key_name=`cat ak.name | xxd -p -c $file_size` # Use -c in xxd so there is no line wrapping
  22. tpm2 readpublic -c 0x81010009 -o ek.pem -f pem -Q
  23. echo "12345678" | tpm2 makecredential -Q -u ek.pem -s - -n $loaded_key_name \
  24. -o mkcred.out -G rsa
  25. # Test the secret data matches after credential activation process
  26. tpm2 startauthsession --policy-session -S session.ctx
  27. tpm2 policysecret -S session.ctx -c e
  28. tpm2 activatecredential -Q -c ak.ctx -C 0x81010009 -i mkcred.out \
  29. -o actcred.out -p akpass -P"session:session.ctx"
  30. tpm2 flushcontext session.ctx
  31. diff actcred.out secret.data
  32. # Capture the yaml output and verify that its the same as the name output
  33. loaded_key_name_yaml=`python << pyscript
  34. from __future__ import print_function
  35. import yaml
  36. with open('ak.out', 'r') as f:
  37. doc = yaml.safe_load(f)
  38. print(doc['loaded-key']['name'])
  39. pyscript`
  40. test "$loaded_key_name_yaml" == "$loaded_key_name"
  41. exit 0