From 9226a05ad84239e8c0e42fae91e7cba63d04ed76 Mon Sep 17 00:00:00 2001 From: Diego Waxemberg Date: Sat, 18 Oct 2014 21:13:48 -0400 Subject: [PATCH] implemented crypto and started pairing --- Limelight.xcodeproj/project.pbxproj | 39 +++++++++++++++++ Limelight/CryptoManager.h | 1 + Limelight/CryptoManager.m | 16 ++++++- Limelight/HttpManager.h | 14 ++++++ Limelight/HttpManager.m | 68 +++++++++++++++++++++++++++++ Limelight/MainFrameViewController.m | 6 ++- 6 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 Limelight/HttpManager.h create mode 100644 Limelight/HttpManager.m diff --git a/Limelight.xcodeproj/project.pbxproj b/Limelight.xcodeproj/project.pbxproj index 7d4f9c7c..03676f48 100644 --- a/Limelight.xcodeproj/project.pbxproj +++ b/Limelight.xcodeproj/project.pbxproj @@ -41,6 +41,7 @@ FBAB29F219EDB08B00929691 /* MDNSManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FBAB29F119EDB08B00929691 /* MDNSManager.m */; }; FBAB29F619EDE0F800929691 /* Computer.m in Sources */ = {isa = PBXBuildFile; fileRef = FBAB29F519EDE0F800929691 /* Computer.m */; }; FBAB29FC19EE13AA00929691 /* CryptoManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FBAB29FB19EE13AA00929691 /* CryptoManager.m */; }; + FBC8622D19F0BEFB0087327B /* HttpManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FBC8622C19F0BEFB0087327B /* HttpManager.m */; }; FBCC0E9A19EF9703009729EB /* libcrypto.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FBCC0E9819EF9703009729EB /* libcrypto.a */; }; FBCC0E9B19EF9703009729EB /* libssl.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FBCC0E9919EF9703009729EB /* libssl.a */; }; FBCC0E9D19F00659009729EB /* mkcert.c in Sources */ = {isa = PBXBuildFile; fileRef = FBCC0E9C19F00659009729EB /* mkcert.c */; }; @@ -62,6 +63,13 @@ remoteGlobalIDString = FB290CED19B2C406004C83CF; remoteInfo = Limelight; }; + FBC8623219F346A60087327B /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = FBC8622E19F346A60087327B /* limelight-common.xcodeproj */; + proxyType = 2; + remoteGlobalIDString = FB290E2E19B37A4E004C83CF; + remoteInfo = "limelight-common"; + }; /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ @@ -118,6 +126,9 @@ FBAB29F519EDE0F800929691 /* Computer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Computer.m; sourceTree = ""; }; FBAB29FA19EE13AA00929691 /* CryptoManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CryptoManager.h; sourceTree = ""; }; FBAB29FB19EE13AA00929691 /* CryptoManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CryptoManager.m; sourceTree = ""; }; + FBC8622B19F0BEFB0087327B /* HttpManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HttpManager.h; sourceTree = ""; }; + FBC8622C19F0BEFB0087327B /* HttpManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HttpManager.m; sourceTree = ""; }; + FBC8622E19F346A60087327B /* limelight-common.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = "limelight-common.xcodeproj"; path = "../../limelight-common-c/limelight-common.xcodeproj"; sourceTree = ""; }; FBCC0E4C19EF9703009729EB /* aes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aes.h; sourceTree = ""; }; FBCC0E4D19EF9703009729EB /* asn1.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = asn1.h; sourceTree = ""; }; FBCC0E4E19EF9703009729EB /* asn1_mac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = asn1_mac.h; sourceTree = ""; }; @@ -358,6 +369,7 @@ FB290CF019B2C406004C83CF /* Frameworks */ = { isa = PBXGroup; children = ( + FBC8622E19F346A60087327B /* limelight-common.xcodeproj */, FB7E794319C8B71B00A15F68 /* libiconv.dylib */, FB290DC319B2E98F004C83CF /* libxml2.dylib */, FB290DB819B2C877004C83CF /* libbz2.dylib */, @@ -401,6 +413,8 @@ FBAB29FB19EE13AA00929691 /* CryptoManager.m */, FBCC0E9E19F00697009729EB /* mkcert.h */, FBCC0E9C19F00659009729EB /* mkcert.c */, + FBC8622B19F0BEFB0087327B /* HttpManager.h */, + FBC8622C19F0BEFB0087327B /* HttpManager.m */, ); path = Limelight; sourceTree = ""; @@ -528,6 +542,14 @@ path = FFmpeg; sourceTree = ""; }; + FBC8622F19F346A60087327B /* Products */ = { + isa = PBXGroup; + children = ( + FBC8623319F346A60087327B /* liblimelight-common.a */, + ); + name = Products; + sourceTree = ""; + }; FBCC0E4919EF9703009729EB /* openssl */ = { isa = PBXGroup; children = ( @@ -871,6 +893,12 @@ mainGroup = FB290CE519B2C406004C83CF; productRefGroup = FB290CEF19B2C406004C83CF /* Products */; projectDirPath = ""; + projectReferences = ( + { + ProductGroup = FBC8622F19F346A60087327B /* Products */; + ProjectRef = FBC8622E19F346A60087327B /* limelight-common.xcodeproj */; + }, + ); projectRoot = ""; targets = ( FB290CED19B2C406004C83CF /* Limelight */, @@ -879,6 +907,16 @@ }; /* End PBXProject section */ +/* Begin PBXReferenceProxy section */ + FBC8623319F346A60087327B /* liblimelight-common.a */ = { + isa = PBXReferenceProxy; + fileType = archive.ar; + path = "liblimelight-common.a"; + remoteRef = FBC8623219F346A60087327B /* PBXContainerItemProxy */; + sourceTree = BUILT_PRODUCTS_DIR; + }; +/* End PBXReferenceProxy section */ + /* Begin PBXResourcesBuildPhase section */ FB290CEC19B2C406004C83CF /* Resources */ = { isa = PBXResourcesBuildPhase; @@ -909,6 +947,7 @@ FBAB29FC19EE13AA00929691 /* CryptoManager.m in Sources */, FB290D3D19B2C6E3004C83CF /* VideoDepacketizer.m in Sources */, FBAB29F219EDB08B00929691 /* MDNSManager.m in Sources */, + FBC8622D19F0BEFB0087327B /* HttpManager.m in Sources */, FB290D0719B2C406004C83CF /* Limelight.xcdatamodeld in Sources */, FB290D3E19B2C6E3004C83CF /* VideoRenderer.m in Sources */, FB290D0419B2C406004C83CF /* AppDelegate.m in Sources */, diff --git a/Limelight/CryptoManager.h b/Limelight/CryptoManager.h index 87ec6460..9d639e46 100644 --- a/Limelight/CryptoManager.h +++ b/Limelight/CryptoManager.h @@ -11,5 +11,6 @@ @interface CryptoManager : NSObject - (void) generateKeyPairUsingSSl; +- (NSString*) getUniqueID; @end diff --git a/Limelight/CryptoManager.m b/Limelight/CryptoManager.m index 174d0261..9909a37d 100644 --- a/Limelight/CryptoManager.m +++ b/Limelight/CryptoManager.m @@ -8,7 +8,7 @@ #import "CryptoManager.h" #import "mkcert.h" - +#import @implementation CryptoManager @@ -31,4 +31,18 @@ freeCertKeyPair(certKeyPair); } +- (NSString*) getUniqueID { + // generate a UUID + NSUUID* uuid = [ASIdentifierManager sharedManager].advertisingIdentifier; + NSString* idString = [NSString stringWithString:[uuid UUIDString]]; + + // we need a 16byte hex-string so we take the last 17 characters + // and remove the '-' to get a 16 character string + NSMutableString* uniqueId = [NSMutableString stringWithString:[idString substringFromIndex:19]]; + [uniqueId deleteCharactersInRange:NSMakeRange(4, 1)]; + + //NSLog(@"Unique ID: %@", uniqueId); + return [NSString stringWithString:uniqueId]; +} + @end diff --git a/Limelight/HttpManager.h b/Limelight/HttpManager.h new file mode 100644 index 00000000..4658e3b2 --- /dev/null +++ b/Limelight/HttpManager.h @@ -0,0 +1,14 @@ +// +// HttpManager.h +// Limelight +// +// Created by Diego Waxemberg on 10/16/14. +// Copyright (c) 2014 Limelight Stream. All rights reserved. +// + +#import + +@interface HttpManager : NSObject +- (NSString*) generatePIN; +- (NSString*) saltPIN:(NSString*)PIN; +@end diff --git a/Limelight/HttpManager.m b/Limelight/HttpManager.m new file mode 100644 index 00000000..d47e6d2e --- /dev/null +++ b/Limelight/HttpManager.m @@ -0,0 +1,68 @@ +// +// HttpManager.m +// Limelight +// +// Created by Diego Waxemberg on 10/16/14. +// Copyright (c) 2014 Limelight Stream. All rights reserved. +// + +#import "HttpManager.h" + +@implementation HttpManager { + NSString* _baseURL; + NSString* _host; + NSString* _uniqueId; + NSString* _deviceName; +} + +static const NSString* PORT = @"47984"; + + +- (id) initWithHost:(NSString*) host uniqueId:(NSString*) uniqueId deviceName:(NSString*) deviceName { + self = [super init]; + _host = host; + _uniqueId = uniqueId; + _deviceName = deviceName; + _baseURL = [[NSString stringWithFormat:@"https://%@:%@", host, PORT] + stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + return self; +} + +- (NSURL*) newPairRequestWithSalt:(NSString*)salt andCert:(NSString*)cert { + NSURL* url = [[NSURL alloc] initWithString: + [NSString stringWithFormat:@"http://%@:%@/pair?uniqueid=%@&devicename=%@&updateState=1&phrase=getservercert&salt=%@&clientcert=%@", + _host, PORT, _uniqueId, _deviceName, salt, cert]]; + return url; +} + +- (void) initiatePairing { + +} + +- (NSString*) generatePIN { + NSString* PIN = [NSString stringWithFormat:@"%d%d%d%d", + arc4random() % 10, arc4random() % 10, + arc4random() % 10, arc4random() % 10]; + NSLog(@"PIN: %@", PIN); + return PIN; +} + +- (NSData*) saltPIN:(NSString*)PIN { + NSMutableData* saltedPIN = [[NSMutableData alloc] initWithCapacity:20]; + [saltedPIN appendData:[self randomBytes:16]]; + [saltedPIN appendBytes:[PIN UTF8String] length:4]; + + NSLog(@"Salted PIN: %@", [saltedPIN description]); + + return saltedPIN; +} + +- (NSData*) randomBytes:(NSInteger)length { + char* bytes = malloc(length); + arc4random_buf(bytes, length); + NSData* randomData = [NSData dataWithBytes:bytes length:length]; + free(bytes); + return randomData; +} + +@end diff --git a/Limelight/MainFrameViewController.m b/Limelight/MainFrameViewController.m index 1a8a0cc4..2631df4f 100644 --- a/Limelight/MainFrameViewController.m +++ b/Limelight/MainFrameViewController.m @@ -11,6 +11,7 @@ #import "ConnectionHandler.h" #import "Computer.h" #import "CryptoManager.h" +#import "HttpManager.h" @implementation MainFrameViewController NSString* hostAddr; @@ -87,7 +88,10 @@ MDNSManager* mDNSManager; mDNSManager = [[MDNSManager alloc] initWithCallback:self]; [mDNSManager searchForHosts]; - [[[CryptoManager alloc] init] generateKeyPairUsingSSl]; + CryptoManager* cryptMan = [[CryptoManager alloc] init]; + [cryptMan getUniqueID]; + HttpManager* hMan = [[HttpManager alloc] init]; + [hMan saltPIN:[hMan generatePIN]]; } - (void)updateHosts:(NSArray *)hosts {