]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/SOSTransportKeyParameter.m
Security-59306.41.2.tar.gz
[apple/security.git] / keychain / SecureObjectSync / SOSTransportKeyParameter.m
1
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"
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 if(newParameters) {
67 secnotice("circleOps", "Publishing Cloud Parameters");
68 } else {
69 secnotice("circleOps", "Tried to publish nil Cloud Parameters");
70 (void) SecRequirementError(newParameters != NULL, error, CFSTR("Tried to publish nil Cloud Parameters"));
71 return false;
72 }
73
74 bool waitForeverForSynchronization = true;
75 CFDictionaryRef changes = NULL;
76 CFDataRef timeData = NULL;
77 CFMutableStringRef timeDescription = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, CFSTR("["));
78 CFAbsoluteTime currentTimeAndDate = CFAbsoluteTimeGetCurrent();
79
80 withStringOfAbsoluteTime(currentTimeAndDate, ^(CFStringRef decription) {
81 CFStringAppend(timeDescription, decription);
82 });
83 CFStringAppend(timeDescription, CFSTR("]"));
84
85 timeData = CFStringCreateExternalRepresentation(NULL,timeDescription,
86 kCFStringEncodingUTF8, '?');
87
88 CFMutableDataRef timeAndKeyParametersMutable = CFDataCreateMutable(kCFAllocatorDefault, CFDataGetLength(timeData) + CFDataGetLength(newParameters));
89 CFDataAppend(timeAndKeyParametersMutable, timeData);
90 CFDataAppend(timeAndKeyParametersMutable, newParameters);
91 CFDataRef timeAndKeyParameters = CFDataCreateCopy(kCFAllocatorDefault, timeAndKeyParametersMutable);
92
93 CFStringRef ourPeerID = (__bridge CFStringRef)account.peerID;
94
95 if(ourPeerID != NULL){
96 CFStringRef keyParamKey = SOSLastKeyParametersPushedKeyCreateWithPeerID(ourPeerID);
97
98 changes = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
99 kSOSKVSKeyParametersKey, newParameters,
100 keyParamKey, timeAndKeyParameters,
101 NULL);
102 CFReleaseNull(keyParamKey);
103 }
104 else
105 {
106 CFStringRef keyParamKeyWithAccount = SOSLastKeyParametersPushedKeyCreateWithAccountGestalt(account);
107 changes = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
108 kSOSKVSKeyParametersKey, newParameters,
109 keyParamKeyWithAccount, timeAndKeyParameters,
110 NULL);
111 CFReleaseNull(keyParamKeyWithAccount);
112 }
113 bool success = SOSTransportKeyParameterKVSUpdateKVS(changes, error);
114
115 sync_the_last_data_to_kvs((__bridge CFTypeRef)(account), waitForeverForSynchronization);
116 CFReleaseNull(changes);
117 CFReleaseNull(timeData);
118 CFReleaseNull(timeAndKeyParameters);
119 CFReleaseNull(timeAndKeyParametersMutable);
120 CFReleaseNull(timeDescription);
121
122 return success;
123 }
124
125 @end
126