2 * Copyright (c) 2019 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 import SecurityFoundation
27 let OT_ESCROW_SIGNING_HKDF_SIZE = 56
28 let OT_ESCROW_ENCRYPTION_HKDF_SIZE = 56
29 let OT_ESCROW_SYMMETRIC_HKDF_SIZE = 32
31 enum EscrowKeyType: Int {
32 case kOTEscrowKeySigning = 1
33 case kOTEscrowKeyEncryption = 2
34 case kOTEscrowKeySymmetric = 3
37 class EscrowKeys: NSObject {
38 public var encryptionKey: _SFECKeyPair
39 public var signingKey: _SFECKeyPair
40 public var symmetricKey: _SFAESKey
42 public var secret: Data
43 public var bottleSalt: String
45 public init (secret: Data, bottleSalt: String) throws {
47 self.bottleSalt = bottleSalt
49 let encryptionKeyData = try EscrowKeys.generateEscrowKey(keyType: EscrowKeyType.kOTEscrowKeyEncryption, masterSecret: secret, bottleSalt: bottleSalt)
50 self.encryptionKey = _SFECKeyPair.init(secKey: try EscrowKeys.createSecKey(keyData: encryptionKeyData))
52 let signingKeyData = try EscrowKeys.generateEscrowKey(keyType: EscrowKeyType.kOTEscrowKeySigning, masterSecret: secret, bottleSalt: bottleSalt)
53 self.signingKey = _SFECKeyPair.init(secKey: try EscrowKeys.createSecKey(keyData: signingKeyData))
55 let symmetricKeyData = try EscrowKeys.generateEscrowKey(keyType: EscrowKeyType.kOTEscrowKeySymmetric, masterSecret: secret, bottleSalt: bottleSalt)
56 let specifier = _SFAESKeySpecifier.init(bitSize: TPHObjectiveC.aes256BitSize())
57 self.symmetricKey = try _SFAESKey.init(data: symmetricKeyData, specifier: specifier)
59 let escrowSigningPubKeyHash = try EscrowKeys.hashEscrowedSigningPublicKey(keyData: self.signingKey.publicKey().spki())
60 _ = try EscrowKeys.storeEscrowedSigningKeyPair(keyData: self.signingKey.keyData, label: escrowSigningPubKeyHash)
61 _ = try EscrowKeys.storeEscrowedEncryptionKeyPair(keyData: self.encryptionKey.keyData, label: escrowSigningPubKeyHash)
62 _ = try EscrowKeys.storeEscrowedSymmetricKey(keyData: self.symmetricKey.keyData, label: escrowSigningPubKeyHash)
65 class func generateEscrowKey(keyType: EscrowKeyType, masterSecret: Data, bottleSalt: String) throws -> (Data) {
72 case EscrowKeyType.kOTEscrowKeySymmetric:
73 keyLength = OT_ESCROW_SYMMETRIC_HKDF_SIZE
75 let infoString = Array("Escrow Symmetric Key".utf8)
76 info = Data(bytes: infoString, count: infoString.count)
78 case EscrowKeyType.kOTEscrowKeyEncryption:
79 keyLength = OT_ESCROW_ENCRYPTION_HKDF_SIZE
81 let infoString = Array("Escrow Encryption Private Key".utf8)
82 info = Data(bytes: infoString, count: infoString.count)
84 case EscrowKeyType.kOTEscrowKeySigning:
85 keyLength = OT_ESCROW_SIGNING_HKDF_SIZE
87 let infoString = Array("Escrow Signing Private Key".utf8)
88 info = Data(bytes: infoString, count: infoString.count)
92 guard let cp = ccec_cp_384() else {
93 throw EscrowKeysError.keyGeneration
97 let fullKey = TPHObjectiveC.ccec384Context()
98 defer { TPHObjectiveC.contextFree(fullKey) }
100 derivedKey = Data(count: keyLength)
102 var masterSecretMutable = masterSecret
104 let bottleSaltData = Data(bytes: Array(bottleSalt.utf8), count: bottleSalt.utf8.count)
106 try derivedKey.withUnsafeMutableBytes { (derivedKeyBytes: UnsafeMutableRawBufferPointer) throws -> Void in
107 try masterSecretMutable.withUnsafeMutableBytes { (masterSecretBytes: UnsafeMutableRawBufferPointer) throws -> Void in
108 try bottleSaltData.withUnsafeBytes { (bottleSaltBytes: UnsafeRawBufferPointer) throws -> Void in
109 try info.withUnsafeBytes { (infoBytes: UnsafeRawBufferPointer) throws -> Void in
110 status = cchkdf(ccsha384_di(),
111 masterSecretBytes.count, masterSecretBytes.baseAddress!,
112 bottleSaltBytes.count, bottleSaltBytes.baseAddress!,
113 infoBytes.count, infoBytes.baseAddress!,
114 derivedKeyBytes.count, derivedKeyBytes.baseAddress!)
116 throw EscrowKeysError.corecryptoKeyGeneration(corecryptoError: status)
119 if keyType == EscrowKeyType.kOTEscrowKeySymmetric {
120 finalKey = Data(buffer: derivedKeyBytes.bindMemory(to: UInt8.self))
122 } else if keyType == EscrowKeyType.kOTEscrowKeyEncryption || keyType == EscrowKeyType.kOTEscrowKeySigning {
123 status = ccec_generate_key_deterministic(cp,
124 derivedKeyBytes.count, derivedKeyBytes.bindMemory(to: UInt8.self).baseAddress!,
126 UInt32(CCEC_GENKEY_DETERMINISTIC_FIPS),
129 guard status == 0 else {
130 throw EscrowKeysError.corecryptoKeyGeneration(corecryptoError: status)
133 let space = ccec_x963_export_size(1, ccec_ctx_pub(fullKey))
134 var key = Data(count: space)
135 key.withUnsafeMutableBytes { (bytes: UnsafeMutableRawBufferPointer) -> Void in
136 ccec_x963_export(1, bytes.baseAddress!, fullKey)
147 class func createSecKey(keyData: Data) throws -> (SecKey) {
148 let keyAttributes = [kSecAttrKeyClass: kSecAttrKeyClassPrivate, kSecAttrKeyType: kSecAttrKeyTypeEC]
149 guard let key = SecKeyCreateWithData(keyData as CFData, keyAttributes as CFDictionary, nil) else {
150 throw EscrowKeysError.keyGeneration
156 class func setKeyMaterialInKeychain(query: [CFString: Any]) throws -> (Bool) {
159 var results: CFTypeRef?
160 var status = SecItemAdd(query as CFDictionary, &results)
162 if status == errSecSuccess {
164 } else if status == errSecDuplicateItem {
165 var updateQuery: [CFString: Any] = query
166 updateQuery[kSecClass] = nil
168 status = SecItemUpdate(query as CFDictionary, updateQuery as CFDictionary)
170 if status != errSecSuccess {
171 throw EscrowKeysError.failedToSaveToKeychain(errorCode: status)
176 throw EscrowKeysError.failedToSaveToKeychain(errorCode: status)
182 class func hashEscrowedSigningPublicKey(keyData: Data) throws -> (String) {
183 let di = ccsha384_di()
184 var result = Data(count: TPHObjectiveC.ccsha384_diSize())
186 var keyDataMutable = keyData
187 result.withUnsafeMutableBytes {(resultBytes: UnsafeMutableRawBufferPointer) -> Void in
188 keyDataMutable.withUnsafeMutableBytes {(keyDataBytes: UnsafeMutableRawBufferPointer) -> Void in
189 ccdigest(di, keyDataBytes.count, keyDataBytes.baseAddress!, resultBytes.baseAddress!)
192 let hash = result.base64EncodedString(options: [])
197 class func storeEscrowedEncryptionKeyPair(keyData: Data, label: String) throws -> (Bool) {
199 let query: [CFString: Any] = [
200 kSecClass: kSecClassKey,
201 kSecAttrAccessible: kSecAttrAccessibleWhenUnlocked,
202 kSecUseDataProtectionKeychain: true,
203 kSecAttrAccessGroup: "com.apple.security.octagon",
204 kSecAttrSynchronizable: false,
205 kSecAttrLabel: label,
206 kSecAttrApplicationLabel: String(format: "Escrowed Encryption Key-%@", NSUUID().uuidString),
207 kSecValueData: keyData,
209 return try EscrowKeys.setKeyMaterialInKeychain(query: query)
212 class func storeEscrowedSigningKeyPair(keyData: Data, label: String) throws -> (Bool) {
213 let query: [CFString: Any] = [
214 kSecClass: kSecClassKey,
215 kSecAttrAccessible: kSecAttrAccessibleWhenUnlocked,
216 kSecUseDataProtectionKeychain: true,
217 kSecAttrAccessGroup: "com.apple.security.octagon",
218 kSecAttrSynchronizable: false,
219 kSecAttrApplicationLabel: String(format: "Escrowed Signing Key-%@", NSUUID().uuidString),
220 kSecAttrLabel: label,
221 kSecValueData: keyData,
223 return try EscrowKeys.setKeyMaterialInKeychain(query: query)
226 class func storeEscrowedSymmetricKey(keyData: Data, label: String) throws -> (Bool) {
227 let query: [CFString: Any] = [
228 kSecClass: kSecClassKey,
229 kSecAttrAccessible: kSecAttrAccessibleWhenUnlocked,
230 kSecUseDataProtectionKeychain: true,
231 kSecAttrAccessGroup: "com.apple.security.octagon",
232 kSecAttrSynchronizable: false,
233 kSecAttrApplicationLabel: String(format: "Escrowed Symmetric Key-%@", NSUUID().uuidString),
234 kSecAttrLabel: label,
235 kSecValueData: keyData,
237 return try EscrowKeys.setKeyMaterialInKeychain(query: query)
240 class func retrieveEscrowKeysFromKeychain(label: String) throws -> [ [CFString: Any]]? {
241 var keySet: [[CFString: Any]]?
243 let query: [CFString: Any] = [
244 kSecClass: kSecClassKey,
245 kSecAttrAccessGroup: "com.apple.security.octagon",
246 kSecAttrLabel: label,
247 kSecReturnAttributes: true,
248 kSecReturnData: true,
249 kSecAttrSynchronizable: false,
250 kSecMatchLimit: kSecMatchLimitAll,
253 var result: CFTypeRef?
254 let status = SecItemCopyMatching(query as CFDictionary, &result)
256 if status != errSecSuccess || result == nil {
257 throw EscrowKeysError.itemDoesNotExist
261 if let dictionaryArray = result as? [[CFString: Any]] {
262 keySet = dictionaryArray
264 if let dictionary = result as? [CFString: Any] {
265 keySet = [dictionary]
274 class func findEscrowKeysForLabel(label: String) throws -> (_SFECKeyPair?, _SFECKeyPair?, _SFAESKey?) {
275 var signingKey: _SFECKeyPair?
276 var encryptionKey: _SFECKeyPair?
277 var symmetricKey: _SFAESKey?
279 let keySet = try retrieveEscrowKeysFromKeychain(label: label)
281 throw EscrowKeysError.itemDoesNotExist
283 for item in keySet! {
284 let keyTypeData = item[kSecAttrApplicationLabel as CFString] as! Data
285 let keyType = String(data: keyTypeData, encoding: .utf8)!
287 if keyType.range(of: "Symmetric") != nil {
288 let keyData = item[kSecValueData as CFString] as! Data
289 let specifier = _SFAESKeySpecifier.init(bitSize: TPHObjectiveC.aes256BitSize())
290 symmetricKey = try _SFAESKey.init(data: keyData, specifier: specifier)
291 } else if keyType.range(of: "Encryption") != nil {
292 let keyData = item[kSecValueData as CFString] as! Data
293 let encryptionSecKey = try EscrowKeys.createSecKey(keyData: keyData)
294 encryptionKey = _SFECKeyPair.init(secKey: encryptionSecKey)
295 } else if keyType.range(of: "Signing") != nil {
296 let keyData = item[kSecValueData as CFString] as! Data
297 let signingSecKey = try EscrowKeys.createSecKey(keyData: keyData)
298 signingKey = _SFECKeyPair.init(secKey: signingSecKey)
300 throw EscrowKeysError.unsupportedKeyType(keyType: keyType)
304 return (signingKey, encryptionKey, symmetricKey)
308 enum EscrowKeysError: Error {
310 case itemDoesNotExist
311 case failedToSaveToKeychain(errorCode: OSStatus)
312 case unsupportedKeyType(keyType: String)
313 case corecryptoKeyGeneration(corecryptoError: Int32)
316 extension EscrowKeysError: LocalizedError {
317 public var errorDescription: String? {
320 return "Key generation failed"
321 case .itemDoesNotExist:
322 return "Item does not exist"
323 case .failedToSaveToKeychain(errorCode: let osError):
324 return "Failed to save item to keychain: \(osError)"
325 case .unsupportedKeyType(keyType: let keyType):
326 return "Unsupported Key Type \(keyType)"
327 case .corecryptoKeyGeneration(corecryptoError: let corecryptoError):
328 return "Key generation error \(corecryptoError)"
333 extension EscrowKeysError: CustomNSError {
335 public static var errorDomain: String {
336 return "com.apple.security.trustedpeers.EscrowKeys"
339 public var errorCode: Int {
343 case .itemDoesNotExist:
345 case .failedToSaveToKeychain:
347 case .unsupportedKeyType:
349 case .corecryptoKeyGeneration:
354 public var errorUserInfo: [String: Any] {
355 var userInfo: [String: Any] = [:]
356 if let desc = self.errorDescription {
357 userInfo[NSLocalizedDescriptionKey] = desc
360 case .failedToSaveToKeychain(errorCode: let osError):
361 userInfo[NSUnderlyingErrorKey] = NSError(domain: NSOSStatusErrorDomain, code: Int(osError), userInfo: nil)
362 case .corecryptoKeyGeneration(corecryptoError: let corecryptoError):
363 userInfo[NSUnderlyingErrorKey] = NSError(domain: "corecrypto", code: Int(corecryptoError), userInfo: nil)