Packed.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 Packed 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 packed 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. $this->_alg = $attStmt['alg'];
  21. $this->_signature = $attStmt['sig']->getBinaryString();
  22. // certificate for validation
  23. if (\array_key_exists('x5c', $attStmt) && \is_array($attStmt['x5c']) && \count($attStmt['x5c']) > 0) {
  24. // The attestation certificate attestnCert MUST be the first element in the array
  25. $attestnCert = array_shift($attStmt['x5c']);
  26. if (!($attestnCert instanceof ByteBuffer)) {
  27. throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
  28. }
  29. $this->_x5c = $attestnCert->getBinaryString();
  30. // certificate chain
  31. foreach ($attStmt['x5c'] as $chain) {
  32. if ($chain instanceof ByteBuffer) {
  33. $this->_x5c_chain[] = $chain->getBinaryString();
  34. }
  35. }
  36. }
  37. }
  38. /*
  39. * returns the key certificate in PEM format
  40. * @return string|null
  41. */
  42. public function getCertificatePem() {
  43. if (!$this->_x5c) {
  44. return null;
  45. }
  46. return $this->_createCertificatePem($this->_x5c);
  47. }
  48. /**
  49. * @param string $clientDataHash
  50. */
  51. public function validateAttestation($clientDataHash) {
  52. if ($this->_x5c) {
  53. return $this->_validateOverX5c($clientDataHash);
  54. } else {
  55. return $this->_validateSelfAttestation($clientDataHash);
  56. }
  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. if (!$this->_x5c) {
  66. return false;
  67. }
  68. $chainC = $this->_createX5cChainFile();
  69. if ($chainC) {
  70. $rootCas[] = $chainC;
  71. }
  72. $v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
  73. if ($v === -1) {
  74. throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
  75. }
  76. return $v;
  77. }
  78. /**
  79. * validate if x5c is present
  80. * @param string $clientDataHash
  81. * @return bool
  82. * @throws WebAuthnException
  83. */
  84. protected function _validateOverX5c($clientDataHash) {
  85. $publicKey = \openssl_pkey_get_public($this->getCertificatePem());
  86. if ($publicKey === false) {
  87. throw new WebAuthnException('invalid public key: ' . \openssl_error_string(), WebAuthnException::INVALID_PUBLIC_KEY);
  88. }
  89. // Verify that sig is a valid signature over the concatenation of authenticatorData and clientDataHash
  90. // using the attestation public key in attestnCert with the algorithm specified in alg.
  91. $dataToVerify = $this->_authenticatorData->getBinary();
  92. $dataToVerify .= $clientDataHash;
  93. $coseAlgorithm = $this->_getCoseAlgorithm($this->_alg);
  94. // check certificate
  95. return \openssl_verify($dataToVerify, $this->_signature, $publicKey, $coseAlgorithm->openssl) === 1;
  96. }
  97. /**
  98. * validate if self attestation is in use
  99. * @param string $clientDataHash
  100. * @return bool
  101. */
  102. protected function _validateSelfAttestation($clientDataHash) {
  103. // Verify that sig is a valid signature over the concatenation of authenticatorData and clientDataHash
  104. // using the credential public key with alg.
  105. $dataToVerify = $this->_authenticatorData->getBinary();
  106. $dataToVerify .= $clientDataHash;
  107. $publicKey = $this->_authenticatorData->getPublicKeyPem();
  108. // check certificate
  109. return \openssl_verify($dataToVerify, $this->_signature, $publicKey, OPENSSL_ALGO_SHA256) === 1;
  110. }
  111. }