diff --git a/Package.swift b/Package.swift index 96e6c67..bfbe632 100644 --- a/Package.swift +++ b/Package.swift @@ -37,7 +37,7 @@ let package = Package( path: "xcode", exclude: [ "OLMKit/Info.plist" ], sources: [ "OLMKit" ], - publicHeadersPath: "OLMKit", + publicHeadersPath: "PublicHeaders", cSettings: [ .headerSearchPath("."), .unsafeFlags([ diff --git a/xcode/PublicHeaders/OLMKit/OLMAccount.h b/xcode/PublicHeaders/OLMKit/OLMAccount.h new file mode 100644 index 0000000..c8d65cd --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMAccount.h @@ -0,0 +1,51 @@ +/* + 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/PublicHeaders/OLMKit/OLMInboundGroupSession.h b/xcode/PublicHeaders/OLMKit/OLMInboundGroupSession.h new file mode 100644 index 0000000..c0d2c59 --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMInboundGroupSession.h @@ -0,0 +1,38 @@ +/* + 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/PublicHeaders/OLMKit/OLMKit.h b/xcode/PublicHeaders/OLMKit/OLMKit.h new file mode 100644 index 0000000..54496a0 --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMKit.h @@ -0,0 +1,39 @@ +/* + 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/PublicHeaders/OLMKit/OLMMessage.h b/xcode/PublicHeaders/OLMKit/OLMMessage.h new file mode 100644 index 0000000..b6e8c8f --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMMessage.h @@ -0,0 +1,38 @@ +/* + 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/PublicHeaders/OLMKit/OLMOutboundGroupSession.h b/xcode/PublicHeaders/OLMKit/OLMOutboundGroupSession.h new file mode 100644 index 0000000..c979b61 --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMOutboundGroupSession.h @@ -0,0 +1,32 @@ +/* + 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/PublicHeaders/OLMKit/OLMPkDecryption.h b/xcode/PublicHeaders/OLMKit/OLMPkDecryption.h new file mode 100644 index 0000000..823dc78 --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMPkDecryption.h @@ -0,0 +1,71 @@ +/* + 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/PublicHeaders/OLMKit/OLMPkEncryption.h b/xcode/PublicHeaders/OLMKit/OLMPkEncryption.h new file mode 100644 index 0000000..6ae767c --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMPkEncryption.h @@ -0,0 +1,42 @@ +/* + 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/PublicHeaders/OLMKit/OLMPkMessage.h b/xcode/PublicHeaders/OLMKit/OLMPkMessage.h new file mode 100644 index 0000000..1559fca --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMPkMessage.h @@ -0,0 +1,31 @@ +/* + 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/PublicHeaders/OLMKit/OLMPkSigning.h b/xcode/PublicHeaders/OLMKit/OLMPkSigning.h new file mode 100644 index 0000000..09724e1 --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMPkSigning.h @@ -0,0 +1,49 @@ +/* + 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/PublicHeaders/OLMKit/OLMSAS.h b/xcode/PublicHeaders/OLMKit/OLMSAS.h new file mode 100644 index 0000000..3785b03 --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMSAS.h @@ -0,0 +1,70 @@ +/* + 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/PublicHeaders/OLMKit/OLMSerializable.h b/xcode/PublicHeaders/OLMKit/OLMSerializable.h new file mode 100644 index 0000000..e929903 --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMSerializable.h @@ -0,0 +1,29 @@ +/* + 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/PublicHeaders/OLMKit/OLMSession.h b/xcode/PublicHeaders/OLMKit/OLMSession.h new file mode 100644 index 0000000..0446f98 --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMSession.h @@ -0,0 +1,44 @@ +/* + 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/PublicHeaders/OLMKit/OLMUtility.h b/xcode/PublicHeaders/OLMKit/OLMUtility.h new file mode 100644 index 0000000..3041da9 --- /dev/null +++ b/xcode/PublicHeaders/OLMKit/OLMUtility.h @@ -0,0 +1,49 @@ +/* + 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