3 // seckeychainnetworkextensionstest
5 // Created by Luke Hiesterman on 2/22/17.
8 #import <Foundation/Foundation.h>
9 #import <Security/Security.h>
10 #import <Security/SecItemPriv.h>
13 static NSString* NetworkExtensionPersistentRefSharingAccessGroup = @"com.apple.NetworkExtensionPersistentRefSharingAccessGroup";
14 static NSString* NetworkExtensionAccessGroup = @"FakeAppPrefix.com.apple.networkextensionsharing";
15 static NSString* TestAccount = @"MyTestAccount";
16 static NSString* TestPassword = @"MyTestPassword";
18 static void cleanupKeychain()
20 NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
21 attributes[(__bridge NSString*)kSecClass] = (__bridge NSString*)kSecClassGenericPassword;
22 attributes[(__bridge NSString*)kSecAttrAccessGroup] = NetworkExtensionAccessGroup;
23 attributes[(__bridge NSString*)kSecAttrAccount] = TestAccount;
24 attributes[(__bridge NSString*)kSecUseDataProtectionKeychain] = @YES;
25 SecItemDelete((__bridge CFDictionaryRef)attributes);
27 attributes = [NSMutableDictionary dictionary];
28 attributes[(__bridge NSString*)kSecClass] = (__bridge NSString*)kSecClassGenericPassword;
29 attributes[(__bridge NSString*)kSecAttrAccessGroup] = NetworkExtensionPersistentRefSharingAccessGroup;
30 attributes[(__bridge NSString*)kSecAttrAccount] = TestAccount;
31 attributes[(__bridge NSString*)kSecUseDataProtectionKeychain] = @YES;
32 SecItemDelete((__bridge CFDictionaryRef)attributes);
36 int main(int argc, const char * argv[])
41 NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
42 attributes[(__bridge NSString*)kSecClass] = (__bridge NSString*)kSecClassGenericPassword;
43 attributes[(__bridge NSString*)kSecAttrAccessGroup] = NetworkExtensionAccessGroup;
44 attributes[(__bridge NSString*)kSecAttrAccount] = TestAccount;
45 attributes[(__bridge NSString*)kSecValueData] = [NSData dataWithBytes:TestPassword.UTF8String length:TestPassword.length];
46 attributes[(__bridge NSString*)kSecReturnPersistentRef] = @YES;
47 attributes[(__bridge NSString*)kSecUseDataProtectionKeychain] = @YES;
49 CFTypeRef returnData = NULL;
50 OSStatus result = SecItemAdd((__bridge CFDictionaryRef)attributes, &returnData);
52 NSLog(@"got an error: %d", (int)result);
53 errx(1, "failed to add item to keychain");
57 attributes = [NSMutableDictionary dictionary];
58 attributes[(__bridge NSString*)kSecClass] = (__bridge NSString*)kSecClassGenericPassword;
59 attributes[(__bridge NSString*)kSecAttrAccessGroup] = NetworkExtensionPersistentRefSharingAccessGroup;
60 attributes[(__bridge NSString*)kSecAttrAccount] = TestAccount;
61 attributes[(__bridge NSString*)kSecValueData] = (__bridge NSData*)returnData;
62 attributes[(__bridge NSString*)kSecUseDataProtectionKeychain] = @YES;
64 result = SecItemAdd((__bridge CFDictionaryRef)attributes, &returnData);
66 NSLog(@"successfully stored persistent ref for shared network extension item to keychain");
69 errx(1, "failed to add persistent ref to keychain");
73 errx(1, "failed to get persistent ref from item added to keychain");