2 * Copyright (c) 2018 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@
26 #import <utilities/debugging.h>
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>
40 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
41 #import "keychain/ot/ObjCImprovements.h"
42 #import "keychain/securityd/SOSCloudCircleServer.h"
44 @interface OTEnsureOctagonKeyConsistency ()
45 @property OTOperationDependencies* deps;
47 @property NSOperation* finishOp;
50 @implementation OTEnsureOctagonKeyConsistency
51 @synthesize intendedState = _intendedState;
53 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
54 intendedState:(OctagonState*)intendedState
55 errorState:(OctagonState*)errorState
57 if((self = [super init])) {
59 _intendedState = intendedState;
60 _nextState = errorState;
67 secnotice("octagon-sos", "Beginning ensuring Octagon keys are set properly in SOS");
69 self.finishOp = [[NSOperation alloc] init];
70 [self dependOnBeforeGroupFinished:self.finishOp];
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];
77 NSError* sosSelfFetchError = nil;
78 id<CKKSSelfPeer> sosSelf = [self.deps.sosAdapter currentSOSSelf:&sosSelfFetchError];
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];
87 secnotice("octagon", "Fetched SOS Self! Fetching Octagon Adapter now.");
89 NSError* getEgoPeerError = nil;
90 NSString* octagonPeerID = [self.deps.stateHolder getEgoPeerID:&getEgoPeerError];
92 secnotice("octagon", "failed to get peer id: %@", getEgoPeerError);
93 self.error = getEgoPeerError;
94 [self runBeforeGroupFinished:self.finishOp];
98 OctagonCKKSPeerAdapter* octagonAdapter = [[OctagonCKKSPeerAdapter alloc] initWithPeerID:octagonPeerID operationDependencies:self.deps];
100 NSError* fetchSelfPeersError = nil;
101 CKKSSelves *selfPeers = [octagonAdapter fetchSelfPeers:&fetchSelfPeersError];
102 if((!selfPeers) || fetchSelfPeersError) {
103 secnotice("octagon", "failed to retrieve self peers: %@", fetchSelfPeersError);
104 self.error = fetchSelfPeersError;
105 [self runBeforeGroupFinished:self.finishOp];
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];
117 NSData* octagonSigningKeyData = currentSelfPeer.publicSigningKey.keyData;
118 NSData* octagonEncryptionKeyData = currentSelfPeer.publicEncryptionKey.keyData;
119 NSData* sosSigningKeyData = sosSelf.publicSigningKey.keyData;
120 NSData* sosEncryptionKeyData = sosSelf.publicEncryptionKey.keyData;
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];
127 self.error = updateError;
128 [self runBeforeGroupFinished:self.finishOp];
132 secnotice("octagon", "SOS and Octagon keys match!");
134 self.nextState = self.intendedState;
135 [self runBeforeGroupFinished:self.finishOp];