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/OTPreloadOctagonKeysOperation.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 OTPreloadOctagonKeysOperation ()
45 @property OTOperationDependencies* deps;
47 @property NSOperation* finishOp;
50 @implementation OTPreloadOctagonKeysOperation
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-preload-keys", "Beginning operation that preloads the SOSAccount with newly created Octagon Keys");
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];
78 NSError* fetchSelfPeersError = nil;
79 CKKSSelves *selfPeers = [self.deps.octagonAdapter fetchSelfPeers:&fetchSelfPeersError];
80 if((!selfPeers) || fetchSelfPeersError) {
81 secnotice("octagon-preload-keys", "failed to retrieve self peers: %@", fetchSelfPeersError);
82 self.error = fetchSelfPeersError;
83 [self runBeforeGroupFinished:self.finishOp];
87 id<CKKSSelfPeer> currentSelfPeer = selfPeers.currentSelf;
88 if(currentSelfPeer == nil) {
89 secnotice("octagon-preload-keys", "failed to retrieve current self");
90 self.error = [NSError errorWithDomain:OctagonErrorDomain code:OTErrorOctagonAdapter userInfo: @{ NSLocalizedDescriptionKey : @"failed to retrieve current self"}];
91 [self runBeforeGroupFinished:self.finishOp];
95 NSError* updateError = nil;
96 BOOL ret = [self.deps.sosAdapter preloadOctagonKeySetOnAccount:currentSelfPeer error:&updateError];
98 self.error = updateError;
99 [self runBeforeGroupFinished:self.finishOp];
103 self.nextState = self.intendedState;
104 [self runBeforeGroupFinished:self.finishOp];