swift3 - How to protect secret key - ios

I am new of iOS swift 3 development. Now, I am working on a project which needs encryption, message authentication code(MAC) and Hashed-base-MAC. These algorithms require secret keys. I know that it is a bad practice to hard-code the keys inside the code, like:
let key = "secretkeyabc123"
Searched and read some articles like:
In iOS, how can I store a secret "key" that will allow me to communicate with my server?
Since other people may perform reverse engineering, I am finding a way to protect my keys. Requirements:
No hash the key. Time allows to crack it, or with hashed table and dictionary
May not connect to Internet (My app. is an offline app. If your answer needs to connect to Internet. Yes, please point it out. I will think about it)
It is a static key. It may change monthly
It is a symmetry key.
Codes, concept or other things are welcome. Thanks!

Don't store the key at all. Perform a Diffie-Hellman key exchange to start an asymmetrically encrypted channel, and use this channel to send across a symmetric key to the client, which can be used for subsequent client use.

Check iCloud Keychain (based on your tags [ios], [swift], [key]).
It functions as a secure database that allows information including a user's website login passwords, Wi-Fi network passwords, credit/debit card management (though without CVV), and other account data, to be securely stored for quick access and auto-fill on webpages and elsewhere when the user needs instant access to them. They are always stored encrypted using 256-bit AES encryption, are stored on device and pushed from iCloud between devices, and only available on a user's trusted devices.

Related

iOS - Store credit/debit card in encrypted form on server from ios app and decrypt it from the app again. How can I manage it most securely?

I have static key in my iOS application. By using that key I am encrypting credit/debit cards and sends it to server for future use. And I am using same key for decryption of card.
Now I can't use dynamic key by any key generation algorithm as I want to decrypt my card later on. So, Every time I require same key.
So, My question is how should I store my key or where I should store my key as it'll be most secure? or Can I manage this stuff by generating dynamic key every time? If yes then how?
Many iOS applications like Amazon, Uber etc are storing the card information in their server! How they people are managing this stuff?
Any help will be appreciated! Thanks!
If you need to ask this question then you are already breaking PCI compliance. Your customers credit card information should never touch your server, encrypted or otherwise. Most payment gateways handle this for you. There is very little reason why you should be doing this yourself.
If you do need to handle this yourself, and there are very few cases where you do, simply symmetrically encrypting the card information in your app is not the way to do it. Literally anyone who can download your app can decrypt other customers credit card information.
The correct approach would be to transport card information to your server secured with TLS, then encrypting and storing the key information in a HSM server side. If you can't manage this, then you are not PCI compliant and will be legally responsible.
If your business operates, or has service available to customers in the EU, then you could suffer very heavy fines for poor handling of consumer credit card information.

How to securely store AWS service ids for iOS?

I'm developing an iOS app that uses Cognito User Pools / Federated Identities, Mobile Analytics, and S3 to manage various features of the app, and recently I have become concerned for the security of these features. I already use IAM roles to control the services unauthenticated vs authenticated users have access to, but most of these services use strings (e.g. user pool app client id or user pool app client secret for User Pools, or app id for Mobile Analytics) to give the app access to that service.
What are the best practices to securely store these strings on the device to be used when necessary? Is it even necessary to secure these strings since the app is using IAM roles?
If it is necessary to securely store the strings, I have read that using the CommonCrypto library to encrypt strings before putting them in the keychain is best, but I'm not sure what key to use for encryption since my user needs unauthenticated access to those services. Any advice would be tremendously helpful.
This is a common problem to any mobile app. If someone really wants to, it's not difficult to decompile the app and scrape the keys from it. It's great that you are using IAM roles to restrict feature usage. This will limit the blast radius of attackers, but not necessarily prevent them.
Wth user pools you also get a globally unique identifier which can be used with IAM to restrict what S3 you can use key pre-fixes (which act similar to folders) to limit the objects that users can access to pre-fixes with their unique identifier. You can refer to https://mobile.awsblog.com/post/Tx1OSMBRHZVM9V0/Understanding-Amazon-Cognito-Authentication-Part-3-Roles-and-Policies (Using user pools as the provider, which will use the identity id as the prefix). Depending on how you structure your app you could use this so each user can only modify their own objects. I don't think Analytics has any way of restricting like this... because it wouldn't really make sense for it.
As far as securing your ID's there are things you can do to help mitigate, but there is no fool proof way to prevent someone taking it. You could for instance have the app make a call to your server for the ID... but then an attacker could just call the server. You could encrypt it, which might make it more difficult for an attacker to get, but you have to keep the key somewhere and if the app could get it so could someone who decompiles the app. Unless your app users get some sort of password from outside the app and put it in there isn't a complete way to lock it against attackers.
Hope this helps.

Security surrounding the iOS shared NSHTTPCookieStorage

