VirgilSecurity / virgil-e3kit-js

E3Kit is a security framework that simplifies work with Virgil services and presents the easiest way to add full end-to-end security to your chat application to become HIPAA and GDPR compliant and more.
https://virgilsecurity.com/e3kit/
BSD 3-Clause "New" or "Revised" License
58 stars 19 forks source link

[TypeError: Argument "virgilPublicKey" must be a VirgilPublicKey object] #159

Closed gz1992 closed 1 year ago

gz1992 commented 1 year ago

Describe the bug Hey guys, me again, kkkk. In my program, we send and receive msgs. This msgs are encrypted by virgil. I can easily decrypt those already encrypted msgs (using my publickey), but when I try to send a new one and encrypt i get this error on the title.

To Reproduce Steps to reproduce the behavior, using my encrypt function as example:

encryptText = async (text: string, publicKey: ICard) => {
    const eThree = this.getEThree();
    let encryptedText = null;
    console.log('Encrypt with publicKey: ', publicKey);
    try {
      encryptedText = await eThree.encrypt(text, publicKey);
    } catch (error) {
      console.log('Error on encript: ', error);
      throw error;
    }
    return encryptedText;
  };

1- If I have a text = 'Virgil is awesome' 2- My publickey is: publickey = {"contentSnapshot": "{\"identity\":\"something\",\"created_at\":1635275449,\"version\":\"5.0\",\"public_key\":\"something2\"}", "createdAt": 2021-10-26T19:10:49.000Z, "id": "something3", "identity": "something", "isOutdated": false, "previousCardId": undefined, "publicKey": {"identifier": [Object], "value": "something4"}, "signatures": [{"extraFields": [Object], "signature": "something5", "signer": "self", "snapshot": undefined}, {"extraFields": [Object], "signature": "something6", "signer": "virgil", "snapshot": undefined}], "version": "5.0"} 3- Calling my encryptText(text, publickey) 4- I get the title error [TypeError: Argument "virgilPublicKey" must be a VirgilPublicKey object]

Expected behavior I would get my response from return encryptedText; with my text encrypted in it

Smartphone (please complete the following information):

Additional context My package.json:

"react": "18.1.0",
"react-native": "0.70.6",
"react-native-keychain": "^8.1.1",
"react-native-virgil-crypto": "^0.6.2",
"@virgilsecurity/e3kit-native": "^2.5.3",
"@virgilsecurity/key-storage-rn": "^1.0.0",
"@react-native-async-storage/async-storage": "^1.17.11",
rstp-god commented 1 year ago

@gz1992 Hello again! Will check this!

rstp-god commented 1 year ago

@gz1992 Can you share imports and Ehtree initilize line?

gz1992 commented 1 year ago

Off course @rstp-god ... assuming I have no problem initializing my EThree (as it is in my case), the flow is as follow:

import {EThree} from '@virgilsecurity/e3kit-native';
import AsyncStorage from '@react-native-async-storage/async-storage';

import {ICard} from 'virgil-sdk';

class ClassExample {
  userPublicKey: ICard | null;
  recipientPublicKey: ICard | null;
  eThree: undefined | EThree;
  myuserid: string | undefined;
  receiveruserid: string | undefined;

  constructor() {
    this.userPublicKey = null;
    this.recipientPublicKey = null;
  }

  initialize = async (myuserid: string) => {
    const initializeFunction = () => getJwtVirgilTokenOnMyBackend(myuserid);
    const eThree = await EThree.initialize(initializeFunction, {AsyncStorage});
    this.eThree = eThree;
  };

  getEThree = () => {
    if (!this.eThree) {
      throw new Error('eThree not initialized ');
    }
    return this.eThree;
  };
}

const instanced = new ClassExample();

await instanced.initialize();
const eThree = await instanced.getEthree();
const hasLocalPrivateKey = await eThree.hasLocalPrivateKey();
rstp-god commented 1 year ago

Hello @gz1992 ! Can not reproduce this, can you send more info about public key initialization and some test case ?

gz1992 commented 1 year ago

Hey @rstp-god , thanks for your time. While I look into, can you show me how should I be doing?

rstp-god commented 1 year ago

@gz1992
Hello again, some solutions here:

  1. call encrypt not with ICard, try to use VirgilPublicKey
  2. Here we have example that works https://github.com/VirgilSecurity/virgil-e3kit-js/blob/master/examples/node/index.js#L58
  3. Try to generateKeys, then encrypt, mayber you have troubles with ICard creating
gz1992 commented 1 year ago

Thanks, @rstp-god , i was using the url above as example, in my code i tryed 2 things:

1- On registering, like alice and bob example, i got this error:

import {EThree} from '@virgilsecurity/e3kit-native';
import AsyncStorage from '@react-native-async-storage/async-storage';

const initializeFunction = () => getJwtVirgilTokenOnMyBackend(myuserid);
const alice = await EThree.initialize(initializeFunction, {AsyncStorage});
console.log('Alice registering');
await alice.register();
LOG  Alice registering
LOG  ERROR ENCRYPT [IdentityAlreadyExistsError: This identity is already registered on Virgil Cloud. To load private key use EThree.restorePrivateKey or EThree.rotatePrivateKey]

2- So I tryed removing and rotating my privateKey, as it is as test, i can remove...

import {EThree} from '@virgilsecurity/e3kit-native';
import AsyncStorage from '@react-native-async-storage/async-storage';

const initializeFunction = () => getJwtVirgilTokenOnMyBackend(myuserid);
const alice = await EThree.initialize(initializeFunction, {AsyncStorage});
console.log('Alice cleaningup');
await alice.cleanup();
console.log('Alice registers...');
await alice.rotatePrivateKey();

