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

Commit

Permalink
String contact identifiers
Browse files Browse the repository at this point in the history
Chose contact identifiers as strings, should help to move it to email
identifiers.
  • Loading branch information
FredericJacobs committed Nov 17, 2014
1 parent dc2cc60 commit 9421f39
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 55 deletions.
24 changes: 12 additions & 12 deletions AxolotlKit Tests/AxolotlInMemoryStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ - (int)localRegistrationId{
return __localRegistrationId;
}

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

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

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

# pragma mark Session Store

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

if (!sessionRecord) {
Expand All @@ -154,22 +154,22 @@ -(SessionRecord*)loadSession:(long)contactIdentifier deviceId:(int)deviceId{
return sessionRecord;
}

- (NSArray*)subDevicesSessions:(long)contactIdentifier{
- (NSArray*)subDevicesSessions:(NSString*)contactIdentifier{
return [[self deviceSessionRecordsForContactIdentifier:contactIdentifier] allKeys];
}

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

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

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

if ([[self.sessionRecords objectForKey:[NSNumber numberWithLong:contactIdentifier]] objectForKey:[NSNumber numberWithInt:deviceId]]){
if ([[self.sessionRecords objectForKey:contactIdentifier] objectForKey:[NSNumber numberWithInt:deviceId]]){
return YES;
}
return NO;
Expand Down
10 changes: 6 additions & 4 deletions AxolotlKit Tests/RatchetingSessionTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ - (void)testSessionInitializationAndRatcheting {

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

NSString *aliceIdentifier = @"+483294823482";
NSString *bobIdentifier = @"+389424728942";

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

[aliceStore storeSession:5L deviceId:1 session:aliceSessionRecord];
SessionCipher *aliceSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:aliceStore recipientId:5L deviceId:1];
[aliceStore storeSession:bobIdentifier deviceId:1 session:aliceSessionRecord];
SessionCipher *aliceSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:aliceStore recipientId:bobIdentifier deviceId:1];

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

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

[bobStore storeSession:5L deviceId:1 session:bobSessionRecord];
[bobStore storeSession:aliceIdentifier deviceId:1 session:bobSessionRecord];

SessionCipher *bobSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:bobStore recipientId:5L deviceId:1];
SessionCipher *bobSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:bobStore recipientId:aliceIdentifier deviceId:1];

NSData *plainData = [bobSessionCipher decrypt:message];

