2 #import "FakeSOSControl.h"
4 @implementation FakeNSXPCConnection
5 - (instancetype) initWithControl:(id<SOSControlProtocol>)control
7 if ((self = [super init])) {
12 - (id)remoteObjectProxyWithErrorHandler:(void(^)(NSError * _Nonnull error))failureHandler
19 @implementation FCPairingFakeSOSControl
21 - (instancetype)initWithRandomAccountKey:(bool)randomAccountKey circle:(SOSCircleRef)circle
23 if ((self = [super init])) {
24 SecKeyRef publicKey = NULL;
25 NSDictionary* parameters = @{
26 (__bridge NSString*)kSecAttrKeyType:(__bridge NSString*) kSecAttrKeyTypeEC,
27 (__bridge NSString*)kSecAttrKeySizeInBits: @(256),
28 (__bridge NSString*)kSecUseDataProtectionKeychain : @YES,
29 (__bridge NSString*)kSecAttrAccessible : (__bridge id)kSecAttrAccessibleAfterFirstUnlock,
30 (__bridge id)kSecPrivateKeyAttrs : @{
31 (__bridge NSString*)kSecAttrLabel : @"delete me test case - private",
32 (__bridge NSString*)kSecAttrIsPermanent : @YES,
33 (__bridge NSString*)kSecAttrAccessible : (__bridge id)kSecAttrAccessibleAfterFirstUnlock,
35 (__bridge id)kSecPublicKeyAttrs : @{
36 (__bridge NSString*)kSecAttrLabel : @"delete me test case - public",
37 (__bridge NSString*)kSecAttrAccessible : (__bridge id)kSecAttrAccessibleAfterFirstUnlock,
40 if(SecKeyGeneratePair((__bridge CFDictionaryRef)parameters, &publicKey, &_deviceKey) != 0) {
41 NSLog(@"failed to create device key");
44 CFReleaseNull(publicKey);
46 NSMutableDictionary* octagonParameters = [parameters mutableCopy];
47 octagonParameters[(__bridge NSString*)kSecAttrKeySizeInBits] = @(384);
48 if(SecKeyGeneratePair((__bridge CFDictionaryRef)octagonParameters, &publicKey, &_octagonSigningKey) != 0) {
49 NSLog(@"failed to create octagon signing key");
52 CFReleaseNull(publicKey);
54 if(SecKeyGeneratePair((__bridge CFDictionaryRef)octagonParameters, &publicKey, &_octagonEncryptionKey) != 0) {
55 NSLog(@"failed to create octagon signing key");
58 CFReleaseNull(publicKey);
61 _circle = (SOSCircleRef)CFRetain(circle);
63 CFErrorRef error = NULL;
65 CFDictionaryRef gestalt = (__bridge CFDictionaryRef)@{
66 @"ComputerName" : @"name",
69 _fullPeerInfo = SOSFullPeerInfoCreate(NULL, gestalt, NULL, _deviceKey, _octagonSigningKey, _octagonEncryptionKey, &error);
72 if (randomAccountKey) {
74 NSDictionary* accountParams = @{
75 (__bridge NSString*)kSecAttrKeyType:(__bridge NSString*) kSecAttrKeyTypeEC,
76 (__bridge NSString*)kSecAttrKeySizeInBits: @(256),
77 (__bridge NSString*)kSecUseDataProtectionKeychain : @YES,
78 (__bridge NSString*)kSecAttrAccessible : (__bridge id)kSecAttrAccessibleAfterFirstUnlock,
81 if(SecKeyGeneratePair((__bridge CFDictionaryRef)accountParams, &publicKey, &_accountPrivateKey) != 0) {
82 NSLog(@"failed to create account signing key");
85 CFReleaseNull(publicKey);
87 _accountPublicKey = SecKeyCopyPublicKey(_accountPrivateKey);
89 [self signApplicationIfNeeded];
97 if (_accountPrivateKey) {
98 SecItemDelete((__bridge CFTypeRef)@{ (__bridge id)kSecValueRef : (__bridge id)_accountPrivateKey });
99 CFReleaseNull(_accountPrivateKey);
102 SecItemDelete((__bridge CFTypeRef)@{ (__bridge id)kSecValueRef : (__bridge id)_deviceKey });
103 CFReleaseNull(_deviceKey);
105 if (_octagonSigningKey) {
106 SecItemDelete((__bridge CFTypeRef)@{ (__bridge id)kSecValueRef : (__bridge id)_octagonSigningKey });
107 CFReleaseNull(_octagonSigningKey);
109 if (_octagonEncryptionKey) {
110 SecItemDelete((__bridge CFTypeRef)@{ (__bridge id)kSecValueRef : (__bridge id)_octagonEncryptionKey });
111 CFReleaseNull(_octagonEncryptionKey);
113 CFReleaseNull(_circle);
114 CFReleaseNull(_fullPeerInfo);
117 - (SOSPeerInfoRef)peerInfo
119 return SOSFullPeerInfoGetPeerInfo(_fullPeerInfo);
122 - (void)signApplicationIfNeeded
124 CFErrorRef error = NULL;
126 _application = SOSFullPeerInfoPromoteToApplication(_fullPeerInfo, _accountPrivateKey, &error);
131 - (void)initialSyncCredentials:(uint32_t)flags complete:(void (^)(NSArray *, NSError *))complete
133 // Make up a fake TLK
134 NSMutableArray<NSDictionary *> *items = [NSMutableArray array];
135 if (flags & SOSControlInitialSyncFlagTLK) {
136 NSString *tlkUUID = [[NSUUID UUID] UUIDString];
137 NSDictionary *fakeTLK = @{
139 @"agrp": @"com.apple.security.ckks",
140 @"vwht": @"PCS-master",
143 @"srvr": @"fakeZone",
146 @"v_Data": [NSData data],
148 [items addObject:fakeTLK];
150 complete(items, nil);
153 - (void)importInitialSyncCredentials:(NSArray *)items complete:(void (^)(bool success, NSError *))complete
158 - (void)rpcTriggerSync:(NSArray<NSString *> *)peers complete:(void(^)(bool success, NSError *))complete
160 complete(true, NULL);
163 //MARK - FCPairingFakeSOSControl SOSControlProtocol
165 - (void)userPublicKey:(void ((^))(BOOL trusted, NSData *spki, NSError *error))complete
167 complete(false, NULL, NULL);
170 - (void)performanceCounters:(void(^)(NSDictionary <NSString *, NSNumber *> *))complete
174 - (void)kvsPerformanceCounters:(void(^)(NSDictionary <NSString *, NSNumber *> *))complete
179 - (void)rateLimitingPerformanceCounters:(void(^)(NSDictionary <NSString *, NSString *> *))complete
183 - (void)stashedCredentialPublicKey:(void(^)(NSData *, NSError *error))complete
185 NSData *publicKey = NULL;
186 NSError *error = NULL;
187 if (self.accountPrivateKey) {
188 publicKey = CFBridgingRelease(SecKeyCopySubjectPublicKeyInfo(self.accountPrivateKey));
190 error = [NSError errorWithDomain:@"FCPairingFakeSOSControl" code:2 userInfo:NULL];
192 complete(publicKey, error);
195 - (void)assertStashedAccountCredential:(void(^)(BOOL result, NSError *error))complete
197 complete(self.accountPrivateKey != NULL, NULL);
200 - (void)validatedStashedAccountCredential:(void(^)(NSData *credential, NSError *error))complete
203 CFErrorRef error = NULL;
204 if (self.accountPrivateKey) {
205 key = CFBridgingRelease(SecKeyCopyExternalRepresentation(self.accountPrivateKey, &error));
207 error = (CFErrorRef)CFBridgingRetain([NSError errorWithDomain:@"FCPairingFakeSOSControl" code:1 userInfo:NULL]);
209 complete(key, (__bridge NSError *)error);
210 CFReleaseNull(error);
213 - (void)stashAccountCredential:(NSData *)credential complete:(void(^)(bool success, NSError *error))complete
215 SecKeyRef accountPrivateKey = NULL;
216 CFErrorRef error = NULL;
217 NSDictionary *attributes = @{
218 (__bridge id)kSecAttrKeyClass : (__bridge id)kSecAttrKeyClassPrivate,
219 (__bridge id)kSecAttrKeyType : (__bridge id)kSecAttrKeyTypeEC,
222 accountPrivateKey = SecKeyCreateWithData((__bridge CFDataRef)credential, (__bridge CFDictionaryRef)attributes, &error);
223 if (accountPrivateKey == NULL) {
224 complete(false, (__bridge NSError *)error);
225 CFReleaseNull(error);
229 _accountPrivateKey = accountPrivateKey;
230 _accountPublicKey = SecKeyCopyPublicKey(_accountPrivateKey);
232 [self signApplicationIfNeeded];
234 complete(true, NULL);
237 - (void)myPeerInfo:(void(^)(NSData *application, NSError *error))complete
239 CFErrorRef error = NULL;
241 [self signApplicationIfNeeded];
243 NSData *application = CFBridgingRelease(SOSPeerInfoCopyEncodedData([self peerInfo], NULL, &error));
244 complete(application, (__bridge NSError *)error);
246 CFReleaseNull(error);
249 - (void)circleHash:(void (^)(NSString *, NSError *))complete
251 NSString *data = CFBridgingRelease(SOSCircleCopyHashString(_circle));
252 complete(data, NULL);
255 - (void)circleJoiningBlob:(NSData *)applicantData complete:(void (^)(NSData *blob, NSError *))complete
257 CFErrorRef error = NULL;
258 CFDataRef signature = NULL;
259 SOSCircleRef prunedCircle = SOSCircleCopyCircle(NULL, _circle, &error);
260 (void)SOSCirclePreGenerationSign(prunedCircle, _accountPublicKey, &error);
262 SOSGenCountRef gencount = SOSGenerationIncrementAndCreate(SOSCircleGetGeneration(prunedCircle));
263 if (gencount == NULL)
267 SOSPeerInfoRef applicant = SOSPeerInfoCreateFromData(NULL, &error, (__bridge CFDataRef)applicantData);
268 if (applicant == NULL)
271 signature = SOSCircleCopyNextGenSignatureWithPeerAdded(prunedCircle, applicant, _deviceKey, &error);
273 CFRelease(applicant);
277 NSData *pbblob = CFBridgingRelease(SOSPiggyBackBlobCopyEncodedData(gencount, _deviceKey, signature, &error));
279 CFReleaseNull(signature);
280 CFReleaseNull(gencount);
281 CFReleaseNull(prunedCircle);
283 complete(pbblob, NULL);
286 - (void)joinCircleWithBlob:(NSData *)blob version:(PiggyBackProtocolVersion)version complete:(void (^)(bool success, NSError *))complete
288 SOSGenCountRef gencount = NULL;
289 SecKeyRef pubKey = NULL;
290 CFDataRef signature = NULL;
291 CFErrorRef error = NULL;
292 bool setInitialSyncTimeoutToV0 = false;
294 if (!SOSPiggyBackBlobCreateFromData(&gencount, &pubKey, &signature, (__bridge CFDataRef)blob, kPiggyV1, &setInitialSyncTimeoutToV0, &error)) {
295 complete(true, (__bridge NSError *)error);
296 CFReleaseNull(error);
300 (void)SOSCircleAcceptPeerFromHSA2(_circle,
308 CFReleaseNull(gencount);
309 CFReleaseNull(pubKey);
310 CFReleaseNull(signature);
312 complete(true, (__bridge NSError *)error);
314 CFReleaseNull(error);
318 - (void)getWatchdogParameters:(void (^)(NSDictionary*, NSError*))complete
320 // intentionally left blank
321 // these are used by the security/2 tool and are only declared here to make the compiler happy about conforming the protocol we shoved the methods into
325 - (void)setWatchdogParmeters:(NSDictionary*)parameters complete:(void (^)(NSError*))complete
327 // intentionally left blank
328 // these are used by the security/2 tool and are only declared here to make the compiler happy about conforming the protocol we shoved the methods into
331 - (void)ghostBust:(SOSAccountGhostBustingOptions)options complete:(void (^)(bool, NSError *))complete {
332 complete(false, nil);
335 - (void)ghostBustPeriodic:(SOSAccountGhostBustingOptions)options complete: (void(^)(bool busted, NSError *error))complete{
336 complete(false, nil);
339 - (void)ghostBustTriggerTimed:(SOSAccountGhostBustingOptions)options complete: (void(^)(bool ghostBusted, NSError *error))complete {
340 complete(false, nil);
343 - (void) ghostBustInfo: (void(^)(NSData *json, NSError *error))complete {
347 - (void)iCloudIdentityStatus_internal: (void(^)(NSDictionary *tableSpid, NSError *error))complete {
351 - (void) iCloudIdentityStatus: (void(^)(NSData *json, NSError *error))complete {
355 - (void)rpcTriggerBackup:(NSArray<NSString *> *)backupPeers complete:(void (^)(NSError *))complete {
359 - (void)rpcTriggerRingUpdate:(void (^)(NSError *))complete {