Tpm.php 6.4 KB

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