从受信任的应用程序访问 OS X 钥匙串项目

2024-02-09

我正在创建一个钥匙串,然后向其中添加一个带有预定义可信应用程序列表的项目:

SecKeychainCreate([keychainPath UTF8String], (UInt32)strlen(keychainPass), keychainPass, FALSE, NULL, &someKeychain);
OSStatus someStatus = SecKeychainItemCreateFromContent(kSecGenericPasswordItemClass, &list, len, encryptedPass, someKeychain, accessRef, &someKeychainItem);

当我使用“钥匙串访问”应用程序打开新创建的钥匙串时,我可以在受信任的应用程序列表中看到我的应用程序:

问题是,当我尝试通过受信任的应用程序之一从该钥匙串读取密钥时

SecKeychainUnlock(someKeychain, (UInt32)strlen(keychainPass), keychainPass, TRUE);

UInt32 passwordLen = 0;
void *passData = nil;

const char *cUser_name = [NSUserName() cStringUsingEncoding:NSUTF8StringEncoding];

OSStatus genericPassErr = SecKeychainFindGenericPassword(someKeychain, 0, NULL, strlen(cUser_name), cUser_name, &passwordLen, &passData, NULL);

genericPassErr equals -25293, 意思是Error: 0xFFFF9D33 -25293 The user name or passphrase you entered is not correct.

在代码的前面,我运行SecKeychainSetUserInteractionAllowed(0),如果我注释掉这一行,系统会提示我允许应用程序访问钥匙串,如果我授予它,一切都会正常运行。然而,重点是我需要能够在不提示用户的情况下做到这一点。自从我将应用程序添加到 ACL 后,我希望它能像这样工作。你知道我做错了什么吗?

当我在所附屏幕截图中勾选“所有程序都可以访问此项目”单选框时,一切都会在没有提示的情况下运行。但我不希望每个人都能够访问它,而只是列出的应用程序。


我能够使类似的测试程序发挥作用。但是,每次重建该工具后,我都必须删除该工具并将其重新添加到始终允许的列表中。当不这样做时,我确实得到了相同的错误代码。

这是代码:

#import <Foundation/Foundation.h>
#import <Security/Security.h>

int main()
{
    @autoreleasepool
    {
        SecKeychainRef kc;
        OSStatus status = SecKeychainSetUserInteractionAllowed(false);
        printf("status: %d\n", status);

        status = SecKeychainOpen("/Users/tsnorri/Library/Keychains/test.keychain", &kc);
        printf("status: %d\n", status);

        {
            char const *keychainPass = "test123";
            status = SecKeychainUnlock(kc, (UInt32) strlen(keychainPass), keychainPass, true);
            CFStringRef err = SecCopyErrorMessageString(status, NULL);
            printf("status: %d err: %s\n", status, [(id) err UTF8String]);
            CFRelease(err);
        }

        UInt32 passwordLen = 0;
        void *passData = NULL;

        char const *userName = "tsnorri";
        char const *serviceName = "test";

        {
            SecKeychainItemRef item = NULL;
            status = SecKeychainFindGenericPassword(kc, strlen(serviceName), serviceName, strlen(userName), userName, &passwordLen, &passData, &item);
            CFStringRef err = SecCopyErrorMessageString(status, NULL);
            printf("status: %d err: %s\n", status, [(id) err UTF8String]);
            CFRelease(err);
        }

        printf("pass: %s\n", passData);
    }
    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从受信任的应用程序访问 OS X 钥匙串项目 的相关文章

随机推荐