Skip to content

Commit

Permalink
Fix bad curve
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Jul 26, 2023
1 parent 269f22d commit 2fcf4f4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ parameters:

-
message: "#^Parameter \\#2 \\$secret_key of function sodium_crypto_sign_detached expects non\\-empty\\-string, string given\\.$#"
count: 1
count: 2
path: src/Algorithm/Signature/EdDSA/EdDSA.php

-
Expand Down
3 changes: 2 additions & 1 deletion src/Algorithm/Signature/EdDSA/EdDSA.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ public function sign(string $data, Key $key): string

return match ($key->curve()) {
OkpKey::CURVE_ED25519 => sodium_crypto_sign_detached($data, $secret),
OkpKey::CURVE_NAME_ED25519 => sodium_crypto_sign_detached($data, $secret),
default => throw new InvalidArgumentException('Unsupported curve'),
};
}

public function verify(string $data, Key $key, string $signature): bool
{
$key = $this->handleKey($key);
if ($key->curve() !== OkpKey::CURVE_ED25519) {
if ($key->curve() !== OkpKey::CURVE_ED25519 && $key->curve() !== OkpKey::CURVE_NAME_ED25519) {
throw new InvalidArgumentException('Unsupported curve');
}
try {
Expand Down
5 changes: 5 additions & 0 deletions src/Key/Ec2Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class Ec2Key extends Key
*/
public function __construct(array $data)
{
foreach ([self::DATA_CURVE, self::TYPE] as $key) {
if (is_numeric($data[$key])) {
$data[$key] = (int) $data[$key];
}
}
parent::__construct($data);
if ($data[self::TYPE] !== self::TYPE_EC2 && $data[self::TYPE] !== self::TYPE_NAME_EC2) {
throw new InvalidArgumentException('Invalid EC2 key. The key type does not correspond to an EC2 key');
Expand Down
25 changes: 21 additions & 4 deletions src/Key/OkpKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use InvalidArgumentException;
use function array_key_exists;
use function in_array;
use function is_int;

/**
* @final
Expand All @@ -23,6 +22,14 @@ class OkpKey extends Key

final public const CURVE_ED448 = 7;

final public const CURVE_NAME_X25519 = 'X25519';

final public const CURVE_NAME_X448 = 'X448';

final public const CURVE_NAME_ED25519 = 'Ed25519';

final public const CURVE_NAME_ED448 = 'Ed448';

final public const DATA_CURVE = -1;

final public const DATA_X = -2;
Expand All @@ -36,22 +43,32 @@ class OkpKey extends Key
self::CURVE_ED448,
];

private const SUPPORTED_CURVES_NAME = ['X25519', 'X448,', 'Ed25519', 'Ed448'];
private const SUPPORTED_CURVES_NAME = [
self::CURVE_NAME_X25519,
self::CURVE_NAME_X448,
self::CURVE_NAME_ED25519,
self::CURVE_NAME_ED448,
];

/**
* @param array<int|string, mixed> $data
*/
public function __construct(array $data)
{
foreach ([self::DATA_CURVE, self::TYPE] as $key) {
if (is_numeric($data[$key])) {
$data[$key] = (int) $data[$key];
}
}
parent::__construct($data);
if ($data[self::TYPE] !== self::TYPE_OKP && $data[self::TYPE] !== self::TYPE_NAME_OKP) {
throw new InvalidArgumentException('Invalid OKP key. The key type does not correspond to an OKP key');
}
if (! isset($data[self::DATA_CURVE], $data[self::DATA_X])) {
throw new InvalidArgumentException('Invalid EC2 key. The curve or the "x" coordinate is missing');
}
if (is_int($data[self::DATA_CURVE])) {
if (! in_array($data[self::DATA_CURVE], self::SUPPORTED_CURVES_INT, true)) {
if (is_numeric($data[self::DATA_CURVE])) {
if (! in_array((int) $data[self::DATA_CURVE], self::SUPPORTED_CURVES_INT, true)) {
throw new InvalidArgumentException('The curve is not supported');
}
} elseif (! in_array($data[self::DATA_CURVE], self::SUPPORTED_CURVES_NAME, true)) {
Expand Down
5 changes: 5 additions & 0 deletions src/Key/RsaKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class RsaKey extends Key
*/
public function __construct(array $data)
{
foreach ([self::TYPE] as $key) {
if (is_numeric($data[$key])) {
$data[$key] = (int) $data[$key];
}
}
parent::__construct($data);
if ($data[self::TYPE] !== self::TYPE_RSA && $data[self::TYPE] !== self::TYPE_NAME_RSA) {
throw new InvalidArgumentException('Invalid RSA key. The key type does not correspond to a RSA key');
Expand Down

0 comments on commit 2fcf4f4

Please sign in to comment.