user1118764
user1118764

Reputation: 9835

KeyStore operations

I'm new to Java/Android KeyStores, and after reading the documents and some tutorials, I'm still a bit confused as to what the operations do exactly, in particular load and store calls. I'm looking at the following piece of code in a Java method, and have some trouble understanding it. inKeyStore is an input parameter of type KeyStore.

String password = "password";
KeyStore newKeyStore;
FileOutputStream out = mContext.openFileOutput("my.keystore", 0);
FileInputStream in = null;
try {
  inKeyStore.store(out, password.toCharArray());
} catch (KeyStoreException e) {
  if (out) out.close();
  in = mContext.openFileInput("my.keystore");
  newKeyStore = KeyStore.getInstance("BKS");
  newKeyStore.load(in, password.toCharArray());
  if (in) in.close();
}

I know that inKeyStore.store() throws a KeyStoreException if it's uninitialized. However, I'm confused about a few things

  1. What is "my.keystore" file? Is it an actual file in the file system, or is it the name of the keystore?
  2. When inKeyStore.store() is called, what data is written to where? Is it the data from the "my.keystore" file written to the inKeyStore?
  3. What is the significance of the password?
  4. Similar to Q2, when newKeyStore.load() is called, what data is written to where?

Thanks!

Upvotes: 0

Views: 722

Answers (1)

Seb B.
Seb B.

Reputation: 697

In your case, you have:
-The KeyStore instance in memory: KeyStore.getInstance(String type, String provider)
-The keystore file on the file system: {app_priv_folder}\my.keystore

To read (file ==> memory): KeyStore:load(InputStream stream, char[] password)

Initializes this KeyStore from the provided InputStream

To Write (memory ==> file): KeyStore:store (OutputStream stream, char[] password)

Writes this KeyStore to the specified OutputStream

Answers:

  1. As you guessed, "my.keystore" is the name of the keystore file on the file system (in the application private folder)
  2. No, it's the opposite.
    inKeyStore.store(out, password.toCharArray()) stores the inKeyStore data in the file "my.keystore": FileOutputStream out = mContext.openFileOutput("my.keystore", 0);
  3. The keystore file is protected by that password
  4. newKeyStore.load(): Reading data from "my.keystore" to load it in the KeyStore instance in memory.

Upvotes: 1

Related Questions