@@ -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