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