jerson / react-native-fast-openpgp

OpenPGP for react native made with golang for fast performance
https://www.npmjs.com/package/react-native-fast-openpgp
MIT License
45 stars 18 forks source link

Question: Is there a way to sign and encrypt a string? #13

Closed commi closed 4 years ago

commi commented 4 years ago

as the subject says.

I need to sign and encrypt a document. While i can pass public key's to the sign() method, i just get a signature back. the public keys do not seem to be used.

Cheers

jerson commented 4 years ago

hi @commi, yeah by now we are using a single method to generate entityList and require public and private

https://github.com/jerson/openpgp-mobile/blob/f50ff7a6089f0748b7b16583dbcccad87a0c2b41/openpgp/common.go#L95-L112

maybe we can reduce this code to only use the privateKey for sign

jerson commented 4 years ago

and if you want to encrypt, first you can encrypt with

const encrypted = await OpenPGP.encrypt(message: string, publicKey: string): Promise<string>;
const outputFile = await OpenPGP.encryptFile(inputFile: string, outputFile: string, publicKey: string): Promise<string>;
commi commented 4 years ago

Ok i get why the param is there. So right now i can either encrypt or sign stuff, but not both (like with gnupg or openpgp.js)?

jerson commented 4 years ago

mmm yeah i think its something that we can add to the library, but at this moment its not supported

mohammadrafigh commented 4 years ago

I don't know go well to send a PR, so all I can do right now is to wait for this API or do some investigating on how I can help with that. also it's really good to see armored APIs as well.

jerson commented 4 years ago

armored API for armor and dearmor i think is something that i can help with, but for sign and encrypt i would like to see some examples about libraries that do that, so if you have and example for that would be great

mohammadrafigh commented 4 years ago

Here is an example from Openpgpjs the encryption method first signs and then encrypts the message armored, the same applies for decrypt, the library decrypts and verifies signature. Also you can take a look at Bouncycastle in Java for more low level examples, I have some Kotlin code implemented PGP using Bouncycastle which I'm already using on Android and server. Here is another example in Java.

If it helps, to generate a single encrypted/signed string you may consider inline signature instead of detached signature.

jerson commented 4 years ago

Hi @mohammadrafigh, thanks for sharing those examples, I think that if it is possible to implement that according to what I see, I will be reviewing these days and maybe publishing some new version

mohammadrafigh commented 4 years ago

Cool! I'm trying to use FastOpenPGP in Nativescript, so I may create a plugin based on your implementation and add a reference.

On Mon, 24 Aug 2020, 19:54 Gerson Alexander Pardo Gamez, < notifications@github.com> wrote:

Hi @mohammadrafigh https://github.com/mohammadrafigh, thanks for sharing those examples, I think that if it is possible to implement that according to what I see, I will be reviewing these days and maybe publishing some new version

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jerson/react-native-fast-openpgp/issues/13#issuecomment-679194803, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTM4ZFVEUPVFRWZ27YGPQTSCKA4NANCNFSM4PVZUZIA .

jerson commented 4 years ago

hi @mohammadrafigh , i was looking example code, but i have a question please,

are you tried to use:

sign -> encrypt for you custom encode case decrypt -> verify for you custom decode case

or i mean why you cant use in that way?

mohammadrafigh commented 4 years ago

With current implementation sign function generates a detached signature so when you sign -> encrypt string like "hello" you will have a detached signature and encrypted "hello". So if you want to encrypt signature with message (like what happens in clearsigned messages you should concat the signature into message and then encrypt which won't be a valid clearsign signature due to changing message digest. I see the main crypto library in go provides both detached and non-detached signatures maybe the non-detached one is the clearsign that we are talking about, you can try it. Also many implementations like Openpgpjs rely on One-pass signatures. So when you decrypt the message the signature would be verified as well. So for this kind of signatures your implementation of decrypt -> verify won't work since your decrypt method doesn't return the decrypted signature included in message to be verified. So verification is not applicable at all. as a reference please take a look at this rfc. I suggest to implement one-pass signatures.

jerson commented 4 years ago

hi @mohammadrafigh maybe is late for this, but latest version 1.6.0 contains new parameter for encrypt signedEntity is probably what you need for sign and encrypt im not too sure but maybe could work

encrypt(
    message: string,
    publicKey: string,
    signedEntity?: Entity,
    fileHints?: FileHints,
    options?: KeyOptions,
  )

/**
 * An Entity represents the components of an OpenPGP key: a primary public key
 * (which must be a signing key), one or more identities claimed by that key,
 * and zero or more subkeys, which may be encryption keys.
 */
export interface Entity {
  publicKey: string;
  privateKey: string;
  passphrase: string;
}
export interface FileHints {
  /**
   * IsBinary can be set to hint that the contents are binary data.
   */
  isBinary: boolean;
  /**
   * FileName hints at the name of the file that should be written. It's
   * truncated to 255 bytes if longer. It may be empty to suggest that the
   * file should not be written to disk. It may be equal to "_CONSOLE" to
   * suggest the data should not be written to disk.
   */
  fileName: string;
  /**
   * ModTime format allowed: RFC3339, contains the modification time of the file, or the zero time if not applicable.
   */
  modTime: string;
}
mohammadrafigh commented 4 years ago

Thank you, I will try it and update you