forked from hyperledger/aries-vcx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support JWK public key VM processing into Key (hyperledger#1297)
* move to latest vdr Signed-off-by: gmulhearn <gmulhearn@proton.me> * move other common git deps to use workspace Signed-off-by: gmulhearn <gmulhearn@proton.me> * cargo update Signed-off-by: gmulhearn <gmulhearn@proton.me> * add decode from jwk with tests Signed-off-by: gmulhearn <gmulhearn@proton.me> * support in VM processing to public key Signed-off-by: gmulhearn <gmulhearn@proton.me> * enable jwk in all unit tests Signed-off-by: gmulhearn <gmulhearn@proton.me> --------- Signed-off-by: gmulhearn <gmulhearn@proton.me> Signed-off-by: George Mulhearn <57472912+gmulhearn@users.noreply.github.com>
- Loading branch information
Showing
11 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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 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,100 @@ | ||
use askar_crypto::{ | ||
alg::{AnyKey, BlsCurves, EcCurves}, | ||
jwk::FromJwk, | ||
repr::ToPublicBytes, | ||
}; | ||
|
||
use crate::{Key, KeyType, PublicKeyError}; | ||
|
||
impl Key { | ||
pub fn from_jwk(jwk: &str) -> Result<Key, PublicKeyError> { | ||
let askar_key: Box<AnyKey> = | ||
FromJwk::from_jwk(jwk).map_err(|e| PublicKeyError::JwkDecodingError(Box::new(e)))?; | ||
|
||
let askar_alg = askar_key.algorithm(); | ||
let pub_key_bytes = askar_key | ||
.to_public_bytes() | ||
.map_err(|e| PublicKeyError::JwkDecodingError(Box::new(e)))? | ||
.to_vec(); | ||
|
||
let key_type = match askar_alg { | ||
askar_crypto::alg::KeyAlg::Ed25519 => KeyType::Ed25519, | ||
askar_crypto::alg::KeyAlg::Bls12_381(BlsCurves::G1G2) => KeyType::Bls12381g1g2, | ||
askar_crypto::alg::KeyAlg::Bls12_381(BlsCurves::G1) => KeyType::Bls12381g1, | ||
askar_crypto::alg::KeyAlg::Bls12_381(BlsCurves::G2) => KeyType::Bls12381g2, | ||
askar_crypto::alg::KeyAlg::X25519 => KeyType::X25519, | ||
askar_crypto::alg::KeyAlg::EcCurve(EcCurves::Secp256r1) => KeyType::P256, | ||
askar_crypto::alg::KeyAlg::EcCurve(EcCurves::Secp384r1) => KeyType::P384, | ||
_ => return Err(PublicKeyError::UnsupportedKeyType(askar_alg.to_string())), | ||
}; | ||
|
||
Key::new(pub_key_bytes, key_type) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_from_ed25519_jwk() { | ||
// vector from https://w3c-ccg.github.io/did-method-key/#ed25519-x25519 | ||
let jwk = r#"{ | ||
"kty": "OKP", | ||
"crv": "Ed25519", | ||
"x": "O2onvM62pC1io6jQKm8Nc2UyFXcd4kOmOsBIoYtZ2ik" | ||
}"#; | ||
let key = Key::from_jwk(jwk).unwrap(); | ||
assert_eq!( | ||
key.fingerprint(), | ||
"z6MkiTBz1ymuepAQ4HEHYSF1H8quG5GLVVQR3djdX3mDooWp" | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_from_x25519_jwk() { | ||
// vector from https://w3c-ccg.github.io/did-method-key/#ed25519-x25519 | ||
let jwk = r#"{ | ||
"kty": "OKP", | ||
"crv": "X25519", | ||
"x": "W_Vcc7guviK-gPNDBmevVw-uJVamQV5rMNQGUwCqlH0" | ||
}"#; | ||
let key = Key::from_jwk(jwk).unwrap(); | ||
assert_eq!( | ||
key.fingerprint(), | ||
"z6LShs9GGnqk85isEBzzshkuVWrVKsRp24GnDuHk8QWkARMW" | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_from_p256_jwk() { | ||
// vector from https://dev.uniresolver.io/ | ||
let jwk = r#"{ | ||
"kty": "EC", | ||
"crv": "P-256", | ||
"x": "fyNYMN0976ci7xqiSdag3buk-ZCwgXU4kz9XNkBlNUI", | ||
"y": "hW2ojTNfH7Jbi8--CJUo3OCbH3y5n91g-IMA9MLMbTU" | ||
}"#; | ||
let key = Key::from_jwk(jwk).unwrap(); | ||
assert_eq!( | ||
key.fingerprint(), | ||
"zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169" | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_from_p384_jwk() { | ||
// vector from https://dev.uniresolver.io/ | ||
let jwk = r#"{ | ||
"kty": "EC", | ||
"crv": "P-384", | ||
"x": "bKq-gg3sJmfkJGrLl93bsumOTX1NubBySttAV19y5ClWK3DxEmqPy0at5lLqBiiv", | ||
"y": "PJQtdHnInU9SY3e8Nn9aOPoP51OFbs-FWJUsU0TGjRtZ4bnhoZXtS92wdzuAotL9" | ||
}"#; | ||
let key = Key::from_jwk(jwk).unwrap(); | ||
assert_eq!( | ||
key.fingerprint(), | ||
"z82Lkytz3HqpWiBmt2853ZgNgNG8qVoUJnyoMvGw6ZEBktGcwUVdKpUNJHct1wvp9pXjr7Y" | ||
); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
mod error; | ||
#[cfg(feature = "jwk")] | ||
mod jwk; | ||
mod key; | ||
mod key_type; | ||
|
||
|
This file contains 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