Skip to content

Commit

Permalink
rename split sid
Browse files Browse the repository at this point in the history
  • Loading branch information
dmihalcik-virtru committed Jul 31, 2024
1 parent 5372dd5 commit 3e8cca7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/tdf3/src/client/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type EncryptStreamMiddleware = (

export type SplitStep = {
kas: string;
split?: string;
sid?: string;
};

export type EncryptParams = {
Expand Down
4 changes: 2 additions & 2 deletions lib/tdf3/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export class Client {

const splits: SplitStep[] = splitPlan || [{kas: this.kasEndpoint}]
encryptionInformation.keyAccess = await Promise.all(
splits.map(async ({kas, split}) => {
splits.map(async ({kas, sid}) => {
const kasPublicKey = await this.kasKeys[kas];
return buildKeyAccess({
attributeSet,
Expand All @@ -402,7 +402,7 @@ export class Client {
kid: kasPublicKey.kid,
publicKey: kasPublicKey.publicKey,
metadata,
split,
sid,
});
})
);
Expand Down
8 changes: 4 additions & 4 deletions lib/tdf3/src/models/encryption-information.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type Segment = {
readonly encryptedSegmentSize?: number;
};

export type SplitType = 'split' | 'flat';
export type SplitType = 'split';

export type EncryptionInformation = {
readonly type: SplitType;
Expand Down Expand Up @@ -77,18 +77,18 @@ export class SplitKey {
}

async getKeyAccessObjects(policy: Policy, keyInfo: KeyInfo): Promise<KeyAccessObject[]> {
const splitIds = [...new Set(this.keyAccess.map(({ split }) => split))].sort();
const splitIds = [...new Set(this.keyAccess.map(({ sid }) => sid))].sort();
const unwrappedKeySplitBuffers = await keySplit(
new Uint8Array(keyInfo.unwrappedKeyBinary.asByteArray()),
splitIds.length,
this.cryptoService
);
const splitsByName = Object.fromEntries(splitIds.map((split, index) => [split, unwrappedKeySplitBuffers[index]]));
const splitsByName = Object.fromEntries(splitIds.map((sid, index) => [sid, unwrappedKeySplitBuffers[index]]));

const keyAccessObjects = [];
for (const item of this.keyAccess) {
// use the key split to encrypt metadata for each key access object
const unwrappedKeySplitBuffer = splitsByName[item.split];
const unwrappedKeySplitBuffer = splitsByName[item.sid];
const unwrappedKeySplitBinary = Binary.fromArrayBuffer(unwrappedKeySplitBuffer.buffer);

const metadata = item.metadata || '';
Expand Down
10 changes: 5 additions & 5 deletions lib/tdf3/src/models/key-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Wrapped {
public readonly kid: string | undefined,
public readonly publicKey: string,
public readonly metadata: unknown,
public readonly split: string
public readonly sid: string
) {}

async write(
Expand Down Expand Up @@ -52,8 +52,8 @@ export class Wrapped {
if (this.kid) {
this.keyAccessObject.kid = this.kid;
}
if (this.split?.length) {
this.keyAccessObject.split = this.split;
if (this.sid?.length) {
this.keyAccessObject.sid = this.sid;
}

return this.keyAccessObject;
Expand All @@ -71,7 +71,7 @@ export class Remote {
public readonly kid: string | undefined,
public readonly publicKey: string,
public readonly metadata: unknown,
public readonly split: string
public readonly sid: string
) {}

async write(
Expand Down Expand Up @@ -114,7 +114,7 @@ export class Remote {
export type KeyAccess = Remote | Wrapped;

export type KeyAccessObject = {
split?: string;
sid?: string;
type: KeyAccessType;
url: string;
kid?: string;
Expand Down
16 changes: 8 additions & 8 deletions lib/tdf3/src/tdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export type BuildKeyAccess = {
publicKey: string;
attributeUrl?: string;
metadata?: Metadata;
split?: string;
sid?: string;
};

type Segment = {
Expand Down Expand Up @@ -343,7 +343,7 @@ export async function buildKeyAccess({
kid,
attributeUrl,
metadata,
split = '',
sid = '',
}: BuildKeyAccess): Promise<KeyAccess> {
/** Internal function to keep it DRY */
function createKeyAccess(
Expand All @@ -355,9 +355,9 @@ export async function buildKeyAccess({
) {
switch (type) {
case 'wrapped':
return new KeyAccessWrapped(kasUrl, kasKeyIdentifier, pubKey, metadata, split);
return new KeyAccessWrapped(kasUrl, kasKeyIdentifier, pubKey, metadata, sid);
case 'remote':
return new KeyAccessRemote(kasUrl, kasKeyIdentifier, pubKey, metadata, split);
return new KeyAccessRemote(kasUrl, kasKeyIdentifier, pubKey, metadata, sid);
default:
throw new KeyAccessError(`buildKeyAccess: Key access type ${type} is unknown`);
}
Expand Down Expand Up @@ -805,8 +805,8 @@ async function loadTDFStream(
}

export function splitLookupTableFactory(keyAccess: KeyAccessObject[], allowedKases: string[]): Record<string, Record<string, KeyAccessObject>> {
const splitIds = new Set(keyAccess.map(({split}) => split || ''));
const accessibleSplits = new Set(keyAccess.filter(({url}) => allowedKases.includes(url)).map(({split}) => split));
const splitIds = new Set(keyAccess.map(({sid}) => sid || ''));
const accessibleSplits = new Set(keyAccess.filter(({url}) => allowedKases.includes(url)).map(({sid}) => sid));
if (splitIds.size > accessibleSplits.size) {
const disallowedKases = new Set(keyAccess.filter(({url}) => !allowedKases.includes(url)).map(({url}) => url));
throw new KasDecryptError(
Expand All @@ -815,9 +815,9 @@ export function splitLookupTableFactory(keyAccess: KeyAccessObject[], allowedKas
}
const splitPotentials: Record<string, Record<string, KeyAccessObject>> = Object.fromEntries([...splitIds].map(s => [s, {}]));
for (const kao of keyAccess) {
const disjunction = splitPotentials[kao.split || ''];
const disjunction = splitPotentials[kao.sid || ''];
if (kao.url in disjunction) {
throw new KasDecryptError(`TODO: Fallback to no split ids. Repetition found for [${kao.url}] on split [${kao.split}]`);
throw new KasDecryptError(`TODO: Fallback to no split ids. Repetition found for [${kao.url}] on split [${kao.sid}]`);
}
if (allowedKases.includes(kao.url)) {
disjunction[kao.url] = kao;
Expand Down

0 comments on commit 3e8cca7

Please sign in to comment.