2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2012-01-05 00:44:15 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2012-01-05 00:44:15 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2012-01-05 00:44:15 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// You can find a copy of the GNU General Public License in the
|
|
|
|
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
|
|
|
|
//==============================================================================
|
2012-01-05 00:44:15 +00:00
|
|
|
|
2015-09-23 04:31:33 +00:00
|
|
|
#ifndef trc
|
|
|
|
#error error
|
|
|
|
#endif
|
2012-07-17 20:57:11 +00:00
|
|
|
#import "MPAlgorithmV0.h"
|
2012-06-10 06:21:41 +00:00
|
|
|
#import "MPEntities.h"
|
2014-09-17 05:34:58 +00:00
|
|
|
#import "MPAppDelegate_Shared.h"
|
2014-09-21 14:29:18 +00:00
|
|
|
#import "MPAppDelegate_InApp.h"
|
2015-01-15 22:43:41 +00:00
|
|
|
#import "mpw-util.h"
|
2012-05-12 16:31:05 +00:00
|
|
|
|
2014-07-28 22:53:50 +00:00
|
|
|
/* An AMD HD 7970 calculates 2495M SHA-1 hashes per second at a cost of ~350$ per GPU */
|
|
|
|
#define CRACKING_PER_SECOND 2495000000UL
|
|
|
|
#define CRACKING_PRICE 350
|
2014-07-26 05:26:33 +00:00
|
|
|
|
2015-03-11 21:30:34 +00:00
|
|
|
NSOperationQueue *_mpwQueue = nil;
|
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
@implementation MPAlgorithmV0
|
2014-07-26 05:26:33 +00:00
|
|
|
|
|
|
|
- (id)init {
|
|
|
|
|
|
|
|
if (!(self = [super init]))
|
|
|
|
return nil;
|
|
|
|
|
2015-03-11 21:30:34 +00:00
|
|
|
static dispatch_once_t once = 0;
|
|
|
|
dispatch_once( &once, ^{
|
|
|
|
_mpwQueue = [NSOperationQueue new];
|
|
|
|
_mpwQueue.maxConcurrentOperationCount = 1;
|
|
|
|
_mpwQueue.name = @"mpw queue";
|
|
|
|
} );
|
2014-07-26 05:26:33 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-01-15 22:43:41 +00:00
|
|
|
- (MPAlgorithmVersion)version {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2015-01-15 22:43:41 +00:00
|
|
|
return MPAlgorithmVersion0;
|
2012-02-13 09:27:08 +00:00
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
- (NSString *)description {
|
|
|
|
|
2015-09-23 04:31:33 +00:00
|
|
|
return strf( @"V%lu", (unsigned long)self.version );
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)debugDescription {
|
|
|
|
|
2014-04-25 02:00:38 +00:00
|
|
|
return strf( @"<%@: version=%lu>", NSStringFromClass( [self class] ), (unsigned long)self.version );
|
2014-04-07 03:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isEqual:(id)other {
|
|
|
|
|
|
|
|
if (other == self)
|
|
|
|
return YES;
|
|
|
|
if (!other || ![other conformsToProtocol:@protocol(MPAlgorithm)])
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
return [(id<MPAlgorithm>)other version] == [self version];
|
|
|
|
}
|
|
|
|
|
2015-03-11 21:30:34 +00:00
|
|
|
- (void)mpw_perform:(void ( ^ )(void))operationBlock {
|
|
|
|
|
|
|
|
if ([NSOperationQueue currentQueue] == _mpwQueue) {
|
|
|
|
operationBlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSOperation *operation = [NSBlockOperation blockOperationWithBlock:operationBlock];
|
|
|
|
if ([operation respondsToSelector:@selector( qualityOfService )])
|
|
|
|
operation.qualityOfService = NSQualityOfServiceUserInitiated;
|
|
|
|
[_mpwQueue addOperations:@[ operation ] waitUntilFinished:YES];
|
|
|
|
}
|
|
|
|
|
2014-09-24 11:58:23 +00:00
|
|
|
- (BOOL)tryMigrateUser:(MPUserEntity *)user inContext:(NSManagedObjectContext *)moc {
|
2013-04-20 18:11:19 +00:00
|
|
|
|
|
|
|
NSError *error = nil;
|
2014-09-21 14:39:09 +00:00
|
|
|
NSFetchRequest *migrationRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass( [MPSiteEntity class] )];
|
2013-04-13 17:40:17 +00:00
|
|
|
migrationRequest.predicate = [NSPredicate predicateWithFormat:@"version_ < %d AND user == %@", self.version, user];
|
2014-09-21 15:47:53 +00:00
|
|
|
NSArray *migrationSites = [moc executeFetchRequest:migrationRequest error:&error];
|
|
|
|
if (!migrationSites) {
|
2014-09-22 03:28:50 +00:00
|
|
|
err( @"While looking for sites to migrate: %@", [error fullDescription] );
|
2013-01-31 05:42:32 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2014-09-24 11:58:23 +00:00
|
|
|
BOOL success = YES;
|
2014-09-21 15:47:53 +00:00
|
|
|
for (MPSiteEntity *migrationSite in migrationSites)
|
2014-09-24 11:58:23 +00:00
|
|
|
if (![migrationSite tryMigrateExplicitly:NO])
|
|
|
|
success = NO;
|
2013-01-31 05:42:32 +00:00
|
|
|
|
2014-09-24 11:58:23 +00:00
|
|
|
return success;
|
2013-01-17 05:37:20 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 11:58:23 +00:00
|
|
|
- (BOOL)tryMigrateSite:(MPSiteEntity *)site explicit:(BOOL)explicit {
|
2012-06-22 14:52:33 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
if ([site.algorithm version] != [self version] - 1)
|
2014-04-26 18:03:44 +00:00
|
|
|
// Only migrate from previous version.
|
2012-07-17 20:57:11 +00:00
|
|
|
return NO;
|
2012-06-22 14:52:33 +00:00
|
|
|
|
2012-07-17 20:57:11 +00:00
|
|
|
if (!explicit) {
|
|
|
|
// This migration requires explicit permission.
|
2014-09-21 15:47:53 +00:00
|
|
|
site.requiresExplicitMigration = YES;
|
2012-07-17 20:57:11 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply migration.
|
2014-09-21 15:47:53 +00:00
|
|
|
site.requiresExplicitMigration = NO;
|
2015-02-28 15:01:41 +00:00
|
|
|
site.algorithm = self;
|
2012-07-17 20:57:11 +00:00
|
|
|
return YES;
|
2012-06-22 14:52:33 +00:00
|
|
|
}
|
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
- (NSData *)keyDataForFullName:(NSString *)fullName withMasterPassword:(NSString *)masterPassword {
|
2012-07-17 20:57:11 +00:00
|
|
|
|
2015-03-11 21:30:34 +00:00
|
|
|
__block NSData *keyData;
|
|
|
|
[self mpw_perform:^{
|
|
|
|
NSDate *start = [NSDate date];
|
|
|
|
uint8_t const *masterKeyBytes = mpw_masterKeyForUser( fullName.UTF8String, masterPassword.UTF8String, [self version] );
|
2016-07-19 16:00:19 +00:00
|
|
|
if (masterKeyBytes) {
|
|
|
|
keyData = [NSData dataWithBytes:masterKeyBytes length:MP_dkLen];
|
|
|
|
trc( @"User: %@, password: %@ derives to key ID: %@ (took %0.2fs)", //
|
|
|
|
fullName, masterPassword, [self keyIDForKeyData:keyData], -[start timeIntervalSinceNow] );
|
|
|
|
mpw_free( masterKeyBytes, MP_dkLen );
|
|
|
|
}
|
2015-03-11 21:30:34 +00:00
|
|
|
}];
|
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
return keyData;
|
2012-02-13 09:27:08 +00:00
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-07-17 20:57:11 +00:00
|
|
|
- (NSData *)keyIDForKeyData:(NSData *)keyData {
|
|
|
|
|
2015-01-15 22:43:41 +00:00
|
|
|
return [keyData hashWith:PearlHashSHA256];
|
2014-09-15 05:00:23 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (NSString *)nameOfType:(MPSiteType)type {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-01-16 08:51:08 +00:00
|
|
|
if (!type)
|
|
|
|
return nil;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
switch (type) {
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedMaximum:
|
2012-06-11 14:15:49 +00:00
|
|
|
return @"Maximum Security Password";
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedLong:
|
2012-01-24 23:30:43 +00:00
|
|
|
return @"Long Password";
|
2012-06-06 20:38:43 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedMedium:
|
2012-01-24 23:30:43 +00:00
|
|
|
return @"Medium Password";
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedBasic:
|
2012-01-24 23:30:43 +00:00
|
|
|
return @"Basic Password";
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedShort:
|
2013-02-08 23:11:24 +00:00
|
|
|
return @"Short Password";
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedPIN:
|
2012-01-05 00:44:15 +00:00
|
|
|
return @"PIN";
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedName:
|
2016-04-23 15:54:07 +00:00
|
|
|
return @"Name";
|
2014-09-15 05:00:23 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedPhrase:
|
|
|
|
return @"Phrase";
|
|
|
|
|
|
|
|
case MPSiteTypeStoredPersonal:
|
2012-01-24 23:30:43 +00:00
|
|
|
return @"Personal Password";
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredDevicePrivate:
|
2012-01-24 23:30:43 +00:00
|
|
|
return @"Device Private Password";
|
2012-01-05 00:44:15 +00:00
|
|
|
}
|
2012-07-17 20:57:11 +00:00
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
Throw( @"Type not supported: %lu", (long)type );
|
2012-01-05 00:44:15 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (NSString *)shortNameOfType:(MPSiteType)type {
|
2012-06-11 14:15:49 +00:00
|
|
|
|
|
|
|
if (!type)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
switch (type) {
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedMaximum:
|
2012-06-11 14:15:49 +00:00
|
|
|
return @"Maximum";
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedLong:
|
2012-06-11 14:15:49 +00:00
|
|
|
return @"Long";
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedMedium:
|
2012-06-11 14:15:49 +00:00
|
|
|
return @"Medium";
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedBasic:
|
2012-06-11 14:15:49 +00:00
|
|
|
return @"Basic";
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedShort:
|
2013-02-08 23:11:24 +00:00
|
|
|
return @"Short";
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedPIN:
|
2012-06-11 14:15:49 +00:00
|
|
|
return @"PIN";
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedName:
|
2014-09-15 05:00:23 +00:00
|
|
|
return @"Name";
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedPhrase:
|
|
|
|
return @"Phrase";
|
|
|
|
|
|
|
|
case MPSiteTypeStoredPersonal:
|
2012-06-11 14:15:49 +00:00
|
|
|
return @"Personal";
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredDevicePrivate:
|
2012-06-11 14:15:49 +00:00
|
|
|
return @"Device";
|
|
|
|
}
|
2012-07-17 20:57:11 +00:00
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
Throw( @"Type not supported: %lu", (long)type );
|
2012-07-17 20:57:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (NSString *)classNameOfType:(MPSiteType)type {
|
2012-07-17 20:57:11 +00:00
|
|
|
|
2013-04-20 18:11:19 +00:00
|
|
|
return NSStringFromClass( [self classOfType:type] );
|
2012-06-11 14:15:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (Class)classOfType:(MPSiteType)type {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-01-19 16:40:39 +00:00
|
|
|
if (!type)
|
2014-04-26 18:03:44 +00:00
|
|
|
Throw( @"No type given." );
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-01-19 16:40:39 +00:00
|
|
|
switch (type) {
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedMaximum:
|
2014-09-21 15:47:53 +00:00
|
|
|
return [MPGeneratedSiteEntity class];
|
2014-09-21 14:39:09 +00:00
|
|
|
|
|
|
|
case MPSiteTypeGeneratedLong:
|
2014-09-21 15:47:53 +00:00
|
|
|
return [MPGeneratedSiteEntity class];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedMedium:
|
2014-09-21 15:47:53 +00:00
|
|
|
return [MPGeneratedSiteEntity class];
|
2012-06-06 20:38:43 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedBasic:
|
2014-09-21 15:47:53 +00:00
|
|
|
return [MPGeneratedSiteEntity class];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedShort:
|
2014-09-21 15:47:53 +00:00
|
|
|
return [MPGeneratedSiteEntity class];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedPIN:
|
2014-09-21 15:47:53 +00:00
|
|
|
return [MPGeneratedSiteEntity class];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedName:
|
2014-09-21 15:47:53 +00:00
|
|
|
return [MPGeneratedSiteEntity class];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedPhrase:
|
2014-09-21 15:47:53 +00:00
|
|
|
return [MPGeneratedSiteEntity class];
|
2014-09-15 05:00:23 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredPersonal:
|
2014-09-21 15:47:53 +00:00
|
|
|
return [MPStoredSiteEntity class];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredDevicePrivate:
|
2014-09-21 15:47:53 +00:00
|
|
|
return [MPStoredSiteEntity class];
|
2012-01-19 16:40:39 +00:00
|
|
|
}
|
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
Throw( @"Type not supported: %lu", (long)type );
|
2014-02-19 05:59:01 +00:00
|
|
|
}
|
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
- (NSArray *)allTypes {
|
2014-02-19 05:59:01 +00:00
|
|
|
|
2016-04-23 15:54:07 +00:00
|
|
|
return [self allTypesStartingWith:MPSiteTypeGeneratedPhrase];
|
2014-04-26 18:03:44 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (NSArray *)allTypesStartingWith:(MPSiteType)startingType {
|
2014-04-26 18:03:44 +00:00
|
|
|
|
|
|
|
NSMutableArray *allTypes = [[NSMutableArray alloc] initWithCapacity:8];
|
2014-09-21 14:39:09 +00:00
|
|
|
MPSiteType currentType = startingType;
|
2014-04-26 18:03:44 +00:00
|
|
|
do {
|
|
|
|
[allTypes addObject:@(currentType)];
|
|
|
|
} while ((currentType = [self nextType:currentType]) != startingType);
|
|
|
|
|
|
|
|
return allTypes;
|
|
|
|
}
|
|
|
|
|
2017-04-20 01:58:10 +00:00
|
|
|
- (MPSiteType)defaultType {
|
|
|
|
|
|
|
|
return MPSiteTypeGeneratedLong;
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (MPSiteType)nextType:(MPSiteType)type {
|
2014-02-19 05:59:01 +00:00
|
|
|
|
|
|
|
switch (type) {
|
2016-04-23 15:54:07 +00:00
|
|
|
case MPSiteTypeGeneratedPhrase:
|
|
|
|
return MPSiteTypeGeneratedName;
|
|
|
|
case MPSiteTypeGeneratedName:
|
|
|
|
return MPSiteTypeGeneratedMaximum;
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedMaximum:
|
|
|
|
return MPSiteTypeGeneratedLong;
|
|
|
|
case MPSiteTypeGeneratedLong:
|
|
|
|
return MPSiteTypeGeneratedMedium;
|
|
|
|
case MPSiteTypeGeneratedMedium:
|
|
|
|
return MPSiteTypeGeneratedBasic;
|
|
|
|
case MPSiteTypeGeneratedBasic:
|
|
|
|
return MPSiteTypeGeneratedShort;
|
|
|
|
case MPSiteTypeGeneratedShort:
|
|
|
|
return MPSiteTypeGeneratedPIN;
|
|
|
|
case MPSiteTypeGeneratedPIN:
|
|
|
|
return MPSiteTypeStoredPersonal;
|
|
|
|
case MPSiteTypeStoredPersonal:
|
|
|
|
return MPSiteTypeStoredDevicePrivate;
|
|
|
|
case MPSiteTypeStoredDevicePrivate:
|
2016-04-23 15:54:07 +00:00
|
|
|
return MPSiteTypeGeneratedPhrase;
|
2014-02-19 05:59:01 +00:00
|
|
|
}
|
2017-04-20 01:58:10 +00:00
|
|
|
|
|
|
|
return [self defaultType];
|
2014-02-19 05:59:01 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (MPSiteType)previousType:(MPSiteType)type {
|
2014-02-19 05:59:01 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
MPSiteType previousType = type, nextType = type;
|
2014-02-19 05:59:01 +00:00
|
|
|
while ((nextType = [self nextType:nextType]) != type)
|
|
|
|
previousType = nextType;
|
|
|
|
|
|
|
|
return previousType;
|
2012-01-24 23:30:43 +00:00
|
|
|
}
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
- (NSString *)generateLoginForSiteNamed:(NSString *)name usingKey:(MPKey *)key {
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
return [self generateContentForSiteNamed:name ofType:MPSiteTypeGeneratedName withCounter:1
|
2014-09-24 11:58:23 +00:00
|
|
|
variant:MPSiteVariantLogin context:nil usingKey:key];
|
2014-09-15 05:00:23 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (NSString *)generatePasswordForSiteNamed:(NSString *)name ofType:(MPSiteType)type withCounter:(NSUInteger)counter
|
2014-09-24 11:58:23 +00:00
|
|
|
usingKey:(MPKey *)key {
|
2014-09-15 05:00:23 +00:00
|
|
|
|
|
|
|
return [self generateContentForSiteNamed:name ofType:type withCounter:counter
|
2014-09-22 02:45:21 +00:00
|
|
|
variant:MPSiteVariantPassword context:nil usingKey:key];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)generateAnswerForSiteNamed:(NSString *)name onQuestion:(NSString *)question usingKey:(MPKey *)key {
|
|
|
|
|
|
|
|
return [self generateContentForSiteNamed:name ofType:MPSiteTypeGeneratedPhrase withCounter:1
|
|
|
|
variant:MPSiteVariantAnswer context:question usingKey:key];
|
2014-09-15 05:00:23 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (NSString *)generateContentForSiteNamed:(NSString *)name ofType:(MPSiteType)type withCounter:(NSUInteger)counter
|
2014-09-24 11:58:23 +00:00
|
|
|
variant:(MPSiteVariant)variant context:(NSString *)context usingKey:(MPKey *)key {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2015-03-11 21:30:34 +00:00
|
|
|
__block NSString *content;
|
|
|
|
[self mpw_perform:^{
|
|
|
|
char const *contentBytes = mpw_passwordForSite( [key keyDataForAlgorithm:self].bytes,
|
|
|
|
name.UTF8String, type, (uint32_t)counter, variant, context.UTF8String, [self version] );
|
2016-07-19 16:00:19 +00:00
|
|
|
if (contentBytes) {
|
|
|
|
content = [NSString stringWithCString:contentBytes encoding:NSUTF8StringEncoding];
|
2016-11-04 00:13:43 +00:00
|
|
|
mpw_free_string( contentBytes );
|
2016-07-19 16:00:19 +00:00
|
|
|
}
|
2015-03-11 21:30:34 +00:00
|
|
|
}];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-01-05 00:44:15 +00:00
|
|
|
return content;
|
2012-01-30 00:02:45 +00:00
|
|
|
}
|
2012-07-12 06:41:18 +00:00
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
- (NSString *)storedLoginForSite:(MPStoredSiteEntity *)site usingKey:(MPKey *)key {
|
2014-09-15 05:00:23 +00:00
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
- (NSString *)storedPasswordForSite:(MPStoredSiteEntity *)site usingKey:(MPKey *)key {
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
return [self decryptContent:site.contentObject usingKey:key];
|
2013-09-13 12:14:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
- (BOOL)savePassword:(NSString *)clearContent toSite:(MPSiteEntity *)site usingKey:(MPKey *)siteKey {
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
NSAssert( [[siteKey keyIDForAlgorithm:site.user.algorithm] isEqualToData:site.user.keyID], @"Site does not belong to current user." );
|
2014-09-21 15:47:53 +00:00
|
|
|
switch (site.type) {
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedMaximum:
|
|
|
|
case MPSiteTypeGeneratedLong:
|
|
|
|
case MPSiteTypeGeneratedMedium:
|
|
|
|
case MPSiteTypeGeneratedBasic:
|
|
|
|
case MPSiteTypeGeneratedShort:
|
|
|
|
case MPSiteTypeGeneratedPIN:
|
|
|
|
case MPSiteTypeGeneratedName:
|
|
|
|
case MPSiteTypeGeneratedPhrase: {
|
2014-09-21 15:47:53 +00:00
|
|
|
wrn( @"Cannot save content to site with generated type %lu.", (long)site.type );
|
2014-09-08 04:15:18 +00:00
|
|
|
return NO;
|
2013-09-13 12:14:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredPersonal: {
|
2014-09-21 15:47:53 +00:00
|
|
|
if (![site isKindOfClass:[MPStoredSiteEntity class]]) {
|
|
|
|
wrn( @"Site with stored type %lu is not an MPStoredSiteEntity, but a %@.",
|
|
|
|
(long)site.type, [site class] );
|
2014-09-08 04:15:18 +00:00
|
|
|
return NO;
|
2014-07-28 22:53:50 +00:00
|
|
|
}
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
NSData *encryptionKey = [siteKey keyDataForAlgorithm:self trimmedLength:PearlCryptKeySize];
|
2013-09-13 12:14:58 +00:00
|
|
|
NSData *encryptedContent = [[clearContent dataUsingEncoding:NSUTF8StringEncoding]
|
2015-02-28 15:01:41 +00:00
|
|
|
encryptWithSymmetricKey:encryptionKey padding:YES];
|
2014-09-21 15:47:53 +00:00
|
|
|
if ([((MPStoredSiteEntity *)site).contentObject isEqualToData:encryptedContent])
|
2014-09-08 04:15:18 +00:00
|
|
|
return NO;
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
((MPStoredSiteEntity *)site).contentObject = encryptedContent;
|
2014-09-08 04:15:18 +00:00
|
|
|
return YES;
|
2013-09-13 12:14:58 +00:00
|
|
|
}
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredDevicePrivate: {
|
2014-09-21 15:47:53 +00:00
|
|
|
if (![site isKindOfClass:[MPStoredSiteEntity class]]) {
|
|
|
|
wrn( @"Site with stored type %lu is not an MPStoredSiteEntity, but a %@.",
|
|
|
|
(long)site.type, [site class] );
|
2014-09-08 04:15:18 +00:00
|
|
|
return NO;
|
2014-07-28 22:53:50 +00:00
|
|
|
}
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
NSData *encryptionKey = [siteKey keyDataForAlgorithm:self trimmedLength:PearlCryptKeySize];
|
2013-09-13 12:14:58 +00:00
|
|
|
NSData *encryptedContent = [[clearContent dataUsingEncoding:NSUTF8StringEncoding]
|
2015-02-28 15:01:41 +00:00
|
|
|
encryptWithSymmetricKey:encryptionKey padding:YES];
|
2014-09-21 15:47:53 +00:00
|
|
|
NSDictionary *siteQuery = [self queryForDevicePrivateSiteNamed:site.name];
|
2013-09-13 12:14:58 +00:00
|
|
|
if (!encryptedContent)
|
2014-09-21 15:47:53 +00:00
|
|
|
[PearlKeyChain deleteItemForQuery:siteQuery];
|
2013-09-13 12:14:58 +00:00
|
|
|
else
|
2014-09-21 15:47:53 +00:00
|
|
|
[PearlKeyChain addOrUpdateItemForQuery:siteQuery withAttributes:@{
|
2017-04-10 15:43:55 +00:00
|
|
|
(__bridge id)kSecValueData : encryptedContent,
|
2013-09-13 12:14:58 +00:00
|
|
|
#if TARGET_OS_IPHONE
|
2017-04-10 15:43:55 +00:00
|
|
|
(__bridge id)kSecAttrAccessible: (__bridge id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
|
2013-09-13 12:14:58 +00:00
|
|
|
#endif
|
|
|
|
}];
|
2014-09-21 15:47:53 +00:00
|
|
|
((MPStoredSiteEntity *)site).contentObject = nil;
|
2014-09-08 04:15:18 +00:00
|
|
|
return YES;
|
2013-09-13 12:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-08 04:15:18 +00:00
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
Throw( @"Unsupported type: %ld", (long)site.type );
|
2013-09-13 12:14:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
- (NSString *)resolveLoginForSite:(MPSiteEntity *)site usingKey:(MPKey *)siteKey {
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2017-04-15 06:28:11 +00:00
|
|
|
return PearlAwait( ^(void (^setResult)(id)) {
|
|
|
|
[self resolveLoginForSite:site usingKey:siteKey result:^(NSString *result_) {
|
|
|
|
setResult( result_ );
|
|
|
|
}];
|
|
|
|
} );
|
2013-09-13 12:14:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
- (NSString *)resolvePasswordForSite:(MPSiteEntity *)site usingKey:(MPKey *)siteKey {
|
2014-09-15 05:00:23 +00:00
|
|
|
|
2017-04-15 06:28:11 +00:00
|
|
|
return PearlAwait( ^(void (^setResult)(id)) {
|
|
|
|
[self resolvePasswordForSite:site usingKey:siteKey result:^(NSString *result_) {
|
|
|
|
setResult( result_ );
|
|
|
|
}];
|
|
|
|
} );
|
2014-09-15 05:00:23 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 02:45:21 +00:00
|
|
|
- (NSString *)resolveAnswerForSite:(MPSiteEntity *)site usingKey:(MPKey *)siteKey {
|
|
|
|
|
2017-04-15 06:28:11 +00:00
|
|
|
return PearlAwait( ^(void (^setResult)(id)) {
|
|
|
|
[self resolveAnswerForSite:site usingKey:siteKey result:^(NSString *result_) {
|
|
|
|
setResult( result_ );
|
|
|
|
}];
|
|
|
|
} );
|
2014-09-22 02:45:21 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 20:30:17 +00:00
|
|
|
- (NSString *)resolveAnswerForQuestion:(MPSiteQuestionEntity *)question usingKey:(MPKey *)siteKey {
|
2014-09-22 02:45:21 +00:00
|
|
|
|
2017-04-15 06:28:11 +00:00
|
|
|
return PearlAwait( ^(void (^setResult)(id)) {
|
|
|
|
[self resolveAnswerForQuestion:question usingKey:siteKey result:^(NSString *result_) {
|
|
|
|
setResult( result_ );
|
|
|
|
}];
|
|
|
|
} );
|
2014-09-22 02:45:21 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
- (void)resolveLoginForSite:(MPSiteEntity *)site usingKey:(MPKey *)siteKey result:(void ( ^ )(NSString *result))resultBlock {
|
2014-09-15 05:00:23 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
NSAssert( [[siteKey keyIDForAlgorithm:site.user.algorithm] isEqualToData:site.user.keyID], @"Site does not belong to current user." );
|
2014-09-21 15:47:53 +00:00
|
|
|
NSString *name = site.name;
|
2014-09-26 04:32:07 +00:00
|
|
|
BOOL loginGenerated = site.loginGenerated && [[MPAppDelegate_Shared get] isFeatureUnlocked:MPProductGenerateLogins];
|
2016-05-16 04:50:17 +00:00
|
|
|
NSString *loginName = site.loginName;
|
2014-09-15 05:00:23 +00:00
|
|
|
id<MPAlgorithm> algorithm = nil;
|
|
|
|
if (!name.length)
|
|
|
|
err( @"Missing name." );
|
2015-02-28 15:01:41 +00:00
|
|
|
else if (!siteKey)
|
2014-09-15 05:00:23 +00:00
|
|
|
err( @"Missing key." );
|
|
|
|
else
|
2014-09-21 15:47:53 +00:00
|
|
|
algorithm = site.algorithm;
|
2014-09-15 05:00:23 +00:00
|
|
|
|
2017-04-15 14:57:52 +00:00
|
|
|
if (!loginGenerated || [loginName length])
|
|
|
|
resultBlock( loginName );
|
|
|
|
else
|
|
|
|
PearlNotMainQueue( ^{
|
|
|
|
resultBlock( [algorithm generateLoginForSiteNamed:name usingKey:siteKey] );
|
|
|
|
} );
|
2014-09-15 05:00:23 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
- (void)resolvePasswordForSite:(MPSiteEntity *)site usingKey:(MPKey *)siteKey result:(void ( ^ )(NSString *result))resultBlock {
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
NSAssert( [[siteKey keyIDForAlgorithm:site.user.algorithm] isEqualToData:site.user.keyID], @"Site does not belong to current user." );
|
2014-09-21 15:47:53 +00:00
|
|
|
switch (site.type) {
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedMaximum:
|
|
|
|
case MPSiteTypeGeneratedLong:
|
|
|
|
case MPSiteTypeGeneratedMedium:
|
|
|
|
case MPSiteTypeGeneratedBasic:
|
|
|
|
case MPSiteTypeGeneratedShort:
|
|
|
|
case MPSiteTypeGeneratedPIN:
|
|
|
|
case MPSiteTypeGeneratedName:
|
|
|
|
case MPSiteTypeGeneratedPhrase: {
|
2014-09-21 15:47:53 +00:00
|
|
|
if (![site isKindOfClass:[MPGeneratedSiteEntity class]]) {
|
|
|
|
wrn( @"Site with generated type %lu is not an MPGeneratedSiteEntity, but a %@.",
|
|
|
|
(long)site.type, [site class] );
|
2014-07-28 22:53:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
NSString *name = site.name;
|
|
|
|
MPSiteType type = site.type;
|
|
|
|
NSUInteger counter = ((MPGeneratedSiteEntity *)site).counter;
|
2013-09-13 12:14:58 +00:00
|
|
|
id<MPAlgorithm> algorithm = nil;
|
2014-09-21 15:47:53 +00:00
|
|
|
if (!site.name.length)
|
2014-04-26 18:03:44 +00:00
|
|
|
err( @"Missing name." );
|
2015-02-28 15:01:41 +00:00
|
|
|
else if (!siteKey)
|
2014-04-26 18:03:44 +00:00
|
|
|
err( @"Missing key." );
|
2013-09-13 12:14:58 +00:00
|
|
|
else
|
2014-09-21 15:47:53 +00:00
|
|
|
algorithm = site.algorithm;
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2017-04-15 14:57:52 +00:00
|
|
|
PearlNotMainQueue( ^{
|
|
|
|
resultBlock( [algorithm generatePasswordForSiteNamed:name ofType:type withCounter:counter usingKey:siteKey] );
|
2013-09-13 12:14:58 +00:00
|
|
|
} );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredPersonal: {
|
2014-09-21 15:47:53 +00:00
|
|
|
if (![site isKindOfClass:[MPStoredSiteEntity class]]) {
|
|
|
|
wrn( @"Site with stored type %lu is not an MPStoredSiteEntity, but a %@.",
|
|
|
|
(long)site.type, [site class] );
|
2014-07-28 22:53:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
NSData *encryptedContent = ((MPStoredSiteEntity *)site).contentObject;
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2017-04-15 14:57:52 +00:00
|
|
|
PearlNotMainQueue( ^{
|
|
|
|
resultBlock( [self decryptContent:encryptedContent usingKey:siteKey] );
|
2013-09-13 12:14:58 +00:00
|
|
|
} );
|
|
|
|
break;
|
|
|
|
}
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredDevicePrivate: {
|
2014-09-21 15:47:53 +00:00
|
|
|
NSAssert( [site isKindOfClass:[MPStoredSiteEntity class]],
|
|
|
|
@"Site with stored type %lu is not an MPStoredSiteEntity, but a %@.", (long)site.type,
|
|
|
|
[site class] );
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
NSDictionary *siteQuery = [self queryForDevicePrivateSiteNamed:site.name];
|
|
|
|
NSData *encryptedContent = [PearlKeyChain dataOfItemForQuery:siteQuery];
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2017-04-15 14:57:52 +00:00
|
|
|
PearlNotMainQueue( ^{
|
|
|
|
resultBlock( [self decryptContent:encryptedContent usingKey:siteKey] );
|
2013-09-13 12:14:58 +00:00
|
|
|
} );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 02:45:21 +00:00
|
|
|
- (void)resolveAnswerForSite:(MPSiteEntity *)site usingKey:(MPKey *)siteKey result:(void ( ^ )(NSString *result))resultBlock {
|
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
NSAssert( [[siteKey keyIDForAlgorithm:site.user.algorithm] isEqualToData:site.user.keyID], @"Site does not belong to current user." );
|
2014-09-22 02:45:21 +00:00
|
|
|
NSString *name = site.name;
|
|
|
|
id<MPAlgorithm> algorithm = nil;
|
|
|
|
if (!site.name.length)
|
|
|
|
err( @"Missing name." );
|
2015-02-28 15:01:41 +00:00
|
|
|
else if (!siteKey)
|
2014-09-22 02:45:21 +00:00
|
|
|
err( @"Missing key." );
|
|
|
|
else
|
|
|
|
algorithm = site.algorithm;
|
|
|
|
|
2017-04-15 14:57:52 +00:00
|
|
|
PearlNotMainQueue( ^{
|
|
|
|
resultBlock( [algorithm generateAnswerForSiteNamed:name onQuestion:nil usingKey:siteKey] );
|
2014-09-22 02:45:21 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2014-09-27 20:30:17 +00:00
|
|
|
- (void)resolveAnswerForQuestion:(MPSiteQuestionEntity *)question usingKey:(MPKey *)siteKey
|
2014-09-22 02:45:21 +00:00
|
|
|
result:(void ( ^ )(NSString *result))resultBlock {
|
|
|
|
|
2015-03-11 21:30:34 +00:00
|
|
|
NSAssert( [[siteKey keyIDForAlgorithm:question.site.user.algorithm] isEqualToData:question.site.user.keyID],
|
|
|
|
@"Site does not belong to current user." );
|
2014-09-27 20:30:17 +00:00
|
|
|
NSString *name = question.site.name;
|
2014-09-22 02:45:21 +00:00
|
|
|
NSString *keyword = question.keyword;
|
|
|
|
id<MPAlgorithm> algorithm = nil;
|
2014-09-27 20:30:17 +00:00
|
|
|
if (!name.length)
|
2014-09-22 02:45:21 +00:00
|
|
|
err( @"Missing name." );
|
2015-02-28 15:01:41 +00:00
|
|
|
else if (!siteKey)
|
2014-09-22 02:45:21 +00:00
|
|
|
err( @"Missing key." );
|
2016-05-16 04:50:17 +00:00
|
|
|
else if ([[MPAppDelegate_Shared get] isFeatureUnlocked:MPProductGenerateAnswers])
|
2014-09-27 20:30:17 +00:00
|
|
|
algorithm = question.site.algorithm;
|
2014-09-22 02:45:21 +00:00
|
|
|
|
2017-04-15 14:57:52 +00:00
|
|
|
PearlNotMainQueue( ^{
|
|
|
|
resultBlock( [algorithm generateAnswerForSiteNamed:name onQuestion:keyword usingKey:siteKey] );
|
2014-09-22 02:45:21 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
- (void)importProtectedPassword:(NSString *)protectedContent protectedByKey:(MPKey *)importKey
|
2014-09-24 11:58:23 +00:00
|
|
|
intoSite:(MPSiteEntity *)site usingKey:(MPKey *)siteKey {
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
NSAssert( [[siteKey keyIDForAlgorithm:site.user.algorithm] isEqualToData:site.user.keyID], @"Site does not belong to current user." );
|
2014-09-21 15:47:53 +00:00
|
|
|
switch (site.type) {
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedMaximum:
|
|
|
|
case MPSiteTypeGeneratedLong:
|
|
|
|
case MPSiteTypeGeneratedMedium:
|
|
|
|
case MPSiteTypeGeneratedBasic:
|
|
|
|
case MPSiteTypeGeneratedShort:
|
|
|
|
case MPSiteTypeGeneratedPIN:
|
|
|
|
case MPSiteTypeGeneratedName:
|
|
|
|
case MPSiteTypeGeneratedPhrase:
|
2013-09-13 12:14:58 +00:00
|
|
|
break;
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredPersonal: {
|
2014-09-21 15:47:53 +00:00
|
|
|
if (![site isKindOfClass:[MPStoredSiteEntity class]]) {
|
|
|
|
wrn( @"Site with stored type %lu is not an MPStoredSiteEntity, but a %@.",
|
|
|
|
(long)site.type, [site class] );
|
2014-07-28 22:53:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-02-28 15:01:41 +00:00
|
|
|
if ([[importKey keyIDForAlgorithm:self] isEqualToData:[siteKey keyIDForAlgorithm:self]])
|
2014-09-21 15:47:53 +00:00
|
|
|
((MPStoredSiteEntity *)site).contentObject = [protectedContent decodeBase64];
|
2013-09-13 12:14:58 +00:00
|
|
|
|
|
|
|
else {
|
|
|
|
NSString *clearContent = [self decryptContent:[protectedContent decodeBase64] usingKey:importKey];
|
2014-09-21 15:47:53 +00:00
|
|
|
[self importClearTextPassword:clearContent intoSite:site usingKey:siteKey];
|
2013-09-13 12:14:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredDevicePrivate:
|
2013-09-13 12:14:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
- (void)importClearTextPassword:(NSString *)clearContent intoSite:(MPSiteEntity *)site usingKey:(MPKey *)siteKey {
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
NSAssert( [[siteKey keyIDForAlgorithm:site.user.algorithm] isEqualToData:site.user.keyID], @"Site does not belong to current user." );
|
2014-09-21 15:47:53 +00:00
|
|
|
switch (site.type) {
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedMaximum:
|
|
|
|
case MPSiteTypeGeneratedLong:
|
|
|
|
case MPSiteTypeGeneratedMedium:
|
|
|
|
case MPSiteTypeGeneratedBasic:
|
|
|
|
case MPSiteTypeGeneratedShort:
|
|
|
|
case MPSiteTypeGeneratedPIN:
|
|
|
|
case MPSiteTypeGeneratedName:
|
|
|
|
case MPSiteTypeGeneratedPhrase:
|
2013-09-13 12:14:58 +00:00
|
|
|
break;
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredPersonal: {
|
2014-09-21 15:47:53 +00:00
|
|
|
[self savePassword:clearContent toSite:site usingKey:siteKey];
|
2013-09-13 12:14:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredDevicePrivate:
|
2013-09-13 12:14:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
- (NSString *)exportPasswordForSite:(MPSiteEntity *)site usingKey:(MPKey *)siteKey {
|
2013-09-13 12:14:58 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
NSAssert( [[siteKey keyIDForAlgorithm:site.user.algorithm] isEqualToData:site.user.keyID], @"Site does not belong to current user." );
|
2014-09-21 15:47:53 +00:00
|
|
|
if (!(site.type & MPSiteFeatureExportContent))
|
2013-09-13 12:14:58 +00:00
|
|
|
return nil;
|
|
|
|
|
|
|
|
NSString *result = nil;
|
2014-09-21 15:47:53 +00:00
|
|
|
switch (site.type) {
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeGeneratedMaximum:
|
|
|
|
case MPSiteTypeGeneratedLong:
|
|
|
|
case MPSiteTypeGeneratedMedium:
|
|
|
|
case MPSiteTypeGeneratedBasic:
|
|
|
|
case MPSiteTypeGeneratedShort:
|
|
|
|
case MPSiteTypeGeneratedPIN:
|
|
|
|
case MPSiteTypeGeneratedName:
|
|
|
|
case MPSiteTypeGeneratedPhrase: {
|
2013-09-13 12:14:58 +00:00
|
|
|
result = nil;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredPersonal: {
|
2014-09-21 15:47:53 +00:00
|
|
|
if (![site isKindOfClass:[MPStoredSiteEntity class]]) {
|
|
|
|
wrn( @"Site with stored type %lu is not an MPStoredSiteEntity, but a %@.",
|
|
|
|
(long)site.type, [site class] );
|
2014-07-28 22:53:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-09-21 15:47:53 +00:00
|
|
|
result = [((MPStoredSiteEntity *)site).contentObject encodeBase64];
|
2013-09-13 12:14:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
case MPSiteTypeStoredDevicePrivate: {
|
2013-09-13 12:14:58 +00:00
|
|
|
result = nil;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)migrateExplicitly:(BOOL)explicit {
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
- (NSDictionary *)queryForDevicePrivateSiteNamed:(NSString *)name {
|
2013-09-13 12:14:58 +00:00
|
|
|
|
|
|
|
return [PearlKeyChain createQueryForClass:kSecClassGenericPassword
|
|
|
|
attributes:@{
|
2017-04-01 04:30:25 +00:00
|
|
|
(__bridge id)kSecAttrService: @"DevicePrivate",
|
|
|
|
(__bridge id)kSecAttrAccount: name
|
2013-09-13 12:14:58 +00:00
|
|
|
}
|
|
|
|
matches:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)decryptContent:(NSData *)encryptedContent usingKey:(MPKey *)key {
|
|
|
|
|
2014-12-29 21:35:23 +00:00
|
|
|
if (!key)
|
|
|
|
return nil;
|
2013-09-13 12:14:58 +00:00
|
|
|
NSData *decryptedContent = nil;
|
2015-02-28 15:01:41 +00:00
|
|
|
if ([encryptedContent length]) {
|
|
|
|
NSData *encryptionKey = [key keyDataForAlgorithm:self trimmedLength:PearlCryptKeySize];
|
|
|
|
decryptedContent = [encryptedContent decryptWithSymmetricKey:encryptionKey padding:YES];
|
|
|
|
}
|
2013-09-13 12:14:58 +00:00
|
|
|
if (!decryptedContent)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
return [[NSString alloc] initWithBytes:decryptedContent.bytes length:decryptedContent.length encoding:NSUTF8StringEncoding];
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (BOOL)timeToCrack:(out TimeToCrack *)timeToCrack passwordOfType:(MPSiteType)type byAttacker:(MPAttacker)attacker {
|
2014-07-26 05:26:33 +00:00
|
|
|
|
2015-02-28 15:01:41 +00:00
|
|
|
if (!(type & MPSiteTypeClassGenerated))
|
2014-07-26 05:26:33 +00:00
|
|
|
return NO;
|
2015-01-15 22:43:41 +00:00
|
|
|
size_t count = 0;
|
|
|
|
const char **templates = mpw_templatesForType( type, &count );
|
|
|
|
if (!templates)
|
2014-07-26 05:26:33 +00:00
|
|
|
return NO;
|
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
NSDecimalNumber *permutations = [NSDecimalNumber zero], *templatePermutations;
|
2015-01-20 04:30:19 +00:00
|
|
|
for (size_t t = 0; t < count; ++t) {
|
2015-01-15 22:43:41 +00:00
|
|
|
const char *template = templates[t];
|
2017-04-10 15:43:55 +00:00
|
|
|
templatePermutations = [NSDecimalNumber one];
|
2014-07-26 05:26:33 +00:00
|
|
|
|
2015-01-15 22:43:41 +00:00
|
|
|
for (NSUInteger c = 0; c < strlen( template ); ++c)
|
2017-04-10 15:43:55 +00:00
|
|
|
templatePermutations = [templatePermutations decimalNumberByMultiplyingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:strlen( mpw_charactersInClass( template[c] ) )]];
|
2014-07-26 05:26:33 +00:00
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
permutations = [permutations decimalNumberByAdding:templatePermutations];
|
2014-07-26 05:26:33 +00:00
|
|
|
}
|
2015-02-07 16:10:59 +00:00
|
|
|
free( templates );
|
2014-07-26 05:26:33 +00:00
|
|
|
|
|
|
|
return [self timeToCrack:timeToCrack permutations:permutations forAttacker:attacker];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)timeToCrack:(out TimeToCrack *)timeToCrack passwordString:(NSString *)password byAttacker:(MPAttacker)attacker {
|
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
NSDecimalNumber *permutations = [NSDecimalNumber one];
|
2014-07-26 05:26:33 +00:00
|
|
|
|
|
|
|
for (NSUInteger c = 0; c < [password length]; ++c) {
|
2015-01-15 22:43:41 +00:00
|
|
|
const char passwordCharacter = [password substringWithRange:NSMakeRange( c, 1 )].UTF8String[0];
|
2014-07-26 05:26:33 +00:00
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
unsigned long characterEntropy = 0;
|
2015-01-15 22:43:41 +00:00
|
|
|
for (NSString *characterClass in @[ @"v", @"c", @"a", @"x" ]) {
|
|
|
|
char const *charactersForClass = mpw_charactersInClass( characterClass.UTF8String[0] );
|
2014-07-26 05:26:33 +00:00
|
|
|
|
2015-01-15 22:43:41 +00:00
|
|
|
if (strchr( charactersForClass, passwordCharacter )) {
|
2014-07-26 05:26:33 +00:00
|
|
|
// Found class for password character.
|
2017-04-10 15:43:55 +00:00
|
|
|
characterEntropy = strlen( charactersForClass );
|
2014-07-26 05:26:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 22:43:41 +00:00
|
|
|
if (!characterEntropy)
|
2014-07-26 05:26:33 +00:00
|
|
|
characterEntropy = 256 /* a byte */;
|
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
permutations = [permutations decimalNumberByMultiplyingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:characterEntropy]];
|
2014-07-26 05:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [self timeToCrack:timeToCrack permutations:permutations forAttacker:attacker];
|
|
|
|
}
|
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
- (BOOL)timeToCrack:(out TimeToCrack *)timeToCrack permutations:(NSDecimalNumber *)permutations forAttacker:(MPAttacker)attacker {
|
2014-07-26 05:26:33 +00:00
|
|
|
|
2014-07-28 22:53:50 +00:00
|
|
|
// Determine base seconds needed to calculate the permutations.
|
2017-04-10 15:43:55 +00:00
|
|
|
NSDecimalNumber *secondsToCrack = [permutations decimalNumberByDividingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:CRACKING_PER_SECOND]];
|
2014-07-28 22:53:50 +00:00
|
|
|
|
|
|
|
// Modify seconds needed by applying our hardware budget.
|
2014-07-26 05:26:33 +00:00
|
|
|
switch (attacker) {
|
2014-07-28 22:53:50 +00:00
|
|
|
case MPAttacker1:
|
|
|
|
break;
|
2014-07-26 05:26:33 +00:00
|
|
|
case MPAttacker5K:
|
2017-04-10 15:43:55 +00:00
|
|
|
secondsToCrack = [secondsToCrack decimalNumberByMultiplyingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:CRACKING_PRICE]];
|
|
|
|
secondsToCrack = [secondsToCrack decimalNumberByDividingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:5000]];
|
2014-07-26 05:26:33 +00:00
|
|
|
break;
|
|
|
|
case MPAttacker20M:
|
2017-04-10 15:43:55 +00:00
|
|
|
secondsToCrack = [secondsToCrack decimalNumberByMultiplyingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:CRACKING_PRICE]];
|
|
|
|
secondsToCrack = [secondsToCrack decimalNumberByDividingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:20000000]];
|
2014-07-26 05:26:33 +00:00
|
|
|
break;
|
|
|
|
case MPAttacker5B:
|
2017-04-10 15:43:55 +00:00
|
|
|
secondsToCrack = [secondsToCrack decimalNumberByMultiplyingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:CRACKING_PRICE]];
|
|
|
|
secondsToCrack = [secondsToCrack decimalNumberByDividingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:5000]];
|
|
|
|
secondsToCrack = [secondsToCrack decimalNumberByDividingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:1000000]];
|
2014-07-26 05:26:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
NSDecimalNumber *ulong_max = (id)[[NSDecimalNumber alloc] initWithUnsignedLong:ULONG_MAX];
|
2014-07-26 05:26:33 +00:00
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
NSDecimalNumber *hoursToCrack = [secondsToCrack decimalNumberByDividingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:3600L]];
|
|
|
|
if ([hoursToCrack compare:ulong_max] == NSOrderedAscending)
|
|
|
|
timeToCrack->hours = (unsigned long long)[hoursToCrack doubleValue];
|
2014-07-26 05:26:33 +00:00
|
|
|
else
|
2017-04-10 15:43:55 +00:00
|
|
|
timeToCrack->hours = ULONG_MAX;
|
2014-07-26 05:26:33 +00:00
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
NSDecimalNumber *daysToCrack = [hoursToCrack decimalNumberByDividingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:24L]];
|
|
|
|
if ([daysToCrack compare:ulong_max] == NSOrderedAscending)
|
|
|
|
timeToCrack->days = (unsigned long long)[daysToCrack doubleValue];
|
2014-07-26 05:26:33 +00:00
|
|
|
else
|
2017-04-10 15:43:55 +00:00
|
|
|
timeToCrack->days = ULONG_MAX;
|
2014-07-26 05:26:33 +00:00
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
NSDecimalNumber *weeksToCrack = [daysToCrack decimalNumberByDividingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:7L]];
|
|
|
|
if ([weeksToCrack compare:ulong_max] == NSOrderedAscending)
|
|
|
|
timeToCrack->weeks = (unsigned long long)[weeksToCrack doubleValue];
|
2014-07-26 05:26:33 +00:00
|
|
|
else
|
2017-04-10 15:43:55 +00:00
|
|
|
timeToCrack->weeks = ULONG_MAX;
|
2014-07-26 05:26:33 +00:00
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
NSDecimalNumber *monthsToCrack = [daysToCrack decimalNumberByDividingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:31L]];
|
|
|
|
if ([monthsToCrack compare:ulong_max] == NSOrderedAscending)
|
|
|
|
timeToCrack->months = (unsigned long long)[monthsToCrack doubleValue];
|
2014-07-26 05:26:33 +00:00
|
|
|
else
|
2017-04-10 15:43:55 +00:00
|
|
|
timeToCrack->months = ULONG_MAX;
|
2014-07-26 05:26:33 +00:00
|
|
|
|
2017-04-10 15:43:55 +00:00
|
|
|
NSDecimalNumber *yearsToCrack = [daysToCrack decimalNumberByDividingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:356L]];
|
|
|
|
if ([yearsToCrack compare:ulong_max] == NSOrderedAscending)
|
|
|
|
timeToCrack->years = (unsigned long long)[yearsToCrack doubleValue];
|
2014-07-26 05:26:33 +00:00
|
|
|
else
|
2017-04-10 15:43:55 +00:00
|
|
|
timeToCrack->years = ULONG_MAX;
|
|
|
|
|
|
|
|
NSDecimalNumber *universesToCrack = [yearsToCrack decimalNumberByDividingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:14000L]];
|
|
|
|
universesToCrack = [universesToCrack decimalNumberByDividingBy:
|
|
|
|
(id)[[NSDecimalNumber alloc] initWithUnsignedLong:1000000L]];
|
|
|
|
if ([universesToCrack compare:ulong_max] == NSOrderedAscending)
|
|
|
|
timeToCrack->universes = (unsigned long long)[universesToCrack doubleValue];
|
2014-07-26 05:26:33 +00:00
|
|
|
else
|
2017-04-10 15:43:55 +00:00
|
|
|
timeToCrack->universes = ULONG_MAX;
|
2014-07-26 05:26:33 +00:00
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2012-07-17 20:57:11 +00:00
|
|
|
@end
|