Saving SecKeyRef device generated public/private key pair on disk

前端 未结 3 1328
轮回少年
轮回少年 2020-12-08 05:39

I\'ve generated an RSA symmetric key pair on a device using SecKeyGeneratePair() on a device. I have SecKeyRef struct pointers for each key. So, ho

3条回答
  •  误落风尘
    2020-12-08 05:48

    Ah, found the answer myself; you can get the bytes for a public key using SecItemCopyMatching().

    - (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;
    }
    

    The above is from Apple's CryptoExercise. Not sure if it works for private keys though.

提交回复
热议问题