ios之keychain

 

 https://developer.apple.com/library/ios/documentation/Security/Conceptual/keychainServConcepts/02concepts/concepts.html#//apple_ref/doc/uid/TP30000897-CH204-TP9html

 

 Keychain Services为一个或多个用户提供了一种安全的保存私密信息(密码,序列号,证书等)的方式。用户可使用一个简单的密码解锁keychain,而且任何一个支持 Keychain Services的应用程序均可以用这个keychain来存储和检索密码等。ios

 Keychain Services Programming Guide  中介绍了Keychain Services、讨论了开发者常用的功能和数据结构,而且提供了一个例子来说解如何在你的应用程序中使用keychain.git

 

1、基本概念github

keychain是一个安全的加密的container,用来为多个应用程序存储密码和提供安全服务。当keychain被锁定时,没有人能够访问它保护的内容。在osX中,用户能够解锁keychain,经过输入一个简单的主密码,为受信任的应用程序(users can unlock a keychain—thus providing trusted applications access to the contents—by entering a single master password)。在ios中,每一个ios app都有一个独立的keychain,用户不须要来解锁keychain。在osX中,只要用户容许,任何能够访问keychain的应用程序均可以访问任何一个keychain item,在ios中,一个app只能访问它本身的keychain items。web

 Keychain只能同时被一个用户locked;OS X里,某个用户的几个相互信任的Apps能够经过同一个master password来同享一个Keychain,这样该用户在使用这些Apps的时候,只须要unlock keychain container一次,系统为每一个用户建立一个名叫login.keychain的default keychain也就是login keychain ;iOS 里,每一个App只能使用本身的Keychain container,这样须要访问Keychain container时候,甚至不用Unlock keychain container操做;iPhone里,keychain container的权限是由对App签名的provisioning profile决定的,因此程序升级时候最好使用同一个provisioning profile.数据库

Note: On iPhone, Keychain rights depend on the provisioning profile used to sign your application. Be sure to consistently use the same provisioning profile across different versions of your application.api

注意:在iPhone上,keychain的权限只取决于用来给app签名的provisioning profile 。必定要确保在不一样版本的应用程序上使用相同的配置概要文件provisioning profile。
安全

        keychain里保存的信息不会因App被删除而丢失,在用户从新安装App后依然有效,数据还在。cookie

 

 

从用户的角度来看,keychain提供的是透明的身份验证服务。也就是说,用户不须要为每个在keychainl里存储了密码的应用程序单独的登陆认证,在os x中,用户只须要输入一个主密码就能够访问到keychain里的全部内容。在ios中,用户甚至不须要输入keychain password。下图清楚的展现了用户、keychain和应用程序之间的关系,对ios来讲,用户输入密码来解锁keychain的那一步直接被省略了。在ios中,一个app永远能够访问它本身的keychain items,可是不能访问到别的app的items。数据结构

 

默认状况下,在OS X每一个登陆帐户只有一个keychain(新登陆在OS X v10.3,这个keychain的名字是login.keychain);然而,一个用户或应用程序能够建立任意数量的keychain。若是login.keychain的密码和用户登陆的帐户密码相同,那么在登陆期间,这个login.keychain是unlocked。在第一次建立时,login Keychain也就是default keychain.

 

keychain的结构

一个keychain能够包含多个keychain items,一个keychain item又能够包含多个attributes。

一个keychain item根据自身的数据类型决定了它是否是须要加密,例如一个存放password或者私有key的item是须要加密的;而后存放certificate的item是能够不加密的,由于certificate数据自己是已经加密了。

与KI相关的attributes取决于这个KI的类型,app最经常使用的KI类型有: Internet passwords 和 generic passwords。

 Internet passwords 包含的attributes有 security domain, protocol type 和 path。passwords和其余secrets是被加密存储在keychain的。

OS X里,加密的KI在locked时候被请求访问的时候,系统会弹出框让用户输入用户名密码;而非加密KI甚至在locked时,用户也能够随时访问。

iOS里,基本没有用户概念,相对的是程序App,每一个App对本身的KIs是有绝对权利的,可是其余程序的KIs无权访问

 

说明:

 

每个keyChain的组成如图,总体是一个字典结构.
1.kSecClass key 定义属于那一种类型的keyChain
2.不一样的类型包含不一样的Attributes,这些attributes定义了这个item的具体信息
3.每一个item能够包含一个密码项来存储对应的密码

