2 * Copyright (c) 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 "SOSRingRecovery.h"
30 #include "SOSRingBackup.h"
32 #include <AssertMacros.h>
34 #include <Security/SecureObjectSync/SOSInternal.h>
35 #include <Security/SecureObjectSync/SOSPeerInfoInternal.h>
36 #include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
37 #include <Security/SecureObjectSync/SOSCircle.h>
38 #include <Security/SecureObjectSync/SOSViews.h>
39 #include <Security/SecureObjectSync/SOSRecoveryKeyBag.h>
41 #include <Security/SecFramework.h>
43 #include <Security/SecKey.h>
44 #include <Security/SecKeyPriv.h>
45 #include <CoreFoundation/CoreFoundation.h>
47 #include <utilities/SecCFWrappers.h>
52 #include "SOSRingUtils.h"
53 #include "SOSRingTypes.h"
54 #include "SOSRingBasic.h"
56 // MARK: Recovery Ring Ops
58 static SOSRingRef
SOSRingCreate_Recovery(CFStringRef name
, CFStringRef myPeerID
, CFErrorRef
*error
) {
59 return SOSRingCreate_ForType(name
, kSOSRingRecovery
, myPeerID
, error
);
64 ringFuncStruct recovery
= {
67 SOSRingCreate_Recovery
,
68 SOSRingResetToEmpty_Basic
,
69 SOSRingResetToOffering_Basic
,
70 SOSRingDeviceIsInRing_Basic
,
72 SOSRingWithdraw_Basic
,
73 SOSRingGenerationSign_Basic
,
74 SOSRingConcordanceSign_Basic
,
75 SOSRingPeerKeyConcordanceTrust
,
78 SOSRingSetPayload_Basic
,
79 SOSRingGetPayload_Basic
,
83 static bool isRecoveryRing(SOSRingRef ring
, CFErrorRef
*error
) {
84 SOSRingType type
= SOSRingGetType(ring
);
85 require_quiet(kSOSRingRecovery
== type
, errOut
);
88 SOSCreateError(kSOSErrorUnexpectedType
, CFSTR("Not recovery ring type"), NULL
, error
);
92 bool SOSRingSetRecoveryKeyBag(SOSRingRef ring
, SOSFullPeerInfoRef fpi
, SOSRecoveryKeyBagRef rkbg
, CFErrorRef
*error
) {
93 SOSRingAssertStable(ring
);
94 CFDataRef rkbg_as_data
= NULL
;
96 require_quiet(isRecoveryRing(ring
, error
), errOut
);
98 rkbg_as_data
= SOSRecoveryKeyBagCopyEncoded(rkbg
, error
);
99 result
= rkbg_as_data
&&
100 SOSRingSetPayload(ring
, NULL
, rkbg_as_data
, fpi
, error
);
102 CFReleaseNull(rkbg_as_data
);
106 SOSRecoveryKeyBagRef
SOSRingCopyRecoveryKeyBag(SOSRingRef ring
, CFErrorRef
*error
) {
107 SOSRingAssertStable(ring
);
109 CFDataRef rkbg_as_data
= NULL
;
110 SOSRecoveryKeyBagRef result
= NULL
;
111 require_quiet(isRecoveryRing(ring
, error
), errOut
);
113 rkbg_as_data
= SOSRingGetPayload(ring
, error
);
114 require_quiet(rkbg_as_data
, errOut
);
116 result
= SOSRecoveryKeyBagCreateFromData(kCFAllocatorDefault
, rkbg_as_data
, error
);