- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试使用 CryptoExercise 的 SecKeyWrapper addPeerPublicKey:keyBits:
方法将 RSA 公钥添加到我的 iPhone 钥匙串(keychain)。此方法的逻辑是它首先尝试将 key 添加到钥匙串(keychain),如果它已经存在 (sanityCheck==errSecDuplicateItem)
它尝试通过调用 SecKeyItemCopyMatching 从钥匙串(keychain)中检索此 key ()
。
这正是我的情况: key 已经在钥匙串(keychain)中,因此对 SecKeyItemAdd() 的调用返回 errSecDuplicateItem
。
然后它尝试检索现有 key ,但 SecKeyItemCopyMatching() 返回 0
(表示没有错误),但第二个参数 (peerKeyRef
) 仍然绝望地为 nil。
这怎么可能?这有什么问题吗?
这里是 CryptoExercise 示例中 [SecKeyWrapper addPeerPublicKey:keyBits:]
的代码以供引用:
- (SecKeyRef)addPeerPublicKey:(NSString *)peerName keyBits:(NSData *)publicKey {
OSStatus sanityCheck = noErr;
SecKeyRef peerKeyRef = NULL;
CFTypeRef persistPeer = NULL;
LOGGING_FACILITY( peerName != nil, @"Peer name parameter is nil." );
LOGGING_FACILITY( publicKey != nil, @"Public key parameter is nil." );
NSData *peerTag = [[NSData alloc] initWithBytes:(const void *) [peerName UTF8String] length:[peerName length]];
NSMutableDictionary *peerPublicKeyAttr = [[NSMutableDictionary alloc] init];
[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:publicKey forKey:(__bridge id) kSecValueData];
[peerPublicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id) kSecReturnPersistentRef];
sanityCheck = SecItemAdd((__bridge CFDictionaryRef) peerPublicKeyAttr, (CFTypeRef *) &persistPeer);
// The nice thing about persistent references is that you can write their value out to disk and
// then use them later. I don't do that here but it certainly can make sense for other situations
// where you don't want to have to keep building up dictionaries of attributes to get a reference.
//
// Also take a look at SecKeyWrapper's methods (CFTypeRef)getPersistentKeyRefWithKeyRef:(SecKeyRef)key
// & (SecKeyRef)getKeyRefWithPersistentKeyRef:(CFTypeRef)persistentRef.
LOGGING_FACILITY1( sanityCheck == noErr || sanityCheck == errSecDuplicateItem, @"Problem adding the peer public key to the keychain, OSStatus == %ld.", sanityCheck );
if (persistPeer) {
peerKeyRef = [self getKeyRefWithPersistentKeyRef:persistPeer];
} else {
[peerPublicKeyAttr removeObjectForKey:(__bridge id) kSecValueData];
[peerPublicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id) kSecReturnRef];
// Let's retry a different way.
sanityCheck = SecItemCopyMatching((__bridge CFDictionaryRef) peerPublicKeyAttr, (CFTypeRef *) &peerKeyRef);
}
LOGGING_FACILITY1( sanityCheck == noErr && peerKeyRef != NULL, @"Problem acquiring reference to the public key, OSStatus == %ld.", sanityCheck );
if (persistPeer) CFRelease(persistPeer);
return peerKeyRef;
}
最佳答案
我遇到了同样的问题,我假设您尝试导入不是从其他 iOS 设备导出的 RSA key 。
原因似乎是不兼容的 key 格式——详细来说,iOS 期望某些 ASN1 header 不被设置。为什么函数返回 OK 对我来说只能用错误来解释......
查看 http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios/ 处的代码这是正确的解决方案并且对我有用 - 感谢 Chris Luke
关于ios - SecItemCopyMatching 返回 nil 值没有任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17623046/
我在下一个代码中出现内存泄漏。我的灵感来自here这是RSA算法的一部分。 - (SecKeyRef)getPublicKeyRef { OSStatus resultCode = noErr; Se
我想在运行时将 OSX/iOS 证书导入到 OpenSSL 上下文中。为此,我使用带有以下代码的 SecItemCopyMatching 从操作系统钥匙串(keychain)中检索证书: CFMuta
首先,我观看了关于使用钥匙串(keychain)保护 secret 的 WWDC 2013 session 。我想做一个基本的密码存储。观看了整个视频,但在视频的前 10 分钟找到了我需要的内容。看起
我的方法: +(SecKeyRef)getKeyByTagWithoutAlert:(NSString *)keyTag status:(OSStatus *) status{ *status
与 REST API 通信并将 API token 保存在 iOS 的钥匙串(keychain)中。但钥匙串(keychain)代码抛出 nil 错误。 KeychainAccess.swift: p
我正在尝试使用钥匙串(keychain)服务来保存一个值,即使用户重新安装应用程序,该值也会持续存在。所以我使用 SecItemCopyMatching 检查一个项目是否存在,它第一次返回 errSe
我正在将一个项目添加到钥匙串(keychain),然后我想获取该项目的值(value)。问题是,它原来是一个空字符串。我究竟做错了什么?谢谢! //add item to keychain NSDic
我正在尝试从已生成的 key 对(两个 SecKeyRef)中提取一个 1024 位 RSA 公钥,以便通过网络发送它。我只需要一个普通的 (modulus, exponent) 对,它应该正好占用
我的应用程序中有以下代码片段。 CFTypeRef result = nil; OSStatus userPresenceStatus = SecItemCopyMatching((_
我无法让此调用适用于 IOS。我尝试了多种方法,但似乎没有任何效果:我总是获得 errSecParam 状态。谁能告诉我我做错了什么? 我开始使用它来获取我从字节中提取的证书的属性列表。那没有用,所以
我正在尝试使用 CryptoExercise 的 SecKeyWrapper addPeerPublicKey:keyBits: 方法将 RSA 公钥添加到我的 iPhone 钥匙串(keychain
KeychainTouchID应用程序是使用 Xcode 安装并执行的。 key 是通过按“添加 protected key ”生成的,并通过按“使用 protected key ”使用。 key 是
我从 Stack Overflow 上抓取了代码来访问一些网络浏览器密码。只要密码在登录钥匙串(keychain)中,它就可以很好地工作。在某些时候,我感兴趣的特定帐户已移至 iCloud 钥匙串(k
我使用 Veracode 扫描我的应用程序并出现以下错误未检查的错误条件。这是我的代码: let status = withUnsafeMutablePointer(to: &queryRes
所以我的问题是关于钥匙串(keychain)访问是如何工作的。如果我已经使用 kSecAttrAccessible 属性集将一个项目保存到钥匙串(keychain),并且如果我想稍后查询这个项目,我是
我想在我的 iOS 应用程序中支持多个帐户,并且我正在使用 KeyChain 来存储凭据。我正在存储具有相同类 (kSecClassGenericPassword) 和属性 (kSecAttrServ
我正在使用 SecItemCopyMatching 获取受 Touch ID 保护的钥匙串(keychain)项。 但是,如果 Touch ID 解锁失败(或用户选择“输入密码”),我想展示我自己的
我在 SecItemCopyMatching 上发现内存泄漏。经过对 SF 的调查,我找到了解决方案: __block NSString *certificateName = nil; SecKeyc
我正在尝试通过 FaceID 解锁钥匙串(keychain)/安全区域中的数据,并使其在用户 session 期间可访问(无需额外解锁)。 Per the documentation for kSec
我正在为我们的 iPhone 应用程序实现 SSL 客户端身份验证,并使用应用程序钥匙串(keychain)来存储客户端身份(证书 + 私钥)。将项目添加到钥匙串(keychain)后,我在使用 Se
我是一名优秀的程序员,十分优秀!