Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store public/private key securely

Looking to store public/private key information securely on an iOS device. I know I want to store this in the KeyChain however I am not 100% sure what sort of attributes I need to populate in the 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
});

Which would store the private key, then follow a similar approach for the public key replacing the Account attribute with a value unique to the user e.g. username. However, not sure if this is the right way to use this.

Does anyone have a good examples on how you would do this specifically for keys?

like image 599
James Avatar asked Sep 28 '12 16:09

James


1 Answers

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 each public key is mapped to an individual user and each private key is mapped to a public key which allows me to store multiple user keys (rather than only storing current logged in users).

Seems to work ok, however, still not 100% sure it is the correct way to do this kind of thing so some clarification would be nice.

like image 138
James Avatar answered Nov 15 '22 10:11

James