Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Better lookup for changed key

  • Loading branch information...
commit 2375221b1b54f2b05426a185535a9ce2312deb9a 1 parent 980370e
@plaetzchen plaetzchen authored
View
19 Classes/Crypto/Crypto.m
@@ -120,9 +120,11 @@ - (NSString *)decryptString: (NSString *)string {
- (void)appendInfoToDictionary: (NSMutableDictionary *)dictionary {
- NSDictionary *cryptedPassword = [self getEncryptedRandomStringForClient];
-
- if (cryptedPassword != nil){
+ NSDictionary *cryptedPassword;
+ if ([[NSUserDefaults standardUserDefaults] boolForKey:@"sendPassword"]){
+ cryptedPassword = [self getEncryptedRandomStringForClient];
+ }
+ if ([[NSUserDefaults standardUserDefaults] boolForKey:@"sendPassword"]){
NSDictionary *encryption = [NSDictionary dictionaryWithObjectsAndKeys:
@"AES", @"method",
[NSNumber numberWithInt:256], @"keysize",
@@ -133,8 +135,15 @@ - (void)appendInfoToDictionary: (NSMutableDictionary *)dictionary {
[dictionary setObject:encryption forKey:@"encryption"];
}
else {
- NSNotification *notification = [NSNotification notificationWithName:@"encryptionError" object:self];
- [[NSNotificationCenter defaultCenter] postNotification:notification];
+ NSDictionary *encryption = [NSDictionary dictionaryWithObjectsAndKeys:
+ @"AES", @"method",
+ [NSNumber numberWithInt:256], @"keysize",
+ [salt asBase64EncodedString], @"salt",
+ @"SHA256", @"hash", nil];
+
+
+ [dictionary setObject:encryption forKey:@"encryption"];
+
}
}
View
20 Classes/Crypto/PublicKeyManager.m
@@ -38,25 +38,31 @@ -(BOOL)storeKeyRef:(SecKeyRef)theKey{
-(BOOL)storeKey:(NSString *)theKey forClient:(NSDictionary *)client{
- NSString *theTag = [NSString stringWithFormat:@"com.hoccer.publickeys.%@",[client objectForKey:@"id"]];
+ NSString *theTag = [NSString stringWithFormat:@"com.hoccer.publickey.%@",[client objectForKey:@"id"]];
BOOL safed = [[RSA sharedInstance] addPublicKey:theKey withTag:theTag];
if (safed){
+ NSArray *IDs = [collectedKeys valueForKey:@"clientId"];
+ NSString *search = [client objectForKey:@"id"];
+ NSUInteger index = [IDs indexOfObject:search];
+ NSDictionary *storedClient = [collectedKeys objectAtIndex: index];
+
+ [collectedKeys removeObject:storedClient];
+
NSDictionary *keyDicitonary = [[NSDictionary alloc]initWithObjectsAndKeys:theKey,@"key",[client objectForKey:@"id"], @"clientId", [client objectForKey:@"name"], @"clientName", nil];
[collectedKeys addObject:keyDicitonary];
[keyDicitonary release];
[[NSUserDefaults standardUserDefaults] setObject:collectedKeys forKey:@"keyStore"];
[[NSUserDefaults standardUserDefaults] synchronize];
- return YES;
}
- return NO;
+ return safed;
}
-(SecKeyRef)getKeyForClient:(NSString *)theId{
- NSString *theName = [NSString stringWithFormat:@"com.hoccer.publickeys.%@",theId];
+ NSString *theName = [NSString stringWithFormat:@"com.hoccer.publickey.%@",theId];
SecKeyRef theKey = [[RSA sharedInstance] getPeerKeyRef:theName];
@@ -71,7 +77,7 @@ -(SecKeyRef)getKeyForClient:(NSString *)theId{
-(BOOL)checkForKeyChange:(NSDictionary *)client withHash:(NSString *)theHash{
BOOL result = YES;
- NSString *theName = [NSString stringWithFormat:@"com.hoccer.publickeys.%@",[client objectForKey:@"id"]];
+ NSString *theName = [NSString stringWithFormat:@"com.hoccer.publickey.%@",[client objectForKey:@"id"]];
NSData *storedKey = [[RSA sharedInstance] getKeyBitsForPeerRef:theName];
@@ -88,7 +94,7 @@ -(BOOL)checkForKeyChange:(NSDictionary *)client withHash:(NSString *)theHash{
NSUInteger index = [IDs indexOfObject:search];
NSDictionary *storedClient = [collectedKeys objectAtIndex: index];
- if (storedClient !=nil && ![[storedClient objectForKey:@"clientName"] isEqualToString:[client objectForKey:@"name"]] && [storedHash isEqualToString:[client objectForKey:@"pubkey"]]){
+ if ((storedClient !=nil && ![[storedClient objectForKey:@"clientName"] isEqualToString:[client objectForKey:@"name"]]) || (storedClient !=nil && ![storedHash isEqualToString: [client objectForKey:@"pubkey_id"]])){
result = YES;
}
@@ -98,7 +104,7 @@ -(BOOL)checkForKeyChange:(NSDictionary *)client withHash:(NSString *)theHash{
}
-(void)deleteKeyForClient:(NSString *)theId{
- NSString *theName = [NSString stringWithFormat:@"com.hoccer.publickeys.%@",theId];
+ NSString *theName = [NSString stringWithFormat:@"com.hoccer.publickey.%@",theId];
[[RSA sharedInstance]removePeerPublicKey:theName];
}
View
28 Classes/Crypto/RSA.m
@@ -35,8 +35,8 @@ @implementation RSA
const size_t CIPHER_BUFFER_SIZE = 1024;
const uint32_t PADDING = kSecPaddingPKCS1;
-static const uint8_t publicKeyIdentifier[] = "com.hoccer.sample.publickey";
-static const uint8_t privateKeyIdentifier[] = "com.hoccer.sample.privatekey";
+static const uint8_t publicKeyIdentifier[] = "com.hoccer.client.publickey";
+static const uint8_t privateKeyIdentifier[] = "com.hoccer.client.privatekey";
SecKeyRef publicKey;
SecKeyRef privateKey;
@@ -72,21 +72,25 @@ - (id)init
- (NSString *)genRandomString:(int)length {
- NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!§$%&/()=?";
-
- NSMutableString *randomString = [NSMutableString stringWithCapacity: length];
-
- for (int i=0; i<length; i++) {
- [randomString appendFormat: @"%c", [letters characterAtIndex: rand()%[letters length]]];
- }
+ if ([[NSUserDefaults standardUserDefaults]boolForKey:@"autoPassword"]){
+ NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!§$%&/()=?";
+
+ NSMutableString *randomString = [NSMutableString stringWithCapacity: length];
- [[NSUserDefaults standardUserDefaults] setObject:randomString forKey:@"encryptionKey"];
- [[NSUserDefaults standardUserDefaults] synchronize];
+ for (int i=0; i<length; i++) {
+ [randomString appendFormat: @"%c", [letters characterAtIndex: rand()%[letters length]]];
+ }
- NSLog (@"generated String %@",randomString);
+ [[NSUserDefaults standardUserDefaults] setObject:randomString forKey:@"encryptionKey"];
+ [[NSUserDefaults standardUserDefaults] synchronize];
+
return randomString;
+ }
+ else {
+ return [[NSUserDefaults standardUserDefaults]stringForKey:@"encryptionKey"];
+ }
}
View
4 Classes/HoccerClient/HCFileCache.m
@@ -39,8 +39,8 @@
#import "NSData+CommonCrypto.h"
#define FILECACHE_URI @"https://filecache.hoccer.com/v3"
-// #define FILECACHE_SANDBOX_URI @"https://filecache-experimental.hoccer.com/v3"
- #define FILECACHE_SANDBOX_URI @"https://filecache-sandbox.hoccer.com/v3"
+ #define FILECACHE_SANDBOX_URI @"https://filecache-experimental.hoccer.com/v3"
+// #define FILECACHE_SANDBOX_URI @"https://filecache-sandbox.hoccer.com/v3"
@implementation HCFileCache
View
4 Classes/HoccerClient/HCLinccer.h
@@ -104,7 +104,7 @@ enum HoccerErrors {
- (void)updateEnvironment;
-- (void)fetchPublicKeyForHash:(NSString *)theHash client:(NSDictionary *)client;
-- (void)storePublicKey:(NSString *)theKey forClient:(NSDictionary *)client;
+- (void)fetchPublicKeyForHash:(NSString *)theHash client:(NSDictionary *)client clientChanged:(BOOL)changed;
+- (void)storePublicKey:(NSString *)theKey forClient:(NSDictionary *)client clientChanged:(BOOL)changed;
@end
View
64 Classes/HoccerClient/HCLinccer.m
@@ -49,8 +49,8 @@
#import "PublicKeyManager.h"
#define LINCCER_URI @"https://linccer.hoccer.com/v3"
-// #define LINCCER_SANDBOX_URI @"https://linccer-experimental.hoccer.com/v3"
-#define LINCCER_SANDBOX_URI @"https://linccer-sandbox.hoccer.com/v3"
+#define LINCCER_SANDBOX_URI @"https://linccer-experimental.hoccer.com/v3"
+//#define LINCCER_SANDBOX_URI @"https://linccer-sandbox.hoccer.com/v3"
#define HOCCER_CLIENT_ID_KEY @"hoccerClientUri"
@interface HCLinccer ()
@@ -164,16 +164,13 @@ - (void)checkGroupForPublicKeys:(NSDictionary *)aDictionary{
}
for (NSDictionary *aClient in others) {
- if ([aClient objectForKey:@"pubkey"] !=nil){
+ if ([aClient objectForKey:@"pubkey_id"] !=nil){
if ([keyManager getKeyForClient:[aClient objectForKey:@"id"]] == nil){
- [self fetchPublicKeyForHash:[aClient objectForKey:@"pubkey"] client:aClient];
+ [self fetchPublicKeyForHash:[aClient objectForKey:@"pubkey_id"] client:aClient clientChanged:NO];
}
else {
- if ([keyManager checkForKeyChange:aClient withHash:[aClient objectForKey:@"pubkey"]]){
- if ([delegate respondsToSelector:@selector(linccer:keyHasChangedForClientName:)]){
- [delegate linccer:self keyHasChangedForClientName:[aClient objectForKey:@"name"]];
- }
- [self fetchPublicKeyForHash:[aClient objectForKey:@"pubkey"] client:aClient];
+ if ([keyManager checkForKeyChange:aClient withHash:[aClient objectForKey:@"pubkey_id"]]){
+ [self fetchPublicKeyForHash:[aClient objectForKey:@"pubkey_id"] client:aClient clientChanged:YES];
}
}
}
@@ -181,19 +178,29 @@ - (void)checkGroupForPublicKeys:(NSDictionary *)aDictionary{
}
-- (void)fetchPublicKeyForHash:(NSString *)theHash client:(NSDictionary *)client{
+- (void)fetchPublicKeyForHash:(NSString *)theHash client:(NSDictionary *)client clientChanged:(BOOL)changed{
if (!isRegistered) {
[self didFailWithError:nil];
}
- NSString *fetchString = [theHash stringByAppendingPathComponent:@"publickey"];
- [httpClient getURI:[uri stringByAppendingPathComponent:fetchString] success:@selector(httpConnection:didReceivePublicKey:)];
- [clientIDCache setObject:client forKey:theHash];
+ if (!changed){
+ NSString *fetchString = [theHash stringByAppendingPathComponent:@"publickey"];
+ [httpClient getURI:[uri stringByAppendingPathComponent:fetchString] success:@selector(httpConnection:didReceivePublicKey:)];
+ [clientIDCache setObject:client forKey:theHash];
+ }
+ else {
+ NSString *fetchString = [theHash stringByAppendingPathComponent:@"publickey"];
+ [httpClient getURI:[uri stringByAppendingPathComponent:fetchString] success:@selector(httpConnection:didReceiveChangedPublicKey:)];
+ [clientIDCache setObject:client forKey:theHash];
+ }
}
-- (void)storePublicKey:(NSString *)theKey forClient:(NSDictionary *)client{
+- (void)storePublicKey:(NSString *)theKey forClient:(NSDictionary *)client clientChanged:(BOOL)changed{
+ if (changed){
+ [keyManager deleteKeyForClient:[client objectForKey:@"id"]];
+ }
if (theKey != nil){
if (![keyManager storeKey:theKey forClient:client]){
NSMutableDictionary *errorInfo = [NSMutableDictionary dictionary];
@@ -206,6 +213,13 @@ - (void)storePublicKey:(NSString *)theKey forClient:(NSDictionary *)client{
[delegate linccer: self didFailWithError:error];
}
}
+ else {
+ if (changed){
+ if ([delegate respondsToSelector:@selector(linccer:keyHasChangedForClientName:)]){
+ [delegate linccer:self keyHasChangedForClientName:[client objectForKey:@"name"]];
+ }
+ }
+ }
}
}
@@ -366,14 +380,24 @@ - (void)httpConnection: (HttpConnection *)connection didUpdateGroup: (NSData *)g
}
-- (void)httpConnection: (HttpConnection *)connection didReceivePublicKey: (NSData *)pubkey {
+- (void)httpConnection: (HttpConnection *)connection didReceivePublicKey: (NSData *)pubkey{
NSString *theKey = [[[NSString stringWithData: pubkey usingEncoding:NSUTF8StringEncoding]componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];
NSArray *uriArray = [connection.uri componentsSeparatedByString:@"/"];
NSString *identifier = [uriArray objectAtIndex:6];
- [self storePublicKey:theKey forClient:[clientIDCache objectForKey:identifier]];
+ [self storePublicKey:theKey forClient:[clientIDCache objectForKey:identifier] clientChanged:NO];
+}
+
+- (void)httpConnection: (HttpConnection *)connection didReceiveChangedPublicKey: (NSData *)pubkey{
+
+
+ NSString *theKey = [[[NSString stringWithData: pubkey usingEncoding:NSUTF8StringEncoding]componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];
+
+ NSArray *uriArray = [connection.uri componentsSeparatedByString:@"/"];
+ NSString *identifier = [uriArray objectAtIndex:6];
+ [self storePublicKey:theKey forClient:[clientIDCache objectForKey:identifier] clientChanged:YES];
}
#pragma mark -
#pragma mark Private Methods
@@ -412,9 +436,11 @@ - (void)updateEnvironment {
NSMutableDictionary *environment = [[environmentController.environment dict] mutableCopy];
[environment setObject:[NSNumber numberWithDouble:self.latency * 1000] forKey:@"latency"];
[environment addEntriesFromDictionary:self.userInfo];
- NSData *pubKey = [[RSA sharedInstance] getPublicKeyBits];
- NSString *pubKeyAsString = [[[pubKey asBase64EncodedString]componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];
- [environment setObject:pubKeyAsString forKey:@"pubkey"];
+ if ([[NSUserDefaults standardUserDefaults] boolForKey:@"autoKey"]){
+ NSData *pubKey = [[RSA sharedInstance] getPublicKeyBits];
+ NSString *pubKeyAsString = [[[pubKey asBase64EncodedString]componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];
+ [environment setObject:pubKeyAsString forKey:@"pubkey"];
+ }
NSString *enviromentAsString = [environment yajl_JSONString];
[httpClient putURI:[uri stringByAppendingPathComponent:@"/environment"]
Please sign in to comment.
Something went wrong with that request. Please try again.