Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS SecKeyRef (Public Key) send it to server [duplicate]

Now I have a problem with my public key, I used SecKeyGeneratePair to generate public and private. Now i have to send my public key to the server. I have used the below method to convert SecKeyRef to NSData, there always I am getting same public key. However I converted it into base64 format and send it to the server. But its not working at all and server(Java) start throwing errors. Here is my iOS code:

- (NSData *)getPublicKeyBits {
    OSStatus sanityCheck = noErr;
    NSData * publicKeyBits = nil;

    NSMutableDictionary * queryPublicKey = [[NSMutableDictionary alloc] init];

    // Set the public key query dictionary.
    [queryPublicKey setObject:(id)kSecClassKey forKey:(id)kSecClass];
    [queryPublicKey setObject:publicTag forKey:(id)kSecAttrApplicationTag];
    [queryPublicKey setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType];
    [queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnData];

    // Get the key bits.
    sanityCheck = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyBits);

    if (sanityCheck != noErr)
    {
        publicKeyBits = nil;
    }

    [queryPublicKey release];

    return publicKeyBits;
}

Can any one help me to convert SecKeyRef to NSString or NSData.

Thanks in advance!!!

-Murali Krishnan

like image 633
Murali Avatar asked Jun 01 '12 12:06

Murali


1 Answers

I think keys generated by iOS are missing some header information that the rest of the world (Java APIs in your case) expect to see. Therefore, in order for Java to work with a key generated by iOS Security APIs, you first need to add this header information to the binary key.

Conversely, in order for the iOS Sec* APIs to work with a key generated by openssl or ssh-keygen (for example), you must first remove this header information from the binary key.

Specifically, iOS doesn't like the ASN.1 OID, and the rest of the world does.

See this article for exporting an iOS-generated key for use by a Java app -- it should get you going:

http://blog.wingsofhermes.org/?p=42

like image 78
Goffredo Avatar answered Nov 15 '22 17:11

Goffredo