Skip to content

Commit e88f1e6

Browse files
committed
fix assertion hash
1 parent b64d181 commit e88f1e6

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

sdk/src/main/java/io/opentdf/platform/sdk/Manifest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.nimbusds.jwt.JWTClaimsSet;
2424
import com.nimbusds.jwt.SignedJWT;
2525
import io.opentdf.platform.sdk.TDF.AssertionException;
26+
import org.apache.commons.codec.binary.Hex;
2627
import org.erdtman.jcs.JsonCanonicalizer;
2728

2829
import java.io.IOException;
@@ -351,7 +352,7 @@ public int hashCode() {
351352
return Objects.hash(id, type, scope, appliesToState, statement, binding);
352353
}
353354

354-
public byte[] hash() throws IOException {
355+
public String hash() throws IOException {
355356
MessageDigest digest;
356357
try {
357358
digest = MessageDigest.getInstance("SHA-256");
@@ -361,7 +362,7 @@ public byte[] hash() throws IOException {
361362

362363
var assertionAsJson = gson.toJson(this);
363364
JsonCanonicalizer jc = new JsonCanonicalizer(assertionAsJson);
364-
return digest.digest(jc.getEncodedUTF8());
365+
return Hex.encodeHexString(digest.digest(jc.getEncodedUTF8()));
365366
}
366367

367368
// Sign the assertion with the given hash and signature using the key.

sdk/src/main/java/io/opentdf/platform/sdk/TDF.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ private static byte[] calculateSignature(byte[] data, byte[] secret, Config.Inte
417417
public TDFObject createTDF(InputStream payload,
418418
OutputStream outputStream,
419419
Config.TDFConfig tdfConfig, SDK.KAS kas, AttributesServiceFutureStub attrService)
420-
throws IOException, JOSEException, AutoConfigureException, InterruptedException, ExecutionException {
420+
throws IOException, JOSEException, AutoConfigureException, InterruptedException, ExecutionException, DecoderException {
421421

422422
if (tdfConfig.autoconfigure) {
423423
Autoconfigure.Granter granter = new Autoconfigure.Granter(new ArrayList<>());
@@ -532,7 +532,8 @@ public TDFObject createTDF(InputStream payload,
532532
assertion.statement = assertionConfig.statement;
533533
assertion.appliesToState = assertionConfig.appliesToState.toString();
534534

535-
var assertionHash = assertion.hash();
535+
var assertionHashAsHex = assertion.hash();
536+
var assertionHash = Hex.decodeHex(assertionHashAsHex);
536537
byte[] completeHash = new byte[aggregateHash.size() + assertionHash.length];
537538
System.arraycopy(aggregateHash.toByteArray(), 0, completeHash, 0, aggregateHash.size());
538539
System.arraycopy(assertionHash, 0, completeHash, aggregateHash.size(), assertionHash.length);
@@ -545,7 +546,7 @@ public TDFObject createTDF(InputStream payload,
545546
assertionSigningKey = assertionConfig.assertionKey;
546547
}
547548
var hashValues = new Manifest.Assertion.HashValues(
548-
Base64.getEncoder().encodeToString(assertionHash),
549+
assertionHashAsHex,
549550
encodedHash
550551
);
551552
assertion.sign(hashValues, assertionSigningKey);
@@ -707,7 +708,7 @@ public Reader loadTDF(SeekableByteChannel tdf, SDK.KAS kas,
707708
throw new SegmentSizeMismatch("mismatch encrypted segment size in manifest");
708709
}
709710

710-
var aggregateSignatureBytes = aggregateHash.toByteArray();
711+
var aggregateHashByteArrayBytes = aggregateHash.toByteArray();
711712
// Validate assertions
712713
for (var assertion : manifest.assertions) {
713714
// Skip assertion verification if disabled
@@ -726,22 +727,17 @@ public Reader loadTDF(SeekableByteChannel tdf, SDK.KAS kas,
726727
}
727728

728729
var hashValues = assertion.verify(assertionKey);
729-
var assertionAsJson = gson.toJson(assertion);
730-
JsonCanonicalizer jc = new JsonCanonicalizer(assertionAsJson);
731-
var hashOfAssertion = digest.digest(jc.getEncodedUTF8());
732-
var assertionCompare = isLegacyTdf ? Hex.encodeHexString(hashOfAssertion) : Base64.getEncoder().encodeToString(hashOfAssertion);
730+
var hashOfAssertionAsHex = assertion.hash();
733731

734-
if (!Objects.equals(assertionCompare, hashValues.getAssertionHash())) {
732+
if (!Objects.equals(hashOfAssertionAsHex, hashValues.getAssertionHash())) {
735733
throw new AssertionException("assertion hash mismatch", assertion.id);
736734
}
737735

738-
if (isLegacyTdf) {
739-
hashOfAssertion = Hex.encodeHexString(hashOfAssertion).getBytes();
740-
}
736+
var hashOfAssertion = Hex.decodeHex(hashOfAssertionAsHex);
741737

742-
var signature = new byte[aggregateSignatureBytes.length + hashOfAssertion.length];
743-
System.arraycopy(aggregateSignatureBytes, 0, signature, 0, aggregateSignatureBytes.length);
744-
System.arraycopy(hashOfAssertion, 0, signature, aggregateSignatureBytes.length, hashOfAssertion.length);
738+
var signature = new byte[aggregateHashByteArrayBytes.length + hashOfAssertion.length];
739+
System.arraycopy(aggregateHashByteArrayBytes, 0, signature, 0, aggregateHashByteArrayBytes.length);
740+
System.arraycopy(hashOfAssertion, 0, signature, aggregateHashByteArrayBytes.length, hashOfAssertion.length);
745741
var encodeSignature = Base64.getEncoder().encodeToString(signature);
746742

747743
if (!Objects.equals(encodeSignature, hashValues.getSignature())) {

0 commit comments

Comments
 (0)