Apple.php 5.1 KB

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