11 #import "EncryptionController.h"
12 #import "SFHFKeychainUtils.h"
13 #import "TSXAdditions.h"
15 @interface EncryptionController (Private)
17 - (BOOL)verifyPassword:(
Encryptor *)decryptor;
18 - (NSData *)encryptedVerificationData;
19 - (void)setEncryptedVerificationData:(
Encryptor *)encryptor;
21 - (NSString *)keychainServerName;
22 - (NSString *)keychainUsername;
23 - (void)setKeychainPassword:(NSString *)password;
24 - (NSString *)keychainPassword;
25 - (NSString *)keychainDefaultPassword;
39 if (_shared_encryption_controller == nil)
43 return _shared_encryption_controller;
46 #pragma mark Getting an encryptor or decryptor
50 if (_shared_encryptor)
51 return _shared_encryptor;
53 NSString *saved_password = [
self keychainPassword];
54 if (saved_password == nil)
56 saved_password = [
self keychainDefaultPassword];
57 Encryptor *encryptor = [[[
Encryptor alloc] initWithPassword:saved_password] autorelease];
58 [
self setEncryptedVerificationData:encryptor];
59 _shared_encryptor = [encryptor retain];
63 Encryptor *encryptor = [[[
Encryptor alloc] initWithPassword:saved_password] autorelease];
64 if ([
self verifyPassword:encryptor])
65 _shared_encryptor = [encryptor retain];
68 return _shared_encryptor;
74 return [
self encryptor];
81 @implementation EncryptionController (Private)
84 #pragma mark Keychain password storage
86 - (NSString *)keychainServerName
88 return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
91 - (NSString *)keychainUsername
93 return @"master.password";
96 - (void)setKeychainPassword:(NSString *)password
102 andServerName:[
self keychainServerName]
109 forServerName:[
self keychainServerName]
114 - (NSString *)keychainPassword
118 andServerName:[
self keychainServerName]
122 - (NSString *)keychainDefaultPassword
124 NSString *password = [[NSUserDefaults standardUserDefaults] stringForKey:@"UUID"];
125 if ([password length] == 0)
127 password = [NSString stringWithUUID];
128 [[NSUserDefaults standardUserDefaults] setObject:password forKey:@"UUID"];
129 [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"TSXMasterPasswordVerification"];
135 #pragma mark Verification of encryption key against verification data
137 - (BOOL)verifyPassword:(
Encryptor *)decryptor
139 return [[decryptor plaintextPassword]
140 isEqualToString:[decryptor decryptString:[
self encryptedVerificationData]]];
143 - (NSData *)encryptedVerificationData
145 return [[NSUserDefaults standardUserDefaults] dataForKey:@"TSXMasterPasswordVerification"];
148 - (void)setEncryptedVerificationData:(
Encryptor *)encryptor
150 [[NSUserDefaults standardUserDefaults]
151 setObject:[encryptor encryptString:[encryptor plaintextPassword]]
152 forKey:@"TSXMasterPasswordVerification"];