]> git.saurik.com Git - apple/security.git/blob - keychain/ot/OTPreloadOctagonKeysOperation.m
Security-59754.80.3.tar.gz
[apple/security.git] / keychain / ot / OTPreloadOctagonKeysOperation.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/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>
39
40 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
41 #import "keychain/ot/ObjCImprovements.h"
42 #import "keychain/securityd/SOSCloudCircleServer.h"
43
44 @interface OTPreloadOctagonKeysOperation ()
45 @property OTOperationDependencies* deps;
46
47 @property NSOperation* finishOp;
48 @end
49
50 @implementation OTPreloadOctagonKeysOperation
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-preload-keys", "Beginning operation that preloads the SOSAccount with newly created Octagon Keys");
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
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];
84 return;
85 }
86
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];
92 return;
93 }
94
95 NSError* updateError = nil;
96 BOOL ret = [self.deps.sosAdapter preloadOctagonKeySetOnAccount:currentSelfPeer error:&updateError];
97 if(!ret) {
98 self.error = updateError;
99 [self runBeforeGroupFinished:self.finishOp];
100 return;
101 }
102
103 self.nextState = self.intendedState;
104 [self runBeforeGroupFinished:self.finishOp];
105 }
106
107 @end
108
109 #endif // OCTAGON