2 * Copyright (c) 2012-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@
26 @header SOSEngine.h - Manifest managent engine and decision making for
27 object syncing protocol.
30 #ifndef _SEC_SOSENGINE_H_
31 #define _SEC_SOSENGINE_H_
33 #include <Security/SecureObjectSync/SOSDataSource.h>
34 #include <Security/SecureObjectSync/SOSMessage.h>
35 #include <Security/SecureObjectSync/SOSPeer.h>
36 #include <dispatch/dispatch.h>
40 // TODO: Move this to SOSPeer.h?
41 typedef void (^SOSEnginePeerMessageSentBlock
)(bool success
);
43 // Return a new engine instance for a given data source.
44 SOSEngineRef
SOSEngineCreate(SOSDataSourceRef dataSource
, CFErrorRef
*error
);
46 // TODO: Nuke from orbit
47 SOSManifestRef
SOSEngineCopyManifest(SOSEngineRef engine
, CFErrorRef
*error
);
49 // Return a snapshot of the current manifest of the engines data source for the views that the given peer is in.
50 SOSManifestRef
SOSEngineCopyLocalPeerManifest(SOSEngineRef engine
, SOSPeerRef peer
, CFErrorRef
*error
);
51 SOSManifestRef
SOSEngineCopyLocalPeerManifest_locked(SOSEngineRef engine
, SOSPeerRef peer
, CFErrorRef
*error
);
53 // Apply changes to all views manifests, and update all peers accordingly
54 bool SOSEngineUpdateChanges(SOSEngineRef engine
, SOSDataSourceTransactionSource source
, CFArrayRef changes
, CFErrorRef
*error
);
56 // Store manifest indexed by it's own digest. Can be retrieved with SOSEngineGetManifestForDigest()
57 void SOSEngineAddManifest(SOSEngineRef engine
, SOSManifestRef manifest
);
59 // Retrive a digest stored with SOSEngineAddManifest()
60 SOSManifestRef
SOSEngineGetManifestForDigest(SOSEngineRef engine
, CFDataRef digest
);
62 // Return the digest for a patched manifest (which is stored in the cache already).
63 CFDataRef
SOSEnginePatchRecordAndCopyDigest(SOSEngineRef engine
, SOSManifestRef base
, SOSManifestRef removals
, SOSManifestRef additions
, CFErrorRef
*error
);
65 // Copy a manifest for a key persisted in a persisted dictionary
66 SOSManifestRef
SOSEngineCopyPersistedManifest(SOSEngineRef engine
, CFDictionaryRef persisted
, CFStringRef key
);
68 // Copy a manifest for a key persisted in a persisted dictionary
69 CFMutableArrayRef
SOSEngineCopyPersistedManifestArray(SOSEngineRef engine
, CFDictionaryRef persisted
, CFStringRef key
, CFErrorRef
*error
);
71 void SOSEngineClearCache(SOSEngineRef engine
);
73 // Dispose of an engine when it's no longer needed.
74 void SOSEngineDispose(SOSEngineRef engine
);
76 // Handle incoming message from a remote peer.
77 bool SOSEngineHandleMessage(SOSEngineRef engine
, CFStringRef peerID
,
78 CFDataRef message
, CFErrorRef
*error
);
80 // Change the set of peers we know about. trustedPeers and untrustedPeers are arrays of SOSPeerMetaRef
81 // trustedPeers is an array of SOSPeerMetaRef (peer SOSPeer.h), untrustedpeers is redundant as the engine
82 // treats a trustedPeer with no views and no publicKey the same as an untrustedPeer.
83 // TODO: Fix the documentation above this line.
84 void SOSEngineCircleChanged(SOSEngineRef engine
, CFStringRef myPeerID
, CFArrayRef trustedPeers
, CFArrayRef untrustedPeers
);
86 // Iterate over all peers.
87 void SOSEngineForEachPeer(SOSEngineRef engine
, void (^with
)(SOSPeerRef peer
));
89 CF_RETURNS_RETAINED CFSetRef
SOSEngineSyncWithBackupPeers(SOSEngineRef engine
, CFSetRef
/* CFStringRef */ peers
, bool forceReset
, CFErrorRef
*error
);
91 // Don't call this unless you know what you are doing. If you do then still don't call it.
92 bool SOSEngineHandleMessage_locked(SOSEngineRef engine
, CFStringRef peerID
, SOSMessageRef message
,
93 SOSTransactionRef txn
, bool *commit
, bool *somethingChanged
, CFErrorRef
*error
);
95 CFDataRef
SOSEngineCreateMessage_locked(SOSEngineRef engine
, SOSTransactionRef txn
, SOSPeerRef peer
,
96 CFMutableArrayRef
*attributeList
, CFErrorRef
*error
, SOSEnginePeerMessageSentBlock
*sent
);
98 // Return a SOSPeerRef for a given peer_id.
99 SOSPeerRef
SOSEngineCopyPeerWithID(SOSEngineRef engine
, CFStringRef peer_id
, CFErrorRef
*error
);
101 // Operate on a peer with a given peer_id under the engine lock
102 bool SOSEngineForPeerID(SOSEngineRef engine
, CFStringRef peer_id
, CFErrorRef
*error
, void (^forPeer
)(SOSTransactionRef txn
, SOSPeerRef peer
));
104 // Modify a peer inside a transaction under then engine lock and optionally force an engine state save when done.
105 bool SOSEngineWithPeerID(SOSEngineRef engine
, CFStringRef peer_id
, CFErrorRef
*error
, void (^with
)(SOSPeerRef peer
, SOSCoderRef coder
, SOSDataSourceRef dataSource
, SOSTransactionRef txn
, bool *forceSaveState
));
107 bool SOSEngineInitializePeerCoder(SOSEngineRef engine
, SOSFullPeerInfoRef myPeerInfo
, SOSPeerInfoRef peerInfo
, CFErrorRef
*error
);
109 // Return a message to be sent for the current state. Returns NULL on errors,
110 // return a zero length CFDataRef if there is nothing to send.
111 // If *ProposedManifest is set the caller is responsible for updating their
112 // proposed manifest upon successful transmission of the message.
113 CFDataRef
SOSEngineCreateMessageToSyncToPeer(SOSEngineRef engine
, CFStringRef peerID
, CFMutableArrayRef
*attributeList
, SOSEnginePeerMessageSentBlock
*sentBlock
, CFErrorRef
*error
);
115 CFStringRef
SOSEngineGetMyID(SOSEngineRef engine
);
116 bool SOSEnginePeerDidConnect(SOSEngineRef engine
, CFStringRef peerID
, CFErrorRef
*error
);
117 bool SOSEngineSetPeerConfirmedManifest(SOSEngineRef engine
, CFStringRef backupName
,
118 CFDataRef keybagDigest
, CFDataRef manifestData
, CFErrorRef
*error
);
119 CFArrayRef
SOSEngineCopyBackupPeerNames(SOSEngineRef engine
, CFErrorRef
*error
);
121 void logRawMessage(CFDataRef message
, bool sending
, uint64_t seqno
);
123 // TODO: TEMPORARY: Get the list of IDs for cleanup, this shouldn't be used instead transport should iterate KVS.
124 CFArrayRef
SOSEngineGetPeerIDs(SOSEngineRef engine
);
126 CFArrayRef
SOSEngineCopyPeerConfirmedDigests(SOSEngineRef engine
, CFErrorRef
*error
);
128 // Private do not use!
129 SOSDataSourceRef
SOSEngineGetDataSource(SOSEngineRef engine
);
130 bool SOSTestEngineSaveWithDER(SOSEngineRef engine
, CFDataRef derState
, CFErrorRef
*error
);
131 bool SOSTestEngineSave(SOSEngineRef engine
, SOSTransactionRef txn
, CFErrorRef
*error
);
132 bool SOSTestEngineLoad(SOSEngineRef engine
, SOSTransactionRef txn
, CFErrorRef
*error
);
133 CFMutableDictionaryRef
derStateToDictionaryCopy(CFDataRef state
, CFErrorRef
*error
);
134 bool SOSTestEngineSaveCoders(CFTypeRef engine
, SOSTransactionRef txn
, CFErrorRef
*error
);
135 bool TestSOSEngineLoadCoders(CFTypeRef engine
, SOSTransactionRef txn
, CFErrorRef
*error
);
136 void TestSOSEngineDoOnQueue(CFTypeRef engine
, dispatch_block_t action
);
137 bool TestSOSEngineDoTxnOnQueue(CFTypeRef engine
, CFErrorRef
*error
, void(^transaction
)(SOSTransactionRef txn
, bool *commit
));
138 CFMutableDictionaryRef
TestSOSEngineGetCoders(CFTypeRef engine
);
140 // MARK: Sync completion notification registration
142 typedef void (^SOSEnginePeerInSyncBlock
)(CFStringRef peerID
, CFSetRef views
);
143 void SOSEngineSetSyncCompleteListener(SOSEngineRef engine
, SOSEnginePeerInSyncBlock notify_block
);
144 void SOSEngineSetSyncCompleteListenerQueue(SOSEngineRef engine
, dispatch_queue_t notify_queue
);
146 // Engine State by Log
147 void SOSEngineLogState(SOSEngineRef engine
);
149 // Keychain/datasource items
150 // Used for the kSecAttrAccount when saving in the datasource with dsSetStateWithKey
151 // Class D [kSecAttrAccessibleAlwaysPrivate/kSecAttrAccessibleAlwaysThisDeviceOnly]
152 extern CFStringRef kSOSEngineStatev2
;
153 extern CFStringRef kSOSEnginePeerStates
;
154 extern CFStringRef kSOSEngineManifestCache
;
155 #define kSOSEngineProtectionDomainClassD kSecAttrAccessibleAlwaysPrivate
156 // Class A [kSecAttrAccessibleWhenUnlockedThisDeviceOnly]
157 extern CFStringRef kSOSEngineCoders
;
158 #define kSOSEngineProtectionDomainClassA kSecAttrAccessibleWhenUnlockedThisDeviceOnly
159 bool SOSEngineGetCodersNeedSaving(SOSEngineRef engine
);
160 void SOSEngineSetCodersNeedSaving(SOSEngineRef engine
, bool saved
);
162 extern CFStringRef kSOSEngineStateVersionKey
;
166 #endif /* !_SEC_SOSENGINE_H_ */