#ifdef STANDALONE
#undef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
#undef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
#endif
#include "SecFramework.h"
#include <dispatch/dispatch.h>
#include <CoreFoundation/CFBundle.h>
#include <CoreFoundation/CFURLAccess.h>
#include <Security/SecRandom.h>
#include <CommonCrypto/CommonRandomSPI.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <utilities/debugging.h>
#include <utilities/SecCFWrappers.h>
#include <Security/SecBase.h>
#include <inttypes.h>
#if !(TARGET_IPHONE_SIMULATOR && defined(IPHONE_SIMULATOR_HOST_MIN_VERSION_REQUIRED) && IPHONE_SIMULATOR_HOST_MIN_VERSION_REQUIRED < 1090)
#include <sys/guarded.h>
#define USE_GUARDED_OPEN 1
#else
#define USE_GUARDED_OPEN 0
#endif
static CFStringRef kSecFrameworkBundleID = CFSTR("com.apple.Security");
CFGiblisGetSingleton(CFBundleRef, SecFrameworkGetBundle, bundle, ^{
*bundle = CFRetainSafe(CFBundleGetBundleWithIdentifier(kSecFrameworkBundleID));
})
CFStringRef SecFrameworkCopyLocalizedString(CFStringRef key,
CFStringRef tableName) {
CFBundleRef bundle = SecFrameworkGetBundle();
if (bundle)
return CFBundleCopyLocalizedString(bundle, key, key, tableName);
return CFRetainSafe(key);
}
CFURLRef SecFrameworkCopyResourceURL(CFStringRef resourceName,
CFStringRef resourceType, CFStringRef subDirName) {
CFURLRef url = NULL;
CFBundleRef bundle = SecFrameworkGetBundle();
if (bundle) {
url = CFBundleCopyResourceURL(bundle, resourceName,
resourceType, subDirName);
if (!url) {
secwarning("resource: %@.%@ in %@ not found", resourceName,
resourceType, subDirName);
}
}
return url;
}
CFDataRef SecFrameworkCopyResourceContents(CFStringRef resourceName,
CFStringRef resourceType, CFStringRef subDirName) {
CFURLRef url = SecFrameworkCopyResourceURL(resourceName, resourceType,
subDirName);
CFDataRef data = NULL;
if (url) {
SInt32 error;
if (!CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault,
url, &data, NULL, NULL, &error)) {
secwarning("read: %ld", (long) error);
}
CFRelease(url);
}
return data;
}
const SecRandomRef kSecRandomDefault = NULL;
int SecRandomCopyBytes(SecRandomRef rnd, size_t count, uint8_t *bytes) {
if (rnd != kSecRandomDefault)
return errSecParam;
return CCRandomCopyBytes(kCCRandomDefault, bytes, count);
}