gpt4 book ai didi

ios - SecItemCopyMatching 返回 nil 值没有任何错误

转载 作者:可可西里 更新时间:2023-11-01 03:56:08 28 4
gpt4 key购买 nike

我正在尝试使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com