2 // KCJoiningAcceptSession.m
7 #import <Foundation/Foundation.h>
9 #import <KeychainCircle/KCJoiningSession.h>
11 #import <KeychainCircle/KCError.h>
12 #import <KeychainCircle/KCDer.h>
13 #import <KeychainCircle/KCJoiningMessages.h>
15 #import <KeychainCircle/NSError+KCCreationHelpers.h>
17 #include <corecrypto/ccder.h>
18 #include <corecrypto/ccrng.h>
19 #include <corecrypto/ccsha2.h>
20 #include <corecrypto/ccdh_gp.h>
22 #include <CommonCrypto/CommonRandomSPI.h>
29 } KCJoiningAcceptSessionState;
31 @interface KCJoiningAcceptSession ()
32 @property (readonly) uint64_t dsid;
33 @property (readonly) NSObject<KCJoiningAcceptSecretDelegate>* secretDelegate;
34 @property (readonly) NSObject<KCJoiningAcceptCircleDelegate>* circleDelegate;
35 @property (readonly) KCSRPServerContext* context;
36 @property (readonly) KCAESGCMDuplexSession* session;
37 @property (readonly) KCJoiningAcceptSessionState state;
38 @property (readwrite) NSData* startMessage;
41 @implementation KCJoiningAcceptSession
43 + (nullable instancetype) sessionWithInitialMessage: (NSData*) message
44 secretDelegate: (NSObject<KCJoiningAcceptSecretDelegate>*) secretDelegate
45 circleDelegate: (NSObject<KCJoiningAcceptCircleDelegate>*) circleDelegate
47 error: (NSError**) error {
50 struct ccrng_state * rng = ccrng(&cc_error);
53 CoreCryptoError(cc_error, error, @"RNG fetch failed");
57 return [[KCJoiningAcceptSession alloc] initWithSecretDelegate: secretDelegate
58 circleDelegate: circleDelegate
64 - (bool) setupSession: (NSError**) error {
65 NSData* key = [self->_context getKey];
68 KCJoiningErrorCreate(kInternalError, error, @"No session key available");
72 self->_session = [KCAESGCMDuplexSession sessionAsReceiver:key context:self.dsid];
74 return self.session != nil;
77 - (nullable instancetype) initWithSecretDelegate: (NSObject<KCJoiningAcceptSecretDelegate>*) secretDelegate
78 circleDelegate: (NSObject<KCJoiningAcceptCircleDelegate>*) circleDelegate
80 rng: (struct ccrng_state *)rng
81 error: (NSError**) error {
84 NSString* name = [NSString stringWithFormat: @"%llu", dsid];
86 self->_context = [[KCSRPServerContext alloc] initWithUser: name
87 password: [secretDelegate secret]
88 digestInfo: ccsha256_di()
89 group: ccsrp_gp_rfc5054_3072()
91 self->_secretDelegate = secretDelegate;
92 self->_circleDelegate = circleDelegate;
93 self->_state = kExpectingA;
99 - (NSString*) stateString {
100 switch (self.state) {
101 case kExpectingA: return @"→A";
102 case kExpectingM: return @"→M";
103 case kExpectingPeerInfo: return @"→PeerInfo";
104 case kAcceptDone: return @"done";
105 default: return [NSString stringWithFormat:@"%d", self.state];
109 - (NSString *)description {
110 return [NSString stringWithFormat: @"<KCJoiningAcceptSession@%p %lld %@ %@>", self, self.dsid, [self stateString], self.context];
113 - (NSData*) copyChallengeMessage: (NSError**) error {
114 NSData* challenge = [self.context copyChallengeFor: self.startMessage error: error];
115 if (challenge == nil) return nil;
117 NSData* srpMessage = [NSData dataWithEncodedSequenceData:self.context.salt data:challenge error:error];
119 if (![self setupSession:error]) return nil;
124 - (NSData*) processInitialMessage: (NSData*) initialMessage error: (NSError**) error {
125 self.startMessage = extractStartFromInitialMessage(initialMessage, error);
126 if (self.startMessage == nil) return nil;
128 NSData* srpMessage = [self copyChallengeMessage: error];
129 if (srpMessage == nil) return nil;
131 self->_state = kExpectingM;
132 return [[KCJoiningMessage messageWithType:kChallenge
137 - (NSData*) processResponse: (KCJoiningMessage*) message error:(NSError**) error {
138 if ([message type] != kResponse) {
139 KCJoiningErrorCreate(kUnexpectedMessage, error, @"Expected response!");
143 // We handle failure, don't capture the error.
144 NSData* confirmation = [self.context copyConfirmationFor:message.firstData error:NULL];
146 // Find out what kind of error we should send.
147 NSData* errorData = nil;
149 switch ([self.secretDelegate verificationFailed: error]) {
151 // We fill in an error if they didn't, but if they did this wont bother.
152 KCJoiningErrorCreate(kInternalError, error, @"Delegate returned error without filling in error: %@", self.secretDelegate);
154 case kKCRetryWithSameChallenge:
155 errorData = [NSData data];
157 case kKCRetryWithNewChallenge:
158 if ([self.context resetWithPassword:[self.secretDelegate secret] error:error]) {
159 errorData = [self copyChallengeMessage: error];
163 if (errorData == nil) return nil;
165 return [[KCJoiningMessage messageWithType:kError
170 NSData* encoded = [NSData dataWithEncodedString:[self.secretDelegate accountCode] error:error];
174 NSData* encrypted = [self.session encrypt:encoded error:error];
175 if (encrypted == nil) return nil;
177 self->_state = kExpectingPeerInfo;
179 return [[KCJoiningMessage messageWithType:kVerification
186 - (NSData*) processApplication: (KCJoiningMessage*) message error:(NSError**) error {
187 if ([message type] != kPeerInfo) {
188 KCJoiningErrorCreate(kUnexpectedMessage, error, @"Expected peerInfo!");
192 NSData* decryptedPayload = [self.session decryptAndVerify:message.firstData error:error];
193 if (decryptedPayload == nil) return nil;
195 CFErrorRef cfError = NULL;
196 SOSPeerInfoRef ref = SOSPeerInfoCreateFromData(NULL, &cfError, (__bridge CFDataRef) decryptedPayload);
198 if (error) *error = (__bridge_transfer NSError*) cfError;
203 NSData* joinData = [self.circleDelegate circleJoinDataFor:ref error:error];
204 if (joinData == nil) return nil;
206 NSData* encryptedOutgoing = [self.session encrypt:joinData error:error];
207 if (encryptedOutgoing == nil) return nil;
209 self->_state = kAcceptDone;
211 return [[KCJoiningMessage messageWithType:kCircleBlob
212 data:encryptedOutgoing
217 - (nullable NSData*) processMessage: (NSData*) incomingMessage error: (NSError**) error {
218 NSData* result = nil;
220 KCJoiningMessage *message = (self.state != kExpectingA) ? [KCJoiningMessage messageWithDER:incomingMessage error:error] : nil;
224 return [self processInitialMessage:incomingMessage error: error];
226 if (message == nil) return nil;
227 return [self processResponse:message error: error];
229 case kExpectingPeerInfo:
230 if (message == nil) return nil;
231 return [self processApplication:message error: error];
234 KCJoiningErrorCreate(kUnexpectedMessage, error, @"Unexpected message while done");
241 return self.state == kAcceptDone;