Skip to content

Commit 9421f39

Browse files
String contact identifiers
Chose contact identifiers as strings, should help to move it to email identifiers.
1 parent dc2cc60 commit 9421f39

File tree

12 files changed

+57
-55
lines changed

12 files changed

+57
-55
lines changed

AxolotlKit Tests/AxolotlInMemoryStore.m

+12-12
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ - (int)localRegistrationId{
128128
return __localRegistrationId;
129129
}
130130

131-
- (void)saveRemoteIdentity:(NSData *)identityKey recipientId:(long)recipientId{
132-
[self.trustedKeys setObject:identityKey forKey:[NSNumber numberWithLong:recipientId]];
131+
- (void)saveRemoteIdentity:(NSData *)identityKey recipientId:(NSString*)recipientId{
132+
[self.trustedKeys setObject:identityKey forKey:recipientId];
133133
}
134134

135-
- (BOOL)isTrustedIdentityKey:(NSData *)identityKey recipientId:(long)recipientId{
136-
NSData *data = [self.trustedKeys objectForKey:[NSNumber numberWithLong:recipientId]];
135+
- (BOOL)isTrustedIdentityKey:(NSData *)identityKey recipientId:(NSString*)recipientId{
136+
NSData *data = [self.trustedKeys objectForKey:recipientId];
137137

138138
if (data) {
139139
return [data isEqualToData:identityKey];
@@ -144,7 +144,7 @@ - (BOOL)isTrustedIdentityKey:(NSData *)identityKey recipientId:(long)recipientId
144144

145145
# pragma mark Session Store
146146

147-
-(SessionRecord*)loadSession:(long)contactIdentifier deviceId:(int)deviceId{
147+
-(SessionRecord*)loadSession:(NSString*)contactIdentifier deviceId:(int)deviceId{
148148
SessionRecord *sessionRecord = [[self deviceSessionRecordsForContactIdentifier:contactIdentifier] objectForKey:[NSNumber numberWithInteger:deviceId]];
149149

150150
if (!sessionRecord) {
@@ -154,22 +154,22 @@ -(SessionRecord*)loadSession:(long)contactIdentifier deviceId:(int)deviceId{
154154
return sessionRecord;
155155
}
156156

157-
- (NSArray*)subDevicesSessions:(long)contactIdentifier{
157+
- (NSArray*)subDevicesSessions:(NSString*)contactIdentifier{
158158
return [[self deviceSessionRecordsForContactIdentifier:contactIdentifier] allKeys];
159159
}
160160

161-
- (NSDictionary*)deviceSessionRecordsForContactIdentifier:(long)contactIdentifier{
162-
return [self.sessionRecords objectForKey:[NSNumber numberWithLong:contactIdentifier]];
161+
- (NSDictionary*)deviceSessionRecordsForContactIdentifier:(NSString*)contactIdentifier{
162+
return [self.sessionRecords objectForKey:contactIdentifier];
163163
}
164164

165-
- (void)storeSession:(long)contactIdentifier deviceId:(int)deviceId session:(SessionRecord *)session{
165+
- (void)storeSession:(NSString*)contactIdentifier deviceId:(int)deviceId session:(SessionRecord *)session{
166166
NSAssert(session, @"Session can't be nil");
167-
[self.sessionRecords setObject:@{[NSNumber numberWithInt:deviceId]:session} forKey:[NSNumber numberWithLong:contactIdentifier]];
167+
[self.sessionRecords setObject:@{[NSNumber numberWithInt:deviceId]:session} forKey:contactIdentifier];
168168
}
169169

170-
- (BOOL)containsSession:(long)contactIdentifier deviceId:(int)deviceId{
170+
- (BOOL)containsSession:(NSString*)contactIdentifier deviceId:(int)deviceId{
171171

172-
if ([[self.sessionRecords objectForKey:[NSNumber numberWithLong:contactIdentifier]] objectForKey:[NSNumber numberWithInt:deviceId]]){
172+
if ([[self.sessionRecords objectForKey:contactIdentifier] objectForKey:[NSNumber numberWithInt:deviceId]]){
173173
return YES;
174174
}
175175
return NO;

AxolotlKit Tests/RatchetingSessionTest.m

+6-4
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ - (void)testSessionInitializationAndRatcheting {
315315

316316
[RatchetingSession initializeSession:bobSessionRecord.sessionState sessionVersion:3 BobParameters:bobAxolotlParams];
317317

318+
NSString *aliceIdentifier = @"+483294823482";
319+
NSString *bobIdentifier = @"+389424728942";
318320

319321
// Logging Alice's Session initialization and first message encryption
320322
XCTAssert([[@"This is a plaintext message." dataUsingEncoding:NSUTF8StringEncoding] isEqualToData:alicePlaintextData], @"Encoding is not correct");
@@ -324,8 +326,8 @@ - (void)testSessionInitializationAndRatcheting {
324326
XCTAssert([aliceSendingIVKeyData isEqualToData:aliceSessionRecord.sessionState.senderChainKey.messageKeys.iv]);
325327
XCTAssert([aliceSendingMacKeyData isEqualToData:aliceSessionRecord.sessionState.senderChainKey.messageKeys.macKey]);
326328

327-
[aliceStore storeSession:5L deviceId:1 session:aliceSessionRecord];
328-
SessionCipher *aliceSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:aliceStore recipientId:5L deviceId:1];
329+
[aliceStore storeSession:bobIdentifier deviceId:1 session:aliceSessionRecord];
330+
SessionCipher *aliceSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:aliceStore recipientId:bobIdentifier deviceId:1];
329331

330332
WhisperMessage *message = [aliceSessionCipher encryptMessage:alicePlaintextData];
331333
XCTAssert([aliceCipherTextData isEqualToData:message.cipherText]);
@@ -334,9 +336,9 @@ - (void)testSessionInitializationAndRatcheting {
334336

335337
XCTAssert([bobRootKeyData isEqualToData:bobSessionRecord.sessionState.rootKey.keyData]);
336338

337-
[bobStore storeSession:5L deviceId:1 session:bobSessionRecord];
339+
[bobStore storeSession:aliceIdentifier deviceId:1 session:bobSessionRecord];
338340

339-
SessionCipher *bobSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:bobStore recipientId:5L deviceId:1];
341+
SessionCipher *bobSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:bobStore recipientId:aliceIdentifier deviceId:1];
340342

341343
NSData *plainData = [bobSessionCipher decrypt:message];
342344

AxolotlKit Tests/SessionBuilderTests.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ - (void)tearDown {
4343

4444
- (void)testBasicPreKey {
4545

46-
long BOB_RECIPIENT_ID = 5L;
47-
long ALICE_RECIPIENT_ID = 3L;
46+
NSString *BOB_RECIPIENT_ID = @"+3828923892";
47+
NSString *ALICE_RECIPIENT_ID = @"alice@gmail.com";
4848

4949
AxolotlInMemoryStore *aliceStore = [AxolotlInMemoryStore new];
5050
SessionBuilder *aliceSessionBuilder = [[SessionBuilder alloc] initWithAxolotlStore:aliceStore recipientId:BOB_RECIPIENT_ID deviceId:1];
@@ -93,8 +93,8 @@ - (void)testBasicPreKey {
9393

9494
- (void)testBasicPreKeyMITM {
9595

96-
long BOB_RECIPIENT_ID = 5L;
97-
long ALICE_RECIPIENT_ID = 3L;
96+
NSString *BOB_RECIPIENT_ID = @"+3828923892";
97+
NSString *ALICE_RECIPIENT_ID = @"alice@gmail.com";
9898

9999
AxolotlInMemoryStore *aliceStore = [AxolotlInMemoryStore new];
100100
SessionBuilder *aliceSessionBuilder = [[SessionBuilder alloc] initWithAxolotlStore:aliceStore recipientId:BOB_RECIPIENT_ID deviceId:1];

AxolotlKit Tests/SessionCipherTest.m

+7-6
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,21 @@ -(void)sessionInitialization:(SessionState*)aliceSessionState bobSessionState:(S
6969

7070
- (void)runInteractionWithAliceRecord:(SessionRecord*)aliceSessionRecord bobRecord:(SessionRecord*)bobSessionRecord {
7171

72+
NSString *aliceIdentifier = @"+3728378173821";
73+
NSString *bobIdentifier = @"bob@gmail.com";
74+
7275
AxolotlInMemoryStore *aliceStore = [AxolotlInMemoryStore new];
7376
AxolotlInMemoryStore *bobStore = [AxolotlInMemoryStore new];
7477

75-
[aliceStore storeSession:2L deviceId:1 session:aliceSessionRecord];
76-
[bobStore storeSession:3L deviceId:1 session:bobSessionRecord];
78+
[aliceStore storeSession:bobIdentifier deviceId:1 session:aliceSessionRecord];
79+
[bobStore storeSession:aliceIdentifier deviceId:1 session:bobSessionRecord];
7780

78-
SessionCipher *aliceSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:aliceStore recipientId:2L deviceId:1];
79-
SessionCipher *bobSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:bobStore recipientId:3L deviceId:1];
81+
SessionCipher *aliceSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:aliceStore recipientId:bobIdentifier deviceId:1];
82+
SessionCipher *bobSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:bobStore recipientId:aliceIdentifier deviceId:1];
8083

8184
NSData *alicePlainText = [@"This is a plaintext message!" dataUsingEncoding:NSUTF8StringEncoding];
8285
WhisperMessage *cipherText = [aliceSessionCipher encryptMessage:alicePlainText];
8386

84-
85-
8687
NSData *bobPlaintext = [bobSessionCipher decrypt:cipherText];
8788

8889
XCTAssert([bobPlaintext isEqualToData:alicePlainText]);

AxolotlKit/Classes/Prekeys/PreKeyBundle.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
@interface PreKeyBundle : NSObject
1212

13-
@property (nonatomic, readonly) NSData *identityKey;
14-
@property (nonatomic, readonly) int registrationId;
15-
@property (nonatomic, readonly) int deviceId;
16-
@property (nonatomic, readonly) NSData *signedPreKeyPublic;
17-
@property (nonatomic, readonly) NSData *preKeyPublic;
18-
@property (nonatomic, readonly) int preKeyId;
19-
@property (nonatomic, readonly) int signedPreKeyId;
20-
@property (nonatomic, readonly) NSData *signedPreKeySignature;
13+
@property (nonatomic, readonly) NSData *identityKey;
14+
@property (nonatomic, readonly) int registrationId;
15+
@property (nonatomic, readonly) int deviceId;
16+
@property (nonatomic, readonly) NSData *signedPreKeyPublic;
17+
@property (nonatomic, readonly) NSData *preKeyPublic;
18+
@property (nonatomic, readonly) int preKeyId;
19+
@property (nonatomic, readonly) int signedPreKeyId;
20+
@property (nonatomic, readonly) NSData *signedPreKeySignature;
2121

2222
- (instancetype)initWithRegistrationId:(int)registrationId
2323
deviceId:(int)deviceId

AxolotlKit/Classes/SessionCipher.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
@interface SessionCipher : NSObject
2121

22-
- (instancetype)initWithAxolotlStore:(id<AxolotlStore>)sessionStore recipientId:(long)recipientId deviceId:(int)deviceId;
22+
- (instancetype)initWithAxolotlStore:(id<AxolotlStore>)sessionStore recipientId:(NSString*)recipientId deviceId:(int)deviceId;
2323

24-
- (instancetype)initWithSessionStore:(id<SessionStore>)sessionStore preKeyStore:(id<PreKeyStore>)preKeyStore signedPreKeyStore:(id<SignedPreKeyStore>)signedPreKeyStore identityKeyStore:(id<IdentityKeyStore>)identityKeyStore recipientId:(long)recipientId deviceId:(int)deviceId;
24+
- (instancetype)initWithSessionStore:(id<SessionStore>)sessionStore preKeyStore:(id<PreKeyStore>)preKeyStore signedPreKeyStore:(id<SignedPreKeyStore>)signedPreKeyStore identityKeyStore:(id<IdentityKeyStore>)identityKeyStore recipientId:(NSString*)recipientId deviceId:(int)deviceId;
2525

2626
- (id<CipherMessage>)encryptMessage:(NSData*)paddedMessage;
2727

AxolotlKit/Classes/SessionCipher.m

+4-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
@interface SessionCipher ()
3333

34-
@property long recipientId;
34+
@property NSString* recipientId;
3535
@property int deviceId;
3636

3737
@property (nonatomic, retain) id<SessionStore> sessionStore;
@@ -44,7 +44,7 @@ @interface SessionCipher ()
4444
@implementation SessionCipher
4545

4646

47-
- (instancetype)initWithAxolotlStore:(id<AxolotlStore>)sessionStore recipientId:(long)recipientId deviceId:(int)deviceId{
47+
- (instancetype)initWithAxolotlStore:(id<AxolotlStore>)sessionStore recipientId:(NSString*)recipientId deviceId:(int)deviceId{
4848
return [self initWithSessionStore:sessionStore
4949
preKeyStore:sessionStore
5050
signedPreKeyStore:sessionStore
@@ -57,12 +57,11 @@ - (instancetype)initWithSessionStore:(id<SessionStore>)sessionStore
5757
preKeyStore:(id<PreKeyStore>)preKeyStore
5858
signedPreKeyStore:(id<SignedPreKeyStore>)signedPreKeyStore
5959
identityKeyStore:(id<IdentityKeyStore>)identityKeyStore
60-
recipientId:(long)recipientId
60+
recipientId:(NSString*)recipientId
6161
deviceId:(int)deviceId{
6262
self = [super init];
6363

6464
if (self){
65-
6665
self.recipientId = recipientId;
6766
self.deviceId = deviceId;
6867
self.sessionStore = sessionStore;
@@ -142,7 +141,7 @@ - (NSData*)decryptPreKeyWhisperMessage:(PreKeyWhisperMessage*)preKeyWhisperMessa
142141

143142
- (NSData*)decryptWhisperMessage:(WhisperMessage*)message{
144143
if (![self.sessionStore containsSession:self.recipientId deviceId:self.deviceId]) {
145-
@throw [NSException exceptionWithName:NoSessionException reason:[NSString stringWithFormat:@"No session for: %ld, %d", self.recipientId, self.deviceId] userInfo:nil];
144+
@throw [NSException exceptionWithName:NoSessionException reason:[NSString stringWithFormat:@"No session for: %@, %d", self.recipientId, self.deviceId] userInfo:nil];
146145
}
147146

148147
SessionRecord *sessionRecord = [self.sessionStore loadSession:self.recipientId deviceId:self.deviceId];

AxolotlKit/Classes/Sessions/SessionBuilder.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
@interface SessionBuilder : NSObject
1919

20-
- (instancetype)initWithAxolotlStore:(id<AxolotlStore>)sessionStore recipientId:(long)recipientId deviceId:(int)deviceId;
20+
- (instancetype)initWithAxolotlStore:(id<AxolotlStore>)sessionStore recipientId:(NSString*)recipientId deviceId:(int)deviceId;
2121

2222
- (instancetype)initWithSessionStore:(id<SessionStore>)sessionStore
2323
preKeyStore:(id<PreKeyStore>)preKeyStore
2424
signedPreKeyStore:(id<SignedPreKeyStore>)signedPreKeyStore
2525
identityKeyStore:(id<IdentityKeyStore>)identityKeyStore
26-
recipientId:(long)recipientId
26+
recipientId:(NSString*)recipientId
2727
deviceId:(int)deviceId;
2828

2929
- (void)processPrekeyBundle:(PreKeyBundle*)preKeyBundle;

AxolotlKit/Classes/Sessions/SessionBuilder.m

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
@interface SessionBuilder ()
3131

32-
@property (nonatomic, readonly)long recipientId;
32+
@property (nonatomic, readonly)NSString* recipientId;
3333
@property (nonatomic, readonly)int deviceId;
3434

3535
@property(nonatomic, readonly)id<SessionStore> sessionStore;
@@ -42,7 +42,7 @@ @interface SessionBuilder ()
4242

4343
@implementation SessionBuilder
4444

45-
- (instancetype)initWithAxolotlStore:(id<AxolotlStore>)sessionStore recipientId:(long)recipientId deviceId:(int)deviceId{
45+
- (instancetype)initWithAxolotlStore:(id<AxolotlStore>)sessionStore recipientId:(NSString*)recipientId deviceId:(int)deviceId{
4646
return [self initWithSessionStore:sessionStore
4747
preKeyStore:sessionStore
4848
signedPreKeyStore:sessionStore
@@ -55,7 +55,7 @@ - (instancetype)initWithSessionStore:(id<SessionStore>)sessionStore
5555
preKeyStore:(id<PreKeyStore>)preKeyStore
5656
signedPreKeyStore:(id<SignedPreKeyStore>)signedPreKeyStore
5757
identityKeyStore:(id<IdentityKeyStore>)identityKeyStore
58-
recipientId:(long)recipientId
58+
recipientId:(NSString*)recipientId
5959
deviceId:(int)deviceId{
6060
self = [super init];
6161

@@ -81,7 +81,7 @@ - (void)processPrekeyBundle:(PreKeyBundle*)preKeyBundle{
8181
@throw [NSException exceptionWithName:InvalidKeyException reason:@"KeyIsNotValidlySigned" userInfo:nil];
8282
}
8383

84-
SessionRecord *sessionRecord = [self.sessionStore loadSession:preKeyBundle.registrationId deviceId:preKeyBundle.deviceId];
84+
SessionRecord *sessionRecord = [self.sessionStore loadSession:self.recipientId deviceId:preKeyBundle.deviceId];
8585
ECKeyPair *ourBaseKey = [Curve25519 generateKeyPair];
8686
NSData *theirOneTimePreKey = preKeyBundle.preKeyPublic.removeKeyType;
8787
int theirOneTimePreKeyId = preKeyBundle.preKeyId;

AxolotlKit/Classes/State/IdentityKeyStore.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
- (ECKeyPair*)identityKeyPair;
1515
- (int)localRegistrationId;
16-
- (void)saveRemoteIdentity:(NSData*)identityKey recipientId:(long)recipientId;
17-
- (BOOL)isTrustedIdentityKey:(NSData*)identityKey recipientId:(long)recipientId;
16+
- (void)saveRemoteIdentity:(NSData*)identityKey recipientId:(NSString*)recipientId;
17+
- (BOOL)isTrustedIdentityKey:(NSData*)identityKey recipientId:(NSString*)recipientId;
1818

1919
@end

AxolotlKit/Classes/State/SessionStore.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
* @return a copy of the SessionRecord corresponding to the recipientId + deviceId tuple.
2323
*/
2424

25-
- (SessionRecord*)loadSession:(long)contactIdentifier deviceId:(int)deviceId;
25+
- (SessionRecord*)loadSession:(NSString*)contactIdentifier deviceId:(int)deviceId;
2626

27-
- (NSArray*)subDevicesSessions:(long)contactIdentifier;
27+
- (NSArray*)subDevicesSessions:(NSString*)contactIdentifier;
2828

29-
- (void)storeSession:(long)contactIdentifier deviceId:(int)deviceId session:(SessionRecord*)session;
29+
- (void)storeSession:(NSString*)contactIdentifier deviceId:(int)deviceId session:(SessionRecord*)session;
3030

31-
- (BOOL)containsSession:(long)contactIdentifier deviceId:(int)deviceId;
31+
- (BOOL)containsSession:(NSString*)contactIdentifier deviceId:(int)deviceId;
3232

33-
- (void)deleteSessionForContact:(long)contactIdentifier deviceId:(int)deviceId;
33+
- (void)deleteSessionForContact:(NSString*)contactIdentifier deviceId:(int)deviceId;
3434

35-
- (void)deleteAllSessionsForContact:(long)contactIdentifier;
35+
- (void)deleteAllSessionsForContact:(NSString*)contactIdentifier;
3636

3737
@end

0 commit comments

Comments
 (0)