Private
Public Access
1
0

Experimental SSL support

This commit is contained in:
James Magahern
2019-01-22 23:31:36 -08:00
parent 90775ebbba
commit 0cb907e2ad
18 changed files with 370 additions and 47 deletions

View File

@@ -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;

View File

@@ -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

View 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

View File

@@ -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"]) {

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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

View File

@@ -44,7 +44,7 @@
}
if (!chat) {
NSLog(@"Chat does not exist: %@", chatGUID);
MBIMLogInfo(@"Chat does not exist: %@", chatGUID);
result = NO;
} else {
[chat sendMessage:reply];

View File

@@ -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;
}

View File

@@ -13,7 +13,7 @@
BOOL HookIMAgent(const char *relativeDylibPath, char **errorString)
{
NSLog(@"Hooking imagent");
MBIMLogInfo(@"Hooking imagent");
const char *hookDylibPath = realpath(relativeDylibPath, NULL);
@@ -61,10 +61,10 @@ BOOL HookIMAgent(const char *relativeDylibPath, char **errorString)
return NO;
}
NSLog(@"Successfully setup environment variables");
MBIMLogInfo(@"Successfully setup environment variables");
// Kill imagent so the new one has the loaded bundle
NSLog(@"Killing imagent...");
MBIMLogInfo(@"Killing imagent...");
int killAgentSuccess = system("killall imagent");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

14
kordophone/KPServer.pch Normal file
View File

@@ -0,0 +1,14 @@
//
// KPServer.pch
// MessagesBridge
//
// Created by James Magahern on 1/22/19.
// Copyright © 2019 James Magahern. All rights reserved.
//
#ifndef KPServer_h
#define KPServer_h
#include "MBIMLogging.h"
#endif /* KPServer_h */

View File

@@ -0,0 +1,34 @@
//
// MBIMLogging.h
// kordophoned
//
// Created by James Magahern on 1/22/19.
// Copyright © 2019 James Magahern. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef enum {
ML_INFO,
ML_NOTIFY,
ML_ERROR,
ML_FATAL
} MBIMLogLevel;
extern void __MBIMLogCommon(MBIMLogLevel level, NSString *format, ...);
#define MBIMLogInfo(format, ...) \
__MBIMLogCommon(ML_INFO, format, ##__VA_ARGS__)
#define MBIMLogNotify(format, ...) \
__MBIMLogCommon(ML_NOTIFY, format, ##__VA_ARGS__)
#define MBIMLogError(format, ...) \
__MBIMLogCommon(ML_ERROR, format, ##__VA_ARGS__)
#define MBIMLogFatal(format, ...) \
__MBIMLogCommon(ML_FATAL, format, ##__VA_ARGS__)
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,44 @@
//
// MBIMLogging.m
// kordophoned
//
// Created by James Magahern on 1/22/19.
// Copyright © 2019 James Magahern. All rights reserved.
//
#import "MBIMLogging.h"
#define ESC(inner) "\033[" inner "m"
#define CLR ESC("0")
#define BLD "1;"
#define RED "31;"
extern void __MBIMLogCommon(MBIMLogLevel level, NSString *format, ...)
{
static dispatch_once_t onceToken;
static NSDateFormatter *dateFormatter = nil;
dispatch_once(&onceToken, ^{
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"Y-MM-d HH:mm:ss";
});
va_list args;
va_start(args, format);
NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
va_end(args);
const char *c_fmt = "%s";
if (level == ML_NOTIFY) {
// BOLD
c_fmt = ESC(BLD) "%s";
} else if (level == ML_ERROR) {
c_fmt = ESC(RED) "%s";
} else if (level == ML_FATAL) {
c_fmt = ESC(BLD RED) "%s";
}
NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];
fprintf(stdout, "%s: ", [dateStr UTF8String]);
fprintf(stdout, c_fmt, [message UTF8String]);
fprintf(stdout, CLR "\n");
}

View File

@@ -10,18 +10,62 @@
#import "MBIMBridge.h"
int main(int argc, const char * argv[]) {
void printUsage()
{
fprintf(stderr, "Usage: kordophoned [-h] [-s | -c (certificate.p12)]\n");
fprintf(stderr, "\t-h \t Show this help message\n");
fprintf(stderr, "\t-s \t Use SSL (requires -c option)\n");
fprintf(stderr, "\t-c \t SSL certificate path encoded as pkcs12\n");
}
int main(int argc, char *const argv[]) {
@autoreleasepool {
MBIMBridge *bridge = [MBIMBridge sharedInstance];
BOOL usesSSL = NO;
BOOL showHelp = NO;
const char *certPath = NULL;
#if HOOK_IMAGENT
if (argc < 2) {
fprintf(stderr, "Usage: kordophoned agentHook.dylib\n");
int c = -1;
while ( (c = getopt(argc, argv, "hsc:")) != -1 ) {
switch (c) {
case 's':
usesSSL = YES;
break;
case 'c':
certPath = optarg;
break;
case 'h':
showHelp = YES;
break;
case '?':
if (optopt == 'c') {
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
} else if (isprint(optopt)) {
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
} else {
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
return 1;
}
default:
abort ();
}
}
if (showHelp) {
printUsage();
return 1;
}
bridge.dylibPath = argv[1];
#endif
if (usesSSL && certPath == NULL) {
fprintf(stderr, "Error: wants SSL (-s) but no ssl certificate path (-c) provided\n");
return 1;
}
MBIMBridge *bridge = [MBIMBridge sharedInstance];
if (usesSSL && certPath != NULL) {
bridge.usesSSL = YES;
bridge.sslCertPath = [NSString stringWithCString:certPath encoding:NSASCIIStringEncoding];
}
[bridge connect];
@@ -30,5 +74,6 @@ int main(int argc, const char * argv[]) {
[[NSRunLoop currentRunLoop] run];
}
}
return 0;
}