Skip to content

Commit

Permalink
Add error when private key material present in did:jwk on dereference
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Nascimento <[email protected]>
  • Loading branch information
theosirian committed Oct 12, 2022
1 parent d3d27f3 commit f17d4c0
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion did-jwk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ssi_dids::{
Context, Contexts, DIDMethod, Document, Source, VerificationMethod, VerificationMethodMap,
DEFAULT_CONTEXT, DIDURL,
};
use ssi_jwk::JWK;

pub struct DIDJWK;

Expand Down Expand Up @@ -51,7 +52,7 @@ impl DIDResolver for DIDJWK {
}
};

let jwk = if let Ok(jwk) = serde_json::from_slice(&data) {
let jwk: JWK = if let Ok(jwk) = serde_json::from_slice(&data) {
jwk
} else {
return (
Expand All @@ -64,6 +65,21 @@ impl DIDResolver for DIDJWK {
None,
);
};

let public_jwk = jwk.to_public();

if public_jwk != jwk {
return (
ResolutionMetadata {
error: Some(ERROR_INVALID_DID.to_string()),
content_type: None,
property_set: None,
},
None,
None,
);
}

let vm_didurl = DIDURL {
did: did.to_string(),
fragment: Some("0".to_string()),
Expand Down Expand Up @@ -245,4 +261,16 @@ mod tests {
};
assert_eq!(public_key_jwk, jwk);
}

#[async_std::test]
async fn deny_private_key() {
let jwk = JWK::generate_ed25519().unwrap();
let json = serde_jcs::to_string(&jwk).unwrap();
let did =
"did:jwk:".to_string() + &multibase::encode(multibase::Base::Base64Url, &json)[1..];

let (res_meta, _object, _meta) =
dereference(&DIDJWK, &did, &DereferencingInputMetadata::default()).await;
assert!(res_meta.error.is_some());
}
}

0 comments on commit f17d4c0

Please sign in to comment.