0

I have little experience with Android app development, and close to none with iOS. I'm writing an app that has to store a certain "key", and I would like this key to be stored in a secure way. As secure as possible, with reasonable development effort. The key has to be stored: the whole point is that the user should not need to be online to fetch it from a server.

I'm not sure which is the best solution for each of the two platforms. I've read a bit about Keystore in Android and Keychain in iOS, but I can't quite get how secure they are. Many say none of them is really secure, as an attacker could reverse engineer the app, find a file, get access to... but in the end people have been storing passwords and keys somewhere on PCs for decades, no method is 100% safe, so I fail to understand whether it's just paranoia* when it comes to mobile apps (well, more paranoia than usual) or if mobile platforms are really that much insecure.

Is it reasonably safe to assume that Keystore and Keychain would give me as much security as I would need to protect the key from a not-too-advanced (or expensive) attack?

PS Don't get me wrong, I appreciate security as much as anyone, and I always strive for it. But reading that an app would be "not secure" "because the phone might get stolen" prompts me to question whether we should write software at all :)

Simone
  • 1,260
  • 1
  • 16
  • 27
  • What is the key going to be used for? – Michael Mar 21 '17 at 12:25
  • Also, what are your threat vectors? Who are you trying to defend against getting this key? – CommonsWare Mar 21 '17 at 12:37
  • Where is the key generates? Is it generated by the app or is there a single key to be distributed with the app? – Paulw11 Mar 21 '17 at 12:48
  • The key is used to generate OTPs. It is an authorization token, so it should be protected against the attacks of identity thiefs. It is generated by an external server and sent (ideally once) over the network. – Simone Mar 21 '17 at 14:13
  • Is the "key" an actual `Key` (if so, what type of key?), or just some sensitive data? Will your app require the user to authenticate using a PIN/password/fingerprint before this key is used? – Michael Mar 21 '17 at 14:35
  • I'm not sure if it is a `Key`. It is a base32 encoded string; is that one of the allowed algorithms for `Key` type? (I think not). Also, the user won't be required any input before the app can use the key. – Simone Mar 21 '17 at 14:44
  • One more detail: the key is user-specific. It identifies the user, not the application, so it's not a problem if a user is able to learn his own secret key. – Simone Mar 21 '17 at 14:46
  • 1
    On iOS the Keychain is the best possible security. It will not however protect from the device owner, for that DRM is needed. – zaph Mar 21 '17 at 14:46
  • Thanks @zaph. The owner is not an attacker in my scenario: he won't usually know the secret (he doesn't need to), but it's not a problem if he does. – Simone Mar 21 '17 at 14:48
  • 1
    @Simone: On Android you could generate a `SecretKey` in the `AndroidKeyStore` and use it to encrypt/decrypt your secret string. Ordinarily only your app would be able to use this key - however I'm not entirely sure if that's the case if some app manages to gain root privilege, so you may want to investigate that. – Michael Mar 21 '17 at 15:07
  • Thanks @Micheal. I'll look into root vulnerabilities. – Simone Mar 21 '17 at 15:15
  • 1
    My concern was that some app that has root privileges manages obtain your app's KeyStore, by running with your app's uid (or whatever Android uses to separate KeyStores). I'm not so sure you can do anything about that though, as long you don't switch to some sort of password-derived key. – Michael Mar 21 '17 at 15:25
  • That's exactly what I was talking about in the question. Short of writing my own encrypted data safe (which I shouldn't really do without massive cryptographic knowledge and experience), is there really _any_ way to store the data securely? – Simone Mar 21 '17 at 15:34
  • Unless use of the key is going to require some information that the app itself doesn't have (e.g. a password provided by the user), I don't see how you're going to be secure from the kind of attack I described. But let's take a step back.. You say that the app shouldn't have to be online for the OTP functionality to work(?). What's the purpose of the OTP then? – Michael Mar 21 '17 at 16:49
  • You might benefit by reading related threads on stack overflow: http://stackoverflow.com/questions/42752247/how-to-hide-api-keys-in-android/42753761#42753761 . I.e. It is easy to sniff the key off the network, and that's the first thing an attacker is going to do unless you protect against it. – TheGreatContini Mar 21 '17 at 18:21
  • @Michael The user doesn't need to be online *on the phone*. He will get the OTP (time-based OTP) from the phone, then he will submit it to an online form, for example on his computer. – Simone Mar 22 '17 at 00:03
  • @TheGreatContini Thanks for the link. The protection from password sniffing is granted by the fact that the password is a time-based OTP. It will become useless in a few seconds. – Simone Mar 22 '17 at 00:06
  • @Simone what API level are you targeting. Android KeyStore provides a very nice solution if you're targeting 23 or above. It's tougher if you need to target lower API levels. – divegeek Apr 15 '17 at 06:06
  • @divegeek I'm targeting 16 and above – Simone Apr 18 '17 at 07:08
  • @Simone unfortunately Android doesn't have a really good answer for this until 23. Previous versions of keystore did a fairly good job of keeping your keys secure, but had various cases in which they would lose the keys, partially due to a very conservative security design (if in doubt, erase the keys) and partially due to bugs in some of the releases. – divegeek Apr 23 '17 at 01:51

1 Answers1

-2

First of all, Keystore is used to sign application before send it to the store, for example if you send your application to a paly/apple store, using keystore/keychain protect your application to stay safe from malware, because some one can download it, put a malware and send it again to store ! Also, if you update the version of your old application, your clients can't update the old app, they will install the 2 versions in the same time, because your app is not linked using the keyStore/Keychain, hope I'm clear.

What are you looking at : is a storage with a encrypted information, in this case I use Realm because The Realm file can be stored encrypted on disk by passing a 512-bit encryption key (64 bytes).

NizarETH
  • 969
  • 3
  • 15
  • 38
  • 2
    I think the OP is referring to [this Keystore](https://developer.android.com/reference/java/security/KeyStore.html) (in particular, `AndroidKeyStore`). – Michael Mar 21 '17 at 12:41
  • he said : **The key has to be stored: the whole point is that the user should not need to be online to fetch it from a server.** , so I think he talk about encryption data – NizarETH Mar 21 '17 at 12:45
  • 1
    Well, like I said, I believe the "Keystore" in the question refers to the place where apps can store cryptographic objects on the device, not the file that you use to sign your APK. – Michael Mar 21 '17 at 12:51
  • 1
    @Michael is correct. The key should be retrieved from the network the first time around, and then stored on the device. There must not be the need to contact the server every time the user wants to take actions that involve the secret key. Sorry if I was not clear enough. – Simone Mar 21 '17 at 14:15