Skip to content

Commit a9f3034

Browse files
committed
Update Keychain.swift
1 parent ece768c commit a9f3034

File tree

1 file changed

+16
-59
lines changed

1 file changed

+16
-59
lines changed

Sources/Keychain/Keychain.swift

+16-59
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import CryptoKit
33
import Foundation
44

55

6-
class Keychain
6+
public class Keychain
77
{
8-
func retrieveOrGeneratePrivateKey(label: String, tag: String) -> P256.KeyAgreement.PrivateKey?
8+
public func retrieveOrGeneratePrivateKey(label: String) -> P256.KeyAgreement.PrivateKey?
99
{
1010
// Do we already have a key?
11-
let searchQuery = generateKeySearchQuery(label: label, tag: tag)
12-
if let key = retrievePrivateKey(query: searchQuery)
11+
if let key = retrievePrivateKey(label: label)
1312
{
1413
return key
1514
}
@@ -28,7 +27,7 @@ class Keychain
2827
return privateKey
2928
}
3029

31-
func generateAndSavePrivateKey(label: String) -> P256.KeyAgreement.PrivateKey?
30+
public func generateAndSavePrivateKey(label: String) -> P256.KeyAgreement.PrivateKey?
3231
{
3332
let privateKey = P256.KeyAgreement.PrivateKey()
3433

@@ -43,7 +42,7 @@ class Keychain
4342
return privateKey
4443
}
4544

46-
func storePrivateKey(_ key: P256.KeyAgreement.PrivateKey, label: String) -> Bool
45+
public func storePrivateKey(_ key: P256.KeyAgreement.PrivateKey, label: String) -> Bool
4746
{
4847
let attributes = [kSecAttrKeyType: kSecAttrKeyTypeECSECPrimeRandom,
4948
kSecAttrKeyClass: kSecAttrKeyClassPrivate] as [String: Any]
@@ -87,8 +86,10 @@ class Keychain
8786
}
8887
}
8988

90-
func retrievePrivateKey(query: CFDictionary) -> P256.KeyAgreement.PrivateKey?
89+
public func retrievePrivateKey(label: String) -> P256.KeyAgreement.PrivateKey?
9190
{
91+
let query: CFDictionary = generateKeySearchQuery(label: label)
92+
9293
// Find and cast the result as a SecKey instance.
9394
var item: CFTypeRef?
9495
var secKey: SecKey
@@ -120,11 +121,11 @@ class Keychain
120121
}
121122
}
122123

123-
func generateKeySearchQuery(label: String, tag: String) -> CFDictionary
124+
public func generateKeySearchQuery(label: String) -> CFDictionary
124125
{
125126
let query: [String: Any] = [kSecClass as String: kSecClassKey,
126127
kSecAttrApplicationLabel as String: label,
127-
kSecAttrApplicationTag as String: tag,
128+
//kSecAttrApplicationTag as String: tag,
128129
kSecMatchLimit as String: kSecMatchLimitOne,
129130
kSecReturnRef as String: true,
130131
kSecReturnAttributes as String: false,
@@ -133,64 +134,20 @@ class Keychain
133134
return query as CFDictionary
134135
}
135136

136-
// func generateKeyAttributesDictionary(tag: String) -> CFDictionary
137-
// {
138-
// //FIXME: Secure Enclave
139-
// // let access = SecAccessControlCreateWithFlags(kCFAllocatorDefault, kSecAttrAccessibleAlwaysThisDeviceOnly, .privateKeyUsage, nil)!
140-
//
141-
// let privateKeyAttributes: [String: Any] = [
142-
// kSecAttrIsPermanent as String: true,
143-
// kSecAttrApplicationTag as String: tag
144-
// //kSecAttrAccessControl as String: access
145-
// ]
146-
//
147-
// let publicKeyAttributes: [String: Any] = [
148-
// kSecAttrIsPermanent as String: true,
149-
// kSecAttrApplicationTag as String: tag
150-
// ]
151-
//
152-
// let attributes: [String: Any] = [
153-
// kSecClass as String: kSecClassKey,
154-
// kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
155-
// kSecAttrKeySizeInBits as String: 256,
156-
// //kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave,
157-
// kSecPrivateKeyAttrs as String: privateKeyAttributes,
158-
// kSecPublicKeyAttrs as String: publicKeyAttributes
159-
// ]
160-
//
161-
// return attributes as CFDictionary
162-
// }
163-
164-
public func deriveSymmetricKey(receiverPublicKey: P256.KeyAgreement.PublicKey, senderPrivateKey:P256.KeyAgreement.PrivateKey) -> SymmetricKey?
165-
{
166-
do
167-
{
168-
let sharedSecret = try senderPrivateKey.sharedSecretFromKeyAgreement(with: receiverPublicKey)
169-
let symmetricKey = sharedSecret.x963DerivedSymmetricKey(using: SHA256.self, sharedInfo: Data(), outputByteCount: 32)
170-
171-
return symmetricKey
172-
}
173-
catch let sharedSecretError
174-
{
175-
print("Unable to encrypt payload. Failed to generate a shared secret: \(sharedSecretError)")
176-
return nil
177-
}
178-
}
179-
180-
func deleteKeys(tag: String)
137+
public func deleteKey(label: String)
181138
{
182-
print("\nAttempted to delete key from secure enclave.")
139+
print("\nAttempted to delete key.")
183140
//Remove client keys from secure enclave
184-
let query: [String: Any] = [kSecClass as String: kSecClassKey,
185-
kSecAttrApplicationTag as String: tag]
141+
//let query: [String: Any] = [kSecClass as String: kSecClassKey, kSecAttrApplicationTag as String: tag]
142+
let query = generateKeySearchQuery(label: label)
186143
let deleteStatus = SecItemDelete(query as CFDictionary)
187144

188145
switch deleteStatus
189146
{
190147
case errSecItemNotFound:
191-
print("Could not find a client key to delete.\n")
148+
print("Could not find a key to delete.\n")
192149
case noErr:
193-
print("Deleted client keys.\n")
150+
print("Deleted a key.\n")
194151
default:
195152
print("Unexpected status: \(deleteStatus.description)\n")
196153
}

0 commit comments

Comments
 (0)