你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> Keychain Services

Keychain Services

編輯:IOS開發綜合

Keychain Services的相關接口可以讓你發現、增加、修改和刪除鑰匙串中的items。

使用OS X的鑰匙鏈訪問密碼保護服務如下所示:

\

使用iPhone訪問網絡服務器密鑰鏈服務如下所示:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPjxpbWcgYWx0PQ=="" src="/uploadfile/Collfiles/20160730/201607300927101045.png" title="\" />

1 Functions

1.1 Using Keychain Item Search Dictionaries

鑰匙串由CFDictionary定義鍵值對。

// 搜索查詢
@available(iOS 2.0, *)
public func SecItemCopyMatching(query: CFDictionary, _ result: UnsafeMutablePointer) -> OSStatus
// 增加
@available(iOS 2.0, *)
public func SecItemAdd(attributes: CFDictionary, _ result: UnsafeMutablePointer) -> OSStatus
// 修改
@available(iOS 2.0, *)
public func SecItemUpdate(query: CFDictionary, _ attributesToUpdate: CFDictionary) -> OSStatus
// 刪除
@available(iOS 2.0, *)
public func SecItemDelete(query: CFDictionary) -> OSStatus

1.2 Creating Access Control Objects

// 創建一個新的訪問控制對象,該對象具有指定的保護類型和標志。
@available(iOS 8.0, *)
public func SecAccessControlCreateWithFlags(allocator: CFAllocator?, _ protection: AnyObject, _ flags: SecAccessControlCreateFlags, _ error: UnsafeMutablePointer?>) -> SecAccessControl?

2 Constants

2.1 OS X Keychain Services API Constants

// 預定義的關鍵常量時,基於字典的參數使用傳遞導入/導出功能
@available(iOS 2.0, *)
public let kSecImportExportPassphrase: CFString

2.2 Keychain Item Class Keys and Values

2.2.1 Item Class Key Constant

// 搜索詞典條目
@available(iOS 2.0, *)
public let kSecClass: CFString

2.2.2 Item Class Value Constants

// 一般密碼
@available(iOS 2.0, *)
public let kSecClassGenericPassword: CFString
// 互聯網密碼
@available(iOS 2.0, *)
public let kSecClassInternetPassword: CFString
// 證書對象
@available(iOS 2.0, *)
public let kSecClassCertificate: CFString
// 專用秘鑰
@available(iOS 2.0, *)
public let kSecClassKey: CFString
// 身份對象,包含kSecClassKey和kSecClassCertificate.
@available(iOS 2.0, *)
public let kSecClassIdentity: CFString

2.3 Attribute Item Keys and Values

2.3.1 Attribute Item Keys

每種類型的鑰匙串項可以有多個描述屬性

