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里保存的信息不會因App被刪除而丟失,在用戶重新安裝App后依然有效,數據還在。

 

 

從用戶的角度來看,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。

 

取出的內容如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM