2 #import "FakeSOSControl.h"
4 @implementation FakeNSXPCConnection
5 - (instancetype) initWithControl:(id<SOSControlProtocol>)control
13 - (id)remoteObjectProxyWithErrorHandler:(void(^)(NSError * _Nonnull error))failureHandler
20 @implementation FCPairingFakeSOSControl
22 - (instancetype)initWithRandomAccountKey:(bool)randomAccountKey circle:(SOSCircleRef)circle
24 if ((self = [super init])) {
25 SecKeyRef publicKey = NULL;
26 NSDictionary* parameters = @{
27 (__bridge NSString*)kSecAttrKeyType:(__bridge NSString*) kSecAttrKeyTypeEC,
28 (__bridge NSString*)kSecAttrKeySizeInBits: @(256),
29 (__bridge NSString*)kSecUseDataProtectionKeychain : @YES,
30 (__bridge NSString*)kSecAttrAccessible : (__bridge id)kSecAttrAccessibleAfterFirstUnlock,
31 (__bridge id)kSecPrivateKeyAttrs : @{
32 (__bridge NSString*)kSecAttrLabel : @"delete me test case - private",
33 (__bridge NSString*)kSecAttrIsPermanent : @YES,
34 (__bridge NSString*)kSecAttrAccessible : (__bridge id)kSecAttrAccessibleAfterFirstUnlock,
36 (__bridge id)kSecPublicKeyAttrs : @{
37 (__bridge NSString*)kSecAttrLabel : @"delete me test case - public",
38 (__bridge NSString*)kSecAttrAccessible : (__bridge id)kSecAttrAccessibleAfterFirstUnlock,
41 if(SecKeyGeneratePair((__bridge CFDictionaryRef)parameters, &publicKey, &_deviceKey) != 0) {
42 NSLog(@"failed to create device key");
45 CFReleaseNull(publicKey);
47 NSMutableDictionary* octagonParameters = [parameters mutableCopy];
48 octagonParameters[(__bridge NSString*)kSecAttrKeySizeInBits] = @(384);
49 if(SecKeyGeneratePair((__bridge CFDictionaryRef)octagonParameters, &publicKey, &_octagonSigningKey) != 0) {
50 NSLog(@"failed to create octagon signing key");
53 CFReleaseNull(publicKey);
55 if(SecKeyGeneratePair((__bridge CFDictionaryRef)octagonParameters, &publicKey, &_octagonEncryptionKey) != 0) {
56 NSLog(@"failed to create octagon signing key");
59 CFReleaseNull(publicKey);
62 _circle = (SOSCircleRef)CFRetain(circle);
64 CFErrorRef error = NULL;
66 CFDictionaryRef gestalt = (__bridge CFDictionaryRef)@{
67 @"ComputerName" : @"name",
70 _fullPeerInfo = SOSFullPeerInfoCreate(NULL, gestalt, NULL, _deviceKey, _octagonSigningKey, _octagonEncryptionKey, &error);
73 if (randomAccountKey) {
75 NSDictionary* accountParams = @{
76 (__bridge NSString*)kSecAttrKeyType:(__bridge NSString*) kSecAttrKeyTypeEC,
77 (__bridge NSString*)kSecAttrKeySizeInBits: @(256),
78 (__bridge NSString*)kSecUseDataProtectionKeychain : @YES,
79 (__bridge NSString*)kSecAttrAccessible : (__bridge id)kSecAttrAccessibleAfterFirstUnlock,
82 if(SecKeyGeneratePair((__bridge CFDictionaryRef)accountParams, &publicKey, &_accountPrivateKey) != 0) {
83 NSLog(@"failed to create account signing key");
86 CFReleaseNull(publicKey);
88 _accountPublicKey = SecKeyCopyPublicKey(_accountPrivateKey);
90 [self signApplicationIfNeeded];
98 if (_accountPrivateKey) {
99 SecItemDelete((__bridge CFTypeRef)@{ (__bridge id)kSecValueRef : (__bridge id)_accountPrivateKey });
100 CFReleaseNull(_accountPrivateKey);
103 SecItemDelete((__bridge CFTypeRef)@{ (__bridge id)kSecValueRef : (__bridge id)_deviceKey });
104 CFReleaseNull(_deviceKey);
106 if (_octagonSigningKey) {
107 SecItemDelete((__bridge CFTypeRef)@{ (__bridge id)kSecValueRef : (__bridge id)_octagonSigningKey });
108 CFReleaseNull(_octagonSigningKey);
110 if (_octagonEncryptionKey) {
111 SecItemDelete((__bridge CFTypeRef)@{ (__bridge id)kSecValueRef : (__bridge id)_octagonEncryptionKey });
112 CFReleaseNull(_octagonEncryptionKey);
114 CFReleaseNull(_circle);
115 CFReleaseNull(_fullPeerInfo);
118 - (SOSPeerInfoRef)peerInfo
120 return SOSFullPeerInfoGetPeerInfo(_fullPeerInfo);
123 - (void)signApplicationIfNeeded
125 CFErrorRef error = NULL;
127 _application = SOSFullPeerInfoPromoteToApplication(_fullPeerInfo, _accountPrivateKey, &error);
132 - (void)initialSyncCredentials:(uint32_t)flags complete:(void (^)(NSArray *, NSError *))complete
134 // Make up a fake TLK
135 NSMutableArray<NSDictionary *> *items = [NSMutableArray array];
136 if (flags & SOSControlInitialSyncFlagTLK) {
137 NSString *tlkUUID = [[NSUUID UUID] UUIDString];
138 NSDictionary *fakeTLK = @{
140 @"agrp": @"com.apple.security.ckks",
141 @"vwht": @"PCS-master",
144 @"srvr": @"fakeZone",
147 @"v_Data": [NSData data],
149 [items addObject:fakeTLK];
151 complete(items, nil);
154 - (void)importInitialSyncCredentials:(NSArray *)items complete:(void (^)(bool success, NSError *))complete
159 - (void)triggerSync:(NSArray<NSString *> *)peers complete:(void(^)(bool success, NSError *))complete
161 complete(true, NULL);
164 //MARK - FCPairingFakeSOSControl SOSControlProtocol
166 - (void)userPublicKey:(void ((^))(BOOL trusted, NSData *spki, NSError *error))complete
168 complete(false, NULL, NULL);
171 - (void)performanceCounters:(void(^)(NSDictionary <NSString *, NSNumber *> *))complete
175 - (void)kvsPerformanceCounters:(void(^)(NSDictionary <NSString *, NSNumber *> *))complete
180 - (void)rateLimitingPerformanceCounters:(void(^)(NSDictionary <NSString *, NSString *> *))complete
184 - (void)stashedCredentialPublicKey:(void(^)(NSData *, NSError *error))complete
186 NSData *publicKey = NULL;
187 NSError *error = NULL;
188 if (self.accountPrivateKey) {
189 publicKey = CFBridgingRelease(SecKeyCopySubjectPublicKeyInfo(self.accountPrivateKey));
191 error = [NSError errorWithDomain:@"FCPairingFakeSOSControl" code:2 userInfo:NULL];
193 complete(publicKey, error);
196 - (void)assertStashedAccountCredential:(void(^)(BOOL result, NSError *error))complete
198 complete(self.accountPrivateKey != NULL, NULL);
201 - (void)validatedStashedAccountCredential:(void(^)(NSData *credential, NSError *error))complete
204 CFErrorRef error = NULL;
205 if (self.accountPrivateKey) {
206 key = CFBridgingRelease(SecKeyCopyExternalRepresentation(self.accountPrivateKey, &error));
208 error = (CFErrorRef)CFBridgingRetain([NSError errorWithDomain:@"FCPairingFakeSOSControl" code:1 userInfo:NULL]);
210 complete(key, (__bridge NSError *)error);
211 CFReleaseNull(error);
214 - (void)stashAccountCredential:(NSData *)credential complete:(void(^)(bool success, NSError *error))complete
216 SecKeyRef accountPrivateKey = NULL;
217 CFErrorRef error = NULL;
218 NSDictionary *attributes = @{
219 (__bridge id)kSecAttrKeyClass : (__bridge id)kSecAttrKeyClassPrivate,
220 (__bridge id)kSecAttrKeyType : (__bridge id)kSecAttrKeyTypeEC,
223 accountPrivateKey = SecKeyCreateWithData((__bridge CFDataRef)credential, (__bridge CFDictionaryRef)attributes, &error);
224 if (accountPrivateKey == NULL) {
225 complete(false, (__bridge NSError *)error);
226 CFReleaseNull(error);
230 _accountPrivateKey = accountPrivateKey;
231 _accountPublicKey = SecKeyCopyPublicKey(_accountPrivateKey);
233 [self signApplicationIfNeeded];
235 complete(true, NULL);
238 - (void)myPeerInfo:(void(^)(NSData *application, NSError *error))complete
240 CFErrorRef error = NULL;
242 [self signApplicationIfNeeded];
244 NSData *application = CFBridgingRelease(SOSPeerInfoCopyEncodedData([self peerInfo], NULL, &error));
245 complete(application, (__bridge NSError *)error);
247 CFReleaseNull(error);
250 - (void)circleHash:(void (^)(NSString *, NSError *))complete
252 NSString *data = CFBridgingRelease(SOSCircleCopyHashString(_circle));
253 complete(data, NULL);
256 - (void)circleJoiningBlob:(NSData *)applicantData complete:(void (^)(NSData *blob, NSError *))complete
258 CFErrorRef error = NULL;
259 CFDataRef signature = NULL;
260 SOSCircleRef prunedCircle = SOSCircleCopyCircle(NULL, _circle, &error);
261 (void)SOSCirclePreGenerationSign(prunedCircle, _accountPublicKey, &error);
263 SOSGenCountRef gencount = SOSGenerationIncrementAndCreate(SOSCircleGetGeneration(prunedCircle));
264 if (gencount == NULL)
268 SOSPeerInfoRef applicant = SOSPeerInfoCreateFromData(NULL, &error, (__bridge CFDataRef)applicantData);
269 if (applicant == NULL)
272 signature = SOSCircleCopyNextGenSignatureWithPeerAdded(prunedCircle, applicant, _deviceKey, &error);
274 CFRelease(applicant);
278 NSData *pbblob = CFBridgingRelease(SOSPiggyBackBlobCopyEncodedData(gencount, _deviceKey, signature, &error));
280 CFReleaseNull(signature);
281 CFReleaseNull(gencount);
282 CFReleaseNull(prunedCircle);
284 complete(pbblob, NULL);
287 - (void)joinCircleWithBlob:(NSData *)blob version:(PiggyBackProtocolVersion)version complete:(void (^)(bool success, NSError *))complete
289 SOSGenCountRef gencount = NULL;
290 SecKeyRef pubKey = NULL;
291 CFDataRef signature = NULL;
292 CFErrorRef error = NULL;
293 bool setInitialSyncTimeoutToV0 = false;
295 if (!SOSPiggyBackBlobCreateFromData(&gencount, &pubKey, &signature, (__bridge CFDataRef)blob, kPiggyV1, &setInitialSyncTimeoutToV0, &error)) {
296 complete(true, (__bridge NSError *)error);
297 CFReleaseNull(error);
301 (void)SOSCircleAcceptPeerFromHSA2(_circle,
309 CFReleaseNull(gencount);
310 CFReleaseNull(pubKey);
311 CFReleaseNull(signature);
313 complete(true, (__bridge NSError *)error);
315 CFReleaseNull(error);
319 - (void)getWatchdogParameters:(void (^)(NSDictionary*, NSError*))complete
321 // intentionally left blank
322 // 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
326 - (void)setWatchdogParmeters:(NSDictionary*)parameters complete:(void (^)(NSError*))complete
328 // intentionally left blank
329 // 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
332 - (void)ghostBust:(SOSAccountGhostBustingOptions)options complete:(void (^)(bool, NSError *))complete {
333 complete(false, nil);
336 - (void)ghostBustPeriodic:(SOSAccountGhostBustingOptions)options complete: (void(^)(bool busted, NSError *error))complete{
337 complete(false, nil);
340 - (void)ghostBustTriggerTimed:(SOSAccountGhostBustingOptions)options complete: (void(^)(bool ghostBusted, NSError *error))complete {
341 complete(false, nil);
344 - (void) ghostBustInfo: (void(^)(NSData *json, NSError *error))complete {
348 - (void)triggerBackup:(NSArray<NSString *> *)backupPeers complete:(void (^)(NSError *))complete {