]> git.saurik.com Git - apple/security.git/blob - keychain/ot/OTEnsureOctagonKeyConsistency.m
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / ot / OTEnsureOctagonKeyConsistency.m
1 /*
2 * Copyright (c) 2018 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 #if OCTAGON
25
26 #import <utilities/debugging.h>
27
28 #import <SecurityFoundation/SecurityFoundation.h>
29 #import "keychain/ot/OTEnsureOctagonKeyConsistency.h"
30 #import "keychain/ot/OTClientStateMachine.h"
31 #import "keychain/ot/OTCuttlefishContext.h"
32 #import "keychain/ot/OTFetchCKKSKeysOperation.h"
33 #import "keychain/ot/OTDefines.h"
34 #import "keychain/ot/OTConstants.h"
35 #import "keychain/ot/OctagonCKKSPeerAdapter.h"
36 #import "utilities/debugging.h"
37 #import <Security/SecKey.h>
38 #import <Security/SecKeyPriv.h>
39
40 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
41 #import "keychain/ot/ObjCImprovements.h"
42 #import "keychain/securityd/SOSCloudCircleServer.h"
43
44 @interface OTEnsureOctagonKeyConsistency ()
45 @property OTOperationDependencies* deps;
46
47 @property NSOperation* finishOp;
48 @end
49
50 @implementation OTEnsureOctagonKeyConsistency
51 @synthesize intendedState = _intendedState;
52
53 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
54 intendedState:(OctagonState*)intendedState
55 errorState:(OctagonState*)errorState
56 {
57 if((self = [super init])) {
58 _deps = dependencies;
59 _intendedState = intendedState;
60 _nextState = errorState;
61 }
62 return self;
63 }
64
65 - (void)groupStart
66 {
67 secnotice("octagon-sos", "Beginning ensuring Octagon keys are set properly in SOS");
68
69 self.finishOp = [[NSOperation alloc] init];
70 [self dependOnBeforeGroupFinished:self.finishOp];
71
72 if(!self.deps.sosAdapter.sosEnabled) {
73 self.error = [NSError errorWithDomain:OctagonErrorDomain code:OTErrorSOSAdapter userInfo:@{NSLocalizedDescriptionKey : @"sos adapter not enabled"}];
74 [self runBeforeGroupFinished:self.finishOp];
75 return;
76 }
77 NSError* sosSelfFetchError = nil;
78 id<CKKSSelfPeer> sosSelf = [self.deps.sosAdapter currentSOSSelf:&sosSelfFetchError];
79
80 if(!sosSelf || sosSelfFetchError) {
81 secnotice("octagon-sos", "Failed to get the current SOS self: %@", sosSelfFetchError);
82 self.error = sosSelfFetchError;
83 [self runBeforeGroupFinished:self.finishOp];
84 return;
85 }
86
87 secnotice("octagon", "Fetched SOS Self! Fetching Octagon Adapter now.");
88
89 NSError* getEgoPeerError = nil;
90 NSString* octagonPeerID = [self.deps.stateHolder getEgoPeerID:&getEgoPeerError];
91 if(getEgoPeerError) {
92 secnotice("octagon", "failed to get peer id: %@", getEgoPeerError);
93 self.error = getEgoPeerError;
94 [self runBeforeGroupFinished:self.finishOp];
95 return;
96 }
97
98 OctagonCKKSPeerAdapter* octagonAdapter = [[OctagonCKKSPeerAdapter alloc] initWithPeerID:octagonPeerID
99 containerName:self.deps.containerName
100 contextID:self.deps.contextID
101 cuttlefishXPC:self.deps.cuttlefishXPCWrapper];
102
103 secnotice("octagon", "Fetched SOS Self! Fetching Octagon Adapter now: %@", octagonAdapter);
104
105 NSError* fetchSelfPeersError = nil;
106 CKKSSelves *selfPeers = [octagonAdapter fetchSelfPeers:&fetchSelfPeersError];
107
108 if((!selfPeers) || fetchSelfPeersError) {
109 secnotice("octagon", "failed to retrieve self peers: %@", fetchSelfPeersError);
110 self.error = fetchSelfPeersError;
111 [self runBeforeGroupFinished:self.finishOp];
112 return;
113 }
114
115 id<CKKSSelfPeer> currentSelfPeer = selfPeers.currentSelf;
116 if(currentSelfPeer == nil) {
117 secnotice("octagon", "failed to retrieve current self");
118 self.error = [NSError errorWithDomain:OctagonErrorDomain code:OTErrorOctagonAdapter userInfo: @{ NSLocalizedDescriptionKey : @"failed to retrieve current self"}];
119 [self runBeforeGroupFinished:self.finishOp];
120 return;
121 }
122
123 NSData* octagonSigningKeyData = currentSelfPeer.publicSigningKey.keyData;
124 NSData* octagonEncryptionKeyData = currentSelfPeer.publicEncryptionKey.keyData;
125 NSData* sosSigningKeyData = sosSelf.publicSigningKey.keyData;
126 NSData* sosEncryptionKeyData = sosSelf.publicEncryptionKey.keyData;
127
128 if(![octagonSigningKeyData isEqualToData:sosSigningKeyData] || ![octagonEncryptionKeyData isEqualToData:sosEncryptionKeyData]) {
129 secnotice("octagon", "SOS and Octagon signing keys do NOT match! updating SOS");
130 NSError* updateError = nil;
131 BOOL ret = [self.deps.sosAdapter updateOctagonKeySetWithAccount:currentSelfPeer error:&updateError];
132 if(!ret) {
133 self.error = updateError;
134 [self runBeforeGroupFinished:self.finishOp];
135 return;
136 }
137 } else {
138 secnotice("octagon", "SOS and Octagon keys match!");
139 }
140 self.nextState = self.intendedState;
141 [self runBeforeGroupFinished:self.finishOp];
142 }
143
144 @end
145
146 #endif // OCTAGON