Skip to content

Commit 9af3b99

Browse files
authored
feat: add ES384 support (#324)
1 parent 8d6bfd4 commit 9af3b99

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/JWT.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class JWT
4242
public static $timestamp = null;
4343

4444
public static $supported_algs = array(
45+
'ES384' => array('openssl', 'SHA384'),
4546
'ES256' => array('openssl', 'SHA256'),
4647
'HS256' => array('hash_hmac', 'SHA256'),
4748
'HS384' => array('hash_hmac', 'SHA384'),
@@ -58,7 +59,8 @@ class JWT
5859
* @param string|array|resource $key The key, or map of keys.
5960
* If the algorithm used is asymmetric, this is the public key
6061
* @param array $allowed_algs List of supported verification algorithms
61-
* Supported algorithms are 'ES256', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
62+
* Supported algorithms are 'ES384','ES256', 'HS256', 'HS384',
63+
* 'HS512', 'RS256', 'RS384', and 'RS512'
6264
*
6365
* @return object The JWT's payload as a PHP object
6466
*
@@ -102,8 +104,8 @@ public static function decode($jwt, $key, array $allowed_algs = array())
102104
if (!\in_array($header->alg, $allowed_algs)) {
103105
throw new UnexpectedValueException('Algorithm not allowed');
104106
}
105-
if ($header->alg === 'ES256') {
106-
// OpenSSL expects an ASN.1 DER sequence for ES256 signatures
107+
if ($header->alg === 'ES256' || $header->alg === 'ES384') {
108+
// OpenSSL expects an ASN.1 DER sequence for ES256/ES384 signatures
107109
$sig = self::signatureToDER($sig);
108110
}
109111

@@ -155,7 +157,8 @@ public static function decode($jwt, $key, array $allowed_algs = array())
155157
* @param string $key The secret key.
156158
* If the algorithm used is asymmetric, this is the private key
157159
* @param string $alg The signing algorithm.
158-
* Supported algorithms are 'ES256', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
160+
* Supported algorithms are 'ES384','ES256', 'HS256', 'HS384',
161+
* 'HS512', 'RS256', 'RS384', and 'RS512'
159162
* @param mixed $keyId
160163
* @param array $head An array with header elements to attach
161164
*
@@ -190,7 +193,8 @@ public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $he
190193
* @param string $msg The message to sign
191194
* @param string|resource $key The secret key
192195
* @param string $alg The signing algorithm.
193-
* Supported algorithms are 'ES256', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
196+
* Supported algorithms are 'ES384','ES256', 'HS256', 'HS384',
197+
* 'HS512', 'RS256', 'RS384', and 'RS512'
194198
*
195199
* @return string An encrypted message
196200
*
@@ -214,6 +218,9 @@ public static function sign($msg, $key, $alg = 'HS256')
214218
if ($alg === 'ES256') {
215219
$signature = self::signatureFromDER($signature, 256);
216220
}
221+
if ($alg === 'ES384') {
222+
$signature = self::signatureFromDER($signature, 384);
223+
}
217224
return $signature;
218225
}
219226
}

tests/JWTTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,20 @@ public function testEncodeAndDecodeEcdsaToken()
300300

301301
$this->assertEquals('bar', $decoded->foo);
302302
}
303+
304+
/**
305+
* @runInSeparateProcess
306+
*/
307+
public function testEncodeAndDecodeEcdsa384Token()
308+
{
309+
$privateKey = file_get_contents(__DIR__ . '/ecdsa384-private.pem');
310+
$payload = array('foo' => 'bar');
311+
$encoded = JWT::encode($payload, $privateKey, 'ES384');
312+
313+
// Verify decoding succeeds
314+
$publicKey = file_get_contents(__DIR__ . '/ecdsa384-public.pem');
315+
$decoded = JWT::decode($encoded, $publicKey, array('ES384'));
316+
317+
$this->assertEquals('bar', $decoded->foo);
318+
}
303319
}

tests/ecdsa384-private.pem

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MIGkAgEBBDBQJuwafREZ1494Fm2MTVXuZbWXVAOwIAxGhyLdc3CChzi0FVXZq8e6
3+
65oR0Qq9Jv2gBwYFK4EEACKhZANiAAQWFddzIqZaROR1VtZhhTd20mqknQmYsZ+0
4+
R03NQQUQpJTkyWcuv8WNyd6zO9cCoQEzi94kX907/OEWTjhuH8QtdunT+ef1BpWJ
5+
W1Cm5O+m7b155/Ho99QypfQr74hLg1A=
6+
-----END EC PRIVATE KEY-----

tests/ecdsa384-public.pem

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEFhXXcyKmWkTkdVbWYYU3dtJqpJ0JmLGf
3+
tEdNzUEFEKSU5MlnLr/FjcneszvXAqEBM4veJF/dO/zhFk44bh/ELXbp0/nn9QaV
4+
iVtQpuTvpu29eefx6PfUMqX0K++IS4NQ
5+
-----END PUBLIC KEY-----

0 commit comments

Comments
 (0)