2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include "keychain/SecureObjectSync/SOSKVSKeys.h"
25 #include <utilities/SecCFWrappers.h>
26 #include "keychain/SecureObjectSync/SOSAccountPriv.h"
27 #include <CoreFoundation/CFDate.h>
29 void AppendCircleKeyName(CFMutableArrayRef array, CFStringRef name) {
30 CFStringRef circle_key = SOSCircleKeyCreateWithName(name, NULL);
31 CFArrayAppendValue(array, circle_key);
32 CFReleaseNull(circle_key);
38 // TODO: Handle '|' and "¬" in other strings.
40 const CFStringRef kSOSKVSKeyParametersKey = CFSTR(">KeyParameters");
41 const CFStringRef kSOSKVSInitialSyncKey = CFSTR("^InitialSync");
42 const CFStringRef kSOSKVSAccountChangedKey = CFSTR("^AccountChanged");
43 const CFStringRef kSOSKVSRequiredKey = CFSTR("^Required");
44 const CFStringRef kSOSKVSOfficialDSIDKey = CFSTR("^OfficialDSID");
45 const CFStringRef kSOSKVSLastCleanupTimestampKey = CFSTR("tLastKVSKeyCleanup");
46 const CFStringRef kSOSKVSOTRConfigVersion = CFSTR("OTRConfigVersion");
47 const CFStringRef kSOSKVSWroteLastKeyParams = CFSTR("WroteLastKeyParams");
48 const CFStringRef kSOSKVSDebugScope = CFSTR("^DebugScope");
50 const CFStringRef sRingPrefix = CFSTR("~");
51 const CFStringRef sDebugInfoPrefix = CFSTR("dbg-");
53 const CFStringRef sWarningPrefix = CFSTR("!");
54 const CFStringRef sCirclePrefix = CFSTR("o");
55 const CFStringRef sRetirementPrefix = CFSTR("-");
56 const CFStringRef sLastKeyParametersPushedPrefix = CFSTR("k");
57 const CFStringRef sCircleSeparator = CFSTR("|");
58 const CFStringRef sFromToSeparator = CFSTR(":");
60 static CFStringRef copyStringEndingIn(CFMutableStringRef in, CFStringRef token) {
61 if(token == NULL) return CFStringCreateCopy(NULL, in);
62 CFRange tokenAt = CFStringFind(in, token, 0);
63 if(tokenAt.location == kCFNotFound) return NULL;
64 CFStringRef retval = CFStringCreateWithSubstring(NULL, in, CFRangeMake(0, tokenAt.location));
65 CFStringDelete(in, CFRangeMake(0, tokenAt.location+1));
69 SOSKVSKeyType SOSKVSKeyGetKeyType(CFStringRef key) {
70 SOSKVSKeyType retval = kUnknownKey;
72 if(CFStringHasPrefix(key, sCirclePrefix)) retval = kCircleKey;
73 else if (CFStringHasPrefix(key, sRingPrefix)) retval = kRingKey;
74 else if(CFStringHasPrefix(key, sRetirementPrefix)) retval = kRetirementKey;
75 else if(CFStringHasPrefix(key, kSOSKVSKeyParametersKey)) retval = kParametersKey;
76 else if(CFStringHasPrefix(key, kSOSKVSInitialSyncKey)) retval = kInitialSyncKey;
77 else if(CFStringHasPrefix(key, kSOSKVSAccountChangedKey)) retval = kAccountChangedKey;
78 else if(CFStringHasPrefix(key, sDebugInfoPrefix)) retval = kDebugInfoKey;
79 else if(CFStringHasPrefix(key, sLastKeyParametersPushedPrefix)) retval = kLastKeyParameterKey;
80 else retval = kMessageKey;
85 bool SOSKVSKeyParse(SOSKVSKeyType keyType, CFStringRef key, CFStringRef *circle, CFStringRef *peerInfo, CFStringRef *ring, CFStringRef *backupName, CFStringRef *from, CFStringRef *to) {
91 CFRange fromRange = CFRangeMake(1, CFStringGetLength(key)-1);
92 *circle = CFStringCreateWithSubstring(NULL, key, fromRange);
96 CFStringRef mCircle = NULL;
97 CFStringRef mFrom = NULL;
98 CFStringRef mTo = NULL;
99 CFMutableStringRef keycopy = CFStringCreateMutableCopy(NULL, 128, key);
101 if( ((mCircle = copyStringEndingIn(keycopy, sCircleSeparator)) != NULL) &&
102 ((mFrom = copyStringEndingIn(keycopy, sFromToSeparator)) != NULL) &&
103 (CFStringGetLength(mFrom) > 0) ) {
104 mTo = copyStringEndingIn(keycopy, NULL);
105 if (circle && mCircle) *circle = CFStringCreateCopy(NULL, mCircle);
106 if (from && mFrom) *from = CFStringCreateCopy(NULL, mFrom);
107 if (to && mTo) *to = CFStringCreateCopy(NULL, mTo);
111 CFReleaseNull(mCircle);
112 CFReleaseNull(mFrom);
114 CFReleaseNull(keycopy);
117 case kRetirementKey: {
118 CFStringRef mCircle = NULL;
119 CFStringRef mPeer = NULL;
120 CFMutableStringRef keycopy = CFStringCreateMutableCopy(NULL, 128, key);
121 CFStringDelete(keycopy, CFRangeMake(0, 1));
122 if( ((mCircle = copyStringEndingIn(keycopy, sCircleSeparator)) != NULL) &&
123 ((mPeer = copyStringEndingIn(keycopy, NULL)) != NULL)) {
124 if (circle) *circle = CFStringCreateCopy(NULL, mCircle);
125 if (from) *from = CFStringCreateCopy(NULL, mPeer);
129 CFReleaseNull(mCircle);
130 CFReleaseNull(mPeer);
131 CFReleaseNull(keycopy);
136 CFRange fromRange = CFRangeMake(1, CFStringGetLength(key)-1);
137 *ring = CFStringCreateWithSubstring(NULL, key, fromRange);
141 /* piggybacking on peerinfo */
143 CFRange dbgRange = CFRangeMake(CFStringGetLength(sDebugInfoPrefix),
144 CFStringGetLength(key)-CFStringGetLength(sDebugInfoPrefix));
145 *peerInfo = CFStringCreateWithSubstring(NULL, key, dbgRange);
148 case kAccountChangedKey:
150 case kInitialSyncKey:
153 case kLastKeyParameterKey:
155 CFStringRef mPrefix = NULL;
156 CFStringRef mFrom = NULL;
157 CFMutableStringRef keycopy = CFStringCreateMutableCopy(NULL, 128, key);
159 if( ((mPrefix = copyStringEndingIn(keycopy, sCircleSeparator)) != NULL) &&
160 ((mFrom = copyStringEndingIn(keycopy, NULL)) != NULL)) {
161 if (from && mFrom) *from = CFStringCreateCopy(NULL, mFrom);
165 CFReleaseNull(mPrefix);
166 CFReleaseNull(mFrom);
167 CFReleaseNull(keycopy);
171 if (circle && from) {
172 CFStringRef mCircle = NULL;
173 CFStringRef mFrom = NULL;
174 CFMutableStringRef keycopy = CFStringCreateMutableCopy(NULL, 128, key);
176 if( ((mCircle = copyStringEndingIn(keycopy, sCircleSeparator)) != NULL) &&
177 ((mFrom = copyStringEndingIn(keycopy, NULL)) != NULL)) {
178 if (circle && mCircle) *circle = CFStringCreateCopy(NULL, mCircle);
179 if (from && mFrom) *from = CFStringCreateCopy(NULL, mFrom);
183 CFReleaseNull(mCircle);
184 CFReleaseNull(mFrom);
185 CFReleaseNull(keycopy);
193 SOSKVSKeyType SOSKVSKeyGetKeyTypeAndParse(CFStringRef key, CFStringRef *circle, CFStringRef *peerInfo, CFStringRef *ring, CFStringRef *backupName, CFStringRef *from, CFStringRef *to)
195 SOSKVSKeyType retval = SOSKVSKeyGetKeyType(key);
196 bool parsed = SOSKVSKeyParse(retval, key, circle, peerInfo, ring, backupName, from, to);
197 if(!parsed) retval = kUnknownKey;
203 CFStringRef SOSCircleKeyCreateWithCircle(SOSCircleRef circle, CFErrorRef *error)
205 return SOSCircleKeyCreateWithName(SOSCircleGetName(circle), error);
209 CFStringRef SOSCircleKeyCreateWithName(CFStringRef circleName, CFErrorRef *error)
211 if(!circleName) return NULL;
212 return CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@"), sCirclePrefix, circleName);
215 CFStringRef SOSRingKeyCreateWithName(CFStringRef ring_name, CFErrorRef *error)
217 if(!ring_name) return NULL;
218 return CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@"), sRingPrefix, ring_name);
221 CFStringRef SOSCircleKeyCopyCircleName(CFStringRef key, CFErrorRef *error)
223 CFStringRef circleName = NULL;
225 if (kCircleKey != SOSKVSKeyGetKeyTypeAndParse(key, &circleName, NULL, NULL, NULL, NULL, NULL)) {
226 SOSCreateErrorWithFormat(kSOSErrorNoCircleName, NULL, error, NULL, CFSTR("Couldn't find circle name in key '%@'"), key);
228 CFReleaseNull(circleName);
234 CFStringRef SOSMessageKeyCopyCircleName(CFStringRef key, CFErrorRef *error)
236 CFStringRef circleName = NULL;
238 if (SOSKVSKeyGetKeyTypeAndParse(key, &circleName, NULL, NULL, NULL, NULL, NULL) != kMessageKey) {
239 SOSCreateErrorWithFormat(kSOSErrorNoCircleName, NULL, error, NULL, CFSTR("Couldn't find circle name in key '%@'"), key);
241 CFReleaseNull(circleName);
246 CFStringRef SOSMessageKeyCopyFromPeerName(CFStringRef messageKey, CFErrorRef *error)
248 CFStringRef fromPeer = NULL;
250 if (SOSKVSKeyGetKeyTypeAndParse(messageKey, NULL, NULL, NULL, NULL, &fromPeer, NULL) != kMessageKey) {
251 SOSCreateErrorWithFormat(kSOSErrorNoCircleName, NULL, error, NULL, CFSTR("Couldn't find from peer in key '%@'"), messageKey);
253 CFReleaseNull(fromPeer);
258 CFStringRef SOSMessageKeyCreateWithCircleNameAndPeerNames(CFStringRef circleName, CFStringRef from_peer_name, CFStringRef to_peer_name)
260 return CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@%@%@%@"),
261 circleName, sCircleSeparator, from_peer_name, sFromToSeparator, to_peer_name);
264 CFStringRef SOSMessageKeyCreateWithCircleAndPeerNames(SOSCircleRef circle, CFStringRef from_peer_name, CFStringRef to_peer_name)
266 return SOSMessageKeyCreateWithCircleNameAndPeerNames(SOSCircleGetName(circle), from_peer_name, to_peer_name);
269 CFStringRef SOSMessageKeyCreateWithCircleAndPeerInfos(SOSCircleRef circle, SOSPeerInfoRef from_peer, SOSPeerInfoRef to_peer)
271 return SOSMessageKeyCreateWithCircleAndPeerNames(circle, SOSPeerInfoGetPeerID(from_peer), SOSPeerInfoGetPeerID(to_peer));
274 CFStringRef SOSMessageKeyCreateFromPeerToTransport(SOSMessage* transport, CFStringRef myName, CFStringRef peer_name){
275 CFErrorRef error = NULL;
277 CFStringRef circleName = [transport SOSTransportMessageGetCircleName];
278 CFStringRef result = SOSMessageKeyCreateWithCircleNameAndPeerNames(circleName, peer_name, myName);
279 CFReleaseSafe(error);
283 CFStringRef SOSMessageKeyCreateFromTransportToPeer(SOSMessage* transport, CFStringRef myID, CFStringRef peer_name) {
284 CFErrorRef error = NULL;
286 CFStringRef circleName = [transport SOSTransportMessageGetCircleName];
288 CFStringRef result = SOSMessageKeyCreateWithCircleNameAndPeerNames(circleName, myID, peer_name);
289 CFReleaseSafe(error);
293 CFStringRef SOSRetirementKeyCreateWithCircleNameAndPeer(CFStringRef circle_name, CFStringRef retirement_peer_name)
295 return CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@%@%@"),
296 sRetirementPrefix, circle_name, sCircleSeparator, retirement_peer_name);
299 CFStringRef SOSRingKeyCreateWithRingName(CFStringRef ring_name)
301 return CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@"),
302 sRingPrefix, ring_name);
305 CFStringRef SOSRetirementKeyCreateWithCircleAndPeer(SOSCircleRef circle, CFStringRef retirement_peer_name)
307 return SOSRetirementKeyCreateWithCircleNameAndPeer(SOSCircleGetName(circle), retirement_peer_name);
310 static CFStringRef SOSAccountCreateCompactDescription(SOSAccount* a) {
312 CFStringRef gestaltDescription = CFDictionaryCopySuperCompactDescription((__bridge CFDictionaryRef)(a.gestalt));
314 CFStringRef result = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@"), gestaltDescription);
316 CFReleaseNull(gestaltDescription);
321 //should be >KeyParameters|ourPeerID
322 CFStringRef SOSLastKeyParametersPushedKeyCreateWithPeerID(CFStringRef peerID){
324 return CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@%@%@"),
325 sLastKeyParametersPushedPrefix,kSOSKVSKeyParametersKey, sCircleSeparator, peerID);
329 //should be >KeyParameters|ourPeerID
330 CFStringRef SOSLastKeyParametersPushedKeyCreateWithAccountGestalt(SOSAccount* account){
332 CFStringRef gestaltInfo = SOSAccountCreateCompactDescription(account);
333 CFStringRef key= CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@%@%@"),
334 sLastKeyParametersPushedPrefix, kSOSKVSKeyParametersKey, sCircleSeparator, gestaltInfo);
335 CFReleaseNull(gestaltInfo);
339 CFStringRef SOSDebugInfoKeyCreateWithTypeName(CFStringRef type_name)
341 return CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@"),
342 sDebugInfoPrefix, type_name);