iOS:将私钥添加到设备KeyChain
我有一个项目,我从服务器接收用户的加密RSA私钥。使用用户提供的信息,我能够将数据解密成预期的格式。但是,我无法弄清楚如何将私钥加载到iOS密钥链中以便在RSA加密函数中使用。iOS:将私钥添加到设备KeyChain
目前,我有以下代码,我已经从各种示例拼凑在一起。此代码适用于添加公钥,但似乎根本无法添加私钥。
[peerPublicKeyAttr setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
[peerPublicKeyAttr setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
[peerPublicKeyAttr setObject:peerTag forKey:(__bridge id)kSecAttrApplicationTag];
[peerPublicKeyAttr setObject:privateKeyData forKey:(__bridge id)kSecValueData];
[peerPublicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef];
sanityCheck = SecItemDelete((__bridge CFDictionaryRef) peerPublicKeyAttr) ;
sanityCheck = SecItemAdd((__bridge CFDictionaryRef) peerPublicKeyAttr, (CFTypeRef *)&privateKey);
当运行此代码与私有密钥数据(解码成DER格式),privateKey
变量由SecItemAdd()
呼叫设置为NULL。但是,sanityCheck
变量指示“无错误”。我不知道我错过了什么。
我需要做什么才能让私钥成功加载到钥匙串中?
在这个确切的主题Official response from Apple是,这是不支持的。获得私钥进入密钥链唯一受支持的方式是通过PKCS#12文件。
但是那么你如何访问私人密钥导入后? – 2015-02-17 20:03:44
导入P12文件后,您可以使用SecItemCopyMatching()函数访问私钥。 – 2015-02-17 22:35:55
此答案中的链接不起作用。它重定向到https://developer.apple.com/account。 – 2017-05-24 12:16:15
我现在不能测试此权利...更详细的资料/样本源等:
- http://blog.wingsofhermes.org/?p=75(注意这个comment太)
- https://github.com/kuapay/iOS-Certificate--Key--and-Trust-Sample-Project一些链接(这不正是你想要什么)
- http://developer.apple.com/library/ios/#DOCUMENTATION/Security/Conceptual/keychainServConcepts/02concepts/concepts.html
- http://useyourloaf.com/blog/2010/3/29/simple-iphone-keychain-access.html
- http://developer.apple.com/library/ios/#DOCUMENTATION/Security/Reference/keychainservices/Reference/reference.html
- https://developer.apple.com/library/ios/#samplecode/GenericKeychain/Listings/ReadMe_txt.html#//apple_ref/doc/uid/DTS40007797-ReadMe_txt-DontLinkElementID_11
- http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios/
[在iOS上使用RSA公钥](http://stackoverflow.com/questions/9728799/using-an-rsa-public-key-on-ios) – bobobobo 2013-04-19 02:00:14