Packed.php 4.6 KB

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