简体   繁体   English

OSStatus 错误代码 -34018

[英]OSStatus error code -34018

I am using SecItemCopyMatching to access the iOS keychain.我正在使用SecItemCopyMatching访问 iOS 钥匙串。 About 1 in a hundred times I get a -34018 result code right after relaunching the app from the background.从后台重新启动应用程序后,大约有一百次我得到-34018结果代码。 The documentation states: 该文件指出:

The assigned error space for Keychain Services is discontinuous: –25240 through –25279 and –25290 through –25329.为钥匙串服务分配的错误空间是不连续的:–25240 到 –25279 和 –25290 到 –25329。 Keychain Item Services may also return noErr (0) or paramErr (–50), or CSSM result codes Keychain Item Services 也可能返回 noErr (0) 或 paramErr (–50),或 CSSM 结果代码

So it seems that -34018 is a 'CSSM result code'.因此, -34018似乎是一个“CSSM 结果代码”。 I have followed the suggested link but could not find result codes.我遵循了建议的链接,但找不到结果代码。

What it the -34018 result code? -34018结果代码是什么? How can I get more reliable keychain access?如何获得更可靠的钥匙串访问权限?

- (NSData *)getKeychainData:(NSString *)key
{
    NSDictionary *query = @{
        (__bridge id)kSecClass:(__bridge id)kSecClassGenericPassword,
        (__bridge id)kSecAttrService:SEC_ATTR_SERVICE,
        (__bridge id)kSecAttrAccount:key,
        (__bridge id)kSecReturnData:@YES
    };

    CFDataRef result = nil;

    OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&result);

    if(status == errSecItemNotFound) {
        return nil;
    }

    if(status == noErr) {
        return CFBridgingRelease(result);
    } else {
        [self logError:[NSString stringWithFormat:@"SecItemCopyMatching status %d", (int)status] :nil];
        return nil;
    }
}

I've been just researching the same error.我一直在研究同样的错误。

The gist of it is that the security service apple uses in order to communicate with the key chain, in rare cases, when the user's device is low on memory, crashes and taking away the app ability to talk to the keychain which results the dreadful -34018.其要点是苹果使用安全服务与钥匙串进行通信,在极少数情况下,当用户的设备内存不足时,会崩溃并剥夺应用程序与钥匙串对话的能力,从而导致可怕的 - 34018。

This is not happening only while running through Xcode like some may claim.这不仅仅是在运行 Xcode 时发生,就像有些人可能声称的那样。

This is the most recent data regarding the issue taken from the Apple developer forums by one of the Apple staff:这是一位 Apple 员工从 Apple 开发者论坛获取的有关该问题的最新数据

UPDATE: We have finally been able to reproduce the -34018 error on iOS 8.3.更新:我们终于能够在 iOS 8.3 上重现 -34018 错误。 This is the first step in identifying the root cause and then coming up with a fix.这是确定根本原因然后提出解决方案的第一步。

As usual, we can't commit to a release timeframe, but this has affected many developers and we really want to get this resolved.像往常一样,我们无法承诺发布时间表,但这已经影响了许多开发人员,我们真的很想解决这个问题。

Earlier I suggested adding a small delay in application:didFinishLaunchingWithOptions and applicationDidBecomeActive: before accessing the keychain as a workaround.早些时候,我建议在访问钥匙串之前在 application:didFinishLaunchingWithOptions 和 applicationDidBecomeActive: 中添加一个小延迟作为解决方法。 However, that doesn't actually appear to help.然而,这实际上似乎没有帮助。 That means that there's no known workaround at this time other than relaunching the app.这意味着除了重新启动应用程序之外,目前没有已知的解决方法。

The issue appears to be related to memory pressure, so perhaps being more aggressive in handling memory warnings may alleviate the problem.该问题似乎与内存压力有关,因此也许更积极地处理内存警告可能会缓解该问题。

From Another Apple staff member:来自另一位苹果员工:

  • Keychain engineering is well aware of how important this issue is.钥匙串工程很清楚这个问题的重要性。
  • The primary problem has been reproducing the failure here at Apple.主要问题是在苹果这里重现失败。
  • We're now able to do that (largely thanks to the work you guys have put in filing and following up on your bug reports).我们现在能够做到这一点(很大程度上要感谢你们在归档和跟进错误报告方面所做的工作)。

From Another Apple staff member on Mar 22, 2016 : 20163 月 22 日,另一位 Apple 员工:

OK, here's the latest.好的,这是最新的。 This is a complex problem with multiple possible causes: Some instances of the problem are caused by incorrect app signing.这是一个具有多种可能原因的复杂问题:该问题的某些实例是由不正确的应用程序签名引起的。 You can easily distinguish this case because the problem is 100% reproducible.您可以轻松区分这种情况,因为问题 100% 可重现。 Some instances of the problem are caused by a bug in how iOS supports app development (r. 23,991,853).该问题的某些实例是由 iOS 如何支持应用程序开发的错误引起的 (r. 23,991,853)。 Debugging this was complicated by the fact that another bug in the OS (r. 23,770,418) masked its effect, meaning the problem only cropped up when the device was under memory pressure.由于操作系统中的另一个错误 (r. 23,770,418) 掩盖了其影响,因此调试变得复杂,这意味着该问题仅在设备处于内存压力下时才会出现。 We believe these problems were resolved in iOS 9.3.我们相信这些问题已在 iOS 9.3 中得到解决。 We suspect that there may be yet more causes of this problem.我们怀疑这个问题可能还有更多的原因。 So, if you see this problem on a user device (one that hasn't been talked to by Xcode) that's running iOS 9.3 or later, please do file a bug report about it.因此,如果您在运行 iOS 9.3 或更高版本的用户设备(Xcode 未处理过的设备)上看到此问题,请提交有关它的错误报告。 Try to include the device system log in your bug report (I realise that can be tricky when dealing with customer devices; one option is to ask the customer to install Apple Configurator, which lets them view the system log).尝试在您的错误报告中包含设备系统日志(我意识到在处理客户设备时可能会很棘手;一种选择是要求客户安装 Apple Configurator,让他们查看系统日志)。 And if you do file a bug, please post your bug number, just for the record.如果您确实提交了错误,请发布您的错误编号,仅供记录。 On behalf of Apple I'd like to thank everyone for their efforts in helping to track down this rather horrid issue.我代表 Apple 感谢大家为帮助追踪这个相当可怕的问题所做的努力。 Share and Enjoy分享和享受

Unfortunately there are no known workarounds and the issue is still not fixed in 9.3.2 Beta 1 (13F51a)不幸的是,没有已知的解决方法,该问题在 9.3.2 Beta 1 (13F51a) 中仍未解决

After some research, I found this: http://opensource.apple.com/source/Security/Security-55471/sec/Security/SecBasePriv.h经过一番研究,我发现了这个: http : //opensource.apple.com/source/Security/Security-55471/sec/Security/SecBasePriv.h

So -34018 is errSecMissingEntitlement and the comment says所以-34018errSecMissingEntitlement并且评论说

Internal error when a required entitlement isn't present.

Do you experience this error while running your unit tests?您在运行单元测试时是否遇到此错误? If so, this might help: https://stackoverflow.com/a/22305193/171933如果是这样,这可能会有所帮助: https : //stackoverflow.com/a/22305193/171933

This issue on github says that it only seems to happen while debugging from Xcode: https://github.com/soffes/sskeychain/issues/97 (also see https://stackoverflow.com/a/28256591/171933 ) github 上的这个问题说它似乎只在从 Xcode 调试时发生: https : //github.com/soffes/sskeychain/issues/97 (另见https://stackoverflow.com/a/28256591/171933

Hopefully some of this will help!希望其中一些会有所帮助!

This code works for me:这段代码对我有用:

static const UInt8 kKeychainItemIdentifier[] = "com.apple.dts.KeychainUI\0";

- (NSData *)getKeychainData:(NSString *)key
{
    NSData *keychainItemID = [NSData dataWithBytes:kKeychainItemIdentifier length:strlen((const char *)kKeychainItemIdentifier)];

    NSDictionary *query = @{
        (__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
        (__bridge id)kSecAttrService: SEC_ATTR_SERVICE,
        (__bridge id)kSecAttrAccount: key,
        (__bridge id)kSecReturnData: (__bridge id)kCFBooleanTrue,
        (__bridge id)kSecAttrGeneric: keychainItemID
    };

    CFDataRef result = NULL;

    OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&result);

    if(status == errSecItemNotFound) {
        return nil;
    }

    if(status == noErr) {
        return CFBridgingRelease(result);
    } else {
        [self logError:[NSString stringWithFormat:@"SecItemCopyMatching status %d", (int)status] :nil];
        return nil;
    }
}

The main difference with OP's code is the addition of a Generic Attribute to the query.与 OP 代码的主要区别在于向查询添加了通用属性。 The Keychain Item Identifier is the default from apple.钥匙串项目标识符是苹果的默认值。 The reason behind this comes to differentiate possible different keychain items from each other.这背后的原因是为了区分可能的不同钥匙串项目。 This is one way to make a more the keychain items access more reliable.这是使更多钥匙串项目访问更可靠的一种方法。 Basically, in other words, this makes sure you access apple's default keychain.基本上,换句话说,这可以确保您访问苹果的默认钥匙串。

After trying many of the fixes in stack overflow, things still didn't work for me.在尝试了许多堆栈溢出修复之后,事情仍然对我不起作用。

What worked was switching the Keychain Sharing Capability in Xcode.有效的是在 Xcode 中切换钥匙串共享功能。 Built and run and it worked right away.构建并运行,它立即开始工作。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM