From d2f8fc12d6aca1128828e3302ce6d06eb9baacb0 Mon Sep 17 00:00:00 2001 From: alexsdsr <114735940+alexsdsr@users.noreply.github.com> Date: Wed, 17 Jan 2024 16:08:54 +0500 Subject: [PATCH] bugfix for SDJWTClaimsStrategy::No support (#19) Fix for #17. Allows holder and verifier to parse SD-JWT with no disclosures included. Signed-off-by: Alexander Sukhachev --- src/lib.rs | 2 +- src/verifier.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 8ad6eff..711294b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,7 +118,7 @@ impl SDJWTCommon { let parts: Vec<&str> = sd_jwt_with_disclosures .split(COMBINED_SERIALIZATION_FORMAT_SEPARATOR) .collect(); - if parts.len() < 3 { + if parts.len() < 2 { // minimal number of SD-JWT parts according to the standard return Err(Error::InvalidInput(format!( "Invalid SD-JWT length: {}", parts.len() diff --git a/src/verifier.rs b/src/verifier.rs index 72d4b39..b2d69dc 100644 --- a/src/verifier.rs +++ b/src/verifier.rs @@ -366,6 +366,57 @@ mod tests { assert_eq!(user_claims, verified_claims); } + #[test] + fn verify_noclaim_presentation() { + let user_claims = json!({ + "sub": "6c5c0a49-b589-431d-bae7-219122a9ec2c", + "iss": "https://example.com/issuer", + "iat": 1683000000, + "exp": 1883000000, + "address": { + "street_address": "Schulstr. 12", + "locality": "Schulpforta", + "region": "Sachsen-Anhalt", + "country": "DE" + } + }); + let private_issuer_bytes = PRIVATE_ISSUER_PEM.as_bytes(); + let issuer_key = EncodingKey::from_ec_pem(private_issuer_bytes).unwrap(); + let sd_jwt = SDJWTIssuer::new(issuer_key, None).issue_sd_jwt( + user_claims.clone(), + SDJWTClaimsStrategy::No, + None, + false, + "compact".to_owned(), + ) + .unwrap(); + + let presentation = SDJWTHolder::new(sd_jwt.clone(), "compact".to_owned()) + .unwrap() + .create_presentation( + user_claims.as_object().unwrap().clone(), + None, + None, + None, + None, + ) + .unwrap(); + assert_eq!(sd_jwt, presentation); + let verified_claims = SDJWTVerifier::new( + presentation, + Box::new(|_, _| { + let public_issuer_bytes = PUBLIC_ISSUER_PEM.as_bytes(); + DecodingKey::from_ec_pem(public_issuer_bytes).unwrap() + }), + None, + None, + "compact".to_owned(), + ) + .unwrap() + .verified_claims; + assert_eq!(user_claims, verified_claims); + } + #[test] fn verify_arrayed_presentation() { let user_claims = json!(