| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/* |
| 6 |
* The MIT License (MIT) |
| 7 |
* |
| 8 |
* Copyright (c) 2014-2021 Spomky-Labs |
| 9 |
* |
| 10 |
* This software may be modified and distributed under the terms |
| 11 |
* of the MIT license. See the LICENSE file for details. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace WP_DEFENDER_VENDOR\Cose\Key; |
| 15 |
|
| 16 |
use function array_key_exists; |
| 17 |
use WP_DEFENDER_VENDOR\Assert\Assertion; |
| 18 |
|
| 19 |
class OkpKey extends Key |
| 20 |
{ |
| 21 |
public const CURVE_X25519 = 4; |
| 22 |
public const CURVE_X448 = 5; |
| 23 |
public const CURVE_ED25519 = 6; |
| 24 |
public const CURVE_ED448 = 7; |
| 25 |
|
| 26 |
public const DATA_CURVE = -1; |
| 27 |
public const DATA_X = -2; |
| 28 |
public const DATA_D = -4; |
| 29 |
|
| 30 |
private const SUPPORTED_CURVES = [ |
| 31 |
self::CURVE_X25519, |
| 32 |
self::CURVE_X448, |
| 33 |
self::CURVE_ED25519, |
| 34 |
self::CURVE_ED448, |
| 35 |
]; |
| 36 |
|
| 37 |
public function __construct(array $data) |
| 38 |
{ |
| 39 |
parent::__construct($data); |
| 40 |
Assertion::eq($data[self::TYPE], self::TYPE_OKP, 'Invalid OKP key. The key type does not correspond to an OKP key'); |
| 41 |
Assertion::keyExists($data, self::DATA_CURVE, 'Invalid EC2 key. The curve is missing'); |
| 42 |
Assertion::keyExists($data, self::DATA_X, 'Invalid OKP key. The x coordinate is missing'); |
| 43 |
Assertion::inArray((int) $data[self::DATA_CURVE], self::SUPPORTED_CURVES, 'The curve is not supported'); |
| 44 |
} |
| 45 |
|
| 46 |
public function x(): string |
| 47 |
{ |
| 48 |
return $this->get(self::DATA_X); |
| 49 |
} |
| 50 |
|
| 51 |
public function isPrivate(): bool |
| 52 |
{ |
| 53 |
return array_key_exists(self::DATA_D, $this->getData()); |
| 54 |
} |
| 55 |
|
| 56 |
public function d(): string |
| 57 |
{ |
| 58 |
Assertion::true($this->isPrivate(), 'The key is not private'); |
| 59 |
|
| 60 |
return $this->get(self::DATA_D); |
| 61 |
} |
| 62 |
|
| 63 |
public function curve(): int |
| 64 |
{ |
| 65 |
return (int) $this->get(self::DATA_CURVE); |
| 66 |
} |
| 67 |
} |
| 68 |
|