I'm struggling to find the exact details on this, so I'm hoping to get some help here. I'm looking for some information regarding the underlying storage mechanism of the iOS shared NSHTTPCookieStorage:
When cookies are stored using the shared NSHTTPCookieStorage, is there any encryption provided by default, such as the use of the keychain services? Or are the cookies simply stored in plaintext using NSUserDefaults?
I understand that the cookies are stored within an app's sandbox, so that other apps will not have access, but is my understanding correct that someone with physical access to the device can easily access an app's cookies and their values (especially if they are stored unencrypted)?
If one had to handle some sensitive data in cookies but wanted to utilise the default cookie handling/storage (shared NSHTTPCookieStorage) of the NSURLSession APIs for instance, what would then be the best option?
Sorry, I know I've asked 3 separate questions but any help on this would be greatly appreciated.
Thanks.
I don't know how cookies specifically are stored, but all application files are encrypted on iOS, with an AES256 key specific to the app. Those keys are derived from other keys that ultimately come from the "secure enclave" within the CPU chip, which will only release the keys when the user unlocks the device. (Apple has a pretty comprehensive security white-paper for iOS that describes this in detail.)
For this reason it's quite difficult to access files on iOS, even with physical access, unless you can get the device's passcode and unlock it. The regular cookie storage is probably secure enough for your purposes.

In iOS application for safe login and secure password protection which mechanisms or methods are good for security purpose.? RSA encryption?

In iOS application i have to make user's login safe and secure and user can login from different iPhones having same application downloaded. Application should work in offline too. So which method should i implement?
I searched and got RSA encryption in which there is private and public key method but i can't figure out how i implement it in application?whether i hold both keys in server and client side or who get which key? And how can i make only one key generation for every application downloaded? As data should roam securely between client server system.
Is there any other mechanism which i should consider implementing in my problem?
You don't need to perform any encryption. Just store the passwords in the keychain, which has stronger encryption than anything you could possibly implement — as it's integrated into the Secure Enclave which has no public API other than keychain and already uses RSA encryption, among other things to make it even stronger than just that alone.
If the user enables iCloud KeyChain syncing, then the password you store there will be synced across all their devices.
Your application will work offline.
If the user has a weak passcode to unlock their device, then the encryption will not be very good - it can probably be cracked in under an hour (although this information is old, and may not be valid for modern hardware). The best security practice is to disable "Simple Passcode" and use alphabetic characters + digits. It doesn't need to be as strong password, just something short and alphanumeric is fine (the iPhone is a special case, and handles weak passwords better than most security systems). If the user has a finger print scanner available, that can be used to avoid having to type their password in regularly. This is quite secure, contrary to early reports otherwise.
If the devise has no passcode at all, then there is no encryption and it is impossible to implement protection. In that case your only option is to store your secure data on a remote server, and never ever allow the device to access it. Or else just hope nobody steals the phone. Without a passcode/password, it is impossible to store anything securely on an iPhone.
It sounds like you want a single device that will be shared by many users? If that's the case you cannot store the password on the device. It is impossible to achieve security unless there is a passcode and the passcode must be secret, anybody who knows it will be able to bypass any encryption you implement.

Validate if the encrypted data is not tampered

I have a string encrypted using RNCryptor v2.2 ObjectiveC library.
After encryption it is stored locally on iPhone.
When want to get the string back, before decryption, I want to make sure that the encrypted is not tampered (like manually changed a character).
How can I verify this?
Thanks in advance!
The theoretically correct answer to your question is that whatever you do in your app locally on the device, you will not be more sure whether your data has been tampered or not compared to doing nothing. You will need to use an external, reliable agent such as a https web server to store some information about your data.
Let me explain this briefly. iOS already gives sufficient protection to the data of your app with its data sandboxing model assuming that the device of the user in not jailbroken. This means that there is no way to alter the private data of your app (saved in the standard Documents, Caches, etc. folder of your app). On the other hand a malicious user who wants to modify the encrypted information stored in the private folder of your app, can jailbreak the device. In this case he will have access to everything and can read and write any private folders.
Some could say that you can create a hash or a digital signature of your data and store it on the device. But if the device if jailbroken the intruder would have access to everything, including the binary code of your app (Objective-C can be reverse engineered without too much effort) and so the hash generating algorithm, the salt or the private key of your digital signature. So he could easily manipulate the digital signature or the hash as well.
You should evaluate the risk of your data being tampered. How much effort worth tampering your data? If you are storing banking information and count about 1 million of users, you should take seriously the risk that somebody could jailbreak the device, reverse engineer your app and tamper your data. If you are storing session tokens of a photo-sharing app, maybe you can just leave it as it is and trust in the sandboxing of iOS. Also it is very important that jailbreaking the device needs active participance of the user so it is not possible to do it remotely, without the consent of the user. This means that the data can not be tampered without the consent of the user.
If you really need to protect your data then the only way is to use an external service that you trust and that can provide your an external security regarding to the device. You could send your sensitive data to a web server through https, let the web server sign it with its private key and resend to you the signature that you can store calmly wherever you want. Every time you need to access to your sensitive data you could verify the signature of the data (and the validity of the signature with the public certificate of the server). As the private key is stored on the remote server, you can be sure that nobody can access it and it can not be used to sign a tampered data.
RNCryptor includes an HMAC to detect this. If the ciphertext is modified, it will not decrypt. You'll get an error ("HMAC Mismatch").

Resources