generated from Firehed/php-library-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
apple
verification format (#65)
While this appears to no longer be in active use, Apple devices/software from before Passkeys most likely use this format. Someone was kind enough to provide some test vectors to the W3C repo that included this, so adding preliminary support was possible.
- Loading branch information
Showing
7 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Firehed\WebAuthn\Attestations; | ||
|
||
use Exception; | ||
use Firehed\CBOR\Decoder; | ||
use Firehed\WebAuthn\AuthenticatorData; | ||
use Firehed\WebAuthn\BinaryString; | ||
use Firehed\WebAuthn\Certificate; | ||
use Firehed\WebAuthn\PublicKey\EllipticCurve; | ||
use OpenSSLCertificate; | ||
|
||
class Apple implements AttestationStatementInterface | ||
{ | ||
private const OID = '1.2.840.113635.100.8.2'; | ||
|
||
/** | ||
* @param array{ | ||
* x5c: string[], | ||
* } $data | ||
*/ | ||
public function __construct( | ||
private array $data, | ||
) { | ||
} | ||
|
||
// 8.8 | ||
public function verify(AuthenticatorData $data, BinaryString $clientDataHash): VerificationResult | ||
{ | ||
// ¶2 | ||
$nonceToHash = new BinaryString( | ||
$data->getRaw()->unwrap() . $clientDataHash->unwrap() | ||
); | ||
// ¶3 | ||
$nonce = hash('sha256', $nonceToHash->unwrap(), binary: true); | ||
|
||
// ¶4 - there's a whole lot of validation and data extraction that the | ||
// spec glosses over. | ||
$certs = array_map(self::parseDer(...), $this->data['x5c']); | ||
assert(count($certs) >= 1); | ||
$credCert = array_shift($certs); | ||
$info = openssl_x509_parse($credCert); | ||
if ($info === false) { | ||
throw new Exception('Invalid certificate'); | ||
} | ||
if (!array_key_exists('extensions', $info)) { | ||
throw new Exception('No extensions in credential cert'); | ||
} | ||
$certExt = $info['extensions']; | ||
if (!array_key_exists(self::OID, $certExt)) { | ||
throw new Exception('Expected OID not present in cert extensions'); | ||
} | ||
$nonceInCert = $certExt[self::OID]; | ||
// This isn't clear in the spec, but the value arrives ASN.1-encoded. | ||
// Manually verify some length assumptions instead of doing some | ||
// full-on parsing. | ||
if (strlen($nonceInCert) !== 38) { | ||
throw new Exception("Malformed nonce in cert"); | ||
} | ||
// 0x3024 SEQUENCE (constructed) legnth 36 | ||
// 0xA122 Element 1, length 34 | ||
// 0x0420 OCTET STRING length 32 | ||
if (!str_starts_with(needle: "\x30\x24\xA1\x22\x04\x20", haystack: $nonceInCert)) { | ||
throw new Exception("Cert nonce has weird encoding"); | ||
} | ||
// (finally, the actual verification procedure) | ||
$decodedNonceInCert = substr($nonceInCert, 6); | ||
if (!hash_equals($nonce, $decodedNonceInCert)) { | ||
throw new Exception('Nonce mismatch of expected value'); | ||
} | ||
|
||
// ¶5 | ||
$pubKey = openssl_pkey_get_public($credCert); | ||
if ($pubKey === false) { | ||
throw new Exception('Could not read pubkey of certificate'); | ||
} | ||
$pkd = openssl_pkey_get_details($pubKey); | ||
if ($pkd === false) { | ||
throw new Exception('Could not extract public key info'); | ||
} | ||
$credPK = $data->getAttestedCredentialData()->coseKey; | ||
$credPKPem = $credPK->getPublicKey()->getPemFormatted(); | ||
// ¶6 | ||
if (trim($pkd['key']) !== trim($credPKPem)) { | ||
throw new Exception('Credential public key does not match cert subject'); | ||
} | ||
return new VerificationResult( | ||
AttestationType::AnonymizationCA, | ||
// trustPath: rest of x5c | ||
); | ||
} | ||
|
||
private static function parseDer(string $certificate): OpenSSLCertificate | ||
{ | ||
// Convert DER to PEM format | ||
$certificate = "-----BEGIN CERTIFICATE-----\n" | ||
. chunk_split(base64_encode($certificate), 64, "\n") | ||
. "-----END CERTIFICATE-----"; | ||
|
||
// Read and parse the certificate | ||
$certResource = openssl_x509_read($certificate); | ||
if ($certResource === false) { | ||
throw new Exception('Certiticate parsing error'); | ||
} | ||
return $certResource; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"id": "f25BX23OovR-Ti84WI6lQcp_qfc", | ||
"origin": "https://mobilepki.org" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"publicKey": { | ||
"challenge": "1Z4m-O7Lmi762WffOu5jSXSnJfF93HHwWd52BhNhrUQ" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"id": "f25BX23OovR-Ti84WI6lQcp_qfc", | ||
"rawId": "f25BX23OovR-Ti84WI6lQcp_qfc", | ||
"response": { | ||
"clientDataJSON": "eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoiMVo0bS1PN0xtaTc2MldmZk91NWpTWFNuSmZGOTNISHdXZDUyQmhOaHJVUSIsIm9yaWdpbiI6Imh0dHBzOi8vbW9iaWxlcGtpLm9yZyJ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