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
137 - (void)importInitialSyncCredentials:(NSArray *)items complete:(void (^)(bool success, NSError *))complete
139 complete(true, NULL);
142 - (void)triggerSync:(NSArray<NSString *> *)peers complete:(void(^)(bool success, NSError *))complete
144 complete(true, NULL);
147 //MARK - FCPairingFakeSOSControl SOSControlProtocol
149 - (void)userPublicKey:(void ((^))(BOOL trusted, NSData *spki, NSError *error))complete
151 complete(false, NULL, NULL);
154 - (void)performanceCounters:(void(^)(NSDictionary <NSString *, NSNumber *> *))complete
158 - (void)kvsPerformanceCounters:(void(^)(NSDictionary <NSString *, NSNumber *> *))complete
163 - (void)rateLimitingPerformanceCounters:(void(^)(NSDictionary <NSString *, NSString *> *))complete
167 - (void)stashedCredentialPublicKey:(void(^)(NSData *, NSError *error))complete
169 NSData *publicKey = NULL;
170 NSError *error = NULL;
171 if (self.accountPrivateKey) {
172 publicKey = CFBridgingRelease(SecKeyCopySubjectPublicKeyInfo(self.accountPrivateKey));
174 error = [NSError errorWithDomain:@"FCPairingFakeSOSControl" code:2 userInfo:NULL];
176 complete(publicKey, error);
179 - (void)assertStashedAccountCredential:(void(^)(BOOL result, NSError *error))complete
181 complete(self.accountPrivateKey != NULL, NULL);
184 - (void)validatedStashedAccountCredential:(void(^)(NSData *credential, NSError *error))complete
187 CFErrorRef error = NULL;
188 if (self.accountPrivateKey) {
189 key = CFBridgingRelease(SecKeyCopyExternalRepresentation(self.accountPrivateKey, &error));
191 error = (CFErrorRef)CFBridgingRetain([NSError errorWithDomain:@"FCPairingFakeSOSControl" code:1 userInfo:NULL]);
193 complete(key, (__bridge NSError *)error);
194 CFReleaseNull(error);
197 - (void)stashAccountCredential:(NSData *)credential complete:(void(^)(bool success, NSError *error))complete
199 SecKeyRef accountPrivateKey = NULL;
200 CFErrorRef error = NULL;
201 NSDictionary *attributes = @{
202 (__bridge id)kSecAttrKeyClass : (__bridge id)kSecAttrKeyClassPrivate,
203 (__bridge id)kSecAttrKeyType : (__bridge id)kSecAttrKeyTypeEC,
206 accountPrivateKey = SecKeyCreateWithData((__bridge CFDataRef)credential, (__bridge CFDictionaryRef)attributes, &error);
207 if (accountPrivateKey == NULL) {
208 complete(false, (__bridge NSError *)error);
209 CFReleaseNull(error);
213 _accountPrivateKey = accountPrivateKey;
214 _accountPublicKey = SecKeyCopyPublicKey(_accountPrivateKey);
216 [self signApplicationIfNeeded];
218 complete(true, NULL);
221 - (void)myPeerInfo:(void(^)(NSData *application, NSError *error))complete
223 CFErrorRef error = NULL;
225 [self signApplicationIfNeeded];
227 NSData *application = CFBridgingRelease(SOSPeerInfoCopyEncodedData([self peerInfo], NULL, &error));
228 complete(application, (__bridge NSError *)error);
230 CFReleaseNull(error);
233 - (void)circleHash:(void (^)(NSString *, NSError *))complete
235 NSString *data = CFBridgingRelease(SOSCircleCopyHashString(_circle));
236 complete(data, NULL);
239 - (void)circleJoiningBlob:(NSData *)applicantData complete:(void (^)(NSData *blob, NSError *))complete
241 CFErrorRef error = NULL;
242 CFDataRef signature = NULL;
243 SOSCircleRef prunedCircle = SOSCircleCopyCircle(NULL, _circle, &error);
244 (void)SOSCirclePreGenerationSign(prunedCircle, _accountPublicKey, &error);
246 SOSGenCountRef gencount = SOSGenerationIncrementAndCreate(SOSCircleGetGeneration(prunedCircle));
247 if (gencount == NULL)
251 SOSPeerInfoRef applicant = SOSPeerInfoCreateFromData(NULL, &error, (__bridge CFDataRef)applicantData);
252 if (applicant == NULL)
255 signature = SOSCircleCopyNextGenSignatureWithPeerAdded(prunedCircle, applicant, _deviceKey, &error);
257 CFRelease(applicant);
261 NSData *pbblob = CFBridgingRelease(SOSPiggyBackBlobCopyEncodedData(gencount, _deviceKey, signature, &error));
263 CFReleaseNull(signature);
264 CFReleaseNull(gencount);
265 CFReleaseNull(prunedCircle);
267 complete(pbblob, NULL);
270 - (void)joinCircleWithBlob:(NSData *)blob version:(PiggyBackProtocolVersion)version complete:(void (^)(bool success, NSError *))complete
272 SOSGenCountRef gencount = NULL;
273 SecKeyRef pubKey = NULL;
274 CFDataRef signature = NULL;
275 CFErrorRef error = NULL;
276 bool setInitialSyncTimeoutToV0 = false;
278 if (!SOSPiggyBackBlobCreateFromData(&gencount, &pubKey, &signature, (__bridge CFDataRef)blob, kPiggyV1, &setInitialSyncTimeoutToV0, &error)) {
279 complete(true, (__bridge NSError *)error);
280 CFReleaseNull(error);
284 (void)SOSCircleAcceptPeerFromHSA2(_circle,
292 CFReleaseNull(gencount);
293 CFReleaseNull(pubKey);
294 CFReleaseNull(signature);
296 complete(true, (__bridge NSError *)error);
298 CFReleaseNull(error);
302 - (void)getWatchdogParameters:(void (^)(NSDictionary*, NSError*))complete
304 // intentionally left blank
305 // 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
309 - (void)setWatchdogParmeters:(NSDictionary*)parameters complete:(void (^)(NSError*))complete
311 // intentionally left blank
312 // 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
315 - (void)ghostBust:(SOSAccountGhostBustingOptions)options complete:(void (^)(bool, NSError *))complete {
316 complete(false, nil);
319 - (void)ghostBustPeriodic:(SOSAccountGhostBustingOptions)options complete: (void(^)(bool busted, NSError *error))complete{
320 complete(false, nil);
323 - (void)ghostBustTriggerTimed:(SOSAccountGhostBustingOptions)options complete: (void(^)(bool ghostBusted, NSError *error))complete {
324 complete(false, nil);
327 - (void) ghostBustInfo: (void(^)(NSData *json, NSError *error))complete {