Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to provide SQLCipher license key #3807

Merged
merged 6 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libs/SmartStore/SmartStore/Classes/SFSmartStore+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ static NSString *const EXPLAIN_ROWS = @"rows";

@interface SFSmartStore ()

+ (NSString *)licenseKey;

@property (nonatomic, strong) FMDatabaseQueue *storeQueue;
@property (nonatomic, strong) SFSmartStoreDatabaseManager *dbMgr;
@property (nonatomic, assign) BOOL isGlobal;
Expand Down
21 changes: 21 additions & 0 deletions libs/SmartStore/SmartStore/Classes/SFSmartStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ NS_SWIFT_NAME(SmartStore)
*/
+ (void)setEncryptionKeyGenerator:(SFSmartStoreEncryptionKeyGenerator)newEncryptionKeyGenerator;

/**
Set license key for SQLCipher
Needed when using commercial or enterprise editions of SQLCipher
Should be called before using SmartStore

@param licenseKey The license key string provided by Zetetic
*/
+ (void)setLicenseKey:(NSString*)licenseKey;

#pragma mark - Soup manipulation methods

/**
Expand Down Expand Up @@ -460,6 +469,18 @@ NS_SWIFT_NAME(SmartStore)
*/
- (NSString *)getSQLCipherVersion NS_SWIFT_NAME(versionOfSQLCipher());

/**
* Return SQLCipher provider version
* @return cipher provider version
*/
- (NSString *)getCipherProviderVersion NS_SWIFT_NAME(cipherProviderVersion());

/**
* Return SQLCipher FIPS status
* @return true if using a FIPS enabled SQLCipher edition
*/
- (BOOL)getCipherFIPSStatus NS_SWIFT_NAME(cipherFIPSStatus());

#pragma mark - Long operations recovery methods

/**
Expand Down
20 changes: 20 additions & 0 deletions libs/SmartStore/SmartStore/Classes/SFSmartStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
static SFSmartStoreEncryptionSaltBlock _encryptionSaltBlock = NULL;
static BOOL _jsonSerializationCheckEnabled = NO;
static BOOL _postRawJsonOnError = NO;
static NSString* _licenseKey = NULL;

// The name of the store name used by the SFSmartStorePlugin for hybrid apps
NSString * const kDefaultSmartStoreName = @"defaultStore";
Expand Down Expand Up @@ -720,6 +721,14 @@ + (void)setEncryptionKeyGenerator:(SFSmartStoreEncryptionKeyGenerator)newEncrypt
}
}

+ (NSString *)licenseKey {
return _licenseKey;
}

+ (void)setLicenseKey:(NSString*)licenseKey {
_licenseKey = [licenseKey copy];
}

- (NSNumber *)currentTimeInMilliseconds {
NSTimeInterval rawTime = 1000 * [[NSDate date] timeIntervalSince1970];
rawTime = floor(rawTime);
Expand Down Expand Up @@ -2027,6 +2036,17 @@ - (NSString*) getSQLCipherVersion
return [[self queryPragma:@"cipher_version"] componentsJoinedByString:@""];
}

- (NSString*) getCipherProviderVersion
{
return [[self queryPragma:@"cipher_provider_version"] componentsJoinedByString:@""];
}

- (BOOL) getCipherFipsStatus
{
NSString *cipherFIPSStatus = [[self queryPragma:@"cipher_fips_status"] componentsJoinedByString:@""];
return [cipherFIPSStatus isEqualToString:@"1"];
}

- (NSArray*) queryPragma:(NSString*) pragma
{
__block NSMutableArray* result = [NSMutableArray new];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

#import "SFSmartStoreDatabaseManager+Internal.h"
#import "SFSmartStore+Internal.h"
#import <SalesforceSDKCore/UIDevice+SFHardware.h>
#import <SalesforceSDKCore/NSData+SFAdditions.h>
#import <SalesforceSDKCore/NSString+SFAdditions.h>
Expand Down Expand Up @@ -199,6 +200,9 @@ + (FMDatabase*) setKeyForDb:(FMDatabase*) db key:(NSString *)key salt:(NSString

+ (FMDatabase*) unlockDatabase:(FMDatabase*)db key:(NSString*)key salt:(NSString *)salt {
if ([db open]) {
if ([SFSmartStore licenseKey])
[[db executeQuery:[NSString stringWithFormat:@"PRAGMA cipher_license = '%@'", [SFSmartStore licenseKey]]] close];

if (key)
[db setKey:key];

Expand Down
12 changes: 12 additions & 0 deletions libs/SmartStore/SmartStoreTests/SFSmartStoreTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ - (void) testSqlCipherVersion
XCTAssertEqualObjects(version, @"4.6.1 community");
}

- (void) testCipherProviderVersion
{
NSString* cipherProviderVersion = [self.store getCipherProviderVersion];
XCTAssertEqualObjects(cipherProviderVersion, @"unknown");
}

- (void) testCipherFIPSStatus
{
BOOL cipherFIPSStatus = [self.store getCipherFIPSStatus];
XCTAssertFalse(cipherFIPSStatus);
}

/**
* Test fts extension
*/
Expand Down
Loading