AndroidSafetyNet.php 5.2 KB

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