| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\CeremonyStep; |
| 6 |
|
| 7 |
use function count; |
| 8 |
use function in_array; |
| 9 |
use Psr\Log\LoggerInterface; |
| 10 |
use Psr\Log\NullLogger; |
| 11 |
use function sprintf; |
| 12 |
use function trigger_deprecation; |
| 13 |
use Webauthn\AttestationStatement\AttestationStatement; |
| 14 |
use Webauthn\AuthenticatorAssertionResponse; |
| 15 |
use Webauthn\AuthenticatorAttestationResponse; |
| 16 |
use Webauthn\CredentialRecord; |
| 17 |
use Webauthn\Exception\AuthenticatorResponseVerificationException; |
| 18 |
use Webauthn\MetadataService\CanLogData; |
| 19 |
use Webauthn\MetadataService\CertificateChain\CertificateChainValidator; |
| 20 |
use Webauthn\MetadataService\CertificateChain\CertificateToolbox; |
| 21 |
use Webauthn\MetadataService\MetadataStatementRepository; |
| 22 |
use Webauthn\MetadataService\Statement\MetadataStatement; |
| 23 |
use Webauthn\MetadataService\StatusReportRepository; |
| 24 |
use Webauthn\PublicKeyCredentialCreationOptions; |
| 25 |
use Webauthn\PublicKeyCredentialRequestOptions; |
| 26 |
use Webauthn\PublicKeyCredentialSource; |
| 27 |
use Webauthn\TrustPath\CertificateTrustPath; |
| 28 |
|
| 29 |
final class CheckMetadataStatement implements CeremonyStep, CanLogData |
| 30 |
{ |
| 31 |
private LoggerInterface $logger; |
| 32 |
|
| 33 |
private null|MetadataStatementRepository $metadataStatementRepository = null; |
| 34 |
|
| 35 |
private null|StatusReportRepository $statusReportRepository = null; |
| 36 |
|
| 37 |
private null|CertificateChainValidator $certificateChainValidator = null; |
| 38 |
|
| 39 |
public function __construct() |
| 40 |
{ |
| 41 |
$this->logger = new NullLogger(); |
| 42 |
} |
| 43 |
|
| 44 |
public function enableMetadataStatementSupport( |
| 45 |
MetadataStatementRepository $metadataStatementRepository, |
| 46 |
StatusReportRepository $statusReportRepository, |
| 47 |
CertificateChainValidator $certificateChainValidator |
| 48 |
): void { |
| 49 |
$this->metadataStatementRepository = $metadataStatementRepository; |
| 50 |
$this->statusReportRepository = $statusReportRepository; |
| 51 |
$this->certificateChainValidator = $certificateChainValidator; |
| 52 |
} |
| 53 |
|
| 54 |
public function enableCertificateChainValidator(CertificateChainValidator $certificateChainValidator): void |
| 55 |
{ |
| 56 |
$this->certificateChainValidator = $certificateChainValidator; |
| 57 |
} |
| 58 |
|
| 59 |
public function setLogger(LoggerInterface $logger): void |
| 60 |
{ |
| 61 |
$this->logger = $logger; |
| 62 |
} |
| 63 |
|
| 64 |
public function process( |
| 65 |
CredentialRecord $credentialRecord, |
| 66 |
AuthenticatorAssertionResponse|AuthenticatorAttestationResponse $authenticatorResponse, |
| 67 |
PublicKeyCredentialRequestOptions|PublicKeyCredentialCreationOptions $publicKeyCredentialOptions, |
| 68 |
?string $userHandle, |
| 69 |
string $host |
| 70 |
): void { |
| 71 |
if ($credentialRecord instanceof PublicKeyCredentialSource) { |
| 72 |
trigger_deprecation( |
| 73 |
'web-auth/webauthn-lib', |
| 74 |
'5.3', |
| 75 |
'Passing a PublicKeyCredentialSource to "%s::process()" is deprecated, pass a CredentialRecord instead.', |
| 76 |
self::class |
| 77 |
); |
| 78 |
} |
| 79 |
if ( |
| 80 |
! $publicKeyCredentialOptions instanceof PublicKeyCredentialCreationOptions |
| 81 |
|| ! $authenticatorResponse instanceof AuthenticatorAttestationResponse |
| 82 |
) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$attestationStatement = $authenticatorResponse->attestationObject->attStmt; |
| 87 |
$attestedCredentialData = $authenticatorResponse->attestationObject->authData |
| 88 |
->attestedCredentialData; |
| 89 |
$attestedCredentialData !== null || throw AuthenticatorResponseVerificationException::create( |
| 90 |
'No attested credential data found' |
| 91 |
); |
| 92 |
|
| 93 |
if (! $this->isAttestationVerificationRequested($publicKeyCredentialOptions)) { |
| 94 |
$this->logger->debug('No attestation verification requested by RP.'); |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
if ($attestationStatement->type === AttestationStatement::TYPE_NONE) { |
| 99 |
$this->logger->debug('None attestation format. No metadata verification required.'); |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
$aaguid = $attestedCredentialData->aaguid |
| 104 |
->__toString(); |
| 105 |
if ($this->isNullAaguid($aaguid)) { |
| 106 |
$this->logger->debug('Null AAGUID detected. Skipping metadata verification.', [ |
| 107 |
'reason' => 'Privacy placeholder or U2F device', |
| 108 |
]); |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
if ($attestationStatement->type === AttestationStatement::TYPE_SELF) { |
| 113 |
$this->logger->debug('Self attestation detected.', [ |
| 114 |
'aaguid' => $aaguid, |
| 115 |
]); |
| 116 |
$this->processSelfAttestation($aaguid); |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
$this->logger->debug('Processing attestation with full metadata validation.', [ |
| 121 |
'type' => $attestationStatement->type, |
| 122 |
'aaguid' => $aaguid, |
| 123 |
]); |
| 124 |
$this->processWithMetadata($attestationStatement, $aaguid); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
private function isAttestationVerificationRequested( |
| 132 |
PublicKeyCredentialCreationOptions $publicKeyCredentialOptions |
| 133 |
): bool { |
| 134 |
return ! in_array( |
| 135 |
$publicKeyCredentialOptions->attestation, |
| 136 |
[null, PublicKeyCredentialCreationOptions::ATTESTATION_CONVEYANCE_PREFERENCE_NONE], |
| 137 |
true |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
private function isNullAaguid(string $aaguid): bool |
| 154 |
{ |
| 155 |
return $aaguid === '00000000-0000-0000-0000-000000000000'; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private function processSelfAttestation(string $aaguid): void |
| 167 |
{ |
| 168 |
$metadataStatement = $this->metadataStatementRepository?->findOneByAAGUID($aaguid); |
| 169 |
if ($metadataStatement === null) { |
| 170 |
$this->logger->info('No metadata statement found for self attestation. Skipping MDS verification.', [ |
| 171 |
'aaguid' => $aaguid, |
| 172 |
]); |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
$this->logger->debug('Metadata statement found for self attestation. Checking status reports.', [ |
| 177 |
'aaguid' => $aaguid, |
| 178 |
]); |
| 179 |
$this->checkStatusReport($aaguid); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private function processWithMetadata(AttestationStatement $attestationStatement, string $aaguid): void |
| 189 |
{ |
| 190 |
$this->metadataStatementRepository !== null || throw AuthenticatorResponseVerificationException::create( |
| 191 |
'The Metadata Statement Repository is mandatory when requesting attestation objects.' |
| 192 |
); |
| 193 |
$metadataStatement = $this->metadataStatementRepository->findOneByAAGUID($aaguid); |
| 194 |
$metadataStatement !== null || throw AuthenticatorResponseVerificationException::create( |
| 195 |
sprintf('The Metadata Statement for the AAGUID "%s" is missing', $aaguid) |
| 196 |
); |
| 197 |
|
| 198 |
$this->logger->debug('Metadata statement found. Starting validation.', [ |
| 199 |
'aaguid' => $aaguid, |
| 200 |
'attestation_type' => $attestationStatement->type, |
| 201 |
]); |
| 202 |
|
| 203 |
$this->checkStatusReport($aaguid); |
| 204 |
$this->logger->debug('Status report verification completed.'); |
| 205 |
|
| 206 |
$this->checkCertificateChain($attestationStatement, $metadataStatement); |
| 207 |
$this->logger->debug('Certificate chain verification completed.'); |
| 208 |
|
| 209 |
$this->checkAttestationTypeIsAllowed($attestationStatement, $metadataStatement); |
| 210 |
$this->logger->debug('Attestation type verification completed.'); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
private function checkAttestationTypeIsAllowed( |
| 217 |
AttestationStatement $attestationStatement, |
| 218 |
MetadataStatement $metadataStatement |
| 219 |
): void { |
| 220 |
if (count($metadataStatement->attestationTypes) === 0) { |
| 221 |
$this->logger->debug('No attestation types restrictions in metadata statement.'); |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
$type = $this->getAttestationType($attestationStatement); |
| 226 |
$this->logger->debug('Checking attestation type.', [ |
| 227 |
'type' => $type, |
| 228 |
'allowed_types' => $metadataStatement->attestationTypes, |
| 229 |
]); |
| 230 |
|
| 231 |
in_array( |
| 232 |
$type, |
| 233 |
$metadataStatement->attestationTypes, |
| 234 |
true |
| 235 |
) || throw AuthenticatorResponseVerificationException::create( |
| 236 |
sprintf( |
| 237 |
'Invalid attestation statement. The attestation type "%s" is not allowed for this authenticator.', |
| 238 |
$type |
| 239 |
) |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
private function getAttestationType(AttestationStatement $attestationStatement): string |
| 244 |
{ |
| 245 |
return match ($attestationStatement->type) { |
| 246 |
AttestationStatement::TYPE_BASIC => MetadataStatement::ATTESTATION_BASIC_FULL, |
| 247 |
AttestationStatement::TYPE_SELF => MetadataStatement::ATTESTATION_BASIC_SURROGATE, |
| 248 |
AttestationStatement::TYPE_ATTCA => MetadataStatement::ATTESTATION_ATTCA, |
| 249 |
AttestationStatement::TYPE_ANONCA => MetadataStatement::ATTESTATION_ANONCA, |
| 250 |
default => throw AuthenticatorResponseVerificationException::create('Invalid attestation type'), |
| 251 |
}; |
| 252 |
} |
| 253 |
|
| 254 |
private function checkStatusReport(string $aaguid): void |
| 255 |
{ |
| 256 |
$statusReports = $this->statusReportRepository === null ? [] : $this->statusReportRepository->findStatusReportsByAAGUID( |
| 257 |
$aaguid |
| 258 |
); |
| 259 |
if (count($statusReports) === 0) { |
| 260 |
$this->logger->debug('No status reports found for authenticator.', [ |
| 261 |
'aaguid' => $aaguid, |
| 262 |
]); |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
$lastStatusReport = end($statusReports); |
| 267 |
$this->logger->debug('Status report found.', [ |
| 268 |
'aaguid' => $aaguid, |
| 269 |
'status' => $lastStatusReport->status, |
| 270 |
]); |
| 271 |
|
| 272 |
if ($lastStatusReport->isCompromised()) { |
| 273 |
$this->logger->warning('Authenticator is marked as compromised in MDS.', [ |
| 274 |
'aaguid' => $aaguid, |
| 275 |
'status' => $lastStatusReport->status, |
| 276 |
]); |
| 277 |
throw AuthenticatorResponseVerificationException::create( |
| 278 |
'The authenticator is compromised and cannot be used' |
| 279 |
); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
private function checkCertificateChain( |
| 284 |
AttestationStatement $attestationStatement, |
| 285 |
MetadataStatement $metadataStatement |
| 286 |
): void { |
| 287 |
$trustPath = $attestationStatement->trustPath; |
| 288 |
$trustPath instanceof CertificateTrustPath || throw AuthenticatorResponseVerificationException::create( |
| 289 |
'Certificate trust path is required for attestation verification' |
| 290 |
); |
| 291 |
$authenticatorCertificates = $trustPath->certificates; |
| 292 |
$trustedCertificates = CertificateToolbox::fixPEMStructures( |
| 293 |
$metadataStatement->attestationRootCertificates |
| 294 |
); |
| 295 |
$this->certificateChainValidator?->check($authenticatorCertificates, $trustedCertificates); |
| 296 |
} |
| 297 |
} |