2 #include "keychain/SecureObjectSync/SOSTransport.h"
3 #include "keychain/SecureObjectSync/SOSTransportKeyParameter.h"
4 #include "keychain/SecureObjectSync/SOSKVSKeys.h"
5 #include "keychain/securityd/SOSCloudCircleServer.h"
6 #include <utilities/SecCFWrappers.h>
7 #include "keychain/SecureObjectSync/SOSAccountPriv.h"
8 #include "keychain/SecureObjectSync/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
38 if ((self = [super init])) {
40 SOSRegisterTransportKeyParameter(self);
45 -(bool) SOSTransportKeyParameterKVSAppendKeyInterests:(CKKeyParameter*)transport ak:(CFMutableArrayRef)alwaysKeys firstUnLock:(CFMutableArrayRef)afterFirstUnlockKeys unlocked:(CFMutableArrayRef) unlockedKeys err:(CFErrorRef *)error
47 CFArrayAppendValue(alwaysKeys, kSOSKVSKeyParametersKey);
52 static bool SOSTransportKeyParameterKVSUpdateKVS(CFDictionaryRef changes, CFErrorRef *error){
53 CloudKeychainReplyBlock log_error = ^(CFDictionaryRef returnedValues __unused, CFErrorRef block_error) {
55 secerror("Error putting: %@", block_error);
59 SOSCloudKeychainPutObjectsInCloud(changes, dispatch_get_global_queue(SOS_TRANSPORT_PRIORITY, 0), log_error);
63 -(bool) SOSTransportKeyParameterPublishCloudParameters:(CKKeyParameter*) transport data:(CFDataRef)newParameters err:(CFErrorRef*) error
66 secnotice("circleOps", "Publishing Cloud Parameters");
68 secnotice("circleOps", "Tried to publish nil Cloud Parameters");
69 (void) SecRequirementError(newParameters != NULL, error, CFSTR("Tried to publish nil Cloud Parameters"));
73 bool waitForeverForSynchronization = true;
74 CFDictionaryRef changes = NULL;
75 CFDataRef timeData = NULL;
76 CFMutableStringRef timeDescription = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, CFSTR("["));
77 CFAbsoluteTime currentTimeAndDate = CFAbsoluteTimeGetCurrent();
79 withStringOfAbsoluteTime(currentTimeAndDate, ^(CFStringRef decription) {
80 CFStringAppend(timeDescription, decription);
82 CFStringAppend(timeDescription, CFSTR("]"));
84 timeData = CFStringCreateExternalRepresentation(NULL,timeDescription,
85 kCFStringEncodingUTF8, '?');
87 CFMutableDataRef timeAndKeyParametersMutable = CFDataCreateMutable(kCFAllocatorDefault, CFDataGetLength(timeData) + CFDataGetLength(newParameters));
88 CFDataAppend(timeAndKeyParametersMutable, timeData);
89 CFDataAppend(timeAndKeyParametersMutable, newParameters);
90 CFDataRef timeAndKeyParameters = CFDataCreateCopy(kCFAllocatorDefault, timeAndKeyParametersMutable);
92 CFStringRef ourPeerID = (__bridge CFStringRef)account.peerID;
94 if(ourPeerID != NULL){
95 CFStringRef keyParamKey = SOSLastKeyParametersPushedKeyCreateWithPeerID(ourPeerID);
97 changes = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
98 kSOSKVSKeyParametersKey, newParameters,
99 keyParamKey, timeAndKeyParameters,
101 CFReleaseNull(keyParamKey);
105 CFStringRef keyParamKeyWithAccount = SOSLastKeyParametersPushedKeyCreateWithAccountGestalt(account);
106 changes = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
107 kSOSKVSKeyParametersKey, newParameters,
108 keyParamKeyWithAccount, timeAndKeyParameters,
110 CFReleaseNull(keyParamKeyWithAccount);
112 bool success = SOSTransportKeyParameterKVSUpdateKVS(changes, error);
114 sync_the_last_data_to_kvs((__bridge CFTypeRef)(account), waitForeverForSynchronization);
115 CFReleaseNull(changes);
116 CFReleaseNull(timeData);
117 CFReleaseNull(timeAndKeyParameters);
118 CFReleaseNull(timeAndKeyParametersMutable);
119 CFReleaseNull(timeDescription);