From 2bd912990fb82bf3cdd54a9268143d8b3a2889ef Mon Sep 17 00:00:00 2001 From: manuroe Date: Mon, 10 Oct 2016 17:10:51 +0200 Subject: OLMKit: Add megolm api: OLMInboundGroupSession and OLMOutboundGroupSession --- xcode/OLMKit/OLMInboundGroupSession.m | 198 ++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 xcode/OLMKit/OLMInboundGroupSession.m (limited to 'xcode/OLMKit/OLMInboundGroupSession.m') diff --git a/xcode/OLMKit/OLMInboundGroupSession.m b/xcode/OLMKit/OLMInboundGroupSession.m new file mode 100644 index 0000000..d95d1ab --- /dev/null +++ b/xcode/OLMKit/OLMInboundGroupSession.m @@ -0,0 +1,198 @@ +/* + Copyright 2016 OpenMarket 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 "OLMInboundGroupSession.h" + +#import "OLMUtility.h" +#include "olm/olm.h" + +@interface OLMInboundGroupSession () +{ + OlmInboundGroupSession *session; +} +@end + + +@implementation OLMInboundGroupSession + +- (void)dealloc { + olm_clear_inbound_group_session(session); + free(session); +} + +- (instancetype)init { + self = [super init]; + if (self) + { + session = malloc(olm_inbound_group_session_size()); + if (session) { + session = olm_inbound_group_session(session); + } + + if (!session) { + return nil; + } + } + return self; +} + +- (instancetype)initInboundGroupSessionWithSessionKey:(NSString *)sessionKey { + self = [self init]; + if (self) { + NSData *sessionKeyData = [sessionKey dataUsingEncoding:NSUTF8StringEncoding]; + size_t result = olm_init_inbound_group_session(session, sessionKeyData.bytes, sessionKeyData.length); + if (result == olm_error()) { + const char *error = olm_inbound_group_session_last_error(session); + NSAssert(NO, @"olm_init_inbound_group_session error: %s", error); + return nil; + } + } + return self; +} + +- (NSString *)sessionIdentifier { + size_t length = olm_inbound_group_session_id_length(session); + NSMutableData *idData = [NSMutableData dataWithLength:length]; + if (!idData) { + return nil; + } + size_t result = olm_inbound_group_session_id(session, idData.mutableBytes, idData.length); + if (result == olm_error()) { + const char *error = olm_inbound_group_session_last_error(session); + NSAssert(NO, @"olm_inbound_group_session_id error: %s", error); + return nil; + } + NSString *idString = [[NSString alloc] initWithData:idData encoding:NSUTF8StringEncoding]; + return idString; +} + +- (NSString *)decryptMessage:(NSString *)message +{ + NSParameterAssert(message != nil); + NSData *messageData = [message dataUsingEncoding:NSUTF8StringEncoding]; + if (!messageData) { + return nil; + } + NSMutableData *mutMessage = messageData.mutableCopy; + size_t maxPlaintextLength = olm_group_decrypt_max_plaintext_length(session, mutMessage.mutableBytes, mutMessage.length); + if (maxPlaintextLength == olm_error()) { + const char *error = olm_inbound_group_session_last_error(session); + NSAssert(NO, @"olm_group_decrypt_max_plaintext_length error: %s", error); + return nil; + } + // message buffer is destroyed by olm_group_decrypt_max_plaintext_length + mutMessage = messageData.mutableCopy; + NSMutableData *plaintextData = [NSMutableData dataWithLength:maxPlaintextLength]; + size_t plaintextLength = olm_group_decrypt(session, mutMessage.mutableBytes, mutMessage.length, plaintextData.mutableBytes, plaintextData.length); + if (plaintextLength == olm_error()) { + const char *error = olm_inbound_group_session_last_error(session); + NSAssert(NO, @"olm_group_decrypt error: %s", error); + return nil; + } + plaintextData.length = plaintextLength; + NSString *plaintext = [[NSString alloc] initWithData:plaintextData encoding:NSUTF8StringEncoding]; + 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 *__autoreleasing *)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_inbound_group_session(session, key.bytes, key.length, pickle.mutableBytes, pickle.length); + if (result == olm_error()) { + const char *olm_error = olm_inbound_group_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_inbound_group_session_length(session); + NSMutableData *pickled = [NSMutableData dataWithLength:length]; + size_t result = olm_pickle_inbound_group_session(session, key.bytes, key.length, pickled.mutableBytes, pickled.length); + if (result == olm_error()) { + const char *olm_error = olm_inbound_group_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 27a8c28da4e5c62d8863ee3d30642109d713c4d6 Mon Sep 17 00:00:00 2001 From: manuroe Date: Mon, 7 Nov 2016 17:27:09 +0100 Subject: OLMKit: Update obj-c wrapper to 2.0.0 --- xcode/OLMKit/OLMInboundGroupSession.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'xcode/OLMKit/OLMInboundGroupSession.m') diff --git a/xcode/OLMKit/OLMInboundGroupSession.m b/xcode/OLMKit/OLMInboundGroupSession.m index d95d1ab..ea79e14 100644 --- a/xcode/OLMKit/OLMInboundGroupSession.m +++ b/xcode/OLMKit/OLMInboundGroupSession.m @@ -79,7 +79,7 @@ return idString; } -- (NSString *)decryptMessage:(NSString *)message +- (NSString *)decryptMessage:(NSString *)message messageIndex:(NSUInteger*)messageIndex { NSParameterAssert(message != nil); NSData *messageData = [message dataUsingEncoding:NSUTF8StringEncoding]; @@ -96,7 +96,7 @@ // message buffer is destroyed by olm_group_decrypt_max_plaintext_length mutMessage = messageData.mutableCopy; NSMutableData *plaintextData = [NSMutableData dataWithLength:maxPlaintextLength]; - size_t plaintextLength = olm_group_decrypt(session, mutMessage.mutableBytes, mutMessage.length, plaintextData.mutableBytes, plaintextData.length); + size_t plaintextLength = olm_group_decrypt(session, mutMessage.mutableBytes, mutMessage.length, plaintextData.mutableBytes, plaintextData.length, messageIndex); if (plaintextLength == olm_error()) { const char *error = olm_inbound_group_session_last_error(session); NSAssert(NO, @"olm_group_decrypt error: %s", error); -- 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/OLMInboundGroupSession.m | 57 +++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 12 deletions(-) (limited to 'xcode/OLMKit/OLMInboundGroupSession.m') diff --git a/xcode/OLMKit/OLMInboundGroupSession.m b/xcode/OLMKit/OLMInboundGroupSession.m index ea79e14..5e108a1 100644 --- a/xcode/OLMKit/OLMInboundGroupSession.m +++ b/xcode/OLMKit/OLMInboundGroupSession.m @@ -49,14 +49,25 @@ return self; } -- (instancetype)initInboundGroupSessionWithSessionKey:(NSString *)sessionKey { +- (instancetype)initInboundGroupSessionWithSessionKey:(NSString *)sessionKey error:(NSError**)error { self = [self init]; if (self) { NSData *sessionKeyData = [sessionKey dataUsingEncoding:NSUTF8StringEncoding]; size_t result = olm_init_inbound_group_session(session, sessionKeyData.bytes, sessionKeyData.length); if (result == olm_error()) { - const char *error = olm_inbound_group_session_last_error(session); - NSAssert(NO, @"olm_init_inbound_group_session error: %s", error); + const char *olm_error = olm_inbound_group_session_last_error(session); + + NSString *errorString = [NSString stringWithUTF8String:olm_error]; + NSLog(@"olm_init_inbound_group_session error: %@", errorString); + + if (error && olm_error && errorString) { + *error = [NSError errorWithDomain:OLMErrorDomain + code:0 + userInfo:@{ + NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_init_inbound_group_session error: %@", errorString] + }]; + } + return nil; } } @@ -72,14 +83,14 @@ size_t result = olm_inbound_group_session_id(session, idData.mutableBytes, idData.length); if (result == olm_error()) { const char *error = olm_inbound_group_session_last_error(session); - NSAssert(NO, @"olm_inbound_group_session_id error: %s", error); + NSLog(@"olm_inbound_group_session_id error: %s", error); return nil; } NSString *idString = [[NSString alloc] initWithData:idData encoding:NSUTF8StringEncoding]; return idString; } -- (NSString *)decryptMessage:(NSString *)message messageIndex:(NSUInteger*)messageIndex +- (NSString *)decryptMessage:(NSString *)message messageIndex:(NSUInteger*)messageIndex error:(NSError**)error { NSParameterAssert(message != nil); NSData *messageData = [message dataUsingEncoding:NSUTF8StringEncoding]; @@ -89,8 +100,19 @@ NSMutableData *mutMessage = messageData.mutableCopy; size_t maxPlaintextLength = olm_group_decrypt_max_plaintext_length(session, mutMessage.mutableBytes, mutMessage.length); if (maxPlaintextLength == olm_error()) { - const char *error = olm_inbound_group_session_last_error(session); - NSAssert(NO, @"olm_group_decrypt_max_plaintext_length error: %s", error); + const char *olm_error = olm_inbound_group_session_last_error(session); + + NSString *errorString = [NSString stringWithUTF8String:olm_error]; + NSLog(@"olm_group_decrypt_max_plaintext_length error: %@", errorString); + + if (error && olm_error && errorString) { + *error = [NSError errorWithDomain:OLMErrorDomain + code:0 + userInfo:@{ + NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_group_decrypt_max_plaintext_length error: %@", errorString] + }]; + } + return nil; } // message buffer is destroyed by olm_group_decrypt_max_plaintext_length @@ -98,8 +120,19 @@ NSMutableData *plaintextData = [NSMutableData dataWithLength:maxPlaintextLength]; size_t plaintextLength = olm_group_decrypt(session, mutMessage.mutableBytes, mutMessage.length, plaintextData.mutableBytes, plaintextData.length, messageIndex); if (plaintextLength == olm_error()) { - const char *error = olm_inbound_group_session_last_error(session); - NSAssert(NO, @"olm_group_decrypt error: %s", error); + const char *olm_error = olm_inbound_group_session_last_error(session); + + NSString *errorString = [NSString stringWithUTF8String:olm_error]; + NSLog(@"olm_group_decrypt error: %@", errorString); + + if (error && olm_error && errorString) { + *error = [NSError errorWithDomain:OLMErrorDomain + code:0 + userInfo:@{ + NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_group_decrypt error: %@", errorString] + }]; + } + return nil; } plaintextData.length = plaintextLength; @@ -120,7 +153,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; } @@ -130,7 +163,7 @@ const char *olm_error = olm_inbound_group_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; } @@ -147,7 +180,7 @@ const char *olm_error = olm_inbound_group_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 bc697bf5e2ddbad947aec65533d41514592ca093 Mon Sep 17 00:00:00 2001 From: manuroe Date: Mon, 14 Nov 2016 17:02:56 +0100 Subject: OLMKit: Fixed type-cast of messageIndex of [OLMInboundGroupSession decryptMessage] for 32 and 64bits platforms --- xcode/OLMKit/OLMInboundGroupSession.m | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'xcode/OLMKit/OLMInboundGroupSession.m') diff --git a/xcode/OLMKit/OLMInboundGroupSession.m b/xcode/OLMKit/OLMInboundGroupSession.m index 5e108a1..32e1376 100644 --- a/xcode/OLMKit/OLMInboundGroupSession.m +++ b/xcode/OLMKit/OLMInboundGroupSession.m @@ -118,7 +118,9 @@ // message buffer is destroyed by olm_group_decrypt_max_plaintext_length mutMessage = messageData.mutableCopy; NSMutableData *plaintextData = [NSMutableData dataWithLength:maxPlaintextLength]; - size_t plaintextLength = olm_group_decrypt(session, mutMessage.mutableBytes, mutMessage.length, plaintextData.mutableBytes, plaintextData.length, messageIndex); + + uint32_t message_index; + size_t plaintextLength = olm_group_decrypt(session, mutMessage.mutableBytes, mutMessage.length, plaintextData.mutableBytes, plaintextData.length, &message_index); if (plaintextLength == olm_error()) { const char *olm_error = olm_inbound_group_session_last_error(session); @@ -137,6 +139,12 @@ } plaintextData.length = plaintextLength; NSString *plaintext = [[NSString alloc] initWithData:plaintextData encoding:NSUTF8StringEncoding]; + + if (messageIndex) + { + *messageIndex = message_index; + } + return plaintext; } -- 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/OLMInboundGroupSession.m | 1 + 1 file changed, 1 insertion(+) (limited to 'xcode/OLMKit/OLMInboundGroupSession.m') diff --git a/xcode/OLMKit/OLMInboundGroupSession.m b/xcode/OLMKit/OLMInboundGroupSession.m index 32e1376..eec2ffa 100644 --- a/xcode/OLMKit/OLMInboundGroupSession.m +++ b/xcode/OLMKit/OLMInboundGroupSession.m @@ -1,5 +1,6 @@ /* 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. -- 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/OLMInboundGroupSession.m | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'xcode/OLMKit/OLMInboundGroupSession.m') diff --git a/xcode/OLMKit/OLMInboundGroupSession.m b/xcode/OLMKit/OLMInboundGroupSession.m index eec2ffa..4f7bdd7 100644 --- a/xcode/OLMKit/OLMInboundGroupSession.m +++ b/xcode/OLMKit/OLMInboundGroupSession.m @@ -65,7 +65,8 @@ *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{ - NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_init_inbound_group_session error: %@", errorString] + NSLocalizedDescriptionKey: errorString, + NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_init_inbound_group_session error: %@", errorString] }]; } @@ -110,7 +111,8 @@ *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{ - NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_group_decrypt_max_plaintext_length error: %@", errorString] + NSLocalizedDescriptionKey: errorString, + NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_group_decrypt_max_plaintext_length error: %@", errorString] }]; } @@ -132,7 +134,8 @@ *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{ - NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_group_decrypt error: %@", errorString] + NSLocalizedDescriptionKey: errorString, + NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_group_decrypt error: %@", errorString] }]; } -- 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/OLMInboundGroupSession.m | 1 + 1 file changed, 1 insertion(+) (limited to 'xcode/OLMKit/OLMInboundGroupSession.m') diff --git a/xcode/OLMKit/OLMInboundGroupSession.m b/xcode/OLMKit/OLMInboundGroupSession.m index 4f7bdd7..6ef51c3 100644 --- a/xcode/OLMKit/OLMInboundGroupSession.m +++ b/xcode/OLMKit/OLMInboundGroupSession.m @@ -143,6 +143,7 @@ } plaintextData.length = plaintextLength; NSString *plaintext = [[NSString alloc] initWithData:plaintextData encoding:NSUTF8StringEncoding]; + [plaintextData resetBytesInRange:NSMakeRange(0, plaintextData.length)]; if (messageIndex) { -- cgit v1.2.3