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/OLMSession.m | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 xcode/OLMKit/OLMSession.m (limited to 'xcode/OLMKit/OLMSession.m') 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 -- cgit v1.2.3 From f505113fb7a6d61015ad8050b3fb4e26df029150 Mon Sep 17 00:00:00 2001 From: Chris Ballinger Date: Sat, 9 Apr 2016 14:00:30 -0700 Subject: Initial test passing --- xcode/OLMKit/OLMSession.m | 165 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 159 insertions(+), 6 deletions(-) (limited to 'xcode/OLMKit/OLMSession.m') diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m index 24a8b36..fa7cb62 100644 --- a/xcode/OLMKit/OLMSession.m +++ b/xcode/OLMKit/OLMSession.m @@ -7,6 +7,8 @@ // #import "OLMSession.h" +#import "OLMUtility.h" +#import "OLMAccount_Private.h" @import olm; @interface OLMSession() @@ -15,16 +17,167 @@ @implementation OLMSession -- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSData*)theirIdentityKey theirOneTimeKey:(NSData*)theirOneTimeKey { - +- (void) dealloc { + olm_clear_session(_session); + free(_session); } -- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSData*)oneTimeKeyMessage { - +- (BOOL) initializeSessionMemory { + size_t size = olm_session_size(); + _session = malloc(size); + NSParameterAssert(_session != nil); + if (!_session) { + return NO; + } + _session = olm_session(_session); + NSParameterAssert(_session != nil); + if (!_session) { + return NO; + } + return YES; } -- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSData*)theirIdentityKey oneTimeKeyMessage:(NSData*)oneTimeKeyMessage { - +- (instancetype) initWithAccount:(OLMAccount*)account { + self = [super init]; + if (!self) { + return nil; + } + BOOL success = [self initializeSessionMemory]; + if (!success) { + return nil; + } + _account = account; + return self; +} + +- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey theirOneTimeKey:(NSString*)theirOneTimeKey { + self = [self initWithAccount:account]; + if (!self) { + return nil; + } + NSMutableData *random = [OLMUtility randomBytesOfLength:olm_create_outbound_session_random_length(_session)]; + NSData *idKey = [theirIdentityKey dataUsingEncoding:NSUTF8StringEncoding]; + NSData *otKey = [theirOneTimeKey dataUsingEncoding:NSUTF8StringEncoding]; + size_t result = olm_create_outbound_session(_session, account.account, idKey.bytes, idKey.length, otKey.bytes, otKey.length, random.mutableBytes, random.length); + if (result == olm_error()) { + const char *error = olm_session_last_error(_session); + NSAssert(NO, @"olm_create_outbound_session error: %s", error); + return nil; + } + return self; +} + +- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSString*)oneTimeKeyMessage { + self = [self initWithAccount:account]; + if (!self) { + return nil; + } + BOOL success = [self initializeSessionMemory]; + if (!success) { + return nil; + } + NSMutableData *otk = [NSMutableData dataWithData:[oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]]; + size_t result = olm_create_inbound_session(_session, account.account, otk.mutableBytes, oneTimeKeyMessage.length); + if (result == olm_error()) { + const char *error = olm_session_last_error(_session); + NSAssert(NO, @"olm_create_inbound_session error: %s", error); + return nil; + } + return self; +} + +- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString*)oneTimeKeyMessage { + self = [self initWithAccount:account]; + if (!self) { + return nil; + } + BOOL success = [self initializeSessionMemory]; + if (!success) { + return nil; + } + NSData *idKey = [theirIdentityKey dataUsingEncoding:NSUTF8StringEncoding]; + NSMutableData *otk = [NSMutableData dataWithData:[oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]]; + size_t result = olm_create_inbound_session_from(_session, account.account, idKey.bytes, idKey.length, otk.mutableBytes, otk.length); + if (result == olm_error()) { + const char *error = olm_session_last_error(_session); + NSAssert(NO, @"olm_create_inbound_session_from error: %s", error); + return nil; + } + return self; +} + +- (NSString*) sessionIdentifier { + size_t length = olm_session_id_length(_session); + NSMutableData *idData = [NSMutableData dataWithLength:length]; + if (!idData) { + return nil; + } + size_t result = olm_session_id(_session, idData.mutableBytes, idData.length); + if (result == olm_error()) { + const char *error = olm_session_last_error(_session); + NSAssert(NO, @"olm_session_id error: %s", error); + return nil; + } + NSString *idString = [[NSString alloc] initWithData:idData encoding:NSUTF8StringEncoding]; + return idString; +} + +- (OLMMessage*) encryptMessage:(NSString*)message { + size_t messageType = olm_encrypt_message_type(_session); + size_t randomLength = olm_encrypt_random_length(_session); + NSMutableData *random = [OLMUtility randomBytesOfLength:randomLength]; + NSData *plaintextData = [message dataUsingEncoding:NSUTF8StringEncoding]; + size_t ciphertextLength = olm_encrypt_message_length(_session, plaintextData.length); + NSMutableData *ciphertext = [NSMutableData dataWithLength:ciphertextLength]; + if (!ciphertext) { + return nil; + } + size_t result = olm_encrypt(_session, plaintextData.bytes, plaintextData.length, random.mutableBytes, random.length, ciphertext.mutableBytes, ciphertext.length); + if (result == olm_error()) { + const char *error = olm_session_last_error(_session); + NSAssert(NO, @"olm_encrypt error: %s", error); + return nil; + } + NSString *ciphertextString = [[NSString alloc] initWithData:ciphertext encoding:NSUTF8StringEncoding]; + OLMMessage *encryptedMessage = [[OLMMessage alloc] initWithCiphertext:ciphertextString type:messageType]; + return encryptedMessage; +} + +- (BOOL) removeOneTimeKeys { + size_t result = olm_remove_one_time_keys(_account.account, _session); + if (result == olm_error()) { + const char *error = olm_session_last_error(_session); + NSAssert(NO, @"olm_remove_one_time_keys error: %s", error); + return NO; + } + return YES; +} + +- (NSString*) decryptMessage:(OLMMessage*)message { + NSParameterAssert(message != nil); + NSData *messageData = [message.ciphertext dataUsingEncoding:NSUTF8StringEncoding]; + if (!messageData) { + return nil; + } + NSMutableData *mutMessage = messageData.mutableCopy; + size_t maxPlaintextLength = olm_decrypt_max_plaintext_length(_session, message.type, mutMessage.mutableBytes, mutMessage.length); + if (maxPlaintextLength == olm_error()) { + const char *error = olm_session_last_error(_session); + NSAssert(NO, @"olm_decrypt_max_plaintext_length error: %s", error); + return nil; + } + // message buffer is destroyed by olm_decrypt_max_plaintext_length + mutMessage = messageData.mutableCopy; + NSMutableData *plaintextData = [NSMutableData dataWithLength:maxPlaintextLength]; + size_t plaintextLength = olm_decrypt(_session, message.type, mutMessage.mutableBytes, mutMessage.length, plaintextData.mutableBytes, plaintextData.length); + if (plaintextLength == olm_error()) { + const char *error = olm_session_last_error(_session); + NSAssert(NO, @"olm_decrypt error: %s", error); + return nil; + } + plaintextData.length = plaintextLength; + NSString *plaintext = [[NSString alloc] initWithData:plaintextData encoding:NSUTF8StringEncoding]; + return plaintext; } @end -- cgit v1.2.3 From daab2a58af947cddd67fe9f30dd3a9fc327650c0 Mon Sep 17 00:00:00 2001 From: Chris Ballinger Date: Wed, 13 Apr 2016 16:53:47 -0700 Subject: OLMAccount and OLMSession serialization --- xcode/OLMKit/OLMSession.m | 124 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 101 insertions(+), 23 deletions(-) (limited to 'xcode/OLMKit/OLMSession.m') diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m index fa7cb62..119079f 100644 --- a/xcode/OLMKit/OLMSession.m +++ b/xcode/OLMKit/OLMSession.m @@ -9,12 +9,9 @@ #import "OLMSession.h" #import "OLMUtility.h" #import "OLMAccount_Private.h" +#import "OLMSession_Private.h" @import olm; -@interface OLMSession() -@property (nonatomic) OlmSession *session; -@end - @implementation OLMSession - (void) dealloc { @@ -37,7 +34,7 @@ return YES; } -- (instancetype) initWithAccount:(OLMAccount*)account { +- (instancetype) init { self = [super init]; if (!self) { return nil; @@ -46,6 +43,18 @@ if (!success) { return nil; } + return self; +} + +- (instancetype) initWithAccount:(OLMAccount*)account { + self = [self init]; + if (!self) { + return nil; + } + NSParameterAssert(account != nil && account.account != NULL); + if (account == nil || account.account == NULL) { + return nil; + } _account = account; return self; } @@ -72,10 +81,6 @@ if (!self) { return nil; } - BOOL success = [self initializeSessionMemory]; - if (!success) { - return nil; - } NSMutableData *otk = [NSMutableData dataWithData:[oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]]; size_t result = olm_create_inbound_session(_session, account.account, otk.mutableBytes, oneTimeKeyMessage.length); if (result == olm_error()) { @@ -91,10 +96,6 @@ if (!self) { return nil; } - BOOL success = [self initializeSessionMemory]; - if (!success) { - return nil; - } NSData *idKey = [theirIdentityKey dataUsingEncoding:NSUTF8StringEncoding]; NSMutableData *otk = [NSMutableData dataWithData:[oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]]; size_t result = olm_create_inbound_session_from(_session, account.account, idKey.bytes, idKey.length, otk.mutableBytes, otk.length); @@ -143,16 +144,6 @@ return encryptedMessage; } -- (BOOL) removeOneTimeKeys { - size_t result = olm_remove_one_time_keys(_account.account, _session); - if (result == olm_error()) { - const char *error = olm_session_last_error(_session); - NSAssert(NO, @"olm_remove_one_time_keys error: %s", error); - return NO; - } - return YES; -} - - (NSString*) decryptMessage:(OLMMessage*)message { NSParameterAssert(message != nil); NSData *messageData = [message.ciphertext dataUsingEncoding:NSUTF8StringEncoding]; @@ -180,4 +171,91 @@ return plaintext; } +#pragma mark OLMSerializable + +/** Initializes from encrypted serialized data. Will throw error if invalid key or invalid base64. */ +- (instancetype) initWithSerializedData:(NSString*)serializedData key:(NSData*)key error:(NSError**)error { + self = [self init]; + if (!self) { + return nil; + } + NSParameterAssert(key.length > 0); + NSParameterAssert(serializedData.length > 0); + if (key.length == 0 || serializedData.length == 0) { + if (error) { + *error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}]; + } + return nil; + } + NSMutableData *pickle = [serializedData dataUsingEncoding:NSUTF8StringEncoding].mutableCopy; + size_t result = olm_unpickle_session(_session, key.bytes, key.length, pickle.mutableBytes, pickle.length); + if (result == olm_error()) { + const char *olm_error = olm_session_last_error(_session); + NSString *errorString = [NSString stringWithUTF8String:olm_error]; + if (error && errorString) { + *error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; + } + return nil; + } + return self; +} + +/** Serializes and encrypts object data, outputs base64 blob */ +- (NSString*) serializeDataWithKey:(NSData*)key error:(NSError**)error { + NSParameterAssert(key.length > 0); + size_t length = olm_pickle_session_length(_session); + NSMutableData *pickled = [NSMutableData dataWithLength:length]; + size_t result = olm_pickle_session(_session, key.bytes, key.length, pickled.mutableBytes, pickled.length); + if (result == olm_error()) { + const char *olm_error = olm_session_last_error(_session); + NSString *errorString = [NSString stringWithUTF8String:olm_error]; + if (error && errorString) { + *error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; + } + return nil; + } + NSString *pickleString = [[NSString alloc] initWithData:pickled encoding:NSUTF8StringEncoding]; + return pickleString; +} + +#pragma mark NSSecureCoding + ++ (BOOL) supportsSecureCoding { + return YES; +} + +#pragma mark NSCoding + +- (id)initWithCoder:(NSCoder *)decoder { + NSString *version = [decoder decodeObjectOfClass:[NSString class] forKey:@"version"]; + + NSError *error = nil; + + if ([version isEqualToString:@"1"]) { + NSString *pickle = [decoder decodeObjectOfClass:[NSString class] forKey:@"pickle"]; + NSData *key = [decoder decodeObjectOfClass:[NSData class] forKey:@"key"]; + + self = [self initWithSerializedData:pickle key:key error:&error]; + } + + NSParameterAssert(error == nil); + NSParameterAssert(self != nil); + if (!self) { + return nil; + } + + return self; +} + +- (void)encodeWithCoder:(NSCoder *)encoder { + NSData *key = [OLMUtility randomBytesOfLength:32]; + NSError *error = nil; + NSString *pickle = [self serializeDataWithKey:key error:&error]; + NSParameterAssert(pickle.length > 0 && error == nil); + + [encoder encodeObject:pickle forKey:@"pickle"]; + [encoder encodeObject:key forKey:@"key"]; + [encoder encodeObject:@"1" forKey:@"version"]; +} + @end -- cgit v1.2.3 From 6f113dd7b3b4de918c4efb81d38a1ffe1d391b5b Mon Sep 17 00:00:00 2001 From: manuroe Date: Tue, 27 Sep 2016 11:57:29 +0200 Subject: OLMKit: Make the project build Make OLMKit CocoaPods expose the obj-c wrapper of libolm --- xcode/OLMKit/OLMSession.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xcode/OLMKit/OLMSession.m') diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m index 119079f..41aef7e 100644 --- a/xcode/OLMKit/OLMSession.m +++ b/xcode/OLMKit/OLMSession.m @@ -10,7 +10,7 @@ #import "OLMUtility.h" #import "OLMAccount_Private.h" #import "OLMSession_Private.h" -@import olm; +#include "olm/olm.h" @implementation OLMSession -- cgit v1.2.3 From cf66af6f2e7c69a3e0712317f8473ab09711d426 Mon Sep 17 00:00:00 2001 From: manuroe Date: Mon, 14 Nov 2016 16:54:51 +0100 Subject: OLMKit: Replaced NSAsserts by NSErrors --- xcode/OLMKit/OLMSession.m | 108 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 87 insertions(+), 21 deletions(-) (limited to 'xcode/OLMKit/OLMSession.m') diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m index 41aef7e..a47deb1 100644 --- a/xcode/OLMKit/OLMSession.m +++ b/xcode/OLMKit/OLMSession.m @@ -59,7 +59,7 @@ return self; } -- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey theirOneTimeKey:(NSString*)theirOneTimeKey { +- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey theirOneTimeKey:(NSString*)theirOneTimeKey error:(NSError**)error { self = [self initWithAccount:account]; if (!self) { return nil; @@ -69,14 +69,25 @@ NSData *otKey = [theirOneTimeKey dataUsingEncoding:NSUTF8StringEncoding]; size_t result = olm_create_outbound_session(_session, account.account, idKey.bytes, idKey.length, otKey.bytes, otKey.length, random.mutableBytes, random.length); if (result == olm_error()) { - const char *error = olm_session_last_error(_session); - NSAssert(NO, @"olm_create_outbound_session error: %s", error); + const char *olm_error = olm_session_last_error(_session); + + NSString *errorString = [NSString stringWithUTF8String:olm_error]; + NSLog(@"olm_create_outbound_session error: %@", errorString); + + if (error && olm_error && errorString) { + *error = [NSError errorWithDomain:OLMErrorDomain + code:0 + userInfo:@{ + NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_create_outbound_session error: %@", errorString] + }]; + } + return nil; } return self; } -- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSString*)oneTimeKeyMessage { +- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSString*)oneTimeKeyMessage error:(NSError**)error { self = [self initWithAccount:account]; if (!self) { return nil; @@ -84,14 +95,25 @@ NSMutableData *otk = [NSMutableData dataWithData:[oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]]; size_t result = olm_create_inbound_session(_session, account.account, otk.mutableBytes, oneTimeKeyMessage.length); if (result == olm_error()) { - const char *error = olm_session_last_error(_session); - NSAssert(NO, @"olm_create_inbound_session error: %s", error); + const char *olm_error = olm_session_last_error(_session); + + NSString *errorString = [NSString stringWithUTF8String:olm_error]; + NSLog(@"olm_create_inbound_session error: %@", errorString); + + if (error && olm_error && errorString) { + *error = [NSError errorWithDomain:OLMErrorDomain + code:0 + userInfo:@{ + NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_create_inbound_session error: %@", errorString] + }]; + } + return nil; } return self; } -- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString*)oneTimeKeyMessage { +- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString*)oneTimeKeyMessage error:(NSError**)error { self = [self initWithAccount:account]; if (!self) { return nil; @@ -100,8 +122,19 @@ NSMutableData *otk = [NSMutableData dataWithData:[oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]]; size_t result = olm_create_inbound_session_from(_session, account.account, idKey.bytes, idKey.length, otk.mutableBytes, otk.length); if (result == olm_error()) { - const char *error = olm_session_last_error(_session); - NSAssert(NO, @"olm_create_inbound_session_from error: %s", error); + const char *olm_error = olm_session_last_error(_session); + + NSString *errorString = [NSString stringWithUTF8String:olm_error]; + NSLog(@"olm_create_inbound_session_from error: %@", errorString); + + if (error && olm_error && errorString) { + *error = [NSError errorWithDomain:OLMErrorDomain + code:0 + userInfo:@{ + NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_create_inbound_session_from error: %@", errorString] + }]; + } + return nil; } return self; @@ -116,14 +149,14 @@ size_t result = olm_session_id(_session, idData.mutableBytes, idData.length); if (result == olm_error()) { const char *error = olm_session_last_error(_session); - NSAssert(NO, @"olm_session_id error: %s", error); + NSLog(@"olm_session_id error: %s", error); return nil; } NSString *idString = [[NSString alloc] initWithData:idData encoding:NSUTF8StringEncoding]; return idString; } -- (OLMMessage*) encryptMessage:(NSString*)message { +- (OLMMessage*) encryptMessage:(NSString*)message error:(NSError**)error { size_t messageType = olm_encrypt_message_type(_session); size_t randomLength = olm_encrypt_random_length(_session); NSMutableData *random = [OLMUtility randomBytesOfLength:randomLength]; @@ -135,8 +168,19 @@ } size_t result = olm_encrypt(_session, plaintextData.bytes, plaintextData.length, random.mutableBytes, random.length, ciphertext.mutableBytes, ciphertext.length); if (result == olm_error()) { - const char *error = olm_session_last_error(_session); - NSAssert(NO, @"olm_encrypt error: %s", error); + const char *olm_error = olm_session_last_error(_session); + + NSString *errorString = [NSString stringWithUTF8String:olm_error]; + NSLog(@"olm_encrypt error: %@", errorString); + + if (error && olm_error && errorString) { + *error = [NSError errorWithDomain:OLMErrorDomain + code:0 + userInfo:@{ + NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_encrypt error: %@", errorString] + }]; + } + return nil; } NSString *ciphertextString = [[NSString alloc] initWithData:ciphertext encoding:NSUTF8StringEncoding]; @@ -144,7 +188,7 @@ return encryptedMessage; } -- (NSString*) decryptMessage:(OLMMessage*)message { +- (NSString*) decryptMessage:(OLMMessage*)message error:(NSError**)error { NSParameterAssert(message != nil); NSData *messageData = [message.ciphertext dataUsingEncoding:NSUTF8StringEncoding]; if (!messageData) { @@ -153,8 +197,19 @@ NSMutableData *mutMessage = messageData.mutableCopy; size_t maxPlaintextLength = olm_decrypt_max_plaintext_length(_session, message.type, mutMessage.mutableBytes, mutMessage.length); if (maxPlaintextLength == olm_error()) { - const char *error = olm_session_last_error(_session); - NSAssert(NO, @"olm_decrypt_max_plaintext_length error: %s", error); + const char *olm_error = olm_session_last_error(_session); + + NSString *errorString = [NSString stringWithUTF8String:olm_error]; + NSLog(@"olm_decrypt_max_plaintext_length error: %@", errorString); + + if (error && olm_error && errorString) { + *error = [NSError errorWithDomain:OLMErrorDomain + code:0 + userInfo:@{ + NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_decrypt_max_plaintext_length error: %@", errorString] + }]; + } + return nil; } // message buffer is destroyed by olm_decrypt_max_plaintext_length @@ -162,8 +217,19 @@ NSMutableData *plaintextData = [NSMutableData dataWithLength:maxPlaintextLength]; size_t plaintextLength = olm_decrypt(_session, message.type, mutMessage.mutableBytes, mutMessage.length, plaintextData.mutableBytes, plaintextData.length); if (plaintextLength == olm_error()) { - const char *error = olm_session_last_error(_session); - NSAssert(NO, @"olm_decrypt error: %s", error); + const char *olm_error = olm_session_last_error(_session); + + NSString *errorString = [NSString stringWithUTF8String:olm_error]; + NSLog(@"olm_decrypt error: %@", errorString); + + if (error && olm_error && errorString) { + *error = [NSError errorWithDomain:OLMErrorDomain + code:0 + userInfo:@{ + NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_decrypt error: %@", errorString] + }]; + } + return nil; } plaintextData.length = plaintextLength; @@ -183,7 +249,7 @@ NSParameterAssert(serializedData.length > 0); if (key.length == 0 || serializedData.length == 0) { if (error) { - *error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}]; + *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}]; } return nil; } @@ -193,7 +259,7 @@ const char *olm_error = olm_session_last_error(_session); NSString *errorString = [NSString stringWithUTF8String:olm_error]; if (error && errorString) { - *error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; + *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; } return nil; } @@ -210,7 +276,7 @@ const char *olm_error = olm_session_last_error(_session); NSString *errorString = [NSString stringWithUTF8String:olm_error]; if (error && errorString) { - *error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; + *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; } return nil; } -- cgit v1.2.3 From 7ee17a295738f0db8cab74cec2343a577f2ade45 Mon Sep 17 00:00:00 2001 From: manuroe Date: Mon, 14 Nov 2016 17:35:24 +0100 Subject: OLMKit: Add missing implementations for matchesInboundSession matchesInboundSessionFrom --- xcode/OLMKit/OLMSession.m | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'xcode/OLMKit/OLMSession.m') diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m index a47deb1..eee65a9 100644 --- a/xcode/OLMKit/OLMSession.m +++ b/xcode/OLMKit/OLMSession.m @@ -156,6 +156,41 @@ return idString; } +- (BOOL)matchesInboundSession:(NSString *)oneTimeKeyMessage { + NSData *otk = [oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]; + + size_t result = olm_matches_inbound_session(_session, otk.bytes, otk.length); + if (result == 1) { + return YES; + } + else { + if (result == olm_error()) { + const char *error = olm_session_last_error(_session); + NSLog(@"olm_matches_inbound_session error: %s", error); + } + return NO; + } +} + +- (BOOL)matchesInboundSessionFrom:(NSString *)theirIdentityKey oneTimeKeyMessage:(NSString *)oneTimeKeyMessage { + NSData *idKey = [theirIdentityKey dataUsingEncoding:NSUTF8StringEncoding]; + NSData *otk = [oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]; + + size_t result = olm_matches_inbound_session_from(_session, + idKey.bytes, idKey.length, + otk.bytes, otk.length); + if (result == 1) { + return YES; + } + else { + if (result == olm_error()) { + const char *error = olm_session_last_error(_session); + NSLog(@"olm_matches_inbound_session error: %s", error); + } + return NO; + } +} + - (OLMMessage*) encryptMessage:(NSString*)message error:(NSError**)error { size_t messageType = olm_encrypt_message_type(_session); size_t randomLength = olm_encrypt_random_length(_session); -- cgit v1.2.3 From 29de7825c9607955d061c5fe75c7f29d78dfaec5 Mon Sep 17 00:00:00 2001 From: manuroe Date: Thu, 17 Nov 2016 15:50:23 +0100 Subject: OLMKit: Update Copyrights --- xcode/OLMKit/OLMSession.m | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'xcode/OLMKit/OLMSession.m') diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m index eee65a9..e50da75 100644 --- a/xcode/OLMKit/OLMSession.m +++ b/xcode/OLMKit/OLMSession.m @@ -1,10 +1,20 @@ -// -// OLMSession.m -// olm -// -// Created by Chris Ballinger on 4/8/16. -// -// +/* + Copyright 2016 Chris Ballinger + Copyright 2016 OpenMarket Ltd + Copyright 2016 Vector Creations Ltd + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ #import "OLMSession.h" #import "OLMUtility.h" -- cgit v1.2.3 From 93926e90477355c671b50704711a58889832b077 Mon Sep 17 00:00:00 2001 From: manuroe Date: Fri, 18 Nov 2016 11:39:39 +0100 Subject: OLMKit: Fixed warnings in objc wrapper --- xcode/OLMKit/OLMSession.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'xcode/OLMKit/OLMSession.m') diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m index e50da75..3801e08 100644 --- a/xcode/OLMKit/OLMSession.m +++ b/xcode/OLMKit/OLMSession.m @@ -167,9 +167,9 @@ } - (BOOL)matchesInboundSession:(NSString *)oneTimeKeyMessage { - NSData *otk = [oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]; + NSMutableData *otk = [NSMutableData dataWithData:[oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]]; - size_t result = olm_matches_inbound_session(_session, otk.bytes, otk.length); + size_t result = olm_matches_inbound_session(_session, otk.mutableBytes, otk.length); if (result == 1) { return YES; } @@ -184,11 +184,11 @@ - (BOOL)matchesInboundSessionFrom:(NSString *)theirIdentityKey oneTimeKeyMessage:(NSString *)oneTimeKeyMessage { NSData *idKey = [theirIdentityKey dataUsingEncoding:NSUTF8StringEncoding]; - NSData *otk = [oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]; + NSMutableData *otk = [NSMutableData dataWithData:[oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]]; size_t result = olm_matches_inbound_session_from(_session, idKey.bytes, idKey.length, - otk.bytes, otk.length); + otk.mutableBytes, otk.length); if (result == 1) { return YES; } -- cgit v1.2.3 From aa12cbcac2d9f380847644febdf1f13f102cebb1 Mon Sep 17 00:00:00 2001 From: manuroe Date: Thu, 24 Nov 2016 11:45:59 +0100 Subject: OLMKit: Make returned NSError provide the raw olm error string (ex:"UNKNOWN_MESSAGE_INDEX") in their NSLocalizedDescriptionKey. NSLocalizedFailureReasonErrorKey can contain more contextual information. --- xcode/OLMKit/OLMSession.m | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'xcode/OLMKit/OLMSession.m') diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m index 3801e08..7cbd358 100644 --- a/xcode/OLMKit/OLMSession.m +++ b/xcode/OLMKit/OLMSession.m @@ -88,7 +88,8 @@ *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{ - NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_create_outbound_session error: %@", errorString] + NSLocalizedDescriptionKey: errorString, + NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_create_outbound_session error: %@", errorString] }]; } @@ -114,7 +115,8 @@ *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{ - NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_create_inbound_session error: %@", errorString] + NSLocalizedDescriptionKey: errorString, + NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_create_inbound_session error: %@", errorString] }]; } @@ -141,7 +143,8 @@ *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{ - NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_create_inbound_session_from error: %@", errorString] + NSLocalizedDescriptionKey: errorString, + NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_create_inbound_session_from error: %@", errorString] }]; } @@ -222,7 +225,8 @@ *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{ - NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_encrypt error: %@", errorString] + NSLocalizedDescriptionKey: errorString, + NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_encrypt error: %@", errorString] }]; } @@ -251,7 +255,8 @@ *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{ - NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_decrypt_max_plaintext_length error: %@", errorString] + NSLocalizedDescriptionKey: errorString, + NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_decrypt_max_plaintext_length error: %@", errorString] }]; } @@ -271,7 +276,8 @@ *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{ - NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_decrypt error: %@", errorString] + NSLocalizedDescriptionKey: errorString, + NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_decrypt error: %@", errorString] }]; } -- cgit v1.2.3 From 3540926b98813e5e5daed709f820f06f6f9ac2ae Mon Sep 17 00:00:00 2001 From: manuroe Date: Mon, 19 Dec 2016 18:10:37 +0100 Subject: OLMKit: Reset intermediate buffers to zeroes --- xcode/OLMKit/OLMSession.m | 2 ++ 1 file changed, 2 insertions(+) (limited to 'xcode/OLMKit/OLMSession.m') diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m index 7cbd358..2111c1c 100644 --- a/xcode/OLMKit/OLMSession.m +++ b/xcode/OLMKit/OLMSession.m @@ -78,6 +78,7 @@ NSData *idKey = [theirIdentityKey dataUsingEncoding:NSUTF8StringEncoding]; NSData *otKey = [theirOneTimeKey dataUsingEncoding:NSUTF8StringEncoding]; size_t result = olm_create_outbound_session(_session, account.account, idKey.bytes, idKey.length, otKey.bytes, otKey.length, random.mutableBytes, random.length); + [random resetBytesInRange:NSMakeRange(0, random.length)]; if (result == olm_error()) { const char *olm_error = olm_session_last_error(_session); @@ -215,6 +216,7 @@ return nil; } size_t result = olm_encrypt(_session, plaintextData.bytes, plaintextData.length, random.mutableBytes, random.length, ciphertext.mutableBytes, ciphertext.length); + [random resetBytesInRange:NSMakeRange(0, random.length)]; if (result == olm_error()) { const char *olm_error = olm_session_last_error(_session); -- cgit v1.2.3 From 46ad79517ec8e005bd2d1de767d3cd59ec038fe2 Mon Sep 17 00:00:00 2001 From: manuroe Date: Tue, 20 Dec 2016 11:46:57 +0100 Subject: OLMKit: More zeroing --- xcode/OLMKit/OLMSession.m | 1 + 1 file changed, 1 insertion(+) (limited to 'xcode/OLMKit/OLMSession.m') diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m index 2111c1c..8c29113 100644 --- a/xcode/OLMKit/OLMSession.m +++ b/xcode/OLMKit/OLMSession.m @@ -287,6 +287,7 @@ } plaintextData.length = plaintextLength; NSString *plaintext = [[NSString alloc] initWithData:plaintextData encoding:NSUTF8StringEncoding]; + [plaintextData resetBytesInRange:NSMakeRange(0, plaintextData.length)]; return plaintext; } -- cgit v1.2.3