getekcertificate.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # SPDX-License-Identifier: BSD-3-Clause
  2. if [ `uname` == "FreeBSD" ]; then
  3. exit 77
  4. fi
  5. source helpers.sh
  6. cleanup() {
  7. rm -f test_rsa_ek.pub rsa_ek_cert.bin stdout_rsa_ek_cert.bin \
  8. test_ecc_ek.pub ecc_ek_cert.bin stdout_ecc_ek_cert.bin
  9. shut_down
  10. }
  11. trap cleanup EXIT
  12. start_up
  13. # Check connectivity
  14. if [ -z "$(curl -V 2>/dev/null)" ]; then
  15. echo "curl is not not installed. Skipping connection check."
  16. else
  17. if [ "$(curl --silent --output /dev/null --write-out %{http_code} \
  18. 'https://ekop.intel.com/')" != '200' ]; then
  19. echo 'No connection to https://ekop.intel.com/'
  20. exit 77
  21. fi
  22. fi
  23. # Sample RSA ek public from a real platform
  24. echo "013a0001000b000300b20020837197674484b3f81a90cc8d46a5d724fd52
  25. d76e06520b64f2a1da1b331469aa00060080004300100800000000000100
  26. c320e2f244a8601aacf3e01d26c665249935562de1da197e9e7f076c4696
  27. 13cfb653e98ec2c386fc1d133f2c8c6cc338b732f0b208bd838a877a3e5b
  28. bc3e1d4084e835c7c8906a1c05b4d2d30fdbebc1dbad950fa6b165bd4b6a
  29. 864603146164c0c4f59d489011ef1f928deea6e90061f3d375e564627315
  30. 1ef622252098be1a4ab01dc0a12227c609fdaceb115af408d4693a6f4991
  31. 9774695b0c12bc18a1ff7120a7337b2fb5f1951d8bb7f094d5b554c11c95
  32. 23b30729fe64787d0a13b9e630488dab4dfd86634a5270ec72fcc5a44dc6
  33. 79a8f32938dd8197e29dae839f5b4ca0f5de27c9522c23c54e1c2ce57859
  34. 525118bd4470b18180eef78ae4267bcd" | xxd -r -p > test_rsa_ek.pub
  35. # Get ek certificate and output to file
  36. tpm2 getekcertificate -u test_rsa_ek.pub -x -X -o rsa_ek_cert.bin
  37. # Test that stdoutput is the same
  38. tpm2 getekcertificate -u test_rsa_ek.pub -x -X > stdout_rsa_ek_cert.bin
  39. # stdout file should match
  40. cmp rsa_ek_cert.bin stdout_rsa_ek_cert.bin
  41. # Retrieved certificate should be valid
  42. tpm2 loadexternal -C e -u test_rsa_ek.pub -c rsa_key.ctx
  43. tpm2 readpublic -c rsa_key.ctx -f pem -o test_rsa_ek.pem
  44. openssl x509 -pubkey -in rsa_ek_cert.bin -noout -out test_ek.pem
  45. diff test_rsa_ek.pem test_ek.pem
  46. # Sample ECC ek public from a real platform
  47. echo "007a0023000b000300b20020837197674484b3f81a90cc8d46a5d724fd52
  48. d76e06520b64f2a1da1b331469aa00060080004300100003001000206d8e
  49. 7630ee5d11e566e80299bfb9e43cec8c44f70bc8ad81b50f690a3deb7498
  50. 002021a536c8fef7482313d7f4517f11c9f2b4cd424cbc8fe9094b895668
  51. 51fe0853" | xxd -r -p > test_ecc_ek.pub
  52. # Get ecc certificate and output to file
  53. tpm2 getekcertificate -u test_ecc_ek.pub -x -X -o ecc_ek_cert.bin
  54. # Test that stdoutput is the same
  55. tpm2 getekcertificate -u test_ecc_ek.pub -x -X > stdout_ecc_ek_cert.bin
  56. # stdout file should match
  57. cmp ecc_ek_cert.bin stdout_ecc_ek_cert.bin
  58. # Retrieved certificate should be valid
  59. tpm2 loadexternal -C e -u test_ecc_ek.pub -c ecc_key.ctx
  60. tpm2 readpublic -c ecc_key.ctx -f pem -o test_ecc_ek.pem
  61. openssl x509 -pubkey -in ecc_ek_cert.bin -noout -out test_ek.pem
  62. diff test_ecc_ek.pem test_ek.pem
  63. # Retrieve EK certificates from NV indices
  64. RSA_EK_CERT_NV_INDEX=0x01C00002
  65. ECC_EK_CERT_NV_INDEX=0x01C0000A
  66. define_ek_cert_nv_index() {
  67. file_size=`ls -l $1 | awk {'print $5'}`
  68. tpm2 nvdefine $2 -C p -s $file_size \
  69. -a 'ppwrite|ppread|ownerread|authread|no_da|platformcreate'
  70. tpm2 nvwrite -C p -i $1 $2
  71. }
  72. ## ECC only INTC certificate from NV index
  73. tpm2 getekcertificate -u test_ecc_ek.pub -x -X -o ecc_ek_cert.bin --raw
  74. define_ek_cert_nv_index ecc_ek_cert.bin $ECC_EK_CERT_NV_INDEX
  75. tpm2 getekcertificate -o nv_ecc_ek_cert.pem
  76. sed 's/-/+/g;s/_/\//g;s/%3D/=/g;s/^{.*certificate":"//g;s/"}$//g;' \
  77. ecc_ek_cert.bin | base64 --decode > ecc_test.der
  78. openssl x509 -inform PEM -outform DER -in nv_ecc_ek_cert.pem \
  79. -out nv_ecc_ek_cert.der
  80. diff nv_ecc_ek_cert.der ecc_test.der
  81. ## RSA only INTC certificate from NV index
  82. tpm2 nvundefine -C p $ECC_EK_CERT_NV_INDEX
  83. tpm2 getekcertificate -u test_rsa_ek.pub -x -X -o rsa_ek_cert.bin --raw
  84. define_ek_cert_nv_index rsa_ek_cert.bin $RSA_EK_CERT_NV_INDEX
  85. tpm2 getekcertificate -o nv_rsa_ek_cert.pem
  86. sed 's/-/+/g;s/_/\//g;s/%3D/=/g;s/^{.*certificate":"//g;s/"}$//g;' \
  87. rsa_ek_cert.bin | base64 --decode > rsa_test.der
  88. openssl x509 -inform PEM -outform DER -in nv_rsa_ek_cert.pem \
  89. -out nv_rsa_ek_cert.der
  90. diff nv_rsa_ek_cert.der rsa_test.der
  91. ## RSA & ECC INTC certificates from NV index
  92. define_ek_cert_nv_index ecc_ek_cert.bin $ECC_EK_CERT_NV_INDEX
  93. tpm2 getekcertificate -o nv_rsa_ek_cert.pem -o nv_ecc_ek_cert.pem
  94. openssl x509 -inform PEM -outform DER -in nv_ecc_ek_cert.pem \
  95. -out nv_ecc_ek_cert.der
  96. openssl x509 -inform PEM -outform DER -in nv_rsa_ek_cert.pem \
  97. -out nv_rsa_ek_cert.der
  98. diff nv_ecc_ek_cert.der ecc_test.der
  99. diff nv_rsa_ek_cert.der rsa_test.der
  100. exit 0