| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Signature; |
| 6 |
|
| 7 |
use InvalidArgumentException; |
| 8 |
use Jose\Component\Core\Algorithm; |
| 9 |
use Jose\Component\Core\AlgorithmManager; |
| 10 |
use Jose\Component\Core\JWK; |
| 11 |
use Jose\Component\Core\JWKSet; |
| 12 |
use Jose\Component\Core\Util\Base64UrlSafe; |
| 13 |
use Jose\Component\Core\Util\KeyChecker; |
| 14 |
use Jose\Component\Signature\Algorithm\MacAlgorithm; |
| 15 |
use Jose\Component\Signature\Algorithm\SignatureAlgorithm; |
| 16 |
use Throwable; |
| 17 |
|
| 18 |
class JWSVerifier |
| 19 |
{ |
| 20 |
public function __construct( |
| 21 |
private readonly AlgorithmManager $signatureAlgorithmManager |
| 22 |
) { |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public function getSignatureAlgorithmManager(): AlgorithmManager |
| 29 |
{ |
| 30 |
return $this->signatureAlgorithmManager; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function verifyWithKey(JWS $jws, JWK $jwk, int $signature, ?string $detachedPayload = null): bool |
| 40 |
{ |
| 41 |
$jwkset = new JWKSet([$jwk]); |
| 42 |
|
| 43 |
return $this->verifyWithKeySet($jws, $jwkset, $signature, $detachedPayload); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function verifyWithKeySet( |
| 58 |
JWS $jws, |
| 59 |
JWKSet $jwkset, |
| 60 |
int $signatureIndex, |
| 61 |
?string $detachedPayload = null, |
| 62 |
?JWK &$jwk = null |
| 63 |
): bool { |
| 64 |
if ($jwkset->count() === 0) { |
| 65 |
throw new InvalidArgumentException('There is no key in the key set.'); |
| 66 |
} |
| 67 |
if ($jws->countSignatures() === 0) { |
| 68 |
throw new InvalidArgumentException('The JWS does not contain any signature.'); |
| 69 |
} |
| 70 |
$this->checkPayload($jws, $detachedPayload); |
| 71 |
$signature = $jws->getSignature($signatureIndex); |
| 72 |
|
| 73 |
return $this->verifySignature($jws, $jwkset, $signature, $detachedPayload, $jwk); |
| 74 |
} |
| 75 |
|
| 76 |
private function verifySignature( |
| 77 |
JWS $jws, |
| 78 |
JWKSet $jwkset, |
| 79 |
Signature $signature, |
| 80 |
?string $detachedPayload = null, |
| 81 |
?JWK &$successJwk = null |
| 82 |
): bool { |
| 83 |
$input = $this->getInputToVerify($jws, $signature, $detachedPayload); |
| 84 |
$algorithm = $this->getAlgorithm($signature); |
| 85 |
foreach ($jwkset->all() as $jwk) { |
| 86 |
try { |
| 87 |
KeyChecker::checkKeyUsage($jwk, 'verification'); |
| 88 |
KeyChecker::checkKeyAlgorithm($jwk, $algorithm->name()); |
| 89 |
if ($algorithm->verify($jwk, $input, $signature->getSignature()) === true) { |
| 90 |
$successJwk = $jwk; |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
} catch (Throwable) { |
| 95 |
|
| 96 |
continue; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
private function getInputToVerify(JWS $jws, Signature $signature, ?string $detachedPayload): string |
| 104 |
{ |
| 105 |
$payload = $jws->getPayload(); |
| 106 |
$isPayloadEmpty = $payload === null || $payload === ''; |
| 107 |
$encodedProtectedHeader = $signature->getEncodedProtectedHeader() ?? ''; |
| 108 |
$isPayloadBase64Encoded = ! $signature->hasProtectedHeaderParameter( |
| 109 |
'b64' |
| 110 |
) || $signature->getProtectedHeaderParameter('b64') === true; |
| 111 |
$encodedPayload = $jws->getEncodedPayload(); |
| 112 |
|
| 113 |
if ($isPayloadBase64Encoded && $encodedPayload !== null) { |
| 114 |
return sprintf('%s.%s', $encodedProtectedHeader, $encodedPayload); |
| 115 |
} |
| 116 |
|
| 117 |
$callable = $isPayloadBase64Encoded === true ? static fn (?string $p): string => Base64UrlSafe::encodeUnpadded( |
| 118 |
$p ?? '' |
| 119 |
) |
| 120 |
: static fn (?string $p): string => $p ?? ''; |
| 121 |
|
| 122 |
$payloadToUse = $callable($isPayloadEmpty ? $detachedPayload : $payload); |
| 123 |
|
| 124 |
return sprintf('%s.%s', $encodedProtectedHeader, $payloadToUse); |
| 125 |
} |
| 126 |
|
| 127 |
private function checkPayload(JWS $jws, ?string $detachedPayload = null): void |
| 128 |
{ |
| 129 |
$isPayloadEmpty = $this->isPayloadEmpty($jws->getPayload()); |
| 130 |
if ($detachedPayload !== null && ! $isPayloadEmpty) { |
| 131 |
throw new InvalidArgumentException('A detached payload is set, but the JWS already has a payload.'); |
| 132 |
} |
| 133 |
if ($isPayloadEmpty && $detachedPayload === null) { |
| 134 |
throw new InvalidArgumentException('The JWS has a detached payload, but no payload is provided.'); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
private function getAlgorithm(Signature $signature): Algorithm |
| 142 |
{ |
| 143 |
$protectedHeader = $signature->getProtectedHeader(); |
| 144 |
if (! isset($protectedHeader['alg'])) { |
| 145 |
throw new InvalidArgumentException('No "alg" parameter set in the protected header.'); |
| 146 |
} |
| 147 |
$alg = $protectedHeader['alg']; |
| 148 |
|
| 149 |
$algorithm = $this->signatureAlgorithmManager->get($alg); |
| 150 |
if (! $algorithm instanceof SignatureAlgorithm && ! $algorithm instanceof MacAlgorithm) { |
| 151 |
throw new InvalidArgumentException(sprintf( |
| 152 |
'The algorithm "%s" is not supported or is not a signature or MAC algorithm.', |
| 153 |
$alg |
| 154 |
)); |
| 155 |
} |
| 156 |
|
| 157 |
return $algorithm; |
| 158 |
} |
| 159 |
|
| 160 |
private function isPayloadEmpty(?string $payload): bool |
| 161 |
{ |
| 162 |
return $payload === null || $payload === ''; |
| 163 |
} |
| 164 |
} |