2 * Copyright (c) 2015-2016 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@
29 #include "SOSRingTypes.h"
30 #include "SOSRingBasic.h"
31 #include "SOSRingBackup.h"
32 #include "SOSRingRecovery.h"
33 #include "keychain/SecureObjectSync/SOSAccountPriv.h"
34 #include "keychain/SecureObjectSync/SOSInternal.h"
35 #include <Security/SecureObjectSync/SOSBackupSliceKeyBag.h>
36 #include <AssertMacros.h>
38 // These need to track the ring type enums in SOSRingTypes.h
39 static ringFuncs ringTypes[] = {
40 &basic, // kSOSRingBase
41 &backup, // kSOSRingBackup
42 NULL, // kSOSRingPeerKeyed
43 NULL, // kSOSRingEntropyKeyed
44 NULL, // kSOSRingPKKeyed
45 &recovery, // kSOSRingRecovery
47 static const size_t typecount = sizeof(ringTypes) / sizeof(ringFuncs);
49 static bool SOSRingValidType(SOSRingType type) {
50 if(type >= typecount || ringTypes[type] == NULL) return false;
54 // MARK: Exported Functions
57 SOSRingRef SOSRingCreate(CFStringRef name, CFStringRef myPeerID, SOSRingType type, CFErrorRef *error) {
58 if(!SOSRingValidType(type)){
59 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
62 if(!(ringTypes[type]->sosRingCreate)){
63 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
66 return ringTypes[type]->sosRingCreate(name, myPeerID, error);
69 bool SOSRingResetToEmpty(SOSRingRef ring, CFStringRef myPeerID, CFErrorRef *error) {
70 SOSRingAssertStable(ring);
71 SOSRingType type = SOSRingGetType(ring);
72 if(!SOSRingValidType(type)){
73 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
76 if(!ringTypes[type]->sosRingResetToEmpty){
77 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
80 return ringTypes[type]->sosRingResetToEmpty(ring, myPeerID, error);
83 bool SOSRingResetToOffering(SOSRingRef ring, SecKeyRef user_privkey, SOSFullPeerInfoRef requestor, CFErrorRef *error) {
84 SOSRingAssertStable(ring);
85 SOSRingType type = SOSRingGetType(ring);
86 if(!SOSRingValidType(type)){
87 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
90 if(!ringTypes[type]->sosRingResetToOffering){
91 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
94 return ringTypes[type]->sosRingResetToOffering(ring, user_privkey, requestor, error);
97 SOSRingStatus SOSRingDeviceIsInRing(SOSRingRef ring, CFStringRef peerID) {
98 SOSRingAssertStable(ring);
99 SOSRingType type = SOSRingGetType(ring);
100 if(!(SOSRingValidType(type))){
101 return kSOSRingError;
103 if(!(ringTypes[type]->sosRingDeviceIsInRing)){
104 return kSOSRingError;
106 return ringTypes[type]->sosRingDeviceIsInRing(ring, peerID);
109 bool SOSRingApply(SOSRingRef ring, SecKeyRef user_pubkey, SOSFullPeerInfoRef fpi, CFErrorRef *error) {
110 SOSRingAssertStable(ring);
111 SOSRingType type = SOSRingGetType(ring);
112 if(!(SOSRingValidType(type))){
113 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
116 if(!(ringTypes[type]->sosRingApply)){
119 if(!(SOSPeerInfoApplicationVerify(SOSFullPeerInfoGetPeerInfo(fpi), user_pubkey, error))){
120 SOSCreateError(kSOSErrorBadSignature, CFSTR("FullPeerInfo fails userkey signature check"), NULL, error);
124 return ringTypes[type]->sosRingApply(ring, user_pubkey, fpi, error);
127 bool SOSRingWithdraw(SOSRingRef ring, SecKeyRef user_privkey, SOSFullPeerInfoRef requestor, CFErrorRef *error) {
128 SOSRingAssertStable(ring);
129 SOSRingType type = SOSRingGetType(ring);
130 if(!SOSRingValidType(type)){
131 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
134 if(!ringTypes[type]->sosRingWithdraw)
137 return ringTypes[type]->sosRingWithdraw(ring, user_privkey, requestor, error);
140 bool SOSRingGenerationSign(SOSRingRef ring, SecKeyRef user_privkey, SOSFullPeerInfoRef requestor, CFErrorRef *error) {
141 SOSRingAssertStable(ring);
142 SOSRingType type = SOSRingGetType(ring);
143 if(!(SOSRingValidType(type))){
144 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
147 if(!(ringTypes[type]->sosRingGenerationSign)){
150 return ringTypes[type]->sosRingGenerationSign(ring, user_privkey, requestor, error);
153 bool SOSRingConcordanceSign(SOSRingRef ring, SOSFullPeerInfoRef requestor, CFErrorRef *error) {
154 SOSRingAssertStable(ring);
155 SOSRingType type = SOSRingGetType(ring);
156 if(!(SOSRingValidType(type))){
157 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
160 if(!(ringTypes[type]->sosRingConcordanceSign)){
163 return ringTypes[type]->sosRingConcordanceSign(ring, requestor, error);
166 SOSConcordanceStatus SOSRingConcordanceTrust(SOSFullPeerInfoRef me, CFSetRef peers,
167 SOSRingRef knownRing, SOSRingRef proposedRing,
168 SecKeyRef knownPubkey, SecKeyRef userPubkey,
169 CFStringRef excludePeerID, CFErrorRef *error) {
170 SOSRingAssertStable(knownRing);
171 SOSRingAssertStable(proposedRing);
172 SOSRingType type1 = SOSRingGetType(knownRing);
173 SOSRingType type2 = SOSRingGetType(proposedRing);
174 if(!(SOSRingValidType(type1))){
175 return kSOSConcordanceError;
177 if(!(SOSRingValidType(type2))){
178 return kSOSConcordanceError;
180 if((type1 != type2)){
181 return kSOSConcordanceError;
184 secnotice("ring", "concordance trust (%s)", ringTypes[type1]->typeName);
185 secnotice("ring", " knownRing: %@", knownRing);
186 secnotice("ring", " proposedRing: %@", proposedRing);
187 CFStringRef knownKeyID = SOSCopyIDOfKeyWithLength(knownPubkey, 8, NULL);
188 CFStringRef userKeyID = SOSCopyIDOfKeyWithLength(userPubkey, 8, NULL);
189 CFStringRef mypeerSPID = CFStringCreateTruncatedCopy(excludePeerID, 8);
191 secnotice("ring", "knownkey: %@ userkey: %@ myPeerID: %@", knownKeyID, userKeyID, mypeerSPID);
192 CFReleaseNull(knownKeyID);
193 CFReleaseNull(userKeyID);
194 CFReleaseNull(mypeerSPID);
196 if(!(ringTypes[type1]->sosRingConcordanceTrust)){
197 return kSOSConcordanceError;
199 return ringTypes[type1]->sosRingConcordanceTrust(me, peers, knownRing, proposedRing, knownPubkey, userPubkey, excludePeerID, error);
202 bool SOSRingAccept(SOSRingRef ring, SecKeyRef user_privkey, SOSFullPeerInfoRef requestor, CFErrorRef *error) {
203 SOSRingAssertStable(ring);
204 SOSRingType type = SOSRingGetType(ring);
205 if(!(SOSRingValidType(type))){
206 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
209 if(!(ringTypes[type]->sosRingAccept)){
212 return ringTypes[type]->sosRingAccept(ring, user_privkey, requestor, error);
215 bool SOSRingReject(SOSRingRef ring, SecKeyRef user_privkey, SOSFullPeerInfoRef requestor, CFErrorRef *error) {
216 SOSRingAssertStable(ring);
217 SOSRingType type = SOSRingGetType(ring);
218 if(!(SOSRingValidType(type))){
219 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
222 if(!(ringTypes[type]->sosRingReject)){
225 return ringTypes[type]->sosRingReject(ring, user_privkey, requestor, error);
228 bool SOSRingSetPayload(SOSRingRef ring, SecKeyRef user_privkey, CFDataRef payload, SOSFullPeerInfoRef requestor, CFErrorRef *error) {
229 SOSRingAssertStable(ring);
230 SOSRingType type = SOSRingGetType(ring);
231 if(!(SOSRingValidType(type))){
232 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
235 if(!(ringTypes[type]->sosRingSetPayload)){
236 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
239 return ringTypes[type]->sosRingSetPayload(ring, user_privkey, payload, requestor, error);
242 CFDataRef SOSRingGetPayload(SOSRingRef ring, CFErrorRef *error) {
243 SOSRingAssertStable(ring);
244 SOSRingType type = SOSRingGetType(ring);
245 if(!SOSRingValidType(type)){
246 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
249 if(!ringTypes[type]->sosRingGetPayload){
250 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
253 return ringTypes[type]->sosRingGetPayload(ring, error);
256 CFSetRef SOSRingGetBackupViewset(SOSRingRef ring, CFErrorRef *error) {
257 SOSRingAssertStable(ring);
258 SOSRingType type = SOSRingGetType(ring);
259 if(kSOSRingBackup != type){
260 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not backup ring type"), NULL, error);
263 return SOSRingGetBackupViewset_Internal(ring);
266 // This returns one string with the view for the ring - we never used multi-view rings
267 CFStringRef SOSRingGetBackupView(SOSRingRef ring, CFErrorRef *error) {
268 __block CFStringRef result = NULL;
269 CFSetRef allTheViews = SOSRingGetBackupViewset(ring, error);
271 if(CFSetGetCount(allTheViews) == 1) {
272 CFSetForEach(allTheViews, ^(const void *value) {
273 result = asString(value, error);
277 SOSCreateError(kSOSErrorParam, CFSTR("Wrong set count for one return"), NULL, error);
282 static bool isBackupRing(SOSRingRef ring, CFErrorRef *error) {
283 SOSRingType type = SOSRingGetType(ring);
284 if(kSOSRingBackup != type){
285 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not backup ring type"), NULL, error);
291 bool SOSRingSetBackupKeyBag(SOSRingRef ring, SOSFullPeerInfoRef fpi, CFSetRef viewSet, SOSBackupSliceKeyBagRef bskb, CFErrorRef *error) {
292 SOSRingAssertStable(ring);
293 CFDataRef bskb_as_data = NULL;
295 if(!isBackupRing(ring, error)){
296 CFReleaseNull(bskb_as_data);
300 bskb_as_data = SOSBSKBCopyEncoded(bskb, error);
301 result = bskb_as_data &&
302 SOSRingSetBackupViewset_Internal(ring, viewSet) &&
303 SOSRingSetPayload(ring, NULL, bskb_as_data, fpi, error);
304 CFReleaseNull(bskb_as_data);
308 SOSBackupSliceKeyBagRef SOSRingCopyBackupSliceKeyBag(SOSRingRef ring, CFErrorRef *error) {
309 SOSRingAssertStable(ring);
311 CFDataRef bskb_as_data = NULL;
312 SOSBackupSliceKeyBagRef result = NULL;
313 if(!isBackupRing(ring, error)){
317 bskb_as_data = SOSRingGetPayload(ring, error);
321 result = SOSBackupSliceKeyBagCreateFromData(kCFAllocatorDefault, bskb_as_data, error);
325 bool SOSRingPKTrusted(SOSRingRef ring, SecKeyRef pubkey, CFErrorRef *error) {
326 SOSRingAssertStable(ring);
327 SOSRingType type = SOSRingGetType(ring);
328 if(!(SOSRingValidType(type))){
329 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not valid ring type"), NULL, error);
332 return SOSRingVerify(ring, pubkey, error);
335 bool SOSRingPeerTrusted(SOSRingRef ring, SOSFullPeerInfoRef requestor, CFErrorRef *error) {
337 SOSPeerInfoRef pi = SOSFullPeerInfoGetPeerInfo(requestor);
338 SecKeyRef pubkey = SOSPeerInfoCopyPubKey(pi, error);
339 require_quiet(pubkey, exit);
340 retval = SOSRingPKTrusted(ring, pubkey, error);
342 CFReleaseNull(pubkey);