2 #include <Security/SecureObjectSync/SOSTransport.h>
3 #include <Security/SecureObjectSync/SOSTransportKeyParameter.h>
4 #include <Security/SecureObjectSync/SOSKVSKeys.h>
5 #include <securityd/SOSCloudCircleServer.h>
6 #include <utilities/SecCFWrappers.h>
7 #include <Security/SecureObjectSync/SOSAccountPriv.h>
8 #include <SOSCircle/CKBridge/SOSCloudKeychainClient.h>
10 @implementation CKKeyParameter
12 @synthesize account = account;
14 -(bool) SOSTransportKeyParameterHandleKeyParameterChanges:(CKKeyParameter*) transport data:(CFDataRef) data err:(CFErrorRef) error
16 return SOSAccountHandleParametersChange(account, data, &error);
19 -(SOSAccount*) SOSTransportKeyParameterGetAccount:(CKKeyParameter*) transport
25 -(CFIndex) SOSTransportKeyParameterGetTransportType:(CKKeyParameter*) transport err:(CFErrorRef *)error
31 -(void) SOSTransportKeyParameterHandleNewAccount:(CKKeyParameter*) transport acct:(SOSAccount*) acct
33 SOSAccountSetToNew(acct);
36 -(id) initWithAccount:(SOSAccount*) acct
41 SOSRegisterTransportKeyParameter(self);
46 -(bool) SOSTransportKeyParameterKVSAppendKeyInterests:(CKKeyParameter*)transport ak:(CFMutableArrayRef)alwaysKeys firstUnLock:(CFMutableArrayRef)afterFirstUnlockKeys unlocked:(CFMutableArrayRef) unlockedKeys err:(CFErrorRef *)error
48 CFArrayAppendValue(alwaysKeys, kSOSKVSKeyParametersKey);
53 static bool SOSTransportKeyParameterKVSUpdateKVS(CFDictionaryRef changes, CFErrorRef *error){
54 CloudKeychainReplyBlock log_error = ^(CFDictionaryRef returnedValues __unused, CFErrorRef block_error) {
56 secerror("Error putting: %@", block_error);
60 SOSCloudKeychainPutObjectsInCloud(changes, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), log_error);
64 -(bool) SOSTransportKeyParameterPublishCloudParameters:(CKKeyParameter*) transport data:(CFDataRef)newParameters err:(CFErrorRef*) error
67 bool waitForeverForSynchronization = true;
68 CFDictionaryRef changes = NULL;
69 CFDataRef timeData = NULL;
70 CFMutableStringRef timeDescription = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, CFSTR("["));
71 CFAbsoluteTime currentTimeAndDate = CFAbsoluteTimeGetCurrent();
73 withStringOfAbsoluteTime(currentTimeAndDate, ^(CFStringRef decription) {
74 CFStringAppend(timeDescription, decription);
76 CFStringAppend(timeDescription, CFSTR("]"));
78 timeData = CFStringCreateExternalRepresentation(NULL,timeDescription,
79 kCFStringEncodingUTF8, '?');
81 CFMutableDataRef timeAndKeyParametersMutable = CFDataCreateMutable(kCFAllocatorDefault, CFDataGetLength(timeData) + CFDataGetLength(newParameters));
82 CFDataAppend(timeAndKeyParametersMutable, timeData);
83 CFDataAppend(timeAndKeyParametersMutable, newParameters);
84 CFDataRef timeAndKeyParameters = CFDataCreateCopy(kCFAllocatorDefault, timeAndKeyParametersMutable);
86 CFStringRef ourPeerID = (__bridge CFStringRef)account.peerID;
88 if(ourPeerID != NULL){
89 CFStringRef keyParamKey = SOSLastKeyParametersPushedKeyCreateWithPeerID(ourPeerID);
91 changes = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
92 kSOSKVSKeyParametersKey, newParameters,
93 keyParamKey, timeAndKeyParameters,
95 CFReleaseNull(keyParamKey);
99 CFStringRef keyParamKeyWithAccount = SOSLastKeyParametersPushedKeyCreateWithAccountGestalt(account);
100 changes = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
101 kSOSKVSKeyParametersKey, newParameters,
102 keyParamKeyWithAccount, timeAndKeyParameters,
104 CFReleaseNull(keyParamKeyWithAccount);
106 bool success = SOSTransportKeyParameterKVSUpdateKVS(changes, error);
108 sync_the_last_data_to_kvs((__bridge CFTypeRef)(account), waitForeverForSynchronization);
109 CFReleaseNull(changes);
110 CFReleaseNull(timeData);
111 CFReleaseNull(timeAndKeyParameters);
112 CFReleaseNull(timeAndKeyParametersMutable);
113 CFReleaseNull(timeDescription);