Skip to content

Commit d47fad2

Browse files
NSSecureCoding serialization of PreKeyBundles.
1 parent 7122ea0 commit d47fad2

File tree

5 files changed

+117
-4
lines changed

5 files changed

+117
-4
lines changed

AxolotlKit Tests/PreKeyBundleTests.m

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// PreKeyBundleTests.m
3+
// AxolotlKit
4+
//
5+
// Created by Frederic Jacobs on 15/02/15.
6+
// Copyright (c) 2015 Frederic Jacobs. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
11+
#import "PreKeyBundle.h"
12+
#import <25519/Curve25519.h>
13+
14+
@interface PreKeyBundleTests : XCTestCase
15+
16+
@end
17+
18+
@implementation PreKeyBundleTests
19+
20+
- (void)setUp {
21+
[super setUp];
22+
// Put setup code here. This method is called before the invocation of each test method in the class.
23+
}
24+
25+
- (void)tearDown {
26+
// Put teardown code here. This method is called after the invocation of each test method in the class.
27+
[super tearDown];
28+
}
29+
30+
- (void)testSerialization {
31+
PreKeyBundle *bundle = [[PreKeyBundle alloc] initWithRegistrationId:1
32+
deviceId:2
33+
preKeyId:3
34+
preKeyPublic:[Curve25519 generateKeyPair].publicKey
35+
signedPreKeyPublic:[Curve25519 generateKeyPair].publicKey
36+
signedPreKeyId:4
37+
signedPreKeySignature:[Curve25519 generateKeyPair].publicKey
38+
identityKey:[Curve25519 generateKeyPair].publicKey];
39+
40+
41+
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:bundle];
42+
43+
PreKeyBundle *bundle2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
44+
45+
XCTAssertEqual(bundle.registrationId, bundle2.registrationId);
46+
XCTAssertEqual(bundle.deviceId, bundle2.deviceId);
47+
XCTAssertEqual(bundle.preKeyId, bundle2.preKeyId);
48+
XCTAssertEqual(bundle.signedPreKeyId, bundle2.signedPreKeyId);
49+
50+
51+
XCTAssert([bundle.preKeyPublic isEqualToData:bundle2.preKeyPublic]);
52+
XCTAssert([bundle.signedPreKeyPublic isEqualToData:bundle2.signedPreKeyPublic]);
53+
XCTAssert([bundle.signedPreKeySignature isEqualToData:bundle2.signedPreKeySignature]);
54+
XCTAssert([bundle.identityKey isEqualToData:bundle2.identityKey]);
55+
56+
}
57+
58+
59+
@end

AxolotlKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "AxolotlKit"
3-
s.version = "0.6.1"
3+
s.version = "0.6.2"
44
s.summary = "AxolotlKit is a Free implementation of the Axolotl protocol in Objective-C"
55
s.homepage = "https://github.com/WhisperSystems/AxolotlKit"
66
s.license = "GPLv2"

AxolotlKit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
719C4E40588B7AD1F1DD22FC /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = AD9F87ECA041E727CC457415 /* libPods.a */; };
1212
B619AF1419FC148D00E33198 /* ProtobuffsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B619AF1319FC148D00E33198 /* ProtobuffsTests.m */; };
1313
B61E841E19D17C33004FC382 /* HKDFTest.m in Sources */ = {isa = PBXBuildFile; fileRef = B61E841C19D1780D004FC382 /* HKDFTest.m */; };
14+
B62EFBF01A9140190072ADD3 /* PreKeyBundleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B62EFBEF1A9140190072ADD3 /* PreKeyBundleTests.m */; };
1415
B63B2F9F197D8AFD00E09F65 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B63B2F9E197D8AFD00E09F65 /* Foundation.framework */; };
1516
B63B2FA1197D8AFD00E09F65 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B63B2FA0197D8AFD00E09F65 /* CoreGraphics.framework */; };
1617
B63B2FA3197D8AFD00E09F65 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B63B2FA2197D8AFD00E09F65 /* UIKit.framework */; };
@@ -72,6 +73,7 @@
7273
AD9F87ECA041E727CC457415 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
7374
B619AF1319FC148D00E33198 /* ProtobuffsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ProtobuffsTests.m; sourceTree = "<group>"; };
7475
B61E841C19D1780D004FC382 /* HKDFTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HKDFTest.m; sourceTree = "<group>"; };
76+
B62EFBEF1A9140190072ADD3 /* PreKeyBundleTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PreKeyBundleTests.m; sourceTree = "<group>"; };
7577
B63B2F9D197D8AFD00E09F65 /* AxolotlKit.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AxolotlKit.app; sourceTree = BUILT_PRODUCTS_DIR; };
7678
B63B2F9E197D8AFD00E09F65 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
7779
B63B2FA0197D8AFD00E09F65 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
@@ -260,6 +262,7 @@
260262
B63B2FDF197D8B9600E09F65 /* AxolotlKit Tests */ = {
261263
isa = PBXGroup;
262264
children = (
265+
B62EFBEF1A9140190072ADD3 /* PreKeyBundleTests.m */,
263266
B619AF1219FC147500E33198 /* Protobuffs */,
264267
B61E841719D172BC004FC382 /* ECC */,
265268
B61E841819D176D7004FC382 /* HKDF */,
@@ -682,6 +685,7 @@
682685
B6A3490319F1A4B4008A3E01 /* AxolotlInMemoryStore.m in Sources */,
683686
B63B2FE6197D8B9600E09F65 /* AxolotlKit_Tests.m in Sources */,
684687
B654B03419FACCB6000458F0 /* RatchetingSessionTest.m in Sources */,
688+
B62EFBF01A9140190072ADD3 /* PreKeyBundleTests.m in Sources */,
685689
B6BB8C3519F7E773008A5895 /* SessionBuilderTests.m in Sources */,
686690
B6E37AE019CF107C00E619CA /* ECCTests.m in Sources */,
687691
B6B3E63C19FD3D8A0035422D /* WhisperMessageSerialization.m in Sources */,

AxolotlKit/Classes/Prekeys/PreKeyBundle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#import <Foundation/Foundation.h>
1010

11-
@interface PreKeyBundle : NSObject
11+
@interface PreKeyBundle : NSObject <NSSecureCoding>
1212

1313
@property (nonatomic, readonly) NSData *identityKey;
1414
@property (nonatomic, readonly) int registrationId;

AxolotlKit/Classes/Prekeys/PreKeyBundle.m

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88

99
#import "PreKeyBundle.h"
1010

11+
12+
static NSString* const kCoderPKBIdentityKey = @"kCoderPKBIdentityKey";
13+
static NSString* const kCoderPKBregistrationId = @"kCoderPKBregistrationId";
14+
static NSString* const kCoderPKBdeviceId = @"kCoderPKBdeviceId";
15+
static NSString* const kCoderPKBsignedPreKeyPublic = @"kCoderPKBsignedPreKeyPublic";
16+
static NSString* const kCoderPKBpreKeyPublic = @"kCoderPKBpreKeyPublic";
17+
static NSString* const kCoderPKBpreKeyId = @"kCoderPKBpreKeyId";
18+
static NSString* const kCoderPKBsignedPreKeyId = @"kCoderPKBsignedPreKeyId";
19+
static NSString* const kCoderPKBsignedPreKeySignature = @"kCoderPKBsignedPreKeySignature";
20+
1121
@implementation PreKeyBundle
1222

1323
- (instancetype)initWithRegistrationId:(int)registrationId
@@ -18,9 +28,9 @@ - (instancetype)initWithRegistrationId:(int)registrationId
1828
signedPreKeyId:(int)signedPreKeyId
1929
signedPreKeySignature:(NSData*)signedPreKeySignature
2030
identityKey:(NSData*)identityKey{
21-
31+
2232
self = [super init];
23-
33+
2434
if (self) {
2535
_identityKey = identityKey;
2636
_registrationId = registrationId;
@@ -31,8 +41,48 @@ - (instancetype)initWithRegistrationId:(int)registrationId
3141
_signedPreKeyId = signedPreKeyId;
3242
_signedPreKeySignature = signedPreKeySignature;
3343
}
44+
45+
return self;
46+
}
3447

48+
- (id)initWithCoder:(NSCoder *)aDecoder{
49+
int registrationId = [aDecoder decodeIntForKey:kCoderPKBregistrationId];
50+
int deviceId = [aDecoder decodeIntForKey:kCoderPKBdeviceId];
51+
int preKeyId = [aDecoder decodeIntForKey:kCoderPKBpreKeyId];
52+
int signedPreKeyId = [aDecoder decodeIntForKey:kCoderPKBsignedPreKeyId];
53+
54+
NSData *preKeyPublic = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCoderPKBpreKeyPublic];
55+
NSData *signedPreKeyPublic = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCoderPKBsignedPreKeyPublic];
56+
NSData *signedPreKeySignature = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCoderPKBsignedPreKeySignature];
57+
NSData *identityKey = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCoderPKBIdentityKey];
58+
59+
60+
self = [self initWithRegistrationId:registrationId
61+
deviceId:deviceId
62+
preKeyId:preKeyId
63+
preKeyPublic:preKeyPublic
64+
signedPreKeyPublic:signedPreKeyPublic
65+
signedPreKeyId:signedPreKeyId
66+
signedPreKeySignature:signedPreKeySignature
67+
identityKey:identityKey];
68+
3569
return self;
3670
}
3771

72+
- (void)encodeWithCoder:(NSCoder *)aCoder{
73+
[aCoder encodeInt:_registrationId forKey:kCoderPKBregistrationId];
74+
[aCoder encodeInt:_deviceId forKey:kCoderPKBdeviceId];
75+
[aCoder encodeInt:_preKeyId forKey:kCoderPKBpreKeyId];
76+
[aCoder encodeInt:_signedPreKeyId forKey:kCoderPKBsignedPreKeyId];
77+
78+
[aCoder encodeObject:_preKeyPublic forKey:kCoderPKBpreKeyPublic];
79+
[aCoder encodeObject:_signedPreKeyPublic forKey:kCoderPKBsignedPreKeyPublic];
80+
[aCoder encodeObject:_signedPreKeySignature forKey:kCoderPKBsignedPreKeySignature];
81+
[aCoder encodeObject:_identityKey forKey:kCoderPKBIdentityKey];
82+
}
83+
84+
+(BOOL)supportsSecureCoding{
85+
return YES;
86+
}
87+
3888
@end

0 commit comments

Comments
 (0)