Skip to content

Commit 8600c63

Browse files
committed
refactor: Various updates to signature suites and tests
1 parent 4a9e43d commit 8600c63

File tree

37 files changed

+175
-146
lines changed

37 files changed

+175
-146
lines changed

src/main/java/com/danubetech/dataintegrity/canonicalizer/JCSCanonicalizer.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import com.danubetech.dataintegrity.DataIntegrityProof;
44
import foundation.identity.jsonld.JsonLDException;
55
import foundation.identity.jsonld.JsonLDObject;
6+
import org.apache.commons.codec.binary.Hex;
67
import org.erdtman.jcs.JsonCanonicalizer;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
710

811
import java.io.IOException;
912
import java.nio.charset.StandardCharsets;
@@ -12,6 +15,8 @@
1215

1316
public abstract class JCSCanonicalizer extends Canonicalizer {
1417

18+
private static final Logger log = LoggerFactory.getLogger(JCSCanonicalizer.class);
19+
1520
public JCSCanonicalizer() {
1621
super(List.of("jcs"));
1722
}
@@ -46,13 +51,14 @@ public byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject j
4651
// canonicalize the LD object and LD proof options
4752

4853
String canonicalizedJsonLdObjectWithoutProof = this.canonicalize(jsonLdObjectWithoutProof);
54+
byte[] canonicalizedJsonLdObjectWithoutProofHash = this.hash(canonicalizedJsonLdObjectWithoutProof.getBytes(StandardCharsets.UTF_8));
55+
if (log.isDebugEnabled()) log.debug("Canonicalized LD object without proof: {}", canonicalizedJsonLdObjectWithoutProof);
56+
if (log.isDebugEnabled()) log.debug("Hashed canonicalized LD object without proof: {}", Hex.encodeHexString(canonicalizedJsonLdObjectWithoutProofHash));
4957

5058
String canonicalizedLdProofWithoutProofValues = this.canonicalize(dataIntegrityProofWithoutProofValues);
51-
52-
// hashing
53-
54-
byte[] canonicalizedJsonLdObjectWithoutProofHash = this.hash(canonicalizedJsonLdObjectWithoutProof.getBytes(StandardCharsets.UTF_8));
5559
byte[] canonicalizedLdProofWithoutProofValuesHash = this.hash(canonicalizedLdProofWithoutProofValues.getBytes(StandardCharsets.UTF_8));
60+
if (log.isDebugEnabled()) log.debug("Canonicalized LD proof without proof value: {}", canonicalizedLdProofWithoutProofValues);
61+
if (log.isDebugEnabled()) log.debug("Hashed canonicalized LD proof without proof value: {}", Hex.encodeHexString(canonicalizedLdProofWithoutProofValuesHash));
5662

5763
// construct the canonicalization result
5864

src/main/java/com/danubetech/dataintegrity/canonicalizer/JCSSHA512Canonicalizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static JCSSHA512Canonicalizer getInstance() {
1313
}
1414

1515
public int hashLength() {
16-
return 48;
16+
return 64;
1717
}
1818

1919
public byte[] hash(byte[] input) throws GeneralSecurityException {

src/main/java/com/danubetech/dataintegrity/canonicalizer/RDFC10Canonicalizer.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
package com.danubetech.dataintegrity.canonicalizer;
22

3-
import com.apicatalog.jsonld.lang.Keywords;
43
import com.apicatalog.rdf.RdfDataset;
54
import com.apicatalog.rdf.RdfNQuad;
65
import com.apicatalog.rdf.canon.RdfCanonicalizer;
76
import com.apicatalog.rdf.io.nquad.NQuadsWriter;
87
import com.danubetech.dataintegrity.DataIntegrityProof;
98
import foundation.identity.jsonld.JsonLDException;
109
import foundation.identity.jsonld.JsonLDObject;
11-
import foundation.identity.jsonld.JsonLDUtils;
10+
import org.apache.commons.codec.binary.Hex;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
1213

1314
import java.io.IOException;
1415
import java.io.StringWriter;
1516
import java.nio.charset.StandardCharsets;
1617
import java.security.GeneralSecurityException;
1718
import java.util.Collection;
1819
import java.util.List;
19-
import java.util.Objects;
2020

2121
public abstract class RDFC10Canonicalizer extends Canonicalizer {
2222

23+
private static final Logger log = LoggerFactory.getLogger(RDFC10Canonicalizer.class);
24+
2325
public RDFC10Canonicalizer() {
2426
super(List.of("RDFC-1.0"));
2527
}
@@ -59,14 +61,15 @@ public byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject j
5961

6062
jsonLdObjectWithoutProof.setDocumentLoader(jsonLdObject.getDocumentLoader());
6163
String canonicalizedJsonLdObjectWithoutProof = this.canonicalize(jsonLdObjectWithoutProof);
64+
byte[] canonicalizedJsonLdObjectWithoutProofHash = this.hash(canonicalizedJsonLdObjectWithoutProof.getBytes(StandardCharsets.UTF_8));
65+
if (log.isDebugEnabled()) log.debug("Canonicalized LD object without proof: {}", canonicalizedJsonLdObjectWithoutProof);
66+
if (log.isDebugEnabled()) log.debug("Hashed canonicalized LD object without proof: {}", Hex.encodeHexString(canonicalizedJsonLdObjectWithoutProofHash));
6267

6368
dataIntegrityProofWithoutProofValues.setDocumentLoader(jsonLdObject.getDocumentLoader());
6469
String canonicalizedLdProofWithoutProofValues = this.canonicalize(dataIntegrityProofWithoutProofValues);
65-
66-
// hashing
67-
68-
byte[] canonicalizedJsonLdObjectWithoutProofHash = this.hash(canonicalizedJsonLdObjectWithoutProof.getBytes(StandardCharsets.UTF_8));
6970
byte[] canonicalizedLdProofWithoutProofValuesHash = this.hash(canonicalizedLdProofWithoutProofValues.getBytes(StandardCharsets.UTF_8));
71+
if (log.isDebugEnabled()) log.debug("Canonicalized LD proof without proof value: {}", canonicalizedLdProofWithoutProofValues);
72+
if (log.isDebugEnabled()) log.debug("Hashed canonicalized LD proof without proof value: {}", Hex.encodeHexString(canonicalizedLdProofWithoutProofValuesHash));
7073

7174
// construct the canonicalization result
7275

src/main/java/com/danubetech/dataintegrity/canonicalizer/URDNA2015Canonicalizer.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import foundation.identity.jsonld.JsonLDException;
88
import foundation.identity.jsonld.JsonLDObject;
99
import io.setl.rdf.normalization.RdfNormalize;
10+
import org.apache.commons.codec.binary.Hex;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
1013

1114
import java.io.IOException;
1215
import java.io.StringWriter;
@@ -16,6 +19,8 @@
1619

1720
public class URDNA2015Canonicalizer extends Canonicalizer {
1821

22+
private static final Logger log = LoggerFactory.getLogger(RDFC10Canonicalizer.class);
23+
1924
public static final URDNA2015Canonicalizer INSTANCE = new URDNA2015Canonicalizer();
2025

2126
public URDNA2015Canonicalizer() {
@@ -61,14 +66,15 @@ public byte[] canonicalize(DataIntegrityProof dataIntegrityProof, JsonLDObject j
6166

6267
jsonLdObjectWithoutProof.setDocumentLoader(jsonLdObject.getDocumentLoader());
6368
String canonicalizedJsonLdObjectWithoutProof = this.canonicalize(jsonLdObjectWithoutProof);
69+
byte[] canonicalizedJsonLdObjectWithoutProofHash = SHAUtil.sha256(canonicalizedJsonLdObjectWithoutProof);
70+
if (log.isDebugEnabled()) log.debug("Canonicalized LD object without proof: {}", canonicalizedJsonLdObjectWithoutProof);
71+
if (log.isDebugEnabled()) log.debug("Hashed canonicalized LD object without proof: {}", Hex.encodeHexString(canonicalizedJsonLdObjectWithoutProofHash));
6472

6573
dataIntegrityProofWithoutProofValues.setDocumentLoader(jsonLdObject.getDocumentLoader());
6674
String canonicalizedLdProofWithoutProofValues = this.canonicalize(dataIntegrityProofWithoutProofValues);
67-
68-
// hashing
69-
70-
byte[] canonicalizedJsonLdObjectWithoutProofHash = SHAUtil.sha256(canonicalizedJsonLdObjectWithoutProof);
7175
byte[] canonicalizedLdProofWithoutProofValuesHash = SHAUtil.sha256(canonicalizedLdProofWithoutProofValues);
76+
if (log.isDebugEnabled()) log.debug("Canonicalized LD proof without proof value: {}", canonicalizedLdProofWithoutProofValues);
77+
if (log.isDebugEnabled()) log.debug("Hashed canonicalized LD proof without proof value: {}", Hex.encodeHexString(canonicalizedLdProofWithoutProofValuesHash));
7278

7379
// construct the canonicalization result
7480

src/main/java/com/danubetech/dataintegrity/signer/DataIntegrityProofLdSigner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public Canonicalizer getCanonicalizer(DataIntegrityProof dataIntegrityProof) {
5050
if (algorithm == null) throw new IllegalStateException("No algorithm: " + this.getSigner());
5151
Canonicalizer canonicalizer = DataIntegritySuites.DATA_INTEGRITY_SUITE_DATAINTEGRITYPROOF.findCanonicalizerForCryptosuiteAndAlgorithm(cryptosuite, algorithm);
5252
if (canonicalizer == null) throw new IllegalArgumentException("No canonicalizer for cryptosuite " + cryptosuite + " and algorithm " + algorithm + ": " + canonicalizer);
53+
if (log.isDebugEnabled()) log.debug("Determined canonicalizer for algorithm {} and cryptosuite {}: {}", algorithm, cryptosuite, canonicalizer.getClass().getSimpleName());
5354
return canonicalizer;
5455
}
5556

src/main/java/com/danubetech/dataintegrity/signer/LdSigner.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.net.URI;
1717
import java.security.GeneralSecurityException;
1818
import java.util.Date;
19+
import java.util.Objects;
1920

2021
public abstract class LdSigner<DATAINTEGRITYSUITE extends DataIntegritySuite> {
2122

@@ -106,7 +107,7 @@ public DataIntegrityProof sign(JsonLDObject jsonLdObject, boolean addToJsonLdObj
106107
// add LD contexts to LD proof options if missing
107108

108109
if (dataIntegrityProof.getContexts() == null || dataIntegrityProof.getContexts().isEmpty()) {
109-
JsonLDUtils.jsonLdAdd(dataIntegrityProof, Keywords.CONTEXT, jsonLdObject.getContexts().stream().map(JsonLDUtils::uriToString).toList());
110+
JsonLDUtils.jsonLdAdd(dataIntegrityProof, Keywords.CONTEXT, jsonLdObject.getContexts().stream().map(JsonLDUtils::uriToString).filter(Objects::nonNull).toList());
110111
}
111112

112113
// obtain the canonicalized document

src/main/java/com/danubetech/dataintegrity/verifier/DataIntegrityProofLdVerifier.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public Canonicalizer getCanonicalizer(DataIntegrityProof dataIntegrityProof) {
4646
if (algorithm == null) throw new IllegalStateException("No algorithm: " + this.getVerifier());
4747
Canonicalizer canonicalizer = DataIntegritySuites.DATA_INTEGRITY_SUITE_DATAINTEGRITYPROOF.findCanonicalizerForCryptosuiteAndAlgorithm(cryptosuite, algorithm);
4848
if (canonicalizer == null) throw new IllegalArgumentException("No canonicalizer for cryptosuite " + cryptosuite + " and algorithm " + algorithm + ": " + canonicalizer);
49+
if (log.isDebugEnabled()) log.debug("Determined canonicalizer for algorithm {} and cryptosuite {}: {}", algorithm, cryptosuite, canonicalizer.getClass().getSimpleName());
4950
return canonicalizer;
5051
}
5152

src/main/java/com/danubetech/dataintegrity/verifier/LdVerifier.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.danubetech.dataintegrity.verifier;
22

3+
import com.apicatalog.jsonld.lang.Keywords;
34
import com.danubetech.dataintegrity.DataIntegrityProof;
45
import com.danubetech.dataintegrity.canonicalizer.Canonicalizer;
56
import com.danubetech.dataintegrity.suites.DataIntegritySuite;
67
import com.danubetech.keyformats.crypto.ByteVerifier;
78
import foundation.identity.jsonld.JsonLDException;
89
import foundation.identity.jsonld.JsonLDObject;
10+
import foundation.identity.jsonld.JsonLDUtils;
911
import org.apache.commons.codec.binary.Hex;
1012
import org.slf4j.Logger;
1113
import org.slf4j.LoggerFactory;
1214

1315
import java.io.IOException;
1416
import java.security.GeneralSecurityException;
17+
import java.util.Objects;
1518

1619
public abstract class LdVerifier<DATAINTEGRITYSUITE extends DataIntegritySuite> {
1720

@@ -55,10 +58,16 @@ public boolean verify(JsonLDObject jsonLdObject, DataIntegrityProof dataIntegrit
5558

5659
this.initialize(dataIntegrityProof);
5760

61+
// add LD contexts to LD proof options if missing
62+
63+
if (dataIntegrityProof.getContexts() == null || dataIntegrityProof.getContexts().isEmpty()) {
64+
JsonLDUtils.jsonLdAdd(dataIntegrityProof, Keywords.CONTEXT, jsonLdObject.getContexts().stream().map(JsonLDUtils::uriToString).filter(Objects::nonNull).toList());
65+
}
66+
5867
// obtain the canonicalized document
5968

6069
Canonicalizer canonicalizer = this.getCanonicalizer(dataIntegrityProof);
61-
byte[] canonicalizationResult = this.getCanonicalizer(dataIntegrityProof).canonicalize(dataIntegrityProof, jsonLdObject);
70+
byte[] canonicalizationResult = canonicalizer.canonicalize(dataIntegrityProof, jsonLdObject);
6271
if (log.isDebugEnabled()) log.debug("Canonicalization result with {}: {}", canonicalizer.getClass().getSimpleName(), Hex.encodeHexString(canonicalizationResult));
6372

6473
// verify

src/test/java/com/danubetech/dataintegrity/BasicSignTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.danubetech.dataintegrity;
22

3+
import com.danubetech.dataintegrity.util.TestKeys;
34
import com.nimbusds.jose.*;
45
import com.nimbusds.jose.crypto.RSASSASigner;
56
import org.junit.jupiter.api.Test;
@@ -30,7 +31,7 @@ public void testSign() throws Exception {
3031

3132
JWSObject jwsObject = new JWSObject(jwsHeader, payload);
3233

33-
JWSSigner jwsSigner = new RSASSASigner(TestUtil.testRSAPrivateKey.getPrivate());
34+
JWSSigner jwsSigner = new RSASSASigner(TestKeys.testRSAPrivateKey.getPrivate());
3435
jwsObject.sign(jwsSigner);
3536
signatureValue = jwsObject.serialize(true);
3637

src/test/java/com/danubetech/dataintegrity/BasicVerifyTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.danubetech.dataintegrity;
22

3+
import com.danubetech.dataintegrity.util.DetachedJWSObject;
4+
import com.danubetech.dataintegrity.util.TestKeys;
35
import com.nimbusds.jose.JWSVerifier;
46
import com.nimbusds.jose.Payload;
57
import com.nimbusds.jose.crypto.RSASSAVerifier;
6-
import com.danubetech.dataintegrity.util.DetachedJWSObject;
78
import org.junit.jupiter.api.Test;
89

910
import java.util.Collections;
@@ -29,7 +30,7 @@ public void testVerify() throws Exception {
2930

3031
DetachedJWSObject jwsObject = DetachedJWSObject.parse(signatureValue, jwsPayload);
3132

32-
JWSVerifier jwsVerifier = new RSASSAVerifier(TestUtil.testRSAPublicKey, Collections.singleton("b64"));
33+
JWSVerifier jwsVerifier = new RSASSAVerifier(TestKeys.testRSAPublicKey, Collections.singleton("b64"));
3334
verify = jwsVerifier.verify(jwsObject.getHeader(), jwsObject.getSigningInput(), jwsObject.getParsedSignature());
3435

3536
// done

0 commit comments

Comments
 (0)