Commit 23e04860 authored by Hubert Chathi's avatar Hubert Chathi
Browse files

Merge branch 'manu/olmkit_pickling_v2' into 'master'

OLMKit:  New pickle version using a pickle key provided externally

See merge request !17
parents c01164f0 4be7cc36
......@@ -21,6 +21,7 @@
#import "OLMSession.h"
#import "OLMSession_Private.h"
#import "OLMUtility.h"
#import "OLMKit.h"
@import Security;
......@@ -243,6 +244,13 @@
self = [self initWithSerializedData:pickle key:key error:&error];
}
else if ([version isEqualToString:@"2"]) {
NSString *pickle = [decoder decodeObjectOfClass:[NSString class] forKey:@"pickle"];
NSData *key = OLMKit.sharedInstance.pickleKeyDelegate.pickleKey;
NSParameterAssert(key);
self = [self initWithSerializedData:pickle key:key error:&error];
}
NSParameterAssert(error == nil);
NSParameterAssert(self != nil);
......@@ -254,14 +262,23 @@
}
- (void)encodeWithCoder:(NSCoder *)encoder {
NSData *key = [OLMUtility randomBytesOfLength:32];
NSData *key = OLMKit.sharedInstance.pickleKeyDelegate.pickleKey;
if (key)
{
[encoder encodeObject:@"2" forKey:@"version"];
}
else
{
key = [OLMUtility randomBytesOfLength:32];
[encoder encodeObject:key forKey:@"key"];
[encoder encodeObject:@"1" forKey:@"version"];
}
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"];
}
......
......@@ -19,6 +19,7 @@
#import "OLMUtility.h"
#include "olm/olm.h"
#import "OLMKit.h"
@interface OLMInboundGroupSession ()
{
......@@ -277,7 +278,14 @@
self = [self initWithSerializedData:pickle key:key error:&error];
}
else if ([version isEqualToString:@"2"]) {
NSString *pickle = [decoder decodeObjectOfClass:[NSString class] forKey:@"pickle"];
NSData *key = OLMKit.sharedInstance.pickleKeyDelegate.pickleKey;
NSParameterAssert(key);
self = [self initWithSerializedData:pickle key:key error:&error];
}
NSParameterAssert(error == nil);
NSParameterAssert(self != nil);
if (!self) {
......@@ -288,14 +296,23 @@
}
- (void)encodeWithCoder:(NSCoder *)encoder {
NSData *key = [OLMUtility randomBytesOfLength:32];
NSData *key = OLMKit.sharedInstance.pickleKeyDelegate.pickleKey;
if (key)
{
[encoder encodeObject:@"2" forKey:@"version"];
}
else
{
key = [OLMUtility randomBytesOfLength:32];
[encoder encodeObject:key forKey:@"key"];
[encoder encodeObject:@"1" forKey:@"version"];
}
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
......@@ -31,9 +31,31 @@
#import <OLMKit/OLMPkSigning.h>
#import <OLMKit/OLMSAS.h>
NS_ASSUME_NONNULL_BEGIN
/**
`OLMKitPickleKeyDelegate` provides the key to use for every pickle operation.
*/
@protocol OLMKitPickleKeyDelegate <NSObject>
- (NSData*)pickleKey;
@end
@interface OLMKit : NSObject
//! Project version string for OLMKit, the same as libolm.
/// Project version string for OLMKit, the same as libolm.
+ (NSString*)versionString;
/// The optional delegate that provides the pickle key.
/// If not provided, OLMKit will use default pickle keys.
@property (nonatomic, weak, nullable) id<OLMKitPickleKeyDelegate> pickleKeyDelegate;
/// The singleton instance.
+ (instancetype)sharedInstance;
@end
NS_ASSUME_NONNULL_END
......@@ -30,4 +30,15 @@
return [NSString stringWithFormat:@"%tu.%tu.%tu", major, minor, patch];
}
+ (instancetype)sharedInstance
{
static OLMKit *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
@end
......@@ -20,6 +20,8 @@
#import "OLMUtility.h"
#include "olm/olm.h"
#import "OLMKit.h"
@interface OLMOutboundGroupSession ()
{
OlmOutboundGroupSession *session;
......@@ -198,6 +200,14 @@
self = [self initWithSerializedData:pickle key:key error:&error];
}
else if ([version isEqualToString:@"2"]) {
NSString *pickle = [decoder decodeObjectOfClass:[NSString class] forKey:@"pickle"];
NSData *key = OLMKit.sharedInstance.pickleKeyDelegate.pickleKey;
NSParameterAssert(key);
self = [self initWithSerializedData:pickle key:key error:&error];
}
NSParameterAssert(error == nil);
NSParameterAssert(self != nil);
......@@ -209,14 +219,23 @@
}
- (void)encodeWithCoder:(NSCoder *)encoder {
NSData *key = [OLMUtility randomBytesOfLength:32];
NSData *key = OLMKit.sharedInstance.pickleKeyDelegate.pickleKey;
if (key)
{
[encoder encodeObject:@"2" forKey:@"version"];
}
else
{
key = [OLMUtility randomBytesOfLength:32];
[encoder encodeObject:key forKey:@"key"];
[encoder encodeObject:@"1" forKey:@"version"];
}
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
......@@ -19,6 +19,7 @@
#include "olm/olm.h"
#include "olm/pk.h"
#include "OLMUtility.h"
#import "OLMKit.h"
@interface OLMPkDecryption ()
{
......@@ -274,7 +275,14 @@
self = [self initWithSerializedData:pickle key:key error:&error];
}
else if ([version isEqualToString:@"2"]) {
NSString *pickle = [decoder decodeObjectOfClass:[NSString class] forKey:@"pickle"];
NSData *key = OLMKit.sharedInstance.pickleKeyDelegate.pickleKey;
NSParameterAssert(key);
self = [self initWithSerializedData:pickle key:key error:&error];
}
NSParameterAssert(error == nil);
NSParameterAssert(self != nil);
if (!self) {
......@@ -285,15 +293,23 @@
}
- (void)encodeWithCoder:(NSCoder *)encoder {
NSData *key = [OLMUtility randomBytesOfLength:32];
NSError *error = nil;
NSData *key = OLMKit.sharedInstance.pickleKeyDelegate.pickleKey;
if (key)
{
[encoder encodeObject:@"2" forKey:@"version"];
}
else
{
key = [OLMUtility randomBytesOfLength:32];
[encoder encodeObject:key forKey:@"key"];
[encoder encodeObject:@"1" forKey:@"version"];
}
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
......@@ -21,6 +21,7 @@
#import "OLMAccount_Private.h"
#import "OLMSession_Private.h"
#include "olm/olm.h"
#import "OLMKit.h"
@implementation OLMSession
......@@ -359,6 +360,13 @@
self = [self initWithSerializedData:pickle key:key error:&error];
}
else if ([version isEqualToString:@"2"]) {
NSString *pickle = [decoder decodeObjectOfClass:[NSString class] forKey:@"pickle"];
NSData *key = OLMKit.sharedInstance.pickleKeyDelegate.pickleKey;
NSParameterAssert(key);
self = [self initWithSerializedData:pickle key:key error:&error];
}
NSParameterAssert(error == nil);
NSParameterAssert(self != nil);
......@@ -370,14 +378,23 @@
}
- (void)encodeWithCoder:(NSCoder *)encoder {
NSData *key = [OLMUtility randomBytesOfLength:32];
NSData *key = OLMKit.sharedInstance.pickleKeyDelegate.pickleKey;
if (key)
{
[encoder encodeObject:@"2" forKey:@"version"];
}
else
{
key = [OLMUtility randomBytesOfLength:32];
[encoder encodeObject:key forKey:@"key"];
[encoder encodeObject:@"1" forKey:@"version"];
}
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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment