2 * Copyright (c) 2017 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@
25 #include <AssertMacros.h>
26 #import <Foundation/Foundation.h>
27 #import <CFNetwork/CFNSURLConnection.h>
29 #include <sys/sysctl.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"
37 #include "SecRevocationDb.h"
38 #import "SecTrustLoggingServer.h"
40 #import "SecRevocationNetworking.h"
42 #define kSecRevocationBasePath "/Library/Keychains/crls"
44 static CFStringRef kSecPrefsDomain = CFSTR("com.apple.security");
45 static CFStringRef kUpdateWiFiOnlyKey = CFSTR("ValidUpdateWiFiOnly");
46 static CFStringRef kUpdateBackgroundKey = CFSTR("ValidUpdateBackground");
48 extern CFAbsoluteTime gUpdateStarted;
49 extern CFAbsoluteTime gNextUpdate;
51 static int checkBasePath(const char *basePath) {
52 return mkpath_np((char*)basePath, 0755);
55 static uint64_t systemUptimeInSeconds() {
56 struct timeval boottime;
57 size_t tv_size = sizeof(boottime);
58 time_t now, uptime = 0;
61 mib[1] = KERN_BOOTTIME;
63 if (sysctl(mib, 2, &boottime, &tv_size, NULL, 0) != -1 &&
64 boottime.tv_sec != 0) {
65 uptime = now - boottime.tv_sec;
67 return (uint64_t)uptime;
70 typedef void (^CompletionHandler)(void);
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;
81 @implementation ValidDelegate
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;
89 /* POWER LOG EVENT: operation canceled */
90 SecPLLogRegisteredEvent(@"ValidUpdateEvent", @{
91 @"timestamp" : @([[NSDate date] timeIntervalSince1970]),
92 @"event" : (self->_finishedDownloading) ? @"updateCanceled" : @"downloadCanceled"
94 secnotice("validupdate", "%s canceled at %f",
95 (self->_finishedDownloading) ? "update" : "download",
96 (double)CFAbsoluteTimeGetCurrent());
99 SecRevocationDbComputeAndSetNextUpdateTime();
102 - (void)updateDb:(NSUInteger)version{
103 __block NSURL *updateFileURL = self->_currentUpdateFileURL;
104 __block NSFileHandle *updateFile = self->_currentUpdateFile;
105 if (!updateFileURL || !updateFile) {
110 dispatch_async(_revDbUpdateQueue, ^{
111 /* POWER LOG EVENT: background update started */
112 SecPLLogRegisteredEvent(@"ValidUpdateEvent", @{
113 @"timestamp" : @([[NSDate date] timeIntervalSince1970]),
114 @"event" : @"updateStarted"
116 secnotice("validupdate", "update started at %f", (double)CFAbsoluteTimeGetCurrent());
118 CFDataRef updateData = NULL;
119 const char *updateFilePath = [updateFileURL fileSystemRepresentation];
121 if ((rtn = readValidFile(updateFilePath, &updateData)) != 0) {
122 secerror("failed to read %@ with error %d", updateFileURL, rtn);
123 TrustdHealthAnalyticsLogErrorCode(TAEventValidUpdate, TAFatalError, rtn);
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);
133 CFReleaseNull(updateData);
135 /* We're done with this file */
136 [updateFile closeFile];
137 if (updateFilePath) {
138 (void)remove(updateFilePath);
140 self->_currentUpdateFile = nil;
141 self->_currentUpdateFileURL = nil;
143 /* POWER LOG EVENT: background update finished */
144 SecPLLogRegisteredEvent(@"ValidUpdateEvent", @{
145 @"timestamp" : @([[NSDate date] timeIntervalSince1970]),
146 @"event" : @"updateFinished"
149 /* Update is complete */
150 secnotice("validupdate", "update finished at %f", (double)CFAbsoluteTimeGetCurrent());
157 - (NSInteger)versionFromTask:(NSURLSessionTask *)task {
158 return atol([task.taskDescription cStringUsingEncoding:NSUTF8StringEncoding]);
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]);
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);
179 /* Clean up any old files from previous tasks. */
180 (void)remove(updateFilePath);
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);
192 /* POWER LOG EVENT: background download actually started */
193 SecPLLogRegisteredEvent(@"ValidUpdateEvent", @{
194 @"timestamp" : @([[NSDate date] timeIntervalSince1970]),
195 @"event" : @"downloadStarted"
197 secnotice("validupdate", "download started at %f", (double)CFAbsoluteTimeGetCurrent());
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);
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);
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]);
222 if (!self->_currentUpdateFile) {
223 secnotice("validupdate", "received data, but output file is not open");
230 /* Writing can fail and throw an exception, e.g. if we run out of disk space. */
231 [self->_currentUpdateFile writeData:data];
233 @catch(NSException *exception) {
234 secnotice("validupdate", "%s", exception.description.UTF8String);
235 TrustdHealthAnalyticsLogErrorCode(TAEventValidUpdate, TARecoverableError, errSecDiskFull);
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;
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
255 /* close file before we leave */
256 [self->_currentUpdateFile closeFile];
257 self->_currentUpdateFile = nil;
258 self->_currentUpdateFileURL = nil;
260 /* POWER LOG EVENT: background download finished */
261 SecPLLogRegisteredEvent(@"ValidUpdateEvent", @{
262 @"timestamp" : @([[NSDate date] timeIntervalSince1970]),
263 @"event" : @"downloadFinished"
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]];
274 @interface ValidUpdateRequest : NSObject
275 @property NSTimeInterval updateScheduled;
276 @property NSURLSession *backgroundSession;
279 static ValidUpdateRequest *request = nil;
281 @implementation ValidUpdateRequest
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);
291 CFReleaseNull(value);
292 bool updateInBackground = true;
293 value = CFPreferencesCopyValue(kUpdateBackgroundKey, kSecPrefsDomain, kCFPreferencesAnyUser, kCFPreferencesCurrentHost);
294 if (isBoolean(value)) {
295 updateInBackground = CFBooleanGetValue((CFBooleanRef)value);
297 CFReleaseNull(value);
299 NSURLSessionConfiguration *config = nil;
300 if (updateInBackground) {
301 config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: @"com.apple.trustd.networking.background"];
302 config.networkServiceType = NSURLNetworkServiceTypeBackground;
303 config.discretionary = YES;
305 config = [NSURLSessionConfiguration ephemeralSessionConfiguration]; // no cookies or data storage
306 config.networkServiceType = NSURLNetworkServiceTypeDefault;
307 config.discretionary = NO;
310 config.HTTPAdditionalHeaders = @{ @"User-Agent" : @"com.apple.trustd/2.0",
312 @"Accept-Encoding" : @"gzip,deflate,br"};
314 config.TLSMinimumSupportedProtocol = kTLSProtocol12;
315 config.TLSMaximumSupportedProtocol = kTLSProtocol13;
317 config._requiresPowerPluggedIn = YES;
319 config.allowsCellularAccess = (!updateOnWiFiOnly) ? YES : NO;
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");
331 delegate.transaction = NULL;
332 delegate.revDbUpdateQueue = updateQueue;
333 delegate.finishedDownloading = NO;
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];
341 - (BOOL) scheduleUpdateFromServer:(NSString *)server forVersion:(NSUInteger)version withQueue:(dispatch_queue_t)updateQueue {
343 secnotice("validupdate", "invalid update request");
348 secnotice("validupdate", "missing update queue, skipping update");
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);
362 uint64_t uptime = systemUptimeInSeconds();
363 const uint64_t minUptime = 180;
364 if (uptime < minUptime) {
365 gNextUpdate = now + (minUptime - uptime);
367 secnotice("validupdate", "postponing update until %f", gNextUpdate);
369 self.updateScheduled = now;
370 secnotice("validupdate", "scheduling update at %f", (double)self.updateScheduled);
374 NSURL *validUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/g3/v%ld",
375 server, (unsigned long)version]];
377 secnotice("validupdate", "invalid update url");
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");
389 if (!self.backgroundSession) {
390 [self createSession:updateQueue];
393 /* POWER LOG EVENT: scheduling our background download session now */
394 SecPLLogRegisteredEvent(@"ValidUpdateEvent", @{
395 @"timestamp" : @([[NSDate date] timeIntervalSince1970]),
396 @"event" : @"downloadScheduled",
397 @"version" : @(version)
400 NSURLSessionDataTask *dataTask = [self.backgroundSession dataTaskWithURL:validUrl];
401 dataTask.taskDescription = [NSString stringWithFormat:@"%lu",(unsigned long)version];
403 secnotice("validupdate", "scheduled background data task %@ at %f", dataTask, CFAbsoluteTimeGetCurrent());
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];
415 return [request scheduleUpdateFromServer:(__bridge NSString*)server forVersion:version withQueue:queue];