forked from pita5/TextSecure-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.m
More file actions
322 lines (276 loc) · 13.2 KB
/
Server.m
File metadata and controls
322 lines (276 loc) · 13.2 KB
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
321
322
//
// Server.m
// TextSecureiOS
//
// Created by Christine Corbett Moran on 3/24/13.
// Copyright (c) 2013 Open Whisper Systems. All rights reserved.
//
#import "Server.h"
#import "Cryptography.h"
#import "NSObject+SBJSON.h"
#import "Message.h"
@implementation Server
@synthesize receivedData;
@synthesize requestQueue;
@synthesize currentRequestApiType;
-(id) init {
if(self==[super init]) {
self.requestQueue = [[NSMutableArray alloc] init];
// how outside world tells server to serve
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doVerifyAccount:) name:@"VerifyAccount" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doCreateAccount:) name:@"CreateAccount" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSendAPN:) name:@"SendAPN" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSendMessage:) name:@"SendMessage" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doGetDirectoryLink:) name:@"GetDirectory" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doRetrieveDirectory:) name:@"RetrieveDirectory" object:nil];
[self getCertificateData];
}
return self;
}
-(NSString*) escapeRequest:(NSString*)request {
return [[request stringByReplacingOccurrencesOfString:@" " withString:@"%20"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
-(void) pushSecureRequest:(Request*) request {
[self.requestQueue insertObject:request atIndex:0];
if([self.requestQueue count]==1) {
[self doNextRequest];
}
}
-(void) doNextRequest {
if([self.requestQueue count] > 0) {
Request* nextRequest=[self.requestQueue lastObject];
self.currentRequestApiType=nextRequest.apiRequestType;
[self serverAuthenticatedRequest:nextRequest];
}
}
-(void) serverAuthenticatedRequest:(Request*)request {
NSURL* requestUrl=request.httpRequestURL;
NSData* requestData=request.httpRequestData;
NSMutableURLRequest *nsRequest = [NSMutableURLRequest requestWithURL:requestUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
if(request.httpRequestType!=DOWNLOAD) {
NSString* method;
if(request.httpRequestType == POST) {
method = @"POST";
}
else if(request.httpRequestType == PUT) {
method = @"PUT";
}
else if (request.httpRequestType == GET) {
method = @"GET";
}
else {
method = @"POST";
}
[nsRequest setHTTPMethod:method];
if(request.httpRequestType != EMPTYPOST) {
[nsRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[nsRequest setValue:[NSString stringWithFormat:@"Basic %@",[Cryptography getAuthorizationToken]] forHTTPHeaderField:@"Authorization"];
[nsRequest addValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
if(requestData!=NULL) {
[nsRequest setHTTPBody:requestData];
}
}
else {
[nsRequest setValue:@"0" forHTTPHeaderField:@"Content-Length"];
}
}
self.receivedData = [[NSMutableData alloc] init];
id urlConnection = [[NSURLConnection alloc] initWithRequest:nsRequest delegate:self];
if(urlConnection==nil) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"ServerError" object:self];
}
}
-(NSURL*) createRequestURL:(NSString*)requestStr withServer:(NSString*)server withAPI:(NSString*) api{
return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@/%@",server,api,[self escapeRequest:requestStr]]];
}
#pragma mark methods
- (NSData*) jsonDataFromDict:(NSDictionary*)parameters {
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error];
if (error) {
DLog(@"The dictionary, %@, could not be serialized. Finished with error : %@", parameters, error);
return nil;
} else {
return data;
}
}
-(void) doCreateAccount:(NSNotification*) notification {
NSString* phoneNumber = [[notification userInfo] objectForKey:@"username"];
NSString* transport = [[notification userInfo] objectForKey:@"transport"];
[Cryptography storeUsernameToken:phoneNumber];
Request* request = [[Request alloc] initWithHttpRequestType:EMPTYPOST
requestUrl:[self createRequestURL:[NSString stringWithFormat:@"%@/%@",transport,phoneNumber] withServer:textSecureServer withAPI:textSecureAccountsAPI]
requestData:NULL
apiRequestType:CREATE_ACCOUNT];
[self pushSecureRequest:request];
}
-(void) doVerifyAccount:(NSNotification*) notification {
NSString* verificationCode = [[notification userInfo] objectForKey:@"verification_code"];
[Cryptography generateAndStoreNewAccountAuthenticationToken];
[Cryptography generateAndStoreNewSignalingKeyToken];
NSDictionary *parameters = [[NSDictionary alloc] initWithObjects:
[[NSArray alloc] initWithObjects:[Cryptography getSignalingKeyToken], nil]
forKeys:[[NSArray alloc] initWithObjects:@"signalingKey",nil]];
Request* request = [[Request alloc] initWithHttpRequestType:PUT
requestUrl:[self createRequestURL:[NSString stringWithFormat:@"code/%@",verificationCode] withServer:textSecureServer withAPI:textSecureAccountsAPI]
requestData:[self jsonDataFromDict:parameters]
apiRequestType:VERIFY_ACCOUNT];
[self pushSecureRequest:request];
}
-(void) doSendAPN:(NSNotification *)notification {
NSString* apn = [[notification userInfo] objectForKey:@"apnRegistrationId"];
NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:apn,@"apnRegistrationId", nil];
Request* request = [[Request alloc] initWithHttpRequestType:PUT
requestUrl:[self createRequestURL:@"apn" withServer:textSecureServer withAPI:textSecureAccountsAPI]
requestData:[self jsonDataFromDict:parameters]
apiRequestType:SEND_APN];
[self pushSecureRequest:request];
}
-(void) doSendMessage:(NSNotification*)notification {
Message* message = [[notification userInfo] objectForKey:@"message"];
NSDictionary *parameters = [[NSDictionary alloc]
initWithObjectsAndKeys:message.destinations,@"destinations",message.text,@"messageText",message.attachments,@"attachments", nil];
Request* request = [[Request alloc] initWithHttpRequestType:POST
requestUrl:[self createRequestURL:@"" withServer:textSecureServer withAPI:textSecureMessagesAPI]
requestData:[self jsonDataFromDict:parameters]
apiRequestType:SEND_MESSAGE];
[self pushSecureRequest:request];
}
-(void) doGetDirectoryLink:(NSNotification*)notification {
Request* request = [[Request alloc] initWithHttpRequestType:GET
requestUrl:[self createRequestURL:@"" withServer:textSecureServer withAPI:textSecureDirectoryAPI]
requestData:NULL
apiRequestType:GET_DIRECTORY_LINK];
[self pushSecureRequest:request];
}
-(void)doRetrieveDirectory:(NSNotification*)notification {
NSString* directoryURL = [[notification userInfo] objectForKey:@"url"];
Request* request = [[Request alloc] initWithHttpRequestType:DOWNLOAD
requestUrl:[NSURL URLWithString:directoryURL]
requestData:NULL
apiRequestType:GET_DIRECTORY];
[self pushSecureRequest:request];
}
#pragma mark - Connection delegate SSL authentication delegate
/* we are using our own trust anchor instead of a CA trust anchor. So we need to make an SSL connection where we verify against our CA */
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (NSData*)getCertificateData {
SecCertificateRef whisperCert = nil;
NSString *whisperCertPath = [[NSBundle mainBundle]
pathForResource:@"whisperca" ofType:@"crt"]; // in DER format
NSData *certData = [[NSData alloc]
initWithContentsOfFile:whisperCertPath];
#ifdef DEBUG
CFDataRef whisperCertData = (__bridge CFDataRef)certData;
whisperCert = SecCertificateCreateWithData(NULL, whisperCertData);
CFStringRef certSummary = SecCertificateCopySubjectSummary(whisperCert);
NSString* summaryString = [[NSString alloc]
initWithString:(__bridge NSString *)certSummary];
NSLog(@"whisper cert summary string %@",summaryString);
#endif
return certData;
}
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
// code adapted from http://stackoverflow.com/questions/14107280/how-to-verify-and-require-self-signed-certificate-in-ios
NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];
if ([protectionSpace authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
// Load anchor cert..
NSData *data = [self getCertificateData];
SecCertificateRef anchorCert = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef)data);
CFMutableArrayRef anchorCerts = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
CFArrayAppendValue(anchorCerts, anchorCert);
// Set anchor cert
SecTrustRef trust = [protectionSpace serverTrust];
SecTrustSetAnchorCertificates(trust, anchorCerts);
SecTrustSetAnchorCertificatesOnly(trust, YES); // only use that certificate
CFRelease(anchorCert);
CFRelease(anchorCerts);
// Validate cert
SecTrustResultType secresult = kSecTrustResultInvalid;
if (SecTrustEvaluate(trust, &secresult) != errSecSuccess) {
[challenge.sender cancelAuthenticationChallenge:challenge];
return;
}
switch (secresult) {
case kSecTrustResultInvalid:
case kSecTrustResultDeny:
case kSecTrustResultFatalTrustFailure:
case kSecTrustResultOtherError:
case kSecTrustResultRecoverableTrustFailure: {
[challenge.sender cancelAuthenticationChallenge:challenge];
return;
}
case kSecTrustResultUnspecified: // The OS trusts this certificate implicitly.
case kSecTrustResultProceed: { // The user explicitly told the OS to trust it.
NSURLCredential *credential = [NSURLCredential credentialForTrust:trust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
return;
}
default: ;
// It's somebody else's key. Fall through.
}
// The server sent a key other than the trusted key.
[connection cancel];
} else {
// TODO: UI error handling
NSLog(@"error not handling %@", [protectionSpace authenticationMethod]);
[connection cancel];
}
}
#pragma mark - Connection delegate methods
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self.requestQueue removeAllObjects];
[self doNextRequest];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ServerError" object:self];
}
-(void) connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {
[self.receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
[self.requestQueue removeLastObject];
if (self.currentRequestApiType == GET_DIRECTORY_LINK||self.currentRequestApiType == GET_DIRECTORY) {
NSError *error;
if(self.currentRequestApiType==GET_DIRECTORY_LINK) {
NSDictionary* directoryInfo = [NSJSONSerialization JSONObjectWithData:self.receivedData options:kNilOptions error:&error];
[[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateDirectoryInfo" object:self userInfo:directoryInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:@"RetrieveDirectory" object:self userInfo:directoryInfo];
}
else {
NSDictionary* directoryInfo = [NSDictionary dictionaryWithObjectsAndKeys:self.receivedData,@"directory",nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateDirectory" object:self userInfo:directoryInfo];
}
}
[self doNextRequest];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// how server alerts outside world of success
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if(self.currentRequestApiType == CREATE_ACCOUNT) {
if([httpResponse statusCode] == 200){
[[NSNotificationCenter defaultCenter] postNotificationName:@"SentVerification" object:self];
}
}
else if(self.currentRequestApiType == VERIFY_ACCOUNT) {
if([httpResponse statusCode] == 200){
[[NSNotificationCenter defaultCenter] postNotificationName:@"VerifiedPhone" object:self];
}
}
else if (self.currentRequestApiType == SEND_APN) {
if([httpResponse statusCode] == 200){
[[NSNotificationCenter defaultCenter] postNotificationName:@"SentAPN" object:self];
}
}
else if (self.currentRequestApiType == SEND_MESSAGE) {
if([httpResponse statusCode] == 200){
[[NSNotificationCenter defaultCenter] postNotificationName:@"SentMessage" object:self];
}
}
else if (self.currentRequestApiType == GET_DIRECTORY_LINK||self.currentRequestApiType == GET_DIRECTORY) {
if([httpResponse statusCode] != 200){
[[NSNotificationCenter defaultCenter] postNotificationName:@"DirectoryRetrieveError" object:self];
}
}
}
@end