diff --git a/MasterPassword/C/mpw-algorithm.c b/MasterPassword/C/mpw-algorithm.c index 89dbf422..c5620e30 100644 --- a/MasterPassword/C/mpw-algorithm.c +++ b/MasterPassword/C/mpw-algorithm.c @@ -19,6 +19,9 @@ const uint8_t *mpw_masterKeyForUser(const char *fullName, const char *masterPassword, const MPAlgorithmVersion algorithmVersion) { + if (!fullName || !masterPassword) + return NULL; + switch (algorithmVersion) { case MPAlgorithmVersion0: return mpw_masterKeyForUser_v0( fullName, masterPassword ); @@ -37,6 +40,9 @@ const uint8_t *mpw_masterKeyForUser(const char *fullName, const char *masterPass const char *mpw_passwordForSite(const uint8_t *masterKey, const char *siteName, const MPSiteType siteType, const uint32_t siteCounter, const MPSiteVariant siteVariant, const char *siteContext, const MPAlgorithmVersion algorithmVersion) { + if (!masterKey || !siteName) + return NULL; + switch (algorithmVersion) { case MPAlgorithmVersion0: return mpw_passwordForSite_v0( masterKey, siteName, siteType, siteCounter, siteVariant, siteContext ); diff --git a/MasterPassword/ObjC/MPAppDelegate_Key.m b/MasterPassword/ObjC/MPAppDelegate_Key.m index 9fd0af6f..b9903868 100644 --- a/MasterPassword/ObjC/MPAppDelegate_Key.m +++ b/MasterPassword/ObjC/MPAppDelegate_Key.m @@ -50,8 +50,8 @@ static NSDictionary *createKeyQuery(MPUserEntity *user, BOOL newItem, MPKeyOrigi return [PearlKeyChain createQueryForClass:kSecClassGenericPassword attributes:@{ - (__bridge id)kSecAttrService : @"Saved Master Password", - (__bridge id)kSecAttrAccount : user.name?: @"", + (__bridge id)kSecAttrService : @"Saved Master Password", + (__bridge id)kSecAttrAccount : user.name?: @"", #if TARGET_OS_IPHONE (__bridge id)kSecAttrAccessible : (__bridge id)(kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly?: kSecAttrAccessibleWhenUnlockedThisDeviceOnly), #endif @@ -76,13 +76,14 @@ static NSDictionary *createKeyQuery(MPUserEntity *user, BOOL newItem, MPKeyOrigi - (void)storeSavedKeyFor:(MPUserEntity *)user { if (user.saveKey) { - inf( @"Saving key in keychain for user: %@", user.userID ); + NSData *keyData = [self.key keyDataForAlgorithm:user.algorithm]; + if (keyData) { + [self forgetSavedKeyFor:user]; - [self forgetSavedKeyFor:user]; - [PearlKeyChain addOrUpdateItemForQuery:createKeyQuery( user, YES, nil ) - withAttributes:@{ - (__bridge id)kSecValueData : [self.key keyDataForAlgorithm:user.algorithm], - }]; + inf( @"Saving key in keychain for user: %@", user.userID ); + [PearlKeyChain addOrUpdateItemForQuery:createKeyQuery( user, YES, nil ) + withAttributes:@{ (__bridge id)kSecValueData : keyData }]; + } } } diff --git a/MasterPassword/ObjC/MPKey.m b/MasterPassword/ObjC/MPKey.m index b34ef369..b482e5ec 100644 --- a/MasterPassword/ObjC/MPKey.m +++ b/MasterPassword/ObjC/MPKey.m @@ -62,9 +62,12 @@ - (NSData *)keyDataForAlgorithm:(id)algorithm { NSData *keyData = [_keyCache objectForKey:algorithm]; - if (!keyData) - [_keyCache setObject:keyData = [algorithm keyDataForFullName:self.fullName withMasterPassword:self.masterPassword] - forKey:algorithm]; + if (keyData) + return keyData; + + keyData = [algorithm keyDataForFullName:self.fullName withMasterPassword:self.masterPassword]; + if (keyData) + [_keyCache setObject:keyData forKey:algorithm]; return keyData; }