-
Notifications
You must be signed in to change notification settings - Fork 393
Add support for EdDSA #457
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -173,7 +173,7 @@ class OpenIDConnectClient | |
|
|
||
| /** | ||
| * @var mixed holds well-known openid configuration parameters, like policy for MS Azure AD B2C User Flow | ||
| * @see https://docs.microsoft.com/en-us/azure/active-directory-b2c/user-flow-overview | ||
| * @see https://docs.microsoft.com/en-us/azure/active-directory-b2c/user-flow-overview | ||
| */ | ||
| private $wellKnownConfigParameters = []; | ||
|
|
||
|
|
@@ -686,7 +686,7 @@ public function getRedirectURL(): string | |
| } else { | ||
| $protocol = 'http'; | ||
| } | ||
|
|
||
|
Author
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. a whitespace fix |
||
| if (isset($_SERVER['HTTP_X_FORWARDED_PORT'])) { | ||
| $port = (int)$_SERVER['HTTP_X_FORWARDED_PORT']; | ||
| } elseif (isset($_SERVER['SERVER_PORT'])) { | ||
|
|
@@ -709,7 +709,7 @@ public function getRedirectURL(): string | |
| } | ||
|
|
||
| $port = (443 === $port) || (80 === $port) ? '' : ':' . $port; | ||
|
|
||
|
Author
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. a whitespace fix |
||
| $explodedRequestUri = isset($_SERVER['REQUEST_URI']) ? explode('?', $_SERVER['REQUEST_URI']) : []; | ||
| return sprintf('%s://%s%s/%s', $protocol, $host, $port, trim(reset($explodedRequestUri), '/')); | ||
| } | ||
|
|
@@ -1087,6 +1087,18 @@ private function verifyRSAJWTSignature(string $hashType, stdClass $key, $payload | |
| return $key->verify($payload, $signature); | ||
| } | ||
|
|
||
| private function verifyEdDSAJWTsignature($key, $payload, $signature) { | ||
| if (!(property_exists($key, 'x'))) { | ||
| throw new OpenIDConnectClientException('Malformed key object'); | ||
| } | ||
|
|
||
| if (!function_exists("sodium_crypto_sign_verify_detached")) { | ||
|
Collaborator
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. This can be handled via composer as well. Please adjust composer.json and add |
||
| throw new OpenIDConnectClientException('sodium_crypto_sign_verify_detached support unavailable.'); | ||
| } | ||
|
|
||
| return sodium_crypto_sign_verify_detached($signature, $payload, base64url_decode($key->x)); | ||
| } | ||
|
|
||
| private function verifyHMACJWTSignature(string $hashType, string $key, string $payload, string $signature): bool | ||
| { | ||
| $expected = hash_hmac($hashType, $payload, $key, true); | ||
|
|
@@ -1145,6 +1157,24 @@ public function verifyJWTSignature(string $jwt): bool | |
| $jwk, | ||
| $payload, $signature, $signatureType); | ||
| break; | ||
| case 'EdDSA': | ||
| if (isset($header->jwk)) { | ||
| $jwk = $header->jwk; | ||
| $this->verifyJWKHeader($jwk); | ||
| } else { | ||
| $jwks = json_decode($this->fetchURL($this->getProviderConfigValue('jwks_uri'))); | ||
| if ($jwks === NULL) { | ||
| throw new OpenIDConnectClientException('Error decoding JSON from jwks_uri'); | ||
| } | ||
| $jwk = $this->getKeyForHeader($jwks->keys, $header); | ||
| } | ||
|
|
||
| $verified = $this->verifyEdDSAJWTsignature( | ||
| $jwk, | ||
| $payload, | ||
| $signature | ||
| ); | ||
| break; | ||
| case 'HS256': | ||
| case 'HS512': | ||
| case 'HS384': | ||
|
|
@@ -1192,12 +1222,19 @@ protected function validateIssuer(string $iss): bool | |
| protected function verifyJWTClaims($claims, string $accessToken = null): bool | ||
| { | ||
| if(isset($claims->at_hash, $accessToken)) { | ||
| if(isset($this->getIdTokenHeader()->alg) && $this->getIdTokenHeader()->alg !== 'none') { | ||
| $bit = substr($this->getIdTokenHeader()->alg, 2, 3); | ||
| } else { | ||
| // TODO: Error case. throw exception??? | ||
| $bit = '256'; | ||
| switch($this->getIdTokenHeader()->alg ?? '') { | ||
| case 'EdDSA': | ||
| $bit = '512'; | ||
| break; | ||
| case 'none': | ||
| case '': | ||
| // TODO: Error case. throw exception??? | ||
| $bit = '256'; | ||
| break; | ||
| default: | ||
| $bit = substr($this->getIdTokenHeader()->alg, 2, 3); | ||
| } | ||
|
|
||
| $len = ((int)$bit)/16; | ||
| $expected_at_hash = $this->urlEncode(substr(hash('sha'.$bit, $accessToken, true), 0, $len)); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "keys": [ | ||
| { | ||
| "kty": "OKP", | ||
| "kid": "-1909572257", | ||
| "alg": "EdDSA", | ||
| "crv": "Ed25519", | ||
| "x": "XWxGtApfcqmKI7p0OKnF5JSEWMVoLsytFXLEP7xZ_l8" | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "keys": [ | ||
| { | ||
| "alg": "RS512", | ||
| "e": "AQAB", | ||
| "key_ops": ["verify"], | ||
| "kty": "RSA", | ||
| "n": "sOFO_fifz82fLvrrOo9S3lBU347DrM3DmzH47SNw1ILMkNwDMK38on-GNcl59If0DTEEr7WITPLJ5FS_Rf3XcX09ui2Ol2y7LhMLpL3AQMf7kFyWKhMtOiEkaUrUc_nozyAF4tsKRAf8anYrImaS1NHHmsLBJ1QAWYd4N6b5L09zHX6LkOrLqBY-xgI1W4hQUOKNwF2BoA6KC8eX89iDqgRKQFXLt2pV6h55BRjJ7EBEkoT7KvaACVoUNL4Y5Pg2CLwLm0bYPMugaO-fLPp9tok5mdTD2fQZRGChzIQjWl6BLT2uO639Ccwmtb3VkjHDv061UI8TIAK4_mWDqscOUdbRpa5Z-oPq7ZM7eps83YxJXKvdkZzmx6tJayrNYH7BEHP03p3TZ1pDszDOLD-kmCppja0SG9NeH4Zu6PqxaNtimOKEakmUx0s9MhnlYWUZnB0mdl4YXr_Ypd0nsOJLrpvvjlQacozDIgflTaXcpZJkiwiRWoBmHuQXJHTuqP3_", | ||
| "use": "sig", | ||
| "kid": "3b4b7fcbac8100fe528912776417009a" | ||
| } | ||
| ] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a whitespace fix