From 6225af1e76bba5b70088745b719c230e0c69b347 Mon Sep 17 00:00:00 2001 From: Diego Waxemberg Date: Sat, 18 Oct 2014 22:26:25 -0400 Subject: [PATCH] more pairing code --- Limelight/CryptoManager.h | 1 + Limelight/CryptoManager.m | 7 +++++++ Limelight/HttpManager.h | 4 +++- Limelight/HttpManager.m | 18 ++++++++++++------ Limelight/MainFrameViewController.h | 2 +- Limelight/MainFrameViewController.m | 15 ++++++++++++--- 6 files changed, 36 insertions(+), 11 deletions(-) diff --git a/Limelight/CryptoManager.h b/Limelight/CryptoManager.h index 9d639e4..85b1757 100644 --- a/Limelight/CryptoManager.h +++ b/Limelight/CryptoManager.h @@ -12,5 +12,6 @@ - (void) generateKeyPairUsingSSl; - (NSString*) getUniqueID; +- (NSData*) readCertFromFile; @end diff --git a/Limelight/CryptoManager.m b/Limelight/CryptoManager.m index 9909a37..663d9c6 100644 --- a/Limelight/CryptoManager.m +++ b/Limelight/CryptoManager.m @@ -17,6 +17,13 @@ } +- (NSData*) readCertFromFile { + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); + NSString *documentsDirectory = [paths objectAtIndex:0]; + NSString *certFile = [documentsDirectory stringByAppendingPathComponent:@"client.crt"]; + return [NSData dataWithContentsOfFile:certFile]; +} + - (void) generateKeyPairUsingSSl { NSLog(@"Generating Certificate: "); CertKeyPair certKeyPair = generateCertKeyPair(); diff --git a/Limelight/HttpManager.h b/Limelight/HttpManager.h index 4658e3b..2d6aa20 100644 --- a/Limelight/HttpManager.h +++ b/Limelight/HttpManager.h @@ -9,6 +9,8 @@ #import @interface HttpManager : NSObject +- (id) initWithHost:(NSString*) host uniqueId:(NSString*) uniqueId deviceName:(NSString*) deviceName; - (NSString*) generatePIN; -- (NSString*) saltPIN:(NSString*)PIN; +- (NSData*) saltPIN:(NSString*)PIN; +- (NSURL*) newPairRequestWithSalt:(NSData*)salt andCert:(NSData*)cert; @end diff --git a/Limelight/HttpManager.m b/Limelight/HttpManager.m index d47e6d2..934c105 100644 --- a/Limelight/HttpManager.m +++ b/Limelight/HttpManager.m @@ -28,15 +28,21 @@ static const NSString* PORT = @"47984"; 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]]; +- (NSURL*) newPairRequestWithSalt:(NSData*)salt andCert:(NSData*)cert { + NSLog(@"host: %@ \nport: %@ \nuniqueID: %@ \ndeviceName: %@ \nsalt: %@ \ncert: %@", _host, PORT, _uniqueId, _deviceName, salt, cert); + NSString* urlString = [[NSString stringWithFormat:@"%@/pair?uniqueid=%@&devicename=%@&updateState=1&phrase=getservercert&salt=%@&clientcert=%@", _baseURL, _uniqueId, _deviceName, [self bytesToHex:salt], [self bytesToHex:cert]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + NSURL* url = [[NSURL alloc] initWithString:urlString]; + NSLog(@"pair url: %@", url); return url; } -- (void) initiatePairing { - +- (NSString*) bytesToHex:(NSData*)data { + const unsigned char* bytes = [data bytes]; + NSMutableString *hex = [[NSMutableString alloc] init]; + for (int i = 0; i < [data length]; i++) { + [hex appendFormat:@"%02X" , bytes[i]]; + } + return hex; } - (NSString*) generatePIN { diff --git a/Limelight/MainFrameViewController.h b/Limelight/MainFrameViewController.h index f3cae6f..514d776 100644 --- a/Limelight/MainFrameViewController.h +++ b/Limelight/MainFrameViewController.h @@ -9,7 +9,7 @@ #import #import "MDNSManager.h" -@interface MainFrameViewController : UIViewController +@interface MainFrameViewController : UIViewController @property (strong, nonatomic) IBOutlet UIPickerView *HostPicker; - (IBAction)StreamButton:(UIButton *)sender; - (IBAction)PairButton:(UIButton *)sender; diff --git a/Limelight/MainFrameViewController.m b/Limelight/MainFrameViewController.m index 2631df4..0e29635 100644 --- a/Limelight/MainFrameViewController.m +++ b/Limelight/MainFrameViewController.m @@ -89,9 +89,18 @@ MDNSManager* mDNSManager; mDNSManager = [[MDNSManager alloc] initWithCallback:self]; [mDNSManager searchForHosts]; CryptoManager* cryptMan = [[CryptoManager alloc] init]; - [cryptMan getUniqueID]; - HttpManager* hMan = [[HttpManager alloc] init]; - [hMan saltPIN:[hMan generatePIN]]; + NSString* uniqueId = [cryptMan getUniqueID]; + [cryptMan generateKeyPairUsingSSl]; + NSData* cert = [cryptMan readCertFromFile]; + HttpManager* hMan = [[HttpManager alloc] initWithHost:hostAddr uniqueId:uniqueId deviceName:@"roth"]; + NSString* PIN = [hMan generatePIN]; + NSData* saltedPIN = [hMan saltPIN:PIN]; + NSLog(@"PIN: %@, saltedPIN: %@", PIN, saltedPIN); + NSURL* pairUrl = [hMan newPairRequestWithSalt:saltedPIN andCert:cert]; + NSURLRequest* pairRequest = [[NSURLRequest alloc] initWithURL:pairUrl]; + NSLog(@"making pair request: %@", [pairRequest description]); + NSData* pairData = [NSURLConnection sendSynchronousRequest:pairRequest returningResponse:nil error:nil]; + NSLog(@"Pair response: %@", [pairData description]); } - (void)updateHosts:(NSArray *)hosts {