2

I am able to export a public key generated in iOS and convert it to base64:

var dataPtr:Unmanaged<AnyObject>?
let query: [String:AnyObject] = [
    kSecClass: kSecClassKey,
    kSecAttrApplicationTag: "com.example.site.public",
    kSecReturnData: kCFBooleanTrue
]
let qResult = SecItemCopyMatching(query, &dataPtr)

// error handling with `qResult` ...

let publicKeyData = dataPtr!.takeRetainedValue() as NSData

// convert to Base64 string
let base64PublicKey = publicKeyData.base64EncodedStringWithOptions(nil)

I also was able to send it to my LAMP server. Now I'm trying to figure out how to use it as a public key over there.

I tried:

$keydata = base64decode($_GET['base64PublicKey']);
$res = gnupg_init();
$info = gnupg_import($res,$keydata);
print_r($info);

Is this the right direction? I'm aware that the size of the data is 270, not the same as block size of the key. I'm somewhat new to PKI, any help would be appreciated.

Community
  • 1
  • 1
jackreichert
  • 1,979
  • 2
  • 23
  • 36
  • The certificate is in DER-encoded X.509 format ? – Edgar Orozco Feb 01 '15 at 02:41
  • I think so, but I am not completely sure. This seems to indicate so: http://stackoverflow.com/questions/3840005/how-to-find-out-the-modulus-and-exponent-of-rsa-public-key-on-iphone-objective-c – jackreichert Feb 01 '15 at 02:47

1 Answers1

0

If your pub key is X.509 conformant you can parse with PHP like this:

$keydata = base64decode($_GET['base64PublicKey']);
$info = openssl_x509_parse($keydata,true);
print_r($info);

PHP includes a library for OpenSSl and X.509

Edgar Orozco
  • 2,722
  • 29
  • 33
  • Thanks for the help! It turns out that `publicKeyData.base64EncodedStringWithOptions(nil)` is returning non-base64 characters -- specifically forward slashes and spaces, so base64_decode() is returning false. Once I get past that I'll be able to test your code. – jackreichert Feb 01 '15 at 19:42