CFTypeRef Declaration value readonly kSecClassGenericPassword kSecClassInternetPassword kSecClassCertificate kSecClassKey kSecClassIdentity kSecAttrAccessible 可訪問性類型透明 CFTypeRef   √ √ √ √ √ kSecAttrAccessControl(iOS 8.0) 訪問控制 SecAccessControl   √ √ √ √ √ kSecAttrAccessGroup 訪問組 CFStringRef   √ √ √ √ √ kSecAttrSynchronizable(iOS 7.0) 數據同步或異步到其他設備 CFBooleanRef   √ √ √ √ √ kSecAttrCreationDate 創建日期 CFDateRef √ √ √       kSecAttrModificationDate 最後一次修改日期 CFDateRef √ √ √       kSecAttrDescription 描述 CFStringRef   √ √       kSecAttrComment 注釋 CFStringRef   √ √       kSecAttrCreator 創造者 CFNumberRef   √ √       kSecAttrType 類型 CFNumberRef   √ √       kSecAttrLabel 標簽 CFStringRef   √ √ √ √ √ kSecAttrIsInvisible 是否隱藏 kCFBooleanTrue   √ √       kSecAttrIsNegative 是否具有密碼 CFBooleanRef   √ √       kSecAttrAccount 賬戶 CFStringRef   √ √       kSecAttrService 所具有服務 CFStringRef   √         kSecAttrGeneric 用戶自定義內容 CFDataRef   √         kSecAttrSecurityDomain 網絡安全域 CFStringRef     √       kSecAttrServer 服務器域名或IP地址 CFStringRef     √       kSecAttrProtocol 協議 CFNumberRef     √       kSecAttrAuthenticationType 認證類型 CFNumberRef     √       kSecAttrPort 網絡端口 CFNumberRef     √       kSecAttrPath 訪問路徑 CFStringRef     √       kSecAttrSubject X.500證書主題名稱 CFDataRef √     √   √ kSecAttrIssuer X.500證書頒發者名稱 CFDataRef √     √   √ kSecAttrSerialNumber 序列號 CFDataRef √     √   √ kSecAttrSubjectKeyID 主題ID CFDataRef √     √   √ kSecAttrPublicKeyHash 公鑰Hash值 CFDataRef √     √   √ kSecAttrCertificateType 證書類型 CFNumberRef √     √   √ kSecAttrCertificateEncoding 證書編碼類型 CFNumberRef √     √   √ kSecAttrKeyClass 加密密鑰類 CFTypeRef √       √ √ kSecAttrApplicationLabel 標簽(給程序使用) CFStringRef         √ √ kSecAttrIsPermanent 是否永久保存加密密鑰 CFBooleanRef         √ √ kSecAttrApplicationTag 標簽(私有標簽數據) CFDataRef         √ √ kSecAttrKeyType 加密密鑰類型(算法) CFNumberRef         √ √ kSecAttrKeySizeInBits 密鑰總位數 CFNumberRef         √ √ kSecAttrEffectiveKeySize 密鑰有效位數 CFNumberRef         √ √ kSecAttrCanEncrypt 密鑰是否可用於加密 CFBooleanRef         √ √ kSecAttrCanDecrypt 密鑰是否可用於解密 CFBooleanRef         √ √ kSecAttrCanDerive 密鑰是否可用於導出其他密鑰 CFBooleanRef         √ √ kSecAttrCanSign 密鑰是否可用於數字簽名 CFBooleanRef         √ √ kSecAttrCanVerify 密鑰是否可用於驗證數字簽名 CFBooleanRef         √ √ kSecAttrCanWrap 密鑰是否可用於打包其他密鑰 CFBooleanRef         √ √ kSecAttrCanUnwrap 密鑰是否可用於解包其他密鑰 CFBooleanRef         √ √ kSecAttrSyncViewHint(iOS 9.0) 同步視圖中的定義查詢 CFStringRef             kSecAttrTokenID(iOS 9.0) 令牌 CFStringRef            
kSecAttrAccessGroup:如果希望這個keychain的item可以被多個應用share,可以給這個item設置這個屬性,類型是CFStringRef。應用程序在被編譯時,可以在entitlement中指定自己的accessgroup,如果應用的accessgroup名字和keychain item的accessgroup名字一致,那這個應用就可以訪問這個item,不過這個設計並不是很好,因為應用的accessgroup是由應用開發者指定的,它可以故意跟其他應用的accessgroup一樣,從而訪問其他應用的item,更可怕的是還支持wildcard,比如keychain-dumper將自己的accessgroup指定為*,從而可以把keychain中的所有item都dump出來。 kSecAttrTokenID: 當前對應的值只有kSecAttrTokenIDSecureEnclave

2.3.2 Protocol Values

kSecAttrProtocol對應的values

let kSecAttrProtocolFTP: CFString // FTP protocol.
let kSecAttrProtocolFTPAccount: CFString // A client side FTP account.
let kSecAttrProtocolHTTP: CFString // HTTP protocol.
let kSecAttrProtocolIRC: CFString // IRC protocol.
let kSecAttrProtocolNNTP: CFString // NNTP protocol.
let kSecAttrProtocolPOP3: CFString // POP3 protocol.
let kSecAttrProtocolSMTP: CFString // SMTP protocol.
let kSecAttrProtocolSOCKS: CFString // SOCKS protocol.
let kSecAttrProtocolIMAP: CFString // IMAP protocol.
let kSecAttrProtocolLDAP: CFString // LDAP protocol.
let kSecAttrProtocolAppleTalk: CFString // AFP over AppleTalk.
let kSecAttrProtocolAFP: CFString // AFP over TCP.
let kSecAttrProtocolTelnet: CFString // Telnet protocol.
let kSecAttrProtocolSSH: CFString // SSH protocol.
let kSecAttrProtocolFTPS: CFString // FTP over TLS/SSL.
let kSecAttrProtocolHTTPS: CFString // HTTP over TLS/SSL.
let kSecAttrProtocolHTTPProxy: CFString // HTTP proxy.
let kSecAttrProtocolHTTPSProxy: CFString // HTTPS proxy.
let kSecAttrProtocolFTPProxy: CFString // FTP proxy.
let kSecAttrProtocolSMB: CFString // SMB protocol.
let kSecAttrProtocolRTSP: CFString // RTSP protocol.
let kSecAttrProtocolRTSPProxy: CFString // RTSP proxy.
let kSecAttrProtocolDAAP: CFString // DAAP protocol.
let kSecAttrProtocolEPPC: CFString // Remote Apple Events.
let kSecAttrProtocolIPP: CFString // IPP protocol.
let kSecAttrProtocolNNTPS: CFString // NNTP over TLS/SSL.
let kSecAttrProtocolLDAPS: CFString // LDAP over TLS/SSL.
let kSecAttrProtocolTelnetS: CFString // Telnet over TLS/SSL.
let kSecAttrProtocolIMAPS: CFString // IMAP over TLS/SSL.
let kSecAttrProtocolIRCS: CFString // IRC over TLS/SSL.
let kSecAttrProtocolPOP3S: CFString // POP3 over TLS/SSL.

