-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
TPAuthenticationManager.m
320 lines (261 loc) · 10.3 KB
/
TPAuthenticationManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// TPAuthenticationManager.m
// teleport
//
// Created by JuL on Thu Mar 04 2004.
// Copyright (c) 2003-2005 abyssoft. All rights reserved.
//
#import "TPAuthenticationManager.h"
#import "TPPreferencesManager.h"
#import "TPHostsManager.h"
#import "TPAuthenticationRequest.h"
#import "TPMainController.h"
#import "TPLocalHost.h"
#import "TPRemoteHost.h"
#import "TPConnectionsManager.h"
#import "TPTransfersManager.h"
#import "TPBackgroundImageTransfer.h"
#import "TPTCPSecureSocket.h"
#import "TPMessage.h"
#define TRUSTED_HOSTS_VERSION 2
NSString * TPTrustedHostsVersionKey = @"TPTrustedHostsVersion";
NSString * TPTrustedHostsKey = @"TPTrustedHosts";
static TPAuthenticationManager * _defaultManager = nil;
static BOOL TPCertificateEqual(SecCertificateRef cert1Ref, SecCertificateRef cert2Ref)
{
CSSM_DATA certData1, certData2;
SecCertificateGetData(cert1Ref, &certData1);
SecCertificateGetData(cert2Ref, &certData2);
if(certData1.Length != certData2.Length)
return NO;
if(memcmp(certData1.Data, certData2.Data, certData1.Length) == 0)
return YES;
else
return NO;
}
@implementation TPAuthenticationManager
+ (TPAuthenticationManager*)defaultManager
{
if(_defaultManager == nil)
_defaultManager = [[TPAuthenticationManager alloc] init];
return _defaultManager;
}
- (instancetype)init
{
self = [super init];
_trustedHosts = [[NSMutableArray alloc] init];
return self;
}
#pragma mark -
#pragma mark Loading/Saving
- (void)loadHosts
{
if([[TPPreferencesManager sharedPreferencesManager] intForPref:TPTrustedHostsVersionKey] != TRUSTED_HOSTS_VERSION) // do not load hosts from an earlier version
return;
NSData * archivedData = [[TPPreferencesManager sharedPreferencesManager] valueForPref:TPTrustedHostsKey];
if(archivedData == nil)
return;
NSArray * trustedHosts = nil;
@try {
trustedHosts = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
}
@catch(NSException * e) {
trustedHosts = nil;
}
if(trustedHosts != nil)
[_trustedHosts addObjectsFromArray:trustedHosts];
}
- (void)saveHosts
{
NSData * archivedData = [NSKeyedArchiver archivedDataWithRootObject:_trustedHosts];
[[TPPreferencesManager sharedPreferencesManager] setValue:archivedData forKey:TPTrustedHostsKey];
[[TPPreferencesManager sharedPreferencesManager] setValue:@TRUSTED_HOSTS_VERSION forKey:TPTrustedHostsVersionKey];
}
#pragma mark -
#pragma mark Authentication requests - client side
- (void)requestAuthenticationOnHost:(TPRemoteHost*)host
{
[[TPConnectionsManager manager] connectToHost:host withDelegate:self infoDict:nil];
}
- (void)connectionToServerSucceeded:(TPNetworkConnection*)connection infoDict:(NSDictionary*)infoDict
{
_currentConnection = connection;
[_currentConnection setDelegate:self];
[_currentConnection sendMessage:[TPMessage messageWithType:TPAuthenticationRequestMsgType]];
}
- (void)connectionToServerFailed:(TPRemoteHost*)host infoDict:(NSDictionary*)infoDict
{
[host setHostState:TPHostSharedState];
NSString * msgTitle = [NSString stringWithFormat:NSLocalizedString(@"Connection to \\U201C%@\\U201D failed.", @"Title for connection failure"), [host computerName]];
NSAlert * alert = [NSAlert alertWithMessageText:msgTitle defaultButton:NSLocalizedString(@"OK", nil) alternateButton:nil otherButton:nil informativeTextWithFormat:NSLocalizedString(@"The server may be down, or an encryption problem may have occured. If encryption is enabled, please check that the certificate algorithms match.", nil)];
[(TPMainController*)[NSApp delegate] performSelector:@selector(presentAlert:) withObject:alert afterDelay:0];
}
- (void)connection:(TPNetworkConnection*)connection receivedMessage:(TPMessage*)message
{
TPMsgType type = [message msgType];
TPRemoteHost * host = [connection connectedHost];
#if DEBUG_GENERAL
DebugLog(@"authentication manager receive msg %ld", type);
#endif
switch(type) {
case TPAuthenticationInProgressMsgType:
{
break;
}
case TPAuthenticationSuccessMsgType:
{
[host setHostState:TPHostPeeredOnlineState];
_currentConnection = nil;
break;
}
case TPAuthenticationFailureMsgType:
{
NSString * msgTitle = [NSString stringWithFormat:NSLocalizedString(@"The host \\U201C%@\\U201D rejected your trust request.", @"Title for trust failure"), [host computerName]];
NSString * explanation = [message infoDict][@"reason"];
if(explanation == nil)
explanation = NSLocalizedString(@"The computer you tried to control rejected your request. You should try on a computer you own.", nil);
[host setHostState:TPHostSharedState];
NSAlert * alert = [NSAlert alertWithMessageText:msgTitle defaultButton:NSLocalizedString(@"OK", nil) alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@", explanation];
[(TPMainController*)[NSApp delegate] presentAlert:alert];
_currentConnection = nil;
break;
}
case TPTransferRequestMsgType:
[[TPTransfersManager manager] receiveTransferRequestWithInfoDict:[message infoDict] onConnection:connection isClient:NO];
break;
case TPTransferSuccessMsgType:
[[TPTransfersManager manager] startTransferWithUID:[message infoDict][TPTransferUIDKey] usingConnection:connection onPort:[[message infoDict][TPTransferPortKey] intValue]];
break;
case TPTransferFailureMsgType:
[[TPTransfersManager manager] abortTransferWithUID:[message string]];
break;
default:
NSLog(@"invalid msg: %ld", type);
}
}
- (void)abortAuthenticationRequest
{
if(_currentConnection != nil)
[_currentConnection sendMessage:[TPMessage messageWithType:TPAuthenticationAbortMsgType]];
}
#pragma mark -
#pragma mark Authentication requests - server side
- (void)authenticationRequestedFromHost:(TPRemoteHost*)host onConnection:(TPNetworkConnection*)connection
{
if([self isHostTrusted:host]) {
if([[TPLocalHost localHost] hasCustomBackgroundImage])
[[TPTransfersManager manager] beginTransfer:[TPOutgoingBackgroundImageTransfer transfer] usingConnection:connection];
[connection sendMessage:[TPMessage messageWithType:TPAuthenticationSuccessMsgType]];
}
else {
if([[TPPreferencesManager sharedPreferencesManager] boolForPref:TRUST_LOCAL_CERTIFICATE]) {
SecCertificateRef remoteHostCertRef = [host certificate];
if(remoteHostCertRef != NULL) {
SecIdentityRef identityRef = [[TPLocalHost localHost] identity];
SecCertificateRef localHostCertRef = NULL;
if(SecIdentityCopyCertificate(identityRef, &localHostCertRef) == noErr) {
if(localHostCertRef != NULL) {
if(TPCertificateEqual(remoteHostCertRef, localHostCertRef)) {
[self host:host setTrusted:YES];
if([[TPLocalHost localHost] hasCustomBackgroundImage])
[[TPTransfersManager manager] beginTransfer:[TPOutgoingBackgroundImageTransfer transfer] usingConnection:connection];
[connection sendMessage:[TPMessage messageWithType:TPAuthenticationSuccessMsgType]];
return;
}
CFRelease(localHostCertRef);
}
}
}
}
int trustRequestBehavior = [[TPPreferencesManager sharedPreferencesManager] intForPref:TRUST_REQUEST_BEHAVIOR];
switch(trustRequestBehavior) {
case TRUST_REQUEST_REJECT:
[connection sendMessage:[TPMessage messageWithType:TPAuthenticationFailureMsgType
andInfoDict:@{@"reason": NSLocalizedString(@"Host not accepting any more trusted hosts.", @"Reason for trust failure")}]];
DebugLog(@"Rejecting authentication: no more authentication");
break;
case TRUST_REQUEST_ACCEPT:
[self host:host setTrusted:YES];
if([[TPLocalHost localHost] hasCustomBackgroundImage])
[[TPTransfersManager manager] beginTransfer:[TPOutgoingBackgroundImageTransfer transfer] usingConnection:connection];
[connection sendMessage:[TPMessage messageWithType:TPAuthenticationSuccessMsgType]];
break;
case TRUST_REQUEST_ASK:
default:
{
[connection sendMessage:[TPMessage messageWithType:TPAuthenticationInProgressMsgType]];
TPAuthenticationRequest * authenticationRequest = [[TPAuthenticationRequest alloc] initWithNetworkConnection:connection demandingHost:host];
TPAuthenticationResult result = [authenticationRequest ask];
[self replyToAuthenticationRequest:authenticationRequest withResult:result];
[connection setDelegate:self];
break;
}
}
}
}
- (void)replyToAuthenticationRequest:(TPAuthenticationRequest*)authRequest withResult:(TPAuthenticationResult)result
{
switch(result) {
case TPAuthenticationAcceptedAndRejectOthersResult:
[[TPPreferencesManager sharedPreferencesManager] setValue:@(TRUST_REQUEST_REJECT) forKey:TRUST_REQUEST_BEHAVIOR];
// no break
case TPAuthenticationAcceptedResult:
{
TPRemoteHost * host = [authRequest demandingHost];
DebugLog(@"authentication request from host %@ accepted", host);
[self host:host setTrusted:YES];
TPNetworkConnection * connection = [authRequest connection];
if([[TPLocalHost localHost] hasCustomBackgroundImage])
[[TPTransfersManager manager] beginTransfer:[TPOutgoingBackgroundImageTransfer transfer] usingConnection:connection];
[connection sendMessage:[TPMessage messageWithType:TPAuthenticationSuccessMsgType]];
break;
}
case TPAuthenticationRejectedResult:
[[authRequest connection] sendMessage:[TPMessage messageWithType:TPAuthenticationFailureMsgType
andInfoDict:@{@"reason": NSLocalizedString(@"User rejected your trust request.", @"Reason for trust failure")}]];
DebugLog(@"Rejecting authentication: user decision");
break;
case TPAuthenticationAbortedResult:
DebugLog(@"Authentication aborted");
}
}
#pragma mark -
#pragma mark Trusted hosts
- (NSArray*)trustedHosts
{
return _trustedHosts;
}
- (void)host:(TPRemoteHost*)remoteHost setTrusted:(BOOL)trusted
{
[_trustedHosts removeObject:remoteHost];
if(trusted)
[_trustedHosts addObject:remoteHost];
[self saveHosts];
}
- (BOOL)isHostTrusted:(TPRemoteHost*)remoteHost
{
BOOL trusted = NO;
#if LEGACY_BUILD
unsigned index;
#else
NSUInteger index;
#endif
index = [_trustedHosts indexOfObject:remoteHost];
if(index != NSNotFound) {
TPRemoteHost * trustedRemoteHost = _trustedHosts[index];
SecCertificateRef trustedCertRef = [trustedRemoteHost certificate];
if(trustedCertRef == NULL) {
trusted = YES; // trusting a non certified host
}
else {
SecCertificateRef certRef = [remoteHost certificate];
if(certRef != NULL) {
trusted = TPCertificateEqual(trustedCertRef, certRef);
}
}
}
if(trusted)
[self host:remoteHost setTrusted:YES];
return trusted;
}
@end