AndroidKey.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace WebAuthn\Attestation\Format;
  3. use WebAuthn\WebAuthnException;
  4. use WebAuthn\Binary\ByteBuffer;
  5. class AndroidKey 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. if (count($attStmt['x5c']) > 1) {
  29. for ($i=1; $i<count($attStmt['x5c']); $i++) {
  30. $this->_x5c_chain[] = $attStmt['x5c'][$i]->getBinaryString();
  31. }
  32. unset ($i);
  33. }
  34. }
  35. /*
  36. * returns the key certificate in PEM format
  37. * @return string
  38. */
  39. public function getCertificatePem() {
  40. return $this->_createCertificatePem($this->_x5c);
  41. }
  42. /**
  43. * @param string $clientDataHash
  44. */
  45. public function validateAttestation($clientDataHash) {
  46. $publicKey = \openssl_pkey_get_public($this->getCertificatePem());
  47. if ($publicKey === false) {
  48. throw new WebAuthnException('invalid public key: ' . \openssl_error_string(), WebAuthnException::INVALID_PUBLIC_KEY);
  49. }
  50. // Verify that sig is a valid signature over the concatenation of authenticatorData and clientDataHash
  51. // using the attestation public key in attestnCert with the algorithm specified in alg.
  52. $dataToVerify = $this->_authenticatorData->getBinary();
  53. $dataToVerify .= $clientDataHash;
  54. $coseAlgorithm = $this->_getCoseAlgorithm($this->_alg);
  55. // check certificate
  56. return \openssl_verify($dataToVerify, $this->_signature, $publicKey, $coseAlgorithm->openssl) === 1;
  57. }
  58. /**
  59. * validates the certificate against root certificates
  60. * @param array $rootCas
  61. * @return boolean
  62. * @throws WebAuthnException
  63. */
  64. public function validateRootCertificate($rootCas) {
  65. $chainC = $this->_createX5cChainFile();
  66. if ($chainC) {
  67. $rootCas[] = $chainC;
  68. }
  69. $v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
  70. if ($v === -1) {
  71. throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
  72. }
  73. return $v;
  74. }
  75. }