-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add Ed25519 support to JWT #343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9a0c210
c4e21e5
cd215ad
bd9e87e
cf58cc3
6664523
f078611
ffdc460
b55ae9b
4c2181b
31b559a
7360262
5118efe
441b75c
3dd4b01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
namespace Firebase\JWT; | ||
|
||
use DomainException; | ||
use Exception; | ||
use InvalidArgumentException; | ||
use UnexpectedValueException; | ||
use DateTime; | ||
|
@@ -50,6 +51,7 @@ class JWT | |
'RS256' => array('openssl', 'SHA256'), | ||
'RS384' => array('openssl', 'SHA384'), | ||
'RS512' => array('openssl', 'SHA512'), | ||
'EdDSA' => array('sodium_crypto', 'EdDSA'), | ||
); | ||
|
||
/** | ||
|
@@ -198,7 +200,7 @@ public static function encode($payload, $key, $alg = 'HS256', $keyId = null, $he | |
* | ||
* @return string An encrypted message | ||
* | ||
* @throws DomainException Unsupported algorithm was specified | ||
* @throws DomainException Unsupported algorithm or bad key was specified | ||
*/ | ||
public static function sign($msg, $key, $alg = 'HS256') | ||
{ | ||
|
@@ -214,14 +216,24 @@ public static function sign($msg, $key, $alg = 'HS256') | |
$success = \openssl_sign($msg, $signature, $key, $algorithm); | ||
if (!$success) { | ||
throw new DomainException("OpenSSL unable to sign data"); | ||
} else { | ||
if ($alg === 'ES256') { | ||
$signature = self::signatureFromDER($signature, 256); | ||
} | ||
if ($alg === 'ES384') { | ||
$signature = self::signatureFromDER($signature, 384); | ||
} | ||
return $signature; | ||
} | ||
if ($alg === 'ES256') { | ||
$signature = self::signatureFromDER($signature, 256); | ||
} elseif ($alg === 'ES384') { | ||
$signature = self::signatureFromDER($signature, 384); | ||
} | ||
return $signature; | ||
case 'sodium_crypto': | ||
if (!function_exists('sodium_crypto_sign_detached')) { | ||
throw new DomainException('libsodium is not available'); | ||
} | ||
try { | ||
// The last non-empty line is used as the key. | ||
$lines = array_filter(explode("\n", $key)); | ||
$key = base64_decode(end($lines)); | ||
return sodium_crypto_sign_detached($msg, $key); | ||
} catch (Exception $e) { | ||
throw new DomainException($e->getMessage(), 0, $e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason (other than staying consistent with the other exceptions thrown from this method) that we are wrapping the libsodium exceptions in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just for consistency and so we are not adding new exception types for BC. Also the other exception types may not be defined in PHP without the library or shim present, so anyone trying to write generic try/catch code will have a harder time. |
||
} | ||
} | ||
} | ||
|
@@ -237,7 +249,7 @@ public static function sign($msg, $key, $alg = 'HS256') | |
* | ||
* @return bool | ||
* | ||
* @throws DomainException Invalid Algorithm or OpenSSL failure | ||
* @throws DomainException Invalid Algorithm, bad key, or OpenSSL failure | ||
*/ | ||
private static function verify($msg, $signature, $key, $alg) | ||
{ | ||
|
@@ -258,6 +270,18 @@ private static function verify($msg, $signature, $key, $alg) | |
throw new DomainException( | ||
'OpenSSL error: ' . \openssl_error_string() | ||
); | ||
case 'sodium_crypto': | ||
if (!function_exists('sodium_crypto_sign_verify_detached')) { | ||
throw new DomainException('libsodium is not available'); | ||
} | ||
try { | ||
// The last non-empty line is used as the key. | ||
$lines = array_filter(explode("\n", $key)); | ||
$key = base64_decode(end($lines)); | ||
return sodium_crypto_sign_verify_detached($signature, $msg, $key); | ||
} catch (Exception $e) { | ||
throw new DomainException($e->getMessage(), 0, $e); | ||
} | ||
case 'hash_hmac': | ||
default: | ||
$hash = \hash_hmac($algorithm, $msg, $key, true); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
uOSJMhbKSG4V5xUHS7B9YHmVg/1yVd+G+Io6oBFhSfY= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
i4eTKkWNIISKumdk3v90cPDrY/g8WRTJWy7DmGDsdzC45IkyFspIbhXnFQdLsH1geZWD/XJV34b4ijqgEWFJ9g== |
Uh oh!
There was an error while loading. Please reload this page.