]> git.saurik.com Git - apple/security.git/blob - keychain/Trieste/OctagonTestHarnessXPCService/OctagonTestHarnessXPCService.m
Security-59306.61.1.tar.gz
[apple/security.git] / keychain / Trieste / OctagonTestHarnessXPCService / OctagonTestHarnessXPCService.m
1 // Copyright (c) 2018 Apple Inc. All rights reserved.
2
3 #import "OctagonTestHarnessXPCService.h"
4
5 #import <objc/runtime.h>
6 #import <Security/CKKSControlProtocol.h>
7 #import <Security/SecAccessControlPriv.h>
8 #import <CloudServices/CloudServices.h>
9
10 #import "SecDbKeychainItem.h"
11 #import "SecRemoteDevice.h"
12 #import "OTControl.h"
13
14 @interface OctagonTestHarnessXPCService ()
15 @property (strong) SecRemoteDevice *remoteDevice;
16 @end
17
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wprotocol"
20
21 @interface NSError (OctagonTestHarnessXPCService)
22 - (NSDictionary*)errorAsDictionary;
23 @end
24
25
26 @implementation NSError (OctagonTestHarnessXPCService)
27
28 - (NSDictionary*)errorAsDictionary {
29 return @{
30 @"domain": self.domain,
31 @"code": @(self.code),
32 };
33 }
34 @end
35
36
37 @implementation OctagonTestHarnessXPCService
38
39 - (instancetype)init {
40 if ((self = [super init]) != NULL) {
41 self.remoteDevice = [SecRemoteDevice new];
42 if (self.remoteDevice == nil) {
43 return nil;
44 }
45 }
46 return self;
47 }
48
49 - (void)octagonReset:(NSString *)altDSID complete:(void (^)(NSNumber *, NSError *))complete {
50
51 [self.remoteDevice otReset:altDSID complete:^(bool success, NSError * _Nullable error) {
52 complete([NSNumber numberWithBool:success], error);
53 }];
54 }
55
56 - (void)octagonPeerID:(NSString *)altDSID complete:(void (^)(NSString *, NSError *))complete {
57
58 [self.remoteDevice otPeerID:altDSID complete:^(NSString *peerID, NSError * _Nullable error) {
59 complete(peerID, error);
60 }];
61 }
62
63 - (void)octagonInCircle:(NSString *)altDSID complete:(void (^)(NSNumber *, NSError *_Nullable error))complete
64 {
65 [self.remoteDevice otInCircle:altDSID complete:^(bool inCircle, NSError * _Nullable error) {
66 complete(@(inCircle), error);
67 }];
68 }
69
70
71 //MARK: Keychain
72
73
74 - (void)secItemAdd:(NSDictionary *)input complete:(void (^)(NSNumber *, NSDictionary * _Nullable))reply
75 {
76 NSMutableDictionary *attributes = [input mutableCopy];
77 CFTypeRef data = NULL;
78
79 attributes[(__bridge NSString *)kSecReturnAttributes] = @YES;
80 attributes[(__bridge NSString *)kSecReturnPersistentRef] = @YES;
81 attributes[(__bridge NSString *)kSecReturnData] = @YES;
82
83 OSStatus status = SecItemAdd((__bridge CFDictionaryRef)attributes, &data);
84 NSDictionary *returnData = CFBridgingRelease(data);
85
86 reply(@(status), returnData);
87
88 }
89 - (void)secItemCopyMatching:(NSDictionary *)input complete:(void (^)(NSNumber *, NSArray<NSDictionary *>* _Nullable))reply
90 {
91 NSMutableDictionary *attributes = [input mutableCopy];
92 CFTypeRef data = NULL;
93
94 attributes[(__bridge NSString *)kSecReturnAttributes] = @YES;
95 attributes[(__bridge NSString *)kSecReturnData] = @YES;
96 attributes[(__bridge NSString *)kSecReturnPersistentRef] = @YES;
97 attributes[(__bridge NSString *)kSecMatchLimit] = (__bridge id)kSecMatchLimitAll;
98
99 OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)attributes, &data);
100 NSArray<NSDictionary *>* array = CFBridgingRelease(data);
101 NSMutableArray *result = [NSMutableArray array];
102 for (NSDictionary *d in array) {
103 NSMutableDictionary *r = [d mutableCopy];
104 r[@"accc"] = nil;
105 [result addObject:r];
106 }
107
108 reply(@(status), result);
109
110 }
111 - (void)secItemDelete:(NSDictionary *)input complete:(void (^)(NSNumber *))reply
112 {
113 NSMutableDictionary *attributes = [input mutableCopy];
114
115 attributes[(__bridge NSString *)kSecReturnAttributes] = @YES;
116 attributes[(__bridge NSString *)kSecReturnPersistentRef] = @YES;
117 attributes[(__bridge NSString *)kSecReturnData] = @YES;
118
119 OSStatus status = SecItemDelete((__bridge CFDictionaryRef)attributes);
120
121 reply(@(status));
122 }
123
124 //MARK: CloudServices
125
126 - (void)csAccountInfo:(NSDictionary *)info complete:(void (^)(NSDictionary * _Nullable, NSDictionary * _Nullable))reply
127 {
128 SecureBackup *sb = [[SecureBackup alloc] init];
129
130 [sb getAccountInfoWithInfo:info completionBlock:^(NSDictionary *results, NSError *error) {
131 reply(results, [error errorAsDictionary]);
132 }];
133 }
134
135 - (void)csEnableInfo:(NSDictionary *)info complete:(void (^)(NSDictionary * _Nullable, NSDictionary * _Nullable))reply
136 {
137 SecureBackup *sb = [[SecureBackup alloc] init];
138
139 [sb enableWithInfo:info completionBlock:^(NSError *error) {
140 reply(@{}, [error errorAsDictionary]);
141 }];
142 }
143 - (void)csUpdateInfo:(NSDictionary *)info complete:(void (^)(NSDictionary * _Nullable, NSDictionary * _Nullable))reply
144 {
145 SecureBackup *sb = [[SecureBackup alloc] init];
146 [sb updateMetadataWithInfo:info completionBlock:^(NSError *error) {
147 reply(@{}, [error errorAsDictionary]);
148 }];
149 }
150 - (void)csDisableInfo:(NSDictionary *)info complete:(void (^)(NSDictionary * _Nullable, NSDictionary * _Nullable))reply
151 {
152 SecureBackup *sb = [[SecureBackup alloc] init];
153 [sb disableWithInfo:info completionBlock:^(NSError *error) {
154 reply(@{}, [error errorAsDictionary]);
155 }];
156 }
157
158 - (void)csRecoverInfo:(NSDictionary *)info complete:(void (^)(NSDictionary * _Nullable, NSDictionary * _Nullable))reply
159 {
160 SecureBackup *sb = [[SecureBackup alloc] init];
161 [sb recoverWithInfo:info completionBlock:^(NSDictionary *results, NSError *error) {
162 reply(results, [error errorAsDictionary]);
163 }];
164 }
165
166 @end
167
168 #pragma clang diagnostic pop