@@ -3,13 +3,12 @@ import CryptoKit
3
3
import Foundation
4
4
5
5
6
- class Keychain
6
+ public class Keychain
7
7
{
8
- func retrieveOrGeneratePrivateKey( label: String , tag : String ) -> P256 . KeyAgreement . PrivateKey ?
8
+ public func retrieveOrGeneratePrivateKey( label: String ) -> P256 . KeyAgreement . PrivateKey ?
9
9
{
10
10
// 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)
13
12
{
14
13
return key
15
14
}
@@ -28,7 +27,7 @@ class Keychain
28
27
return privateKey
29
28
}
30
29
31
- func generateAndSavePrivateKey( label: String ) -> P256 . KeyAgreement . PrivateKey ?
30
+ public func generateAndSavePrivateKey( label: String ) -> P256 . KeyAgreement . PrivateKey ?
32
31
{
33
32
let privateKey = P256 . KeyAgreement. PrivateKey ( )
34
33
@@ -43,7 +42,7 @@ class Keychain
43
42
return privateKey
44
43
}
45
44
46
- func storePrivateKey( _ key: P256 . KeyAgreement . PrivateKey , label: String ) -> Bool
45
+ public func storePrivateKey( _ key: P256 . KeyAgreement . PrivateKey , label: String ) -> Bool
47
46
{
48
47
let attributes = [ kSecAttrKeyType: kSecAttrKeyTypeECSECPrimeRandom,
49
48
kSecAttrKeyClass: kSecAttrKeyClassPrivate] as [ String : Any ]
@@ -87,8 +86,10 @@ class Keychain
87
86
}
88
87
}
89
88
90
- func retrievePrivateKey( query : CFDictionary ) -> P256 . KeyAgreement . PrivateKey ?
89
+ public func retrievePrivateKey( label : String ) -> P256 . KeyAgreement . PrivateKey ?
91
90
{
91
+ let query : CFDictionary = generateKeySearchQuery ( label: label)
92
+
92
93
// Find and cast the result as a SecKey instance.
93
94
var item : CFTypeRef ?
94
95
var secKey : SecKey
@@ -120,11 +121,11 @@ class Keychain
120
121
}
121
122
}
122
123
123
- func generateKeySearchQuery( label: String , tag : String ) -> CFDictionary
124
+ public func generateKeySearchQuery( label: String ) -> CFDictionary
124
125
{
125
126
let query : [ String : Any ] = [ kSecClass as String : kSecClassKey,
126
127
kSecAttrApplicationLabel as String : label,
127
- kSecAttrApplicationTag as String : tag,
128
+ // kSecAttrApplicationTag as String: tag,
128
129
kSecMatchLimit as String : kSecMatchLimitOne,
129
130
kSecReturnRef as String : true ,
130
131
kSecReturnAttributes as String : false ,
@@ -133,64 +134,20 @@ class Keychain
133
134
return query as CFDictionary
134
135
}
135
136
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 )
181
138
{
182
- print ( " \n Attempted to delete key from secure enclave . " )
139
+ print ( " \n Attempted to delete key. " )
183
140
//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 )
186
143
let deleteStatus = SecItemDelete ( query as CFDictionary )
187
144
188
145
switch deleteStatus
189
146
{
190
147
case errSecItemNotFound:
191
- print ( " Could not find a client key to delete. \n " )
148
+ print ( " Could not find a key to delete. \n " )
192
149
case noErr:
193
- print ( " Deleted client keys . \n " )
150
+ print ( " Deleted a key . \n " )
194
151
default :
195
152
print ( " Unexpected status: \( deleteStatus. description) \n " )
196
153
}
0 commit comments