2 // Copyright 2015 - 2016 Apple. All rights reserved.
6 * This is to fool os services to not provide the Keychain manager
7 * interface tht doens't work since we don't have unified headers
8 * between iOS and OS X. rdar://23405418/
10 #define __KEYCHAINCORE__ 1
12 #include <Foundation/Foundation.h>
13 #include <Security/Security.h>
15 #include <TargetConditionals.h>
17 #include <Security/SecItemPriv.h>
21 #if TARGET_OS_SIMULATOR
34 keybag_handle_t handle;
39 result = aks_create_bag("foo", 3, kAppleKeyStoreAsymmetricBackupBag, &handle);
41 errx(1, "aks_create_bag: %08x", result);
43 result = aks_save_bag(handle, &data, &length);
45 errx(1, "aks_save_bag");
47 return [NSData dataWithBytes:data length:length];
50 int main (int argc, const char * argv[])
53 NSData *bag = NULL, *password = NULL;
54 CFErrorRef error = NULL;
57 password = [NSData dataWithBytes:"foo" length:3];
59 NSData *backup = CFBridgingRelease(_SecKeychainCopyBackup((__bridge CFDataRef)bag, (__bridge CFDataRef)password));
61 errx(1, "backup failed");
64 char path[] = "/tmp/secbackuptestXXXXXXX";
65 int fd = mkstemp(path);
67 bool status = _SecKeychainWriteBackupToFileDescriptor((__bridge CFDataRef)bag, (__bridge CFDataRef)password, fd, &error);
69 NSLog(@"backup failed: %@", error);
70 errx(1, "failed backup 2");
76 if (sb.st_size != (off_t)[backup length])
77 warn("backup different ");
79 if (abs((int)(sb.st_size - (off_t)[backup length])) > 1000)
80 errx(1, "backup different enough to fail");
83 status = _SecKeychainRestoreBackupFromFileDescriptor(fd, (__bridge CFDataRef)bag, (__bridge CFDataRef)password, &error);
85 NSLog(@"restore failed: %@", error);
86 errx(1, "restore failed");
92 NSData *backup2 = CFBridgingRelease(_SecKeychainCopyBackup((__bridge CFDataRef)bag, (__bridge CFDataRef)password));
93 if (backup2 == NULL) {
94 errx(1, "backup 3 failed");
97 if (abs((int)(sb.st_size - (off_t)[backup2 length])) > 1000)
98 errx(1, "backup different enough to fail (mem vs backup2): %d vs %d", (int)sb.st_size, (int)[backup2 length]);
99 if (abs((int)([backup length] - [backup2 length])) > 1000)
100 errx(1, "backup different enough to fail (backup1 vs backup2: %d vs %d", (int)[backup length], (int)[backup2 length]);
106 #endif /* TARGET_OS_SIMULATOR */