]> git.saurik.com Git - apple/security.git/blob - RegressionTests/seckeychainnetworkextensionstest/main.m
Security-59306.11.20.tar.gz
[apple/security.git] / RegressionTests / seckeychainnetworkextensionstest / main.m
1 //
2 // main.m
3 // seckeychainnetworkextensionstest
4 //
5 // Created by Luke Hiesterman on 2/22/17.
6 //
7
8 #import <Foundation/Foundation.h>
9 #import <Security/Security.h>
10 #import <Security/SecItemPriv.h>
11 #import <err.h>
12
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";
17
18 static void cleanupKeychain()
19 {
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);
26
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);
33
34 }
35
36 int main(int argc, const char * argv[])
37 {
38 @autoreleasepool {
39 cleanupKeychain();
40
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;
48
49 CFTypeRef returnData = NULL;
50 OSStatus result = SecItemAdd((__bridge CFDictionaryRef)attributes, &returnData);
51 if (result != 0) {
52 NSLog(@"got an error: %d", (int)result);
53 errx(1, "failed to add item to keychain");
54 }
55
56 if (returnData) {
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;
63
64 result = SecItemAdd((__bridge CFDictionaryRef)attributes, &returnData);
65 if (result == 0) {
66 NSLog(@"successfully stored persistent ref for shared network extension item to keychain");
67 }
68 else {
69 errx(1, "failed to add persistent ref to keychain");
70 }
71 }
72 else {
73 errx(1, "failed to get persistent ref from item added to keychain");
74 }
75 }
76 return 0;
77 }