btnguyen2k / swiftutils

Utility library in Swift
MIT License
72 stars 22 forks source link

Nil Value Returned as SecKey while trying to upload RSA Private Key #11

Open vdharmarajan opened 4 years ago

vdharmarajan commented 4 years ago

Thank you for sharing your work.

FYI, I have been trying to load external generated key (public or private) into iOS keychain without any success for the past several weeks. I came across your work.

I was trying to load a private key generated in another server into iOS. I get a Nil value. I have been debugging the code with "Print" commands throughout AddRSAPrivateKey functions, both public and private. The function executes all the way to the end, but while returning, I get a Nil.

The Nil comes from

@discardableResult private static func addRSAPrivateKey(_ privkey: Data, tagName: String) throws -> SecKey? { } function.

Background: After several failed attempts to load iOS generated public key to our remote Windows server, the Windows admin generated a key pair in server using puTTYYGen. He added the public key for my SSH user name and sent the key pair to me to add to iOS keychain. I exported the private key in Windows with puTTYgen, covered to base64 encoding and used it in your RSAUtils as a key string with a tag.

I came across several posts about how tricky it is to load an external key into keychain. If I am not mistaken, your code tries to deal with key header issues very similar to Objective C code done at http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios/.

Your help is greatly appreciated.

Thanks

btnguyen2k commented 4 years ago

Hi @vdharmarajan, Putty keys need to be converted to OpenSSH format first. I dont have Windows but google says this

Open your private key in PuTTYGen Top menu “Conversions”->”Export OpenSSH key”. Save the new OpenSSH key when prompted."

Cheers, Thanh Nguyen