| Total Complexity | 98 |
| Total Lines | 731 |
| Duplicated Lines | 0 % |
| Changes | 24 | ||
| Bugs | 4 | Features | 0 |
Complex classes like WebAuthnRegistrationEvent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WebAuthnRegistrationEvent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class WebAuthnRegistrationEvent extends WebAuthnAbstractEvent |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Public key algorithm supported. This is -7 - ECDSA with curve P-256, or -275 (RS256) |
||
| 31 | */ |
||
| 32 | public const PK_ALGORITHM_ECDSA = "-7"; |
||
| 33 | public const PK_ALGORITHM_RSA = "-257"; |
||
| 34 | public const PK_ALGORITHM = [self::PK_ALGORITHM_ECDSA, self::PK_ALGORITHM_RSA]; |
||
| 35 | public const AAGUID_ASSURANCE_LEVEL_NONE = 'None'; |
||
| 36 | public const AAGUID_ASSURANCE_LEVEL_SELF = 'Self'; |
||
| 37 | public const AAGUID_ASSURANCE_LEVEL_BASIC = 'Basic'; |
||
| 38 | public const AAGUID_ASSURANCE_LEVEL_ATTCA = 'AttCA'; |
||
| 39 | |||
| 40 | // nomenclature from the MDS3 spec |
||
| 41 | public const FIDO_REVOKED = "REVOKED"; |
||
| 42 | public const CERTIFICATION_NOT_REQUIRED = "CERTIFICATION_NOT_REQUIRED"; |
||
| 43 | public const FIDO_CERTIFIED_L1 = "FIDO_CERTIFIED_L1"; |
||
| 44 | public const FIDO_CERTIFIED_L1PLUS = "FIDO_CERTIFIED_L1plus"; |
||
| 45 | public const FIDO_CERTIFIED_L2 = "FIDO_CERTIFIED_L2"; |
||
| 46 | public const FIDO_CERTIFIED_L3 = "FIDO_CERTIFIED_L3"; |
||
| 47 | public const FIDO_CERTIFIED_L3PLUS = "FIDO_CERTIFIED_L3plus"; |
||
| 48 | /** |
||
| 49 | * the AAGUID of the newly registered authenticator |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected string $AAGUID; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * how sure are we about the AAGUID? |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected string $AAGUIDAssurance; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * An array of known hardware tokens |
||
| 62 | * |
||
| 63 | * @var \SimpleSAML\Module\webauthn\WebAuthn\AAGUID |
||
| 64 | */ |
||
| 65 | protected AAGUID $AAGUIDDictionary; |
||
| 66 | protected string $AttFmt; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Initialize the event object. |
||
| 70 | * |
||
| 71 | * Validates and parses the configuration. |
||
| 72 | * |
||
| 73 | * @param string $pubkeyCredType PublicKeyCredential.type |
||
| 74 | * @param string $scope the scope of the event |
||
| 75 | * @param string $challenge the challenge which was used to trigger this event |
||
| 76 | * @param string $attestationData the attestation data CBOR blob |
||
| 77 | * @param string $responseId the response ID |
||
| 78 | * @param string $clientDataJSON the client data JSON string which is present in all types of events |
||
| 79 | * @param bool $debugMode print debugging statements? |
||
| 80 | */ |
||
| 81 | public function __construct( |
||
| 110 | } |
||
| 111 | |||
| 112 | private function verifyAcceptability($acceptabilityPolicy) |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Validate the incoming attestation data CBOR blob and return the embedded authData |
||
| 175 | * @param string $attestationData |
||
| 176 | * @return void |
||
| 177 | */ |
||
| 178 | private function validateAttestationData(string $attestationData): void |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param array $attestationArray |
||
| 223 | * @return void |
||
| 224 | */ |
||
| 225 | private function validateAttestationFormatNone(array $attestationArray): void |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param array $attestationArray |
||
| 243 | */ |
||
| 244 | private function validateAttestationFormatApple(array $attestationArray): void |
||
| 360 | } |
||
| 361 | |||
| 362 | private function commonX5cSignatureChecks(array $attestationArray): void |
||
| 363 | { |
||
| 364 | $stmtDecoded = $attestationArray['attStmt']; |
||
| 365 | /** |
||
| 366 | * §8.2 Step 4 Bullet 1: check algorithm |
||
| 367 | */ |
||
| 368 | if (!in_array($stmtDecoded['alg'], self::PK_ALGORITHM)) { |
||
| 369 | $this->fail("Unexpected algorithm type in packed basic attestation: " . $stmtDecoded['alg'] . "."); |
||
| 370 | } |
||
| 371 | $keyResource = openssl_pkey_get_public($this->der2pem($stmtDecoded['x5c'][0])); |
||
| 372 | if ($keyResource === false) { |
||
| 373 | $this->fail("Unable to construct public key resource from PEM."); |
||
| 374 | } |
||
| 375 | /** |
||
| 376 | * §8.2 Step 2: check x5c attestation |
||
| 377 | */ |
||
| 378 | $sigdata = $attestationArray['authData'] . $this->clientDataHash; |
||
| 379 | /** |
||
| 380 | * §8.2 Step 2 Bullet 1: check signature |
||
| 381 | */ |
||
| 382 | $retCode = openssl_verify($sigdata, $stmtDecoded['sig'], $keyResource, "sha256"); |
||
| 383 | if ( $retCode !== 1) { |
||
| 384 | $this->fail("Packed signature mismatch (return code $retCode, for :authdata:".$attestationArray['authData']." - :clientDataHash:".$this->clientDataHash." - :signature:".$stmtDecoded['sig']."), attestation failed."); |
||
| 385 | } |
||
| 386 | $this->pass("x5c sig check passed."); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param array $attestationArray |
||
| 391 | */ |
||
| 392 | private function validateAttestationFormatPacked(array $attestationArray): void |
||
| 393 | { |
||
| 394 | $stmtDecoded = $attestationArray['attStmt']; |
||
| 395 | $this->debugBuffer .= "AttStmt: " . /** @scrutinizer ignore-type */ print_r($stmtDecoded, true) . "<br/>"; |
||
| 396 | /** |
||
| 397 | * §7.1 Step 16: attestation is either done with x5c or ecdaa. |
||
| 398 | */ |
||
| 399 | if (isset($stmtDecoded['x5c'])) { |
||
| 400 | $this->commonX5cSignatureChecks($attestationArray); |
||
| 401 | $this->validateAttestationFormatPackedX5C($attestationArray); |
||
| 402 | } elseif (isset($stmtDecoded['ecdaa'])) { |
||
| 403 | $this->fail("ecdaa attestation not supported right now."); |
||
| 404 | } else { |
||
| 405 | // if we are still here, we are in the "self" type. |
||
| 406 | // signature checks already done, nothing more to do |
||
| 407 | $this->pass("Self-Attestation verified."); |
||
| 408 | $this->AAGUIDAssurance = self::AAGUID_ASSURANCE_LEVEL_SELF; |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param array $attestationArray |
||
| 414 | * @return void |
||
| 415 | */ |
||
| 416 | private function validateAttestationFormatPackedX5C(array $attestationArray): void |
||
| 417 | { |
||
| 418 | $stmtDecoded = $attestationArray['attStmt']; |
||
| 419 | // still need to perform sanity checks on the attestation certificate |
||
| 420 | /** |
||
| 421 | * §8.2 Step 2 Bullet 2: check certificate properties listed in §8.2.1 |
||
| 422 | */ |
||
| 423 | $certProps = openssl_x509_parse($this->der2pem($stmtDecoded['x5c'][0])); |
||
| 424 | $this->debugBuffer .= "Attestation Certificate:" . /** @scrutinizer ignore-type */ print_r($certProps, true) . "<br/>"; |
||
| 425 | if ( |
||
| 426 | $certProps['version'] !== 2 || /** §8.2.1 Bullet 1 */ |
||
| 427 | $certProps['subject']['OU'] !== "Authenticator Attestation" || /** §8.2.1 Bullet 2 [Subject-OU] */ |
||
| 428 | !isset($certProps['subject']['CN']) || /** §8.2.1 Bullet 2 [Subject-CN] */ |
||
| 429 | !isset($certProps['extensions']['basicConstraints']) || |
||
| 430 | strstr("CA:FALSE", $certProps['extensions']['basicConstraints']) === false /** §8.2.1 Bullet 4 */ |
||
| 431 | ) { |
||
| 432 | $this->fail("Attestation certificate properties are no good."); |
||
| 433 | } |
||
| 434 | |||
| 435 | if ($this->AAGUIDDictionary->hasToken($this->AAGUID)) { |
||
| 436 | $token = $this->AAGUIDDictionary->get($this->AAGUID); |
||
| 437 | /** |
||
| 438 | * Checking the OID is not programmatically possible. Text per spec: |
||
| 439 | * "If the related attetation root certificate is used for multiple |
||
| 440 | * authenticator models, the Extension OID ... MUST be present." |
||
| 441 | * |
||
| 442 | * FIDO MDS3 metadata does not disclose whether the root CAs are |
||
| 443 | * used for multiple models. |
||
| 444 | */ |
||
| 445 | /* if ($token['multi'] === true) { // need to check the OID |
||
| 446 | if ( |
||
| 447 | !isset($certProps['extensions']['1.3.6.1.4.1.45724.1.1.4']) || empty($certProps['extensions']['1.3.6.1.4.1.45724.1.1.4']) |
||
| 448 | ) { // §8.2.1 Bullet 3 |
||
| 449 | $this->fail( |
||
| 450 | "This vendor uses one cert for multiple authenticator model attestations, but lacks the AAGUID OID." |
||
| 451 | ); |
||
| 452 | } |
||
| 453 | /** |
||
| 454 | * §8.2 Step 2 Bullet 3: compare AAGUID values |
||
| 455 | */ |
||
| 456 | /* $AAGUIDFromOid = substr(bin2hex($certProps['extensions']['1.3.6.1.4.1.45724.1.1.4']), 4); |
||
| 457 | $this->debugBuffer .= "AAGUID from OID = $AAGUIDFromOid<br/>"; |
||
| 458 | if (strtolower($AAGUIDFromOid) !== strtolower($this->AAGUID)) { |
||
| 459 | $this->fail("AAGUID mismatch between attestation certificate and attestation statement."); |
||
| 460 | } |
||
| 461 | }*/ |
||
| 462 | // we would need to verify the attestation certificate against a known-good |
||
| 463 | // root CA certificate to get more than basic |
||
| 464 | /* |
||
| 465 | * §7.1 Step 17 is to look at $token['RootPEMs'] |
||
| 466 | */ |
||
| 467 | foreach ($token['metadataStatement']['attestationRootCertificates'] as $oneRoot) { |
||
| 468 | openssl_x509_parse("-----BEGIN CERTIFICATE-----\n$oneRoot\n-----END CERTIFICATE-----", true); |
||
| 469 | } |
||
| 470 | /* |
||
| 471 | * §7.1 Step 18 is skipped, and we unconditionally return "only" Basic. |
||
| 472 | */ |
||
| 473 | $this->AAGUIDAssurance = self::AAGUID_ASSURANCE_LEVEL_BASIC; |
||
| 474 | } else { |
||
| 475 | $this->warn("Unknown authenticator model found: " . $this->AAGUID . "."); |
||
| 476 | // unable to verify all cert properties, so this is not enough for BASIC. |
||
| 477 | // but it's our own fault, we should add the device to our DB. |
||
| 478 | $this->AAGUIDAssurance = self::AAGUID_ASSURANCE_LEVEL_SELF; |
||
| 479 | } |
||
| 480 | $this->pass("x5c attestation passed."); |
||
| 481 | return; |
||
| 482 | } |
||
| 483 | |||
| 484 | // Keymaster 3 - KeyMint ??? |
||
| 485 | private const ORIGINS_3 = [ // https://source.android.com/docs/security/features/keystore/tags#origin |
||
| 486 | 0 => "GENERATED", |
||
| 487 | 1 => "DERIVED", |
||
| 488 | 2 => "IMPORTED", |
||
| 489 | 3 => "UNKNOWN", |
||
| 490 | ]; |
||
| 491 | private const PURPOSE_3 = [ |
||
| 492 | 0 => "ENCRYPT", |
||
| 493 | 1 => "DECRYPT", |
||
| 494 | 2 => "SIGN", |
||
| 495 | 3 => "VERIFY", |
||
| 496 | 4 => "DERIVE_KEY", |
||
| 497 | 5 => "WRAP_KEY", |
||
| 498 | ]; |
||
| 499 | |||
| 500 | private const MIN_SUPPORTED_KEYMASTER_VERSION = 3; |
||
| 501 | |||
| 502 | private function validateAttestationFormatAndroidKey(array $attestationArray): void |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * support legacy U2F tokens |
||
| 579 | * |
||
| 580 | * @param array $attestationData the incoming attestation data |
||
| 581 | * @return void |
||
| 582 | */ |
||
| 583 | private function validateAttestationFormatFidoU2F(array $attestationData): void |
||
| 584 | { |
||
| 585 | /** |
||
| 586 | * §8.6 Verification Step 1 is a NOOP: if we're here, the attStmt was |
||
| 587 | * already successfully CBOR decoded |
||
| 588 | */ |
||
| 589 | $stmtDecoded = $attestationData['attStmt']; |
||
| 590 | if (!isset($stmtDecoded['x5c'])) { |
||
| 591 | $this->fail("FIDO U2F attestation needs to have the 'x5c' key"); |
||
| 592 | } |
||
| 593 | /** |
||
| 594 | * §8.6 Verification Step 2: extract attCert and sanity check it |
||
| 595 | */ |
||
| 596 | if (count($stmtDecoded['x5c']) !== 1) { |
||
| 597 | $this->fail("FIDO U2F attestation requires 'x5c' to have only exactly one key."); |
||
| 598 | } |
||
| 599 | $attCert = $this->der2pem($stmtDecoded['x5c'][0]); |
||
| 600 | $key = openssl_pkey_get_public($attCert); |
||
| 601 | $keyProps = openssl_pkey_get_details($key); |
||
| 602 | if (!isset($keyProps['ec']['curve_name']) || $keyProps['ec']['curve_name'] !== "prime256v1") { |
||
| 603 | $this->fail("FIDO U2F attestation public key is not P-256!"); |
||
| 604 | } |
||
| 605 | /** |
||
| 606 | * §8.6 Verification Step 3 is a NOOP as these properties are already |
||
| 607 | * available as class members: |
||
| 608 | * |
||
| 609 | * $this->rpIdHash; |
||
| 610 | * $this->credentialId; |
||
| 611 | * $this->credential; |
||
| 612 | */ |
||
| 613 | /** |
||
| 614 | * §8.6 Verification Step 4: encode the public key in ANSI X9.62 format |
||
| 615 | */ |
||
| 616 | if ( |
||
| 617 | isset($this->credential[-2]) && |
||
| 618 | strlen($this->credential[-2]) === 32 && |
||
| 619 | isset($this->credential[-3]) && |
||
| 620 | strlen($this->credential[-3]) === 32 |
||
| 621 | ) { |
||
| 622 | $publicKeyU2F = chr(4) . $this->credential[-2] . $this->credential[-3]; |
||
| 623 | } else { |
||
| 624 | $publicKeyU2F = false; |
||
| 625 | $this->fail("FIDO U2F attestation: the public key is not as expected."); |
||
| 626 | } |
||
| 627 | /** |
||
| 628 | * §8.6 Verification Step 5: create verificationData |
||
| 629 | * |
||
| 630 | * @psalm-var string $publicKeyU2F |
||
| 631 | */ |
||
| 632 | $verificationData = chr(0) . $this->rpIdHash . $this->clientDataHash . $this->credentialId . $publicKeyU2F; |
||
| 633 | /** |
||
| 634 | * §8.6 Verification Step 6: verify signature |
||
| 635 | */ |
||
| 636 | if (openssl_verify($verificationData, $stmtDecoded['sig'], $attCert, OPENSSL_ALGO_SHA256) !== 1) { |
||
| 637 | $this->fail("FIDO U2F Attestation verification failed."); |
||
| 638 | } else { |
||
| 639 | $this->pass("Successfully verified FIDO U2F signature."); |
||
| 640 | } |
||
| 641 | /** |
||
| 642 | * §8.6 Verification Step 7: not performed, this is optional as per spec |
||
| 643 | */ |
||
| 644 | /** |
||
| 645 | * §8.6 Verification Step 8: so we always settle for "Basic" |
||
| 646 | */ |
||
| 647 | $this->AAGUIDAssurance = self::AAGUID_ASSURANCE_LEVEL_BASIC; |
||
| 648 | } |
||
| 649 | |||
| 650 | /** |
||
| 651 | * support Android authenticators (fingerprint etc.) |
||
| 652 | * |
||
| 653 | * @param array $attestationData the incoming attestation data |
||
| 654 | * @return void |
||
| 655 | */ |
||
| 656 | private function validateAttestationFormatAndroidSafetyNet(array $attestationData): void |
||
| 661 | } |
||
| 662 | |||
| 663 | /** |
||
| 664 | * The registration contains the actual credential. This function parses it. |
||
| 665 | * @param string $attData the attestation data binary blob |
||
| 666 | * @param string $responseId the response ID |
||
| 667 | * @return void |
||
| 668 | */ |
||
| 669 | private function validateAttestedCredentialData(string $attData, string $responseId): void |
||
| 670 | { |
||
| 671 | $aaguid = substr($attData, 0, 16); |
||
| 672 | $credIdLenBytes = substr($attData, 16, 2); |
||
| 673 | $credIdLen = intval(bin2hex($credIdLenBytes), 16); |
||
| 674 | $credId = substr($attData, 18, $credIdLen); |
||
| 675 | $this->debugBuffer .= "AAGUID (hex) = " . bin2hex($aaguid) . "</br/>"; |
||
| 676 | $this->AAGUID = bin2hex($aaguid); |
||
| 677 | $this->debugBuffer .= "Length Raw = " . bin2hex($credIdLenBytes) . "<br/>"; |
||
| 678 | $this->debugBuffer .= "Credential ID Length (decimal) = " . $credIdLen . "<br/>"; |
||
| 679 | $this->debugBuffer .= "Credential ID (hex) = " . bin2hex($credId) . "<br/>"; |
||
| 680 | if (bin2hex(WebAuthnAbstractEvent::base64urlDecode($responseId)) === bin2hex($credId)) { |
||
| 681 | $this->pass("Credential IDs in authenticator response and in attestation data match."); |
||
| 682 | } else { |
||
| 683 | $this->fail( |
||
| 684 | "Mismatch of credentialId (" . bin2hex($credId) . ") vs. response ID (" . |
||
| 685 | bin2hex(WebAuthnAbstractEvent::base64urlDecode($responseId)) . ")." |
||
| 686 | ); |
||
| 687 | } |
||
| 688 | // so far so good. Now extract the actual public key from its COSE |
||
| 689 | // encoding. |
||
| 690 | // finding out the number of bytes to CBOR decode appears non-trivial. |
||
| 691 | // The simple case is if no ED is present as the CBOR data then goes to |
||
| 692 | // the end of the byte sequence. |
||
| 693 | // since we don't know the algoritm yet, we don't know how many bytes |
||
| 694 | // of credential CBOR follow. Let's read to the end; the CBOR decoder |
||
| 695 | // silently ignores trailing extensions (if any) |
||
| 696 | $pubKeyCBOR = substr($attData, 18 + $credIdLen); |
||
| 697 | $arrayPK = $this->cborDecode($pubKeyCBOR); |
||
| 698 | $this->debugBuffer .= "pubKey in canonical form: <pre>" . /** @scrutinizer ignore-type */ print_r($arrayPK, true) . "</pre>"; |
||
| 699 | /** |
||
| 700 | * STEP 13 of the validation procedure in § 7.1 of the spec: is the algorithm the expected one? |
||
| 701 | */ |
||
| 702 | if (in_array($arrayPK['3'], self::PK_ALGORITHM)) { // we requested -7 or -257, so want to see it here |
||
| 703 | $this->algo = (int)$arrayPK['3']; |
||
| 704 | $this->pass("Public Key Algorithm is expected (" . implode(' or ', WebAuthnRegistrationEvent::PK_ALGORITHM) . ")."); |
||
| 705 | } else { |
||
| 706 | $this->fail("Public Key Algorithm mismatch!"); |
||
| 707 | } |
||
| 708 | $this->credentialId = bin2hex($credId); |
||
| 709 | $this->credential = bin2hex($pubKeyCBOR); |
||
| 710 | |||
| 711 | // now that we know credential and its length, we can CBOR-decode the |
||
| 712 | // trailing extensions |
||
| 713 | switch ($this->algo) { |
||
| 714 | case self::PK_ALGORITHM_ECDSA: |
||
| 715 | $credentialLength = 77; |
||
| 716 | break; |
||
| 717 | case self::PK_ALGORITHM_RSA: |
||
| 718 | $credentialLength = 272; |
||
| 719 | break; |
||
| 720 | default: |
||
| 721 | $this->fail("No credential length information for $this->algo"); |
||
| 722 | // be sure to end execution even if the Exception is caught |
||
| 723 | exit(1); |
||
| 724 | } |
||
| 725 | $extensions = substr($attData, 18 + $credIdLen + $credentialLength); |
||
| 726 | if (strlen($extensions) !== 0) { |
||
| 727 | $this->pass("Found the following extensions (" . strlen($extensions) . " bytes) during registration ceremony: "); |
||
| 728 | } |
||
| 729 | } |
||
| 730 | |||
| 731 | /** |
||
| 732 | * transform DER formatted certificate to PEM format |
||
| 733 | * |
||
| 734 | * @param string $derData blob of DER data |
||
| 735 | * @return string the PEM representation of the certificate |
||
| 736 | */ |
||
| 737 | private function der2pem(string $derData): string |
||
| 738 | { |
||
| 739 | $pem = chunk_split(base64_encode($derData), 64, "\n"); |
||
| 740 | $pem = "-----BEGIN CERTIFICATE-----\n" . $pem . "-----END CERTIFICATE-----\n"; |
||
| 741 | return $pem; |
||
| 742 | } |
||
| 743 | |||
| 744 | /** |
||
| 745 | * @return string |
||
| 746 | */ |
||
| 747 | public function getAAGUID() |
||
| 748 | { |
||
| 749 | return $this->AAGUID; |
||
| 750 | } |
||
| 751 | |||
| 752 | /** |
||
| 753 | * @return string |
||
| 754 | */ |
||
| 755 | public function getAttestationLevel() |
||
| 758 | } |
||
| 759 | } |
||
| 760 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.