Apple.php 5.1 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 Apple extends FormatBase {
  7. private $_x5c;
  8. public function __construct($AttestionObject, AuthenticatorData $authenticatorData) {
  9. parent::__construct($AttestionObject, $authenticatorData);
  10. // check packed data
  11. $attStmt = $this->_attestationObject['attStmt'];
  12. // certificate for validation
  13. if (\array_key_exists('x5c', $attStmt) && \is_array($attStmt['x5c']) && \count($attStmt['x5c']) > 0) {
  14. // The attestation certificate attestnCert MUST be the first element in the array
  15. $attestnCert = array_shift($attStmt['x5c']);
  16. if (!($attestnCert instanceof ByteBuffer)) {
  17. throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
  18. }
  19. $this->_x5c = $attestnCert->getBinaryString();
  20. // certificate chain
  21. foreach ($attStmt['x5c'] as $chain) {
  22. if ($chain instanceof ByteBuffer) {
  23. $this->_x5c_chain[] = $chain->getBinaryString();
  24. }
  25. }
  26. } else {
  27. throw new WebAuthnException('invalid Apple attestation statement: missing x5c', WebAuthnException::INVALID_DATA);
  28. }
  29. }
  30. /*
  31. * returns the key certificate in PEM format
  32. * @return string|null
  33. */
  34. public function getCertificatePem() {
  35. return $this->_createCertificatePem($this->_x5c);
  36. }
  37. /**
  38. * @param string $clientDataHash
  39. */
  40. public function validateAttestation($clientDataHash) {
  41. return $this->_validateOverX5c($clientDataHash);
  42. }
  43. /**
  44. * validates the certificate against root certificates
  45. * @param array $rootCas
  46. * @return boolean
  47. * @throws WebAuthnException
  48. */
  49. public function validateRootCertificate($rootCas) {
  50. $chainC = $this->_createX5cChainFile();
  51. if ($chainC) {
  52. $rootCas[] = $chainC;
  53. }
  54. $v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
  55. if ($v === -1) {
  56. throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
  57. }
  58. return $v;
  59. }
  60. /**
  61. * validate if x5c is present
  62. * @param string $clientDataHash
  63. * @return bool
  64. * @throws WebAuthnException
  65. */
  66. protected function _validateOverX5c($clientDataHash) {
  67. $publicKey = \openssl_pkey_get_public($this->getCertificatePem());
  68. if ($publicKey === false) {
  69. throw new WebAuthnException('invalid public key: ' . \openssl_error_string(), WebAuthnException::INVALID_PUBLIC_KEY);
  70. }
  71. // Concatenate authenticatorData and clientDataHash to form nonceToHash.
  72. $nonceToHash = $this->_authenticatorData->getBinary();
  73. $nonceToHash .= $clientDataHash;
  74. // Perform SHA-256 hash of nonceToHash to produce nonce
  75. $nonce = hash('SHA256', $nonceToHash, true);
  76. $credCert = openssl_x509_read($this->getCertificatePem());
  77. if ($credCert === false) {
  78. throw new WebAuthnException('invalid x5c certificate: ' . \openssl_error_string(), WebAuthnException::INVALID_DATA);
  79. }
  80. $keyData = openssl_pkey_get_details(openssl_pkey_get_public($credCert));
  81. $key = is_array($keyData) && array_key_exists('key', $keyData) ? $keyData['key'] : null;
  82. // Verify that nonce equals the value of the extension with OID ( 1.2.840.113635.100.8.2 ) in credCert.
  83. $parsedCredCert = openssl_x509_parse($credCert);
  84. $nonceExtension = isset($parsedCredCert['extensions']['1.2.840.113635.100.8.2']) ? $parsedCredCert['extensions']['1.2.840.113635.100.8.2'] : '';
  85. // nonce padded by ASN.1 string: 30 24 A1 22 04 20
  86. // 30 — type tag indicating sequence
  87. // 24 — 36 byte following
  88. // A1 — Enumerated [1]
  89. // 22 — 34 byte following
  90. // 04 — type tag indicating octet string
  91. // 20 — 32 byte following
  92. $asn1Padding = "\x30\x24\xA1\x22\x04\x20";
  93. if (substr($nonceExtension, 0, strlen($asn1Padding)) === $asn1Padding) {
  94. $nonceExtension = substr($nonceExtension, strlen($asn1Padding));
  95. }
  96. if ($nonceExtension !== $nonce) {
  97. throw new WebAuthnException('nonce doesn\'t equal the value of the extension with OID 1.2.840.113635.100.8.2', WebAuthnException::INVALID_DATA);
  98. }
  99. // Verify that the credential public key equals the Subject Public Key of credCert.
  100. $authKeyData = openssl_pkey_get_details(openssl_pkey_get_public($this->_authenticatorData->getPublicKeyPem()));
  101. $authKey = is_array($authKeyData) && array_key_exists('key', $authKeyData) ? $authKeyData['key'] : null;
  102. if ($key === null || $key !== $authKey) {
  103. throw new WebAuthnException('credential public key doesn\'t equal the Subject Public Key of credCert', WebAuthnException::INVALID_DATA);
  104. }
  105. return true;
  106. }
  107. }