| 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\Algorithm\Manager; |
| 11 |
use Cose\Algorithm\Signature\Signature; |
| 12 |
use Cose\Algorithms; |
| 13 |
use Cose\Key\Key; |
| 14 |
use function count; |
| 15 |
use function is_array; |
| 16 |
use function is_string; |
| 17 |
use function openssl_verify; |
| 18 |
use Psr\EventDispatcher\EventDispatcherInterface; |
| 19 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\OctetString; |
| 20 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 21 |
use SpomkyLabs\Pki\CryptoEncoding\PEM; |
| 22 |
use SpomkyLabs\Pki\X501\ASN1\AttributeType; |
| 23 |
use SpomkyLabs\Pki\X509\Certificate\Certificate; |
| 24 |
use SpomkyLabs\Pki\X509\Certificate\Extension\UnknownExtension; |
| 25 |
use SpomkyLabs\Pki\X509\Certificate\TBSCertificate; |
| 26 |
use Webauthn\AuthenticatorData; |
| 27 |
use Webauthn\Event\AttestationStatementLoaded; |
| 28 |
use Webauthn\Event\CanDispatchEvents; |
| 29 |
use Webauthn\Event\NullEventDispatcher; |
| 30 |
use Webauthn\Exception\AttestationStatementLoadingException; |
| 31 |
use Webauthn\Exception\AttestationStatementVerificationException; |
| 32 |
use Webauthn\Exception\InvalidAttestationStatementException; |
| 33 |
use Webauthn\Exception\InvalidDataException; |
| 34 |
use Webauthn\MetadataService\CertificateChain\CertificateToolbox; |
| 35 |
use Webauthn\StringStream; |
| 36 |
use Webauthn\TrustPath\CertificateTrustPath; |
| 37 |
use Webauthn\TrustPath\EmptyTrustPath; |
| 38 |
use Webauthn\Util\CoseSignatureFixer; |
| 39 |
|
| 40 |
final class PackedAttestationStatementSupport implements AttestationStatementSupport, CanDispatchEvents |
| 41 |
{ |
| 42 |
private const OID_FIDO_GEN_CE_AAGUID = '1.3.6.1.4.1.45724.1.1.4'; |
| 43 |
|
| 44 |
private readonly Decoder $decoder; |
| 45 |
|
| 46 |
private EventDispatcherInterface $dispatcher; |
| 47 |
|
| 48 |
public function __construct( |
| 49 |
private readonly Manager $algorithmManager |
| 50 |
) { |
| 51 |
$this->decoder = Decoder::create(); |
| 52 |
$this->dispatcher = new NullEventDispatcher(); |
| 53 |
} |
| 54 |
|
| 55 |
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void |
| 56 |
{ |
| 57 |
$this->dispatcher = $eventDispatcher; |
| 58 |
} |
| 59 |
|
| 60 |
public static function create(Manager $algorithmManager): self |
| 61 |
{ |
| 62 |
return new self($algorithmManager); |
| 63 |
} |
| 64 |
|
| 65 |
public function name(): string |
| 66 |
{ |
| 67 |
return 'packed'; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function load(array $attestation): AttestationStatement |
| 74 |
{ |
| 75 |
|
| 76 |
$attStmt = $attestation['attStmt']; |
| 77 |
array_key_exists('sig', $attStmt) || throw AttestationStatementLoadingException::create( |
| 78 |
$attestation, |
| 79 |
'The attestation statement value "sig" is missing.' |
| 80 |
); |
| 81 |
array_key_exists('alg', $attStmt) || throw AttestationStatementLoadingException::create( |
| 82 |
$attestation, |
| 83 |
'The attestation statement value "alg" is missing.' |
| 84 |
); |
| 85 |
is_string($attStmt['sig']) || throw AttestationStatementLoadingException::create( |
| 86 |
$attestation, |
| 87 |
'The attestation statement value "sig" is missing.' |
| 88 |
); |
| 89 |
|
| 90 |
return match (true) { |
| 91 |
array_key_exists('x5c', $attStmt) => $this->loadBasicType($attestation), |
| 92 |
default => $this->loadEmptyType($attestation), |
| 93 |
}; |
| 94 |
} |
| 95 |
|
| 96 |
public function isValid( |
| 97 |
string $clientDataJSONHash, |
| 98 |
AttestationStatement $attestationStatement, |
| 99 |
AuthenticatorData $authenticatorData |
| 100 |
): bool { |
| 101 |
$trustPath = $attestationStatement->trustPath; |
| 102 |
|
| 103 |
return match (true) { |
| 104 |
$trustPath instanceof CertificateTrustPath => $this->processWithCertificate( |
| 105 |
$clientDataJSONHash, |
| 106 |
$attestationStatement, |
| 107 |
$authenticatorData, |
| 108 |
$trustPath |
| 109 |
), |
| 110 |
$trustPath instanceof EmptyTrustPath => $this->processWithSelfAttestation( |
| 111 |
$clientDataJSONHash, |
| 112 |
$attestationStatement, |
| 113 |
$authenticatorData |
| 114 |
), |
| 115 |
default => throw InvalidAttestationStatementException::create( |
| 116 |
$attestationStatement, |
| 117 |
'Unsupported attestation statement' |
| 118 |
), |
| 119 |
}; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private function loadBasicType(array $attestation): AttestationStatement |
| 126 |
{ |
| 127 |
|
| 128 |
$attStmt = $attestation['attStmt']; |
| 129 |
|
| 130 |
$certificates = $attStmt['x5c']; |
| 131 |
is_array($certificates) || throw AttestationStatementVerificationException::create( |
| 132 |
'The attestation statement value "x5c" must be a list with at least one certificate.' |
| 133 |
); |
| 134 |
count($certificates) > 0 || throw AttestationStatementVerificationException::create( |
| 135 |
'The attestation statement value "x5c" must be a list with at least one certificate.' |
| 136 |
); |
| 137 |
$certificates = CertificateToolbox::convertAllDERToPEM($certificates); |
| 138 |
|
| 139 |
|
| 140 |
$fmt = $attestation['fmt']; |
| 141 |
$attestationStatement = AttestationStatement::createBasic( |
| 142 |
$fmt, |
| 143 |
$attStmt, |
| 144 |
CertificateTrustPath::create($certificates) |
| 145 |
); |
| 146 |
$this->dispatcher->dispatch(AttestationStatementLoaded::create($attestationStatement)); |
| 147 |
|
| 148 |
return $attestationStatement; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
private function loadEmptyType(array $attestation): AttestationStatement |
| 155 |
{ |
| 156 |
|
| 157 |
$fmt = $attestation['fmt']; |
| 158 |
|
| 159 |
$attStmt = $attestation['attStmt']; |
| 160 |
$attestationStatement = AttestationStatement::createSelf($fmt, $attStmt, EmptyTrustPath::create()); |
| 161 |
$this->dispatcher->dispatch(AttestationStatementLoaded::create($attestationStatement)); |
| 162 |
|
| 163 |
return $attestationStatement; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
private function checkCertificate(string $attestnCert, AuthenticatorData $authenticatorData): void |
| 168 |
{ |
| 169 |
$certificate = Certificate::fromPEM(PEM::fromString($attestnCert)); |
| 170 |
$tbsCertificate = $certificate->tbsCertificate(); |
| 171 |
|
| 172 |
|
| 173 |
$tbsCertificate->version() === TBSCertificate::VERSION_3 || throw AttestationStatementVerificationException::create( |
| 174 |
'Invalid certificate version' |
| 175 |
); |
| 176 |
|
| 177 |
|
| 178 |
$subject = $tbsCertificate->subject(); |
| 179 |
$subject->countOfType( |
| 180 |
AttributeType::OID_COUNTRY_NAME |
| 181 |
) > 0 || throw AttestationStatementVerificationException::create('Certificate Subject-C must be set'); |
| 182 |
$subject->countOfType( |
| 183 |
AttributeType::OID_ORGANIZATION_NAME |
| 184 |
) > 0 || throw AttestationStatementVerificationException::create('Certificate Subject-O must be set'); |
| 185 |
$subject->countOfType( |
| 186 |
AttributeType::OID_ORGANIZATIONAL_UNIT_NAME |
| 187 |
) > 0 || throw AttestationStatementVerificationException::create('Certificate Subject-OU must be set'); |
| 188 |
$subject->countOfType( |
| 189 |
AttributeType::OID_COMMON_NAME |
| 190 |
) > 0 || throw AttestationStatementVerificationException::create('Certificate Subject-CN must be set'); |
| 191 |
$ouValue = $subject->firstValueOf('OU') |
| 192 |
->stringValue(); |
| 193 |
$ouValue === 'Authenticator Attestation' || throw AttestationStatementVerificationException::create( |
| 194 |
'Invalid certificate name. The Subject Organization Unit must be "Authenticator Attestation"' |
| 195 |
); |
| 196 |
|
| 197 |
|
| 198 |
$extensions = $tbsCertificate->extensions(); |
| 199 |
|
| 200 |
|
| 201 |
$extensions->hasBasicConstraints() || throw AttestationStatementVerificationException::create( |
| 202 |
'The Basic Constraints extension must have the CA component set to false' |
| 203 |
); |
| 204 |
! $extensions->basicConstraints() |
| 205 |
->isCA() || throw AttestationStatementVerificationException::create( |
| 206 |
'The Basic Constraints extension must have the CA component set to false' |
| 207 |
); |
| 208 |
|
| 209 |
$attestedCredentialData = $authenticatorData->attestedCredentialData; |
| 210 |
$attestedCredentialData !== null || throw AttestationStatementVerificationException::create( |
| 211 |
'No attested credential available' |
| 212 |
); |
| 213 |
|
| 214 |
|
| 215 |
if ($extensions->has(self::OID_FIDO_GEN_CE_AAGUID)) { |
| 216 |
|
| 217 |
$aaguidExtension = $extensions->get(self::OID_FIDO_GEN_CE_AAGUID); |
| 218 |
! $aaguidExtension->isCritical() || throw AttestationStatementVerificationException::create( |
| 219 |
'Extension ' . self::OID_FIDO_GEN_CE_AAGUID . ' must not be marked as critical' |
| 220 |
); |
| 221 |
|
| 222 |
$aaguidElement = UnspecifiedType::fromDER($aaguidExtension->extensionValue())->asElement(); |
| 223 |
$aaguidElement instanceof OctetString || throw AttestationStatementVerificationException::create( |
| 224 |
'Invalid ' . self::OID_FIDO_GEN_CE_AAGUID . ' extension format' |
| 225 |
); |
| 226 |
$aaguidValue = $aaguidElement->string(); |
| 227 |
hash_equals( |
| 228 |
$attestedCredentialData->aaguid |
| 229 |
->toBinary(), |
| 230 |
$aaguidValue |
| 231 |
) || throw AttestationStatementVerificationException::create( |
| 232 |
'The value of the "aaguid" does not match with the certificate extension ' . self::OID_FIDO_GEN_CE_AAGUID |
| 233 |
); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
private function processWithCertificate( |
| 238 |
string $clientDataJSONHash, |
| 239 |
AttestationStatement $attestationStatement, |
| 240 |
AuthenticatorData $authenticatorData, |
| 241 |
CertificateTrustPath $trustPath |
| 242 |
): bool { |
| 243 |
$certificates = $trustPath->certificates; |
| 244 |
|
| 245 |
|
| 246 |
$this->checkCertificate($certificates[0], $authenticatorData); |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
$algRaw = $attestationStatement->get('alg'); |
| 251 |
$coseAlgorithmIdentifier = (int) $algRaw; |
| 252 |
$opensslAlgorithmIdentifier = Algorithms::getOpensslAlgorithmFor($coseAlgorithmIdentifier); |
| 253 |
|
| 254 |
|
| 255 |
$signedData = $authenticatorData->authData . $clientDataJSONHash; |
| 256 |
|
| 257 |
$sig = $attestationStatement->get('sig'); |
| 258 |
$result = openssl_verify($signedData, $sig, $certificates[0], $opensslAlgorithmIdentifier); |
| 259 |
|
| 260 |
return $result === 1; |
| 261 |
} |
| 262 |
|
| 263 |
private function processWithSelfAttestation( |
| 264 |
string $clientDataJSONHash, |
| 265 |
AttestationStatement $attestationStatement, |
| 266 |
AuthenticatorData $authenticatorData |
| 267 |
): bool { |
| 268 |
$attestedCredentialData = $authenticatorData->attestedCredentialData; |
| 269 |
$attestedCredentialData !== null || throw AttestationStatementVerificationException::create( |
| 270 |
'No attested credential available' |
| 271 |
); |
| 272 |
$credentialPublicKey = $attestedCredentialData->credentialPublicKey; |
| 273 |
$credentialPublicKey !== null || throw AttestationStatementVerificationException::create( |
| 274 |
'No credential public key available' |
| 275 |
); |
| 276 |
$publicKeyStream = new StringStream($credentialPublicKey); |
| 277 |
$publicKey = $this->decoder->decode($publicKeyStream); |
| 278 |
$publicKeyStream->isEOF() || throw AttestationStatementVerificationException::create( |
| 279 |
'Invalid public key. Presence of extra bytes.' |
| 280 |
); |
| 281 |
$publicKeyStream->close(); |
| 282 |
$publicKey instanceof MapObject || throw AttestationStatementVerificationException::create( |
| 283 |
'The attested credential data does not contain a valid public key.' |
| 284 |
); |
| 285 |
$publicKey = $publicKey->normalize(); |
| 286 |
$publicKey = new Key($publicKey); |
| 287 |
|
| 288 |
$algRaw = $attestationStatement->get('alg'); |
| 289 |
$alg = (int) $algRaw; |
| 290 |
$publicKey->alg() === $alg || throw AttestationStatementVerificationException::create( |
| 291 |
'The algorithm of the attestation statement and the key are not identical.' |
| 292 |
); |
| 293 |
|
| 294 |
$dataToVerify = $authenticatorData->authData . $clientDataJSONHash; |
| 295 |
$algorithm = $this->algorithmManager->get($alg); |
| 296 |
if (! $algorithm instanceof Signature) { |
| 297 |
throw InvalidDataException::create($algorithm, 'Invalid algorithm'); |
| 298 |
} |
| 299 |
|
| 300 |
$sigForFix = $attestationStatement->get('sig'); |
| 301 |
$signature = CoseSignatureFixer::fix($sigForFix, $algorithm); |
| 302 |
|
| 303 |
return $algorithm->verify($dataToVerify, $publicKey, $signature); |
| 304 |
} |
| 305 |
} |