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。

 

取出的內容以下:

相關文章
相關標籤/搜索