U2f.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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;
  7. private $_signature;
  8. private $_x5c;
  9. public function __construct($AttestionObject, AuthenticatorData $authenticatorData) {
  10. parent::__construct($AttestionObject, $authenticatorData);
  11. // check u2f data
  12. $attStmt = $this->_attestationObject['attStmt'];
  13. if (\array_key_exists('alg', $attStmt) && $attStmt['alg'] !== $this->_alg) {
  14. throw new WebAuthnException('u2f only accepts algorithm -7 ("ES256"), but got ' . $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->_signature = $attStmt['sig']->getBinaryString();
  26. $this->_x5c = $attStmt['x5c'][0]->getBinaryString();
  27. }
  28. /*
  29. * returns the key certificate in PEM format
  30. * @return string
  31. */
  32. public function getCertificatePem() {
  33. $pem = '-----BEGIN CERTIFICATE-----' . "\n";
  34. $pem .= \chunk_split(\base64_encode($this->_x5c), 64, "\n");
  35. $pem .= '-----END CERTIFICATE-----' . "\n";
  36. return $pem;
  37. }
  38. /**
  39. * @param string $clientDataHash
  40. */
  41. public function validateAttestation($clientDataHash) {
  42. $publicKey = \openssl_pkey_get_public($this->getCertificatePem());
  43. if ($publicKey === false) {
  44. throw new WebAuthnException('invalid public key: ' . \openssl_error_string(), WebAuthnException::INVALID_PUBLIC_KEY);
  45. }
  46. // Let verificationData be the concatenation of (0x00 || rpIdHash || clientDataHash || credentialId || publicKeyU2F)
  47. $dataToVerify = "\x00";
  48. $dataToVerify .= $this->_authenticatorData->getRpIdHash();
  49. $dataToVerify .= $clientDataHash;
  50. $dataToVerify .= $this->_authenticatorData->getCredentialId();
  51. $dataToVerify .= $this->_authenticatorData->getPublicKeyU2F();
  52. $coseAlgorithm = $this->_getCoseAlgorithm($this->_alg);
  53. // check certificate
  54. return \openssl_verify($dataToVerify, $this->_signature, $publicKey, $coseAlgorithm->openssl) === 1;
  55. }
  56. /**
  57. * validates the certificate against root certificates
  58. * @param array $rootCas
  59. * @return boolean
  60. * @throws WebAuthnException
  61. */
  62. public function validateRootCertificate($rootCas) {
  63. $chainC = $this->_createX5cChainFile();
  64. if ($chainC) {
  65. $rootCas[] = $chainC;
  66. }
  67. $v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
  68. if ($v === -1) {
  69. throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
  70. }
  71. return $v;
  72. }
  73. }