Search code examples
objective-cmacoswifikeychain

Getting Airport network password using SecKeychainFindGenericPassword


I'm trying to get Airport network password using This api SecKeychainFindGenericPassword(). But I always get itemnotfound error . I'm not sure what to pass in Account name and service name in the API . I added code snippet to show what I m doing. Any help would be appreciated . Thanks

 OSStatus status1 ;

SecKeychainRef kychain = nil;
SecKeychainCopyDefault(&kychain);
status1 = SecKeychainFindGenericPassword (
                                          kychain,           // default keychain
                                          15,             // length of service name
                                          "AirPort Network",   // service name
                                          38,             // length of account name
                                          "com.apple.network.wlan.ssid.xxxxxxxx",   // account name
                                          passwordLength,  // length of password
                                          &passwordData,   // pointer to password data
                                          itemRef          // the item reference
                                          );
return (status1);

I'm using osx 10.8


Solution

  • You have the service and account swapped. The service should be the SSID, and the account name should be "AirPort".

    SecKeychainRef keychain;
    OSStatus err = SecKeychainOpen("/Library/Keychains/System.keychain", &keychain);
    
    #define kServiceName "com.apple.network.wlan.ssid.Rackus"
    #define kAccountName "AirPort"
    
    UInt32 passwordLength = 0;
    char* passwordData = nil;
    UInt32 serviceNameLength = strlen(kServiceName);
    UInt32 accountNameLength = strlen(kAccountName);
    SecKeychainItemRef itemRef;
    
    err = SecKeychainFindGenericPassword(keychain, serviceNameLength, kServiceName, accountNameLength, kAccountName, &passwordLength, (void**)&passwordData, &itemRef);