1

我想使用钥匙串从我的 Mac OSX 应用程序中存储 SMTP 数据。我阅读了 Apple 的 Keychain Services Programming Guide 并编写了这个方法来存储数据:

    - (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

也许有人可以帮助我?

4

1 回答 1

2

不要声明passworda void **,而是声明它 avoid *并传递&password倒数第二个参数。

您可能不需要 SecKeychainItemRef 来完成您要完成的工作。

顺便说一句,您是否尝试过使用钥匙串访问来验证物品是否进入钥匙串?

于 2012-12-10T06:43:48.997 回答