Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C: Exporting Private and Public Key from Keychain

I am able to create a public-private keypair using SecKeyGeneratePair [Apple CryptoExercise]function.

Q1. The keys in the keychain appear as without displaying any name. How can we add a friendly name to the keys. enter image description here

Q2. However how can i export public and private key that has been generated in the usable format:

-----BEGIN RSA PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqCWtYiGnhAv... 
-----END RSA PUBLIC KEY-----

and:

-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----

Note that they can be manually exported from the keychain but how can this be achieved using objective C Apis.

Any help would be appreciable.

There is a similar question here but without any answer: iPhone: How do you export a SecKeyRef or an NSData containing public key bits to the PEM format? There is no need of using OpenSSL just for this purpose.

like image 464
ZestyZest Avatar asked Jun 07 '15 02:06

ZestyZest


People also ask

How do I export a public and private key?

Open Encryption Desktop. Click "PGP Keys". Right-click the key to export then select Send To from the drop-down list. The option to send the public key to a key server listed in the drop-down list, a smart card, or to mail recipient can be chosen.

Can we extract private key from public key?

You cannot generate private key from public key but you can generate public key from the private key using puttygen. As @alfasin mentioned if you could generate the private key from public key then RSA would be useless and this would make you vulnerable to attack.

How do I export from Keypair?

To export an OpenPGP key pair or a public key, simply go to your account Settings -> Messages -> Encryption: For exporting Key pair/Personal key: under My personal keys: Click on the Key pair that you would like to export. Click on Export.


1 Answers

Maybe you could refer to these documents from Apple:

Obtaining a SecKeyRef Object for Public Key Cryptography and Certificate, Key, and Trust Services Programmer’s Guide

Obtaining a SecKeyRef Object for Public Key Cryptography

Extracting Keys from the Keychain If you are using existing public and private keys from your keychain, read Certificate, Key, and Trust Services Programming Guide to learn how to retrieve a SecKeychainItemRef object for that key.

Once you have obtained a SecKeychainItemRef, you can cast it to a SecKeyRef for use with this API.

Importing Existing Public and Private Keys Importing and exporting public and private key pairs is somewhat more complicated than generating new keys because of the number of different key formats in common use.

This example describes how to import and export a key pair in PEM (Privacy Enhanced Mail) format.

To export keys to a CFDataRef object

  1. Create and populate the key usage array.
  2. Create and populate the key attributes array.
  3. Set the key usage and attributes fields in the parameters object.
  4. Set the external format and flag values appropriately.
  5. Export the key with API as follows.
OSStatus oserr = SecItemExport(publickey,
    externalFormat, // See SecExternalFormat for details
    flags, // See SecItemImportExportFlags for details
    &params,
    (CFDataRef *)&pkdata); if (oserr) {
    fprintf(stderr, "SecItemExport failed (oserr=%d)\n", oserr);
    exit(-1); }
like image 177
Eric Tsui Avatar answered Sep 28 '22 06:09

Eric Tsui