From 2a8202e74846d191a321cca1202175af9db6107d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 5 Nov 2020 01:45:06 +0100 Subject: Convert to sibs project --- xcode/OLMKit/Info.plist | 26 --- xcode/OLMKit/OLMAccount.h | 51 ----- xcode/OLMKit/OLMAccount.m | 268 ----------------------- xcode/OLMKit/OLMAccount_Private.h | 25 --- xcode/OLMKit/OLMInboundGroupSession.h | 38 ---- xcode/OLMKit/OLMInboundGroupSession.m | 301 -------------------------- xcode/OLMKit/OLMKit.h | 39 ---- xcode/OLMKit/OLMKit.m | 33 --- xcode/OLMKit/OLMMessage.h | 38 ---- xcode/OLMKit/OLMMessage.m | 34 --- xcode/OLMKit/OLMOutboundGroupSession.h | 32 --- xcode/OLMKit/OLMOutboundGroupSession.m | 222 ------------------- xcode/OLMKit/OLMPkDecryption.h | 71 ------ xcode/OLMKit/OLMPkDecryption.m | 299 ------------------------- xcode/OLMKit/OLMPkEncryption.h | 42 ---- xcode/OLMKit/OLMPkEncryption.m | 111 ---------- xcode/OLMKit/OLMPkMessage.h | 31 --- xcode/OLMKit/OLMPkMessage.m | 32 --- xcode/OLMKit/OLMPkSigning.h | 49 ----- xcode/OLMKit/OLMPkSigning.m | 125 ----------- xcode/OLMKit/OLMSAS.h | 70 ------ xcode/OLMKit/OLMSAS.m | 174 --------------- xcode/OLMKit/OLMSerializable.h | 29 --- xcode/OLMKit/OLMSession.h | 44 ---- xcode/OLMKit/OLMSession.m | 383 --------------------------------- xcode/OLMKit/OLMSession_Private.h | 26 --- xcode/OLMKit/OLMUtility.h | 49 ----- xcode/OLMKit/OLMUtility.m | 121 ----------- 28 files changed, 2763 deletions(-) delete mode 100644 xcode/OLMKit/Info.plist delete mode 100644 xcode/OLMKit/OLMAccount.h delete mode 100644 xcode/OLMKit/OLMAccount.m delete mode 100644 xcode/OLMKit/OLMAccount_Private.h delete mode 100644 xcode/OLMKit/OLMInboundGroupSession.h delete mode 100644 xcode/OLMKit/OLMInboundGroupSession.m delete mode 100644 xcode/OLMKit/OLMKit.h delete mode 100644 xcode/OLMKit/OLMKit.m delete mode 100644 xcode/OLMKit/OLMMessage.h delete mode 100644 xcode/OLMKit/OLMMessage.m delete mode 100644 xcode/OLMKit/OLMOutboundGroupSession.h delete mode 100644 xcode/OLMKit/OLMOutboundGroupSession.m delete mode 100644 xcode/OLMKit/OLMPkDecryption.h delete mode 100644 xcode/OLMKit/OLMPkDecryption.m delete mode 100644 xcode/OLMKit/OLMPkEncryption.h delete mode 100644 xcode/OLMKit/OLMPkEncryption.m delete mode 100644 xcode/OLMKit/OLMPkMessage.h delete mode 100644 xcode/OLMKit/OLMPkMessage.m delete mode 100644 xcode/OLMKit/OLMPkSigning.h delete mode 100644 xcode/OLMKit/OLMPkSigning.m delete mode 100644 xcode/OLMKit/OLMSAS.h delete mode 100644 xcode/OLMKit/OLMSAS.m delete mode 100644 xcode/OLMKit/OLMSerializable.h delete mode 100644 xcode/OLMKit/OLMSession.h delete mode 100644 xcode/OLMKit/OLMSession.m delete mode 100644 xcode/OLMKit/OLMSession_Private.h delete mode 100644 xcode/OLMKit/OLMUtility.h delete mode 100644 xcode/OLMKit/OLMUtility.m (limited to 'xcode/OLMKit') diff --git a/xcode/OLMKit/Info.plist b/xcode/OLMKit/Info.plist deleted file mode 100644 index d3de8ee..0000000 --- a/xcode/OLMKit/Info.plist +++ /dev/null @@ -1,26 +0,0 @@ - - - - - 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 deleted file mode 100644 index c8d65cd..0000000 --- a/xcode/OLMKit/OLMAccount.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - 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 -#import "OLMSerializable.h" - -@class OLMSession; - -@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 */ -- (NSString*) signMessage:(NSData*)messageData; - -/** Public parts of the unpublished one time keys for the account */ -- (NSDictionary*) oneTimeKeys; - -- (BOOL) removeOneTimeKeysForSession:(OLMSession*)session; - -/** Marks the current set of one time keys as being published. */ -- (void) markOneTimeKeysAsPublished; - -/** 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 deleted file mode 100644 index 9e48c2d..0000000 --- a/xcode/OLMKit/OLMAccount.m +++ /dev/null @@ -1,268 +0,0 @@ -/* - 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 "OLMAccount.h" -#import "OLMAccount_Private.h" -#import "OLMSession.h" -#import "OLMSession_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) init { - self = [super init]; - if (!self) { - return nil; - } - BOOL success = [self initializeAccountMemory]; - if (!success) { - return nil; - } - return self; -} - -- (instancetype) initNewAccount { - self = [self init]; - if (!self) { - return nil; - } - size_t randomLength = olm_create_account_random_length(_account); - NSMutableData *random = [OLMUtility randomBytesOfLength:randomLength]; - size_t accountResult = olm_create_account(_account, random.mutableBytes, random.length); - [random resetBytesInRange:NSMakeRange(0, random.length)]; - if (accountResult == olm_error()) { - const char *error = olm_account_last_error(_account); - NSLog(@"error creating account: %s", error); - return nil; - } - return self; -} - -- (NSUInteger) 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; -} - -- (NSString *)signMessage:(NSData *)messageData { - size_t signatureLength = olm_account_signature_length(_account); - uint8_t *signatureBytes = malloc(signatureLength); - if (!signatureBytes) { - return nil; - } - - size_t result = olm_account_sign(_account, messageData.bytes, messageData.length, signatureBytes, signatureLength); - if (result == olm_error()) { - const char *error = olm_account_last_error(_account); - NSLog(@"error signing message: %s", error); - free(signatureBytes); - return nil; - } - - NSData *signatureData = [NSData dataWithBytesNoCopy:signatureBytes length:signatureLength freeWhenDone:YES]; - return [[NSString alloc] initWithData:signatureData encoding:NSUTF8StringEncoding]; -} - -- (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); - return nil; - } - 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); - NSMutableData *random = [OLMUtility randomBytesOfLength:randomLength]; - size_t result = olm_account_generate_one_time_keys(_account, numberOfKeys, random.mutableBytes, random.length); - [random resetBytesInRange:NSMakeRange(0, random.length)]; - if (result == olm_error()) { - const char *error = olm_account_last_error(_account); - NSLog(@"error generating keys: %s", error); - } -} - -- (BOOL) removeOneTimeKeysForSession:(OLMSession *)session { - NSParameterAssert(session != nil); - if (!session) { - return NO; - } - size_t result = olm_remove_one_time_keys(self.account, session.session); - if (result == olm_error()) { - const char *error = olm_account_last_error(_account); - NSLog(@"olm_remove_one_time_keys error: %s", error); - return NO; - } - return YES; -} - -- (void)markOneTimeKeysAsPublished -{ - olm_account_mark_keys_as_published(self.account); -} - -#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:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}]; - } - return nil; - } - NSMutableData *pickle = [serializedData dataUsingEncoding:NSUTF8StringEncoding].mutableCopy; - size_t result = olm_unpickle_account(_account, key.bytes, key.length, pickle.mutableBytes, pickle.length); - [pickle resetBytesInRange:NSMakeRange(0, pickle.length)]; - if (result == olm_error()) { - const char *olm_error = olm_account_last_error(_account); - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - if (error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain 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_account_length(_account); - NSMutableData *pickled = [NSMutableData dataWithLength:length]; - size_t result = olm_pickle_account(_account, key.bytes, key.length, pickled.mutableBytes, pickled.length); - if (result == olm_error()) { - const char *olm_error = olm_account_last_error(_account); - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - if (error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; - } - return nil; - } - NSString *pickleString = [[NSString alloc] initWithData:pickled encoding:NSUTF8StringEncoding]; - [pickled resetBytesInRange:NSMakeRange(0, pickled.length)]; - 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 diff --git a/xcode/OLMKit/OLMAccount_Private.h b/xcode/OLMKit/OLMAccount_Private.h deleted file mode 100644 index 313ab71..0000000 --- a/xcode/OLMKit/OLMAccount_Private.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - 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. - */ - -#include "olm/olm.h" - -@interface OLMAccount() - -@property (nonatomic) OlmAccount *account; - -@end diff --git a/xcode/OLMKit/OLMInboundGroupSession.h b/xcode/OLMKit/OLMInboundGroupSession.h deleted file mode 100644 index c0d2c59..0000000 --- a/xcode/OLMKit/OLMInboundGroupSession.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - 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 -#import "OLMSerializable.h" - -@interface OLMInboundGroupSession : NSObject - -- (instancetype)initInboundGroupSessionWithSessionKey:(NSString*)sessionKey error:(NSError**)error; - -- (instancetype)initInboundGroupSessionWithImportedSession:(NSString*)sessionKey error:(NSError**)error; - -- (NSString*)sessionIdentifier; - -/** base64 ciphertext -> UTF-8 plaintext */ -- (NSString*)decryptMessage:(NSString*)message messageIndex:(NSUInteger*)messageIndex error:(NSError**)error; - -- (NSUInteger)firstKnownIndex; - -- (BOOL)isVerified; - -- (NSString*)exportSessionAtMessageIndex:(NSUInteger)messageIndex error:(NSError**)error; - -@end diff --git a/xcode/OLMKit/OLMInboundGroupSession.m b/xcode/OLMKit/OLMInboundGroupSession.m deleted file mode 100644 index 9e57741..0000000 --- a/xcode/OLMKit/OLMInboundGroupSession.m +++ /dev/null @@ -1,301 +0,0 @@ -/* - 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 "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 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 *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: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_init_inbound_group_session error: %@", errorString] - }]; - } - - return nil; - } - } - return self; -} - -- (instancetype)initInboundGroupSessionWithImportedSession:(NSString *)sessionKey error:(NSError *__autoreleasing *)error -{ - self = [self init]; - if (self) { - NSData *sessionKeyData = [sessionKey dataUsingEncoding:NSUTF8StringEncoding]; - size_t result = olm_import_inbound_group_session(session, sessionKeyData.bytes, sessionKeyData.length); - if (result == olm_error()) { - const char *olm_error = olm_inbound_group_session_last_error(session); - - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - NSLog(@"olm_import_inbound_group_session error: %@", errorString); - - if (error && olm_error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain - code:0 - userInfo:@{ - NSLocalizedDescriptionKey: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_import_inbound_group_session error: %@", errorString] - }]; - } - - 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); - 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 error:(NSError**)error -{ - 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 *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: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_group_decrypt_max_plaintext_length error: %@", errorString] - }]; - } - - return nil; - } - // message buffer is destroyed by olm_group_decrypt_max_plaintext_length - mutMessage = messageData.mutableCopy; - NSMutableData *plaintextData = [NSMutableData dataWithLength:maxPlaintextLength]; - - 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); - - 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: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_group_decrypt error: %@", errorString] - }]; - } - - return nil; - } - plaintextData.length = plaintextLength; - NSString *plaintext = [[NSString alloc] initWithData:plaintextData encoding:NSUTF8StringEncoding]; - [plaintextData resetBytesInRange:NSMakeRange(0, plaintextData.length)]; - - if (messageIndex) - { - *messageIndex = message_index; - } - - return plaintext; -} - -- (NSUInteger)firstKnownIndex -{ - return olm_inbound_group_session_first_known_index(session); -} - -- (BOOL)isVerified -{ - return (0 != olm_inbound_group_session_is_verified(session)); -} - -- (NSString*)exportSessionAtMessageIndex:(NSUInteger)messageIndex error:(NSError**)error; -{ - size_t length = olm_export_inbound_group_session_length(session); - NSMutableData *key = [NSMutableData dataWithLength:length]; - size_t result = olm_export_inbound_group_session(session, key.mutableBytes, key.length, (uint32_t)messageIndex); - 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:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; - } - return nil; - } - NSString *keyString = [[NSString alloc] initWithData:key encoding:NSUTF8StringEncoding]; - [key resetBytesInRange:NSMakeRange(0, key.length)]; - return keyString; -} - - -#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:OLMErrorDomain 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); - [pickle resetBytesInRange:NSMakeRange(0, 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:OLMErrorDomain 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:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; - } - return nil; - } - NSString *pickleString = [[NSString alloc] initWithData:pickled encoding:NSUTF8StringEncoding]; - [pickled resetBytesInRange:NSMakeRange(0, pickled.length)]; - 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 diff --git a/xcode/OLMKit/OLMKit.h b/xcode/OLMKit/OLMKit.h deleted file mode 100644 index 54496a0..0000000 --- a/xcode/OLMKit/OLMKit.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - 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 - -// In this header, you should import all the public headers of your framework using statements like #import - -#import -#import -#import -#import -#import -#import -#import -#import -#import -#import - -@interface OLMKit : NSObject - -//! Project version string for OLMKit, the same as libolm. -+ (NSString*)versionString; - -@end diff --git a/xcode/OLMKit/OLMKit.m b/xcode/OLMKit/OLMKit.m deleted file mode 100644 index c383650..0000000 --- a/xcode/OLMKit/OLMKit.m +++ /dev/null @@ -1,33 +0,0 @@ -/* - 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 "OLMKit.h" - -#include "olm/olm.h" - -@implementation OLMKit - -+ (NSString*)versionString -{ - uint8_t major, minor, patch; - - olm_get_library_version(&major, &minor, &patch); - - return [NSString stringWithFormat:@"%tu.%tu.%tu", major, minor, patch]; -} - -@end diff --git a/xcode/OLMKit/OLMMessage.h b/xcode/OLMKit/OLMMessage.h deleted file mode 100644 index b6e8c8f..0000000 --- a/xcode/OLMKit/OLMMessage.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - 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 - -/* - from olm.hh - static const size_t OLM_MESSAGE_TYPE_PRE_KEY = 0; - static const size_t OLM_MESSAGE_TYPE_MESSAGE = 1; - */ -typedef NS_ENUM(NSInteger, OLMMessageType) { - OLMMessageTypePreKey = 0, - OLMMessageTypeMessage = 1 -}; - -@interface OLMMessage : NSObject - -@property (nonatomic, copy, readonly, nonnull) NSString *ciphertext; -@property (readonly) OLMMessageType type; - -- (nullable instancetype) initWithCiphertext:(nonnull NSString*)ciphertext type:(OLMMessageType)type; - -@end diff --git a/xcode/OLMKit/OLMMessage.m b/xcode/OLMKit/OLMMessage.m deleted file mode 100644 index 949f834..0000000 --- a/xcode/OLMKit/OLMMessage.m +++ /dev/null @@ -1,34 +0,0 @@ -/* - 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 "OLMMessage.h" - -@implementation OLMMessage - -- (nullable instancetype) initWithCiphertext:(nonnull NSString*)ciphertext type:(OLMMessageType)type { - NSParameterAssert(ciphertext != nil); - self = [super init]; - if (!self) { - return nil; - } - _ciphertext = [ciphertext copy]; - _type = type; - return self; -} - -@end diff --git a/xcode/OLMKit/OLMOutboundGroupSession.h b/xcode/OLMKit/OLMOutboundGroupSession.h deleted file mode 100644 index c979b61..0000000 --- a/xcode/OLMKit/OLMOutboundGroupSession.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - 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 -#import "OLMSerializable.h" - -@interface OLMOutboundGroupSession : NSObject - -- (instancetype) initOutboundGroupSession; - -- (NSString*)sessionIdentifier; -- (NSUInteger)messageIndex; -- (NSString*)sessionKey; - -/** UTF-8 plaintext -> base64 ciphertext */ -- (NSString*)encryptMessage:(NSString*)message error:(NSError**)error; - -@end diff --git a/xcode/OLMKit/OLMOutboundGroupSession.m b/xcode/OLMKit/OLMOutboundGroupSession.m deleted file mode 100644 index a0a7cc6..0000000 --- a/xcode/OLMKit/OLMOutboundGroupSession.m +++ /dev/null @@ -1,222 +0,0 @@ -/* - 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 "OLMOutboundGroupSession.h" - -#import "OLMUtility.h" -#include "olm/olm.h" - -@interface OLMOutboundGroupSession () -{ - OlmOutboundGroupSession *session; -} -@end - -@implementation OLMOutboundGroupSession - -- (void)dealloc { - olm_clear_outbound_group_session(session); - free(session); -} - -- (instancetype)init { - self = [super init]; - if (self) - { - session = malloc(olm_outbound_group_session_size()); - if (session) { - session = olm_outbound_group_session(session); - } - - if (!session) { - return nil; - } - } - return self; -} - -- (instancetype)initOutboundGroupSession { - self = [self init]; - if (self) { - NSMutableData *random = [OLMUtility randomBytesOfLength:olm_init_outbound_group_session_random_length(session)]; - - size_t result = olm_init_outbound_group_session(session, random.mutableBytes, random.length); - [random resetBytesInRange:NSMakeRange(0, random.length)]; - if (result == olm_error()) { - const char *error = olm_outbound_group_session_last_error(session); - NSLog(@"olm_init_outbound_group_session error: %s", error); - return nil; - } - } - return self; -} - -- (NSString *)sessionIdentifier { - size_t length = olm_outbound_group_session_id_length(session); - NSMutableData *idData = [NSMutableData dataWithLength:length]; - if (!idData) { - return nil; - } - 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); - NSLog(@"olm_outbound_group_session_id error: %s", error); - return nil; - } - NSString *idString = [[NSString alloc] initWithData:idData encoding:NSUTF8StringEncoding]; - return idString; -} - -- (NSUInteger)messageIndex { - return olm_outbound_group_session_message_index(session); -} - -- (NSString *)sessionKey { - size_t length = olm_outbound_group_session_key_length(session); - NSMutableData *sessionKeyData = [NSMutableData dataWithLength:length]; - if (!sessionKeyData) { - return nil; - } - 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); - NSLog(@"olm_outbound_group_session_key error: %s", error); - return nil; - } - NSString *sessionKey = [[NSString alloc] initWithData:sessionKeyData encoding:NSUTF8StringEncoding]; - [sessionKeyData resetBytesInRange:NSMakeRange(0, sessionKeyData.length)]; - return sessionKey; -} - -- (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]; - if (!ciphertext) { - return nil; - } - size_t result = olm_group_encrypt(session, plaintextData.bytes, plaintextData.length, ciphertext.mutableBytes, ciphertext.length); - if (result == olm_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: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_group_encrypt error: %@", errorString] - }]; - } - - return nil; - } - return [[NSString alloc] initWithData:ciphertext encoding:NSUTF8StringEncoding]; -} - -#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:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}]; - } - return nil; - } - NSMutableData *pickle = [serializedData dataUsingEncoding:NSUTF8StringEncoding].mutableCopy; - size_t result = olm_unpickle_outbound_group_session(session, key.bytes, key.length, pickle.mutableBytes, pickle.length); - [pickle resetBytesInRange:NSMakeRange(0, pickle.length)]; - if (result == olm_error()) { - const char *olm_error = olm_outbound_group_session_last_error(session); - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - if (error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain 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_outbound_group_session_length(session); - NSMutableData *pickled = [NSMutableData dataWithLength:length]; - size_t result = olm_pickle_outbound_group_session(session, key.bytes, key.length, pickled.mutableBytes, pickled.length); - if (result == olm_error()) { - const char *olm_error = olm_outbound_group_session_last_error(session); - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - if (error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; - } - return nil; - } - NSString *pickleString = [[NSString alloc] initWithData:pickled encoding:NSUTF8StringEncoding]; - [pickled resetBytesInRange:NSMakeRange(0, pickled.length)]; - 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 diff --git a/xcode/OLMKit/OLMPkDecryption.h b/xcode/OLMKit/OLMPkDecryption.h deleted file mode 100644 index 823dc78..0000000 --- a/xcode/OLMKit/OLMPkDecryption.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - Copyright 2018 New Vector 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 - -#import "OLMSerializable.h" -#import "OLMPkMessage.h" - -NS_ASSUME_NONNULL_BEGIN - -@interface OLMPkDecryption : NSObject - -/** - Initialise the key from the private part of a key as returned by `privateKey`. - - Note that the pubkey is a base64 encoded string, but the private key is - an unencoded byte array. - - @param privateKey the private key part. - @param error the error if any. - @return the associated public key. - */ -- (NSString *)setPrivateKey:(NSData*)privateKey error:(NSError* _Nullable *)error; - -/** - Generate a new key to use for decrypting messages. - - @param error the error if any. - @return the public part of the generated key. - */ -- (NSString *)generateKey:(NSError* _Nullable *)error; - -/** - Get the private key. - - @return the private key; - */ -- (NSData *)privateKey; - -/** - Decrypt a ciphertext. - - @param message the cipher message to decrypt. - @param error the error if any. - @return the decrypted message. - */ -- (NSString *)decryptMessage:(OLMPkMessage*)message error:(NSError* _Nullable *)error; - -/** - Private key length. - - @return the length in bytes. - */ -+ (NSUInteger)privateKeyLength; - -@end - -NS_ASSUME_NONNULL_END diff --git a/xcode/OLMKit/OLMPkDecryption.m b/xcode/OLMKit/OLMPkDecryption.m deleted file mode 100644 index 4af2c71..0000000 --- a/xcode/OLMKit/OLMPkDecryption.m +++ /dev/null @@ -1,299 +0,0 @@ -/* - Copyright 2018 New Vector 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 "OLMPkDecryption.h" - -#include "olm/olm.h" -#include "olm/pk.h" -#include "OLMUtility.h" - -@interface OLMPkDecryption () -{ - OlmPkDecryption *session; -} -@end - -@implementation OLMPkDecryption - -- (void)dealloc { - olm_clear_pk_decryption(session); - free(session); -} - -- (instancetype)init { - self = [super init]; - if (self) { - session = (OlmPkDecryption *)malloc(olm_pk_decryption_size()); - olm_pk_decryption(session); - } - return self; -} - -- (NSString *)setPrivateKey:(NSData *)privateKey error:(NSError *__autoreleasing _Nullable *)error { - size_t publicKeyLength = olm_pk_key_length(); - NSMutableData *publicKeyData = [NSMutableData dataWithLength:publicKeyLength]; - if (!publicKeyData) { - return nil; - } - - size_t result = olm_pk_key_from_private(session, - publicKeyData.mutableBytes, publicKeyLength, - (void*)privateKey.bytes, privateKey.length); - if (result == olm_error()) { - const char *olm_error = olm_pk_decryption_last_error(session); - NSLog(@"[OLMPkDecryption] setPrivateKey: olm_pk_key_from_private error: %s", olm_error); - - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - if (error && olm_error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain - code:0 - userInfo:@{ - NSLocalizedDescriptionKey: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_pk_key_from_private error: %@", errorString] - }]; - } - return nil; - } - - NSString *publicKey = [[NSString alloc] initWithData:publicKeyData encoding:NSUTF8StringEncoding]; - return publicKey; -} - -- (NSString *)generateKey:(NSError *__autoreleasing _Nullable *)error { - size_t randomLength = olm_pk_private_key_length(); - NSMutableData *random = [OLMUtility randomBytesOfLength:randomLength]; - if (!random) { - return nil; - } - - size_t publicKeyLength = olm_pk_key_length(); - NSMutableData *publicKeyData = [NSMutableData dataWithLength:publicKeyLength]; - if (!publicKeyData) { - return nil; - } - - size_t result = olm_pk_key_from_private(session, - publicKeyData.mutableBytes, publicKeyData.length, - random.mutableBytes, randomLength); - [random resetBytesInRange:NSMakeRange(0, randomLength)]; - if (result == olm_error()) { - const char *olm_error = olm_pk_decryption_last_error(session); - NSLog(@"[OLMPkDecryption] generateKey: olm_pk_key_from_private error: %s", olm_error); - - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - if (error && olm_error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain - code:0 - userInfo:@{ - NSLocalizedDescriptionKey: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_pk_key_from_private error: %@", errorString] - }]; - } - return nil; - } - - NSString *publicKey = [[NSString alloc] initWithData:publicKeyData encoding:NSUTF8StringEncoding]; - return publicKey; -} - -- (NSData *)privateKey { - size_t privateKeyLength = olm_pk_private_key_length(); - NSMutableData *privateKeyData = [NSMutableData dataWithLength:privateKeyLength]; - if (!privateKeyData) { - return nil; - } - - size_t result = olm_pk_get_private_key(session, - privateKeyData.mutableBytes, privateKeyLength); - if (result == olm_error()) { - const char *olm_error = olm_pk_decryption_last_error(session); - NSLog(@"[OLMPkDecryption] privateKey: olm_pk_get_private_key error: %s", olm_error); - return nil; - } - - NSData *privateKey = [privateKeyData copy]; - [privateKeyData resetBytesInRange:NSMakeRange(0, privateKeyData.length)]; - - return privateKey; -} - -- (NSString *)decryptMessage:(OLMPkMessage *)message error:(NSError *__autoreleasing _Nullable *)error { - NSData *messageData = [message.ciphertext dataUsingEncoding:NSUTF8StringEncoding]; - NSData *macData = [message.mac dataUsingEncoding:NSUTF8StringEncoding]; - NSData *ephemeralKeyData = [message.ephemeralKey dataUsingEncoding:NSUTF8StringEncoding]; - if (!messageData || !macData || !ephemeralKeyData) { - return nil; - } - - NSMutableData *mutMessage = messageData.mutableCopy; - size_t maxPlaintextLength = olm_pk_max_plaintext_length(session, mutMessage.length); - if (maxPlaintextLength == olm_error()) { - const char *olm_error = olm_pk_decryption_last_error(session); - - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - NSLog(@"[OLMPkDecryption] decryptMessage: olm_pk_max_plaintext_length error: %@", errorString); - - if (error && olm_error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain - code:0 - userInfo:@{ - NSLocalizedDescriptionKey: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_pk_max_plaintext_length error: %@", errorString] - }]; - } - - return nil; - } - - mutMessage = messageData.mutableCopy; - NSMutableData *plaintextData = [NSMutableData dataWithLength:maxPlaintextLength]; - size_t plaintextLength = olm_pk_decrypt(session, - ephemeralKeyData.bytes, ephemeralKeyData.length, - macData.bytes, macData.length, - mutMessage.mutableBytes, mutMessage.length, - plaintextData.mutableBytes, plaintextData.length); - if (plaintextLength == olm_error()) { - const char *olm_error = olm_pk_decryption_last_error(session); - - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - NSLog(@"[OLMPkDecryption] decryptMessage: olm_pk_decrypt error: %@", errorString); - - if (error && olm_error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain - code:0 - userInfo:@{ - NSLocalizedDescriptionKey: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_decrypt error: %@", errorString] - }]; - } - - return nil; - } - - plaintextData.length = plaintextLength; - NSString *plaintext = [[NSString alloc] initWithData:plaintextData encoding:NSUTF8StringEncoding]; - [plaintextData resetBytesInRange:NSMakeRange(0, plaintextData.length)]; - return plaintext; -} - -+ (NSUInteger)privateKeyLength { - return olm_pk_private_key_length(); -} - -#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:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}]; - } - return nil; - } - - size_t ephemeralLength = olm_pk_key_length(); - NSMutableData *ephemeralBuffer = [NSMutableData dataWithLength:ephemeralLength]; - - NSMutableData *pickle = [serializedData dataUsingEncoding:NSUTF8StringEncoding].mutableCopy; - size_t result = olm_unpickle_pk_decryption(session, - key.bytes, key.length, - pickle.mutableBytes, pickle.length, - ephemeralBuffer.mutableBytes, ephemeralLength); - [pickle resetBytesInRange:NSMakeRange(0, pickle.length)]; - if (result == olm_error()) { - const char *olm_error = olm_pk_decryption_last_error(session); - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - if (error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain 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_pk_decryption_length(session); - NSMutableData *pickled = [NSMutableData dataWithLength:length]; - - size_t result = olm_pickle_pk_decryption(session, - key.bytes, key.length, - pickled.mutableBytes, pickled.length); - if (result == olm_error()) { - const char *olm_error = olm_pk_decryption_last_error(session); - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - if (error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; - } - return nil; - } - - NSString *pickleString = [[NSString alloc] initWithData:pickled encoding:NSUTF8StringEncoding]; - [pickled resetBytesInRange:NSMakeRange(0, pickled.length)]; - - 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 diff --git a/xcode/OLMKit/OLMPkEncryption.h b/xcode/OLMKit/OLMPkEncryption.h deleted file mode 100644 index 6ae767c..0000000 --- a/xcode/OLMKit/OLMPkEncryption.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - Copyright 2018 New Vector 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 - -#import "OLMPkMessage.h" - -NS_ASSUME_NONNULL_BEGIN - -@interface OLMPkEncryption : NSObject - -/** - Set the recipient's public key for encrypting to. - - @param recipientKey the recipient's public key. - */ -- (void)setRecipientKey:(NSString*)recipientKey; - -/** - Encrypt a plaintext for the recipient. - - @param message the message to encrypt. - @param error the error if any. - @return the encrypted message. - */ -- (OLMPkMessage *)encryptMessage:(NSString*)message error:(NSError* _Nullable *)error; - -@end - -NS_ASSUME_NONNULL_END diff --git a/xcode/OLMKit/OLMPkEncryption.m b/xcode/OLMKit/OLMPkEncryption.m deleted file mode 100644 index 34ad57c..0000000 --- a/xcode/OLMKit/OLMPkEncryption.m +++ /dev/null @@ -1,111 +0,0 @@ -/* - Copyright 2018 New Vector 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 "OLMPkEncryption.h" - -#include "olm/olm.h" -#include "olm/pk.h" -#include "OLMUtility.h" - -@interface OLMPkEncryption () -{ - OlmPkEncryption *session; -} -@end - -@implementation OLMPkEncryption - -- (void)dealloc { - olm_clear_pk_encryption(session); - free(session); -} - - -- (instancetype)init { - self = [super init]; - if (self) { - session = (OlmPkEncryption *)malloc(olm_pk_encryption_size()); - olm_pk_encryption(session); - } - return self; -} - -- (void)setRecipientKey:(NSString*)recipientKey { - NSData *recipientKeyData = [recipientKey dataUsingEncoding:NSUTF8StringEncoding]; - olm_pk_encryption_set_recipient_key(session, recipientKeyData.bytes, recipientKeyData.length); -} - -- (OLMPkMessage *)encryptMessage:(NSString *)message error:(NSError *__autoreleasing _Nullable *)error { - NSData *plaintextData = [message dataUsingEncoding:NSUTF8StringEncoding]; - - size_t randomLength = olm_pk_encrypt_random_length(session); - NSMutableData *random = [OLMUtility randomBytesOfLength:randomLength]; - if (!random) { - return nil; - } - - size_t ciphertextLength = olm_pk_ciphertext_length(session, plaintextData.length); - NSMutableData *ciphertext = [NSMutableData dataWithLength:ciphertextLength]; - if (!ciphertext) { - return nil; - } - - size_t macLength = olm_pk_mac_length(session); - NSMutableData *macData = [NSMutableData dataWithLength:macLength]; - if (!macData) { - return nil; - } - - size_t ephemeralKeyLength = olm_pk_key_length(); - NSMutableData *ephemeralKeyData = [NSMutableData dataWithLength:ephemeralKeyLength]; - if (!ephemeralKeyData) { - return nil; - } - - size_t result = olm_pk_encrypt(session, - plaintextData.bytes, plaintextData.length, - ciphertext.mutableBytes, ciphertext.length, - macData.mutableBytes, macLength, - ephemeralKeyData.mutableBytes, ephemeralKeyLength, - random.mutableBytes, randomLength); - if (result == olm_error()) { - const char *olm_error = olm_pk_encryption_last_error(session); - - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - NSLog(@"[OLMPkEncryption] encryptMessage: olm_group_encrypt error: %@", errorString); - - if (error && olm_error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain - code:0 - userInfo:@{ - NSLocalizedDescriptionKey: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_group_encrypt error: %@", errorString] - }]; - } - - return nil; - } - - OLMPkMessage *encryptedMessage = [[OLMPkMessage alloc] - initWithCiphertext:[[NSString alloc] initWithData:ciphertext encoding:NSUTF8StringEncoding] - mac:[[NSString alloc] initWithData:macData encoding:NSUTF8StringEncoding] - ephemeralKey:[[NSString alloc] initWithData:ephemeralKeyData encoding:NSUTF8StringEncoding]]; - - - return encryptedMessage; -} - -@end diff --git a/xcode/OLMKit/OLMPkMessage.h b/xcode/OLMKit/OLMPkMessage.h deleted file mode 100644 index 1559fca..0000000 --- a/xcode/OLMKit/OLMPkMessage.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - Copyright 2018 New Vector 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 - -NS_ASSUME_NONNULL_BEGIN - -@interface OLMPkMessage : NSObject - -@property (nonatomic, copy, readonly) NSString *ciphertext; -@property (nonatomic, copy, readonly,) NSString *mac; -@property (nonatomic, copy, readonly) NSString *ephemeralKey; - -- (instancetype) initWithCiphertext:(NSString*)ciphertext mac:(NSString*)mac ephemeralKey:(NSString*)ephemeralKey; - -@end - -NS_ASSUME_NONNULL_END diff --git a/xcode/OLMKit/OLMPkMessage.m b/xcode/OLMKit/OLMPkMessage.m deleted file mode 100644 index 0f24512..0000000 --- a/xcode/OLMKit/OLMPkMessage.m +++ /dev/null @@ -1,32 +0,0 @@ -/* - Copyright 2018 New Vector 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 "OLMPkMessage.h" - -@implementation OLMPkMessage - -- (instancetype)initWithCiphertext:(NSString *)ciphertext mac:(NSString *)mac ephemeralKey:(NSString *)ephemeralKey { - self = [super init]; - if (!self) { - return nil; - } - _ciphertext = [ciphertext copy]; - _mac = [mac copy]; - _ephemeralKey = [ephemeralKey copy]; - return self; -} - -@end diff --git a/xcode/OLMKit/OLMPkSigning.h b/xcode/OLMKit/OLMPkSigning.h deleted file mode 100644 index 09724e1..0000000 --- a/xcode/OLMKit/OLMPkSigning.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - Copyright 2019 New Vector 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 - -NS_ASSUME_NONNULL_BEGIN - -@interface OLMPkSigning : NSObject - -/** - Initialise the signing object with a public/private keypair from a seed. - - @param seed the seed. - @param error the error if any. - @return the public key - */ -- (NSString *)doInitWithSeed:(NSData*)seed error:(NSError* _Nullable *)error; - -/** - Sign a message. - - @param message the message to sign. - @param error the error if any. - @return the signature. - */ -- (NSString *)sign:(NSString*)message error:(NSError* _Nullable *)error; - -/** - Generate a seed. - - @return the generated seed. - */ -+ (NSData *)generateSeed; - -@end - -NS_ASSUME_NONNULL_END diff --git a/xcode/OLMKit/OLMPkSigning.m b/xcode/OLMKit/OLMPkSigning.m deleted file mode 100644 index d5c7d09..0000000 --- a/xcode/OLMKit/OLMPkSigning.m +++ /dev/null @@ -1,125 +0,0 @@ -/* - Copyright 2019 New Vector 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 "OLMPkSigning.h" - -#include "olm/olm.h" -#include "olm/pk.h" -#include "OLMUtility.h" - -@interface OLMPkSigning () -{ - OlmPkSigning *sign; -} -@end - -@implementation OLMPkSigning - -- (void)dealloc { - olm_clear_pk_signing(sign); - free(sign); -} - - -- (instancetype)init { - self = [super init]; - if (self) { - sign = (OlmPkSigning *)malloc(olm_pk_signing_size()); - olm_pk_signing(sign); - } - return self; -} - -- (NSString *)doInitWithSeed:(NSData *)seed error:(NSError *__autoreleasing _Nullable *)error { - size_t publicKeyLength = olm_pk_signing_public_key_length(); - NSMutableData *publicKeyData = [NSMutableData dataWithLength:publicKeyLength]; - if (!publicKeyData) { - return nil; - } - - NSMutableData *mutableSeed = [NSMutableData dataWithData:seed]; - - size_t result = olm_pk_signing_key_from_seed(sign, - publicKeyData.mutableBytes, publicKeyLength, - mutableSeed.mutableBytes, mutableSeed.length); - if (result == olm_error()) { - const char *olm_error = olm_pk_signing_last_error(sign); - - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - NSLog(@"[OLMPkSigning] doInitWithSeed: olm_pk_signing_key_from_seed error: %@", errorString); - - if (error && olm_error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain - code:0 - userInfo:@{ - NSLocalizedDescriptionKey: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_pk_signing_key_from_seed error: %@", errorString] - }]; - } - - return nil; - } - - [mutableSeed resetBytesInRange:NSMakeRange(0, mutableSeed.length)]; - - NSString *publicKey = [[NSString alloc] initWithData:publicKeyData encoding:NSUTF8StringEncoding]; - return publicKey; -} - -- (NSString *)sign:(NSString *)message error:(NSError *__autoreleasing _Nullable *)error { - NSData *messageData = [message dataUsingEncoding:NSUTF8StringEncoding]; - - size_t signatureLength = olm_pk_signature_length(); - NSMutableData *signatureData = [NSMutableData dataWithLength:signatureLength]; - if (!signatureData) { - return nil; - } - - size_t result = olm_pk_sign(sign, - messageData.bytes, messageData.length, - signatureData.mutableBytes, signatureLength); - if (result == olm_error()) { - const char *olm_error = olm_pk_signing_last_error(sign); - - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - NSLog(@"[OLMPkSigning] sign: olm_pk_sign error: %@", errorString); - - if (error && olm_error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain - code:0 - userInfo:@{ - NSLocalizedDescriptionKey: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_pk_sign error: %@", errorString] - }]; - } - - return nil; - } - - NSString *signature = [[NSString alloc] initWithData:signatureData encoding:NSUTF8StringEncoding]; - return signature; -} - -+ (NSData *)generateSeed { - size_t seedLength = olm_pk_signing_seed_length(); - NSMutableData *seed = [OLMUtility randomBytesOfLength:seedLength]; - if (!seed) { - return nil; - } - - return seed; -} - -@end diff --git a/xcode/OLMKit/OLMSAS.h b/xcode/OLMKit/OLMSAS.h deleted file mode 100644 index 3785b03..0000000 --- a/xcode/OLMKit/OLMSAS.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - Copyright 2019 New Vector 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 - -NS_ASSUME_NONNULL_BEGIN - -/** - Short Authentication String verification utility class. - */ -@interface OLMSAS : NSObject - -/** - Get the public key of the SAS object. - */ -- (NSString * _Nullable)publicKey; - -/** - Set the public key of other user. - - @param theirPublicKey the other user's public key. - @return error the error if any. - */ -- (NSError* _Nullable)setTheirPublicKey:(NSString*)theirPublicKey; - -/** - Generate bytes to use for the short authentication string. - - @param info extra information to mix in when generating the bytes, as per the Matrix spec. - @param length the size of the output buffer. For hex-based SAS as in the Matrix spec, this will be 5. - @return generated bytes - */ -- (NSData *)generateBytes:(NSString*)info length:(NSUInteger)length; - -/** - Generate a message authentication code (MAC) based on the shared secret. - - @param input the message to produce the authentication code for. - @param info extra information to mix in when generating the MAC, as per the Matrix spec. - @param error the error if any. - @return the MAC. - */ -- (NSString *)calculateMac:(NSString*)input info:(NSString*)info error:(NSError* _Nullable *)error; - -/** - Generate a message authentication code (MAC) based on the shared secret. - For compatibility with an old version of olm.js. - - @param input the message to produce the authentication code for. - @param info extra information to mix in when generating the MAC, as per the Matrix spec. - @param error the error if any. - @return the MAC. - */ -- (NSString *)calculateMacLongKdf:(NSString*)input info:(NSString*)info error:(NSError* _Nullable *)error; - -@end - -NS_ASSUME_NONNULL_END diff --git a/xcode/OLMKit/OLMSAS.m b/xcode/OLMKit/OLMSAS.m deleted file mode 100644 index fed370b..0000000 --- a/xcode/OLMKit/OLMSAS.m +++ /dev/null @@ -1,174 +0,0 @@ -/* - Copyright 2018 New Vector 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 "OLMSAS.h" - -#include "olm/olm.h" -#include "olm/sas.h" -#include "OLMUtility.h" - -@interface OLMSAS () { - void *olmSASbuffer; - OlmSAS *olmSAS; -} -@end - -@implementation OLMSAS - -- (void)dealloc { - olm_clear_sas(olmSAS); - free(olmSASbuffer); -} - -- (instancetype)init { - self = [super init]; - if (self) { - olmSASbuffer = malloc(olm_sas_size()); - olmSAS = olm_sas(olmSASbuffer); - - size_t randomLength = olm_create_sas_random_length(olmSAS); - NSMutableData *random = [OLMUtility randomBytesOfLength:randomLength]; - if (!random) { - return nil; - } - - olm_create_sas(olmSAS, random.mutableBytes, randomLength); - - [random resetBytesInRange:NSMakeRange(0, randomLength)]; - } - return self; -} - -- (NSString * _Nullable)publicKey { - size_t publicKeyLength = olm_sas_pubkey_length(olmSAS); - NSMutableData *publicKeyData = [NSMutableData dataWithLength:publicKeyLength]; - if (!publicKeyData) { - return nil; - } - - size_t result = olm_sas_get_pubkey(olmSAS, publicKeyData.mutableBytes, publicKeyLength); - if (result == olm_error()) { - const char *olm_error = olm_sas_last_error(olmSAS); - NSLog(@"[OLMSAS] publicKey: olm_sas_get_pubkey error: %s", olm_error); - return nil; - } - - NSString *publicKey = [[NSString alloc] initWithData:publicKeyData encoding:NSUTF8StringEncoding]; - return publicKey; -} - -- (NSError * _Nullable)setTheirPublicKey:(NSString*)theirPublicKey { - NSMutableData *theirPublicKeyData = [theirPublicKey dataUsingEncoding:NSUTF8StringEncoding].mutableCopy; - - size_t result = olm_sas_set_their_key(olmSAS, theirPublicKeyData.mutableBytes, theirPublicKeyData.length); - if (result == olm_error()) { - const char *olm_error = olm_sas_last_error(olmSAS); - NSLog(@"[OLMSAS] setTheirPublicKey: olm_sas_set_their_key error: %s", olm_error); - - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - if (olm_error && errorString) { - return [NSError errorWithDomain:OLMErrorDomain - code:0 - userInfo:@{ - NSLocalizedDescriptionKey: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_sas_set_their_key error: %@", errorString] - }]; - } - } - - return nil; -} - -- (NSData *)generateBytes:(NSString *)info length:(NSUInteger)length { - NSData *infoData = [info dataUsingEncoding:NSUTF8StringEncoding]; - - NSMutableData *bytes = [NSMutableData dataWithLength:length]; - if (!bytes) { - return nil; - } - - olm_sas_generate_bytes(olmSAS, infoData.bytes, infoData.length, bytes.mutableBytes, length); - return bytes; -} - -- (NSString *)calculateMac:(NSString *)input info:(NSString *)info error:(NSError *__autoreleasing _Nullable *)error { - NSMutableData *inputData = [input dataUsingEncoding:NSUTF8StringEncoding].mutableCopy; - NSData *infoData = [info dataUsingEncoding:NSUTF8StringEncoding]; - - size_t macLength = olm_sas_mac_length(olmSAS); - NSMutableData *macData = [NSMutableData dataWithLength:macLength]; - if (!macData) { - return nil; - } - - size_t result = olm_sas_calculate_mac(olmSAS, - inputData.mutableBytes, inputData.length, - infoData.bytes, infoData.length, - macData.mutableBytes, macLength); - if (result == olm_error()) { - const char *olm_error = olm_sas_last_error(olmSAS); - NSLog(@"[OLMSAS] calculateMac: olm_sas_calculate_mac error: %s", olm_error); - - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - if (error && olm_error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain - code:0 - userInfo:@{ - NSLocalizedDescriptionKey: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_sas_calculate_mac error: %@", errorString] - }]; - } - return nil; - } - - NSString *mac = [[NSString alloc] initWithData:macData encoding:NSUTF8StringEncoding]; - return mac; -} - -- (NSString *)calculateMacLongKdf:(NSString *)input info:(NSString *)info error:(NSError *__autoreleasing _Nullable *)error { - NSMutableData *inputData = [input dataUsingEncoding:NSUTF8StringEncoding].mutableCopy; - NSData *infoData = [info dataUsingEncoding:NSUTF8StringEncoding]; - - size_t macLength = olm_sas_mac_length(olmSAS); - NSMutableData *macData = [NSMutableData dataWithLength:macLength]; - if (!macData) { - return nil; - } - - size_t result = olm_sas_calculate_mac_long_kdf(olmSAS, - inputData.mutableBytes, inputData.length, - infoData.bytes, infoData.length, - macData.mutableBytes, macLength); - if (result == olm_error()) { - const char *olm_error = olm_sas_last_error(olmSAS); - NSLog(@"[OLMSAS] calculateMacLongKdf: olm_sas_calculate_mac error: %s", olm_error); - - NSString *errorString = [NSString stringWithUTF8String:olm_error]; - if (error && olm_error && errorString) { - *error = [NSError errorWithDomain:OLMErrorDomain - code:0 - userInfo:@{ - NSLocalizedDescriptionKey: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_sas_calculate_mac_long_kdf error: %@", errorString] - }]; - } - return nil; - } - - NSString *mac = [[NSString alloc] initWithData:macData encoding:NSUTF8StringEncoding]; - return mac; -} - -@end diff --git a/xcode/OLMKit/OLMSerializable.h b/xcode/OLMKit/OLMSerializable.h deleted file mode 100644 index e929903..0000000 --- a/xcode/OLMKit/OLMSerializable.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - 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 - -@protocol 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; - -/** Serializes and encrypts object data, outputs base64 blob */ -- (NSString*) serializeDataWithKey:(NSData*)key error:(NSError**)error; - -@end diff --git a/xcode/OLMKit/OLMSession.h b/xcode/OLMKit/OLMSession.h deleted file mode 100644 index 0446f98..0000000 --- a/xcode/OLMKit/OLMSession.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - 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 -#import "OLMSerializable.h" -#import "OLMAccount.h" -#import "OLMMessage.h" - -@interface OLMSession : NSObject - -- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey theirOneTimeKey:(NSString*)theirOneTimeKey error:(NSError**)error; - -- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSString*)oneTimeKeyMessage error:(NSError**)error; - -- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString*)oneTimeKeyMessage error:(NSError**)error; - -- (NSString*) sessionIdentifier; - -- (BOOL) matchesInboundSession:(NSString*)oneTimeKeyMessage; - -- (BOOL) matchesInboundSessionFrom:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString *)oneTimeKeyMessage; - -/** UTF-8 plaintext -> base64 ciphertext */ -- (OLMMessage*) encryptMessage:(NSString*)message error:(NSError**)error; - -/** base64 ciphertext -> UTF-8 plaintext */ -- (NSString*) decryptMessage:(OLMMessage*)message error:(NSError**)error; - -@end diff --git a/xcode/OLMKit/OLMSession.m b/xcode/OLMKit/OLMSession.m deleted file mode 100644 index fc58a08..0000000 --- a/xcode/OLMKit/OLMSession.m +++ /dev/null @@ -1,383 +0,0 @@ -/* - 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" -#import "OLMAccount_Private.h" -#import "OLMSession_Private.h" -#include "olm/olm.h" - -@implementation OLMSession - -- (void) dealloc { - olm_clear_session(_session); - free(_session); -} - -- (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) init { - self = [super init]; - if (!self) { - return nil; - } - BOOL success = [self initializeSessionMemory]; - 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; -} - -- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey theirOneTimeKey:(NSString*)theirOneTimeKey error:(NSError**)error { - 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); - [random resetBytesInRange:NSMakeRange(0, random.length)]; - if (result == olm_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: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_create_outbound_session error: %@", errorString] - }]; - } - - return nil; - } - return self; -} - -- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSString*)oneTimeKeyMessage error:(NSError**)error { - self = [self initWithAccount:account]; - if (!self) { - 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 *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: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_create_inbound_session error: %@", errorString] - }]; - } - - return nil; - } - return self; -} - -- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString*)oneTimeKeyMessage error:(NSError**)error { - self = [self initWithAccount:account]; - if (!self) { - 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 *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: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_create_inbound_session_from error: %@", errorString] - }]; - } - - 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); - NSLog(@"olm_session_id error: %s", error); - return nil; - } - NSString *idString = [[NSString alloc] initWithData:idData encoding:NSUTF8StringEncoding]; - return idString; -} - -- (BOOL)matchesInboundSession:(NSString *)oneTimeKeyMessage { - NSMutableData *otk = [NSMutableData dataWithData:[oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]]; - - size_t result = olm_matches_inbound_session(_session, otk.mutableBytes, 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]; - NSMutableData *otk = [NSMutableData dataWithData:[oneTimeKeyMessage dataUsingEncoding:NSUTF8StringEncoding]]; - - size_t result = olm_matches_inbound_session_from(_session, - idKey.bytes, idKey.length, - otk.mutableBytes, 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); - 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); - [random resetBytesInRange:NSMakeRange(0, random.length)]; - if (result == olm_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: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_encrypt error: %@", errorString] - }]; - } - - return nil; - } - NSString *ciphertextString = [[NSString alloc] initWithData:ciphertext encoding:NSUTF8StringEncoding]; - OLMMessage *encryptedMessage = [[OLMMessage alloc] initWithCiphertext:ciphertextString type:messageType]; - return encryptedMessage; -} - -- (NSString*) decryptMessage:(OLMMessage*)message error:(NSError**)error { - 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 *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: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_decrypt_max_plaintext_length error: %@", errorString] - }]; - } - - 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 *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: errorString, - NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:@"olm_decrypt error: %@", errorString] - }]; - } - - return nil; - } - plaintextData.length = plaintextLength; - NSString *plaintext = [[NSString alloc] initWithData:plaintextData encoding:NSUTF8StringEncoding]; - [plaintextData resetBytesInRange:NSMakeRange(0, plaintextData.length)]; - 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:OLMErrorDomain 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); - [pickle resetBytesInRange:NSMakeRange(0, 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:OLMErrorDomain 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:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; - } - return nil; - } - NSString *pickleString = [[NSString alloc] initWithData:pickled encoding:NSUTF8StringEncoding]; - [pickled resetBytesInRange:NSMakeRange(0, pickled.length)]; - 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 diff --git a/xcode/OLMKit/OLMSession_Private.h b/xcode/OLMKit/OLMSession_Private.h deleted file mode 100644 index 28ba5e1..0000000 --- a/xcode/OLMKit/OLMSession_Private.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - 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. - */ - -#include "olm/olm.h" - -@interface OLMSession() - -@property (nonatomic) OlmSession *session; -@property (nonatomic, strong) OLMAccount *account; - -@end diff --git a/xcode/OLMKit/OLMUtility.h b/xcode/OLMKit/OLMUtility.h deleted file mode 100644 index 3041da9..0000000 --- a/xcode/OLMKit/OLMUtility.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - 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 - -FOUNDATION_EXPORT NSString *const OLMErrorDomain; - -@interface OLMUtility : NSObject - -/** - Calculate the SHA-256 hash of the input and encodes it as base64. - - @param message the message to hash. - @return the base64-encoded hash value. - */ -- (NSString*)sha256:(NSData*)message; - -/** - Verify an ed25519 signature. - - @param signature the base64-encoded signature to be checked. - @param key the ed25519 key. - @param message the message which was signed. - @param error if there is a problem with the verification. - If the key was too small then the message will be "OLM.INVALID_BASE64". - If the signature was invalid then the message will be "OLM.BAD_MESSAGE_MAC". - - @return YES if valid. - */ -- (BOOL)verifyEd25519Signature:(NSString*)signature key:(NSString*)key message:(NSData*)message error:(NSError**)error; - -+ (NSMutableData*) randomBytesOfLength:(NSUInteger)length; - -@end diff --git a/xcode/OLMKit/OLMUtility.m b/xcode/OLMKit/OLMUtility.m deleted file mode 100644 index 936785a..0000000 --- a/xcode/OLMKit/OLMUtility.m +++ /dev/null @@ -1,121 +0,0 @@ -/* - 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 "OLMUtility.h" - -#include "olm/olm.h" - -NSString *const OLMErrorDomain = @"org.matrix.olm"; - -@interface OLMUtility() - -@property (nonatomic) OlmUtility *utility; - -@end - -@implementation OLMUtility - -- (void) dealloc { - olm_clear_utility(_utility); - free(_utility); -} - -- (BOOL) initializeUtilityMemory { - size_t utilitySize = olm_utility_size(); - _utility = malloc(utilitySize); - NSParameterAssert(_utility != nil); - if (!_utility) { - return NO; - } - _utility = olm_utility(_utility); - NSParameterAssert(_utility != nil); - if (!_utility) { - return NO; - } - return YES; -} - -- (instancetype) init { - self = [super init]; - if (!self) { - return nil; - } - BOOL success = [self initializeUtilityMemory]; - if (!success) { - return nil; - } - return self; -} - -- (NSString *)sha256:(NSData *)message { - size_t length = olm_sha256_length(_utility); - - NSMutableData *shaData = [NSMutableData dataWithLength:length]; - if (!shaData) { - return nil; - } - - 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); - NSLog(@"olm_sha256 error: %s", error); - return nil; - } - - NSString *sha = [[NSString alloc] initWithData:shaData encoding:NSUTF8StringEncoding]; - return sha; -} - -- (BOOL)verifyEd25519Signature:(NSString*)signature key:(NSString*)key message:(NSData*)message error:(NSError**)error { - - NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding]; - NSData *signatureData = [signature dataUsingEncoding:NSUTF8StringEncoding]; - - size_t result = olm_ed25519_verify(_utility, - keyData.bytes, keyData.length, - message.bytes, message.length, - (void*)signatureData.bytes, signatureData.length - ); - - if (result == olm_error()) { - if (error) { - NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: [NSString stringWithUTF8String:olm_utility_last_error(_utility)]}; - - // @TODO - *error = [[NSError alloc] initWithDomain:@"OLMKitErrorDomain" code:0 userInfo:userInfo]; - } - return NO; - } - else { - return YES; - } -} - -+ (NSMutableData*) randomBytesOfLength:(NSUInteger)length { - NSMutableData *randomData = [NSMutableData dataWithLength:length]; - if (!randomData) { - return nil; - } - int result = SecRandomCopyBytes(kSecRandomDefault, randomData.length, randomData.mutableBytes); - if (result != 0) { - return nil; - } - return randomData; -} - -@end -- cgit v1.2.3