| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\AttestationStatement; |
| 6 |
|
| 7 |
use function array_key_exists; |
| 8 |
use CBOR\Decoder; |
| 9 |
use CBOR\MapObject; |
| 10 |
use Cose\Key\Ec2Key; |
| 11 |
use function count; |
| 12 |
use function is_array; |
| 13 |
use const OPENSSL_ALGO_SHA256; |
| 14 |
use function openssl_pkey_get_public; |
| 15 |
use function openssl_verify; |
| 16 |
use OpenSSLAsymmetricKey; |
| 17 |
use Psr\EventDispatcher\EventDispatcherInterface; |
| 18 |
use function sprintf; |
| 19 |
use Throwable; |
| 20 |
use Webauthn\AuthenticatorData; |
| 21 |
use Webauthn\Event\AttestationStatementLoaded; |
| 22 |
use Webauthn\Event\CanDispatchEvents; |
| 23 |
use Webauthn\Event\NullEventDispatcher; |
| 24 |
use Webauthn\Exception\AttestationStatementLoadingException; |
| 25 |
use Webauthn\Exception\AttestationStatementVerificationException; |
| 26 |
use Webauthn\Exception\InvalidAttestationStatementException; |
| 27 |
use Webauthn\MetadataService\CertificateChain\CertificateToolbox; |
| 28 |
use Webauthn\StringStream; |
| 29 |
use Webauthn\TrustPath\CertificateTrustPath; |
| 30 |
|
| 31 |
final class FidoU2FAttestationStatementSupport implements AttestationStatementSupport, CanDispatchEvents |
| 32 |
{ |
| 33 |
private readonly Decoder $decoder; |
| 34 |
|
| 35 |
private EventDispatcherInterface $dispatcher; |
| 36 |
|
| 37 |
public function __construct() |
| 38 |
{ |
| 39 |
$this->decoder = Decoder::create(); |
| 40 |
$this->dispatcher = new NullEventDispatcher(); |
| 41 |
} |
| 42 |
|
| 43 |
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void |
| 44 |
{ |
| 45 |
$this->dispatcher = $eventDispatcher; |
| 46 |
} |
| 47 |
|
| 48 |
public static function create(): self |
| 49 |
{ |
| 50 |
return new self(); |
| 51 |
} |
| 52 |
|
| 53 |
public function name(): string |
| 54 |
{ |
| 55 |
return 'fido-u2f'; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function load(array $attestation): AttestationStatement |
| 62 |
{ |
| 63 |
array_key_exists('attStmt', $attestation) || throw AttestationStatementLoadingException::create( |
| 64 |
$attestation, |
| 65 |
'Invalid attestation object' |
| 66 |
); |
| 67 |
|
| 68 |
$attStmt = $attestation['attStmt']; |
| 69 |
foreach (['sig', 'x5c'] as $key) { |
| 70 |
array_key_exists($key, $attStmt) || throw AttestationStatementLoadingException::create( |
| 71 |
$attestation, |
| 72 |
sprintf('The attestation statement value "%s" is missing.', $key) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
$certificates = $attStmt['x5c']; |
| 77 |
is_array($certificates) || throw AttestationStatementLoadingException::create( |
| 78 |
$attestation, |
| 79 |
'The attestation statement value "x5c" must be a list with one certificate.' |
| 80 |
); |
| 81 |
count($certificates) === 1 || throw AttestationStatementLoadingException::create( |
| 82 |
$attestation, |
| 83 |
'The attestation statement value "x5c" must be a list with one certificate.' |
| 84 |
); |
| 85 |
|
| 86 |
reset($certificates); |
| 87 |
$certificates = CertificateToolbox::convertAllDERToPEM($certificates); |
| 88 |
$this->checkCertificate($certificates[0]); |
| 89 |
|
| 90 |
|
| 91 |
$fmt = $attestation['fmt']; |
| 92 |
$attestationStatement = AttestationStatement::createBasic( |
| 93 |
$fmt, |
| 94 |
$attStmt, |
| 95 |
CertificateTrustPath::create($certificates) |
| 96 |
); |
| 97 |
$this->dispatcher->dispatch(AttestationStatementLoaded::create($attestationStatement)); |
| 98 |
|
| 99 |
return $attestationStatement; |
| 100 |
} |
| 101 |
|
| 102 |
public function isValid( |
| 103 |
string $clientDataJSONHash, |
| 104 |
AttestationStatement $attestationStatement, |
| 105 |
AuthenticatorData $authenticatorData |
| 106 |
): bool { |
| 107 |
$authenticatorData->attestedCredentialData |
| 108 |
?->aaguid |
| 109 |
->__toString() === '00000000-0000-0000-0000-000000000000' || throw InvalidAttestationStatementException::create( |
| 110 |
$attestationStatement, |
| 111 |
'Invalid AAGUID for fido-u2f attestation statement. Shall be "00000000-0000-0000-0000-000000000000"' |
| 112 |
); |
| 113 |
$trustPath = $attestationStatement->trustPath; |
| 114 |
$trustPath instanceof CertificateTrustPath || throw InvalidAttestationStatementException::create( |
| 115 |
$attestationStatement, |
| 116 |
'Invalid trust path' |
| 117 |
); |
| 118 |
$dataToVerify = "\0"; |
| 119 |
$dataToVerify .= $authenticatorData->rpIdHash; |
| 120 |
$dataToVerify .= $clientDataJSONHash; |
| 121 |
$dataToVerify .= $authenticatorData->attestedCredentialData |
| 122 |
->credentialId; |
| 123 |
$dataToVerify .= $this->extractPublicKey($authenticatorData->attestedCredentialData ->credentialPublicKey); |
| 124 |
|
| 125 |
|
| 126 |
$sig = $attestationStatement->get('sig'); |
| 127 |
return openssl_verify($dataToVerify, $sig, $trustPath->certificates[0], OPENSSL_ALGO_SHA256) === 1; |
| 128 |
} |
| 129 |
|
| 130 |
private function extractPublicKey(?string $publicKey): string |
| 131 |
{ |
| 132 |
$publicKey !== null || throw AttestationStatementVerificationException::create( |
| 133 |
'The attested credential data does not contain a valid public key.' |
| 134 |
); |
| 135 |
|
| 136 |
$publicKeyStream = new StringStream($publicKey); |
| 137 |
$coseKey = $this->decoder->decode($publicKeyStream); |
| 138 |
$publicKeyStream->isEOF() || throw AttestationStatementVerificationException::create( |
| 139 |
'Invalid public key. Presence of extra bytes.' |
| 140 |
); |
| 141 |
$publicKeyStream->close(); |
| 142 |
$coseKey instanceof MapObject || throw AttestationStatementVerificationException::create( |
| 143 |
'The attested credential data does not contain a valid public key.' |
| 144 |
); |
| 145 |
|
| 146 |
$coseKey = $coseKey->normalize(); |
| 147 |
$ec2Key = new Ec2Key($coseKey + [ |
| 148 |
Ec2Key::TYPE => 2, |
| 149 |
Ec2Key::DATA_CURVE => Ec2Key::CURVE_P256, |
| 150 |
]); |
| 151 |
|
| 152 |
return "\x04" . $ec2Key->x() . $ec2Key->y(); |
| 153 |
} |
| 154 |
|
| 155 |
private function checkCertificate(string $publicKey): void |
| 156 |
{ |
| 157 |
try { |
| 158 |
$resource = openssl_pkey_get_public($publicKey); |
| 159 |
$resource !== false || throw AttestationStatementVerificationException::create( |
| 160 |
'Invalid certificate or certificate chain' |
| 161 |
); |
| 162 |
|
| 163 |
$details = openssl_pkey_get_details($resource); |
| 164 |
} catch (Throwable $throwable) { |
| 165 |
throw AttestationStatementVerificationException::create( |
| 166 |
'Invalid certificate or certificate chain', |
| 167 |
$throwable |
| 168 |
); |
| 169 |
} |
| 170 |
is_array($details) || throw AttestationStatementVerificationException::create( |
| 171 |
'Invalid certificate or certificate chain' |
| 172 |
); |
| 173 |
array_key_exists('ec', $details) || throw AttestationStatementVerificationException::create( |
| 174 |
'Invalid certificate or certificate chain' |
| 175 |
); |
| 176 |
|
| 177 |
$ec = $details['ec']; |
| 178 |
array_key_exists('curve_name', $ec) || throw AttestationStatementVerificationException::create( |
| 179 |
'Invalid certificate or certificate chain' |
| 180 |
); |
| 181 |
$ec['curve_name'] === 'prime256v1' || throw AttestationStatementVerificationException::create( |
| 182 |
'Invalid certificate or certificate chain' |
| 183 |
); |
| 184 |
array_key_exists('curve_oid', $ec) || throw AttestationStatementVerificationException::create( |
| 185 |
'Invalid certificate or certificate chain' |
| 186 |
); |
| 187 |
$ec['curve_oid'] === '1.2.840.10045.3.1.7' || throw AttestationStatementVerificationException::create( |
| 188 |
'Invalid certificate or certificate chain' |
| 189 |
); |
| 190 |
} |
| 191 |
} |