Keychain
public class Keychain
General class for interacting with the Keychain and Secure Enclave.
-
Accessibility of keychain item.
See moreDeclaration
Swift
public enum AccessibleProtection -
The accessGroup allows multiple apps (including extensions) in the same team to share the same Keychain.
Declaration
Swift
public let accessGroup: String -
Init with accessGroup. The accessGroup allows multiple apps (including extensions) in the same team to share the same Keychain.
Declaration
Swift
public init(accessGroup: String)Parameters
accessGroupThe access group should be an
App Groupon the developer account. -
Save a value to the Keychain.
Declaration
Swift
public func saveValue(name: String, value: String, service: String, protection: AccessibleProtection = .afterFirstUnlockThisDeviceOnly, bioFactor: BioFactor = .none) -> BoolParameters
nameThe name associated with this item.
valueThe value to save as String.
serviceThe service associated with this item.
protectionThe device status protection level associated with this item.
bioFactorThe biometric presence factor associated with this item.
Return Value
True if saved, otherwise false.
-
Save a value to the Keychain.
Declaration
Swift
public func saveValue(name: String, value: Data, service: String, protection: AccessibleProtection = .afterFirstUnlockThisDeviceOnly, bioFactor: BioFactor = .none) -> BoolParameters
nameThe name associated with this item.
valueThe value to save as Data.
serviceThe service associated with this item.
protectionThe device status protection level associated with this item.
bioFactorThe biometric presence factor associated with this item.
Return Value
True if saved, otherwise false.
-
Update a value in the Keychain.
Declaration
Swift
public func updateValue(name: String, value: String, service: String) -> BoolParameters
nameThe name associated with this item.
valueThe updated value.
serviceThe service associated with this item.
Return Value
True if updated, otherwise false.
-
Update a value in the Keychain.
Declaration
Swift
public func updateValue(name: String, value: Data, service: String) -> BoolParameters
nameThe name associated with this item.
valueThe updated value.
serviceThe service associated with this item.
Return Value
True if updated, otherwise false.
-
Delete an item from the Keychain.
Declaration
Swift
public func delete(name: String, service: String)Parameters
nameThe name of the item to delete.
serviceThe service associated with this item.
-
Get a value from the Keychain.
Declaration
Swift
public func getValueAsData(name: String, service: String) -> Data?Parameters
nameThe name of the item.
serviceThe service associated with this item.
Return Value
The value for the specified item as Data.
-
Get a value from the Keychain.
Declaration
Swift
public func getValue(name: String, service: String) -> String?Parameters
nameThe name of the item.
serviceThe service associated with this item.
Return Value
The value for the specified item as String.
-
Get a dictionary of values from the Keychain for the specified service.
Declaration
Swift
public func getValuesAsData(service: String) -> [String : Data]?Parameters
serviceA service name.
Return Value
A dictionary of names and Data values for the specified service.
-
Get a dictionary of values from the Keychain for the specified service.
Declaration
Swift
public func getValues(service: String) -> [String : String]?Parameters
serviceA service name.
Return Value
A dictionary of names and String values for the specified service.
-
Delete key given the SecKey.
Declaration
Swift
public func deleteKey(secKey: SecKey)Parameters
secKeyThe SecKey to delete.
-
Delete key if public key exists.
Declaration
Swift
public func deleteKey(publicKey: Data)Parameters
publicKeyThe public key of the key to delete.
-
Update label.
Declaration
Swift
public func update(label: String, publicKey: Data)Parameters
labelThe new label value.
publicKeyThe public key of the key to update.
-
Get elliptic curve key – getting the key from the Keychain given the key is used for testing.
Declaration
Swift
public func getSecKey(key: SecKey) -> SecKey? -
Get an elliptic curve key given the public key. IMPORTANT: If the key requires a biometric check for access, the system will prompt the user for FaceID/TouchID
Declaration
Swift
public func getEllipticCurveKey(publicKey: Data) -> ECKey?Parameters
publicKeyThe public key.
Return Value
An ECKey corresponding to the public key.
-
Get all elliptic curve keys with option to filter by tag. IMPORTANT: If any of the keys returned by the search query require a biometric check for access, the system will prompt the user for FaceID/TouchID
Throws
If there is an error in the key query.Declaration
Swift
public func getAllEllipticCurveKeys(tag: String? = nil, label: String? = nil) throws -> [ECKey]Parameters
tagThe tag to filter by (defaults to
nil).Return Value
An array of ECKeys.
-
Get all attributes for elliptic curve keys with option to filter by tag. IMPORTANT: If any of the keys returned by the search query require a biometric check for access, the system will prompt the user for FaceID/TouchID
Throws
If there is an error in the key query.Declaration
Swift
public func getAttributesForAllEllipticCurveKeys(tag: String? = nil, label: String? = nil, matchLimitAll: Bool = true) throws -> [[String : Any]]Parameters
tagThe tag to filter by (defaults to
nil).Return Value
An array of ECKeys.
-
Get an elliptic curve keys for the provided application label (for ec keys this is the sha1 hash of the public key) IMPORTANT: If the key requires a biometric check for access, the system will prompt the user for FaceID/TouchID
Throws
If there is a error getting the keyDeclaration
Swift
public func getEllipticCurveKey(applicationLabel: Data) throws -> ECKeyParameters
applicationLabelThe application label to search for
Return Value
An ECKey
-
Get an elliptic curve keys for the provided public key IMPORTANT: If the key requires a biometric check for access, the system will prompt the user for FaceID/TouchID
Throws
If there is a error getting the keyDeclaration
Swift
public func getEllipticCurveKey(publicKey: Data) throws -> ECKeyParameters
publicKeyThe publickey
Return Value
An ECKey
-
Get all elliptic curve private Sec Keys. For Secure Enclave private keys, the SecKey is a reference. It’s not posible to export the actual private key data.
Declaration
Swift
public func getAllEllipticCurvePrivateSecKeys(tag: String? = nil) -> [SecKey]?Parameters
tagThe tag to filter by (defaults to
nil).Return Value
An array of SecKeys.
-
Get all elliptic curve keys and return the public keys. IMPORTANT: If any of the keys returned by the search query require a biometric check for access, the system will prompt the user for FaceID/TouchID
Declaration
Swift
public func getAllEllipticCurvePublicSecKeys() -> [SecKey]?Return Value
An array of public SecKeys.
-
Get the private SecKey for the public key if the key exists in the Keychain. Public key data can be in either compressed or uncompressed format. IMPORTANT: If the key requires a biometric check for access, the system will prompt the user for FaceID/TouchID
Declaration
Swift
public func getPrivateSecKey(publicKey: Data) -> SecKey?Parameters
publicKeyA public key in either compressed or uncompressed format.
Return Value
A SecKey.
-
Create a NON-Secure-Enclave elliptic curve private key.
Declaration
Swift
public func createEllipticCurvePrivateKey(isPermanent: Bool = false) -> SecKey?Parameters
isPermanentIs the key stored permanently in the Keychain?
Return Value
A SecKey.
-
Declaration
Swift
public func importExternal(privateKey: Data, tag: String? = nil, label: String? = nil, // swiftlint:disable:this function_body_length protection: AccessibleProtection = .whenUnlockedThisDeviceOnly, accessFlag: SecAccessControlCreateFlags? = nil) throws -> ECKey -
Create a new Secure Enclave key.
Throws
If a key cannot be created.Declaration
Swift
public func createSecureEnclaveSecKey(tag: String? = nil, label: String? = nil, accessFlag: SecAccessControlCreateFlags? = nil) throws -> SecKeyParameters
tagA tag to associate with this key.
labelA label to associate with this key.
accessFlagaccessFlag for this key.
Return Value
A Secure Enclave SecKey.
-
Create a new elliptic curve key.
Throws
If a key cannot be created.Declaration
Swift
public func createEllipticCurveSecKey(secureEnclave: Bool, tag: String? = nil, label: String? = nil, protection: AccessibleProtection = .whenUnlockedThisDeviceOnly, accessFlag: SecAccessControlCreateFlags? = nil) throws -> SecKeyParameters
secureEnclaveGenerate this key in Secure Enclave?
tagA tag to associate with this key.
labelA label to associate with this key.
protectionAccessibility defaults to whenUnlockedThisDeviceOnly.
accessFlagThe accessFlag for this key.
Return Value
A SecKey.
-
Create a new elliptic curve key.
Throws
If a key cannot be created.Declaration
Swift
public func createEllipticCurveKey(secureEnclave: Bool, tag: String? = nil, label: String? = nil, protection: AccessibleProtection = .whenUnlockedThisDeviceOnly, accessFlag: SecAccessControlCreateFlags? = nil) throws -> ECKeyParameters
secureEnclaveGenerate this key in Secure Enclave?
tagA tag to associate with this key.
labelA label to associate with this key.
protectionAccessibility defaults to whenUnlockedThisDeviceOnly.
accessFlagThe accessFlag for this key.
Return Value
An ECKey.
-
Sign if the key is in the Keychain.
Throws
If private key is not available.Declaration
Swift
public func sign(publicKey: Data, data: Data) throws -> DataParameters
publicKeyThe public key corresponding to a private key to use for signing.
dataThe data to sign.
Return Value
A signature.
-
Sign with Secure Enclave or Keychain.
Throws
If an error is encountered attempting to sign.Declaration
Swift
public func sign(privateKey: SecKey, data: Data) throws -> DataParameters
privateKeyThe private key to use for signing.
dataThe data to sign.
Return Value
A signature.
-
Decrypt data using
SecKeyAlgorithm.eciesEncryptionCofactorVariableIVX963SHA256AESGCM.Throws
If the private key is not found or the message cannot be decrypted.Declaration
Swift
public func decrypt(publicKey: Data, message: Data) throws -> DataParameters
publicKeyThe public key corresponding to a private key to use for decrypting.
messageThe encrypted message.
Return Value
The decrypted message.
-
ECKey collects properties into a single object for an elliptic curve key.
See moreDeclaration
Swift
class ECKey
View on GitHub
Keychain Class Reference