Skip to content

Commit 2e964c8

Browse files
authored
fix(PlayKeychain): a crash when storing dictionary (#196)
* fix(PlayKeychain): a crash when storing dictionary --------- Co-authored-by: King <[email protected]>
1 parent ed9e95d commit 2e964c8

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

PlayTools/MysticRunes/PlayedApple.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,25 @@ public class PlayKeychain: NSObject {
4949
}
5050

5151
if attributes["class"] as? String == "keys" {
52-
// kSecAttrKeyType is stored as `type` in the dictionary
53-
// kSecAttrKeyClass is stored as `kcls` in the dictionary
54-
let keyAttributes = [
55-
kSecAttrKeyType: attributes["type"] as! CFString, // swiftlint:disable:this force_cast
56-
kSecAttrKeyClass: attributes["kcls"] as! CFString // swiftlint:disable:this force_cast
57-
]
58-
let keyData = vData as! Data // swiftlint:disable:this force_cast
59-
let key = SecKeyCreateWithData(keyData as CFData, keyAttributes as CFDictionary, nil)
60-
result?.pointee = Unmanaged.passRetained(key!)
61-
return errSecSuccess
52+
if let keyDataValue = vData as? Data {
53+
// kSecAttrKeyType is stored as `type` in the dictionary
54+
// kSecAttrKeyClass is stored as `kcls` in the dictionary
55+
let keyAttributesDictionary = [
56+
kSecAttrKeyType: attributes["type"] as? String
57+
?? (attributes[kSecAttrKeyType as String] as? String),
58+
kSecAttrKeyClass: attributes["kcls"] as? String
59+
?? (attributes[kSecAttrKeyType as String] as? String)
60+
]
61+
if let key = SecKeyCreateWithData(
62+
keyDataValue as CFData,
63+
keyAttributesDictionary as CFDictionary,
64+
nil
65+
) {
66+
result?.pointee = Unmanaged.passRetained(key)
67+
return errSecSuccess
68+
}
69+
}
70+
return errSecBadReq
6271
}
6372
result?.pointee = Unmanaged.passRetained(vData)
6473
return errSecSuccess

0 commit comments

Comments
 (0)