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 6, 2024
1 parent b7dcd8a commit 86c3d24
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
34 changes: 27 additions & 7 deletions lib/tdf3/src/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ export function isAssertionConfig(obj: unknown): obj is AssertionConfig {
*/
export async function verify(
thiz: Assertion,
aggregateHash: string,
key: AssertionKey
aggregateHash: Uint8Array,
key: AssertionKey,
isLegacyTDF: boolean
): Promise<void> {
let payload: AssertionPayload;
try {
Expand All @@ -126,14 +127,23 @@ export async function verify(

// Get the hash of the assertion
const hashOfAssertion = await hash(thiz);
const combinedHash = aggregateHash + hashOfAssertion;
const encodedHash = base64.encode(combinedHash);

// check if assertionHash is same as hashOfAssertion
if (hashOfAssertion !== assertionHash) {
throw new IntegrityError('Assertion hash mismatch');
}

let encodedHash: string;
if (isLegacyTDF) {
const aggregateHashAsStr = new TextDecoder('utf-8').decode(aggregateHash);
const combinedHash = aggregateHashAsStr + hashOfAssertion;
encodedHash = base64.encode(combinedHash);
} else {
const combinedHash = concatenateUint8Arrays(aggregateHash,
new Uint8Array(hex.decodeArrayBuffer(assertionHash)));
encodedHash = base64.encodeArrayBuffer(combinedHash);
}

// check if assertionSig is same as encodedHash
if (assertionSig !== encodedHash) {
throw new IntegrityError('Failed integrity check on assertion signature');
Expand All @@ -144,7 +154,7 @@ export async function verify(
* Creates an Assertion object with the specified properties.
*/
export async function CreateAssertion(
aggregateHash: string,
aggregateHash: Uint8Array,
assertionConfig: AssertionConfig
): Promise<Assertion> {
if (!assertionConfig.signingKey) {
Expand All @@ -162,8 +172,8 @@ export async function CreateAssertion(
};

const assertionHash = await hash(a);
const combinedHash = aggregateHash + assertionHash;
const encodedHash = base64.encode(combinedHash);
const combinedHash = concatenateUint8Arrays(aggregateHash, new Uint8Array(hex.decodeArrayBuffer(assertionHash)));
const encodedHash = base64.encodeArrayBuffer(combinedHash);

return await sign(a, assertionHash, encodedHash, assertionConfig.signingKey);
}
Expand All @@ -189,3 +199,13 @@ export type AssertionVerificationKeys = {
DefaultKey?: AssertionKey;
Keys: Record<string, AssertionKey>;
};

function concatenateUint8Arrays(array1: Uint8Array, array2: Uint8Array): Uint8Array {
const combinedLength = array1.length + array2.length;
const combinedArray = new Uint8Array(combinedLength);

combinedArray.set(array1, 0);
combinedArray.set(array2, array1.length);

return combinedArray;
}
17 changes: 10 additions & 7 deletions lib/tdf3/src/tdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,7 @@ export async function writeStream(cfg: EncryptConfiguration): Promise<DecoratedR
alg: 'HS256',
key: new Uint8Array(cfg.keyForEncryption.unwrappedKeyBinary.asArrayBuffer()),
};
const combinedHashString = new TextDecoder().decode(aggregateHash);
const assertion = await assertions.CreateAssertion(combinedHashString, {
const assertion = await assertions.CreateAssertion(aggregateHash, {
...assertionConfig,
signingKey,
});
Expand Down Expand Up @@ -1239,16 +1238,20 @@ export async function readStream(cfg: DecryptConfiguration) {
// check if the TDF is a legacy TDF
const isLegacyTDF = manifest.tdf_spec_version ? false : true;

// check the combined string of hashes
const aggregateHash = segments.map(({ hash }) => base64.decode(hash)).join('');
// Decode each hash and store it in an array of Uint8Array
const segmentHashList = segments.map(({ hash }) => new Uint8Array(base64.decodeArrayBuffer(hash)));

// Concatenate all segment hashes into a single Uint8Array
const aggregateHash = await concatenateUint8Array(segmentHashList);

const integrityAlgorithm = rootSignature.alg;
if (integrityAlgorithm !== 'GMAC' && integrityAlgorithm !== 'HS256') {
throw new UnsupportedError(`Unsupported integrity alg [${integrityAlgorithm}]`);
}

const payloadForSigCalculation = isLegacyTDF
? Binary.fromString(hex.encode(aggregateHash))
: Binary.fromString(aggregateHash);
? Binary.fromString(hex.encodeArrayBuffer(aggregateHash))
: Binary.fromArrayBuffer(aggregateHash.buffer);
const payloadSigInHex = await getSignature(
keyForDecryption,
payloadForSigCalculation,
Expand Down Expand Up @@ -1278,7 +1281,7 @@ export async function readStream(cfg: DecryptConfiguration) {
assertionKey = foundKey;
}
}
await assertions.verify(assertion, aggregateHash, assertionKey);
await assertions.verify(assertion, aggregateHash, assertionKey, isLegacyTDF);
}
}

Expand Down

0 comments on commit 86c3d24

Please sign in to comment.