private key creation from data failed

Hi,


I got a Service Account private key from Google (Something like "MIIEvAIBADAN....uWqbWl871RnFOg==". In fact the key is larger than this. It is 1649 characters long.

I try to get a SecKey from it but i always get nil.

Here is the code from a playground:


let privateKey = "MIIEvAIBADANBgkqhki.......s6ipqUCFro0XLj/5q5Oh+1C9drU\ncz+fZlCRuWqbWl871RnFOg==" // Get a Private key for a service account from Google

let privateKeyData = privateKey.data(using: String.Encoding.utf8)

let base64privateKeyData = privateKeyData?.base64EncodedData()


let keyDict:[NSObject:NSObject] = [

kSecAttrKeyType: kSecAttrKeyTypeRSA,

kSecAttrKeyClass: kSecAttrKeyClassPrivate,

kSecAttrKeySizeInBits: NSNumber(value: 4096),

kSecReturnPersistentRef: true as NSObject

]


let cfData = base64privateKeyData! as CFData

var error: Unmanaged<CFError>?

let publicKey = SecKeyCreateWithData(cfData, keyDict as CFDictionary, &error)

NSLog(error.debugDescription)




In Xcode 9.0 i can't get the error.debugDescription, but in previous version it contains a code 50 error


Is there a problem with the SecKeyCreateWithData function? or am i making some mistake?


Thanks,


Fernando

It’s hard to say what’s going on here without seeing the full key. My recommendation:

  1. Use

    base64
    to decode your key into binary form.
  2. Dump the key using dumpasn1.

  3. Post that dump so we can see the structure

IMPORTANT As this is a private key, make sure to redact the bulk of the key bits.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
private key creation from data failed
 
 
Q