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/U2f.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 U2f extends FormatBase {
10
private $_alg = -7;
11
private $_signature;
12
private $_x5c;
14
public function __construct($AttestionObject, AuthenticatorData $authenticatorData) {
15
parent::__construct($AttestionObject, $authenticatorData);
17
// check u2f data
18
$attStmt = $this->_attestationObject['attStmt'];
20
if (\array_key_exists('alg', $attStmt) && $attStmt['alg'] !== $this->_alg) {
21
throw new WebAuthnException('u2f only accepts algorithm -7 ("ES256"), but got ' . $attStmt['alg'], WebAuthnException::INVALID_DATA);
22
}
24
if (!\array_key_exists('sig', $attStmt) || !\is_object($attStmt['sig']) || !($attStmt['sig'] instanceof ByteBuffer)) {
25
throw new WebAuthnException('no signature found', WebAuthnException::INVALID_DATA);
26
}
28
if (!\array_key_exists('x5c', $attStmt) || !\is_array($attStmt['x5c']) || \count($attStmt['x5c']) !== 1) {
29
throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
30
}
32
if (!\is_object($attStmt['x5c'][0]) || !($attStmt['x5c'][0] instanceof ByteBuffer)) {
33
throw new WebAuthnException('invalid x5c certificate', WebAuthnException::INVALID_DATA);
34
}
36
$this->_signature = $attStmt['sig']->getBinaryString();
37
$this->_x5c = $attStmt['x5c'][0]->getBinaryString();
38
}
41
/*
42
* returns the key certificate in PEM format
43
* @return string
44
*/
45
public function getCertificatePem() {
46
$pem = '-----BEGIN CERTIFICATE-----' . "\n";
47
$pem .= \chunk_split(\base64_encode($this->_x5c), 64, "\n");
48
$pem .= '-----END CERTIFICATE-----' . "\n";
49
return $pem;
50
}
52
/**
53
* @param string $clientDataHash
54
*/
55
public function validateAttestation($clientDataHash) {
56
$publicKey = \openssl_pkey_get_public($this->getCertificatePem());
58
if ($publicKey === false) {
59
throw new WebAuthnException('invalid public key: ' . \openssl_error_string(), WebAuthnException::INVALID_PUBLIC_KEY);
60
}
62
// Let verificationData be the concatenation of (0x00 || rpIdHash || clientDataHash || credentialId || publicKeyU2F)
63
$dataToVerify = "\x00";
64
$dataToVerify .= $this->_authenticatorData->getRpIdHash();
65
$dataToVerify .= $clientDataHash;
66
$dataToVerify .= $this->_authenticatorData->getCredentialId();
67
$dataToVerify .= $this->_authenticatorData->getPublicKeyU2F();
69
$coseAlgorithm = $this->_getCoseAlgorithm($this->_alg);
71
// check certificate
72
return \openssl_verify($dataToVerify, $this->_signature, $publicKey, $coseAlgorithm->openssl) === 1;
73
}
75
/**
76
* validates the certificate against root certificates
77
* @param array $rootCas
78
* @return boolean
79
* @throws WebAuthnException
80
*/
81
public function validateRootCertificate($rootCas) {
82
$chainC = $this->_createX5cChainFile();
83
if ($chainC) {
84
$rootCas[] = $chainC;
85
}
87
$v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
88
if ($v === -1) {
89
throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
90
}
91
return $v;
92
}
93
}