AttestationObject.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace WebAuthn\Attestation;
  3. use WebAuthn\WebAuthnException;
  4. use WebAuthn\CBOR\CborDecoder;
  5. use WebAuthn\Binary\ByteBuffer;
  6. /**
  7. * @author Lukas Buchs
  8. * @license https://github.com/lbuchs/WebAuthn/blob/master/LICENSE MIT
  9. */
  10. class AttestationObject {
  11. private $_authenticatorData;
  12. private $_attestationFormat;
  13. public function __construct($binary , $allowedFormats) {
  14. $enc = CborDecoder::decode($binary);
  15. // validation
  16. if (!\is_array($enc) || !\array_key_exists('fmt', $enc) || !is_string($enc['fmt'])) {
  17. throw new WebAuthnException('invalid attestation format', WebAuthnException::INVALID_DATA);
  18. }
  19. if (!\array_key_exists('attStmt', $enc) || !\is_array($enc['attStmt'])) {
  20. throw new WebAuthnException('invalid attestation format (attStmt not available)', WebAuthnException::INVALID_DATA);
  21. }
  22. if (!\array_key_exists('authData', $enc) || !\is_object($enc['authData']) || !($enc['authData'] instanceof ByteBuffer)) {
  23. throw new WebAuthnException('invalid attestation format (authData not available)', WebAuthnException::INVALID_DATA);
  24. }
  25. $this->_authenticatorData = new AuthenticatorData($enc['authData']->getBinaryString());
  26. // Format ok?
  27. if (!in_array($enc['fmt'], $allowedFormats)) {
  28. throw new WebAuthnException('invalid atttestation format: ' . $enc['fmt'], WebAuthnException::INVALID_DATA);
  29. }
  30. switch ($enc['fmt']) {
  31. case 'android-key': $this->_attestationFormat = new Format\AndroidKey($enc, $this->_authenticatorData); break;
  32. case 'android-safetynet': $this->_attestationFormat = new Format\AndroidSafetyNet($enc, $this->_authenticatorData); break;
  33. case 'fido-u2f': $this->_attestationFormat = new Format\U2f($enc, $this->_authenticatorData); break;
  34. case 'none': $this->_attestationFormat = new Format\None($enc, $this->_authenticatorData); break;
  35. case 'packed': $this->_attestationFormat = new Format\Packed($enc, $this->_authenticatorData); break;
  36. case 'tpm': $this->_attestationFormat = new Format\Tpm($enc, $this->_authenticatorData); break;
  37. default: throw new WebAuthnException('invalid attestation format: ' . $enc['fmt'], WebAuthnException::INVALID_DATA);
  38. }
  39. }
  40. /**
  41. * returns the attestation public key in PEM format
  42. * @return AuthenticatorData
  43. */
  44. public function getAuthenticatorData() {
  45. return $this->_authenticatorData;
  46. }
  47. /**
  48. * returns the certificate chain as PEM
  49. * @return string|null
  50. */
  51. public function getCertificateChain() {
  52. return $this->_attestationFormat->getCertificateChain();
  53. }
  54. /**
  55. * return the certificate issuer as string
  56. * @return string
  57. */
  58. public function getCertificateIssuer() {
  59. $pem = $this->getCertificatePem();
  60. $issuer = '';
  61. if ($pem) {
  62. $certInfo = \openssl_x509_parse($pem);
  63. if (\is_array($certInfo) && \is_array($certInfo['issuer'])) {
  64. if ($certInfo['issuer']['CN']) {
  65. $issuer .= \trim($certInfo['issuer']['CN']);
  66. }
  67. if ($certInfo['issuer']['O'] || $certInfo['issuer']['OU']) {
  68. if ($issuer) {
  69. $issuer .= ' (' . \trim($certInfo['issuer']['O'] . ' ' . $certInfo['issuer']['OU']) . ')';
  70. } else {
  71. $issuer .= \trim($certInfo['issuer']['O'] . ' ' . $certInfo['issuer']['OU']);
  72. }
  73. }
  74. }
  75. }
  76. return $issuer;
  77. }
  78. /**
  79. * return the certificate subject as string
  80. * @return string
  81. */
  82. public function getCertificateSubject() {
  83. $pem = $this->getCertificatePem();
  84. $subject = '';
  85. if ($pem) {
  86. $certInfo = \openssl_x509_parse($pem);
  87. if (\is_array($certInfo) && \is_array($certInfo['subject'])) {
  88. if ($certInfo['subject']['CN']) {
  89. $subject .= \trim($certInfo['subject']['CN']);
  90. }
  91. if ($certInfo['subject']['O'] || $certInfo['subject']['OU']) {
  92. if ($subject) {
  93. $subject .= ' (' . \trim($certInfo['subject']['O'] . ' ' . $certInfo['subject']['OU']) . ')';
  94. } else {
  95. $subject .= \trim($certInfo['subject']['O'] . ' ' . $certInfo['subject']['OU']);
  96. }
  97. }
  98. }
  99. }
  100. return $subject;
  101. }
  102. /**
  103. * returns the key certificate in PEM format
  104. * @return string
  105. */
  106. public function getCertificatePem() {
  107. return $this->_attestationFormat->getCertificatePem();
  108. }
  109. /**
  110. * checks validity of the signature
  111. * @param string $clientDataHash
  112. * @return bool
  113. * @throws WebAuthnException
  114. */
  115. public function validateAttestation($clientDataHash) {
  116. return $this->_attestationFormat->validateAttestation($clientDataHash);
  117. }
  118. /**
  119. * validates the certificate against root certificates
  120. * @param array $rootCas
  121. * @return boolean
  122. * @throws WebAuthnException
  123. */
  124. public function validateRootCertificate($rootCas) {
  125. return $this->_attestationFormat->validateRootCertificate($rootCas);
  126. }
  127. /**
  128. * checks if the RpId-Hash is valid
  129. * @param string$rpIdHash
  130. * @return bool
  131. */
  132. public function validateRpIdHash($rpIdHash) {
  133. return $rpIdHash === $this->_authenticatorData->getRpIdHash();
  134. }
  135. }