How to safely store public / private key

Looking for reliable storage of public / secret key information on your iOS device. I know that I want to save this in KeyChain, but I'm not 100% sure which attributes I need to fill in in SecRecord . I was going to do something like:

 // private key SecKeyChain.Add(new SecRecord(SecKind.Key) { Accessible = SecAccessible.AlwaysThisDeviceOnly, KeySizeInBits = 512, KeyClass = SecKeyClass.Private, CanSign = true, ValueData = privateKeyValue, Account = publicKeyValue }); 

To save the private key, and then follow a similar approach for the public key, replacing the Account attribute with a value unique to the user, for example. Username. However, I'm not sure if this is the right way to use this.

Does anyone have any good examples of how you will do this specifically for keys?

+4
source share
1 answer

I decided to go with the following approach:

 // store public key SecKeyChain.Add(new SecRecord(SecKind.Key) { ApplicationLabel = userName, Accessible = SecAccessible.AlwaysThisDeviceOnly, KeySizeInBits = 512, KeyClass = SecKeyClass.Public, ValueData = NSData.FromString(publicKey) }); // store private key SecKeyChain.Add(new SecRecord(SecKind.Key) { ApplicationLabel = publicKey, Accessible = SecAccessible.AlwaysThisDeviceOnly, KeySizeInBits = 512, KeyClass = SecKeyClass.Private, CanSign = true, ValueData = NSData.FromString(secretKey) }); 

This means that each public key is mapped to an individual user, and each private key is mapped to a public key, which allows you to store several user keys (and not just to store current registered users).

Everything seems to be working fine, but is 100% not sure if this is the right way to do such things so that some clarifications are pleasant.

+3
source

Source: https://habr.com/ru/post/1436856/


All Articles