Skip to content

Commit

Permalink
fix assertion hash
Browse files Browse the repository at this point in the history
  • Loading branch information
sujankota committed Dec 17, 2024
1 parent b64d181 commit e88f1e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
5 changes: 3 additions & 2 deletions sdk/src/main/java/io/opentdf/platform/sdk/Manifest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import io.opentdf.platform.sdk.TDF.AssertionException;
import org.apache.commons.codec.binary.Hex;
import org.erdtman.jcs.JsonCanonicalizer;

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

public byte[] hash() throws IOException {
public String hash() throws IOException {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
Expand All @@ -361,7 +362,7 @@ public byte[] hash() throws IOException {

var assertionAsJson = gson.toJson(this);
JsonCanonicalizer jc = new JsonCanonicalizer(assertionAsJson);
return digest.digest(jc.getEncodedUTF8());
return Hex.encodeHexString(digest.digest(jc.getEncodedUTF8()));
}

// Sign the assertion with the given hash and signature using the key.
Expand Down
26 changes: 11 additions & 15 deletions sdk/src/main/java/io/opentdf/platform/sdk/TDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private static byte[] calculateSignature(byte[] data, byte[] secret, Config.Inte
public TDFObject createTDF(InputStream payload,
OutputStream outputStream,
Config.TDFConfig tdfConfig, SDK.KAS kas, AttributesServiceFutureStub attrService)
throws IOException, JOSEException, AutoConfigureException, InterruptedException, ExecutionException {
throws IOException, JOSEException, AutoConfigureException, InterruptedException, ExecutionException, DecoderException {

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

var assertionHash = assertion.hash();
var assertionHashAsHex = assertion.hash();
var assertionHash = Hex.decodeHex(assertionHashAsHex);
byte[] completeHash = new byte[aggregateHash.size() + assertionHash.length];
System.arraycopy(aggregateHash.toByteArray(), 0, completeHash, 0, aggregateHash.size());
System.arraycopy(assertionHash, 0, completeHash, aggregateHash.size(), assertionHash.length);
Expand All @@ -545,7 +546,7 @@ public TDFObject createTDF(InputStream payload,
assertionSigningKey = assertionConfig.assertionKey;
}
var hashValues = new Manifest.Assertion.HashValues(
Base64.getEncoder().encodeToString(assertionHash),
assertionHashAsHex,
encodedHash
);
assertion.sign(hashValues, assertionSigningKey);
Expand Down Expand Up @@ -707,7 +708,7 @@ public Reader loadTDF(SeekableByteChannel tdf, SDK.KAS kas,
throw new SegmentSizeMismatch("mismatch encrypted segment size in manifest");
}

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

var hashValues = assertion.verify(assertionKey);
var assertionAsJson = gson.toJson(assertion);
JsonCanonicalizer jc = new JsonCanonicalizer(assertionAsJson);
var hashOfAssertion = digest.digest(jc.getEncodedUTF8());
var assertionCompare = isLegacyTdf ? Hex.encodeHexString(hashOfAssertion) : Base64.getEncoder().encodeToString(hashOfAssertion);
var hashOfAssertionAsHex = assertion.hash();

if (!Objects.equals(assertionCompare, hashValues.getAssertionHash())) {
if (!Objects.equals(hashOfAssertionAsHex, hashValues.getAssertionHash())) {
throw new AssertionException("assertion hash mismatch", assertion.id);
}

if (isLegacyTdf) {
hashOfAssertion = Hex.encodeHexString(hashOfAssertion).getBytes();
}
var hashOfAssertion = Hex.decodeHex(hashOfAssertionAsHex);

var signature = new byte[aggregateSignatureBytes.length + hashOfAssertion.length];
System.arraycopy(aggregateSignatureBytes, 0, signature, 0, aggregateSignatureBytes.length);
System.arraycopy(hashOfAssertion, 0, signature, aggregateSignatureBytes.length, hashOfAssertion.length);
var signature = new byte[aggregateHashByteArrayBytes.length + hashOfAssertion.length];
System.arraycopy(aggregateHashByteArrayBytes, 0, signature, 0, aggregateHashByteArrayBytes.length);
System.arraycopy(hashOfAssertion, 0, signature, aggregateHashByteArrayBytes.length, hashOfAssertion.length);
var encodeSignature = Base64.getEncoder().encodeToString(signature);

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

0 comments on commit e88f1e6

Please sign in to comment.