aboutsummaryrefslogtreecommitdiff
path: root/xcode
diff options
context:
space:
mode:
authormanuroe <manu@matrix.org>2016-11-14 16:54:51 +0100
committermanuroe <manu@matrix.org>2016-11-14 16:54:51 +0100
commitcf66af6f2e7c69a3e0712317f8473ab09711d426 (patch)
tree425ad41ff31014973628daa8054772939e53edcd /xcode
parent27a8c28da4e5c62d8863ee3d30642109d713c4d6 (diff)
OLMKit: Replaced NSAsserts by NSErrors
Diffstat (limited to 'xcode')
-rw-r--r--xcode/OLMKit/OLMAccount.m8
-rw-r--r--xcode/OLMKit/OLMInboundGroupSession.h4
-rw-r--r--xcode/OLMKit/OLMInboundGroupSession.m57
-rw-r--r--xcode/OLMKit/OLMOutboundGroupSession.h2
-rw-r--r--xcode/OLMKit/OLMOutboundGroupSession.m29
-rw-r--r--xcode/OLMKit/OLMSession.h10
-rw-r--r--xcode/OLMKit/OLMSession.m108
-rw-r--r--xcode/OLMKit/OLMUtility.h2
-rw-r--r--xcode/OLMKit/OLMUtility.m4
-rw-r--r--xcode/OLMKitTests/OLMKitGroupTests.m14
-rw-r--r--xcode/OLMKitTests/OLMKitTests.m56
11 files changed, 211 insertions, 83 deletions
diff --git a/xcode/OLMKit/OLMAccount.m b/xcode/OLMKit/OLMAccount.m
index 085b487..4830995 100644
--- a/xcode/OLMKit/OLMAccount.m
+++ b/xcode/OLMKit/OLMAccount.m
@@ -151,7 +151,7 @@
size_t result = olm_remove_one_time_keys(self.account, session.session);
if (result == olm_error()) {
const char *error = olm_account_last_error(_account);
- NSAssert(NO, @"olm_remove_one_time_keys error: %s", error);
+ NSLog(@"olm_remove_one_time_keys error: %s", error);
return NO;
}
return YES;
@@ -174,7 +174,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;
}
@@ -184,7 +184,7 @@
const char *olm_error = olm_account_last_error(_account);
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;
}
@@ -201,7 +201,7 @@
const char *olm_error = olm_account_last_error(_account);
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;
}
diff --git a/xcode/OLMKit/OLMInboundGroupSession.h b/xcode/OLMKit/OLMInboundGroupSession.h
index 46de3a0..40dfce7 100644
--- a/xcode/OLMKit/OLMInboundGroupSession.h
+++ b/xcode/OLMKit/OLMInboundGroupSession.h
@@ -19,11 +19,11 @@
@interface OLMInboundGroupSession : NSObject <OLMSerializable, NSSecureCoding>
-- (instancetype) initInboundGroupSessionWithSessionKey:(NSString*)sessionKey;
+- (instancetype) initInboundGroupSessionWithSessionKey:(NSString*)sessionKey error:(NSError**)error;
- (NSString*)sessionIdentifier;
/** base64 ciphertext -> UTF-8 plaintext */
-- (NSString*)decryptMessage:(NSString*)message messageIndex:(NSUInteger*)messageIndex;
+- (NSString*)decryptMessage:(NSString*)message messageIndex:(NSUInteger*)messageIndex error:(NSError**)error;
@end
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;
}
diff --git a/xcode/OLMKit/OLMOutboundGroupSession.h b/xcode/OLMKit/OLMOutboundGroupSession.h
index e7a8a91..4586a25 100644
--- a/xcode/OLMKit/OLMOutboundGroupSession.h
+++ b/xcode/OLMKit/OLMOutboundGroupSession.h
@@ -26,6 +26,6 @@
- (NSString*)sessionKey;
/** UTF-8 plaintext -> base64 ciphertext */
-- (NSString*)encryptMessage:(NSString*)message;
+- (NSString*)encryptMessage:(NSString*)message error:(NSError**)error;
@end
diff --git a/xcode/OLMKit/OLMOutboundGroupSession.m b/xcode/OLMKit/OLMOutboundGroupSession.m
index c86fa9f..9741db1 100644
--- a/xcode/OLMKit/OLMOutboundGroupSession.m
+++ b/xcode/OLMKit/OLMOutboundGroupSession.m
@@ -56,7 +56,7 @@
size_t result = olm_init_outbound_group_session(session, random.mutableBytes, random.length);
if (result == olm_error()) {
const char *error = olm_outbound_group_session_last_error(session);
- NSAssert(NO, @"olm_init_outbound_group_session error: %s", error);
+ NSLog(@"olm_init_outbound_group_session error: %s", error);
return nil;
}
}
@@ -72,7 +72,7 @@
size_t result = olm_outbound_group_session_id(session, idData.mutableBytes, idData.length);
if (result == olm_error()) {
const char *error = olm_outbound_group_session_last_error(session);
- NSAssert(NO, @"olm_outbound_group_session_id error: %s", error);
+ NSLog(@"olm_outbound_group_session_id error: %s", error);
return nil;
}
NSString *idString = [[NSString alloc] initWithData:idData encoding:NSUTF8StringEncoding];
@@ -92,14 +92,14 @@
size_t result = olm_outbound_group_session_key(session, sessionKeyData.mutableBytes, sessionKeyData.length);
if (result == olm_error()) {
const char *error = olm_outbound_group_session_last_error(session);
- NSAssert(NO, @"olm_outbound_group_session_key error: %s", error);
+ NSLog(@"olm_outbound_group_session_key error: %s", error);
return nil;
}
NSString *sessionKey = [[NSString alloc] initWithData:sessionKeyData encoding:NSUTF8StringEncoding];
return sessionKey;
}
-- (NSString *)encryptMessage:(NSString *)message {
+- (NSString *)encryptMessage:(NSString *)message error:(NSError**)error {
NSData *plaintextData = [message dataUsingEncoding:NSUTF8StringEncoding];
size_t ciphertextLength = olm_group_encrypt_message_length(session, plaintextData.length);
NSMutableData *ciphertext = [NSMutableData dataWithLength:ciphertextLength];
@@ -108,8 +108,19 @@
}
size_t result = olm_group_encrypt(session, plaintextData.bytes, plaintextData.length, ciphertext.mutableBytes, ciphertext.length);
if (result == olm_error()) {
- const char *error = olm_outbound_group_session_last_error(session);
- NSAssert(NO, @"olm_group_encrypt error: %s", error);
+ const char *olm_error = olm_outbound_group_session_last_error(session);
+
+ NSString *errorString = [NSString stringWithUTF8String:olm_error];
+ NSLog(@"olm_group_encrypt error: %@", errorString);
+
+ if (error && olm_error && errorString) {
+ *error = [NSError errorWithDomain:OLMErrorDomain
+ code:0
+ userInfo:@{
+ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_group_encrypt error: %@", errorString]
+ }];
+ }
+
return nil;
}
return [[NSString alloc] initWithData:ciphertext encoding:NSUTF8StringEncoding];
@@ -127,7 +138,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;
}
@@ -137,7 +148,7 @@
const char *olm_error = olm_outbound_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;
}
@@ -154,7 +165,7 @@
const char *olm_error = olm_outbound_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;
}
diff --git a/xcode/OLMKit/OLMSession.h b/xcode/OLMKit/OLMSession.h
index c209564..b10e481 100644
--- a/xcode/OLMKit/OLMSession.h
+++ b/xcode/OLMKit/OLMSession.h
@@ -13,11 +13,11 @@
@interface OLMSession : NSObject <OLMSerializable, NSSecureCoding>
-- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey theirOneTimeKey:(NSString*)theirOneTimeKey;
+- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey theirOneTimeKey:(NSString*)theirOneTimeKey error:(NSError**)error;
-- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSString*)oneTimeKeyMessage;
+- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSString*)oneTimeKeyMessage error:(NSError**)error;
-- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString*)oneTimeKeyMessage;
+- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString*)oneTimeKeyMessage error:(NSError**)error;
- (NSString*) sessionIdentifier;
@@ -26,9 +26,9 @@
- (BOOL) matchesInboundSessionFrom:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString *)oneTimeKeyMessage;
/** UTF-8 plaintext -> base64 ciphertext */
-- (OLMMessage*) encryptMessage:(NSString*)message;
+- (OLMMessage*) encryptMessage:(NSString*)message error:(NSError**)error;
/** base64 ciphertext -> UTF-8 plaintext */
-- (NSString*) decryptMessage:(OLMMessage*)message;
+- (NSString*) decryptMessage:(OLMMessage*)message error:(NSError**)error;
@end
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;
}
diff --git a/xcode/OLMKit/OLMUtility.h b/xcode/OLMKit/OLMUtility.h
index 1952b8e..c316829 100644
--- a/xcode/OLMKit/OLMUtility.h
+++ b/xcode/OLMKit/OLMUtility.h
@@ -8,6 +8,8 @@
#import <Foundation/Foundation.h>
+FOUNDATION_EXPORT NSString *const OLMErrorDomain;
+
@interface OLMUtility : NSObject
/**
diff --git a/xcode/OLMKit/OLMUtility.m b/xcode/OLMKit/OLMUtility.m
index 292fc21..3c6ce6e 100644
--- a/xcode/OLMKit/OLMUtility.m
+++ b/xcode/OLMKit/OLMUtility.m
@@ -10,6 +10,8 @@
#include "olm/olm.h"
+NSString *const OLMErrorDomain = @"org.matrix.olm";
+
@interface OLMUtility()
@property (nonatomic) OlmUtility *utility;
@@ -61,7 +63,7 @@
size_t result = olm_sha256(_utility, message.bytes, message.length, shaData.mutableBytes, shaData.length);
if (result == olm_error()) {
const char *error = olm_utility_last_error(_utility);
- NSAssert(NO, @"olm_sha256 error: %s", error);
+ NSLog(@"olm_sha256 error: %s", error);
return nil;
}
diff --git a/xcode/OLMKitTests/OLMKitGroupTests.m b/xcode/OLMKitTests/OLMKitGroupTests.m
index cdfb704..43e2df8 100644
--- a/xcode/OLMKitTests/OLMKitGroupTests.m
+++ b/xcode/OLMKitTests/OLMKitGroupTests.m
@@ -37,6 +37,7 @@
}
- (void)testAliceAndBob {
+ NSError *error;
OLMOutboundGroupSession *aliceSession = [[OLMOutboundGroupSession alloc] initOutboundGroupSession];
XCTAssertGreaterThan(aliceSession.sessionIdentifier.length, 0);
@@ -47,18 +48,23 @@
NSString *sessionKey = aliceSession.sessionKey;
NSString *message = @"Hello!";
- NSString *aliceToBobMsg = [aliceSession encryptMessage:message];
+ NSString *aliceToBobMsg = [aliceSession encryptMessage:message error:&error];
XCTAssertEqual(aliceSession.messageIndex, 1);
XCTAssertGreaterThanOrEqual(aliceToBobMsg.length, 0);
+ XCTAssertNil(error);
- OLMInboundGroupSession *bobSession = [[OLMInboundGroupSession alloc] initInboundGroupSessionWithSessionKey:sessionKey];
+ OLMInboundGroupSession *bobSession = [[OLMInboundGroupSession alloc] initInboundGroupSessionWithSessionKey:sessionKey error:&error];
XCTAssertEqualObjects(aliceSession.sessionIdentifier, bobSession.sessionIdentifier);
+ XCTAssertNil(error);
NSUInteger messageIndex;
- NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg messageIndex:&messageIndex];
+
+ NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg messageIndex:&messageIndex error:&error];
XCTAssertEqualObjects(message, plaintext);
+
XCTAssertEqual(messageIndex, 0);
+ XCTAssertNil(error);
}
- (void)testOutboundGroupSessionSerialization {
@@ -76,7 +82,7 @@
OLMOutboundGroupSession *aliceSession = [[OLMOutboundGroupSession alloc] initOutboundGroupSession];
- OLMInboundGroupSession *bobSession = [[OLMInboundGroupSession alloc] initInboundGroupSessionWithSessionKey:aliceSession.sessionKey];
+ OLMInboundGroupSession *bobSession = [[OLMInboundGroupSession alloc] initInboundGroupSessionWithSessionKey:aliceSession.sessionKey error:nil];
NSData *bobData = [NSKeyedArchiver archivedDataWithRootObject:bobSession];
OLMInboundGroupSession *bobSession2 = [NSKeyedUnarchiver unarchiveObjectWithData:bobData];
diff --git a/xcode/OLMKitTests/OLMKitTests.m b/xcode/OLMKitTests/OLMKitTests.m
index 7edc062..251c90e 100644
--- a/xcode/OLMKitTests/OLMKitTests.m
+++ b/xcode/OLMKitTests/OLMKitTests.m
@@ -26,6 +26,8 @@
}
- (void)testAliceAndBob {
+ NSError *error;
+
OLMAccount *alice = [[OLMAccount alloc] initNewAccount];
OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
[bob generateOneTimeKeys:5];
@@ -41,13 +43,15 @@
}];
XCTAssert([bobOneTimeKey isKindOfClass:[NSString class]]);
- OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey];
+ OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey error:nil];
NSString *message = @"Hello!";
- OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message];
+ OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message error:&error];
+ XCTAssertNil(error);
- OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext];
- NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg];
+ OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext error:nil];
+ NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg error:&error];
XCTAssertEqualObjects(message, plaintext);
+ XCTAssertNil(error);
BOOL success = [bob removeOneTimeKeysForSession:bobSession];
XCTAssertTrue(success);
}
@@ -68,12 +72,12 @@
}];
XCTAssert([bobOneTimeKey isKindOfClass:[NSString class]]);
- OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey];
+ OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey error:nil];
NSString *message = @"Hello I'm Alice!";
- OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message];
+ OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message error:nil];
- OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext];
- NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg];
+ OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext error:nil];
+ NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg error:nil];
XCTAssertEqualObjects(message, plaintext);
BOOL success = [bob removeOneTimeKeysForSession:bobSession];
XCTAssertTrue(success);
@@ -82,13 +86,13 @@
NSString *msg2 = @"Isn't life grand?";
NSString *msg3 = @"Let's go to the opera.";
- OLMMessage *eMsg1 = [bobSession encryptMessage:msg1];
- OLMMessage *eMsg2 = [bobSession encryptMessage:msg2];
- OLMMessage *eMsg3 = [bobSession encryptMessage:msg3];
+ OLMMessage *eMsg1 = [bobSession encryptMessage:msg1 error:nil];
+ OLMMessage *eMsg2 = [bobSession encryptMessage:msg2 error:nil];
+ OLMMessage *eMsg3 = [bobSession encryptMessage:msg3 error:nil];
- NSString *dMsg1 = [aliceSession decryptMessage:eMsg1];
- NSString *dMsg2 = [aliceSession decryptMessage:eMsg2];
- NSString *dMsg3 = [aliceSession decryptMessage:eMsg3];
+ NSString *dMsg1 = [aliceSession decryptMessage:eMsg1 error:nil];
+ NSString *dMsg2 = [aliceSession decryptMessage:eMsg2 error:nil];
+ NSString *dMsg3 = [aliceSession decryptMessage:eMsg3 error:nil];
XCTAssertEqualObjects(msg1, dMsg1);
XCTAssertEqualObjects(msg2, dMsg2);
XCTAssertEqualObjects(msg3, dMsg3);
@@ -113,6 +117,8 @@
}
- (void) testSessionSerialization {
+ NSError *error;
+
OLMAccount *alice = [[OLMAccount alloc] initNewAccount];
OLMAccount *bob = [[OLMAccount alloc] initNewAccount];
[bob generateOneTimeKeys:1];
@@ -128,12 +134,14 @@
}];
XCTAssert([bobOneTimeKey isKindOfClass:[NSString class]]);
- OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey];
+ OLMSession *aliceSession = [[OLMSession alloc] initOutboundSessionWithAccount:alice theirIdentityKey:bobIdKey theirOneTimeKey:bobOneTimeKey error:nil];
NSString *message = @"Hello I'm Alice!";
- OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message];
+ OLMMessage *aliceToBobMsg = [aliceSession encryptMessage:message error:&error];
+ XCTAssertNil(error);
+
- OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext];
- NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg];
+ OLMSession *bobSession = [[OLMSession alloc] initInboundSessionWithAccount:bob oneTimeKeyMessage:aliceToBobMsg.ciphertext error:nil];
+ NSString *plaintext = [bobSession decryptMessage:aliceToBobMsg error:nil];
XCTAssertEqualObjects(message, plaintext);
BOOL success = [bob removeOneTimeKeysForSession:bobSession];
XCTAssertTrue(success);
@@ -142,16 +150,16 @@
NSString *msg2 = @"Isn't life grand?";
NSString *msg3 = @"Let's go to the opera.";
- OLMMessage *eMsg1 = [bobSession encryptMessage:msg1];
- OLMMessage *eMsg2 = [bobSession encryptMessage:msg2];
- OLMMessage *eMsg3 = [bobSession encryptMessage:msg3];
+ OLMMessage *eMsg1 = [bobSession encryptMessage:msg1 error:nil];
+ OLMMessage *eMsg2 = [bobSession encryptMessage:msg2 error:nil];
+ OLMMessage *eMsg3 = [bobSession encryptMessage:msg3 error:nil];
NSData *aliceData = [NSKeyedArchiver archivedDataWithRootObject:aliceSession];
OLMSession *alice2 = [NSKeyedUnarchiver unarchiveObjectWithData:aliceData];
- NSString *dMsg1 = [alice2 decryptMessage:eMsg1];
- NSString *dMsg2 = [alice2 decryptMessage:eMsg2];
- NSString *dMsg3 = [alice2 decryptMessage:eMsg3];
+ NSString *dMsg1 = [alice2 decryptMessage:eMsg1 error:nil];
+ NSString *dMsg2 = [alice2 decryptMessage:eMsg2 error:nil];
+ NSString *dMsg3 = [alice2 decryptMessage:eMsg3 error:nil];
XCTAssertEqualObjects(msg1, dMsg1);
XCTAssertEqualObjects(msg2, dMsg2);
XCTAssertEqualObjects(msg3, dMsg3);