the result

 LOG  Alice cleaningup
 LOG  Alice registers...
 LOG  Alice creates private key backup...
 LOG  ERROR ENCRYPT [Error: Exception in HostFunction: com.virgilsecurity.crypto.pythia.PythiaException: Underlying pythia library returns -1.]

If I ignore this error, i am going to the same of the issue title [TypeError: Argument "virgilPublicKey" must be a VirgilPublicKey object], the backup also keeps breaking...

Does it helps?

rstp-god commented 1 year ago

@gz1992
Hello, does you try to push publicKey.publicKey inside encrypt function?

gz1992 commented 1 year ago

Hey, @rstp-god , as you can see In my response above, I get problems registering my Alice key. But if I ignore the registers error above and go through the process using the findUser that returns my necessary publicKey, I get:

import {EThree} from '@virgilsecurity/e3kit-native';
import AsyncStorage from '@react-native-async-storage/async-storage';

const initializeFunction = () => getJwtVirgilTokenOnMyBackend(myuserid);
const alice = await EThree.initialize(initializeFunction, {AsyncStorage});
await alice.register();
const publicKey = await alice.findUsers(bob.identity);
alice.encrypt(text, publicKey);

On encrypt I get this log:

LOG [TypeError: Argument "virgilPublicKey" must be a VirgilPublicKey object]
gz1992 commented 1 year ago

If it show better:

import {EThree} from '@virgilsecurity/e3kit-native';
import AsyncStorage from '@react-native-async-storage/async-storage';

const initializeFunction = () => getMyJWTTOken(aliceUserId);
const alice = await EThree.initialize(initializeFunction, {
  AsyncStorage,
});
console.log('Alice registering');
// await alice.register(); // AS MY REGISTER WAS GIVING AN ERROR, I CLEANUP AND ROTATEPRIVATEKEY
console.log('Alice cleaningup');
await alice.cleanup();
console.log('Alice registers...');
await alice.rotatePrivateKey();

console.log("Alice searches for Bob's card...");
const bobCard = await alice.findUsers(bobUserId);

const encryptedForBob = await alice.encrypt(
  text,
  bobCard,
);

My log is here:

 LOG  Alice registering
 LOG  Alice cleaningup
 LOG  Alice registers...
 LOG  Alice searches for Bob's card...
 LOG  ERROR ENCRYPT [TypeError: Argument "virgilPublicKey" must be a VirgilPublicKey object]

If it helps, my decrypt function is working fine

alice.decrypt(text, senderPublicKey);
gz1992 commented 1 year ago

@rstp-god , hey man.. i've been testing, and i've brought some things that might help you reproduce in your enviroment my problem..

1- we developed this project, in 2019, or 2020, and we used as package.json:

"react": "16.11.0",
"react-native": "0.62.3",
"@react-native-community/async-storage": "^1.12.1",
"@virgilsecurity/e3kit": "^0.7.0-beta.2",
"@virgilsecurity/key-storage-rn": "^0.2.1",
"react-native-virgil-crypto": "^0.6.1",

In this 2023 version, i get this packages

"react": "18.1.0",
"react-native": "0.70.6",
"@react-native-async-storage/async-storage": "^1.17.11",
"@virgilsecurity/e3kit-native": "^2.5.3",
"@virgilsecurity/key-storage-rn": "^1.0.0",
"react-native-virgil-crypto": "^0.6.2",

async-storage has changed to a different package, so i am not sure if it is a problem... image

2- As you can see, I updated all packages from virgil, maybe you could create this interaction with the old one and trying to reuse it on the new and updated virgil

3- Our (bob and alice, as the example shows) identity is a "fixed" valued, but if we restore all privatekeys this will be a big problem, eheheh.

4- What is this VirgilPublicKey object? because in the bobCard example, i get this card:

 {"contentSnapshot": "{\"identity\":\"something\",\"created_at\":1580172985,\"version\":\"5.0\",\"public_key\":\"my_public_key\"}", "createdAt": 2020-01-28T00:56:25.000Z, "id": "my_id", "identity": "my_identity", "isOutdated": false, "previousCardId": undefined, "publicKey": {"identifier": [Object], "value": "my_public_key"}, "signatures": [{"extraFields": [Object], "signature": "a_signature", "signer": "self", "snapshot": undefined}, {"extraFields": [Object], "signature": "another_signature", "signer": "virgil", "snapshot": undefined}], "version": "5.0"}

is it a wrong type?

Thanks again for the support!

rstp-god commented 1 year ago

@gz1992 Hello! Did your prev version working fine? As i see bob card is fine in your comment, like in example i think. I will try to reproduce with this information, and send you some feedback with result!

gz1992 commented 1 year ago

@rstp-god , hey!!! Yep, in previous version i have no problema encrypting and decrypting... But the new one i cant encrypt msgs

rstp-god commented 1 year ago

@gz1992 Hello! I think i have some answer, for you! I'am again check your bob card, and bob card from node example and my bobcard do not have value inside, may be some update issues, i will check it, and relese patch if it needed! image

gz1992 commented 1 year ago

NICE!!! Very good news @rstp-god

gz1992 commented 1 year ago

hey @rstp-god , is everything all right? Do you need anything?

rstp-god commented 1 year ago

@gz1992 Hello! relese 2.5.3 on npm just today. try this for errors if still having this issue, will check again )

gz1992 commented 1 year ago

more good news!!!

rstp-god commented 1 year ago

@gz1992 How this goes? Working fine ?

gz1992 commented 1 year ago

@rstp-god , finishing all tests, for now seems awesome

gz1992 commented 1 year ago

@rstp-god , all good!!!!! If I have anything, I will create another issue!