diff --git a/.gitignore b/.gitignore index 2e9cba3d..b1dd9bc7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ target/ !**/src/main/**/target/ !**/src/test/**/target/ .DS_Store +.class ### STS ### .apt_generated diff --git a/docs/README.md b/docs/README.md index ca87e188..203914e7 100644 --- a/docs/README.md +++ b/docs/README.md @@ -184,7 +184,7 @@ public static VerifiableCredential createVCWithoutProof() { ``` -5. To Generate VerifiableCredential with ED21559/JWS proof: +5. To Generate VerifiableCredential with ED25519/JWS proof: ```java @@ -202,7 +202,7 @@ import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCreden import org.eclipse.tractusx.ssi.lib.proof.LinkedDataProofGenerator; - public static VerifiableCredential createVCWithED21559Proof( + public static VerifiableCredential createVCWithED25519Proof( VerifiableCredential credential, byte[] privateKey, Did issuer) { // VC Builder @@ -217,7 +217,7 @@ import org.eclipse.tractusx.ssi.lib.proof.LinkedDataProofGenerator; .type(credential.getTypes()); // Ed25519 Proof Builder - final LinkedDataProofGenerator generator = LinkedDataProofGenerator.newInstance(SignatureType.ED21559); + final LinkedDataProofGenerator generator = LinkedDataProofGenerator.newInstance(SignatureType.ED25519); final Ed25519Signature2020 proof = (Ed25519Signature2020) generator.createProof( builder.build(), URI.create(issuer + "#key-1"), privateKey); @@ -370,7 +370,7 @@ import org.eclipse.tractusx.ssi.lib.proof.LinkedDataProofValidation; import org.eclipse.tractusx.ssi.lib.resolver.DidDocumentResolverRegistryImpl; import org.eclipse.tractusx.ssi.lib.model.proof.jws.JWSSignature2020; - public static boolean verifyED21559LD(VerifiableCredential verifiableCredential) { + public static boolean verifyED25519LD(VerifiableCredential verifiableCredential) { // DID Resolver Constracture params DidWebParser didParser = new DidWebParser(); var httpClient = HttpClient.newHttpClient(); @@ -381,7 +381,7 @@ import org.eclipse.tractusx.ssi.lib.model.proof.jws.JWSSignature2020; new DidWebDocumentResolver(httpClient, didParser, enforceHttps)); LinkedDataProofValidation proofValidation = - LinkedDataProofValidation.newInstance(SignatureType.ED21559,didDocumentResolverRegistry); + LinkedDataProofValidation.newInstance(SignatureType.ED25519,didDocumentResolverRegistry); return proofValidation.verify(verifiableCredential); } diff --git a/src/main/java/org/eclipse/tractusx/ssi/examples/BuildDIDDocEd25519VerificationKey2020.java b/src/main/java/org/eclipse/tractusx/ssi/examples/BuildDIDDocEd25519VerificationKey2020.java index 922c3b02..21a54b67 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/examples/BuildDIDDocEd25519VerificationKey2020.java +++ b/src/main/java/org/eclipse/tractusx/ssi/examples/BuildDIDDocEd25519VerificationKey2020.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -27,9 +27,9 @@ import org.eclipse.tractusx.ssi.lib.crypt.IKeyGenerator; import org.eclipse.tractusx.ssi.lib.crypt.IPublicKey; import org.eclipse.tractusx.ssi.lib.crypt.KeyPair; -import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559Generator; +import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519Generator; import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory; -import org.eclipse.tractusx.ssi.lib.exception.KeyGenerationException; +import org.eclipse.tractusx.ssi.lib.exception.key.KeyGenerationException; import org.eclipse.tractusx.ssi.lib.model.MultibaseString; import org.eclipse.tractusx.ssi.lib.model.base.MultibaseFactory; import org.eclipse.tractusx.ssi.lib.model.did.Did; @@ -54,7 +54,7 @@ public static DidDocument buildDidDocument(String hostName) throws KeyGeneration // Extracting keys // final Ed25519KeySet keySet = new Ed25519KeySet(privateKey, publicKey); - IKeyGenerator keyGenerator = new x21559Generator(); + IKeyGenerator keyGenerator = new x25519Generator(); KeyPair keyPair = keyGenerator.generateKey(); IPublicKey publicKey = keyPair.getPublicKey(); diff --git a/src/main/java/org/eclipse/tractusx/ssi/examples/BuildDIDDocJsonWebKey2020.java b/src/main/java/org/eclipse/tractusx/ssi/examples/BuildDIDDocJsonWebKey2020.java index 5ecb4489..d0c1cead 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/examples/BuildDIDDocJsonWebKey2020.java +++ b/src/main/java/org/eclipse/tractusx/ssi/examples/BuildDIDDocJsonWebKey2020.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -30,8 +30,8 @@ import org.eclipse.tractusx.ssi.lib.crypt.IPrivateKey; import org.eclipse.tractusx.ssi.lib.crypt.IPublicKey; import org.eclipse.tractusx.ssi.lib.crypt.jwk.JsonWebKey; -import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559PrivateKey; -import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559PublicKey; +import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519PrivateKey; +import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519PublicKey; import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; @@ -54,8 +54,8 @@ public static DidDocument buildDidDocument(String hostName) { final Did did = DidWebFactory.fromHostname(hostName); OctetKeyPair octetKeyPair = new OctetKeyPairGenerator(Curve.Ed25519).keyID("1").generate(); - IPrivateKey privateKey = new x21559PrivateKey(octetKeyPair.getDecodedD()); - IPublicKey publicKey = new x21559PublicKey(octetKeyPair.getDecodedX()); + IPrivateKey privateKey = new x25519PrivateKey(octetKeyPair.getDecodedD()); + IPublicKey publicKey = new x25519PublicKey(octetKeyPair.getDecodedX()); // JWK JsonWebKey jwk = new JsonWebKey(octetKeyPair.getKeyID(), publicKey, privateKey); diff --git a/src/main/java/org/eclipse/tractusx/ssi/examples/ResolveDIDDoc.java b/src/main/java/org/eclipse/tractusx/ssi/examples/ResolveDIDDoc.java index f3c180e7..3b9713fc 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/examples/ResolveDIDDoc.java +++ b/src/main/java/org/eclipse/tractusx/ssi/examples/ResolveDIDDoc.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -22,14 +22,13 @@ package org.eclipse.tractusx.ssi.examples; import java.net.http.HttpClient; -import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistryImpl; -import org.eclipse.tractusx.ssi.lib.did.web.DidWebDocumentResolver; import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory; +import org.eclipse.tractusx.ssi.lib.did.web.DidWebResolver; import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser; -import org.eclipse.tractusx.ssi.lib.exception.DidDocumentResolverNotRegisteredException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; -import org.eclipse.tractusx.ssi.lib.model.did.DidMethod; /** This is an example class to demonstrate did document resolve from given did web url */ public class ResolveDIDDoc { @@ -38,26 +37,23 @@ public class ResolveDIDDoc { * * @param didUrl the did url * @return the did document + * @throws org.eclipse.tractusx.ssi.lib.did.resolver.DidResolverException * @throws DidDocumentResolverNotRegisteredException the did document resolver not registered * exception */ public static DidDocument ResovleDocument(String didUrl) - throws DidDocumentResolverNotRegisteredException { + throws DidParseException, DidResolverException { // DID Resolver Constracture params DidWebParser didParser = new DidWebParser(); var httpClient = HttpClient.newHttpClient(); var enforceHttps = false; - // DID Method - DidMethod didWeb = new DidMethod("web"); - // DID Did did = DidWebFactory.fromHostname(didUrl); - var didDocumentResolverRegistry = new DidDocumentResolverRegistryImpl(); - didDocumentResolverRegistry.register( - new DidWebDocumentResolver(httpClient, didParser, enforceHttps)); - return didDocumentResolverRegistry.get(didWeb).resolve(did); + var didResolver = new DidWebResolver(httpClient, didParser, enforceHttps); + + return didResolver.resolve(did); } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/examples/VC.java b/src/main/java/org/eclipse/tractusx/ssi/examples/VC.java index 061f26bd..9bb6e3c2 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/examples/VC.java +++ b/src/main/java/org/eclipse/tractusx/ssi/examples/VC.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -26,11 +26,12 @@ import java.util.List; import java.util.Map; import org.eclipse.tractusx.ssi.lib.crypt.IPrivateKey; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePrivateKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.SsiException; -import org.eclipse.tractusx.ssi.lib.exception.UnsupportedSignatureTypeException; +import org.eclipse.tractusx.ssi.lib.exception.json.TransformJsonLdException; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPrivateKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureGenerateFailedException; +import org.eclipse.tractusx.ssi.lib.exception.proof.UnsupportedSignatureTypeException; import org.eclipse.tractusx.ssi.lib.model.did.Did; -import org.eclipse.tractusx.ssi.lib.model.proof.ed21559.Ed25519Signature2020; +import org.eclipse.tractusx.ssi.lib.model.proof.ed25519.Ed25519Signature2020; import org.eclipse.tractusx.ssi.lib.model.proof.jws.JWSSignature2020; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialBuilder; @@ -71,7 +72,7 @@ public static VerifiableCredential createVCWithoutProof() { } /** - * Create verifiable credential with ED21559 proof + * Create verifiable credential with ED25519 proof * * @param credential the credential * @param privateKey the private key @@ -81,9 +82,10 @@ public static VerifiableCredential createVCWithoutProof() { * @throws SsiException the ssi exception * @throws InvalidePrivateKeyFormat the invalide private key format */ - public static VerifiableCredential createVCWithED21559Proof( + public static VerifiableCredential createVCWithED25519Proof( VerifiableCredential credential, IPrivateKey privateKey, Did issuer) - throws UnsupportedSignatureTypeException, SsiException, InvalidePrivateKeyFormat { + throws UnsupportedSignatureTypeException, InvalidPrivateKeyFormatException, + SignatureGenerateFailedException, TransformJsonLdException { // VC Builder final VerifiableCredentialBuilder builder = @@ -98,7 +100,7 @@ public static VerifiableCredential createVCWithED21559Proof( // Ed25519 Proof Builder final LinkedDataProofGenerator generator = - LinkedDataProofGenerator.newInstance(SignatureType.ED21559); + LinkedDataProofGenerator.newInstance(SignatureType.ED25519); final Ed25519Signature2020 proof = (Ed25519Signature2020) generator.createProof(builder.build(), URI.create(issuer + "#key-1"), privateKey); @@ -122,7 +124,8 @@ public static VerifiableCredential createVCWithED21559Proof( */ public static VerifiableCredential createVCWithJWSProof( VerifiableCredential credential, IPrivateKey privateKey, Did issuer) - throws UnsupportedSignatureTypeException, SsiException, InvalidePrivateKeyFormat { + throws UnsupportedSignatureTypeException, InvalidPrivateKeyFormatException, + SignatureGenerateFailedException, TransformJsonLdException { // VC Builder final VerifiableCredentialBuilder builder = @@ -136,9 +139,10 @@ public static VerifiableCredential createVCWithJWSProof( .type(credential.getTypes()); // JWS Proof Builder - final LinkedDataProofGenerator generator = - LinkedDataProofGenerator.newInstance(SignatureType.JWS); - final JWSSignature2020 proof = + LinkedDataProofGenerator generator = LinkedDataProofGenerator.newInstance(SignatureType.JWS); + + JWSSignature2020 proof = null; + proof = (JWSSignature2020) generator.createProof(builder.build(), URI.create(issuer + "#key-1"), privateKey); diff --git a/src/main/java/org/eclipse/tractusx/ssi/examples/Validation.java b/src/main/java/org/eclipse/tractusx/ssi/examples/Validation.java index 43bf4b79..82c6c767 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/examples/Validation.java +++ b/src/main/java/org/eclipse/tractusx/ssi/examples/Validation.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -22,8 +22,9 @@ package org.eclipse.tractusx.ssi.examples; import com.nimbusds.jwt.SignedJWT; -import org.eclipse.tractusx.ssi.lib.exception.JwtAudienceCheckFailedException; -import org.eclipse.tractusx.ssi.lib.exception.JwtExpiredException; +import org.eclipse.tractusx.ssi.lib.exception.proof.JwtAudienceCheckException; +import org.eclipse.tractusx.ssi.lib.exception.proof.JwtExpiredException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureParseException; import org.eclipse.tractusx.ssi.lib.jwt.SignedJwtValidator; /** This is example class to demonstrate how to validate JWT expiry date and audience */ @@ -33,11 +34,11 @@ public class Validation { * * @param signedJWT the signed jwt * @param audience the audience - * @throws JwtAudienceCheckFailedException the jwt audience check failed exception + * @throws JwtAudienceCheckException the jwt audience check failed exception * @throws JwtExpiredException the jwt expired exception */ public static void validateJWTDate(SignedJWT signedJWT, String audience) - throws JwtAudienceCheckFailedException, JwtExpiredException { + throws JwtAudienceCheckException, JwtExpiredException, SignatureParseException { SignedJwtValidator jwtValidator = new SignedJwtValidator(); jwtValidator.validateDate(signedJWT); } @@ -47,11 +48,11 @@ public static void validateJWTDate(SignedJWT signedJWT, String audience) * * @param signedJWT the signed jwt * @param audience the audience - * @throws JwtAudienceCheckFailedException the jwt audience check failed exception + * @throws JwtAudienceCheckException the jwt audience check failed exception * @throws JwtExpiredException the jwt expired exception */ public static void validateJWTAudiences(SignedJWT signedJWT, String audience) - throws JwtAudienceCheckFailedException, JwtExpiredException { + throws JwtAudienceCheckException, JwtExpiredException, SignatureParseException { SignedJwtValidator jwtValidator = new SignedJwtValidator(); jwtValidator.validateAudiences(signedJWT, audience); } diff --git a/src/main/java/org/eclipse/tractusx/ssi/examples/Verification.java b/src/main/java/org/eclipse/tractusx/ssi/examples/Verification.java index 59497719..00c77c18 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/examples/Verification.java +++ b/src/main/java/org/eclipse/tractusx/ssi/examples/Verification.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -23,10 +23,19 @@ import com.nimbusds.jwt.SignedJWT; import java.net.http.HttpClient; +import java.security.SignatureException; import org.eclipse.tractusx.ssi.lib.did.web.DidWebResolver; import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser; -import org.eclipse.tractusx.ssi.lib.exception.DidDocumentResolverNotRegisteredException; -import org.eclipse.tractusx.ssi.lib.exception.JwtException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException; +import org.eclipse.tractusx.ssi.lib.exception.json.TransformJsonLdException; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPublicKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.proof.NoVerificationKeyFoundException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureParseException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureVerificationException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureVerificationFailedException; +import org.eclipse.tractusx.ssi.lib.exception.proof.UnsupportedSignatureTypeException; +import org.eclipse.tractusx.ssi.lib.exception.proof.UnsupportedVerificationMethodException; import org.eclipse.tractusx.ssi.lib.jwt.SignedJwtVerifier; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; import org.eclipse.tractusx.ssi.lib.proof.LinkedDataProofValidation; @@ -34,15 +43,19 @@ /** * This is example class to demonstrate how to verify @{@link SignedJWT} and {@link * VerifiableCredential} + * + * @throws DidParseException + * @throws SignatureException + * @throws DidResolverException + * @throws SignatureVerificationException + * @throws UnsupportedVerificationMethodException */ public class Verification { - /** - * Verify jwt. - * - * @param jwt the jwt - */ - public static void verifyJWT(SignedJWT jwt) { + public static void verifyJWT(SignedJWT jwt) + throws DidParseException, SignatureException, DidResolverException, + SignatureVerificationException, UnsupportedVerificationMethodException, + SignatureParseException { // DID Resolver constructor params DidWebParser didParser = new DidWebParser(); var httpClient = HttpClient.newHttpClient(); @@ -50,22 +63,30 @@ public static void verifyJWT(SignedJWT jwt) { var didResolver = new DidWebResolver(httpClient, didParser, enforceHttps); SignedJwtVerifier jwtVerifier = new SignedJwtVerifier(didResolver); - try { - jwtVerifier.verify(jwt); - } catch (JwtException | DidDocumentResolverNotRegisteredException e) { - // An exception will be thrown here in case JWT verification failed or DID - // Document Resolver not able to resolve. - e.printStackTrace(); - } + + jwtVerifier.verify(jwt); } /** - * Verify ed21559 signed ld. + * Verify ed25519 signed ld. * * @param verifiableCredential the verifiable credential * @return the boolean + * @throws DidDocumentResolverNotRegisteredException + * @throws TransformJsonLdException + * @throws NoVerificationKeyFoundException + * @throws SignatureVerificationException + * @throws InvalidPublicKeyFormatException + * @throws DidParseException + * @throws SignatureParseException + * @throws UnsupportedSignatureTypeException + * @throws SignatureVerificationFailedException */ - public static boolean verifyED21559LD(VerifiableCredential verifiableCredential) { + public static boolean verifyED25519LD(VerifiableCredential verifiableCredential) + throws UnsupportedSignatureTypeException, SignatureParseException, DidParseException, + InvalidPublicKeyFormatException, SignatureVerificationException, + NoVerificationKeyFoundException, TransformJsonLdException, + SignatureVerificationFailedException { // DID Resolver constructor params DidWebParser didParser = new DidWebParser(); var httpClient = HttpClient.newHttpClient(); @@ -76,13 +97,11 @@ public static boolean verifyED21559LD(VerifiableCredential verifiableCredential) return proofValidation.verify(verifiableCredential); } - /** - * Verify jws signed ld. - * - * @param verifiableCredential the verifiable credential - * @return the boolean - */ - public static boolean verifyJWSLD(VerifiableCredential verifiableCredential) { + public static boolean verifyJWSLD(VerifiableCredential verifiableCredential) + throws UnsupportedSignatureTypeException, SignatureParseException, DidParseException, + InvalidPublicKeyFormatException, SignatureVerificationException, + NoVerificationKeyFoundException, TransformJsonLdException, + SignatureVerificationFailedException { // DID Resolver constructor params DidWebParser didParser = new DidWebParser(); var httpClient = HttpClient.newHttpClient(); diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/IKey.java b/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/IKey.java index 6fa68596..5f99fd30 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/IKey.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/IKey.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,7 +21,7 @@ package org.eclipse.tractusx.ssi.lib.crypt; -import java.io.IOException; +import org.eclipse.tractusx.ssi.lib.exception.key.KeyTransformationException; import org.eclipse.tractusx.ssi.lib.model.base.EncodeType; /** The interface Key. */ @@ -39,7 +39,7 @@ public interface IKey { * @return the string * @throws IOException the io exception */ - String asStringForStoring() throws IOException; + String asStringForStoring() throws KeyTransformationException; /** * Convert the key to a string for exchange. @@ -48,7 +48,7 @@ public interface IKey { * @return the string * @throws IOException the io exception */ - String asStringForExchange(EncodeType encodeType) throws IOException; + String asStringForExchange(EncodeType encodeType) throws KeyTransformationException; /** * Convert the key to a byte array. diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/IKeyGenerator.java b/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/IKeyGenerator.java index 70a380a1..7e4273b4 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/IKeyGenerator.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/IKeyGenerator.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,7 +21,7 @@ package org.eclipse.tractusx.ssi.lib.crypt; -import org.eclipse.tractusx.ssi.lib.exception.KeyGenerationException; +import org.eclipse.tractusx.ssi.lib.exception.key.KeyGenerationException; /** The interface KeyGenerator. */ public interface IKeyGenerator { diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x21559/x21559Generator.java b/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x25519/x25519Generator.java similarity index 71% rename from src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x21559/x21559Generator.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x25519/x25519Generator.java index 50b14a09..5c65bd8d 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x21559/x21559Generator.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x25519/x25519Generator.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -19,7 +19,7 @@ * ******************************************************************************* */ -package org.eclipse.tractusx.ssi.lib.crypt.x21559; +package org.eclipse.tractusx.ssi.lib.crypt.x25519; import java.security.SecureRandom; import org.bouncycastle.crypto.AsymmetricCipherKeyPair; @@ -29,12 +29,12 @@ import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters; import org.eclipse.tractusx.ssi.lib.crypt.IKeyGenerator; import org.eclipse.tractusx.ssi.lib.crypt.KeyPair; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePrivateKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePublicKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.KeyGenerationException; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPrivateKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPublicKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.key.KeyGenerationException; -/** X21559 key generator. */ -public class x21559Generator implements IKeyGenerator { +/** X25519 key generator. */ +public class x25519Generator implements IKeyGenerator { @Override public KeyPair generateKey() throws KeyGenerationException { @@ -48,18 +48,18 @@ public KeyPair generateKey() throws KeyGenerationException { Ed25519PrivateKeyParameters privateKey = (Ed25519PrivateKeyParameters) keyPair.getPrivate(); Ed25519PublicKeyParameters publicKey = (Ed25519PublicKeyParameters) keyPair.getPublic(); - x21559PrivateKey x21559PrivateKey; + x25519PrivateKey x25519PrivateKey; try { - x21559PrivateKey = new x21559PrivateKey(privateKey.getEncoded()); - } catch (InvalidePrivateKeyFormat e) { + x25519PrivateKey = new x25519PrivateKey(privateKey.getEncoded()); + } catch (InvalidPrivateKeyFormatException e) { throw new KeyGenerationException(e.getCause()); } - x21559PublicKey x21559PublicKey; + x25519PublicKey x25519PublicKey; try { - x21559PublicKey = new x21559PublicKey(publicKey.getEncoded()); - } catch (InvalidePublicKeyFormat e) { + x25519PublicKey = new x25519PublicKey(publicKey.getEncoded()); + } catch (InvalidPublicKeyFormatException e) { throw new KeyGenerationException(e.getCause()); } - return new KeyPair(x21559PublicKey, x21559PrivateKey); + return new KeyPair(x25519PublicKey, x25519PrivateKey); } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x21559/x21559PrivateKey.java b/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x25519/x25519PrivateKey.java similarity index 64% rename from src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x21559/x21559PrivateKey.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x25519/x25519PrivateKey.java index 49f31f1e..24709425 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x21559/x21559PrivateKey.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x25519/x25519PrivateKey.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -19,7 +19,7 @@ * ******************************************************************************* */ -package org.eclipse.tractusx.ssi.lib.crypt.x21559; +package org.eclipse.tractusx.ssi.lib.crypt.x25519; import java.io.IOException; import java.io.StringReader; @@ -31,42 +31,49 @@ import org.bouncycastle.util.io.pem.PemReader; import org.bouncycastle.util.io.pem.PemWriter; import org.eclipse.tractusx.ssi.lib.crypt.IPrivateKey; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePrivateKeyFormat; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPrivateKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.key.KeyTransformationException; import org.eclipse.tractusx.ssi.lib.model.base.EncodeType; import org.eclipse.tractusx.ssi.lib.model.base.MultibaseFactory; -/** The type X21559 private key. */ -public class x21559PrivateKey implements IPrivateKey { +/** The type X25519 private key. */ +public class x25519PrivateKey implements IPrivateKey { + private final int KEY_LENGTH = 32; private final @NonNull byte[] key; /** - * Instantiates a new X 21559 private key. + * Instantiates a new X 25519 private key. * * @param privateKey the private key * @throws InvalidePrivateKeyFormat the invalide private key format */ - public x21559PrivateKey(byte[] privateKey) throws InvalidePrivateKeyFormat { + public x25519PrivateKey(byte[] privateKey) throws InvalidPrivateKeyFormatException { if (this.getKeyLength() != privateKey.length) { - throw new InvalidePrivateKeyFormat(getKeyLength(), privateKey.length); + throw new InvalidPrivateKeyFormatException(getKeyLength(), privateKey.length); } this.key = privateKey; } /** - * Instantiates a new X 21559 private key. + * Instantiates a new X 25519 private key. * * @param privateKey the private key * @param pemFormat the pem format * @throws InvalidePrivateKeyFormat the invalide private key format * @throws IOException the io exception */ - public x21559PrivateKey(String privateKey, boolean pemFormat) - throws InvalidePrivateKeyFormat, IOException { - if (pemFormat) { + public x25519PrivateKey(String privateKey, boolean PEMFormat) + throws InvalidPrivateKeyFormatException { + if (PEMFormat) { StringReader sr = new StringReader(privateKey); PemReader reader = new PemReader(sr); - PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent()); + PKCS8EncodedKeySpec keySpec; + try { + keySpec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent()); + } catch (IOException e) { + throw new InvalidPrivateKeyFormatException(e.getMessage()); + } Ed25519PrivateKeyParameters ed25519PrivateKeyParameters = new Ed25519PrivateKeyParameters(keySpec.getEncoded()); this.key = ed25519PrivateKeyParameters.getEncoded(); @@ -76,18 +83,23 @@ public x21559PrivateKey(String privateKey, boolean pemFormat) } if (this.getKeyLength() != key.length) { - throw new InvalidePrivateKeyFormat(getKeyLength(), privateKey.length()); + throw new InvalidPrivateKeyFormatException(getKeyLength(), privateKey.length()); } } @Override - public String asStringForStoring() throws IOException { + public String asStringForStoring() throws KeyTransformationException { - PemObject pemObject = new PemObject("ED21559 Private Key", this.key); + PemObject pemObject = new PemObject("ED25519 Private Key", this.key); StringWriter sw = new StringWriter(); PemWriter writer = new PemWriter(sw); - writer.writeObject(pemObject); - writer.close(); + try { + writer.writeObject(pemObject); + writer.close(); + } catch (IOException e) { + throw new KeyTransformationException(e.getMessage()); + } + return sw.toString(); } @@ -104,6 +116,6 @@ public byte[] asByte() { @Override public int getKeyLength() { - return 32; + return KEY_LENGTH; } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x21559/x21559PublicKey.java b/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x25519/x25519PublicKey.java similarity index 68% rename from src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x21559/x21559PublicKey.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x25519/x25519PublicKey.java index 1be1e7d3..b0646aa0 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x21559/x21559PublicKey.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/crypt/x25519/x25519PublicKey.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -19,7 +19,7 @@ * ******************************************************************************* */ -package org.eclipse.tractusx.ssi.lib.crypt.x21559; +package org.eclipse.tractusx.ssi.lib.crypt.x25519; import java.io.IOException; import java.io.StringReader; @@ -31,40 +31,42 @@ import org.bouncycastle.util.io.pem.PemReader; import org.bouncycastle.util.io.pem.PemWriter; import org.eclipse.tractusx.ssi.lib.crypt.IPublicKey; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePublicKeyFormat; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPublicKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.key.KeyTransformationException; import org.eclipse.tractusx.ssi.lib.model.base.EncodeType; import org.eclipse.tractusx.ssi.lib.model.base.MultibaseFactory; -/** The type X 21559 public key. */ -public class x21559PublicKey implements IPublicKey { +/** The type X 25519 public key. */ +public class x25519PublicKey implements IPublicKey { + private final int KEY_LENGTH = 32; private final @NonNull byte[] originalKey; /** - * Instantiates a new X 21559 public key. + * Instantiates a new X 25519 public key. * * @param publicKey the public key * @throws InvalidePublicKeyFormat the invalide public key format */ - public x21559PublicKey(byte[] publicKey) throws InvalidePublicKeyFormat { + public x25519PublicKey(byte[] publicKey) throws InvalidPublicKeyFormatException { if (this.getKeyLength() != publicKey.length) { - throw new InvalidePublicKeyFormat(getKeyLength(), publicKey.length); + throw new InvalidPublicKeyFormatException(getKeyLength(), publicKey.length); } this.originalKey = publicKey; } /** - * Instantiates a new X21559 public key. + * Instantiates a new X25519 public key. * * @param publicKey the public key * @param pemFormat the pe mformat * @throws InvalidePublicKeyFormat the invalide public key format * @throws IOException the io exception */ - public x21559PublicKey(String publicKey, boolean pemFormat) - throws InvalidePublicKeyFormat, IOException { + public x25519PublicKey(String publicKey, boolean PEMformat) + throws InvalidPublicKeyFormatException, IOException { - if (pemFormat) { + if (PEMformat) { StringReader sr = new StringReader(publicKey); PemReader reader = new PemReader(sr); PemObject pemObject = reader.readPemObject(); @@ -77,22 +79,27 @@ public x21559PublicKey(String publicKey, boolean pemFormat) } if (this.getKeyLength() != originalKey.length) { - throw new InvalidePublicKeyFormat(getKeyLength(), originalKey.length); + throw new InvalidPublicKeyFormatException(getKeyLength(), originalKey.length); } } @Override - public String asStringForStoring() throws IOException { - PemObject pemObject = new PemObject("ED21559 Public Key", this.originalKey); + public String asStringForStoring() throws KeyTransformationException { + PemObject pemObject = new PemObject("ED25519 Public Key", this.originalKey); StringWriter sw = new StringWriter(); PemWriter writer = new PemWriter(sw); - writer.writeObject(pemObject); - writer.close(); + try { + writer.writeObject(pemObject); + writer.close(); + } catch (IOException e) { + throw new KeyTransformationException(e.getMessage()); + } + return sw.toString(); } @Override - public String asStringForExchange(EncodeType encodeType) throws IOException { + public String asStringForExchange(EncodeType encodeType) { return MultibaseFactory.create(encodeType, originalKey).getEncoded(); } @@ -103,6 +110,6 @@ public byte[] asByte() { @Override public int getKeyLength() { - return 32; + return KEY_LENGTH; } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/CompositeDidResolver.java b/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/CompositeDidResolver.java index cd7f2618..f7720439 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/CompositeDidResolver.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/CompositeDidResolver.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,6 +21,7 @@ package org.eclipse.tractusx.ssi.lib.did.resolver; import java.util.Arrays; +import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidDocumentResolverRegistry.java b/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidDocumentResolverRegistry.java deleted file mode 100644 index 07ac4d55..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidDocumentResolverRegistry.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.did.resolver; - -import org.eclipse.tractusx.ssi.lib.exception.DidDocumentResolverNotRegisteredException; -import org.eclipse.tractusx.ssi.lib.model.did.DidMethod; - -/** - * The interface Did document resolver registry. - * - * @deprecated replaced by {@link DidResolver} - */ -public interface DidDocumentResolverRegistry { - /** - * Get did document resolver. - * - * @param did the did - * @return the did document resolver - * @throws DidDocumentResolverNotRegisteredException the did document resolver not registered - * exception - */ - DidDocumentResolver get(DidMethod did) throws DidDocumentResolverNotRegisteredException; - - /** - * Register a new did document resolver - * - * @param resolver the resolver - */ - void register(DidDocumentResolver resolver); - - /** - * Unregister a did document resolver - * - * @param resolver the resolver - */ - void unregister(DidDocumentResolver resolver); -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidDocumentResolverRegistryImpl.java b/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidDocumentResolverRegistryImpl.java deleted file mode 100644 index a9f8f553..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidDocumentResolverRegistryImpl.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.did.resolver; - -import java.util.HashMap; -import java.util.Map; -import org.eclipse.tractusx.ssi.lib.exception.DidDocumentResolverNotRegisteredException; -import org.eclipse.tractusx.ssi.lib.exception.SsiException; -import org.eclipse.tractusx.ssi.lib.model.did.DidMethod; - -/** - * The type Did document resolver registry. - * - * @deprecated replaced by {@link DidResolver} - */ -public class DidDocumentResolverRegistryImpl implements DidDocumentResolverRegistry { - - /** The Resolvers. */ - public final Map resolvers = new HashMap<>(); - - @Override - public DidDocumentResolver get(DidMethod didMethod) - throws DidDocumentResolverNotRegisteredException { - - if (!resolvers.containsKey(didMethod)) { - throw new DidDocumentResolverNotRegisteredException(didMethod); - } - - return resolvers.get(didMethod); - } - - @Override - public void register(DidDocumentResolver resolver) { - if (resolvers.containsKey(resolver.getSupportedMethod())) { - throw new SsiException( - String.format( - "Resolver for method '%s' is already registered", resolver.getSupportedMethod())); - } - resolvers.put(resolver.getSupportedMethod(), resolver); - } - - @Override - public void unregister(DidDocumentResolver resolver) { - if (!resolvers.containsKey(resolver.getSupportedMethod())) { - throw new SsiException( - String.format("Resolver for method '%s' not registered", resolver.getSupportedMethod())); - } - resolvers.remove(resolver.getSupportedMethod()); - } -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidResolver.java b/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidResolver.java index fc0ed659..0298e57b 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidResolver.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidResolver.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,6 +21,8 @@ package org.eclipse.tractusx.ssi.lib.did.resolver; +import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; @@ -34,8 +36,9 @@ public interface DidResolver { * @return the resolved DID document or null if the provided {@code did} could not be * resolved * @throws DidResolverException if the DID is invalid or cannot be resolved to a DID document + * @throws DidParseException */ - DidDocument resolve(Did did) throws DidResolverException; + DidDocument resolve(Did did) throws DidResolverException, DidParseException; /** * Indicates whether the resolver is capable of resolving the provided {@code did} to a DID diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidUniResolver.java b/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidUniResolver.java index d139dcbd..55174295 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidUniResolver.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidUniResolver.java @@ -27,6 +27,8 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; +import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; @@ -70,7 +72,7 @@ public DidUniResolver(HttpClient client, URI uniResolverEndpoint) } @Override - public DidDocument resolve(Did did) throws DidResolverException { + public DidDocument resolve(Did did) throws DidResolverException, DidParseException { URI requestUri = uniResolverEndpoint.resolve(uniResolverResolvePath).resolve("./" + did.toString()); final HttpRequest request = HttpRequest.newBuilder().uri(requestUri).GET().build(); diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebDocumentResolver.java b/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebDocumentResolver.java deleted file mode 100644 index 55e85ed6..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebDocumentResolver.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.did.web; - -import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.nio.charset.StandardCharsets; -import java.util.Map; -import lombok.RequiredArgsConstructor; -import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolver; -import org.eclipse.tractusx.ssi.lib.did.web.util.Constants; -import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser; -import org.eclipse.tractusx.ssi.lib.exception.DidWebException; -import org.eclipse.tractusx.ssi.lib.exception.SsiException; -import org.eclipse.tractusx.ssi.lib.model.did.Did; -import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; -import org.eclipse.tractusx.ssi.lib.model.did.DidMethod; - -/** The type Did web document resolver. */ -@RequiredArgsConstructor -@Deprecated -/** - * @deprecated replaced by {@link DidWebResolver} - */ -public class DidWebDocumentResolver implements DidDocumentResolver { - - private final HttpClient client; - private final DidWebParser parser; - private final boolean enforceHttps; - - @Override - public DidMethod getSupportedMethod() { - return Constants.DID_WEB_METHOD; - } - - @Override - public DidDocument resolve(Did did) { - if (!did.getMethod().equals(Constants.DID_WEB_METHOD)) { - throw new SsiException( - "Handler can only handle the following methods:" + Constants.DID_WEB_METHOD); - } - - final URI uri = parser.parse(did, enforceHttps); - - final HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build(); - - try { - final HttpResponse response = - client.send(request, HttpResponse.BodyHandlers.ofString()); - - if (response.statusCode() < 200 || response.statusCode() > 299) { - throw new DidWebException( - String.format( - "Unexpected response when resolving did document [Code=%s, Payload=%s]", - response.statusCode(), response.body())); - } - if (response.body() == null) { - throw new DidWebException("Empty response body"); - } - - final byte[] body = response.body().getBytes(StandardCharsets.UTF_8); - - // TODO Fix this - final ObjectMapper mapper = new ObjectMapper(); - final Map json = mapper.readValue(body, Map.class); - - return new DidDocument(json); - } catch (IOException | InterruptedException e) { - throw new RuntimeException(e); - } - } -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebResolver.java b/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebResolver.java index 403c4724..e268e75a 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebResolver.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebResolver.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -30,9 +30,10 @@ import java.util.Map; import lombok.RequiredArgsConstructor; import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolver; -import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolverException; import org.eclipse.tractusx.ssi.lib.did.web.util.Constants; import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser; +import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; @@ -51,7 +52,7 @@ public boolean isResolvable(Did did) { @SuppressWarnings("unchecked") @Override - public DidDocument resolve(Did did) throws DidResolverException { + public DidDocument resolve(Did did) throws DidResolverException, DidParseException { if (!did.getMethod().equals(Constants.DID_WEB_METHOD)) { throw new DidResolverException( String.format( @@ -62,7 +63,6 @@ public DidDocument resolve(Did did) throws DidResolverException { final URI uri = parser.parse(did, enforceHttps); final HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build(); - try { final HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); @@ -83,6 +83,7 @@ public DidDocument resolve(Did did) throws DidResolverException { final Map json = mapper.readValue(body, Map.class); return new DidDocument(json); + } catch (Exception e) { throw new DidResolverException( String.format("Unexpected exception: %s", e.getClass().getName()), e); diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/util/DidWebParser.java b/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/util/DidWebParser.java index f15c9adf..c302a2f7 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/util/DidWebParser.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/util/DidWebParser.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -24,8 +24,7 @@ import java.net.URI; import java.net.URISyntaxException; import lombok.RequiredArgsConstructor; -import lombok.SneakyThrows; -import org.eclipse.tractusx.ssi.lib.exception.DidParseException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException; import org.eclipse.tractusx.ssi.lib.model.did.Did; /** The type Did web parser. */ @@ -41,7 +40,7 @@ public class DidWebParser { * @param did the did * @return the uri */ - public URI parse(Did did) { + public URI parse(Did did) throws DidParseException { return parse(did, true); } @@ -52,8 +51,7 @@ public URI parse(Did did) { * @param enforceHttps the enforce https * @return the uri */ - @SneakyThrows({URISyntaxException.class}) - public URI parse(Did did, boolean enforceHttps) { + public URI parse(Did did, boolean enforceHttps) throws DidParseException { if (!did.getMethod().equals(Constants.DID_WEB_METHOD)) { throw new DidParseException( "Did Method not allowed: " + did.getMethod() + ". Expected did:web"); @@ -75,6 +73,10 @@ public URI parse(Did did, boolean enforceHttps) { didUrl = didUrl + WELL_KNOWN_DID_JSON; } - return new URI(didUrl); + try { + return new URI(didUrl); + } catch (URISyntaxException e) { + throw new DidParseException(e.getMessage()); + } } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/util/Ed25519PublicKeyParser.java b/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/util/Ed25519PublicKeyParser.java index 845916ad..d00d049e 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/util/Ed25519PublicKeyParser.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/did/web/util/Ed25519PublicKeyParser.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,9 +21,10 @@ package org.eclipse.tractusx.ssi.lib.did.web.util; +import java.io.IOException; import java.io.StringReader; -import lombok.SneakyThrows; import org.bouncycastle.util.io.pem.PemReader; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPublicKeyFormatException; import org.eclipse.tractusx.ssi.lib.model.MultibaseString; import org.eclipse.tractusx.ssi.lib.model.base.MultibaseFactory; @@ -36,16 +37,24 @@ public class Ed25519PublicKeyParser { * * @param publicKey the public key * @return public key as multibase string + * @throws InvalidPublicKeyFormatException */ - public static MultibaseString parsePublicKey(String publicKey) { - final byte[] publicKey64 = readPublicKey(publicKey); + public static MultibaseString parsePublicKey(String publicKey) + throws InvalidPublicKeyFormatException { + byte[] publicKey64 = null; + + publicKey64 = readPublicKey(publicKey); + return MultibaseFactory.create(publicKey64); } - @SneakyThrows - private static byte[] readPublicKey(String publicKey) { + private static byte[] readPublicKey(String publicKey) throws InvalidPublicKeyFormatException { PemReader pemReader = new PemReader(new StringReader(publicKey)); - return pemReader.readPemObject().getContent(); + try { + return pemReader.readPemObject().getContent(); + } catch (IOException e) { + throw new InvalidPublicKeyFormatException(e.getMessage()); + } } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/DidDocumentResolverNotRegisteredException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/DidDocumentResolverNotRegisteredException.java deleted file mode 100644 index 906df501..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/DidDocumentResolverNotRegisteredException.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.exception; - -import org.eclipse.tractusx.ssi.lib.model.did.DidMethod; - -/** The type Did document resolver not registered exception. */ -public class DidDocumentResolverNotRegisteredException extends Exception { - - /** - * Instantiates a new Did document resolver not registered exception. - * - * @param didMethod the did method - */ - public DidDocumentResolverNotRegisteredException(DidMethod didMethod) { - super(String.format("No DID document resolver registered for DID method '%s'", didMethod)); - } -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/InvalidePrivateKeyFormat.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/InvalidePrivateKeyFormat.java deleted file mode 100644 index 16a4f9c6..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/InvalidePrivateKeyFormat.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.exception; - -/** The type Invalide private key format. */ -public class InvalidePrivateKeyFormat extends Exception { - /** - * Instantiates a new Invalide private key format. - * - * @param correctLength the correct length - * @param providedLength the provided length - */ - public InvalidePrivateKeyFormat(int correctLength, int providedLength) { - super( - String.format( - "Invalide Private Key Format, this key should have '%s' as lenght but we got %s", - correctLength, providedLength)); - } - - /** - * Instantiates a new Invalide private key format. - * - * @param cause the cause - */ - public InvalidePrivateKeyFormat(Throwable cause) {} -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/InvalidePublicKeyFormat.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/InvalidePublicKeyFormat.java deleted file mode 100644 index 7be04f24..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/InvalidePublicKeyFormat.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.exception; - -/** The type Invalide public key format. */ -public class InvalidePublicKeyFormat extends Exception { - - /** - * Instantiates a new Invalide public key format. - * - * @param correctLength the correct length - * @param providedLength the provided length - */ - public InvalidePublicKeyFormat(int correctLength, int providedLength) { - super( - String.format( - "Invalide Publice Key Format, this key should have '%s' as lenght but we got %s", - correctLength, providedLength)); - } - - /** - * Instantiates a new Invalide public key format. - * - * @param cause the cause - */ - public InvalidePublicKeyFormat(Throwable cause) {} -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtExpiredException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtExpiredException.java deleted file mode 100644 index aea6b0c8..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtExpiredException.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.exception; - -import java.util.Date; - -/** The type Jwt expired exception. */ -public class JwtExpiredException extends JwtException { - - /** - * Instantiates a new Jwt expired exception. - * - * @param expiryDate the expiry date - */ - public JwtExpiredException(Date expiryDate) { - super("JWT expired at " + expiryDate); - } -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtSignatureCheckFailedException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtSignatureCheckFailedException.java deleted file mode 100644 index e698e664..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtSignatureCheckFailedException.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.exception; - -import java.net.URI; -import lombok.Getter; -import org.eclipse.tractusx.ssi.lib.model.did.Did; - -/** The type Jwt signature check failed exception. */ -@Getter -public class JwtSignatureCheckFailedException extends JwtException { - - /** Issuer did */ - private final Did issuerDid; - - /** Verification key */ - private final URI verificationKey; - - /** - * Instantiates a new Jwt signature check failed exception. - * - * @param issuerDid the issuer did - * @param verificationKey the verification key - */ - public JwtSignatureCheckFailedException(Did issuerDid, URI verificationKey) { - super( - "JWT signature check failed for issuer " - + issuerDid - + " and verification key " - + verificationKey); - this.issuerDid = issuerDid; - this.verificationKey = verificationKey; - } -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtSignatureVerificationKeyNotFoundException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtSignatureVerificationKeyNotFoundException.java deleted file mode 100644 index b3d2dcae..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtSignatureVerificationKeyNotFoundException.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.exception; - -/** The type Jwt signature verification key not found exception. */ -public class JwtSignatureVerificationKeyNotFoundException {} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/KeyGenerationException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/KeyGenerationException.java deleted file mode 100644 index e87cb316..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/KeyGenerationException.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.exception; - -/** The type Key generation exception. */ -public class KeyGenerationException extends Exception { - - /** - * Instantiates a new Key generation exception. - * - * @param cause the cause - */ - public KeyGenerationException(Throwable cause) {} -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/NoVerificationKeyFoundExcpetion.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/NoVerificationKeyFoundExcpetion.java deleted file mode 100644 index 1f18380d..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/NoVerificationKeyFoundExcpetion.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.exception; - -/** NoVerificationKeyFoundExcpetion */ -public class NoVerificationKeyFoundExcpetion extends Exception { - - /** - * Instantiates a new No verification key found excpetion. - * - * @param message the message - */ - public NoVerificationKeyFoundExcpetion(String message) { - super(message); - } -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/SSIException.class b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/SSIException.class new file mode 100644 index 00000000..0fad65c9 Binary files /dev/null and b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/SSIException.class differ diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/SsiException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/SSIException.java similarity index 73% rename from src/main/java/org/eclipse/tractusx/ssi/lib/exception/SsiException.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/exception/SSIException.java index 8a55cf30..a37f7542 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/SsiException.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/SSIException.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,49 +21,48 @@ package org.eclipse.tractusx.ssi.lib.exception; -/** The type Ssi exception. */ -public class SsiException extends RuntimeException { +/** The type SSI exception. */ +public class SSIException extends Exception { - /** Instantiates a new Ssi exception. */ - public SsiException() {} + private static final long serialVersionUID = 1L; /** - * Instantiates a new Ssi exception. + * Instantiates a new SSI exception. * * @param message the message */ - public SsiException(String message) { + protected SSIException(String message) { super(message); } /** - * Instantiates a new Ssi exception. + * Instantiates a new SSI exception. * * @param message the message * @param cause the cause */ - public SsiException(String message, Throwable cause) { + protected SSIException(String message, Throwable cause) { super(message, cause); } /** - * Instantiates a new Ssi exception. + * Instantiates a new SSI exception. * * @param cause the cause */ - public SsiException(Throwable cause) { + protected SSIException(Throwable cause) { super(cause); } /** - * Instantiates a new Ssi exception. + * Instantiates a new SSI exception. * * @param message the message * @param cause the cause * @param enableSuppression the enable suppression * @param writableStackTrace the writable stack trace */ - public SsiException( + protected SSIException( String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/UnsupportedDidMethodException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/UnsupportedDidMethodException.java deleted file mode 100644 index 75a6853b..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/UnsupportedDidMethodException.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.exception; - -import org.eclipse.tractusx.ssi.lib.model.did.Did; - -/** The type Unsupported did method exception. */ -public class UnsupportedDidMethodException extends Exception { - /** - * Instantiates a new Unsupported did method exception. - * - * @param did the did - * @param contextMessage the context message - */ - public UnsupportedDidMethodException(Did did, String contextMessage) { - super( - String.format( - "No DID document resolver registered for DID method '%s'. %s", - did.getMethod(), contextMessage)); - } -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/UnsupportedSignatureTypeException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/UnsupportedSignatureTypeException.java deleted file mode 100644 index 64f11b23..00000000 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/UnsupportedSignatureTypeException.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.exception; - -/** The type Unsupported signature type exception. */ -public class UnsupportedSignatureTypeException extends Exception { - /** - * Instantiates a new Unsupported signature type exception. - * - * @param signatureType the signature type - */ - public UnsupportedSignatureTypeException(String signatureType) { - super("Unsupported signature type: " + signatureType); - } -} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/DidParseException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/did/DidParseException.java similarity index 55% rename from src/main/java/org/eclipse/tractusx/ssi/lib/exception/DidParseException.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/exception/did/DidParseException.java index 8f34dc98..a2cbc78d 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/DidParseException.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/did/DidParseException.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -19,19 +19,22 @@ * ******************************************************************************* */ -package org.eclipse.tractusx.ssi.lib.exception; +package org.eclipse.tractusx.ssi.lib.exception.did; + +import org.eclipse.tractusx.ssi.lib.exception.SSIException; /** The type Did parse exception. */ -public class DidParseException extends SsiException { +public class DidParseException extends SSIException { + + private static final long serialVersionUID = 1L; /** * Instantiates a new Did parse exception. * * @param message the message */ - public DidParseException(String message) { - super(message); + public DidParseException(String did) { + super(String.format("Invalid DID URL: %s, not able to parse it", did)); } - /** * Instantiates a new Did parse exception. * @@ -41,4 +44,24 @@ public DidParseException(String message) { public DidParseException(String message, Throwable cause) { super(message, cause); } + /** + * Instantiates a new Did parse exception. + * + * @param cause the cause + */ + public DidParseException(Throwable cause) { + super(cause); + } + /** + * Instantiates a new Did parse exception. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + public DidParseException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidResolverException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/did/DidResolverException.java similarity index 89% rename from src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidResolverException.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/exception/did/DidResolverException.java index 9be80912..b453e210 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidResolverException.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/did/DidResolverException.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -19,16 +19,14 @@ * ******************************************************************************* */ -package org.eclipse.tractusx.ssi.lib.did.resolver; +package org.eclipse.tractusx.ssi.lib.exception.did; + +import org.eclipse.tractusx.ssi.lib.exception.SSIException; /** The type Did resolver exception. */ -public class DidResolverException extends Exception { +public class DidResolverException extends SSIException { private static final long serialVersionUID = 1L; - - /** Instantiates a new Did resolver exception. */ - public DidResolverException() {} - /** * Instantiates a new Did resolver exception. * diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/InvalidJsonLdException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/json/InvalidJsonLdException.java similarity index 66% rename from src/main/java/org/eclipse/tractusx/ssi/lib/exception/InvalidJsonLdException.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/exception/json/InvalidJsonLdException.java index 093a21da..8f640adb 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/InvalidJsonLdException.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/json/InvalidJsonLdException.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -19,10 +19,13 @@ * ******************************************************************************* */ -package org.eclipse.tractusx.ssi.lib.exception; +package org.eclipse.tractusx.ssi.lib.exception.json; + +import org.eclipse.tractusx.ssi.lib.exception.SSIException; /** The type Invalid json ld exception. */ -public class InvalidJsonLdException extends Exception { +public class InvalidJsonLdException extends SSIException { + private static final long serialVersionUID = 1L; /** * Instantiates a new Invalid json ld exception. * @@ -50,4 +53,17 @@ public InvalidJsonLdException(String message, Throwable cause) { public InvalidJsonLdException(Throwable cause) { super(cause); } + + /** + * Instantiates a new Invalid json ld exception. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + public InvalidJsonLdException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/json/TransformJsonLdException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/json/TransformJsonLdException.java new file mode 100644 index 00000000..5747cc09 --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/json/TransformJsonLdException.java @@ -0,0 +1,70 @@ +/* + * ****************************************************************************** + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ******************************************************************************* + */ + +package org.eclipse.tractusx.ssi.lib.exception.json; + +import org.eclipse.tractusx.ssi.lib.exception.SSIException; + +/** The type tranform json LD exception. */ +public class TransformJsonLdException extends SSIException { + + private static final long serialVersionUID = 1L; + /** + * Instantiates a new tranform json LD exception. + * + * @param message the message + */ + public TransformJsonLdException(String message) { + super(message); + } + + /** + * Instantiates a new tranform json LD exception. + * + * @param message the message + * @param cause the cause + */ + public TransformJsonLdException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Instantiates a new tranform json LD exception. + * + * @param cause the cause + */ + public TransformJsonLdException(Throwable cause) { + super(cause); + } + + /** + * Instantiates a new tranform json LD exception. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + public TransformJsonLdException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/key/InvalidPrivateKeyFormatException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/key/InvalidPrivateKeyFormatException.java new file mode 100644 index 00000000..84f31771 --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/key/InvalidPrivateKeyFormatException.java @@ -0,0 +1,80 @@ +/* + * ****************************************************************************** + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ******************************************************************************* + */ + +package org.eclipse.tractusx.ssi.lib.exception.key; + +/** The type Invalide private key format. */ +public class InvalidPrivateKeyFormatException extends KeyGenerationException { + private static final long serialVersionUID = 1L; + /** + * Instantiates a new Invalid private key format. + * + * @param correctLength the correct length + * @param providedLength the provided length + */ + public InvalidPrivateKeyFormatException(int correctLength, int providedLength) { + super( + String.format( + "Invalide Private Key Format, this key should have '%s' as lenght but we got %s", + correctLength, providedLength)); + } + + /** + * Instantiates a new Invalid private key format. + * + * @param message message + */ + public InvalidPrivateKeyFormatException(String message) { + super(message); + } + + /** + * Instantiates a new Invalid private key format. + * + * @param message message + * @param cuase the cause + */ + public InvalidPrivateKeyFormatException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Instantiates a new Invalid private key format. + * + * @param cuase the cause + */ + public InvalidPrivateKeyFormatException(Throwable cause) { + super(cause); + } + + /** + * Instantiates a new Invalid private key format. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + public InvalidPrivateKeyFormatException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/key/InvalidPublicKeyFormatException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/key/InvalidPublicKeyFormatException.java new file mode 100644 index 00000000..ca109141 --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/key/InvalidPublicKeyFormatException.java @@ -0,0 +1,81 @@ +/* + * ****************************************************************************** + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ******************************************************************************* + */ + +package org.eclipse.tractusx.ssi.lib.exception.key; + +/** The type Invalid public key format. */ +public class InvalidPublicKeyFormatException extends KeyGenerationException { + + private static final long serialVersionUID = 1L; + /** + * Instantiates a new Invalide public key format. + * + * @param correctLength the correct length + * @param providedLength the provided length + */ + public InvalidPublicKeyFormatException(int correctLength, int providedLength) { + super( + String.format( + "Invalide Publice Key Format, this key should have '%s' as lenght but we got %s", + correctLength, providedLength)); + } + + /** + * Instantiates a new invalide public key format exception. + * + * @param message the message + */ + public InvalidPublicKeyFormatException(String message) { + super(message); + } + + /** + * Instantiates a new invalide public key format exception. + * + * @param message the message + * @param cause the cause + */ + public InvalidPublicKeyFormatException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Instantiates a new invalide public key format exception. + * + * @param cause the cause + */ + public InvalidPublicKeyFormatException(Throwable cause) { + super(cause); + } + + /** + * Instantiates a new invalide public key format exception. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + public InvalidPublicKeyFormatException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/key/KeyGenerationException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/key/KeyGenerationException.java new file mode 100644 index 00000000..cb9257af --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/key/KeyGenerationException.java @@ -0,0 +1,71 @@ +/* + * ****************************************************************************** + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ******************************************************************************* + */ + +package org.eclipse.tractusx.ssi.lib.exception.key; + +import org.eclipse.tractusx.ssi.lib.exception.SSIException; + +/** The type Key Generation Exception */ +public class KeyGenerationException extends SSIException { + + private static final long serialVersionUID = 1L; + + /** + * Instantiates a new key generation exception. + * + * @param message the message + */ + public KeyGenerationException(String message) { + super(message); + } + + /** + * Instantiates a new key generation exception. + * + * @param message the message + * @param cause the cause + */ + public KeyGenerationException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Instantiates a new key generation exception. + * + * @param cause the cause + */ + public KeyGenerationException(Throwable cause) { + super(cause); + } + + /** + * Instantiates a key generation exception. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + public KeyGenerationException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/DidWebException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/key/KeyTransformationException.java similarity index 66% rename from src/main/java/org/eclipse/tractusx/ssi/lib/exception/DidWebException.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/exception/key/KeyTransformationException.java index 3284555c..af6b17d4 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/DidWebException.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/key/KeyTransformationException.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -19,51 +19,50 @@ * ******************************************************************************* */ -package org.eclipse.tractusx.ssi.lib.exception; +package org.eclipse.tractusx.ssi.lib.exception.key; -/** The type Did web exception. */ -public class DidWebException extends RuntimeException { +import org.eclipse.tractusx.ssi.lib.exception.SSIException; - /** Instantiates a new Did web exception. */ - public DidWebException() {} +public class KeyTransformationException extends SSIException { + private static final long serialVersionUID = 1L; /** - * Instantiates a new Did web exception. + * Instantiates a new key trnasformation exception. * * @param message the message */ - public DidWebException(String message) { + public KeyTransformationException(String message) { super(message); } /** - * Instantiates a new Did web exception. + * Instantiates a new key trnasformation exception. * * @param message the message * @param cause the cause */ - public DidWebException(String message, Throwable cause) { + public KeyTransformationException(String message, Throwable cause) { super(message, cause); } /** - * Instantiates a new Did web exception. + * Instantiates a new key trnasformation exception. * * @param cause the cause */ - public DidWebException(Throwable cause) { + public KeyTransformationException(Throwable cause) { super(cause); } /** - * Instantiates a new Did web exception. + * Instantiates a new key trnasformation exception. * * @param message the message * @param cause the cause * @param enableSuppression the enable suppression * @param writableStackTrace the writable stack trace */ - public DidWebException( + public KeyTransformationException( String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtAudienceCheckFailedException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/JwtAudienceCheckException.java similarity index 61% rename from src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtAudienceCheckFailedException.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/JwtAudienceCheckException.java index aace1b2e..2c02cdc7 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtAudienceCheckFailedException.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/JwtAudienceCheckException.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -19,23 +19,41 @@ * ******************************************************************************* */ -package org.eclipse.tractusx.ssi.lib.exception; +package org.eclipse.tractusx.ssi.lib.exception.proof; import java.util.List; /** The type Jwt audience check failed exception. */ -public class JwtAudienceCheckFailedException extends JwtException { +public class JwtAudienceCheckException extends SignatureValidationException { + private static final long serialVersionUID = 1L; /** * Instantiates a new Jwt audience check failed exception. * * @param expectedAudience the expected audience * @param actualAudience the actual audience */ - public JwtAudienceCheckFailedException(String expectedAudience, List actualAudience) { + public JwtAudienceCheckException(String expectedAudience, List actualAudience) { super( "JWT audience check failed. Expected audience: " + expectedAudience + ", actual audience: " + String.join(", ", actualAudience)); } + + public JwtAudienceCheckException(String message) { + super(message); + } + + public JwtAudienceCheckException(String message, Throwable cause) { + super(message, cause); + } + + public JwtAudienceCheckException(Throwable cause) { + super(cause); + } + + public JwtAudienceCheckException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/JwtExpiredException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/JwtExpiredException.java new file mode 100644 index 00000000..53cdd0a2 --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/JwtExpiredException.java @@ -0,0 +1,78 @@ +/* + * ****************************************************************************** + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ******************************************************************************* + */ + +package org.eclipse.tractusx.ssi.lib.exception.proof; + +import java.util.Date; + +/** The type Jwt expired exception. */ +public class JwtExpiredException extends SignatureValidationException { + private static final long serialVersionUID = 1L; + /** + * Instantiates a new Jwt expired exception. + * + * @param expiryDate the expiry date + */ + public JwtExpiredException(Date expiryDate) { + super("JWT expired at " + expiryDate); + } + + /** + * Instantiates a new jwt expired exception. + * + * @param message the message + */ + public JwtExpiredException(String message) { + super(message); + } + + /** + * Instantiates a new jwt expired exception. + * + * @param message the message + * @param cause the cause + */ + public JwtExpiredException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Instantiates a new jwt expired exception. + * + * @param cause the cause + */ + public JwtExpiredException(Throwable cause) { + super(cause); + } + + /** + * Instantiates a new jwt expired exception. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + public JwtExpiredException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/NoVerificationKeyFoundException.java similarity index 57% rename from src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtException.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/NoVerificationKeyFoundException.java index 90eab35a..8e26aaa4 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/JwtException.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/NoVerificationKeyFoundException.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -19,36 +19,26 @@ * ******************************************************************************* */ -package org.eclipse.tractusx.ssi.lib.exception; +package org.eclipse.tractusx.ssi.lib.exception.proof; -/** The type Jwt exception. */ -public class JwtException extends Exception { +/** NoVerificationKeyFoundExcpetion */ +public class NoVerificationKeyFoundException extends SignatureVerificationException { + private static final long serialVersionUID = 1L; - /** - * Instantiates a new Jwt exception. - * - * @param message the message - */ - public JwtException(String message) { + public NoVerificationKeyFoundException(String message) { super(message); } - /** - * Instantiates a new Jwt exception. - * - * @param message the message - * @param cause the cause - */ - public JwtException(String message, Throwable cause) { + public NoVerificationKeyFoundException(String message, Throwable cause) { super(message, cause); } - /** - * Instantiates a new Jwt exception. - * - * @param cause the cause - */ - public JwtException(Throwable cause) { + public NoVerificationKeyFoundException(Throwable cause) { super(cause); } + + public NoVerificationKeyFoundException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureGenerateFailedException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureGenerateFailedException.java new file mode 100644 index 00000000..c82e947d --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureGenerateFailedException.java @@ -0,0 +1,70 @@ +/* + * ****************************************************************************** + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ******************************************************************************* + */ + +package org.eclipse.tractusx.ssi.lib.exception.proof; + +import org.eclipse.tractusx.ssi.lib.exception.SSIException; + +/** The type signature check failed exception. */ +public class SignatureGenerateFailedException extends SSIException { + private static final long serialVersionUID = 1L; + + /** + * Instantiates a new signature generate failed exception. + * + * @param message the message + */ + public SignatureGenerateFailedException(String message) { + super(message); + } + + /** + * Instantiates a new signature generate failed exception. + * + * @param message the message + * @param cause the cause + */ + public SignatureGenerateFailedException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Instantiates a new signature generate failed exception. + * + * @param cause the cause + */ + public SignatureGenerateFailedException(Throwable cause) { + super(cause); + } + + /** + * Instantiates a new signature generate failed exception. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + public SignatureGenerateFailedException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureParseException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureParseException.java new file mode 100644 index 00000000..f747bbb5 --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureParseException.java @@ -0,0 +1,69 @@ +/* + * ****************************************************************************** + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ******************************************************************************* + */ + +package org.eclipse.tractusx.ssi.lib.exception.proof; + +import org.eclipse.tractusx.ssi.lib.exception.SSIException; + +/** The type signature parse exception. */ +public class SignatureParseException extends SSIException { + private static final long serialVersionUID = 1L; + /** + * Instantiates a new signature parse exception. + * + * @param message the message + */ + public SignatureParseException(String message) { + super(message); + } + + /** + * Instantiatesa a new signature parse exception. + * + * @param message the message + * @param cause the cause + */ + public SignatureParseException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Instantiates a new signature parse exception. + * + * @param cause the cause + */ + public SignatureParseException(Throwable cause) { + super(cause); + } + + /** + * Instantiates a new signature parse exception. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + public SignatureParseException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureValidationException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureValidationException.java new file mode 100644 index 00000000..f13ab412 --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureValidationException.java @@ -0,0 +1,70 @@ +/* + * ****************************************************************************** + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ******************************************************************************* + */ + +package org.eclipse.tractusx.ssi.lib.exception.proof; + +import org.eclipse.tractusx.ssi.lib.exception.SSIException; + +/** The type Signature Validation exception. */ +public class SignatureValidationException extends SSIException { + + private static final long serialVersionUID = 1L; + /** + * Instantiates a new signature verification exception. + * + * @param message the message + */ + public SignatureValidationException(String message) { + super(message); + } + + /** + * Instantiates a new signature validation exception. + * + * @param message the message + * @param cause the cause + */ + protected SignatureValidationException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Instantiates a new signature verification exception. + * + * @param cause the cause + */ + protected SignatureValidationException(Throwable cause) { + super(cause); + } + + /** + * Instantiates a new signature verification exception. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + protected SignatureValidationException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureVerificationException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureVerificationException.java new file mode 100644 index 00000000..ce4f98bd --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureVerificationException.java @@ -0,0 +1,70 @@ +/* + * ****************************************************************************** + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ******************************************************************************* + */ + +package org.eclipse.tractusx.ssi.lib.exception.proof; + +import org.eclipse.tractusx.ssi.lib.exception.SSIException; + +/** The type Signature Verification Failed exception. */ +public class SignatureVerificationException extends SSIException { + + private static final long serialVersionUID = 1L; + /** + * Instantiates a new signature verification failed exception. + * + * @param message the message + */ + public SignatureVerificationException(String message) { + super(message); + } + + /** + * Instantiates a new signature verification failed exception. + * + * @param message the message + * @param cause the cause + */ + protected SignatureVerificationException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Instantiates a new signature verification failed exception. + * + * @param cause the cause + */ + protected SignatureVerificationException(Throwable cause) { + super(cause); + } + + /** + * Instantiates a new signature verification failed exception. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + protected SignatureVerificationException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureVerificationFailedException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureVerificationFailedException.java new file mode 100644 index 00000000..dfc677ed --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/SignatureVerificationFailedException.java @@ -0,0 +1,70 @@ +/* + * ****************************************************************************** + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ******************************************************************************* + */ + +package org.eclipse.tractusx.ssi.lib.exception.proof; + +import org.eclipse.tractusx.ssi.lib.exception.SSIException; + +/** The type Signature Verification Failed exception. */ +public class SignatureVerificationFailedException extends SSIException { + + private static final long serialVersionUID = 1L; + /** + * Instantiates a new signature verification failed exception. + * + * @param message the message + */ + public SignatureVerificationFailedException(String message) { + super(message); + } + + /** + * Instantiates a new signature verification failed exception. + * + * @param message the message + * @param cause the cause + */ + protected SignatureVerificationFailedException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Instantiates a new signature verification failed exception. + * + * @param cause the cause + */ + protected SignatureVerificationFailedException(Throwable cause) { + super(cause); + } + + /** + * Instantiates a new signature verification failed exception. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + protected SignatureVerificationFailedException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/UnsupportedSignatureTypeException.class b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/UnsupportedSignatureTypeException.class new file mode 100644 index 00000000..1eda9c0a Binary files /dev/null and b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/UnsupportedSignatureTypeException.class differ diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/UnsupportedSignatureTypeException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/UnsupportedSignatureTypeException.java new file mode 100644 index 00000000..c57f063f --- /dev/null +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/UnsupportedSignatureTypeException.java @@ -0,0 +1,69 @@ +/* + * ****************************************************************************** + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ******************************************************************************* + */ + +package org.eclipse.tractusx.ssi.lib.exception.proof; + +/** The type Unsupported signature type exception. */ +public class UnsupportedSignatureTypeException extends SignatureVerificationException { + + private static final long serialVersionUID = 1L; + + /** + * Instantiates a new unsupported signature type exception. + * + * @param signatureType the signature type + */ + public UnsupportedSignatureTypeException(String signatureType) { + super("Unsupported signature type: " + signatureType); + } + + /** + * Instantiates a unsupported signature type exception. + * + * @param message the message + * @param cause the cause + */ + public UnsupportedSignatureTypeException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Instantiates a unsupported signature type exception. + * + * @param cause the cause + */ + public UnsupportedSignatureTypeException(Throwable cause) { + super(cause); + } + + /** + * Instantiates a unsupported signature type exception. + * + * @param message the message + * @param cause the cause + * @param enableSuppression the enable suppression + * @param writableStackTrace the writable stack trace + */ + public UnsupportedSignatureTypeException( + String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/UnsupportedVerificationMethodException.java b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/UnsupportedVerificationMethodException.java similarity index 84% rename from src/main/java/org/eclipse/tractusx/ssi/lib/exception/UnsupportedVerificationMethodException.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/UnsupportedVerificationMethodException.java index 229dbcb2..41137b31 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/exception/UnsupportedVerificationMethodException.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/exception/proof/UnsupportedVerificationMethodException.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -19,13 +19,18 @@ * ******************************************************************************* */ -package org.eclipse.tractusx.ssi.lib.exception; +package org.eclipse.tractusx.ssi.lib.exception.proof; import lombok.Getter; import org.eclipse.tractusx.ssi.lib.model.did.VerificationMethod; /** The type Unsupported verification method exception. */ -public class UnsupportedVerificationMethodException extends RuntimeException { +public class UnsupportedVerificationMethodException extends SignatureVerificationException { + + private static final long serialVersionUID = 1L; + + /** The verification method */ + @Getter private final VerificationMethod method; /** * Instantiates a new Unsupported verification method exception. @@ -39,7 +44,4 @@ public UnsupportedVerificationMethodException(VerificationMethod method, String "Unsupported verification method: %s. %s", method.getClass().getName(), message)); this.method = method; } - - /** The verification method */ - @Getter private final VerificationMethod method; } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/jwt/SignedJwtValidator.java b/src/main/java/org/eclipse/tractusx/ssi/lib/jwt/SignedJwtValidator.java index 83820a08..161610bd 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/jwt/SignedJwtValidator.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/jwt/SignedJwtValidator.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -22,41 +22,40 @@ package org.eclipse.tractusx.ssi.lib.jwt; import com.nimbusds.jwt.SignedJWT; +import java.text.ParseException; import java.util.Date; import java.util.List; -import lombok.SneakyThrows; -import org.eclipse.tractusx.ssi.lib.exception.JwtAudienceCheckFailedException; -import org.eclipse.tractusx.ssi.lib.exception.JwtExpiredException; +import org.eclipse.tractusx.ssi.lib.exception.proof.JwtAudienceCheckException; +import org.eclipse.tractusx.ssi.lib.exception.proof.JwtExpiredException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureParseException; /** The type Signed jwt validator. */ public class SignedJwtValidator { - /** - * Validate date. - * - * @param jwt the jwt - */ - @SneakyThrows - public void validateDate(SignedJWT jwt) { - Date expiryDate = jwt.getJWTClaimsSet().getExpirationTime(); + public void validateDate(SignedJWT jwt) throws JwtExpiredException, SignatureParseException { + Date expiryDate; + try { + expiryDate = jwt.getJWTClaimsSet().getExpirationTime(); + } catch (ParseException e) { + throw new SignatureParseException(e.getMessage()); + } boolean isExpired = expiryDate.before(new Date()); // Todo add Timezone if (isExpired) { throw new JwtExpiredException(expiryDate); } } - /** - * Validate audiences. - * - * @param jwt the jwt - * @param expectedAudience the expected audience - */ - @SneakyThrows - public void validateAudiences(SignedJWT jwt, String expectedAudience) { - List audiences = jwt.getJWTClaimsSet().getAudience(); + public void validateAudiences(SignedJWT jwt, String expectedAudience) + throws SignatureParseException, JwtAudienceCheckException { + List audiences; + try { + audiences = jwt.getJWTClaimsSet().getAudience(); + } catch (ParseException e) { + throw new SignatureParseException(e.getMessage()); + } boolean isValidAudience = audiences.stream().anyMatch(x -> x.equals(expectedAudience)); if (!isValidAudience) { - throw new JwtAudienceCheckFailedException(expectedAudience, audiences); + throw new JwtAudienceCheckException(expectedAudience, audiences); } } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/jwt/SignedJwtVerifier.java b/src/main/java/org/eclipse/tractusx/ssi/lib/jwt/SignedJwtVerifier.java index e083525b..c2e45112 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/jwt/SignedJwtVerifier.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/jwt/SignedJwtVerifier.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -28,16 +28,17 @@ import com.nimbusds.jose.util.Base64URL; import com.nimbusds.jwt.JWTClaimsSet; import com.nimbusds.jwt.SignedJWT; +import java.security.SignatureException; import java.text.ParseException; import java.util.List; import lombok.RequiredArgsConstructor; -import lombok.SneakyThrows; import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters; import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolver; -import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolverException; -import org.eclipse.tractusx.ssi.lib.exception.DidDocumentResolverNotRegisteredException; -import org.eclipse.tractusx.ssi.lib.exception.JwtException; -import org.eclipse.tractusx.ssi.lib.exception.UnsupportedVerificationMethodException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureParseException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureVerificationException; +import org.eclipse.tractusx.ssi.lib.exception.proof.UnsupportedVerificationMethodException; import org.eclipse.tractusx.ssi.lib.model.MultibaseString; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; @@ -60,19 +61,22 @@ public class SignedJwtVerifier { * * @param jwt a {@link SignedJWT} that was sent by the claiming party. * @return true if verified, false otherwise - * @throws JwtException the jwt exception - * @throws DidDocumentResolverNotRegisteredException the did document resolver not registered - * exception + * @throws DidParseException + * @throws SignatureException + * @throws DidResolverException + * @throws SignatureVerificationException + * @throws UnsupportedVerificationMethodException + * @throws SignatureParseException */ - @SneakyThrows({JOSEException.class, DidResolverException.class}) public boolean verify(SignedJWT jwt) - throws JwtException, DidDocumentResolverNotRegisteredException { + throws DidParseException, DidResolverException, SignatureVerificationException, + UnsupportedVerificationMethodException, SignatureParseException { JWTClaimsSet jwtClaimsSet; try { jwtClaimsSet = jwt.getJWTClaimsSet(); } catch (ParseException e) { - throw new JwtException(e); + throw new SignatureParseException(e.getMessage()); } final String issuer = jwtClaimsSet.getIssuer(); @@ -89,11 +93,16 @@ public boolean verify(SignedJWT jwt) final String kty = method.getPublicKeyJwk().getKty(); final String crv = method.getPublicKeyJwk().getCrv(); final String x = method.getPublicKeyJwk().getX(); + if (kty.equals("OKP") && crv.equals("Ed25519")) { final OctetKeyPair keyPair = new OctetKeyPair.Builder(Curve.Ed25519, Base64URL.from(x)).build(); - if (jwt.verify(new Ed25519Verifier(keyPair))) { - return true; + try { + if (jwt.verify(new Ed25519Verifier(keyPair))) { + return true; + } + } catch (JOSEException e) { + throw new SignatureVerificationException(e.getMessage()); } } else { throw new UnsupportedVerificationMethodException( @@ -109,8 +118,12 @@ public boolean verify(SignedJWT jwt) Curve.Ed25519, Base64URL.encode(publicKeyParameters.getEncoded())) .build(); - if (jwt.verify(new Ed25519Verifier(keyPair))) { - return true; + try { + if (jwt.verify(new Ed25519Verifier(keyPair))) { + return true; + } + } catch (JOSEException e) { + throw new SignatureVerificationException(e.getMessage()); } } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/model/RemoteDocumentLoader.java b/src/main/java/org/eclipse/tractusx/ssi/lib/model/RemoteDocumentLoader.java index a8be25d8..927854d7 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/model/RemoteDocumentLoader.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/model/RemoteDocumentLoader.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -44,6 +44,7 @@ /** The type Remote document loader. */ public class RemoteDocumentLoader implements DocumentLoader { + private static int CACHE_PERIOD_IN_DAYS = 1; private static DocumentLoader DEFAULT_HTTP_LOADER; private static DocumentLoader DEFAULT_FILE_LOADER; @Getter private DocumentLoader httpLoader; @@ -57,7 +58,7 @@ public class RemoteDocumentLoader implements DocumentLoader { @Getter @Setter private Cache remoteCache = - Caffeine.newBuilder().expireAfterWrite(Duration.ofDays(1)).build(); + Caffeine.newBuilder().expireAfterWrite(Duration.ofDays(CACHE_PERIOD_IN_DAYS)).build(); @Getter @Setter private List httpContexts = new ArrayList(); @Getter @Setter private List httpsContexts = new ArrayList(); diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/model/did/DidParser.java b/src/main/java/org/eclipse/tractusx/ssi/lib/model/did/DidParser.java index 7f01f88e..189a2629 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/model/did/DidParser.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/model/did/DidParser.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -26,18 +26,17 @@ import java.util.List; import java.util.Objects; import java.util.stream.Collectors; -import org.eclipse.tractusx.ssi.lib.exception.DidParseException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException; /** The type Did parser. */ public class DidParser { - /** - * Parse did. + * Parse did from URI. * * @param uri the uri * @return the did */ - public static Did parse(URI uri) { + public static Did parse(URI uri) throws DidParseException { Objects.requireNonNull(uri); if (!uri.getScheme().equals("did")) { @@ -66,12 +65,12 @@ public static Did parse(URI uri) { } /** - * Parse did. + * Parse did from String. * - * @param did the did + * @param did the did String * @return the did */ - public static Did parse(String did) { + public static Did parse(String did) throws DidParseException { Objects.requireNonNull(did); final URI uri; diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/model/proof/ed21559/Ed25519Signature2020.java b/src/main/java/org/eclipse/tractusx/ssi/lib/model/proof/ed25519/Ed25519Signature2020.java similarity index 96% rename from src/main/java/org/eclipse/tractusx/ssi/lib/model/proof/ed21559/Ed25519Signature2020.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/model/proof/ed25519/Ed25519Signature2020.java index 08ed63b2..4c45eb53 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/model/proof/ed21559/Ed25519Signature2020.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/model/proof/ed25519/Ed25519Signature2020.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -19,7 +19,7 @@ * ******************************************************************************* */ -package org.eclipse.tractusx.ssi.lib.model.proof.ed21559; +package org.eclipse.tractusx.ssi.lib.model.proof.ed25519; import java.net.URI; import java.time.Instant; diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/model/proof/ed21559/Ed25519Signature2020Builder.java b/src/main/java/org/eclipse/tractusx/ssi/lib/model/proof/ed25519/Ed25519Signature2020Builder.java similarity index 96% rename from src/main/java/org/eclipse/tractusx/ssi/lib/model/proof/ed21559/Ed25519Signature2020Builder.java rename to src/main/java/org/eclipse/tractusx/ssi/lib/model/proof/ed25519/Ed25519Signature2020Builder.java index e2a6155c..dc20ba47 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/model/proof/ed21559/Ed25519Signature2020Builder.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/model/proof/ed25519/Ed25519Signature2020Builder.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -19,7 +19,7 @@ * ******************************************************************************* */ -package org.eclipse.tractusx.ssi.lib.model.proof.ed21559; +package org.eclipse.tractusx.ssi.lib.model.proof.ed25519; import java.net.URI; import java.time.Instant; diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/ISigner.java b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/ISigner.java index 3505e281..c6d26170 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/ISigner.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/ISigner.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -22,8 +22,8 @@ package org.eclipse.tractusx.ssi.lib.proof; import org.eclipse.tractusx.ssi.lib.crypt.IPrivateKey; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePrivateKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.SsiException; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPrivateKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureGenerateFailedException; import org.eclipse.tractusx.ssi.lib.proof.hash.HashedLinkedData; /** The interface Signer. */ @@ -38,5 +38,5 @@ public interface ISigner { * @throws InvalidePrivateKeyFormat the invalide private key format */ public byte[] sign(HashedLinkedData hashedLinkedData, IPrivateKey privateKey) - throws SsiException, InvalidePrivateKeyFormat; + throws InvalidPrivateKeyFormatException, SignatureGenerateFailedException; } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/IVerifier.java b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/IVerifier.java index ce0e2449..e973a09f 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/IVerifier.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/IVerifier.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,10 +21,13 @@ package org.eclipse.tractusx.ssi.lib.proof; -import org.eclipse.tractusx.ssi.lib.exception.DidDocumentResolverNotRegisteredException; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePublicKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.NoVerificationKeyFoundExcpetion; -import org.eclipse.tractusx.ssi.lib.exception.UnsupportedSignatureTypeException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPublicKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.proof.NoVerificationKeyFoundException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureGenerateFailedException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureParseException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureVerificationException; +import org.eclipse.tractusx.ssi.lib.exception.proof.UnsupportedSignatureTypeException; import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; import org.eclipse.tractusx.ssi.lib.proof.hash.HashedLinkedData; @@ -36,15 +39,18 @@ public interface IVerifier { * model to get the public key of issuer. * * @param hashedLinkedData the hashed linked data - * @param verifiable the verifiable - * @return the boolean - * @throws UnsupportedSignatureTypeException the unsupported signature type exception - * @throws DidDocumentResolverNotRegisteredException the did document resolver not registered - * exception - * @throws InvalidePublicKeyFormat the invalide public key format - * @throws NoVerificationKeyFoundExcpetion the no verification key found excpetion + * @param document {@link VerifiableCredential} the verifiable + * @return boolean if verified or not + * @throws UnsupportedSignatureTypeException + * @throws SignatureParseException + * @throws InvalidPublicKeyFormatException + * @throws SignatureGenerateFailedException + * @throws SignatureVerificationException + * @throws DidParseException + * @throws NoVerificationKeyFoundException */ public boolean verify(HashedLinkedData hashedLinkedData, Verifiable verifiable) - throws UnsupportedSignatureTypeException, DidDocumentResolverNotRegisteredException, - InvalidePublicKeyFormat, NoVerificationKeyFoundExcpetion; + throws SignatureParseException, DidParseException, InvalidPublicKeyFormatException, + SignatureVerificationException, UnsupportedSignatureTypeException, + NoVerificationKeyFoundException; } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofGenerator.java b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofGenerator.java index 37b8fd58..d78d91d3 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofGenerator.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofGenerator.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -26,14 +26,15 @@ import java.time.Instant; import lombok.RequiredArgsConstructor; import org.eclipse.tractusx.ssi.lib.crypt.IPrivateKey; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePrivateKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.SsiException; -import org.eclipse.tractusx.ssi.lib.exception.UnsupportedSignatureTypeException; +import org.eclipse.tractusx.ssi.lib.exception.json.TransformJsonLdException; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPrivateKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureGenerateFailedException; +import org.eclipse.tractusx.ssi.lib.exception.proof.UnsupportedSignatureTypeException; import org.eclipse.tractusx.ssi.lib.model.MultibaseString; import org.eclipse.tractusx.ssi.lib.model.base.MultibaseFactory; import org.eclipse.tractusx.ssi.lib.model.proof.Proof; -import org.eclipse.tractusx.ssi.lib.model.proof.ed21559.Ed25519Signature2020; -import org.eclipse.tractusx.ssi.lib.model.proof.ed21559.Ed25519Signature2020Builder; +import org.eclipse.tractusx.ssi.lib.model.proof.ed25519.Ed25519Signature2020; +import org.eclipse.tractusx.ssi.lib.model.proof.ed25519.Ed25519Signature2020Builder; import org.eclipse.tractusx.ssi.lib.model.proof.jws.JWSSignature2020; import org.eclipse.tractusx.ssi.lib.model.proof.jws.JWSSignature2020Builder; import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable; @@ -57,7 +58,7 @@ public class LinkedDataProofGenerator { */ public static LinkedDataProofGenerator newInstance(SignatureType type) throws UnsupportedSignatureTypeException { - if (type == SignatureType.ED21559) { + if (type == SignatureType.ED25519) { return new LinkedDataProofGenerator( type, new LinkedDataHasher(), new LinkedDataTransformer(), new Ed25519ProofSigner()); } else if (type == SignatureType.JWS) { @@ -84,14 +85,15 @@ public static LinkedDataProofGenerator newInstance(SignatureType type) * @throws InvalidePrivateKeyFormat the invalide private key format */ public Proof createProof(Verifiable document, URI verificationMethodId, IPrivateKey privateKey) - throws SsiException, InvalidePrivateKeyFormat { + throws InvalidPrivateKeyFormatException, SignatureGenerateFailedException, + TransformJsonLdException { final TransformedLinkedData transformedData = transformer.transform(document); final HashedLinkedData hashedData = hasher.hash(transformedData); byte[] signature; signature = signer.sign(new HashedLinkedData(hashedData.getValue()), privateKey); - if (type == SignatureType.ED21559) { + if (type == SignatureType.ED25519) { final MultibaseString multibaseString = MultibaseFactory.create(signature); return new Ed25519Signature2020Builder() diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofValidation.java b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofValidation.java index 657d584b..ff88bbcd 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofValidation.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofValidation.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -22,12 +22,14 @@ package org.eclipse.tractusx.ssi.lib.proof; import java.util.logging.Logger; +// import org.eclipse.tractusx.ssi.lib.serialization.jsonLd.JsonLdValidator; +// import org.eclipse.tractusx.ssi.lib.serialization.jsonLd.JsonLdValidatorImpl; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolver; -import org.eclipse.tractusx.ssi.lib.exception.InvalidJsonLdException; -import org.eclipse.tractusx.ssi.lib.exception.UnsupportedSignatureTypeException; +import org.eclipse.tractusx.ssi.lib.exception.json.InvalidJsonLdException; +import org.eclipse.tractusx.ssi.lib.exception.proof.UnsupportedSignatureTypeException; import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable; import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable.VerifiableType; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; @@ -87,7 +89,7 @@ public boolean verify(Verifiable verifiable) { IVerifier verifier = null; if (type != null && !type.isBlank()) { - if (type.equals(SignatureType.ED21559.toString())) { + if (type.equals(SignatureType.ED25519.toString())) { verifier = new Ed25519ProofVerifier(this.didResolver); } else if (type.equals(SignatureType.JWS.toString())) { verifier = new JWSProofVerifier(this.didResolver); diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/SignatureType.java b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/SignatureType.java index 5e928f6c..15211d07 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/SignatureType.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/SignatureType.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -23,8 +23,8 @@ /** The enum Signature type. */ public enum SignatureType { - /** The Ed 21559. */ - ED21559 { + /** The Ed 25519. */ + ED25519 { public String toString() { return "Ed25519Signature2020"; } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/transform/LinkedDataTransformer.java b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/transform/LinkedDataTransformer.java index abc171bb..7a9a405d 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/transform/LinkedDataTransformer.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/transform/LinkedDataTransformer.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -33,30 +33,29 @@ import java.io.IOException; import java.io.StringWriter; import java.security.NoSuchAlgorithmException; -import lombok.SneakyThrows; import org.apache.commons.lang3.SerializationUtils; +import org.eclipse.tractusx.ssi.lib.exception.json.TransformJsonLdException; import org.eclipse.tractusx.ssi.lib.model.JsonLdObject; import org.eclipse.tractusx.ssi.lib.model.RemoteDocumentLoader; import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable; /** The type Linked data transformer. */ public class LinkedDataTransformer { - /** * Transform linked data. * * @param document the document * @return the transformed linked data */ - @SneakyThrows - public TransformedLinkedData transform(Verifiable document) { + public TransformedLinkedData transform(Verifiable document) throws TransformJsonLdException { // Make a copy and remove proof, as it is not part of the linked data var copy = (JsonLdObject) SerializationUtils.clone(document); copy.remove(Verifiable.PROOF); return this.canocliztion(copy); } - private TransformedLinkedData canocliztion(JsonLdObject document) { + private TransformedLinkedData canocliztion(JsonLdObject document) + throws TransformJsonLdException { try { RdfDataset rdfDataset = toDataset(document); @@ -68,10 +67,8 @@ private TransformedLinkedData canocliztion(JsonLdObject document) { return new TransformedLinkedData(normalized); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); + } catch (NoSuchAlgorithmException | IOException e) { + throw new TransformJsonLdException(e.getMessage()); } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/ed25519/Ed25519ProofSigner.java b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/ed25519/Ed25519ProofSigner.java index f8752880..4e7a4324 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/ed25519/Ed25519ProofSigner.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/ed25519/Ed25519ProofSigner.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -24,7 +24,6 @@ import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters; import org.bouncycastle.crypto.signers.Ed25519Signer; import org.eclipse.tractusx.ssi.lib.crypt.IPrivateKey; -import org.eclipse.tractusx.ssi.lib.exception.SsiException; import org.eclipse.tractusx.ssi.lib.proof.ISigner; import org.eclipse.tractusx.ssi.lib.proof.hash.HashedLinkedData; @@ -32,8 +31,7 @@ public class Ed25519ProofSigner implements ISigner { @Override - public byte[] sign(HashedLinkedData hashedLinkedData, IPrivateKey privateKey) - throws SsiException { + public byte[] sign(HashedLinkedData hashedLinkedData, IPrivateKey privateKey) { final byte[] message = hashedLinkedData.getValue(); Ed25519PrivateKeyParameters secretKeyParameters = diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/ed25519/Ed25519ProofVerifier.java b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/ed25519/Ed25519ProofVerifier.java index a33bb0ab..94755a21 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/ed25519/Ed25519ProofVerifier.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/ed25519/Ed25519ProofVerifier.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -29,20 +29,20 @@ import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters; import org.bouncycastle.crypto.signers.Ed25519Signer; import org.eclipse.tractusx.ssi.lib.crypt.IPublicKey; -import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559PublicKey; +import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519PublicKey; import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolver; -import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolverException; -import org.eclipse.tractusx.ssi.lib.exception.DidDocumentResolverNotRegisteredException; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePublicKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.NoVerificationKeyFoundExcpetion; -import org.eclipse.tractusx.ssi.lib.exception.UnsupportedSignatureTypeException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPublicKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.proof.NoVerificationKeyFoundException; +import org.eclipse.tractusx.ssi.lib.exception.proof.UnsupportedSignatureTypeException; import org.eclipse.tractusx.ssi.lib.model.MultibaseString; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; import org.eclipse.tractusx.ssi.lib.model.did.DidParser; import org.eclipse.tractusx.ssi.lib.model.did.Ed25519VerificationMethod; import org.eclipse.tractusx.ssi.lib.model.proof.Proof; -import org.eclipse.tractusx.ssi.lib.model.proof.ed21559.Ed25519Signature2020; +import org.eclipse.tractusx.ssi.lib.model.proof.ed25519.Ed25519Signature2020; import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable; import org.eclipse.tractusx.ssi.lib.proof.IVerifier; import org.eclipse.tractusx.ssi.lib.proof.hash.HashedLinkedData; @@ -55,8 +55,8 @@ public class Ed25519ProofVerifier implements IVerifier { @SneakyThrows({DidResolverException.class}) public boolean verify(HashedLinkedData hashedLinkedData, Verifiable verifiable) - throws UnsupportedSignatureTypeException, DidDocumentResolverNotRegisteredException, - InvalidePublicKeyFormat, NoVerificationKeyFoundExcpetion { + throws UnsupportedSignatureTypeException, InvalidPublicKeyFormatException, + NoVerificationKeyFoundException, DidParseException { final Proof proof = verifiable.getProof(); final Ed25519Signature2020 ed25519Signature2020 = new Ed25519Signature2020(proof); @@ -73,8 +73,8 @@ public boolean verify(HashedLinkedData hashedLinkedData, Verifiable verifiable) } private IPublicKey discoverPublicKey(Ed25519Signature2020 signature) - throws DidDocumentResolverNotRegisteredException, UnsupportedSignatureTypeException, - InvalidePublicKeyFormat, NoVerificationKeyFoundExcpetion, DidResolverException { + throws UnsupportedSignatureTypeException, InvalidPublicKeyFormatException, + NoVerificationKeyFoundException, DidResolverException, DidParseException { final Did issuer = DidParser.parse(signature.getVerificationMethod()); @@ -89,14 +89,14 @@ private IPublicKey discoverPublicKey(Ed25519Signature2020 signature) .findFirst() .orElseThrow( () -> - new NoVerificationKeyFoundExcpetion( + new NoVerificationKeyFoundException( "No Ed25519 verification key found in DID Document")); IPublicKey publicKey; try { - publicKey = (IPublicKey) new x21559PublicKey(key.getPublicKeyBase58().getEncoded(), false); + publicKey = (IPublicKey) new x25519PublicKey(key.getPublicKeyBase58().getEncoded(), false); } catch (IOException e) { - throw new InvalidePublicKeyFormat(e.getCause()); + throw new InvalidPublicKeyFormatException(e.getCause()); } return publicKey; diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/jws/JWSProofSigner.java b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/jws/JWSProofSigner.java index 973ce991..7eb8735a 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/jws/JWSProofSigner.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/jws/JWSProofSigner.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -32,8 +32,8 @@ import java.io.IOException; import org.eclipse.tractusx.ssi.lib.crypt.IPrivateKey; import org.eclipse.tractusx.ssi.lib.crypt.octet.OctetKeyPairFactory; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePrivateKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.SsiException; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPrivateKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureGenerateFailedException; import org.eclipse.tractusx.ssi.lib.proof.ISigner; import org.eclipse.tractusx.ssi.lib.proof.hash.HashedLinkedData; @@ -42,21 +42,21 @@ public class JWSProofSigner implements ISigner { @Override public byte[] sign(HashedLinkedData hashedLinkedData, IPrivateKey privateKey) - throws InvalidePrivateKeyFormat { + throws InvalidPrivateKeyFormatException, SignatureGenerateFailedException { OctetKeyPairFactory octetKeyPairFactory = new OctetKeyPairFactory(); OctetKeyPair keyPair; try { keyPair = octetKeyPairFactory.fromPrivateKey(privateKey); } catch (IOException e) { - throw new InvalidePrivateKeyFormat(e.getCause()); + throw new InvalidPrivateKeyFormatException(e.getCause()); } JWSSigner signer; try { signer = new Ed25519Signer(keyPair); } catch (JOSEException e) { - throw new SsiException(e.getMessage()); + throw new InvalidPrivateKeyFormatException(e.getMessage()); } var header = new JWSHeader.Builder(JWSAlgorithm.EdDSA).build(); @@ -66,7 +66,7 @@ public byte[] sign(HashedLinkedData hashedLinkedData, IPrivateKey privateKey) try { jwsObject.sign(signer); } catch (JOSEException e) { - throw new SsiException(e.getMessage()); + throw new SignatureGenerateFailedException(e.getMessage()); } return jwsObject.serialize(true).getBytes(); diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/jws/JWSProofVerifier.java b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/jws/JWSProofVerifier.java index 3e4125d1..56b6e97e 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/jws/JWSProofVerifier.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/proof/types/jws/JWSProofVerifier.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -35,12 +35,12 @@ import lombok.SneakyThrows; import org.eclipse.tractusx.ssi.lib.crypt.IPublicKey; import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolver; -import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolverException; -import org.eclipse.tractusx.ssi.lib.exception.DidDocumentResolverNotRegisteredException; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePublicKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.NoVerificationKeyFoundExcpetion; -import org.eclipse.tractusx.ssi.lib.exception.SsiException; -import org.eclipse.tractusx.ssi.lib.exception.UnsupportedSignatureTypeException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPublicKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.proof.NoVerificationKeyFoundException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureParseException; +import org.eclipse.tractusx.ssi.lib.exception.proof.SignatureVerificationException; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; import org.eclipse.tractusx.ssi.lib.model.did.DidParser; @@ -59,12 +59,13 @@ public class JWSProofVerifier implements IVerifier { @SneakyThrows({DidResolverException.class}) public boolean verify(HashedLinkedData hashedLinkedData, Verifiable document) - throws UnsupportedSignatureTypeException, DidDocumentResolverNotRegisteredException, - NoVerificationKeyFoundExcpetion, InvalidePublicKeyFormat { + throws SignatureParseException, DidParseException, InvalidPublicKeyFormatException, + SignatureVerificationException { final Proof proof = document.getProof(); if (!proof.getType().equals(JWSSignature2020.JWS_VERIFICATION_KEY_2020)) { - throw new UnsupportedSignatureTypeException(proof.getType()); + throw new SignatureParseException( + String.format("Unsupported verification method: %s", proof.getType())); } final JWSSignature2020 jwsSignature2020 = new JWSSignature2020(proof); @@ -76,28 +77,32 @@ public boolean verify(HashedLinkedData hashedLinkedData, Verifiable document) try { jws = JWSObject.parse(jwsSignature2020.getJws(), payload); } catch (ParseException e) { - throw new SsiException(e.getMessage()); + throw new SignatureParseException(jwsSignature2020.getJws()); } - var keyPair = this.discoverOctectKey(jwsSignature2020); - JWSVerifier verifier; + OctetKeyPair keyPair = null; + try { + keyPair = this.discoverOctectKey(jwsSignature2020); + } catch (NoVerificationKeyFoundException e) { + throw new SignatureParseException(e.getMessage()); + } + JWSVerifier verifier; try { verifier = new Ed25519Verifier(keyPair); } catch (JOSEException e) { - throw new SsiException(e.getMessage()); + throw new InvalidPublicKeyFormatException(e.getMessage()); } try { return jws.verify(verifier); } catch (JOSEException e) { - throw new SsiException(e.getMessage()); + throw new SignatureVerificationException(e.getMessage()); } } private OctetKeyPair discoverOctectKey(JWSSignature2020 signature) - throws DidDocumentResolverNotRegisteredException, UnsupportedSignatureTypeException, - InvalidePublicKeyFormat, NoVerificationKeyFoundExcpetion, DidResolverException { + throws NoVerificationKeyFoundException, DidParseException, DidResolverException { final Did issuer = DidParser.parse(signature.getVerificationMethod()); @@ -113,7 +118,7 @@ private OctetKeyPair discoverOctectKey(JWSSignature2020 signature) .findFirst() .orElseThrow( () -> - new NoVerificationKeyFoundExcpetion( + new NoVerificationKeyFoundException( "No JWS verification Key found in DID Document")); var x = Base64URL.from(key.getPublicKeyJwk().getX()); @@ -130,21 +135,34 @@ private OctetKeyPair discoverOctectKey(JWSSignature2020 signature) * @param signature the signature * @param publicKey the public key * @return the boolean + * @throws SignatureParseException + * @throws SignatureVerificationException + * @throws InvalidPublicKeyFormatException */ - @SneakyThrows - public boolean verify(HashedLinkedData hashedLinkedData, byte[] signature, IPublicKey publicKey) { + public boolean verify(HashedLinkedData hashedLinkedData, byte[] signature, IPublicKey publicKey) + throws SignatureParseException, SignatureVerificationException, + InvalidPublicKeyFormatException { var keyPair = new OctetKeyPair.Builder(Curve.Ed25519, Base64URL.encode(publicKey.asByte())).build(); - JWSVerifier verifier = (JWSVerifier) new Ed25519Verifier(keyPair.toPublicJWK()); + JWSVerifier verifier; + try { + verifier = (JWSVerifier) new Ed25519Verifier(keyPair.toPublicJWK()); + } catch (JOSEException e) { + throw new InvalidPublicKeyFormatException(e.getMessage()); + } Payload payload = new Payload(hashedLinkedData.getValue()); JWSObject jws; try { jws = JWSObject.parse(new String(signature), payload); } catch (ParseException e) { - throw new SsiException(e.getMessage()); + throw new SignatureParseException(String.format("Error while parsing JWS %s", signature)); + } + try { + return jws.verify(verifier); + } catch (JOSEException e) { + throw new SignatureVerificationException(e.getMessage()); } - return jws.verify(verifier); } } diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdSerializer.java b/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdSerializer.java index b6cc2cc2..916a3de4 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdSerializer.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdSerializer.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,7 +21,7 @@ package org.eclipse.tractusx.ssi.lib.serialization.jsonLd; -import org.eclipse.tractusx.ssi.lib.exception.InvalidJsonLdException; +import org.eclipse.tractusx.ssi.lib.exception.json.InvalidJsonLdException; import org.eclipse.tractusx.ssi.lib.model.verifiable.presentation.VerifiablePresentation; import org.eclipse.tractusx.ssi.lib.serialization.jwt.SerializedVerifiablePresentation; diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdSerializerImpl.java b/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdSerializerImpl.java index d289b4fb..e63d9305 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdSerializerImpl.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/serialization/jsonLd/JsonLdSerializerImpl.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,7 +21,7 @@ package org.eclipse.tractusx.ssi.lib.serialization.jsonLd; -import org.eclipse.tractusx.ssi.lib.exception.InvalidJsonLdException; +import org.eclipse.tractusx.ssi.lib.exception.json.InvalidJsonLdException; import org.eclipse.tractusx.ssi.lib.model.verifiable.presentation.VerifiablePresentation; import org.eclipse.tractusx.ssi.lib.serialization.jwt.SerializedVerifiablePresentation; import org.eclipse.tractusx.ssi.lib.validation.JsonLdValidator; diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidator.java b/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidator.java index 3a9df3c7..3d6dde86 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidator.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidator.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,7 +21,7 @@ package org.eclipse.tractusx.ssi.lib.validation; -import org.eclipse.tractusx.ssi.lib.exception.InvalidJsonLdException; +import org.eclipse.tractusx.ssi.lib.exception.json.InvalidJsonLdException; import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable; public interface JsonLdValidator { diff --git a/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidatorImpl.java b/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidatorImpl.java index 9e3ff27b..dd4d54bb 100644 --- a/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidatorImpl.java +++ b/src/main/java/org/eclipse/tractusx/ssi/lib/validation/JsonLdValidatorImpl.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -32,7 +32,7 @@ import jakarta.json.JsonValue; import java.util.Map; import java.util.logging.Logger; -import org.eclipse.tractusx.ssi.lib.exception.InvalidJsonLdException; +import org.eclipse.tractusx.ssi.lib.exception.json.InvalidJsonLdException; import org.eclipse.tractusx.ssi.lib.model.JsonLdObject; import org.eclipse.tractusx.ssi.lib.model.RemoteDocumentLoader; import org.eclipse.tractusx.ssi.lib.model.verifiable.Verifiable; @@ -44,19 +44,28 @@ public class JsonLdValidatorImpl implements JsonLdValidator { static final Logger LOG = Logger.getLogger(JsonLdValidatorImpl.class.getName()); private static final String UNDEFINED_TERM_URI = "urn:UNDEFINEDTERM"; - public void validate(Verifiable verifiable) throws InvalidJsonLdException { - if (verifiable instanceof VerifiableCredential) { - validateJsonLd(verifiable); - } else if (verifiable instanceof VerifiablePresentation) { - VerifiablePresentation verifiablePresentation = (VerifiablePresentation) verifiable; - for (VerifiableCredential verifiableCredential : - verifiablePresentation.getVerifiableCredentials()) { - validate(verifiableCredential); + private static void findUndefinedTerms(JsonArray jsonArray) throws InvalidJsonLdException { + for (JsonValue entry : jsonArray) { + if (entry instanceof JsonObject) { + findUndefinedTerms((JsonObject) entry); + } + } + } + + private static void findUndefinedTerms(JsonObject jsonObject) throws InvalidJsonLdException { + for (Map.Entry entry : jsonObject.entrySet()) { + if (entry.getKey().startsWith(UNDEFINED_TERM_URI)) { + + throw new InvalidJsonLdException( + "Undefined JSON-LD term: " + entry.getKey().substring(UNDEFINED_TERM_URI.length())); + } + + if (entry.getValue() instanceof JsonArray) { + findUndefinedTerms((JsonArray) entry.getValue()); + } + if (entry.getValue() instanceof JsonObject) { + findUndefinedTerms((JsonObject) entry.getValue()); } - } else { - LOG.warning("Unsupported Verifiable type: " + verifiable.getClass().getName()); - throw new InvalidJsonLdException( - String.format("Unsupported Verifiable type: %s", verifiable.getClass().getName())); } } @@ -66,6 +75,7 @@ private void validateJsonLd(JsonLdObject jsonLdObject) throws InvalidJsonLdExcep var documentLoader = RemoteDocumentLoader.getInstance(); documentLoader.setEnableHttps(true); documentLoader.setHttpsContexts(jsonLdObject.getContext()); + documentLoader.setEnableFile(true); final JsonObject expandContext = Json.createObjectBuilder().add("@vocab", Json.createValue(UNDEFINED_TERM_URI)).build(); @@ -89,28 +99,20 @@ private void validateJsonLd(JsonLdObject jsonLdObject) throws InvalidJsonLdExcep } } - private static void findUndefinedTerms(JsonArray jsonArray) { - for (JsonValue entry : jsonArray) { - if (entry instanceof JsonObject) { - findUndefinedTerms((JsonObject) entry); - } - } - } - - private static void findUndefinedTerms(JsonObject jsonObject) { - for (Map.Entry entry : jsonObject.entrySet()) { - if (entry.getKey().startsWith(UNDEFINED_TERM_URI)) { - - throw new RuntimeException( - "Undefined JSON-LD term: " + entry.getKey().substring(UNDEFINED_TERM_URI.length())); - } - - if (entry.getValue() instanceof JsonArray) { - findUndefinedTerms((JsonArray) entry.getValue()); - } - if (entry.getValue() instanceof JsonObject) { - findUndefinedTerms((JsonObject) entry.getValue()); + public void validate(Verifiable verifiable) throws InvalidJsonLdException { + if (verifiable instanceof VerifiableCredential) { + validateJsonLd(verifiable); + } else if (verifiable instanceof VerifiablePresentation) { + VerifiablePresentation verifiablePresentation = (VerifiablePresentation) verifiable; + for (VerifiableCredential verifiableCredential : + verifiablePresentation.getVerifiableCredentials()) { + validate(verifiableCredential); } + } else { + LOG.warning("Unsupported Verifiable type: " + verifiable.getClass().getName()); + throw new InvalidJsonLdException( + String.format( + "Verifiable type %s is not supported", verifiable.getClass().getSimpleName())); } } } diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/base/Base58BitcoinTest.java b/src/test/java/org/eclipse/tractusx/ssi/lib/base/Base58BitcoinTest.java index f6c2d017..0c6e621c 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/base/Base58BitcoinTest.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/base/Base58BitcoinTest.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,13 +21,16 @@ package org.eclipse.tractusx.ssi.lib.base; +import java.io.Serializable; import java.nio.charset.StandardCharsets; import org.eclipse.tractusx.ssi.lib.model.base.Base58Bitcoin; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** The type Base 58 bitcoin test. */ -public class Base58BitcoinTest { +public class Base58BitcoinTest implements Serializable { + + static final long serialVersionUID = 6034044314589513430L; private static final byte[] DECODED = "Multibase is awesome! \\o/".getBytes(StandardCharsets.UTF_8); diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/cypto/ed21995/ed21559KeyTest.java b/src/test/java/org/eclipse/tractusx/ssi/lib/cypto/ed21995/ed25519KeyTest.java similarity index 67% rename from src/test/java/org/eclipse/tractusx/ssi/lib/cypto/ed21995/ed21559KeyTest.java rename to src/test/java/org/eclipse/tractusx/ssi/lib/cypto/ed21995/ed25519KeyTest.java index 00524296..1b392de7 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/cypto/ed21995/ed21559KeyTest.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/cypto/ed21995/ed25519KeyTest.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -26,42 +26,44 @@ import java.io.IOException; import java.util.Arrays; +import lombok.SneakyThrows; import org.eclipse.tractusx.ssi.lib.crypt.IKeyGenerator; import org.eclipse.tractusx.ssi.lib.crypt.KeyPair; -import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559Generator; -import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559PrivateKey; -import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559PublicKey; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePrivateKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePublicKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.KeyGenerationException; +import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519Generator; +import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519PrivateKey; +import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519PublicKey; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPrivateKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.key.InvalidPublicKeyFormatException; +import org.eclipse.tractusx.ssi.lib.exception.key.KeyGenerationException; import org.eclipse.tractusx.ssi.lib.model.base.EncodeType; import org.junit.jupiter.api.Test; -/** The type Ed 21559 key test. */ -public class ed21559KeyTest { +/** The type Ed 25519 key test. */ +public class ed25519KeyTest { /** - * Test ed 21559 key generation. + * Test ed 25519 key generation. * * @throws KeyGenerationException the key generation exception */ @Test - public void testED21559KeyGeneration() throws KeyGenerationException { - IKeyGenerator keyGenerator = new x21559Generator(); + public void testED25519KeyGeneration() throws KeyGenerationException { + IKeyGenerator keyGenerator = new x25519Generator(); KeyPair keyPair = keyGenerator.generateKey(); assertNotNull(keyPair.getPrivateKey()); assertNotNull(keyPair.getPublicKey()); } /** - * Test ed 21559 key serliztion. + * Test ed 25519 key serliztion. * * @throws KeyGenerationException the key generation exception * @throws IOException the io exception */ @Test - public void testED21559KeySerliztion() throws KeyGenerationException, IOException { - IKeyGenerator keyGenerator = new x21559Generator(); + @SneakyThrows + public void testED25519KeySerliztion() throws KeyGenerationException, IOException { + IKeyGenerator keyGenerator = new x25519Generator(); KeyPair keyPair = keyGenerator.generateKey(); assertNotNull(keyPair.getPrivateKey().asStringForStoring()); @@ -74,7 +76,7 @@ public void testED21559KeySerliztion() throws KeyGenerationException, IOExceptio } /** - * Test ed 21559 key deserliztion. + * Test ed 25519 key deserliztion. * * @throws KeyGenerationException the key generation exception * @throws IOException the io exception @@ -82,20 +84,21 @@ public void testED21559KeySerliztion() throws KeyGenerationException, IOExceptio * @throws InvalidePublicKeyFormat the invalide public key format */ @Test - public void testED21559KeyDeserliztion() - throws KeyGenerationException, IOException, InvalidePrivateKeyFormat, - InvalidePublicKeyFormat { - IKeyGenerator keyGenerator = new x21559Generator(); + @SneakyThrows + public void testED25519KeyDeserliztion() + throws KeyGenerationException, IOException, InvalidPrivateKeyFormatException, + InvalidPublicKeyFormatException { + IKeyGenerator keyGenerator = new x25519Generator(); KeyPair keyPair = keyGenerator.generateKey(); var originalPrivateKey = keyPair.getPrivateKey().asByte(); var originalPublicKey = keyPair.getPublicKey().asByte(); String privateKeyString = keyPair.getPrivateKey().asStringForStoring(); - var privateKey = new x21559PrivateKey(privateKeyString, true); + var privateKey = new x25519PrivateKey(privateKeyString, true); String publicKeyString = keyPair.getPublicKey().asStringForStoring(); - var publicKey = new x21559PublicKey(publicKeyString, true); + var publicKey = new x25519PublicKey(publicKeyString, true); assertTrue(Arrays.equals(originalPrivateKey, privateKey.asByte())); diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/did/resolver/CompositeDidResolverTest.java b/src/test/java/org/eclipse/tractusx/ssi/lib/did/resolver/CompositeDidResolverTest.java index 62ded3e0..bb8862ef 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/did/resolver/CompositeDidResolverTest.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/did/resolver/CompositeDidResolverTest.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -33,6 +33,8 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import lombok.SneakyThrows; +import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; import org.eclipse.tractusx.ssi.lib.model.did.DidMethod; @@ -68,7 +70,8 @@ public void resetMocks() { * @throws DidResolverException the did resolver exception */ @Test - public void shouldResolveWithOneResolver() throws DidResolverException { + @SneakyThrows + public void shouldResolveWithOneResolver() { when(resolver1.isResolvable(any())).thenReturn(true); when(resolver1.resolve(any())).thenReturn(RESOLVED_DID_DOC); CompositeDidResolver resolver = new CompositeDidResolver(resolver1); @@ -82,7 +85,8 @@ public void shouldResolveWithOneResolver() throws DidResolverException { * @throws DidResolverException the did resolver exception */ @Test - public void shouldResolveWithFirstResolver() throws DidResolverException { + @SneakyThrows + public void shouldResolveWithFirstResolver() { when(resolver1.isResolvable(any())).thenReturn(true); when(resolver1.resolve(any())).thenReturn(RESOLVED_DID_DOC); @@ -99,7 +103,8 @@ public void shouldResolveWithFirstResolver() throws DidResolverException { * @throws DidResolverException the did resolver exception */ @Test - public void shouldResolveWithSecondResolver() throws DidResolverException { + @SneakyThrows + public void shouldResolveWithSecondResolver() { when(resolver1.isResolvable(any())).thenReturn(false); when(resolver2.isResolvable(any())).thenReturn(true); when(resolver2.resolve(any())).thenReturn(RESOLVED_DID_DOC); @@ -116,7 +121,8 @@ public void shouldResolveWithSecondResolver() throws DidResolverException { * @throws DidResolverException the did resolver exception */ @Test - public void mustNotResolveOnFalse() throws DidResolverException { + @SneakyThrows + public void mustNotResolveOnFalse() { when(resolver1.isResolvable(any())).thenReturn(false); when(resolver2.isResolvable(any())).thenReturn(false); @@ -133,7 +139,8 @@ public void mustNotResolveOnFalse() throws DidResolverException { * @throws DidResolverException the did resolver exception */ @Test - public void mustNotResolveOnException() throws DidResolverException { + @SneakyThrows + public void mustNotResolveOnException() { when(resolver1.isResolvable(any())).thenReturn(true); when(resolver1.resolve(any())).thenThrow(DidResolverException.class); when(resolver1.isResolvable(any())).thenReturn(true); @@ -154,7 +161,8 @@ public void mustNotResolveOnException() throws DidResolverException { * @throws DidResolverException the did resolver exception */ @Test - public void shouldConstructCompositeResolverUsingStaticMethod() throws DidResolverException { + @SneakyThrows + public void shouldConstructCompositeResolverUsingStaticMethod() { when(resolver1.isResolvable(any())).thenReturn(true); when(resolver1.resolve(any())).thenReturn(RESOLVED_DID_DOC); diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidUniResolverIT.java b/src/test/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidUniResolverIT.java index 6b0495a2..5e3bc595 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidUniResolverIT.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/did/resolver/DidUniResolverIT.java @@ -27,6 +27,8 @@ import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.http.HttpClient; +import org.eclipse.tractusx.ssi.lib.exception.did.DidParseException; +import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; import org.eclipse.tractusx.ssi.lib.model.did.DidMethod; @@ -84,9 +86,10 @@ public void initEach() throws MalformedURLException, URISyntaxException { * Should resolve valid did. * * @throws DidResolverException the did resolver exception + * @throws DidParseException */ @Test - public void shouldResolveValidDid() throws DidResolverException { + public void shouldResolveValidDid() throws DidResolverException, DidParseException { Did validDidWeb = new Did( new DidMethod("key"), diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebResolverIT.java b/src/test/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebResolverIT.java index 191de5c4..116856de 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebResolverIT.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebResolverIT.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -25,7 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.net.http.HttpClient; -import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolverException; +import lombok.SneakyThrows; import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; @@ -68,7 +68,8 @@ public void initEach() { * @throws DidResolverException the did resolver exception */ @Test - public void shouldResolveValidWebDid() throws DidResolverException { + @SneakyThrows + public void shouldResolveValidWebDid() { Did validDidWeb = new Did( new DidMethod("web"), @@ -85,7 +86,8 @@ public void shouldResolveValidWebDid() throws DidResolverException { * @throws DidResolverException the did resolver exception */ @Test - public void shouldResolveValidExternalWebDid() throws DidResolverException { + @SneakyThrows + public void shouldResolveValidExternalWebDid() { final String didIdentifier = "did.actor:alice"; Did validDidWeb = new Did(new DidMethod("web"), new DidMethodIdentifier(didIdentifier), null); assertTrue(httpsResolver.isResolvable(validDidWeb)); diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebResolverTest.java b/src/test/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebResolverTest.java index 19c3af54..d512a3fc 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebResolverTest.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/did/web/DidWebResolverTest.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -29,13 +29,12 @@ import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Mockito.when; -import java.io.IOException; import java.net.URI; -import java.net.URISyntaxException; import java.net.http.HttpClient; import java.net.http.HttpResponse; -import org.eclipse.tractusx.ssi.lib.did.resolver.DidResolverException; +import lombok.SneakyThrows; import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser; +import org.eclipse.tractusx.ssi.lib.exception.did.DidResolverException; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; import org.eclipse.tractusx.ssi.lib.model.did.DidMethod; @@ -72,8 +71,8 @@ public void initEach() { * @throws URISyntaxException the uri syntax exception */ @Test - public void shouldResolveValidWebDid() - throws DidResolverException, IOException, InterruptedException, URISyntaxException { + @SneakyThrows + public void shouldResolveValidWebDid() { Did validDidWeb = new Did(new DidMethod("web"), new DidMethodIdentifier("localhost"), null); assertTrue(resolver.isResolvable(validDidWeb)); when(httpClient.send(any(), any())).thenReturn(response); @@ -91,7 +90,8 @@ public void shouldResolveValidWebDid() * @throws DidResolverException the did resolver exception */ @Test - public void shouldNotResolveNonWebDid() throws DidResolverException { + @SneakyThrows + public void shouldNotResolveNonWebDid() { Did validDidKey = new Did( new DidMethod("key"), diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/did/web/util/DidWebParserTest.java b/src/test/java/org/eclipse/tractusx/ssi/lib/did/web/util/DidWebParserTest.java index be8f5f9d..18f0c7e1 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/did/web/util/DidWebParserTest.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/did/web/util/DidWebParserTest.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -22,6 +22,7 @@ package org.eclipse.tractusx.ssi.lib.did.web.util; import java.net.URI; +import lombok.SneakyThrows; import org.eclipse.tractusx.ssi.lib.model.did.Did; import org.eclipse.tractusx.ssi.lib.model.did.DidMethod; import org.eclipse.tractusx.ssi.lib.model.did.DidMethodIdentifier; @@ -49,6 +50,7 @@ public class DidWebParserTest { "some-host:path1:path2, https://some-host/path1/path2/did.json", "some-host%3A9090:path1:path2, https://some-host:9090/path1/path2/did.json" }) + @SneakyThrows public void testResolveUriFromDid(String methodIdentifier, String expectedUri) { final Did did = new Did(new DidMethod("web"), new DidMethodIdentifier(methodIdentifier), null); diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/model/did/JsonWebKey2020BuilderTest.java b/src/test/java/org/eclipse/tractusx/ssi/lib/model/did/JsonWebKey2020BuilderTest.java index bf6d6b1a..a305f304 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/model/did/JsonWebKey2020BuilderTest.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/model/did/JsonWebKey2020BuilderTest.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -33,8 +33,8 @@ import org.eclipse.tractusx.ssi.lib.crypt.IPrivateKey; import org.eclipse.tractusx.ssi.lib.crypt.IPublicKey; import org.eclipse.tractusx.ssi.lib.crypt.jwk.JsonWebKey; -import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559PrivateKey; -import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559PublicKey; +import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519PrivateKey; +import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519PublicKey; import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory; import org.junit.jupiter.api.Test; @@ -50,8 +50,8 @@ public void testJsonWebKey2020VerificationMethod() { OctetKeyPair octetKeyPair = new OctetKeyPairGenerator(Curve.Ed25519).keyID(keyId).keyUse(KeyUse.SIGNATURE).generate(); - IPrivateKey privateKey = new x21559PrivateKey(octetKeyPair.getDecodedD()); - IPublicKey publicKey = new x21559PublicKey(octetKeyPair.getDecodedX()); + IPrivateKey privateKey = new x25519PrivateKey(octetKeyPair.getDecodedD()); + IPublicKey publicKey = new x25519PublicKey(octetKeyPair.getDecodedX()); // JWK JsonWebKey jwk = new JsonWebKey(keyId, publicKey, privateKey); diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofValidationComponentTest.java b/src/test/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofValidationComponentTest.java index 15fcb242..daf83206 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofValidationComponentTest.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/proof/LinkedDataProofValidationComponentTest.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,17 +21,13 @@ package org.eclipse.tractusx.ssi.lib.proof; -import java.io.IOException; import java.net.URI; import java.time.Instant; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.List; +import lombok.SneakyThrows; import org.eclipse.tractusx.ssi.lib.SsiLibrary; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePrivateKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.KeyGenerationException; -import org.eclipse.tractusx.ssi.lib.exception.SsiException; -import org.eclipse.tractusx.ssi.lib.exception.UnsupportedSignatureTypeException; import org.eclipse.tractusx.ssi.lib.model.proof.Proof; import org.eclipse.tractusx.ssi.lib.model.proof.jws.JWSSignature2020; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; @@ -60,24 +56,15 @@ public void setup() { this.didResolver = new TestDidResolver(); } - /** - * Test vc proof failure on manipulated credential. - * - * @throws IOException the io exception - * @throws UnsupportedSignatureTypeException the unsupported signature type exception - * @throws InvalidePrivateKeyFormat the invalide private key format - * @throws KeyGenerationException the key generation exception - */ + /** Test vc proof failure on manipulated credential. */ @Test - public void testVCProofFailureOnManipulatedCredential() - throws IOException, UnsupportedSignatureTypeException, InvalidePrivateKeyFormat, - KeyGenerationException { - + @SneakyThrows + public void testVCProofFailureOnManipulatedCredential() { credentialIssuer = TestIdentityFactory.newIdentityWithED25519Keys(); didResolver.register(credentialIssuer); // Generator - linkedDataProofGenerator = LinkedDataProofGenerator.newInstance(SignatureType.ED21559); + linkedDataProofGenerator = LinkedDataProofGenerator.newInstance(SignatureType.ED25519); // Verification linkedDataProofValidation = LinkedDataProofValidation.newInstance(this.didResolver); @@ -106,24 +93,16 @@ public void testVCProofFailureOnManipulatedCredential() Assertions.assertFalse(isOk); } - /** - * Test vc ed 21559 proof generation and verification. - * - * @throws IOException the io exception - * @throws UnsupportedSignatureTypeException the unsupported signature type exception - * @throws InvalidePrivateKeyFormat the invalide private key format - * @throws KeyGenerationException the key generation exception - */ + /** Test vc ed 25519 proof generation and verification. */ @Test - public void testVCEd21559ProofGenerationAndVerification() - throws IOException, UnsupportedSignatureTypeException, InvalidePrivateKeyFormat, - KeyGenerationException { + @SneakyThrows + public void testVCEd25519ProofGenerationAndVerification() { credentialIssuer = TestIdentityFactory.newIdentityWithED25519Keys(); didResolver.register(credentialIssuer); // Generator - linkedDataProofGenerator = LinkedDataProofGenerator.newInstance(SignatureType.ED21559); + linkedDataProofGenerator = LinkedDataProofGenerator.newInstance(SignatureType.ED25519); // Verification linkedDataProofValidation = LinkedDataProofValidation.newInstance(this.didResolver); @@ -146,18 +125,10 @@ public void testVCEd21559ProofGenerationAndVerification() Assertions.assertTrue(isOk); } - /** - * Test vcjws proof generation and verification. - * - * @throws IOException the io exception - * @throws UnsupportedSignatureTypeException the unsupported signature type exception - * @throws InvalidePrivateKeyFormat the invalide private key format - * @throws KeyGenerationException the key generation exception - */ + /** Test vcjws proof generation and verification. */ @Test - public void testVCJWSProofGenerationAndVerification() - throws IOException, UnsupportedSignatureTypeException, InvalidePrivateKeyFormat, - KeyGenerationException { + @SneakyThrows + public void testVCJWSProofGenerationAndVerification() { credentialIssuer = TestIdentityFactory.newIdentityWithED25519Keys(); didResolver.register(credentialIssuer); @@ -187,24 +158,16 @@ public void testVCJWSProofGenerationAndVerification() Assertions.assertTrue(isOk); } - /** - * Test vp ed 21559 proof generation and verification. - * - * @throws IOException the io exception - * @throws UnsupportedSignatureTypeException the unsupported signature type exception - * @throws InvalidePrivateKeyFormat the invalide private key format - * @throws KeyGenerationException the key generation exception - */ + /** Test vp ed 25519 proof generation and verification. */ @Test - public void testVPEd21559ProofGenerationAndVerification() - throws IOException, UnsupportedSignatureTypeException, InvalidePrivateKeyFormat, - KeyGenerationException { + @SneakyThrows + public void testVPEd25519ProofGenerationAndVerification() { credentialIssuer = TestIdentityFactory.newIdentityWithED25519Keys(); didResolver.register(credentialIssuer); // Generator - linkedDataProofGenerator = LinkedDataProofGenerator.newInstance(SignatureType.ED21559); + linkedDataProofGenerator = LinkedDataProofGenerator.newInstance(SignatureType.ED25519); // Verifier linkedDataProofValidation = LinkedDataProofValidation.newInstance(didResolver); @@ -236,18 +199,10 @@ public void testVPEd21559ProofGenerationAndVerification() Assertions.assertTrue(isOk); } - /** - * Test vpjws proof generation and verification. - * - * @throws IOException the io exception - * @throws UnsupportedSignatureTypeException the unsupported signature type exception - * @throws InvalidePrivateKeyFormat the invalide private key format - * @throws KeyGenerationException the key generation exception - */ + /** Test vpjws proof generation and verification. */ @Test - public void testVPJWSProofGenerationAndVerification() - throws IOException, UnsupportedSignatureTypeException, InvalidePrivateKeyFormat, - KeyGenerationException { + @SneakyThrows + public void testVPJWSProofGenerationAndVerification() { credentialIssuer = TestIdentityFactory.newIdentityWithED25519Keys(); didResolver.register(credentialIssuer); @@ -287,16 +242,16 @@ public void testVPJWSProofGenerationAndVerification() Assertions.assertTrue(isOk); } + /** Test verification method. */ @Test - public void testVerificationMethodOfVC() - throws IOException, KeyGenerationException, UnsupportedSignatureTypeException, SsiException, - InvalidePrivateKeyFormat { + @SneakyThrows + public void testVerificationMethodOfVC() { credentialIssuer = TestIdentityFactory.newIdentityWithED25519Keys(); didResolver.register(credentialIssuer); // Generator - linkedDataProofGenerator = LinkedDataProofGenerator.newInstance(SignatureType.ED21559); + linkedDataProofGenerator = LinkedDataProofGenerator.newInstance(SignatureType.ED25519); // Verification linkedDataProofValidation = LinkedDataProofValidation.newInstance(this.didResolver); diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/proof/SignAndVerifyTest.java b/src/test/java/org/eclipse/tractusx/ssi/lib/proof/SignAndVerifyTest.java index c23cd128..2afd0707 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/proof/SignAndVerifyTest.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/proof/SignAndVerifyTest.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,13 +21,8 @@ package org.eclipse.tractusx.ssi.lib.proof; -import com.nimbusds.jose.JOSEException; -import java.io.IOException; import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePrivateKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.InvalidePublicKeyFormat; -import org.eclipse.tractusx.ssi.lib.exception.KeyGenerationException; +import lombok.SneakyThrows; import org.eclipse.tractusx.ssi.lib.proof.hash.HashedLinkedData; import org.eclipse.tractusx.ssi.lib.proof.types.ed25519.Ed25519ProofSigner; import org.eclipse.tractusx.ssi.lib.proof.types.ed25519.Ed25519ProofVerifier; @@ -50,9 +45,8 @@ public class SignAndVerifyTest { * @throws KeyGenerationException the key generation exception */ @Test - public void testSignAndVerify_ED201559() - throws IOException, InvalidePrivateKeyFormat, InvalidePublicKeyFormat, - KeyGenerationException { + @SneakyThrows + public void testSignAndVerify_ED201559() { final TestDidResolver didResolver = new TestDidResolver(); var testIdentity = TestIdentityFactory.newIdentityWithED25519Keys(); @@ -81,9 +75,8 @@ public void testSignAndVerify_ED201559() * @throws KeyGenerationException the key generation exception */ @Test - public void testSignAndVerify_JWS() - throws IOException, JOSEException, NoSuchAlgorithmException, InvalidePrivateKeyFormat, - InvalidePublicKeyFormat, KeyGenerationException { + @SneakyThrows + public void testSignAndVerify_JWS() { final TestDidResolver didResolver = new TestDidResolver(); var testIdentity = TestIdentityFactory.newIdentityWithED25519Keys(); diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/proof/proof/LinkedDataTransformerTest.java b/src/test/java/org/eclipse/tractusx/ssi/lib/proof/proof/LinkedDataTransformerTest.java index 65407f78..a0811ea8 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/proof/proof/LinkedDataTransformerTest.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/proof/proof/LinkedDataTransformerTest.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -73,6 +73,7 @@ public class LinkedDataTransformerTest { /** Test two transformations equal. */ @Test + @SneakyThrows public void testTwoTransformationsEqual() { final VerifiableCredential credential1 = deserializeCredential(SummaryVerifiableCredential); final var data1 = transformer.transform(credential1); @@ -92,6 +93,7 @@ public void testTwoTransformationsEqual() { value = { "issuer\": \"did:web:localhost%3A8080:BPNOPERATOR, issuer\": \"did:web:localhost%3A8080:BPNATTACKER" }) + @SneakyThrows public void testTwoTransformationDifference(String original, String replace) { final VerifiableCredential credential1 = deserializeCredential(SummaryVerifiableCredential); final VerifiableCredential credential2 = diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/serialization/SerializeUtilTest.java b/src/test/java/org/eclipse/tractusx/ssi/lib/serialization/SerializeUtilTest.java index 4557bec8..96051b55 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/serialization/SerializeUtilTest.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/serialization/SerializeUtilTest.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -28,6 +28,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import lombok.SneakyThrows; import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; import org.eclipse.tractusx.ssi.lib.model.did.Ed25519VerificationMethod; import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential; @@ -69,7 +70,8 @@ public void shouldSerializeVpContextFirst() { */ @Test @DisplayName("Test property order in json string for VC") - void testVCJsonPropertyOrder() throws JsonProcessingException { + @SneakyThrows + void testVCJsonPropertyOrder() { ObjectMapper objectMapper = new ObjectMapper(); // test VC json @@ -99,7 +101,8 @@ void testVCJsonPropertyOrder() throws JsonProcessingException { */ @Test @DisplayName("Test property order in json string for did document") - void testDidDocumentJsonPropertyOrder() throws JsonProcessingException { + @SneakyThrows + void testDidDocumentJsonPropertyOrder() { ObjectMapper objectMapper = new ObjectMapper(); // test did document json DidDocument didDocument = diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/serialization/jsonld/DanubeTechMapperTest.java b/src/test/java/org/eclipse/tractusx/ssi/lib/serialization/jsonld/DanubeTechMapperTest.java deleted file mode 100644 index 4f9a26b0..00000000 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/serialization/jsonld/DanubeTechMapperTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.serialization.jsonld; - -/** The type Danube tech mapper test. */ -public class DanubeTechMapperTest { - - // @SneakyThrows - // @Test - // public void canMapVerifiableCredential() { - - // VerifiableCredential expected = - // new VerifiableCredential(TestResourceUtil.getAlumniVerifiableCredential()); - - // com.danubetech.verifiablecredentials.VerifiableCredential dtCredential = - // DanubeTechMapper.map(expected); - - // VerifiableCredential result = DanubeTechMapper.map(dtCredential); - - // Assertions.assertEquals(expected.toPrettyJson(), result.toPrettyJson()); - // } - - // @SneakyThrows - // @Test - // public void canMapVerifiablePresentation() { - - // VerifiablePresentation expected = - // new VerifiablePresentation(TestResourceUtil.getAlumniVerifiablePresentation()); - - // com.danubetech.verifiablecredentials.VerifiablePresentation dtCredential = - // DanubeTechMapper.map(expected); - // VerifiablePresentation result = //DanubeTechMapper.map(dtCredential); - - // Assertions.assertEquals(expected.toPrettyJson(), result.toPrettyJson()); - // } -} diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/util/identity/TestDidDocumentResolver.java b/src/test/java/org/eclipse/tractusx/ssi/lib/util/identity/TestDidDocumentResolver.java deleted file mode 100644 index a61a408a..00000000 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/util/identity/TestDidDocumentResolver.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ******************************************************************************* - */ - -package org.eclipse.tractusx.ssi.lib.util.identity; - -import java.util.HashMap; -import java.util.Map; -import java.util.stream.Collectors; -import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolver; -import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistry; -import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistryImpl; -import org.eclipse.tractusx.ssi.lib.model.did.Did; -import org.eclipse.tractusx.ssi.lib.model.did.DidDocument; -import org.eclipse.tractusx.ssi.lib.model.did.DidMethod; - -/** The type Test did document resolver. */ -@Deprecated -/** Remove when {@code DidDocumentResolver} is removed. */ -public class TestDidDocumentResolver implements DidDocumentResolver { - private final Map documents = new HashMap<>(); - - @Override - public DidMethod getSupportedMethod() { - return TestDidFactory.DID_METHOD; - } - - @Override - public DidDocument resolve(Did did) { - - // We don't need fragment when we are searching for DID document ID. - Did didWithoutFragment = did.excludeFragment(); - if (!documents.containsKey(didWithoutFragment)) { - throw new RuntimeException( - String.format( - "Did not found: %s. Got [%s]", - did.toString(), - documents.values().stream() - .map(DidDocument::toString) - .collect(Collectors.joining(", ")))); - } - - return documents.get(didWithoutFragment); - } - - /** - * Register. - * - * @param testIdentity the test identity - */ - public void register(TestIdentity testIdentity) { - documents.put(testIdentity.getDid(), testIdentity.getDidDocument()); - } - - /** - * With registry did document resolver registry. - * - * @return the did document resolver registry - */ - public DidDocumentResolverRegistry withRegistry() { - final DidDocumentResolverRegistry registry = new DidDocumentResolverRegistryImpl(); - registry.register(this); - return registry; - } -} diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/util/identity/TestIdentityFactory.java b/src/test/java/org/eclipse/tractusx/ssi/lib/util/identity/TestIdentityFactory.java index d3275795..8f43c2f5 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/util/identity/TestIdentityFactory.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/util/identity/TestIdentityFactory.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,16 +21,15 @@ package org.eclipse.tractusx.ssi.lib.util.identity; -import java.io.IOException; import java.net.URI; import java.util.List; +import lombok.SneakyThrows; import org.eclipse.tractusx.ssi.lib.crypt.IKeyGenerator; import org.eclipse.tractusx.ssi.lib.crypt.IPrivateKey; import org.eclipse.tractusx.ssi.lib.crypt.IPublicKey; import org.eclipse.tractusx.ssi.lib.crypt.KeyPair; import org.eclipse.tractusx.ssi.lib.crypt.jwk.JsonWebKey; -import org.eclipse.tractusx.ssi.lib.crypt.x21559.x21559Generator; -import org.eclipse.tractusx.ssi.lib.exception.KeyGenerationException; +import org.eclipse.tractusx.ssi.lib.crypt.x25519.x25519Generator; import org.eclipse.tractusx.ssi.lib.model.MultibaseString; import org.eclipse.tractusx.ssi.lib.model.base.MultibaseFactory; import org.eclipse.tractusx.ssi.lib.model.did.Did; @@ -48,15 +47,13 @@ public class TestIdentityFactory { * New identity with ed 25519 keys test identity. * * @return the test identity - * @throws IOException the io exception - * @throws KeyGenerationException the key generation exception */ - public static TestIdentity newIdentityWithED25519Keys() - throws IOException, KeyGenerationException { + @SneakyThrows + public static TestIdentity newIdentityWithED25519Keys() { final Did did = TestDidFactory.createRandom(); - IKeyGenerator keyGenerator = new x21559Generator(); + IKeyGenerator keyGenerator = new x25519Generator(); KeyPair keyPair = keyGenerator.generateKey(); IPublicKey publicKey = keyPair.getPublicKey(); IPrivateKey privateKey = keyPair.getPrivateKey(); diff --git a/src/test/java/org/eclipse/tractusx/ssi/lib/verifiable/VerifiableCredentialTest.java b/src/test/java/org/eclipse/tractusx/ssi/lib/verifiable/VerifiableCredentialTest.java index 8bd4ef38..71b535cd 100644 --- a/src/test/java/org/eclipse/tractusx/ssi/lib/verifiable/VerifiableCredentialTest.java +++ b/src/test/java/org/eclipse/tractusx/ssi/lib/verifiable/VerifiableCredentialTest.java @@ -1,6 +1,6 @@ /* * ****************************************************************************** - * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * Copyright (c) 2021,2024 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -21,8 +21,6 @@ package org.eclipse.tractusx.ssi.lib.verifiable; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Map; import lombok.SneakyThrows; @@ -45,7 +43,7 @@ public class VerifiableCredentialTest { */ @Test @SneakyThrows - public void canSerializeVC() throws JsonMappingException, JsonProcessingException { + public void canSerializeVC() { final Map vpFromMap = TestResourceUtil.getAlumniVerifiableCredential(); var vp = new VerifiableCredential(vpFromMap); var json = vp.toJson();