diff options
-rw-r--r-- | Source/SPDataAdditions.m | 10 | ||||
-rw-r--r-- | Source/SPFunctions.h | 7 | ||||
-rw-r--r-- | Source/SPFunctions.m | 30 | ||||
-rw-r--r-- | sequel-pro.xcodeproj/project.pbxproj | 4 |
4 files changed, 47 insertions, 4 deletions
diff --git a/Source/SPDataAdditions.m b/Source/SPDataAdditions.m index 8002595c..8b2207c5 100644 --- a/Source/SPDataAdditions.m +++ b/Source/SPDataAdditions.m @@ -37,6 +37,7 @@ #include <zlib.h> #include <CommonCrypto/CommonCrypto.h> #include <stdlib.h> +#import "SPFunctions.h" uint32_t LimitUInt32(NSUInteger i); @@ -74,11 +75,12 @@ uint32_t LimitUInt32(NSUInteger i); { // Create a random 128-bit initialization vector // IV is block "-1" of plaintext data, therefore it is blockSize long - srand((unsigned int)time(NULL)); - NSInteger ivIndex; unsigned char iv[kCCBlockSizeAES128]; - for (ivIndex = 0; ivIndex < kCCBlockSizeAES128; ivIndex++) - iv[ivIndex] = rand() & 0xff; + if(SPBetterRandomBytes(iv,sizeof(iv)) != 0) + @throw [NSException exceptionWithName:NSInternalInconsistencyException + reason:@"Getting random data bytes failed!" + userInfo:@{@"errno":@(errno)}]; + NSData *ivData = [NSData dataWithBytes:iv length:sizeof(iv)]; // Create the key from first 128-bits of the 160-bit password hash diff --git a/Source/SPFunctions.h b/Source/SPFunctions.h index b68964ca..e462b8cb 100644 --- a/Source/SPFunctions.h +++ b/Source/SPFunctions.h @@ -35,3 +35,10 @@ */ void SPMainQSync(void (^block)(void)); +/** + * Copies count bytes into buf provided by caller + * @param buf Base address to copy to + * @param count Number of bytes to copy + * @return 0 on success or -1 if something went wrong, check errno + */ +int SPBetterRandomBytes(uint8_t *buf, size_t count); diff --git a/Source/SPFunctions.m b/Source/SPFunctions.m index f485d36a..851c2422 100644 --- a/Source/SPFunctions.m +++ b/Source/SPFunctions.m @@ -29,6 +29,8 @@ // More info at <https://github.com/sequelpro/sequelpro> #import "SPFunctions.h" +#import <Security/Security.h> +#import "SPOSInfo.h" void SPMainQSync(void (^block)(void)) { @@ -39,3 +41,31 @@ void SPMainQSync(void (^block)(void)) dispatch_sync(dispatch_get_main_queue(), block); } } + +int SPBetterRandomBytes(uint8_t *buf, size_t count) +{ +#if MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_7 + if([SPOSInfo isOSVersionAtLeastMajor:10 minor:7 patch:0]) { + return SecRandomCopyBytes(kSecRandomDefault, count, buf); + } +#endif + // Version for 10.6 + // https://developer.apple.com/library/prerelease/mac/documentation/Security/Conceptual/cryptoservices/RandomNumberGenerationAPIs/RandomNumberGenerationAPIs.html#//apple_ref/doc/uid/TP40011172-CH12-SW1 + FILE *fp = fopen("/dev/random", "r"); + + if (!fp) return -1; + + size_t i; + for (i=0; i<count; i++) { + int c = fgetc(fp); + if(c == EOF) { // /dev/random should never EOF + errno = ferror(fp); + return -1; + } + buf[i] = c; + } + + fclose(fp); + + return 0; +} diff --git a/sequel-pro.xcodeproj/project.pbxproj b/sequel-pro.xcodeproj/project.pbxproj index 9a78e182..3f78a15e 100644 --- a/sequel-pro.xcodeproj/project.pbxproj +++ b/sequel-pro.xcodeproj/project.pbxproj @@ -193,6 +193,8 @@ 507FF1621BBF0D5000104523 /* SPTableCopyTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 112730551180788A000737FD /* SPTableCopyTest.m */; }; 507FF2421BC33BBC00104523 /* SPOSInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 50EAB5B71A8FBB08008F627A /* SPOSInfo.m */; }; 507FF26A1BC8450100104523 /* SPExportSettingsPersistence.m in Sources */ = {isa = PBXBuildFile; fileRef = 507FF2691BC8450100104523 /* SPExportSettingsPersistence.m */; }; + 507FF2A11BCD27A700104523 /* SPFunctions.m in Sources */ = {isa = PBXBuildFile; fileRef = 507FF1111BBCC57600104523 /* SPFunctions.m */; }; + 507FF2A21BCD27AE00104523 /* SPOSInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 50EAB5B71A8FBB08008F627A /* SPOSInfo.m */; }; 50A9F8B119EAD4B90053E571 /* SPGotoDatabaseController.m in Sources */ = {isa = PBXBuildFile; fileRef = 50A9F8B019EAD4B90053E571 /* SPGotoDatabaseController.m */; }; 50D3C3491A75B8A800B5429C /* GotoDatabaseDialog.xib in Resources */ = {isa = PBXBuildFile; fileRef = 50D3C34B1A75B8A800B5429C /* GotoDatabaseDialog.xib */; }; 50D3C3521A77135F00B5429C /* SPParserUtils.c in Sources */ = {isa = PBXBuildFile; fileRef = 50D3C3501A77135F00B5429C /* SPParserUtils.c */; }; @@ -3102,6 +3104,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 507FF2A21BCD27AE00104523 /* SPOSInfo.m in Sources */, + 507FF2A11BCD27A700104523 /* SPFunctions.m in Sources */, 50D3C3541A7715E600B5429C /* SPParserUtils.c in Sources */, BC34F3281292AD6F000DA1AA /* SPConstants.m in Sources */, BC6D709E120C4C9F008027B5 /* SPEditorTokens.l in Sources */, |