]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/SecureObjectSync/SOSTransportKeyParameter.m
Security-58286.41.2.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / SecureObjectSync / SOSTransportKeyParameter.m
1
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>
9
10 @implementation CKKeyParameter
11
12 @synthesize account = account;
13
14 -(bool) SOSTransportKeyParameterHandleKeyParameterChanges:(CKKeyParameter*) transport data:(CFDataRef) data err:(CFErrorRef) error
15 {
16 return SOSAccountHandleParametersChange(account, data, &error);
17 }
18
19 -(SOSAccount*) SOSTransportKeyParameterGetAccount:(CKKeyParameter*) transport
20 {
21 return account;
22 }
23
24
25 -(CFIndex) SOSTransportKeyParameterGetTransportType:(CKKeyParameter*) transport err:(CFErrorRef *)error
26 {
27 return kKVS;
28 }
29
30
31 -(void) SOSTransportKeyParameterHandleNewAccount:(CKKeyParameter*) transport acct:(SOSAccount*) acct
32 {
33 SOSAccountSetToNew(acct);
34 }
35
36 -(id) initWithAccount:(SOSAccount*) acct
37 {
38 self = [super init];
39 if(self){
40 self.account = acct;
41 SOSRegisterTransportKeyParameter(self);
42 }
43 return self;
44 }
45
46 -(bool) SOSTransportKeyParameterKVSAppendKeyInterests:(CKKeyParameter*)transport ak:(CFMutableArrayRef)alwaysKeys firstUnLock:(CFMutableArrayRef)afterFirstUnlockKeys unlocked:(CFMutableArrayRef) unlockedKeys err:(CFErrorRef *)error
47 {
48 CFArrayAppendValue(alwaysKeys, kSOSKVSKeyParametersKey);
49
50 return true;
51 }
52
53 static bool SOSTransportKeyParameterKVSUpdateKVS(CFDictionaryRef changes, CFErrorRef *error){
54 CloudKeychainReplyBlock log_error = ^(CFDictionaryRef returnedValues __unused, CFErrorRef block_error) {
55 if (block_error) {
56 secerror("Error putting: %@", block_error);
57 }
58 };
59
60 SOSCloudKeychainPutObjectsInCloud(changes, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), log_error);
61 return true;
62 }
63
64 -(bool) SOSTransportKeyParameterPublishCloudParameters:(CKKeyParameter*) transport data:(CFDataRef)newParameters err:(CFErrorRef*) error
65 {
66
67 bool waitForeverForSynchronization = true;
68 CFDictionaryRef changes = NULL;
69 CFDataRef timeData = NULL;
70 CFMutableStringRef timeDescription = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, CFSTR("["));
71 CFAbsoluteTime currentTimeAndDate = CFAbsoluteTimeGetCurrent();
72
73 withStringOfAbsoluteTime(currentTimeAndDate, ^(CFStringRef decription) {
74 CFStringAppend(timeDescription, decription);
75 });
76 CFStringAppend(timeDescription, CFSTR("]"));
77
78 timeData = CFStringCreateExternalRepresentation(NULL,timeDescription,
79 kCFStringEncodingUTF8, '?');
80
81 CFMutableDataRef timeAndKeyParametersMutable = CFDataCreateMutable(kCFAllocatorDefault, CFDataGetLength(timeData) + CFDataGetLength(newParameters));
82 CFDataAppend(timeAndKeyParametersMutable, timeData);
83 CFDataAppend(timeAndKeyParametersMutable, newParameters);
84 CFDataRef timeAndKeyParameters = CFDataCreateCopy(kCFAllocatorDefault, timeAndKeyParametersMutable);
85
86 CFStringRef ourPeerID = (__bridge CFStringRef)account.peerID;
87
88 if(ourPeerID != NULL){
89 CFStringRef keyParamKey = SOSLastKeyParametersPushedKeyCreateWithPeerID(ourPeerID);
90
91 changes = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
92 kSOSKVSKeyParametersKey, newParameters,
93 keyParamKey, timeAndKeyParameters,
94 NULL);
95 CFReleaseNull(keyParamKey);
96 }
97 else
98 {
99 CFStringRef keyParamKeyWithAccount = SOSLastKeyParametersPushedKeyCreateWithAccountGestalt(account);
100 changes = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
101 kSOSKVSKeyParametersKey, newParameters,
102 keyParamKeyWithAccount, timeAndKeyParameters,
103 NULL);
104 CFReleaseNull(keyParamKeyWithAccount);
105 }
106 bool success = SOSTransportKeyParameterKVSUpdateKVS(changes, error);
107
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);
114
115 return success;
116 }
117
118 @end
119