2016-12-14 180 views
0

我从openSSL创建CSR,但由于OpenSSL没有将密钥存储在安全区域,因此我选择客观C在安全区域创建密钥对(私钥和公钥)并发送给OpenSSL for X509证书。我在NSData中成功获取公钥,然后转换const unsigned char * bitsOfKeyDataPublicKey = (unsigned char *) [publicKey bytes];,然后创建公钥EC_KEY*_ec_keyPublic = d2i_EC_PUBKEY(NULL,&bitsOfKeyDataPublicKey, publicKeyLegnth);。但对于私钥,我们从目标c得到SecKeyRef,因此创建EC_Key我们如何转换私钥或以任何方式转换或使用私钥? 寻找答复。 感谢在iOS中将SecKeyRef转换为EC_KEY

回答

1

您可以更改私钥从SecKeyRefNSData

例子:

- (NSData *)getPrivateKeyBits { 
    OSStatus sanityCheck = noErr; 
    NSData * privateKeyBits = nil; 

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

    // Set the public key query dictionary. 

    [queryPrivateKey setObject:(id)kSecClassKey forKey:(id)kSecClass]; 
    [queryPrivateKey setObject:_privateTag forKey:(id)kSecAttrApplicationTag]; 
    [queryPrivateKey setObject:(id)kSecAttrKeyTypeEC forKey:(id)kSecAttrKeyType]; 
    [queryPrivateKey setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnData]; 

    // Get the key bits. 
    sanityCheck = SecItemCopyMatching((__bridge CFDictionaryRef)queryPrivateKey, (void *)&privateKeyBits); 

    if (sanityCheck != noErr) { 
     privateKeyBits = nil; 
    } 
    else if (sanityCheck == errSecItemNotFound) { 
     privateKeyBits = nil; 
    } 

    return privateKeyBits; 
} 

不要忘了使用用于生成_privateTag私钥

现在你可以使用:

const unsigned char *bitsOfKeyDataPrivateKey = (unsigned char *) [[self getPrivateKeyBits] bytes]; 
EC_KEY *_ec_keyPrivate = d2i_EC_PUBKEY(NULL,&bitsOfKeyDataPrivateKey, privateKeyLegnth); 
+0

必须在“privateKeyLegnth”中给出什么? –