安全钥匙串加载项目



我想使用钥匙串存储来自Mac OSX应用程序的SMTP数据。我阅读了Apple的钥匙串服务编程指南,并编写了以下方法来存储数据:

    - (BOOL)saveSMPTData
{
    OSStatus err;
    SecKeychainItemRef item = nil;
    SecProtocolType protocol = kSecProtocolTypeSMTP;
    const char *accessLabelUTF8 = [KEYCHAIN_NAME UTF8String];
    const char *serverNameUTF8 = [self.serverName UTF8String];
    const char *usernameUTF8 = [self.username UTF8String];
    const char *passwordUTF8 = [self.password UTF8String];
    SecAccessRef access = createAccess(KEYCHAIN_NAME);
    SecKeychainAttribute attrs[] = {
        { kSecLabelItemAttr, (int)strlen(accessLabelUTF8), (char *)accessLabelUTF8 },
        { kSecAccountItemAttr, (int)strlen(usernameUTF8), (char *)usernameUTF8 },
        { kSecServerItemAttr, (int)strlen(serverNameUTF8), (char *)serverNameUTF8 },
        { kSecProtocolItemAttr, sizeof(SecProtocolType), (SecProtocolType *)&protocol }
    };
    SecKeychainAttributeList attributes = { sizeof(attrs) / sizeof(attrs[0]), attrs };
    err = SecKeychainItemCreateFromContent(kSecInternetPasswordItemClass,
                                           &attributes,
                                           (int)strlen(passwordUTF8),
                                           passwordUTF8,
                                           NULL,
                                           access,
                                           &item);
    if (access) CFRelease(access);
    if (item) CFRelease(item);
    return (err == noErr);
}
SecAccessRef createAccess(NSString *accessLabel)
{
    OSStatus err;
    SecAccessRef access = nil;
    NSArray *trustedApplications = nil;
    SecTrustedApplicationRef myself;
    err = SecTrustedApplicationCreateFromPath(NULL, &myself);
    trustedApplications = [NSArray arrayWithObjects:(__bridge id)myself, nil];
    err = SecAccessCreate((__bridge CFStringRef)accessLabel,
                          (__bridge CFArrayRef)trustedApplications, &access);
    if (err) return nil;
    return access;
}

当然,我也想加载它们。我的第一次尝试如下所示:

- (BOOL)loadDataFromKeychain
{
    uint32_t serverNameLength = 0;
    const char *serverName = NULL;
    uint32_t usernameLength = 0;
    const char *username = NULL;
    uint32_t passwordLength = 0;
    void **password = NULL;
    OSStatus err = SecKeychainFindInternetPassword(NULL,
                                                   serverNameLength, serverName,
                                                   0, NULL,
                                                   usernameLength, username,
                                                   0, NULL,
                                                   0, 0,
                                                   0,
                                                   &passwordLength, password,
                                                   NULL); // How do I get the ItemRef?
    return (err == noErr);
}

但这行不通,我想我知道为什么不行。我不知道如何获取 SecKeychainFindInternetPassword 方法的 SecKeychainItemRef

也许有人可以帮助我?

不是声明password void **,而是将其声明为void *并为倒数第二个参数传递&password

您可能不需要 SecKeychainItemRef 来完成您要完成的任务。

顺便问一下,您是否尝试过使用"钥匙串访问"来验证项目是否进入钥匙串

最新更新