2014-07-11 00:33:51 +02:00
|
|
|
//
|
2018-03-05 15:30:58 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2014-07-11 00:33:51 +02:00
|
|
|
//
|
2017-02-13 17:40:30 +01:00
|
|
|
|
2014-11-21 00:51:23 +01:00
|
|
|
#import "SignalKeyingStorage.h"
|
2020-11-11 01:50:01 +01:00
|
|
|
#import <SignalUtilitiesKit/OWSPrimaryStorage.h>
|
|
|
|
#import <SignalUtilitiesKit/YapDatabaseConnection+OWS.h>
|
2020-11-12 00:41:45 +01:00
|
|
|
#import <SignalCoreKit/Randomness.h>
|
2014-07-11 00:33:51 +02:00
|
|
|
|
2014-11-21 00:51:23 +01:00
|
|
|
#define SignalKeyingCollection @"SignalKeyingCollection"
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
#define SIGNALING_MAC_KEY_LENGTH 20
|
2014-07-11 00:33:51 +02:00
|
|
|
#define SIGNALING_CIPHER_KEY_LENGTH 16
|
|
|
|
#define SIGNALING_EXTRA_KEY_LENGTH 4
|
|
|
|
|
2014-11-21 00:51:23 +01:00
|
|
|
@implementation SignalKeyingStorage
|
2014-07-11 00:33:51 +02:00
|
|
|
|
2017-12-01 21:17:29 +01:00
|
|
|
+ (void)generateSignaling
|
|
|
|
{
|
2018-07-27 20:40:58 +02:00
|
|
|
[self storeData:[Randomness generateRandomBytes:SIGNALING_MAC_KEY_LENGTH] forKey:SIGNALING_MAC_KEY];
|
|
|
|
[self storeData:[Randomness generateRandomBytes:SIGNALING_CIPHER_KEY_LENGTH] forKey:SIGNALING_CIPHER_KEY];
|
|
|
|
[self storeData:[Randomness generateRandomBytes:SIGNALING_EXTRA_KEY_LENGTH] forKey:SIGNALING_EXTRA_KEY];
|
2014-07-11 00:33:51 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:17:29 +01:00
|
|
|
+ (int64_t)getAndIncrementOneTimeCounter
|
|
|
|
{
|
2014-07-11 00:33:51 +02:00
|
|
|
__block int64_t oldCounter;
|
2017-12-01 21:17:29 +01:00
|
|
|
oldCounter = [[self stringForKey:PASSWORD_COUNTER_KEY] longLongValue];
|
2015-12-22 12:45:09 +01:00
|
|
|
int64_t newCounter = (oldCounter == INT64_MAX) ? INT64_MIN : (oldCounter + 1);
|
2014-08-13 02:02:29 +02:00
|
|
|
[self storeString:[@(newCounter) stringValue] forKey:PASSWORD_COUNTER_KEY];
|
2014-07-11 00:33:51 +02:00
|
|
|
return newCounter;
|
|
|
|
}
|
|
|
|
|
2017-12-01 21:17:29 +01:00
|
|
|
+ (NSData *)signalingCipherKey
|
|
|
|
{
|
2014-07-11 00:33:51 +02:00
|
|
|
return [self dataForKey:SIGNALING_CIPHER_KEY andVerifyLength:SIGNALING_CIPHER_KEY_LENGTH];
|
|
|
|
}
|
|
|
|
|
2017-12-01 21:17:29 +01:00
|
|
|
+ (NSData *)signalingMacKey
|
|
|
|
{
|
2014-07-11 00:33:51 +02:00
|
|
|
return [self dataForKey:SIGNALING_MAC_KEY andVerifyLength:SIGNALING_MAC_KEY_LENGTH];
|
|
|
|
}
|
|
|
|
|
2017-12-01 21:17:29 +01:00
|
|
|
+ (NSData *)signalingExtraKey
|
|
|
|
{
|
2014-07-11 00:33:51 +02:00
|
|
|
return [self dataForKey:SIGNALING_EXTRA_KEY andVerifyLength:SIGNALING_EXTRA_KEY_LENGTH];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark Keychain wrapper methods
|
|
|
|
|
2017-12-01 21:17:29 +01:00
|
|
|
+ (void)storeData:(NSData *)data forKey:(NSString *)key
|
|
|
|
{
|
2018-03-05 15:30:58 +01:00
|
|
|
[OWSPrimaryStorage.dbReadWriteConnection setObject:data forKey:key inCollection:SignalKeyingCollection];
|
2014-07-11 00:33:51 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:17:29 +01:00
|
|
|
+ (NSData *)dataForKey:(NSString *)key andVerifyLength:(uint)length
|
|
|
|
{
|
2014-07-11 00:33:51 +02:00
|
|
|
NSData *data = [self dataForKey:key];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-08-14 03:13:24 +02:00
|
|
|
if (data.length != length) {
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogError(@"Length of data not matching. Got %lu, expected %u", (unsigned long)data.length, length);
|
2014-07-11 00:33:51 +02:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2014-07-11 00:33:51 +02:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-12-01 21:17:29 +01:00
|
|
|
+ (NSData *)dataForKey:(NSString *)key
|
|
|
|
{
|
2018-03-05 15:30:58 +01:00
|
|
|
return [OWSPrimaryStorage.dbReadConnection dataForKey:key inCollection:SignalKeyingCollection];
|
2014-07-11 00:33:51 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:17:29 +01:00
|
|
|
+ (NSString *)stringForKey:(NSString *)key
|
|
|
|
{
|
2018-03-05 15:30:58 +01:00
|
|
|
return [OWSPrimaryStorage.dbReadConnection stringForKey:key inCollection:SignalKeyingCollection];
|
2014-07-11 00:33:51 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:17:29 +01:00
|
|
|
+ (void)storeString:(NSString *)string forKey:(NSString *)key
|
|
|
|
{
|
2018-03-05 15:30:58 +01:00
|
|
|
[OWSPrimaryStorage.dbReadWriteConnection setObject:string forKey:key inCollection:SignalKeyingCollection];
|
2014-07-11 00:33:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|