实施和测试iOS数据保护 [英] Implementing and Testing iOS data protection

查看:154
本文介绍了实施和测试iOS数据保护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚看到了会议209 - 从2010 WWDC保护应用程序数据。

Just saw the Session 209 - Securing Application Data from de 2010 WWDC.

主题演讲解释了很多内容,包括你可以设置数据保护属性的方式你的文件(NSFileProtectionComplete,NSFileProtectionNone)以及如何确定哪种保护最适合你的情况。

The keynote explains a lot of things, including the way you can set data protection attributes to your files (NSFileProtectionComplete, NSFileProtectionNone) and how to decide which protection is best for your case.

我刚刚实现了它,但无法弄清楚如何测试安全性,任何想法?

I just implemented it, but can't figure out how to test if the security is on, any ideas?

另外,我有一个sql lite数据库,需要不时在后台访问,这种数据保护方法似乎不够好..任何指导我通过最好的数据库保护的链接或教程? (找到sql密码,但在一个不同的项目中添加有点重)

In addition, I have a sql lite database that needs to be accessed in background from time to time, and this method of data protection seems to be not good enough.. any link or tutorial that guide me through the best db protection? (found sql cipher but is kinda heavy to add in a evoluted project)

谢谢!

推荐答案

更新:对于iOS 6,可以通过使用需要在iOS配置文件中的App ID上配置的权利来为您的应用程序提供数据保护。我还没有测试过,这是我能找到的最好的信息 https:// devforums.apple.com/message/707939#707939

Update: With iOS 6 it's supposedly possible to require data protection for your application by using an entitlement that needs to be configured on the App ID in the iOS provisioning profile. I haven't tested this yet, and this is the best information I could find on it https://devforums.apple.com/message/707939#707939

我对此事的调查让我相信很难确定设备上是否启用了数据保护。

My investigations into this matter lead me to believe that it is very difficult to determine if data protection is enabled on a device.

通过设置 NSFileProtectionKey 文件属性为 NSFileProtectionComplete

例如,要创建受保护的文件,您可以运行如下代码: p>

For example, to create a protected file you could run code like:

[[NSFileManager defaultManager] createFileAtPath:[self filePath]
                                        contents:[@"super secret file contents" dataUsingEncoding:NSUTF8StringEncoding]
                                      attributes:[NSDictionary dictionaryWithObject:NSFileProtectionComplete
                                                                             forKey:NSFileProtectionKey]];

不幸的是,即使设备上未启用数据保护,此代码也会毫无错误地执行(或者如果代码在数据保护不可用的模拟器上运行。)

Unfortunately this code will execute without error even if Data Protection is not enabled on the device (or if the code is run on the Simulator where Data Protection is not available).

更糟糕的是, NSFileProtectionComplete 属性将是无论文件是否受保护,都要设置。以下内容:

Worse, the NSFileProtectionComplete attribute will be be set regardless of whether the file is protected or not. The following:

self.fileProtectionValue = [[[NSFileManager defaultManager] attributesOfItemAtPath:[self filePath]
                                                                             error:NULL] valueForKey:NSFileProtectionKey];

NSLog(@"file protection value: %@", self.fileProtectionValue);

将吐出文件保护值:NSFileProtectionComplete 无论是否启用数据保护。

will spit out file protection value: NSFileProtectionComplete no matter whether Data Protection is enabled or not.

我可以使用两种方法来发现文件保护是否按预期工作。遗憾的是,这些方法都不适合检测是否在现场设备上启用了数据保护。

There are two methods that I've been able to use to discover if File Protection is working as expected. Unfortunately neither of these methods are suitable for detecting if Data Protection is enabled on a device in the field.

两种方法都认为无法读取受保护文件如果设备被锁定。

Both methods work on the idea that a protected file can not be read if the device is locked.

方法一涉及使用计时器在设备被锁定后尝试读取文件,但是当您的应用程序继续运行时:

Method one involves using a timer to attempt to read the file after the device is locked, but while your application continues to run:

[self performSelector:@selector(doReload) withObject:nil afterDelay:20];

- (void)doReload {

    NSLog(@"protected data available: %@",[[UIApplication sharedApplication] isProtectedDataAvailable] ? @"yes" : @"no");

    NSError *error;

    self.fileContents = [NSString stringWithContentsOfFile:[self filePath]
                                              encoding:NSUTF8StringEncoding
                                                 error:&error];

    NSLog(@"file contents: %@\nerror: %@", self.fileContents, error);
}

如果您运行上述代码并锁定受数据保护的设备,它将吐出:

If you run the above code and lock a data protected device it will spit out:

protected data available: no
file contents: (null)
error: Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x16e110 {NSFilePath=/var/mobile/Applications/D71F1F1F-6C25-4848-BB1F-51539B47EC79/Documents/protected_file, NSUnderlyingError=0x16e010 "The operation couldn’t be completed. Operation not permitted"}

20秒延迟是必要的,因为那里是一个10秒左右的宽限期,在启用数据保护的设备被锁定后,受保护的数据仍然可用。

The 20 second delay is necessary because there is a 10 second or so grace period where protected data is still available after a Data Protection enabled device is locked.

第二种方法是在应用程序中创建受保护的文件,退出应用程序,锁定设备,等待10秒,然后使用XCode管理器下载应用程序的内容。这将产生错误消息,受保护的文件将为空。

The second method is to create a protected file in an application, exit the application, lock the device, wait 10 seconds, and then use the XCode organizer to download the contents of the application. This will produce an error message and the protected file will be empty.

如果上述任一测试无法按照描述的方式运行,则数据保护要么未启用,要么文件保护代码未正确实现。

If either of the above tests fail to behave as described then Data Protection is either not enable, or your File Protection code was not implemented correctly.

因为在将机密信息写入磁盘之前,我没有找到任何方法在应用程序中验证数据保护已启用,已向Apple提交功能增强请求,以便能够将应用程序标记为要求启用数据保护。 (rdar:// 10167256)

Because I've not found any way to verify within the application that Data Protection is enabled before I write confidential information to disk, I've filed a feature enhancement request with Apple to be able to mark an application as requiring Data Protection to be enabled. (rdar://10167256)

Apple确实通过其移动设备管理(MDM)API提供了解决方案,与第三方服务器相结合可用于强制要求在设备上启用数据保护的策略。

Apple does offer a solution to this through their Mobile Device Management (MDM) APIs, which combined with a third party server can be used to enforce policies that require Data Protection to be enabled on devices.

这篇关于实施和测试iOS数据保护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