目标C-SecItemAdd具有错误:EXC_BAD_ACCESS(第一次)和errSecDuplicateItem(第二次) [英] Objective C - SecItemAdd has error:EXC_BAD_ACCESS (first time) and errSecDuplicateItem (second time)

查看:54
本文介绍了目标C-SecItemAdd具有错误:EXC_BAD_ACCESS(第一次)和errSecDuplicateItem(第二次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码 http://wiki.effectiveprogramming.com/index.php?title = CocoaEncryption& redirect = no 在我的应用程序中实现了登录功能.但是我有一个错误方法saveRSAPublicKey:

  +(BOOL)saveRSAPublicKey:(NSData *)publicKey appTag:(NSString *)appTag覆盖:(BOOL)overwrite {//这里出错(第一次调用时)-程序收到信号:"EXC_BAD_ACCESS"->碰撞OSStatus status = SecItemAdd((CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:(id)kSecClassKey,kSecClass,(id)kSecAttrKeyTypeRSA,kSecAttrKeyType,(id)kSecAttrKeyClassPublic,kSecAttrKeyClass,kCFBooleanTrue,kSecAttrIsPermanent,[appTag dataUsingEncoding:NSUTF8StringEncoding],kSecAttrApplicationTag,publicKey,kSecValueData,kCFBooleanTrue,kSecReturnPersistentRef,零],空值);//不需要公钥引用DebugLog(@"result =%@",[KeychainUtil fetchStatus:status]);if(状态== noErr)返回是;否则if(状态== errSecDuplicateItem&&覆盖==是)返回[CryptoUtil updateRSAPublicKey:publicKey appTag:appTag];返回否;} 

当我第一次调用 saveRSAPublickey 时,我无法创建OSStatus并且我的应用程序崩溃了.当我第二次调用 saveRSAPublickey 时:status == errSecDuplicateItem 并运行 [CryptoUtil updateRSAPublicKey:publicKey appTag:appTag] ->成功.>

调试时很难发现此问题,因为它仅出现在新设备中(从未安装过我的应用程序).最后,我通过以下方式发现了错误:在调用saveRSAPublicKey之前设置新的appTag.我在system.log中看到:

4月6日12:30:29 MACs-MacBook-Pro安全性[4372]:无法访问密钥>

请帮助我!!!谢谢大家.

解决方案

如果您将 NULL 传递给第二个arg(结果将显示在此处),则不允许您使用调用 SecItemAdd()时设置的返回类型键(将 kCFBooleanTrue,kSecReturnPersistentRef 更改为 kCFBooleanFalse,kSecReturnPersistentRef 或将其删除).

我刚才提交了一个错误报告.

I use this code http://wiki.effectiveprogramming.com/index.php?title=CocoaEncryption&redirect=no in my application to implement Login function. But I have a error method saveRSAPublicKey:

+ (BOOL)saveRSAPublicKey:(NSData*)publicKey appTag:(NSString*)appTag overwrite:(BOOL)overwrite {
    //Error here (when first call) - Program received signal: "EXC_BAD_ACCESS" -> crash
    OSStatus status = SecItemAdd((CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
                                                   (id)kSecClassKey, kSecClass,
                                                   (id)kSecAttrKeyTypeRSA, kSecAttrKeyType,
                                                   (id)kSecAttrKeyClassPublic, kSecAttrKeyClass,
                                                   kCFBooleanTrue, kSecAttrIsPermanent,
                                                   [appTag dataUsingEncoding:NSUTF8StringEncoding], kSecAttrApplicationTag,
                                                   publicKey, kSecValueData,
                                                   kCFBooleanTrue, kSecReturnPersistentRef,
                                                   nil],
                                                   NULL);   //don't need public key ref

    DebugLog(@"result = %@", [KeychainUtil fetchStatus:status]);

    if(status == noErr)
        return YES;
    else if(status == errSecDuplicateItem && overwrite == YES)
        return [CryptoUtil updateRSAPublicKey:publicKey appTag:appTag];

    return NO;
}

When I call saveRSAPublickey for first time, I can't create OSStatus and my application is crashed. When I call saveRSAPublickey for second time: status == errSecDuplicateItem and run [CryptoUtil updateRSAPublicKey:publicKey appTag:appTag] -> success.

It's difficult to find this problem when debugging because it just only appear in new device ( have never been installed my app). Finally, I have found the error by the way: set new appTag before call saveRSAPublicKey. I see in system.log:

Apr 6 12:30:29 MACs-MacBook-Pro securityd[4372]: unable to access hwaes key

Please help me !!! Thanks all.

解决方案

If you pass NULL for the 2nd arg (where your result would come out), then you're not allowed to have a Return Type Key set when calling SecItemAdd() (change kCFBooleanTrue, kSecReturnPersistentRef to kCFBooleanFalse, kSecReturnPersistentRef or just delete it).

I submitted a bug report on this just now.

这篇关于目标C-SecItemAdd具有错误:EXC_BAD_ACCESS(第一次)和errSecDuplicateItem(第二次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