Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
NSSecureCoding serialization of PreKeyBundles.
Browse files Browse the repository at this point in the history
  • Loading branch information
FredericJacobs committed Feb 15, 2015
1 parent 7122ea0 commit d47fad2
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 4 deletions.
59 changes: 59 additions & 0 deletions AxolotlKit Tests/PreKeyBundleTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// PreKeyBundleTests.m
// AxolotlKit
//
// Created by Frederic Jacobs on 15/02/15.
// Copyright (c) 2015 Frederic Jacobs. All rights reserved.
//

#import <XCTest/XCTest.h>

#import "PreKeyBundle.h"
#import <25519/Curve25519.h>

@interface PreKeyBundleTests : XCTestCase

@end

@implementation PreKeyBundleTests

- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}

- (void)testSerialization {
PreKeyBundle *bundle = [[PreKeyBundle alloc] initWithRegistrationId:1
deviceId:2
preKeyId:3
preKeyPublic:[Curve25519 generateKeyPair].publicKey
signedPreKeyPublic:[Curve25519 generateKeyPair].publicKey
signedPreKeyId:4
signedPreKeySignature:[Curve25519 generateKeyPair].publicKey
identityKey:[Curve25519 generateKeyPair].publicKey];


NSData *data = [NSKeyedArchiver archivedDataWithRootObject:bundle];

PreKeyBundle *bundle2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];

XCTAssertEqual(bundle.registrationId, bundle2.registrationId);
XCTAssertEqual(bundle.deviceId, bundle2.deviceId);
XCTAssertEqual(bundle.preKeyId, bundle2.preKeyId);
XCTAssertEqual(bundle.signedPreKeyId, bundle2.signedPreKeyId);


XCTAssert([bundle.preKeyPublic isEqualToData:bundle2.preKeyPublic]);
XCTAssert([bundle.signedPreKeyPublic isEqualToData:bundle2.signedPreKeyPublic]);
XCTAssert([bundle.signedPreKeySignature isEqualToData:bundle2.signedPreKeySignature]);
XCTAssert([bundle.identityKey isEqualToData:bundle2.identityKey]);

}


@end
2 changes: 1 addition & 1 deletion AxolotlKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "AxolotlKit"
s.version = "0.6.1"
s.version = "0.6.2"
s.summary = "AxolotlKit is a Free implementation of the Axolotl protocol in Objective-C"
s.homepage = "https://github.com/WhisperSystems/AxolotlKit"
s.license = "GPLv2"
Expand Down
4 changes: 4 additions & 0 deletions AxolotlKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
719C4E40588B7AD1F1DD22FC /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = AD9F87ECA041E727CC457415 /* libPods.a */; };
B619AF1419FC148D00E33198 /* ProtobuffsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B619AF1319FC148D00E33198 /* ProtobuffsTests.m */; };
B61E841E19D17C33004FC382 /* HKDFTest.m in Sources */ = {isa = PBXBuildFile; fileRef = B61E841C19D1780D004FC382 /* HKDFTest.m */; };
B62EFBF01A9140190072ADD3 /* PreKeyBundleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B62EFBEF1A9140190072ADD3 /* PreKeyBundleTests.m */; };
B63B2F9F197D8AFD00E09F65 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B63B2F9E197D8AFD00E09F65 /* Foundation.framework */; };
B63B2FA1197D8AFD00E09F65 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B63B2FA0197D8AFD00E09F65 /* CoreGraphics.framework */; };
B63B2FA3197D8AFD00E09F65 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B63B2FA2197D8AFD00E09F65 /* UIKit.framework */; };
Expand Down Expand Up @@ -72,6 +73,7 @@
AD9F87ECA041E727CC457415 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
B619AF1319FC148D00E33198 /* ProtobuffsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ProtobuffsTests.m; sourceTree = "<group>"; };
B61E841C19D1780D004FC382 /* HKDFTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HKDFTest.m; sourceTree = "<group>"; };
B62EFBEF1A9140190072ADD3 /* PreKeyBundleTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PreKeyBundleTests.m; sourceTree = "<group>"; };
B63B2F9D197D8AFD00E09F65 /* AxolotlKit.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AxolotlKit.app; sourceTree = BUILT_PRODUCTS_DIR; };
B63B2F9E197D8AFD00E09F65 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
B63B2FA0197D8AFD00E09F65 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -260,6 +262,7 @@
B63B2FDF197D8B9600E09F65 /* AxolotlKit Tests */ = {
isa = PBXGroup;
children = (
B62EFBEF1A9140190072ADD3 /* PreKeyBundleTests.m */,
B619AF1219FC147500E33198 /* Protobuffs */,
B61E841719D172BC004FC382 /* ECC */,
B61E841819D176D7004FC382 /* HKDF */,
Expand Down Expand Up @@ -682,6 +685,7 @@
B6A3490319F1A4B4008A3E01 /* AxolotlInMemoryStore.m in Sources */,
B63B2FE6197D8B9600E09F65 /* AxolotlKit_Tests.m in Sources */,
B654B03419FACCB6000458F0 /* RatchetingSessionTest.m in Sources */,
B62EFBF01A9140190072ADD3 /* PreKeyBundleTests.m in Sources */,
B6BB8C3519F7E773008A5895 /* SessionBuilderTests.m in Sources */,
B6E37AE019CF107C00E619CA /* ECCTests.m in Sources */,
B6B3E63C19FD3D8A0035422D /* WhisperMessageSerialization.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion AxolotlKit/Classes/Prekeys/PreKeyBundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import <Foundation/Foundation.h>

@interface PreKeyBundle : NSObject
@interface PreKeyBundle : NSObject <NSSecureCoding>

@property (nonatomic, readonly) NSData *identityKey;
@property (nonatomic, readonly) int registrationId;
Expand Down
54 changes: 52 additions & 2 deletions AxolotlKit/Classes/Prekeys/PreKeyBundle.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@

#import "PreKeyBundle.h"


static NSString* const kCoderPKBIdentityKey = @"kCoderPKBIdentityKey";
static NSString* const kCoderPKBregistrationId = @"kCoderPKBregistrationId";
static NSString* const kCoderPKBdeviceId = @"kCoderPKBdeviceId";
static NSString* const kCoderPKBsignedPreKeyPublic = @"kCoderPKBsignedPreKeyPublic";
static NSString* const kCoderPKBpreKeyPublic = @"kCoderPKBpreKeyPublic";
static NSString* const kCoderPKBpreKeyId = @"kCoderPKBpreKeyId";
static NSString* const kCoderPKBsignedPreKeyId = @"kCoderPKBsignedPreKeyId";
static NSString* const kCoderPKBsignedPreKeySignature = @"kCoderPKBsignedPreKeySignature";

@implementation PreKeyBundle

- (instancetype)initWithRegistrationId:(int)registrationId
Expand All @@ -18,9 +28,9 @@ - (instancetype)initWithRegistrationId:(int)registrationId
signedPreKeyId:(int)signedPreKeyId
signedPreKeySignature:(NSData*)signedPreKeySignature
identityKey:(NSData*)identityKey{

self = [super init];

if (self) {
_identityKey = identityKey;
_registrationId = registrationId;
Expand All @@ -31,8 +41,48 @@ - (instancetype)initWithRegistrationId:(int)registrationId
_signedPreKeyId = signedPreKeyId;
_signedPreKeySignature = signedPreKeySignature;
}

return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder{
int registrationId = [aDecoder decodeIntForKey:kCoderPKBregistrationId];
int deviceId = [aDecoder decodeIntForKey:kCoderPKBdeviceId];
int preKeyId = [aDecoder decodeIntForKey:kCoderPKBpreKeyId];
int signedPreKeyId = [aDecoder decodeIntForKey:kCoderPKBsignedPreKeyId];

NSData *preKeyPublic = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCoderPKBpreKeyPublic];
NSData *signedPreKeyPublic = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCoderPKBsignedPreKeyPublic];
NSData *signedPreKeySignature = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCoderPKBsignedPreKeySignature];
NSData *identityKey = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCoderPKBIdentityKey];


self = [self initWithRegistrationId:registrationId
deviceId:deviceId
preKeyId:preKeyId
preKeyPublic:preKeyPublic
signedPreKeyPublic:signedPreKeyPublic
signedPreKeyId:signedPreKeyId
signedPreKeySignature:signedPreKeySignature
identityKey:identityKey];

return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeInt:_registrationId forKey:kCoderPKBregistrationId];
[aCoder encodeInt:_deviceId forKey:kCoderPKBdeviceId];
[aCoder encodeInt:_preKeyId forKey:kCoderPKBpreKeyId];
[aCoder encodeInt:_signedPreKeyId forKey:kCoderPKBsignedPreKeyId];

[aCoder encodeObject:_preKeyPublic forKey:kCoderPKBpreKeyPublic];
[aCoder encodeObject:_signedPreKeyPublic forKey:kCoderPKBsignedPreKeyPublic];
[aCoder encodeObject:_signedPreKeySignature forKey:kCoderPKBsignedPreKeySignature];
[aCoder encodeObject:_identityKey forKey:kCoderPKBIdentityKey];
}

+(BOOL)supportsSecureCoding{
return YES;
}

@end

0 comments on commit d47fad2

Please sign in to comment.