Expand Down
8 changes: 4 additions & 4 deletions AxolotlKit Tests/SessionBuilderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ - (void)tearDown {

- (void)testBasicPreKey {

long BOB_RECIPIENT_ID = 5L;
long ALICE_RECIPIENT_ID = 3L;
NSString *BOB_RECIPIENT_ID = @"+3828923892";
NSString *ALICE_RECIPIENT_ID = @"[email protected]";

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

- (void)testBasicPreKeyMITM {

long BOB_RECIPIENT_ID = 5L;
long ALICE_RECIPIENT_ID = 3L;
NSString *BOB_RECIPIENT_ID = @"+3828923892";
NSString *ALICE_RECIPIENT_ID = @"[email protected]";

AxolotlInMemoryStore *aliceStore = [AxolotlInMemoryStore new];
SessionBuilder *aliceSessionBuilder = [[SessionBuilder alloc] initWithAxolotlStore:aliceStore recipientId:BOB_RECIPIENT_ID deviceId:1];
Expand Down
13 changes: 7 additions & 6 deletions AxolotlKit Tests/SessionCipherTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,21 @@ -(void)sessionInitialization:(SessionState*)aliceSessionState bobSessionState:(S

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

NSString *aliceIdentifier = @"+3728378173821";
NSString *bobIdentifier = @"[email protected]";

AxolotlInMemoryStore *aliceStore = [AxolotlInMemoryStore new];
AxolotlInMemoryStore *bobStore = [AxolotlInMemoryStore new];

[aliceStore storeSession:2L deviceId:1 session:aliceSessionRecord];
[bobStore storeSession:3L deviceId:1 session:bobSessionRecord];
[aliceStore storeSession:bobIdentifier deviceId:1 session:aliceSessionRecord];
[bobStore storeSession:aliceIdentifier deviceId:1 session:bobSessionRecord];

SessionCipher *aliceSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:aliceStore recipientId:2L deviceId:1];
SessionCipher *bobSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:bobStore recipientId:3L deviceId:1];
SessionCipher *aliceSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:aliceStore recipientId:bobIdentifier deviceId:1];
SessionCipher *bobSessionCipher = [[SessionCipher alloc] initWithAxolotlStore:bobStore recipientId:aliceIdentifier deviceId:1];

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



NSData *bobPlaintext = [bobSessionCipher decrypt:cipherText];

XCTAssert([bobPlaintext isEqualToData:alicePlainText]);
Expand Down
Binary file not shown.
16 changes: 8 additions & 8 deletions AxolotlKit/Classes/Prekeys/PreKeyBundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

@interface PreKeyBundle : NSObject

@property (nonatomic, readonly) NSData *identityKey;
@property (nonatomic, readonly) int registrationId;
@property (nonatomic, readonly) int deviceId;
@property (nonatomic, readonly) NSData *signedPreKeyPublic;
@property (nonatomic, readonly) NSData *preKeyPublic;
@property (nonatomic, readonly) int preKeyId;
@property (nonatomic, readonly) int signedPreKeyId;
@property (nonatomic, readonly) NSData *signedPreKeySignature;
@property (nonatomic, readonly) NSData *identityKey;
@property (nonatomic, readonly) int registrationId;
@property (nonatomic, readonly) int deviceId;
@property (nonatomic, readonly) NSData *signedPreKeyPublic;
@property (nonatomic, readonly) NSData *preKeyPublic;
@property (nonatomic, readonly) int preKeyId;
@property (nonatomic, readonly) int signedPreKeyId;
@property (nonatomic, readonly) NSData *signedPreKeySignature;

- (instancetype)initWithRegistrationId:(int)registrationId
deviceId:(int)deviceId
Expand Down
4 changes: 2 additions & 2 deletions AxolotlKit/Classes/SessionCipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

@interface SessionCipher : NSObject

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

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

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

Expand Down
9 changes: 4 additions & 5 deletions AxolotlKit/Classes/SessionCipher.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

@interface SessionCipher ()

@property long recipientId;
@property NSString* recipientId;
@property int deviceId;

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


- (instancetype)initWithAxolotlStore:(id<AxolotlStore>)sessionStore recipientId:(long)recipientId deviceId:(int)deviceId{
- (instancetype)initWithAxolotlStore:(id<AxolotlStore>)sessionStore recipientId:(NSString*)recipientId deviceId:(int)deviceId{
return [self initWithSessionStore:sessionStore
preKeyStore:sessionStore
signedPreKeyStore:sessionStore
Expand All @@ -57,12 +57,11 @@ - (instancetype)initWithSessionStore:(id<SessionStore>)sessionStore
preKeyStore:(id<PreKeyStore>)preKeyStore
signedPreKeyStore:(id<SignedPreKeyStore>)signedPreKeyStore
identityKeyStore:(id<IdentityKeyStore>)identityKeyStore
recipientId:(long)recipientId
recipientId:(NSString*)recipientId
deviceId:(int)deviceId{
self = [super init];

if (self){

self.recipientId = recipientId;
self.deviceId = deviceId;
self.sessionStore = sessionStore;
Expand Down Expand Up @@ -142,7 +141,7 @@ - (NSData*)decryptPreKeyWhisperMessage:(PreKeyWhisperMessage*)preKeyWhisperMessa

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

SessionRecord *sessionRecord = [self.sessionStore loadSession:self.recipientId deviceId:self.deviceId];
Expand Down
4 changes: 2 additions & 2 deletions AxolotlKit/Classes/Sessions/SessionBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

@interface SessionBuilder : NSObject

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

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

- (void)processPrekeyBundle:(PreKeyBundle*)preKeyBundle;
Expand Down
8 changes: 4 additions & 4 deletions AxolotlKit/Classes/Sessions/SessionBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

@interface SessionBuilder ()

@property (nonatomic, readonly)long recipientId;
@property (nonatomic, readonly)NSString* recipientId;
@property (nonatomic, readonly)int deviceId;

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

@implementation SessionBuilder

- (instancetype)initWithAxolotlStore:(id<AxolotlStore>)sessionStore recipientId:(long)recipientId deviceId:(int)deviceId{
- (instancetype)initWithAxolotlStore:(id<AxolotlStore>)sessionStore recipientId:(NSString*)recipientId deviceId:(int)deviceId{
return [self initWithSessionStore:sessionStore
preKeyStore:sessionStore
signedPreKeyStore:sessionStore
Expand All @@ -55,7 +55,7 @@ - (instancetype)initWithSessionStore:(id<SessionStore>)sessionStore
preKeyStore:(id<PreKeyStore>)preKeyStore
signedPreKeyStore:(id<SignedPreKeyStore>)signedPreKeyStore
identityKeyStore:(id<IdentityKeyStore>)identityKeyStore
recipientId:(long)recipientId
recipientId:(NSString*)recipientId
deviceId:(int)deviceId{
self = [super init];

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

SessionRecord *sessionRecord = [self.sessionStore loadSession:preKeyBundle.registrationId deviceId:preKeyBundle.deviceId];
SessionRecord *sessionRecord = [self.sessionStore loadSession:self.recipientId deviceId:preKeyBundle.deviceId];
ECKeyPair *ourBaseKey = [Curve25519 generateKeyPair];
NSData *theirOneTimePreKey = preKeyBundle.preKeyPublic.removeKeyType;
int theirOneTimePreKeyId = preKeyBundle.preKeyId;
Expand Down
4 changes: 2 additions & 2 deletions AxolotlKit/Classes/State/IdentityKeyStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

- (ECKeyPair*)identityKeyPair;
- (int)localRegistrationId;
- (void)saveRemoteIdentity:(NSData*)identityKey recipientId:(long)recipientId;
- (BOOL)isTrustedIdentityKey:(NSData*)identityKey recipientId:(long)recipientId;
- (void)saveRemoteIdentity:(NSData*)identityKey recipientId:(NSString*)recipientId;
- (BOOL)isTrustedIdentityKey:(NSData*)identityKey recipientId:(NSString*)recipientId;

@end
12 changes: 6 additions & 6 deletions AxolotlKit/Classes/State/SessionStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
* @return a copy of the SessionRecord corresponding to the recipientId + deviceId tuple.
*/

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

- (NSArray*)subDevicesSessions:(long)contactIdentifier;
- (NSArray*)subDevicesSessions:(NSString*)contactIdentifier;

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

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

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

- (void)deleteAllSessionsForContact:(long)contactIdentifier;
- (void)deleteAllSessionsForContact:(NSString*)contactIdentifier;

@end

0 comments on commit 9421f39

Please sign in to comment.