| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn; |
| 6 |
|
| 7 |
use CBOR\ByteStringObject; |
| 8 |
use CBOR\MapItem; |
| 9 |
use CBOR\MapObject; |
| 10 |
use CBOR\NegativeIntegerObject; |
| 11 |
use CBOR\UnsignedIntegerObject; |
| 12 |
use Cose\Algorithms; |
| 13 |
use Cose\Key\Ec2Key; |
| 14 |
use function strlen; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
final readonly class U2FPublicKey |
| 20 |
{ |
| 21 |
private const U2F_KEY_PREFIX = "\x04"; |
| 22 |
|
| 23 |
private const U2F_KEY_LENGTH = 65; |
| 24 |
|
| 25 |
private const U2F_KEY_PART_SIZE = 32; |
| 26 |
|
| 27 |
public static function isU2FKey(string $publicKey): bool |
| 28 |
{ |
| 29 |
return $publicKey[0] === self::U2F_KEY_PREFIX && strlen($publicKey) === self::U2F_KEY_LENGTH; |
| 30 |
} |
| 31 |
|
| 32 |
public static function convertToCoseKey(string $publicKey): string |
| 33 |
{ |
| 34 |
return MapObject::create([ |
| 35 |
MapItem::create( |
| 36 |
UnsignedIntegerObject::create(Ec2Key::TYPE), |
| 37 |
UnsignedIntegerObject::create(Ec2Key::TYPE_EC2) |
| 38 |
), |
| 39 |
MapItem::create( |
| 40 |
UnsignedIntegerObject::create(Ec2Key::ALG), |
| 41 |
NegativeIntegerObject::create(Algorithms::COSE_ALGORITHM_ES256) |
| 42 |
), |
| 43 |
MapItem::create( |
| 44 |
NegativeIntegerObject::create(Ec2Key::DATA_CURVE), |
| 45 |
UnsignedIntegerObject::create(Ec2Key::CURVE_P256) |
| 46 |
), |
| 47 |
MapItem::create( |
| 48 |
NegativeIntegerObject::create(Ec2Key::DATA_X), |
| 49 |
ByteStringObject::create(substr($publicKey, 1, self::U2F_KEY_PART_SIZE)) |
| 50 |
), |
| 51 |
MapItem::create( |
| 52 |
NegativeIntegerObject::create(Ec2Key::DATA_Y), |
| 53 |
ByteStringObject::create(substr($publicKey, 1 + self::U2F_KEY_PART_SIZE)) |
| 54 |
), |
| 55 |
])->__toString(); |
| 56 |
} |
| 57 |
} |