-
-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The heavy work is offloaded to phpseclib/phpseclib v3, which is added as a dependency.
- Loading branch information
Showing
11 changed files
with
602 additions
and
1 deletion.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,72 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\JWT\Signer; | ||
|
||
use Lcobucci\JWT\Signer; | ||
use phpseclib3\Crypt\PublicKeyLoader; | ||
use phpseclib3\Crypt\RSA; | ||
use phpseclib3\Crypt\RSA\PrivateKey; | ||
use phpseclib3\Crypt\RSA\PublicKey; | ||
use phpseclib3\Exception\NoKeyLoadedException; | ||
|
||
use function assert; | ||
use function is_string; | ||
|
||
abstract class RsaPss implements Signer | ||
{ | ||
private const MINIMUM_KEY_LENGTH = 2048; | ||
|
||
final public function sign(string $payload, Key $key): string | ||
{ | ||
try { | ||
$private = PublicKeyLoader::loadPrivateKey($key->contents(), $key->passphrase()); | ||
} catch (NoKeyLoadedException $e) { | ||
throw new InvalidKeyProvided('It was not possible to parse your key, reason: ' . $e->getMessage()); | ||
Check warning on line 25 in src/Signer/RsaPss.php GitHub Actions / Mutation tests (locked, 8.2, ubuntu-latest)
Check warning on line 25 in src/Signer/RsaPss.php GitHub Actions / Mutation tests (locked, 8.2, ubuntu-latest)
|
||
} | ||
|
||
if (! $private instanceof PrivateKey) { | ||
throw InvalidKeyProvided::incompatibleKeyType('RSA', $private::class); | ||
} | ||
|
||
if ($private->getLength() < self::MINIMUM_KEY_LENGTH) { | ||
throw InvalidKeyProvided::tooShort(self::MINIMUM_KEY_LENGTH, $private->getLength()); | ||
} | ||
|
||
$signature = $private | ||
->withPadding(RSA::SIGNATURE_PSS) | ||
->withHash($this->algorithm()) | ||
->withMGFHash($this->algorithm()) | ||
->sign($payload); | ||
|
||
assert(is_string($signature) && $signature !== ''); | ||
Check warning on line 42 in src/Signer/RsaPss.php GitHub Actions / Mutation tests (locked, 8.2, ubuntu-latest)
|
||
|
||
return $signature; | ||
} | ||
|
||
final public function verify(string $expected, string $payload, Key $key): bool | ||
{ | ||
try { | ||
$public = PublicKeyLoader::loadPublicKey($key->contents()); | ||
} catch (NoKeyLoadedException $e) { | ||
throw new InvalidKeyProvided('It was not possible to parse your key, reason: ' . $e->getMessage()); | ||
Check warning on line 52 in src/Signer/RsaPss.php GitHub Actions / Mutation tests (locked, 8.2, ubuntu-latest)
Check warning on line 52 in src/Signer/RsaPss.php GitHub Actions / Mutation tests (locked, 8.2, ubuntu-latest)
|
||
} | ||
|
||
if (! $public instanceof PublicKey) { | ||
throw InvalidKeyProvided::incompatibleKeyType('RSA', $public::class); | ||
} | ||
|
||
return $public | ||
->withPadding(RSA::SIGNATURE_PSS) | ||
->withHash($this->algorithm()) | ||
->withMGFHash($this->algorithm()) | ||
->verify($payload, $expected); | ||
} | ||
|
||
/** | ||
* Returns which algorithm to be used to create/verify the signature (using phpseclib hash identifiers) | ||
* | ||
* @internal | ||
*/ | ||
abstract public function algorithm(): string; | ||
} |
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,19 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\JWT\Signer\RsaPss; | ||
|
||
use Lcobucci\JWT\Signer\RsaPss; | ||
|
||
final class Sha256 extends RsaPss | ||
{ | ||
public function algorithmId(): string | ||
{ | ||
return 'PS256'; | ||
} | ||
|
||
public function algorithm(): string | ||
{ | ||
return 'sha256'; | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\JWT\Signer\RsaPss; | ||
|
||
use Lcobucci\JWT\Signer\RsaPss; | ||
|
||
final class Sha384 extends RsaPss | ||
{ | ||
public function algorithmId(): string | ||
{ | ||
return 'PS384'; | ||
} | ||
|
||
public function algorithm(): string | ||
{ | ||
return 'sha384'; | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\JWT\Signer\RsaPss; | ||
|
||
use Lcobucci\JWT\Signer\RsaPss; | ||
|
||
final class Sha512 extends RsaPss | ||
{ | ||
public function algorithmId(): string | ||
{ | ||
return 'PS512'; | ||
} | ||
|
||
public function algorithm(): string | ||
{ | ||
return 'sha512'; | ||
} | ||
} |
Oops, something went wrong.