1.区别(标识)一个item要用kSecAttrAccount和kSecAttrService

使用:
引入Security包,引入文件 #import <Security/Security.h>

 

 

iPhone Keychain Backups 备份

in iOS, an application always has access to its own keychain items and does not have access to any other application’s items. The system generates its own password for the keychain, and stores the key on the device in such a way that it is not accessible to any application. When a user backs up iPhone data, the keychain data is backed up but the secrets in the keychain remain encrypted in the backup. The keychain password is not included in the backup. Therefore, passwords and other secrets stored in the keychain on the iPhone cannot be used by someone who gains access to an iPhone backup. For this reason, it is important to use the keychain on iPhone to store passwords and other data (such as cookies) that can be used to log into secure web sites.

 在ios中,一个app老是能够访问它本身的 keychain items ,而且不容许访问其余app的items。系统为keychain自动生动生成password,而且以一种其它app访问不到的途径存储这个key。当用户备份iphone数据时,keychain里的数据也同时被备份了,只是keychain里存储的secrets等依然是被加密的。 keychain password 不在此备份中。因此说,存储在keychain的passwords和其余secrets不会被别的用户经过备份iphone数据而得到并使用。

 

Keychain Services Ease of Use 易用性

To create a keychain item and add it to a keychain in OS X, for example, you call one of two functions, depending on whether you want to add an Internet password or some other type of password: SecKeychainAddInternetPassword orSecKeychainAddGenericPassword. In your function call, you pass only those attributes for which there is no obvious default value. For example, you must specify the name of the service and the user’s account name, but you do not have to specify the creation date and creator, because the function can figure those out by itself. You also pass the data (usually a password) that you want to store in the keychain. You do not even have to specify a keychain; if you pass NULL for the keychain reference, the function uses the default keychain. If the keychain is locked, the function automatically displays a dialog prompting the user to unlock the keychain. The function also creates the access object for you, listing the calling application as the only trusted application.

建立一个keychain item并把它加入到keychain中,你须要调用 SecKeychainAddInternetPassword  或 SecKeychainAddGenericPassword 这两个函数之一,具体调哪一个取决于你是想加入一个Internet password或者其余类型的password。

无论调哪一个方法,你只须要传递如下几个attributes:service的name, 用户的帐号名。你也能够把你要在keychain里存储的data (usually a password)传递进去。你甚至不须要指定keychain,若是你为keychain reference传入null,则使用默认的keychain。

 

The iOS Keychain Services API uses a different paradigm (see the following section, iOS Keychain Services Search Dictionaries). This API has a single function (SecItemAdd) for adding an item to a keychain.

ios api用了个范例,你只须要调用SecItemAdd方法便可把一个item添加到keychain。

 

IOS Keychain Services Search Dictionaries 搜索字典

iOS里,对KI进行操做(CRUD)是经过一个k-v的dictionary(一种数据结构)来实现的。具体这个k-v的dictionary里面该放哪些数据是由KI的类型决定的。

基本的一个search操做的dictionary由四个K-V组成:KI的类型(class,例如Internet passwords,加密的公私钥);KI的数据(data,例如标签(label),建立日期(date));用于优化搜索的数据,例如证书的issuing或者email地址;查询返回值类型,例如返回的KI放在一个dictionary里或者一个引用地址)。

Type of key

Key

Value

Item class

kSecClass

kSecClassGenericPassword

Attribute

kSecAttrAccount

"ImaUser"

Attribute

kSecAttrService

"Apple Store"

Search attribute

kSecMatchCaseInsensitive

kCFBooleanTrue

Return type

kSecReturnData

kCFBooleanTrue

 

导出Keychain的数据

http://blog.csdn.net/yiyaaixuexi/article/details/18404343

操做步骤极其简单:

在越狱机上下载terminal终端,

电脑上下载keychain_dumper ,地址:https://github.com/ptoomey3/Keychain-Dumper,经过itools工具把其中的keychain_dumper 文件导入到某一目录中

1)赋予Keychain数据库可读权限

 

2)使用Keychain-Dumper导出Keychain

这样会提示keychain_dumper command not found,

使用这个命令    ./keychain_dumper > data.txt。

 

取出的内容以下:

相关文章
相关标签/搜索