Find Internet password from keychain?

I have a internet password stored in my keychain with below details:

Internet Password Item:

Account: user

Server: some Ip address(Let's say w.x.y.z)

Protocol: htpx

But when I use the below code, I receive item not found. But when I remove kSecAttrProtocol attribute from my dictionary, it works. The document says kSecProtocolTypeHTTPProxy corresponds to htpx. Not sure what I am doing wrong, Please guide. I have a dependency on SecProtocolType in my code to look for an internet password in keychain. https://developer.apple.com/documentation/security/secprotocoltype/ksecprotocoltypehttpproxy/

    NSString *account = @"user";
    NSString *server = @"w.x.y.z";
    SecProtocolType protocol = kSecProtocolTypeHTTPProxy;
    
    NSDictionary *query = @{
           (__bridge id)kSecClass: (__bridge id)kSecClassInternetPassword,
           (__bridge id)kSecAttrAccount: account,
           (__bridge id)kSecAttrServer: server,
           (__bridge id)kSecAttrProtocol:@(protocol),
           (__bridge id)kSecReturnAttributes: (__bridge id)kCFBooleanTrue,
           (__bridge id)kSecReturnData: (__bridge id)kCFBooleanFalse,
           (__bridge id)kSecMatchLimit: (__bridge id)kSecMatchLimitOne
       };

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

       if (status == errSecSuccess) {
           NSDictionary *passwordItem = CFBridgingRelease(result);
           NSLog(@"Internet Password Item Found:");
       } else if (status == errSecItemNotFound) {
           NSLog(@"Internet Password Item Not Found");
       } else {
           NSLog(@"Error retrieving Internet password: %d (%@)", (int)status, CFBridgingRelease(SecCopyErrorMessageString(status, NULL)));
       }

Replies

This is on macOS, right?

If you remove the kSecAttrProtocol constraint and look at the returned attributes, what value do you get for kSecAttrProtocol?

ps I have two post that discuss the SecItem API in some detail:

I recommend that you read them through because this API is a tricky one to use correctly.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks @eskimo for the valuable links. I went through them. But as I have mentioned, my query is when I remove kSecAttrProtocol constraint, I get the below output as one the attribute in result ref.

kSecAttrProtocol : htpx

But when I add that in constraint, I recieve item not found.

    NSString *account = @"user";
    NSString *server = @"w.x.y.z";
    SecProtocolType protocol = kSecProtocolTypeHTTPProxy;
    
    NSDictionary *query = @{
           (__bridge id)kSecClass: (__bridge id)kSecClassInternetPassword,
           (__bridge id)kSecAttrAccount: account,
           (__bridge id)kSecAttrServer: server,
           (__bridge id)kSecAttrProtocol:@(protocol),
           (__bridge id)kSecReturnAttributes: (__bridge id)kCFBooleanTrue,
           (__bridge id)kSecReturnData: (__bridge id)kCFBooleanFalse,
           (__bridge id)kSecMatchLimit: (__bridge id)kSecMatchLimitOne
       };

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

       if (status == errSecSuccess) {
           NSDictionary *passwordItem = CFBridgingRelease(result);
           NSLog(@"Internet Password Item Found:");
       } else if (status == errSecItemNotFound) {
           NSLog(@"Internet Password Item Not Found");
       } else {
           NSLog(@"Error retrieving Internet password: %d (%@)", (int)status, CFBridgingRelease(SecCopyErrorMessageString(status, NULL)));
       }

I get the below output as one the attribute in result ref.

kSecAttrProtocol : htpx

What type is that attribute? If you get the value and use type(of:) to print the type, what do you get?

Also, what platform is this code running on?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"