]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/SOSRingRecovery.m
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / SecureObjectSync / SOSRingRecovery.m
1 /*
2 * Copyright (c) 2016 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 //
25 // SOSRingRecovery.c
26 // sec
27 //
28
29 #include "SOSRingRecovery.h"
30 #include "SOSRingBackup.h"
31
32 #include <AssertMacros.h>
33
34 #include "keychain/SecureObjectSync/SOSInternal.h"
35 #include "keychain/SecureObjectSync/SOSPeerInfoInternal.h"
36 #include "keychain/SecureObjectSync/SOSPeerInfoCollections.h"
37 #include "keychain/SecureObjectSync/SOSCircle.h"
38 #include <Security/SecureObjectSync/SOSViews.h>
39 #include "keychain/SecureObjectSync/SOSRecoveryKeyBag.h"
40
41 #include <Security/SecFramework.h>
42
43 #include <Security/SecKey.h>
44 #include <Security/SecKeyPriv.h>
45 #include <CoreFoundation/CoreFoundation.h>
46
47 #include <utilities/SecCFWrappers.h>
48
49 #include <stdlib.h>
50 #include <assert.h>
51
52 #include "SOSRingUtils.h"
53 #include "SOSRingTypes.h"
54 #include "SOSRingBasic.h"
55
56 // MARK: Recovery Ring Ops
57
58 static SOSRingRef SOSRingCreate_Recovery(CFStringRef name, CFStringRef myPeerID, CFErrorRef *error) {
59 return SOSRingCreate_ForType(name, kSOSRingRecovery, myPeerID, error);
60 }
61
62
63
64 ringFuncStruct recovery = {
65 "Recovery",
66 1,
67 SOSRingCreate_Recovery,
68 SOSRingResetToEmpty_Basic,
69 SOSRingResetToOffering_Basic,
70 SOSRingDeviceIsInRing_Basic,
71 SOSRingApply_Basic,
72 SOSRingWithdraw_Basic,
73 SOSRingGenerationSign_Basic,
74 SOSRingConcordanceSign_Basic,
75 SOSRingPeerKeyConcordanceTrust,
76 NULL,
77 NULL,
78 SOSRingSetPayload_Basic,
79 SOSRingGetPayload_Basic,
80 };
81
82
83 static bool isRecoveryRing(SOSRingRef ring, CFErrorRef *error) {
84 SOSRingType type = SOSRingGetType(ring);
85 require_quiet(kSOSRingRecovery == type, errOut);
86 return true;
87 errOut:
88 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not recovery ring type"), NULL, error);
89 return false;
90 }
91
92 bool SOSRingSetRecoveryKeyBag(SOSRingRef ring, SOSFullPeerInfoRef fpi, SOSRecoveryKeyBagRef rkbg, CFErrorRef *error) {
93 SOSRingAssertStable(ring);
94 CFDataRef rkbg_as_data = NULL;
95 bool result = false;
96 require_quiet(isRecoveryRing(ring, error), errOut);
97
98 rkbg_as_data = SOSRecoveryKeyBagCopyEncoded(rkbg, error);
99 result = rkbg_as_data &&
100 SOSRingSetPayload(ring, NULL, rkbg_as_data, fpi, error);
101 errOut:
102 CFReleaseNull(rkbg_as_data);
103 return result;
104 }
105
106 SOSRecoveryKeyBagRef SOSRingCopyRecoveryKeyBag(SOSRingRef ring, CFErrorRef *error) {
107 SOSRingAssertStable(ring);
108
109 CFDataRef rkbg_as_data = NULL;
110 SOSRecoveryKeyBagRef result = NULL;
111 require_quiet(isRecoveryRing(ring, error), errOut);
112
113 rkbg_as_data = SOSRingGetPayload(ring, error);
114 require_quiet(rkbg_as_data, errOut);
115
116 result = SOSRecoveryKeyBagCreateFromData(kCFAllocatorDefault, rkbg_as_data, error);
117
118 errOut:
119 return result;
120 }