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) {
73 case escrowKeyType.kOTEscrowKeySymmetric:
74 keyLength = OT_ESCROW_SYMMETRIC_HKDF_SIZE
76 let infoString = Array("Escrow Symmetric Key".utf8)
77 info = Data(bytes: infoString, count: infoString.count)
78 infoLength = info.count
81 case escrowKeyType.kOTEscrowKeyEncryption:
82 keyLength = OT_ESCROW_ENCRYPTION_HKDF_SIZE
84 let infoString = Array("Escrow Encryption Private Key".utf8)
85 info = Data(bytes: infoString, count: infoString.count)
86 infoLength = info.count
89 case escrowKeyType.kOTEscrowKeySigning:
90 keyLength = OT_ESCROW_SIGNING_HKDF_SIZE
92 let infoString = Array("Escrow Signing Private Key".utf8)
93 info = Data(bytes: infoString, count: infoString.count)
94 infoLength = info.count
99 guard let cp = ccec_cp_384() else {
100 throw EscrowKeysError.keyGeneration
102 var status: Int32 = 0
104 let fullKey = TPHObjectiveC.ccec384Context()
105 defer { TPHObjectiveC.contextFree(fullKey) }
107 derivedKey = Data(count: keyLength)
109 var masterSecretMutable = masterSecret
110 let masterSecretLength = masterSecret.count
111 let derivedKeySize = derivedKey.count
113 let bottleSaltData = Data(bytes: Array(bottleSalt.utf8), count: bottleSalt.utf8.count)
115 try derivedKey.withUnsafeMutableBytes { (derivedKeyBytes: UnsafeMutablePointer<UInt8>) throws ->Void in
116 try masterSecretMutable.withUnsafeMutableBytes { (masterSecretBytes: UnsafeMutablePointer<UInt8>) throws ->Void in
117 try bottleSaltData.withUnsafeBytes { (bottleSaltBytes: UnsafePointer<UInt8>) throws -> Void in
118 try info.withUnsafeBytes { (infoBytes: UnsafePointer<UInt8>) throws -> Void in
119 status = cchkdf(ccsha384_di(),
120 masterSecretLength, masterSecretBytes,
121 bottleSaltData.count, bottleSaltBytes,
122 infoLength, infoBytes,
123 keyLength, derivedKeyBytes)
125 throw EscrowKeysError.corecryptoKeyGeneration(corecryptoError: status)
128 if(keyType == escrowKeyType.kOTEscrowKeySymmetric) {
129 finalKey = Data(buffer: UnsafeBufferPointer(start: derivedKeyBytes, count: derivedKeySize))
131 } else if(keyType == escrowKeyType.kOTEscrowKeyEncryption || keyType == escrowKeyType.kOTEscrowKeySigning) {
132 status = ccec_generate_key_deterministic(cp,
133 derivedKeySize, derivedKeyBytes,
135 UInt32(CCEC_GENKEY_DETERMINISTIC_FIPS),
138 guard status == 0 else {
139 throw EscrowKeysError.corecryptoKeyGeneration(corecryptoError: status)
142 let space = ccec_x963_export_size(1, ccec_ctx_pub(fullKey))
143 var key = Data(count: space)
144 key.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) -> Void in
145 ccec_x963_export(1, bytes, fullKey)
156 class func createSecKey(keyData: Data) throws -> (SecKey) {
157 let keyAttributes = [kSecAttrKeyClass: kSecAttrKeyClassPrivate, kSecAttrKeyType: kSecAttrKeyTypeEC]
158 guard let key = SecKeyCreateWithData(keyData as CFData, keyAttributes as CFDictionary, nil) else {
159 throw EscrowKeysError.keyGeneration
165 class func setKeyMaterialInKeychain(query: Dictionary<CFString, Any>) throws -> (Bool) {
168 var results: CFTypeRef?
169 var status = SecItemAdd(query as CFDictionary, &results)
171 if status == errSecSuccess {
173 } else if status == errSecDuplicateItem {
174 var updateQuery: Dictionary<CFString, Any> = query
175 updateQuery[kSecClass] = nil
177 status = SecItemUpdate(query as CFDictionary, updateQuery as CFDictionary)
179 if status != errSecSuccess {
180 throw EscrowKeysError.failedToSaveToKeychain(errorCode: status)
185 throw EscrowKeysError.failedToSaveToKeychain(errorCode: status)
191 class func hashEscrowedSigningPublicKey(keyData: Data) throws -> (String) {
192 let di = ccsha384_di()
193 var result = Data(count: TPHObjectiveC.ccsha384_diSize())
195 let derivedKeySize = keyData.count
196 var keyDataMutable = keyData
197 result.withUnsafeMutableBytes {(resultBytes: UnsafeMutablePointer<UInt8>) -> Void in
198 keyDataMutable.withUnsafeMutableBytes {(keyDataBytes: UnsafeMutablePointer<UInt8>) -> Void in
199 ccdigest(di, derivedKeySize, keyDataBytes, resultBytes)
202 let hash = result.base64EncodedString(options: [])
207 class func storeEscrowedEncryptionKeyPair(keyData: Data, label: String) throws -> (Bool) {
209 let query: [CFString: Any] = [
210 kSecClass: kSecClassKey,
211 kSecAttrAccessible: kSecAttrAccessibleWhenUnlocked,
212 kSecUseDataProtectionKeychain: true,
213 kSecAttrAccessGroup: "com.apple.security.octagon",
214 kSecAttrSynchronizable: kCFBooleanFalse,
215 kSecAttrLabel: label,
216 kSecAttrApplicationLabel: String(format: "Escrowed Encryption Key-%@", NSUUID().uuidString),
217 kSecValueData: keyData,
219 return try EscrowKeys.setKeyMaterialInKeychain(query: query)
222 class func storeEscrowedSigningKeyPair(keyData: Data, label: String) throws -> (Bool) {
223 let query: [CFString: Any] = [
224 kSecClass: kSecClassKey,
225 kSecAttrAccessible: kSecAttrAccessibleWhenUnlocked,
226 kSecUseDataProtectionKeychain: true,
227 kSecAttrAccessGroup: "com.apple.security.octagon",
228 kSecAttrSynchronizable: kCFBooleanFalse,
229 kSecAttrApplicationLabel: String(format: "Escrowed Signing Key-%@", NSUUID().uuidString),
230 kSecAttrLabel: label,
231 kSecValueData: keyData,
233 return try EscrowKeys.setKeyMaterialInKeychain(query: query)
236 class func storeEscrowedSymmetricKey(keyData: Data, label: String) throws -> (Bool) {
237 let query: [CFString: Any] = [
238 kSecClass: kSecClassKey,
239 kSecAttrAccessible: kSecAttrAccessibleWhenUnlocked,
240 kSecUseDataProtectionKeychain: true,
241 kSecAttrAccessGroup: "com.apple.security.octagon",
242 kSecAttrSynchronizable: kCFBooleanFalse,
243 kSecAttrApplicationLabel: String(format: "Escrowed Symmetric Key-%@", NSUUID().uuidString),
244 kSecAttrLabel: label,
245 kSecValueData: keyData,
247 return try EscrowKeys.setKeyMaterialInKeychain(query: query)
250 class func retrieveEscrowKeysFromKeychain(label: String) throws -> [Dictionary <CFString, Any>]? {
251 var keySet: [Dictionary<CFString, Any>]?
253 let query: [CFString: Any] = [
254 kSecClass: kSecClassKey,
255 kSecAttrAccessGroup: "com.apple.security.octagon",
256 kSecAttrLabel: label,
257 kSecReturnAttributes: true,
258 kSecReturnData: true,
259 kSecAttrSynchronizable: kCFBooleanFalse,
260 kSecMatchLimit: kSecMatchLimitAll,
263 var result: CFTypeRef?
264 let status = SecItemCopyMatching(query as CFDictionary, &result)
266 if status != errSecSuccess || result == nil {
267 throw EscrowKeysError.itemDoesNotExist
271 if let dictionaryArray = result as? [Dictionary<CFString, Any>] {
272 keySet = dictionaryArray
274 if let dictionary = result as? Dictionary<CFString, Any> {
275 keySet = [dictionary]
284 class func findEscrowKeysForLabel(label: String) throws -> (_SFECKeyPair?, _SFECKeyPair?, _SFAESKey?) {
285 var signingKey: _SFECKeyPair?
286 var encryptionKey: _SFECKeyPair?
287 var symmetricKey: _SFAESKey?
289 let keySet = try retrieveEscrowKeysFromKeychain(label: label)
291 throw EscrowKeysError.itemDoesNotExist
293 for item in keySet! {
294 let keyTypeData = item[kSecAttrApplicationLabel as CFString] as! Data
295 let keyType = String(data: keyTypeData, encoding: .utf8)!
297 if keyType.range(of: "Symmetric") != nil {
298 let keyData = item[kSecValueData as CFString] as! Data
299 let specifier = _SFAESKeySpecifier.init(bitSize: TPHObjectiveC.aes256BitSize())
300 symmetricKey = try _SFAESKey.init(data: keyData, specifier: specifier)
301 } else if keyType.range(of: "Encryption") != nil {
302 let keyData = item[kSecValueData as CFString] as! Data
303 let encryptionSecKey = try EscrowKeys.createSecKey(keyData: keyData)
304 encryptionKey = _SFECKeyPair.init(secKey: encryptionSecKey)
305 } else if keyType.range(of: "Signing") != nil {
306 let keyData = item[kSecValueData as CFString] as! Data
307 let signingSecKey = try EscrowKeys.createSecKey(keyData: keyData)
308 signingKey = _SFECKeyPair.init(secKey: signingSecKey)
310 throw EscrowKeysError.unsupportedKeyType(keyType: keyType)
314 return (signingKey, encryptionKey, symmetricKey)
318 enum EscrowKeysError: Error {
320 case itemDoesNotExist
321 case failedToSaveToKeychain(errorCode: OSStatus)
322 case unsupportedKeyType(keyType: String)
323 case corecryptoKeyGeneration(corecryptoError: Int32)
326 extension EscrowKeysError: LocalizedError {
327 public var errorDescription: String? {
330 return "Key generation failed"
331 case .itemDoesNotExist:
332 return "Item does not exist"
333 case .failedToSaveToKeychain(errorCode: let osError):
334 return "Failed to save item to keychain: \(osError)"
335 case .unsupportedKeyType(keyType: let keyType):
336 return "Unsupported Key Type \(keyType)"
337 case .corecryptoKeyGeneration(corecryptoError: let corecryptoError):
338 return "Key generation error \(corecryptoError)"
343 extension EscrowKeysError: CustomNSError {
345 public static var errorDomain: String {
346 return "com.apple.security.trustedpeers.EscrowKeys"
349 public var errorCode: Int {
353 case .itemDoesNotExist:
355 case .failedToSaveToKeychain:
357 case .unsupportedKeyType:
359 case .corecryptoKeyGeneration:
364 public var errorUserInfo: [String: Any] {
365 var userInfo: [String: Any] = [:]
366 if let desc = self.errorDescription {
367 userInfo[NSLocalizedDescriptionKey] = desc
370 case .failedToSaveToKeychain(errorCode: let osError):
371 userInfo[NSUnderlyingErrorKey] = NSError.init(domain: NSOSStatusErrorDomain, code: Int(osError), userInfo: nil)
372 case .corecryptoKeyGeneration(corecryptoError: let corecryptoError):
373 userInfo[NSUnderlyingErrorKey] = NSError.init(domain: "corecrypto", code: Int(corecryptoError), userInfo: nil)