2.3.3 Authentication Type Values

kSecAttrAuthenticationType對應的values

let kSecAttrAuthenticationTypeNTLM: CFString // Windows NT LAN Manager authentication.
let kSecAttrAuthenticationTypeMSN: CFString // Microsoft Network default authentication.
let kSecAttrAuthenticationTypeDPA: CFString // Distributed Password authentication.
let kSecAttrAuthenticationTypeRPA: CFString // Remote Password authentication.
let kSecAttrAuthenticationTypeHTTPBasic: CFString // HTTP Basic authentication.
let kSecAttrAuthenticationTypeHTTPDigest: CFString // HTTP Digest Access authentication.
let kSecAttrAuthenticationTypeHTMLForm: CFString // HTML form based authentication.
let kSecAttrAuthenticationTypeDefault: CFString // The default authentication type.

2.3.4 Key Class Values

kSecAttrKeyClass對應的values

let kSecAttrKeyClassPublic: CFString // 公鑰 
let kSecAttrKeyClassPrivate: CFString // 私鑰
let kSecAttrKeyClassSymmetric: CFString // 對稱密鑰

2.3.5 Key Type Values

kSecAttrKeyType對應的values

let kSecAttrKeyTypeRSA: CFString // RSA公鑰加密算法
let kSecAttrKeyTypeEC: CFString // 非對稱加密

2.3.6 Keychain Item Accessibility Constants

kSecAttrAccessible對應的常量,默認kSecAttrAccessibleWhenUnlocked

let kSecAttrAccessibleWhenUnlocked: CFString // 解鎖可訪問,加密備份
let kSecAttrAccessibleAfterFirstUnlock: CFString // 設備重啟、第一次解鎖後可訪問,加密備份
let kSecAttrAccessibleAlways: CFString // 一直可訪問,加密備份
@available(iOS 8.0, *)
let kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly: CFString // 設備解鎖時才被訪問,不備份,禁用設備密碼會導致這類項目被刪除。
let kSecAttrAccessibleWhenUnlockedThisDeviceOnly: CFString // 解鎖可訪問,不備份
let kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly: CFString // 設備重啟、第一次解鎖後可訪問,不備份
let kSecAttrAccessibleAlwaysThisDeviceOnly: CFString // 一直可訪問,不備份

2.3.7 kSecAttrSynchronizable Value Constants

使用於SecItemCopyMatching, SecItemUpdate, or SecItemDelete.

@available(iOS 7.0, *)
public let kSecAttrSynchronizableAny: CFString // 同步和非同步返回查詢結果

2.3.8 kSecAttrTokenID Value Constants

使用kSecAttrKeyTypeEC 256-bits加密,對應使用的kSecAttrTokenID和kSecAttrTokenIDSecureEnclave

@available(iOS 9.0, *)
public let kSecAttrTokenIDSecureEnclave: CFString // 秘鑰

2.4 Search Keys

2.4.1 Search Attribute Keys

查詢時使用的屬性key

let kSecMatchPolicy: CFString // 指定策略
let kSecMatchItemList: CFString // 指定搜索范圍 CFArrayRef(SecKeychainItemRef, SecKeyRef, SecCertificateRef, SecIdentityRef,CFDataRef)數組內的類型必須唯一。仍然會搜索鑰匙串,但是搜索結果需要與該數組取交集作為最終結果。
let kSecMatchSearchList: CFString // 搜索列表  CFArray
let kSecMatchIssuers: CFString // 指定發行人數組 CFArrayRef(kSecAttrIssuer對應的value)
let kSecMatchEmailAddressIfPresent: CFString // 指定郵件地址 CFStringRef
let kSecMatchSubjectContains: CFString // 指定主題 CFStringRef
let kSecMatchCaseInsensitive: CFString // 指定是否不區分大小寫 CFBooleanRef(kCFBooleanFalse或不提供此參數,區分大小寫;kCFBooleanTrue,不區分大小寫)
let kSecMatchTrustedOnly: CFString // 指定只搜索可信證書 CFBooleanRef(kCFBooleanFalse或不提供此參數,全部證書;kCFBooleanTrue,只搜索可信證書)
let kSecMatchValidOnDate: CFString // 指定有效日期 CFDateRef(kCFNull表示今天)
let kSecMatchLimit: CFString // 指定結果數量 CFNumberRef(kSecMatchLimitOne or kSecMatchLimitAll)
let kSecMatchLimitOne: CFString // 首條結果
let kSecMatchLimitAll: CFString // 全部結果

