U2f.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace WebAuthn\Attestation\Format;
  3. use WebAuthn\WebAuthnException;
  4. use WebAuthn\Binary\ByteBuffer;
  5. class U2f extends FormatBase {
  6. private $_alg;
  7. private $_signature;
  8. private $_x5c;
  9. public function __construct($AttestionObject, \WebAuthn\Attestation\AuthenticatorData $authenticatorData) {
  10. parent::__construct($AttestionObject, $authenticatorData);
  11. // check u2f data
  12. $attStmt = $this->_attestationObject['attStmt'];
  13. if (!\array_key_exists('alg', $attStmt) || $this->_getCoseAlgorithm($attStmt['alg']) === null) {
  14. throw new WebAuthnException('unsupported alg: ' . $attStmt['alg'], WebAuthnException::INVALID_DATA);
  15. }
  16. if (!\array_key_exists('sig', $attStmt) || !\is_object($attStmt['sig']) || !($attStmt['sig'] instanceof ByteBuffer)) {
  17. throw new WebAuthnException('no signature found', WebAuthnException::INVALID_DATA);
  18. }
  19. if (!\array_key_exists('x5c', $attStmt) || !\is_array($attStmt['x5c']) || \count($attStmt['x5c']) !== 1) {
  20. throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
  21. }
  22. if (!\is_object($attStmt['x5c'][0]) || !($attStmt['x5c'][0] instanceof ByteBuffer)) {
  23. throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
  24. }
  25. $this->_alg = $attStmt['alg'];
  26. $this->_signature = $attStmt['sig']->getBinaryString();
  27. $this->_x5c = $attStmt['x5c'][0]->getBinaryString();
  28. }
  29. /*
  30. * returns the key certificate in PEM format
  31. * @return string
  32. */
  33. public function getCertificatePem() {
  34. $pem = '-----BEGIN CERTIFICATE-----' . "\n";
  35. $pem .= \chunk_split(\base64_encode($this->_x5c), 64, "\n");
  36. $pem .= '-----END CERTIFICATE-----' . "\n";
  37. return $pem;
  38. }
  39. /**
  40. * @param string $clientDataHash
  41. */
  42. public function validateAttestation($clientDataHash) {
  43. $publicKey = \openssl_pkey_get_public($this->getCertificatePem());
  44. if ($publicKey === false) {
  45. throw new WebAuthnException('invalid public key: ' . \openssl_error_string(), WebAuthnException::INVALID_PUBLIC_KEY);
  46. }
  47. // Let verificationData be the concatenation of (0x00 || rpIdHash || clientDataHash || credentialId || publicKeyU2F)
  48. $dataToVerify = "\x00";
  49. $dataToVerify .= $this->_authenticatorData->getRpIdHash();
  50. $dataToVerify .= $clientDataHash;
  51. $dataToVerify .= $this->_authenticatorData->getCredentialId();
  52. $dataToVerify .= $this->_authenticatorData->getPublicKeyU2F();
  53. $coseAlgorithm = $this->_getCoseAlgorithm($this->_alg);
  54. // check certificate
  55. return \openssl_verify($dataToVerify, $this->_signature, $publicKey, $coseAlgorithm->openssl) === 1;
  56. }
  57. /**
  58. * validates the certificate against root certificates
  59. * @param array $rootCas
  60. * @return boolean
  61. * @throws WebAuthnException
  62. */
  63. public function validateRootCertificate($rootCas) {
  64. $chainC = $this->_createX5cChainFile();
  65. if ($chainC) {
  66. $rootCas[] = $chainC;
  67. }
  68. $v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
  69. if ($v === -1) {
  70. throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
  71. }
  72. return $v;
  73. }
  74. }