2 // KCJoiningSessionTest.m
7 #import <XCTest/XCTest.h>
9 #import <Foundation/Foundation.h>
11 #import <KeychainCircle/KCJoiningSession.h>
12 #import <KeychainCircle/KCError.h>
13 #import <KeychainCircle/NSError+KCCreationHelpers.h>
14 #import <KeychainCircle/KCAESGCMDuplexSession.h>
16 #include <Security/SecBase.h>
17 #include <Security/SecureObjectSync/SOSFullPeerInfo.h>
18 #include <Security/SecureObjectSync/SOSPeerInfoInternal.h>
20 #include <CommonCrypto/CommonRandomSPI.h>
23 __unused static SOSFullPeerInfoRef SOSNSFullPeerInfoCreate(NSDictionary* gestalt,
24 NSData* backupKey, SecKeyRef signingKey,
27 CFErrorRef errorRef = NULL;
29 SOSFullPeerInfoRef result = SOSFullPeerInfoCreate(NULL, (__bridge CFDictionaryRef) gestalt, (__bridge CFDataRef) backupKey, signingKey, &errorRef);
31 if (errorRef && error) {
32 *error = (__bridge_transfer NSError*) errorRef;
39 static SecKeyRef GenerateFullECKey_internal(int keySize, NSError** error)
41 SecKeyRef full_key = NULL;
43 NSDictionary* keygen_parameters = @{ (__bridge NSString*)kSecAttrKeyType:(__bridge NSString*) kSecAttrKeyTypeEC,
44 (__bridge NSString*)kSecAttrKeySizeInBits: [NSNumber numberWithInt: keySize] };
47 (void) OSStatusError(SecKeyGeneratePair((__bridge CFDictionaryRef)keygen_parameters, NULL, &full_key), error, @"Generate Key failed");
52 static SecKeyRef GenerateFullECKey(int keySize, NSError** error) {
53 return GenerateFullECKey_internal(keySize, error);
57 __unused static SOSFullPeerInfoRef SOSCreateFullPeerInfoFromName(NSString* name, SecKeyRef* outSigningKey, NSError** error)
59 if (outSigningKey == NULL)
62 *outSigningKey = GenerateFullECKey(256, error);
63 if (*outSigningKey == NULL)
66 return SOSNSFullPeerInfoCreate(@{(__bridge NSString*)kPIUserDefinedDeviceNameKey:name}, nil, *outSigningKey, error);
70 @interface KCJoiningRequestTestDelegate : NSObject <KCJoiningRequestSecretDelegate, KCJoiningRequestCircleDelegate>
71 @property (readwrite) NSString* sharedSecret;
73 @property (readonly) NSString* accountCode;
74 @property (readonly) NSData* circleJoinData;
75 @property (readwrite) SOSPeerInfoRef peerInfo;
77 @property (readwrite) NSString* incorrectSecret;
78 @property (readwrite) int incorrectTries;
81 + (id) requestDelegateWithSecret:(NSString*) secret;
82 - (id) init NS_UNAVAILABLE;
83 - (id) initWithSecret: (NSString*) secret
84 incorrectSecret: (NSString*) wrongSecret
85 incorrectTries: (int) retries NS_DESIGNATED_INITIALIZER;
87 - (NSString*) verificationFailed: (bool) codeChanged;
88 - (SOSPeerInfoRef) copyPeerInfoError: (NSError**) error;
89 - (bool) processCircleJoinData: (NSData*) circleJoinData error: (NSError**)error ;
90 - (bool) processAccountCode: (NSString*) accountCode error: (NSError**)error;
94 @implementation KCJoiningRequestTestDelegate
96 + (id) requestDelegateWithSecret:(NSString*) secret {
97 return [[KCJoiningRequestTestDelegate alloc] initWithSecret:secret
102 + (id) requestDelegateWithSecret:(NSString*) secret
103 incorrectSecret:(NSString*) wrongSecret
104 incorrectTries:(int) retries {
105 return [[KCJoiningRequestTestDelegate alloc] initWithSecret:secret
106 incorrectSecret:wrongSecret
107 incorrectTries:retries];
111 - (id) initWithSecret: (NSString*) secret
112 incorrectSecret: (NSString*) incorrectSecret
113 incorrectTries: (int) retries {
116 SecKeyRef signingKey = GenerateFullECKey(256, NULL);
118 self.peerInfo = SOSPeerInfoCreate(NULL, (__bridge CFDictionaryRef) @{(__bridge NSString*)kPIUserDefinedDeviceNameKey:@"Fakey"}, NULL, signingKey, NULL);
120 if (self.peerInfo == NULL)
123 self.sharedSecret = secret;
124 self.incorrectSecret = incorrectSecret;
125 self.incorrectTries = retries;
130 - (NSString*) nextSecret {
131 if (self.incorrectTries > 0) {
132 self.incorrectTries -= 1;
133 return self.incorrectSecret;
135 return self.sharedSecret;
138 - (NSString*) secret {
139 return [self nextSecret];
142 - (NSString*) verificationFailed: (bool) codeChanged {
143 return [self nextSecret];
146 - (SOSPeerInfoRef) copyPeerInfoError: (NSError**) error {
147 return self.peerInfo;
150 - (bool) processCircleJoinData: (NSData*) circleJoinData error: (NSError**)error {
151 self->_circleJoinData = circleJoinData;
155 - (bool) processAccountCode: (NSString*) accountCode error: (NSError**)error {
156 self->_accountCode = accountCode;
162 @interface KCJoiningAcceptTestDelegate : NSObject <KCJoiningAcceptSecretDelegate, KCJoiningAcceptCircleDelegate>
163 @property (readonly) NSArray<NSString*>* secrets;
164 @property (readwrite) NSUInteger currentSecret;
165 @property (readwrite) int retriesLeft;
166 @property (readwrite) int retriesPerSecret;
168 @property (readonly) NSString* codeToUse;
169 @property (readonly) NSData* circleJoinData;
170 @property (readonly) SOSPeerInfoRef peerInfo;
172 + (id) acceptDelegateWithSecret: (NSString*) secret code: (NSString*) code;
173 + (id) acceptDelegateWithSecrets: (NSArray<NSString*>*) secrets retries: (int) retries code: (NSString*) code;
174 - (id) initWithSecrets: (NSArray<NSString*>*) secrets retries: (int) retries code: (NSString*) code NS_DESIGNATED_INITIALIZER;
177 - (NSString*) secret;
178 - (NSString*) accountCode;
180 - (KCRetryOrNot) verificationFailed: (NSError**) error;
181 - (NSData*) circleJoinDataFor: (SOSPeerInfoRef) peer
182 error: (NSError**) error;
184 - (id) init NS_UNAVAILABLE;
188 @implementation KCJoiningAcceptTestDelegate
190 + (id) acceptDelegateWithSecrets: (NSArray<NSString*>*) secrets retries: (int) retries code: (NSString*) code {
191 return [[KCJoiningAcceptTestDelegate alloc] initWithSecrets:secrets retries:retries code:code];
195 + (id) acceptDelegateWithSecret: (NSString*) secret code: (NSString*) code {
196 return [[KCJoiningAcceptTestDelegate alloc] initWithSecret:secret code:code];
199 - (id) initWithSecret: (NSString*) secret code: (NSString*) code {
200 return [self initWithSecrets:@[secret] retries:3 code:code];
203 - (id) initWithSecrets: (NSArray<NSString*>*) secrets retries: (int) retries code: (NSString*) code {
206 self->_secrets = secrets;
207 self.currentSecret = 0;
208 self->_retriesPerSecret = retries;
209 self->_retriesLeft = self.retriesPerSecret;
211 self->_codeToUse = code;
213 uint8_t joinDataBuffer[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
214 self->_circleJoinData = [NSData dataWithBytes: joinDataBuffer length: sizeof(joinDataBuffer) ];
219 - (KCRetryOrNot) advanceSecret {
220 if (self.retriesLeft == 0) {
221 self.currentSecret += 1;
222 if (self.currentSecret >= [self.secrets count]) {
223 self.currentSecret = [self.secrets count] - 1;
225 self.retriesLeft = self.retriesPerSecret;
226 return kKCRetryWithNewChallenge;
228 self.retriesLeft -= 1;
229 return kKCRetryWithSameChallenge;
233 - (NSString*) secret {
234 return self.secrets[self.currentSecret];
236 - (NSString*) accountCode {
237 return self.codeToUse;
240 - (KCRetryOrNot) verificationFailed: (NSError**) error {
241 return [self advanceSecret];
244 - (NSData*) circleJoinDataFor: (SOSPeerInfoRef) peer
245 error: (NSError**) error {
246 uint8_t joinDataBuffer[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
248 self->_peerInfo = peer;
249 return [NSData dataWithBytes: joinDataBuffer length: sizeof(joinDataBuffer) ];
256 @interface KCJoiningSessionTest : XCTestCase
260 @implementation KCJoiningSessionTest
264 // Put setup code here. This method is called before the invocation of each test method in the class.
268 // Put teardown code here. This method is called after the invocation of each test method in the class.
272 - (void)testJoiningSession {
273 NSError* error = nil;
275 NSString* secret = @"123456";
276 NSString* code = @"987654";
278 uint64_t dsid = 0x1234567887654321;
280 KCJoiningRequestTestDelegate* requestDelegate = [KCJoiningRequestTestDelegate requestDelegateWithSecret: secret];
281 KCJoiningRequestSecretSession *requestSession = [[KCJoiningRequestSecretSession alloc] initWithSecretDelegate:requestDelegate
283 rng:ccDRBGGetRngState()
286 NSData* initialMessage = [requestSession initialMessage: &error];
288 XCTAssertNotNil(initialMessage, @"No initial message");
289 XCTAssertNil(error, @"Got error %@", error);
291 KCJoiningAcceptTestDelegate* acceptDelegate = [KCJoiningAcceptTestDelegate acceptDelegateWithSecret:secret code:code];
292 KCJoiningAcceptSession* acceptSession = [[KCJoiningAcceptSession alloc] initWithSecretDelegate:acceptDelegate
293 circleDelegate:acceptDelegate
295 rng:ccDRBGGetRngState()
299 NSData* challenge = [acceptSession processMessage: initialMessage error: &error];
301 XCTAssertNotNil(challenge, @"No initial message");
302 XCTAssertNil(error, @"Got error %@", error);
305 NSData* response = [requestSession processMessage: challenge error: &error];
307 XCTAssertNotNil(response, @"No response message");
308 XCTAssertNil(error, @"Got error %@", error);
311 NSData* verification = [acceptSession processMessage: response error: &error];
313 XCTAssertNotNil(verification, @"No verification message");
314 XCTAssertNil(error, @"Got error %@", error);
317 NSData* doneMessage = [requestSession processMessage: verification error: &error];
319 XCTAssertNotNil(doneMessage, @"No response message");
320 XCTAssertNil(error, @"Got error %@", error);
322 XCTAssertTrue([requestSession isDone], @"SecretSession done");
323 XCTAssertFalse([acceptSession isDone], @"Unexpected accept session done");
325 KCAESGCMDuplexSession* aesSession = [requestSession session];
326 requestSession = nil;
328 KCJoiningRequestCircleSession* requestSecretSession = [KCJoiningRequestCircleSession sessionWithCircleDelegate:requestDelegate session:aesSession error:&error];
330 XCTAssertNotNil(requestSecretSession, @"No request secret session");
331 XCTAssertNil(error, @"Got error %@", error);
334 NSData* peerInfoMessage = [requestSecretSession initialMessage: &error];
336 XCTAssertNotNil(peerInfoMessage, @"No peerInfo message");
337 XCTAssertNil(error, @"Got error %@", error);
339 XCTAssertEqualObjects(requestDelegate.accountCode, acceptDelegate.codeToUse, @"Code made it");
342 NSData* blobMessage = [acceptSession processMessage:peerInfoMessage error: &error];
344 XCTAssertNotNil(blobMessage, @"No blob message");
345 XCTAssertNil(error, @"Got error %@", error);
347 // We have different peer_info types due to wierd linking of our tests.
348 // Compare the der representations:
349 NSData* rp_der = requestDelegate.peerInfo != nil ? (__bridge_transfer NSData*) SOSPeerInfoCopyEncodedData(requestDelegate.peerInfo, NULL, NULL) : nil;
350 NSData* ap_der = acceptDelegate.peerInfo != nil ? (__bridge_transfer NSData*) SOSPeerInfoCopyEncodedData(acceptDelegate.peerInfo, NULL, NULL) : nil;
352 XCTAssertEqualObjects(rp_der, ap_der, @"Peer infos match");
355 NSData* nothing = [requestSecretSession processMessage:blobMessage error: &error];
357 XCTAssertEqualObjects(requestDelegate.circleJoinData, acceptDelegate.circleJoinData);
359 XCTAssertNotNil(nothing, @"No initial message");
360 XCTAssertNil(error, @"Got error %@", error);
362 XCTAssertTrue([requestSecretSession isDone], @"requesor done");
363 XCTAssertTrue([acceptSession isDone], @"acceptor done");
367 - (void)testJoiningSessionRetry {
368 NSError* error = nil;
370 NSString* secret = @"123456";
371 NSString* code = @"987654";
373 uint64_t dsid = 0x1234567887654321;
375 KCJoiningRequestTestDelegate* requestDelegate = [KCJoiningRequestTestDelegate requestDelegateWithSecret: secret incorrectSecret:@"777888" incorrectTries:3];
376 KCJoiningRequestSecretSession *requestSession = [[KCJoiningRequestSecretSession alloc] initWithSecretDelegate:requestDelegate
378 rng:ccDRBGGetRngState()
381 NSData* initialMessage = [requestSession initialMessage: &error];
383 XCTAssertNotNil(initialMessage, @"No initial message");
384 XCTAssertNil(error, @"Got error %@", error);
386 KCJoiningAcceptTestDelegate* acceptDelegate = [KCJoiningAcceptTestDelegate acceptDelegateWithSecret:secret code:code];
387 KCJoiningAcceptSession* acceptSession = [[KCJoiningAcceptSession alloc] initWithSecretDelegate:acceptDelegate
388 circleDelegate:acceptDelegate
390 rng:ccDRBGGetRngState()
394 NSData* challenge = [acceptSession processMessage: initialMessage error: &error];
396 XCTAssertNotNil(challenge, @"No initial message");
397 XCTAssertNil(error, @"Got error %@", error);
399 NSData* response = nil;
400 NSData* verification = nil;
402 NSData* nextChallenge = challenge;
403 for (int tries = 0; tries < 4; ++tries) {
405 response = [requestSession processMessage: nextChallenge error: &error];
407 XCTAssertNotNil(response, @"No response message");
408 XCTAssertNil(error, @"Got error %@", error);
410 XCTAssertNotEqualObjects(requestDelegate.accountCode, acceptDelegate.codeToUse, @"Code should not make it");
413 verification = [acceptSession processMessage: response error: &error];
415 XCTAssertNotNil(verification, @"No verification message");
416 XCTAssertNil(error, @"Got error %@", error);
418 nextChallenge = verification;
422 NSData* doneMessage = [requestSession processMessage: verification error: &error];
424 XCTAssertNotNil(doneMessage, @"No response message");
425 XCTAssertNil(error, @"Got error %@", error);
427 XCTAssertTrue([requestSession isDone], @"SecretSession done");
428 XCTAssertFalse([acceptSession isDone], @"Unexpected accept session done");
430 KCAESGCMDuplexSession* aesSession = [requestSession session];
431 requestSession = nil;
434 KCJoiningRequestCircleSession* requestSecretSession = [KCJoiningRequestCircleSession sessionWithCircleDelegate:requestDelegate session:aesSession error:&error];
436 XCTAssertNotNil(requestSecretSession, @"No request secret session");
437 XCTAssertNil(error, @"Got error %@", error);
440 NSData* peerInfoMessage = [requestSecretSession initialMessage: &error];
442 XCTAssertNotNil(peerInfoMessage, @"No peerInfo message");
443 XCTAssertNil(error, @"Got error %@", error);
445 XCTAssertEqualObjects(requestDelegate.accountCode, acceptDelegate.codeToUse, @"Code made it");
448 NSData* blobMessage = [acceptSession processMessage:peerInfoMessage error: &error];
450 XCTAssertNotNil(blobMessage, @"No blob message");
451 XCTAssertNil(error, @"Got error %@", error);
453 // We have different peer_info types due to wierd linking of our tests.
454 // Compare the der representations:
455 NSData* rp_der = requestDelegate.peerInfo != nil ? (__bridge_transfer NSData*) SOSPeerInfoCopyEncodedData(requestDelegate.peerInfo, NULL, NULL) : nil;
456 NSData* ap_der = acceptDelegate.peerInfo != nil ? (__bridge_transfer NSData*) SOSPeerInfoCopyEncodedData(acceptDelegate.peerInfo, NULL, NULL) : nil;
458 XCTAssertEqualObjects(rp_der, ap_der, @"Peer infos match");
461 NSData* nothing = [requestSecretSession processMessage:blobMessage error: &error];
463 XCTAssertEqualObjects(requestDelegate.circleJoinData, acceptDelegate.circleJoinData);
465 XCTAssertNotNil(nothing, @"No initial message");
466 XCTAssertNil(error, @"Got error %@", error);
468 XCTAssertTrue([requestSecretSession isDone], @"requesor done");
469 XCTAssertTrue([acceptSession isDone], @"acceptor done");
473 - (void)testJoiningSessionCodeChange {
474 NSError* error = nil;
476 NSString* secret = @"123456";
477 NSString* code = @"987654";
479 uint64_t dsid = 0x1234567887654321;
481 KCJoiningRequestTestDelegate* requestDelegate = [KCJoiningRequestTestDelegate requestDelegateWithSecret: secret];
482 KCJoiningRequestSecretSession *requestSession = [[KCJoiningRequestSecretSession alloc] initWithSecretDelegate:requestDelegate
484 rng:ccDRBGGetRngState()
487 NSData* initialMessage = [requestSession initialMessage: &error];
489 XCTAssertNotNil(initialMessage, @"No initial message");
490 XCTAssertNil(error, @"Got error %@", error);
492 KCJoiningAcceptTestDelegate* acceptDelegate = [KCJoiningAcceptTestDelegate acceptDelegateWithSecrets:@[@"222222", @"3333333", secret] retries:1 code:code];
493 KCJoiningAcceptSession* acceptSession = [[KCJoiningAcceptSession alloc] initWithSecretDelegate:acceptDelegate
494 circleDelegate:acceptDelegate
496 rng:ccDRBGGetRngState()
500 NSData* challenge = [acceptSession processMessage: initialMessage error: &error];
502 XCTAssertNotNil(challenge, @"No initial message");
503 XCTAssertNil(error, @"Got error %@", error);
505 NSData* response = nil;
506 NSData* verification = nil;
508 NSData* nextChallenge = challenge;
509 for (int tries = 0; tries < 5; ++tries) {
511 response = [requestSession processMessage: nextChallenge error: &error];
513 XCTAssertNotNil(response, @"No response message");
514 XCTAssertNil(error, @"Got error %@", error);
516 XCTAssertNotEqualObjects(requestDelegate.accountCode, acceptDelegate.codeToUse, @"Code should not make it");
519 verification = [acceptSession processMessage: response error: &error];
521 XCTAssertNotNil(verification, @"No verification message");
522 XCTAssertNil(error, @"Got error %@", error);
524 nextChallenge = verification;
528 NSData* doneMessage = [requestSession processMessage: verification error: &error];
530 XCTAssertNotNil(doneMessage, @"No response message");
531 XCTAssertNil(error, @"Got error %@", error);
533 XCTAssertTrue([requestSession isDone], @"SecretSession done");
534 XCTAssertFalse([acceptSession isDone], @"Unexpected accept session done");
536 KCAESGCMDuplexSession* aesSession = [requestSession session];
537 requestSession = nil;
540 KCJoiningRequestCircleSession* requestSecretSession = [KCJoiningRequestCircleSession sessionWithCircleDelegate:requestDelegate session:aesSession error:&error];
542 XCTAssertNotNil(requestSecretSession, @"No request secret session");
543 XCTAssertNil(error, @"Got error %@", error);
546 NSData* peerInfoMessage = [requestSecretSession initialMessage: &error];
548 XCTAssertNotNil(peerInfoMessage, @"No peerInfo message");
549 XCTAssertNil(error, @"Got error %@", error);
551 XCTAssertEqualObjects(requestDelegate.accountCode, acceptDelegate.codeToUse, @"Code made it");
554 NSData* blobMessage = [acceptSession processMessage:peerInfoMessage error: &error];
556 XCTAssertNotNil(blobMessage, @"No blob message");
557 XCTAssertNil(error, @"Got error %@", error);
559 // We have different peer_info types due to wierd linking of our tests.
560 // Compare the der representations:
561 NSData* rp_der = requestDelegate.peerInfo != nil ? (__bridge_transfer NSData*) SOSPeerInfoCopyEncodedData(requestDelegate.peerInfo, NULL, NULL) : nil;
562 NSData* ap_der = acceptDelegate.peerInfo != nil ? (__bridge_transfer NSData*) SOSPeerInfoCopyEncodedData(acceptDelegate.peerInfo, NULL, NULL) : nil;
564 XCTAssertEqualObjects(rp_der, ap_der, @"Peer infos match");
567 NSData* nothing = [requestSecretSession processMessage:blobMessage error: &error];
569 XCTAssertEqualObjects(requestDelegate.circleJoinData, acceptDelegate.circleJoinData);
571 XCTAssertNotNil(nothing, @"No initial message");
572 XCTAssertNil(error, @"Got error %@", error);
574 XCTAssertTrue([requestSecretSession isDone], @"requesor done");
575 XCTAssertTrue([acceptSession isDone], @"acceptor done");