output_formats.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. source helpers.sh
  3. # Purpose of this test is to cover the additional code paths that come into
  4. # play when non-default output formats for public keys or signatures are used
  5. # in the various tools.
  6. #
  7. # The test covers all available output formats, makes sure the tools
  8. # successfully run in these cases and checks the output files by feeding them
  9. # to OpenSSL as appropriate.
  10. alg_ek=rsa
  11. file_pubek_base=ek_${alg_ek}
  12. file_pubek_orig=${file_pubek_base}.tss.orig
  13. handle_ek=0x81010014
  14. alg_ak=rsa
  15. file_pubak_name="ak.${alg_ak}.name"
  16. file_pubak_tss="ak.${alg_ak}.tss"
  17. file_pubak_pem="ak.${alg_ak}.pem"
  18. handle_ak=0x81010016
  19. handle_ak_file=ak.handle
  20. ak_ctx=ak.ctx
  21. file_hash_input="hash.in"
  22. file_hash_ticket=hash.ticket
  23. file_hash_result=hash.result
  24. file_sig_base=hash.sig
  25. alg_hash=sha256
  26. file_quote_msg=quote.msg
  27. file_quote_sig_base=quote.sig
  28. cleanup() {
  29. rm -f "$file_pubek_base".*
  30. rm -f "$file_pubak_tss" "$file_pubak_name" "$file_pubak_pem"
  31. rm -f "$file_hash_ticket" "$file_hash_result" "$file_sig_base".*
  32. rm -f "$file_quote_msg" "$file_quote_sig_base".* $file_hash_input
  33. rm -f primary.ctx ecc.ctx ecc.pub ecc.priv ecc.fmt.pub $ak_ctx
  34. # Evict persistent handles, we want them to always succeed and never trip
  35. # the onerror trap.
  36. for handle in $handle_ek $handle_ak; do
  37. tpm2 evictcontrol -Q -C o -c $handle 2>/dev/null || true
  38. done
  39. shut_down
  40. }
  41. trap cleanup EXIT
  42. start_up
  43. head -c 4096 /dev/urandom > $file_hash_input
  44. tpm2 createek -Q -G $alg_ek -u "$file_pubek_orig" -c $handle_ek
  45. for fmt in tss pem der; do
  46. this_key="${file_pubek_base}.${fmt}"
  47. tpm2 readpublic -Q -c $handle_ek -f "$fmt" -o "$this_key"
  48. if [ "$fmt" = tss ]; then
  49. diff "$file_pubek_orig" "$this_key" > /dev/null
  50. else
  51. openssl rsa -pubin -inform "$fmt" -text -in "$this_key" &> /dev/null
  52. fi
  53. done
  54. tpm2 createak -Q -G $alg_ak -C $handle_ek -c $ak_ctx -u "$file_pubak_tss" \
  55. -n "$file_pubak_name"
  56. echo "tpm2 evictcontrol -Q -c $ak_ctx -o $handle_ak_file" $handle_ak
  57. tpm2 evictcontrol -Q -c $ak_ctx -o $handle_ak_file $handle_ak
  58. tpm2 readpublic -Q -c $handle_ak_file -f "pem" -o "$file_pubak_pem"
  59. tpm2 hash -Q -C e -g $alg_hash -t "$file_hash_ticket" -o "$file_hash_result" \
  60. "$file_hash_input"
  61. for fmt in tss plain; do
  62. this_sig="${file_sig_base}.${fmt}"
  63. tpm2 sign -Q -c $handle_ak -g $alg_hash -f $fmt -o "${this_sig}" \
  64. -t "${file_hash_ticket}" "${file_hash_input}"
  65. if [ "$fmt" = plain ]; then
  66. openssl dgst -verify "$file_pubak_pem" -keyform pem -${alg_hash} \
  67. -signature "$this_sig" "$file_hash_input" > /dev/null
  68. fi
  69. done
  70. for fmt in tss plain; do
  71. this_sig="${file_quote_sig_base}.${fmt}"
  72. tpm2 quote -Q -c $handle_ak -l "$alg_hash":0 -f $fmt -m "$file_quote_msg" \
  73. -s "$this_sig"
  74. if [ "$fmt" = plain ]; then
  75. openssl dgst -verify "$file_pubak_pem" -keyform pem -${alg_hash} \
  76. -signature "$this_sig" "$file_quote_msg" > /dev/null
  77. fi
  78. done
  79. #
  80. # Test ECC keys
  81. #
  82. tpm2 createprimary -c primary.ctx
  83. tpm2 create -Q -C primary.ctx -G ecc -u ecc.pub -r ecc.priv
  84. tpm2 load -C primary.ctx -u ecc.pub -r ecc.priv -c ecc.ctx
  85. for fmt in pem der; do
  86. tpm2 readpublic -Q -c ecc.ctx -f "$fmt" -o ecc.fmt.pub
  87. openssl ec -pubin -inform "$fmt" -text -in ecc.fmt.pub &> /dev/null
  88. done
  89. exit 0