Skip to content

private key saving, structure and security #17

Closed

Description

@tomholub

Here's an outline of what we should do:

  1. Some information about private keys will have to go into a database
  2. Create a class to encrypt secrets using the KeyStore, call it something like KeyStoreEncryption
  3. look up private keys

1: Database

You should have a table called something like key. The table should have roughly the following structure:

CREATE TABLE IF NOT EXISTS `key` (
    `longid` VARCHAR(16) NOT NULL,
    `source` VARCHAR(20) NOT NULL,
    `public` BLOB NOT NULL,
    `private` BLOB NOT NULL,
    `passphrase` varchar(100) DEFAULT NULL,
    PRIMARY KEY(longid)
);

Explaining each column:

  • longid: get this when recovering the key with js.crypto_key_longid()
  • source: any of following strings: "backup", "new", "import". For now, will always be backup
  • public: armored public key. get with key.toPublic().armor()
  • private: armored private key, that was first encrypted using KeyStoreEncryption. Something like: String KeyStoreEncryption.encrypt(String normalized_armored_private_key);
  • passphrase: also encrypted eg String KeyStoreEncryption.encrypt(String passphrase);. This column may stay empty (a null). For now though, you will always fill it with the encrypted pass phrase.

2: KeyStoreEncryption

Unfortunately, KeyStore cannot just store any string and encrypt it - we have to do it ourselves and use KeyStore to do it.
Basically, we will create a new keypair in KeyStore and then use that keypair to encrypt and decrypt sensitive information. When creating the keypair, you will have to assign a an alias to it. You can probably pick something static like "flowcrypt_main".
The details are outlined here: http://www.androidauthority.com/use-android-keystore-store-passwords-sensitive-information-623779/
KeyStore specs if you need them: https://developer.android.com/training/articles/keystore.html
This functionality should be put as methods into a class, something like:

class KeyStoreEncryption {
  private String alias = "flowcrypt_main";

  public String encrypt(String plain_data);
  public String decrypt(String encrypted_data);
}

The details of how you structure this is of course up to you. The result will be that private and passphrase fields in the database will be encrypted using the KeyStore keypair, and therefore protected by user's Android passcode.

Anytime you save or load the private key from database, you'll need to use that class.

3: Look up private keys

This will make it possible to connect this to SecurityStorageConnector and look up private keys and pass phrases based on longid as originally intended

Activity

  1. added this to the milestone on May 10, 2017
  2. tomholub commented on May 10, 2017

    @tomholub
    CollaboratorAuthor

    This is what I believe we should do based on my understanding of Android so far, but I may have missed something. Let me know if you have any feedback.

  3. DenBond7 commented on May 11, 2017

    @DenBond7
    Collaborator

    For reference: The Keystore itself is encrypted using the user’s own lockscreen pin/password, hence, when the device screen is locked the Keystore is unavailable. Keep this in mind if you have a background service that could need to access your application secrets.

  4. tomholub commented on May 11, 2017

    @tomholub
    CollaboratorAuthor
  5. DenBond7 commented on May 11, 2017

    @DenBond7
    Collaborator

    Yes, because we will use the decrypt function only when showing message when the device is active.

  6. tomholub commented on May 11, 2017

    @tomholub
    CollaboratorAuthor
  7. DenBond7 commented on May 12, 2017

    @DenBond7
    Collaborator
  8. DenBond7 commented on May 15, 2017

    @DenBond7
    Collaborator
  9. added a commit that references this issue on May 16, 2017
  10. DenBond7 commented on May 16, 2017

    @DenBond7
    Collaborator

    I completed this task. Now the keys are stored in the database in encrypted form. The current build of apk file will work with the new algorithm.

  11. tomholub commented on May 20, 2017

    @tomholub
    CollaboratorAuthor

    Just tested it, working well on my end. Closing this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions