0

So this is my first question on Stack Overflow and it's about keychain so, I read apple documentation about keychain and they mention that the key can be generated or obtain by other means "which is my case" then I created "query dictionary" like they said ,but when I tried to add it I get an error and my status is equal to -50 I don't know what does than mean also the reason I am trying to store my key is so I can use it with "SecKeyDecrypt" to decrypt messages which require SecKey as parameter

UPDATE: I found that what the return code means -50 errSecParam which mean "One or more parameters passed to the function are not valid." then I try to remove my kSecValueRef as String: privateKey and it works but still, I want that key to be stored ??

 let privateKey = "myKey"

    let tag = "mybunlde.com".data(using: .utf8)!

        let addPrivateKey: [String: Any] = [kSecClass as String: kSecClassKey,
                                            kSecAttrApplicationTag as String: tag,
                                            kSecValueRef as String: privateKey]
        let status = SecItemAdd(addPrivateKey as CFDictionary, nil)
        guard status == errSecSuccess else { print("error while creating the key")
            return
        }

        let getPrivateKey: [String: Any] = [kSecClass as String: kSecClassKey,
                                            kSecAttrApplicationTag as String: tag,
                                            kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
                                            kSecReturnRef as String: true]

        print("getPrivateKey \(getPrivateKey)")
    } else {
        print("no key found ")
    }
AtulParmar
  • 4,358
  • 1
  • 24
  • 45
NSD
  • 89
  • 6

2 Answers2

0

https://developer.apple.com/documentation/security/certificate_key_and_trust_services/keys/storing_keys_in_the_keychain?language=objc

https://apple.stackexchange.com/questions/8993/how-can-i-add-a-private-key-to-my-keychain Adding private key into iOS Keychain

use above link.....

  • this is for object C and I am using swift? and I have read an article smaller to it https://developer.apple.com/documentation/security/certificate_key_and_trust_services/keys/storing_keys_in_the_keychain – NSD May 22 '19 at 10:27
0

you can use this lib for store & retrieve data from keychain https://github.com/jrendel/SwiftKeychainWrapper

e.g

let save: Bool = KeychainWrapper.standard.set("hello keychain", forKey: "key_name")
let get: String? = KeychainWrapper.standard.string(forKey: "key_name")
Marwen Doukh
  • 1,946
  • 17
  • 26
Samir Shaikh
  • 547
  • 3
  • 9
  • SecKeyDecrypt requires parameter of try secKey and the lib return it as string? – NSD May 22 '19 at 13:01