|
27 | 27 | namespace web_eid\web_eid_authtoken_validation_php\validator;
|
28 | 28 |
|
29 | 29 | use GuzzleHttp\Psr7\Uri;
|
| 30 | +use phpseclib3\Crypt\RSA; |
30 | 31 | use web_eid\web_eid_authtoken_validation_php\exceptions\AuthTokenParseException;
|
31 | 32 | use web_eid\web_eid_authtoken_validation_php\exceptions\ChallengeNullOrEmptyException;
|
32 | 33 | use InvalidArgumentException;
|
|
36 | 37 | class AuthTokenSignatureValidator
|
37 | 38 | {
|
38 | 39 |
|
| 40 | + private const ECDSA_ALGORITHMS = ['ES256', 'ES384', 'ES512']; |
| 41 | + |
| 42 | + private const RSASSA_PSS_ALGORITHMS = ['PS256', 'PS384', 'PS512']; |
| 43 | + |
39 | 44 | /** Supported subset of JSON Web Signature algorithms as defined in RFC 7518, sections 3.3, 3.4, 3.5.
|
40 | 45 | * See https://github.com/web-eid/libelectronic-id/blob/main/include/electronic-id/enums.hpp#L176.
|
41 | 46 | */
|
@@ -72,10 +77,17 @@ public function validate(string $algorithm, string $signature, $publicKey, strin
|
72 | 77 | $decodedSignature = base64_decode($signature);
|
73 | 78 |
|
74 | 79 | // Note that in case of ECDSA, some eID cards output raw R||S, so we need to trascode it to DER
|
75 |
| - if (in_array($algorithm, ["ES256", "ES384", "ES512"]) && !AsnUtil::isSignatureInAsn1Format($decodedSignature)) { |
| 80 | + if (in_array($algorithm, self::ECDSA_ALGORITHMS) && !AsnUtil::isSignatureInAsn1Format($decodedSignature)) { |
76 | 81 | $decodedSignature = AsnUtil::transcodeSignatureToDER($decodedSignature);
|
77 | 82 | }
|
78 | 83 |
|
| 84 | + if (in_array($algorithm, self::RSASSA_PSS_ALGORITHMS)) { |
| 85 | + $publicKey = openssl_get_publickey($publicKey->withPadding(RSA::SIGNATURE_PSS)->toString('PSS')); |
| 86 | + if (!$publicKey) { |
| 87 | + throw new AuthTokenParseException(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
79 | 91 | $hashAlgorithm = $this->hashAlgorithmForName($algorithm);
|
80 | 92 |
|
81 | 93 | $originHash = openssl_digest($this->siteOrigin->jsonSerialize(), $hashAlgorithm, true);
|
|
0 commit comments