AndroidSafetyNet.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 AndroidSafetyNet extends FormatBase {
  7. private $_signature;
  8. private $_signedValue;
  9. private $_x5c;
  10. private $_payload;
  11. public function __construct($AttestionObject, AuthenticatorData $authenticatorData) {
  12. parent::__construct($AttestionObject, $authenticatorData);
  13. // check data
  14. $attStmt = $this->_attestationObject['attStmt'];
  15. if (!\array_key_exists('ver', $attStmt) || !$attStmt['ver']) {
  16. throw new WebAuthnException('invalid Android Safety Net Format', WebAuthnException::INVALID_DATA);
  17. }
  18. if (!\array_key_exists('response', $attStmt) || !($attStmt['response'] instanceof ByteBuffer)) {
  19. throw new WebAuthnException('invalid Android Safety Net Format', WebAuthnException::INVALID_DATA);
  20. }
  21. $response = $attStmt['response']->getBinaryString();
  22. // Response is a JWS [RFC7515] object in Compact Serialization.
  23. // JWSs have three segments separated by two period ('.') characters
  24. $parts = \explode('.', $response);
  25. unset ($response);
  26. if (\count($parts) !== 3) {
  27. throw new WebAuthnException('invalid JWS data', WebAuthnException::INVALID_DATA);
  28. }
  29. $header = $this->_base64url_decode($parts[0]);
  30. $payload = $this->_base64url_decode($parts[1]);
  31. $this->_signature = $this->_base64url_decode($parts[2]);
  32. $this->_signedValue = $parts[0] . '.' . $parts[1];
  33. unset ($parts);
  34. $header = \json_decode($header);
  35. $payload = \json_decode($payload);
  36. if (!($header instanceof \stdClass)) {
  37. throw new WebAuthnException('invalid JWS header', WebAuthnException::INVALID_DATA);
  38. }
  39. if (!($payload instanceof \stdClass)) {
  40. throw new WebAuthnException('invalid JWS payload', WebAuthnException::INVALID_DATA);
  41. }
  42. if (!$header->x5c || !is_array($header->x5c) || count($header->x5c) === 0) {
  43. throw new WebAuthnException('No X.509 signature in JWS Header', WebAuthnException::INVALID_DATA);
  44. }
  45. // algorithm
  46. if (!\in_array($header->alg, array('RS256', 'ES256'))) {
  47. throw new WebAuthnException('invalid JWS algorithm ' . $header->alg, WebAuthnException::INVALID_DATA);
  48. }
  49. $this->_x5c = \base64_decode($header->x5c[0]);
  50. $this->_payload = $payload;
  51. if (count($header->x5c) > 1) {
  52. for ($i=1; $i<count($header->x5c); $i++) {
  53. $this->_x5c_chain[] = \base64_decode($header->x5c[$i]);
  54. }
  55. unset ($i);
  56. }
  57. }
  58. /*
  59. * returns the key certificate in PEM format
  60. * @return string
  61. */
  62. public function getCertificatePem() {
  63. return $this->_createCertificatePem($this->_x5c);
  64. }
  65. /**
  66. * @param string $clientDataHash
  67. */
  68. public function validateAttestation($clientDataHash) {
  69. $publicKey = \openssl_pkey_get_public($this->getCertificatePem());
  70. // Verify that the nonce in the response is identical to the Base64 encoding
  71. // of the SHA-256 hash of the concatenation of authenticatorData and clientDataHash.
  72. if (!$this->_payload->nonce || $this->_payload->nonce !== \base64_encode(\hash('SHA256', $this->_authenticatorData->getBinary() . $clientDataHash, true))) {
  73. throw new WebAuthnException('invalid nonce in JWS payload', WebAuthnException::INVALID_DATA);
  74. }
  75. // Verify that attestationCert is issued to the hostname "attest.android.com"
  76. $certInfo = \openssl_x509_parse($this->getCertificatePem());
  77. if (!\is_array($certInfo) || !$certInfo['subject'] || $certInfo['subject']['CN'] !== 'attest.android.com') {
  78. throw new WebAuthnException('invalid certificate CN in JWS (' . $certInfo['subject']['CN']. ')', WebAuthnException::INVALID_DATA);
  79. }
  80. // Verify that the ctsProfileMatch attribute in the payload of response is true.
  81. if (!$this->_payload->ctsProfileMatch) {
  82. throw new WebAuthnException('invalid ctsProfileMatch in payload', WebAuthnException::INVALID_DATA);
  83. }
  84. // check certificate
  85. return \openssl_verify($this->_signedValue, $this->_signature, $publicKey, OPENSSL_ALGO_SHA256) === 1;
  86. }
  87. /**
  88. * validates the certificate against root certificates
  89. * @param array $rootCas
  90. * @return boolean
  91. * @throws WebAuthnException
  92. */
  93. public function validateRootCertificate($rootCas) {
  94. $chainC = $this->_createX5cChainFile();
  95. if ($chainC) {
  96. $rootCas[] = $chainC;
  97. }
  98. $v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
  99. if ($v === -1) {
  100. throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
  101. }
  102. return $v;
  103. }
  104. /**
  105. * decode base64 url
  106. * @param string $data
  107. * @return string
  108. */
  109. private function _base64url_decode($data) {
  110. return \base64_decode(\strtr($data, '-_', '+/') . \str_repeat('=', 3 - (3 + \strlen($data)) % 4));
  111. }
  112. }