diff options
Diffstat (limited to 'QSThingsPlugin/Helpers/KeychainHelper.m')
| -rw-r--r-- | QSThingsPlugin/Helpers/KeychainHelper.m | 117 |
1 files changed, 58 insertions, 59 deletions
diff --git a/QSThingsPlugin/Helpers/KeychainHelper.m b/QSThingsPlugin/Helpers/KeychainHelper.m index fcf8291..50a8eac 100644 --- a/QSThingsPlugin/Helpers/KeychainHelper.m +++ b/QSThingsPlugin/Helpers/KeychainHelper.m @@ -5,70 +5,69 @@ NS_ASSUME_NONNULL_BEGIN @implementation KeychainHelper + (NSString *)authenticationKey { - const char *service = [kKeychainService UTF8String]; - const char *account = [kAuthenticationKeyKeychainKey UTF8String]; - - UInt32 authenticationKeyLength = 0; - void *authenticationKeyData = NULL; - - OSStatus status = SecKeychainFindGenericPassword( - NULL, (UInt32)strlen(service), service, (UInt32)strlen(account), account, - &authenticationKeyLength, &authenticationKeyData, NULL); - - if (status == errSecSuccess && authenticationKeyData != NULL) { - NSString *authenticationKey = [[NSString alloc] initWithBytes:authenticationKeyData - length:authenticationKeyLength - encoding:NSUTF8StringEncoding]; - SecKeychainItemFreeContent(NULL, authenticationKeyData); - return authenticationKey; - } - - return nil; + CFDictionaryRef query = (__bridge CFDictionaryRef)@{ + (__bridge NSString *)kSecClass: (__bridge NSString *)kSecClassGenericPassword, + (__bridge NSString *)kSecAttrService: kKeychainService, + (__bridge NSString *)kSecAttrAccount: kAuthenticationKeyKeychainKey, + (__bridge NSString *)kSecReturnData: @YES, + (__bridge NSString *)kSecMatchLimit: (__bridge NSString *)kSecMatchLimitOne + }; + + CFTypeRef result = NULL; + OSStatus status = SecItemCopyMatching(query, &result); + + if (status == errSecSuccess && result != NULL) { + NSData *keyData = (__bridge NSData *)result; + NSString *authenticationKey = [[[NSString alloc] initWithData:keyData + encoding:NSUTF8StringEncoding] autorelease]; + CFRelease(result); + return authenticationKey; + } + + return nil; } + (OSStatus)setAuthenticationKey:(NSString *)authenticationKey { - const char *service = [kKeychainService UTF8String]; - const char *account = [kAuthenticationKeyKeychainKey UTF8String]; - const char *authenticationKeyCString = [authenticationKey UTF8String]; - - // First try to find existing item - SecKeychainItemRef item = NULL; - OSStatus findStatus = SecKeychainFindGenericPassword( - NULL, (UInt32)strlen(service), service, (UInt32)strlen(account), account, - NULL, NULL, &item); - - OSStatus status; - if (findStatus == errSecSuccess) { - // Update existing item - status = SecKeychainItemModifyAttributesAndData( - item, NULL, (UInt32)strlen(authenticationKeyCString), authenticationKeyCString); - CFRelease(item); - } else { - // Create new item - status = SecKeychainAddGenericPassword( - NULL, (UInt32)strlen(service), service, (UInt32)strlen(account), - account, (UInt32)strlen(authenticationKeyCString), authenticationKeyCString, NULL); - } - - return status; + NSData *passwordData = [authenticationKey dataUsingEncoding:NSUTF8StringEncoding]; + + // First try to update existing item + CFDictionaryRef query = (__bridge CFDictionaryRef)@{ + (__bridge NSString *)kSecClass: (__bridge NSString *)kSecClassGenericPassword, + (__bridge NSString *)kSecAttrService: kKeychainService, + (__bridge NSString *)kSecAttrAccount: kAuthenticationKeyKeychainKey + }; + + CFDictionaryRef attributesToUpdate = (__bridge CFDictionaryRef)@{ + (__bridge NSString *)kSecValueData: passwordData + }; + + OSStatus status = SecItemUpdate(query, attributesToUpdate); + + if (status == errSecItemNotFound) { + // Item doesn't exist, create new one + CFDictionaryRef attributes = (__bridge CFDictionaryRef)@{ + (__bridge NSString *)kSecClass: (__bridge NSString *)kSecClassGenericPassword, + (__bridge NSString *)kSecAttrService: kKeychainService, + (__bridge NSString *)kSecAttrAccount: kAuthenticationKeyKeychainKey, + (__bridge NSString *)kSecValueData: passwordData, + (__bridge NSString *)kSecAttrAccessible: (__bridge NSString *)kSecAttrAccessibleWhenUnlockedThisDeviceOnly + }; + + status = SecItemAdd(attributes, NULL); + } + + return status; } -+ (OSStatus) deleteAuthenticationKey { - const char *service = [kKeychainService UTF8String]; - const char *account = [kAuthenticationKeyKeychainKey UTF8String]; - - SecKeychainItemRef item = NULL; - OSStatus findStatus = SecKeychainFindGenericPassword( - NULL, (UInt32)strlen(service), service, (UInt32)strlen(account), account, - NULL, NULL, &item); - - if (findStatus == errSecSuccess) { - OSStatus deleteStatus = SecKeychainItemDelete(item); - CFRelease(item); - return deleteStatus; - } - - return findStatus; ++ (OSStatus)deleteAuthenticationKey { + CFDictionaryRef query = (__bridge CFDictionaryRef)@{ + (__bridge NSString *)kSecClass: (__bridge NSString *)kSecClassGenericPassword, + (__bridge NSString *)kSecAttrService: kKeychainService, + (__bridge NSString *)kSecAttrAccount: kAuthenticationKeyKeychainKey + }; + + OSStatus status = SecItemDelete(query); + return status; } @end |