-
Notifications
You must be signed in to change notification settings - Fork 356
Allow deserializing unknown key type JWKs #530
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
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
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
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 |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| //! Most of the code in this file is taken from <https://github.com/lawliet89/biscuit> but | ||
| //! tweaked to remove the private bits as it's not the goal for this crate currently. | ||
|
|
||
| use std::collections::BTreeMap; | ||
| use std::{fmt, str::FromStr}; | ||
|
|
||
| use serde::{Deserialize, Deserializer, Serialize, Serializer, de}; | ||
|
|
@@ -149,6 +150,7 @@ impl<'de> Deserialize<'de> for KeyOperations { | |
| /// The algorithms of the keys | ||
| #[allow(non_camel_case_types, clippy::upper_case_acronyms)] | ||
| #[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize)] | ||
| #[non_exhaustive] | ||
| pub enum KeyAlgorithm { | ||
| /// HMAC using SHA-256 | ||
| HS256, | ||
|
|
@@ -439,20 +441,31 @@ pub struct OctetKeyPairParameters { | |
| pub x: String, | ||
| } | ||
|
|
||
| /// Parameters for unknown keys | ||
| #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Default, Hash)] | ||
| pub struct OtherKeyParameters { | ||
| #[serde(flatten)] | ||
| #[allow(missing_docs)] | ||
| pub fields: BTreeMap<String, serde_json::Value>, | ||
| } | ||
|
|
||
| /// Algorithm specific parameters | ||
| #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)] | ||
| #[serde(untagged)] | ||
| #[allow(missing_docs)] | ||
| #[non_exhaustive] | ||
| pub enum AlgorithmParameters { | ||
| EllipticCurve(EllipticCurveKeyParameters), | ||
| RSA(RSAKeyParameters), | ||
| OctetKey(OctetKeyParameters), | ||
| OctetKeyPair(OctetKeyPairParameters), | ||
| Other(OtherKeyParameters), | ||
| } | ||
|
|
||
| /// The function to use to hash the intermediate thumbprint data. | ||
| #[derive(Debug, Clone, Eq, PartialEq)] | ||
| #[allow(missing_docs)] | ||
| #[non_exhaustive] | ||
|
Owner
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. can there be other?
Collaborator
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. Not that I'm aware of currently, but I for one would appreciate a future capability of using SHA-3 |
||
| pub enum ThumbprintHash { | ||
| SHA256, | ||
| SHA384, | ||
|
|
@@ -643,6 +656,7 @@ impl Jwk { | |
| ) | ||
| } | ||
| }, | ||
| AlgorithmParameters::Other(_) => return Err(ErrorKind::UnsupportedAlgorithm.into()), | ||
| }; | ||
|
|
||
| Ok(b64_encode((CryptoProvider::get_default().key_utils.compute_digest)( | ||
|
|
@@ -670,6 +684,8 @@ impl JwkSet { | |
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::collections::BTreeMap; | ||
|
|
||
| use serde_json::json; | ||
| use wasm_bindgen_test::wasm_bindgen_test; | ||
|
|
||
|
|
@@ -721,6 +737,42 @@ mod tests { | |
| assert_eq!(key_alg_result, KeyAlgorithm::UNKNOWN_ALGORITHM); | ||
| } | ||
|
|
||
| #[test] | ||
| fn deserialize_unknown_kty() { | ||
| let parameters_json = json!({ | ||
| "kty": "AKP", | ||
| "foo": "bar", | ||
| "solution": 42 | ||
| }); | ||
| let parameters_result: AlgorithmParameters = | ||
| serde_json::from_value(parameters_json).expect("Could not deserialize json"); | ||
| match parameters_result { | ||
| AlgorithmParameters::Other(other_key_parameters) => { | ||
| let mut expected = BTreeMap::new(); | ||
| expected.insert("kty".to_owned(), serde_json::to_value("AKP").unwrap()); | ||
| expected.insert("foo".to_owned(), serde_json::to_value("bar").unwrap()); | ||
| expected.insert("solution".to_owned(), serde_json::to_value(42).unwrap()); | ||
| assert_eq!(other_key_parameters.fields, expected); | ||
| } | ||
| _ => { | ||
| panic!("Unexpected deserialization result"); | ||
| } | ||
| } | ||
|
|
||
| // RFC 9964 Appendix A.1 JWK | ||
| let jwk: Jwk = serde_json::from_value(json!({ | ||
| "kid": "T4xl70S7MT6Zeq6r9V9fPJGVn76wfnXJ21-gyo0Gu6o", | ||
| "kty": "AKP", | ||
| "alg": "ML-DSA-44", | ||
| "pub": "...", | ||
| "priv": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", | ||
| })) | ||
| .expect("Could not deserialize json"); | ||
|
|
||
| assert!(!jwk.is_supported()); | ||
| assert!(matches!(jwk.algorithm, AlgorithmParameters::Other(_))); | ||
| } | ||
|
|
||
| #[test] | ||
| #[wasm_bindgen_test] | ||
| fn check_thumbprint() { | ||
|
|
||
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.
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.
It would nice to have a value in
ErrorKind::UnsupportedAlgorithmbut probably not a blocker