Browse Source

Merge pull request #3 from ente-io/shared_collections

Decrypt shared collection files
Pushkar Anand 4 years ago
parent
commit
274b45fa0f

+ 1 - 1
src/pages/generate/index.tsx

@@ -58,7 +58,7 @@ export default function Generate() {
                 const kekHash = await cryptoWorker.hash(kek);
                 const kekHash = await cryptoWorker.hash(kek);
                 const encryptedKeyAttributes = await cryptoWorker.encrypt(key, kek);
                 const encryptedKeyAttributes = await cryptoWorker.encrypt(key, kek);
                 const keyPair = await cryptoWorker.generateKeyPair();
                 const keyPair = await cryptoWorker.generateKeyPair();
-                const encryptedKeyPairAttributes = await cryptoWorker.encrypt(keyPair.privateKey, kek);
+                const encryptedKeyPairAttributes = await cryptoWorker.encrypt(keyPair.privateKey, key);
                 const keyAttributes = {
                 const keyAttributes = {
                     kekSalt: await cryptoWorker.toB64(kekSalt),
                     kekSalt: await cryptoWorker.toB64(kekSalt),
                     kekHash: kekHash,
                     kekHash: kekHash,

+ 9 - 5
src/services/fileService.ts

@@ -54,8 +54,15 @@ const getCollectionKey = async (collection: collection, key: Uint8Array) => {
             await worker.fromB64(collection.keyDecryptionNonce),
             await worker.fromB64(collection.keyDecryptionNonce),
             key);
             key);
     } else {
     } else {
-        // TODO
-        decryptedKey = null;
+        const keyAttributes = getData(LS_KEYS.KEY_ATTRIBUTES);
+        const secretKey = await worker.decrypt(
+            await worker.fromB64(keyAttributes.encryptedSecretKey),
+            await worker.fromB64(keyAttributes.secretKeyDecryptionNonce),
+            key);
+        decryptedKey = await worker.boxSealOpen(
+            await worker.fromB64(collection.encryptedKey),
+            await worker.fromB64(keyAttributes.publicKey),
+            secretKey);
     }
     }
     return {
     return {
         ...collection,
         ...collection,
@@ -81,9 +88,6 @@ export const getFiles = async (sinceTime: string, token: string, limit: string,
     var files: Array<file> = [];
     var files: Array<file> = [];
     for (const index in collections) {
     for (const index in collections) {
         const collection = collections[index];
         const collection = collections[index];
-        if (collection.key == null) {
-            continue;
-        }
         const resp = await HTTPService.get(`${ENDPOINT}/collections/diff`, {
         const resp = await HTTPService.get(`${ENDPOINT}/collections/diff`, {
             'collectionID': collection.id.toString(), sinceTime, token, limit,
             'collectionID': collection.id.toString(), sinceTime, token, limit,
         });
         });

+ 5 - 0
src/utils/crypto/libsodium.ts

@@ -92,6 +92,11 @@ export async function generateKeyPair() {
     return sodium.crypto_box_keypair();
     return sodium.crypto_box_keypair();
 }
 }
 
 
+export async function boxSealOpen(input: Uint8Array, publicKey: Uint8Array, secretKey: Uint8Array) {
+    await sodium.ready;
+    return sodium.crypto_box_seal_open(input, publicKey, secretKey);
+}
+
 export async function fromB64(input: string) {
 export async function fromB64(input: string) {
     await sodium.ready;
     await sodium.ready;
     return sodium.from_base64(input, sodium.base64_variants.ORIGINAL);
     return sodium.from_base64(input, sodium.base64_variants.ORIGINAL);

+ 4 - 0
src/worker/crypto.worker.js

@@ -57,6 +57,10 @@ export class Crypto {
         return libsodium.generateKeyPair();
         return libsodium.generateKeyPair();
     }
     }
 
 
+    async boxSealOpen(input, publicKey, secretKey) {
+        return libsodium.boxSealOpen(input, publicKey, secretKey)
+    }
+
     async fromString(string) {
     async fromString(string) {
         return libsodium.fromString(string);
         return libsodium.fromString(string);
     }
     }