-
Notifications
You must be signed in to change notification settings - Fork 350
Feat: Added support for JWK decoding Ed25519 keys #506
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
56f02f6
Feat: Added support for JWK decoding Ed25519 keys
richy1623 3a26212
added some basis for ed488
richy1623 f946deb
wasm test import fix
richy1623 8533706
Merge branch 'master' of github.com:Keats/jsonwebtoken into feat/jwk_…
richy1623 d44949a
resolved more of the merge issues
richy1623 7d2e74a
correction
richy1623 ab6da44
Review changes
richy1623 60d4df1
Non-exhasutive curves
richy1623 0a16e3e
Added ans1 as a dep
richy1623 fa50ec0
review changes
richy1623 a088a7a
Removed the ability to extract Ed448 from a PEM
richy1623 25357a3
Added a warning to the decode method
richy1623 0f411c9
Removed all Ed448 references
richy1623 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ target | |
| Cargo.lock | ||
| .idea/ | ||
| .vscode | ||
| .zed | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -334,6 +334,7 @@ pub enum EllipticCurveKeyType { | |
| /// Type of cryptographic curve used by a key. This is defined in | ||
| /// [RFC 7518 #7.6](https://tools.ietf.org/html/rfc7518#section-7.6) | ||
| #[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize, Hash)] | ||
| #[non_exhaustive] | ||
| pub enum EllipticCurve { | ||
| /// P-256 curve | ||
| #[serde(rename = "P-256")] | ||
|
|
@@ -478,8 +479,6 @@ impl Jwk { | |
| } | ||
|
|
||
| /// Create a `JWK` from an `EncodingKey`. | ||
| /// | ||
| /// Edwards curve based keys are not supported. | ||
| pub fn from_encoding_key(key: &EncodingKey, alg: Algorithm) -> errors::Result<Self> { | ||
| Ok(Self { | ||
| common: CommonParameters { key_algorithm: Some(alg.into()), ..Default::default() }, | ||
|
|
@@ -514,15 +513,32 @@ impl Jwk { | |
| }) | ||
| } | ||
| AlgorithmFamily::Ed => { | ||
| unimplemented!("Edwards curves are not supported"); | ||
| // Get the curve type based off the encoding key length | ||
| // Note: here we will receive a DER key which contains a 16 byte ANS.1 header | ||
| let curve_type: EllipticCurve = match key.inner().len() { | ||
| // 16 byte header + 32 byte Ed25519 key | ||
| 48 => Ok(EllipticCurve::Ed25519), | ||
| _ => Err(Error::from(ErrorKind::InvalidEddsaKey)), | ||
| }?; | ||
|
|
||
| // Extract the public key from the encoding key | ||
| let public_key_bytes = (CryptoProvider::get_default() | ||
| .key_utils | ||
| .ed_pub_components_from_private_key)( | ||
| key.inner(), &curve_type | ||
| )?; | ||
|
|
||
| AlgorithmParameters::OctetKeyPair(OctetKeyPairParameters { | ||
| key_type: OctetKeyPairType::OctetKeyPair, | ||
| curve: curve_type, | ||
| x: b64_encode(public_key_bytes), | ||
| }) | ||
| } | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| /// Create a `JWK` from a `DecodingKey`. | ||
| /// | ||
| /// Edwards curve based keys are not supported. | ||
| pub fn from_decoding_key( | ||
| key: &DecodingKey, | ||
| alg: Option<Algorithm>, | ||
|
|
@@ -574,7 +590,22 @@ impl Jwk { | |
| }) | ||
| } | ||
| crate::algorithms::AlgorithmFamily::Ed => { | ||
| unimplemented!("Edwards curves are not supported"); | ||
| let (curve_type, x) = match &key.kind() { | ||
| DecodingKeyKind::SecretOrDer(pub_bytes) => { | ||
| match pub_bytes.len() { | ||
| // ED25519: https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5 | ||
| 32 => (EllipticCurve::Ed25519, pub_bytes), | ||
| _ => return Err(ErrorKind::InvalidEddsaKey.into()), | ||
| } | ||
| } | ||
| _ => return Err(ErrorKind::InvalidKeyFormat.into()), | ||
| }; | ||
|
Comment on lines
+593
to
+602
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. In #515 should be able to use |
||
|
|
||
| AlgorithmParameters::OctetKeyPair(OctetKeyPairParameters { | ||
| key_type: OctetKeyPairType::OctetKeyPair, | ||
| curve: curve_type, | ||
| x: b64_encode(x), | ||
| }) | ||
| } | ||
| }, | ||
| }) | ||
|
|
@@ -595,7 +626,9 @@ impl Jwk { | |
| a.y, | ||
| ) | ||
| } | ||
| EllipticCurve::Ed25519 => panic!("EllipticCurve can't contain this curve type"), | ||
| EllipticCurve::Ed25519 => { | ||
| panic!("EllipticCurve can't contain this curve type") | ||
| } | ||
| }, | ||
| AlgorithmParameters::RSA(a) => { | ||
| format!( | ||
|
|
@@ -784,4 +817,18 @@ mod tests { | |
| let jwk = Jwk::from_decoding_key(&dec_key, Some(Algorithm::ES256)).unwrap(); | ||
| assert_eq!(jwk, expected_jwk); | ||
| } | ||
|
|
||
| #[test] | ||
| #[cfg(feature = "use_pem")] | ||
| fn check_jwk_from_decoding_key_ed() { | ||
| let enc_key = | ||
| EncodingKey::from_ed_pem(include_bytes!("../tests/eddsa/private_ed25519_key.pem")) | ||
| .unwrap(); | ||
| let dec_key = | ||
| DecodingKey::from_ed_pem(include_bytes!("../tests/eddsa/public_ed25519_key.pem")) | ||
| .unwrap(); | ||
| let expected_jwk = Jwk::from_encoding_key(&enc_key, Algorithm::EdDSA).unwrap(); | ||
| let jwk = Jwk::from_decoding_key(&dec_key, Some(Algorithm::EdDSA)).unwrap(); | ||
| assert_eq!(jwk, expected_jwk); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| -----BEGIN PRIVATE KEY----- | ||
| MEcCAQAwBQYDK2VxBDsEOXkzGoM834lGCR1VS1qNA+fldfH2c31QnRqEeV+voTVX | ||
| dkKVmRC1TYzvN52g2mAbFhEHV2DxtXSUUA== | ||
| -----END PRIVATE KEY----- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.