]> git.saurik.com Git - apple/security.git/blob - keychain/ot/OTEnsureOctagonKeyConsistency.m
Security-59306.11.20.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 <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 the same between ");
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 operationDependencies:self.deps];
99
100 NSError* fetchSelfPeersError = nil;
101 CKKSSelves *selfPeers = [octagonAdapter fetchSelfPeers:&fetchSelfPeersError];
102 if(fetchSelfPeersError) {
103 secnotice("octagon", "failed to retrieve self peers: %@", fetchSelfPeersError);
104 self.error = fetchSelfPeersError;
105 [self runBeforeGroupFinished:self.finishOp];
106 return;
107 }
108
109 id<CKKSSelfPeer> currentSelfPeer = selfPeers.currentSelf;
110 if(currentSelfPeer == nil) {
111 secnotice("octagon", "failed to retrieve current self");
112 self.error = [NSError errorWithDomain:OctagonErrorDomain code:OTErrorOctagonAdapter userInfo: @{ NSLocalizedDescriptionKey : @"failed to retrieve current self"}];
113 [self runBeforeGroupFinished:self.finishOp];
114 return;
115 }
116
117 NSData* octagonSigningKeyData = currentSelfPeer.publicSigningKey.keyData;
118 NSData* octagonEncryptionKeyData = currentSelfPeer.publicEncryptionKey.keyData;
119 NSData* sosSigningKeyData = sosSelf.publicSigningKey.keyData;
120 NSData* sosEncryptionKeyData = sosSelf.publicEncryptionKey.keyData;
121
122 if(![octagonSigningKeyData isEqualToData:sosSigningKeyData] || ![octagonEncryptionKeyData isEqualToData:sosEncryptionKeyData]) {
123 secnotice("octagon", "SOS and Octagon signing keys do NOT match! updating SOS");
124 NSError* updateError = nil;
125 [self.deps.sosAdapter updateOctagonKeySetWithAccount:currentSelfPeer error:&updateError];
126 if(updateError) {
127 self.error = updateError;
128 [self runBeforeGroupFinished:self.finishOp];
129 return;
130 }
131 } else {
132 secnotice("octagon", "SOS and Octagon keys match!");
133 }
134 self.nextState = self.intendedState;
135 [self runBeforeGroupFinished:self.finishOp];
136 }
137
138 @end
139
140 #endif // OCTAGON