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"
27 #import <Security/SecKey.h>
28 #import <Security/SecKeyPriv.h>
30 #import "keychain/ot/OTCuttlefishContext.h"
31 #import "keychain/ot/OTFetchViewsOperation.h"
32 #import "keychain/ot/OTOperationDependencies.h"
33 #import "keychain/ot/OTPrepareOperation.h"
35 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
36 #import "keychain/ot/ObjCImprovements.h"
38 @interface OTPrepareOperation ()
39 @property OTOperationDependencies* deps;
40 @property NSOperation* finishedOp;
43 @implementation OTPrepareOperation
44 @synthesize intendedState = _intendedState;
45 @synthesize nextState = _nextState;
47 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
48 intendedState:(OctagonState*)intendedState
49 errorState:(OctagonState*)errorState
50 deviceInfo:(OTDeviceInformation*)deviceInfo
53 if((self = [super init])) {
56 _deviceInfo = deviceInfo;
59 _intendedState = intendedState;
60 _nextState = errorState;
67 secnotice("octagon", "preparing an identity");
69 self.finishedOp = [[NSOperation alloc] init];
70 [self dependOnBeforeGroupFinished:self.finishedOp];
72 NSString* bottleSalt = nil;
74 NSError *authKitError = nil;
75 NSString *altDSID = [self.deps.authKitAdapter primaryiCloudAccountAltDSID:&authKitError];
81 secnotice("octagon-sos", "AuthKit doesn't know about the altDSID: %@", authKitError);
83 NSError* accountError = nil;
84 OTAccountMetadataClassC* account = [self.deps.stateHolder loadOrCreateAccountMetadata:&accountError];
86 if(account && !accountError) {
87 secnotice("octagon", "retrieved account, altdsid is: %@", account.altDSID);
88 bottleSalt = account.altDSID;
90 if(accountError || !account){
91 secerror("failed to rerieve account object: %@", accountError);
96 // But, if this device is SOS-enabled and SOS is present, use the SOS octagon keys (if present)
97 NSData* signingKeyPersistRef = nil;
98 NSData* encryptionKeyPersistRef = nil;
99 if(self.deps.sosAdapter.sosEnabled) {
100 secnotice("octagon-sos", "Investigating use of Octagon keys from SOS identity");
102 NSError* error = nil;
103 id<CKKSSelfPeer> sosSelf = [self.deps.sosAdapter currentSOSSelf:&error];
105 if(!sosSelf || error) {
106 secnotice("octagon-sos", "Failed to get the current SOS self: %@", error);
108 // Fetch the persistent references for our signing and encryption keys
109 OSStatus status = errSecSuccess;
110 CFDataRef cfSigningKeyPersistRef = NULL;
111 status = SecKeyCopyPersistentRef(sosSelf.signingKey.secKey, &cfSigningKeyPersistRef);
112 if(status != errSecSuccess || !cfSigningKeyPersistRef) {
113 secnotice("octagon-sos", "Failed to get the persistent ref for our SOS signing key: %d", (int)status);
115 CFDataRef cfEncryptionKeyPersistRef = NULL;
116 status = SecKeyCopyPersistentRef(sosSelf.encryptionKey.secKey, &cfEncryptionKeyPersistRef);
117 if(status != errSecSuccess || !cfEncryptionKeyPersistRef) {
118 secnotice("octagon-sos", "Failed to get the persistent ref for our SOS encryption key: %d", (int)status);
119 CFReleaseNull(cfSigningKeyPersistRef);
120 CFReleaseNull(cfEncryptionKeyPersistRef);
122 // We only want to use these keys if we successfully have both
123 signingKeyPersistRef = CFBridgingRelease(cfSigningKeyPersistRef);
124 encryptionKeyPersistRef = CFBridgingRelease(cfEncryptionKeyPersistRef);
130 NSError* persistError = nil;
131 BOOL persisted = [self.deps.stateHolder persistOctagonJoinAttempt:OTAccountMetadataClassC_AttemptedAJoinState_ATTEMPTED error:&persistError];
132 if(!persisted || persistError) {
133 secerror("octagon: failed to save 'attempted join' state: %@", persistError);
136 [self.deps.cuttlefishXPCWrapper prepareWithContainer:self.deps.containerName
137 context:self.deps.contextID
139 machineID:self.deviceInfo.machineID
140 bottleSalt:bottleSalt
141 bottleID:[NSUUID UUID].UUIDString
142 modelID:self.deviceInfo.modelID
143 deviceName:self.deviceInfo.deviceName
144 serialNumber:self.deviceInfo.serialNumber
145 osVersion:self.deviceInfo.osVersion
148 signingPrivKeyPersistentRef:signingKeyPersistRef
149 encPrivKeyPersistentRef:encryptionKeyPersistRef
150 reply:^(NSString * _Nullable peerID, NSData * _Nullable permanentInfo, NSData * _Nullable permanentInfoSig, NSData * _Nullable stableInfo, NSData * _Nullable stableInfoSig, NSError * _Nullable error) {
152 [[CKKSAnalytics logger] logResultForEvent:OctagonEventPrepareIdentity hardFailure:true result:error];
154 secerror("octagon: Error preparing identity: %@", error);
156 [self runBeforeGroupFinished:self.finishedOp];
158 secnotice("octagon", "Prepared: %@ %@ %@", peerID, permanentInfo, permanentInfoSig);
159 self.peerID = peerID;
160 self.permanentInfo = permanentInfo;
161 self.permanentInfoSig = permanentInfoSig;
162 self.stableInfo = stableInfo;
163 self.stableInfoSig = stableInfoSig;
165 NSError* localError = nil;
166 BOOL persisted = [self.deps.stateHolder persistNewEgoPeerID:peerID error:&localError];
167 if(!persisted || localError) {
168 secnotice("octagon", "Couldn't persist peer ID: %@", localError);
169 self.error = localError;
170 [self runBeforeGroupFinished:self.finishedOp];
174 CKKSResultOperation *doneOp = [CKKSResultOperation named:@"ot-prepare"
177 self.nextState = self.intendedState;
180 OTFetchViewsOperation *fetchViewsOp = [[OTFetchViewsOperation alloc] initWithDependencies:self.deps];
181 [self runBeforeGroupFinished:fetchViewsOp];
182 [doneOp addDependency:fetchViewsOp];
183 [self runBeforeGroupFinished:doneOp];
184 [self.finishedOp addDependency:doneOp];
185 [self runBeforeGroupFinished:self.finishedOp];