| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Signature\Algorithm; |
| 6 |
|
| 7 |
use InvalidArgumentException; |
| 8 |
use Jose\Component\Core\JWK; |
| 9 |
use Jose\Component\Core\Util\RSAKey; |
| 10 |
use Jose\Component\Signature\Algorithm\Util\RSA as JoseRSA; |
| 11 |
use function in_array; |
| 12 |
|
| 13 |
abstract class RSAPSS implements SignatureAlgorithm |
| 14 |
{ |
| 15 |
public function allowedKeyTypes(): array |
| 16 |
{ |
| 17 |
return ['RSA']; |
| 18 |
} |
| 19 |
|
| 20 |
public function verify(JWK $key, string $input, string $signature): bool |
| 21 |
{ |
| 22 |
$this->checkKey($key); |
| 23 |
$pub = RSAKey::createFromJWK($key->toPublic()); |
| 24 |
|
| 25 |
return JoseRSA::verify($pub, $input, $signature, $this->getAlgorithm(), JoseRSA::SIGNATURE_PSS); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public function sign(JWK $key, string $input): string |
| 32 |
{ |
| 33 |
$this->checkKey($key); |
| 34 |
if (! $key->has('d')) { |
| 35 |
throw new InvalidArgumentException('The key is not a private key.'); |
| 36 |
} |
| 37 |
|
| 38 |
$priv = RSAKey::createFromJWK($key); |
| 39 |
|
| 40 |
return JoseRSA::sign($priv, $input, $this->getAlgorithm(), JoseRSA::SIGNATURE_PSS); |
| 41 |
} |
| 42 |
|
| 43 |
abstract protected function getAlgorithm(): string; |
| 44 |
|
| 45 |
private function checkKey(JWK $key): void |
| 46 |
{ |
| 47 |
if (! in_array($key->get('kty'), $this->allowedKeyTypes(), true)) { |
| 48 |
throw new InvalidArgumentException('Wrong key type.'); |
| 49 |
} |
| 50 |
foreach (['n', 'e'] as $k) { |
| 51 |
if (! $key->has($k)) { |
| 52 |
throw new InvalidArgumentException(sprintf('The key parameter "%s" is missing.', $k)); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |