Commit 1d06f2a4 authored by manuroe's avatar manuroe
Browse files

Merge remote-tracking branch 'OLMKit/olmkit' into olmkit

# Conflicts:
#	.gitignore
#	include/olm/olm.hh
#	javascript/README.md
parents 8a8d100e daab2a58
#
# Be sure to run `pod spec lint olm.podspec' to ensure this is a
# valid spec and to remove all comments including this before submitting the spec.
#
# To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#
Pod::Spec.new do |s|
# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# These will help people to find your library, and whilst it
# can feel like a chore to fill in it's definitely to your advantage. The
# summary should be tweet-length, and the description more in depth.
#
s.name = "olm"
s.version = "0.1.0"
s.summary = "olm"
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
s.description = <<-DESC
DESC
s.homepage = "http://EXAMPLE/olm"
# s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Licensing your code is important. See http://choosealicense.com for more info.
# CocoaPods will detect a license file if there is a named LICENSE*
# Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
#
s.license = "MIT (example)"
# s.license = { :type => "MIT", :file => "FILE_LICENSE" }
# ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the authors of the library, with email addresses. Email addresses
# of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
# accepts just a name if you'd rather not provide an email address.
#
# Specify a social_media_url where others can refer to, for example a twitter
# profile URL.
#
s.author = { "Chris Ballinger" => "chrisballinger@gmail.com" }
# Or just: s.author = "Chris Ballinger"
# s.authors = { "Chris Ballinger" => "chrisballinger@gmail.com" }
# s.social_media_url = "http://twitter.com/Chris Ballinger"
# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# If this Pod runs only on iOS or OS X, then specify the platform and
# the deployment target. You can optionally include the target after the platform.
#
# s.platform = :ios
# s.platform = :ios, "5.0"
# When using multiple platforms
# s.ios.deployment_target = "5.0"
# s.osx.deployment_target = "10.7"
# s.watchos.deployment_target = "2.0"
# s.tvos.deployment_target = "9.0"
# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Specify the location from where the source should be retrieved.
# Supports git, hg, bzr, svn and HTTP.
#
s.source = { :git => "http://EXAMPLE/olm.git", :tag => "0.0.1" }
# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# CocoaPods is smart about how it includes source code. For source files
# giving a folder will include any swift, h, m, mm, c & cpp files.
# For header files it will include any header in the folder.
# Not including the public_header_files will make all headers public.
#
s.source_files = ["include/olm/*.hh","src/*.cpp"]
s.public_header_files = "include/olm/olm.hh"
s.library = "c++"
s.xcconfig = { 'HEADER_SEARCH_PATHS' => '$(PODS_ROOT)/olm/include $(PODS_ROOT)/olm/lib $(PODS_ROOT)/../../include $(PODS_ROOT)/../../lib' }
end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
//
// OLMAccount.h
// olm
//
// Created by Chris Ballinger on 4/8/16.
//
//
#import <Foundation/Foundation.h>
#import "OLMSerializable.h"
@class OLMSession;
@interface OLMAccount : NSObject <OLMSerializable, NSSecureCoding>
/** 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) markKeysAsPublished;
/** 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
//
// OLMAccount.m
// olm
//
// Created by Chris Ballinger on 4/8/16.
//
//
#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);
if (accountResult == olm_error()) {
const char *error = olm_account_last_error(_account);
NSLog(@"error creating account: %s", error);
return nil;
}
return self;
}
- (size_t) 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;
}
- (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);
}
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);
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 nil;
}
size_t result = olm_remove_one_time_keys(self.account, session.session);
if (result == olm_error()) {
const char *error = olm_session_last_error(session.session);
NSAssert(NO, @"olm_remove_one_time_keys error: %s", error);
return NO;
}
return YES;
}
#pragma mark OLMSerializable
/** Initializes from encrypted serialized data. Will throw error if invalid key or invalid base64. */
- (instancetype) initWithSerializedData:(NSString*)serializedData key:(NSData*)key error:(NSError**)error {
self = [self init];
if (!self) {
return nil;
}
NSParameterAssert(key.length > 0);
NSParameterAssert(serializedData.length > 0);
if (key.length == 0 || serializedData.length == 0) {
if (error) {
*error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}];
}
return nil;
}
NSMutableData *pickle = [serializedData dataUsingEncoding:NSUTF8StringEncoding].mutableCopy;
size_t result = olm_unpickle_account(_account, key.bytes, key.length, pickle.mutableBytes, 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:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
}
return nil;
}
return self;
}
/** Serializes and encrypts object data, outputs base64 blob */
- (NSString*) serializeDataWithKey:(NSData*)key error:(NSError**)error {
NSParameterAssert(key.length > 0);
size_t length = olm_pickle_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:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}];
}
return nil;
}
NSString *pickleString = [[NSString alloc] initWithData:pickled encoding:NSUTF8StringEncoding];
return pickleString;
}
#pragma mark NSSecureCoding
+ (BOOL) supportsSecureCoding {
return YES;
}
#pragma mark NSCoding
- (id)initWithCoder:(NSCoder *)decoder {
NSString *version = [decoder decodeObjectOfClass:[NSString class] forKey:@"version"];
NSError *error = nil;
if ([version isEqualToString:@"1"]) {
NSString *pickle = [decoder decodeObjectOfClass:[NSString class] forKey:@"pickle"];
NSData *key = [decoder decodeObjectOfClass:[NSData class] forKey:@"key"];
self = [self initWithSerializedData:pickle key:key error:&error];
}
NSParameterAssert(error == nil);
NSParameterAssert(self != nil);
if (!self) {
return nil;
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
NSData *key = [OLMUtility randomBytesOfLength:32];
NSError *error = nil;
NSString *pickle = [self serializeDataWithKey:key error:&error];
NSParameterAssert(pickle.length > 0 && error == nil);
[encoder encodeObject:pickle forKey:@"pickle"];
[encoder encodeObject:key forKey:@"key"];
[encoder encodeObject:@"1" forKey:@"version"];
}
@end
//
// OLMAccount_Private.h
// olm
//
// Created by Chris Ballinger on 4/8/16.
//
//
@import olm;
@interface OLMAccount()
@property (nonatomic) OlmAccount *account;
@end
\ No newline at end of file
//
// OLMKit.h
// OLMKit
//
// Created by Chris Ballinger on 4/8/16.
//
//
#import <UIKit/UIKit.h>
//! Project version number for OLMKit.
FOUNDATION_EXPORT double OLMKitVersionNumber;
//! Project version string for OLMKit.
FOUNDATION_EXPORT const unsigned char OLMKitVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <OLMKit/PublicHeader.h>
#import "OLMAccount.h"
#import "OLMSession.h"
#import "OLMMessage.h"
#import "OLMUtility.h"
\ No newline at end of file
//
// OLMMessage.h
// olm
//
// Created by Chris Ballinger on 4/8/16.
//
//
#import <Foundation/Foundation.h>
/*
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
//
// OLMMessage.m
// olm
//
// Created by Chris Ballinger on 4/8/16.
//
//
#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
//
// OLMSerializable.h
// olm
//
// Created by Chris Ballinger on 4/8/16.
//
//
#import <Foundation/Foundation.h>
@protocol OLMSerializable <NSObject>
/** 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
//
// OLMSession.h
// olm
//
// Created by Chris Ballinger on 4/8/16.
//
//
#import <Foundation/Foundation.h>
#import "OLMSerializable.h"
#import "OLMAccount.h"
#import "OLMMessage.h"
@interface OLMSession : NSObject <OLMSerializable, NSSecureCoding>
- (instancetype) initOutboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey theirOneTimeKey:(NSString*)theirOneTimeKey;
- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account oneTimeKeyMessage:(NSString*)oneTimeKeyMessage;
- (instancetype) initInboundSessionWithAccount:(OLMAccount*)account theirIdentityKey:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString*)oneTimeKeyMessage;
- (NSString*) sessionIdentifier;
- (BOOL) matchesInboundSession:(NSString*)oneTimeKeyMessage;
- (BOOL) matchesInboundSessionFrom:(NSString*)theirIdentityKey oneTimeKeyMessage:(NSString *)oneTimeKeyMessage;
/** UTF-8 plaintext -> base64 ciphertext */
- (OLMMessage*) encryptMessage:(NSString*)message;
/** base64 ciphertext -> UTF-8 plaintext */
- (NSString*) decryptMessage:(OLMMessage*)message;
@end
//
// OLMSession.m
// olm
//
// Created by Chris Ballinger on 4/8/16.
//
//
#import "OLMSession.h"
#import "OLMUtility.h"
#import "OLMAccount_Private.h"
#import "OLMSession_Private.h"
@import olm;
@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 {
self = [self initWithAccount:account];
if (!self) {