Keychain Services的相關接口可以讓你發現、增加、修改和刪除鑰匙串中的items。
使用OS X的鑰匙鏈訪問密碼保護服務如下所示:
使用iPhone訪問網絡服務器密鑰鏈服務如下所示:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPjxpbWcgYWx0PQ=="" src="/uploadfile/Collfiles/20160730/201607300927101045.png" title="\" />
鑰匙串由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
// 創建一個新的訪問控制對象,該對象具有指定的保護類型和標志。 @available(iOS 8.0, *) public func SecAccessControlCreateWithFlags(allocator: CFAllocator?, _ protection: AnyObject, _ flags: SecAccessControlCreateFlags, _ error: UnsafeMutablePointer?>) -> SecAccessControl?
// 預定義的關鍵常量時,基於字典的參數使用傳遞導入/導出功能 @available(iOS 2.0, *) public let kSecImportExportPassphrase: CFString
// 搜索詞典條目 @available(iOS 2.0, *) public let kSecClass: CFString
// 一般密碼 @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
每種類型的鑰匙串項可以有多個描述屬性
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
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.
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.
kSecAttrKeyClass對應的values
let kSecAttrKeyClassPublic: CFString // 公鑰 let kSecAttrKeyClassPrivate: CFString // 私鑰 let kSecAttrKeyClassSymmetric: CFString // 對稱密鑰
kSecAttrKeyType對應的values
let kSecAttrKeyTypeRSA: CFString // RSA公鑰加密算法 let kSecAttrKeyTypeEC: CFString // 非對稱加密
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 // 一直可訪問,不備份
使用於SecItemCopyMatching, SecItemUpdate, or SecItemDelete.
@available(iOS 7.0, *) public let kSecAttrSynchronizableAny: CFString // 同步和非同步返回查詢結果
使用kSecAttrKeyTypeEC 256-bits加密,對應使用的kSecAttrTokenID和kSecAttrTokenIDSecureEnclave
@available(iOS 9.0, *) public let kSecAttrTokenIDSecureEnclave: CFString // 秘鑰
查詢時使用的屬性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 // 全部結果
用於指定要搜索或添加的項目列表的鍵。用戶提供用於查詢的列表。當這個列表被提供的時候,不會再搜索鑰匙串。
let kSecUseItemList: CFString // CFArrayRef(SecKeychainItemRef, SecKeyRef, SecCertificateRef, SecIdentityRef, or (for persistent item references) CFDataRef items. )
搜索的返回值
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
let kSecValueData: CFString // data數據(CFDataRef) let kSecValueRef: CFString // 引用數據(SecKeychainItemRef, SecKeyRef, SecCertificateRef, or SecIdentityRef.) let kSecValuePersistentRef: CFString // 強引用數據(CFDataRef)
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. }
@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)
@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校驗跳過