0

I tried to ask a follow-up question on this topic with example code on how to generate rsa-keys which did not work for me, but it got deleted by moderator for some reason. So I'm going to try agin with posting a new question.

My problem with that answer is that Xcode tells me that "kSecPrivateKeyAttrs" and "kSecPublicKeyAttrs" identifiers is not declared. These identifiers are mentioned in apple developer docs, but they does not seem to exist in Secure framework.

I'm using Xcode 4.5 and OS X SDK 10.8.

appreciate any help i can get, I'm fairly new att OC-programming. If i get this to work, I Would also like to know how to get pubkey and privkey as NSString or NSData.

Thank you

EDIT: I still have problems with this, surely there's someone else out there with the same problem that have fixed it and can point me in the right direction?

EDIT2 As I Said, i was trying out the code from the link i posted, but here is the full code anyways:

Keypair.h

#import <Security/Security.h>

@interface Keypair
{
    SecKeyRef publicKey;
    SecKeyRef privateKey;
    NSData *publicTag;
    NSData *privateTag;
 }
 - (void)generateKeyPair:(NSUInteger)keySize;
 @end

Keypair.m

#import "Keypair.h"

@implementation Keypair

static const UInt8 publicKeyIdentifier[] = "com.XXXXXXX.publickey\0";
static const UInt8 privateKeyIdentifier[] = "com.XXXXXXX.privatekey\0";

+ (void)generateKeyPair:(NSUInteger)keySize {
    OSStatus sanityCheck = noErr;
    SecKeyRef publicKey = NULL;
    SecKeyRef privateKey = NULL;
    NSData *publicTag;
    NSData *privateTag;

    // Container dictionaries.
    NSMutableDictionary * privateKeyAttr = [[NSMutableDictionary alloc] init];
    NSMutableDictionary * publicKeyAttr = [[NSMutableDictionary alloc] init];
    NSMutableDictionary * keyPairAttr = [[NSMutableDictionary alloc] init];

    // Set top level dictionary for the keypair.
    [keyPairAttr setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
    [keyPairAttr setObject:[NSNumber numberWithUnsignedInteger:keySize] forKey:(__bridge id)kSecAttrKeySizeInBits];
    [keyPairAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];

    // Set the private key dictionary.
    [privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];
    [privateKeyAttr setObject:privateTag forKey:(__bridge id)kSecAttrApplicationTag];
    // See SecKey.h to set other flag values.

    // Set the public key dictionary.
    [publicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent];
    [publicKeyAttr setObject:publicTag forKey:(__bridge id)kSecAttrApplicationTag];
    // See SecKey.h to set other flag values.

    // Set attributes to top level dictionary.
    [keyPairAttr setObject:privateKeyAttr forKey:(id)kSecPrivateKeyAttrs];
    [keyPairAttr setObject:publicKeyAttr forKey:(id)kSecPublicKeyAttrs];


    // SecKeyGeneratePair returns the SecKeyRefs just for educational purposes.
    sanityCheck = SecKeyGeneratePair((__bridge CFDictionaryRef)keyPairAttr, &publicKey, &privateKey);
    if(sanityCheck == noErr  && publicKey != NULL && privateKey != NULL)
    {
        NSLog(@"Successful");
    }
}

@end
Community
  • 1
  • 1
Dlq
  • 91
  • 2
  • 5
  • Your answer was deleted as this is not a forum. Please show your code; you are almost certainly failing to `#import` a header file. – trojanfoe Oct 16 '12 at 12:11
  • I've posted the full code I'm trying to use. Can't find any missing #import. Maybe I need something else for OSX? I can't see why this would be different from IOS, but if it is, please suggest what other than kSec{Public,Private}KeyAttrs i should use. – Dlq Oct 27 '12 at 20:07
  • Ok, so i got this to work by simply replacing kSecPublicKeyAttrs with @"public" and @"private", for some reason they only exist in Ios version of Security.framework. Now it's just a mather of retrieving the public key for later use. OT: Why do Apple hate me? – Dlq Oct 28 '12 at 22:45

2 Answers2

0

Could you retrieve the keys after that? I cannot get the keys back. It is like they are not in the key chain. Calling SecItemCopyMatching returns error saying the key was not found.

Raphael
  • 71
  • 2
0

A little late but since I'm currently dealing with a related issue I figured I shed some light.

For iOS you specify individual dictionaries for private and public key, their attributes can be different. But for Mac you put all the attributes directly in one dictionary and they are applied to both keys.