]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/SOSRingRecovery.m
Security-59754.80.3.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
51 #include "SOSRingUtils.h"
52 #include "SOSRingTypes.h"
53 #include "SOSRingBasic.h"
54
55 // MARK: Recovery Ring Ops
56
57 static SOSRingRef SOSRingCreate_Recovery(CFStringRef name, CFStringRef myPeerID, CFErrorRef *error) {
58 return SOSRingCreate_ForType(name, kSOSRingRecovery, myPeerID, error);
59 }
60
61
62
63 ringFuncStruct recovery = {
64 "Recovery",
65 1,
66 SOSRingCreate_Recovery,
67 SOSRingResetToEmpty_Basic,
68 SOSRingResetToOffering_Basic,
69 SOSRingDeviceIsInRing_Basic,
70 SOSRingApply_Basic,
71 SOSRingWithdraw_Basic,
72 SOSRingGenerationSign_Basic,
73 SOSRingConcordanceSign_Basic,
74 SOSRingPeerKeyConcordanceTrust,
75 NULL,
76 NULL,
77 SOSRingSetPayload_Basic,
78 SOSRingGetPayload_Basic,
79 };
80
81
82 static bool isRecoveryRing(SOSRingRef ring, CFErrorRef *error) {
83 SOSRingType type = SOSRingGetType(ring);
84 require_quiet(kSOSRingRecovery == type, errOut);
85 return true;
86 errOut:
87 SOSCreateError(kSOSErrorUnexpectedType, CFSTR("Not recovery ring type"), NULL, error);
88 return false;
89 }
90
91 bool SOSRingSetRecoveryKeyBag(SOSRingRef ring, SOSFullPeerInfoRef fpi, SOSRecoveryKeyBagRef rkbg, CFErrorRef *error) {
92 SOSRingAssertStable(ring);
93 CFDataRef rkbg_as_data = NULL;
94 bool result = false;
95 require_quiet(isRecoveryRing(ring, error), errOut);
96
97 rkbg_as_data = SOSRecoveryKeyBagCopyEncoded(rkbg, error);
98 result = rkbg_as_data &&
99 SOSRingSetPayload(ring, NULL, rkbg_as_data, fpi, error);
100 errOut:
101 CFReleaseNull(rkbg_as_data);
102 return result;
103 }
104
105 SOSRecoveryKeyBagRef SOSRingCopyRecoveryKeyBag(SOSRingRef ring, CFErrorRef *error) {
106 SOSRingAssertStable(ring);
107
108 CFDataRef rkbg_as_data = NULL;
109 SOSRecoveryKeyBagRef result = NULL;
110 require_quiet(isRecoveryRing(ring, error), errOut);
111
112 rkbg_as_data = SOSRingGetPayload(ring, error);
113 require_quiet(rkbg_as_data, errOut);
114
115 result = SOSRecoveryKeyBagCreateFromData(kCFAllocatorDefault, rkbg_as_data, error);
116
117 errOut:
118 return result;
119 }