1 // Copyright (c) 2018 Apple Inc. All rights reserved.
3 #import "OctagonTestHarnessXPCService.h"
5 #import <objc/runtime.h>
6 #import <Security/CKKSControlProtocol.h>
7 #import <Security/SecAccessControlPriv.h>
8 #import <CloudServices/CloudServices.h>
10 #import "SecDbKeychainItem.h"
11 #import "SecRemoteDevice.h"
14 @interface OctagonTestHarnessXPCService ()
15 @property (strong) SecRemoteDevice *remoteDevice;
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wprotocol"
21 @interface NSError (OctagonTestHarnessXPCService)
22 - (NSDictionary*)errorAsDictionary;
26 @implementation NSError (OctagonTestHarnessXPCService)
28 - (NSDictionary*)errorAsDictionary {
30 @"domain": self.domain,
31 @"code": @(self.code),
37 @implementation OctagonTestHarnessXPCService
39 - (instancetype)init {
40 if ((self = [super init]) != NULL) {
41 self.remoteDevice = [SecRemoteDevice new];
42 if (self.remoteDevice == nil) {
49 - (void)octagonReset:(NSString *)altDSID complete:(void (^)(NSNumber *, NSError *))complete {
51 [self.remoteDevice otReset:altDSID complete:^(bool success, NSError * _Nullable error) {
52 complete([NSNumber numberWithBool:success], error);
56 - (void)octagonPeerID:(NSString *)altDSID complete:(void (^)(NSString *, NSError *))complete {
58 [self.remoteDevice otPeerID:altDSID complete:^(NSString *peerID, NSError * _Nullable error) {
59 complete(peerID, error);
63 - (void)octagonInCircle:(NSString *)altDSID complete:(void (^)(NSNumber *, NSError *_Nullable error))complete
65 [self.remoteDevice otInCircle:altDSID complete:^(bool inCircle, NSError * _Nullable error) {
66 complete(@(inCircle), error);
74 - (void)secItemAdd:(NSDictionary *)input complete:(void (^)(NSNumber *, NSDictionary * _Nullable))reply
76 NSMutableDictionary *attributes = [input mutableCopy];
77 CFTypeRef data = NULL;
79 attributes[(__bridge NSString *)kSecReturnAttributes] = @YES;
80 attributes[(__bridge NSString *)kSecReturnPersistentRef] = @YES;
81 attributes[(__bridge NSString *)kSecReturnData] = @YES;
83 OSStatus status = SecItemAdd((__bridge CFDictionaryRef)attributes, &data);
84 NSDictionary *returnData = CFBridgingRelease(data);
86 reply(@(status), returnData);
89 - (void)secItemCopyMatching:(NSDictionary *)input complete:(void (^)(NSNumber *, NSArray<NSDictionary *>* _Nullable))reply
91 NSMutableDictionary *attributes = [input mutableCopy];
92 CFTypeRef data = NULL;
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;
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];
105 [result addObject:r];
108 reply(@(status), result);
111 - (void)secItemDelete:(NSDictionary *)input complete:(void (^)(NSNumber *))reply
113 NSMutableDictionary *attributes = [input mutableCopy];
115 attributes[(__bridge NSString *)kSecReturnAttributes] = @YES;
116 attributes[(__bridge NSString *)kSecReturnPersistentRef] = @YES;
117 attributes[(__bridge NSString *)kSecReturnData] = @YES;
119 OSStatus status = SecItemDelete((__bridge CFDictionaryRef)attributes);
124 //MARK: CloudServices
126 - (void)csAccountInfo:(NSDictionary *)info complete:(void (^)(NSDictionary * _Nullable, NSDictionary * _Nullable))reply
128 SecureBackup *sb = [[SecureBackup alloc] init];
130 [sb getAccountInfoWithInfo:info completionBlock:^(NSDictionary *results, NSError *error) {
131 reply(results, [error errorAsDictionary]);
135 - (void)csEnableInfo:(NSDictionary *)info complete:(void (^)(NSDictionary * _Nullable, NSDictionary * _Nullable))reply
137 SecureBackup *sb = [[SecureBackup alloc] init];
139 [sb enableWithInfo:info completionBlock:^(NSError *error) {
140 reply(@{}, [error errorAsDictionary]);
143 - (void)csUpdateInfo:(NSDictionary *)info complete:(void (^)(NSDictionary * _Nullable, NSDictionary * _Nullable))reply
145 SecureBackup *sb = [[SecureBackup alloc] init];
146 [sb updateMetadataWithInfo:info completionBlock:^(NSError *error) {
147 reply(@{}, [error errorAsDictionary]);
150 - (void)csDisableInfo:(NSDictionary *)info complete:(void (^)(NSDictionary * _Nullable, NSDictionary * _Nullable))reply
152 SecureBackup *sb = [[SecureBackup alloc] init];
153 [sb disableWithInfo:info completionBlock:^(NSError *error) {
154 reply(@{}, [error errorAsDictionary]);
158 - (void)csRecoverInfo:(NSDictionary *)info complete:(void (^)(NSDictionary * _Nullable, NSDictionary * _Nullable))reply
160 SecureBackup *sb = [[SecureBackup alloc] init];
161 [sb recoverWithInfo:info completionBlock:^(NSDictionary *results, NSError *error) {
162 reply(results, [error errorAsDictionary]);
168 #pragma clang diagnostic pop