Skip to content

Commit

Permalink
Update tdf.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
dmihalcik-virtru committed May 1, 2024
1 parent 5f67033 commit 383359d
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/tdf3/src/tdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import PolicyObject from '../../src/tdf/PolicyObject.js';
import { type CryptoService, type DecryptResult } from './crypto/declarations.js';
import { CentralDirectory } from './utils/zip-reader.js';
import { SymmetricCipher } from './ciphers/symmetric-cipher-base.js';
import { cryptoToPem } from './crypto/crypto-utils.js';

// TODO: input validation on manifest JSON
const DEFAULT_SEGMENT_SIZE = 1024 * 1024;
Expand Down Expand Up @@ -824,6 +823,10 @@ async function unwrapKey({
// const pkKeyLike = await importPKCS8(privateKey, 'RS256');
// Get key access information to know the KAS URLS
// TODO: logic that runs on multiple KAS's
const ephemeralEncryptionKeys = await cryptoService.cryptoToPemPair(
await cryptoService.generateKeyPair()
);
const clientPublicKey = ephemeralEncryptionKeys.publicKey;

const rewrappedKeys = await Promise.all(
keyAccess.map(async (keySplitInfo) => {
Expand All @@ -832,8 +835,6 @@ async function unwrapKey({
}
const url = `${keySplitInfo.url}/${isAppIdProvider ? '' : 'v2/'}rewrap`;

const clientPublicKey = await cryptoToPem(dpopKeys.publicKey);

const requestBodyStr = JSON.stringify({
algorithm: 'RS256',
keyAccess: keySplitInfo,
Expand Down Expand Up @@ -875,8 +876,10 @@ async function unwrapKey({
} = await axios.post(httpReq.url, httpReq.body, { headers: httpReq.headers });
responseMetadata = metadata;
const key = Binary.fromString(base64.decode(entityWrappedKey));
const { privateKey } = await cryptoService.cryptoToPemPair(dpopKeys);
const decryptedKeyBinary = await cryptoService.decryptWithPrivateKey(key, privateKey);
const decryptedKeyBinary = await cryptoService.decryptWithPrivateKey(
key,
ephemeralEncryptionKeys.privateKey
);
return new Uint8Array(decryptedKeyBinary.asByteArray());
} catch (e) {
console.error(e);
Expand Down

0 comments on commit 383359d

Please sign in to comment.