2.4.2 Item List Key

用於指定要搜索或添加的項目列表的鍵。用戶提供用於查詢的列表。當這個列表被提供的時候,不會再搜索鑰匙串。

let kSecUseItemList: CFString // CFArrayRef(SecKeychainItemRef, SecKeyRef, SecCertificateRef, SecIdentityRef, or (for persistent item references) CFDataRef items. )

2.5 Search Results Constants

2.5.1 Return Type Keys

搜索的返回值

let kSecReturnData: CFString // 返回數據(CFDataRef) CFBooleanRef
let kSecReturnAttributes: CFString // 返回屬性字典(CFDictionaryRef) CFBooleanRef
let kSecReturnRef: CFString // 返回實例(SecKeychainItemRef, SecKeyRef, SecCertificateRef, SecIdentityRef, or CFDataRef) CFBooleanRef
let kSecReturnPersistentRef: CFString // 返回持久型實例(CFDataRef) CFBooleanRef

2.5.2 Value Type Keys

let kSecValueData: CFString // data數據(CFDataRef)
let kSecValueRef: CFString // 引用數據(SecKeychainItemRef, SecKeyRef, SecCertificateRef, or SecIdentityRef.)
let kSecValuePersistentRef: CFString // 強引用數據(CFDataRef)

2.6 Access Control Create Flags

SecAccessControlCreateFlags方法使用的常數

@available(iOS 8.0, *)
public struct SecAccessControlCreateFlags : OptionSetType {
    public init(rawValue: CFIndex)

    public static var UserPresence: SecAccessControlCreateFlags { get } // User presence policy using Touch ID or Passcode. Touch ID does not have to be available or enrolled. Item is still accessible by Touch ID even if fingers are added or removed.
    @available(iOS 9.0, *)
    public static var TouchIDAny: SecAccessControlCreateFlags { get } // Constraint: Touch ID (any finger). Touch ID must be available and at least one finger must be enrolled. Item is still accessible by Touch ID even if fingers are added or removed.
    @available(iOS 9.0, *)
    public static var TouchIDCurrentSet: SecAccessControlCreateFlags { get } // Constraint: Touch ID from the set of currently enrolled fingers. Touch ID must be available and at least one finger must be enrolled. When fingers are added or removed, the item is invalidated.
    @available(iOS 9.0, *)
    public static var DevicePasscode: SecAccessControlCreateFlags { get } // Constraint: Device passcode
    @available(iOS 9.0, *)
    public static var Or: SecAccessControlCreateFlags { get } // Constraint logic operation: when using more than one constraint, at least one of them must be satisfied.
    @available(iOS 9.0, *)
    public static var And: SecAccessControlCreateFlags { get } // Constraint logic operation: when using more than one constraint, all must be satisfied.
    @available(iOS 9.0, *)
    public static var PrivateKeyUsage: SecAccessControlCreateFlags { get } // Create access control for private key operations (i.e. sign operation)
    @available(iOS 9.0, *)
    public static var ApplicationPassword: SecAccessControlCreateFlags { get } // Security: Application provided password for data encryption key generation. This is not a constraint but additional item encryption mechanism.
}

2.7 Other Constants

2.7.1 predefined constants

@available(iOS 8.0, *)
public let kSecUseOperationPrompt: CFString // UI校驗通過
@available(iOS 9.0, *)
public let kSecUseAuthenticationUI: CFString // 驗證UI(CFBooleanRef)
@available(iOS 9.0, *)
public let kSecUseAuthenticationContext: CFString // 秘鑰item驗證(LAContext)

2.7.2 kSecUseAuthenticationUI Value Constants

@available(iOS 9.0, *)
public let kSecUseAuthenticationUIAllow: CFString // UI校驗通過
@available(iOS 9.0, *)
public let kSecUseAuthenticationUIFail: CFString // UI校驗出錯
@available(iOS 9.0, *)
public let kSecUseAuthenticationUISkip: CFString // UI校驗跳過

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved