簡體   English   中英

目標 C - SecItemAdd 有錯誤:EXC_BAD_ACCESS(第一次)和 errSecDuplicateItem(第二次)

[英]Objective C - SecItemAdd has error:EXC_BAD_ACCESS (first time) and errSecDuplicateItem (second time)

我在我的應用程序中使用此代碼http://wiki.effectiveprogramming.com/index.php?title=CocoaEncryption&redirect=no來實現登錄 function。但是我有一個錯誤方法 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;
}

當我第一次調用saveRSAPublickey時,我無法創建 OSStatus 並且我的應用程序崩潰了。 當我第二次調用saveRSAPublickey時:status == errSecDuplicateItem並運行[CryptoUtil updateRSAPublicKey:publicKey appTag:appTag] -> 成功。

調試時很難發現這個問題,因為它只出現在新設備上(從未安裝過我的應用程序)。 最后順便發現錯誤:set new appTag before call saveRSAPublicKey。 我在 system.log 中看到:

4 月 6 日 12:30:29 MACs-MacBook-Pro securityd[4372]:無法訪問 hwaes 密鑰

請幫我。!! 謝謝大家。

如果您為第二個 arg 傳遞NULL (您的結果將在此處出現),則不允許在調用SecItemAdd()時設置Return Type Key (將kCFBooleanTrue, kSecReturnPersistentRef更改為kCFBooleanFalse, kSecReturnPersistentRef或將其刪除)。

我剛才提交了一份錯誤報告。

我猜想 publicKey 或者更可能是 appTag 不包含您認為的內容。 嘗試將它們注銷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM