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