Skip to content

Commit a39efe6

Browse files
v1 backwards compatability
1 parent 098e396 commit a39efe6

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

lib/src/nanotdf/Client.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
cryptoPublicToPem,
1919
getRequiredObligationFQNs,
2020
pemToCryptoPublicKey,
21+
upgradeRewrapResponseV1,
2122
validateSecureUrl,
2223
} from '../utils.js';
2324

@@ -282,6 +283,13 @@ export default class Client {
282283
algorithm: DefaultParams.defaultECAlgorithm,
283284
}),
284285
],
286+
keyAccess: {
287+
header: new Uint8Array(nanoTdfHeader),
288+
kasUrl: '',
289+
protocol: Client.KAS_PROTOCOL,
290+
keyType: Client.KEY_ACCESS_REMOTE,
291+
},
292+
algorithm: DefaultParams.defaultECAlgorithm,
285293
});
286294

287295
const requestBodyStr = toJsonString(UnsignedRewrapRequestSchema, unsignedRequest);
@@ -299,6 +307,7 @@ export default class Client {
299307
this.authProvider,
300308
this.fulfillableObligationFQNs
301309
);
310+
upgradeRewrapResponseV1(rewrapResp);
302311

303312
// Assume only one response and one result for now (V1 style)
304313
const result = rewrapResp.responses[0].results[0];

lib/src/utils.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { exportSPKI, importX509 } from 'jose';
33
import { base64 } from './encodings/index.js';
44
import { pemCertToCrypto, pemPublicToCrypto } from './nanotdf-crypto/pemPublicToCrypto.js';
55
import { ConfigurationError } from './errors.js';
6-
import { RewrapResponse } from './platform/kas/kas_pb.js';
6+
import {
7+
RewrapResponse,
8+
PolicyRewrapResultSchema,
9+
KeyAccessRewrapResultSchema,
10+
} from './platform/kas/kas_pb.js';
11+
import { create } from '@bufbuild/protobuf';
712
import { ConnectError } from '@connectrpc/connect';
813

914
const REQUIRED_OBLIGATIONS_METADATA_KEY = 'X-Required-Obligations';
@@ -255,3 +260,31 @@ export function getRequiredObligationFQNs(response: RewrapResponse) {
255260

256261
return [...requiredObligations.values()];
257262
}
263+
264+
/**
265+
* Upgrades a RewrapResponse from v1 format to v2.
266+
*/
267+
export function upgradeRewrapResponseV1(response: RewrapResponse) {
268+
if (response.responses.length > 0) {
269+
return;
270+
}
271+
if (response.entityWrappedKey.length === 0) {
272+
return;
273+
}
274+
275+
response.responses = [
276+
create(PolicyRewrapResultSchema, {
277+
policyId: 'policy',
278+
results: [
279+
create(KeyAccessRewrapResultSchema, {
280+
keyAccessObjectId: 'kao-0',
281+
status: 'permit',
282+
result: {
283+
case: 'kasWrappedKey',
284+
value: response.entityWrappedKey,
285+
},
286+
}),
287+
],
288+
}),
289+
];
290+
}

lib/tdf3/src/tdf.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import { ZipReader, ZipWriter, keyMerge, concatUint8, buffToString } from './uti
6363
import { CentralDirectory } from './utils/zip-reader.js';
6464
import { ztdfSalt } from './crypto/salt.js';
6565
import { Payload } from './models/payload.js';
66-
import { getRequiredObligationFQNs } from '../../src/utils.js';
66+
import { getRequiredObligationFQNs, upgradeRewrapResponseV1 } from '../../src/utils.js';
6767

6868
// TODO: input validation on manifest JSON
6969
const DEFAULT_SEGMENT_SIZE = 1024 * 1024;
@@ -822,6 +822,10 @@ async function unwrapKey({
822822
}),
823823
}),
824824
],
825+
// include deprecated fields for backward compatibility
826+
algorithm: 'RS256',
827+
keyAccess: keyAccessProto,
828+
policy: manifest.encryptionInformation.policy,
825829
});
826830

827831
const requestBodyStr = toJsonString(UnsignedRewrapRequestSchema, unsignedRequest);
@@ -835,6 +839,7 @@ async function unwrapKey({
835839
authProvider,
836840
fulfillableObligations
837841
);
842+
upgradeRewrapResponseV1(rewrapResp);
838843
const { sessionPublicKey } = rewrapResp;
839844
const requiredObligations = getRequiredObligationFQNs(rewrapResp);
840845
// Assume only one response and one result for now (V1 style)

0 commit comments

Comments
 (0)