From 719eb543a8d08c4f536ea7933ffb3af0a8553e87 Mon Sep 17 00:00:00 2001 From: Chris Ballinger Date: Fri, 8 Apr 2016 17:24:41 -0700 Subject: Xcode, podspec, wrapper --- xcode/OLMKit/Info.plist | 26 +++++++++ xcode/OLMKit/OLMAccount.h | 37 ++++++++++++ xcode/OLMKit/OLMAccount.m | 116 ++++++++++++++++++++++++++++++++++++++ xcode/OLMKit/OLMAccount_Private.h | 15 +++++ xcode/OLMKit/OLMKit.h | 23 ++++++++ xcode/OLMKit/OLMMessage.h | 24 ++++++++ xcode/OLMKit/OLMMessage.m | 13 +++++ xcode/OLMKit/OLMSerializable.h | 19 +++++++ xcode/OLMKit/OLMSession.h | 29 ++++++++++ xcode/OLMKit/OLMSession.m | 30 ++++++++++ xcode/OLMKit/OLMUtility.h | 15 +++++ xcode/OLMKit/OLMUtility.m | 28 +++++++++ 12 files changed, 375 insertions(+) create mode 100644 xcode/OLMKit/Info.plist create mode 100644 xcode/OLMKit/OLMAccount.h create mode 100644 xcode/OLMKit/OLMAccount.m create mode 100644 xcode/OLMKit/OLMAccount_Private.h create mode 100644 xcode/OLMKit/OLMKit.h create mode 100644 xcode/OLMKit/OLMMessage.h create mode 100644 xcode/OLMKit/OLMMessage.m create mode 100644 xcode/OLMKit/OLMSerializable.h create mode 100644 xcode/OLMKit/OLMSession.h create mode 100644 xcode/OLMKit/OLMSession.m create mode 100644 xcode/OLMKit/OLMUtility.h create mode 100644 xcode/OLMKit/OLMUtility.m (limited to 'xcode/OLMKit') diff --git a/xcode/OLMKit/Info.plist b/xcode/OLMKit/Info.plist new file mode 100644 index 0000000..d3de8ee --- /dev/null +++ b/xcode/OLMKit/Info.plist @@ -0,0 +1,26 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + FMWK + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + $(CURRENT_PROJECT_VERSION) + NSPrincipalClass + + + diff --git a/xcode/OLMKit/OLMAccount.h b/xcode/OLMKit/OLMAccount.h new file mode 100644 index 0000000..cfa7129 --- /dev/null +++ b/xcode/OLMKit/OLMAccount.h @@ -0,0 +1,37 @@ +// +// OLMAccount.h +// olm +// +// Created by Chris Ballinger on 4/8/16. +// +// + +#import +#import "OLMSerializable.h" + +@interface OLMAccount : NSObject + +/** Creates new account */ +- (instancetype) initNewAccount; + +/** public identity keys. base64 encoded in "curve25519" and "ed25519" keys */ +- (NSDictionary*) identityKeys; + +/** signs message with ed25519 key for account */ +- (NSData*) signMessage:(NSData*)messageData; + +/** Public parts of the unpublished one time keys for the account */ +- (NSDictionary*) oneTimeKeys; + +/** Marks the current set of one time keys as being published. */ +- (void) markKeysAsPublished; + +/** The largest number of one time keys this account can store. */ +- (NSUInteger) maxOneTimeKeys; + +/** Generates a number of new one time keys. If the total number of keys stored + * by this account exceeds -maxOneTimeKeys then the old keys are + * discarded. */ +- (void) generateOneTimeKeys:(NSUInteger)numberOfKeys; + +@end diff --git a/xcode/OLMKit/OLMAccount.m b/xcode/OLMKit/OLMAccount.m new file mode 100644 index 0000000..58dd4ad --- /dev/null +++ b/xcode/OLMKit/OLMAccount.m @@ -0,0 +1,116 @@ +// +// OLMAccount.m +// olm +// +// Created by Chris Ballinger on 4/8/16. +// +// + +#import "OLMAccount.h" +#import "OLMAccount_Private.h" +#import "OLMUtility.h" + +@import Security; + +@implementation OLMAccount + +- (void) dealloc { + olm_clear_account(_account); + free(_account); +} + +- (BOOL) initializeAccountMemory { + size_t accountSize = olm_account_size(); + _account = malloc(accountSize); + NSParameterAssert(_account != nil); + if (!_account) { + return NO; + } + _account = olm_account(_account); + NSParameterAssert(_account != nil); + if (!_account) { + return NO; + } + return YES; +} + +- (instancetype) initNewAccount { + self = [super init]; + if (!self) { + return nil; + } + BOOL success = [self initializeAccountMemory]; + if (!success) { + return nil; + } + size_t randomLength = olm_create_account_random_length(_account); + size_t accountResult = olm_create_account(_account, (void*)[OLMUtility randomBytesOfLength:randomLength].bytes, randomLength); + if (accountResult == olm_error()) { + const char *error = olm_account_last_error(_account); + NSLog(@"error creating account: %s", error); + return nil; + } + return self; +} + +- (size_t) maxOneTimeKeys { + return olm_account_max_number_of_one_time_keys(_account); +} + + +/** public identity keys */ +- (NSDictionary*) identityKeys { + size_t identityKeysLength = olm_account_identity_keys_length(_account); + uint8_t *identityKeysBytes = malloc(identityKeysLength); + if (!identityKeysBytes) { + return nil; + } + size_t result = olm_account_identity_keys(_account, identityKeysBytes, identityKeysLength); + if (result == olm_error()) { + const char *error = olm_account_last_error(_account); + NSLog(@"error getting id keys: %s", error); + free(identityKeysBytes); + return nil; + } + NSData *idKeyData = [NSData dataWithBytesNoCopy:identityKeysBytes length:identityKeysLength freeWhenDone:YES]; + NSError *error = nil; + NSDictionary *keysDictionary = [NSJSONSerialization JSONObjectWithData:idKeyData options:0 error:&error]; + if (error) { + NSLog(@"Could not decode JSON: %@", error.localizedDescription); + } + return keysDictionary; +} + +- (NSDictionary*) oneTimeKeys { + size_t otkLength = olm_account_one_time_keys_length(_account); + uint8_t *otkBytes = malloc(otkLength); + if (!otkBytes) { + return nil; + } + size_t result = olm_account_one_time_keys(_account, otkBytes, otkLength); + if (result == olm_error()) { + const char *error = olm_account_last_error(_account); + NSLog(@"error getting id keys: %s", error); + free(otkBytes); + } + NSData *otk = [NSData dataWithBytesNoCopy:otkBytes length:otkLength freeWhenDone:YES]; + NSError *error = nil; + NSDictionary *keysDictionary = [NSJSONSerialization JSONObjectWithData:otk options:0 error:&error]; + if (error) { + NSLog(@"Could not decode JSON: %@", error.localizedDescription); + } + return keysDictionary; +} + + +- (void) generateOneTimeKeys:(NSUInteger)numberOfKeys { + size_t randomLength = olm_account_generate_one_time_keys_random_length(_account, numberOfKeys); + size_t result = olm_account_generate_one_time_keys(_account, numberOfKeys, (void*)[OLMUtility randomBytesOfLength:randomLength].bytes, randomLength); + if (result == olm_error()) { + const char *error = olm_account_last_error(_account); + NSLog(@"error generating keys: %s", error); + } +} + + +@end diff --git a/xcode/OLMKit/OLMAccount_Private.h b/xcode/OLMKit/OLMAccount_Private.h new file mode 100644 index 0000000..4eb3e2b --- /dev/null +++ b/xcode/OLMKit/OLMAccount_Private.h @@ -0,0 +1,15 @@ +// +// OLMAccount_Private.h +// olm +// +// Created by Chris Ballinger on 4/8/16. +// +// + +@import olm; + +@interface OLMAccount() + +@property (nonatomic) OlmAccount *account; + +@end \ No newline at end of file diff --git a/xcode/OLMKit/OLMKit.h b/xcode/OLMKit/OLMKit.h new file mode 100644 index 0000000..745af43 --- /dev/null +++ b/xcode/OLMKit/OLMKit.h @@ -0,0 +1,23 @@ +// +// OLMKit.h +// OLMKit +// +// Created by Chris Ballinger on 4/8/16. +// +// + +#import + +//! Project version number for OLMKit. +FOUNDATION_EXPORT double OLMKitVersionNumber; + +//! Project version string for OLMKit. +FOUNDATION_EXPORT const unsigned char OLMKitVersionString[]; + +// In this header, you should import all the public headers of your framework using statements like #import + + +#import "OLMAccount.h" +#import "OLMSession.h" +#import "OLMMessage.h" +#import "OLMUtility.h" \ No newline at end of file diff --git a/xcode/OLMKit/OLMMessage.h b/xcode/OLMKit/OLMMessage.h new file mode 100644 index 0000000..2b747fb --- /dev/null +++ b/xcode/OLMKit/OLMMessage.h @@ -0,0 +1,24 @@ +// +// OLMMessage.h +// olm +// +// Created by Chris Ballinger on 4/8/16. +// +// + +#import + +typedef NS_ENUM(NSUInteger, OLMMessageType) { + OLMMessageTypeUnknown, + OLMMessageTypePreKey, + OLMMessageTypeMessage +}; + +@interface OLMMessage : NSObject + +@property (nonatomic, readonly, nonnull) NSString *message; +@property (readonly) OLMMessageType type; + +- (nonnull instancetype) initWithMessage:(nonnull NSString*)message type:(OLMMessageType)type; + +@end diff --git a/xcode/OLMKit/OLMMessage.m b/xcode/OLMKit/OLMMessage.m new file mode 100644 index 0000000..ce732ec --- /dev/null +++ b/xcode/OLMKit/OLMMessage.m @@ -0,0 +1,13 @@ +// +// OLMMessage.m +// olm +// +// Created by Chris Ballinger on 4/8/16. +// +// + +#import "OLMMessage.h" + +@implementation OLMMessage + +@end diff --git a/xcode/OLMKit/OLMSerializable.h b/xcode/OLMKit/OLMSerializable.h new file mode 100644 index 0000000..afacdaa --- /dev/null +++ b/xcode/OLMKit/OLMSerializable.h @@ -0,0 +1,19 @@ +// +// OLMSerializable.h +// olm +// +// Created by Chris Ballinger on 4/8/16. +// +// + +#import + +@protocol OLMSerializable + +/** Initializes from encrypted serialized data. Will throw error if invalid key or invalid base64. */ +- (instancetype) initWithSerializedData:(NSData*)serializedData key:(NSData*)key error:(NSError**)error; + +/** Serializes and encrypts object data */ +- (NSData*) serializeDataWithKey:(NSData*)key; + +@end diff --git a/xcode/OLMKit/OLMSession.h b/xcode/OLMKit/OLMSession.h new file mode 100644 index 0000000..196900f --- /dev/null +++ b/xcode/OLMKit/OLMSession.h @@ -0,0 +1,29 @@ +// +// OLMSession.h +// olm +// +// Created by Chris Ballinger on 4/8/16. +// +// + +#import +#import "OLMSerializable.h" +#import "OLMAccount.h" + +@interface OLMSession : NSObject + +- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSData*)theirIdentityKey theirOneTimeKey:(NSData*)theirOneTimeKey; + +- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSData*)oneTimeKeyMessage; + +- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSData*)theirIdentityKey oneTimeKeyMessage:(NSData*)oneTimeKeyMessage; + +- (NSData*) sessionIdentifier; + +- (BOOL) matchesInboundSession:(NSData*)oneTimeKeyMessage; + +- (BOOL) matchesInboundSessionFrom:(NSData*)theirIdentityKey oneTimeKeyMessage:(NSData *)oneTimeKeyMessage; + +- (void) removeOneTimeKeys; + +@end diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m new file mode 100644 index 0000000..24a8b36 --- /dev/null +++ b/xcode/OLMKit/OLMSession.m @@ -0,0 +1,30 @@ +// +// OLMSession.m +// olm +// +// Created by Chris Ballinger on 4/8/16. +// +// + +#import "OLMSession.h" +@import olm; + +@interface OLMSession() +@property (nonatomic) OlmSession *session; +@end + +@implementation OLMSession + +- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSData*)theirIdentityKey theirOneTimeKey:(NSData*)theirOneTimeKey { + +} + +- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSData*)oneTimeKeyMessage { + +} + +- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSData*)theirIdentityKey oneTimeKeyMessage:(NSData*)oneTimeKeyMessage { + +} + +@end diff --git a/xcode/OLMKit/OLMUtility.h b/xcode/OLMKit/OLMUtility.h new file mode 100644 index 0000000..0de9725 --- /dev/null +++ b/xcode/OLMKit/OLMUtility.h @@ -0,0 +1,15 @@ +// +// OLMUtility.h +// olm +// +// Created by Chris Ballinger on 4/8/16. +// +// + +#import + +@interface OLMUtility : NSObject + ++ (NSData*) randomBytesOfLength:(NSUInteger)length; + +@end diff --git a/xcode/OLMKit/OLMUtility.m b/xcode/OLMKit/OLMUtility.m new file mode 100644 index 0000000..0148932 --- /dev/null +++ b/xcode/OLMKit/OLMUtility.m @@ -0,0 +1,28 @@ +// +// OLMUtility.m +// olm +// +// Created by Chris Ballinger on 4/8/16. +// +// + +#import "OLMUtility.h" + +@implementation OLMUtility + ++ (NSData*) randomBytesOfLength:(NSUInteger)length { + uint8_t *randomBytes = malloc(length * sizeof(uint8_t)); + NSParameterAssert(randomBytes != NULL); + if (!randomBytes) { + return nil; + } + int result = SecRandomCopyBytes(kSecRandomDefault, length, randomBytes); + if (result != 0) { + free(randomBytes); + return nil; + } + NSData *data = [NSData dataWithBytesNoCopy:randomBytes length:length freeWhenDone:YES]; + return data; +} + +@end -- cgit v1.2.3