AndroidKey.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 AndroidKey extends FormatBase {
  7. private $_alg;
  8. private $_signature;
  9. private $_x5c;
  10. public function __construct($AttestionObject, AuthenticatorData $authenticatorData) {
  11. parent::__construct($AttestionObject, $authenticatorData);
  12. // check u2f data
  13. $attStmt = $this->_attestationObject['attStmt'];
  14. if (!\array_key_exists('alg', $attStmt) || $this->_getCoseAlgorithm($attStmt['alg']) === null) {
  15. throw new WebAuthnException('unsupported alg: ' . $attStmt['alg'], WebAuthnException::INVALID_DATA);
  16. }
  17. if (!\array_key_exists('sig', $attStmt) || !\is_object($attStmt['sig']) || !($attStmt['sig'] instanceof ByteBuffer)) {
  18. throw new WebAuthnException('no signature found', WebAuthnException::INVALID_DATA);
  19. }
  20. if (!\array_key_exists('x5c', $attStmt) || !\is_array($attStmt['x5c']) || \count($attStmt['x5c']) < 1) {
  21. throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
  22. }
  23. if (!\is_object($attStmt['x5c'][0]) || !($attStmt['x5c'][0] instanceof ByteBuffer)) {
  24. throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
  25. }
  26. $this->_alg = $attStmt['alg'];
  27. $this->_signature = $attStmt['sig']->getBinaryString();
  28. $this->_x5c = $attStmt['x5c'][0]->getBinaryString();
  29. if (count($attStmt['x5c']) > 1) {
  30. for ($i=1; $i<count($attStmt['x5c']); $i++) {
  31. $this->_x5c_chain[] = $attStmt['x5c'][$i]->getBinaryString();
  32. }
  33. unset ($i);
  34. }
  35. }
  36. /*
  37. * returns the key certificate in PEM format
  38. * @return string
  39. */
  40. public function getCertificatePem() {
  41. return $this->_createCertificatePem($this->_x5c);
  42. }
  43. /**
  44. * @param string $clientDataHash
  45. */
  46. public function validateAttestation($clientDataHash) {
  47. $publicKey = \openssl_pkey_get_public($this->getCertificatePem());
  48. if ($publicKey === false) {
  49. throw new WebAuthnException('invalid public key: ' . \openssl_error_string(), WebAuthnException::INVALID_PUBLIC_KEY);
  50. }
  51. // Verify that sig is a valid signature over the concatenation of authenticatorData and clientDataHash
  52. // using the attestation public key in attestnCert with the algorithm specified in alg.
  53. $dataToVerify = $this->_authenticatorData->getBinary();
  54. $dataToVerify .= $clientDataHash;
  55. $coseAlgorithm = $this->_getCoseAlgorithm($this->_alg);
  56. // check certificate
  57. return \openssl_verify($dataToVerify, $this->_signature, $publicKey, $coseAlgorithm->openssl) === 1;
  58. }
  59. /**
  60. * validates the certificate against root certificates
  61. * @param array $rootCas
  62. * @return boolean
  63. * @throws WebAuthnException
  64. */
  65. public function validateRootCertificate($rootCas) {
  66. $chainC = $this->_createX5cChainFile();
  67. if ($chainC) {
  68. $rootCas[] = $chainC;
  69. }
  70. $v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
  71. if ($v === -1) {
  72. throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
  73. }
  74. return $v;
  75. }
  76. }