Tpm.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace lbuchs\WebAuthn\Attestation\Format;
  3. use lbuchs\WebAuthn\Attestation\AuthenticatorData;
  4. use lbuchs\WebAuthn\WebAuthnException;
  5. use lbuchs\WebAuthn\Binary\ByteBuffer;
  6. class Tpm extends FormatBase {
  7. private $_TPM_GENERATED_VALUE = "\xFF\x54\x43\x47";
  8. private $_TPM_ST_ATTEST_CERTIFY = "\x80\x17";
  9. private $_alg;
  10. private $_signature;
  11. private $_pubArea;
  12. private $_x5c;
  13. /**
  14. * @var ByteBuffer
  15. */
  16. private $_certInfo;
  17. public function __construct($AttestionObject, AuthenticatorData $authenticatorData) {
  18. parent::__construct($AttestionObject, $authenticatorData);
  19. // check packed data
  20. $attStmt = $this->_attestationObject['attStmt'];
  21. if (!\array_key_exists('ver', $attStmt) || $attStmt['ver'] !== '2.0') {
  22. throw new WebAuthnException('invalid tpm version: ' . $attStmt['ver'], WebAuthnException::INVALID_DATA);
  23. }
  24. if (!\array_key_exists('alg', $attStmt) || $this->_getCoseAlgorithm($attStmt['alg']) === null) {
  25. throw new WebAuthnException('unsupported alg: ' . $attStmt['alg'], WebAuthnException::INVALID_DATA);
  26. }
  27. if (!\array_key_exists('sig', $attStmt) || !\is_object($attStmt['sig']) || !($attStmt['sig'] instanceof ByteBuffer)) {
  28. throw new WebAuthnException('signature not found', WebAuthnException::INVALID_DATA);
  29. }
  30. if (!\array_key_exists('certInfo', $attStmt) || !\is_object($attStmt['certInfo']) || !($attStmt['certInfo'] instanceof ByteBuffer)) {
  31. throw new WebAuthnException('certInfo not found', WebAuthnException::INVALID_DATA);
  32. }
  33. if (!\array_key_exists('pubArea', $attStmt) || !\is_object($attStmt['pubArea']) || !($attStmt['pubArea'] instanceof ByteBuffer)) {
  34. throw new WebAuthnException('pubArea not found', WebAuthnException::INVALID_DATA);
  35. }
  36. $this->_alg = $attStmt['alg'];
  37. $this->_signature = $attStmt['sig']->getBinaryString();
  38. $this->_certInfo = $attStmt['certInfo'];
  39. $this->_pubArea = $attStmt['pubArea'];
  40. // certificate for validation
  41. if (\array_key_exists('x5c', $attStmt) && \is_array($attStmt['x5c']) && \count($attStmt['x5c']) > 0) {
  42. // The attestation certificate attestnCert MUST be the first element in the array
  43. $attestnCert = array_shift($attStmt['x5c']);
  44. if (!($attestnCert instanceof ByteBuffer)) {
  45. throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
  46. }
  47. $this->_x5c = $attestnCert->getBinaryString();
  48. // certificate chain
  49. foreach ($attStmt['x5c'] as $chain) {
  50. if ($chain instanceof ByteBuffer) {
  51. $this->_x5c_chain[] = $chain->getBinaryString();
  52. }
  53. }
  54. } else {
  55. throw new WebAuthnException('no x5c certificate found', WebAuthnException::INVALID_DATA);
  56. }
  57. }
  58. /*
  59. * returns the key certificate in PEM format
  60. * @return string|null
  61. */
  62. public function getCertificatePem() {
  63. if (!$this->_x5c) {
  64. return null;
  65. }
  66. return $this->_createCertificatePem($this->_x5c);
  67. }
  68. /**
  69. * @param string $clientDataHash
  70. */
  71. public function validateAttestation($clientDataHash) {
  72. return $this->_validateOverX5c($clientDataHash);
  73. }
  74. /**
  75. * validates the certificate against root certificates
  76. * @param array $rootCas
  77. * @return boolean
  78. * @throws WebAuthnException
  79. */
  80. public function validateRootCertificate($rootCas) {
  81. if (!$this->_x5c) {
  82. return false;
  83. }
  84. $chainC = $this->_createX5cChainFile();
  85. if ($chainC) {
  86. $rootCas[] = $chainC;
  87. }
  88. $v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
  89. if ($v === -1) {
  90. throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
  91. }
  92. return $v;
  93. }
  94. /**
  95. * validate if x5c is present
  96. * @param string $clientDataHash
  97. * @return bool
  98. * @throws WebAuthnException
  99. */
  100. protected function _validateOverX5c($clientDataHash) {
  101. $publicKey = \openssl_pkey_get_public($this->getCertificatePem());
  102. if ($publicKey === false) {
  103. throw new WebAuthnException('invalid public key: ' . \openssl_error_string(), WebAuthnException::INVALID_PUBLIC_KEY);
  104. }
  105. // Concatenate authenticatorData and clientDataHash to form attToBeSigned.
  106. $attToBeSigned = $this->_authenticatorData->getBinary();
  107. $attToBeSigned .= $clientDataHash;
  108. // Validate that certInfo is valid:
  109. // Verify that magic is set to TPM_GENERATED_VALUE.
  110. if ($this->_certInfo->getBytes(0, 4) !== $this->_TPM_GENERATED_VALUE) {
  111. throw new WebAuthnException('tpm magic not TPM_GENERATED_VALUE', WebAuthnException::INVALID_DATA);
  112. }
  113. // Verify that type is set to TPM_ST_ATTEST_CERTIFY.
  114. if ($this->_certInfo->getBytes(4, 2) !== $this->_TPM_ST_ATTEST_CERTIFY) {
  115. throw new WebAuthnException('tpm type not TPM_ST_ATTEST_CERTIFY', WebAuthnException::INVALID_DATA);
  116. }
  117. $offset = 6;
  118. $qualifiedSigner = $this->_tpmReadLengthPrefixed($this->_certInfo, $offset);
  119. $extraData = $this->_tpmReadLengthPrefixed($this->_certInfo, $offset);
  120. $coseAlg = $this->_getCoseAlgorithm($this->_alg);
  121. // Verify that extraData is set to the hash of attToBeSigned using the hash algorithm employed in "alg".
  122. if ($extraData->getBinaryString() !== \hash($coseAlg->hash, $attToBeSigned, true)) {
  123. throw new WebAuthnException('certInfo:extraData not hash of attToBeSigned', WebAuthnException::INVALID_DATA);
  124. }
  125. // Verify the sig is a valid signature over certInfo using the attestation
  126. // public key in aikCert with the algorithm specified in alg.
  127. return \openssl_verify($this->_certInfo->getBinaryString(), $this->_signature, $publicKey, $coseAlg->openssl) === 1;
  128. }
  129. /**
  130. * returns next part of ByteBuffer
  131. * @param ByteBuffer $buffer
  132. * @param int $offset
  133. * @return ByteBuffer
  134. */
  135. protected function _tpmReadLengthPrefixed(ByteBuffer $buffer, &$offset) {
  136. $len = $buffer->getUint16Val($offset);
  137. $data = $buffer->getBytes($offset + 2, $len);
  138. $offset += (2 + $len);
  139. return new ByteBuffer($data);
  140. }
  141. }