]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/SecRevocationNetworking.m
a91c58512d0cb2c7c427ad00a34d1e921a4b71e2
[apple/security.git] / OSX / sec / securityd / SecRevocationNetworking.m
1 /*
2 * Copyright (c) 2017 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
25 #include <AssertMacros.h>
26 #import <Foundation/Foundation.h>
27 #import <CFNetwork/CFNSURLConnection.h>
28
29 #include <sys/sysctl.h>
30 #include <sys/time.h>
31 #include <os/transaction_private.h>
32 #include "utilities/debugging.h"
33 #include "utilities/SecCFWrappers.h"
34 #include "utilities/SecPLWrappers.h"
35 #include "utilities/SecFileLocations.h"
36
37 #include "SecRevocationDb.h"
38 #import "SecTrustLoggingServer.h"
39
40 #import "SecRevocationNetworking.h"
41
42 #define kSecRevocationBasePath "/Library/Keychains/crls"
43
44 static CFStringRef kSecPrefsDomain = CFSTR("com.apple.security");
45 static CFStringRef kUpdateWiFiOnlyKey = CFSTR("ValidUpdateWiFiOnly");
46 static CFStringRef kUpdateBackgroundKey = CFSTR("ValidUpdateBackground");
47
48 extern CFAbsoluteTime gUpdateStarted;
49 extern CFAbsoluteTime gNextUpdate;
50
51 static int checkBasePath(const char *basePath) {
52 return mkpath_np((char*)basePath, 0755);
53 }
54
55 static uint64_t systemUptimeInSeconds() {
56 struct timeval boottime;
57 size_t tv_size = sizeof(boottime);
58 time_t now, uptime = 0;
59 int mib[2];
60 mib[0] = CTL_KERN;
61 mib[1] = KERN_BOOTTIME;
62 (void) time(&now);
63 if (sysctl(mib, 2, &boottime, &tv_size, NULL, 0) != -1 &&
64 boottime.tv_sec != 0) {
65 uptime = now - boottime.tv_sec;
66 }
67 return (uint64_t)uptime;
68 }
69
70 typedef void (^CompletionHandler)(void);
71
72 @interface ValidDelegate : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
73 @property CompletionHandler handler;
74 @property dispatch_queue_t revDbUpdateQueue;
75 @property os_transaction_t transaction;
76 @property NSFileHandle *currentUpdateFile;
77 @property NSURL *currentUpdateFileURL;
78 @property BOOL finishedDownloading;
79 @end
80
81 @implementation ValidDelegate
82
83 - (void)reschedule {
84 /* make sure we release any os transaction, if we went active */
85 if (self->_transaction) {
86 //os_release(self->_transaction); // ARC does this for us and won't let us call release
87 self->_transaction = NULL;
88 }
89 /* POWER LOG EVENT: operation canceled */
90 SecPLLogRegisteredEvent(@"ValidUpdateEvent", @{
91 @"timestamp" : @([[NSDate date] timeIntervalSince1970]),
92 @"event" : (self->_finishedDownloading) ? @"updateCanceled" : @"downloadCanceled"
93 });
94 secnotice("validupdate", "%s canceled at %f",
95 (self->_finishedDownloading) ? "update" : "download",
96 (double)CFAbsoluteTimeGetCurrent());
97
98 self->_handler();
99 SecRevocationDbComputeAndSetNextUpdateTime();
100 }
101
102 - (void)updateDb:(NSUInteger)version{
103 __block NSURL *updateFileURL = self->_currentUpdateFileURL;
104 __block NSFileHandle *updateFile = self->_currentUpdateFile;
105 if (!updateFileURL || !updateFile) {
106 [self reschedule];
107 return;
108 }
109
110 dispatch_async(_revDbUpdateQueue, ^{
111 /* POWER LOG EVENT: background update started */
112 SecPLLogRegisteredEvent(@"ValidUpdateEvent", @{
113 @"timestamp" : @([[NSDate date] timeIntervalSince1970]),
114 @"event" : @"updateStarted"
115 });
116 secnotice("validupdate", "update started at %f", (double)CFAbsoluteTimeGetCurrent());
117
118 CFDataRef updateData = NULL;
119 const char *updateFilePath = [updateFileURL fileSystemRepresentation];
120 int rtn;
121 if ((rtn = readValidFile(updateFilePath, &updateData)) != 0) {
122 secerror("failed to read %@ with error %d", updateFileURL, rtn);
123 TrustdHealthAnalyticsLogErrorCode(TAEventValidUpdate, TAFatalError, rtn);
124 [self reschedule];
125 return;
126 }
127
128 secdebug("validupdate", "verifying and ingesting data from %@", updateFileURL);
129 SecValidUpdateVerifyAndIngest(updateData);
130 if ((rtn = munmap((void *)CFDataGetBytePtr(updateData), CFDataGetLength(updateData))) != 0) {
131 secerror("unable to unmap current update %ld bytes at %p (error %d)", CFDataGetLength(updateData), CFDataGetBytePtr(updateData), rtn);
132 }
133 CFReleaseNull(updateData);
134
135 /* We're done with this file */
136 [updateFile closeFile];
137 if (updateFilePath) {
138 (void)remove(updateFilePath);
139 }
140 self->_currentUpdateFile = nil;
141 self->_currentUpdateFileURL = nil;
142
143 /* POWER LOG EVENT: background update finished */
144 SecPLLogRegisteredEvent(@"ValidUpdateEvent", @{
145 @"timestamp" : @([[NSDate date] timeIntervalSince1970]),
146 @"event" : @"updateFinished"
147 });
148
149 /* Update is complete */
150 secnotice("validupdate", "update finished at %f", (double)CFAbsoluteTimeGetCurrent());
151 gUpdateStarted = 0;
152
153 self->_handler();
154 });
155 }
156
157 - (NSInteger)versionFromTask:(NSURLSessionTask *)task {
158 return atol([task.taskDescription cStringUsingEncoding:NSUTF8StringEncoding]);
159 }
160
161 - (void)URLSession:(NSURLSession *)session
162 dataTask:(NSURLSessionDataTask *)dataTask
163 didReceiveResponse:(NSURLResponse *)response
164 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
165 secinfo("validupdate", "Session %@ data task %@ returned response %ld, expecting %lld bytes", session, dataTask,
166 (long)[(NSHTTPURLResponse *)response statusCode],[response expectedContentLength]);
167
168 (void)checkBasePath(kSecRevocationBasePath);
169 CFURLRef updateFileURL = SecCopyURLForFileInRevocationInfoDirectory(CFSTR("update-current"));
170 self->_currentUpdateFileURL = (updateFileURL) ? CFBridgingRelease(updateFileURL) : nil;
171 const char *updateFilePath = [self->_currentUpdateFileURL fileSystemRepresentation];
172 if (!updateFilePath) {
173 secnotice("validupdate", "failed to find revocation info directory. canceling task %@", dataTask);
174 completionHandler(NSURLSessionResponseCancel);
175 [self reschedule];
176 return;
177 }
178
179 /* Clean up any old files from previous tasks. */
180 (void)remove(updateFilePath);
181
182 int fd;
183 off_t off;
184 fd = open(updateFilePath, O_RDWR | O_CREAT | O_TRUNC, 0644);
185 if (fd < 0 || (off = lseek(fd, 0, SEEK_SET)) < 0) {
186 secnotice("validupdate","unable to open %@ (errno %d)", self->_currentUpdateFileURL, errno);
187 }
188 if (fd >= 0) {
189 close(fd);
190 }
191
192 /* POWER LOG EVENT: background download actually started */
193 SecPLLogRegisteredEvent(@"ValidUpdateEvent", @{
194 @"timestamp" : @([[NSDate date] timeIntervalSince1970]),
195 @"event" : @"downloadStarted"
196 });
197 secnotice("validupdate", "download started at %f", (double)CFAbsoluteTimeGetCurrent());
198
199 NSError *error = nil;
200 self->_currentUpdateFile = [NSFileHandle fileHandleForWritingToURL:self->_currentUpdateFileURL error:&error];
201 if (!self->_currentUpdateFile) {
202 secnotice("validupdate", "failed to open %@: %@. canceling task %@", self->_currentUpdateFileURL, error, dataTask);
203 #if ENABLE_TRUSTD_ANALYTICS
204 [[TrustdHealthAnalytics logger] logResultForEvent:TrustdHealthAnalyticsEventValidUpdate hardFailure:NO result:error];
205 #endif // ENABLE_TRUSTD_ANALYTICS
206 completionHandler(NSURLSessionResponseCancel);
207 [self reschedule];
208 return;
209 }
210
211 /* We're about to begin downloading -- go active now so we don't get jetsammed */
212 self->_transaction = os_transaction_create("com.apple.trustd.valid.download");
213 completionHandler(NSURLSessionResponseAllow);
214 }
215
216 - (void)URLSession:(NSURLSession *)session
217 dataTask:(NSURLSessionDataTask *)dataTask
218 didReceiveData:(NSData *)data {
219 secdebug("validupdate", "Session %@ data task %@ returned %lu bytes (%lld bytes so far) out of expected %lld bytes",
220 session, dataTask, (unsigned long)[data length], [dataTask countOfBytesReceived], [dataTask countOfBytesExpectedToReceive]);
221
222 if (!self->_currentUpdateFile) {
223 secnotice("validupdate", "received data, but output file is not open");
224 [dataTask cancel];
225 [self reschedule];
226 return;
227 }
228
229 @try {
230 /* Writing can fail and throw an exception, e.g. if we run out of disk space. */
231 [self->_currentUpdateFile writeData:data];
232 }
233 @catch(NSException *exception) {
234 secnotice("validupdate", "%s", exception.description.UTF8String);
235 TrustdHealthAnalyticsLogErrorCode(TAEventValidUpdate, TARecoverableError, errSecDiskFull);
236 [dataTask cancel];
237 [self reschedule];
238 }
239 }
240
241 - (void)URLSession:(NSURLSession *)session
242 task:(NSURLSessionTask *)task
243 didCompleteWithError:(NSError *)error {
244 /* all finished downloading data -- go inactive */
245 if (self->_transaction) {
246 // os_release(self->_transaction); // ARC does this for us and won't let us call release
247 self->_transaction = NULL;
248 }
249 if (error) {
250 secnotice("validupdate", "Session %@ task %@ failed with error %@", session, task, error);
251 #if ENABLE_TRUSTD_ANALYTICS
252 [[TrustdHealthAnalytics logger] logResultForEvent:TrustdHealthAnalyticsEventValidUpdate hardFailure:NO result:error];
253 #endif // ENABLE_TRUSTD_ANALYTICS
254 [self reschedule];
255 /* close file before we leave */
256 [self->_currentUpdateFile closeFile];
257 self->_currentUpdateFile = nil;
258 self->_currentUpdateFileURL = nil;
259 } else {
260 /* POWER LOG EVENT: background download finished */
261 SecPLLogRegisteredEvent(@"ValidUpdateEvent", @{
262 @"timestamp" : @([[NSDate date] timeIntervalSince1970]),
263 @"event" : @"downloadFinished"
264 });
265 secnotice("validupdate", "download finished at %f", (double)CFAbsoluteTimeGetCurrent());
266 secdebug("validupdate", "Session %@ task %@ succeeded", session, task);
267 self->_finishedDownloading = YES;
268 [self updateDb:[self versionFromTask:task]];
269 }
270 }
271
272 @end
273
274 @interface ValidUpdateRequest : NSObject
275 @property NSTimeInterval updateScheduled;
276 @property NSURLSession *backgroundSession;
277 @end
278
279 static ValidUpdateRequest *request = nil;
280
281 @implementation ValidUpdateRequest
282
283 - (NSURLSessionConfiguration *)validUpdateConfiguration {
284 /* preferences to override defaults */
285 CFTypeRef value = NULL;
286 bool updateOnWiFiOnly = true;
287 value = CFPreferencesCopyValue(kUpdateWiFiOnlyKey, kSecPrefsDomain, kCFPreferencesAnyUser, kCFPreferencesCurrentHost);
288 if (isBoolean(value)) {
289 updateOnWiFiOnly = CFBooleanGetValue((CFBooleanRef)value);
290 }
291 CFReleaseNull(value);
292 bool updateInBackground = true;
293 value = CFPreferencesCopyValue(kUpdateBackgroundKey, kSecPrefsDomain, kCFPreferencesAnyUser, kCFPreferencesCurrentHost);
294 if (isBoolean(value)) {
295 updateInBackground = CFBooleanGetValue((CFBooleanRef)value);
296 }
297 CFReleaseNull(value);
298
299 NSURLSessionConfiguration *config = nil;
300 if (updateInBackground) {
301 config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: @"com.apple.trustd.networking.background"];
302 config.networkServiceType = NSURLNetworkServiceTypeBackground;
303 config.discretionary = YES;
304 } else {
305 config = [NSURLSessionConfiguration ephemeralSessionConfiguration]; // no cookies or data storage
306 config.networkServiceType = NSURLNetworkServiceTypeDefault;
307 config.discretionary = NO;
308 }
309
310 config.HTTPAdditionalHeaders = @{ @"User-Agent" : @"com.apple.trustd/2.0",
311 @"Accept" : @"*/*",
312 @"Accept-Encoding" : @"gzip,deflate,br"};
313
314 config.TLSMinimumSupportedProtocol = kTLSProtocol12;
315 config.TLSMaximumSupportedProtocol = kTLSProtocol13;
316
317 config._requiresPowerPluggedIn = YES;
318
319 config.allowsCellularAccess = (!updateOnWiFiOnly) ? YES : NO;
320
321 return config;
322 }
323
324 - (void) createSession:(dispatch_queue_t)updateQueue {
325 NSURLSessionConfiguration *config = [self validUpdateConfiguration];
326 ValidDelegate *delegate = [[ValidDelegate alloc] init];
327 delegate.handler = ^(void) {
328 request.updateScheduled = 0.0;
329 secdebug("validupdate", "resetting scheduled time");
330 };
331 delegate.transaction = NULL;
332 delegate.revDbUpdateQueue = updateQueue;
333 delegate.finishedDownloading = NO;
334
335 /* Callbacks should be on a separate NSOperationQueue.
336 We'll then dispatch the work on updateQueue and return from the callback. */
337 NSOperationQueue *queue = [[NSOperationQueue alloc] init];
338 _backgroundSession = [NSURLSession sessionWithConfiguration:config delegate:delegate delegateQueue:queue];
339 }
340
341 - (BOOL) scheduleUpdateFromServer:(NSString *)server forVersion:(NSUInteger)version withQueue:(dispatch_queue_t)updateQueue {
342 if (!server) {
343 secnotice("validupdate", "invalid update request");
344 return NO;
345 }
346
347 if (!updateQueue) {
348 secnotice("validupdate", "missing update queue, skipping update");
349 return NO;
350 }
351
352 /* nsurlsessiond waits for unlock to finish launching, so we can't block trust evaluations
353 * on scheduling this background task. Also, we want to wait a sufficient amount of time
354 * after system boot before trying to initiate network activity, to avoid the possibility
355 * of a performance regression in the boot path. */
356 dispatch_async(updateQueue, ^{
357 CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
358 if (self.updateScheduled != 0.0) {
359 secdebug("validupdate", "update in progress (scheduled %f)", (double)self.updateScheduled);
360 return;
361 } else {
362 uint64_t uptime = systemUptimeInSeconds();
363 const uint64_t minUptime = 180;
364 if (uptime < minUptime) {
365 gNextUpdate = now + (minUptime - uptime);
366 gUpdateStarted = 0;
367 secnotice("validupdate", "postponing update until %f", gNextUpdate);
368 } else {
369 self.updateScheduled = now;
370 secnotice("validupdate", "scheduling update at %f", (double)self.updateScheduled);
371 }
372 }
373
374 NSURL *validUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/g3/v%ld",
375 server, (unsigned long)version]];
376 if (!validUrl) {
377 secnotice("validupdate", "invalid update url");
378 return;
379 }
380
381 /* clear all old sessions and cleanup disk (for previous download tasks) */
382 static dispatch_once_t onceToken;
383 dispatch_once(&onceToken, ^{
384 [NSURLSession _obliterateAllBackgroundSessionsWithCompletionHandler:^{
385 secnotice("validupdate", "removing all old sessions for trustd");
386 }];
387 });
388
389 if (!self.backgroundSession) {
390 [self createSession:updateQueue];
391 }
392
393 /* POWER LOG EVENT: scheduling our background download session now */
394 SecPLLogRegisteredEvent(@"ValidUpdateEvent", @{
395 @"timestamp" : @([[NSDate date] timeIntervalSince1970]),
396 @"event" : @"downloadScheduled",
397 @"version" : @(version)
398 });
399
400 NSURLSessionDataTask *dataTask = [self.backgroundSession dataTaskWithURL:validUrl];
401 dataTask.taskDescription = [NSString stringWithFormat:@"%lu",(unsigned long)version];
402 [dataTask resume];
403 secnotice("validupdate", "scheduled background data task %@ at %f", dataTask, CFAbsoluteTimeGetCurrent());
404 });
405
406 return YES;
407 }
408 @end
409
410 bool SecValidUpdateRequest(dispatch_queue_t queue, CFStringRef server, CFIndex version) {
411 static dispatch_once_t onceToken;
412 dispatch_once(&onceToken, ^{
413 request = [[ValidUpdateRequest alloc] init];
414 });
415 return [request scheduleUpdateFromServer:(__bridge NSString*)server forVersion:version withQueue:queue];
416 }