ios之keychain

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

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

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

一、基本概念

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。

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.

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

从用户的角度来看,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无权访问

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

时间: 2024-08-28 18:56:34

ios之keychain的相关文章

[转] ios中KeyChain用途

转自  http://www.2cto.com/kf/201311/255684.html 一.在应用间利用KeyChain共享数据 我们可以把KeyChain理解为一个Dictionary,所有数据都以key-value的形式存储,可以对这个Dictionary进行add. update.get.delete这四个操作.对于每一个应用来说,KeyChain都有两个访问区,私有区和公共区.私有区是一个sandbox,本 程序存储的任何数据都对其他程序不可见.而要想在将存储的内容放在公共区,需要先

iOS钥匙串Keychain浅析

根据苹果的介绍,iOS设备中的Keychain是一个安全的存储容器,可以用来为不同应用保存敏感信息比如用户名,密码,网络密码,认证令牌.苹果自己用keychain来保存WiFi密码,VPN凭证等.它是一个SQLite数据库,位于/private/var/Keychains/keychain-2.db,其保存的所有数据都是加密过的. 比如在Mac的LaunchPad中,就有一个钥匙串访问,打开就可以看到存储有很多重要的信息: . 开发者通常会希望能够利用操作系统提供的功能来保存凭证(credent

ios中KeyChain用途

一.在应用间利用KeyChain共享数据 我们可以把KeyChain理解为一个Dictionary,所有数据都以key-value的形式存储,可以对这个Dictionary进行add.update.get.delete这四个操作.对于每一个应用来说,KeyChain都有两个访问区,私有区和公共区.私有区是一个sandbox,本程序存储的任何数据都对其他程序不可见.而要想在将存储的内容放在公共区,需要先声明公共区的名称,官方文档管这个名称叫“keychain access group”,声明的方法

ios下 KeyChain 的研究

iOS的keyChain是一个相对独立的空间,当我们的程序(App)被替换或者删除时并不会删除保存在keyChain的内容.相对于NSUserDefaults.plist文件保存等一般方式,keychain保存更为安全.所以我们会用keyChain保存一些私密信息,比如密码.证书.设备唯一码(UDID)等等. 我们可以把KeyChain理解为一个Dictionary,所有数据都以key-value的形式存储,可以对这个Dictionary进行add.update.get.delete这四个操作.

ios中KeyChain用途(整理)

原文地址http://blog.csdn.net/u011439689/article/details/18707387 一.在应用间利用KeyChain共享数据 我们可以把KeyChain理解为一个Dictionary,所有数据都以key-value的形式存储,可以对这个Dictionary进行add.update.get.delete这四个操作.对于每一个应用来说,KeyChain都有两个访问区,私有区和公共区.私有区是一个sandbox,本程序存储的任何数据都对其他程序不可见.而要想在将存

iOS中keychain存储结构的研究

keychain在ios中是保存在sqlite数据库中的. 这个数据库文件的位置: 真机: /private/var/Keychains/keychain-2.db 虚拟机: /Users/USER-HOME/Library/Developer/CoreSimulator/Devices/26DCA62C-B516-4DEA-A601-5C2D0EA07710/data/Library/Keychains/keychain-2-debug.db 在虚拟机中,这个数据库考出来就不能读了,很奇怪.

iOS中keyChain 实现UUID 设备唯一标识符碰到的问题

废话少说,置入正题: 最近在研究实现获取设备唯一标识符时,经过各方面调研,发现使用苹果官方推荐的keyChain还是挺不错的. Apple提供了一个叫GenericKeychain的例子程序,在这里:http://developer.apple.com/library/ios/#samplecode/GenericKeychain/Listings/Classes_KeychainItemWrapper_h.html#//apple_ref/doc/uid/DTS40007797-Classes

IOS 用keychain(钥匙串)保存用户名和密码

IOS系统中,获取设备唯一标识的方法有很多: 一.UDID(Unique Device Identifier) UDID的全称是Unique Device Identifier,顾名思义,它就是苹果IOS设备的唯一识别码,它由40个字符的字母和数字组成. 二.UUID(Universally Unique Identifier)  UUID是Universally Unique Identifier的缩写,中文意思是通用唯一识别码. 三.MAC Address 四.OPEN UDID 五.广告标

iOS 使用Keychain 保存 用户名和密码到 本地

之前曾把一些简单的数据保存在了plist,文件,及NsuserDefault里面, 但是如果要保存密码之类的,保存在本地就很不安全了: 但是利用 Keychain 我们可以很安全的把 用户密码等信息保存在本地 1:使用苹果官方封装好的Keychain操作类 GenericKeychain 2:使用 开源的keychain操作 sskeychain #import <XCTest/XCTest.h> #import "SSKeychain.h" static NSString