public
nobgit
read
NobMail
Based on mailcow: dockerized
Languages
Repository composition by tracked source files.
PHP
49%
JavaScript
35%
HTML
9%
CSS
4%
Shell
2%
Python
1%
Lua
0%
Perl
0%
Ruby
0%
SCSS
0%
Create file
Wiki Documentation
Clone
https://nobgit.com/orgs/nobgit/nobmail.git
ssh://[email protected]:2222/orgs/nobgit/nobmail.git
Trace
data/web/inc/lib/WebAuthn/Attestation/Format/AndroidSafetyNet.php
Trace helps you understand code history line by line. See who changed each line, when it changed, and which commit introduced it.
Author
Date
Commit
Line
Code
1
<?php
4
namespace lbuchs\WebAuthn\Attestation\Format;
5
use lbuchs\WebAuthn\Attestation\AuthenticatorData;
6
use lbuchs\WebAuthn\WebAuthnException;
7
use lbuchs\WebAuthn\Binary\ByteBuffer;
9
class AndroidSafetyNet extends FormatBase {
10
private $_signature;
11
private $_signedValue;
12
private $_x5c;
13
private $_payload;
15
public function __construct($AttestionObject, AuthenticatorData $authenticatorData) {
16
parent::__construct($AttestionObject, $authenticatorData);
18
// check data
19
$attStmt = $this->_attestationObject['attStmt'];
21
if (!\array_key_exists('ver', $attStmt) || !$attStmt['ver']) {
22
throw new WebAuthnException('invalid Android Safety Net Format', WebAuthnException::INVALID_DATA);
23
}
25
if (!\array_key_exists('response', $attStmt) || !($attStmt['response'] instanceof ByteBuffer)) {
26
throw new WebAuthnException('invalid Android Safety Net Format', WebAuthnException::INVALID_DATA);
27
}
29
$response = $attStmt['response']->getBinaryString();
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
}
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);
45
$header = \json_decode($header);
46
$payload = \json_decode($payload);
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
}
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
}
59
// algorithm
60
if (!\in_array($header->alg, array('RS256', 'ES256'))) {
61
throw new WebAuthnException('invalid JWS algorithm ' . $header->alg, WebAuthnException::INVALID_DATA);
62
}
64
$this->_x5c = \base64_decode($header->x5c[0]);
65
$this->_payload = $payload;
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
}
76
/*
77
* returns the key certificate in PEM format
78
* @return string
79
*/
80
public function getCertificatePem() {
81
return $this->_createCertificatePem($this->_x5c);
82
}
84
/**
85
* @param string $clientDataHash
86
*/
87
public function validateAttestation($clientDataHash) {
88
$publicKey = \openssl_pkey_get_public($this->getCertificatePem());
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
}
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
}
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
}
107
// check certificate
108
return \openssl_verify($this->_signedValue, $this->_signature, $publicKey, OPENSSL_ALGO_SHA256) === 1;
109
}
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
}
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
}
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
}