| Total Complexity | 44 |
| Total Lines | 362 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 18 | class WebAuthnRegistrationEvent extends WebAuthnAbstractEvent |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Public key algorithm supported. This is -7 - ECDSA with curve P-256 |
||
| 22 | */ |
||
| 23 | const PK_ALGORITHM = -7; |
||
| 24 | const AAGUID_ASSURANCE_LEVEL_NONE = 0; |
||
| 25 | const AAGUID_ASSURANCE_LEVEL_SELF = 1; |
||
| 26 | const AAGUID_ASSURANCE_LEVEL_BASIC = 2; |
||
| 27 | const AAGUID_ASSURANCE_LEVEL_ATTCA = 3; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * the AAGUID of the newly registered authenticator |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | public $AAGUID; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * how sure are we about the AAGUID? |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | public $AAGUIDAssurance; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Initialize the event object. |
||
| 43 | * |
||
| 44 | * Validates and parses the configuration. |
||
| 45 | * |
||
| 46 | * @param string $pubkeyCredType PublicKeyCredential.type |
||
| 47 | * @param string $scope the scope of the event |
||
| 48 | * @param string $challenge the challenge which was used to trigger this event |
||
| 49 | * @param string $idpEntityId the entity ID of our IdP |
||
| 50 | * @param string $attestationData the attestation data CBOR blob |
||
| 51 | * @param string $responseId the response ID |
||
| 52 | * @param string $clientDataJSON the client data JSON string which is present in all types of events |
||
| 53 | * @param bool $debugMode print debugging statements? |
||
| 54 | */ |
||
| 55 | public function __construct( |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * validate the incoming attestation data CBOR blob and return the embedded authData |
||
| 83 | * @param string $attestationData |
||
| 84 | * @param string $clientDataJSON |
||
| 85 | * @return void |
||
| 86 | */ |
||
| 87 | private function validateAttestationData(string $attestationData, string $clientDataJSON) : void |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param array $attestationArray |
||
| 125 | * @return void |
||
| 126 | */ |
||
| 127 | private function validateAttestationFormatNone(array $attestationArray) : void |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param array $attestationArray |
||
| 145 | * @param string $clientDataJSON |
||
| 146 | * @return void |
||
| 147 | */ |
||
| 148 | private function validateAttestationFormatPacked(array $attestationArray, string $clientDataJSON) : void |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * support legacy U2F tokens |
||
| 252 | * |
||
| 253 | * @param array $attestationData the incoming attestation data |
||
| 254 | */ |
||
| 255 | private function validateAttestationFormatFidoU2F($attestationData) { |
||
| 256 | /** |
||
| 257 | * §8.6 Verification Step 1 is a NOOP: if we're here, the attStmt was |
||
| 258 | * already successfully CBOR decoded |
||
| 259 | */ |
||
| 260 | $stmtDecoded = $attestationData['attStmt']; |
||
| 261 | if (!isset($stmtDecoded['x5c'])) { |
||
| 262 | fail("FIDO U2F attestation needs to have the 'x5c' key"); |
||
|
|
|||
| 263 | } |
||
| 264 | /** |
||
| 265 | * §8.6 Verification Step 2: extract attCert and sanity check it |
||
| 266 | */ |
||
| 267 | if (count($stmtDecoded['x5c']) != 1) { |
||
| 268 | fail("FIDO U2F attestation requires 'x5c' to have only exactly one key."); |
||
| 269 | } |
||
| 270 | $attCert = $this->der2pem($stmtDecoded['x5c'][0]); |
||
| 271 | $key = openssl_pkey_get_public($attCert); |
||
| 272 | $keyProps = openssl_pkey_get_details($key); |
||
| 273 | if (!isset($keyProps['ec']['curve_name']) || $keyProps['ec']['curve_name'] != "prime256v1") { |
||
| 274 | $this->fail("FIDO U2F attestation public key is not P-256!"); |
||
| 275 | } |
||
| 276 | /** |
||
| 277 | * §8.6 Verification Step 3 is a NOOP as these properties are already |
||
| 278 | * available as class members: |
||
| 279 | * |
||
| 280 | * $this->rpIdHash; |
||
| 281 | * $this->credentialId; |
||
| 282 | * $this->credential; |
||
| 283 | */ |
||
| 284 | |||
| 285 | /** |
||
| 286 | * §8.6 Verification Step 4: encode the public key in ANSI X9.62 format |
||
| 287 | */ |
||
| 288 | if (isset($this->credential[-2]) && sizeof($this->credential[-2]) == 32 |
||
| 289 | && |
||
| 290 | isset($this->credential[-3]) && sizeof($this->credential[-3]) == 32) { |
||
| 291 | $publicKeyU2F = chr(4).$this->credential[-2].$this->credential[-3]; |
||
| 292 | } else { |
||
| 293 | $this->fail("FIDO U2F attestation: the public key is not as expected."); |
||
| 294 | } |
||
| 295 | /** |
||
| 296 | * §8.6 Verification Step 5: create verificationData |
||
| 297 | */ |
||
| 298 | $verificationData = chr(0).$this->rpIdHash.$this->clientDataHash.$this->credentialId.$publicKeyU2F; |
||
| 299 | /** |
||
| 300 | * §8.6 Verification Step 6: verify signature |
||
| 301 | */ |
||
| 302 | if (openssl_verify($verificationData, $stmtDecoded['sig'],$attCert, OPENSSL_ALGO_SHA256) !== 1) { |
||
| 303 | $this->fail("FIDO U2F Attestation verification failed."); |
||
| 304 | } else { |
||
| 305 | $this->pass("Successfully verified FIDO U2F signature."); |
||
| 306 | } |
||
| 307 | /** |
||
| 308 | * §8.6 Verification Step 7: not performed, this is optional as per spec |
||
| 309 | */ |
||
| 310 | /** |
||
| 311 | * §8.6 Verification Step 8: so we always settle for "Basic" |
||
| 312 | */ |
||
| 313 | $this->AAGUIDAssurance = WebAuthnRegistrationEvent::AAGUID_ASSURANCE_LEVEL_BASIC; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * support Android authenticators (fingerprint etc.) |
||
| 318 | * |
||
| 319 | * @param array $attestationData the incoming attestation data |
||
| 320 | */ |
||
| 321 | private function validateAttestationFormatAndroidSafetyNet($attestationData) { |
||
| 322 | |||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * The registration contains the actual credential. This function parses it. |
||
| 327 | * @param string $attData the attestation data binary blob |
||
| 328 | * @param string $responseId the response ID |
||
| 329 | * @return void |
||
| 330 | */ |
||
| 331 | private function validateAttestedCredentialData(string $attData, string $responseId) : void |
||
| 332 | { |
||
| 333 | $aaguid = substr($attData, 0, 16); |
||
| 334 | $credIdLenBytes = substr($attData, 16, 2); |
||
| 335 | $credIdLen = intval(bin2hex($credIdLenBytes), 16); |
||
| 336 | $credId = substr($attData, 18, $credIdLen); |
||
| 337 | $this->debugBuffer .= "AAGUID (hex) = " . bin2hex($aaguid) . "</br/>"; |
||
| 338 | $this->AAGUID = bin2hex($aaguid); |
||
| 339 | $this->debugBuffer .= "Length Raw = " . bin2hex($credIdLenBytes) . "<br/>"; |
||
| 340 | $this->debugBuffer .= "Credential ID Length (decimal) = " . $credIdLen . "<br/>"; |
||
| 341 | $this->debugBuffer .= "Credential ID (hex) = " . bin2hex($credId) . "<br/>"; |
||
| 342 | if (bin2hex(WebAuthnAbstractEvent::base64url_decode($responseId)) == bin2hex($credId)) { |
||
| 343 | $this->pass("Credential IDs in authenticator response and in attestation data match."); |
||
| 344 | } else { |
||
| 345 | $this->fail("Mismatch of credentialId (" . bin2hex($credId) . ") vs. response ID (" . bin2hex(WebAuthnAbstractEvent::base64url_decode($responseId)) . ")."); |
||
| 346 | } |
||
| 347 | // so far so good. Now extract the actual public key from its COSE |
||
| 348 | // encoding. |
||
| 349 | // finding out the number of bytes to CBOR decode appears non-trivial. |
||
| 350 | // The simple case is if no ED is present as the CBOR data then goes to |
||
| 351 | // the end of the byte sequence. |
||
| 352 | // Since we made sure above that no ED is in the sequence, take the rest |
||
| 353 | // of the sequence in its entirety. |
||
| 354 | $pubKeyCBOR = substr($attData, 18 + $credIdLen); |
||
| 355 | $arrayPK = $this->cborDecode($pubKeyCBOR); |
||
| 356 | $this->debugBuffer .= "pubKey in canonical form: <pre>" . print_r($arrayPK, true) . "</pre>"; |
||
| 357 | /** |
||
| 358 | * STEP 13 of the validation procedure in § 7.1 of the spec: is the algorithm the expected one? |
||
| 359 | */ |
||
| 360 | if ($arrayPK['3'] == WebAuthnRegistrationEvent::PK_ALGORITHM) { // we requested -7, so want to see it here |
||
| 361 | $this->pass("Public Key Algorithm is the expected one (-7, ECDSA)."); |
||
| 362 | } else { |
||
| 363 | $this->fail("Public Key Algorithm mismatch!"); |
||
| 364 | } |
||
| 365 | $this->credentialId = bin2hex($credId); |
||
| 366 | $this->credential = bin2hex($pubKeyCBOR); |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * transform DER formatted certificate to PEM format |
||
| 371 | * |
||
| 372 | * @param string $derData blob of DER data |
||
| 373 | * @return string the PEM representation of the certificate |
||
| 374 | */ |
||
| 375 | private function der2pem(string $derData) : string |
||
| 380 | } |
||
| 381 | |||
| 382 | } |
||
| 383 |