Experimental SSL support
This commit is contained in:
@@ -14,9 +14,13 @@
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MBIMBridge : NSObject
|
||||
@property (nonatomic, assign) const char *dylibPath;
|
||||
@property (nonatomic, strong) NSString *dylibPath;
|
||||
@property (nonatomic, assign) UInt16 port;
|
||||
@property (nonatomic, readonly) NSOperationQueue *operationQueue;
|
||||
|
||||
@property (nonatomic, assign) BOOL usesSSL;
|
||||
@property (nonatomic, strong) NSString *sslCertPath;
|
||||
|
||||
+ (instancetype)sharedInstance;
|
||||
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
#import "MBIMBridge.h"
|
||||
#import "MBIMBridge_Private.h"
|
||||
#import "MBIMBridgeOperation.h"
|
||||
#import "MBIMConcurrentHTTPServer.h"
|
||||
#import "MBIMHTTPConnection.h"
|
||||
@@ -21,9 +22,14 @@
|
||||
#import <IMFoundation/IMFoundation.h>
|
||||
#import <IMFoundation/IMFoundation_Private.h>
|
||||
|
||||
static const UInt16 kDefaultPort = 5738;
|
||||
|
||||
static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
||||
|
||||
@interface MBIMBridge (/* INTERNAL */)
|
||||
@interface MBIMBridge (/* INTERNAL */) {
|
||||
__strong NSArray *_sslCertificateAndIdentity;
|
||||
}
|
||||
|
||||
@property (nonatomic, strong) MBIMConcurrentHTTPServer *httpServer;
|
||||
@property (nonatomic, strong) NSOperationQueue *operationQueue;
|
||||
|
||||
@@ -47,11 +53,7 @@ static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
[self registerForNotifications];
|
||||
[self startWebServer];
|
||||
|
||||
[sDaemonController setDelegate:self];
|
||||
[sDaemonListener addHandler:self];
|
||||
self.port = kDefaultPort;
|
||||
|
||||
_operationQueue = [[NSOperationQueue alloc] init];
|
||||
_operationQueue.maxConcurrentOperationCount = 5;
|
||||
@@ -63,7 +65,58 @@ static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
||||
- (void)_terminate
|
||||
{
|
||||
// *shrug*
|
||||
exit(0);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
- (NSArray *)sslCertificateAndIdentity
|
||||
{
|
||||
if (!_sslCertificateAndIdentity && self.sslCertPath) {
|
||||
// Get the p12
|
||||
NSError *error = nil;
|
||||
NSData *certData = [NSData dataWithContentsOfFile:self.sslCertPath options:0 error:&error];
|
||||
if (!certData || error) {
|
||||
MBIMLogError(@"Unable to load SSL certificate from file: %@", [error localizedDescription]);
|
||||
return nil;
|
||||
}
|
||||
|
||||
CFArrayRef items = nil;
|
||||
OSStatus status = SecPKCS12Import(
|
||||
(__bridge CFDataRef)certData,
|
||||
(__bridge CFDictionaryRef) @{
|
||||
(__bridge id)kSecImportExportPassphrase : @"xNAq3vn)^PNu}[&gyQ4MZeV?J"
|
||||
},
|
||||
&items
|
||||
);
|
||||
|
||||
if (status != noErr) {
|
||||
MBIMLogError(@"Error importing PKCS12: SecPKCS12Import status: %d", status);
|
||||
return nil;
|
||||
}
|
||||
|
||||
CFDictionaryRef certDict = CFArrayGetValueAtIndex(items, 0);
|
||||
if (!certDict) {
|
||||
MBIMLogError(@"Error parsing the SSL certificate");
|
||||
return nil;
|
||||
}
|
||||
|
||||
SecIdentityRef identity = (SecIdentityRef)CFDictionaryGetValue(certDict, kSecImportItemIdentity);
|
||||
_sslCertificateAndIdentity = @[ (__bridge id)identity ];
|
||||
}
|
||||
|
||||
return _sslCertificateAndIdentity;
|
||||
}
|
||||
|
||||
- (void)checkSSLCertificate
|
||||
{
|
||||
if (self.usesSSL) {
|
||||
NSArray *certAndIdentity = [self sslCertificateAndIdentity];
|
||||
if ([certAndIdentity count]) {
|
||||
MBIMLogInfo(@"SSL Certificate looks okay");
|
||||
} else {
|
||||
MBIMLogFatal(@"Wasn't able to load SSL certificate. Bailing...");
|
||||
[self _terminate];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
@@ -76,18 +129,26 @@ static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
||||
BOOL hooked = HookIMAgent(self.dylibPath, &errorString);
|
||||
if (!hooked) {
|
||||
NSString *errorNSString = [NSString stringWithUTF8String:errorString];
|
||||
NSLog(@"Error hooking imagent: %@", errorNSString);
|
||||
MBIMLogInfo(@"Error hooking imagent: %@", errorNSString);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (![sDaemonController hasListenerForID: MBIMBridgeToken]) {
|
||||
[self registerForNotifications];
|
||||
|
||||
[sDaemonController setDelegate:self];
|
||||
[sDaemonListener addHandler:self];
|
||||
|
||||
if (![sDaemonController hasListenerForID:MBIMBridgeToken]) {
|
||||
if (![sDaemonController addListenerID:MBIMBridgeToken capabilities:(kFZListenerCapFileTransfers | kFZListenerCapManageStatus | kFZListenerCapChats | kFZListenerCapMessageHistory | kFZListenerCapIDQueries | kFZListenerCapSendMessages)]) {
|
||||
NSLog(@"Failed to connect to imagent");
|
||||
MBIMLogFatal(@"Failed to connect to imagent");
|
||||
|
||||
[self _terminate];
|
||||
}
|
||||
}
|
||||
|
||||
[self checkSSLCertificate];
|
||||
[self startWebServer];
|
||||
}
|
||||
|
||||
- (void)disconnect
|
||||
@@ -116,7 +177,7 @@ static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
||||
|
||||
- (void)_messageReceived:(NSNotification *)notification
|
||||
{
|
||||
NSLog(@"Received message from chat with GUID: %@", [[notification object] guid]);
|
||||
MBIMLogInfo(@"Received message from chat with GUID: %@", [[notification object] guid]);
|
||||
|
||||
IMChat *chat = [notification object];
|
||||
IMMessage *message = [[notification userInfo] objectForKey:IMChatValueKey];
|
||||
@@ -135,14 +196,14 @@ static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
||||
|
||||
- (void)_chatRegistryDidLoad:(NSNotification *)notification
|
||||
{
|
||||
NSLog(@"Loaded chat registry. %lu existing chats", (unsigned long)[sChatRegistry numberOfExistingChats]);
|
||||
MBIMLogInfo(@"Loaded chat registry. %lu existing chats", (unsigned long)[sChatRegistry numberOfExistingChats]);
|
||||
}
|
||||
|
||||
- (void)_chatItemsDidChange:(NSNotification *)notification
|
||||
{
|
||||
IMChat *chat = [notification object];
|
||||
if (chat) {
|
||||
NSLog(@"Chat items change for GUID: %@", [chat guid]);
|
||||
MBIMLogInfo(@"Chat items change for GUID: %@", [chat guid]);
|
||||
|
||||
MBIMUpdateItem *updateItem = [[MBIMUpdateItem alloc] init];
|
||||
updateItem.changedChat = chat;
|
||||
@@ -157,11 +218,13 @@ static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
||||
{
|
||||
self.httpServer = [[MBIMConcurrentHTTPServer alloc] init];
|
||||
[self.httpServer setConnectionClass:[MBIMHTTPConnection class]];
|
||||
[self.httpServer setPort:8080];
|
||||
[self.httpServer setPort:self.port];
|
||||
|
||||
NSError *error = nil;
|
||||
if (![self.httpServer start:&error]) {
|
||||
NSLog(@"Error starting HTTP server: %@", [error localizedDescription]);
|
||||
MBIMLogError(@"Error starting HTTP server: %@", [error localizedDescription]);
|
||||
} else {
|
||||
MBIMLogNotify(@"Started Kordophone HTTP server on port %u", self.port);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,31 +233,31 @@ static NSString *const MBIMBridgeToken = @"net.buzzert.kordophone";
|
||||
|
||||
- (void)daemonControllerWillConnect
|
||||
{
|
||||
NSLog(@"Connecting to imagent...");
|
||||
MBIMLogInfo(@"Connecting to imagent...");
|
||||
}
|
||||
|
||||
- (void)daemonControllerDidConnect
|
||||
{
|
||||
NSLog(@"imagent responded.");
|
||||
MBIMLogInfo(@"imagent responded.");
|
||||
|
||||
IMAccount *iMessageAccount = [[IMAccountController sharedInstance] bestAccountForService:[IMServiceImpl iMessageService]];
|
||||
if (iMessageAccount) {
|
||||
NSLog(@"Successfully got accounts from imagent");
|
||||
NSLog(@"iMessage account connected: %@", iMessageAccount);
|
||||
MBIMLogInfo(@"Successfully got accounts from imagent");
|
||||
MBIMLogInfo(@"iMessage account connected: %@", iMessageAccount);
|
||||
} else {
|
||||
NSLog(@"imagent returned no accounts (not entitled?)");
|
||||
MBIMLogFatal(@"ERROR: imagent returned no accounts (not entitled? speak with Agent Hook)");
|
||||
[self _terminate];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)daemonControllerDidDisconnect
|
||||
{
|
||||
NSLog(@"Disconnected from imagent");
|
||||
MBIMLogInfo(@"Disconnected from imagent");
|
||||
}
|
||||
|
||||
- (void)daemonConnectionLost
|
||||
{
|
||||
NSLog(@"Connection lost to imagent");
|
||||
MBIMLogError(@"Connection lost to imagent");
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
14
kordophone/Bridge/MBIMBridge_Private.h
Normal file
14
kordophone/Bridge/MBIMBridge_Private.h
Normal file
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// MBIMBridge_Private.h
|
||||
// MessagesBridge
|
||||
//
|
||||
// Created by James Magahern on 1/22/19.
|
||||
// Copyright © 2019 James Magahern. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MBIMBridge.h"
|
||||
|
||||
@interface MBIMBridge (/*PRIVATE*/)
|
||||
- (NSArray *)sslCertificateAndIdentity;
|
||||
|
||||
@end
|
||||
@@ -9,13 +9,26 @@
|
||||
#import "MBIMHTTPConnection.h"
|
||||
|
||||
#import "MBIMBridge.h"
|
||||
#import "MBIMBridge_Private.h"
|
||||
#import "MBIMBridgeOperation.h"
|
||||
|
||||
#import <Security/Security.h>
|
||||
|
||||
@implementation MBIMHTTPConnection {
|
||||
NSMutableData *_bodyData;
|
||||
MBIMBridgeOperation *_currentOperation;
|
||||
}
|
||||
|
||||
- (BOOL)isSecureServer
|
||||
{
|
||||
return [[MBIMBridge sharedInstance] usesSSL];
|
||||
}
|
||||
|
||||
- (NSArray *)sslIdentityAndCertificates
|
||||
{
|
||||
return [[MBIMBridge sharedInstance] sslCertificateAndIdentity];
|
||||
}
|
||||
|
||||
- (BOOL)supportsMethod:(NSString *)method atPath:(NSString *)path
|
||||
{
|
||||
if ([method isEqualToString:@"GET"] || [method isEqualToString:@"POST"]) {
|
||||
|
||||
@@ -27,20 +27,20 @@
|
||||
NSString *guid = [self valueForQueryItemWithName:@"guid"];
|
||||
|
||||
if (!guid) {
|
||||
NSLog(@"No query item provided");
|
||||
MBIMLogInfo(@"No query item provided");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
IMFileTransfer *transfer = [[IMFileTransferCenter sharedInstance] transferForGUID:guid];
|
||||
if (!transfer) {
|
||||
NSLog(@"No transfer found for guid: %@", guid);
|
||||
MBIMLogInfo(@"No transfer found for guid: %@", guid);
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
|
||||
break;
|
||||
}
|
||||
|
||||
if (![transfer existsAtLocalPath]) {
|
||||
NSLog(@"We don't have the file for this yet (still downloading to server?)");
|
||||
MBIMLogInfo(@"We don't have the file for this yet (still downloading to server?)");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
|
||||
break;
|
||||
}
|
||||
@@ -48,7 +48,7 @@
|
||||
NSString *localPath = [transfer localPath];
|
||||
NSData *responseData = [NSData dataWithContentsOfFile:localPath];
|
||||
if (!responseData) {
|
||||
NSLog(@"Wasn't able to load data from local path: %@", localPath);
|
||||
MBIMLogInfo(@"Wasn't able to load data from local path: %@", localPath);
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:404];
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
NSString *guid = [self valueForQueryItemWithName:@"guid"];
|
||||
|
||||
if (!guid) {
|
||||
NSLog(@"No query item provided");
|
||||
MBIMLogInfo(@"No query item provided");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
@@ -33,7 +33,7 @@
|
||||
dispatch_sync([[self class] sharedIMAccessQueue], ^{
|
||||
IMChat *chat = [sChatRegistry existingChatWithGUID:guid];
|
||||
if (!chat) {
|
||||
NSLog(@"Chat with guid: %@ not found", guid);
|
||||
MBIMLogInfo(@"Chat with guid: %@ not found", guid);
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
} else {
|
||||
// TODO: be smarter about this and mark individual messages as read? Could lead
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
NSString *guid = [self valueForQueryItemWithName:@"guid"];
|
||||
|
||||
if (!guid) {
|
||||
NSLog(@"No query item provided");
|
||||
MBIMLogInfo(@"No query item provided");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
@@ -37,7 +37,7 @@
|
||||
dispatch_sync([[self class] sharedIMAccessQueue], ^{
|
||||
IMChat *chat = [sChatRegistry existingChatWithGUID:guid];
|
||||
if (!chat) {
|
||||
NSLog(@"Chat with guid: %@ not found", guid);
|
||||
MBIMLogInfo(@"Chat with guid: %@ not found", guid);
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
} else {
|
||||
// Load messages
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
}
|
||||
|
||||
if (!chat) {
|
||||
NSLog(@"Chat does not exist: %@", chatGUID);
|
||||
MBIMLogInfo(@"Chat does not exist: %@", chatGUID);
|
||||
result = NO;
|
||||
} else {
|
||||
[chat sendMessage:reply];
|
||||
|
||||
@@ -26,14 +26,14 @@
|
||||
do {
|
||||
NSString *filename = [self valueForQueryItemWithName:@"filename"];
|
||||
if ([filename length] == 0) {
|
||||
NSLog(@"No filename provided");
|
||||
MBIMLogInfo(@"No filename provided");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
NSData *attachmentData = self.requestBodyData;
|
||||
if ([attachmentData length] == 0) {
|
||||
NSLog(@"No attachment data in request");
|
||||
MBIMLogInfo(@"No attachment data in request");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
@@ -42,14 +42,14 @@
|
||||
NSURL *localURL = [NSURL fileURLWithPath:localPath];
|
||||
BOOL success = [attachmentData writeToURL:localURL atomically:NO];
|
||||
if (!success) {
|
||||
NSLog(@"Error writing attachment to temporary directory");
|
||||
MBIMLogInfo(@"Error writing attachment to temporary directory");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
NSString *guid = [[IMFileTransferCenter sharedInstance] guidForNewOutgoingTransferWithLocalURL:localURL];
|
||||
if (!guid) {
|
||||
NSLog(@"There was some problem shuttling the file to IMCore");
|
||||
MBIMLogInfo(@"There was some problem shuttling the file to IMCore");
|
||||
response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user